forma/composition/
layer.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
// Copyright 2022 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 surpass::painter::Props;
use surpass::{GeomId, GeomPresTransform, Order, Path};

use crate::small_bit_set::SmallBitSet;

use super::interner::Interned;
use super::state::LayerSharedState;

#[derive(Debug)]
pub struct Layer {
    pub(crate) inner: surpass::Layer,
    pub(crate) shared_state: LayerSharedState,
    pub(crate) geom_id: GeomId,
    pub(crate) props: Interned<Props>,
    pub(crate) is_unchanged: SmallBitSet,
    pub(crate) lines_count: usize,
}

impl Layer {
    pub fn insert(&mut self, path: &Path) -> &mut Self {
        {
            let mut state = self.shared_state.inner();
            let builder = state.lines_builder.as_mut().expect("lines_builder should not be None");

            let old_len = builder.len();
            builder.push_path(self.geom_id, path);
            let len = builder.len() - old_len;

            state.geom_id_to_order.insert(self.geom_id, self.inner.order);

            self.lines_count += len;
        }

        self.is_unchanged.clear();
        self
    }

    pub fn clear(&mut self) -> &mut Self {
        {
            let mut state = self.shared_state.inner();

            state.geom_id_to_order.remove(&self.geom_id);

            self.geom_id = state.new_geom_id();
        }

        self.lines_count = 0;

        self.is_unchanged.clear();
        self
    }

    pub(crate) fn set_order(&mut self, order: Option<Order>) {
        if order.is_some() && self.inner.order != order {
            self.inner.order = order;
            self.is_unchanged.clear();
        }

        let geom_id = self.geom_id();
        self.shared_state.inner().geom_id_to_order.insert(geom_id, order);
    }

    pub fn geom_id(&self) -> GeomId {
        self.geom_id
    }

    pub(crate) fn is_unchanged(&self, cache_id: u8) -> bool {
        self.is_unchanged.contains(&cache_id)
    }

    pub(crate) fn set_is_unchanged(&mut self, cache_id: u8, is_unchanged: bool) -> bool {
        if is_unchanged {
            self.is_unchanged.insert(cache_id)
        } else {
            self.is_unchanged.remove(cache_id)
        }
    }

    #[inline]
    pub fn is_enabled(&self) -> bool {
        self.inner.is_enabled
    }

    #[inline]
    pub fn set_is_enabled(&mut self, is_enabled: bool) -> &mut Self {
        self.inner.is_enabled = is_enabled;
        self
    }

    #[inline]
    pub fn disable(&mut self) -> &mut Self {
        self.set_is_enabled(false)
    }

    #[inline]
    pub fn enable(&mut self) -> &mut Self {
        self.set_is_enabled(true)
    }

    #[inline]
    pub fn transform(&self) -> GeomPresTransform {
        self.inner.affine_transform.unwrap_or_default()
    }

    #[inline]
    pub fn set_transform(&mut self, transform: GeomPresTransform) -> &mut Self {
        // We want to perform a cheap check for the common case without hampering this function too
        // much.
        #[allow(clippy::float_cmp)]
        let affine_transform = if transform.is_identity() { None } else { Some(transform) };

        if self.inner.affine_transform != affine_transform {
            self.is_unchanged.clear();
            self.inner.affine_transform = affine_transform;
        }

        self
    }

    #[inline]
    pub fn props(&self) -> &Props {
        &self.props
    }

    #[inline]
    pub fn set_props(&mut self, props: Props) -> &mut Self {
        if *self.props != props {
            self.is_unchanged.clear();
            self.props = self.shared_state.inner().props_interner.get(props);
        }

        self
    }
}

impl Drop for Layer {
    fn drop(&mut self) {
        self.shared_state.inner().geom_id_to_order.remove(&self.geom_id);
    }
}