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.
33 #include "apple_glx.h"
34 #include "glcontextmodes.h"
35 #include "apple_glx_context.h"
36 #include "apple_glx_drawable.h"
37 #include "apple_cgl.h"
39 static bool pbuffer_make_current(struct apple_glx_context
*ac
,
40 struct apple_glx_drawable
*d
);
42 static void pbuffer_destroy(Display
* dpy
, struct apple_glx_drawable
*d
);
44 static struct apple_glx_drawable_callbacks callbacks
= {
45 .type
= APPLE_GLX_DRAWABLE_PBUFFER
,
46 .make_current
= pbuffer_make_current
,
47 .destroy
= pbuffer_destroy
51 /* Return true if an error occurred. */
53 pbuffer_make_current(struct apple_glx_context
*ac
,
54 struct apple_glx_drawable
*d
)
56 struct apple_glx_pbuffer
*pbuf
= &d
->types
.pbuffer
;
59 assert(APPLE_GLX_DRAWABLE_PBUFFER
== d
->type
);
61 cglerr
= apple_cgl
.set_pbuffer(ac
->context_obj
, pbuf
->buffer_obj
, 0, 0, 0);
63 if (kCGLNoError
!= cglerr
) {
64 fprintf(stderr
, "set_pbuffer: %s\n", apple_cgl
.error_string(cglerr
));
68 if (!ac
->made_current
) {
69 glViewport(0, 0, pbuf
->width
, pbuf
->height
);
70 glScissor(0, 0, pbuf
->width
, pbuf
->height
);
71 ac
->made_current
= true;
74 apple_glx_diagnostic("made pbuffer drawable 0x%lx current\n", d
->drawable
);
80 pbuffer_destroy(Display
* dpy
, struct apple_glx_drawable
*d
)
82 struct apple_glx_pbuffer
*pbuf
= &d
->types
.pbuffer
;
84 assert(APPLE_GLX_DRAWABLE_PBUFFER
== d
->type
);
86 apple_glx_diagnostic("destroying pbuffer for drawable 0x%lx\n",
89 apple_cgl
.destroy_pbuffer(pbuf
->buffer_obj
);
90 XFreePixmap(dpy
, pbuf
->xid
);
93 /* Return true if an error occurred. */
95 apple_glx_pbuffer_destroy(Display
* dpy
, GLXPbuffer pbuf
)
97 return !apple_glx_drawable_destroy_by_type(dpy
, pbuf
,
98 APPLE_GLX_DRAWABLE_PBUFFER
);
101 /* Return true if an error occurred. */
103 apple_glx_pbuffer_create(Display
* dpy
, GLXFBConfig config
,
104 int width
, int height
, int *errorcode
,
107 struct apple_glx_drawable
*d
;
108 struct apple_glx_pbuffer
*pbuf
= NULL
;
113 __GLcontextModes
*modes
= (__GLcontextModes
*) config
;
115 root
= DefaultRootWindow(dpy
);
116 screen
= DefaultScreen(dpy
);
119 * This pixmap is only used for a persistent XID.
120 * The XC-MISC extension cleans up XIDs and reuses them transparently,
121 * so we need to retain a server-side reference.
123 xid
= XCreatePixmap(dpy
, root
, (unsigned int) 1,
124 (unsigned int) 1, DefaultDepth(dpy
, screen
));
127 *errorcode
= BadAlloc
;
131 if (apple_glx_drawable_create(dpy
, screen
, xid
, &d
, &callbacks
)) {
132 *errorcode
= BadAlloc
;
136 /* The lock is held in d from create onward. */
137 pbuf
= &d
->types
.pbuffer
;
141 pbuf
->height
= height
;
143 err
= apple_cgl
.create_pbuffer(width
, height
, GL_TEXTURE_RECTANGLE_EXT
,
144 (modes
->alphaBits
> 0) ? GL_RGBA
: GL_RGB
,
145 0, &pbuf
->buffer_obj
);
147 if (kCGLNoError
!= err
) {
150 *errorcode
= BadMatch
;
154 pbuf
->fbconfigID
= modes
->fbconfigID
;
156 pbuf
->event_mask
= 0;
167 /* Return true if an error occurred. */
169 get_max_size(int *widthresult
, int *heightresult
)
171 CGLContextObj oldcontext
;
174 oldcontext
= apple_cgl
.get_current_context();
178 * There is no current context, so we need to make one in order
179 * to call glGetInteger.
181 CGLPixelFormatObj pfobj
;
183 CGLPixelFormatAttribute attr
[10];
186 CGLContextObj newcontext
;
188 attr
[c
++] = kCGLPFAColorSize
;
192 err
= apple_cgl
.choose_pixel_format(attr
, &pfobj
, &vsref
);
193 if (kCGLNoError
!= err
) {
194 if (getenv("LIBGL_DIAGNOSTIC")) {
195 printf("choose_pixel_format error in %s: %s\n", __func__
,
196 apple_cgl
.error_string(err
));
203 err
= apple_cgl
.create_context(pfobj
, NULL
, &newcontext
);
205 if (kCGLNoError
!= err
) {
206 if (getenv("LIBGL_DIAGNOSTIC")) {
207 printf("create_context error in %s: %s\n", __func__
,
208 apple_cgl
.error_string(err
));
211 apple_cgl
.destroy_pixel_format(pfobj
);
216 err
= apple_cgl
.set_current_context(newcontext
);
218 if (kCGLNoError
!= err
) {
219 printf("set_current_context error in %s: %s\n", __func__
,
220 apple_cgl
.error_string(err
));
225 glGetIntegerv(GL_MAX_VIEWPORT_DIMS
, ar
);
227 apple_cgl
.set_current_context(oldcontext
);
228 apple_cgl
.destroy_context(newcontext
);
229 apple_cgl
.destroy_pixel_format(pfobj
);
232 /* We have a valid context. */
234 glGetIntegerv(GL_MAX_VIEWPORT_DIMS
, ar
);
237 *widthresult
= ar
[0];
238 *heightresult
= ar
[1];
244 apple_glx_pbuffer_query(GLXPbuffer p
, int attr
, unsigned int *value
)
247 struct apple_glx_drawable
*d
;
248 struct apple_glx_pbuffer
*pbuf
;
250 d
= apple_glx_drawable_find_by_type(p
, APPLE_GLX_DRAWABLE_PBUFFER
,
251 APPLE_GLX_DRAWABLE_LOCK
);
254 pbuf
= &d
->types
.pbuffer
;
258 *value
= pbuf
->width
;
263 *value
= pbuf
->height
;
267 case GLX_PRESERVED_CONTENTS
:
272 case GLX_LARGEST_PBUFFER
:{
274 if (get_max_size(&width
, &height
)) {
275 fprintf(stderr
, "internal error: "
276 "unable to find the largest pbuffer!\n");
285 case GLX_FBCONFIG_ID
:
286 *value
= pbuf
->fbconfigID
;
298 apple_glx_pbuffer_set_event_mask(GLXDrawable drawable
, unsigned long mask
)
300 struct apple_glx_drawable
*d
;
303 d
= apple_glx_drawable_find_by_type(drawable
, APPLE_GLX_DRAWABLE_PBUFFER
,
304 APPLE_GLX_DRAWABLE_LOCK
);
307 d
->types
.pbuffer
.event_mask
= mask
;
316 apple_glx_pbuffer_get_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
);
324 *mask
= d
->types
.pbuffer
.event_mask
;