Port QXlibEglIntegration::getCompatibleVisualId to xcb

In combination with EGL_EXT_platform_xcb support, this allows xcb_egl
to be used without xlib. Without EGL_EXT_platform_xcb support, this
still reduces amount of code using xlib.

Pick-to: 6.4
Change-Id: I29e2b29f7ef8ea34320887f62697f84232b86fba
Reviewed-by: Liang Qi <liang.qi@qt.io>
This commit is contained in:
Ilya Fedin 2021-10-04 10:32:38 +04:00 committed by Liang Qi
parent 1c106b37c7
commit c3e624eb50
7 changed files with 142 additions and 190 deletions

View File

@ -853,13 +853,6 @@ qt_internal_extend_target(Gui CONDITION QT_FEATURE_egl AND QT_FEATURE_opengl
opengl/platform/egl/qeglplatformcontext.cpp opengl/platform/egl/qeglplatformcontext_p.h
)
qt_internal_extend_target(Gui CONDITION QT_FEATURE_egl AND QT_FEATURE_egl_x11
SOURCES
opengl/platform/egl/qxlibeglintegration.cpp opengl/platform/egl/qxlibeglintegration_p.h
LIBRARIES
X11::X11
)
qt_internal_extend_target(Gui CONDITION QT_FEATURE_egl AND NOT QT_FEATURE_egl_x11
DEFINES
QT_EGL_NO_X11

View File

@ -1,130 +0,0 @@
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include <QtCore/qloggingcategory.h>
#include "qxlibeglintegration_p.h"
QT_BEGIN_NAMESPACE
Q_LOGGING_CATEGORY(lcXlibEglDebug, "qt.egl.xlib.debug")
VisualID QXlibEglIntegration::getCompatibleVisualId(Display *display, EGLDisplay eglDisplay, EGLConfig config)
{
VisualID visualId = 0;
EGLint eglValue = 0;
EGLint configRedSize = 0;
eglGetConfigAttrib(eglDisplay, config, EGL_RED_SIZE, &configRedSize);
EGLint configGreenSize = 0;
eglGetConfigAttrib(eglDisplay, config, EGL_GREEN_SIZE, &configGreenSize);
EGLint configBlueSize = 0;
eglGetConfigAttrib(eglDisplay, config, EGL_BLUE_SIZE, &configBlueSize);
EGLint configAlphaSize = 0;
eglGetConfigAttrib(eglDisplay, config, EGL_ALPHA_SIZE, &configAlphaSize);
eglGetConfigAttrib(eglDisplay, config, EGL_CONFIG_ID, &eglValue);
int configId = eglValue;
// See if EGL provided a valid VisualID:
eglGetConfigAttrib(eglDisplay, config, EGL_NATIVE_VISUAL_ID, &eglValue);
visualId = (VisualID)eglValue;
if (visualId) {
// EGL has suggested a visual id, so get the rest of the visual info for that id:
XVisualInfo visualInfoTemplate;
memset(&visualInfoTemplate, 0, sizeof(XVisualInfo));
visualInfoTemplate.visualid = visualId;
XVisualInfo *chosenVisualInfo;
int matchingCount = 0;
chosenVisualInfo = XGetVisualInfo(display, VisualIDMask, &visualInfoTemplate, &matchingCount);
if (chosenVisualInfo) {
// Skip size checks if implementation supports non-matching visual
// and config (QTBUG-9444).
if (q_hasEglExtension(eglDisplay,"EGL_NV_post_convert_rounding")) {
XFree(chosenVisualInfo);
return visualId;
}
// Skip also for i.MX6 where 565 visuals are suggested for the default 444 configs and it works just fine.
const char *vendor = eglQueryString(eglDisplay, EGL_VENDOR);
if (vendor && strstr(vendor, "Vivante")) {
XFree(chosenVisualInfo);
return visualId;
}
int visualRedSize = qPopulationCount(chosenVisualInfo->red_mask);
int visualGreenSize = qPopulationCount(chosenVisualInfo->green_mask);
int visualBlueSize = qPopulationCount(chosenVisualInfo->blue_mask);
int visualAlphaSize = chosenVisualInfo->depth - visualRedSize - visualBlueSize - visualGreenSize;
const bool visualMatchesConfig = visualRedSize >= configRedSize
&& visualGreenSize >= configGreenSize
&& visualBlueSize >= configBlueSize
&& visualAlphaSize >= configAlphaSize;
// In some cases EGL tends to suggest a 24-bit visual for 8888
// configs. In such a case we have to fall back to XGetVisualInfo.
if (!visualMatchesConfig) {
visualId = 0;
qCDebug(lcXlibEglDebug,
"EGL suggested using X Visual ID %d (%d %d %d %d depth %d) for EGL config %d"
"(%d %d %d %d), but this is incompatible",
(int)visualId, visualRedSize, visualGreenSize, visualBlueSize, visualAlphaSize, chosenVisualInfo->depth,
configId, configRedSize, configGreenSize, configBlueSize, configAlphaSize);
}
} else {
qCDebug(lcXlibEglDebug, "EGL suggested using X Visual ID %d for EGL config %d, but that isn't a valid ID",
(int)visualId, configId);
visualId = 0;
}
XFree(chosenVisualInfo);
}
else
qCDebug(lcXlibEglDebug, "EGL did not suggest a VisualID (EGL_NATIVE_VISUAL_ID was zero) for EGLConfig %d", configId);
if (visualId) {
qCDebug(lcXlibEglDebug, configAlphaSize > 0
? "Using ARGB Visual ID %d provided by EGL for config %d"
: "Using Opaque Visual ID %d provided by EGL for config %d", (int)visualId, configId);
return visualId;
}
// Finally, try to use XGetVisualInfo and only use the bit depths to match on:
if (!visualId) {
XVisualInfo visualInfoTemplate;
memset(&visualInfoTemplate, 0, sizeof(XVisualInfo));
XVisualInfo *matchingVisuals;
int matchingCount = 0;
visualInfoTemplate.depth = configRedSize + configGreenSize + configBlueSize + configAlphaSize;
matchingVisuals = XGetVisualInfo(display,
VisualDepthMask,
&visualInfoTemplate,
&matchingCount);
if (!matchingVisuals) {
// Try again without taking the alpha channel into account:
visualInfoTemplate.depth = configRedSize + configGreenSize + configBlueSize;
matchingVisuals = XGetVisualInfo(display,
VisualDepthMask,
&visualInfoTemplate,
&matchingCount);
}
if (matchingVisuals) {
visualId = matchingVisuals[0].visualid;
XFree(matchingVisuals);
}
}
if (visualId) {
qCDebug(lcXlibEglDebug, "Using Visual ID %d provided by XGetVisualInfo for EGL config %d", (int)visualId, configId);
return visualId;
}
qWarning("Unable to find an X11 visual which matches EGL config %d", configId);
return (VisualID)0;
}
QT_END_NAMESPACE

View File

@ -1,33 +0,0 @@
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#ifndef QXLIBEGLINTEGRATION_H
#define QXLIBEGLINTEGRATION_H
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#include <QtGui/private/qeglconvenience_p.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
QT_BEGIN_NAMESPACE
class Q_GUI_EXPORT QXlibEglIntegration
{
public:
static VisualID getCompatibleVisualId(Display *display, EGLDisplay eglDisplay, EGLConfig config);
};
QT_END_NAMESPACE
#endif // QXLIBEGLINTEGRATION_H

View File

@ -6,12 +6,55 @@
#include "qxcbeglcontext.h"
#include <QtGui/QOffscreenSurface>
#include <QtGui/private/qeglconvenience_p.h>
#include <QtGui/private/qeglstreamconvenience_p.h>
#include <optional>
#include "qxcbeglnativeinterfacehandler.h"
QT_BEGIN_NAMESPACE
namespace {
struct VisualInfo
{
xcb_visualtype_t visualType;
uint8_t depth;
};
std::optional<VisualInfo> getVisualInfo(xcb_screen_t *screen,
std::optional<xcb_visualid_t> requestedVisualId,
std::optional<uint8_t> requestedDepth = std::nullopt)
{
xcb_depth_iterator_t depthIterator = xcb_screen_allowed_depths_iterator(screen);
while (depthIterator.rem) {
xcb_depth_t *depth = depthIterator.data;
xcb_visualtype_iterator_t visualTypeIterator = xcb_depth_visuals_iterator(depth);
while (visualTypeIterator.rem) {
xcb_visualtype_t *visualType = visualTypeIterator.data;
if (requestedVisualId && visualType->visual_id != *requestedVisualId) {
xcb_visualtype_next(&visualTypeIterator);
continue;
}
if (requestedDepth && depth->depth != *requestedDepth) {
xcb_visualtype_next(&visualTypeIterator);
continue;
}
return VisualInfo{ *visualType, depth->depth };
}
xcb_depth_next(&depthIterator);
}
return std::nullopt;
}
} // namespace
QXcbEglIntegration::QXcbEglIntegration()
: m_connection(nullptr)
, m_egl_display(EGL_NO_DISPLAY)
@ -93,4 +136,99 @@ void *QXcbEglIntegration::xlib_display() const
#endif
}
xcb_visualid_t QXcbEglIntegration::getCompatibleVisualId(xcb_screen_t *screen, EGLConfig config) const
{
xcb_visualid_t visualId = 0;
EGLint eglValue = 0;
EGLint configRedSize = 0;
eglGetConfigAttrib(eglDisplay(), config, EGL_RED_SIZE, &configRedSize);
EGLint configGreenSize = 0;
eglGetConfigAttrib(eglDisplay(), config, EGL_GREEN_SIZE, &configGreenSize);
EGLint configBlueSize = 0;
eglGetConfigAttrib(eglDisplay(), config, EGL_BLUE_SIZE, &configBlueSize);
EGLint configAlphaSize = 0;
eglGetConfigAttrib(eglDisplay(), config, EGL_ALPHA_SIZE, &configAlphaSize);
eglGetConfigAttrib(eglDisplay(), config, EGL_CONFIG_ID, &eglValue);
int configId = eglValue;
// See if EGL provided a valid VisualID:
eglGetConfigAttrib(eglDisplay(), config, EGL_NATIVE_VISUAL_ID, &eglValue);
visualId = eglValue;
if (visualId) {
// EGL has suggested a visual id, so get the rest of the visual info for that id:
std::optional<VisualInfo> chosenVisualInfo = getVisualInfo(screen, visualId);
if (chosenVisualInfo) {
// Skip size checks if implementation supports non-matching visual
// and config (QTBUG-9444).
if (q_hasEglExtension(eglDisplay(), "EGL_NV_post_convert_rounding"))
return visualId;
// Skip also for i.MX6 where 565 visuals are suggested for the default 444 configs and it works just fine.
const char *vendor = eglQueryString(eglDisplay(), EGL_VENDOR);
if (vendor && strstr(vendor, "Vivante"))
return visualId;
int visualRedSize = qPopulationCount(chosenVisualInfo->visualType.red_mask);
int visualGreenSize = qPopulationCount(chosenVisualInfo->visualType.green_mask);
int visualBlueSize = qPopulationCount(chosenVisualInfo->visualType.blue_mask);
int visualAlphaSize = chosenVisualInfo->depth - visualRedSize - visualBlueSize - visualGreenSize;
const bool visualMatchesConfig = visualRedSize >= configRedSize
&& visualGreenSize >= configGreenSize
&& visualBlueSize >= configBlueSize
&& visualAlphaSize >= configAlphaSize;
// In some cases EGL tends to suggest a 24-bit visual for 8888
// configs. In such a case we have to fall back to getVisualInfo.
if (!visualMatchesConfig) {
visualId = 0;
qCDebug(lcQpaGl,
"EGL suggested using X Visual ID %d (%d %d %d %d depth %d) for EGL config %d"
"(%d %d %d %d), but this is incompatible",
visualId, visualRedSize, visualGreenSize, visualBlueSize, visualAlphaSize, chosenVisualInfo->depth,
configId, configRedSize, configGreenSize, configBlueSize, configAlphaSize);
}
} else {
qCDebug(lcQpaGl, "EGL suggested using X Visual ID %d for EGL config %d, but that isn't a valid ID",
visualId, configId);
visualId = 0;
}
}
else
qCDebug(lcQpaGl, "EGL did not suggest a VisualID (EGL_NATIVE_VISUAL_ID was zero) for EGLConfig %d", configId);
if (visualId) {
qCDebug(lcQpaGl, configAlphaSize > 0
? "Using ARGB Visual ID %d provided by EGL for config %d"
: "Using Opaque Visual ID %d provided by EGL for config %d", visualId, configId);
return visualId;
}
// Finally, try to use getVisualInfo and only use the bit depths to match on:
if (!visualId) {
uint8_t depth = configRedSize + configGreenSize + configBlueSize + configAlphaSize;
std::optional<VisualInfo> matchingVisual = getVisualInfo(screen, std::nullopt, depth);
if (!matchingVisual) {
// Try again without taking the alpha channel into account:
depth = configRedSize + configGreenSize + configBlueSize;
matchingVisual = getVisualInfo(screen, std::nullopt, depth);
}
if (matchingVisual)
visualId = matchingVisual->visualType.visual_id;
}
if (visualId) {
qCDebug(lcQpaGl, "Using Visual ID %d provided by getVisualInfo for EGL config %d", visualId, configId);
return visualId;
}
qWarning("Unable to find an X11 visual which matches EGL config %d", configId);
return 0;
}
QT_END_NAMESPACE

