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"
46 #include "apple_glx.h"
47 #include "glxconfig.h"
48 #include "apple_cgl.h"
50 /* mesa defines in glew.h, Apple in glext.h.
51 * Due to namespace nightmares, just do it here.
53 #ifndef GL_TEXTURE_RECTANGLE_EXT
54 #define GL_TEXTURE_RECTANGLE_EXT 0x84F5
57 static bool pbuffer_make_current(struct apple_glx_context
*ac
,
58 struct apple_glx_drawable
*d
);
60 static void pbuffer_destroy(Display
* dpy
, struct apple_glx_drawable
*d
);
62 static struct apple_glx_drawable_callbacks callbacks
= {
63 .type
= APPLE_GLX_DRAWABLE_PBUFFER
,
64 .make_current
= pbuffer_make_current
,
65 .destroy
= pbuffer_destroy
69 /* Return true if an error occurred. */
71 pbuffer_make_current(struct apple_glx_context
*ac
,
72 struct apple_glx_drawable
*d
)
74 struct apple_glx_pbuffer
*pbuf
= &d
->types
.pbuffer
;
77 assert(APPLE_GLX_DRAWABLE_PBUFFER
== d
->type
);
79 cglerr
= apple_cgl
.set_pbuffer(ac
->context_obj
, pbuf
->buffer_obj
, 0, 0, 0);
81 if (kCGLNoError
!= cglerr
) {
82 fprintf(stderr
, "set_pbuffer: %s\n", apple_cgl
.error_string(cglerr
));
86 if (!ac
->made_current
) {
87 glViewport(0, 0, pbuf
->width
, pbuf
->height
);
88 glScissor(0, 0, pbuf
->width
, pbuf
->height
);
89 ac
->made_current
= true;
92 apple_glx_diagnostic("made pbuffer drawable 0x%lx current\n", d
->drawable
);
98 pbuffer_destroy(Display
* dpy
, struct apple_glx_drawable
*d
)
100 struct apple_glx_pbuffer
*pbuf
= &d
->types
.pbuffer
;
102 assert(APPLE_GLX_DRAWABLE_PBUFFER
== d
->type
);
104 apple_glx_diagnostic("destroying pbuffer for drawable 0x%lx\n",
107 apple_cgl
.destroy_pbuffer(pbuf
->buffer_obj
);
108 XFreePixmap(dpy
, pbuf
->xid
);
111 /* Return true if an error occurred. */
113 apple_glx_pbuffer_destroy(Display
* dpy
, GLXPbuffer pbuf
)
115 return !apple_glx_drawable_destroy_by_type(dpy
, pbuf
,
116 APPLE_GLX_DRAWABLE_PBUFFER
);
119 /* Return true if an error occurred. */
121 apple_glx_pbuffer_create(Display
* dpy
, GLXFBConfig config
,
122 int width
, int height
, int *errorcode
,
125 struct apple_glx_drawable
*d
;
126 struct apple_glx_pbuffer
*pbuf
= NULL
;
131 struct glx_config
*modes
= (struct glx_config
*) config
;
133 root
= DefaultRootWindow(dpy
);
134 screen
= DefaultScreen(dpy
);
137 * This pixmap is only used for a persistent XID.
138 * The XC-MISC extension cleans up XIDs and reuses them transparently,
139 * so we need to retain a server-side reference.
141 xid
= XCreatePixmap(dpy
, root
, (unsigned int) 1,
142 (unsigned int) 1, DefaultDepth(dpy
, screen
));
145 *errorcode
= BadAlloc
;
149 if (apple_glx_drawable_create(dpy
, screen
, xid
, &d
, &callbacks
)) {
150 *errorcode
= BadAlloc
;
154 /* The lock is held in d from create onward. */
155 pbuf
= &d
->types
.pbuffer
;
159 pbuf
->height
= height
;
161 err
= apple_cgl
.create_pbuffer(width
, height
, GL_TEXTURE_RECTANGLE_EXT
,
162 (modes
->alphaBits
> 0) ? GL_RGBA
: GL_RGB
,
163 0, &pbuf
->buffer_obj
);
165 if (kCGLNoError
!= err
) {
168 *errorcode
= BadMatch
;
172 pbuf
->fbconfigID
= modes
->fbconfigID
;
174 pbuf
->event_mask
= 0;
185 /* Return true if an error occurred. */
187 get_max_size(int *widthresult
, int *heightresult
)
189 CGLContextObj oldcontext
;
192 oldcontext
= apple_cgl
.get_current_context();
196 * There is no current context, so we need to make one in order
197 * to call glGetInteger.
199 CGLPixelFormatObj pfobj
;
201 CGLPixelFormatAttribute attr
[10];
204 CGLContextObj newcontext
;
206 attr
[c
++] = kCGLPFAColorSize
;
210 err
= apple_cgl
.choose_pixel_format(attr
, &pfobj
, &vsref
);
211 if (kCGLNoError
!= err
) {
212 if (getenv("LIBGL_DIAGNOSTIC")) {
213 printf("choose_pixel_format error in %s: %s\n", __func__
,
214 apple_cgl
.error_string(err
));
221 err
= apple_cgl
.create_context(pfobj
, NULL
, &newcontext
);
223 if (kCGLNoError
!= err
) {
224 if (getenv("LIBGL_DIAGNOSTIC")) {
225 printf("create_context error in %s: %s\n", __func__
,
226 apple_cgl
.error_string(err
));
229 apple_cgl
.destroy_pixel_format(pfobj
);
234 err
= apple_cgl
.set_current_context(newcontext
);
236 if (kCGLNoError
!= err
) {
237 printf("set_current_context error in %s: %s\n", __func__
,
238 apple_cgl
.error_string(err
));
243 glGetIntegerv(GL_MAX_VIEWPORT_DIMS
, ar
);
245 apple_cgl
.set_current_context(oldcontext
);
246 apple_cgl
.destroy_context(newcontext
);
247 apple_cgl
.destroy_pixel_format(pfobj
);
250 /* We have a valid context. */
252 glGetIntegerv(GL_MAX_VIEWPORT_DIMS
, ar
);
255 *widthresult
= ar
[0];
256 *heightresult
= ar
[1];
262 apple_glx_pbuffer_query(GLXPbuffer p
, int attr
, unsigned int *value
)
265 struct apple_glx_drawable
*d
;
266 struct apple_glx_pbuffer
*pbuf
;
268 d
= apple_glx_drawable_find_by_type(p
, APPLE_GLX_DRAWABLE_PBUFFER
,
269 APPLE_GLX_DRAWABLE_LOCK
);
272 pbuf
= &d
->types
.pbuffer
;
276 *value
= pbuf
->width
;
281 *value
= pbuf
->height
;
285 case GLX_PRESERVED_CONTENTS
:
290 case GLX_LARGEST_PBUFFER
:{
292 if (get_max_size(&width
, &height
)) {
293 fprintf(stderr
, "internal error: "
294 "unable to find the largest pbuffer!\n");
303 case GLX_FBCONFIG_ID
:
304 *value
= pbuf
->fbconfigID
;
316 apple_glx_pbuffer_set_event_mask(GLXDrawable drawable
, unsigned long mask
)
318 struct apple_glx_drawable
*d
;
321 d
= apple_glx_drawable_find_by_type(drawable
, APPLE_GLX_DRAWABLE_PBUFFER
,
322 APPLE_GLX_DRAWABLE_LOCK
);
325 d
->types
.pbuffer
.event_mask
= mask
;
334 apple_glx_pbuffer_get_event_mask(GLXDrawable drawable
, unsigned long *mask
)
336 struct apple_glx_drawable
*d
;
339 d
= apple_glx_drawable_find_by_type(drawable
, APPLE_GLX_DRAWABLE_PBUFFER
,
340 APPLE_GLX_DRAWABLE_LOCK
);
342 *mask
= d
->types
.pbuffer
.event_mask
;