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
//! Borrowed host targets, including GPU-backed Skia surfaces.
use skia_safe::{AutoCanvasRestore, Canvas, Matrix, Rect, Surface};
use super::renderer::colorref_to_skia;
use super::SkiaLayerRenderer;
use crate::error::HwpError;
use crate::model::ColorRef;
use crate::paint::{validate_text_variant_scope, PageLayerTree};
use crate::renderer::layer_renderer::LayerRenderResult;
/// Placement of a page on a host-owned Skia canvas.
///
/// Placement composes with the canvas's existing affine transform and clip.
/// `origin_x`/`origin_y` are in its current local units, before `scale` maps
/// page CSS pixels to those units. The page rectangle is always clipped even
/// when the tree disables its internal body/cell clips.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SkiaSurfaceRenderOptions {
pub scale: f64,
pub origin_x: f64,
pub origin_y: f64,
/// Optional opaque COLORREF page background. `None` preserves the target.
pub background_color: Option<ColorRef>,
}
impl Default for SkiaSurfaceRenderOptions {
fn default() -> Self {
Self {
scale: 1.0,
origin_x: 0.0,
origin_y: 0.0,
background_color: None,
}
}
}
impl SkiaLayerRenderer {
/// Replay directly into a host-owned surface, with strict resource errors.
///
/// This delegates to [`Self::render_to_canvas_strict`]. It neither allocates
/// a destination surface nor reads back its pixels, flushes a GPU context,
/// submits work, or presents a frame. The host owns those operations and
/// must keep the surface's context alive and usable on the calling thread.
pub fn render_to_surface_strict(
&self,
surface: &mut Surface,
tree: &PageLayerTree,
options: SkiaSurfaceRenderOptions,
) -> LayerRenderResult<()> {
self.render_to_canvas_strict(surface.canvas(), tree, options)
}
/// Replay a page into a borrowed canvas without a final CPU RGBA readback.
///
/// The existing affine transform and clip are respected. Matrix, clip, and
/// save count are restored on success and error. Geometry, transform, and
/// text-contract errors are rejected before any target pixels are changed.
/// Perspective, singular, non-finite, and unrepresentable transforms fail.
/// The largest device scale controls resolution-dependent SVG fallbacks.
///
/// This uses the same ordered paint replay as CPU raster/PDF, with the PDF
/// path's strict resource policy: missing/undecodable images and failed SVG
/// raster fallbacks return an error instead of the CPU raster placeholder.
/// Replay is **not atomic**: a late resource error can leave earlier paint
/// and the requested background in the target. Hosts requiring atomic
/// frames should render into their own offscreen surface before publishing.
///
/// GPU-backed surfaces can consume this directly when the host enables a
/// compatible skia-safe backend. Some individual resources still use CPU
/// raster fallbacks; this is not a promise that every paint operation runs
/// on a GPU. Context creation, synchronization, and presentation stay with
/// the host. No backend feature is enabled implicitly.
pub fn render_to_canvas_strict(
&self,
canvas: &Canvas,
tree: &PageLayerTree,
options: SkiaSurfaceRenderOptions,
) -> LayerRenderResult<()> {
let invalid = || HwpError::RenderError("invalid Skia host surface geometry".into());
let scale = options.scale as f32;
let origin_x = options.origin_x as f32;
let origin_y = options.origin_y as f32;
let width = tree.page_width as f32;
let height = tree.page_height as f32;
if [
options.scale,
options.origin_x,
options.origin_y,
tree.page_width,
tree.page_height,
]
.into_iter()
.any(|value| !value.is_finite())
|| [scale, origin_x, origin_y, width, height]
.into_iter()
.any(|value| !value.is_finite())
|| scale <= 0.0
|| width <= 0.0
|| height <= 0.0
{
return Err(invalid());
}
let page = Rect::from_wh(width, height);
let placement = Matrix::scale_translate((scale, scale), (origin_x, origin_y));
let device = Matrix::concat(&canvas.local_to_device_as_3x3(), &placement);
let fallback_raster_scale = device.max_scale();
let (device_page, _) = device.map_rect(page);
if !device.is_finite()
|| device.has_perspective()
|| !device.min_scale().is_finite()
|| device.min_scale() <= 0.0
|| !fallback_raster_scale.is_finite()
|| fallback_raster_scale <= 0.0
|| !device_page.is_finite()
|| device_page.is_empty()
{
return Err(invalid());
}
// Validate before an optional clear. The shared replay validates again
// for its internal callers, which do not all use this host boundary.
validate_text_variant_scope(tree).map_err(|error| {
HwpError::RenderError(format!("invalid PageLayerTree text contract: {error}"))
})?;
let _restore = AutoCanvasRestore::guard(canvas, true);
canvas.concat(&placement);
canvas.clip_rect(page, None, false);
if let Some(color) = options.background_color {
// clear ignores the CTM but respects both the host and page clips.
canvas.clear(colorref_to_skia(color, 1.0));
}
self.render_page_to_canvas_strict(canvas, tree, fallback_raster_scale)
}
}