@@ -52,23 +52,32 @@ void PhysicalShapeLayer::Preroll(PrerollContext* context,
5252
5353 context->total_elevation += elevation_;
5454 total_elevation_ = context->total_elevation ;
55+ #if defined(OS_FUCHSIA)
56+ child_layer_exists_below_ = context->child_scene_layer_exists_below ;
57+ context->child_scene_layer_exists_below = false ;
58+ #endif
59+
5560 SkRect child_paint_bounds;
5661 PrerollChildren (context, matrix, &child_paint_bounds);
62+
63+ #if defined(OS_FUCHSIA)
64+ children_need_system_compositing_ = needs_system_composite ();
65+ if (child_layer_exists_below_) {
66+ set_needs_system_composite (true );
67+ }
68+ context->child_scene_layer_exists_below =
69+ context->child_scene_layer_exists_below || child_layer_exists_below_;
70+ #endif
5771 context->total_elevation -= elevation_;
5872
5973 if (elevation_ == 0 ) {
6074 set_paint_bounds (path_.getBounds ());
6175 } else {
62- #if defined(OS_FUCHSIA)
63- // Let the system compositor draw all shadows for us.
64- set_needs_system_composite (true );
65- #else
6676 // We will draw the shadow in Paint(), so add some margin to the paint
6777 // bounds to leave space for the shadow. We fill this whole region and clip
6878 // children to it so we don't need to join the child paint bounds.
6979 set_paint_bounds (ComputeShadowBounds (path_.getBounds (), elevation_,
7080 context->frame_device_pixel_ratio ));
71- #endif // defined(OS_FUCHSIA)
7281 }
7382}
7483
@@ -78,30 +87,64 @@ void PhysicalShapeLayer::UpdateScene(SceneUpdateContext& context) {
7887 FML_DCHECK (needs_system_composite ());
7988 TRACE_EVENT0 (" flutter" , " PhysicalShapeLayer::UpdateScene" );
8089
81- // Retained rendering: speedup by reusing a retained entity node if possible.
82- // When an entity node is reused, no paint layer is added to the frame so we
83- // won't call PhysicalShapeLayer::Paint.
84- LayerRasterCacheKey key (unique_id (), context.Matrix ());
85- if (context.HasRetainedNode (key)) {
86- TRACE_EVENT_INSTANT0 (" flutter" , " retained layer cache hit" );
87- const scenic::EntityNode& retained_node = context.GetRetainedNode (key);
88- FML_DCHECK (context.top_entity ());
89- FML_DCHECK (retained_node.session () == context.session ());
90- context.top_entity ()->entity_node ().AddChild (retained_node);
91- return ;
92- }
90+ // If there is embedded Fuchsia content in the scene (a ChildSceneLayer),
91+ // PhysicalShapeLayers that appear above the embedded content will be turned
92+ // into their own Scenic layers.
93+ if (child_layer_exists_below_) {
94+ // Reset paint bounds, because shadows are not rendered in this case
95+ // (see comment in Paint()).
96+ set_paint_bounds (path_.getBounds ());
97+
98+ float global_scenic_elevation = context.GetGlobalElevationForNextScenicLayer ();
99+ float local_scenic_elevation_ =
100+ global_scenic_elevation - context.scenic_elevation ();
101+ float z_translation = -local_scenic_elevation_;
102+
103+ // Retained rendering: speedup by reusing a retained entity node if
104+ // possible. When an entity node is reused, no paint layer is added to the
105+ // frame so we won't call PhysicalShapeLayer::Paint.
106+ LayerRasterCacheKey key (unique_id (), context.Matrix ());
107+ if (context.HasRetainedNode (key)) {
108+ TRACE_EVENT_INSTANT0 (" flutter" , " retained layer cache hit" );
109+ scenic::EntityNode* retained_node = context.GetRetainedNode (key);
110+ FML_DCHECK (context.top_entity ());
111+ FML_DCHECK (retained_node->session () == context.session ());
93112
94- TRACE_EVENT_INSTANT0 (" flutter" , " cache miss, creating" );
95- // If we can't find an existing retained surface, create one.
96- SceneUpdateContext::Frame frame (context, frameRRect_, color_, SK_AlphaOPAQUE,
97- elevation_, total_elevation_, this );
98- for (auto & layer : layers ()) {
99- if (layer->needs_painting ()) {
100- frame.AddPaintLayer (layer.get ());
113+ // Re-adjust the elevation.
114+ retained_node->SetTranslation (0 .f , 0 .f , z_translation);
115+
116+ context.top_entity ()->entity_node ().AddChild (*retained_node);
117+ return ;
118+ }
119+
120+ TRACE_EVENT_INSTANT0 (" flutter" , " cache miss, creating" );
121+ // If we can't find an existing retained surface, create one.
122+ SceneUpdateContext::Frame frame (context, frameRRect_, SK_ColorTRANSPARENT,
123+ sk_float_round2int (context.alphaf () * 255 ),
124+ " flutter::PhysicalShapeLayer" ,
125+ z_translation, this );
126+
127+ frame.AddPaintLayer (this );
128+
129+ // Node: UpdateSceneChildren needs to be called here so that |frame| is still
130+ // in scope (and therefore alive) while UpdateSceneChildren is being called.
131+ if (children_need_system_compositing_) {
132+ UpdateSceneChildren (context);
133+ }
134+ } else {
135+ if (children_need_system_compositing_) {
136+ UpdateSceneChildren (context);
101137 }
102138 }
139+ }
140+
141+ void PhysicalShapeLayer::UpdateSceneChildren (SceneUpdateContext& context) {
142+ float scenic_elevation = context.scenic_elevation ();
143+ context.set_scenic_elevation (scenic_elevation + local_scenic_elevation_);
103144
104- UpdateSceneChildren (context);
145+ ContainerLayer::UpdateSceneChildren (context);
146+
147+ context.set_scenic_elevation (scenic_elevation);
105148}
106149
107150#endif // defined(OS_FUCHSIA)
@@ -110,7 +153,20 @@ void PhysicalShapeLayer::Paint(PaintContext& context) const {
110153 TRACE_EVENT0 (" flutter" , " PhysicalShapeLayer::Paint" );
111154 FML_DCHECK (needs_painting ());
112155
156+
157+ #if defined(OS_FUCHSIA)
158+ // TODO: Re-enable shadow drawing here.
159+ // Shadows are not rendered for PhysicalShapeLayers that exist as separate
160+ // system services; this is to maintain compatibility with the previous
161+ // implementation and has the added benefit of requiring smaller textures, since
162+ // extra space is not needed for the shadows.
163+ // This behavior might change after clients adjust their usage of PhysicalShaperLayer
164+ // to make elevation correlate to desired shadow size.
165+ // if (false && !child_layer_exists_below_ && elevation_ != 0) {
166+ if (elevation_ != 0 ) {
167+ #else
113168 if (elevation_ != 0 ) {
169+ #endif
114170 DrawShadow (context.leaf_nodes_canvas , path_, shadow_color_, elevation_,
115171 SkColorGetA (color_) != 0xff , context.frame_device_pixel_ratio );
116172 }
0 commit comments