applegl_glx.c revision 3464ebd5
1444c061aSmrg/* 2444c061aSmrg * Copyright © 2010 Intel Corporation 3444c061aSmrg * Copyright © 2011 Apple Inc. 4444c061aSmrg * 5444c061aSmrg * Permission is hereby granted, free of charge, to any person obtaining a 6444c061aSmrg * copy of this software and associated documentation files (the "Soft- 7444c061aSmrg * ware"), to deal in the Software without restriction, including without 8444c061aSmrg * limitation the rights to use, copy, modify, merge, publish, distribute, 9444c061aSmrg * and/or sell copies of the Software, and to permit persons to whom the 10444c061aSmrg * Software is furnished to do so, provided that the above copyright 11444c061aSmrg * notice(s) and this permission notice appear in all copies of the Soft- 12444c061aSmrg * ware and that both the above copyright notice(s) and this permission 13444c061aSmrg * notice appear in supporting documentation. 14444c061aSmrg * 15444c061aSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 16444c061aSmrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- 17444c061aSmrg * ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY 18444c061aSmrg * RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN 19444c061aSmrg * THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSE- 20444c061aSmrg * QUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 21444c061aSmrg * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 22444c061aSmrg * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFOR- 23444c061aSmrg * MANCE OF THIS SOFTWARE. 24444c061aSmrg * 25444c061aSmrg * Except as contained in this notice, the name of a copyright holder shall 26444c061aSmrg * not be used in advertising or otherwise to promote the sale, use or 27444c061aSmrg * other dealings in this Software without prior written authorization of 28444c061aSmrg * the copyright holder. 29444c061aSmrg * 30444c061aSmrg * Authors: 31444c061aSmrg * Kristian Høgsberg (krh@bitplanet.net) 32444c061aSmrg */ 33444c061aSmrg 34444c061aSmrg#if defined(GLX_USE_APPLEGL) 35444c061aSmrg 36444c061aSmrg#include <stdbool.h> 37444c061aSmrg#include <dlfcn.h> 38444c061aSmrg 39444c061aSmrg#include "glxclient.h" 40444c061aSmrg#include "apple_glx_context.h" 410568f49bSmrg#include "apple_glx.h" 42444c061aSmrg#include "apple_cgl.h" 439e7bcd65Smrg#include "glx_error.h" 44444c061aSmrg 45444c061aSmrgstatic void 469e7bcd65Smrgapplegl_destroy_context(struct glx_context *gc) 47444c061aSmrg{ 48444c061aSmrg apple_glx_destroy_context(&gc->driContext, gc->psc->dpy); 49444c061aSmrg} 50444c061aSmrg 51444c061aSmrgstatic int 520568f49bSmrgapplegl_bind_context(struct glx_context *gc, struct glx_context *old, 53444c061aSmrg GLXDrawable draw, GLXDrawable read) 549e7bcd65Smrg{ 550568f49bSmrg Display *dpy = gc->psc->dpy; 56444c061aSmrg bool error = apple_glx_make_current_context(dpy, 57444c061aSmrg (old && old != &dummyContext) ? old->driContext : NULL, 58444c061aSmrg gc ? gc->driContext : NULL, draw); 59444c061aSmrg 60444c061aSmrg apple_glx_diagnostic("%s: error %s\n", __func__, error ? "YES" : "NO"); 61444c061aSmrg if (error) 62444c061aSmrg return 1; /* GLXBadContext is the same as Success (0) */ 63444c061aSmrg 640568f49bSmrg apple_glapi_set_dispatch(); 65444c061aSmrg 66444c061aSmrg return Success; 67444c061aSmrg} 680568f49bSmrg 69444c061aSmrgstatic void 70444c061aSmrgapplegl_unbind_context(struct glx_context *gc, struct glx_context *new) 71444c061aSmrg{ 720568f49bSmrg Display *dpy; 730568f49bSmrg bool error; 740568f49bSmrg 75444c061aSmrg /* If we don't have a context, then we have nothing to unbind */ 76444c061aSmrg if (!gc) 77444c061aSmrg return; 78444c061aSmrg 799e7bcd65Smrg /* If we have a new context, keep this one around and remove it during bind. */ 809e7bcd65Smrg if (new) 81444c061aSmrg return; 820568f49bSmrg 830568f49bSmrg dpy = gc->psc->dpy; 840568f49bSmrg 85444c061aSmrg error = apple_glx_make_current_context(dpy, 86444c061aSmrg (gc != &dummyContext) ? gc->driContext : NULL, 87444c061aSmrg NULL, None); 880568f49bSmrg 89444c061aSmrg apple_glx_diagnostic("%s: error %s\n", __func__, error ? "YES" : "NO"); 900568f49bSmrg} 910568f49bSmrg 920568f49bSmrgstatic void 93444c061aSmrgapplegl_wait_gl(struct glx_context *gc) 949e7bcd65Smrg{ 959e7bcd65Smrg glFinish(); 96444c061aSmrg} 97444c061aSmrg 980568f49bSmrgstatic void 990568f49bSmrgapplegl_wait_x(struct glx_context *gc) 1000568f49bSmrg{ 101444c061aSmrg Display *dpy = gc->psc->dpy; 102444c061aSmrg apple_glx_waitx(dpy, gc->driContext); 103444c061aSmrg} 1040568f49bSmrg 1050568f49bSmrgstatic void * 106444c061aSmrgapplegl_get_proc_address(const char *symbol) 107444c061aSmrg{ 108444c061aSmrg return dlsym(apple_cgl_get_dl_handle(), symbol); 1090568f49bSmrg} 110444c061aSmrg 1110568f49bSmrgstatic const struct glx_context_vtable applegl_context_vtable = { 112444c061aSmrg applegl_destroy_context, 113444c061aSmrg applegl_bind_context, 114444c061aSmrg applegl_unbind_context, 115444c061aSmrg applegl_wait_gl, 1160568f49bSmrg applegl_wait_x, 117444c061aSmrg DRI_glXUseXFont, 118bdf0f55dSmrg NULL, /* bind_tex_image, */ 119444c061aSmrg NULL, /* release_tex_image, */ 120444c061aSmrg applegl_get_proc_address, 121444c061aSmrg}; 122444c061aSmrg 123struct glx_context * 124applegl_create_context(struct glx_screen *psc, 125 struct glx_config *config, 126 struct glx_context *shareList, int renderType) 127{ 128 struct glx_context *gc; 129 int errorcode; 130 bool x11error; 131 Display *dpy = psc->dpy; 132 int screen = psc->scr; 133 134 /* TODO: Integrate this with apple_glx_create_context and make 135 * struct apple_glx_context inherit from struct glx_context. */ 136 137 gc = Xcalloc(1, sizeof (*gc)); 138 if (gc == NULL) 139 return NULL; 140 141 if (!glx_context_init(gc, psc, config)) { 142 Xfree(gc); 143 return NULL; 144 } 145 146 gc->vtable = &applegl_context_vtable; 147 gc->driContext = NULL; 148 149 /* TODO: darwin: Integrate with above to do indirect */ 150 if(apple_glx_create_context(&gc->driContext, dpy, screen, config, 151 shareList ? shareList->driContext : NULL, 152 &errorcode, &x11error)) { 153 __glXSendError(dpy, errorcode, 0, X_GLXCreateContext, x11error); 154 gc->vtable->destroy(gc); 155 return NULL; 156 } 157 158 gc->currentContextTag = -1; 159 gc->config = config; 160 gc->isDirect = GL_TRUE; 161 gc->xid = 1; /* Just something not None, so we know when to destroy 162 * it in MakeContextCurrent. */ 163 164 return gc; 165} 166 167struct glx_screen_vtable applegl_screen_vtable = { 168 applegl_create_context 169}; 170 171_X_HIDDEN struct glx_screen * 172applegl_create_screen(int screen, struct glx_display * priv) 173{ 174 struct glx_screen *psc; 175 176 psc = Xmalloc(sizeof *psc); 177 if (psc == NULL) 178 return NULL; 179 180 memset(psc, 0, sizeof *psc); 181 glx_screen_init(psc, screen, priv); 182 psc->vtable = &applegl_screen_vtable; 183 184 return psc; 185} 186 187_X_HIDDEN int 188applegl_create_display(struct glx_display *glx_dpy) 189{ 190 if(!apple_init_glx(glx_dpy->dpy)) 191 return 1; 192 193 return GLXBadContext; 194} 195 196#endif 197