rhwp 0.9.1

HWP/HWPX document engine for Rust applications, WebAssembly, and CLI automation
Documentation
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! Shared owner-thread GPU tile planning, LRU pins and target reservations.
//! Backend surface creation, submission and uncertain-completion retention stay
//! with each presenter; this module never recycles an externally published target.

use std::cell::RefCell;
use std::rc::Rc;

#[path = "gpu_tiles/cache_trim.rs"]
pub(crate) mod cache_trim;

pub(crate) fn trim_receipt(
    before: usize,
    after: usize,
    target: usize,
    reason: &str,
) -> serde_json::Value {
    serde_json::json!({"ok":true,"before":before,"after":after,
        "trimmed":before.saturating_sub(after),"targetBytes":target,
        "busy":after > target || !reason.is_empty(),"reason":reason,
        "scope":"managedGpuTargets","wholeFrameReadbacks":0})
}

use serde::Serialize;
use skia_safe::Surface;

#[cfg(any(
    all(feature = "native-skia-metal", target_os = "macos"),
    all(feature = "native-skia-d3d", target_os = "windows"),
))]
#[path = "gpu_tiles/frame_resources.rs"]
pub(crate) mod frame_resources;

use super::super::paint::{LayerOutputOptions, RenderProfile};
use super::presentation::NativePresentationStamp;
use super::render_snapshot::NativeRenderSnapshot;
use super::NativeHostError;

#[derive(Debug, Clone, Copy, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NativeGpuTargetStats {
    pub targets: usize,
    pub estimated_bytes: usize,
    pub peak_targets: usize,
    pub peak_estimated_bytes: usize,
    pub rejected_admissions: u64,
}

struct TargetState {
    max_targets: usize,
    max_bytes: usize,
    usage: NativeGpuTargetStats,
}

#[derive(Clone)]
pub(crate) struct TargetBudget(Rc<RefCell<TargetState>>);

impl TargetBudget {
    pub fn new(max_targets: usize, max_bytes: usize) -> Self {
        Self(Rc::new(RefCell::new(TargetState {
            max_targets,
            max_bytes,
            usage: NativeGpuTargetStats::default(),
        })))
    }

    pub fn stats(&self) -> NativeGpuTargetStats {
        self.0.borrow().usage
    }

    pub fn can_reserve(&self, count: usize, bytes: usize) -> bool {
        let state = self.0.borrow();
        state
            .usage
            .targets
            .checked_add(count)
            .is_some_and(|total| total <= state.max_targets)
            && state
                .usage
                .estimated_bytes
                .checked_add(bytes)
                .is_some_and(|total| total <= state.max_bytes)
    }

    pub fn reserve(&self, bytes: usize) -> Result<TargetAllocation, NativeHostError> {
        if !self.can_reserve(1, bytes) {
            let mut state = self.0.borrow_mut();
            state.usage.rejected_admissions = state.usage.rejected_admissions.saturating_add(1);
            return Err(NativeHostError::LimitExceeded(
                "GPU managed target quota exceeded".into(),
            ));
        }
        let mut state = self.0.borrow_mut();
        state.usage.targets += 1;
        state.usage.estimated_bytes += bytes;
        state.usage.peak_targets = state.usage.peak_targets.max(state.usage.targets);
        state.usage.peak_estimated_bytes = state
            .usage
            .peak_estimated_bytes
            .max(state.usage.estimated_bytes);
        Ok(TargetAllocation {
            budget: self.clone(),
            bytes,
        })
    }
}

pub(crate) struct TargetAllocation {
    budget: TargetBudget,
    bytes: usize,
}

impl Drop for TargetAllocation {
    fn drop(&mut self) {
        // A failed borrow leaves the credits retained and the owner fails
        // closed; never free a credit while another operation mutates it.
        if let Ok(mut state) = self.budget.0.try_borrow_mut() {
            state.usage.targets -= 1;
            state.usage.estimated_bytes -= self.bytes;
        }
    }
}

pub(crate) struct ManagedTarget {
    // Surface drops before its reservation, including through an Rc tile pin.
    pub surface: Surface,
    pub width: i32,
    pub height: i32,
    allocation: TargetAllocation,
}

impl ManagedTarget {
    pub fn new(surface: Surface, width: i32, height: i32, allocation: TargetAllocation) -> Self {
        Self {
            surface,
            width,
            height,
            allocation,
        }
    }

