Skip to content

Commit 2dcfaae

Browse files
author
Chris Yang
authored
Reland "Guarding EAGLContext used by Flutter flutter#13314" (flutter#13759)
1 parent 55c64a9 commit 2dcfaae

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+745
-123
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,8 @@ FILE: ../../../flutter/shell/common/pointer_data_dispatcher.cc
511511
FILE: ../../../flutter/shell/common/pointer_data_dispatcher.h
512512
FILE: ../../../flutter/shell/common/rasterizer.cc
513513
FILE: ../../../flutter/shell/common/rasterizer.h
514+
FILE: ../../../flutter/shell/common/renderer_context_switch_manager.cc
515+
FILE: ../../../flutter/shell/common/renderer_context_switch_manager.h
514516
FILE: ../../../flutter/shell/common/run_configuration.cc
515517
FILE: ../../../flutter/shell/common/run_configuration.h
516518
FILE: ../../../flutter/shell/common/shell.cc
@@ -800,6 +802,8 @@ FILE: ../../../flutter/shell/platform/darwin/ios/ios_external_texture_gl.h
800802
FILE: ../../../flutter/shell/platform/darwin/ios/ios_external_texture_gl.mm
801803
FILE: ../../../flutter/shell/platform/darwin/ios/ios_gl_context.h
802804
FILE: ../../../flutter/shell/platform/darwin/ios/ios_gl_context.mm
805+
FILE: ../../../flutter/shell/platform/darwin/ios/ios_gl_context_switch_manager.h
806+
FILE: ../../../flutter/shell/platform/darwin/ios/ios_gl_context_switch_manager.mm
803807
FILE: ../../../flutter/shell/platform/darwin/ios/ios_gl_render_target.h
804808
FILE: ../../../flutter/shell/platform/darwin/ios/ios_gl_render_target.mm
805809
FILE: ../../../flutter/shell/platform/darwin/ios/ios_surface.h

shell/common/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ source_set("common") {
7878
"pointer_data_dispatcher.h",
7979
"rasterizer.cc",
8080
"rasterizer.h",
81+
"renderer_context_switch_manager.cc",
82+
"renderer_context_switch_manager.h",
8183
"run_configuration.cc",
8284
"run_configuration.h",
8385
"shell.cc",

shell/common/rasterizer.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ sk_sp<SkImage> Rasterizer::MakeRasterSnapshot(sk_sp<SkPicture> picture,
163163
// happen in case of software rendering.
164164
surface = SkSurface::MakeRaster(image_info);
165165
} else {
166-
if (!surface_->MakeRenderContextCurrent()) {
166+
std::unique_ptr<RendererContextSwitchManager::RendererContextSwitch>
167+
context_switch = surface_->MakeRenderContextCurrent();
168+
if (context_switch->GetSwitchResult()) {
167169
return nullptr;
168170
}
169171

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "renderer_context_switch_manager.h"
6+
7+
namespace flutter {
8+
9+
RendererContextSwitchManager::RendererContextSwitchManager() = default;
10+
11+
RendererContextSwitchManager::~RendererContextSwitchManager() = default;
12+
13+
RendererContextSwitchManager::RendererContextSwitch::RendererContextSwitch() =
14+
default;
15+
16+
RendererContextSwitchManager::RendererContextSwitch::~RendererContextSwitch(){};
17+
18+
RendererContextSwitchManager::RendererContextSwitchPureResult::
19+
RendererContextSwitchPureResult(bool switch_result)
20+
: switch_result_(switch_result){};
21+
22+
RendererContextSwitchManager::RendererContextSwitchPureResult::
23+
~RendererContextSwitchPureResult() = default;
24+
25+
bool RendererContextSwitchManager::RendererContextSwitchPureResult::
26+
GetSwitchResult() {
27+
return switch_result_;
28+
}
29+
30+
} // namespace flutter
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef FLUTTER_SHELL_COMMON_GL_CONTEXT_SWITCH_MANAGER_H_
6+
#define FLUTTER_SHELL_COMMON_GL_CONTEXT_SWITCH_MANAGER_H_
7+
8+
#include <memory>
9+
#include "flutter/fml/macros.h"
10+
11+
namespace flutter {
12+
13+
//------------------------------------------------------------------------------
14+
/// Manages `RendererContextSwitch`.
15+
///
16+
/// Should be subclassed for platforms that uses GL and requires context
17+
/// switching. Always use `MakeCurrent` and `ResourceMakeCurrent` in the
18+
/// `RendererContextSwitchManager` to set gl contexts.
19+
///
20+
class RendererContextSwitchManager {
21+
public:
22+
//------------------------------------------------------------------------------
23+
/// Switches the gl context to the flutter's contexts.
24+
///
25+
/// Should be subclassed for each platform embedder that uses GL.
26+
/// In construction, it should set the current context to a flutter's context
27+
/// In destruction, it should rest the current context.
28+
///
29+
class RendererContextSwitch {
30+
public:
31+
RendererContextSwitch();
32+
33+
virtual ~RendererContextSwitch();
34+
35+
virtual bool GetSwitchResult() = 0;
36+
37+
FML_DISALLOW_COPY_AND_ASSIGN(RendererContextSwitch);
38+
};
39+
40+
RendererContextSwitchManager();
41+
~RendererContextSwitchManager();
42+
43+
//----------------------------------------------------------------------------
44+
/// @brief Creates a shell instance using the provided settings. The
45+
/// callbacks to create the various shell subcomponents will be
46+
/// called on the appropriate threads before this method returns.
47+
/// If this is the first instance of a shell in the process, this
48+
/// call also bootstraps the Dart VM.
49+
///
50+
/// @param[in] task_runners The task runners
51+
/// @param[in] settings The settings
52+
/// @param[in] on_create_platform_view The callback that must return a
53+
/// platform view. This will be called on
54+
/// the platform task runner before this
55+
/// method returns.
56+
/// @param[in] on_create_rasterizer That callback that must provide a
57+
/// valid rasterizer. This will be called
58+
/// on the render task runner before this
59+
/// method returns.
60+
///
61+
/// @return A full initialized shell if the settings and callbacks are
62+
/// valid. The root isolate has been created but not yet launched.
63+
/// It may be launched by obtaining the engine weak pointer and
64+
/// posting a task onto the UI task runner with a valid run
65+
/// configuration to run the isolate. The embedder must always
66+
/// check the validity of the shell (using the IsSetup call)
67+
/// immediately after getting a pointer to it.
68+
///
69+
70+
//----------------------------------------------------------------------------
71+
/// @brief Make the flutter's context as current context.
72+
///
73+
/// @return A `RendererContextSwitch` with `GetSwitchResult` returning
74+
/// true if the setting process is succesful.
75+
virtual std::unique_ptr<RendererContextSwitch> MakeCurrent() = 0;
76+
77+
//----------------------------------------------------------------------------
78+
/// @brief Make the flutter's resources context as current context.
79+
///
80+
/// @return A `RendererContextSwitch` with `GetSwitchResult` returning
81+
/// true if the setting process is succesful.
82+
virtual std::unique_ptr<RendererContextSwitch> ResourceMakeCurrent() = 0;
83+
84+
//------------------------------------------------------------------------------
85+
/// A representation of a `RendererContextSwitch` that doesn't require actual
86+
/// context switching.
87+
///
88+
class RendererContextSwitchPureResult final : public RendererContextSwitch {
89+
public:
90+
// Constructor that creates an `RendererContextSwitchPureResult`.
91+
// The `GetSwitchResult` will return the same value as `switch_result`.
92+
93+
//----------------------------------------------------------------------------
94+
/// @brief Constructs a `RendererContextSwitchPureResult`.
95+
///
96+
/// @param[in] switch_result the switch result that will be returned
97+
/// in `GetSwitchResult()`
98+
///
99+
RendererContextSwitchPureResult(bool switch_result);
100+
101+
~RendererContextSwitchPureResult();
102+
103+
bool GetSwitchResult() override;
104+
105+
private:
106+
bool switch_result_;
107+
108+
FML_DISALLOW_COPY_AND_ASSIGN(RendererContextSwitchPureResult);
109+
};
110+
111+
FML_DISALLOW_COPY_AND_ASSIGN(RendererContextSwitchManager);
112+
};
113+
114+
} // namespace flutter
115+
116+
#endif

shell/common/shell_test.cc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,11 @@ PointerDataDispatcherMaker ShellTestPlatformView::GetDispatcherMaker() {
355355
}
356356

357357
// |GPUSurfaceGLDelegate|
358-
bool ShellTestPlatformView::GLContextMakeCurrent() {
359-
return gl_surface_.MakeCurrent();
358+
std::unique_ptr<RendererContextSwitchManager::RendererContextSwitch>
359+
ShellTestPlatformView::GLContextMakeCurrent() {
360+
return std::make_unique<
361+
RendererContextSwitchManager::RendererContextSwitchPureResult>(
362+
gl_surface_.MakeCurrent());
360363
}
361364

362365
// |GPUSurfaceGLDelegate|
@@ -387,5 +390,10 @@ ExternalViewEmbedder* ShellTestPlatformView::GetExternalViewEmbedder() {
387390
return nullptr;
388391
}
389392

393+
std::shared_ptr<RendererContextSwitchManager>
394+
ShellTestPlatformView::GetRendererContextSwitchManager() {
395+
return nullptr;
396+
}
397+
390398
} // namespace testing
391399
} // namespace flutter

shell/common/shell_test.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ class ShellTestPlatformView : public PlatformView, public GPUSurfaceGLDelegate {
144144
PointerDataDispatcherMaker GetDispatcherMaker() override;
145145

146146
// |GPUSurfaceGLDelegate|
147-
bool GLContextMakeCurrent() override;
147+
std::unique_ptr<RendererContextSwitchManager::RendererContextSwitch>
148+
GLContextMakeCurrent() override;
148149

149150
// |GPUSurfaceGLDelegate|
150151
bool GLContextClearCurrent() override;
@@ -161,6 +162,9 @@ class ShellTestPlatformView : public PlatformView, public GPUSurfaceGLDelegate {
161162
// |GPUSurfaceGLDelegate|
162163
ExternalViewEmbedder* GetExternalViewEmbedder() override;
163164

165+
std::shared_ptr<RendererContextSwitchManager>
166+
GetRendererContextSwitchManager() override;
167+
164168
FML_DISALLOW_COPY_AND_ASSIGN(ShellTestPlatformView);
165169
};
166170

shell/common/surface.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ flutter::ExternalViewEmbedder* Surface::GetExternalViewEmbedder() {
6060
return nullptr;
6161
}
6262

63-
bool Surface::MakeRenderContextCurrent() {
64-
return true;
63+
std::unique_ptr<RendererContextSwitchManager::RendererContextSwitch>
64+
Surface::MakeRenderContextCurrent() {
65+
return std::make_unique<
66+
RendererContextSwitchManager::RendererContextSwitchPureResult>(true);
6567
}
6668

6769
} // namespace flutter

shell/common/surface.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "flutter/flow/compositor_context.h"
1111
#include "flutter/flow/embedded_views.h"
1212
#include "flutter/fml/macros.h"
13+
#include "flutter/shell/common/renderer_context_switch_manager.h"
1314
#include "third_party/skia/include/core/SkCanvas.h"
1415

1516
namespace flutter {
@@ -58,7 +59,8 @@ class Surface {
5859

5960
virtual flutter::ExternalViewEmbedder* GetExternalViewEmbedder();
6061

61-
virtual bool MakeRenderContextCurrent();
62+
virtual std::unique_ptr<RendererContextSwitchManager::RendererContextSwitch>
63+
MakeRenderContextCurrent();
6264

6365
private:
6466
FML_DISALLOW_COPY_AND_ASSIGN(Surface);

shell/gpu/gpu_surface_gl.cc

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "flutter/fml/size.h"
1010
#include "flutter/fml/trace_event.h"
1111
#include "flutter/shell/common/persistent_cache.h"
12+
#include "flutter/shell/common/renderer_context_switch_manager.h"
1213
#include "third_party/skia/include/core/SkColorFilter.h"
1314
#include "third_party/skia/include/core/SkSurface.h"
1415
#include "third_party/skia/include/gpu/GrBackendSurface.h"
@@ -39,7 +40,10 @@ GPUSurfaceGL::GPUSurfaceGL(GPUSurfaceGLDelegate* delegate,
3940
: delegate_(delegate),
4041
render_to_surface_(render_to_surface),
4142
weak_factory_(this) {
42-
if (!delegate_->GLContextMakeCurrent()) {
43+
std::unique_ptr<RendererContextSwitchManager::RendererContextSwitch>
44+
context_switch = delegate_->GLContextMakeCurrent();
45+
46+
if (!context_switch->GetSwitchResult()) {
4347
FML_LOG(ERROR)
4448
<< "Could not make the context current to setup the gr context.";
4549
return;
@@ -87,8 +91,6 @@ GPUSurfaceGL::GPUSurfaceGL(GPUSurfaceGLDelegate* delegate,
8791
}
8892
FML_LOG(INFO) << "Found " << caches.size() << " SkSL shaders; precompiled "
8993
<< compiled_count;
90-
91-
delegate_->GLContextClearCurrent();
9294
}
9395

9496
GPUSurfaceGL::GPUSurfaceGL(sk_sp<GrContext> gr_context,
@@ -98,7 +100,9 @@ GPUSurfaceGL::GPUSurfaceGL(sk_sp<GrContext> gr_context,
98100
context_(gr_context),
99101
render_to_surface_(render_to_surface),
100102
weak_factory_(this) {
101-
if (!delegate_->GLContextMakeCurrent()) {
103+
std::unique_ptr<RendererContextSwitchManager::RendererContextSwitch>
104+
context_switch = delegate_->GLContextMakeCurrent();
105+
if (!context_switch->GetSwitchResult()) {
102106
FML_LOG(ERROR)
103107
<< "Could not make the context current to setup the gr context.";
104108
return;
@@ -114,8 +118,9 @@ GPUSurfaceGL::~GPUSurfaceGL() {
114118
if (!valid_) {
115119
return;
116120
}
117-
118-
if (!delegate_->GLContextMakeCurrent()) {
121+
std::unique_ptr<RendererContextSwitchManager::RendererContextSwitch>
122+
context_switch = delegate_->GLContextMakeCurrent();
123+
if (!context_switch->GetSwitchResult()) {
119124
FML_LOG(ERROR) << "Could not make the context current to destroy the "
120125
"GrContext resources.";
121126
return;
@@ -126,8 +131,6 @@ GPUSurfaceGL::~GPUSurfaceGL() {
126131
context_->releaseResourcesAndAbandonContext();
127132
}
128133
context_ = nullptr;
129-
130-
delegate_->GLContextClearCurrent();
131134
}
132135

133136
// |Surface|
@@ -253,7 +256,9 @@ std::unique_ptr<SurfaceFrame> GPUSurfaceGL::AcquireFrame(const SkISize& size) {
253256
return nullptr;
254257
}
255258

256-
if (!delegate_->GLContextMakeCurrent()) {
259+
std::unique_ptr<RendererContextSwitchManager::RendererContextSwitch>
260+
context_switch = delegate_->GLContextMakeCurrent();
261+
if (!context_switch->GetSwitchResult()) {
257262
FML_LOG(ERROR)
258263
<< "Could not make the context current to acquire the frame.";
259264
return nullptr;
@@ -285,14 +290,18 @@ std::unique_ptr<SurfaceFrame> GPUSurfaceGL::AcquireFrame(const SkISize& size) {
285290
return weak ? weak->PresentSurface(canvas) : false;
286291
};
287292

288-
return std::make_unique<SurfaceFrame>(surface, submit_callback);
293+
std::unique_ptr<SurfaceFrame> result =
294+
std::make_unique<SurfaceFrame>(surface, submit_callback);
295+
return result;
289296
}
290297

291298
bool GPUSurfaceGL::PresentSurface(SkCanvas* canvas) {
292299
if (delegate_ == nullptr || canvas == nullptr || context_ == nullptr) {
293300
return false;
294301
}
295302

303+
std::unique_ptr<RendererContextSwitchManager::RendererContextSwitch>
304+
context_switch = delegate_->GLContextMakeCurrent();
296305
if (offscreen_surface_ != nullptr) {
297306
TRACE_EVENT0("flutter", "CopyTextureOnscreen");
298307
SkPaint paint;
@@ -329,7 +338,6 @@ bool GPUSurfaceGL::PresentSurface(SkCanvas* canvas) {
329338

330339
onscreen_surface_ = std::move(new_onscreen_surface);
331340
}
332-
333341
return true;
334342
}
335343

@@ -360,7 +368,8 @@ flutter::ExternalViewEmbedder* GPUSurfaceGL::GetExternalViewEmbedder() {
360368
}
361369

362370
// |Surface|
363-
bool GPUSurfaceGL::MakeRenderContextCurrent() {
371+
std::unique_ptr<RendererContextSwitchManager::RendererContextSwitch>
372+
GPUSurfaceGL::MakeRenderContextCurrent() {
364373
return delegate_->GLContextMakeCurrent();
365374
}
366375

0 commit comments

Comments
 (0)