@@ -178,10 +178,6 @@ void Rasterizer::Draw(fml::RefPtr<Pipeline<flutter::LayerTree>> pipeline) {
178178 consume_result = PipelineConsumeResult::MoreAvailable;
179179 }
180180
181- if (surface_ != nullptr ) {
182- surface_->ClearRenderContext ();
183- }
184-
185181 // Merging the thread as we know the next `Draw` should be run on the platform
186182 // thread.
187183 if (surface_ != nullptr && surface_->GetExternalViewEmbedder () != nullptr ) {
@@ -408,7 +404,9 @@ RasterStatus Rasterizer::DoDraw(
408404
409405RasterStatus Rasterizer::DrawToSurface (flutter::LayerTree& layer_tree) {
410406 TRACE_EVENT0 (" flutter" , " Rasterizer::DrawToSurface" );
411- FML_DCHECK (surface_);
407+ if (!surface_) {
408+ return RasterStatus::kFailed ;
409+ }
412410
413411 // There is no way for the compositor to know how long the layer tree
414412 // construction took. Fortunately, the layer tree does. Grab that time
@@ -474,6 +472,11 @@ RasterStatus Rasterizer::DrawToSurface(flutter::LayerTree& layer_tree) {
474472 surface_->GetContext ()->performDeferredCleanup (kSkiaCleanupExpiration );
475473 }
476474
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+
477480 return raster_status;
478481 }
479482
@@ -549,13 +552,6 @@ sk_sp<SkData> Rasterizer::ScreenshotLayerTreeAsImage(
549552 SkMatrix root_surface_transformation;
550553 root_surface_transformation.reset ();
551554
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-
559555 // snapshot_surface->makeImageSnapshot needs the GL context to be set if the
560556 // render context is GL. frame->Raster() pops the gl context in platforms that
561557 // gl context switching are used. (For example, older iOS that uses GL) We
@@ -565,6 +561,14 @@ sk_sp<SkData> Rasterizer::ScreenshotLayerTreeAsImage(
565561 FML_LOG (ERROR) << " Screenshot: unable to make image screenshot" ;
566562 return nullptr ;
567563 }
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+
568572 // Prepare an image from the surface, this image may potentially be on th GPU.
569573 auto potentially_gpu_snapshot = snapshot_surface->makeImageSnapshot ();
570574 if (!potentially_gpu_snapshot) {
0 commit comments