Skip to content

Commit 7c2da3b

Browse files
authored
1 parent 004d8ad commit 7c2da3b

9 files changed

+59
-49
lines changed

shell/gpu/gpu_surface_gl.cc

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

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();
37+
sk_sp<GrDirectContext> GPUSurfaceGL::MakeGLContext(
38+
GPUSurfaceGLDelegate* delegate) {
39+
auto context_switch = delegate->GLContextMakeCurrent();
4340
if (!context_switch->GetResult()) {
4441
FML_LOG(ERROR)
4542
<< "Could not make the context current to setup the gr context.";
46-
return;
43+
return nullptr;
4744
}
4845

4946
GrContextOptions options;
@@ -64,39 +61,39 @@ GPUSurfaceGL::GPUSurfaceGL(GPUSurfaceGLDelegate* delegate,
6461
// TODO(goderbauer): remove option when skbug.com/7523 is fixed.
6562
// A similar work-around is also used in shell/common/io_manager.cc.
6663
options.fDisableGpuYUVConversion = true;
64+
auto context = GrDirectContext::MakeGL(delegate->GetGLInterface(), options);
6765

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

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

8373
std::vector<PersistentCache::SkSLCache> caches =
8474
PersistentCache::GetCacheForProcess()->LoadSkSLs();
8575
int compiled_count = 0;
8676
for (const auto& cache : caches) {
87-
compiled_count += context_->precompileShader(*cache.first, *cache.second);
77+
compiled_count += context->precompileShader(*cache.first, *cache.second);
8878
}
8979
FML_LOG(INFO) << "Found " << caches.size() << " SkSL shaders; precompiled "
9080
<< compiled_count;
9181

92-
delegate_->GLContextClearCurrent();
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;
9389
}
9490

9591
GPUSurfaceGL::GPUSurfaceGL(sk_sp<GrDirectContext> gr_context,
9692
GPUSurfaceGLDelegate* delegate,
9793
bool render_to_surface)
9894
: delegate_(delegate),
9995
context_(gr_context),
96+
context_owner_(false),
10097
render_to_surface_(render_to_surface),
10198
weak_factory_(this) {
10299
auto context_switch = delegate_->GLContextMakeCurrent();
@@ -108,8 +105,7 @@ GPUSurfaceGL::GPUSurfaceGL(sk_sp<GrDirectContext> gr_context,
108105

109106
delegate_->GLContextClearCurrent();
110107

111-
valid_ = true;
112-
context_owner_ = false;
108+
valid_ = gr_context != nullptr;
113109
}
114110

115111
GPUSurfaceGL::~GPUSurfaceGL() {

shell/gpu/gpu_surface_gl.h

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

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

2527
// Creates a new GL surface reusing an existing GrDirectContext.
@@ -53,13 +55,13 @@ class GPUSurfaceGL : public Surface {
5355
sk_sp<GrDirectContext> context_;
5456
sk_sp<SkSurface> onscreen_surface_;
5557
/// FBO backing the current `onscreen_surface_`.
56-
uint32_t fbo_id_;
57-
bool context_owner_;
58+
uint32_t fbo_id_ = 0;
59+
bool context_owner_ = false;
5860
// TODO(38466): Refactor GPU surface APIs take into account the fact that an
5961
// external view embedder may want to render to the root surface. This is a
6062
// hack to make avoid allocating resources for the root surface when an
6163
// external view embedder is present.
62-
const bool render_to_surface_;
64+
const bool render_to_surface_ = true;
6365
bool valid_ = false;
6466
fml::TaskRunnerAffineWeakPtrFactory<GPUSurfaceGL> weak_factory_;
6567

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@ - (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 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.
36+
// If this assert fails it means we may be using the software. For software rendering, this is
37+
// expected to be nullptr.
4038
XCTAssertTrue(engine_context->GetMainContext() != nullptr);
4139
XCTAssertEqual(engine_context->GetMainContext(), spawn_context->GetMainContext());
4240
[engine release];

shell/platform/darwin/ios/ios_context_gl.h

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

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

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

3230
// |IOSContext|
3331
sk_sp<GrDirectContext> CreateResourceContext() override;
3432

35-
// |IOSContext|
36-
sk_sp<GrDirectContext> GetMainContext() const override;
37-
3833
// |IOSContext|
3934
std::unique_ptr<GLContextResult> MakeCurrent() override;
4035

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

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+
4649
FML_DISALLOW_COPY_AND_ASSIGN(IOSContextGL);
4750
};
4851

shell/platform/darwin/ios/ios_context_gl.mm

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

99
#include "flutter/shell/common/shell_io_manager.h"
10+
#include "flutter/shell/gpu/gpu_surface_gl.h"
1011
#include "flutter/shell/gpu/gpu_surface_gl_delegate.h"
1112
#import "flutter/shell/platform/darwin/ios/ios_external_texture_gl.h"
1213

@@ -24,7 +25,11 @@
2425
}
2526
}
2627

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

2934
std::unique_ptr<IOSRenderTargetGL> IOSContextGL::CreateRenderTarget(
3035
fml::scoped_nsobject<CAEAGLLayer> layer) {
@@ -45,12 +50,11 @@
4550

4651
// |IOSContext|
4752
sk_sp<GrDirectContext> IOSContextGL::GetMainContext() const {
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;
53+
return main_context_;
54+
}
55+
56+
void IOSContextGL::SetMainContext(const sk_sp<GrDirectContext>& main_context) {
57+
main_context_ = main_context;
5458
}
5559

5660
// |IOSContext|

shell/platform/darwin/ios/ios_surface_gl.mm

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,16 @@
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);
4150
}
42-
return std::make_unique<GPUSurfaceGL>(this, true);
4351
}
4452

4553
// |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* /* unused */) {
44-
auto metal_context = CastToMetalContext(GetContext());
45-
return std::make_unique<GPUSurfaceMetal>(this, // layer
46-
metal_context->GetMainContext() // context
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
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();
151+
return ios_surface_->CreateGPUSurface(ios_context_->GetMainContext().get());
152152
}
153153

154154
// |PlatformView|

shell/platform/embedder/embedder_surface_gl.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ 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-
8483
);
8584
}
8685

0 commit comments

Comments
 (0)