[Fix, Test] (EGL, buffers, wire): preserve context, surface, and buffer lifetimes

This commit is contained in:
BZLZHH committed 2026-11-29 03:49:44 +08:00
1 parent 434407f1bc
commit 0059ccdd79
59 files changed
+1822 -1013

No files matched your search

@@ -17,14 +17,10 @@ namespace MobileGL::MG_Backend::DirectGLES {
Uint contextGeneration = 0;
};
// Backend timer-query handle: a native GL query object name plus the
// ES context generation it was created under. Stale-generation
// handles read as available with a zero result, and deleting them
// only frees the wrapper (the dead ES context already reclaimed the
// query object).
// Query names belong to the native context that allocated them.
struct GLESQueryObject {
GLuint queryId = 0;
Uint contextGeneration = 0;
Uint64 nativeContextSerial = 0;
// Non-zero for the core (non-timer) query targets - occlusion and transform
// feedback primitives - and then holds the glBeginQuery target, which glEndQuery
// needs back. Their results are counts reachable only through the core
@@ -251,12 +247,12 @@ namespace MobileGL::MG_Backend::DirectGLES {
return nullptr;
}
g_GLESFuncs.glBeginQuery(GL_TIME_ELAPSED, queryId);
return new GLESQueryObject{queryId, g_syncContextGeneration};
return new GLESQueryObject{queryId, CurrentNativeContextSerial()};
}
void EndTimeElapsedQuery(BackendQueryHandle handle) {
const auto* query = static_cast<GLESQueryObject*>(handle);
if (query == nullptr || query->contextGeneration != g_syncContextGeneration ||
if (query == nullptr || query->nativeContextSerial != CurrentNativeContextSerial() ||
!IsBackendContextCurrentOnThisThread() || !g_GLESFuncs.glEndQuery) {
return;
}
@@ -275,7 +271,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
return nullptr;
}
g_GLESFuncs.glQueryCounterEXT(queryId, GL_TIMESTAMP);
return new GLESQueryObject{queryId, g_syncContextGeneration};
return new GLESQueryObject{queryId, CurrentNativeContextSerial()};
}
// The core (non-timer) query targets - occlusion and transform feedback primitives.
@@ -296,12 +292,12 @@ namespace MobileGL::MG_Backend::DirectGLES {
return nullptr;
}
g_GLESFuncs.glBeginQuery(target, queryId);
return new GLESQueryObject{queryId, g_syncContextGeneration, target};
return new GLESQueryObject{queryId, CurrentNativeContextSerial(), target};
}
static void EndCoreQuery(BackendQueryHandle handle) {
const auto* query = static_cast<GLESQueryObject*>(handle);
if (query == nullptr || query->coreTarget == 0 || query->contextGeneration != g_syncContextGeneration ||
if (query == nullptr || query->coreTarget == 0 || query->nativeContextSerial != CurrentNativeContextSerial() ||
!IsBackendContextCurrentOnThisThread() || !g_GLESFuncs.glEndQuery) {
return;
}
@@ -344,7 +340,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
// not own the ES context also reports available: GetQueryResult64
// then returns false and the frontend keeps the handle for a later
// read from the owning thread.
if (query == nullptr || query->contextGeneration != g_syncContextGeneration ||
if (query == nullptr || query->nativeContextSerial != CurrentNativeContextSerial() ||
!IsBackendContextCurrentOnThisThread() || !g_GLESFuncs.glGetQueryObjectuiv) {
return true;
}
@@ -362,7 +358,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
// require them): zero is the FINAL result in all three cases, so
// report it as produced and let the frontend cache it and release
// the handle.
if (query == nullptr || query->contextGeneration != g_syncContextGeneration ||
if (query == nullptr || query->nativeContextSerial != CurrentNativeContextSerial() ||
!g_GLESFuncs.glGetQueryObjectuiv || (query->coreTarget == 0 && !g_GLESFuncs.glGetQueryObjectui64vEXT)) {
return true;
}
@@ -418,7 +414,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
if (query == nullptr) {
return;
}
if (query->contextGeneration == g_syncContextGeneration && IsBackendContextCurrentOnThisThread() &&
if (query->nativeContextSerial == CurrentNativeContextSerial() && IsBackendContextCurrentOnThisThread() &&
g_GLESFuncs.glDeleteQueries) {
g_GLESFuncs.glDeleteQueries(1, &query->queryId);
}
@@ -531,6 +527,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
return false;
}
t_deviceLossLatched = true;
BufferImpl::RevokeResidentBuffersForCurrentSession();
(void)MG_Pipe::MGPipeSessionLatch(MG_Pipe::MGPipeFatalFamily::DeviceLost,
"MGPipe: Fatal{BackendDeviceLost, \"Espryt:%s\"} - this session's EGL "
"context was reset (glGetGraphicsResetStatus 0x%x); the session ends "
@@ -613,7 +610,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
// ONLY A WINDOW SURFACE TAKES RECTANGLES: the platform's swap-with-damage hands them to the
// surface's native window, and a pbuffer has none (Android's dereferences it regardless).
NativeSessionState& session = ActiveNativeSession(); // before the lock: it takes it too
const EGLSurface drawSurface = session.Draw;
const EGLSurface drawSurface = CurrentContextDrawSurface();
Bool windowSurface = false;
if (drawSurface != EGL_NO_SURFACE) {
const std::lock_guard<std::mutex> lock(g_nativeRegistryMutex);