frust_engine/compile/mod.rs
1//! Scene compilation: a `frust_scene::Scene` becomes sparse strips plus draws.
2//!
3//! [`SceneCompiler`] is a stateless walk over an already-recorded display list.
4//! Unlike an immediate-mode scene recorder, there is no render state to save
5//! and restore and no transform stack to unwind — `frust_scene::SceneBuilder`
6//! has already composed every command's transform, so each command carries the
7//! only transform it needs and the walk composes it with the frame's root once.
8//!
9//! The compiler owns the retained scratch a
10//! [`StripGenerator`] needs (line buffer, tiles, flatten/stroke context) so a
11//! steady-state frame reuses those allocations, and the two pieces of state
12//! that genuinely span frames — the [`ImageResidency`] that keeps an image's
13//! atlas rectangle alive for as long as the scene keeps drawing it, and the
14//! [`GlyphPrepCache`] that keeps a glyph's fetched outline and its font's
15//! hinting instance alive on the same terms. Everything else is per-frame:
16//! [`compile`](SceneCompiler::compile) returns what a frame produced in one
17//! [`CompiledFrame`] and keeps nothing of it.
18//!
19//! Compiled here: the geometry primitives — axis-aligned and rounded
20//! rectangles, lines, arbitrary filled/stroked paths — the clip bracket around
21//! them, which lowers to a scissor rectangle or a coverage mask and never to an
22//! intermediate texture (see [`clip`]), the opacity-layer and retained brackets
23//! that group them (see [`layers`]), the hole punch that erases what they
24//! painted (see [`clear`]), images, whose destination rectangle is
25//! rasterized like any other fill and painted by an atlas-backed image paint
26//! (see [`paint`] and [`crate::cache::images`]), and blurred rounded
27//! rectangles, whose padded bounding rectangle is rasterized the same way and
28//! painted by a gaussian-falloff paint the fragment shader evaluates per pixel
29//! (see [`blur_rrect`]), and glyph runs, which take one of two routes decided
30//! per run by [`crate::text::atlas_policy`] before the walk begins: a settled
31//! run is resolved through the glyph atlas and each glyph drawn as one image
32//! paint over its slot, while an animating, oversized or refused one has its
33//! outlines fetched and scaled by `glifo` and rasterized as any other filled
34//! path would be, painted by the run's own brush (see [`crate::text`]).
35//! Shader quads are recognised
36//! and skipped — the engine grows them in a later pass, and skipping is the
37//! conservative behaviour (a frame draws less, never wrong). A backdrop blur
38//! is a layer of its own holding one draw — its rectangle's coverage under
39//! whatever clip is open, painted by nothing the compiler resolves — listed in
40//! [`CompiledFrame::backdrops`] for the scheduler and the renderer to cut the
41//! frame at and to paint from the blurred page (see
42//! [`crate::filters`]' *The backdrop blur*).
43
44pub mod blur_rrect;
45
46pub mod clear;
47
48pub mod clip;
49
50pub(crate) mod cull;
51
52pub mod layers;
53
54pub mod paint;
55
56pub mod draw;
57
58pub mod external;
59
60pub(crate) mod parallel;
61
62pub use clear::ClearPunch;
63pub use clip::ClipStack;
64pub use draw::{DepthCounter, EngineDraw};
65pub use external::{ExternalExtents, ExternalSkip};
66pub use layers::{
67 GroupStack, LayerLowering, RetainedBracket, RetainedKind, RetainedPlacement, RetainedStack,
68};
69pub use parallel::{DEFAULT_PARALLEL_MIN_COST, DEFAULT_PARALLEL_WORKERS, MAX_PARALLEL_WORKERS};
70
71/// The most glyph runs a compiler's resident-run cache remembers at once (see
72/// [`SceneCompiler::resident_glyph_run_count`]). A byte budget bounds the same
73/// records alongside it, so a scene of very long runs is held to fewer.
74pub const MAX_RESIDENT_GLYPH_RUNS: usize = crate::text::atlas_policy::MAX_RESIDENT_RUNS;
75
76use std::sync::Once;
77use std::time::Duration;
78
79use kurbo::{Affine, BezPath, Cap, Join, PathEl, Rect, RoundedRectRadii, Shape, Stroke};
80use peniko::{Brush, Color, Fill, ImageData};
81
82use frust_gpu::{SceneTextureId, TierCaps};
83use frust_scene::{Command, CornerRadii, DashPattern, GlyphRun, PathStyle, Scene};
84
85use glifo::{GlyphAtlas, GlyphPrepCache, PendingClearRect};
86
87use vello_common::encode::EncodedPaint;
88use vello_common::fearless_simd::Level;
89use vello_common::geometry::RectU16;
90use vello_common::paint::ImageId;
91use vello_common::record::CommandRecorder;
92use vello_common::strip_generator::{GenerationMode, StripGenerator, StripStorage};
93use vello_common::tile::Tile;
94use vello_common::util::is_axis_aligned;
95
96use crate::cache::images::{
97 AtlasBudget, AtlasRegion, ImageResidency, ImageSkip, ImageUpload, is_mobile_tier,
98};
99use crate::compile::blur_rrect::encode_blurred_rounded_rect;
100use crate::compile::clear::StagedPunch;
101use crate::compile::external::encode_scene_texture;
102use crate::compile::paint::{LutRequest, encode_brush, encode_image_brush, encode_image_command};
103use crate::compile::parallel::{Claim, Geometry, Merge, Pool};
104use crate::config;
105use crate::error::EngineError;
106use crate::filters::{Backdrop, MAX_BACKDROPS_PER_FRAME, backdrop_region, backdrop_sigma};
107use crate::text::{
108 AtlasPolicy, FontFacts, GlyphQuadScratch, GlyphQuadTargets, GlyphRunTargets, RunKey, RunRoute,
109 context_paint, glyph_atlas_policy, lower_glyph_run,
110};
111
112/// Curve-flattening tolerance, in device pixels.
113///
114/// The value `vello_hybrid`'s own scene recorder flattens at; keeping it
115/// identical is what lets the two rasterizers be compared strip-for-strip.
116pub(crate) const FLATTEN_TOLERANCE: f64 = 0.1;
117
118/// Raised the first time an image is refused residency, so a scene that draws
119/// an unsupported image says so at least once at warning level without the
120/// per-frame repetition a per-skip warning would produce.
121static IMAGE_SKIP_WARNING: Once = Once::new();
122
123/// Raised the first time a glyph run is refused for an unreadable font, on the
124/// same once-per-process terms as [`IMAGE_SKIP_WARNING`].
125static FONT_SKIP_WARNING: Once = Once::new();
126
127/// Raised the first time the compiler drops a [`Command::ShaderQuad`] whose
128/// program has no rendered target, on the same once-per-process terms as
129/// [`IMAGE_SKIP_WARNING`].
130static SHADER_QUAD_SKIP_WARNING: Once = Once::new();
131
132/// Raised the first time a [`Command::ShaderQuad`] is dropped because the
133/// shader-effect kill switch is set, on the same once-per-process terms as
134/// [`IMAGE_SKIP_WARNING`]. Separate from [`SHADER_QUAD_SKIP_WARNING`] because
135/// it reports a deliberate configuration rather than a missing pre-pass, and
136/// conflating the two would tell an operator who set the switch that something
137/// went wrong.
138static SHADER_EFFECTS_DISABLED_WARNING: Once = Once::new();
139
140/// A stopwatch for the CPU phases one frame's encode splits into, compiled
141/// away entirely without `perf-trace`.
142///
143/// The engine's own CPU profile is measured by lapping this once per phase
144/// rather than by sampling: a phase is tens to hundreds of microseconds and no
145/// sampling profiler rides along on a phone under a benchmark harness, while a
146/// lap is two clock reads. Under `perf-trace` a lap reads
147/// [`std::time::Instant`]; without it the type is zero-sized, [`Self::lap`]
148/// answers [`Duration::ZERO`] and no clock is read at all — the "zero clock
149/// reads in a disabled build" terms `docs/RENDER_DEVELOPMENT.md`'s perf-trace
150/// convention and `docs/PERFORMANCE_BASELINES.md`'s Release-lean note set for an
151/// FFI-sensitive path, met at compile time rather than by a runtime branch.
152///
153/// Laps are cumulative by construction: each one both reports the span since
154/// the previous lap and opens the next, so a phase can never be double-counted
155/// or silently skipped the way two independent `Instant` pairs could.
156#[derive(Debug, Clone, Copy)]
157pub(crate) struct PhaseClock {
158 /// When the phase now being timed began.
159 #[cfg(feature = "perf-trace")]
160 last: std::time::Instant,
161}
162
163impl PhaseClock {
164 /// Opens the first phase at "now".
165 #[must_use]
166 pub(crate) fn start() -> Self {
167 Self {
168 #[cfg(feature = "perf-trace")]
169 last: std::time::Instant::now(),
170 }
171 }
172
173 /// Closes the phase in flight, answering what it cost, and opens the next.
174 #[must_use]
175 pub(crate) fn lap(&mut self) -> Duration {
176 #[cfg(feature = "perf-trace")]
177 {
178 let now = std::time::Instant::now();
179 // Saturating rather than `-`: a clock that went backwards across a
180 // lap is a measurement artefact, and a zero span reports it far
181 // better than a panicked frame would (E17).
182 let span = now.saturating_duration_since(self.last);
183 self.last = now;
184 span
185 }
186 #[cfg(not(feature = "perf-trace"))]
187 Duration::ZERO
188 }
189}
190
191/// What one [`SceneCompiler::compile`] call spent, phase by phase.
192///
193/// Zero across the board in a build without `perf-trace` — see [`PhaseClock`].
194/// The phases partition the call in the order they run, so their sum is the
195/// whole compile minus call overhead. Two edges are worth naming rather than
196/// leaving to be inferred: the frame record and its depth counter are built
197/// after the `admit` lap, so `walk` spans their construction as well as the
198/// command walk itself; and the `frust-perf img` line a `perf-trace` build
199/// emits is written *after* the last lap, so no phase is charged the cost of
200/// reporting on one.
201///
202/// 1. [`validate`](Self::validate) — the up-front finiteness and geometry
203/// sweep over every command. A frame is refused whole or not at all, so
204/// this sweep runs before the walk records anything and is paid on every
205/// frame in the command count.
206/// 2. [`prepare`](Self::prepare) — resetting the per-frame scratch (strip
207/// generator, clip/group/retained stacks, punches) and ageing the two
208/// caches that span frames (image residency, `glifo`'s prep cache).
209/// 3. [`classify`](Self::classify) — routing every glyph run through
210/// [`crate::text::atlas_policy`] before any of them is drawn.
211/// 4. [`admit`](Self::admit) — closing the glyph atlas's own frame, which is
212/// where admission packs what the routing pass asked for.
213/// 5. [`walk`](Self::walk) — building the frame record the walk fills, then
214/// the command walk itself: strip generation and paint encoding, and on a
215/// text-heavy scene the bulk of the call. The part of it spent inside glyph
216/// runs is reported separately by [`glyphs`](Self::glyphs), which is a
217/// subset of this rather than a phase of its own. It also carries the
218/// parallel walk's overhead: the planning pass that picks the shape
219/// commands helper threads generate, spawning the helpers and joining them
220/// (see the `parallel` module).
221/// 6. [`finish`](Self::finish) — closing open groups, generating the hole
222/// punches, ageing the glyph atlas and taking the frame's image plan.
223///
224/// Observational only: nothing downstream branches on any of it.
225#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
226pub struct CompileSpans {
227 /// The up-front finiteness and geometry sweep over every command.
228 pub validate: Duration,
229 /// Per-frame scratch resets and the two cross-frame caches' ageing.
230 pub prepare: Duration,
231 /// Glyph-run routing, ahead of the walk.
232 pub classify: Duration,
233 /// Glyph atlas admission, closing the routing pass.
234 pub admit: Duration,
235 /// The command walk: strip generation and paint encoding, with the
236 /// parallel walk's planning, spawn and join.
237 pub walk: Duration,
238 /// The part of [`walk`](Self::walk) spent inside glyph runs.
239 ///
240 /// A **subset** of `walk`, not a seventh phase beside it, and so
241 /// deliberately excluded from [`Self::total`]: adding it would count the
242 /// glyph work twice. It exists because "the walk dominates" is not on its
243 /// own an actionable measurement on a text-heavy scene — whether the cost
244 /// is the text or everything drawn around it is the question that decides
245 /// where a lever could go.
246 ///
247 /// Accumulated per [`Command::GlyphRun`] rather than per glyph: a run is
248 /// the unit the cache and the atlas policy both work in, and two clock
249 /// reads a glyph would cost more than the phase being measured.
250 pub glyphs: Duration,
251 /// Closing groups, punch generation, atlas ageing, the image plan.
252 pub finish: Duration,
253}
254
255impl CompileSpans {
256 /// What the six phases sum to.
257 ///
258 /// Saturating rather than `+`: a sum is only ever read by a diagnostic
259 /// line, and overflowing one must not take the frame with it (E17).
260 #[must_use]
261 pub fn total(&self) -> Duration {
262 [
263 self.prepare,
264 self.classify,
265 self.admit,
266 self.walk,
267 self.finish,
268 ]
269 .iter()
270 .fold(self.validate, |acc, span| acc.saturating_add(*span))
271 }
272}
273
274/// Where one glyph an atlas-routed draw sampled lives in the atlas array.
275///
276/// The image half of residency travels as an [`ImageUpload`], carrying pixels;
277/// a glyph's pixels are produced *on the GPU* by the replay pass, so nothing
278/// travels here but the rectangle — which the renderer still needs, because a
279/// glyph paint names its slot by [`ImageId`] and only the sink that drew it was
280/// ever handed the slot itself.
281///
282/// Reported per draw rather than per allocation, so a recycled handle can never
283/// be resolved against a previous occupant's rectangle: `glifo` returns an
284/// evicted slot's id to the shared allocator, and whatever takes it next — a
285/// glyph or an image — reports its own rectangle on the frame it is drawn.
286#[derive(Debug, Clone, Copy, PartialEq, Eq)]
287pub struct GlyphSlot {
288 /// The handle the draw's image paint names this slot by.
289 pub id: ImageId,
290 /// The slot's own rectangle, padding excluded.
291 pub region: AtlasRegion,
292 /// Transparent padding texels `glifo` keeps around `region`.
293 pub padding: u32,
294}
295
296/// One glyph drawn as a *glyph quad*: the whole-pixel rectangle its single
297/// rect instance covers, and the atlas texel under that rectangle's top-left
298/// corner (see [`crate::text`]'s backend for when a glyph is one).
299///
300/// The draw it belongs to is an [`EngineDraw`] with the run's shared record as
301/// its paint, found by its painter-order depth, which no two draws of a frame
302/// share. Its strip range is empty unless the glyph was drawn inside an
303/// isolated layer, where it carries the rectangle's own coverage so the
304/// recorder can fold its bounds into the layer's: the renderer draws a quad
305/// from this alone — one rect instance, no edge coverage (the rectangle lands
306/// on whole pixels), its payload this atlas texel — and never from strips.
307#[derive(Debug, Clone, Copy, PartialEq, Eq)]
308pub struct GlyphQuad {
309 /// The depth of the draw this quad is.
310 pub depth: u32,
311 /// Left edge in device pixels, already trimmed to the viewport and the
312 /// clip stack's scissor.
313 pub x: u16,
314 /// Top edge in device pixels, trimmed likewise.
315 pub y: u16,
316 /// Width in device pixels; never zero.
317 pub width: u16,
318 /// Height in device pixels; never zero.
319 pub height: u16,
320 /// Atlas texel column under the rectangle's left edge.
321 pub atlas_x: u16,
322 /// Atlas texel row under the rectangle's top edge.
323 pub atlas_y: u16,
324}
325
326/// Everything one compiled frame produced.
327///
328/// The strips and their alpha coverage share one
329/// [`StripStorage`]: `strips.strips` is the frame's whole strip buffer (each
330/// [`EngineDraw::strip_range`] indexes into it) and `strips.alphas` the alpha
331/// runs those strips reference. They are kept together because a strip's
332/// packed alpha index is only meaningful against the alpha buffer generated
333/// alongside it.
334///
335/// The draws themselves live in `recorder.draws` rather than in a field of
336/// their own: [`CommandRecorder`] already owns that vector, and its node
337/// ranges index into it, so a second parallel copy could only drift out of
338/// agreement with the recording. Read them through [`CompiledFrame::draws`].
339#[derive(Debug)]
340pub struct CompiledFrame {
341 /// The frame's strips and the alpha coverage they index.
342 pub strips: StripStorage,
343 /// The recorded render graph, owning the frame's draws.
344 pub recorder: CommandRecorder<EngineDraw>,
345 /// Paints too complex to inline into a draw, indexed by
346 /// [`Paint::Indexed`](vello_common::paint::Paint::Indexed).
347 pub encoded_paints: Vec<EncodedPaint>,
348 /// The frame's hole punches, hoisted to the root and issued at the
349 /// punch's own painter-order position (see [`clear`]).
350 ///
351 /// Deliberately not draws: a punch erases rather than paints, and keeping
352 /// it out of the recording is what lets a target that disregards alpha
353 /// drop the whole pass and read the frame unchanged.
354 pub clears: Vec<ClearPunch>,
355 /// The colour ramps `encoded_paints` needs made resident before the frame
356 /// is drawn, one per gradient entry.
357 ///
358 /// Deliberately not serviced here: the compiler holds no gradient cache,
359 /// so ramp residency is decided once per frame by the renderer rather than
360 /// per draw by the walk (see [`paint`]).
361 pub lut_requests: Vec<LutRequest>,
362 /// How many of this frame's draws wrote their strip coverage directly as a
363 /// rectangle, bypassing flattening and tiling (see [`fast_rect`]).
364 ///
365 /// Purely observational — nothing downstream branches on it. It exists so
366 /// the fast path's admission rule is measurable from outside the compiler
367 /// rather than inferred from a strip count that both paths can produce.
368 pub fast_rect_draws: u32,
369 /// How many of this frame's clips lowered to a scissor rectangle, costing
370 /// no rasterization at all (see [`clip`]).
371 pub scissor_clips: u32,
372 /// How many of this frame's clips lowered to a coverage mask.
373 pub mask_clips: u32,
374 /// Atlas regions whose texels must be cleared before this frame draws,
375 /// freed by the residency reap at the head of the frame.
376 ///
377 /// Serviced **before** [`image_uploads`](Self::image_uploads): a rectangle
378 /// freed this frame can be re-allocated in the same frame, so clearing
379 /// after writing would erase the image that just moved in.
380 pub image_evictions: Vec<AtlasRegion>,
381 /// Atlas regions whose texels must be written before this frame draws, one
382 /// per image that became resident during it.
383 ///
384 /// Empty in the steady state: an image drawn on a thousand consecutive
385 /// frames appears here exactly once, on the first.
386 pub image_uploads: Vec<ImageUpload>,
387 /// The atlas array depth this frame's paints address — the layer count the
388 /// array texture must have grown to before the uploads are written.
389 pub atlas_layers: u32,
390 /// How many of this frame's draws painted with an atlas-backed image.
391 pub image_draws: u32,
392 /// How many image draws were dropped because the image could not be made
393 /// resident (unsupported format, oversized, malformed, atlas full, the
394 /// same blob already resolved this frame at another extent, or the atlas
395 /// disabled outright).
396 ///
397 /// Observational, and the counter that makes "an image the engine cannot
398 /// hold is a skipped draw, not a panicked frame" measurable rather than
399 /// asserted.
400 pub skipped_images: u32,
401 /// How many of this frame's draws painted with an externally bound
402 /// texture.
403 pub external_draws: u32,
404 /// How many external-texture draws were dropped — an id nothing is
405 /// registered under, or a destination the texture cannot be mapped onto.
406 ///
407 /// Observational, and the external counterpart of
408 /// [`skipped_images`](Self::skipped_images): "a texture the engine cannot
409 /// resolve is a skipped draw, not a wrongly-sampled one", measured rather
410 /// than asserted.
411 pub skipped_externals: u32,
412 /// How many strips this frame's coverage masks cost.
413 ///
414 /// Observational, and the counter the clip lowering's whole claim rests on:
415 /// a frame whose clips all scissored reports zero here, which is what
416 /// "a rectangular clip is free" means measured rather than asserted.
417 pub clip_mask_strips: usize,
418 /// How many of this frame's draws painted one glyph outline.
419 ///
420 /// A glyph run costs one draw per glyph that produced coverage, so this is
421 /// bounded by — and usually below — the run's own glyph count: a glyph
422 /// clipped away or carrying no ink (a space) records nothing.
423 pub glyph_draws: u32,
424 /// How many glyphs were dropped because the engine has no way to paint
425 /// them on this path — a colour (COLR) glyph, a bitmap-strike glyph, or a
426 /// stroked outline.
427 ///
428 /// Observational, and the counter that makes "a glyph the engine cannot
429 /// paint goes missing rather than landing wrong" measurable rather than
430 /// asserted, the same way [`skipped_images`](Self::skipped_images) does
431 /// for images.
432 pub skipped_glyphs: u32,
433 /// How many of [`glyph_draws`](Self::glyph_draws) sampled the glyph atlas
434 /// rather than rasterizing an outline.
435 ///
436 /// The measure of what the policy is actually buying: a page of settled
437 /// text reads all-atlas, an animating size reads zero, and the difference
438 /// between them is the frame's rasterization work.
439 ///
440 /// Observational only. It is **not** the signal for whether the atlas has
441 /// pixel work outstanding — a run whose draws were all culled still
442 /// inserted entries and dirtied a page while reporting zero here. That
443 /// question is [`SceneCompiler::glyph_replay_pending`]'s.
444 pub atlas_glyph_draws: u32,
445 /// Where each of this frame's atlas-sampled glyphs lives, one entry per
446 /// atlas draw (see [`GlyphSlot`]).
447 pub glyph_slots: Vec<GlyphSlot>,
448 /// The frame's glyph quads, one per draw a fully resident glyph run drew
449 /// as one, in depth order (see [`GlyphQuad`]). A quad's draw is the one
450 /// kind of draw whose strip range may be empty: outside an isolated layer
451 /// nothing reads its coverage, so none is generated.
452 ///
453 /// A subset of [`atlas_glyph_draws`](Self::atlas_glyph_draws), and empty
454 /// whenever quads are off (`FRUST_ENGINE_NO_GLYPH_QUADS`,
455 /// [`SceneCompiler::set_glyph_quads`]).
456 pub glyph_quads: Vec<GlyphQuad>,
457 /// The indices into [`encoded_paints`](Self::encoded_paints) of the
458 /// records glyph quads are painted with — one per glyph run and atlas
459 /// layer — in ascending order.
460 ///
461 /// Each is an image record naming one of the run's slots, read for its
462 /// atlas layer and tint alone; a draw painted with one is drawn from its
463 /// [`GlyphQuad`], never from the record's own placement.
464 pub glyph_run_paints: Vec<usize>,
465 /// How many glyph runs this frame drew out of the resident-run cache —
466 /// every glyph resolved from slots remembered on an earlier frame, without
467 /// the run reaching `glifo` (see [`crate::text::ResidentPlan`]).
468 ///
469 /// Observational: a resident run draws exactly the quads, records and
470 /// slots `glifo`'s own fully resident run would, so nothing downstream
471 /// tells the two apart. Zero whenever the cache is off
472 /// (`FRUST_ENGINE_NO_GLYPH_RUN_CACHE`, [`SceneCompiler::set_glyph_run_cache`])
473 /// or quads are.
474 pub resident_glyph_runs: u32,
475 /// How many of this frame's draws took strips a helper thread generated
476 /// rather than strips the walk generated itself (see the `parallel` module).
477 ///
478 /// Observational: a helper's run is byte-identical to the one the walk
479 /// would have generated, so nothing downstream tells the two apart. Zero
480 /// whenever the frame spawned no helper — one worker
481 /// (`FRUST_ENGINE_NO_PARALLEL`, [`SceneCompiler::set_parallel_workers`]),
482 /// planned work below [`SceneCompiler::parallel_min_cost`], or a walk that
483 /// reached every planned command before a helper did.
484 pub parallel_draws: u32,
485 /// Atlas rectangles freed by the *previous* frame's glyph eviction, to be
486 /// zeroed before this frame writes anything into the array.
487 ///
488 /// Carried a frame late deliberately: `glifo` evicts at the end of a frame,
489 /// and a rectangle it frees can be handed straight back out on the next
490 /// one, so clearing it after that frame's uploads and replay would erase
491 /// whatever just moved in. Same ordering, same reason, as
492 /// [`image_evictions`](Self::image_evictions).
493 ///
494 /// Reported rather than consumed, on the same terms as
495 /// [`image_evictions`](Self::image_evictions): the same rectangles appear
496 /// on every later frame until a caller that really wrote them calls
497 /// [`SceneCompiler::acknowledge_glyph_clears`]. A frame compiled and then
498 /// refused takes none of them with it.
499 pub glyph_clears: Vec<PendingClearRect>,
500 /// The retained brackets this frame recorded, in recording order — empty
501 /// unless retention is on ([`SceneCompiler::set_retained_layers`]).
502 ///
503 /// Each names the recorded layer standing for it; the renderer's
504 /// retained-page store (`crate::cache::retained`) resolves every one to a
505 /// page, a composite or the inline lowering before the frame is scheduled.
506 pub retained: Vec<RetainedBracket>,
507 /// The keys of every `PushRetained` this frame carried that is *not* in
508 /// [`retained`](Self::retained), in recording order — empty unless
509 /// retention is on.
510 ///
511 /// Only the outermost bracket is recorded; one nested inside it (or one
512 /// whose transform left the device grid) is painted as part of the
513 /// enclosing body and its own key's page receives nothing this frame. The
514 /// retained-page store evicts every key listed here, so the ledger never
515 /// names a page older than the frame's own recording of the body (see
516 /// `crate::cache::retained`'s *A page holds this frame's recording, or
517 /// goes*).
518 pub retained_superseded: Vec<u64>,
519 /// The frame's backdrop blurs, in recording order — and so in ascending
520 /// [`Backdrop::depth`] — one per `Command::BackdropBlur` whose rectangle
521 /// produced coverage (see [`crate::filters`]' *The backdrop blur*).
522 ///
523 /// Empty on every frame that records none, and then nothing downstream
524 /// plans, cuts or draws anything differently.
525 pub backdrops: Vec<Backdrop>,
526 /// What compiling this frame cost, phase by phase — all zero without
527 /// `perf-trace` (see [`CompileSpans`]).
528 ///
529 /// Carried on the frame rather than kept on the compiler because the
530 /// consumer is the renderer's own encode-phase accounting, which already
531 /// holds the frame and would otherwise have to reach back into the
532 /// compiler for a number belonging to this frame alone.
533 pub compile_spans: CompileSpans,
534}
535
536impl CompiledFrame {
537 /// The frame's draws, in paint order (back-most first).
538 pub fn draws(&self) -> &[EngineDraw] {
539 &self.recorder.draws
540 }
541
542 /// The frame's whole strip buffer; a draw's `strip_range` indexes into it.
543 pub fn strip_buf(&self) -> &[vello_common::strip::Strip] {
544 &self.strips.strips
545 }
546
547 /// The alpha coverage the frame's strips reference.
548 pub fn alphas(&self) -> &[u8] {
549 &self.strips.alphas
550 }
551}
552
553/// Compiles a `frust_scene::Scene` into strips and draws.
554///
555/// Create one per surface and reuse it across frames — the retained
556/// [`StripGenerator`] is the point.
557#[derive(Debug)]
558pub struct SceneCompiler {
559 generator: StripGenerator,
560 /// The SIMD level the walk's generator was built at, so a helper's
561 /// generator is built at the same one (see [`parallel`]).
562 level: Level,
563 /// How many threads generate a frame's strips, the calling thread
564 /// included; one is the sequential walk. Starts at
565 /// [`parallel::default_workers`]; [`Self::set_parallel_workers`] decides
566 /// it per compiler.
567 parallel_workers: usize,
568 /// The estimated cost a frame's planned helper work must reach before a
569 /// helper is spawned for it (see [`DEFAULT_PARALLEL_MIN_COST`]). Starts at
570 /// that default, or `FRUST_ENGINE_PARALLEL_MIN_COST`;
571 /// [`Self::set_parallel_min_cost`] decides it per compiler.
572 parallel_min_cost: u64,
573 /// The helpers' generators and the per-job slots their runs wait in,
574 /// retained across frames for their allocations alone.
575 pool: Pool,
576 clips: ClipStack,
577 groups: GroupStack,
578 retained_stack: RetainedStack,
579 /// Whether a `PushRetained` bracket is recorded for the retained-page
580 /// store and a `DrawRetained` lowered rather than refused (see
581 /// [`Self::set_retained_layers`]). Off for a compiler built on its own.
582 retain: bool,
583 /// The [`CompiledFrame::retained`] entry of the outermost `PushRetained`
584 /// body the walk is inside, while it is inside one — what a hole punch or
585 /// an external texture drawn in the body marks unretainable.
586 retained_open: Option<usize>,
587 punches: Vec<StagedPunch>,
588 images: ImageResidency,
589 glyphs: GlyphPrepCache,
590 /// Which glyphs earn an atlas slot, and the entry map they live in.
591 ///
592 /// A sibling field of [`Self::images`] rather than a member of it: the two
593 /// share one allocator but decide different things, and every call that
594 /// needs both takes them as disjoint borrows of this struct (see
595 /// [`crate::text::atlas_policy`] for why the allocator is the residency's).
596 glyph_atlas: AtlasPolicy,
597 /// This frame's per-run routing decisions, in the order the scene records
598 /// its glyph runs.
599 ///
600 /// Filled by the collect walk at the head of [`Self::compile`] and consumed
601 /// by the draw walk one run at a time. Retained across frames only for its
602 /// allocation.
603 run_routes: Vec<RunRoute>,
604 /// How many of [`Self::run_routes`] the draw walk has consumed.
605 next_run: usize,
606 /// Scratch for the collect walk's distinct-glyph count, retained across
607 /// runs and frames for its allocation alone. A run's admission is charged
608 /// against the atlas budget at that count (see
609 /// [`crate::text::RunKey::distinct_glyphs`]), counted by sorting the run's
610 /// ids and dropping repeats; one buffer owned here is one not allocated
611 /// per run. It carries nothing between calls — `RunKey::for_run` empties it
612 /// before it counts.
613 run_glyph_ids: Vec<u32>,
614 /// Whether a glyph run resolved entirely out of the glyph atlas is drawn
615 /// as glyph quads (see [`crate::text`]). On unless
616 /// `FRUST_ENGINE_NO_GLYPH_QUADS` is set; [`Self::set_glyph_quads`] decides
617 /// it per compiler.
618 glyph_quads: bool,
619 /// Where a run's glyph quads wait for the run to finish, retained for its
620 /// allocations alone.
621 glyph_quad_scratch: GlyphQuadScratch,
622 /// Whether a run drawn as glyph quads may be drawn out of the glyph-atlas
623 /// policy's resident-run cache rather than through `glifo` (see
624 /// [`crate::text::ResidentPlan`]). On unless
625 /// `FRUST_ENGINE_NO_GLYPH_RUN_CACHE` is set; [`Self::set_glyph_run_cache`]
626 /// decides it per compiler.
627 glyph_run_cache: bool,
628 /// The font gate's answers for the faces drawn most recently, so a frame
629 /// of many short runs reads each face's table directory once (see
630 /// [`crate::text::FontFacts`]).
631 font_facts: FontFacts,
632 /// Whether a glyph run's outline is hinted before it is rasterized (see
633 /// [`crate::text`]'s module doc for the split this half of the policy
634 /// answers). Mobile-safe by default — `false`, the same "known nothing
635 /// about the device yet" reasoning [`Self::new`] gives
636 /// [`AtlasBudget::MOBILE`] — and set from the adapter's own class by
637 /// [`Self::for_caps`], or directly by [`Self::set_hint_text`] for a test
638 /// that wants either answer without a `TierCaps` in hand.
639 hint_text: bool,
640 /// The texel extent of every externally bound texture, so a
641 /// [`Command::SceneTexture`] can be lowered without this crate's compile
642 /// half knowing anything about `wgpu` (see
643 /// [`crate::compile::external`]). Written through
644 /// [`Self::bind_external_texture`]/[`Self::unbind_external_texture`],
645 /// which the renderer calls alongside its own view registry so the two
646 /// halves are always registered together.
647 externals: ExternalExtents,
648 /// The frame's own target extent — the `(width, height)` most recently
649 /// passed to [`Self::compile`] (or, before the first call, this
650 /// compiler's own construction size). Kept only so the `ShaderQuad` arm
651 /// can tell a quad deliberately culled by the shader-quad pre-pass's own
652 /// target-extent check (`crate::effects::shader_quad`) apart from one
653 /// whose pre-pass genuinely never ran — see [`shader_quad_is_culled`] and
654 /// [`note_shader_quad_unrendered`]/[`note_shader_quad_culled`].
655 frame_extent: (u16, u16),
656}
657
658impl SceneCompiler {
659 /// A compiler sized for a `width` x `height` viewport.
660 ///
661 /// The size is re-asserted on every [`compile`](Self::compile) call, so
662 /// this is only the initial allocation hint; pass the surface's current
663 /// size to avoid an immediate resize.
664 ///
665 /// Image residency starts on [`AtlasBudget::MOBILE`], the smaller of the
666 /// two tiers. A compiler built without an adapter in hand knows nothing
667 /// about the device it will end up on, and over-budgeting a phone costs
668 /// real memory while under-budgeting a desktop costs only an extra atlas
669 /// layer — call [`for_caps`](Self::for_caps) or
670 /// [`set_atlas_budget`](Self::set_atlas_budget) once the adapter is known.
671 pub fn new(width: u16, height: u16) -> Self {
672 Self::with_atlas_budget(width, height, AtlasBudget::MOBILE)
673 }
674
675 /// A compiler sized for a `width` x `height` viewport, with image
676 /// residency budgeted for `caps`' adapter and glyph hinting decided by
677 /// `caps`' device class.
678 ///
679 /// Hinting is turned on for a desktop-class adapter and left off for a
680 /// mobile one — the same `!`[`is_mobile_tier`] split
681 /// [`AtlasBudget::for_caps`] draws its own tier from, so a caller with an
682 /// adapter in hand only ever answers the mobile-or-desktop question once.
683 /// See [`crate::text`]'s module doc for why hinting defaults off and what
684 /// the other half of the policy — the transform predicate `glifo` applies
685 /// on top of this — is not this crate's to make.
686 pub fn for_caps(width: u16, height: u16, caps: &TierCaps) -> Self {
687 let mut compiler = Self::with_atlas_budget(width, height, AtlasBudget::for_caps(caps));
688 compiler.hint_text = !is_mobile_tier(caps);
689 compiler
690 }
691
692 /// Set whether a glyph run's outline is hinted before it is rasterized,
693 /// bypassing [`Self::for_caps`]' `TierCaps` reading.
694 ///
695 /// For a test that wants a chosen answer without building a `TierCaps` —
696 /// [`Self::new`] and [`Self::with_atlas_budget`] already default to the
697 /// mobile-safe `false`, so this is also how a caller that built one of
698 /// those turns hinting on.
699 pub fn set_hint_text(&mut self, hint_text: bool) {
700 self.hint_text = hint_text;
701 }
702
703 /// Set whether a glyph run resolved entirely out of the glyph atlas is
704 /// drawn as glyph quads — one rect instance per glyph, one record per run
705 /// and atlas layer — or the general way, one image record and one
706 /// generated rectangle per glyph (see [`crate::text`]).
707 ///
708 /// The per-compiler counterpart of `FRUST_ENGINE_NO_GLYPH_QUADS`, which
709 /// only sets the value a compiler starts with: a caller comparing the two
710 /// lowerings in one process sets it here rather than through a
711 /// process-global environment variable. Takes effect from the next run.
712 pub fn set_glyph_quads(&mut self, enabled: bool) {
713 self.glyph_quads = enabled;
714 }
715
716 /// Whether resident glyph runs are drawn as glyph quads.
717 #[must_use]
718 pub fn glyph_quads_enabled(&self) -> bool {
719 self.glyph_quads
720 }
721
722 /// Set whether a glyph run drawn as glyph quads may be drawn out of the
723 /// resident-run cache — its glyphs' slots remembered from an earlier
724 /// frame — instead of being resolved glyph by glyph through `glifo` (see
725 /// [`crate::text::ResidentPlan`]).
726 ///
727 /// The per-compiler counterpart of `FRUST_ENGINE_NO_GLYPH_RUN_CACHE`, on
728 /// the same terms as [`Self::set_glyph_quads`]: the variable sets the
729 /// value a compiler starts with, this decides it for one compiler. Takes
730 /// effect from the next run. The cache only ever serves quads, so turning
731 /// quads off turns it off too.
732 pub fn set_glyph_run_cache(&mut self, enabled: bool) {
733 self.glyph_run_cache = enabled;
734 }
735
736 /// Whether glyph runs may be drawn out of the resident-run cache.
737 #[must_use]
738 pub fn glyph_run_cache_enabled(&self) -> bool {
739 self.glyph_run_cache
740 }
741
742 /// Set whether retained brackets are recorded for a retained-page store.
743 ///
744 /// Off by default: a compiler built on its own has no store behind it, so
745 /// it lowers every `PushRetained` inline exactly as `frust_scene` tells a
746 /// renderer that does not retain to, and refuses a `DrawRetained` with
747 /// [`EngineError::RetainedKeyMissing`]. The renderer turns it on for the
748 /// compiler it owns (unless `FRUST_ENGINE_NO_LAYERS` is set): each
749 /// outermost bracket's body then becomes a recorded layer whatever its
750 /// alpha and is listed in [`CompiledFrame::retained`], and a `DrawRetained`
751 /// becomes an empty layer at its painter-order position (see
752 /// [`layers`]). Takes effect from the next frame.
753 pub fn set_retained_layers(&mut self, enabled: bool) {
754 self.retain = enabled;
755 }
756
757 /// Whether retained brackets are recorded for a retained-page store.
758 #[must_use]
759 pub fn retained_layers_enabled(&self) -> bool {
760 self.retain
761 }
762
763 /// How many glyph runs the resident-run cache currently remembers.
764 ///
765 /// Bounded by [`MAX_RESIDENT_GLYPH_RUNS`] records (and a byte budget
766 /// beside it), however many distinct runs have been drawn.
767 #[must_use]
768 pub fn resident_glyph_run_count(&self) -> usize {
769 self.glyph_atlas.resident_runs()
770 }
771
772 /// Set how many threads generate a frame's strips, the calling thread
773 /// included (see the `parallel` module). One — or zero, read as one — is the
774 /// sequential walk: no planning pass, no helper thread.
775 ///
776 /// The per-compiler counterpart of `FRUST_ENGINE_NO_PARALLEL`, on the same
777 /// terms as [`Self::set_glyph_quads`]: the variable sets the value a
778 /// compiler starts with ([`DEFAULT_PARALLEL_WORKERS`] capped by the
779 /// machine, or one with it set), this decides it for one compiler. Takes
780 /// effect from the next frame. The frame a compiler produces is the same
781 /// at every setting. Capped at [`MAX_PARALLEL_WORKERS`] — every
782 /// helper is a thread spawned per frame and a strip generator kept across
783 /// them, so a count far past any machine would only pay for threads that
784 /// cannot run; the variable's path is capped by the machine's own thread
785 /// count instead, so that a test can ask for the helper count it means to
786 /// exercise on any host.
787 pub fn set_parallel_workers(&mut self, workers: usize) {
788 self.parallel_workers = workers.clamp(1, parallel::MAX_PARALLEL_WORKERS);
789 }
790
791 /// How many threads generate a frame's strips, the calling thread
792 /// included.
793 #[must_use]
794 pub fn parallel_workers(&self) -> usize {
795 self.parallel_workers
796 }
797
798 /// Set the estimated cost a frame's planned helper work must reach before
799 /// a helper is spawned for it (see [`DEFAULT_PARALLEL_MIN_COST`] for the
800 /// unit). Zero spawns for any frame with a single planned command, which
801 /// is how a test drives the helpers over a scene too small to earn them.
802 pub fn set_parallel_min_cost(&mut self, cost: u64) {
803 self.parallel_min_cost = cost;
804 }
805
806 /// The estimated cost a frame's planned work must reach before a helper
807 /// is spawned.
808 #[must_use]
809 pub fn parallel_min_cost(&self) -> u64 {
810 self.parallel_min_cost
811 }
812
813 /// A compiler sized for a `width` x `height` viewport, with image
814 /// residency budgeted explicitly.
815 pub fn with_atlas_budget(width: u16, height: u16, budget: AtlasBudget) -> Self {
816 let level = Level::try_detect().unwrap_or(Level::baseline());
817 let images = ImageResidency::new(budget);
818 Self {
819 generator: StripGenerator::new(width, height, level),
820 level,
821 parallel_workers: parallel::default_workers(),
822 parallel_min_cost: config::parallel_min_cost().unwrap_or(DEFAULT_PARALLEL_MIN_COST),
823 pool: Pool::default(),
824 clips: ClipStack::new(),
825 groups: GroupStack::new(),
826 retained_stack: RetainedStack::new(),
827 retain: false,
828 retained_open: None,
829 punches: Vec::new(),
830 // Built from the residency, so the policy's page geometry is read
831 // off the allocator it will pack into rather than derived a second
832 // time from the same budget.
833 glyph_atlas: glyph_atlas_policy(&images),
834 images,
835 glyphs: GlyphPrepCache::default(),
836 run_routes: Vec::new(),
837 next_run: 0,
838 run_glyph_ids: Vec::new(),
839 glyph_quads: !config::glyph_quads_disabled(),
840 glyph_quad_scratch: GlyphQuadScratch::default(),
841 glyph_run_cache: !config::glyph_run_cache_disabled(),
842 font_facts: FontFacts::default(),
843 hint_text: false,
844 externals: ExternalExtents::new(),
845 frame_extent: (width, height),
846 }
847 }
848
849 /// Records an externally owned texture as bound under `id` at `size`
850 /// texels, answering whether the extent is one a paint can be composed
851 /// against at all (see [`ExternalExtents::bind`]).
852 ///
853 /// Only the extent: the view the frame's passes sample is the renderer's
854 /// (see [`crate::gpu::bindings`]). A caller that registers one half without
855 /// the other gets a texture that draws nothing, which is why the renderer's
856 /// own `bind_texture` writes both.
857 pub fn bind_external_texture(&mut self, id: u64, size: (u32, u32)) -> bool {
858 self.externals.bind(id, size)
859 }
860
861 /// Forgets the extent recorded for `id`, so a `SceneTexture` naming it
862 /// draws nothing again.
863 pub fn unbind_external_texture(&mut self, id: u64) {
864 self.externals.unbind(id);
865 }
866
867 /// The externally bound extents this compiler resolves against.
868 #[must_use]
869 pub fn externals(&self) -> &ExternalExtents {
870 &self.externals
871 }
872
873 /// The glyph entry map, for the caller that has to drain the pages this
874 /// compiler's last frame dirtied.
875 ///
876 /// The engine produces no glyph pixels itself: `glifo` records the fills
877 /// that rasterize a newly cached glyph into a per-page recorder, and
878 /// [`crate::gpu::atlas::AtlasRenderer::render_pending`] replays them into
879 /// the atlas array before the frame's scene pass. That replay needs the map
880 /// itself, which is what this hands over.
881 pub fn glyph_atlas_mut(&mut self) -> &mut GlyphAtlas {
882 self.glyph_atlas.atlas_mut()
883 }
884
885 /// How many glyphs this compiler currently holds resident in the atlas.
886 ///
887 /// Observational, and the counter the policy's whole claim rests on: a page
888 /// of static text reaches a fixed number here and stays there, while an
889 /// animating size never contributes at all.
890 #[must_use]
891 pub fn glyph_atlas_entries(&self) -> usize {
892 self.glyph_atlas.entry_count()
893 }
894
895 /// Whether any glyph may be cached at all — `false` under
896 /// `FRUST_ENGINE_NO_ATLAS`.
897 #[must_use]
898 pub fn glyph_atlas_enabled(&self) -> bool {
899 self.glyph_atlas.is_enabled()
900 }
901
902 /// The glyph-atlas policy's counters for the last compiled frame — what
903 /// the `frust-perf enc` line's miss, eviction and run-key counts read.
904 #[cfg(feature = "perf-trace")]
905 pub(crate) fn glyph_atlas_trace(&self) -> &crate::text::atlas_policy::AtlasTrace {
906 self.glyph_atlas.trace()
907 }
908
909 /// The images this compiler currently holds resident.
910 pub fn images(&self) -> &ImageResidency {
911 &self.images
912 }
913
914 /// Record that a compiled frame's
915 /// [`image_evictions`](CompiledFrame::image_evictions) and
916 /// [`image_uploads`](CompiledFrame::image_uploads) have been serviced
917 /// against a live atlas array.
918 ///
919 /// The other half of the plan seam: [`compile`](Self::compile) reports the
920 /// plan without consuming it, and it goes on being reported — identically,
921 /// never duplicated — until this is called. Call it only once the regions
922 /// have really been written, so a frame refused after compiling keeps its
923 /// uploads for the next frame that is not (see [`crate::cache::images`]'s
924 /// module doc).
925 pub fn acknowledge_image_plan(&mut self) {
926 self.images.acknowledge_plan();
927 }
928
929 /// Whether `glifo` still holds recorded page commands, bitmap uploads or
930 /// freed rectangles that have not reached the atlas array.
931 ///
932 /// The gate a caller drives
933 /// [`crate::gpu::atlas::AtlasRenderer::render_pending`] from. Deliberately
934 /// *not* [`CompiledFrame::atlas_glyph_draws`]: `glifo` dirties a page when
935 /// it inserts an entry, not when a draw survives, so a run scrolled behind
936 /// a clip inserts entries and records fills while contributing no draw at
937 /// all. Gating on draws leaves those commands recorded — and a recorded
938 /// command outliving the slot it names is old ink replayed into whichever
939 /// glyph was let that rectangle next.
940 ///
941 /// Stays `true` across a frame the caller refuses, exactly as the image
942 /// plan does, until [`acknowledge_glyph_replay`](Self::acknowledge_glyph_replay).
943 #[must_use]
944 pub fn glyph_replay_pending(&self) -> bool {
945 self.glyph_atlas.replay_pending()
946 }
947
948 /// Record that the recorded page commands were replayed into the atlas
949 /// array.
950 ///
951 /// Also what lets `glifo`'s eviction pass resume: while a replay is
952 /// outstanding the policy defers ageing, so that no rectangle a recorded
953 /// command still names can be freed and re-let underneath it (see
954 /// [`crate::text::atlas_policy`]).
955 pub fn acknowledge_glyph_replay(&mut self) {
956 self.glyph_atlas.acknowledge_replay();
957 }
958
959 /// Whether any rectangle freed by glyph eviction is still waiting to be
960 /// zeroed.
961 #[must_use]
962 pub fn glyph_clears_pending(&self) -> bool {
963 self.glyph_atlas.has_pending_clears()
964 }
965
966 /// Record that this frame's [`CompiledFrame::glyph_clears`] were written to
967 /// the atlas array.
968 ///
969 /// The glyph half of the same re-offer contract
970 /// [`acknowledge_image_plan`](Self::acknowledge_image_plan) closes for
971 /// images: [`compile`](Self::compile) reports the clears without consuming
972 /// them, and goes on reporting the same ones, until a caller that really
973 /// issued the writes says so. A frame compiled and then dropped therefore
974 /// leaves no rectangle holding an evicted glyph's pixels.
975 pub fn acknowledge_glyph_clears(&mut self) {
976 self.glyph_atlas.acknowledge_clears();
977 }
978
979 /// Re-budget image residency, dropping every image currently resident.
980 ///
981 /// The atlas geometry is what an allocation's coordinates mean, so a change
982 /// to it invalidates every rectangle already handed out: residency starts
983 /// over and each image re-uploads on the next frame that draws it. A caller
984 /// that owns the atlas texture must recreate it at the new extent in the
985 /// same step — this is an adapter-change or start-up operation, never a
986 /// per-frame one.
987 pub fn set_atlas_budget(&mut self, budget: AtlasBudget) {
988 self.set_image_residency(ImageResidency::new(budget));
989 }
990
991 /// Replace this compiler's image residency wholesale, dropping every image
992 /// currently resident.
993 ///
994 /// The same invalidation [`set_atlas_budget`](Self::set_atlas_budget)
995 /// carries, exposed for the residencies a budget alone cannot express — a
996 /// deliberately [disabled](ImageResidency::disabled) one, or one a caller
997 /// built against an adapter's own capabilities.
998 ///
999 /// The glyph policy is rebuilt alongside it, and for the same reason: its
1000 /// slots came out of the allocator being replaced, so every one of them
1001 /// names a rectangle of a geometry that no longer exists. Text re-caches on
1002 /// the next frame that draws it, exactly as an image re-uploads.
1003 pub fn set_image_residency(&mut self, images: ImageResidency) {
1004 self.images = images;
1005 self.glyph_atlas = glyph_atlas_policy(&self.images);
1006 }
1007
1008 /// Compile `scene` for a `size` viewport, with `root` applied ahead of
1009 /// every command's own transform.
1010 ///
1011 /// # Errors
1012 ///
1013 /// [`EngineError::TargetTooLarge`] when `size` cannot be rounded up to
1014 /// whole tiles inside `u16`; [`EngineError::InvalidTransform`] when a
1015 /// composed transform is non-finite and so maps geometry to coordinates no
1016 /// `u16` pixel can hold; and [`EngineError::InvalidGeometry`] when a
1017 /// command the compiler lowers carries non-finite geometry of its own (see
1018 /// [`check_geometry`]). All three are refused before any strip is
1019 /// generated — the frame path returns errors and never panics.
1020 pub fn compile(
1021 &mut self,
1022 scene: &Scene,
1023 root: Affine,
1024 size: (u16, u16),
1025 ) -> Result<CompiledFrame, EngineError> {
1026 // The compiler's half of the encode's CPU accounting; the renderer
1027 // laps the rest of the call around it (see [`CompileSpans`]). Free
1028 // without `perf-trace`.
1029 let mut clock = PhaseClock::start();
1030 let (width, height) = size;
1031 check_tile_addressable(width, height)?;
1032 check_finite(root)?;
1033
1034 // The whole scene is refused up front rather than mid-walk, so a
1035 // rejected frame never leaves half its draws recorded.
1036 let mut backdrops = 0_usize;
1037 for command in scene.commands() {
1038 if matches!(command, Command::BackdropBlur { .. }) {
1039 backdrops = backdrops.saturating_add(1);
1040 }
1041 if let Some(transform) = command_transform(command) {
1042 check_finite(root * transform)?;
1043 }
1044 if let Command::DrawRetained { key, .. } = command
1045 && !self.retain
1046 {
1047 // This compiler retains nothing, so no ledger can have
1048 // declared a key resident and a `DrawRetained` here is a
1049 // recorder error. Refused with the whole frame, before any
1050 // strip is generated; the caller logs the reason and skips
1051 // the frame. With retention on, whether the key is resident is
1052 // the renderer's store's to answer, after the walk.
1053 return Err(EngineError::RetainedKeyMissing { key: *key });
1054 }
1055 check_geometry(command)?;
1056 }
1057 // Counted over the commands the scene records, not over the ones that
1058 // end up drawing: the bound is a statement about the display list a
1059 // widget tree produced, refused whole before a strip is generated.
1060 if backdrops > MAX_BACKDROPS_PER_FRAME {
1061 return Err(EngineError::TooManyBackdrops {
1062 recorded: backdrops,
1063 max: MAX_BACKDROPS_PER_FRAME,
1064 });
1065 }
1066
1067 let validate = clock.lap();
1068
1069 self.generator.reset(width, height);
1070 self.frame_extent = (width, height);
1071 self.clips.reset();
1072 self.groups.reset();
1073 self.retained_stack.reset();
1074 self.retained_open = None;
1075 self.punches.clear();
1076 // Ahead of the walk, so a rectangle this frame's reap frees is
1077 // available to this frame's own allocations and its clear is ordered
1078 // ahead of their uploads.
1079 self.images.begin_frame();
1080 // Once per compiled frame, which is the cadence `glifo` ages its
1081 // outline entries by. Ahead of the walk rather than after it for the
1082 // same reason as the reap above: the glyphs this frame is about to
1083 // draw should be stamped as used *after* the ageing pass, not before
1084 // it.
1085 self.glyphs.maintain();
1086 let prepare = clock.lap();
1087
1088 // Phase one of the frame: every glyph run is *routed* before any of
1089 // them is drawn. Opened here, beside the residency's own frame, because
1090 // the two age against the same clock.
1091 // Nothing is rasterized in that phase and no slot is allocated — the
1092 // walk only asks the policy which runs may be cached, which is what
1093 // records their sizes against the animation guard before a single glyph
1094 // reaches `glifo`. Closing the phase hands back the rectangles last
1095 // frame's eviction freed, to be zeroed ahead of anything this frame
1096 // writes (see [`CompiledFrame::glyph_clears`]).
1097 self.glyph_atlas.begin_frame();
1098 self.classify_runs(scene, root);
1099 let classify = clock.lap();
1100
1101 let glyph_clears = self
1102 .glyph_atlas
1103 .build(self.images.allocator_mut(), |_| {
1104 // Unreachable: the collect walk claims no glyph, because the
1105 // allocation and the rasterization of a cached glyph are
1106 // `glifo`'s own — it keys, packs and records every one of them
1107 // itself once a run reaches it with the cacher enabled. So the
1108 // pass this closes carries clears and nothing else.
1109 None
1110 })
1111 .clears;
1112 let admit = clock.lap();
1113
1114 let mut frame = CompiledFrame {
1115 strips: StripStorage::new(GenerationMode::Append),
1116 recorder: CommandRecorder::new(width, height),
1117 encoded_paints: Vec::new(),
1118 clears: Vec::new(),
1119 lut_requests: Vec::new(),
1120 fast_rect_draws: 0,
1121 scissor_clips: 0,
1122 mask_clips: 0,
1123 clip_mask_strips: 0,
1124 image_evictions: Vec::new(),
1125 image_uploads: Vec::new(),
1126 atlas_layers: 0,
1127 image_draws: 0,
1128 skipped_images: 0,
1129 external_draws: 0,
1130 skipped_externals: 0,
1131 glyph_draws: 0,
1132 skipped_glyphs: 0,
1133 atlas_glyph_draws: 0,
1134 glyph_slots: Vec::new(),
1135 glyph_quads: Vec::new(),
1136 glyph_run_paints: Vec::new(),
1137 resident_glyph_runs: 0,
1138 parallel_draws: 0,
1139 glyph_clears,
1140 retained: Vec::new(),
1141 retained_superseded: Vec::new(),
1142 backdrops: Vec::new(),
1143 compile_spans: CompileSpans::default(),
1144 };
1145 let mut depth = DepthCounter::new();
1146
1147 // The walk proper, with its shape commands' strips generated by
1148 // helper threads behind it whenever the plan earns one (see
1149 // [`parallel`]). Planned and spawned inside the walk's lap, and the
1150 // helpers joined before it closes, so the parallel walk's overhead is
1151 // charged to the phase it shortens.
1152 let helpers = self.parallel_workers.saturating_sub(1);
1153 let plan = if helpers > 0 {
1154 parallel::plan_with(
1155 scene,
1156 root,
1157 Rect::new(0.0, 0.0, f64::from(width), f64::from(height)),
1158 self.retain,
1159 )
1160 } else {
1161 parallel::Plan::default()
1162 };
1163 if helpers > 0 && !plan.is_empty() && plan.cost() >= self.parallel_min_cost {
1164 // Moved out for the walk's duration: the helpers borrow it while
1165 // the walk borrows the compiler. It is put back on the normal
1166 // return only: a helper panic propagates out of the scope, which
1167 // aborts a release build (`panic = "abort"` in the release profile)
1168 // and, in a dev, test or downstream build that unwinds, leaves the
1169 // compiler with an empty pool — the sequential walk plus a fresh
1170 // pool's allocations on the next frame, slower and never wrong.
1171 let mut pool = std::mem::take(&mut self.pool);
1172 let level = self.level;
1173 pool.run(&plan, helpers, level, (width, height), |merge| {
1174 for (index, command) in scene.commands().iter().enumerate() {
1175 self.compile_command(index, command, root, &mut frame, &mut depth, Some(merge));
1176 }
1177 });
1178 self.pool = pool;
1179 } else {
1180 for (index, command) in scene.commands().iter().enumerate() {
1181 self.compile_command(index, command, root, &mut frame, &mut depth, None);
1182 }
1183 }
1184 let walk = clock.lap();
1185
1186 self.close_open_groups(&mut frame);
1187 self.generate_punches(&mut frame);
1188
1189 // Closes the frame the policy opened: ages `glifo`'s entry map, frees
1190 // whatever aged out back to the shared allocator, and takes the clear
1191 // rects that eviction produced — which belong to the *next* frame's
1192 // pass, not this one's.
1193 self.glyph_atlas.end_frame(self.images.allocator_mut());
1194
1195 frame.scissor_clips = self.clips.scissor_clips();
1196 frame.mask_clips = self.clips.mask_clips();
1197 frame.clip_mask_strips = self.clips.mask_strips();
1198 // Copied rather than drained. Compiling is not the moment residency
1199 // becomes true — this frame can still be refused by the caller after it
1200 // returns, and a refused frame never reaches the atlas. The plan stays
1201 // pending in the residency, re-offered on every later frame, until the
1202 // consumer that actually wrote the regions acknowledges it through
1203 // [`acknowledge_image_plan`](SceneCompiler::acknowledge_image_plan).
1204 let (evictions, uploads) = self.images.plan();
1205 frame.image_evictions = evictions;
1206 frame.image_uploads = uploads;
1207 frame.atlas_layers = self.images.layers();
1208
1209 // Field by field rather than as a whole struct, so the glyph subset
1210 // the walk accumulated into `frame` survives. Last, so `finish` covers
1211 // every phase above it and the six partition the call rather than
1212 // sampling parts of it.
1213 frame.compile_spans.validate = validate;
1214 frame.compile_spans.prepare = prepare;
1215 frame.compile_spans.classify = classify;
1216 frame.compile_spans.admit = admit;
1217 frame.compile_spans.walk = walk;
1218 frame.compile_spans.finish = clock.lap();
1219
1220 // After the last lap, deliberately: the line reports this frame's
1221 // residency, and a phase that included the cost of reporting on itself
1222 // would be measuring the instrumentation rather than the compile.
1223 #[cfg(feature = "perf-trace")]
1224 note_image_pressure(&frame, &self.images);
1225
1226 Ok(frame)
1227 }
1228
1229 fn compile_command(
1230 &mut self,
1231 index: usize,
1232 command: &Command,
1233 root: Affine,
1234 frame: &mut CompiledFrame,
1235 depth: &mut DepthCounter,
1236 merge: Option<&Merge<'_, '_>>,
1237 ) {
1238 // The frame root with any open retained bracket's presentation scale
1239 // composed ahead of it (see [`layers`]). The identity outside a
1240 // bracket, so this is the plain frame root for every frame that
1241 // records none.
1242 let combined = root * self.retained_stack.correction();
1243
1244 // Claimed for every command, drawn or not, ahead of every early return
1245 // below: claiming is what moves the walk's position past a planned
1246 // command, which is what stops a helper from starting work the walk
1247 // has already gone by (see [`parallel`]).
1248 let claim = merge.and_then(|merge| merge.claim(index, combined));
1249
1250 // Taken here rather than inside the glyph arm, and taken for every
1251 // glyph run whether or not it goes on to be drawn: the collect walk
1252 // classified one run per `Command::GlyphRun` in this same order, so
1253 // consuming one per `Command::GlyphRun` is what keeps the two walks in
1254 // step through every early return below.
1255 let route = match command {
1256 Command::GlyphRun(_) => self.take_run_route(),
1257 _ => None,
1258 };
1259
1260 // A correction composes a transform the up-front walk never saw, and
1261 // the product can leave the finite device grid even though both
1262 // factors are on it. Such a command draws nothing rather than refusing
1263 // the frame: the refusal is the up-front walk's to make over the
1264 // numbers a scene actually carries, and a bracket's presentation scale
1265 // is not one of them. Only a command *inside* a retained bracket can
1266 // land here at all — outside one the composition is the frame root's,
1267 // which that walk already checked.
1268 //
1269 // Drawing nothing is not the same as doing nothing: a command that
1270 // opens a bracket still has to open one, or its pop would close the
1271 // bracket around it instead. So a bracket lands blocked rather than
1272 // absent, which draws nothing inside it and balances its own pop.
1273 let on_grid = command_on_grid(command, combined);
1274 if !on_grid {
1275 match command {
1276 Command::PushClip { .. }
1277 | Command::PushClipRounded { .. }
1278 | Command::PushClipPath { .. }
1279 | Command::PushLayer { .. } => self.open_blocked_group(),
1280 // The correction is the outermost bracket's, so a bracket
1281 // reaching here is a nested one, whose presentation is ignored
1282 // anyway; only its depth has to be counted. The substitution is
1283 // still made through [`retained_entry`] rather than inline,
1284 // because it is the collect walk's to make identically (see
1285 // [`Self::classify_runs`]).
1286 Command::PushRetained {
1287 key,
1288 rect,
1289 scale,
1290 transform,
1291 ..
1292 } => {
1293 let (scale, transform) = retained_entry(on_grid, *scale, *transform);
1294 self.retained_stack.enter(*rect, scale, transform);
1295 self.supersede_retained(frame, *key);
1296 }
1297 _ => {}
1298 }
1299 return;
1300 }
1301
1302 match command {
1303 // A shape is its geometry's coverage under its brush; the
1304 // geometry is resolved by the one function a helper resolves it by
1305 // too, so a helper's run and the walk's own can never differ.
1306 Command::FillRect {
1307 brush, transform, ..
1308 }
1309 | Command::RoundedRect {
1310 brush, transform, ..
1311 }
1312 | Command::Line {
1313 brush, transform, ..
1314 }
1315 | Command::Path {
1316 brush, transform, ..
1317 } => {
1318 if let Some(geometry) = Geometry::of(command, combined) {
1319 self.draw_shape(
1320 frame,
1321 depth,
1322 PaintSource::Brush(brush),
1323 combined * *transform,
1324 geometry,
1325 claim,
1326 );
1327 }
1328 }
1329 Command::PushClip { rect, transform } => {
1330 let transform = combined * *transform;
1331 self.clips.push_rect(*rect, transform, &mut self.generator);
1332 self.groups.push_clip(transform.transform_rect_bbox(*rect));
1333 }
1334 Command::PushClipRounded {
1335 rect,
1336 radii,
1337 transform,
1338 } => {
1339 let transform = combined * *transform;
1340 self.clips.push_rounded(
1341 *rect,
1342 rounded_rect_radii(*radii),
1343 transform,
1344 &mut self.generator,
1345 );
1346 self.groups.push_clip(transform.transform_rect_bbox(*rect));
1347 }
1348 // An outline is always a coverage mask, never a layer page — so a
1349 // `PushRetained` body opened under it finds the mask open and is
1350 // marked unretainable on exactly a rounded clip's terms (see
1351 // [`Self::open_retained`]). The group is confined to the outline's
1352 // device bounding box, which is what a clear hoisted past it is
1353 // limited to.
1354 Command::PushClipPath { path, transform } => {
1355 let transform = combined * *transform;
1356 self.clips.push_path(path, transform, &mut self.generator);
1357 self.groups
1358 .push_clip(transform.transform_rect_bbox(path.bounding_box()));
1359 }
1360 Command::PushLayer {
1361 rect,
1362 alpha,
1363 transform,
1364 } => {
1365 self.open_layer(frame, *rect, *alpha, combined * *transform);
1366 }
1367 // One bracket stack serves all three kinds, so whichever pop
1368 // arrives closes the innermost open bracket (see [`layers`]). A pop
1369 // with nothing open is ignored: an unbalanced widget tree must not
1370 // be able to lift a bracket a sibling still relies on.
1371 Command::PopClip | Command::PopLayer => self.close_group(frame),
1372 Command::ClearRect { rect, transform } => {
1373 // A punch is hoisted out of every bracket to the frame root, so
1374 // a page could never carry it: a body that punches is drawn
1375 // inline every frame.
1376 self.mark_retained_body_unretainable(frame);
1377 let transform = combined * *transform;
1378 // Hoisted here rather than at the end of the frame because
1379 // this is the only point the brackets confining it are still
1380 // open; its coverage is generated once the frame's draws are
1381 // done (see [`clear`]).
1382 let punch = clear::punch_rect(*rect, transform, self.groups.bounds());
1383 if let Some(device) = punch {
1384 self.punches.push(StagedPunch {
1385 device,
1386 depth: depth.advance(),
1387 });
1388 }
1389 }
1390 Command::PushRetained {
1391 key,
1392 rect,
1393 alpha,
1394 scale,
1395 transform,
1396 } => {
1397 let mut listed = false;
1398 if self.retained_stack.enter(*rect, *scale, *transform) {
1399 // The bracket's own correction is the one that applies to
1400 // the layer it opens, so the transform is recomposed here
1401 // rather than reusing `combined` from before the entry.
1402 let corrected = root * self.retained_stack.correction() * *transform;
1403 if check_finite(corrected).is_ok() {
1404 if self.retain {
1405 self.open_retained(frame, *key, *rect, *alpha, corrected);
1406 listed = true;
1407 } else if *alpha < 1.0 {
1408 self.open_layer(frame, *rect, *alpha, corrected);
1409 self.retained_stack.record_layer(self.groups.depth());
1410 }
1411 }
1412 }
1413 // A nested bracket is painted as part of the outermost body,
1414 // so its own key's page receives nothing this frame.
1415 if !listed {
1416 self.supersede_retained(frame, *key);
1417 }
1418 }
1419 Command::PopRetained => {
1420 let closes = self.retained_stack.leave(self.groups.depth());
1421 if closes {
1422 self.close_group(frame);
1423 }
1424 // The outermost bracket just ended. A body whose own group a
1425 // stray pop inside it already closed drew its tail outside the
1426 // layer, so the layer is not the body and is never retained.
1427 if !self.retained_stack.is_open()
1428 && let Some(index) = self.retained_open.take()
1429 && !closes
1430 {
1431 mark_unretainable(frame, index);
1432 }
1433 }
1434 // Refused by the up-front walk in [`Self::compile`] when retention
1435 // is off, so it reaches the lowering only with a store behind it.
1436 Command::DrawRetained {
1437 key,
1438 rect,
1439 transform,
1440 alpha,
1441 scale,
1442 } => {
1443 if self.retain {
1444 let mapped = combined * *transform * Affine::scale_about(*scale, rect.center());
1445 self.record_draw_retained(frame, depth, *key, *rect, *alpha, mapped);
1446 }
1447 }
1448 // An image is its destination rectangle's coverage under an image
1449 // paint — the same two rectangle paths a solid fill takes, so a
1450 // pixel-aligned image costs no flattening either.
1451 Command::Image {
1452 data,
1453 dest,
1454 transform,
1455 } => {
1456 if let Some(geometry) = Geometry::of(command, combined) {
1457 let source = PaintSource::Image { data, dest: *dest };
1458 self.draw_shape(frame, depth, source, combined * *transform, geometry, claim);
1459 }
1460 }
1461 // The strip generator rasterizes the padded bounding rectangle,
1462 // not `rect` itself and not a rounded shape — see [`blur_rrect`]'s
1463 // module doc for why. It takes the same fast rectangle path a fill
1464 // or an image does whenever that padded rectangle lands
1465 // pixel-aligned under the composed transform.
1466 Command::BlurredRoundedRect {
1467 rect,
1468 radii,
1469 std_dev,
1470 color,
1471 transform,
1472 } => {
1473 if let Some(geometry) = Geometry::of(command, combined) {
1474 let source = PaintSource::BlurredRect {
1475 rect: *rect,
1476 radii: *radii,
1477 std_dev: *std_dev,
1478 color: *color,
1479 };
1480 self.draw_shape(frame, depth, source, combined * *transform, geometry, claim);
1481 }
1482 }
1483 Command::GlyphRun(run) => {
1484 // Two clock reads a run, free without `perf-trace` (see
1485 // [`PhaseClock`]); the accumulated total is a subset of the
1486 // walk this arm runs inside.
1487 let mut clock = PhaseClock::start();
1488 self.compile_glyph_run(run, combined * run.transform, route, frame, depth);
1489 frame.compile_spans.glyphs = frame.compile_spans.glyphs.saturating_add(clock.lap());
1490 }
1491 // A fragment program's own pixels were produced before the frame
1492 // was compiled, into a texture registered under an id derived from
1493 // the program alone (`crate::effects::shader_quad`). From here on
1494 // the quad is an external texture like any other — the pre-pass is
1495 // the only thing that distinguishes it.
1496 Command::ShaderQuad {
1497 program,
1498 dest,
1499 transform,
1500 ..
1501 } => {
1502 // A fragment program's pixels change with no command changing,
1503 // so a body drawing one is never retained.
1504 self.mark_retained_body_unretainable(frame);
1505 if config::shader_effects_disabled() {
1506 note_shader_effects_disabled();
1507 return;
1508 }
1509 let id = shader_quad_texture_id(program.id());
1510 if self.externals.get(id).is_none() {
1511 // No pre-pass ran for this program, its shader failed to
1512 // compile, or this exact quad was deliberately culled by
1513 // the pre-pass's own target-extent check (see
1514 // `shader_quad_is_culled`) — an expected, routine outcome
1515 // told apart from the other two so it is never reported
1516 // through the missing-pre-pass warning below.
1517 if shader_quad_is_culled(*dest, combined * *transform, self.frame_extent) {
1518 note_shader_quad_culled(program.id());
1519 } else {
1520 // Reported in its own words rather than as an
1521 // unregistered scene texture, whose id would name
1522 // nothing a reader could look up.
1523 note_shader_quad_unrendered(program.id());
1524 }
1525 frame.skipped_externals = frame.skipped_externals.saturating_add(1);
1526 return;
1527 }
1528 self.draw_external_texture(id, *dest, combined * *transform, frame, depth);
1529 }
1530 Command::SceneTexture {
1531 id,
1532 dest,
1533 transform,
1534 } => {
1535 // An externally bound texture's pixels change with no command
1536 // changing (a camera, a video, a 3D pass), so a body drawing
1537 // one is never retained.
1538 self.mark_retained_body_unretainable(frame);
1539 self.draw_external_texture(*id, *dest, combined * *transform, frame, depth);
1540 }
1541 Command::BackdropBlur {
1542 rect,
1543 radius,
1544 transform,
1545 } => {
1546 // A page is rasterized offscreen, where there is no frame
1547 // beneath the body to sample, so a body blurring its backdrop
1548 // is drawn inline every frame — on exactly a punch's terms.
1549 self.mark_retained_body_unretainable(frame);
1550 self.record_backdrop(frame, depth, *rect, *radius, combined * *transform);
1551 }
1552 }
1553 }
1554
1555 /// Record a `Command::BackdropBlur`: a layer of its own holding the
1556 /// rectangle's coverage, listed in [`CompiledFrame::backdrops`].
1557 ///
1558 /// The layer is what places the backdrop at its painter-order position in
1559 /// the recording — the node the scheduler cuts the frame's surface round
1560 /// at — and it opens no clip and composites at full opacity, so a
1561 /// scheduler that served it as an ordinary layer would inline it. Its one
1562 /// draw is the rectangle generated under the clip stack exactly like a
1563 /// fill's, so a scissor trims it and a coverage mask (a rounded or path
1564 /// clip) masks it; it is painted transparent, because what it really
1565 /// paints — the blurred page — is the renderer's to bind once the page
1566 /// exists. A rectangle producing no coverage records the empty layer and
1567 /// no backdrop, which then costs nothing anywhere.
1568 fn record_backdrop(
1569 &mut self,
1570 frame: &mut CompiledFrame,
1571 depth: &mut DepthCounter,
1572 rect: Rect,
1573 radius: f64,
1574 transform: Affine,
1575 ) {
1576 let Some(sigma) = backdrop_sigma(radius, transform) else {
1577 return;
1578 };
1579 let (width, height) = self.frame_extent;
1580 let Some(region) =
1581 backdrop_region(transform.transform_rect_bbox(rect), sigma, (width, height))
1582 else {
1583 return;
1584 };
1585
1586 let layer = u32::try_from(frame.recorder.layers.len()).unwrap_or(u32::MAX);
1587 frame.recorder.push_layer(layers::layer_props(1.0), None);
1588 let transparent = Brush::Solid(Color::TRANSPARENT);
1589 let recorded = self.record(
1590 frame,
1591 depth,
1592 PaintSource::Brush(&transparent),
1593 transform,
1594 Geometry::rect(rect, transform),
1595 None,
1596 );
1597 frame.recorder.pop_layer();
1598
1599 if recorded && let Some(draw) = frame.draws().last() {
1600 frame.backdrops.push(Backdrop {
1601 layer,
1602 depth: draw.depth,
1603 region,
1604 sigma,
1605 });
1606 }
1607 }
1608
1609 /// Record the texture bound under `id` scaled to fill `dest` under
1610 /// `transform` — the body both [`Command::SceneTexture`] and
1611 /// [`Command::ShaderQuad`] lower to, since the only thing separating them
1612 /// is where the texels came from.
1613 ///
1614 /// An externally owned texture is its destination rectangle's coverage
1615 /// under an image paint that samples the caller's texture rather than the
1616 /// atlas — the same two rectangle paths [`Command::Image`] takes, so a
1617 /// pixel-aligned one costs no flattening either.
1618 fn draw_external_texture(
1619 &mut self,
1620 id: u64,
1621 dest: Rect,
1622 transform: Affine,
1623 frame: &mut CompiledFrame,
1624 depth: &mut DepthCounter,
1625 ) {
1626 let source = PaintSource::SceneTexture { id, dest };
1627 let geometry = Geometry::rect(dest, transform);
1628 self.draw_shape(frame, depth, source, transform, geometry, None);
1629 }
1630
1631 /// Route every glyph run the scene records, in recording order.
1632 ///
1633 /// The whole of the frame's collect phase. It is a walk of its own rather
1634 /// than a question asked inside the draw walk because the answer for one
1635 /// run depends on what the *font* has been drawn at recently, and a policy
1636 /// that learned a size only as it drew it would route the first run of a
1637 /// changing frame as settled and the second as animating — the two halves
1638 /// of one line of text taking different paths.
1639 ///
1640 /// Every run is classified, including ones the draw walk will refuse: the
1641 /// refusals it makes (an empty run, a blocked clip, an unreadable face)
1642 /// are not size observations, and a size drawn on a frame is a size drawn
1643 /// on that frame whatever else happens to it.
1644 ///
1645 /// The walk carries a [`RetainedStack`] of its own for one reason: a run's
1646 /// route depends on the scale in its *device* transform (see
1647 /// [`crate::text::atlas_policy`]), and inside a `PushRetained` bracket that
1648 /// transform carries the bracket's presentation scale as well as the frame
1649 /// root. Classifying against `root * run.transform` alone would answer for
1650 /// a size the run is not drawn at. Only the correction is tracked here —
1651 /// the bracket's *layers* are the draw walk's to open, and this walk opens
1652 /// nothing.
1653 ///
1654 /// A bracket is entered on the draw walk's exact terms, through the same
1655 /// [`command_on_grid`] check and the same [`retained_entry`] substitution
1656 /// it uses, so the correction the two walks carry is one decision made
1657 /// twice rather than two decisions that happen to agree.
1658 fn classify_runs(&mut self, scene: &Scene, root: Affine) {
1659 self.run_routes.clear();
1660 self.next_run = 0;
1661
1662 let mut retained_stack = RetainedStack::new();
1663 for command in scene.commands() {
1664 match command {
1665 Command::PushRetained {
1666 rect,
1667 scale,
1668 transform,
1669 ..
1670 } => {
1671 // The draw walk's own entry, made here on exactly its
1672 // terms: [`command_on_grid`] is the check it asks and
1673 // [`retained_entry`] is the substitution it makes. A
1674 // bracket it enters neutrally installs no correction, so a
1675 // walk that entered it with the recorded pair would
1676 // classify every run inside against a device transform
1677 // nothing is ever drawn through — a route decided for one
1678 // magnitude and a draw made at another.
1679 let combined = root * retained_stack.correction();
1680 let on_grid = command_on_grid(command, combined);
1681 let (scale, transform) = retained_entry(on_grid, *scale, *transform);
1682 retained_stack.enter(*rect, scale, transform);
1683 }
1684 Command::PopRetained => {
1685 // The group depth a real close would be tested against is
1686 // the draw walk's; nothing here closes a group, so zero is
1687 // the honest answer and the return value is unused.
1688 retained_stack.leave(0);
1689 }
1690 Command::GlyphRun(run) => {
1691 // The context colour `glifo` would resolve a COLR layer
1692 // against — the run's own brush when it is solid, black
1693 // otherwise, which is the same answer
1694 // `EngineGlyphSink::get_context_color` gives it.
1695 let context_color = match context_paint(&run.brush) {
1696 vello_common::paint::PaintType::Solid(color) => color,
1697 _ => peniko::color::palette::css::BLACK,
1698 };
1699 let key = RunKey::for_run(
1700 run,
1701 root * retained_stack.correction() * run.transform,
1702 self.hint_text,
1703 self.font_facts.of(run.font.font()).color,
1704 context_color,
1705 &mut self.run_glyph_ids,
1706 );
1707 let route = self.glyph_atlas.classify_run(&key);
1708 self.run_routes.push(route);
1709 }
1710 _ => {}
1711 }
1712 }
1713 }
1714
1715 /// The next run's route, or `None` once the collect walk's answers are
1716 /// exhausted.
1717 ///
1718 /// `None` is the conservative answer rather than an error: a run with no
1719 /// recorded route is drawn as outlines, which is correct pixels by the path
1720 /// the engine has always used.
1721 ///
1722 /// The route is re-tested against *live* glyph residency on the way out
1723 /// (see [`crate::text::atlas_policy::AtlasPolicy::admit_run`]). The collect
1724 /// walk answered every run of this frame from the population the frame
1725 /// opened with, because `glifo` inserts nothing until the draw walk reaches
1726 /// the run; without this second test a frame one entry below the budget
1727 /// would admit every run it carries and overshoot by as much as one frame's
1728 /// whole text. Here the population is the real one — every earlier run of
1729 /// this same frame has already inserted, and the re-test charges those
1730 /// insertions before it answers — so the bound holds within a frame and not
1731 /// merely across frames. It can only ever *narrow* an answer, which is the
1732 /// outline path: correct pixels, and the only direction that is safe to
1733 /// decide late.
1734 fn take_run_route(&mut self) -> Option<RunRoute> {
1735 let route = self.run_routes.get(self.next_run).copied();
1736 self.next_run = self.next_run.saturating_add(1);
1737 route.map(|route| self.glyph_atlas.admit_run(route))
1738 }
1739
1740 /// Draw one glyph run: its brush encoded once, then every glyph's outline
1741 /// rasterized under the active clip (see [`crate::text`]).
1742 ///
1743 /// `transform` is the run's own transform composed with the frame root.
1744 /// The brush is encoded against it once for the whole run rather than once
1745 /// per glyph, because that transform *is* the paint's placement — a glyph
1746 /// moves the outline, never the paint behind it — so a gradient-brushed
1747 /// line of text costs one encoded entry and one colour ramp.
1748 ///
1749 /// A run whose brush cannot be encoded draws nothing, on the same terms an
1750 /// image draw the atlas refuses does: the refusal is already counted in
1751 /// [`CompiledFrame::skipped_images`] by the encoding, and every glyph in
1752 /// the run simply goes missing rather than being painted with a
1753 /// substitute. An image-brushed run is likewise counted as the one image
1754 /// draw its single encoding is, not as one per glyph.
1755 ///
1756 /// A run whose font cannot be read is refused the same way and for a
1757 /// harder reason: the text backend's font gate is what keeps a blob that
1758 /// is not a font off the frame path at all (see [`crate::text`]).
1759 fn compile_glyph_run(
1760 &mut self,
1761 run: &GlyphRun,
1762 transform: Affine,
1763 route: Option<RunRoute>,
1764 frame: &mut CompiledFrame,
1765 depth: &mut DepthCounter,
1766 ) {
1767 // All three checked before the brush is encoded, so a run that can
1768 // draw nothing leaves no orphan entry in the frame's encoded-paint
1769 // table and no ramp request for a gradient nothing paints with — the
1770 // same rule [`record`](Self::record) keeps for a shape.
1771 if run.glyphs.is_empty() || self.clips.blocks_everything() {
1772 return;
1773 }
1774 if !self.font_facts.of(run.font.font()).readable {
1775 note_font_skip();
1776 let glyphs = u32::try_from(run.glyphs.len()).unwrap_or(u32::MAX);
1777 frame.skipped_glyphs = frame.skipped_glyphs.saturating_add(glyphs);
1778 return;
1779 }
1780
1781 let Some(paint) = self.encode_paint(PaintSource::Brush(&run.brush), transform, frame)
1782 else {
1783 return;
1784 };
1785
1786 // Read out ahead of the destructure below: `hint_text` is `Copy`, and
1787 // reading it through `self` after the destructure moved out its other
1788 // fields would fight the borrow checker for no reason.
1789 let hint_text = self.hint_text;
1790 let glyph_run_cache = self.glyph_run_cache;
1791 // Destructured rather than passed as `self`, because the sink borrows
1792 // the generator and the clip stack mutably while the glyph caches, the
1793 // entry map and the shared allocator are borrowed mutably alongside
1794 // them — five disjoint fields of one struct.
1795 let Self {
1796 generator,
1797 clips,
1798 groups,
1799 glyphs,
1800 images,
1801 glyph_atlas,
1802 glyph_quads,
1803 glyph_quad_scratch,
1804 ..
1805 } = self;
1806 // The policy's decision, turned into the borrow `glifo` caches
1807 // through. A run it refused reaches `glifo` with no cache at all, which
1808 // is the outline path unchanged rather than a cache that declines every
1809 // lookup — those are the same pixels but not the same work.
1810 let atlas = match route {
1811 Some(RunRoute::Atlas(_)) => Some((glyph_atlas, images.allocator_mut())),
1812 Some(RunRoute::Outline(_)) | None => None,
1813 };
1814 // Quads only for a run the atlas serves, and never under a coverage
1815 // mask: a quad's rectangle can be trimmed to the scissor but cannot
1816 // carry a mask (see [`crate::text`]'s backend).
1817 let quads =
1818 (*glyph_quads && atlas.is_some() && clips.mask().is_none()).then(|| GlyphQuadTargets {
1819 scratch: glyph_quad_scratch,
1820 window: quad_window(generator, groups),
1821 });
1822 let outcome = lower_glyph_run(
1823 run,
1824 transform,
1825 paint,
1826 &run.brush,
1827 hint_text,
1828 atlas,
1829 glyph_run_cache,
1830 GlyphRunTargets {
1831 generator,
1832 clips,
1833 prep: glyphs,
1834 frame,
1835 depth,
1836 quads,
1837 },
1838 );
1839
1840 frame.glyph_draws = frame.glyph_draws.saturating_add(outcome.drawn);
1841 frame.skipped_glyphs = frame.skipped_glyphs.saturating_add(outcome.skipped);
1842 if outcome.resident {
1843 frame.resident_glyph_runs = frame.resident_glyph_runs.saturating_add(1);
1844 }
1845 }
1846
1847 /// Open a layer bracket: its rectangle's clip, and — below full opacity —
1848 /// a recorded layer for the scheduler to give a page of its own.
1849 ///
1850 /// The clip and the bracket are pushed together and unconditionally, which
1851 /// is what keeps the two stacks in step for [`close_group`](Self::close_group).
1852 fn open_layer(&mut self, frame: &mut CompiledFrame, rect: Rect, alpha: f32, transform: Affine) {
1853 self.clips.push_rect(rect, transform, &mut self.generator);
1854
1855 let isolated = layers::lower_layer(alpha) == LayerLowering::Isolated;
1856 if isolated {
1857 frame.recorder.push_layer(layers::layer_props(alpha), None);
1858 }
1859 self.groups
1860 .push_layer(transform.transform_rect_bbox(rect), isolated);
1861 }
1862
1863 /// Open the outermost `PushRetained` bracket of a compiler with retention
1864 /// on: the same lowering the inline emulation gives it, recorded as a
1865 /// layer whatever its alpha, and listed for the retained-page store.
1866 ///
1867 /// Below full opacity that is exactly [`Self::open_layer`]. At full opacity
1868 /// the inline emulation opens no layer and clips nothing, so the layer
1869 /// opened here carries no clip either ([`GroupStack::push_retained`]) — the
1870 /// body's draws are the inline lowering's draws, gathered — and the store
1871 /// that refuses it leaves the scheduler a full-opacity layer it inlines.
1872 ///
1873 /// Whether the body can be retained at all is measured here, before the
1874 /// bracket's own group narrows anything: a page can only hold what the
1875 /// body really drew, so a body reaching past the viewport or a scissor
1876 /// around it, or under a coverage mask, would be retained missing what was
1877 /// clipped away — and composited somewhere else on a later frame, the loss
1878 /// would show.
1879 fn open_retained(
1880 &mut self,
1881 frame: &mut CompiledFrame,
1882 key: u64,
1883 rect: Rect,
1884 alpha: f32,
1885 transform: Affine,
1886 ) {
1887 let window = quad_window(&self.generator, &self.groups);
1888 let device = transform.transform_rect_bbox(rect);
1889 let retainable = self.clips.mask().is_none()
1890 && !self.clips.blocks_everything()
1891 && rect_within(device, window);
1892 // The id `push_layer` is about to hand the layer, in either branch.
1893 let layer = u32::try_from(frame.recorder.layers.len()).unwrap_or(u32::MAX);
1894
1895 if alpha < 1.0 {
1896 self.open_layer(frame, rect, alpha, transform);
1897 } else {
1898 frame.recorder.push_layer(layers::layer_props(alpha), None);
1899 self.groups.push_retained();
1900 }
1901 self.retained_stack.record_layer(self.groups.depth());
1902
1903 let placement = RetainedPlacement::of(transform);
1904 frame.retained.push(RetainedBracket {
1905 key,
1906 layer,
1907 opacity: alpha,
1908 placement,
1909 extent_px: placement.extent(rect),
1910 kind: RetainedKind::Record {
1911 rect: tile_bounds(device),
1912 retainable,
1913 },
1914 });
1915 self.retained_open = Some(frame.retained.len() - 1);
1916 }
1917
1918 /// Record a `DrawRetained` of a compiler with retention on: an empty layer
1919 /// at the command's painter-order position, standing for the composite the
1920 /// retained-page store resolves it to, plus what the store needs to place
1921 /// that composite.
1922 ///
1923 /// `mapped` is the body's local space mapped onto the device grid with the
1924 /// presentation scale folded in — the same composition a `PushRetained`
1925 /// bracket's correction produces for its body. A non-finite one draws
1926 /// nothing, the same answer every off-grid command gets.
1927 fn record_draw_retained(
1928 &mut self,
1929 frame: &mut CompiledFrame,
1930 depth: &mut DepthCounter,
1931 key: u64,
1932 rect: Rect,
1933 alpha: f32,
1934 mapped: Affine,
1935 ) {
1936 if check_finite(mapped).is_err() {
1937 return;
1938 }
1939 let window = quad_window(&self.generator, &self.groups);
1940 let masked = self.clips.mask().is_some();
1941 let layer = u32::try_from(frame.recorder.layers.len()).unwrap_or(u32::MAX);
1942 // Pushed and popped at once: a layer with no draws of its own, whose
1943 // node is what puts the composite at this point of its stream.
1944 frame.recorder.push_layer(layers::layer_props(alpha), None);
1945 frame.recorder.pop_layer();
1946
1947 let placement = RetainedPlacement::of(mapped);
1948 frame.retained.push(RetainedBracket {
1949 key,
1950 layer,
1951 opacity: alpha,
1952 placement,
1953 extent_px: placement.extent(rect),
1954 kind: RetainedKind::Draw {
1955 depth: depth.advance(),
1956 window: if self.clips.blocks_everything() {
1957 RectU16::new(0, 0, 0, 0)
1958 } else {
1959 window
1960 },
1961 masked,
1962 },
1963 });
1964 }
1965
1966 /// List `key`, a `PushRetained` this frame carries but records no bracket
1967 /// for, in [`CompiledFrame::retained_superseded`] — with retention on;
1968 /// without it there is no store to evict from.
1969 fn supersede_retained(&self, frame: &mut CompiledFrame, key: u64) {
1970 if self.retain {
1971 frame.retained_superseded.push(key);
1972 }
1973 }
1974
1975 /// Mark the retained body the walk is inside, if any, as one no page may
1976 /// hold.
1977 fn mark_retained_body_unretainable(&mut self, frame: &mut CompiledFrame) {
1978 if let Some(index) = self.retained_open {
1979 mark_unretainable(frame, index);
1980 }
1981 }
1982
1983 /// Open a bracket that admits nothing, for a push whose transform does not
1984 /// land on the device grid.
1985 ///
1986 /// A degenerate rectangle under the identity, rather than the push's own
1987 /// geometry under its own transform: the point is to reach an empty
1988 /// scissor without handing the flattener a transform it cannot subdivide
1989 /// against, which is the very thing that made this push unusable.
1990 fn open_blocked_group(&mut self) {
1991 self.clips
1992 .push_rect(Rect::ZERO, Affine::IDENTITY, &mut self.generator);
1993 self.groups.push_clip(Rect::ZERO);
1994 }
1995
1996 /// Close the innermost open bracket, undoing exactly what opened it.
1997 ///
1998 /// The recording is only popped when this bracket is the one that pushed
1999 /// it *and* the recording agrees a layer is open — the recorder's own pop
2000 /// panics on an empty layer stack, and the frame path returns errors
2001 /// rather than panicking (E17).
2002 fn close_group(&mut self, frame: &mut CompiledFrame) {
2003 let Some(group) = self.groups.pop() else {
2004 return;
2005 };
2006 if group.closes_clip() {
2007 self.clips.pop();
2008 }
2009 if group.closes_layer() && frame.recorder.has_layers() {
2010 frame.recorder.pop_layer();
2011 }
2012 }
2013
2014 /// Close every bracket the display list left open at the end of the frame.
2015 ///
2016 /// A recorded layer that is never popped has no bounds — the recorder
2017 /// computes them at the pop — so an unbalanced push would otherwise leave
2018 /// the scheduler a layer it cannot place. Closing here is the same policy
2019 /// an unbalanced pop gets, applied at the other end.
2020 fn close_open_groups(&mut self, frame: &mut CompiledFrame) {
2021 while !self.groups.is_empty() {
2022 self.close_group(frame);
2023 }
2024 self.retained_stack.reset();
2025 // A body still open at the end of the frame never reached its own
2026 // `PopRetained`, so where it would have ended is unknown.
2027 if let Some(index) = self.retained_open.take() {
2028 mark_unretainable(frame, index);
2029 }
2030 }
2031
2032 /// Generate the coverage for every punch the frame hoisted.
2033 ///
2034 /// Runs after the walk, so the strips land past every draw's own range and
2035 /// no draw references them. The punch is rasterized at the frame root
2036 /// under no clip at all — being hoisted out of its brackets is exactly
2037 /// what the confinement in [`clear::punch_rect`] already accounted for —
2038 /// and takes the fast rectangle path whenever its edges fall on whole
2039 /// pixels, which is what makes a pixel-aligned punch pixel-exact however
2040 /// its edges fall inside a tile.
2041 fn generate_punches(&mut self, frame: &mut CompiledFrame) {
2042 for index in 0..self.punches.len() {
2043 let Some(punch) = self.punches.get(index).copied() else {
2044 continue;
2045 };
2046
2047 let start = frame.strips.strips.len();
2048 match fast_rect(punch.device, Affine::IDENTITY) {
2049 Some(device) => {
2050 self.generator
2051 .generate_filled_rect_fast(&device, &mut frame.strips, None);
2052 }
2053 None => {
2054 self.generator.generate_filled_path(
2055 punch.device.path_elements(FLATTEN_TOLERANCE),
2056 Fill::NonZero,
2057 Affine::IDENTITY,
2058 None,
2059 &mut frame.strips,
2060 None,
2061 );
2062 }
2063 }
2064
2065 let strip_range = start..frame.strips.strips.len();
2066 if strip_range.is_empty() {
2067 continue;
2068 }
2069
2070 frame.clears.push(ClearPunch {
2071 strip_range,
2072 bounds: clear::device_bounds(punch.device),
2073 depth: punch.depth,
2074 });
2075 }
2076 }
2077
2078 /// [`record`](Self::record) `geometry`, counting the draw as a fast
2079 /// rectangle when it took that path (see [`fast_rect`]).
2080 fn draw_shape(
2081 &mut self,
2082 frame: &mut CompiledFrame,
2083 depth: &mut DepthCounter,
2084 source: PaintSource<'_>,
2085 transform: Affine,
2086 geometry: Geometry<'_>,
2087 claim: Option<Claim<'_>>,
2088 ) {
2089 let fast = geometry.is_fast_rect();
2090 if self.record(frame, depth, source, transform, geometry, claim) && fast {
2091 frame.fast_rect_draws = frame.fast_rect_draws.saturating_add(1);
2092 }
2093 }
2094
2095 /// Generate `geometry` under the active clip, then record whatever strips
2096 /// survived as one draw painted from `source` under `transform`.
2097 ///
2098 /// The clip stack's coverage mask is handed to the strip generator, which
2099 /// is what intersects a mask clip while the draw's own coverage is
2100 /// produced; the scissor is applied afterwards, to the run the generator
2101 /// appended. A scissor admitting nothing skips generation entirely rather
2102 /// than generating coverage to throw away.
2103 ///
2104 /// `claim` is the command's helper-generated run, when one was planned
2105 /// (see [`parallel`]). It stands in for generating only while no mask is
2106 /// open — a helper generates unclipped — and only when a helper actually
2107 /// got to it; otherwise the run is generated here exactly as it would be
2108 /// without one. Everything after generation is the same either way.
2109 ///
2110 /// Returns whether a draw was recorded. A generator call that produced no
2111 /// strips (fully culled, clipped away, degenerate, or empty geometry)
2112 /// records nothing and consumes no depth, so a frame's depths stay dense
2113 /// over the draws that actually exist.
2114 ///
2115 /// The paint is encoded only once the strips are known to be non-empty, so
2116 /// a culled draw leaves no orphan entry in the frame's encoded-paint table
2117 /// and no ramp request for a gradient nothing paints with. An image the
2118 /// atlas refuses arrives *after* that point, so its coverage is rolled back
2119 /// to where the generator started rather than left behind as strips no draw
2120 /// references.
2121 fn record(
2122 &mut self,
2123 frame: &mut CompiledFrame,
2124 depth: &mut DepthCounter,
2125 source: PaintSource<'_>,
2126 transform: Affine,
2127 geometry: Geometry<'_>,
2128 claim: Option<Claim<'_>>,
2129 ) -> bool {
2130 if self.clips.blocks_everything() {
2131 return false;
2132 }
2133
2134 let start = frame.strips.strips.len();
2135 let alpha_start = frame.strips.alphas.len();
2136 let mask = self.clips.mask();
2137 let merged =
2138 mask.is_none() && claim.is_some_and(|claim| claim.append_to(&mut frame.strips));
2139 if !merged {
2140 geometry.generate(&mut self.generator, &mut frame.strips, mask);
2141 }
2142 self.clips.clip_run(&mut frame.strips, start, alpha_start);
2143 let strip_range = start..frame.strips.strips.len();
2144
2145 // A run is only a draw when it carries content. Under a mask clip
2146 // `vello_common::clip::intersect_impl` gates its trailing sentinel on
2147 // the *whole* target buffer being non-empty, not on what this call
2148 // added — and `frame.strips` is one `Append`-mode buffer shared by
2149 // every draw of the frame, so a masked path that contributed no rows
2150 // (zero-area geometry, coverage the mask removed entirely) after an
2151 // earlier draw's content still gets a sentinel: a *lone* sentinel.
2152 // The renderer's pairwise walk reads each span's extent off the strip
2153 // after it, so that is not a run at all — roll it back exactly like an
2154 // empty one. Every generation path pairs a content strip with its own
2155 // sentinel in the same call, so a one-strip run can only be that
2156 // sentinel; the assertion keeps a future generator change from being
2157 // swallowed here as "nothing to draw".
2158 if strip_range.len() < 2 {
2159 debug_assert!(
2160 strip_range.is_empty() || frame.strips.strips[start].is_sentinel(),
2161 "a one-strip run must be a lone sentinel, not an unterminated content strip"
2162 );
2163 frame.strips.strips.truncate(start);
2164 frame.strips.alphas.truncate(alpha_start);
2165 return false;
2166 }
2167
2168 let Some(paint) = self.encode_paint(source, transform, frame) else {
2169 frame.strips.strips.truncate(start);
2170 frame.strips.alphas.truncate(alpha_start);
2171 return false;
2172 };
2173
2174 let draw = EngineDraw::new(paint, depth.advance(), strip_range.clone());
2175 frame
2176 .recorder
2177 .push_draw(draw, &frame.strips.strips[strip_range]);
2178 if merged {
2179 frame.parallel_draws = frame.parallel_draws.saturating_add(1);
2180 }
2181 true
2182 }
2183
2184 /// Encode `source` into the paint a draw carries, or `None` when the paint
2185 /// cannot be resolved and the draw is to be dropped.
2186 ///
2187 /// A solid and a gradient are always encodable (a degenerate gradient falls
2188 /// back to a solid). The two that can answer `None` are an image, which
2189 /// needs atlas space the residency may refuse, and an externally bound
2190 /// texture, whose id may name nothing registered.
2191 fn encode_paint(
2192 &mut self,
2193 source: PaintSource<'_>,
2194 transform: Affine,
2195 frame: &mut CompiledFrame,
2196 ) -> Option<vello_common::paint::Paint> {
2197 let encoded = match source {
2198 PaintSource::Brush(Brush::Image(brush)) => encode_image_brush(
2199 brush,
2200 transform,
2201 &mut frame.encoded_paints,
2202 &mut self.images,
2203 ),
2204 PaintSource::Brush(brush) => {
2205 let encoding = encode_brush(brush, transform, &mut frame.encoded_paints);
2206 frame.lut_requests.extend(encoding.lut_request);
2207 return Some(encoding.paint);
2208 }
2209 PaintSource::Image { data, dest } => encode_image_command(
2210 data,
2211 dest,
2212 transform,
2213 &mut frame.encoded_paints,
2214 &mut self.images,
2215 ),
2216 PaintSource::SceneTexture { id, dest } => {
2217 // Its own error type and its own counters, so it returns here
2218 // rather than joining the atlas-residency match below: nothing
2219 // is made resident and nothing is uploaded — the texels are
2220 // the caller's and are already on the device.
2221 return match encode_scene_texture(
2222 id,
2223 dest,
2224 transform,
2225 &mut self.externals,
2226 &mut frame.encoded_paints,
2227 ) {
2228 Ok(encoding) => {
2229 frame.external_draws = frame.external_draws.saturating_add(1);
2230 Some(encoding.paint)
2231 }
2232 Err(_) => {
2233 frame.skipped_externals = frame.skipped_externals.saturating_add(1);
2234 None
2235 }
2236 };
2237 }
2238 PaintSource::BlurredRect {
2239 rect,
2240 radii,
2241 std_dev,
2242 color,
2243 } => {
2244 // Unlike an image, this can never be refused (see
2245 // [`encode_blurred_rounded_rect`]'s doc), so it returns
2246 // straight away rather than joining the fallible match below.
2247 let paint = encode_blurred_rounded_rect(
2248 rect,
2249 radii,
2250 std_dev,
2251 color,
2252 transform,
2253 &mut frame.encoded_paints,
2254 );
2255 return Some(paint);
2256 }
2257 };
2258
2259 match encoded {
2260 Ok(encoding) => {
2261 frame.image_draws = frame.image_draws.saturating_add(1);
2262 Some(encoding.paint)
2263 }
2264 Err(skip) => {
2265 frame.skipped_images = frame.skipped_images.saturating_add(1);
2266 note_image_skip(skip);
2267 None
2268 }
2269 }
2270 }
2271}
2272
2273/// What a draw is painted with, as the walk hands it to
2274/// [`SceneCompiler::record`].
2275///
2276/// An image is not a [`Brush`] in the display list — [`Command::Image`] carries
2277/// its pixels and a destination rectangle directly — so the two arrive by
2278/// different routes and are distinguished here rather than by forcing one into
2279/// the shape of the other.
2280enum PaintSource<'a> {
2281 /// A solid, gradient or image brush recorded on a shape command.
2282 Brush(&'a Brush),
2283 /// A [`Command::Image`]'s pixels scaled to fill `dest`.
2284 Image {
2285 /// The decoded image to make resident.
2286 data: &'a ImageData,
2287 /// The destination rectangle, in the command's own coordinate space.
2288 dest: Rect,
2289 },
2290 /// A [`Command::SceneTexture`]'s externally bound texture scaled to fill
2291 /// `dest`.
2292 SceneTexture {
2293 /// The opaque id the display list names the texture by.
2294 id: u64,
2295 /// The destination rectangle, in the command's own coordinate space.
2296 dest: Rect,
2297 },
2298 /// A [`Command::BlurredRoundedRect`]'s shadow parameters, in the
2299 /// command's own (pre-transform) coordinate space.
2300 BlurredRect {
2301 /// The un-padded rectangle the shadow is cast from.
2302 rect: Rect,
2303 /// Per-corner radii, collapsed to their largest at encode time (see
2304 /// [`blur_rrect`]).
2305 radii: CornerRadii,
2306 /// The blur's standard deviation.
2307 std_dev: f64,
2308 /// The shadow's base colour.
2309 color: Color,
2310 },
2311}
2312
2313/// Report an image the atlas refused.
2314///
2315/// The first refusal in a process is a warning, because a blank image where one
2316/// was expected is otherwise invisible; the rest are debug, because a scene
2317/// that keeps drawing a refused image would repeat the message every frame.
2318fn note_image_skip(skip: ImageSkip) {
2319 IMAGE_SKIP_WARNING.call_once(|| {
2320 log::warn!("image draw skipped: {skip} (further skips are logged at debug level)");
2321 });
2322 log::debug!("image draw skipped: {skip}");
2323}
2324
2325/// Report what this frame's image residency cost, on a frame where it cost
2326/// anything.
2327///
2328/// The counterpart to [`note_image_skip`]'s once-per-process warning, which
2329/// says *that* an image was refused and then goes quiet: this says how many
2330/// draws a given frame lost and how hard the atlas is being churned to avoid
2331/// losing more. `frust-perf`-prefixed and at info level, which is what carries
2332/// it into a benchmark capture — the harness keeps every line with that prefix
2333/// and drops the rest, so a run's own log answers "did the atlas hold this
2334/// scene?" without a parallel system-log capture beside it.
2335///
2336/// Silent on a frame that skipped nothing and evicted nothing, which is every
2337/// frame of a steady scene: the common path pays two comparisons and writes no
2338/// line. `skipped` is the frame's own count of image draws that painted
2339/// nothing, so it includes the few refusals decided before residency is even
2340/// consulted (a singular paint transform, a destination with no area) as well
2341/// as the atlas's own — every one of them is a draw the display list asked for
2342/// and the frame did not paint, which is the question the line answers.
2343///
2344/// `perf-trace`-only, like every other `frust-perf` line in this workspace
2345/// (`frust_render::context::log_render_path`, [`PhaseClock`], the encode
2346/// window): the release-lean gate asserts a shipping binary contains no
2347/// `frust-perf` bytes at all, and a `#[cfg]` is what makes that the compiler's
2348/// answer rather than a hope about the optimizer.
2349#[cfg(feature = "perf-trace")]
2350fn note_image_pressure(frame: &CompiledFrame, images: &ImageResidency) {
2351 if !image_pressure_reported(frame, images) {
2352 return;
2353 }
2354 // The text is built *inside* the macro's argument list, so the log ceiling
2355 // covers the `format!` and not merely the emission. `evicted>0` is the
2356 // expected steady state of a working set larger than the atlas, and a
2357 // build whose ceiling drops info lines must not pay a `String` per frame
2358 // for one it will never record.
2359 log::info!("{}", image_pressure_line(frame, images));
2360}
2361
2362/// Whether `frame` has any image-residency cost to report against `images`.
2363///
2364/// Split from the line itself so the text can be built where the log macro can
2365/// elide it — see [`note_image_pressure`] — and so "a frame the atlas held
2366/// reports nothing at all" stays a property a test can name.
2367#[cfg(feature = "perf-trace")]
2368#[must_use]
2369pub fn image_pressure_reported(frame: &CompiledFrame, images: &ImageResidency) -> bool {
2370 frame.skipped_images != 0 || images.frame_pressure_evictions() != 0
2371}
2372
2373/// The `frust-perf img` line `frame` reports against `images`.
2374///
2375/// Separate from the logging above because the *text* is a contract: a
2376/// benchmark capture is graded by grepping these fields, so the field order and
2377/// the names are pinned by a test rather than only by this module. Ask
2378/// [`image_pressure_reported`] first — on a quiet frame this still formats a
2379/// line, it is simply one nothing asks for.
2380#[cfg(feature = "perf-trace")]
2381#[must_use]
2382pub fn image_pressure_line(frame: &CompiledFrame, images: &ImageResidency) -> String {
2383 let budget = images.budget();
2384 format!(
2385 "frust-perf img skipped={} evicted={} resident={} budget={}x{}x{}",
2386 frame.skipped_images,
2387 images.frame_pressure_evictions(),
2388 images.entry_count(),
2389 budget.atlas_size.0,
2390 budget.atlas_size.1,
2391 budget.max_atlases,
2392 )
2393}
2394
2395/// Report a glyph run whose font could not be read.
2396///
2397/// The same once-warning-then-debug shape [`note_image_skip`] uses, and for
2398/// the same reason: a missing line of text is otherwise invisible, while a
2399/// scene that keeps drawing against an unloaded font would repeat the message
2400/// every frame.
2401fn note_font_skip() {
2402 FONT_SKIP_WARNING.call_once(|| {
2403 log::warn!(
2404 "glyph run skipped: its font blob is not a readable face \
2405 (further skips are logged at debug level)"
2406 );
2407 });
2408 log::debug!("glyph run skipped: its font blob is not a readable face");
2409}
2410
2411/// The id the offscreen target of fragment program `program_id` is registered
2412/// under — the compiler's half of the agreement
2413/// [`crate::effects::shader_quad`] makes with the pre-pass that renders it.
2414///
2415/// A pure function of the program id, computed independently on both sides
2416/// rather than exchanged through a side table, because the walk holds nothing
2417/// else: a [`Command::ShaderQuad`] carries its `ShaderProgram` and no texture
2418/// handle. `SceneTextureId::for_shader_program` is what keeps the derived
2419/// value out of the range host textures mint from.
2420#[must_use]
2421pub fn shader_quad_texture_id(program_id: u64) -> u64 {
2422 SceneTextureId::for_shader_program(program_id).get()
2423}
2424
2425/// Report a [`Command::ShaderQuad`] dropped for want of a rendered target.
2426///
2427/// Latched to once per process rather than following [`note_image_skip`] and
2428/// [`note_font_skip`]'s warn-then-debug shape: a compiler driven without the
2429/// pre-pass (a host encoding frames straight through
2430/// [`crate::EngineRenderer`], or a program whose shader failed to compile)
2431/// produces this on every frame forever, and the second report says nothing
2432/// the first did not. Never raised for a quad [`shader_quad_is_culled`]
2433/// reports deliberately culled — that case is [`note_shader_quad_culled`]'s.
2434fn note_shader_quad_unrendered(program_id: u64) {
2435 SHADER_QUAD_SKIP_WARNING.call_once(|| {
2436 log::warn!(
2437 "ShaderQuad draws nothing: fragment program {program_id} has no rendered target \
2438 — either the frame's shader pre-pass did not run before this compile, or the \
2439 program failed to compile (logged once per process)"
2440 );
2441 });
2442}
2443
2444/// Whether a [`Command::ShaderQuad`] at `dest` under `transform` (the
2445/// command's own transform composed with the frame's) is entirely outside
2446/// `frame_extent` — the same target-extent overlap test
2447/// `crate::effects::shader_quad::frame_demands`/`culled_program_ids` apply to
2448/// decide whether the pre-pass renders this program's quad at all this frame,
2449/// restated independently here (the compiler and the pre-pass share no
2450/// channel for "this id was culled, not missing") so an id nothing is
2451/// registered under can be told apart from one whose pre-pass genuinely never
2452/// ran — see [`note_shader_quad_unrendered`]/[`note_shader_quad_culled`].
2453///
2454/// Conservative on the same terms as the pre-pass's own check: a non-finite
2455/// `bbox` answers `false` (never treated as culled — some other refusal
2456/// accounts for it), and a shared edge counts as overlapping via
2457/// [`kurbo::Rect::overlaps`]. Unlike the pre-pass's walk, this has no
2458/// [`Command::PushRetained`]-bracket exemption to make: it is asked only
2459/// about the one quad instance actually being compiled right now, under its
2460/// own already-composed `transform` (which, inside a bracket, already
2461/// includes the enclosing retained bracket's own presentation correction — see
2462/// `compile_command`'s `combined`) — there is no separate "the bracket might
2463/// still move it" case to guard against here, only the geometry this exact
2464/// draw is about to be attempted at.
2465fn shader_quad_is_culled(dest: Rect, transform: Affine, frame_extent: (u16, u16)) -> bool {
2466 let frame_rect = Rect::new(
2467 0.0,
2468 0.0,
2469 f64::from(frame_extent.0),
2470 f64::from(frame_extent.1),
2471 );
2472 let bbox = transform.transform_rect_bbox(dest);
2473 bbox.is_finite() && !bbox.overlaps(frame_rect)
2474}
2475
2476/// Report a [`Command::ShaderQuad`] dropped because the shader-quad pre-pass
2477/// (`crate::effects::shader_quad`) deliberately culled every one of the
2478/// program's quads this frame — its device rectangle does not intersect the
2479/// frame's own target at all — rather than because no pre-pass ran for it at
2480/// all.
2481///
2482/// Debug-level and unlatched (unlike [`note_shader_quad_unrendered`]): a
2483/// culled quad is an expected, routine outcome of a scene drawing an
2484/// off-screen or fully clipped program, not a signal that something is
2485/// missing, so it does not need the once-per-process rate limit a genuine
2486/// "nothing rendered this" warning does.
2487fn note_shader_quad_culled(program_id: u64) {
2488 log::debug!(
2489 "ShaderQuad draws nothing: fragment program {program_id} was culled by the \
2490 shader-quad pre-pass — its destination does not intersect this frame's target \
2491 (expected; not a missing pre-pass)"
2492 );
2493}
2494
2495/// Report a [`Command::ShaderQuad`] dropped because
2496/// `FRUST_ENGINE_NO_SHADER_EFFECTS` is set.
2497///
2498/// Once per process, like the sighting above: the switch is read once and
2499/// cannot change under a running process, so the fact is stated once.
2500fn note_shader_effects_disabled() {
2501 SHADER_EFFECTS_DISABLED_WARNING.call_once(|| {
2502 log::warn!(
2503 "ShaderQuad commands draw nothing: FRUST_ENGINE_NO_SHADER_EFFECTS is set \
2504 (logged once per process)"
2505 );
2506 });
2507}
2508
2509/// The transform a command carries, or `None` for one that carries none.
2510fn command_transform(command: &Command) -> Option<Affine> {
2511 match command {
2512 Command::FillRect { transform, .. }
2513 | Command::RoundedRect { transform, .. }
2514 | Command::Line { transform, .. }
2515 | Command::PushClip { transform, .. }
2516 | Command::PushClipRounded { transform, .. }
2517 | Command::PushClipPath { transform, .. }
2518 | Command::Image { transform, .. }
2519 | Command::BlurredRoundedRect { transform, .. }
2520 | Command::PushLayer { transform, .. }
2521 | Command::ClearRect { transform, .. }
2522 | Command::Path { transform, .. }
2523 | Command::ShaderQuad { transform, .. }
2524 | Command::SceneTexture { transform, .. }
2525 | Command::PushRetained { transform, .. }
2526 | Command::DrawRetained { transform, .. }
2527 | Command::BackdropBlur { transform, .. } => Some(*transform),
2528 Command::GlyphRun(run) => Some(run.transform),
2529 Command::PopClip | Command::PopLayer | Command::PopRetained => None,
2530 }
2531}
2532
2533/// Whether `command`'s own transform still lands on the finite device grid once
2534/// `combined` — the frame root with any open retained bracket's correction — is
2535/// composed ahead of it.
2536///
2537/// Asked by both of the frame's walks, from one place, because they have to ask
2538/// it the same way. The draw walk draws nothing for a command that answers
2539/// `false` and enters a `PushRetained` neutrally instead (see
2540/// [`retained_entry`]); the collect walk classifies against the transform that
2541/// entry implies. Two copies of this expression could drift a coefficient
2542/// apart and route a run for a device size it is never drawn at.
2543///
2544/// A command carrying no transform of its own is on the grid trivially: there
2545/// is nothing to compose.
2546fn command_on_grid(command: &Command, combined: Affine) -> bool {
2547 command_transform(command).is_none_or(|transform| check_finite(combined * transform).is_ok())
2548}
2549
2550/// The presentation scale and transform a `PushRetained` bracket is entered
2551/// with: the recorded pair on the grid, and the neutral pair off it.
2552///
2553/// The neutral pair is what makes an off-grid bracket *inert* rather than
2554/// absent — it still has a depth to count and a pop to balance, but it installs
2555/// no correction, so nothing inside it is drawn through a transform the frame
2556/// refused. Both walks substitute through this one function so that the route
2557/// a run is given and the transform it is drawn through can never be decided
2558/// from different magnitudes.
2559fn retained_entry(on_grid: bool, scale: f64, transform: Affine) -> (f64, Affine) {
2560 if on_grid {
2561 (scale, transform)
2562 } else {
2563 (1.0, Affine::IDENTITY)
2564 }
2565}
2566
2567/// Refuse a viewport whose tile-snapped extent would not fit in `u16`.
2568///
2569/// The recorder snaps the scene size up to whole tiles, and that rounding is
2570/// checked arithmetic upstream — an extent within three pixels of `u16::MAX`
2571/// has no representable tile-aligned bound. Refusing it here is what keeps the
2572/// frame path free of that panic.
2573fn check_tile_addressable(width: u16, height: u16) -> Result<(), EngineError> {
2574 let addressable = width.checked_next_multiple_of(Tile::WIDTH).is_some()
2575 && height.checked_next_multiple_of(Tile::HEIGHT).is_some();
2576
2577 if addressable {
2578 Ok(())
2579 } else {
2580 Err(EngineError::TargetTooLarge)
2581 }
2582}
2583
2584/// Refuse a transform that maps geometry off the finite device grid.
2585///
2586/// A non-finite coefficient (`NaN` from a degenerate inverse, an infinity from
2587/// an overflowed scale) sends every coordinate it touches outside the `u16`
2588/// pixel range the strip pipeline addresses, so the frame is refused rather
2589/// than rasterized into whatever the downstream float-to-integer conversions
2590/// happen to saturate to.
2591fn check_finite(transform: Affine) -> Result<(), EngineError> {
2592 if transform.as_coeffs().iter().all(|c| c.is_finite()) {
2593 Ok(())
2594 } else {
2595 Err(EngineError::InvalidTransform)
2596 }
2597}
2598
2599/// Refuse a command whose own geometry is non-finite.
2600///
2601/// A finite transform is not enough on its own: a `NaN` corner radius, an
2602/// infinite rectangle extent, a `NaN` control point or stroke width all reach
2603/// the flattener and the stroker as they were recorded, and neither of those
2604/// bails on a non-finite number. They subdivide against it — a rounded rect of
2605/// unbounded extent with a `NaN` radius never finishes at all, and a `NaN`
2606/// stroke width buys hundreds of milliseconds and megabytes of scratch to emit
2607/// no coverage whatsoever. Refusing here, in the same up-front walk the
2608/// transforms are checked in, is what bounds the frame path's work by the
2609/// scene rather than by the arithmetic.
2610///
2611/// Only the commands the compiler actually lowers are checked. A command it
2612/// recognises and skips contributes no geometry to the frame, so refusing the
2613/// whole frame over one would draw *nothing* where skipping draws less — the
2614/// weaker outcome. A clip is checked because it *is* lowered: its rectangle and
2615/// radii decide whether the clip scissors or masks and where its edges land, so
2616/// a non-finite one is refused on the same terms as a fill's, matching the
2617/// refusal its transform already drew. A layer and a retained bracket are
2618/// checked on those same terms, and for the same reason: each lowers its
2619/// rectangle through the clip stack, so a non-finite one reaches the flattener
2620/// exactly as a clip's would. Their `alpha` and `scale` are checked alongside
2621/// it because neither is decoration — an alpha decides whether the layer
2622/// isolates, and a scale composes a transform every command inside the bracket
2623/// is drawn under. A clear is checked because its rectangle *is* the coverage
2624/// it erases with. An image's destination rectangle is checked on those same
2625/// terms — it is both the coverage the image paints through and the scale its
2626/// natural-to-destination transform is derived from, so a non-finite one would
2627/// reach the flattener and the paint encoding alike. A blurred rounded
2628/// rectangle's rectangle, corner radii and standard deviation are checked on
2629/// those same terms — together they decide the padded rectangle the strip
2630/// generator rasterizes ([`blur_rrect::inflated_bounds`]) and the falloff the
2631/// fragment shader evaluates from the encoded paint
2632/// ([`blur_rrect::encode_blurred_rounded_rect`]), so a non-finite one would
2633/// reach the flattener and the paint encoding exactly as a non-finite rounded
2634/// rect's radii already do. A lowered command carrying no geometry at all
2635/// ([`Command::PopClip`] and its two siblings) has nothing to check and sits
2636/// with the skipped group. A glyph run's font size and per-glyph positions are
2637/// checked for the same reason a stroke width is: they are not decoration
2638/// either, but the numbers every glyph's own draw transform is derived from
2639/// ([`crate::text`]), so a non-finite one reaches the flattener as a transform
2640/// no subdivision converges against. The scan is per glyph and therefore the
2641/// one check here whose cost grows with a command's contents — bounded by the
2642/// glyph count the run already carries, and paid once per frame rather than
2643/// once per glyph drawn.
2644///
2645/// The match is exhaustive over every [`Command`] variant, the same as
2646/// [`SceneCompiler::compile_command`]'s: a variant added to the enum fails to
2647/// compile here until it is placed in the checked group or the unchecked one.
2648/// Moving a variant *between* those two groups is not itself compiler-enforced
2649/// — the match stays exhaustive either way — so that half of the discipline
2650/// still has to be kept by hand alongside `compile_command`.
2651fn check_geometry(command: &Command) -> Result<(), EngineError> {
2652 let finite = match command {
2653 Command::FillRect { rect, .. } => rect.is_finite(),
2654 Command::RoundedRect { rect, radii, .. } => rect.is_finite() && radii_are_finite(*radii),
2655 Command::Line { p0, p1, width, .. } => {
2656 p0.is_finite() && p1.is_finite() && width.is_finite()
2657 }
2658 Command::Path { path, style, .. } => path.is_finite() && style_is_finite(style),
2659 Command::PushClip { rect, .. } => rect.is_finite(),
2660 Command::PushClipRounded { rect, radii, .. } => {
2661 rect.is_finite() && radii_are_finite(*radii)
2662 }
2663 // An outline is the mask's geometry outright, so a non-finite point
2664 // reaches the flattener exactly as a filled path's would.
2665 Command::PushClipPath { path, .. } => path.is_finite(),
2666 Command::PushLayer { rect, alpha, .. } => rect.is_finite() && alpha.is_finite(),
2667 Command::ClearRect { rect, .. } => rect.is_finite(),
2668 Command::PushRetained {
2669 rect, alpha, scale, ..
2670 }
2671 | Command::DrawRetained {
2672 rect, alpha, scale, ..
2673 } => rect.is_finite() && alpha.is_finite() && scale.is_finite(),
2674 Command::Image { dest, .. } => dest.is_finite(),
2675 Command::SceneTexture { dest, .. } => dest.is_finite(),
2676 // Carries a destination rectangle like the two above, and lowers
2677 // through the same external-texture path, so the same check applies.
2678 Command::ShaderQuad { dest, .. } => dest.is_finite(),
2679 Command::BlurredRoundedRect {
2680 rect,
2681 radii,
2682 std_dev,
2683 ..
2684 } => rect.is_finite() && radii_are_finite(*radii) && std_dev.is_finite(),
2685 Command::GlyphRun(run) => glyph_run_is_finite(run),
2686 // Its rectangle is the coverage the blurred backdrop is drawn through
2687 // and the region the blur reads is grown from it, and its radius is
2688 // the σ the planner's decimation loop runs over — a non-finite one of
2689 // either reaches the flattener or that loop as it was recorded.
2690 Command::BackdropBlur { rect, radius, .. } => rect.is_finite() && radius.is_finite(),
2691 // Carrying no geometry of their own — see above.
2692 Command::PopClip | Command::PopLayer | Command::PopRetained => true,
2693 };
2694
2695 if finite {
2696 Ok(())
2697 } else {
2698 Err(EngineError::InvalidGeometry)
2699 }
2700}
2701
2702/// Whether a glyph run's own numbers are finite.
2703///
2704/// The font size and every glyph position, because those are exactly the run's
2705/// numbers that end up inside a transform: `glifo` absorbs the font size into
2706/// each glyph's draw transform and translates that transform by the glyph's
2707/// position, so either one non-finite produces a transform the flattener
2708/// subdivides against forever. The font itself is not checked — a malformed or
2709/// unreadable face yields no outline and draws nothing, which is a missing
2710/// glyph rather than an unbounded loop.
2711fn glyph_run_is_finite(run: &GlyphRun) -> bool {
2712 run.font_size.is_finite()
2713 && run
2714 .glyphs
2715 .iter()
2716 .all(|glyph| glyph.x.is_finite() && glyph.y.is_finite())
2717}
2718
2719/// Whether every corner radius is finite.
2720fn radii_are_finite(radii: CornerRadii) -> bool {
2721 radii.top_left.is_finite()
2722 && radii.top_right.is_finite()
2723 && radii.bottom_right.is_finite()
2724 && radii.bottom_left.is_finite()
2725}
2726
2727/// Whether a path style's own numbers are finite.
2728///
2729/// The dash lengths and phase are checked even though
2730/// [`DashPattern::is_effective`] would strike a non-finite pattern out and
2731/// stroke solid: a frame the compiler refuses for a `NaN` stroke width would
2732/// otherwise be accepted for a `NaN` dash phase, and one contract over every
2733/// number a *lowered* command carries is the one a caller can hold in their
2734/// head — not a claim about a command [`check_geometry`] skips rather than
2735/// lowers, whose numbers this function never sees.
2736///
2737/// An effective dash pattern is checked further, past its own fields: see
2738/// [`dash_cycle_is_normalizable`].
2739fn style_is_finite(style: &PathStyle) -> bool {
2740 match style {
2741 PathStyle::Fill => true,
2742 PathStyle::Stroke { width, dash } => {
2743 let dash_finite = match dash {
2744 Some(dash) => {
2745 dash.on.is_finite()
2746 && dash.off.is_finite()
2747 && dash.phase.is_finite()
2748 && dash_cycle_is_normalizable(dash)
2749 }
2750 None => true,
2751 };
2752 width.is_finite() && dash_finite
2753 }
2754 }
2755}
2756
2757/// Whether a dash pattern's derived cycle survives kurbo's own normalization
2758/// arithmetic, so `kurbo::dash` terminates instead of spinning forever.
2759///
2760/// [`DashPattern::is_effective`] already screens out a non-positive or
2761/// sub-epsilon pattern in favour of a solid stroke, but its own period check —
2762/// `on + off >= DASH_PERIOD_EPSILON` — can itself be fooled: two individually
2763/// finite lengths can sum past `f64::MAX` into `+inf`, and `+inf >=
2764/// DASH_PERIOD_EPSILON` still reads as effective. kurbo doubles this crate's
2765/// on/off pair into its own length-2 dash array, so the period it derives is
2766/// always `on + off`; once that overflows, `phase.rem_euclid(period)`
2767/// overflows with it, and the catch-up loop `kurbo::dash` runs before it ever
2768/// pulls a `PathEl` adds an infinite step to a value that never converges —
2769/// `on = off = f64::MAX`, `phase = -1.0` hangs this way. Refusing here, where
2770/// `on`, `off` and `phase` already passed their own finiteness checks, is what
2771/// keeps that unbounded loop out of the frame path.
2772///
2773/// Public for the same reason [`dash_path`] and [`well_formed`] are: the CPU
2774/// oracle carries an identical copy, and a cross-crate test pins the two
2775/// against each other so they cannot drift apart silently.
2776pub fn dash_cycle_is_normalizable(dash: &DashPattern) -> bool {
2777 if !dash.is_effective() {
2778 // A degenerate pattern never reaches `dash_path`: `is_effective` is
2779 // what routes it to a solid stroke instead, so its derived period is
2780 // moot here.
2781 return true;
2782 }
2783 let period = dash.on + dash.off;
2784 period.is_finite() && period > 0.0 && dash.phase.rem_euclid(period).is_finite()
2785}
2786
2787/// The device-space rectangle to hand the fast rectangle path, or `None` when
2788/// this rectangle has to go through full path processing.
2789///
2790/// The fast path writes strip coverage for a rectangle directly, skipping
2791/// flattening and tiling entirely, and is taken only when the result is
2792/// indistinguishable from the general path: the composed transform must keep
2793/// the rectangle axis-aligned (no rotation or skew), and the transformed
2794/// rectangle must land on whole pixels, so no edge needs partial coverage.
2795///
2796/// [`clip`] admits a rectangular clip to its scissor path by the same rule and
2797/// through this same function: a rectangle whose coverage can be written
2798/// exactly is a rectangle whose *clip* can be applied exactly, so the two share
2799/// one admission rule rather than two that could drift apart.
2800fn fast_rect(rect: Rect, transform: Affine) -> Option<Rect> {
2801 if !is_axis_aligned(&transform) {
2802 return None;
2803 }
2804
2805 let device = transform.transform_rect_bbox(rect);
2806 is_pixel_aligned(device).then_some(device)
2807}
2808
2809/// The device window a glyph quad drawn now is trimmed to: the generator's
2810/// viewport intersected with the scissor every open bracket's clip applies.
2811///
2812/// Only asked while no coverage mask is on the clip stack, and then every open
2813/// bracket's clip is a scissor whose rectangle is exactly the bracket's
2814/// recorded bounds: a bracket and its clip are pushed together from one
2815/// rectangle under one transform ([`GroupStack`]'s invariant), and a clip
2816/// scissors only when [`fast_rect`] admits that rectangle — the same
2817/// `transform_rect_bbox` the bounds are recorded from, on whole pixels — clamped
2818/// onto the `u16` grid the same way. So this is the rectangle the general
2819/// draw's coverage is culled and scissored to, without a second clip stack.
2820fn quad_window(generator: &StripGenerator, groups: &GroupStack) -> RectU16 {
2821 let coordinate = |value: f64| value.clamp(0.0, f64::from(u16::MAX)) as u16;
2822 groups.bounds().fold(
2823 RectU16::new(0, 0, generator.width(), generator.height()),
2824 |window, bounds| {
2825 window.intersect(RectU16::new(
2826 coordinate(bounds.x0),
2827 coordinate(bounds.y0),
2828 coordinate(bounds.x1),
2829 coordinate(bounds.y1),
2830 ))
2831 },
2832 )
2833}
2834
2835/// Mark `frame`'s retained bracket `index`, a `PushRetained` body, as one no
2836/// page may hold.
2837fn mark_unretainable(frame: &mut CompiledFrame, index: usize) {
2838 if let Some(RetainedBracket {
2839 kind: RetainedKind::Record { retainable, .. },
2840 ..
2841 }) = frame.retained.get_mut(index)
2842 {
2843 *retainable = false;
2844 }
2845}
2846
2847/// Whether `device` lies wholly inside `window`.
2848fn rect_within(device: Rect, window: RectU16) -> bool {
2849 device.is_finite()
2850 && !window.is_empty()
2851 && device.x0 >= f64::from(window.x0)
2852 && device.y0 >= f64::from(window.y0)
2853 && device.x1 <= f64::from(window.x1)
2854 && device.y1 <= f64::from(window.y1)
2855}
2856
2857/// `device` rounded out to whole tiles and clamped onto the `u16` grid — the
2858/// alignment a recorded layer's own bounds take.
2859fn tile_bounds(device: Rect) -> RectU16 {
2860 let snap = |value: f64, step: u16, up: bool| {
2861 let step = f64::from(step);
2862 let snapped = if up {
2863 (value / step).ceil() * step
2864 } else {
2865 (value / step).floor() * step
2866 };
2867 if snapped.is_finite() {
2868 snapped.clamp(0.0, f64::from(u16::MAX)) as u16
2869 } else {
2870 0
2871 }
2872 };
2873 RectU16::new(
2874 snap(device.x0, Tile::WIDTH, false),
2875 snap(device.y0, Tile::HEIGHT, false),
2876 snap(device.x1, Tile::WIDTH, true),
2877 snap(device.y1, Tile::HEIGHT, true),
2878 )
2879}
2880
2881/// Whether every edge of `rect` falls on a whole pixel.
2882fn is_pixel_aligned(rect: Rect) -> bool {
2883 [rect.x0, rect.y0, rect.x1, rect.y1]
2884 .iter()
2885 .all(|v| v.is_finite() && v.fract() == 0.0)
2886}
2887
2888/// A stroke of `width` with round caps and joins — the only stroke style the
2889/// display list can express.
2890fn round_stroke(width: f64) -> Stroke {
2891 Stroke::new(width)
2892 .with_caps(Cap::Round)
2893 .with_join(Join::Round)
2894}
2895
2896/// `path` expanded into the sub-paths `dash` breaks it into.
2897///
2898/// Both ends of the expansion go through [`well_formed`]. The input needs it
2899/// because `kurbo::dash` mishandles a subpath that closes without ever
2900/// producing a segment: it emits that subpath's closing element ahead of the
2901/// `MoveTo` meant to open the output, so a path whose *first* subpath is a
2902/// zero-length closed one (a dashed arc at zero sweep records exactly that)
2903/// dashes to a sequence beginning with `ClosePath`. Such a sequence is not a
2904/// path any consumer can read — `BezPath`'s own "begins with `MoveTo`"
2905/// invariant is asserted in a debug build and silently strokes malformed
2906/// geometry in a release one. Normalizing those subpaths away first removes
2907/// the input the iterator gets wrong; normalizing the result as well makes the
2908/// well-formedness of what this returns a property of this function rather
2909/// than of the dash iterator's internal states.
2910///
2911/// Callers inside this crate only ever reach `dash` here once
2912/// [`dash_cycle_is_normalizable`] has passed it, since `kurbo::dash` itself
2913/// does not bound its catch-up loop against a non-normalizable cycle; a caller
2914/// outside the up-front walk carries that same obligation. Made `pub` (rather
2915/// than `pub(crate)`) so `frust-testing`'s CPU oracle, which keeps its own
2916/// independent copy of this lowering (see that crate's `oracle_cpu` module
2917/// docs for why), can pin its output against this one directly rather than
2918/// only through a rendered image.
2919pub fn dash_path(path: &BezPath, dash: DashPattern) -> BezPath {
2920 let source = well_formed(path.iter());
2921 well_formed(kurbo::dash(source.iter(), dash.phase, &[dash.on, dash.off]))
2922}
2923
2924/// `elements` as a path every consumer can read: opened by a `MoveTo`, and
2925/// carrying no `ClosePath` that closes a subpath with no segments in it.
2926///
2927/// Both rules drop elements that describe no geometry — an element before the
2928/// first `MoveTo` has no start point to be drawn from, and closing a subpath
2929/// that never left its start point adds no segment — so a well-formed path in
2930/// yields itself back unchanged.
2931///
2932/// `pub` for the same cross-crate-parity reason as [`dash_path`].
2933pub fn well_formed(elements: impl Iterator<Item = PathEl>) -> BezPath {
2934 let mut out = BezPath::new();
2935 // Tracked rather than read back off `out`: `BezPath::is_empty` asks whether
2936 // a path holds any SEGMENT, which a path holding only its opening `MoveTo`
2937 // does not.
2938 let mut opened = false;
2939 let mut segments_in_subpath = 0_usize;
2940
2941 for element in elements {
2942 match element {
2943 PathEl::MoveTo(_) => {
2944 opened = true;
2945 segments_in_subpath = 0;
2946 out.push(element);
2947 }
2948 PathEl::ClosePath => {
2949 if segments_in_subpath > 0 {
2950 segments_in_subpath = 0;
2951 out.push(element);
2952 }
2953 }
2954 PathEl::LineTo(_) | PathEl::QuadTo(..) | PathEl::CurveTo(..) => {
2955 if opened {
2956 segments_in_subpath += 1;
2957 out.push(element);
2958 }
2959 }
2960 }
2961 }
2962 out
2963}
2964
2965/// The display list's per-corner radii as kurbo's, in its clockwise-from-top-left
2966/// argument order.
2967fn rounded_rect_radii(radii: CornerRadii) -> RoundedRectRadii {
2968 RoundedRectRadii::new(
2969 radii.top_left,
2970 radii.top_right,
2971 radii.bottom_right,
2972 radii.bottom_left,
2973 )
2974}
2975
2976#[cfg(test)]
2977mod tests {
2978 use super::*;
2979
2980 #[test]
2981 fn a_viewport_within_three_pixels_of_the_u16_ceiling_is_refused() {
2982 assert!(check_tile_addressable(65532, 65532).is_ok());
2983 assert!(matches!(
2984 check_tile_addressable(65533, 16),
2985 Err(EngineError::TargetTooLarge)
2986 ));
2987 assert!(matches!(
2988 check_tile_addressable(16, u16::MAX),
2989 Err(EngineError::TargetTooLarge)
2990 ));
2991 }
2992
2993 /// The substitution both walks make for an off-grid `PushRetained`, and why
2994 /// making it in only one of them would matter.
2995 ///
2996 /// A bracket entered with the recorded presentation installs a correction;
2997 /// one entered neutrally installs none. That correction is precisely the
2998 /// affine a run inside the bracket is *classified* against, so a walk that
2999 /// substituted and a walk that did not would decide a run's route from one
3000 /// magnitude and draw it at another — which is the atlas route handed to a
3001 /// transform `glifo` will not absorb.
3002 #[test]
3003 fn an_off_grid_retained_bracket_is_entered_neutrally() {
3004 // A frame root already carrying an outer bracket's correction, and an
3005 // inner bracket whose own transform overflows against it. Both factors
3006 // are finite; only the composition is not.
3007 let combined = Affine::scale(1e200);
3008 let transform = Affine::scale(1e200);
3009 let rect = Rect::new(0.0, 0.0, 10.0, 10.0);
3010 let command = Command::PushRetained {
3011 key: 0,
3012 rect,
3013 alpha: 1.0,
3014 scale: 2.0,
3015 transform,
3016 };
3017
3018 assert!(!command_on_grid(&command, combined));
3019 assert!(command_on_grid(&command, Affine::IDENTITY));
3020
3021 let (scale, entered) = retained_entry(false, 2.0, transform);
3022 assert_eq!(scale, 1.0);
3023 assert_eq!(entered.as_coeffs(), Affine::IDENTITY.as_coeffs());
3024 let (scale, entered) = retained_entry(true, 2.0, transform);
3025 assert_eq!(scale, 2.0);
3026 assert_eq!(entered.as_coeffs(), transform.as_coeffs());
3027
3028 // And the difference reaches the quantity that is classified: an
3029 // outermost bracket entered with the recorded pair corrects, one
3030 // entered neutrally does not.
3031 let mut recorded = RetainedStack::new();
3032 recorded.enter(rect, 2.0, transform);
3033 assert_ne!(
3034 recorded.correction().as_coeffs(),
3035 Affine::IDENTITY.as_coeffs()
3036 );
3037
3038 let mut neutral = RetainedStack::new();
3039 let (scale, entered) = retained_entry(false, 2.0, transform);
3040 neutral.enter(rect, scale, entered);
3041 assert_eq!(
3042 neutral.correction().as_coeffs(),
3043 Affine::IDENTITY.as_coeffs()
3044 );
3045 }
3046
3047 #[test]
3048 fn pixel_alignment_rejects_fractional_edges() {
3049 assert!(is_pixel_aligned(Rect::new(0.0, 0.0, 4.0, 4.0)));
3050 assert!(!is_pixel_aligned(Rect::new(0.0, 0.5, 4.0, 4.0)));
3051 assert!(!is_pixel_aligned(Rect::new(0.0, 0.0, 4.0, f64::INFINITY)));
3052 }
3053
3054 /// [`SHADER_QUAD_SKIP_WARNING`] is a process-global [`Once`], so this
3055 /// proves the half of "exactly once" a test can still observe once
3056 /// another test in the same binary may already have tripped it: the
3057 /// latch never un-completes, whatever else in this binary called
3058 /// [`note_shader_quad_unrendered`] first. `Once::call_once` itself is the
3059 /// standard-library guarantee behind the other half — that the closure
3060 /// inside it runs at most once ever — so calling the reporting function
3061 /// twice here and observing the latch hold is a structural stand-in for
3062 /// capturing and counting the actual log line.
3063 #[test]
3064 fn an_unrendered_shader_quad_is_latched_to_once_per_process() {
3065 note_shader_quad_unrendered(1);
3066 assert!(SHADER_QUAD_SKIP_WARNING.is_completed());
3067 note_shader_quad_unrendered(1);
3068 assert!(SHADER_QUAD_SKIP_WARNING.is_completed());
3069 }
3070
3071 /// The kill switch's own report latches independently of the one above —
3072 /// two distinct facts about why a quad drew nothing, each stated once.
3073 #[test]
3074 fn a_disabled_shader_quad_is_latched_to_once_per_process() {
3075 note_shader_effects_disabled();
3076 assert!(SHADER_EFFECTS_DISABLED_WARNING.is_completed());
3077 note_shader_effects_disabled();
3078 assert!(SHADER_EFFECTS_DISABLED_WARNING.is_completed());
3079 }
3080
3081 /// A shader quad's target id is derived, never minted, and lands in the
3082 /// half of the id space `SceneTextureId::mint` cannot reach — the whole
3083 /// reason the compiler can name a texture it never saw registered.
3084 #[test]
3085 fn a_shader_quad_target_id_is_derived_from_its_program() {
3086 assert_eq!(shader_quad_texture_id(7), shader_quad_texture_id(7));
3087 assert_ne!(shader_quad_texture_id(7), shader_quad_texture_id(8));
3088 assert_ne!(shader_quad_texture_id(7), 7);
3089 assert_eq!(
3090 shader_quad_texture_id(7),
3091 SceneTextureId::for_shader_program(7).get()
3092 );
3093 }
3094
3095 #[test]
3096 fn shader_quad_is_culled_true_when_entirely_outside_the_frame() {
3097 assert!(shader_quad_is_culled(
3098 Rect::new(1000.0, 1000.0, 1008.0, 1008.0),
3099 Affine::IDENTITY,
3100 (64, 64)
3101 ));
3102 }
3103
3104 #[test]
3105 fn shader_quad_is_culled_false_when_overlapping_the_frame() {
3106 assert!(!shader_quad_is_culled(
3107 Rect::new(0.0, 0.0, 8.0, 8.0),
3108 Affine::IDENTITY,
3109 (64, 64)
3110 ));
3111 }
3112
3113 #[test]
3114 fn shader_quad_is_culled_false_touching_the_frame_edge() {
3115 // A shared edge counts as overlapping (kurbo::Rect::overlaps), so a
3116 // quad exactly abutting the frame boundary is never wrongly reported
3117 // culled.
3118 assert!(!shader_quad_is_culled(
3119 Rect::new(64.0, 0.0, 80.0, 16.0),
3120 Affine::IDENTITY,
3121 (64, 64)
3122 ));
3123 }
3124
3125 #[test]
3126 fn shader_quad_is_culled_false_for_a_non_finite_bbox() {
3127 assert!(!shader_quad_is_culled(
3128 Rect::new(0.0, 0.0, f64::NAN, 8.0),
3129 Affine::IDENTITY,
3130 (64, 64)
3131 ));
3132 }
3133
3134 #[test]
3135 fn a_culled_shader_quad_is_reported_at_debug_level_not_through_the_unrendered_warning() {
3136 // `note_shader_quad_culled` carries no process-latch to observe the
3137 // way `SHADER_QUAD_SKIP_WARNING` does above — it is meant to fire
3138 // every time, unlike the once-per-process missing-pre-pass report.
3139 // What is asserted here is the compile-time distinction itself: a
3140 // quad `shader_quad_is_culled` reports true for must never also read
3141 // as "un-rendered" by the same geometry.
3142 let frame_extent = (64, 64);
3143 let culled_dest = Rect::new(1000.0, 1000.0, 1008.0, 1008.0);
3144 let unrendered_dest = Rect::new(0.0, 0.0, 8.0, 8.0);
3145
3146 assert!(shader_quad_is_culled(
3147 culled_dest,
3148 Affine::IDENTITY,
3149 frame_extent
3150 ));
3151 assert!(!shader_quad_is_culled(
3152 unrendered_dest,
3153 Affine::IDENTITY,
3154 frame_extent
3155 ));
3156 }
3157}