This repository has been archived by the owner on Apr 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
glxtexturefrompixmap.cpp
332 lines (278 loc) · 9.62 KB
/
glxtexturefrompixmap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include "glxtexturefrompixmap.h"
#include <QMap>
#include <QMetaType>
#include <QDataStream>
#include <QTextStream>
#include <QDebug>
#include <QOpenGLContext>
#include <QLoggingCategory>
#include <QX11Info>
#include <xcb/xcb_renderutil.h>
#include "compositor.h"
#include <GL/glx.h>
class GLXInfo
{
public:
static GLXInfo &instance();
Display *display;
PFNGLXBINDTEXIMAGEEXTPROC tfpBind;
PFNGLXRELEASETEXIMAGEEXTPROC tfpRelease;
struct VisualInfo
{
GLXFBConfig config;
int textureFormat;
int yInverted;
int depth;
int stencil;
bool alphaMatches;
VisualInfo()
: config(Q_NULLPTR), textureFormat(0), yInverted(0),
depth(INT_MAX / 2), stencil(INT_MAX / 2), alphaMatches(false)
{
}
};
const VisualInfo &configFor(xcb_visualid_t);
private:
Q_DISABLE_COPY(GLXInfo)
static const QLoggingCategory &log();
VisualInfo createVisualInfo(xcb_visualid_t) const;
xcb_connection_t *connection;
int screen;
xcb_render_query_pict_formats_reply_t *pictFormats;
QMap<xcb_visualid_t, int> visualDepth;
QMap<xcb_visualid_t, VisualInfo> visualInfos;
GLXInfo();
~GLXInfo();
};
GLXInfo::GLXInfo()
: display(QX11Info::display()),
tfpBind(Q_NULLPTR),
tfpRelease(Q_NULLPTR),
connection(QX11Info::connection()),
screen(QX11Info::appScreen()),
pictFormats(Q_NULLPTR)
{
auto extensionsString = glXQueryExtensionsString(display, screen);
QByteArrayList extensions(QByteArray(extensionsString).split(' '));
if (!extensions.contains(QByteArrayLiteral("GLX_EXT_texture_from_pixmap"))) {
qCritical(log) << "GLX_EXT_texture_from_pixmap is not supported";
return;
}
tfpBind = reinterpret_cast<PFNGLXBINDTEXIMAGEEXTPROC>
(glXGetProcAddress(reinterpret_cast<const GLubyte *>("glXBindTexImageEXT")));
if (!tfpBind) {
qCritical(log) << "GLX_EXT_texture_from_pixmap is reported, but glXBindTexImageEXT isn't available";
}
tfpRelease = reinterpret_cast<PFNGLXRELEASETEXIMAGEEXTPROC>
(glXGetProcAddress(reinterpret_cast<const GLubyte *>("glXReleaseTexImageEXT")));
if (!tfpRelease) {
qCritical(log) << "GLX_EXT_texture_from_pixmap is reported, but glXReleaseTexImageEXT isn't available";
}
auto pictFormatsCookie = xcb_render_query_pict_formats_unchecked(connection);
pictFormats = xcb_render_query_pict_formats_reply(connection, pictFormatsCookie, Q_NULLPTR);
if (!pictFormats) {
qCritical(log) << "xcb_render_query_pict_formats failed";
}
auto setup = xcb_get_setup(connection);
for (auto screen = xcb_setup_roots_iterator(setup); screen.rem; xcb_screen_next(&screen)) {
for (auto depth = xcb_screen_allowed_depths_iterator(screen.data); depth.rem; xcb_depth_next(&depth)) {
int len = xcb_depth_visuals_length(depth.data);
auto visuals = xcb_depth_visuals(depth.data);
for (int i = 0; i < len; i++) {
visualDepth[visuals[i].visual_id] = depth.data->depth;
}
}
}
}
GLXInfo::~GLXInfo()
{
}
GLXInfo &GLXInfo::instance()
{
static GLXInfo instance_;
return instance_;
}
const QLoggingCategory &GLXInfo::log()
{
static const QLoggingCategory log_("GLXInfo");
return log_;
}
GLXInfo::VisualInfo GLXInfo::createVisualInfo(xcb_visualid_t visual) const
{
VisualInfo bestConfig;
auto pictVisual = xcb_render_util_find_visual_format(pictFormats, visual);
if (!pictVisual) {
qCritical(log) << "Can't find pictformat for visual" << visual;
return bestConfig;
}
auto pictFormInfos = xcb_render_query_pict_formats_formats(pictFormats);
auto nPictFormInfos = xcb_render_query_pict_formats_formats_length(pictFormats);
const xcb_render_pictforminfo_t *pictFormInfo = Q_NULLPTR;
for (int i = 0; i < nPictFormInfos; i++) {
if (pictFormInfos[i].id == pictVisual->format) {
pictFormInfo = &pictFormInfos[i];
}
}
Q_ASSERT(pictFormInfo);
int redBits = __builtin_popcount(pictFormInfo->direct.red_mask);
int greenBits = __builtin_popcount(pictFormInfo->direct.green_mask);
int blueBits = __builtin_popcount(pictFormInfo->direct.blue_mask);
int alphaBits = __builtin_popcount(pictFormInfo->direct.alpha_mask);
int attrs[] = {
GLX_RENDER_TYPE, GLX_RGBA_BIT,
GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT | GLX_PIXMAP_BIT,
GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR,
GLX_X_RENDERABLE, True,
GLX_CONFIG_CAVEAT, int(GLX_DONT_CARE),
GLX_BUFFER_SIZE, redBits + greenBits + blueBits + alphaBits,
GLX_RED_SIZE, redBits,
GLX_GREEN_SIZE, greenBits,
GLX_BLUE_SIZE, blueBits,
GLX_ALPHA_SIZE, alphaBits,
GLX_STENCIL_SIZE, 0,
GLX_DEPTH_SIZE, 0,
0
};
int nConfigs = 0;
GLXFBConfig *configs = glXChooseFBConfig(display, screen, attrs, &nConfigs);
if (nConfigs <= 0) {
qCritical(log) << "glXChooseFBConfig: no FBConfig for visual" << visual;
return bestConfig;
}
for (int i = 0; i < nConfigs; i++) {
VisualInfo info;
info.config = configs[i];
#define INIT_FROM_ATTR(var, attr) \
if (glXGetFBConfigAttrib(display, configs[i], attr, &var) != Success) { \
qWarning(log) << ("Can't get " #attr " of FBConfig") << configs[i]; \
continue; \
}
int fbRedBits, fbGreenBits, fbBlueBits;
INIT_FROM_ATTR(fbRedBits, GLX_RED_SIZE);
INIT_FROM_ATTR(fbGreenBits, GLX_GREEN_SIZE);
INIT_FROM_ATTR(fbBlueBits, GLX_BLUE_SIZE);
if (fbRedBits != redBits || fbGreenBits != greenBits || fbBlueBits != blueBits) {
continue;
}
int fbVisualId;
INIT_FROM_ATTR(fbVisualId, GLX_VISUAL_ID);
if (visualDepth[xcb_visualid_t(fbVisualId)] != visualDepth[visual]) {
continue;
}
int fbBindRGB = 0, fbBindRGBA = 0;
glXGetFBConfigAttrib(display, configs[i], GLX_BIND_TO_TEXTURE_RGB_EXT, &fbBindRGB);
glXGetFBConfigAttrib(display, configs[i], GLX_BIND_TO_TEXTURE_RGBA_EXT, &fbBindRGBA);
if (!fbBindRGB && !fbBindRGBA) {
continue;
}
glXGetFBConfigAttrib(display, configs[i], GLX_Y_INVERTED_EXT, &info.yInverted);
int fbTextureTargets;
INIT_FROM_ATTR(fbTextureTargets, GLX_BIND_TO_TEXTURE_TARGETS_EXT);
if (!(fbTextureTargets & GLX_TEXTURE_2D_BIT_EXT)) {
continue;
}
if (alphaBits) {
info.alphaMatches = (fbBindRGBA != 0);
info.textureFormat = fbBindRGBA ? GLX_TEXTURE_FORMAT_RGBA_EXT : GLX_TEXTURE_FORMAT_RGB_EXT;
} else {
info.alphaMatches = (fbBindRGB != 0);
info.textureFormat = fbBindRGB ? GLX_TEXTURE_FORMAT_RGB_EXT : GLX_TEXTURE_FORMAT_RGBA_EXT;
}
INIT_FROM_ATTR(info.depth, GLX_DEPTH_SIZE);
INIT_FROM_ATTR(info.stencil, GLX_STENCIL_SIZE);
if (info.alphaMatches != bestConfig.alphaMatches) {
if (info.alphaMatches) {
bestConfig = info;
continue;
}
} else if (info.depth + info.stencil < bestConfig.depth + bestConfig.stencil) {
bestConfig = info;
}
}
XFree(configs);
return bestConfig;
}
const GLXInfo::VisualInfo &GLXInfo::configFor(xcb_visualid_t visual)
{
auto existing = visualInfos.constFind(visual);
if (existing != visualInfos.constEnd()) {
return existing.value();
}
return visualInfos.insert(visual, createVisualInfo(visual)).value();
}
GLXTextureFromPixmap::GLXTextureFromPixmap(xcb_pixmap_t pixmap, xcb_visualid_t visual, const QSize &size)
: QOpenGLFunctions(QOpenGLContext::currentContext()),
texture_(0),
glxPixmap_(XCB_NONE),
pixmap_(pixmap),
hasAlpha_(false),
isYInverted_(false),
size_(size),
rebindTFP_(false)
{
glGenTextures(1, &texture_);
glBindTexture(GL_TEXTURE_2D, texture_);
setFiltering(Linear);
setHorizontalWrapMode(ClampToEdge);
setVerticalWrapMode(ClampToEdge);
updateBindOptions(true);
auto &glx = GLXInfo::instance();
auto &info = glx.configFor(visual);
if (info.config) {
int attr[] = {
GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT,
GLX_TEXTURE_FORMAT_EXT, info.textureFormat,
GLX_MIPMAP_TEXTURE_EXT, false,
None
};
glxPixmap_ = glXCreatePixmap(glx.display, info.config, pixmap_, attr);
rebindTFP_ = true;
hasAlpha_ = (info.textureFormat == GLX_TEXTURE_FORMAT_RGBA_EXT);
isYInverted_ = !info.yInverted;
}
}
GLXTextureFromPixmap::~GLXTextureFromPixmap()
{
if (texture_) {
glDeleteTextures(1, &texture_);
}
if (!glxPixmap_) {
return;
}
auto &glx = GLXInfo::instance();
glx.tfpRelease(glx.display, glxPixmap_, GLX_FRONT_LEFT_EXT);
glXDestroyPixmap(glx.display, glxPixmap_);
glxPixmap_ = 0;
}
int GLXTextureFromPixmap::textureId() const
{
return static_cast<int>(texture_);
}
QSize GLXTextureFromPixmap::textureSize() const
{
return size_;
}
bool GLXTextureFromPixmap::hasAlphaChannel() const
{
return hasAlpha_;
}
bool GLXTextureFromPixmap::hasMipmaps() const
{
return false;
}
void GLXTextureFromPixmap::rebind()
{
if (glxPixmap_) {
rebindTFP_ = true;
}
}
void GLXTextureFromPixmap::bind()
{
glBindTexture(GL_TEXTURE_2D, texture_);
updateBindOptions();
if (glxPixmap_ && rebindTFP_) {
rebindTFP_ = false;
auto &glx = GLXInfo::instance();
glx.tfpBind(glx.display, glxPixmap_, GLX_FRONT_LEFT_EXT, Q_NULLPTR);
}
}