carnelian/render/
dynamic.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::color::Color;
use crate::render::generic::forma::*;
use crate::render::generic::{
    self, Composition as _, Context as _, PathBuilder as _, Raster as _, RasterBuilder as _,
};
pub use crate::render::generic::{BlendMode, Fill, FillRule, Gradient, GradientType, Order, Style};
use crate::{Point, ViewAssistantContext};
use anyhow::Error;
use display_utils::PixelFormat;
use euclid::default::{Point2D, Rect, Size2D, Transform2D, Vector2D};
use euclid::{point2, size2};
use std::io::Read;
use std::ops::Add;
use std::{mem, u32};
/// Rendering context and API start point.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Image {
    inner: ImageInner,
}
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
enum ImageInner {
    Forma(FormaImage),
}
/// Rectangular copy region.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CopyRegion {
    /// Top-left origin of source rectangle.
    pub src_offset: Point2D<u32>,
    /// Top-left origin of destination rectangle.
    pub dst_offset: Point2D<u32>,
    /// Size of both source and destination rectangles.
    pub extent: Size2D<u32>,
}
/// Rendering extensions.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct RenderExt {
    /// Clears render image before rendering.
    pub pre_clear: Option<PreClear>,
    /// Copies from source image to render image before rendering.
    pub pre_copy: Option<PreCopy>,
    /// Copies from render image to destination image after rendering.
    pub post_copy: Option<PostCopy>,
}
/// Pre-render image clear.
#[derive(Clone, Debug, PartialEq)]
pub struct PreClear {
    /// Clear color.
    pub color: Color,
}
/// Pre-render image copy.
#[derive(Clone, Debug, PartialEq)]
pub struct PreCopy {
    /// Source image to copy from.
    pub image: Image,
    /// Copy region properties.
    pub copy_region: CopyRegion,
}
/// Post-render image copy.
#[derive(Clone, Debug, PartialEq)]
pub struct PostCopy {
    /// Destination image to copy to. Must be different from render image.
    pub image: Image,
    /// Copy region properties.
    pub copy_region: CopyRegion,
}
/// Rendering context and API start point.
#[derive(Debug)]
pub struct Context {
    pub inner: ContextInner,
}
#[derive(Debug)]
pub enum ContextInner {
    Forma(FormaContext),
}
impl Context {
    /// Returns the context's pixel format.
    pub fn pixel_format(&self) -> PixelFormat {
        match &self.inner {
            ContextInner::Forma(context) => context.pixel_format(),
        }
    }
    /// Optionally returns a `PathBuilder`. May return `None` of old builder is still alive.
    pub fn path_builder(&self) -> Option<PathBuilder> {
        match &self.inner {
            ContextInner::Forma(context) => context
                .path_builder()
                .map(|path_builder| PathBuilder { inner: PathBuilderInner::Forma(path_builder) }),
        }
    }
    /// Optionally returns a `RasterBuilder`. May return `None` of old builder is still alive.
    pub fn raster_builder(&self) -> Option<RasterBuilder> {
        match &self.inner {
            ContextInner::Forma(context) => context.raster_builder().map(|raster_builder| {
                RasterBuilder { inner: RasterBuilderInner::Forma(raster_builder) }
            }),
        }
    }
    /// Creates a new image with `size`.
    pub fn new_image(&mut self, size: Size2D<u32>) -> Image {
        match &mut self.inner {
            ContextInner::Forma(ref mut context) => {
                Image { inner: ImageInner::Forma(context.new_image(size)) }
            }
        }
    }
    /// Creates a new image from PNG `reader`.
    pub fn new_image_from_png<R: Read>(
        &mut self,
        reader: &mut png::Reader<R>,
    ) -> Result<Image, Error> {
        Ok(Image {
            inner: match &mut self.inner {
                ContextInner::Forma(ref mut context) => {
                    ImageInner::Forma(context.new_image_from_png(reader)?)
                }
            },
        })
    }
    /// Returns the image at `image_index`.
    pub fn get_image(&mut self, image_index: u32) -> Image {
        match &mut self.inner {
            ContextInner::Forma(ref mut render_context) => {
                Image { inner: ImageInner::Forma(render_context.get_image(image_index)) }
            }
        }
    }
    /// Returns the `context`'s current image.
    pub fn get_current_image(&mut self, context: &ViewAssistantContext) -> Image {
        match &mut self.inner {
            ContextInner::Forma(ref mut render_context) => {
                Image { inner: ImageInner::Forma(render_context.get_current_image(context)) }
            }
        }
    }

