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 "apple_glx.h"
48 #include "glxconfig.h"
49 #include "apple_cgl.h"
50 #include "util/debug.h"
52 /* mesa defines in glew.h, Apple in glext.h.
53 * Due to namespace nightmares, just do it here.
55 #ifndef GL_TEXTURE_RECTANGLE_EXT
56 #define GL_TEXTURE_RECTANGLE_EXT 0x84F5
59 static bool pbuffer_make_current(struct apple_glx_context
*ac
,
60 struct apple_glx_drawable
*d
);
62 static void pbuffer_destroy(Display
* dpy
, struct apple_glx_drawable
*d
);
64 static struct apple_glx_drawable_callbacks callbacks
= {
65 .type
= APPLE_GLX_DRAWABLE_PBUFFER
,
66 .make_current
= pbuffer_make_current
,
67 .destroy
= pbuffer_destroy
71 /* Return true if an error occurred. */
73 pbuffer_make_current(struct apple_glx_context
*ac
,
74 struct apple_glx_drawable
*d
)
76 struct apple_glx_pbuffer
*pbuf
= &d
->types
.pbuffer
;
79 assert(APPLE_GLX_DRAWABLE_PBUFFER
== d
->type
);
81 cglerr
= apple_cgl
.set_pbuffer(ac
->context_obj
, pbuf
->buffer_obj
, 0, 0, 0);
83 if (kCGLNoError
!= cglerr
) {
84 fprintf(stderr
, "set_pbuffer: %s\n", apple_cgl
.error_string(cglerr
));
88 if (!ac
->made_current
) {
89 apple_glapi_oglfw_viewport_scissor(0, 0, pbuf
->width
, pbuf
->height
);
90 ac
->made_current
= true;
93 apple_glx_diagnostic("made pbuffer drawable 0x%lx current\n", d
->drawable
);
99 pbuffer_destroy(Display
* dpy
, struct apple_glx_drawable
*d
)
101 struct apple_glx_pbuffer
*pbuf
= &d
->types
.pbuffer
;
103 assert(APPLE_GLX_DRAWABLE_PBUFFER
== d
->type
);
105 apple_glx_diagnostic("destroying pbuffer for drawable 0x%lx\n",
108 apple_cgl
.destroy_pbuffer(pbuf
->buffer_obj
);
109 XFreePixmap(dpy
, pbuf
->xid
);
112 /* Return true if an error occurred. */
114 apple_glx_pbuffer_destroy(Display
* dpy
, GLXPbuffer pbuf
)
116 return !apple_glx_drawable_destroy_by_type(dpy
, pbuf
,
117 APPLE_GLX_DRAWABLE_PBUFFER
);
120 /* Return true if an error occurred. */
122 apple_glx_pbuffer_create(Display
* dpy
, GLXFBConfig config
,
123 int width
, int height
, int *errorcode
,
126 struct apple_glx_drawable
*d
;
127 struct apple_glx_pbuffer
*pbuf
= NULL
;
132 struct glx_config
*modes
= (struct glx_config
*) config
;
134 root
= DefaultRootWindow(dpy
);
135 screen
= DefaultScreen(dpy
);
138 * This pixmap is only used for a persistent XID.
139 * The XC-MISC extension cleans up XIDs and reuses them transparently,
140 * so we need to retain a server-side reference.
142 xid
= XCreatePixmap(dpy
, root
, (unsigned int) 1,
143 (unsigned int) 1, DefaultDepth(dpy
, screen
));
146 *errorcode
= BadAlloc
;
150 if (apple_glx_drawable_create(dpy
, screen
, xid
, &d
, &callbacks
)) {
151 *errorcode
= BadAlloc
;
155 /* The lock is held in d from create onward. */
156 pbuf
= &d
->types
.pbuffer
;
160 pbuf
->height
= height
;
162 err
= apple_cgl
.create_pbuffer(width
, height
, GL_TEXTURE_RECTANGLE_EXT
,
163 (modes
->alphaBits
> 0) ? GL_RGBA
: GL_RGB
,
164 0, &pbuf
->buffer_obj
);
166 if (kCGLNoError
!= err
) {
169 *errorcode
= BadMatch
;
173 pbuf
->fbconfigID
= modes
->fbconfigID
;
175 pbuf
->event_mask
= 0;
186 /* Return true if an error occurred. */
188 get_max_size(int *widthresult
, int *heightresult
)
190 CGLContextObj oldcontext
;
193 oldcontext
= apple_cgl
.get_current_context();
197 * There is no current context, so we need to make one in order
198 * to call glGetInteger.
200 CGLPixelFormatObj pfobj
;
202 CGLPixelFormatAttribute attr
[10];
205 CGLContextObj newcontext
;
207 attr
[c
++] = kCGLPFAColorSize
;
211 err
= apple_cgl
.choose_pixel_format(attr
, &pfobj
, &vsref
);
212 if (kCGLNoError
!= err
) {
213 if (env_var_as_boolean("LIBGL_DIAGNOSTIC", false)) {
214 printf("choose_pixel_format error in %s: %s\n", __func__
,
215 apple_cgl
.error_string(err
));
222 err
= apple_cgl
.create_context(pfobj
, NULL
, &newcontext
);
224 if (kCGLNoError
!= err
) {
225 if (env_var_as_boolean("LIBGL_DIAGNOSTIC", false)) {
226 printf("create_context error in %s: %s\n", __func__
,
227 apple_cgl
.error_string(err
));
230 apple_cgl
.destroy_pixel_format(pfobj
);
235 err
= apple_cgl
.set_current_context(newcontext
);
237 if (kCGLNoError
!= err
) {
238 printf("set_current_context error in %s: %s\n", __func__
,
239 apple_cgl
.error_string(err
));
244 glGetIntegerv(GL_MAX_VIEWPORT_DIMS
, ar
);
246 apple_cgl
.set_current_context(oldcontext
);
247 apple_cgl
.destroy_context(newcontext
);
248 apple_cgl
.destroy_pixel_format(pfobj
);
251 /* We have a valid context. */
253 glGetIntegerv(GL_MAX_VIEWPORT_DIMS
, ar
);
256 *widthresult
= ar
[0];
257 *heightresult
= ar
[1];
263 apple_glx_pbuffer_query(GLXPbuffer p
, int attr
, unsigned int *value
)
266 struct apple_glx_drawable
*d
;
267 struct apple_glx_pbuffer
*pbuf
;
269 d
= apple_glx_drawable_find_by_type(p
, APPLE_GLX_DRAWABLE_PBUFFER
,
270 APPLE_GLX_DRAWABLE_LOCK
);
273 pbuf
= &d
->types
.pbuffer
;
277 *value
= pbuf
->width
;
282 *value
= pbuf
->height
;
286 case GLX_PRESERVED_CONTENTS
:
291 case GLX_LARGEST_PBUFFER
:{
293 if (get_max_size(&width
, &height
)) {
294 fprintf(stderr
, "internal error: "
295 "unable to find the largest pbuffer!\n");
304 case GLX_FBCONFIG_ID
:
305 *value
= pbuf
->fbconfigID
;
317 apple_glx_pbuffer_set_event_mask(GLXDrawable drawable
, unsigned long mask
)
319 struct apple_glx_drawable
*d
;
322 d
= apple_glx_drawable_find_by_type(drawable
, APPLE_GLX_DRAWABLE_PBUFFER
,
323 APPLE_GLX_DRAWABLE_LOCK
);
326 d
->types
.pbuffer
.event_mask
= mask
;
335 apple_glx_pbuffer_get_event_mask(GLXDrawable drawable
, unsigned long *mask
)
337 struct apple_glx_drawable
*d
;
340 d
= apple_glx_drawable_find_by_type(drawable
, APPLE_GLX_DRAWABLE_PBUFFER
,
341 APPLE_GLX_DRAWABLE_LOCK
);
343 *mask
= d
->types
.pbuffer
.event_mask
;