    pub fn bytes(&self) -> usize {
        self.allocation.bytes
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct TileRegion {
    pub x: i32,
    pub y: i32,
    pub width: i32,
    pub height: i32,
}

impl TileRegion {
    pub fn bytes(self) -> usize {
        self.width as usize * self.height as usize * 4
    }
}

#[derive(Clone, PartialEq, Eq)]
pub(crate) struct TileKey {
    pub region: TileRegion,
    context_id: u64,
    device_generation: u64,
    device_scale_bits: u64,
    document_id: u64,
    revision: u64,
    font_generation: u64,
    font_fingerprint: String,
    page: u32,
    profile: RenderProfile,
    page_width_bits: u64,
    page_height_bits: u64,
    output_options: LayerOutputOptions,
    raster_text_geometry: bool,
    raster_clip_coverage: bool,
    scale_bits: u64,
    phase_x_bits: u32,
    phase_y_bits: u32,
    background_color: u32,
}

/// Canonical staging and retained-tile limits. A backend may apply a tighter
/// total target budget; admission then falls back to its original viewport path.
#[derive(Clone, Copy)]
pub(crate) struct TileConfig {
    pub max_dimension: i32,
    pub max_pixels: usize,
    pub max_target_bytes: usize,
    pub tile_size: usize,
    pub max_cached_tiles: usize,
    pub max_tile_bytes: usize,
}

#[derive(Clone, Copy)]
pub(crate) struct TileTarget {
    pub context_id: u64,
    pub width: i32,
    pub height: i32,
}

#[derive(Clone, Copy)]
pub(crate) struct TilePlacement {
    pub scale: f64,
    pub origin_x: f64,
    pub origin_y: f64,
    pub background_color: Option<u32>,
}

pub(crate) struct TilePlan {
    pub width: i32,
    pub height: i32,
    pub canonical_bytes: usize,
    pub integer_x: i32,
    pub integer_y: i32,
    pub phase_x: f64,
    pub phase_y: f64,
    pub keys: Vec<TileKey>,
}

impl TilePlan {
    pub fn new(
        config: TileConfig,
        stamp: NativePresentationStamp,
        target: TileTarget,
        placement: TilePlacement,
        snapshot: &NativeRenderSnapshot,
        clear_color: u32,
    ) -> Option<Self> {
        // COLORREF Some is opaque RGB in the existing strict API. None and a
        // partial-alpha viewport clear retain the original P90 replay path.
        let background_color = placement.background_color?;
        if config.max_cached_tiles == 0
            || config.max_tile_bytes == 0
            || ![128, 256, 512, 1024].contains(&config.tile_size)
            || target.context_id == 0
            || target.width <= 0
            || target.height <= 0
            || clear_color >> 24 != 255
        {
            return None;
        }
        // Use the exact f32 placement passed to Skia, without phase rounding.
        let origin_x = f64::from(placement.origin_x as f32);
        let origin_y = f64::from(placement.origin_y as f32);
        let integer_x = origin_x.floor() as i32;
        let integer_y = origin_y.floor() as i32;
        let phase_x = origin_x - f64::from(integer_x);
        let phase_y = origin_y - f64::from(integer_y);
        let scale = placement.scale as f32;
        let options = snapshot.tree().output_options;
        let width = f64::from(snapshot.tree().page_width as f32 * scale + phase_x as f32).ceil();
        let height = f64::from(snapshot.tree().page_height as f32 * scale + phase_y as f32).ceil();
        if width <= 0.0
            || height <= 0.0
            || width > f64::from(config.max_dimension)
            || height > f64::from(config.max_dimension)
        {
            return None;
        }
        let (width, height) = (width as i32, height as i32);
        let canonical_bytes = (width as usize)
            .checked_mul(height as usize)
            .filter(|pixels| *pixels <= config.max_pixels)?
            .checked_mul(4)
            .filter(|bytes| *bytes <= config.max_target_bytes)?;
        let left = (-i64::from(integer_x)).clamp(0, i64::from(width)) as i32;
        let top = (-i64::from(integer_y)).clamp(0, i64::from(height)) as i32;
        let right =
            (i64::from(target.width) - i64::from(integer_x)).clamp(0, i64::from(width)) as i32;
        let bottom =
            (i64::from(target.height) - i64::from(integer_y)).clamp(0, i64::from(height)) as i32;
        if left >= right || top >= bottom {
            return None;
        }
        let side = config.tile_size as i32;
        let mut keys = Vec::new();
        let mut tile_bytes = 0usize;
        for y in ((top / side * side)..bottom).step_by(config.tile_size) {
            for x in ((left / side * side)..right).step_by(config.tile_size) {
                let region = TileRegion {
                    x,
                    y,
                    width: side.min(width - x),
                    height: side.min(height - y),
                };
                tile_bytes = tile_bytes.checked_add(region.bytes())?;
                if keys.len() >= config.max_cached_tiles || tile_bytes > config.max_tile_bytes {
                    return None;
                }
                keys.push(TileKey {
                    region,
                    context_id: target.context_id,
                    device_generation: stamp.device_generation,
                    device_scale_bits: stamp.device_scale.to_bits(),
                    document_id: stamp.document_id,
                    revision: stamp.revision,
                    font_generation: stamp.font_generation,
                    font_fingerprint: snapshot.font_state().fingerprint.clone(),
                    page: snapshot.page(),
                    profile: snapshot.profile(),
                    page_width_bits: snapshot.tree().page_width.to_bits(),
                    page_height_bits: snapshot.tree().page_height.to_bits(),
                    output_options: options,
                    raster_text_geometry: snapshot.tree().preserves_raster_text_geometry(),
                    raster_clip_coverage: snapshot.tree().uses_raster_clip_coverage(),
                    scale_bits: placement.scale.to_bits(),
                    phase_x_bits: (phase_x as f32).to_bits(),
                    phase_y_bits: (phase_y as f32).to_bits(),
                    background_color,
                });
            }
        }
        Some(Self {
            width,
            height,
            canonical_bytes,
            integer_x,
            integer_y,
            phase_x,
            phase_y,
            keys,
        })
    }
}

pub(crate) struct CachedTile {
    pub key: TileKey,
    pub target: Rc<RefCell<ManagedTarget>>,
}

#[derive(Debug, Clone, Copy, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NativeGpuTileStats {
    pub cached_tiles: usize,
    pub cached_bytes: usize,
    pub hits: u64,
    pub misses: u64,
    pub evictions: u64,
    pub evicted_bytes: u64,
    pub canonical_replays: u64,
    pub canonical_failures: u64,
    pub viewport_fallbacks: u64,
    pub pinned_admission_fallbacks: u64,
    pub target_admission_fallbacks: u64,
}

#[derive(Default)]
pub(crate) struct TileCache {
    entries: Vec<Rc<CachedTile>>,
    pub observations: NativeGpuTileStats,
}

impl TileCache {
    pub fn stats(&self) -> NativeGpuTileStats {
        NativeGpuTileStats {
            cached_tiles: self.entries.len(),
            cached_bytes: self
                .entries
                .iter()
                .map(|tile| tile.key.region.bytes())
                .sum(),
            ..self.observations
        }
    }