View File

@ -39,6 +39,8 @@ public:
EGLDisplay eglDisplay() const { return m_egl_display; }
void *xlib_display() const;
xcb_visualid_t getCompatibleVisualId(xcb_screen_t *screen, EGLConfig config) const;
private:
QXcbConnection *m_connection;
EGLDisplay m_egl_display;

View File

@ -6,7 +6,6 @@
#include "qxcbeglintegration.h"
#include <QtGui/private/qeglconvenience_p.h>
#include <QtGui/private/qxlibeglintegration_p.h>
QT_BEGIN_NAMESPACE
@ -29,29 +28,15 @@ void QXcbEglWindow::resolveFormat(const QSurfaceFormat &format)
m_format = q_glFormatFromConfig(m_glIntegration->eglDisplay(), m_config, format);
}
#if QT_CONFIG(xcb_xlib)
const xcb_visualtype_t *QXcbEglWindow::createVisual()
{
QXcbScreen *scr = xcbScreen();
if (!scr)
return QXcbWindow::createVisual();
Display *xdpy = static_cast<Display *>(m_glIntegration->xlib_display());
VisualID id = QXlibEglIntegration::getCompatibleVisualId(xdpy, m_glIntegration->eglDisplay(), m_config);
XVisualInfo visualInfoTemplate;
memset(&visualInfoTemplate, 0, sizeof(XVisualInfo));
visualInfoTemplate.visualid = id;
XVisualInfo *visualInfo;
int matchingCount = 0;
visualInfo = XGetVisualInfo(xdpy, VisualIDMask, &visualInfoTemplate, &matchingCount);
const xcb_visualtype_t *xcb_visualtype = scr->visualForId(visualInfo->visualid);
XFree(visualInfo);
return xcb_visualtype;
xcb_visualid_t id = m_glIntegration->getCompatibleVisualId(scr->screen(), m_config);
return scr->visualForId(id);
}
#endif
void QXcbEglWindow::create()
{

View File

@ -25,10 +25,7 @@ public:
protected:
void create() override;
void resolveFormat(const QSurfaceFormat &format) override;
#if QT_CONFIG(xcb_xlib)
const xcb_visualtype_t *createVisual() override;
#endif
private:
QXcbEglIntegration *m_glIntegration;