Skip to content

Commit

Permalink
Clear the GL context only after submitting the frame (flutter#20931)
Browse files Browse the repository at this point in the history
* Clear the GL context only after submitting the frame
* Fix Screenshot test
  • Loading branch information
Emmanuel Garcia authored Sep 2, 2020
1 parent 15bf1bb commit 615e668
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions shell/common/rasterizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,6 @@ void Rasterizer::Draw(fml::RefPtr<Pipeline<flutter::LayerTree>> 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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -549,13 +552,6 @@ sk_sp<SkData> 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
Expand All @@ -565,6 +561,14 @@ sk_sp<SkData> 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) {
Expand Down

0 comments on commit 615e668

Please sign in to comment.