    /// Renders the composition with an optional clip to the image.
    pub fn render(
        &mut self,
        composition: &mut Composition,
        clip: Option<Rect<u32>>,
        image: Image,
        ext: &RenderExt,
    ) {
        self.render_with_clip(
            composition,
            clip.unwrap_or_else(|| {
                Rect::new(point2(u32::MIN, u32::MIN), size2(u32::MAX, u32::MAX))
            }),
            image,
            ext,
        );
    }
    /// Renders the composition with a clip to the image.
    pub fn render_with_clip(
        &mut self,
        composition: &mut Composition,
        clip: Rect<u32>,
        image: Image,
        ext: &RenderExt,
    ) {
        let background_color = composition.background_color;

        match &mut self.inner {
            ContextInner::Forma(ref mut context) => {
                let ImageInner::Forma(image) = image.inner;
                let ext = generic::RenderExt {
                    pre_clear: ext
                        .pre_clear
                        .clone()
                        .map(|pre_clear| generic::PreClear { color: pre_clear.color }),
                    pre_copy: ext.pre_copy.clone().map(|pre_copy| generic::PreCopy {
                        image: image,
                        copy_region: generic::CopyRegion {
                            src_offset: pre_copy.copy_region.src_offset,
                            dst_offset: pre_copy.copy_region.dst_offset,
                            extent: pre_copy.copy_region.extent,
                        },
                    }),
                    post_copy: ext.post_copy.clone().map(|post_copy| generic::PostCopy {
                        image: image,
                        copy_region: generic::CopyRegion {
                            src_offset: post_copy.copy_region.src_offset,
                            dst_offset: post_copy.copy_region.dst_offset,
                            extent: post_copy.copy_region.extent,
                        },
                    }),
                };
                composition.with_inner_composition(|inner| {
                    let mut composition = match inner {
                        CompositionInner::Forma(composition) => composition,
                        CompositionInner::Empty => FormaComposition::new(background_color),
                    };
                    context.render_with_clip(&mut composition, clip, image, &ext);
                    CompositionInner::Forma(composition)
                });
            }
        }
    }
}
/// Closed path built by a `PathBuilder`.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Path {
    inner: PathInner,
}
#[derive(Clone, Debug, Eq, PartialEq)]
enum PathInner {
    Forma(FormaPath),
}
/// Builds one closed path.
#[derive(Debug)]
pub struct PathBuilder {
    inner: PathBuilderInner,
}
#[derive(Debug)]
enum PathBuilderInner {
    Forma(FormaPathBuilder),
}
impl PathBuilder {
    /// Move end-point to.
    pub fn move_to(&mut self, point: Point) -> &mut Self {
        match &mut self.inner {
            PathBuilderInner::Forma(ref mut path_builder) => {
                path_builder.move_to(point);
            }
        }
        self
    }
    /// Create line from end-point to point and update end-point.
    pub fn line_to(&mut self, point: Point) -> &mut Self {
        match &mut self.inner {
            PathBuilderInner::Forma(ref mut path_builder) => {
                path_builder.line_to(point);
            }
        }
        self
    }
    /// Create quadratic Bézier from end-point to `p2` with `p1` as control point.
    pub fn quad_to(&mut self, p1: Point, p2: Point) -> &mut Self {
        match &mut self.inner {
            PathBuilderInner::Forma(ref mut path_builder) => {
                path_builder.quad_to(p1, p2);
            }
        }
        self
    }
    /// Create cubic Bézier from end-point to `p3` with `p1` and `p2` as control points.
    pub fn cubic_to(&mut self, p1: Point, p2: Point, p3: Point) -> &mut Self {
        match &mut self.inner {
            PathBuilderInner::Forma(ref mut path_builder) => {
                path_builder.cubic_to(p1, p2, p3);
            }
        }
        self
    }
    /// Create rational quadratic Bézier from end-point to `p2` with `p1` as control point
    /// and `w` as its weight.
    pub fn rat_quad_to(&mut self, p1: Point, p2: Point, w: f32) -> &mut Self {
        match &mut self.inner {
            PathBuilderInner::Forma(ref mut path_builder) => {
                path_builder.rat_quad_to(p1, p2, w);
            }
        }
        self
    }
    /// Create rational cubic Bézier from end-point to `p3` with `p1` and `p2` as control
    /// points, and `w1` and `w2` their weights.
    pub fn rat_cubic_to(&mut self, p1: Point, p2: Point, p3: Point, w1: f32, w2: f32) -> &mut Self {
        match &mut self.inner {
            PathBuilderInner::Forma(ref mut path_builder) => {
                path_builder.rat_cubic_to(p1, p2, p3, w1, w2);
            }
        }
        self
    }
    /// Closes the path with a line if not yet closed and builds the path.
    ///
    /// Consumes the builder; another one can be requested from the `Context`.
    pub fn build(self) -> Path {
        match self.inner {
            PathBuilderInner::Forma(path_builder) => {
                Path { inner: PathInner::Forma(path_builder.build()) }
            }
        }
    }
}
/// Raster built by a `RasterBuilder`.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Raster {
    inner: RasterInner,
}
#[derive(Clone, Debug, Eq, PartialEq)]
enum RasterInner {
    Forma(FormaRaster),
}
impl Raster {
    /// Translate raster.
    pub fn translate(self, translation: Vector2D<i32>) -> Self {
        match self.inner {
            RasterInner::Forma(raster) => {
                Raster { inner: RasterInner::Forma(raster.translate(translation)) }
            }
        }
    }
}
impl Add for Raster {
    type Output = Self;
    fn add(self, other: Self) -> Self::Output {
        let RasterInner::Forma(other_raster) = other.inner;
        match self.inner {
            RasterInner::Forma(raster) => {
                Raster { inner: RasterInner::Forma(raster + other_raster) }
            }
        }
    }
}
/// Builds one Raster.
#[derive(Debug)]
pub struct RasterBuilder {
    inner: RasterBuilderInner,
}
#[derive(Debug)]
enum RasterBuilderInner {
    Forma(FormaRasterBuilder),
}
impl RasterBuilder {
    /// Add a path to the raster with optional transform.
    pub fn add(&mut self, path: &Path, transform: Option<&Transform2D<f32>>) -> &mut Self {
        #[allow(clippy::or_fun_call)] // TODO(https://fxbug.dev/379717231)
        self.add_with_transform(path, transform.unwrap_or(&Transform2D::identity()))
    }
    /// Add a path to the raster with transform.
    pub fn add_with_transform(&mut self, path: &Path, transform: &Transform2D<f32>) -> &mut Self {
        match &mut self.inner {
            RasterBuilderInner::Forma(ref mut raster_builder) => {
                let PathInner::Forma(path) = &path.inner;
                raster_builder.add_with_transform(path, transform);
            }
        }
        self
    }
    /// Builds the raster.
    ///
    /// Consumes the builder; another one can be requested from the `Context`.
    pub fn build(self) -> Raster {
        match self.inner {
            RasterBuilderInner::Forma(raster_builder) => {
                Raster { inner: RasterInner::Forma(raster_builder.build()) }
            }
        }
    }
}

