apple_glx_pbuffer.c revision f5aa5377a52c50c94b6a6d6d48c0057dbe874692
1/* 2 Copyright (c) 2009 Apple Inc. 3 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: 11 12 The above copyright notice and this permission notice shall be 13 included in all copies or substantial portions of the Software. 14 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. 23 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. 28*/ 29 30#include <stdlib.h> 31#include <pthread.h> 32#include <assert.h> 33#include <GL/glew.h> 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" 39 40static bool pbuffer_make_current(struct apple_glx_context *ac, 41 struct apple_glx_drawable *d); 42 43static void pbuffer_destroy(Display * dpy, struct apple_glx_drawable *d); 44 45static struct apple_glx_drawable_callbacks callbacks = { 46 .type = APPLE_GLX_DRAWABLE_PBUFFER, 47 .make_current = pbuffer_make_current, 48 .destroy = pbuffer_destroy 49}; 50 51 52/* Return true if an error occurred. */ 53bool 54pbuffer_make_current(struct apple_glx_context *ac, 55 struct apple_glx_drawable *d) 56{ 57 struct apple_glx_pbuffer *pbuf = &d->types.pbuffer; 58 CGLError cglerr; 59 60 assert(APPLE_GLX_DRAWABLE_PBUFFER == d->type); 61 62 cglerr = apple_cgl.set_pbuffer(ac->context_obj, pbuf->buffer_obj, 0, 0, 0); 63 64 if (kCGLNoError != cglerr) { 65 fprintf(stderr, "set_pbuffer: %s\n", apple_cgl.error_string(cglerr)); 66 return true; 67 } 68 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; 73 } 74 75 apple_glx_diagnostic("made pbuffer drawable 0x%lx current\n", d->drawable); 76 77 return false; 78} 79 80void 81pbuffer_destroy(Display * dpy, struct apple_glx_drawable *d) 82{ 83 struct apple_glx_pbuffer *pbuf = &d->types.pbuffer; 84 85 assert(APPLE_GLX_DRAWABLE_PBUFFER == d->type); 86 87 apple_glx_diagnostic("destroying pbuffer for drawable 0x%lx\n", 88 d->drawable); 89 90 apple_cgl.destroy_pbuffer(pbuf->buffer_obj); 91 XFreePixmap(dpy, pbuf->xid); 92} 93 94/* Return true if an error occurred. */ 95bool 96apple_glx_pbuffer_destroy(Display * dpy, GLXPbuffer pbuf) 97{ 98 return !apple_glx_drawable_destroy_by_type(dpy, pbuf, 99 APPLE_GLX_DRAWABLE_PBUFFER); 100} 101 102/* Return true if an error occurred. */ 103bool 104apple_glx_pbuffer_create(Display * dpy, GLXFBConfig config, 105 int width, int height, int *errorcode, 106 GLXPbuffer * result) 107{ 108 struct apple_glx_drawable *d; 109 struct apple_glx_pbuffer *pbuf = NULL; 110 CGLError err; 111 Window root; 112 int screen; 113 Pixmap xid; 114 __GLcontextModes *modes = (__GLcontextModes *) config; 115 116 root = DefaultRootWindow(dpy); 117 screen = DefaultScreen(dpy); 118 119 /* 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. 123 */ 124 xid = XCreatePixmap(dpy, root, (unsigned int) 1, 125 (unsigned int) 1, DefaultDepth(dpy, screen)); 126 127 if (None == xid) { 128 *errorcode = BadAlloc; 129 return true; 130 } 131 132 if (apple_glx_drawable_create(dpy, screen, xid, &d, &callbacks)) { 133 *errorcode = BadAlloc; 134 return true; 135 } 136 137 /* The lock is held in d from create onward. */ 138 pbuf = &d->types.pbuffer; 139 140 pbuf->xid = xid; 141 pbuf->width = width; 142 pbuf->height = height; 143 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); 147 148 if (kCGLNoError != err) { 149 d->unlock(d); 150 d->destroy(d); 151 *errorcode = BadMatch; 152 return true; 153 } 154 155 pbuf->fbconfigID = modes->fbconfigID; 156 157 pbuf->event_mask = 0; 158 159 *result = pbuf->xid; 160 161 d->unlock(d); 162 163 return false; 164} 165 166 167 168/* Return true if an error occurred. */ 169static bool 170get_max_size(int *widthresult, int *heightresult) 171{ 172 CGLContextObj oldcontext; 173 GLint ar[2]; 174 175 oldcontext = apple_cgl.get_current_context(); 176 177 if (!oldcontext) { 178 /* 179 * There is no current context, so we need to make one in order 180 * to call glGetInteger. 181 */ 182 CGLPixelFormatObj pfobj; 183 CGLError err; 184 CGLPixelFormatAttribute attr[10]; 185 int c = 0; 186 GLint vsref = 0; 187 CGLContextObj newcontext; 188 189 attr[c++] = kCGLPFAColorSize; 190 attr[c++] = 32; 191 attr[c++] = 0; 192 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)); 198 } 199 200 return true; 201 } 202 203 204 err = apple_cgl.create_context(pfobj, NULL, &newcontext); 205 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)); 210 } 211 212 apple_cgl.destroy_pixel_format(pfobj); 213 214 return true; 215 } 216 217 err = apple_cgl.set_current_context(newcontext); 218 219 if (kCGLNoError != err) { 220 printf("set_current_context error in %s: %s\n", __func__, 221 apple_cgl.error_string(err)); 222 return true; 223 } 224 225 226 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, ar); 227 228 apple_cgl.set_current_context(oldcontext); 229 apple_cgl.destroy_context(newcontext); 230 apple_cgl.destroy_pixel_format(pfobj); 231 } 232 else { 233 /* We have a valid context. */ 234 235 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, ar); 236 } 237 238 *widthresult = ar[0]; 239 *heightresult = ar[1]; 240 241 return false; 242} 243 244bool 245apple_glx_pbuffer_query(GLXPbuffer p, int attr, unsigned int *value) 246{ 247 bool result = false; 248 struct apple_glx_drawable *d; 249 struct apple_glx_pbuffer *pbuf; 250 251 d = apple_glx_drawable_find_by_type(p, APPLE_GLX_DRAWABLE_PBUFFER, 252 APPLE_GLX_DRAWABLE_LOCK); 253 254 if (d) { 255 pbuf = &d->types.pbuffer; 256 257 switch (attr) { 258 case GLX_WIDTH: 259 *value = pbuf->width; 260 result = true; 261 break; 262 263 case GLX_HEIGHT: 264 *value = pbuf->height; 265 result = true; 266 break; 267 268 case GLX_PRESERVED_CONTENTS: 269 *value = true; 270 result = true; 271 break; 272 273 case GLX_LARGEST_PBUFFER:{ 274 int width, height; 275 if (get_max_size(&width, &height)) { 276 fprintf(stderr, "internal error: " 277 "unable to find the largest pbuffer!\n"); 278 } 279 else { 280 *value = width; 281 result = true; 282 } 283 } 284 break; 285 286 case GLX_FBCONFIG_ID: 287 *value = pbuf->fbconfigID; 288 result = true; 289 break; 290 } 291 292 d->unlock(d); 293 } 294 295 return result; 296} 297 298bool 299apple_glx_pbuffer_set_event_mask(GLXDrawable drawable, unsigned long mask) 300{ 301 struct apple_glx_drawable *d; 302 bool result = false; 303 304 d = apple_glx_drawable_find_by_type(drawable, APPLE_GLX_DRAWABLE_PBUFFER, 305 APPLE_GLX_DRAWABLE_LOCK); 306 307 if (d) { 308 d->types.pbuffer.event_mask = mask; 309 result = true; 310 d->unlock(d); 311 } 312 313 return result; 314} 315 316bool 317apple_glx_pbuffer_get_event_mask(GLXDrawable drawable, unsigned long *mask) 318{ 319 struct apple_glx_drawable *d; 320 bool result = false; 321 322 d = apple_glx_drawable_find_by_type(drawable, APPLE_GLX_DRAWABLE_PBUFFER, 323 APPLE_GLX_DRAWABLE_LOCK); 324 if (d) { 325 *mask = d->types.pbuffer.event_mask; 326 result = true; 327 d->unlock(d); 328 } 329 330 return result; 331} 332