99
1010namespace flutter {
1111
12- OpacityLayer::OpacityLayer (int alpha, const SkPoint& offset)
13- : alpha_(alpha), offset_(offset) {
12+ // The OpacityLayer has no real "elevation", but we want to avoid Z-fighting
13+ // when using the system compositor. Choose a small but non-zero value for
14+ // this.
15+ constexpr float kOpacityElevationWhenUsingSystemCompositor = 0 .01f ;
16+
17+ #if !defined(OS_FUCHSIA)
18+ void OpacityLayerBase::Preroll (PrerollContext* context,
19+ const SkMatrix& matrix) {
20+ const float parent_is_opaque = context->is_opaque ;
21+
22+ context->mutators_stack .PushOpacity (opacity_);
23+ context->is_opaque = parent_is_opaque && (opacity_ == SK_AlphaOPAQUE);
24+ ContainerLayer::Preroll (context, matrix);
25+ context->is_opaque = parent_is_opaque;
26+ context->mutators_stack .Pop ();
27+ }
28+ #endif
29+
30+ OpacityLayer::OpacityLayer (SkAlpha opacity, const SkPoint& offset)
31+ : OpacityLayerBase(SK_ColorTRANSPARENT,
32+ opacity,
33+ kOpacityElevationWhenUsingSystemCompositor ),
34+ offset_ (offset) {
1435 // Ensure OpacityLayer has only one direct child.
1536 //
1637 // This is needed to ensure that retained rendering can always be applied to
@@ -31,32 +52,53 @@ void OpacityLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) {
3152 ContainerLayer* container = GetChildContainer ();
3253 FML_DCHECK (!container->layers ().empty ()); // OpacityLayer can't be a leaf.
3354
55+ // Factor in the offset during Preroll. |OpacityLayerBase| will handle the
56+ // opacity.
3457 SkMatrix child_matrix = matrix;
3558 child_matrix.postTranslate (offset_.fX , offset_.fY );
3659 context->mutators_stack .PushTransform (
3760 SkMatrix::MakeTrans (offset_.fX , offset_.fY ));
38- context->mutators_stack .PushOpacity (alpha_);
39- ContainerLayer::Preroll (context, child_matrix);
40- context->mutators_stack .Pop ();
61+ OpacityLayerBase::Preroll (context, child_matrix);
4162 context->mutators_stack .Pop ();
42- set_paint_bounds (paint_bounds ().makeOffset (offset_.fX , offset_.fY ));
4363
44- if (!context->has_platform_view && context->raster_cache &&
45- SkRect::Intersects (context->cull_rect , paint_bounds ())) {
46- SkMatrix ctm = child_matrix;
64+ // When using the system compositor, do not include the offset since we are
65+ // rendering as a separate piece of geometry and the offset will be baked into
66+ // that geometry's transform.
67+ if (OpacityLayerBase::can_system_composite ()) {
68+ set_dimensions (SkRRect::MakeRect (paint_bounds ()));
69+ set_needs_system_composite (true );
70+ } else {
71+ set_paint_bounds (paint_bounds ().makeOffset (offset_.fX , offset_.fY ));
72+
73+ if (!context->has_platform_view && context->raster_cache &&
74+ SkRect::Intersects (context->cull_rect , paint_bounds ())) {
75+ SkMatrix ctm = child_matrix;
4776#ifndef SUPPORT_FRACTIONAL_TRANSLATION
48- ctm = RasterCache::GetIntegralTransCTM (ctm);
77+ ctm = RasterCache::GetIntegralTransCTM (ctm);
4978#endif
50- context->raster_cache ->Prepare (context, container, ctm);
79+ context->raster_cache ->Prepare (context, container, ctm);
80+ }
5181 }
5282}
5383
84+ #if defined(OS_FUCHSIA)
85+
86+ void OpacityLayer::UpdateScene (SceneUpdateContext& context) {
87+ SceneUpdateContext::Transform transform (
88+ context, SkMatrix::MakeTrans (offset_.fX , offset_.fY ));
89+
90+ // OpacityLayerBase will handle applying the opacity itself.
91+ OpacityLayerBase::UpdateScene (context);
92+ }
93+
94+ #endif
95+
5496void OpacityLayer::Paint (PaintContext& context) const {
5597 TRACE_EVENT0 (" flutter" , " OpacityLayer::Paint" );
5698 FML_DCHECK (needs_painting ());
5799
58100 SkPaint paint;
59- paint.setAlpha (alpha_ );
101+ paint.setAlpha (opacity () );
60102
61103 SkAutoCanvasRestore save (context.internal_nodes_canvas , true );
62104 context.internal_nodes_canvas ->translate (offset_.fX , offset_.fY );
@@ -83,16 +125,15 @@ void OpacityLayer::Paint(PaintContext& context) const {
83125 // RasterCache::GetIntegralTransCTM optimization.
84126 //
85127 // Note that the following lines are only accessible when the raster cache is
86- // not available (e.g., when we're using the software backend in golden
87- // tests).
128+ // not available, or when a cache miss occurs.
88129 SkRect saveLayerBounds;
89130 paint_bounds ()
90131 .makeOffset (-offset_.fX , -offset_.fY )
91132 .roundOut (&saveLayerBounds);
92133
93134 Layer::AutoSaveLayer save_layer =
94135 Layer::AutoSaveLayer::Create (context, saveLayerBounds, &paint);
95- PaintChildren (context);
136+ OpacityLayerBase::Paint (context);
96137}
97138
98139ContainerLayer* OpacityLayer::GetChildContainer () const {
0 commit comments