Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Clear the GL context only after submitting the frame #20931

Merged
merged 4 commits into from
Sep 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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