-
Notifications
You must be signed in to change notification settings - Fork 54
/
scene.rs
268 lines (237 loc) · 9.3 KB
/
scene.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use api::{BuiltDisplayList, ColorF, DynamicProperties, Epoch};
use api::{FilterOp, TempFilterData, FilterData, ComponentTransferFuncType};
use api::{PipelineId, PropertyBinding, PropertyBindingId, ItemRange, MixBlendMode, StackingContext};
use api::units::{LayoutSize, LayoutTransform};
use crate::internal_types::{FastHashMap, Filter};
use std::sync::Arc;
/// Stores a map of the animated property bindings for the current display list. These
/// can be used to animate the transform and/or opacity of a display list without
/// re-submitting the display list itself.
#[cfg_attr(feature = "capture", derive(Serialize))]
#[cfg_attr(feature = "replay", derive(Deserialize))]
pub struct SceneProperties {
transform_properties: FastHashMap<PropertyBindingId, LayoutTransform>,
float_properties: FastHashMap<PropertyBindingId, f32>,
current_properties: DynamicProperties,
pending_properties: Option<DynamicProperties>,
}
impl SceneProperties {
pub fn new() -> Self {
SceneProperties {
transform_properties: FastHashMap::default(),
float_properties: FastHashMap::default(),
current_properties: DynamicProperties::default(),
pending_properties: None,
}
}
/// Set the current property list for this display list.
pub fn set_properties(&mut self, properties: DynamicProperties) {
self.pending_properties = Some(properties);
}
/// Add to the current property list for this display list.
pub fn add_properties(&mut self, properties: DynamicProperties) {
let mut pending_properties = self.pending_properties
.take()
.unwrap_or_default();
pending_properties.transforms.extend(properties.transforms);
pending_properties.floats.extend(properties.floats);
self.pending_properties = Some(pending_properties);
}
/// Flush any pending updates to the scene properties. Returns
/// true if the properties have changed since the last flush
/// was called. This code allows properties to be changed by
/// multiple set_properties and add_properties calls during a
/// single transaction, and still correctly determine if any
/// properties have changed. This can have significant power
/// saving implications, allowing a frame build to be skipped
/// if the properties haven't changed in many cases.
pub fn flush_pending_updates(&mut self) -> bool {
let mut properties_changed = false;
if let Some(ref pending_properties) = self.pending_properties {
if *pending_properties != self.current_properties {
self.transform_properties.clear();
self.float_properties.clear();
for property in &pending_properties.transforms {
self.transform_properties
.insert(property.key.id, property.value);
}
for property in &pending_properties.floats {
self.float_properties
.insert(property.key.id, property.value);
}
self.current_properties = pending_properties.clone();
properties_changed = true;
}
}
properties_changed
}
/// Get the current value for a transform property.
pub fn resolve_layout_transform(
&self,
property: &PropertyBinding<LayoutTransform>,
) -> LayoutTransform {
match *property {
PropertyBinding::Value(value) => value,
PropertyBinding::Binding(ref key, v) => {
self.transform_properties
.get(&key.id)
.cloned()
.unwrap_or(v)
}
}
}
/// Get the current value for a float property.
pub fn resolve_float(
&self,
property: &PropertyBinding<f32>
) -> f32 {
match *property {
PropertyBinding::Value(value) => value,
PropertyBinding::Binding(ref key, v) => {
self.float_properties
.get(&key.id)
.cloned()
.unwrap_or(v)
}
}
}
pub fn float_properties(&self) -> &FastHashMap<PropertyBindingId, f32> {
&self.float_properties
}
}
/// A representation of the layout within the display port for a given document or iframe.
#[cfg_attr(feature = "capture", derive(Serialize))]
#[cfg_attr(feature = "replay", derive(Deserialize))]
#[derive(Clone)]
pub struct ScenePipeline {
pub pipeline_id: PipelineId,
pub viewport_size: LayoutSize,
pub content_size: LayoutSize,
pub background_color: Option<ColorF>,
pub display_list: BuiltDisplayList,
}
/// A complete representation of the layout bundling visible pipelines together.
#[cfg_attr(feature = "capture", derive(Serialize))]
#[cfg_attr(feature = "replay", derive(Deserialize))]
#[derive(Clone)]
pub struct Scene {
pub root_pipeline_id: Option<PipelineId>,
pub pipelines: FastHashMap<PipelineId, Arc<ScenePipeline>>,
pub pipeline_epochs: FastHashMap<PipelineId, Epoch>,
}
impl Scene {
pub fn new() -> Self {
Scene {
root_pipeline_id: None,
pipelines: FastHashMap::default(),
pipeline_epochs: FastHashMap::default(),
}
}
pub fn set_root_pipeline_id(&mut self, pipeline_id: PipelineId) {
self.root_pipeline_id = Some(pipeline_id);
}
pub fn get_display_list_for_pipeline(&self, pipeline_id: PipelineId) -> &BuiltDisplayList {
&self.pipelines
.get(&pipeline_id)
.expect("Expected to find display list for pipeline")
.display_list
}
pub fn set_display_list(
&mut self,
pipeline_id: PipelineId,
epoch: Epoch,
display_list: BuiltDisplayList,
background_color: Option<ColorF>,
viewport_size: LayoutSize,
content_size: LayoutSize,
) {
let new_pipeline = ScenePipeline {
pipeline_id,
viewport_size,
content_size,
background_color,
display_list,
};
self.pipelines.insert(pipeline_id, Arc::new(new_pipeline));
self.pipeline_epochs.insert(pipeline_id, epoch);
}
pub fn remove_pipeline(&mut self, pipeline_id: PipelineId) {
if self.root_pipeline_id == Some(pipeline_id) {
self.root_pipeline_id = None;
}
self.pipelines.remove(&pipeline_id);
self.pipeline_epochs.remove(&pipeline_id);
}
pub fn update_epoch(&mut self, pipeline_id: PipelineId, epoch: Epoch) {
self.pipeline_epochs.insert(pipeline_id, epoch);
}
pub fn has_root_pipeline(&self) -> bool {
if let Some(ref root_id) = self.root_pipeline_id {
return self.pipelines.contains_key(root_id);
}
false
}
}
pub trait StackingContextHelpers {
fn mix_blend_mode_for_compositing(&self) -> Option<MixBlendMode>;
fn filter_ops_for_compositing(
&self,
display_list: &BuiltDisplayList,
input_filters: ItemRange<FilterOp>,
) -> Vec<Filter>;
fn filter_datas_for_compositing(
&self,
display_list: &BuiltDisplayList,
input_filter_datas: &[TempFilterData],
) -> Vec<FilterData>;
}
impl StackingContextHelpers for StackingContext {
fn mix_blend_mode_for_compositing(&self) -> Option<MixBlendMode> {
match self.mix_blend_mode {
MixBlendMode::Normal => None,
_ => Some(self.mix_blend_mode),
}
}
fn filter_ops_for_compositing(
&self,
display_list: &BuiltDisplayList,
input_filters: ItemRange<FilterOp>,
) -> Vec<Filter> {
// TODO(gw): Now that we resolve these later on,
// we could probably make it a bit
// more efficient than cloning these here.
let mut filters = vec![];
for filter in display_list.get(input_filters) {
filters.push(filter.into());
}
filters
}
fn filter_datas_for_compositing(
&self,
display_list: &BuiltDisplayList,
input_filter_datas: &[TempFilterData],
) -> Vec<FilterData> {
// TODO(gw): Now that we resolve these later on,
// we could probably make it a bit
// more efficient than cloning these here.
let mut filter_datas = vec![];
for temp_filter_data in input_filter_datas {
let func_types : Vec<ComponentTransferFuncType> = display_list.get(temp_filter_data.func_types).collect();
debug_assert!(func_types.len() == 4);
filter_datas.push( FilterData {
func_r_type: func_types[0],
r_values: display_list.get(temp_filter_data.r_values).collect(),
func_g_type: func_types[1],
g_values: display_list.get(temp_filter_data.g_values).collect(),
func_b_type: func_types[2],
b_values: display_list.get(temp_filter_data.b_values).collect(),
func_a_type: func_types[3],
a_values: display_list.get(temp_filter_data.a_values).collect(),
});
}
filter_datas
}
}