#[derive(Clone, Debug)]
pub struct Layer {
    /// Layer raster.
    pub raster: Raster,
    /// Layer clip.
    pub clip: Option<Raster>,
    /// Layer style.
    pub style: Style, // Will also contain txty when available.
}
#[derive(Debug)]
/// A group of ordered layers.
pub struct Composition {
    inner: CompositionInner,
    background_color: Color,
}
#[derive(Debug)]
enum CompositionInner {
    Forma(FormaComposition),
    Empty,
}
impl Composition {
    /// Creates a composition of ordered layers where the layers with lower index appear on top.
    pub fn new(background_color: Color) -> Self {
        Self { inner: CompositionInner::Empty, background_color }
    }
    fn with_inner_composition(&mut self, mut f: impl FnMut(CompositionInner) -> CompositionInner) {
        let inner = f(mem::replace(&mut self.inner, CompositionInner::Empty));
        self.inner = inner;
    }
    /// Resets composition by removing all layers.
    pub fn clear(&mut self) {
        match &mut self.inner {
            CompositionInner::Forma(composition) => composition.clear(),
            CompositionInner::Empty => (),
        }
    }
    /// Insert layer into composition.
    pub fn insert(&mut self, order: Order, layer: Layer) {
        if let CompositionInner::Empty = self.inner {
            match layer {
                Layer { raster: Raster { inner: RasterInner::Forma(_) }, .. } => {
                    self.inner =
                        CompositionInner::Forma(FormaComposition::new(self.background_color));
                }
            }
        }
        match &mut self.inner {
            CompositionInner::Forma(composition) => composition.insert(
                order,
                generic::Layer {
                    raster: {
                        let RasterInner::Forma(raster) = layer.raster.inner;
                        raster
                    },
                    clip: layer.clip.map(|clip| {
                        let RasterInner::Forma(clip) = clip.inner;
                        clip
                    }),
                    style: layer.style,
                },
            ),
            _ => unreachable!(),
        }
    }
    /// Remove layer from composition.
    pub fn remove(&mut self, order: Order) {
        match &mut self.inner {
            CompositionInner::Forma(composition) => composition.remove(order),
            _ => (),
        }
    }
}