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

Commit c825073

Browse files
authored
Only call make gl context current if not already current (#32967)
1 parent dc2a7da commit c825073

File tree

4 files changed

+74
-7
lines changed

4 files changed

+74
-7
lines changed

shell/platform/android/android_context_gl.cc

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,40 @@ bool AndroidEGLSurface::IsValid() const {
252252
return surface_ != EGL_NO_SURFACE;
253253
}
254254

255-
bool AndroidEGLSurface::MakeCurrent() const {
255+
bool AndroidEGLSurface::IsContextCurrent() const {
256+
EGLContext current_egl_context = eglGetCurrentContext();
257+
if (context_ != current_egl_context) {
258+
return false;
259+
}
260+
261+
EGLDisplay current_egl_display = eglGetCurrentDisplay();
262+
if (display_ != current_egl_display) {
263+
return false;
264+
}
265+
266+
EGLSurface draw_surface = eglGetCurrentSurface(EGL_DRAW);
267+
if (draw_surface != surface_) {
268+
return false;
269+
}
270+
271+
EGLSurface read_surface = eglGetCurrentSurface(EGL_READ);
272+
if (read_surface != surface_) {
273+
return false;
274+
}
275+
276+
return true;
277+
}
278+
279+
AndroidEGLSurfaceMakeCurrentStatus AndroidEGLSurface::MakeCurrent() const {
280+
if (IsContextCurrent()) {
281+
return AndroidEGLSurfaceMakeCurrentStatus::kSuccessAlreadyCurrent;
282+
}
256283
if (eglMakeCurrent(display_, surface_, surface_, context_) != EGL_TRUE) {
257284
FML_LOG(ERROR) << "Could not make the context current";
258285
LogLastEGLError();
259-
return false;
286+
return AndroidEGLSurfaceMakeCurrentStatus::kFailure;
260287
}
261-
return true;
288+
return AndroidEGLSurfaceMakeCurrentStatus::kSuccessMadeCurrent;
262289
}
263290

264291
void AndroidEGLSurface::SetDamageRegion(
@@ -350,7 +377,8 @@ AndroidContextGL::~AndroidContextGL() {
350377
if (main_context) {
351378
std::unique_ptr<AndroidEGLSurface> pbuffer_surface =
352379
CreatePbufferSurface();
353-
if (pbuffer_surface->MakeCurrent()) {
380+
auto status = pbuffer_surface->MakeCurrent();
381+
if (status != AndroidEGLSurfaceMakeCurrentStatus::kFailure) {
354382
main_context->releaseResourcesAndAbandonContext();
355383
main_context.reset();
356384
ClearCurrent();

shell/platform/android/android_context_gl.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ namespace flutter {
2525
///
2626
class AndroidEGLSurfaceDamage;
2727

28+
/// Result of calling MakeCurrent on AndroidEGLSurface.
29+
enum class AndroidEGLSurfaceMakeCurrentStatus {
30+
/// Success, the egl context for the surface was already current.
31+
kSuccessAlreadyCurrent,
32+
/// Success, the egl context for the surface made current.
33+
kSuccessMadeCurrent,
34+
/// Failed to make the egl context for the surface current.
35+
kFailure,
36+
};
37+
2838
class AndroidEGLSurface {
2939
public:
3040
AndroidEGLSurface(EGLSurface surface, EGLDisplay display, EGLContext context);
@@ -43,7 +53,7 @@ class AndroidEGLSurface {
4353
///
4454
/// @return Whether the surface was made current.
4555
///
46-
bool MakeCurrent() const;
56+
AndroidEGLSurfaceMakeCurrentStatus MakeCurrent() const;
4757

4858
//----------------------------------------------------------------------------
4959
///
@@ -81,6 +91,9 @@ class AndroidEGLSurface {
8191
SkISize GetSize() const;
8292

8393
private:
94+
/// Returns true if the EGLContext held is current for the display and surface
95+
bool IsContextCurrent() const;
96+
8497
const EGLSurface surface_;
8598
const EGLDisplay display_;
8699
const EGLContext context_;

shell/platform/android/android_context_gl_unittests.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,30 @@ TEST(AndroidContextGl, MSAAx4) {
184184
&sample_count);
185185
EXPECT_EQ(sample_count, 4);
186186
}
187+
188+
TEST(AndroidContextGl, EnsureMakeCurrentChecksCurrentContextStatus) {
189+
GrMockOptions main_context_options;
190+
sk_sp<GrDirectContext> main_context =
191+
GrDirectContext::MakeMock(&main_context_options);
192+
auto environment = fml::MakeRefCounted<AndroidEnvironmentGL>();
193+
std::string thread_label =
194+
::testing::UnitTest::GetInstance()->current_test_info()->name();
195+
196+
ThreadHost thread_host(ThreadHost::ThreadHostConfig(
197+
thread_label,
198+
ThreadHost::Type::UI | ThreadHost::Type::RASTER | ThreadHost::Type::IO));
199+
TaskRunners task_runners = MakeTaskRunners(thread_label, thread_host);
200+
auto context = std::make_unique<AndroidContextGL>(
201+
AndroidRenderingAPI::kOpenGLES, environment, task_runners, 0);
202+
203+
auto pbuffer_surface = context->CreatePbufferSurface();
204+
auto status = pbuffer_surface->MakeCurrent();
205+
EXPECT_EQ(AndroidEGLSurfaceMakeCurrentStatus::kSuccessMadeCurrent, status);
206+
207+
// context already current, so status must reflect that.
208+
status = pbuffer_surface->MakeCurrent();
209+
EXPECT_EQ(AndroidEGLSurfaceMakeCurrentStatus::kSuccessAlreadyCurrent, status);
210+
}
187211
} // namespace android
188212
} // namespace testing
189213
} // namespace flutter

shell/platform/android/android_surface_gl.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ bool AndroidSurfaceGL::OnScreenSurfaceResize(const SkISize& size) {
8787

8888
bool AndroidSurfaceGL::ResourceContextMakeCurrent() {
8989
FML_DCHECK(IsValid());
90-
return offscreen_surface_->MakeCurrent();
90+
auto status = offscreen_surface_->MakeCurrent();
91+
return status != AndroidEGLSurfaceMakeCurrentStatus::kFailure;
9192
}
9293

9394
bool AndroidSurfaceGL::ResourceContextClearCurrent() {
@@ -116,8 +117,9 @@ bool AndroidSurfaceGL::SetNativeWindow(
116117
std::unique_ptr<GLContextResult> AndroidSurfaceGL::GLContextMakeCurrent() {
117118
FML_DCHECK(IsValid());
118119
FML_DCHECK(onscreen_surface_);
120+
auto status = onscreen_surface_->MakeCurrent();
119121
auto default_context_result = std::make_unique<GLContextDefaultResult>(
120-
onscreen_surface_->MakeCurrent());
122+
status != AndroidEGLSurfaceMakeCurrentStatus::kFailure);
121123
return std::move(default_context_result);
122124
}
123125

0 commit comments

Comments
 (0)