@@ -178,10 +178,6 @@ void Rasterizer::Draw(fml::RefPtr<Pipeline<flutter::LayerTree>> pipeline) {
178
178
consume_result = PipelineConsumeResult::MoreAvailable;
179
179
}
180
180
181
- if (surface_ != nullptr ) {
182
- surface_->ClearRenderContext ();
183
- }
184
-
185
181
// Merging the thread as we know the next `Draw` should be run on the platform
186
182
// thread.
187
183
if (surface_ != nullptr && surface_->GetExternalViewEmbedder () != nullptr ) {
@@ -408,7 +404,9 @@ RasterStatus Rasterizer::DoDraw(
408
404
409
405
RasterStatus Rasterizer::DrawToSurface (flutter::LayerTree& layer_tree) {
410
406
TRACE_EVENT0 (" flutter" , " Rasterizer::DrawToSurface" );
411
- FML_DCHECK (surface_);
407
+ if (!surface_) {
408
+ return RasterStatus::kFailed ;
409
+ }
412
410
413
411
// There is no way for the compositor to know how long the layer tree
414
412
// construction took. Fortunately, the layer tree does. Grab that time
@@ -474,6 +472,11 @@ RasterStatus Rasterizer::DrawToSurface(flutter::LayerTree& layer_tree) {
474
472
surface_->GetContext ()->performDeferredCleanup (kSkiaCleanupExpiration );
475
473
}
476
474
475
+ // Clear the render context after submitting the frame.
476
+ // This ensures that the GL context is released after drawing to the
477
+ // surface.
478
+ surface_->ClearRenderContext ();
479
+
477
480
return raster_status;
478
481
}
479
482
@@ -549,13 +552,6 @@ sk_sp<SkData> Rasterizer::ScreenshotLayerTreeAsImage(
549
552
SkMatrix root_surface_transformation;
550
553
root_surface_transformation.reset ();
551
554
552
- auto frame = compositor_context.ACQUIRE_FRAME (
553
- surface_context, canvas, nullptr , root_surface_transformation, false ,
554
- true , nullptr );
555
- canvas->clear (SK_ColorTRANSPARENT);
556
- frame->Raster (*tree, true );
557
- canvas->flush ();
558
-
559
555
// snapshot_surface->makeImageSnapshot needs the GL context to be set if the
560
556
// render context is GL. frame->Raster() pops the gl context in platforms that
561
557
// gl context switching are used. (For example, older iOS that uses GL) We
@@ -565,6 +561,14 @@ sk_sp<SkData> Rasterizer::ScreenshotLayerTreeAsImage(
565
561
FML_LOG (ERROR) << " Screenshot: unable to make image screenshot" ;
566
562
return nullptr ;
567
563
}
564
+
565
+ auto frame = compositor_context.ACQUIRE_FRAME (
566
+ surface_context, canvas, nullptr , root_surface_transformation, false ,
567
+ true , nullptr );
568
+ canvas->clear (SK_ColorTRANSPARENT);
569
+ frame->Raster (*tree, true );
570
+ canvas->flush ();
571
+
568
572
// Prepare an image from the surface, this image may potentially be on th GPU.
569
573
auto potentially_gpu_snapshot = snapshot_surface->makeImageSnapshot ();
570
574
if (!potentially_gpu_snapshot) {
0 commit comments