-
Notifications
You must be signed in to change notification settings - Fork 1
/
apple_glx_pbuffer.c
330 lines (257 loc) · 8.49 KB
/
apple_glx_pbuffer.c
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
/*
Copyright (c) 2009 Apple Inc.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name(s) of the above
copyright holders shall not be used in advertising or otherwise to
promote the sale, use or other dealings in this Software without
prior written authorization.
*/
#include <stdlib.h>
#include <pthread.h>
#include <assert.h>
#include "apple_glx.h"
#include "glcontextmodes.h"
#include "apple_glx_context.h"
#include "apple_glx_drawable.h"
#include "apple_cgl.h"
static bool pbuffer_make_current(struct apple_glx_context *ac,
struct apple_glx_drawable *d);
static void pbuffer_destroy(Display * dpy, struct apple_glx_drawable *d);
static struct apple_glx_drawable_callbacks callbacks = {
.type = APPLE_GLX_DRAWABLE_PBUFFER,
.make_current = pbuffer_make_current,
.destroy = pbuffer_destroy
};
/* Return true if an error occurred. */
bool
pbuffer_make_current(struct apple_glx_context *ac,
struct apple_glx_drawable *d)
{
struct apple_glx_pbuffer *pbuf = &d->types.pbuffer;
CGLError cglerr;
assert(APPLE_GLX_DRAWABLE_PBUFFER == d->type);
cglerr = apple_cgl.set_pbuffer(ac->context_obj, pbuf->buffer_obj, 0, 0, 0);
if (kCGLNoError != cglerr) {
fprintf(stderr, "set_pbuffer: %s\n", apple_cgl.error_string(cglerr));
return true;
}
if (!ac->made_current) {
glViewport(0, 0, pbuf->width, pbuf->height);
glScissor(0, 0, pbuf->width, pbuf->height);
ac->made_current = true;
}
apple_glx_diagnostic("made pbuffer drawable 0x%lx current\n", d->drawable);
return false;
}
void
pbuffer_destroy(Display * dpy, struct apple_glx_drawable *d)
{
struct apple_glx_pbuffer *pbuf = &d->types.pbuffer;
assert(APPLE_GLX_DRAWABLE_PBUFFER == d->type);
apple_glx_diagnostic("destroying pbuffer for drawable 0x%lx\n",
d->drawable);
apple_cgl.destroy_pbuffer(pbuf->buffer_obj);
XFreePixmap(dpy, pbuf->xid);
}
/* Return true if an error occurred. */
bool
apple_glx_pbuffer_destroy(Display * dpy, GLXPbuffer pbuf)
{
return !apple_glx_drawable_destroy_by_type(dpy, pbuf,
APPLE_GLX_DRAWABLE_PBUFFER);
}
/* Return true if an error occurred. */
bool
apple_glx_pbuffer_create(Display * dpy, GLXFBConfig config,
int width, int height, int *errorcode,
GLXPbuffer * result)
{
struct apple_glx_drawable *d;
struct apple_glx_pbuffer *pbuf = NULL;
CGLError err;
Window root;
int screen;
Pixmap xid;
__GLcontextModes *modes = (__GLcontextModes *) config;
root = DefaultRootWindow(dpy);
screen = DefaultScreen(dpy);
/*
* This pixmap is only used for a persistent XID.
* The XC-MISC extension cleans up XIDs and reuses them transparently,
* so we need to retain a server-side reference.
*/
xid = XCreatePixmap(dpy, root, (unsigned int) 1,
(unsigned int) 1, DefaultDepth(dpy, screen));
if (None == xid) {
*errorcode = BadAlloc;
return true;
}
if (apple_glx_drawable_create(dpy, screen, xid, &d, &callbacks)) {
*errorcode = BadAlloc;
return true;
}
/* The lock is held in d from create onward. */
pbuf = &d->types.pbuffer;
pbuf->xid = xid;
pbuf->width = width;
pbuf->height = height;
err = apple_cgl.create_pbuffer(width, height, GL_TEXTURE_RECTANGLE_EXT,
(modes->alphaBits > 0) ? GL_RGBA : GL_RGB,
0, &pbuf->buffer_obj);
if (kCGLNoError != err) {
d->unlock(d);
d->destroy(d);
*errorcode = BadMatch;
return true;
}
pbuf->fbconfigID = modes->fbconfigID;
pbuf->event_mask = 0;
*result = pbuf->xid;
d->unlock(d);
return false;
}
/* Return true if an error occurred. */
static bool
get_max_size(int *widthresult, int *heightresult)
{
CGLContextObj oldcontext;
GLint ar[2];
oldcontext = apple_cgl.get_current_context();
if (!oldcontext) {
/*
* There is no current context, so we need to make one in order
* to call glGetInteger.
*/
CGLPixelFormatObj pfobj;
CGLError err;
CGLPixelFormatAttribute attr[10];
int c = 0;
GLint vsref = 0;
CGLContextObj newcontext;
attr[c++] = kCGLPFAColorSize;
attr[c++] = 32;
attr[c++] = 0;
err = apple_cgl.choose_pixel_format(attr, &pfobj, &vsref);
if (kCGLNoError != err) {
if (getenv("LIBGL_DIAGNOSTIC")) {
printf("choose_pixel_format error in %s: %s\n", __func__,
apple_cgl.error_string(err));
}
return true;
}
err = apple_cgl.create_context(pfobj, NULL, &newcontext);
if (kCGLNoError != err) {
if (getenv("LIBGL_DIAGNOSTIC")) {
printf("create_context error in %s: %s\n", __func__,
apple_cgl.error_string(err));
}
apple_cgl.destroy_pixel_format(pfobj);
return true;
}
err = apple_cgl.set_current_context(newcontext);
if (kCGLNoError != err) {
printf("set_current_context error in %s: %s\n", __func__,
apple_cgl.error_string(err));
return true;
}
glGetIntegerv(GL_MAX_VIEWPORT_DIMS, ar);
apple_cgl.set_current_context(oldcontext);
apple_cgl.destroy_context(newcontext);
apple_cgl.destroy_pixel_format(pfobj);
}
else {
/* We have a valid context. */
glGetIntegerv(GL_MAX_VIEWPORT_DIMS, ar);
}
*widthresult = ar[0];
*heightresult = ar[1];
return false;
}
bool
apple_glx_pbuffer_query(GLXPbuffer p, int attr, unsigned int *value)
{
bool result = false;
struct apple_glx_drawable *d;
struct apple_glx_pbuffer *pbuf;
d = apple_glx_drawable_find_by_type(p, APPLE_GLX_DRAWABLE_PBUFFER,
APPLE_GLX_DRAWABLE_LOCK);
if (d) {
pbuf = &d->types.pbuffer;
switch (attr) {
case GLX_WIDTH:
*value = pbuf->width;
result = true;
break;
case GLX_HEIGHT:
*value = pbuf->height;
result = true;
break;
case GLX_PRESERVED_CONTENTS:
*value = true;
result = true;
break;
case GLX_LARGEST_PBUFFER:{
int width, height;
if (get_max_size(&width, &height)) {
fprintf(stderr, "internal error: "
"unable to find the largest pbuffer!\n");
}
else {
*value = width;
result = true;
}
}
break;
case GLX_FBCONFIG_ID:
*value = pbuf->fbconfigID;
result = true;
break;
}
d->unlock(d);
}
return result;
}
bool
apple_glx_pbuffer_set_event_mask(GLXDrawable drawable, unsigned long mask)
{
struct apple_glx_drawable *d;
bool result = false;
d = apple_glx_drawable_find_by_type(drawable, APPLE_GLX_DRAWABLE_PBUFFER,
APPLE_GLX_DRAWABLE_LOCK);
if (d) {
d->types.pbuffer.event_mask = mask;
result = true;
d->unlock(d);
}
return result;
}
bool
apple_glx_pbuffer_get_event_mask(GLXDrawable drawable, unsigned long *mask)
{
struct apple_glx_drawable *d;
bool result = false;
d = apple_glx_drawable_find_by_type(drawable, APPLE_GLX_DRAWABLE_PBUFFER,
APPLE_GLX_DRAWABLE_LOCK);
if (d) {
*mask = d->types.pbuffer.event_mask;
result = true;
d->unlock(d);
}
return result;
}