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

Commit d4cd120

Browse files
committed
started providing the GPU sync switch to external view embedders so they can block background gpu calls
1 parent ce0a30c commit d4cd120

14 files changed

+67
-29
lines changed

flow/embedded_views.cc

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

77
namespace flutter {
88

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

flow/embedded_views.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
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"
1314
#include "third_party/skia/include/core/SkCanvas.h"
1415
#include "third_party/skia/include/core/SkPath.h"
1516
#include "third_party/skia/include/core/SkPoint.h"
@@ -309,8 +310,10 @@ class ExternalViewEmbedder {
309310
// This method can mutate the root Skia canvas before submitting the frame.
310311
//
311312
// It can also allocate frames for overlay surfaces to compose hybrid views.
312-
virtual void SubmitFrame(GrDirectContext* context,
313-
std::unique_ptr<SurfaceFrame> frame);
313+
virtual void SubmitFrame(
314+
GrDirectContext* context,
315+
std::unique_ptr<SurfaceFrame> frame,
316+
const std::shared_ptr<fml::SyncSwitch>& gpu_disable_sync_switch);
314317

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

shell/common/rasterizer.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,9 @@ RasterStatus Rasterizer::DrawToSurface(flutter::LayerTree& layer_tree) {
471471
}
472472
if (external_view_embedder != nullptr) {
473473
FML_DCHECK(!frame->IsSubmitted());
474-
external_view_embedder->SubmitFrame(surface_->GetContext(),
475-
std::move(frame));
474+
external_view_embedder->SubmitFrame(
475+
surface_->GetContext(), std::move(frame),
476+
delegate_.GetIsGpuDisabledSyncSwitch());
476477
} else {
477478
frame->Submit();
478479
}

shell/common/shell_test_external_view_embedder.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ SkCanvas* ShellTestExternalViewEmbedder::CompositeEmbeddedView(int view_id) {
6363
// |ExternalViewEmbedder|
6464
void ShellTestExternalViewEmbedder::SubmitFrame(
6565
GrDirectContext* context,
66-
std::unique_ptr<SurfaceFrame> frame) {
66+
std::unique_ptr<SurfaceFrame> frame,
67+
const std::shared_ptr<fml::SyncSwitch>& gpu_disable_sync_switch) {
6768
frame->Submit();
6869
if (frame && frame->SkiaSurface()) {
6970
last_submitted_frame_size_ = SkISize::Make(frame->SkiaSurface()->width(),

shell/common/shell_test_external_view_embedder.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ class ShellTestExternalViewEmbedder final : public ExternalViewEmbedder {
6262
SkCanvas* CompositeEmbeddedView(int view_id) override;
6363

6464
// |ExternalViewEmbedder|
65-
void SubmitFrame(GrDirectContext* context,
66-
std::unique_ptr<SurfaceFrame> frame) override;
65+
void SubmitFrame(
66+
GrDirectContext* context,
67+
std::unique_ptr<SurfaceFrame> frame,
68+
const std::shared_ptr<fml::SyncSwitch>& gpu_disable_sync_switch) override;
6769

6870
// |ExternalViewEmbedder|
6971
void EndFrame(

shell/platform/android/external_view_embedder/external_view_embedder.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ SkRect AndroidExternalViewEmbedder::GetViewRect(int view_id) const {
7575
// |ExternalViewEmbedder|
7676
void AndroidExternalViewEmbedder::SubmitFrame(
7777
GrDirectContext* context,
78-
std::unique_ptr<SurfaceFrame> frame) {
78+
std::unique_ptr<SurfaceFrame> frame,
79+
const std::shared_ptr<fml::SyncSwitch>& gpu_disable_sync_switch) {
7980
TRACE_EVENT0("flutter", "AndroidExternalViewEmbedder::SubmitFrame");
8081

8182
if (!FrameHasPlatformLayers()) {

shell/platform/android/external_view_embedder/external_view_embedder.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ class AndroidExternalViewEmbedder final : public ExternalViewEmbedder {
4646
std::vector<SkCanvas*> GetCurrentCanvases() override;
4747

4848
// |ExternalViewEmbedder|
49-
void SubmitFrame(GrDirectContext* context,
50-
std::unique_ptr<SurfaceFrame> frame) override;
49+
void SubmitFrame(
50+
GrDirectContext* context,
51+
std::unique_ptr<SurfaceFrame> frame,
52+
const std::shared_ptr<fml::SyncSwitch>& gpu_disable_sync_switch) override;
5153

5254
// |ExternalViewEmbedder|
5355
PostPrerollResult PostPrerollAction(

shell/platform/android/external_view_embedder/external_view_embedder_unittests.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ TEST(AndroidExternalViewEmbedder, SubmitFrame) {
329329
return true;
330330
});
331331

332-
embedder->SubmitFrame(gr_context.get(), std::move(surface_frame));
332+
embedder->SubmitFrame(gr_context.get(), std::move(surface_frame), nullptr);
333333
// Submits frame if no Android view in the current frame.
334334
EXPECT_TRUE(did_submit_frame);
335335
// Doesn't resubmit frame.
@@ -396,7 +396,7 @@ TEST(AndroidExternalViewEmbedder, SubmitFrame) {
396396
return true;
397397
});
398398

399-
embedder->SubmitFrame(gr_context.get(), std::move(surface_frame));
399+
embedder->SubmitFrame(gr_context.get(), std::move(surface_frame), nullptr);
400400
// Doesn't submit frame if there aren't Android views in the previous frame.
401401
EXPECT_FALSE(did_submit_frame);
402402
// Resubmits frame.
@@ -460,7 +460,7 @@ TEST(AndroidExternalViewEmbedder, SubmitFrame) {
460460
}
461461
return true;
462462
});
463-
embedder->SubmitFrame(gr_context.get(), std::move(surface_frame));
463+
embedder->SubmitFrame(gr_context.get(), std::move(surface_frame), nullptr);
464464
// Submits frame if there are Android views in the previous frame.
465465
EXPECT_TRUE(did_submit_frame);
466466
// Doesn't resubmit frame.
@@ -558,7 +558,7 @@ TEST(AndroidExternalViewEmbedder, DestroyOverlayLayersOnSizeChange) {
558558
std::make_unique<SurfaceFrame>(SkSurface::MakeNull(1000, 1000), false,
559559
[](const SurfaceFrame& surface_frame,
560560
SkCanvas* canvas) { return true; });
561-
embedder->SubmitFrame(gr_context.get(), std::move(surface_frame));
561+
embedder->SubmitFrame(gr_context.get(), std::move(surface_frame), nullptr);
562562

563563
EXPECT_CALL(*jni_mock, FlutterViewEndFrame());
564564
embedder->EndFrame(/*should_resubmit_frame=*/false, raster_thread_merger);
@@ -639,7 +639,7 @@ TEST(AndroidExternalViewEmbedder, DoesNotDestroyOverlayLayersOnSizeChange) {
639639
std::make_unique<SurfaceFrame>(SkSurface::MakeNull(1000, 1000), false,
640640
[](const SurfaceFrame& surface_frame,
641641
SkCanvas* canvas) { return true; });
642-
embedder->SubmitFrame(gr_context.get(), std::move(surface_frame));
642+
embedder->SubmitFrame(gr_context.get(), std::move(surface_frame), nullptr);
643643

644644
EXPECT_CALL(*jni_mock, FlutterViewEndFrame());
645645
embedder->EndFrame(/*should_resubmit_frame=*/false, raster_thread_merger);

shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,22 @@
466466
);
467467
}
468468

469-
bool FlutterPlatformViewsController::SubmitFrame(GrDirectContext* gr_context,
470-
std::shared_ptr<IOSContext> ios_context,
471-
std::unique_ptr<SurfaceFrame> frame) {
469+
bool FlutterPlatformViewsController::SubmitFrame(
470+
GrDirectContext* gr_context,
471+
std::shared_ptr<IOSContext> ios_context,
472+
std::unique_ptr<SurfaceFrame> frame,
473+
const std::shared_ptr<fml::SyncSwitch>& gpu_disable_sync_switch) {
474+
bool result = false;
475+
gpu_disable_sync_switch->Execute(
476+
fml::SyncSwitch::Handlers().SetIfTrue([&] { result = false; }).SetIfFalse([&] {
477+
result = SubmitFrameGpuSafe(gr_context, ios_context, std::move(frame));
478+
}));
479+
return result;
480+
}
481+
482+
bool FlutterPlatformViewsController::SubmitFrameGpuSafe(GrDirectContext* gr_context,
483+
std::shared_ptr<IOSContext> ios_context,
484+
std::unique_ptr<SurfaceFrame> frame) {
472485
FML_DCHECK(flutter_view_);
473486

474487
// Any UIKit related code has to run on main thread.

shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ class FlutterPlatformViewsController {
176176

177177
bool SubmitFrame(GrDirectContext* gr_context,
178178
std::shared_ptr<IOSContext> ios_context,
179-
std::unique_ptr<SurfaceFrame> frame);
179+
std::unique_ptr<SurfaceFrame> frame,
180+
const std::shared_ptr<fml::SyncSwitch>& gpu_disable_sync_switch);
180181

181182
// Invoked at the very end of a frame.
182183
// After invoking this method, nothing should happen on the current TaskRunner during the same
@@ -308,6 +309,10 @@ class FlutterPlatformViewsController {
308309
// Commit a CATransaction if |BeginCATransaction| has been called during the frame.
309310
void CommitCATransactionIfNeeded();
310311

312+
bool SubmitFrameGpuSafe(GrDirectContext* gr_context,
313+
std::shared_ptr<IOSContext> ios_context,
314+
std::unique_ptr<SurfaceFrame> frame);
315+
311316
FML_DISALLOW_COPY_AND_ASSIGN(FlutterPlatformViewsController);
312317
};
313318

0 commit comments

Comments
 (0)