forked from servo/webrender
-
Notifications
You must be signed in to change notification settings - Fork 3
/
frame_builder.rs
2030 lines (1777 loc) · 95.4 KB
/
frame_builder.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 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::{BorderDetails, BorderDisplayItem, BoxShadowClipMode, ClipAndScrollInfo, ClipId, ColorF};
use api::{DeviceIntPoint, DeviceIntRect, DeviceIntSize, DeviceUintRect, DeviceUintSize};
use api::{ExtendMode, FontKey, FontRenderMode, GlyphInstance, GlyphOptions, GradientStop};
use api::{ImageKey, ImageRendering, ItemRange, LayerPoint, LayerRect, LayerSize, SubpixelDirection};
use api::{LayerToScrollTransform, LayerVector2D, LayoutVector2D, LineOrientation, LineStyle};
use api::{LocalClip, PipelineId, RepeatMode, ScrollSensitivity, TextShadow, TileOffset};
use api::{TransformStyle, WebGLContextId, WorldPixel, YuvColorSpace, YuvData};
use app_units::Au;
use frame::FrameId;
use gpu_cache::GpuCache;
use internal_types::{FastHashMap, HardwareCompositeOp};
use mask_cache::{ClipMode, ClipRegion, ClipSource, MaskCacheInfo};
use plane_split::{BspSplitter, Polygon, Splitter};
use prim_store::{GradientPrimitiveCpu, ImagePrimitiveCpu, LinePrimitive, PrimitiveKind};
use prim_store::{ImagePrimitiveKind, PrimitiveContainer, PrimitiveIndex};
use prim_store::{PrimitiveStore, RadialGradientPrimitiveCpu, TextRunMode};
use prim_store::{RectanglePrimitive, TextRunPrimitiveCpu, TextShadowPrimitiveCpu};
use prim_store::{BoxShadowPrimitiveCpu, TexelRect, YuvImagePrimitiveCpu};
use profiler::{FrameProfileCounters, GpuCacheProfileCounters, TextureCacheProfileCounters};
use render_task::{AlphaRenderItem, ClipWorkItem, MaskCacheKey, RenderTask, RenderTaskIndex};
use render_task::{RenderTaskId, RenderTaskLocation};
use resource_cache::ResourceCache;
use clip_scroll_node::{ClipInfo, ClipScrollNode, NodeType};
use clip_scroll_tree::ClipScrollTree;
use std::{cmp, f32, i32, mem, usize};
use euclid::{SideOffsets2D, vec2, vec3};
use tiling::{ContextIsolation, StackingContextIndex};
use tiling::{ClipScrollGroup, ClipScrollGroupIndex, CompositeOps, DisplayListMap, Frame};
use tiling::{PackedLayer, PackedLayerIndex, PrimitiveFlags, PrimitiveRunCmd, RenderPass};
use tiling::{RenderTargetContext, RenderTaskCollection, ScrollbarPrimitive, StackingContext};
use util::{self, pack_as_float, subtract_rect, recycle_vec};
use util::{MatrixHelpers, RectHelpers};
#[derive(Debug, Clone)]
struct ImageBorderSegment {
geom_rect: LayerRect,
sub_rect: TexelRect,
stretch_size: LayerSize,
tile_spacing: LayerSize,
}
impl ImageBorderSegment {
fn new(rect: LayerRect,
sub_rect: TexelRect,
repeat_horizontal: RepeatMode,
repeat_vertical: RepeatMode) -> ImageBorderSegment {
let tile_spacing = LayerSize::zero();
debug_assert!(sub_rect.uv1.x >= sub_rect.uv0.x);
debug_assert!(sub_rect.uv1.y >= sub_rect.uv0.y);
let image_size = LayerSize::new(sub_rect.uv1.x - sub_rect.uv0.x,
sub_rect.uv1.y - sub_rect.uv0.y);
let stretch_size_x = match repeat_horizontal {
RepeatMode::Stretch => rect.size.width,
RepeatMode::Repeat => image_size.width,
RepeatMode::Round | RepeatMode::Space => {
error!("Round/Space not supported yet!");
rect.size.width
}
};
let stretch_size_y = match repeat_vertical {
RepeatMode::Stretch => rect.size.height,
RepeatMode::Repeat => image_size.height,
RepeatMode::Round | RepeatMode::Space => {
error!("Round/Space not supported yet!");
rect.size.height
}
};
ImageBorderSegment {
geom_rect: rect,
sub_rect,
stretch_size: LayerSize::new(stretch_size_x, stretch_size_y),
tile_spacing,
}
}
}
/// Construct a polygon from stacking context boundaries.
/// `anchor` here is an index that's going to be preserved in all the
/// splits of the polygon.
fn make_polygon(stacking_context: &StackingContext, node: &ClipScrollNode,
anchor: usize) -> Polygon<f32, WorldPixel> {
//TODO: only work with `isolated_items_bounds.size` worth of space
// This can be achieved by moving the `origin` shift
// from the primitive local coordinates into the layer transformation.
// Which in turn needs it to be a render task property obeyed by all primitives
// upon rendering, possibly not limited to `write_*_vertex` implementations.
let size = stacking_context.isolated_items_bounds.bottom_right();
let bounds = LayerRect::new(LayerPoint::zero(), LayerSize::new(size.x, size.y));
Polygon::from_transformed_rect(bounds, node.world_content_transform, anchor)
}
#[derive(Clone, Copy)]
pub struct FrameBuilderConfig {
pub enable_scrollbars: bool,
pub default_font_render_mode: FontRenderMode,
pub debug: bool,
pub cache_expiry_frames: u32,
}
pub struct FrameBuilder {
screen_size: DeviceUintSize,
background_color: Option<ColorF>,
prim_store: PrimitiveStore,
cmds: Vec<PrimitiveRunCmd>,
config: FrameBuilderConfig,
stacking_context_store: Vec<StackingContext>,
clip_scroll_group_store: Vec<ClipScrollGroup>,
clip_scroll_group_indices: FastHashMap<ClipAndScrollInfo,
ClipScrollGroupIndex>,
packed_layers: Vec<PackedLayer>,
// A stack of the current text-shadow primitives.
shadow_prim_stack: Vec<PrimitiveIndex>,
scrollbar_prims: Vec<ScrollbarPrimitive>,
/// A stack of scroll nodes used during display list processing to properly
/// parent new scroll nodes.
reference_frame_stack: Vec<ClipId>,
/// A stack of stacking contexts used for creating ClipScrollGroups as
/// primitives are added to the frame.
stacking_context_stack: Vec<StackingContextIndex>,
/// Whether or not we've pushed a root stacking context for the current pipeline.
has_root_stacking_context: bool,
}
impl FrameBuilder {
pub fn new(previous: Option<FrameBuilder>,
screen_size: DeviceUintSize,
background_color: Option<ColorF>,
config: FrameBuilderConfig) -> FrameBuilder {
match previous {
Some(prev) => {
FrameBuilder {
stacking_context_store: recycle_vec(prev.stacking_context_store),
clip_scroll_group_store: recycle_vec(prev.clip_scroll_group_store),
clip_scroll_group_indices: FastHashMap::default(),
cmds: recycle_vec(prev.cmds),
packed_layers: recycle_vec(prev.packed_layers),
shadow_prim_stack: recycle_vec(prev.shadow_prim_stack),
scrollbar_prims: recycle_vec(prev.scrollbar_prims),
reference_frame_stack: recycle_vec(prev.reference_frame_stack),
stacking_context_stack: recycle_vec(prev.stacking_context_stack),
prim_store: prev.prim_store.recycle(),
screen_size,
background_color,
config,
has_root_stacking_context: false,
}
}
None => {
FrameBuilder {
stacking_context_store: Vec::new(),
clip_scroll_group_store: Vec::new(),
clip_scroll_group_indices: FastHashMap::default(),
cmds: Vec::new(),
packed_layers: Vec::new(),
shadow_prim_stack: Vec::new(),
scrollbar_prims: Vec::new(),
reference_frame_stack: Vec::new(),
stacking_context_stack: Vec::new(),
prim_store: PrimitiveStore::new(),
screen_size,
background_color,
config,
has_root_stacking_context: false,
}
}
}
}
pub fn create_clip_scroll_group_if_necessary(&mut self, info: ClipAndScrollInfo) {
if self.clip_scroll_group_indices.contains_key(&info) {
return;
}
let group_index = self.create_clip_scroll_group(info);
self.clip_scroll_group_indices.insert(info, group_index);
}
/// Create a primitive and add it to the prim store. This method doesn't
/// add the primitive to the draw list, so can be used for creating
/// sub-primitives.
fn create_primitive(&mut self,
clip_and_scroll: ClipAndScrollInfo,
rect: &LayerRect,
local_clip: &LocalClip,
extra_clips: &[ClipSource],
container: PrimitiveContainer) -> PrimitiveIndex {
self.create_clip_scroll_group_if_necessary(clip_and_scroll);
let mut clip_sources = extra_clips.to_vec();
if let &LocalClip::RoundedRect(_, _) = local_clip {
clip_sources.push(ClipSource::Region(ClipRegion::create_for_local_clip(local_clip)))
}
let clip_info = if !clip_sources.is_empty() {
Some(MaskCacheInfo::new(&clip_sources))
} else {
None
};
let prim_index = self.prim_store.add_primitive(rect,
&local_clip.clip_rect(),
clip_sources,
clip_info,
container);
prim_index
}
/// Add an already created primitive to the draw lists.
pub fn add_primitive_to_draw_list(&mut self,
prim_index: PrimitiveIndex,
clip_and_scroll: ClipAndScrollInfo) {
match self.cmds.last_mut().unwrap() {
&mut PrimitiveRunCmd::PrimitiveRun(run_prim_index, ref mut count, run_clip_and_scroll) => {
if run_clip_and_scroll == clip_and_scroll &&
run_prim_index.0 + *count == prim_index.0 {
*count += 1;
return;
}
}
&mut PrimitiveRunCmd::PushStackingContext(..) |
&mut PrimitiveRunCmd::PopStackingContext => {}
}
self.cmds.push(PrimitiveRunCmd::PrimitiveRun(prim_index, 1, clip_and_scroll));
}
/// Convenience interface that creates a primitive entry and adds it
/// to the draw list.
pub fn add_primitive(&mut self,
clip_and_scroll: ClipAndScrollInfo,
rect: &LayerRect,
local_clip: &LocalClip,
extra_clips: &[ClipSource],
container: PrimitiveContainer) -> PrimitiveIndex {
let prim_index = self.create_primitive(clip_and_scroll,
rect,
local_clip,
extra_clips,
container);
self.add_primitive_to_draw_list(prim_index, clip_and_scroll);
prim_index
}
pub fn create_clip_scroll_group(&mut self, info: ClipAndScrollInfo) -> ClipScrollGroupIndex {
let packed_layer_index = PackedLayerIndex(self.packed_layers.len());
self.packed_layers.push(PackedLayer::empty());
self.clip_scroll_group_store.push(ClipScrollGroup {
scroll_node_id: info.scroll_node_id,
clip_node_id: info.clip_node_id(),
packed_layer_index,
screen_bounding_rect: None,
});
ClipScrollGroupIndex(self.clip_scroll_group_store.len() - 1, info)
}
pub fn notify_waiting_for_root_stacking_context(&mut self) {
self.has_root_stacking_context = false;
}
pub fn push_stacking_context(&mut self,
reference_frame_offset: &LayerVector2D,
pipeline_id: PipelineId,
composite_ops: CompositeOps,
transform_style: TransformStyle) {
if let Some(parent_index) = self.stacking_context_stack.last() {
let parent_is_root = self.stacking_context_store[parent_index.0].is_page_root;
if composite_ops.mix_blend_mode.is_some() && !parent_is_root {
// the parent stacking context of a stacking context with mix-blend-mode
// must be drawn with a transparent background, unless the parent stacking context
// is the root of the page
let isolation = &mut self.stacking_context_store[parent_index.0].isolation;
if *isolation != ContextIsolation::None {
error!("Isolation conflict detected on {:?}: {:?}", parent_index, *isolation);
}
*isolation = ContextIsolation::Full;
}
}
let stacking_context_index = StackingContextIndex(self.stacking_context_store.len());
let reference_frame_id = self.current_reference_frame_id();
self.stacking_context_store.push(StackingContext::new(pipeline_id,
*reference_frame_offset,
!self.has_root_stacking_context,
reference_frame_id,
transform_style,
composite_ops));
self.has_root_stacking_context = true;
self.cmds.push(PrimitiveRunCmd::PushStackingContext(stacking_context_index));
self.stacking_context_stack.push(stacking_context_index);
}
pub fn pop_stacking_context(&mut self) {
self.cmds.push(PrimitiveRunCmd::PopStackingContext);
self.stacking_context_stack.pop();
assert!(self.shadow_prim_stack.is_empty(),
"Found unpopped text shadows when popping stacking context!");
}
pub fn push_reference_frame(&mut self,
parent_id: Option<ClipId>,
pipeline_id: PipelineId,
rect: &LayerRect,
transform: &LayerToScrollTransform,
clip_scroll_tree: &mut ClipScrollTree)
-> ClipId {
let new_id = clip_scroll_tree.add_reference_frame(rect, transform, pipeline_id, parent_id);
self.reference_frame_stack.push(new_id);
new_id
}
pub fn current_reference_frame_id(&self) -> ClipId {
*self.reference_frame_stack.last().unwrap()
}
pub fn setup_viewport_offset(&mut self,
window_size: DeviceUintSize,
inner_rect: DeviceUintRect,
device_pixel_ratio: f32,
clip_scroll_tree: &mut ClipScrollTree) {
let inner_origin = inner_rect.origin.to_f32();
let viewport_offset = LayerPoint::new((inner_origin.x / device_pixel_ratio).round(),
(inner_origin.y / device_pixel_ratio).round());
let outer_size = window_size.to_f32();
let outer_size = LayerSize::new((outer_size.width / device_pixel_ratio).round(),
(outer_size.height / device_pixel_ratio).round());
let clip_size = LayerSize::new(outer_size.width + 2.0 * viewport_offset.x,
outer_size.height + 2.0 * viewport_offset.y);
let viewport_clip = LayerRect::new(LayerPoint::new(-viewport_offset.x, -viewport_offset.y),
LayerSize::new(clip_size.width, clip_size.height));
let root_id = clip_scroll_tree.root_reference_frame_id();
if let Some(root_node) = clip_scroll_tree.nodes.get_mut(&root_id) {
if let NodeType::ReferenceFrame(ref mut transform) = root_node.node_type {
*transform = LayerToScrollTransform::create_translation(viewport_offset.x,
viewport_offset.y,
0.0);
}
root_node.local_clip_rect = viewport_clip;
}
let clip_id = clip_scroll_tree.topmost_scrolling_node_id();
if let Some(root_node) = clip_scroll_tree.nodes.get_mut(&clip_id) {
root_node.local_clip_rect = viewport_clip;
}
}
pub fn push_root(&mut self,
pipeline_id: PipelineId,
viewport_size: &LayerSize,
content_size: &LayerSize,
clip_scroll_tree: &mut ClipScrollTree)
-> ClipId {
let viewport_rect = LayerRect::new(LayerPoint::zero(), *viewport_size);
let identity = &LayerToScrollTransform::identity();
self.push_reference_frame(None, pipeline_id, &viewport_rect, identity, clip_scroll_tree);
let topmost_scrolling_node_id = ClipId::root_scroll_node(pipeline_id);
clip_scroll_tree.topmost_scrolling_node_id = topmost_scrolling_node_id;
self.add_scroll_frame(topmost_scrolling_node_id,
clip_scroll_tree.root_reference_frame_id,
pipeline_id,
&viewport_rect,
content_size,
ScrollSensitivity::ScriptAndInputEvents,
clip_scroll_tree);
topmost_scrolling_node_id
}
pub fn add_clip_node(&mut self,
new_node_id: ClipId,
parent_id: ClipId,
pipeline_id: PipelineId,
clip_region: ClipRegion,
clip_scroll_tree: &mut ClipScrollTree) {
let clip_info = ClipInfo::new(clip_region, PackedLayerIndex(self.packed_layers.len()));
let node = ClipScrollNode::new(pipeline_id, parent_id, clip_info);
clip_scroll_tree.add_node(node, new_node_id);
self.packed_layers.push(PackedLayer::empty());
}
pub fn add_scroll_frame(&mut self,
new_node_id: ClipId,
parent_id: ClipId,
pipeline_id: PipelineId,
frame_rect: &LayerRect,
content_size: &LayerSize,
scroll_sensitivity: ScrollSensitivity,
clip_scroll_tree: &mut ClipScrollTree) {
let node = ClipScrollNode::new_scroll_frame(pipeline_id,
parent_id,
frame_rect,
content_size,
scroll_sensitivity);
clip_scroll_tree.add_node(node, new_node_id);
}
pub fn pop_reference_frame(&mut self) {
self.reference_frame_stack.pop();
}
pub fn push_text_shadow(&mut self,
shadow: TextShadow,
clip_and_scroll: ClipAndScrollInfo,
local_clip: &LocalClip) {
let prim = TextShadowPrimitiveCpu {
shadow,
primitives: Vec::new(),
};
// Create an empty text-shadow primitive. Insert it into
// the draw lists immediately so that it will be drawn
// before any visual text elements that are added as
// part of this text-shadow context.
let prim_index = self.add_primitive(clip_and_scroll,
&LayerRect::zero(),
local_clip,
&[],
PrimitiveContainer::TextShadow(prim));
self.shadow_prim_stack.push(prim_index);
}
pub fn pop_text_shadow(&mut self) {
let prim_index = self.shadow_prim_stack
.pop()
.expect("invalid shadow push/pop count");
// By now, the local rect of the text shadow has been calculated. It
// is calculated as the items in the shadow are added. It's now
// safe to offset the local rect by the offset of the shadow, which
// is then used when blitting the shadow to the final location.
let metadata = &mut self.prim_store.cpu_metadata[prim_index.0];
let prim = &self.prim_store.cpu_text_shadows[metadata.cpu_prim_index.0];
metadata.local_rect = metadata.local_rect.translate(&prim.shadow.offset);
}
pub fn add_solid_rectangle(&mut self,
clip_and_scroll: ClipAndScrollInfo,
rect: &LayerRect,
local_clip: &LocalClip,
color: &ColorF,
flags: PrimitiveFlags) {
let prim = RectanglePrimitive {
color: *color,
};
let prim_index = self.add_primitive(clip_and_scroll,
rect,
local_clip,
&[],
PrimitiveContainer::Rectangle(prim));
match flags {
PrimitiveFlags::None => {}
PrimitiveFlags::Scrollbar(clip_id, border_radius) => {
self.scrollbar_prims.push(ScrollbarPrimitive {
prim_index,
clip_id,
border_radius,
});
}
}
}
pub fn add_line(&mut self,
clip_and_scroll: ClipAndScrollInfo,
local_clip: &LocalClip,
baseline: f32,
start: f32,
end: f32,
orientation: LineOrientation,
width: f32,
color: &ColorF,
style: LineStyle) {
let new_rect = match orientation {
LineOrientation::Horizontal => {
LayerRect::new(LayerPoint::new(start, baseline),
LayerSize::new(end - start, width))
}
LineOrientation::Vertical => {
LayerRect::new(LayerPoint::new(baseline, start),
LayerSize::new(width, end - start))
}
};
let line = LinePrimitive {
color: *color,
style: style,
orientation: orientation,
};
let mut fast_text_shadow_prims = Vec::new();
for shadow_prim_index in &self.shadow_prim_stack {
let shadow_metadata = &self.prim_store.cpu_metadata[shadow_prim_index.0];
let shadow_prim = &self.prim_store.cpu_text_shadows[shadow_metadata.cpu_prim_index.0];
if shadow_prim.shadow.blur_radius == 0.0 {
fast_text_shadow_prims.push(shadow_prim.shadow);
}
}
for shadow in fast_text_shadow_prims {
let mut line = line.clone();
line.color = shadow.color;
self.add_primitive(clip_and_scroll,
&new_rect.translate(&shadow.offset),
local_clip,
&[],
PrimitiveContainer::Line(line));
}
let prim_index = self.create_primitive(clip_and_scroll,
&new_rect,
local_clip,
&[],
PrimitiveContainer::Line(line));
if color.a > 0.0 {
self.add_primitive_to_draw_list(prim_index, clip_and_scroll);
}
for shadow_prim_index in &self.shadow_prim_stack {
let shadow_metadata = &mut self.prim_store.cpu_metadata[shadow_prim_index.0];
debug_assert_eq!(shadow_metadata.prim_kind, PrimitiveKind::TextShadow);
let shadow_prim = &mut self.prim_store.cpu_text_shadows[shadow_metadata.cpu_prim_index.0];
// Only run real blurs here (fast path zero blurs are handled above).
if shadow_prim.shadow.blur_radius > 0.0 {
let shadow_rect = new_rect.inflate(shadow_prim.shadow.blur_radius,
shadow_prim.shadow.blur_radius);
shadow_metadata.local_rect = shadow_metadata.local_rect.union(&shadow_rect);
shadow_prim.primitives.push(prim_index);
}
}
}
pub fn add_border(&mut self,
clip_and_scroll: ClipAndScrollInfo,
rect: LayerRect,
local_clip: &LocalClip,
border_item: &BorderDisplayItem,
gradient_stops: ItemRange<GradientStop>,
gradient_stops_count: usize) {
let create_segments = |outset: SideOffsets2D<f32>| {
// Calculate the modified rect as specific by border-image-outset
let origin = LayerPoint::new(rect.origin.x - outset.left,
rect.origin.y - outset.top);
let size = LayerSize::new(rect.size.width + outset.left + outset.right,
rect.size.height + outset.top + outset.bottom);
let rect = LayerRect::new(origin, size);
let tl_outer = LayerPoint::new(rect.origin.x, rect.origin.y);
let tl_inner = tl_outer + vec2(border_item.widths.left, border_item.widths.top);
let tr_outer = LayerPoint::new(rect.origin.x + rect.size.width, rect.origin.y);
let tr_inner = tr_outer + vec2(-border_item.widths.right, border_item.widths.top);
let bl_outer = LayerPoint::new(rect.origin.x, rect.origin.y + rect.size.height);
let bl_inner = bl_outer + vec2(border_item.widths.left, -border_item.widths.bottom);
let br_outer = LayerPoint::new(rect.origin.x + rect.size.width,
rect.origin.y + rect.size.height);
let br_inner = br_outer - vec2(border_item.widths.right, border_item.widths.bottom);
// Build the list of gradient segments
vec![
// Top left
LayerRect::from_floats(tl_outer.x, tl_outer.y, tl_inner.x, tl_inner.y),
// Top right
LayerRect::from_floats(tr_inner.x, tr_outer.y, tr_outer.x, tr_inner.y),
// Bottom right
LayerRect::from_floats(br_inner.x, br_inner.y, br_outer.x, br_outer.y),
// Bottom left
LayerRect::from_floats(bl_outer.x, bl_inner.y, bl_inner.x, bl_outer.y),
// Top
LayerRect::from_floats(tl_inner.x, tl_outer.y, tr_inner.x, tl_inner.y),
// Bottom
LayerRect::from_floats(bl_inner.x, bl_inner.y, br_inner.x, bl_outer.y),
// Left
LayerRect::from_floats(tl_outer.x, tl_inner.y, tl_inner.x, bl_inner.y),
// Right
LayerRect::from_floats(tr_inner.x, tr_inner.y, br_outer.x, br_inner.y),
]
};
match border_item.details {
BorderDetails::Image(ref border) => {
// Calculate the modified rect as specific by border-image-outset
let origin = LayerPoint::new(rect.origin.x - border.outset.left,
rect.origin.y - border.outset.top);
let size = LayerSize::new(rect.size.width + border.outset.left + border.outset.right,
rect.size.height + border.outset.top + border.outset.bottom);
let rect = LayerRect::new(origin, size);
// Calculate the local texel coords of the slices.
let px0 = 0;
let px1 = border.patch.slice.left;
let px2 = border.patch.width - border.patch.slice.right;
let px3 = border.patch.width;
let py0 = 0;
let py1 = border.patch.slice.top;
let py2 = border.patch.height - border.patch.slice.bottom;
let py3 = border.patch.height;
let tl_outer = LayerPoint::new(rect.origin.x, rect.origin.y);
let tl_inner = tl_outer + vec2(border_item.widths.left, border_item.widths.top);
let tr_outer = LayerPoint::new(rect.origin.x + rect.size.width, rect.origin.y);
let tr_inner = tr_outer + vec2(-border_item.widths.right, border_item.widths.top);
let bl_outer = LayerPoint::new(rect.origin.x, rect.origin.y + rect.size.height);
let bl_inner = bl_outer + vec2(border_item.widths.left, -border_item.widths.bottom);
let br_outer = LayerPoint::new(rect.origin.x + rect.size.width,
rect.origin.y + rect.size.height);
let br_inner = br_outer - vec2(border_item.widths.right, border_item.widths.bottom);
// Build the list of image segments
let mut segments = vec![
// Top left
ImageBorderSegment::new(LayerRect::from_floats(tl_outer.x, tl_outer.y, tl_inner.x, tl_inner.y),
TexelRect::new(px0, py0, px1, py1),
RepeatMode::Stretch,
RepeatMode::Stretch),
// Top right
ImageBorderSegment::new(LayerRect::from_floats(tr_inner.x, tr_outer.y, tr_outer.x, tr_inner.y),
TexelRect::new(px2, py0, px3, py1),
RepeatMode::Stretch,
RepeatMode::Stretch),
// Bottom right
ImageBorderSegment::new(LayerRect::from_floats(br_inner.x, br_inner.y, br_outer.x, br_outer.y),
TexelRect::new(px2, py2, px3, py3),
RepeatMode::Stretch,
RepeatMode::Stretch),
// Bottom left
ImageBorderSegment::new(LayerRect::from_floats(bl_outer.x, bl_inner.y, bl_inner.x, bl_outer.y),
TexelRect::new(px0, py2, px1, py3),
RepeatMode::Stretch,
RepeatMode::Stretch),
];
// Center
if border.fill {
segments.push(ImageBorderSegment::new(
LayerRect::from_floats(tl_inner.x, tl_inner.y, tr_inner.x, bl_inner.y),
TexelRect::new(px1, py1, px2, py2),
border.repeat_horizontal,
border.repeat_vertical))
}
// Add edge segments if valid size.
if px1 < px2 && py1 < py2 {
segments.extend_from_slice(&[
// Top
ImageBorderSegment::new(LayerRect::from_floats(tl_inner.x, tl_outer.y, tr_inner.x, tl_inner.y),
TexelRect::new(px1, py0, px2, py1),
border.repeat_horizontal,
RepeatMode::Stretch),
// Bottom
ImageBorderSegment::new(LayerRect::from_floats(bl_inner.x, bl_inner.y, br_inner.x, bl_outer.y),
TexelRect::new(px1, py2, px2, py3),
border.repeat_horizontal,
RepeatMode::Stretch),
// Left
ImageBorderSegment::new(LayerRect::from_floats(tl_outer.x, tl_inner.y, tl_inner.x, bl_inner.y),
TexelRect::new(px0, py1, px1, py2),
RepeatMode::Stretch,
border.repeat_vertical),
// Right
ImageBorderSegment::new(LayerRect::from_floats(tr_inner.x, tr_inner.y, br_outer.x, br_inner.y),
TexelRect::new(px2, py1, px3, py2),
RepeatMode::Stretch,
border.repeat_vertical),
]);
}
for segment in segments {
self.add_image(clip_and_scroll,
segment.geom_rect,
local_clip,
&segment.stretch_size,
&segment.tile_spacing,
Some(segment.sub_rect),
border.image_key,
ImageRendering::Auto,
None);
}
}
BorderDetails::Normal(ref border) => {
self.add_normal_border(&rect,
border,
&border_item.widths,
clip_and_scroll,
local_clip);
}
BorderDetails::Gradient(ref border) => {
for segment in create_segments(border.outset) {
let segment_rel = segment.origin - rect.origin;
self.add_gradient(clip_and_scroll,
segment,
local_clip,
border.gradient.start_point - segment_rel,
border.gradient.end_point - segment_rel,
gradient_stops,
gradient_stops_count,
border.gradient.extend_mode,
segment.size,
LayerSize::zero());
}
}
BorderDetails::RadialGradient(ref border) => {
for segment in create_segments(border.outset) {
let segment_rel = segment.origin - rect.origin;
self.add_radial_gradient(clip_and_scroll,
segment,
local_clip,
border.gradient.start_center - segment_rel,
border.gradient.start_radius,
border.gradient.end_center - segment_rel,
border.gradient.end_radius,
border.gradient.ratio_xy,
gradient_stops,
border.gradient.extend_mode,
segment.size,
LayerSize::zero());
}
}
}
}
pub fn add_gradient(&mut self,
clip_and_scroll: ClipAndScrollInfo,
rect: LayerRect,
local_clip: &LocalClip,
start_point: LayerPoint,
end_point: LayerPoint,
stops: ItemRange<GradientStop>,
stops_count: usize,
extend_mode: ExtendMode,
tile_size: LayerSize,
tile_spacing: LayerSize) {
let tile_repeat = tile_size + tile_spacing;
let is_not_tiled = tile_repeat.width >= rect.size.width &&
tile_repeat.height >= rect.size.height;
let aligned_and_fills_rect = (start_point.x == end_point.x &&
start_point.y.min(end_point.y) <= 0.0 &&
start_point.y.max(end_point.y) >= rect.size.height) ||
(start_point.y == end_point.y &&
start_point.x.min(end_point.x) <= 0.0 &&
start_point.x.max(end_point.x) >= rect.size.width);
// Fast path for clamped, axis-aligned gradients, with gradient lines intersecting all of rect:
let aligned = extend_mode == ExtendMode::Clamp && is_not_tiled && aligned_and_fills_rect;
// Try to ensure that if the gradient is specified in reverse, then so long as the stops
// are also supplied in reverse that the rendered result will be equivalent. To do this,
// a reference orientation for the gradient line must be chosen, somewhat arbitrarily, so
// just designate the reference orientation as start < end. Aligned gradient rendering
// manages to produce the same result regardless of orientation, so don't worry about
// reversing in that case.
let reverse_stops = !aligned &&
(start_point.x > end_point.x ||
(start_point.x == end_point.x &&
start_point.y > end_point.y));
// To get reftests exactly matching with reverse start/end
// points, it's necessary to reverse the gradient
// line in some cases.
let (sp, ep) = if reverse_stops {
(end_point, start_point)
} else {
(start_point, end_point)
};
let gradient_cpu = GradientPrimitiveCpu {
stops_range: stops,
stops_count,
extend_mode,
reverse_stops,
gpu_blocks: [
[sp.x, sp.y, ep.x, ep.y].into(),
[tile_size.width, tile_size.height, tile_repeat.width, tile_repeat.height].into(),
[pack_as_float(extend_mode as u32), 0.0, 0.0, 0.0].into(),
],
};
let prim = if aligned {
PrimitiveContainer::AlignedGradient(gradient_cpu)
} else {
PrimitiveContainer::AngleGradient(gradient_cpu)
};
self.add_primitive(clip_and_scroll, &rect, local_clip, &[], prim);
}
pub fn add_radial_gradient(&mut self,
clip_and_scroll: ClipAndScrollInfo,
rect: LayerRect,
local_clip: &LocalClip,
start_center: LayerPoint,
start_radius: f32,
end_center: LayerPoint,
end_radius: f32,
ratio_xy: f32,
stops: ItemRange<GradientStop>,
extend_mode: ExtendMode,
tile_size: LayerSize,
tile_spacing: LayerSize) {
let tile_repeat = tile_size + tile_spacing;
let radial_gradient_cpu = RadialGradientPrimitiveCpu {
stops_range: stops,
extend_mode,
gpu_data_count: 0,
gpu_blocks: [
[start_center.x, start_center.y, end_center.x, end_center.y].into(),
[start_radius, end_radius, ratio_xy, pack_as_float(extend_mode as u32)].into(),
[tile_size.width, tile_size.height, tile_repeat.width, tile_repeat.height].into(),
],
};
self.add_primitive(clip_and_scroll,
&rect,
local_clip,
&[],
PrimitiveContainer::RadialGradient(radial_gradient_cpu));
}
pub fn add_text(&mut self,
clip_and_scroll: ClipAndScrollInfo,
run_offset: LayoutVector2D,
rect: LayerRect,
local_clip: &LocalClip,
font_key: FontKey,
size: Au,
color: &ColorF,
glyph_range: ItemRange<GlyphInstance>,
glyph_count: usize,
glyph_options: Option<GlyphOptions>) {
// Trivial early out checks
if size.0 <= 0 {
return
}
// TODO(gw): Use a proper algorithm to select
// whether this item should be rendered with
// subpixel AA!
let mut normal_render_mode = self.config.default_font_render_mode;
// There are some conditions under which we can't use
// subpixel text rendering, even if enabled.
if normal_render_mode == FontRenderMode::Subpixel {
if color.a != 1.0 {
normal_render_mode = FontRenderMode::Alpha;
}
// text on a stacking context that has filters
// (e.g. opacity) can't use sub-pixel.
// TODO(gw): It's possible we can relax this in
// the future, if we modify the way
// we handle subpixel blending.
if let Some(sc_index) = self.stacking_context_stack.last() {
let stacking_context = &self.stacking_context_store[sc_index.0];
if stacking_context.composite_ops.count() > 0 {
normal_render_mode = FontRenderMode::Alpha;
}
}
}
// Shadows never use subpixel AA, but need to respect the alpha/mono flag
// for reftests.
let (shadow_render_mode, subpx_dir) = match self.config.default_font_render_mode {
FontRenderMode::Subpixel | FontRenderMode::Alpha => {
// TODO(gw): Expose subpixel direction in API once WR supports
// vertical text runs.
(FontRenderMode::Alpha, SubpixelDirection::Horizontal)
}
FontRenderMode::Mono => {
(FontRenderMode::Mono, SubpixelDirection::None)
}
};
let prim = TextRunPrimitiveCpu {
font_key,
logical_font_size: size,
glyph_range,
glyph_count,
glyph_instances: Vec::new(),
glyph_options,
normal_render_mode,
shadow_render_mode,
offset: run_offset,
color: *color,
subpx_dir,
};
// Text shadows that have a blur radius of 0 need to be rendered as normal
// text elements to get pixel perfect results for reftests. It's also a big
// performance win to avoid blurs and render target allocations where
// possible. For any text shadows that have zero blur, create a normal text
// primitive with the shadow's color and offset. These need to be added
// *before* the visual text primitive in order to get the correct paint
// order. Store them in a Vec first to work around borrowck issues.
// TODO(gw): Refactor to avoid having to store them in a Vec first.
let mut fast_text_shadow_prims = Vec::new();
for shadow_prim_index in &self.shadow_prim_stack {
let shadow_metadata = &self.prim_store.cpu_metadata[shadow_prim_index.0];
let shadow_prim = &self.prim_store.cpu_text_shadows[shadow_metadata.cpu_prim_index.0];
if shadow_prim.shadow.blur_radius == 0.0 {
let mut text_prim = prim.clone();
text_prim.color = shadow_prim.shadow.color;
text_prim.offset += shadow_prim.shadow.offset;
fast_text_shadow_prims.push(text_prim);
}
}
for text_prim in fast_text_shadow_prims {
self.add_primitive(clip_and_scroll,
&rect.translate(&text_prim.offset),
local_clip,
&[],
PrimitiveContainer::TextRun(text_prim));
}
// Create (and add to primitive store) the primitive that will be
// used for both the visual element and also the shadow(s).
let prim_index = self.create_primitive(clip_and_scroll,
&rect,
local_clip,
&[],
PrimitiveContainer::TextRun(prim));
// Only add a visual element if it can contribute to the scene.
if color.a > 0.0 {
self.add_primitive_to_draw_list(prim_index, clip_and_scroll);
}
// Now add this primitive index to all the currently active text shadow
// primitives. Although we're adding the indices *after* the visual
// primitive here, they will still draw before the visual text, since
// the text-shadow primitive itself has been added to the draw cmd
// list *before* the visual element, during push_text_shadow. We need
// the primitive index of the visual element here before we can add
// the indices as sub-primitives to the shadow primitives.
for shadow_prim_index in &self.shadow_prim_stack {
let shadow_metadata = &mut self.prim_store.cpu_metadata[shadow_prim_index.0];
debug_assert_eq!(shadow_metadata.prim_kind, PrimitiveKind::TextShadow);
let shadow_prim = &mut self.prim_store.cpu_text_shadows[shadow_metadata.cpu_prim_index.0];
// Only run real blurs here (fast path zero blurs are handled above).
if shadow_prim.shadow.blur_radius > 0.0 {
let shadow_rect = rect.inflate(shadow_prim.shadow.blur_radius,
shadow_prim.shadow.blur_radius);
shadow_metadata.local_rect = shadow_metadata.local_rect.union(&shadow_rect);
shadow_prim.primitives.push(prim_index);
}
}
}
pub fn fill_box_shadow_rect(&mut self,
clip_and_scroll: ClipAndScrollInfo,
box_bounds: &LayerRect,
bs_rect: LayerRect,
local_clip: &LocalClip,
color: &ColorF,
border_radius: f32,