diff --git a/shell/common/rasterizer.cc b/shell/common/rasterizer.cc index 8516d3cd5006a..6a2c2ee1f189d 100644 --- a/shell/common/rasterizer.cc +++ b/shell/common/rasterizer.cc @@ -178,10 +178,6 @@ void Rasterizer::Draw(fml::RefPtr> pipeline) { consume_result = PipelineConsumeResult::MoreAvailable; } - if (surface_ != nullptr) { - surface_->ClearRenderContext(); - } - // Merging the thread as we know the next `Draw` should be run on the platform // thread. if (surface_ != nullptr && surface_->GetExternalViewEmbedder() != nullptr) { @@ -408,7 +404,9 @@ RasterStatus Rasterizer::DoDraw( RasterStatus Rasterizer::DrawToSurface(flutter::LayerTree& layer_tree) { TRACE_EVENT0("flutter", "Rasterizer::DrawToSurface"); - FML_DCHECK(surface_); + if (!surface_) { + return RasterStatus::kFailed; + } // There is no way for the compositor to know how long the layer tree // construction took. Fortunately, the layer tree does. Grab that time @@ -474,6 +472,11 @@ RasterStatus Rasterizer::DrawToSurface(flutter::LayerTree& layer_tree) { surface_->GetContext()->performDeferredCleanup(kSkiaCleanupExpiration); } + // Clear the render context after submitting the frame. + // This ensures that the GL context is released after drawing to the + // surface. + surface_->ClearRenderContext(); + return raster_status; } @@ -549,13 +552,6 @@ sk_sp Rasterizer::ScreenshotLayerTreeAsImage( SkMatrix root_surface_transformation; root_surface_transformation.reset(); - auto frame = compositor_context.ACQUIRE_FRAME( - surface_context, canvas, nullptr, root_surface_transformation, false, - true, nullptr); - canvas->clear(SK_ColorTRANSPARENT); - frame->Raster(*tree, true); - canvas->flush(); - // snapshot_surface->makeImageSnapshot needs the GL context to be set if the // render context is GL. frame->Raster() pops the gl context in platforms that // gl context switching are used. (For example, older iOS that uses GL) We @@ -565,6 +561,14 @@ sk_sp Rasterizer::ScreenshotLayerTreeAsImage( FML_LOG(ERROR) << "Screenshot: unable to make image screenshot"; return nullptr; } + + auto frame = compositor_context.ACQUIRE_FRAME( + surface_context, canvas, nullptr, root_surface_transformation, false, + true, nullptr); + canvas->clear(SK_ColorTRANSPARENT); + frame->Raster(*tree, true); + canvas->flush(); + // Prepare an image from the surface, this image may potentially be on th GPU. auto potentially_gpu_snapshot = snapshot_surface->makeImageSnapshot(); if (!potentially_gpu_snapshot) {