Skip to content

Commit 3864e3f

Browse files
author
Emmanuel Garcia
authored
Revert "Add RAII wrapper for EGLSurface (flutter#18849)" (flutter#18971)
This reverts commit d8fe71f.
1 parent a960e72 commit 3864e3f

File tree

4 files changed

+63
-66
lines changed

4 files changed

+63
-66
lines changed

shell/platform/android/android_context_gl.cc

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,6 @@ static bool TeardownContext(EGLDisplay display, EGLContext context) {
105105
return true;
106106
}
107107

108-
AndroidEGLSurface::AndroidEGLSurface(EGLSurface surface, EGLDisplay display)
109-
: surface(surface), display_(display) {}
110-
111-
AndroidEGLSurface::~AndroidEGLSurface() {
112-
auto result = eglDestroySurface(display_, surface);
113-
FML_DCHECK(result == EGL_TRUE);
114-
}
115-
116108
AndroidContextGL::AndroidContextGL(
117109
AndroidRenderingAPI rendering_api,
118110
fml::RefPtr<AndroidEnvironmentGL> environment)
@@ -169,28 +161,25 @@ AndroidContextGL::~AndroidContextGL() {
169161
}
170162
}
171163

172-
std::unique_ptr<AndroidEGLSurface> AndroidContextGL::CreateOnscreenSurface(
164+
EGLSurface AndroidContextGL::CreateOnscreenSurface(
173165
fml::RefPtr<AndroidNativeWindow> window) const {
174166
EGLDisplay display = environment_->Display();
175167

176168
const EGLint attribs[] = {EGL_NONE};
177169

178-
EGLSurface surface = eglCreateWindowSurface(
170+
return eglCreateWindowSurface(
179171
display, config_, reinterpret_cast<EGLNativeWindowType>(window->handle()),
180172
attribs);
181-
return std::make_unique<AndroidEGLSurface>(surface, display);
182173
}
183174

184-
std::unique_ptr<AndroidEGLSurface> AndroidContextGL::CreateOffscreenSurface()
185-
const {
175+
EGLSurface AndroidContextGL::CreateOffscreenSurface() const {
186176
// We only ever create pbuffer surfaces for background resource loading
187177
// contexts. We never bind the pbuffer to anything.
188178
EGLDisplay display = environment_->Display();
189179

190180
const EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
191181

192-
EGLSurface surface = eglCreatePbufferSurface(display, config_, attribs);
193-
return std::make_unique<AndroidEGLSurface>(surface, display);
182+
return eglCreatePbufferSurface(display, config_, attribs);
194183
}
195184

196185
fml::RefPtr<AndroidEnvironmentGL> AndroidContextGL::Environment() const {
@@ -201,21 +190,19 @@ bool AndroidContextGL::IsValid() const {
201190
return valid_;
202191
}
203192

204-
bool AndroidContextGL::MakeCurrent(
205-
std::unique_ptr<AndroidEGLSurface> surface_wrapper) {
206-
if (eglMakeCurrent(environment_->Display(), surface_wrapper->surface,
207-
surface_wrapper->surface, context_) != EGL_TRUE) {
193+
bool AndroidContextGL::MakeCurrent(EGLSurface& surface) {
194+
if (eglMakeCurrent(environment_->Display(), surface, surface, context_) !=
195+
EGL_TRUE) {
208196
FML_LOG(ERROR) << "Could not make the context current";
209197
LogLastEGLError();
210198
return false;
211199
}
212200
return true;
213201
}
214202

215-
bool AndroidContextGL::ResourceMakeCurrent(
216-
std::unique_ptr<AndroidEGLSurface> surface_wrapper) {
217-
if (eglMakeCurrent(environment_->Display(), surface_wrapper->surface,
218-
surface_wrapper->surface, resource_context_) != EGL_TRUE) {
203+
bool AndroidContextGL::ResourceMakeCurrent(EGLSurface& surface) {
204+
if (eglMakeCurrent(environment_->Display(), surface, surface,
205+
resource_context_) != EGL_TRUE) {
219206
FML_LOG(ERROR) << "Could not make the context current";
220207
LogLastEGLError();
221208
return false;
@@ -236,26 +223,30 @@ bool AndroidContextGL::ClearCurrent() {
236223
return true;
237224
}
238225

239-
bool AndroidContextGL::SwapBuffers(
240-
std::unique_ptr<AndroidEGLSurface> surface_wrapper) {
226+
bool AndroidContextGL::SwapBuffers(EGLSurface& surface) {
241227
TRACE_EVENT0("flutter", "AndroidContextGL::SwapBuffers");
242-
return eglSwapBuffers(environment_->Display(), surface_wrapper->surface);
228+
return eglSwapBuffers(environment_->Display(), surface);
243229
}
244230

245-
SkISize AndroidContextGL::GetSize(
246-
std::unique_ptr<AndroidEGLSurface> surface_wrapper) {
231+
SkISize AndroidContextGL::GetSize(EGLSurface& surface) {
247232
EGLint width = 0;
248233
EGLint height = 0;
249234

250-
if (!eglQuerySurface(environment_->Display(), surface_wrapper->surface,
251-
EGL_WIDTH, &width) ||
252-
!eglQuerySurface(environment_->Display(), surface_wrapper->surface,
253-
EGL_HEIGHT, &height)) {
235+
if (!eglQuerySurface(environment_->Display(), surface, EGL_WIDTH, &width) ||
236+
!eglQuerySurface(environment_->Display(), surface, EGL_HEIGHT, &height)) {
254237
FML_LOG(ERROR) << "Unable to query EGL surface size";
255238
LogLastEGLError();
256239
return SkISize::Make(0, 0);
257240
}
258241
return SkISize::Make(width, height);
259242
}
260243

244+
bool AndroidContextGL::TeardownSurface(EGLSurface& surface) {
245+
if (surface != EGL_NO_SURFACE) {
246+
return eglDestroySurface(environment_->Display(), surface) == EGL_TRUE;
247+
}
248+
249+
return true;
250+
}
251+
261252
} // namespace flutter

shell/platform/android/android_context_gl.h

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,6 @@
1616

1717
namespace flutter {
1818

19-
//------------------------------------------------------------------------------
20-
/// Holds an `EGLSurface` reference.
21-
///
22-
///
23-
/// This can be used in conjuction to unique_ptr to provide better guarantees
24-
/// about the lifespam of the `EGLSurface` object.
25-
///
26-
struct AndroidEGLSurface {
27-
AndroidEGLSurface(EGLSurface surface, EGLDisplay display);
28-
~AndroidEGLSurface();
29-
30-
const EGLSurface surface;
31-
32-
private:
33-
const EGLDisplay display_;
34-
};
35-
3619
//------------------------------------------------------------------------------
3720
/// The Android context is used by `AndroidSurfaceGL` to create and manage
3821
/// EGL surfaces.
@@ -51,18 +34,24 @@ class AndroidContextGL : public AndroidContext {
5134
/// @brief Allocates an new EGL window surface that is used for on-screen
5235
/// pixels.
5336
///
37+
/// @attention Consumers must tear down the surface by calling
38+
/// `AndroidContextGL::TeardownSurface`.
39+
///
5440
/// @return The window surface.
5541
///
56-
std::unique_ptr<AndroidEGLSurface> CreateOnscreenSurface(
42+
EGLSurface CreateOnscreenSurface(
5743
fml::RefPtr<AndroidNativeWindow> window) const;
5844

5945
//----------------------------------------------------------------------------
6046
/// @brief Allocates an 1x1 pbuffer surface that is used for making the
6147
/// offscreen current for texture uploads.
6248
///
49+
/// @attention Consumers must tear down the surface by calling
50+
/// `AndroidContextGL::TeardownSurface`.
51+
///
6352
/// @return The pbuffer surface.
6453
///
65-
std::unique_ptr<AndroidEGLSurface> CreateOffscreenSurface() const;
54+
EGLSurface CreateOffscreenSurface() const;
6655

6756
//----------------------------------------------------------------------------
6857
/// @return The Android environment that contains a reference to the
@@ -88,28 +77,35 @@ class AndroidContextGL : public AndroidContext {
8877
///
8978
/// @return Whether the surface was made current.
9079
///
91-
bool MakeCurrent(std::unique_ptr<AndroidEGLSurface> surface_wrapper);
80+
bool MakeCurrent(EGLSurface& surface);
9281

9382
//----------------------------------------------------------------------------
9483
/// @brief Binds the resource EGLContext context to the current rendering
9584
/// thread and to the draw and read surface.
9685
///
9786
/// @return Whether the surface was made current.
9887
///
99-
bool ResourceMakeCurrent(std::unique_ptr<AndroidEGLSurface> surface_wrapper);
88+
bool ResourceMakeCurrent(EGLSurface& surface);
10089

10190
//----------------------------------------------------------------------------
10291
/// @brief This only applies to on-screen surfaces such as those created
10392
/// by `AndroidContextGL::CreateOnscreenSurface`.
10493
///
10594
/// @return Whether the EGL surface color buffer was swapped.
10695
///
107-
bool SwapBuffers(std::unique_ptr<AndroidEGLSurface> surface_wrapper);
96+
bool SwapBuffers(EGLSurface& surface);
10897

10998
//----------------------------------------------------------------------------
11099
/// @return The size of an `EGLSurface`.
111100
///
112-
SkISize GetSize(std::unique_ptr<AndroidEGLSurface> surface_wrapper);
101+
SkISize GetSize(EGLSurface& surface);
102+
103+
//----------------------------------------------------------------------------
104+
/// @brief Destroys an `EGLSurface`.
105+
///
106+
/// @return Whether the surface was destroyed.
107+
///
108+
bool TeardownSurface(EGLSurface& surface);
113109

114110
private:
115111
fml::RefPtr<AndroidEnvironmentGL> environment_;

shell/platform/android/android_surface_gl.cc

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,22 @@ AndroidSurfaceGL::AndroidSurfaceGL(
2020
std::static_pointer_cast<AndroidContextGL>(android_context);
2121
// Acquire the offscreen surface.
2222
offscreen_surface_ = android_context_->CreateOffscreenSurface();
23-
if (offscreen_surface_->surface == EGL_NO_SURFACE) {
23+
if (offscreen_surface_ == EGL_NO_SURFACE) {
2424
offscreen_surface_ = nullptr;
2525
}
2626
external_view_embedder_ = std::make_unique<AndroidExternalViewEmbedder>();
2727
}
2828

29-
AndroidSurfaceGL::~AndroidSurfaceGL() = default;
29+
AndroidSurfaceGL::~AndroidSurfaceGL() {
30+
if (offscreen_surface_) {
31+
android_context_->TeardownSurface(offscreen_surface_);
32+
offscreen_surface_ = nullptr;
33+
}
34+
if (onscreen_surface_) {
35+
android_context_->TeardownSurface(onscreen_surface_);
36+
onscreen_surface_ = nullptr;
37+
}
38+
}
3039

3140
void AndroidSurfaceGL::TeardownOnScreenContext() {
3241
android_context_->ClearCurrent();
@@ -49,24 +58,25 @@ bool AndroidSurfaceGL::OnScreenSurfaceResize(const SkISize& size) {
4958
FML_DCHECK(onscreen_surface_);
5059
FML_DCHECK(native_window_);
5160

52-
if (size == android_context_->GetSize(std::move(onscreen_surface_))) {
61+
if (size == android_context_->GetSize(onscreen_surface_)) {
5362
return true;
5463
}
5564

5665
android_context_->ClearCurrent();
66+
android_context_->TeardownSurface(onscreen_surface_);
5767

5868
onscreen_surface_ = android_context_->CreateOnscreenSurface(native_window_);
59-
if (onscreen_surface_->surface == EGL_NO_SURFACE) {
69+
if (!onscreen_surface_ || onscreen_surface_ == EGL_NO_SURFACE) {
6070
FML_LOG(ERROR) << "Unable to create EGL window surface on resize.";
6171
return false;
6272
}
63-
android_context_->MakeCurrent(std::move(onscreen_surface_));
73+
android_context_->MakeCurrent(onscreen_surface_);
6474
return true;
6575
}
6676

6777
bool AndroidSurfaceGL::ResourceContextMakeCurrent() {
6878
FML_DCHECK(IsValid());
69-
return android_context_->ResourceMakeCurrent(std::move(offscreen_surface_));
79+
return android_context_->ResourceMakeCurrent(offscreen_surface_);
7080
}
7181

7282
bool AndroidSurfaceGL::ResourceContextClearCurrent() {
@@ -81,7 +91,7 @@ bool AndroidSurfaceGL::SetNativeWindow(
8191
native_window_ = window;
8292
// Create the onscreen surface.
8393
onscreen_surface_ = android_context_->CreateOnscreenSurface(window);
84-
if (onscreen_surface_->surface == EGL_NO_SURFACE) {
94+
if (onscreen_surface_ == EGL_NO_SURFACE) {
8595
return false;
8696
}
8797
return true;
@@ -91,7 +101,7 @@ std::unique_ptr<GLContextResult> AndroidSurfaceGL::GLContextMakeCurrent() {
91101
FML_DCHECK(IsValid());
92102
FML_DCHECK(onscreen_surface_);
93103
auto default_context_result = std::make_unique<GLContextDefaultResult>(
94-
android_context_->MakeCurrent(std::move(onscreen_surface_)));
104+
android_context_->MakeCurrent(onscreen_surface_));
95105
return std::move(default_context_result);
96106
}
97107

@@ -103,7 +113,7 @@ bool AndroidSurfaceGL::GLContextClearCurrent() {
103113
bool AndroidSurfaceGL::GLContextPresent() {
104114
FML_DCHECK(IsValid());
105115
FML_DCHECK(onscreen_surface_);
106-
return android_context_->SwapBuffers(std::move(onscreen_surface_));
116+
return android_context_->SwapBuffers(onscreen_surface_);
107117
}
108118

109119
intptr_t AndroidSurfaceGL::GLContextFBO() const {

shell/platform/android/android_surface_gl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ class AndroidSurfaceGL final : public GPUSurfaceGLDelegate,
6464
fml::RefPtr<AndroidNativeWindow> native_window_;
6565
std::unique_ptr<AndroidExternalViewEmbedder> external_view_embedder_;
6666
std::shared_ptr<AndroidContextGL> android_context_;
67-
std::unique_ptr<AndroidEGLSurface> onscreen_surface_;
68-
std::unique_ptr<AndroidEGLSurface> offscreen_surface_;
67+
EGLSurface onscreen_surface_;
68+
EGLSurface offscreen_surface_;
6969

7070
FML_DISALLOW_COPY_AND_ASSIGN(AndroidSurfaceGL);
7171
};

0 commit comments

Comments
 (0)