/
webrender_helpers.rs
309 lines (281 loc) · 14.1 KB
/
webrender_helpers.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
/* 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 https://mozilla.org/MPL/2.0/. */
// TODO(gw): This contains helper traits and implementations for converting Servo display lists
// into WebRender display lists. In the future, this step should be completely removed.
// This might be achieved by sharing types between WR and Servo display lists, or
// completely converting layout to directly generate WebRender display lists, for example.
use crate::display_list::items::{BaseDisplayItem, ClipScrollNode, ClipScrollNodeType};
use crate::display_list::items::{DisplayItem, DisplayList, StackingContextType};
use msg::constellation_msg::PipelineId;
use webrender_api::units::LayoutPoint;
use webrender_api::{self, ClipId, CommonItemProperties, DisplayItem as WrDisplayItem};
use webrender_api::{DisplayListBuilder, PropertyBinding, PushStackingContextDisplayItem};
use webrender_api::{
RasterSpace, ReferenceFrameKind, SpaceAndClipInfo, SpatialId, StackingContext,
};
struct ClipScrollState {
clip_ids: Vec<Option<ClipId>>,
spatial_ids: Vec<Option<SpatialId>>,
active_clip_id: ClipId,
active_spatial_id: SpatialId,
}
impl DisplayList {
pub fn convert_to_webrender(&mut self, pipeline_id: PipelineId) -> DisplayListBuilder {
let mut clip_ids = vec![None; self.clip_scroll_nodes.len()];
let mut spatial_ids = vec![None; self.clip_scroll_nodes.len()];
// We need to add the WebRender root reference frame and root scroll node ids
// here manually, because WebRender creates these automatically.
// We also follow the "old" WebRender API for clip/scroll for now,
// hence both arrays are initialized based on FIRST_SPATIAL_NODE_INDEX,
// while FIRST_CLIP_NODE_INDEX is not taken into account.
let webrender_pipeline = pipeline_id.to_webrender();
clip_ids[0] = Some(ClipId::root(webrender_pipeline));
clip_ids[1] = Some(ClipId::root(webrender_pipeline));
spatial_ids[0] = Some(SpatialId::root_reference_frame(webrender_pipeline));
spatial_ids[1] = Some(SpatialId::root_scroll_node(webrender_pipeline));
let mut state = ClipScrollState {
clip_ids,
spatial_ids,
active_clip_id: ClipId::root(webrender_pipeline),
active_spatial_id: SpatialId::root_scroll_node(webrender_pipeline),
};
let mut builder = DisplayListBuilder::with_capacity(
webrender_pipeline,
self.bounds().size,
1024 * 1024, // 1 MB of space
);
for item in &mut self.list {
item.convert_to_webrender(&self.clip_scroll_nodes, &mut state, &mut builder);
}
builder
}
}
impl DisplayItem {
fn convert_to_webrender(
&mut self,
clip_scroll_nodes: &[ClipScrollNode],
state: &mut ClipScrollState,
builder: &mut DisplayListBuilder,
) {
// Note: for each time of a display item, if we register one of `clip_ids` or `spatial_ids`,
// we also register the other one as inherited from the current state or the stack.
// This is not an ideal behavior, but it is compatible with the old WebRender model
// of the clip-scroll tree.
let clip_and_scroll_indices = self.base().clipping_and_scrolling;
trace!("converting {:?}", clip_and_scroll_indices);
let cur_spatial_id = state.spatial_ids[clip_and_scroll_indices.scrolling.to_index()]
.expect("Tried to use WebRender SpatialId before it was defined.");
if cur_spatial_id != state.active_spatial_id {
state.active_spatial_id = cur_spatial_id;
}
let internal_clip_id = clip_and_scroll_indices
.clipping
.unwrap_or(clip_and_scroll_indices.scrolling);
let cur_clip_id = state.clip_ids[internal_clip_id.to_index()]
.expect("Tried to use WebRender ClipId before it was defined.");
if cur_clip_id != state.active_clip_id {
state.active_clip_id = cur_clip_id;
}
match *self {
DisplayItem::Rectangle(ref mut item) => {
item.item.common = build_common_item_properties(&item.base, state);
builder.push_item(&WrDisplayItem::Rectangle(item.item));
},
DisplayItem::Text(ref mut item) => {
item.item.common = build_common_item_properties(&item.base, state);
builder.push_item(&WrDisplayItem::Text(item.item));
builder.push_iter(item.data.iter());
},
DisplayItem::Image(ref mut item) => {
item.item.common = build_common_item_properties(&item.base, state);
builder.push_item(&WrDisplayItem::Image(item.item));
},
DisplayItem::Border(ref mut item) => {
item.item.common = build_common_item_properties(&item.base, state);
if !item.data.is_empty() {
builder.push_stops(item.data.as_ref());
}
builder.push_item(&WrDisplayItem::Border(item.item));
},
DisplayItem::Gradient(ref mut item) => {
item.item.common = build_common_item_properties(&item.base, state);
builder.push_stops(item.data.as_ref());
builder.push_item(&WrDisplayItem::Gradient(item.item));
},
DisplayItem::RadialGradient(ref mut item) => {
item.item.common = build_common_item_properties(&item.base, state);
builder.push_stops(item.data.as_ref());
builder.push_item(&WrDisplayItem::RadialGradient(item.item));
},
DisplayItem::Line(ref mut item) => {
item.item.common = build_common_item_properties(&item.base, state);
builder.push_item(&WrDisplayItem::Line(item.item));
},
DisplayItem::BoxShadow(ref mut item) => {
item.item.common = build_common_item_properties(&item.base, state);
builder.push_item(&WrDisplayItem::BoxShadow(item.item));
},
DisplayItem::PushTextShadow(ref mut item) => {
let common = build_common_item_properties(&item.base, state);
builder.push_shadow(
&SpaceAndClipInfo {
spatial_id: common.spatial_id,
clip_id: common.clip_id,
},
item.shadow,
true,
);
},
DisplayItem::PopAllTextShadows(_) => {
builder.push_item(&WrDisplayItem::PopAllShadows);
},
DisplayItem::Iframe(ref mut item) => {
let common = build_common_item_properties(&item.base, state);
builder.push_iframe(
item.bounds,
common.clip_rect,
&SpaceAndClipInfo {
spatial_id: common.spatial_id,
clip_id: common.clip_id,
},
item.iframe.to_webrender(),
true,
);
},
DisplayItem::PushStackingContext(ref mut item) => {
let stacking_context = &item.stacking_context;
debug_assert_eq!(stacking_context.context_type, StackingContextType::Real);
//let mut info = webrender_api::LayoutPrimitiveInfo::new(stacking_context.bounds);
let mut bounds = stacking_context.bounds;
let spatial_id =
if let Some(frame_index) = stacking_context.established_reference_frame {
let (transform, ref_frame) =
match (stacking_context.transform, stacking_context.perspective) {
(None, Some(p)) => (
p,
ReferenceFrameKind::Perspective {
scrolling_relative_to: None,
},
),
(Some(t), None) => (t, ReferenceFrameKind::Transform),
(Some(t), Some(p)) => (
t.pre_transform(&p),
ReferenceFrameKind::Perspective {
scrolling_relative_to: None,
},
),
(None, None) => unreachable!(),
};
let spatial_id = builder.push_reference_frame(
stacking_context.bounds.origin,
state.active_spatial_id,
stacking_context.transform_style,
PropertyBinding::Value(transform),
ref_frame,
);
state.spatial_ids[frame_index.to_index()] = Some(spatial_id);
state.clip_ids[frame_index.to_index()] = Some(cur_clip_id);
bounds.origin = LayoutPoint::zero();
spatial_id
} else {
state.active_spatial_id
};
if !stacking_context.filters.is_empty() {
builder.push_item(&WrDisplayItem::SetFilterOps);
builder.push_iter(&stacking_context.filters);
}
let wr_item = PushStackingContextDisplayItem {
origin: bounds.origin,
spatial_id,
is_backface_visible: true,
stacking_context: StackingContext {
transform_style: stacking_context.transform_style,
mix_blend_mode: stacking_context.mix_blend_mode,
clip_id: None,
raster_space: RasterSpace::Screen,
// TODO(pcwalton): Enable picture caching?
cache_tiles: false,
is_backdrop_root: false,
},
};
builder.push_item(&WrDisplayItem::PushStackingContext(wr_item));
},
DisplayItem::PopStackingContext(_) => builder.pop_stacking_context(),
DisplayItem::DefineClipScrollNode(ref mut item) => {
let node = &clip_scroll_nodes[item.node_index.to_index()];
let item_rect = node.clip.main;
let parent_spatial_id = state.spatial_ids[node.parent_index.to_index()]
.expect("Tried to use WebRender parent SpatialId before it was defined.");
let parent_clip_id = state.clip_ids[node.parent_index.to_index()]
.expect("Tried to use WebRender parent ClipId before it was defined.");
match node.node_type {
ClipScrollNodeType::Clip => {
let id = builder.define_clip(
&SpaceAndClipInfo {
clip_id: parent_clip_id,
spatial_id: parent_spatial_id,
},
item_rect,
node.clip.complex.clone(),
None,
);
state.spatial_ids[item.node_index.to_index()] = Some(parent_spatial_id);
state.clip_ids[item.node_index.to_index()] = Some(id);
},
ClipScrollNodeType::ScrollFrame(scroll_sensitivity, external_id) => {
let space_clip_info = builder.define_scroll_frame(
&SpaceAndClipInfo {
clip_id: parent_clip_id,
spatial_id: parent_spatial_id,
},
Some(external_id),
node.content_rect,
node.clip.main,
node.clip.complex.clone(),
None,
scroll_sensitivity,
webrender_api::units::LayoutVector2D::zero(),
);
state.clip_ids[item.node_index.to_index()] = Some(space_clip_info.clip_id);
state.spatial_ids[item.node_index.to_index()] =
Some(space_clip_info.spatial_id);
},
ClipScrollNodeType::StickyFrame(ref sticky_data) => {
// TODO: Add define_sticky_frame_with_parent to WebRender.
let id = builder.define_sticky_frame(
parent_spatial_id,
item_rect,
sticky_data.margins,
sticky_data.vertical_offset_bounds,
sticky_data.horizontal_offset_bounds,
webrender_api::units::LayoutVector2D::zero(),
);
state.spatial_ids[item.node_index.to_index()] = Some(id);
state.clip_ids[item.node_index.to_index()] = Some(parent_clip_id);
},
ClipScrollNodeType::Placeholder => {
unreachable!("Found DefineClipScrollNode for Placeholder type node.");
},
};
},
}
}
}
fn build_common_item_properties(
base: &BaseDisplayItem,
state: &ClipScrollState,
) -> CommonItemProperties {
let tag = match base.metadata.pointing {
Some(cursor) => Some((base.metadata.node.0 as u64, cursor)),
None => None,
};
CommonItemProperties {
clip_rect: base.clip_rect,
spatial_id: state.active_spatial_id,
clip_id: state.active_clip_id,
// TODO(gw): Make use of the WR backface visibility functionality.
is_backface_visible: true,
hit_info: tag,
}
}