apple_glx_pixmap.c revision ad503c41557606d15b0420c824369456f6d20a8f
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 <stdio.h> 31#include <stdlib.h> 32#include <pthread.h> 33#include <fcntl.h> 34#include <sys/types.h> 35#include <sys/mman.h> 36#include <unistd.h> 37#include <assert.h> 38#include "apple_glx.h" 39#include "apple_cgl.h" 40#include "apple_visual.h" 41#include "apple_glx_drawable.h" 42#include "appledri.h" 43#include "glcontextmodes.h" 44 45static bool pixmap_make_current(struct apple_glx_context *ac, 46 struct apple_glx_drawable *d); 47 48static void pixmap_destroy(Display * dpy, struct apple_glx_drawable *d); 49 50static struct apple_glx_drawable_callbacks callbacks = { 51 .type = APPLE_GLX_DRAWABLE_PIXMAP, 52 .make_current = pixmap_make_current, 53 .destroy = pixmap_destroy 54}; 55 56static bool 57pixmap_make_current(struct apple_glx_context *ac, 58 struct apple_glx_drawable *d) 59{ 60 CGLError cglerr; 61 struct apple_glx_pixmap *p = &d->types.pixmap; 62 63 assert(APPLE_GLX_DRAWABLE_PIXMAP == d->type); 64 65 cglerr = apple_cgl.set_current_context(p->context_obj); 66 67 if (kCGLNoError != cglerr) { 68 fprintf(stderr, "set current context: %s\n", 69 apple_cgl.error_string(cglerr)); 70 return true; 71 } 72 73 cglerr = apple_cgl.set_off_screen(p->context_obj, p->width, p->height, 74 p->pitch, p->buffer); 75 76 if (kCGLNoError != cglerr) { 77 fprintf(stderr, "set off screen: %s\n", apple_cgl.error_string(cglerr)); 78 79 return true; 80 } 81 82 if (!ac->made_current) { 83 glViewport(0, 0, p->width, p->height); 84 glScissor(0, 0, p->width, p->height); 85 ac->made_current = true; 86 } 87 88 return false; 89} 90 91static void 92pixmap_destroy(Display * dpy, struct apple_glx_drawable *d) 93{ 94 struct apple_glx_pixmap *p = &d->types.pixmap; 95 96 if (p->pixel_format_obj) 97 (void) apple_cgl.destroy_pixel_format(p->pixel_format_obj); 98 99 if (p->context_obj) 100 (void) apple_cgl.destroy_context(p->context_obj); 101 102 XAppleDRIDestroyPixmap(dpy, p->xpixmap); 103 104 if (p->buffer) { 105 if (munmap(p->buffer, p->size)) 106 perror("munmap"); 107 108 if (-1 == close(p->fd)) 109 perror("close"); 110 111 if (shm_unlink(p->path)) 112 perror("shm_unlink"); 113 } 114 115 apple_glx_diagnostic("destroyed pixmap buffer for: 0x%lx\n", d->drawable); 116} 117 118/* Return true if an error occurred. */ 119bool 120apple_glx_pixmap_create(Display * dpy, int screen, Pixmap pixmap, 121 const void *mode) 122{ 123 struct apple_glx_drawable *d; 124 struct apple_glx_pixmap *p; 125 bool double_buffered; 126 bool uses_stereo; 127 CGLError error; 128 const __GLcontextModes *cmodes = mode; 129 130 if (apple_glx_drawable_create(dpy, screen, pixmap, &d, &callbacks)) 131 return true; 132 133 /* d is locked and referenced at this point. */ 134 135 p = &d->types.pixmap; 136 137 p->xpixmap = pixmap; 138 p->buffer = NULL; 139 140 if (!XAppleDRICreatePixmap(dpy, screen, pixmap, 141 &p->width, &p->height, &p->pitch, &p->bpp, 142 &p->size, p->path, PATH_MAX)) { 143 d->unlock(d); 144 d->destroy(d); 145 return true; 146 } 147 148 p->fd = shm_open(p->path, O_RDWR, 0); 149 150 if (p->fd < 0) { 151 perror("shm_open"); 152 d->unlock(d); 153 d->destroy(d); 154 return true; 155 } 156 157 p->buffer = mmap(NULL, p->size, PROT_READ | PROT_WRITE, 158 MAP_FILE | MAP_SHARED, p->fd, 0); 159 160 if (MAP_FAILED == p->buffer) { 161 perror("mmap"); 162 d->unlock(d); 163 d->destroy(d); 164 return true; 165 } 166 167 apple_visual_create_pfobj(&p->pixel_format_obj, mode, &double_buffered, 168 &uses_stereo, /*offscreen */ true); 169 170 error = apple_cgl.create_context(p->pixel_format_obj, NULL, 171 &p->context_obj); 172 173 if (kCGLNoError != error) { 174 d->unlock(d); 175 d->destroy(d); 176 return true; 177 } 178 179 p->fbconfigID = cmodes->fbconfigID; 180 181 d->unlock(d); 182 183 apple_glx_diagnostic("created: pixmap buffer for 0x%lx\n", d->drawable); 184 185 return false; 186} 187 188bool 189apple_glx_pixmap_query(GLXPixmap pixmap, int attr, unsigned int *value) 190{ 191 struct apple_glx_drawable *d; 192 struct apple_glx_pixmap *p; 193 bool result = false; 194 195 d = apple_glx_drawable_find_by_type(pixmap, APPLE_GLX_DRAWABLE_PIXMAP, 196 APPLE_GLX_DRAWABLE_LOCK); 197 198 if (d) { 199 p = &d->types.pixmap; 200 201 switch (attr) { 202 case GLX_WIDTH: 203 *value = p->width; 204 result = true; 205 break; 206 207 case GLX_HEIGHT: 208 *value = p->height; 209 result = true; 210 break; 211 212 case GLX_FBCONFIG_ID: 213 *value = p->fbconfigID; 214 result = true; 215 break; 216 } 217 218 d->unlock(d); 219 } 220 221 return result; 222} 223 224/* Return true if the type is valid for pixmap. */ 225bool 226apple_glx_pixmap_destroy(Display * dpy, GLXPixmap pixmap) 227{ 228 return !apple_glx_drawable_destroy_by_type(dpy, pixmap, 229 APPLE_GLX_DRAWABLE_PIXMAP); 230} 231