1use kurbo::{Affine, Shape};
2pub use pax_engine::api::Size;
3use pax_message::AppleLiquidGlassPatch;
4use pax_runtime::api as pax_runtime_api;
5use pax_runtime::api::RenderContext;
6use pax_runtime::{ExpandedNode, RuntimeContext};
7
8pub(crate) fn to_kurbo_point(x: Size, y: Size, bounds: (f64, f64)) -> kurbo::Point {
10 let x = x.evaluate(bounds, pax_engine::api::Axis::X);
11 let y = y.evaluate(bounds, pax_engine::api::Axis::Y);
12 kurbo::Point { x, y }
13}
14
15pub fn patch_if_needed<T: PartialEq + Clone>(
17 old_state: &mut Option<T>,
18 patch: &mut Option<T>,
19 new_value: T,
20) -> bool {
21 if !old_state.as_ref().is_some_and(|v| v == &new_value) {
22 *patch = Some(new_value.clone());
23 *old_state = Some(new_value);
24 true
25 } else {
26 false
27 }
28}
29
30pub fn patch_liquid_glass_if_needed(
32 old_state: &mut Option<Option<AppleLiquidGlassPatch>>,
33 patch: &mut Option<Option<AppleLiquidGlassPatch>>,
34 expanded_node: &ExpandedNode,
35) -> bool {
36 patch_if_needed(
37 old_state,
38 patch,
39 expanded_node
40 .liquid_glass_scope
41 .get()
42 .map(|scope| scope.to_message()),
43 )
44}
45
46pub fn native_surface_opacity(expanded_node: &ExpandedNode, context: &RuntimeContext) -> f64 {
48 let opacity = expanded_node.computed_opacity.get().clamp(0.0, 1.0);
49 let Some(parent_frame_id) = expanded_node.parent_frame.get() else {
50 return opacity;
51 };
52 let Some(parent_frame) = context.get_expanded_node_by_eid(parent_frame_id) else {
53 return opacity;
54 };
55 let parent_opacity = parent_frame.computed_opacity.get().clamp(0.0, 1.0);
56 if parent_opacity <= f64::EPSILON {
57 0.0
58 } else {
59 (opacity / parent_opacity).clamp(0.0, 1.0)
60 }
61}
62
63pub fn canvas_surface_transform(expanded_node: &ExpandedNode, context: &RuntimeContext) -> Affine {
65 if expanded_node.is_render_source() {
66 return Affine::from(expanded_node.transform_and_bounds.get().transform);
67 }
68 context.canvas_surface_transform_for_node(expanded_node)
69}
70
71pub struct CanvasNodeRenderScope {
73 pub layer_id: usize,
74 pub node_id: u32,
75 pub surface_transform: Affine,
76 pub bounds: (f64, f64),
77 pub paint_opacity: f64,
79}
80
81pub fn begin_bounded_canvas_node(
86 rc: &mut dyn RenderContext,
87 expanded_node: &ExpandedNode,
88 context: &RuntimeContext,
89) -> Option<CanvasNodeRenderScope> {
90 let geometry = context.canvas_geometry_for_node(expanded_node);
91 let layer_id = rc.alpha_source_layer().unwrap_or(geometry.layer);
92 let node_id = expanded_node.id.to_u32();
93 let coverage_bounds = geometry.coverage_bounds.unwrap_or(kurbo::Rect::new(
94 f64::NEG_INFINITY,
95 f64::NEG_INFINITY,
96 f64::INFINITY,
97 f64::INFINITY,
98 ));
99
100 if !rc.begin_node_with_bounds(
101 layer_id,
102 node_id,
103 expanded_node.occlusion.get().z_index,
104 coverage_bounds,
105 context.canvas_node_light_mask(expanded_node.id),
106 ) {
107 if rc.take_clean_skipped_node(layer_id, node_id) {
108 context.clear_canvas_node_dirty(&expanded_node.id);
109 }
110 return None;
111 }
112
113 let paint_opacity = if rc.supports_subtree_opacity() {
114 rc.set_node_opacity_scopes(
115 layer_id,
116 node_id,
117 &expanded_node.computed_opacity_scopes.get(),
118 );
119 1.0
120 } else {
121 expanded_node.computed_opacity.get()
122 };
123 Some(CanvasNodeRenderScope {
124 layer_id,
125 node_id,
126 surface_transform: geometry.surface_transform,
127 bounds: geometry.bounds,
128 paint_opacity,
129 })
130}
131pub(crate) fn alpha_mask_paints(
133 node: &ExpandedNode,
134 path: kurbo::BezPath,
135 fill: Vec<pax_runtime_api::Fill>,
136 stroke: Vec<pax_runtime_api::Stroke>,
137) -> Vec<pax_runtime_api::AlphaMaskPaint> {
138 alpha_mask_paints_with_range(node, path, fill, stroke, 0.0, 1.0)
139}
140
141pub(crate) fn alpha_mask_paints_with_range(
142 node: &ExpandedNode,
143 path: kurbo::BezPath,
144 fills: Vec<pax_runtime_api::Fill>,
145 strokes: Vec<pax_runtime_api::Stroke>,
146 draw_start: f64,
147 draw_end: f64,
148) -> Vec<pax_runtime_api::AlphaMaskPaint> {
149 use pax_runtime_api::drawing::{path_trim::trim_bez_path, stroke_utils::stroked_outline_path};
150 let transform = kurbo::Affine::from(node.transform_and_bounds.get().transform);
151 let mut paints = Vec::new();
152 for fill in fills.iter().rev() {
153 paints.push(pax_runtime_api::AlphaMaskPaint {
154 path: path.clone(),
155 transform,
156 fill: fill.paint.local().get(),
157 opacity: fill.opacity.local().get().to_float_0_1(),
158 paint_bounds: path.bounding_box(),
159 composition: Some((node.id.to_u32(), 1.0)),
160 });
161 }
162 let paint_bounds = path.bounding_box();
163 let trimmed = trim_bez_path(&path, draw_start, draw_end);
164 for stroke in strokes.iter().rev() {
165 if let Some(path) = stroked_outline_path(&trimmed, &stroke.resolve_in_graph()) {
166 paints.push(pax_runtime_api::AlphaMaskPaint {
167 path,
168 transform,
169 fill: stroke.paint.local().get(),
170 opacity: stroke.opacity.local().get().to_float_0_1(),
171 paint_bounds,
172 composition: Some((node.id.to_u32(), 1.0)),
173 });
174 }
175 }
176 paints
177}
178
179pub(crate) fn draw_appearance(
180 rc: &mut dyn RenderContext,
181 layer: usize,
182 path: kurbo::BezPath,
183 fills: &[pax_runtime_api::Fill],
184 strokes: &[pax_runtime_api::Stroke],
185 opacity: f64,
186 smoothing: pax_runtime_api::PathSmoothing,
187 draw_start: f64,
188 draw_end: f64,
189) {
190 for fill in fills.iter().rev() {
191 rc.fill_with_material_and_opacity_and_smoothing(
192 layer,
193 path.clone(),
194 &fill.paint.local().get(),
195 &fill.material.local().get().resolve_in_graph(),
196 opacity * fill.opacity.local().get().to_float_0_1(),
197 smoothing,
198 );
199 }
200 for stroke in strokes
201 .iter()
202 .rev()
203 .filter(|stroke| stroke.resolve_in_graph().width_pixels() > f64::EPSILON)
204 {
205 if draw_start < draw_end {
206 rc.stroke_with_draw_range_and_material_and_opacity_and_smoothing(
207 layer,
208 path.clone(),
209 &stroke.resolve_in_graph(),
210 &stroke.material.local().get().resolve_in_graph(),
211 opacity * stroke.opacity.local().get().to_float_0_1(),
212 draw_start,
213 draw_end,
214 smoothing,
215 );
216 }
217 }
218}
219
220pub(crate) fn appearance_coverage_path(
221 path: &kurbo::BezPath,
222 fills: &[pax_runtime_api::Fill],
223 strokes: &[pax_runtime_api::Stroke],
224) -> kurbo::BezPath {
225 let mut coverage = kurbo::BezPath::new();
226 if fills.iter().any(|fill| {
227 fill.paint.local().get().coverage_alpha_0_1() * fill.opacity.local().get().to_float_0_1()
228 > f64::EPSILON
229 }) {
230 coverage.extend(path.elements().iter().copied());
231 }
232 for stroke in strokes {
233 if stroke.paint.local().get().coverage_alpha_0_1()
234 * stroke.opacity.local().get().to_float_0_1()
235 > f64::EPSILON
236 {
237 if let Some(outline) = pax_runtime_api::drawing::stroke_utils::stroked_outline_path(
238 path,
239 &stroke.resolve_in_graph(),
240 ) {
241 coverage.extend(outline.elements().iter().copied());
242 }
243 }
244 }
245 coverage
246}
247
248pub(crate) fn appearance_coverage_alpha(
249 fills: &[pax_runtime_api::Fill],
250 strokes: &[pax_runtime_api::Stroke],
251) -> f64 {
252 fills
255 .iter()
256 .map(|fill| {
257 fill.paint.local().get().coverage_alpha_0_1()
258 * fill.opacity.local().get().to_float_0_1()
259 })
260 .chain(
261 strokes
262 .iter()
263 .filter(|s| s.resolve_in_graph().width_pixels() > 0.0)
264 .map(|s| {
265 s.paint.local().get().coverage_alpha_0_1()
266 * s.opacity.local().get().to_float_0_1()
267 }),
268 )
269 .filter(|alpha| *alpha > f64::EPSILON)
270 .reduce(f64::min)
271 .unwrap_or(0.0)
272}
273
274pub(crate) fn watch_appearance(
278 node: &std::rc::Rc<ExpandedNode>,
279 context: &std::rc::Rc<RuntimeContext>,
280 fills: pax_runtime_api::LocalProperty<Vec<pax_runtime_api::Fill>>,
281 strokes: pax_runtime_api::LocalProperty<Vec<pax_runtime_api::Stroke>>,
282) -> pax_runtime_api::LocalProperty<()> {
283 use pax_runtime_api::{LocalProperty, Material};
284 use std::{cell::RefCell, rc::Rc};
285 let deps = [fills.untyped(), strokes.untyped()];
286 let weak_node = Rc::downgrade(node);
287 let weak_context = Rc::downgrade(context);
288 let watchers = RefCell::new(Vec::<LocalProperty<()>>::new());
289 let appearance = LocalProperty::computed(
290 move || {
291 let (Some(node), Some(context)) = (weak_node.upgrade(), weak_context.upgrade()) else {
292 return;
293 };
294 let mut layers = Vec::new();
295 for fill in fills.get() {
296 layers.push((
297 fill.material.local(),
298 vec![
299 fill.paint.local().untyped(),
300 fill.material.local().untyped(),
301 fill.opacity.local().untyped(),
302 ],
303 ));
304 }
305 for stroke in strokes.get() {
306 layers.push((
307 stroke.material.local(),
308 vec![
309 stroke.paint.local().untyped(),
310 stroke.material.local().untyped(),
311 stroke.opacity.local().untyped(),
312 stroke.width.local().untyped(),
313 stroke.cap.local().untyped(),
314 stroke.join.local().untyped(),
315 ],
316 ));
317 }
318 let mut next = Vec::new();
319 for (material, dependencies) in layers {
320 let weak_node = Rc::downgrade(&node);
321 let weak_context = Rc::downgrade(&context);
322 let material_watch = LocalProperty::default();
323 let watch = LocalProperty::computed(
324 move || {
325 let (Some(node), Some(context)) =
326 (weak_node.upgrade(), weak_context.upgrade())
327 else {
328 return;
329 };
330 let material_dependencies = match material.get() {
331 Material::Unlit => vec![],
332 Material::Lit(p) => vec![
333 p.ambient.local().untyped(),
334 p.diffuse.local().untyped(),
335 p.specular.local().untyped(),
336 p.roughness.local().untyped(),
337 p.metallic.local().untyped(),
338 p.emissive.local().untyped(),
339 p.emissive_intensity.local().untyped(),
340 ],
341 };
342 let weak_node = Rc::downgrade(&node);
343 let weak_context = Rc::downgrade(&context);
344 material_watch.replace_with(LocalProperty::computed(
345 move || {
346 if let (Some(node), Some(context)) =
347 (weak_node.upgrade(), weak_context.upgrade())
348 {
349 node.changed_listener.invalidate();
350 context.mark_canvas_node_dirty(node.id);
351 context.mark_node_occlusion_dirty(node.id.to_u32());
352 }
353 },
354 &material_dependencies,
355 ));
356 context.register_expanded_node_effect_property_named(
357 &node,
358 &material_watch,
359 "appearance material",
360 );
361 material_watch.get();
362 },
363 &dependencies,
364 );
365 context.register_expanded_node_effect_property_named(
366 &node,
367 &watch,
368 "appearance layer",
369 );
370 watch.get();
371 next.push(watch);
372 }
373 *watchers.borrow_mut() = next;
374 },
375 &deps,
376 );
377 context.register_expanded_node_effect_property_named(node, &appearance, "appearance stack");
380 appearance.get();
381 appearance
382}
383
384pub(crate) fn stroke_coverage_bounds(
386 bounds: kurbo::Rect,
387 strokes: &[pax_runtime_api::Stroke],
388) -> kurbo::Rect {
389 let pad = strokes
390 .iter()
391 .map(|stroke| 2.0 * stroke.resolve_in_graph().width_pixels())
392 .fold(0.0, f64::max);
393 bounds.inflate(pad, pad)
394}
395
396pub(crate) fn native_stroke_color(stroke: &pax_runtime_api::Stroke) -> pax_runtime_api::Color {
399 match stroke.paint.local().get() {
400 pax_runtime_api::Paint::Solid(color) => color,
401 _ => {
402 log::warn!("Native control outlines require solid paint; gradient stroke omitted");
403 pax_runtime_api::Color::TRANSPARENT
404 }
405 }
406}