From 6d96b8c4af00c37f954d2d70d295c679d74647fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Gr=C3=A4=C3=9Flin?= Date: Fri, 13 Nov 2015 08:29:49 +0100 Subject: [PATCH] Move egl context creation into AbstractEglContext Code was more or less copied into each backend, so it's better to have a shared implementation. --- abstract_egl_backend.cpp | 40 +++++++++++++++++++ abstract_egl_backend.h | 5 +-- backends/drm/egl_gbm_backend.cpp | 37 +---------------- .../hwcomposer/egl_hwcomposer_backend.cpp | 12 +----- backends/virtual/egl_gbm_backend.cpp | 34 +--------------- backends/wayland/egl_wayland_backend.cpp | 35 +--------------- eglonxbackend.cpp | 36 +---------------- 7 files changed, 50 insertions(+), 149 deletions(-) diff --git a/abstract_egl_backend.cpp b/abstract_egl_backend.cpp index 9f2d5fbdac..e8701e8d09 100644 --- a/abstract_egl_backend.cpp +++ b/abstract_egl_backend.cpp @@ -182,6 +182,46 @@ bool AbstractEglBackend::isOpenGLES() const return QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGLES; } +bool AbstractEglBackend::createContext() +{ + EGLContext ctx = EGL_NO_CONTEXT; + if (isOpenGLES()) { + const EGLint context_attribs[] = { + EGL_CONTEXT_CLIENT_VERSION, 2, + EGL_NONE + }; + + ctx = eglCreateContext(m_display, config(), EGL_NO_CONTEXT, context_attribs); + } else { + const EGLint context_attribs_31_core[] = { + EGL_CONTEXT_MAJOR_VERSION_KHR, 3, + EGL_CONTEXT_MINOR_VERSION_KHR, 1, + EGL_NONE + }; + + const EGLint context_attribs_legacy[] = { + EGL_NONE + }; + + const QByteArray eglExtensions = eglQueryString(m_display, EGL_EXTENSIONS); + const QList extensions = eglExtensions.split(' '); + + // Try to create a 3.1 core context + if (options->glCoreProfile() && extensions.contains("EGL_KHR_create_context")) + ctx = eglCreateContext(m_display, config(), EGL_NO_CONTEXT, context_attribs_31_core); + + if (ctx == EGL_NO_CONTEXT) + ctx = eglCreateContext(m_display, config(), EGL_NO_CONTEXT, context_attribs_legacy); + } + + if (ctx == EGL_NO_CONTEXT) { + qCCritical(KWIN_CORE) << "Create Context failed"; + return false; + } + m_context = ctx; + return true; +} + AbstractEglTexture::AbstractEglTexture(SceneOpenGL::Texture *texture, AbstractEglBackend *backend) : SceneOpenGL::TexturePrivate() , q(texture) diff --git a/abstract_egl_backend.h b/abstract_egl_backend.h index 93cce8ded4..caa14ac609 100644 --- a/abstract_egl_backend.h +++ b/abstract_egl_backend.h @@ -51,9 +51,6 @@ protected: void setEglDisplay(const EGLDisplay &display) { m_display = display; } - void setContext(const EGLContext &context) { - m_context = context; - } void setSurface(const EGLSurface &surface) { m_surface = surface; } @@ -70,6 +67,8 @@ protected: bool hasClientExtension(const QByteArray &ext) const; bool isOpenGLES() const; + bool createContext(); + private: EGLDisplay m_display = EGL_NO_DISPLAY; EGLSurface m_surface = EGL_NO_SURFACE; diff --git a/backends/drm/egl_gbm_backend.cpp b/backends/drm/egl_gbm_backend.cpp index afcfc986ed..aedd297e4b 100644 --- a/backends/drm/egl_gbm_backend.cpp +++ b/backends/drm/egl_gbm_backend.cpp @@ -131,42 +131,9 @@ bool EglGbmBackend::initRenderingContext() { initBufferConfigs(); - EGLContext context = EGL_NO_CONTEXT; - if (isOpenGLES()) { - const EGLint context_attribs[] = { - EGL_CONTEXT_CLIENT_VERSION, 2, - EGL_NONE - }; - - context = eglCreateContext(eglDisplay(), config(), EGL_NO_CONTEXT, context_attribs); - } else { - const EGLint context_attribs_31_core[] = { - EGL_CONTEXT_MAJOR_VERSION_KHR, 3, - EGL_CONTEXT_MINOR_VERSION_KHR, 1, - EGL_CONTEXT_FLAGS_KHR, EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR, - EGL_NONE - }; - - const EGLint context_attribs_legacy[] = { - EGL_NONE - }; - - const char* eglExtensionsCString = eglQueryString(eglDisplay(), EGL_EXTENSIONS); - const QList extensions = QByteArray::fromRawData(eglExtensionsCString, qstrlen(eglExtensionsCString)).split(' '); - - // Try to create a 3.1 core context - if (options->glCoreProfile() && extensions.contains(QByteArrayLiteral("EGL_KHR_create_context"))) - context = eglCreateContext(eglDisplay(), config(), EGL_NO_CONTEXT, context_attribs_31_core); - - if (context == EGL_NO_CONTEXT) - context = eglCreateContext(eglDisplay(), config(), EGL_NO_CONTEXT, context_attribs_legacy); - } - - if (context == EGL_NO_CONTEXT) { - qCCritical(KWIN_DRM) << "Create Context failed"; - return false; + if (!createContext()) { + return false; } - setContext(context); const auto outputs = m_backend->outputs(); for (DrmOutput *drmOutput: outputs) { diff --git a/backends/hwcomposer/egl_hwcomposer_backend.cpp b/backends/hwcomposer/egl_hwcomposer_backend.cpp index 6ac30bc072..3c4387f1c2 100644 --- a/backends/hwcomposer/egl_hwcomposer_backend.cpp +++ b/backends/hwcomposer/egl_hwcomposer_backend.cpp @@ -102,19 +102,9 @@ bool EglHwcomposerBackend::initRenderingContext() return false; } - EGLContext context = EGL_NO_CONTEXT; - const EGLint context_attribs[] = { - EGL_CONTEXT_CLIENT_VERSION, 2, - EGL_NONE - }; - - context = eglCreateContext(eglDisplay(), config(), EGL_NO_CONTEXT, context_attribs); - - if (context == EGL_NO_CONTEXT) { - qCCritical(KWIN_HWCOMPOSER) << "Create Context failed"; + if (!createContext()) { return false; } - setContext(context); m_nativeSurface = m_backend->createSurface(); EGLSurface surface = eglCreateWindowSurface(eglDisplay(), config(), (EGLNativeWindowType)static_cast(m_nativeSurface), nullptr); diff --git a/backends/virtual/egl_gbm_backend.cpp b/backends/virtual/egl_gbm_backend.cpp index fe9b9639ff..d096111221 100644 --- a/backends/virtual/egl_gbm_backend.cpp +++ b/backends/virtual/egl_gbm_backend.cpp @@ -112,39 +112,9 @@ bool EglGbmBackend::initRenderingContext() return false; } - EGLContext context = EGL_NO_CONTEXT; - if (isOpenGLES()) { - const EGLint context_attribs[] = { - EGL_CONTEXT_CLIENT_VERSION, 2, - EGL_NONE - }; - - context = eglCreateContext(eglDisplay(), config(), EGL_NO_CONTEXT, context_attribs); - } else { - const EGLint context_attribs_31_core[] = { - EGL_CONTEXT_MAJOR_VERSION_KHR, 3, - EGL_CONTEXT_MINOR_VERSION_KHR, 1, - EGL_CONTEXT_FLAGS_KHR, EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR, - EGL_NONE - }; - - const EGLint context_attribs_legacy[] = { - EGL_NONE - }; - - // Try to create a 3.1 core context - if (options->glCoreProfile() && extensions.contains(QByteArrayLiteral("EGL_KHR_create_context"))) - context = eglCreateContext(eglDisplay(), config(), EGL_NO_CONTEXT, context_attribs_31_core); - - if (context == EGL_NO_CONTEXT) - context = eglCreateContext(eglDisplay(), config(), EGL_NO_CONTEXT, context_attribs_legacy); - } - - if (context == EGL_NO_CONTEXT) { - // qCCritical(KWIN_DRM) << "Create Context failed"; - return false; + if (!createContext()) { + return false; } - setContext(context); setSurfaceLessContext(true); return makeCurrent(); diff --git a/backends/wayland/egl_wayland_backend.cpp b/backends/wayland/egl_wayland_backend.cpp index c63d409f73..7906bf0e72 100644 --- a/backends/wayland/egl_wayland_backend.cpp +++ b/backends/wayland/egl_wayland_backend.cpp @@ -113,42 +113,9 @@ bool EglWaylandBackend::initRenderingContext() { initBufferConfigs(); - EGLContext context = EGL_NO_CONTEXT; - if (isOpenGLES()) { - const EGLint context_attribs[] = { - EGL_CONTEXT_CLIENT_VERSION, 2, - EGL_NONE - }; - - context = eglCreateContext(eglDisplay(), config(), EGL_NO_CONTEXT, context_attribs); - } else { - const EGLint context_attribs_31_core[] = { - EGL_CONTEXT_MAJOR_VERSION_KHR, 3, - EGL_CONTEXT_MINOR_VERSION_KHR, 1, - EGL_CONTEXT_FLAGS_KHR, EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR, - EGL_NONE - }; - - const EGLint context_attribs_legacy[] = { - EGL_NONE - }; - - const char* eglExtensionsCString = eglQueryString(eglDisplay(), EGL_EXTENSIONS); - const QList extensions = QByteArray::fromRawData(eglExtensionsCString, qstrlen(eglExtensionsCString)).split(' '); - - // Try to create a 3.1 core context - if (options->glCoreProfile() && extensions.contains(QByteArrayLiteral("EGL_KHR_create_context"))) - context = eglCreateContext(eglDisplay(), config(), EGL_NO_CONTEXT, context_attribs_31_core); - - if (context == EGL_NO_CONTEXT) - context = eglCreateContext(eglDisplay(), config(), EGL_NO_CONTEXT, context_attribs_legacy); - } - - if (context == EGL_NO_CONTEXT) { - qCCritical(KWIN_WAYLAND_BACKEND) << "Create Context failed"; + if (!createContext()) { return false; } - setContext(context); if (!m_wayland->surface()) { return false; diff --git a/eglonxbackend.cpp b/eglonxbackend.cpp index 990be752c0..f7c9475cc1 100644 --- a/eglonxbackend.cpp +++ b/eglonxbackend.cpp @@ -230,43 +230,11 @@ bool EglOnXBackend::initRenderingContext() } setSurface(surface); - EGLContext ctx = EGL_NO_CONTEXT; - if (isOpenGLES()) { - const EGLint context_attribs[] = { - EGL_CONTEXT_CLIENT_VERSION, 2, - EGL_NONE - }; - - ctx = eglCreateContext(dpy, config(), EGL_NO_CONTEXT, context_attribs); - } else { - const EGLint context_attribs_31_core[] = { - EGL_CONTEXT_MAJOR_VERSION_KHR, 3, - EGL_CONTEXT_MINOR_VERSION_KHR, 1, - EGL_NONE - }; - - const EGLint context_attribs_legacy[] = { - EGL_NONE - }; - - const QByteArray eglExtensions = eglQueryString(dpy, EGL_EXTENSIONS); - const QList extensions = eglExtensions.split(' '); - - // Try to create a 3.1 core context - if (options->glCoreProfile() && extensions.contains("EGL_KHR_create_context")) - ctx = eglCreateContext(dpy, config(), EGL_NO_CONTEXT, context_attribs_31_core); - - if (ctx == EGL_NO_CONTEXT) - ctx = eglCreateContext(dpy, config(), EGL_NO_CONTEXT, context_attribs_legacy); - } - - if (ctx == EGL_NO_CONTEXT) { - qCCritical(KWIN_CORE) << "Create Context failed"; + if (!createContext()) { return false; } - setContext(ctx); - if (eglMakeCurrent(dpy, surface, surface, ctx) == EGL_FALSE) { + if (eglMakeCurrent(dpy, surface, surface, context()) == EGL_FALSE) { qCCritical(KWIN_CORE) << "Make Context Current failed"; return false; }