From 60f06d1b8d29a2916a9b426cfcec417f413db4fe Mon Sep 17 00:00:00 2001 From: BZLZHH Date: Fri, 20 Feb 2026 15:26:46 +0800 Subject: [PATCH] [Improvement] (MG_Backend): Separate Initialize and InitCapabilities. --- MobileGL/MG_Backend/BackendObject.h | 2 +- .../DirectGLES/BackendObject_DirectGLES.cpp | 9 +- .../DirectGLES/BackendObject_DirectGLES.h | 2 + MobileGL/MG_Impl/EGLImpl/EGLImpl.cpp | 7 ++ .../MG_Util/BackendLoaders/OpenGL/Loader.cpp | 106 +----------------- .../MG_Util/BackendLoaders/OpenGL/Loader.h | 3 +- 6 files changed, 20 insertions(+), 109 deletions(-) diff --git a/MobileGL/MG_Backend/BackendObject.h b/MobileGL/MG_Backend/BackendObject.h index 6833afe0..727bb67f 100644 --- a/MobileGL/MG_Backend/BackendObject.h +++ b/MobileGL/MG_Backend/BackendObject.h @@ -10,7 +10,6 @@ #include namespace MobileGL { - enum class BackendType { DirectGLES, BackendTypeCount, @@ -91,6 +90,7 @@ namespace MobileGL { virtual ~BackendObject() = default; virtual void Initialize() = 0; + virtual void InitCapabilities() = 0; virtual void InitWindowSurface() = 0; void SetWindowHandle(const WindowHandle& handle); diff --git a/MobileGL/MG_Backend/DirectGLES/BackendObject_DirectGLES.cpp b/MobileGL/MG_Backend/DirectGLES/BackendObject_DirectGLES.cpp index 3f45da86..81a63e40 100644 --- a/MobileGL/MG_Backend/DirectGLES/BackendObject_DirectGLES.cpp +++ b/MobileGL/MG_Backend/DirectGLES/BackendObject_DirectGLES.cpp @@ -38,8 +38,15 @@ namespace MobileGL::MG_Backend::DirectGLES { DirectGLES::SetEGLFuncsTable(m_EGLFunctions); DirectGLES::SetGLESFuncsTable(m_GLESFunctions); + } - if (!MG_Util::BackendLoader::FillInGLESCapabilities(m_GLESCapabilities, m_GLESFunctions, m_EGLFunctions)) { + void BackendObject_DirectGLES::InitCapabilities() { + if (!m_initialized) { + MGLOG_E("DirectGLES backend not initialized"); + return; + } + + if (!MG_Util::BackendLoader::FillInGLESCapabilities(m_GLESCapabilities, m_GLESFunctions)) { MGLOG_E("Failed to fill in GLES capabilities for DirectGLES backend"); return; } diff --git a/MobileGL/MG_Backend/DirectGLES/BackendObject_DirectGLES.h b/MobileGL/MG_Backend/DirectGLES/BackendObject_DirectGLES.h index e539a271..d85b9623 100644 --- a/MobileGL/MG_Backend/DirectGLES/BackendObject_DirectGLES.h +++ b/MobileGL/MG_Backend/DirectGLES/BackendObject_DirectGLES.h @@ -15,7 +15,9 @@ namespace MobileGL::MG_Backend::DirectGLES { class BackendObject_DirectGLES : public BackendObject { public: ~BackendObject_DirectGLES() override; + void Initialize() override; + void InitCapabilities() override; void InitWindowSurface() override; const RendererInfo& GetRendererInfo() const override; diff --git a/MobileGL/MG_Impl/EGLImpl/EGLImpl.cpp b/MobileGL/MG_Impl/EGLImpl/EGLImpl.cpp index 973205e7..f5122970 100644 --- a/MobileGL/MG_Impl/EGLImpl/EGLImpl.cpp +++ b/MobileGL/MG_Impl/EGLImpl/EGLImpl.cpp @@ -59,6 +59,13 @@ namespace MobileGL::MG_Impl::EGLImpl { } EGLBoolean MakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx) { + MGLOG_D("EGLImpl::CreateWindowSurface called with window=%p", window); + const auto& activeBackendObject = MG_Backend::pActiveBackendObject; + if (!activeBackendObject) { + MGLOG_E("activeBackendObject not initialized!"); + return EGL_FALSE; + } + activeBackendObject->InitCapabilities(); return EGL_TRUE; } diff --git a/MobileGL/MG_Util/BackendLoaders/OpenGL/Loader.cpp b/MobileGL/MG_Util/BackendLoaders/OpenGL/Loader.cpp index 7c8b7863..dad8a1cd 100644 --- a/MobileGL/MG_Util/BackendLoaders/OpenGL/Loader.cpp +++ b/MobileGL/MG_Util/BackendLoaders/OpenGL/Loader.cpp @@ -496,114 +496,11 @@ namespace MobileGL::MG_Util::BackendLoader { return true; } - static EGLDisplay eglDisplay = EGL_NO_DISPLAY; - static EGLSurface eglSurface = EGL_NO_SURFACE; - static EGLContext eglContext = EGL_NO_CONTEXT; - inline void DestroyTempEGLCtx(const MG_External::EGLFunctionsTable& eglFuncs) { - if (eglSurface != EGL_NO_SURFACE) { - eglFuncs.eglDestroySurface(eglDisplay, eglSurface); - eglSurface = EGL_NO_SURFACE; - } - if (eglContext != EGL_NO_CONTEXT) { - eglFuncs.eglDestroyContext(eglDisplay, eglContext); - eglContext = EGL_NO_CONTEXT; - } - if (eglDisplay != EGL_NO_DISPLAY) { - eglFuncs.eglTerminate(eglDisplay); - eglDisplay = EGL_NO_DISPLAY; - } - } - inline Bool InitTmpEGLContext(const MG_External::EGLFunctionsTable& eglFuncs) { - EGLint configAttribs[] = {EGL_RED_SIZE, - 8, - EGL_GREEN_SIZE, - 8, - EGL_BLUE_SIZE, - 8, - EGL_ALPHA_SIZE, - 8, - EGL_SURFACE_TYPE, - EGL_PBUFFER_BIT, - EGL_RENDERABLE_TYPE, - EGL_OPENGL_ES2_BIT, - EGL_NONE}; - - EGLint ctxAttribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE}; - - EGLint pbAttribs[] = {EGL_WIDTH, 32, EGL_HEIGHT, 32, EGL_NONE}; - - EGLConfig pbufConfig; - EGLint configsFound = 0; - - eglDisplay = eglFuncs.eglGetDisplay(EGL_DEFAULT_DISPLAY); - if (eglDisplay == EGL_NO_DISPLAY) { - DestroyTempEGLCtx(eglFuncs); - return false; - } - - if (eglFuncs.eglInitialize(eglDisplay, nullptr, nullptr) != EGL_TRUE) { - DestroyTempEGLCtx(eglFuncs); - return false; - } - - if (eglFuncs.eglBindAPI(EGL_OPENGL_ES_API) != EGL_TRUE) { - DestroyTempEGLCtx(eglFuncs); - return false; - } - - if (eglFuncs.eglChooseConfig(eglDisplay, configAttribs, &pbufConfig, 1, &configsFound) != EGL_TRUE) { - DestroyTempEGLCtx(eglFuncs); - return false; - } - - if (configsFound == 0) { - configAttribs[6] = 0; - if (eglFuncs.eglChooseConfig(eglDisplay, configAttribs, &pbufConfig, 1, &configsFound) != EGL_TRUE) { - DestroyTempEGLCtx(eglFuncs); - return false; - } - if (!configsFound) { - DestroyTempEGLCtx(eglFuncs); - return false; - } - } - - eglContext = eglFuncs.eglCreateContext(eglDisplay, pbufConfig, EGL_NO_CONTEXT, ctxAttribs); - if (eglContext == EGL_NO_CONTEXT) { - DestroyTempEGLCtx(eglFuncs); - return false; - } - - eglSurface = eglFuncs.eglCreatePbufferSurface(eglDisplay, pbufConfig, pbAttribs); - if (eglSurface == EGL_NO_SURFACE) { - DestroyTempEGLCtx(eglFuncs); - return false; - } - - if (eglFuncs.eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext) != EGL_TRUE) { - DestroyTempEGLCtx(eglFuncs); - return false; - } - return true; - } - - Bool FillInGLESCapabilities(MG_External::GLESCapabilities& caps, const MG_External::GLESFunctionsTable& glesFuncs, - const MG_External::EGLFunctionsTable& eglFuncs) { + Bool FillInGLESCapabilities(MG_External::GLESCapabilities& caps, const MG_External::GLESFunctionsTable& glesFuncs) { if (!glesFuncs.glGetString || !glesFuncs.glGetIntegerv) { MGLOG_E("Required GLES functions are not loaded, cannot query capabilities"); return false; } - if (!eglFuncs.eglGetDisplay || !eglFuncs.eglInitialize || !eglFuncs.eglChooseConfig || - !eglFuncs.eglCreateContext || !eglFuncs.eglCreatePbufferSurface || !eglFuncs.eglMakeCurrent) { - MGLOG_E("Required EGL functions are not loaded, cannot create temporary EGL context"); - return false; - } - - Bool eglCtxCreated = InitTmpEGLContext(eglFuncs); - if (!eglCtxCreated) { - MGLOG_E("Failed to create temporary EGL context, cannot query GLES capabilities"); - return false; - } auto* vendorName = glesFuncs.glGetString(GL_VENDOR); MGLOG_I("GL_VENDOR: %s", vendorName); @@ -637,7 +534,6 @@ namespace MobileGL::MG_Util::BackendLoader { glesFuncs.glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &caps.UniformBufferOffsetAlignment); MGLOG_I(" GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT: %d", caps.UniformBufferOffsetAlignment); - DestroyTempEGLCtx(eglFuncs); return true; } } // namespace MobileGL::MG_Util::BackendLoader diff --git a/MobileGL/MG_Util/BackendLoaders/OpenGL/Loader.h b/MobileGL/MG_Util/BackendLoaders/OpenGL/Loader.h index 8b6441b4..17b0f884 100644 --- a/MobileGL/MG_Util/BackendLoaders/OpenGL/Loader.h +++ b/MobileGL/MG_Util/BackendLoaders/OpenGL/Loader.h @@ -1020,8 +1020,7 @@ namespace MobileGL { MG_External::EGL::eglGetProcAddress_PTR procAddress); Bool AcquireEGLFunctions(MG_External::EGLFunctionsTable& funcs); Bool FillInGLESCapabilities(MG_External::GLESCapabilities& caps, - const MG_External::GLESFunctionsTable& glesFuncs, - const MG_External::EGLFunctionsTable& eglFuncs); + const MG_External::GLESFunctionsTable& glesFuncs); } // namespace MG_Util::BackendLoader } // namespace MobileGL #undef MOBILEGL_EXTERNAL_GLES