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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use super::Paint;
use crate::{
Context, ImageDecoder, ImageError, ImageHandle, ImageLimits, ImageMetrics, ImageSource,
ImageState, ImageTiming, Rect, Vec2,
};
use std::{sync::Arc, time::Duration};
impl Context {
/// Observe a source without starting work. A not-yet-requested source is Loading.
pub fn image_state(&self, source: impl Into<ImageSource>) -> ImageState {
self.images.lock().state(source.into())
}
/// Retain a context-local handle for explicit replacement/reload/free operations.
/// Loading begins when displayed; this does not synchronously read a file.
pub fn load_image(
&mut self,
source: impl Into<ImageSource>,
) -> Result<ImageHandle, ImageError> {
let handle = self.images.lock().resolve(source.into())?;
self.images.lock().pin(handle);
Ok(handle)
}
/// Replace source data. Completion of an older generation is discarded.
pub fn update_image(
&mut self,
handle: ImageHandle,
source: impl Into<ImageSource>,
) -> Result<(), ImageError> {
self.images.lock().reload(handle, Some(source.into()))?;
self.request_repaint();
Ok(())
}
/// Explicit retry/re-read; files are never watched or read again automatically.
pub fn reload_image(&mut self, handle: ImageHandle) -> Result<(), ImageError> {
self.images.lock().reload(handle, None)?;
self.request_repaint();
Ok(())
}
pub fn invalidate_image(&mut self, source: impl Into<ImageSource>) -> Result<(), ImageError> {
self.images.lock().invalidate(source.into())?;
self.request_repaint();
Ok(())
}
/// Invalidate this handle. Pending completions cannot recreate it.
pub fn release_image(&mut self, handle: ImageHandle) {
self.images.lock().release(handle);
self.request_repaint();
}
/// Drop decoded/raster CPU residency; handles retain recoverable source data.
pub fn clear_image_cache(&mut self) {
self.images.lock().clear_decoded();
self.request_repaint();
}
pub fn image_limits(&self) -> ImageLimits {
self.images.lock().limits.clone()
}
pub fn set_image_limits(&mut self, limits: ImageLimits) {
self.images.lock().limits = limits;
self.clear_image_cache();
}
pub fn add_image_decoder(&mut self, decoder: Arc<dyn ImageDecoder>) {
self.images.lock().add_decoder(decoder);
}
/// Custom hosts must install a callback which wakes their sleeping event loop.
/// Called from workers: enqueue an event, never lock/call the Context here.
/// The desktop runner installs its EventLoopProxy automatically.
pub fn set_image_waker(&mut self, waker: impl Fn() + Send + Sync + 'static) {
self.images.lock().set_waker(Some(Arc::new(waker)));
}
/// Decode images on the thread that draws, spending at most `frame_budget` per frame
/// (one image always runs, so a single large one can exceed it), instead of on worker
/// threads. This is the only mode on `wasm32`, where it is the default. `None` returns
/// to worker threads on targets that have them.
pub fn decode_images_inline(&mut self, frame_budget: Option<Duration>) {
self.images.lock().set_inline_decoding(frame_budget);
}
pub fn clear_image_waker(&mut self) {
self.images.lock().set_waker(None);
}
pub fn image_metrics(&self) -> ImageMetrics {
self.images.lock().metrics()
}
/// Bounded diagnostic history; drain outside measured UI work in benchmarks.
pub fn take_image_timings(&mut self) -> Vec<ImageTiming> {
self.images.lock().take_timings()
}
/// Request the images of one element at the size they are displayed at, visuals
/// included. Returns the scale the element is tessellated at: an element with images
/// is cached per displayed scale.
pub(super) fn request_painted_images(&mut self, paint: &[Paint], clip: Rect) -> f32 {
let mut images = false;
for primitive in paint {
if let Paint::Material {
rect,
image: Some(image),
..
} = primitive
{
images = true;
if !rect.intersect(clip).is_empty() {
self.images.lock().request(
image.handle,
rect.size() / image.crop.size().max(Vec2::splat(0.001))
* self.scale
* self.paint_state.image_visual_scale(),
image.texture,
);
}
}
if let Paint::Image {
rect,
uv,
handle,
texture,
..
} = primitive
{
images = true;
if !rect.intersect(clip).is_empty() {
self.images.lock().request(
*handle,
rect.size() / uv.size().max(Vec2::splat(0.001))
* self.scale
* self.paint_state.image_visual_scale(),
*texture,
);
}
}
}
if images {
self.scale * self.paint_state.image_visual_scale()
} else {
self.scale
}
}
/// End of pass: images that settle later schedule their pass, finished loads repaint.
pub(super) fn finish_images(&mut self) {
let settle = self.images.lock().finish_frame();
if let Some(deadline) = settle {
self.request_repaint_after(deadline.saturating_duration_since(self.frame_time));
}
if self.images.lock().take_state_changed() {
self.request_repaint();
}
}
}