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

Commit be8467e

Browse files
Started providing the GPU sync switch to Rasterizer.DrawToSurface() (#28383)
Co-authored-by: Aaron Clarke <gaaclarke>
1 parent 5bb203d commit be8467e

37 files changed

+335
-97
lines changed

flow/compositor_context.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ enum class RasterStatus {
4646
kEnqueuePipeline,
4747
// Failed to rasterize the frame.
4848
kFailed,
49-
// Layer tree was discarded due to LayerTreeDiscardCallback
50-
kDiscarded
49+
// Layer tree was discarded due to LayerTreeDiscardCallback or inability to
50+
// access the GPU.
51+
kDiscarded,
52+
// Drawing was yielded to allow the correct thread to draw as a result of the
53+
// RasterThreadMerger.
54+
kYielded,
5155
};
5256

5357
class CompositorContext {

flow/embedded_views.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66

77
namespace flutter {
88

9-
void ExternalViewEmbedder::SubmitFrame(
10-
GrDirectContext* context,
11-
std::unique_ptr<SurfaceFrame> frame,
12-
const std::shared_ptr<const fml::SyncSwitch>& gpu_disable_sync_switch) {
9+
void ExternalViewEmbedder::SubmitFrame(GrDirectContext* context,
10+
std::unique_ptr<SurfaceFrame> frame) {
1311
frame->Submit();
1412
};
1513

flow/embedded_views.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "flutter/flow/surface_frame.h"
1111
#include "flutter/fml/memory/ref_counted.h"
1212
#include "flutter/fml/raster_thread_merger.h"
13-
#include "flutter/fml/synchronization/sync_switch.h"
1413
#include "third_party/skia/include/core/SkCanvas.h"
1514
#include "third_party/skia/include/core/SkPath.h"
1615
#include "third_party/skia/include/core/SkPoint.h"
@@ -313,10 +312,8 @@ class ExternalViewEmbedder {
313312
// This method can mutate the root Skia canvas before submitting the frame.
314313
//
315314
// It can also allocate frames for overlay surfaces to compose hybrid views.
316-
virtual void SubmitFrame(
317-
GrDirectContext* context,
318-
std::unique_ptr<SurfaceFrame> frame,
319-
const std::shared_ptr<const fml::SyncSwitch>& gpu_disable_sync_switch);
315+
virtual void SubmitFrame(GrDirectContext* context,
316+
std::unique_ptr<SurfaceFrame> frame);
320317

321318
// This method provides the embedder a way to do additional tasks after
322319
// |SubmitFrame|. For example, merge task runners if `should_resubmit_frame`

flow/surface.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ bool Surface::ClearRenderContext() {
1818
return false;
1919
}
2020

21+
bool Surface::AllowsDrawingWhenGpuDisabled() const {
22+
return true;
23+
}
24+
2125
} // namespace flutter

flow/surface.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class Surface {
3333

3434
virtual bool ClearRenderContext();
3535

36+
virtual bool AllowsDrawingWhenGpuDisabled() const;
37+
3638
private:
3739
FML_DISALLOW_COPY_AND_ASSIGN(Surface);
3840
};

shell/common/rasterizer.cc

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ void Rasterizer::DrawLastLayerTree(
147147
DrawToSurface(*frame_timings_recorder, *last_layer_tree_);
148148
}
149149

150-
void Rasterizer::Draw(
150+
RasterStatus Rasterizer::Draw(
151151
std::unique_ptr<FrameTimingsRecorder> frame_timings_recorder,
152152
std::shared_ptr<Pipeline<flutter::LayerTree>> pipeline,
153153
LayerTreeDiscardCallback discardCallback) {
@@ -156,7 +156,7 @@ void Rasterizer::Draw(
156156
if (raster_thread_merger_ &&
157157
!raster_thread_merger_->IsOnRasterizingThread()) {
158158
// we yield and let this frame be serviced on the right thread.
159-
return;
159+
return RasterStatus::kYielded;
160160
}
161161
FML_DCHECK(delegate_.GetTaskRunners()
162162
.GetRasterTaskRunner()
@@ -217,6 +217,8 @@ void Rasterizer::Draw(
217217
default:
218218
break;
219219
}
220+
221+
return raster_status;
220222
}
221223

222224
namespace {
@@ -385,6 +387,8 @@ RasterStatus Rasterizer::DoDraw(
385387
raster_status == RasterStatus::kSkipAndRetry) {
386388
resubmitted_layer_tree_ = std::move(layer_tree);
387389
return raster_status;
390+
} else if (raster_status == RasterStatus::kDiscarded) {
391+
return raster_status;
388392
}
389393

390394
if (persistent_cache->IsDumpingSkp() &&
@@ -465,6 +469,31 @@ RasterStatus Rasterizer::DrawToSurface(
465469
TRACE_EVENT0("flutter", "Rasterizer::DrawToSurface");
466470
FML_DCHECK(surface_);
467471

472+
RasterStatus raster_status;
473+
if (surface_->AllowsDrawingWhenGpuDisabled()) {
474+
raster_status = DrawToSurfaceUnsafe(frame_timings_recorder, layer_tree);
475+
} else {
476+
delegate_.GetIsGpuDisabledSyncSwitch()->Execute(
477+
fml::SyncSwitch::Handlers()
478+
.SetIfTrue([&] { raster_status = RasterStatus::kDiscarded; })
479+
.SetIfFalse([&] {
480+
raster_status =
481+
DrawToSurfaceUnsafe(frame_timings_recorder, layer_tree);
482+
}));
483+
}
484+
485+
return raster_status;
486+
}
487+
488+
/// Unsafe because it assumes we have access to the GPU which isn't the case
489+
/// when iOS is backgrounded, for example.
490+
/// \see Rasterizer::DrawToSurface
491+
RasterStatus Rasterizer::DrawToSurfaceUnsafe(
492+
FrameTimingsRecorder& frame_timings_recorder,
493+
flutter::LayerTree& layer_tree) {
494+
TRACE_EVENT0("flutter", "Rasterizer::DrawToSurfaceUnsafe");
495+
FML_DCHECK(surface_);
496+
468497
compositor_context_->ui_time().SetLapTime(
469498
frame_timings_recorder.GetBuildDuration());
470499

@@ -512,9 +541,8 @@ RasterStatus Rasterizer::DrawToSurface(
512541
if (external_view_embedder_ &&
513542
(!raster_thread_merger_ || raster_thread_merger_->IsMerged())) {
514543
FML_DCHECK(!frame->IsSubmitted());
515-
external_view_embedder_->SubmitFrame(
516-
surface_->GetContext(), std::move(frame),
517-
delegate_.GetIsGpuDisabledSyncSwitch());
544+
external_view_embedder_->SubmitFrame(surface_->GetContext(),
545+
std::move(frame));
518546
} else {
519547
frame->Submit();
520548
}

shell/common/rasterizer.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,10 @@ class Rasterizer final : public SnapshotDelegate {
233233
/// @param[in] discardCallback if specified and returns true, the layer tree
234234
/// is discarded instead of being rendered
235235
///
236-
void Draw(std::unique_ptr<FrameTimingsRecorder> frame_timings_recorder,
237-
std::shared_ptr<Pipeline<flutter::LayerTree>> pipeline,
238-
LayerTreeDiscardCallback discardCallback = NoDiscard);
236+
RasterStatus Draw(
237+
std::unique_ptr<FrameTimingsRecorder> frame_timings_recorder,
238+
std::shared_ptr<Pipeline<flutter::LayerTree>> pipeline,
239+
LayerTreeDiscardCallback discardCallback = NoDiscard);
239240

240241
//----------------------------------------------------------------------------
241242
/// @brief The type of the screenshot to obtain of the previously
@@ -492,6 +493,9 @@ class Rasterizer final : public SnapshotDelegate {
492493
RasterStatus DrawToSurface(FrameTimingsRecorder& frame_timings_recorder,
493494
flutter::LayerTree& layer_tree);
494495

496+
RasterStatus DrawToSurfaceUnsafe(FrameTimingsRecorder& frame_timings_recorder,
497+
flutter::LayerTree& layer_tree);
498+
495499
void FireNextFrameCallbackIfPresent();
496500

497501
static bool NoDiscard(const flutter::LayerTree& layer_tree) { return false; }

0 commit comments

Comments
 (0)