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.
34 #include "apple_glx.h"
35 #include "glcontextmodes.h"
36 #include "apple_glx_context.h"
37 #include "apple_glx_drawable.h"
38 #include "apple_cgl.h"
40 static bool pbuffer_make_current(struct apple_glx_context
*ac
,
41 struct apple_glx_drawable
*d
);
43 static void pbuffer_destroy(Display
* dpy
, struct apple_glx_drawable
*d
);
45 static struct apple_glx_drawable_callbacks callbacks
= {
46 .type
= APPLE_GLX_DRAWABLE_PBUFFER
,
47 .make_current
= pbuffer_make_current
,
48 .destroy
= pbuffer_destroy
52 /* Return true if an error occurred. */
54 pbuffer_make_current(struct apple_glx_context
*ac
,
55 struct apple_glx_drawable
*d
)
57 struct apple_glx_pbuffer
*pbuf
= &d
->types
.pbuffer
;
60 assert(APPLE_GLX_DRAWABLE_PBUFFER
== d
->type
);
62 cglerr
= apple_cgl
.set_pbuffer(ac
->context_obj
, pbuf
->buffer_obj
, 0, 0, 0);
64 if (kCGLNoError
!= cglerr
) {
65 fprintf(stderr
, "set_pbuffer: %s\n", apple_cgl
.error_string(cglerr
));
69 if (!ac
->made_current
) {
70 glViewport(0, 0, pbuf
->width
, pbuf
->height
);
71 glScissor(0, 0, pbuf
->width
, pbuf
->height
);
72 ac
->made_current
= true;
75 apple_glx_diagnostic("made pbuffer drawable 0x%lx current\n", d
->drawable
);
81 pbuffer_destroy(Display
* dpy
, struct apple_glx_drawable
*d
)
83 struct apple_glx_pbuffer
*pbuf
= &d
->types
.pbuffer
;
85 assert(APPLE_GLX_DRAWABLE_PBUFFER
== d
->type
);
87 apple_glx_diagnostic("destroying pbuffer for drawable 0x%lx\n",
90 apple_cgl
.destroy_pbuffer(pbuf
->buffer_obj
);
91 XFreePixmap(dpy
, pbuf
->xid
);
94 /* Return true if an error occurred. */
96 apple_glx_pbuffer_destroy(Display
* dpy
, GLXPbuffer pbuf
)
98 return !apple_glx_drawable_destroy_by_type(dpy
, pbuf
,
99 APPLE_GLX_DRAWABLE_PBUFFER
);
102 /* Return true if an error occurred. */
104 apple_glx_pbuffer_create(Display
* dpy
, GLXFBConfig config
,
105 int width
, int height
, int *errorcode
,
108 struct apple_glx_drawable
*d
;
109 struct apple_glx_pbuffer
*pbuf
= NULL
;
114 __GLcontextModes
*modes
= (__GLcontextModes
*) config
;
116 root
= DefaultRootWindow(dpy
);
117 screen
= DefaultScreen(dpy
);
120 * This pixmap is only used for a persistent XID.
121 * The XC-MISC extension cleans up XIDs and reuses them transparently,
122 * so we need to retain a server-side reference.
124 xid
= XCreatePixmap(dpy
, root
, (unsigned int) 1,
125 (unsigned int) 1, DefaultDepth(dpy
, screen
));
128 *errorcode
= BadAlloc
;
132 if (apple_glx_drawable_create(dpy
, screen
, xid
, &d
, &callbacks
)) {
133 *errorcode
= BadAlloc
;
137 /* The lock is held in d from create onward. */
138 pbuf
= &d
->types
.pbuffer
;
142 pbuf
->height
= height
;
144 err
= apple_cgl
.create_pbuffer(width
, height
, GL_TEXTURE_RECTANGLE_EXT
,
145 (modes
->alphaBits
> 0) ? GL_RGBA
: GL_RGB
,
146 0, &pbuf
->buffer_obj
);
148 if (kCGLNoError
!= err
) {
151 *errorcode
= BadMatch
;
155 pbuf
->fbconfigID
= modes
->fbconfigID
;
157 pbuf
->event_mask
= 0;
168 /* Return true if an error occurred. */
170 get_max_size(int *widthresult
, int *heightresult
)
172 CGLContextObj oldcontext
;
175 oldcontext
= apple_cgl
.get_current_context();
179 * There is no current context, so we need to make one in order
180 * to call glGetInteger.
182 CGLPixelFormatObj pfobj
;
184 CGLPixelFormatAttribute attr
[10];
187 CGLContextObj newcontext
;
189 attr
[c
++] = kCGLPFAColorSize
;
193 err
= apple_cgl
.choose_pixel_format(attr
, &pfobj
, &vsref
);
194 if (kCGLNoError
!= err
) {
195 if (getenv("LIBGL_DIAGNOSTIC")) {
196 printf("choose_pixel_format error in %s: %s\n", __func__
,
197 apple_cgl
.error_string(err
));
204 err
= apple_cgl
.create_context(pfobj
, NULL
, &newcontext
);
206 if (kCGLNoError
!= err
) {
207 if (getenv("LIBGL_DIAGNOSTIC")) {
208 printf("create_context error in %s: %s\n", __func__
,
209 apple_cgl
.error_string(err
));
212 apple_cgl
.destroy_pixel_format(pfobj
);
217 err
= apple_cgl
.set_current_context(newcontext
);
219 if (kCGLNoError
!= err
) {
220 printf("set_current_context error in %s: %s\n", __func__
,
221 apple_cgl
.error_string(err
));
226 glGetIntegerv(GL_MAX_VIEWPORT_DIMS
, ar
);
228 apple_cgl
.set_current_context(oldcontext
);
229 apple_cgl
.destroy_context(newcontext
);
230 apple_cgl
.destroy_pixel_format(pfobj
);
233 /* We have a valid context. */
235 glGetIntegerv(GL_MAX_VIEWPORT_DIMS
, ar
);
238 *widthresult
= ar
[0];
239 *heightresult
= ar
[1];
245 apple_glx_pbuffer_query(GLXPbuffer p
, int attr
, unsigned int *value
)
248 struct apple_glx_drawable
*d
;
249 struct apple_glx_pbuffer
*pbuf
;
251 d
= apple_glx_drawable_find_by_type(p
, APPLE_GLX_DRAWABLE_PBUFFER
,
252 APPLE_GLX_DRAWABLE_LOCK
);
255 pbuf
= &d
->types
.pbuffer
;
259 *value
= pbuf
->width
;
264 *value
= pbuf
->height
;
268 case GLX_PRESERVED_CONTENTS
:
273 case GLX_LARGEST_PBUFFER
:{
275 if (get_max_size(&width
, &height
)) {
276 fprintf(stderr
, "internal error: "
277 "unable to find the largest pbuffer!\n");
286 case GLX_FBCONFIG_ID
:
287 *value
= pbuf
->fbconfigID
;
299 apple_glx_pbuffer_set_event_mask(GLXDrawable drawable
, unsigned long mask
)
301 struct apple_glx_drawable
*d
;
304 d
= apple_glx_drawable_find_by_type(drawable
, APPLE_GLX_DRAWABLE_PBUFFER
,
305 APPLE_GLX_DRAWABLE_LOCK
);
308 d
->types
.pbuffer
.event_mask
= mask
;
317 apple_glx_pbuffer_get_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
);
325 *mask
= d
->types
.pbuffer
.event_mask
;