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

Commit 71d264d

Browse files
authored
Revert "implemented GetMainContext() for opengl (#23634)" (#23859)
This reverts commit 296902b.
1 parent ffc77f0 commit 71d264d

File tree

9 files changed

+48
-58
lines changed

9 files changed

+48
-58
lines changed

shell/gpu/gpu_surface_gl.cc

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,16 @@ static const int kGrCacheMaxCount = 8192;
3434
// system channel.
3535
static const size_t kGrCacheMaxByteSize = 24 * (1 << 20);
3636

37-
sk_sp<GrDirectContext> GPUSurfaceGL::MakeGLContext(
38-
GPUSurfaceGLDelegate* delegate) {
39-
auto context_switch = delegate->GLContextMakeCurrent();
37+
GPUSurfaceGL::GPUSurfaceGL(GPUSurfaceGLDelegate* delegate,
38+
bool render_to_surface)
39+
: delegate_(delegate),
40+
render_to_surface_(render_to_surface),
41+
weak_factory_(this) {
42+
auto context_switch = delegate_->GLContextMakeCurrent();
4043
if (!context_switch->GetResult()) {
4144
FML_LOG(ERROR)
4245
<< "Could not make the context current to setup the gr context.";
43-
return nullptr;
46+
return;
4447
}
4548

4649
GrContextOptions options;
@@ -61,39 +64,39 @@ sk_sp<GrDirectContext> GPUSurfaceGL::MakeGLContext(
6164
// TODO(goderbauer): remove option when skbug.com/7523 is fixed.
6265
// A similar work-around is also used in shell/common/io_manager.cc.
6366
options.fDisableGpuYUVConversion = true;
64-
auto context = GrDirectContext::MakeGL(delegate->GetGLInterface(), options);
6567

66-
if (!context) {
68+
auto context = GrDirectContext::MakeGL(delegate_->GetGLInterface(), options);
69+
70+
if (context == nullptr) {
6771
FML_LOG(ERROR) << "Failed to setup Skia Gr context.";
68-
return nullptr;
72+
return;
6973
}
7074

71-
context->setResourceCacheLimits(kGrCacheMaxCount, kGrCacheMaxByteSize);
75+
context_ = std::move(context);
76+
77+
context_->setResourceCacheLimits(kGrCacheMaxCount, kGrCacheMaxByteSize);
78+
79+
context_owner_ = true;
80+
81+
valid_ = true;
7282

7383
std::vector<PersistentCache::SkSLCache> caches =
7484
PersistentCache::GetCacheForProcess()->LoadSkSLs();
7585
int compiled_count = 0;
7686
for (const auto& cache : caches) {
77-
compiled_count += context->precompileShader(*cache.first, *cache.second);
87+
compiled_count += context_->precompileShader(*cache.first, *cache.second);
7888
}
7989
FML_LOG(INFO) << "Found " << caches.size() << " SkSL shaders; precompiled "
8090
<< compiled_count;
8191

82-
return context;
83-
}
84-
85-
GPUSurfaceGL::GPUSurfaceGL(GPUSurfaceGLDelegate* delegate,
86-
bool render_to_surface)
87-
: GPUSurfaceGL(MakeGLContext(delegate), delegate, render_to_surface) {
88-
context_owner_ = true;
92+
delegate_->GLContextClearCurrent();
8993
}
9094

9195
GPUSurfaceGL::GPUSurfaceGL(sk_sp<GrDirectContext> gr_context,
9296
GPUSurfaceGLDelegate* delegate,
9397
bool render_to_surface)
9498
: delegate_(delegate),
9599
context_(gr_context),
96-
context_owner_(false),
97100
render_to_surface_(render_to_surface),
98101
weak_factory_(this) {
99102
auto context_switch = delegate_->GLContextMakeCurrent();
@@ -106,6 +109,7 @@ GPUSurfaceGL::GPUSurfaceGL(sk_sp<GrDirectContext> gr_context,
106109
delegate_->GLContextClearCurrent();
107110

108111
valid_ = true;
112+
context_owner_ = false;
109113
}
110114

111115
GPUSurfaceGL::~GPUSurfaceGL() {

shell/gpu/gpu_surface_gl.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ namespace flutter {
2020

2121
class GPUSurfaceGL : public Surface {
2222
public:
23-
static sk_sp<GrDirectContext> MakeGLContext(GPUSurfaceGLDelegate* delegate);
24-
2523
GPUSurfaceGL(GPUSurfaceGLDelegate* delegate, bool render_to_surface);
2624

2725
// Creates a new GL surface reusing an existing GrDirectContext.
@@ -55,13 +53,13 @@ class GPUSurfaceGL : public Surface {
5553
sk_sp<GrDirectContext> context_;
5654
sk_sp<SkSurface> onscreen_surface_;
5755
/// FBO backing the current `onscreen_surface_`.
58-
uint32_t fbo_id_ = 0;
59-
bool context_owner_ = false;
56+
uint32_t fbo_id_;
57+
bool context_owner_;
6058
// TODO(38466): Refactor GPU surface APIs take into account the fact that an
6159
// external view embedder may want to render to the root surface. This is a
6260
// hack to make avoid allocating resources for the root surface when an
6361
// external view embedder is present.
64-
const bool render_to_surface_ = true;
62+
const bool render_to_surface_;
6563
bool valid_ = false;
6664
fml::TaskRunnerAffineWeakPtrFactory<GPUSurfaceGL> weak_factory_;
6765

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ - (void)testSpawnsShareGpuContext {
3333
std::shared_ptr<flutter::IOSContext> engine_context = [engine iosPlatformView]->GetIosContext();
3434
std::shared_ptr<flutter::IOSContext> spawn_context = [spawn iosPlatformView]->GetIosContext();
3535
XCTAssertEqual(engine_context, spawn_context);
36-
// If this assert fails it means we may be using the software. For software rendering, this is
37-
// expected to be nullptr.
36+
// If this assert fails it means we may be using the software or OpenGL
37+
// renderer when we were expecting Metal. For software rendering, this is
38+
// expected to be nullptr. For OpenGL, implementing this is an outstanding
39+
// change see https://github.com/flutter/flutter/issues/73744.
3840
XCTAssertTrue(engine_context->GetMainContext() != nullptr);
3941
XCTAssertEqual(engine_context->GetMainContext(), spawn_context->GetMainContext());
4042
[engine release];

shell/platform/darwin/ios/ios_context_gl.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,16 @@ class IOSContextGL final : public IOSContext {
2525

2626
std::unique_ptr<IOSRenderTargetGL> CreateRenderTarget(fml::scoped_nsobject<CAEAGLLayer> layer);
2727

28-
void SetMainContext(const sk_sp<GrDirectContext>& main_context);
28+
private:
29+
fml::scoped_nsobject<EAGLContext> context_;
30+
fml::scoped_nsobject<EAGLContext> resource_context_;
2931

3032
// |IOSContext|
3133
sk_sp<GrDirectContext> CreateResourceContext() override;
3234

35+
// |IOSContext|
36+
sk_sp<GrDirectContext> GetMainContext() const override;
37+
3338
// |IOSContext|
3439
std::unique_ptr<GLContextResult> MakeCurrent() override;
3540

@@ -38,14 +43,6 @@ class IOSContextGL final : public IOSContext {
3843
int64_t texture_id,
3944
fml::scoped_nsobject<NSObject<FlutterTexture>> texture) override;
4045

41-
// |IOSContext|
42-
sk_sp<GrDirectContext> GetMainContext() const override;
43-
44-
private:
45-
fml::scoped_nsobject<EAGLContext> context_;
46-
fml::scoped_nsobject<EAGLContext> resource_context_;
47-
sk_sp<GrDirectContext> main_context_;
48-
4946
FML_DISALLOW_COPY_AND_ASSIGN(IOSContextGL);
5047
};
5148

shell/platform/darwin/ios/ios_context_gl.mm

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#import <OpenGLES/EAGL.h>
88

99
#include "flutter/shell/common/shell_io_manager.h"
10-
#include "flutter/shell/gpu/gpu_surface_gl.h"
1110
#include "flutter/shell/gpu/gpu_surface_gl_delegate.h"
1211
#import "flutter/shell/platform/darwin/ios/ios_external_texture_gl.h"
1312

@@ -25,11 +24,7 @@
2524
}
2625
}
2726

28-
IOSContextGL::~IOSContextGL() {
29-
if (main_context_) {
30-
main_context_->releaseResourcesAndAbandonContext();
31-
}
32-
}
27+
IOSContextGL::~IOSContextGL() = default;
3328

3429
std::unique_ptr<IOSRenderTargetGL> IOSContextGL::CreateRenderTarget(
3530
fml::scoped_nsobject<CAEAGLLayer> layer) {
@@ -50,11 +45,12 @@
5045

5146
// |IOSContext|
5247
sk_sp<GrDirectContext> IOSContextGL::GetMainContext() const {
53-
return main_context_;
54-
}
55-
56-
void IOSContextGL::SetMainContext(const sk_sp<GrDirectContext>& main_context) {
57-
main_context_ = main_context;
48+
/// TODO(73744): Currently the GPUSurfaceGL creates the main context for
49+
/// OpenGL. With Metal the IOSContextMetal creates the main context and is
50+
/// shared across surfaces. We should refactor the OpenGL Context/Surfaces to
51+
/// behave like the Metal equivalents. Until then engines in the same group
52+
/// will have a heavier memory cost if they are using OpenGL.
53+
return nullptr;
5854
}
5955

6056
// |IOSContext|

shell/platform/darwin/ios/ios_surface_gl.mm

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,8 @@
3838
std::unique_ptr<Surface> IOSSurfaceGL::CreateGPUSurface(GrDirectContext* gr_context) {
3939
if (gr_context) {
4040
return std::make_unique<GPUSurfaceGL>(sk_ref_sp(gr_context), this, true);
41-
} else {
42-
IOSContextGL* gl_context = CastToGLContext(GetContext());
43-
sk_sp<GrDirectContext> context = gl_context->GetMainContext();
44-
if (!context) {
45-
context = GPUSurfaceGL::MakeGLContext(this);
46-
gl_context->SetMainContext(context);
47-
}
48-
49-
return std::make_unique<GPUSurfaceGL>(context, this, true);
5041
}
42+
return std::make_unique<GPUSurfaceGL>(this, true);
5143
}
5244

5345
// |GPUSurfaceGLDelegate|

shell/platform/darwin/ios/ios_surface_metal.mm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040
}
4141

4242
// |IOSSurface|
43-
std::unique_ptr<Surface> IOSSurfaceMetal::CreateGPUSurface(GrDirectContext* context) {
44-
FML_DCHECK(context);
45-
return std::make_unique<GPUSurfaceMetal>(this, // layer
46-
sk_ref_sp(context) // context
43+
std::unique_ptr<Surface> IOSSurfaceMetal::CreateGPUSurface(GrDirectContext* /* unused */) {
44+
auto metal_context = CastToMetalContext(GetContext());
45+
return std::make_unique<GPUSurfaceMetal>(this, // layer
46+
metal_context->GetMainContext() // context
4747
);
4848
}
4949

shell/platform/darwin/ios/platform_view_ios.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@
148148
"has no ViewController.";
149149
return nullptr;
150150
}
151-
return ios_surface_->CreateGPUSurface(ios_context_->GetMainContext().get());
151+
return ios_surface_->CreateGPUSurface();
152152
}
153153

154154
// |PlatformView|

shell/platform/embedder/embedder_surface_gl.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ std::unique_ptr<Surface> EmbedderSurfaceGL::CreateGPUSurface() {
8080
const bool render_to_surface = !external_view_embedder_;
8181
return std::make_unique<GPUSurfaceGL>(this, // GPU surface GL delegate
8282
render_to_surface // render to surface
83+
8384
);
8485
}
8586

0 commit comments

Comments
 (0)