apple_glx.c revision 7d215e7c4d0ca8a18d91d4f30f79b97835a6d6c4
1/* 2 Copyright (c) 2008, 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 <string.h> 33#include <assert.h> 34#include <stdarg.h> 35#include <dlfcn.h> 36#include "appledri.h" 37#include "apple_glx.h" 38#include "apple_glx_context.h" 39#include "apple_cgl.h" 40 41static bool initialized = false; 42static int dri_event_base = 0; 43 44const GLuint __glXDefaultPixelStore[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 1 }; 45 46#ifndef OPENGL_LIB_PATH 47#define OPENGL_LIB_PATH "/System/Library/Frameworks/OpenGL.framework/Libraries/libGL.dylib" 48#endif 49 50static void *libgl_handle = NULL; 51 52static bool diagnostic = false; 53 54void 55apple_glx_diagnostic(const char *fmt, ...) 56{ 57 va_list vl; 58 59 if (diagnostic) { 60 fprintf(stderr, "DIAG: "); 61 62 va_start(vl, fmt); 63 vfprintf(stderr, fmt, vl); 64 va_end(vl); 65 } 66} 67 68int 69apple_get_dri_event_base(void) 70{ 71 if (!initialized) { 72 fprintf(stderr, 73 "error: dri_event_base called before apple_init_glx!\n"); 74 abort(); 75 } 76 return dri_event_base; 77} 78 79static void 80surface_notify_handler(Display * dpy, unsigned int uid, int kind) 81{ 82 83 switch (kind) { 84 case AppleDRISurfaceNotifyDestroyed: 85 apple_glx_diagnostic("%s: surface destroyed %u\n", __func__, uid); 86 apple_glx_surface_destroy(uid); 87 break; 88 89 case AppleDRISurfaceNotifyChanged:{ 90 int updated; 91 92 updated = apple_glx_context_surface_changed(uid, pthread_self()); 93 94 apple_glx_diagnostic("surface notify updated %d\n", updated); 95 } 96 break; 97 98 default: 99 fprintf(stderr, "unhandled kind of event: %d in %s\n", kind, __func__); 100 } 101} 102 103xp_client_id 104apple_glx_get_client_id(void) 105{ 106 static xp_client_id id; 107 108 if (0 == id) { 109 if ((XP_Success != xp_init(XP_IN_BACKGROUND)) || 110 (Success != xp_get_client_id(&id))) { 111 return 0; 112 } 113 } 114 115 return id; 116} 117 118/* Return true if an error occured. */ 119bool 120apple_init_glx(Display * dpy) 121{ 122 int eventBase, errorBase; 123 int major, minor, patch; 124 125 if (!XAppleDRIQueryExtension(dpy, &eventBase, &errorBase)) 126 return true; 127 128 if (!XAppleDRIQueryVersion(dpy, &major, &minor, &patch)) 129 return true; 130 131 if (initialized) 132 return false; 133 134 if (getenv("LIBGL_DIAGNOSTIC")) { 135 printf("initializing libGL in %s\n", __func__); 136 diagnostic = true; 137 } 138 139 apple_cgl_init(); 140 apple_xgl_init_direct(); 141 libgl_handle = dlopen(OPENGL_LIB_PATH, RTLD_LAZY); 142 (void) apple_glx_get_client_id(); 143 144 XAppleDRISetSurfaceNotifyHandler(surface_notify_handler); 145 146 /* This should really be per display. */ 147 dri_event_base = eventBase; 148 initialized = true; 149 150 return false; 151} 152 153void 154apple_glx_swap_buffers(void *ptr) 155{ 156 struct apple_glx_context *ac = ptr; 157 158 /* This may not be needed with CGLFlushDrawable: */ 159 glFlush(); 160 apple_cgl.flush_drawable(ac->context_obj); 161} 162 163void * 164apple_glx_get_proc_address(const GLubyte * procname) 165{ 166 size_t len; 167 void *h, *s; 168 char *pname = (char *) procname; 169 170 assert(NULL != procname); 171 len = strlen(pname); 172 173 if (len < 3) { 174 return NULL; 175 } 176 177 if ((pname != strstr(pname, "glX")) && (pname != strstr(pname, "gl"))) { 178 fprintf(stderr, 179 "warning: get proc address request is not for a gl or glX function"); 180 return NULL; 181 } 182 183 /* Search using the default symbols first. */ 184 (void) dlerror(); /*drain dlerror */ 185 h = dlopen(NULL, RTLD_NOW); 186 if (NULL == h) { 187 fprintf(stderr, "warning: get proc address: %s\n", dlerror()); 188 return NULL; 189 } 190 191 s = dlsym(h, pname); 192 193 if (NULL == s) { 194 /* Try the libGL.dylib from the OpenGL.framework. */ 195 s = dlsym(libgl_handle, pname); 196 } 197 198 dlclose(h); 199 200 return s; 201} 202 203void 204apple_glx_waitx(Display * dpy, void *ptr) 205{ 206 struct apple_private_context *ac = ptr; 207 208 (void) ac; 209 210 glFlush(); 211 glFinish(); 212 XSync(dpy, False); 213} 214