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 apple_glapi_oglfw_viewport_scissor(0, 0, pbuf
->width
, pbuf
->height
);
88 ac
->made_current
= true;
91 apple_glx_diagnostic("made pbuffer drawable 0x%lx current\n", d
->drawable
);
97 pbuffer_destroy(Display
* dpy
, struct apple_glx_drawable
*d
)
99 struct apple_glx_pbuffer
*pbuf
= &d
->types
.pbuffer
;
101 assert(APPLE_GLX_DRAWABLE_PBUFFER
== d
->type
);
103 apple_glx_diagnostic("destroying pbuffer for drawable 0x%lx\n",
106 apple_cgl
.destroy_pbuffer(pbuf
->buffer_obj
);
107 XFreePixmap(dpy
, pbuf
->xid
);
110 /* Return true if an error occurred. */
112 apple_glx_pbuffer_destroy(Display
* dpy
, GLXPbuffer pbuf
)
114 return !apple_glx_drawable_destroy_by_type(dpy
, pbuf
,
115 APPLE_GLX_DRAWABLE_PBUFFER
);
118 /* Return true if an error occurred. */
120 apple_glx_pbuffer_create(Display
* dpy
, GLXFBConfig config
,
121 int width
, int height
, int *errorcode
,
124 struct apple_glx_drawable
*d
;
125 struct apple_glx_pbuffer
*pbuf
= NULL
;
130 struct glx_config
*modes
= (struct glx_config
*) config
;
132 root
= DefaultRootWindow(dpy
);
133 screen
= DefaultScreen(dpy
);
136 * This pixmap is only used for a persistent XID.
137 * The XC-MISC extension cleans up XIDs and reuses them transparently,
138 * so we need to retain a server-side reference.
140 xid
= XCreatePixmap(dpy
, root
, (unsigned int) 1,
141 (unsigned int) 1, DefaultDepth(dpy
, screen
));
144 *errorcode
= BadAlloc
;
148 if (apple_glx_drawable_create(dpy
, screen
, xid
, &d
, &callbacks
)) {
149 *errorcode
= BadAlloc
;
153 /* The lock is held in d from create onward. */
154 pbuf
= &d
->types
.pbuffer
;
158 pbuf
->height
= height
;
160 err
= apple_cgl
.create_pbuffer(width
, height
, GL_TEXTURE_RECTANGLE_EXT
,
161 (modes
->alphaBits
> 0) ? GL_RGBA
: GL_RGB
,
162 0, &pbuf
->buffer_obj
);
164 if (kCGLNoError
!= err
) {
167 *errorcode
= BadMatch
;
171 pbuf
->fbconfigID
= modes
->fbconfigID
;
173 pbuf
->event_mask
= 0;
184 /* Return true if an error occurred. */
186 get_max_size(int *widthresult
, int *heightresult
)
188 CGLContextObj oldcontext
;
191 oldcontext
= apple_cgl
.get_current_context();
195 * There is no current context, so we need to make one in order
196 * to call glGetInteger.
198 CGLPixelFormatObj pfobj
;
200 CGLPixelFormatAttribute attr
[10];
203 CGLContextObj newcontext
;
205 attr
[c
++] = kCGLPFAColorSize
;
209 err
= apple_cgl
.choose_pixel_format(attr
, &pfobj
, &vsref
);
210 if (kCGLNoError
!= err
) {
211 if (getenv("LIBGL_DIAGNOSTIC")) {
212 printf("choose_pixel_format error in %s: %s\n", __func__
,
213 apple_cgl
.error_string(err
));
220 err
= apple_cgl
.create_context(pfobj
, NULL
, &newcontext
);
222 if (kCGLNoError
!= err
) {
223 if (getenv("LIBGL_DIAGNOSTIC")) {
224 printf("create_context error in %s: %s\n", __func__
,
225 apple_cgl
.error_string(err
));
228 apple_cgl
.destroy_pixel_format(pfobj
);
233 err
= apple_cgl
.set_current_context(newcontext
);
235 if (kCGLNoError
!= err
) {
236 printf("set_current_context error in %s: %s\n", __func__
,
237 apple_cgl
.error_string(err
));
242 glGetIntegerv(GL_MAX_VIEWPORT_DIMS
, ar
);
244 apple_cgl
.set_current_context(oldcontext
);
245 apple_cgl
.destroy_context(newcontext
);
246 apple_cgl
.destroy_pixel_format(pfobj
);
249 /* We have a valid context. */
251 glGetIntegerv(GL_MAX_VIEWPORT_DIMS
, ar
);
254 *widthresult
= ar
[0];
255 *heightresult
= ar
[1];
261 apple_glx_pbuffer_query(GLXPbuffer p
, int attr
, unsigned int *value
)
264 struct apple_glx_drawable
*d
;
265 struct apple_glx_pbuffer
*pbuf
;
267 d
= apple_glx_drawable_find_by_type(p
, APPLE_GLX_DRAWABLE_PBUFFER
,
268 APPLE_GLX_DRAWABLE_LOCK
);
271 pbuf
= &d
->types
.pbuffer
;
275 *value
= pbuf
->width
;
280 *value
= pbuf
->height
;
284 case GLX_PRESERVED_CONTENTS
:
289 case GLX_LARGEST_PBUFFER
:{
291 if (get_max_size(&width
, &height
)) {
292 fprintf(stderr
, "internal error: "
293 "unable to find the largest pbuffer!\n");
302 case GLX_FBCONFIG_ID
:
303 *value
= pbuf
->fbconfigID
;
315 apple_glx_pbuffer_set_event_mask(GLXDrawable drawable
, unsigned long mask
)
317 struct apple_glx_drawable
*d
;
320 d
= apple_glx_drawable_find_by_type(drawable
, APPLE_GLX_DRAWABLE_PBUFFER
,
321 APPLE_GLX_DRAWABLE_LOCK
);
324 d
->types
.pbuffer
.event_mask
= mask
;
333 apple_glx_pbuffer_get_event_mask(GLXDrawable drawable
, unsigned long *mask
)
335 struct apple_glx_drawable
*d
;
338 d
= apple_glx_drawable_find_by_type(drawable
, APPLE_GLX_DRAWABLE_PBUFFER
,
339 APPLE_GLX_DRAWABLE_LOCK
);
341 *mask
= d
->types
.pbuffer
.event_mask
;