Skip to main content

cranpose_render_common/
style_shared.rs

1use std::rc::Rc;
2
3use cranpose_foundation::PointerEvent;
4use cranpose_ui::{Brush, DrawCommand, DrawCommandFn, LayoutNodeData, ModifierNodeSlices};
5use cranpose_ui_graphics::{
6    BlendMode, Color, ColorFilter, CommandRecording, CompositingStrategy, CornerRadii,
7    DrawPrimitive, FinishedRecording, GraphicsLayer, Point, RoundedCornerShape, Size,
8};
9
10use crate::layer_transform::{layer_scale_x, layer_scale_y, layer_uniform_scale};
11
12pub struct NodeStyle {
13    pub padding: cranpose_ui_graphics::EdgeInsets,
14    pub background: Option<Color>,
15    pub click_actions: Vec<Rc<dyn Fn(Point)>>,
16    pub shape: Option<RoundedCornerShape>,
17    pub pointer_inputs: Vec<Rc<dyn Fn(PointerEvent)>>,
18    pub draw_commands: Vec<DrawCommand>,
19    pub graphics_layer: Option<GraphicsLayer>,
20    pub clip_to_bounds: bool,
21}
22
23impl NodeStyle {
24    pub fn from_layout_node(data: &LayoutNodeData) -> Self {
25        let resolved = data.resolved_modifiers;
26        let slices: &ModifierNodeSlices = data.modifier_slices();
27        let pointer_inputs = slices.pointer_inputs().to_vec();
28
29        Self {
30            padding: resolved.padding(),
31            background: None,
32            click_actions: slices.click_handlers().to_vec(),
33            shape: None,
34            pointer_inputs,
35            draw_commands: slices.draw_commands().to_vec(),
36            graphics_layer: slices.graphics_layer(),
37            clip_to_bounds: slices.clip_to_bounds(),
38        }
39    }
40}
41
42pub fn combine_layers(
43    current: GraphicsLayer,
44    modifier_layer: Option<GraphicsLayer>,
45) -> GraphicsLayer {
46    if let Some(layer) = modifier_layer {
47        GraphicsLayer {
48            alpha: (current.alpha * layer.alpha).clamp(0.0, 1.0),
49            scale: current.scale * layer.scale,
50            scale_x: current.scale_x * layer.scale_x,
51            scale_y: current.scale_y * layer.scale_y,
52            rotation_x: current.rotation_x + layer.rotation_x,
53            rotation_y: current.rotation_y + layer.rotation_y,
54            rotation_z: current.rotation_z + layer.rotation_z,
55            camera_distance: layer.camera_distance,
56            transform_origin: layer.transform_origin,
57            translation_x: current.translation_x + layer.translation_x,
58            translation_y: current.translation_y + layer.translation_y,
59            shadow_elevation: layer.shadow_elevation,
60            ambient_shadow_color: layer.ambient_shadow_color,
61            spot_shadow_color: layer.spot_shadow_color,
62            shape: layer.shape,
63            clip: current.clip || layer.clip,
64            color_filter: compose_color_filters(current.color_filter, layer.color_filter),
65            compositing_strategy: layer.compositing_strategy,
66            blend_mode: layer.blend_mode,
67            // render_effect is NOT inherited — it applies only to this layer's subtree
68            render_effect: layer.render_effect,
69            // backdrop_effect is NOT inherited — it applies only to this node's backdrop.
70            backdrop_effect: layer.backdrop_effect,
71        }
72    } else {
73        GraphicsLayer {
74            compositing_strategy: CompositingStrategy::Auto,
75            blend_mode: BlendMode::SrcOver,
76            render_effect: None,
77            backdrop_effect: None,
78            ..current
79        }
80    }
81}
82
83pub use crate::graph::quad_bounds;
84
85/// The colour a primitive is actually painted in, once this layer has had its
86/// say.
87///
88/// The colour is snapped to eight bits *first*, because that is where the
89/// platform's own colour type already is by the time anything paints with it
90/// (see [`Color::srgb_8bit`]). Only then does the layer's alpha multiply it.
91/// The order is the whole point: an isolated layer's contents land in an 8-bit
92/// buffer and the alpha multiplies whole channel values, so
93/// `round(round(c * 255) * a)` and not `round(c * 255 * a)`.
94pub fn apply_layer_to_color(color: Color, layer: &GraphicsLayer) -> Color {
95    let color = color.srgb_8bit();
96    apply_color_filter_to_color(
97        Color(
98            color.0,
99            color.1,
100            color.2,
101            (color.3 * layer.alpha).clamp(0.0, 1.0),
102        ),
103        layer.color_filter,
104    )
105}
106
107fn apply_color_filter_to_color(color: Color, filter: Option<ColorFilter>) -> Color {
108    match filter {
109        Some(filter) => {
110            let [r, g, b, a] = filter.apply_rgba([color.0, color.1, color.2, color.3]);
111            Color(r, g, b, a)
112        }
113        None => color,
114    }
115}
116
117pub fn compose_color_filters(
118    base: Option<ColorFilter>,
119    overlay: Option<ColorFilter>,
120) -> Option<ColorFilter> {
121    match (base, overlay) {
122        (None, None) => None,
123        (Some(filter), None) | (None, Some(filter)) => Some(filter),
124        (Some(filter), Some(next)) => Some(filter.compose(next)),
125    }
126}
127
128pub fn apply_layer_to_brush(brush: Brush, layer: &GraphicsLayer) -> Brush {
129    // The overwhelmingly common case — full-alpha layer, no filter, unit
130    // scale — leaves every brush's geometry untouched; a scene of thousands of
131    // shape draws per frame should not rebuild it to discover that. The
132    // colours are still snapped, because a colour reaches the framebuffer at
133    // eight bits whether or not a layer touched it on the way.
134    if layer.alpha == 1.0
135        && layer.color_filter.is_none()
136        && layer_scale_x(layer) == 1.0
137        && layer_scale_y(layer) == 1.0
138    {
139        return map_brush_colors(brush, Color::srgb_8bit);
140    }
141    map_brush_colors(scale_brush_geometry(brush, layer), |color| {
142        apply_layer_to_color(color, layer)
143    })
144}
145
146/// Every colour a brush carries through `paint`, leaving its geometry alone.
147fn map_brush_colors(brush: Brush, paint: impl Fn(Color) -> Color) -> Brush {
148    match brush {
149        Brush::Solid(color) => Brush::solid(paint(color)),
150        Brush::LinearGradient {
151            colors,
152            stops,
153            start,
154            end,
155            tile_mode,
156        } => Brush::LinearGradient {
157            colors: colors.into_iter().map(paint).collect(),
158            stops,
159            start,
160            end,
161            tile_mode,
162        },
163        Brush::RadialGradient {
164            colors,
165            stops,
166            center,
167            radius,
168            tile_mode,
169        } => Brush::RadialGradient {
170            colors: colors.into_iter().map(paint).collect(),
171            stops,
172            center,
173            radius,
174            tile_mode,
175        },
176        Brush::SweepGradient {
177            colors,
178            stops,
179            center,
180        } => Brush::SweepGradient {
181            colors: colors.into_iter().map(paint).collect(),
182            stops,
183            center,
184        },
185    }
186}
187
188/// A brush's geometry under this layer's scale, leaving its colours alone.
189fn scale_brush_geometry(brush: Brush, layer: &GraphicsLayer) -> Brush {
190    let scale_x = layer_scale_x(layer);
191    let scale_y = layer_scale_y(layer);
192    let uniform_scale = layer_uniform_scale(layer);
193
194    match brush {
195        Brush::Solid(color) => Brush::Solid(color),
196        Brush::LinearGradient {
197            colors,
198            stops,
199            mut start,
200            mut end,
201            tile_mode,
202        } => {
203            start.x *= scale_x;
204            start.y *= scale_y;
205            end.x *= scale_x;
206            end.y *= scale_y;
207            Brush::LinearGradient {
208                colors,
209                stops,
210                start,
211                end,
212                tile_mode,
213            }
214        }
215        Brush::RadialGradient {
216            colors,
217            stops,
218            mut center,
219            mut radius,
220            tile_mode,
221        } => {
222            center.x *= scale_x;
223            center.y *= scale_y;
224            radius *= uniform_scale;
225            Brush::RadialGradient {
226                colors,
227                stops,
228                center,
229                radius,
230                tile_mode,
231            }
232        }
233        Brush::SweepGradient {
234            colors,
235            stops,
236            mut center,
237        } => {
238            center.x *= scale_x;
239            center.y *= scale_y;
240            Brush::SweepGradient {
241                colors,
242                stops,
243                center,
244            }
245        }
246    }
247}
248
249/// A layer-resolved brush, split at the solid/gradient boundary.
250///
251/// The per-frame shape emit produces one of these for every draw: the solid
252/// case — effectively all of a heavy animated scene — carries its color
253/// inline, so emitting a shape neither clones a `Brush` nor leaves an enum
254/// with heap-carrying variants for frame teardown to walk. Only the rare
255/// gradient still travels as a cloned [`Brush`].
256#[derive(Clone, Debug, PartialEq)]
257pub enum ResolvedBrush {
258    Solid(Color),
259    /// A non-solid brush (gradients), already layer-resolved.
260    Other(Brush),
261}
262
263impl ResolvedBrush {
264    pub fn from_brush(brush: Brush) -> Self {
265        match brush {
266            Brush::Solid(color) => Self::Solid(color),
267            other => Self::Other(other),
268        }
269    }
270
271    /// The plain `Brush` this resolved form stands for — same values,
272    /// reassembled for consumers that keep speaking `Brush`.
273    pub fn into_brush(self) -> Brush {
274        match self {
275            Self::Solid(color) => Brush::Solid(color),
276            Self::Other(brush) => brush,
277        }
278    }
279}
280
281/// [`apply_layer_to_brush`] without the solid-brush clone: the borrowed
282/// brush's color is copied (or layer-adjusted) inline, and only gradients
283/// are cloned. Produces exactly the values `apply_layer_to_brush` would —
284/// both branches below mirror its fast path and its `Solid` arm verbatim.
285pub fn resolve_layer_brush(brush: &Brush, layer: &GraphicsLayer) -> ResolvedBrush {
286    match brush {
287        Brush::Solid(color) => {
288            if layer.alpha == 1.0
289                && layer.color_filter.is_none()
290                && layer_scale_x(layer) == 1.0
291                && layer_scale_y(layer) == 1.0
292            {
293                ResolvedBrush::Solid(*color)
294            } else {
295                ResolvedBrush::Solid(apply_layer_to_color(*color, layer))
296            }
297        }
298        other => ResolvedBrush::Other(apply_layer_to_brush(other.clone(), layer)),
299    }
300}
301
302pub fn scale_corner_radii(radii: CornerRadii, scale: f32) -> CornerRadii {
303    CornerRadii {
304        top_left: radii.top_left * scale,
305        top_right: radii.top_right * scale,
306        bottom_right: radii.bottom_right * scale,
307        bottom_left: radii.bottom_left * scale,
308    }
309}
310
311#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
312pub enum DrawPlacement {
313    Behind,
314    Overlay,
315}
316
317pub fn primitives_for_placement(
318    command: &DrawCommand,
319    placement: DrawPlacement,
320    size: Size,
321) -> Vec<DrawPrimitive> {
322    primitives_for_placement_reusing(command, placement, size, Vec::new())
323}
324
325/// [`primitives_for_placement`] recording into `storage` the caller retains
326/// between frames. A command that re-records every frame keeps one buffer
327/// whose capacity was earned on earlier frames; only the rare marker-bearing
328/// recording pays for a second output vector.
329pub fn primitives_for_placement_reusing(
330    command: &DrawCommand,
331    placement: DrawPlacement,
332    size: Size,
333    storage: Vec<DrawPrimitive>,
334) -> Vec<DrawPrimitive> {
335    primitives_for_placement_retained(
336        command,
337        placement,
338        size,
339        CommandRecording::default(),
340        storage,
341    )
342    .0
343}
344
345/// The full retained form: the compact recording buffers come from and
346/// return to the caller alongside the materialized primitives, so a command
347/// re-recording every frame allocates nothing in the steady state.
348pub fn primitives_for_placement_retained(
349    command: &DrawCommand,
350    placement: DrawPlacement,
351    size: Size,
352    recording: CommandRecording,
353    storage: Vec<DrawPrimitive>,
354) -> (Vec<DrawPrimitive>, CommandRecording) {
355    let mut no_replay = None;
356    let (primitives, recording, _) = primitives_for_placement_verified(
357        command,
358        placement,
359        size,
360        recording,
361        storage,
362        &mut no_replay,
363        None,
364    );
365    (primitives, recording)
366}
367
368/// [`primitives_for_placement_retained`] with per-command similarity
369/// verification: when `replay` carries the command's state, the freshly
370/// recorded compact form advances it BEFORE materialization, yielding the
371/// frame's retained/dynamic span structure in primitive space. Rendering
372/// still materializes everything — consuming the spans (and skipping
373/// materialization for retained ones) is the renderer-side half of the
374/// retention work. The frame is only returned for marker-free recordings:
375/// content splitting reindexes the primitive vector, and no game-scale
376/// command records content markers.
377pub fn primitives_for_placement_verified(
378    command: &DrawCommand,
379    placement: DrawPlacement,
380    size: Size,
381    recording: CommandRecording,
382    storage: Vec<DrawPrimitive>,
383    replay: &mut Option<&mut cranpose_ui_graphics::CommandReplayState>,
384    command_id: Option<crate::graph::DrawCommandId>,
385) -> (
386    Vec<DrawPrimitive>,
387    CommandRecording,
388    Option<cranpose_ui_graphics::CommandReplayFrame>,
389) {
390    // `markers` is the recording scope's own count of `Content` markers,
391    // maintained while the command records (`draw_content()` calls and pushed
392    // batches both keep it current), so it is authoritative: zero means the
393    // vector holds no marker and passes through untouched. On a watch-class
394    // core, re-streaming a fresh multi-thousand-primitive recording just to
395    // learn "no markers" is measurable frame time.
396    let filter_content = |primitives: Vec<DrawPrimitive>, markers: u32| {
397        if markers == 0 {
398            return primitives;
399        }
400        // `filter(...).collect()` reports a zero lower-bound size hint, so it
401        // grows the output through the whole doubling schedule; for an
402        // animated scene these vectors hold thousands of primitives and are
403        // rebuilt every frame. `Content` markers are rare, so the input
404        // length is the right capacity.
405        let mut out = Vec::with_capacity(primitives.len());
406        out.extend(
407            primitives
408                .into_iter()
409                .filter(|primitive| !matches!(primitive, DrawPrimitive::Content)),
410        );
411        out
412    };
413
414    let split_with_content = |primitives: Vec<DrawPrimitive>, placement, markers: u32| {
415        let last_content_idx = if markers == 0 {
416            None
417        } else {
418            primitives
419                .iter()
420                .rposition(|primitive| matches!(primitive, DrawPrimitive::Content))
421        };
422        let Some(last_content_idx) = last_content_idx else {
423            return if matches!(placement, DrawPlacement::Overlay) {
424                filter_content(primitives, markers)
425            } else {
426                Vec::new()
427            };
428        };
429
430        let mut out = Vec::with_capacity(primitives.len());
431        out.extend(
432            primitives
433                .into_iter()
434                .enumerate()
435                .filter_map(|(index, primitive)| {
436                    if matches!(primitive, DrawPrimitive::Content) {
437                        return None;
438                    }
439                    let is_before = index < last_content_idx;
440                    match placement {
441                        DrawPlacement::Behind if is_before => Some(primitive),
442                        DrawPlacement::Overlay if !is_before => Some(primitive),
443                        _ => None,
444                    }
445                }),
446        );
447        out
448    };
449
450    // The command records into a scope this consumer owns, so the marker
451    // count travels with the recording instead of through a side channel.
452    fn record_into(
453        func: &DrawCommandFn,
454        size: Size,
455        recording: CommandRecording,
456        storage: Vec<DrawPrimitive>,
457        replay: &mut Option<&mut cranpose_ui_graphics::CommandReplayState>,
458        command: Option<crate::graph::DrawCommandId>,
459    ) -> (
460        FinishedRecording,
461        Option<cranpose_ui_graphics::CommandReplayFrame>,
462    ) {
463        let mut scope = cranpose_ui::command_draw_scope_retained(size, recording, storage);
464        func(&mut scope);
465        let Some(state) = replay.as_mut() else {
466            return (scope.finish(), None);
467        };
468        let outcome =
469            state.advance_pooled(scope.recorded(), crate::scene_builder::verify_executor());
470        // Span counts are taken before `finish_replay` consumes the outcome
471        // (recolor patch lists move into the frame instead of being cloned
472        // every frame).
473        let diag = if cranpose_core::env_flag!("CRANPOSE_COMMAND_REPLAY_DIAG") {
474            if let cranpose_ui_graphics::ReplayOutcome::Spans(spans) = &outcome {
475                let (mut retained, mut dynamic) = (0usize, 0usize);
476                for span in spans {
477                    match span {
478                        cranpose_ui_graphics::ReplaySpan::Retained { .. } => retained += 1,
479                        cranpose_ui_graphics::ReplaySpan::Dynamic {
480                            tape_start,
481                            tape_end,
482                        } => dynamic += tape_end - tape_start,
483                    }
484                }
485                Some((
486                    scope.recorded().len(),
487                    retained,
488                    dynamic,
489                    state.segments().len(),
490                    state.stats(),
491                    state.optimistic_commits(),
492                    state.prefix_commits(),
493                ))
494            } else {
495                None
496            }
497        } else {
498            None
499        };
500        // Rate-limited engagement line, always on: the watch cannot take
501        // flag-gated diagnostics (its logcat is the only channel), and
502        // whether the pooled and prefix-commit fast paths engage on-device
503        // must be provable from a plain measurement window. warn level:
504        // the platform loggers filter info on desktop.
505        if !state.segments().is_empty() {
506            thread_local! {
507                static VERIFIED_FRAMES: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
508            }
509            let frames = VERIFIED_FRAMES.with(|cell| {
510                let next = cell.get().wrapping_add(1);
511                cell.set(next);
512                next
513            });
514            if frames.is_multiple_of(256) {
515                let (deaths, splits) = state.stats();
516                log::warn!(
517                    "[command-replay] health: {} segments, pooled commits {} + prefix {}, \
518                     lifetime deaths {} splits {}",
519                    state.segments().len(),
520                    state.optimistic_commits(),
521                    state.prefix_commits(),
522                    deaths,
523                    splits,
524                );
525            }
526        }
527        let center = state.center();
528        // Only spans whose retained buffer the renderer has confirmed may
529        // skip materialization: everything else must exist as primitives
530        // for this frame's ordinary path. A marker-bearing recording drops
531        // its frame after the split, so it must materialize whole.
532        let markers = scope.content_marker_count();
533        let mut bypass = |slot: u32| {
534            markers == 0
535                && command.is_some_and(|id| crate::scene_builder::retained_slot_confirmed(id, slot))
536        };
537        let (finished, frame) = scope.finish_replay(center, outcome, &mut bypass);
538        if let Some((records, retained, dynamic, segments, (deaths, splits), pooled, prefix)) = diag
539        {
540            log::warn!(
541                "[command-replay] {} records: {} retained spans, {} dynamic records, \
542                 {} materialized; {} segments alive, lifetime deaths {} splits {}, \
543                 pooled commits {} + prefix {}",
544                records,
545                retained,
546                dynamic,
547                finished.primitives.len(),
548                segments,
549                deaths,
550                splits,
551                pooled,
552                prefix,
553            );
554        }
555        (finished, frame)
556    }
557    match (placement, command) {
558        (DrawPlacement::Behind, DrawCommand::Behind(func)) => {
559            let (finished, frame) = record_into(func, size, recording, storage, replay, command_id);
560            let frame = (finished.content_markers == 0).then_some(frame).flatten();
561            (
562                filter_content(finished.primitives, finished.content_markers),
563                finished.recording,
564                frame,
565            )
566        }
567        (DrawPlacement::Overlay, DrawCommand::Overlay(func)) => {
568            let (finished, frame) = record_into(func, size, recording, storage, replay, command_id);
569            let frame = (finished.content_markers == 0).then_some(frame).flatten();
570            (
571                filter_content(finished.primitives, finished.content_markers),
572                finished.recording,
573                frame,
574            )
575        }
576        (_, DrawCommand::WithContent(func)) => {
577            // Content splitting reindexes the vector; the frame's ranges
578            // would not survive it. No id means no bypass either.
579            let (finished, _) = record_into(func, size, recording, storage, replay, None);
580            (
581                split_with_content(finished.primitives, placement, finished.content_markers),
582                finished.recording,
583                None,
584            )
585        }
586        _ => (Vec::new(), recording, None),
587    }
588}
589
590#[cfg(test)]
591mod tests {
592    use super::*;
593    use cranpose_ui_graphics::{DrawScope, DrawScopeDefault, Rect};
594
595    fn recorded_command(record: impl Fn(&mut dyn DrawScope) + 'static) -> DrawCommandFn {
596        Rc::new(move |scope: &mut DrawScopeDefault| record(scope))
597    }
598
599    fn rect_at(x: f32) -> Rect {
600        Rect {
601            x,
602            y: 0.0,
603            width: 1.0,
604            height: 1.0,
605        }
606    }
607
608    fn rect_xs(primitives: &[DrawPrimitive]) -> Vec<f32> {
609        primitives
610            .iter()
611            .map(|primitive| match primitive {
612                DrawPrimitive::Rect { rect, .. } => rect.x,
613                other => panic!("unexpected primitive {other:?}"),
614            })
615            .collect()
616    }
617
618    #[test]
619    fn marker_free_recording_passes_through() {
620        let command = DrawCommand::Behind(recorded_command(|scope| {
621            scope.draw_rect_at(rect_at(1.0), Brush::solid(Color::WHITE));
622            scope.draw_rect_at(rect_at(2.0), Brush::solid(Color::WHITE));
623        }));
624        let out = primitives_for_placement(&command, DrawPlacement::Behind, Size::new(10.0, 10.0));
625        assert_eq!(rect_xs(&out), [1.0, 2.0]);
626    }
627
628    #[test]
629    fn recorded_markers_still_split_content_placements() {
630        let with_content = recorded_command(|scope| {
631            scope.draw_rect_at(rect_at(1.0), Brush::solid(Color::WHITE));
632            scope.draw_content();
633            scope.draw_rect_at(rect_at(2.0), Brush::solid(Color::WHITE));
634        });
635        let command = DrawCommand::WithContent(with_content);
636        let size = Size::new(10.0, 10.0);
637        let behind = primitives_for_placement(&command, DrawPlacement::Behind, size);
638        assert_eq!(rect_xs(&behind), [1.0]);
639        let overlay = primitives_for_placement(&command, DrawPlacement::Overlay, size);
640        assert_eq!(rect_xs(&overlay), [2.0]);
641    }
642
643    /// Recording into a previously used buffer must be indistinguishable
644    /// from recording into a fresh one — for every placement, including the
645    /// marker-splitting paths.
646    #[test]
647    fn reused_storage_records_identically_to_fresh() {
648        let command = DrawCommand::WithContent(recorded_command(|scope| {
649            scope.draw_rect_at(rect_at(1.0), Brush::solid(Color::WHITE));
650            scope.draw_content();
651            scope.draw_rect_at(rect_at(2.0), Brush::solid(Color::WHITE));
652        }));
653        let size = Size::new(10.0, 10.0);
654        for placement in [DrawPlacement::Behind, DrawPlacement::Overlay] {
655            let fresh = primitives_for_placement(&command, placement, size);
656            // Junk in the reused buffer must not leak into the recording.
657            let dirty = vec![DrawPrimitive::Content; 8];
658            let reused = primitives_for_placement_reusing(&command, placement, size, dirty);
659            assert_eq!(fresh, reused);
660        }
661    }
662
663    #[test]
664    fn pushed_batches_keep_marker_count_authoritative() {
665        // A pre-built vector enters through `push_recorded`, which counts the
666        // `Content` marker it carries; the filter path must then strip it.
667        let command = DrawCommand::Behind(Rc::new(|scope: &mut DrawScopeDefault| {
668            scope.push_recorded(vec![
669                DrawPrimitive::Rect {
670                    rect: rect_at(1.0),
671                    brush: Brush::solid(Color::WHITE),
672                    stroke: None,
673                },
674                DrawPrimitive::Content,
675                DrawPrimitive::Rect {
676                    rect: rect_at(2.0),
677                    brush: Brush::solid(Color::WHITE),
678                    stroke: None,
679                },
680            ]);
681        }));
682        let out = primitives_for_placement(&command, DrawPlacement::Behind, Size::new(10.0, 10.0));
683        assert_eq!(rect_xs(&out), [1.0, 2.0]);
684    }
685}