2
Copyright (c) 2009 Apple Inc.
4
Permission is hereby granted, free of charge, to any person
5
obtaining a copy of this software and associated documentation files
6
(the "Software"), to deal in the Software without restriction,
7
including without limitation the rights to use, copy, modify, merge,
8
publish, distribute, sublicense, and/or sell copies of the Software,
9
and to permit persons to whom the Software is furnished to do so,
10
subject to the following conditions:
12
The above copyright notice and this permission notice shall be
13
included in all copies or substantial portions of the Software.
15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
19
HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
DEALINGS IN THE SOFTWARE.
24
Except as contained in this notice, the name(s) of the above
25
copyright holders shall not be used in advertising or otherwise to
26
promote the sale, use or other dealings in this Software without
27
prior written authorization.
30
/* Must be before OpenGL.framework is included. Remove once fixed:
31
* <rdar://problem/7872773>
35
#define __gltypes_h_ 1
38
* <rdar://problem/6953344>
40
#include "apple_glx_context.h"
41
#include "apple_glx_drawable.h"
47
#include "glxclient.h"
48
#include "apple_glx.h"
49
#include "glxconfig.h"
50
#include "apple_cgl.h"
51
#include "util/debug.h"
53
/* mesa defines in glew.h, Apple in glext.h.
54
* Due to namespace nightmares, just do it here.
56
#ifndef GL_TEXTURE_RECTANGLE_EXT
57
#define GL_TEXTURE_RECTANGLE_EXT 0x84F5
60
static bool pbuffer_make_current(struct apple_glx_context *ac,
61
struct apple_glx_drawable *d);
63
static void pbuffer_destroy(Display * dpy, struct apple_glx_drawable *d);
65
static struct apple_glx_drawable_callbacks callbacks = {
66
.type = APPLE_GLX_DRAWABLE_PBUFFER,
67
.make_current = pbuffer_make_current,
68
.destroy = pbuffer_destroy
72
/* Return true if an error occurred. */
74
pbuffer_make_current(struct apple_glx_context *ac,
75
struct apple_glx_drawable *d)
77
struct apple_glx_pbuffer *pbuf = &d->types.pbuffer;
80
assert(APPLE_GLX_DRAWABLE_PBUFFER == d->type);
82
cglerr = apple_cgl.set_pbuffer(ac->context_obj, pbuf->buffer_obj, 0, 0, 0);
84
if (kCGLNoError != cglerr) {
85
fprintf(stderr, "set_pbuffer: %s\n", apple_cgl.error_string(cglerr));
89
if (!ac->made_current) {
90
apple_glapi_oglfw_viewport_scissor(0, 0, pbuf->width, pbuf->height);
91
ac->made_current = true;
94
apple_glx_diagnostic("made pbuffer drawable 0x%lx current\n", d->drawable);
100
pbuffer_destroy(Display * dpy, struct apple_glx_drawable *d)
102
struct apple_glx_pbuffer *pbuf = &d->types.pbuffer;
104
assert(APPLE_GLX_DRAWABLE_PBUFFER == d->type);
106
apple_glx_diagnostic("destroying pbuffer for drawable 0x%lx\n",
109
apple_cgl.destroy_pbuffer(pbuf->buffer_obj);
110
XFreePixmap(dpy, pbuf->xid);
113
/* Return true if an error occurred. */
115
apple_glx_pbuffer_destroy(Display * dpy, GLXPbuffer pbuf)
117
return !apple_glx_drawable_destroy_by_type(dpy, pbuf,
118
APPLE_GLX_DRAWABLE_PBUFFER);
121
/* Return true if an error occurred. */
123
apple_glx_pbuffer_create(Display * dpy, GLXFBConfig config,
124
int width, int height, int *errorcode,
127
struct apple_glx_drawable *d;
128
struct apple_glx_pbuffer *pbuf = NULL;
133
struct glx_config *modes = (struct glx_config *) config;
135
root = DefaultRootWindow(dpy);
136
screen = DefaultScreen(dpy);
139
* This pixmap is only used for a persistent XID.
140
* The XC-MISC extension cleans up XIDs and reuses them transparently,
141
* so we need to retain a server-side reference.
143
xid = XCreatePixmap(dpy, root, (unsigned int) 1,
144
(unsigned int) 1, DefaultDepth(dpy, screen));
147
*errorcode = BadAlloc;
151
if (apple_glx_drawable_create(dpy, screen, xid, &d, &callbacks)) {
152
*errorcode = BadAlloc;
156
/* The lock is held in d from create onward. */
157
pbuf = &d->types.pbuffer;
161
pbuf->height = height;
163
err = apple_cgl.create_pbuffer(width, height, GL_TEXTURE_RECTANGLE_EXT,
164
(modes->alphaBits > 0) ? GL_RGBA : GL_RGB,
165
0, &pbuf->buffer_obj);
167
if (kCGLNoError != err) {
170
*errorcode = BadMatch;
174
pbuf->fbconfigID = modes->fbconfigID;
176
pbuf->event_mask = 0;
187
/* Return true if an error occurred. */
189
get_max_size(int *widthresult, int *heightresult)
191
CGLContextObj oldcontext;
194
oldcontext = apple_cgl.get_current_context();
198
* There is no current context, so we need to make one in order
199
* to call glGetInteger.
201
CGLPixelFormatObj pfobj;
203
CGLPixelFormatAttribute attr[10];
206
CGLContextObj newcontext;
208
attr[c++] = kCGLPFAColorSize;
212
err = apple_cgl.choose_pixel_format(attr, &pfobj, &vsref);
213
if (kCGLNoError != err) {
214
DebugMessageF("choose_pixel_format error in %s: %s\n", __func__,
215
apple_cgl.error_string(err));
221
err = apple_cgl.create_context(pfobj, NULL, &newcontext);
223
if (kCGLNoError != err) {
224
DebugMessageF("create_context error in %s: %s\n", __func__,
225
apple_cgl.error_string(err));
227
apple_cgl.destroy_pixel_format(pfobj);
232
err = apple_cgl.set_current_context(newcontext);
234
if (kCGLNoError != err) {
235
DebugMessageF("set_current_context error in %s: %s\n", __func__,
236
apple_cgl.error_string(err));
241
glGetIntegerv(GL_MAX_VIEWPORT_DIMS, ar);
243
apple_cgl.set_current_context(oldcontext);
244
apple_cgl.destroy_context(newcontext);
245
apple_cgl.destroy_pixel_format(pfobj);
248
/* We have a valid context. */
250
glGetIntegerv(GL_MAX_VIEWPORT_DIMS, ar);
253
*widthresult = ar[0];
254
*heightresult = ar[1];
260
apple_glx_pbuffer_query(GLXPbuffer p, int attr, unsigned int *value)
263
struct apple_glx_drawable *d;
264
struct apple_glx_pbuffer *pbuf;
266
d = apple_glx_drawable_find_by_type(p, APPLE_GLX_DRAWABLE_PBUFFER,
267
APPLE_GLX_DRAWABLE_LOCK);
270
pbuf = &d->types.pbuffer;
274
*value = pbuf->width;
279
*value = pbuf->height;
283
case GLX_PRESERVED_CONTENTS:
288
case GLX_LARGEST_PBUFFER:{
290
if (get_max_size(&width, &height)) {
291
fprintf(stderr, "internal error: "
292
"unable to find the largest pbuffer!\n");
301
case GLX_FBCONFIG_ID:
302
*value = pbuf->fbconfigID;
314
apple_glx_pbuffer_set_event_mask(GLXDrawable drawable, unsigned long mask)
316
struct apple_glx_drawable *d;
319
d = apple_glx_drawable_find_by_type(drawable, APPLE_GLX_DRAWABLE_PBUFFER,
320
APPLE_GLX_DRAWABLE_LOCK);
323
d->types.pbuffer.event_mask = mask;
332
apple_glx_pbuffer_get_event_mask(GLXDrawable drawable, unsigned long *mask)
334
struct apple_glx_drawable *d;
337
d = apple_glx_drawable_find_by_type(drawable, APPLE_GLX_DRAWABLE_PBUFFER,
338
APPLE_GLX_DRAWABLE_LOCK);
340
*mask = d->types.pbuffer.event_mask;