1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//! The interface drawn into a block of pixels in memory, for whatever shows it: a renderer (as
//! a texture for Direct3D, OpenGL, Vulkan, Metal, a web canvas), a system with no window here
//! yet, or a test that compares pixels.
//!
//! ```ignore
//! let changed = ui.run(&mut describe);
//! surface.redraw(&mut ui, changed); // only what changed is drawn again
//! // then: copy the pixels inside `changed` to the texture, and draw the texture
//! ```
//!
//! A pixel is `0xAARRGGBB`, premultiplied (in memory: blue, green, red, alpha). Where nothing
//! is drawn on a page of [`Color::NONE`](crate::Color::NONE), pixels stay clear, so the picture
//! can lie over a game or anything else.
use crate::canvas::Canvas;
use crate::geom::Rect;
use crate::ui::Ui;
/// An interface drawn into a block of pixels with no window: each pixel `0xAARRGGBB`,
/// premultiplied. `Surface::default()` has no pixels; `update` draws it whole the first time.
#[doc(alias("offscreen", "render_target"))]
#[derive(Default)]
pub struct Surface {
/// Width in pixels.
pub width: usize,
/// Height in pixels.
pub height: usize,
/// Rows of pixels, top first.
pub pixels: Vec<u32>,
}
impl std::fmt::Debug for Surface {
/// Its size; not its pixels.
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Surface").field("width", &self.width).field("height", &self.height).finish_non_exhaustive()
}
}
impl Surface {
/// The whole interface as last laid out, drawn afresh `strip` rows at a time, as a window
/// draws through one small block.
pub fn draw(ui: &mut Ui, strip: usize) -> Surface {
let (scale, strip) = (ui.scale(), strip.max(1));
let (width, height) = ((ui.size().w * scale).round() as usize, (ui.size().h * scale).round() as usize);
let mut pixels = vec![0u32; width * height];
let mut block = vec![0u32; width * strip];
for top in (0..height).step_by(strip) {
let rows = strip.min(height - top);
block.fill(0);
ui.paint(&mut Canvas::new(&mut block[..width * rows], width, rows, (0, top as i32), scale));
pixels[top * width..(top + rows) * width].copy_from_slice(&block[..width * rows]);
}
Surface { width, height, pixels }
}
/// Redraws the part of the interface in `area` and leaves the rest: what a window does with
/// the rectangle a run gives back. Within it only the changed places are drawn, when there
/// are several far apart. If the interface changed size, all of it is drawn.
pub fn redraw(&mut self, ui: &mut Ui, area: Rect) {
let scale = ui.scale();
if (self.width, self.height) != ((ui.size().w * scale).round() as usize, (ui.size().h * scale).round() as usize) {
*self = Surface::draw(ui, 64);
return;
}
// The waiting places are what `area` stands for when it is the rectangle around them;
// once all of them lie in what is drawn here, nothing waits.
let places = if area == ui.around { ui.places.len().max(1) } else { 1 };
for place in 0..places {
let part = ui.places.get(place).copied().filter(|_| places > 1).unwrap_or(area);
let mut canvas = Canvas::new(&mut self.pixels, self.width, self.height, (0, 0), scale);
canvas.clip(part.on_pixels(scale));
if ui.theme.bg.a() < 255 {
canvas.erase(); // a see-through page must be cleared first
}
ui.paint(&mut canvas);
}
if area.intersect(ui.around) == ui.around || ui.around.is_empty() {
ui.changes();
}
}
/// Brings the surface up to date after a run: redraws what changed since the last call and
/// gives back where, in pixels (left, top, right, bottom), one rectangle per place: what a
/// renderer copies to its texture. If the interface changed size, all of it is drawn and
/// given back.
pub fn update(&mut self, ui: &mut Ui) -> Vec<[usize; 4]> {
let scale = ui.scale();
// Copied as a slice, not collected: collecting would add a second user of the windows'
// own way of taking changes, and change what every program carries.
let places = ui.changes().as_slice().to_vec();
if (self.width, self.height) != ((ui.size().w * scale).round() as usize, (ui.size().h * scale).round() as usize) {
*self = Surface::draw(ui, 64);
return [[0, 0, self.width, self.height]].into_iter().filter(|_| !self.pixels.is_empty()).collect();
}
let edge = |at: f32, most: usize| ((at * scale).round().max(0.0) as usize).min(most);
let mut changed = Vec::new();
for place in places {
self.redraw(ui, place);
let place = place.on_pixels(scale);
let [left, top, right, bottom] = [edge(place.x, self.width), edge(place.y, self.height), edge(place.right(), self.width), edge(place.bottom(), self.height)];
changed.extend((left < right && top < bottom).then_some([left, top, right, bottom]));
}
changed
}
/// The picture as a PNG file; empty for a surface never drawn.
pub fn png(&self) -> Vec<u8> {
crate::png::encode(self.width, &self.pixels).unwrap_or_default()
}
}