    pub fn get(&mut self, key: &TileKey) -> Option<Rc<CachedTile>> {
        let Some(index) = self.entries.iter().position(|entry| &entry.key == key) else {
            self.observations.misses = self.observations.misses.saturating_add(1);
            return None;
        };
        self.observations.hits = self.observations.hits.saturating_add(1);
        let tile = self.entries.remove(index);
        let result = Rc::clone(&tile);
        self.entries.push(tile);
        Some(result)
    }

    pub fn insert(&mut self, tile: Rc<CachedTile>) {
        self.entries.push(tile);
    }

    pub fn has_room(&self, max_tiles: usize, max_bytes: usize, count: usize, bytes: usize) -> bool {
        let stats = self.stats();
        stats
            .cached_tiles
            .checked_add(count)
            .is_some_and(|total| total <= max_tiles)
            && stats
                .cached_bytes
                .checked_add(bytes)
                .is_some_and(|total| total <= max_bytes)
    }

    /// Only unpinned owner-thread entries may be destroyed after the backend
    /// has drained or retained all work that references them.
    pub fn evict_idle(&mut self) -> bool {
        let Some(entry) = cache_trim::remove_oldest_idle(&mut self.entries) else {
            return false;
        };
        self.observations.evictions = self.observations.evictions.saturating_add(1);
        self.observations.evicted_bytes = self
            .observations
            .evicted_bytes
            .saturating_add(entry.key.region.bytes() as u64);
        drop(entry);
        true
    }

    pub fn clear(&mut self) {
        self.observations.evictions = self
            .observations
            .evictions
            .saturating_add(self.entries.len() as u64);
        self.observations.evicted_bytes = self
            .observations
            .evicted_bytes
            .saturating_add(self.stats().cached_bytes as u64);
        self.entries.clear();
    }
}