1 /*
2  * Copyright © 2010 Intel Corporation
3  * Copyright © 2011 Apple Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Soft-
7  * ware"), to deal in the Software without restriction, including without
8  * limitation the rights to use, copy, modify, merge, publish, distribute,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, provided that the above copyright
11  * notice(s) and this permission notice appear in all copies of the Soft-
12  * ware and that both the above copyright notice(s) and this permission
13  * notice appear in supporting documentation.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
17  * ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
18  * RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN
19  * THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSE-
20  * QUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFOR-
23  * MANCE OF THIS SOFTWARE.
24  *
25  * Except as contained in this notice, the name of a copyright holder shall
26  * not be used in advertising or otherwise to promote the sale, use or
27  * other dealings in this Software without prior written authorization of
28  * the copyright holder.
29  *
30  * Authors:
31  *   Kristian Høgsberg (krh@bitplanet.net)
32  */
33 
34 #if defined(GLX_USE_APPLEGL)
35 
36 #include <stdbool.h>
37 #include <dlfcn.h>
38 
39 #include "glxclient.h"
40 #include "apple_glx_context.h"
41 #include "apple_glx.h"
42 #include "apple_cgl.h"
43 #include "glx_error.h"
44 
45 static void
applegl_destroy_context(struct glx_context * gc)46 applegl_destroy_context(struct glx_context *gc)
47 {
48    apple_glx_destroy_context(&gc->driContext, gc->psc->dpy);
49 }
50 
51 static int
applegl_bind_context(struct glx_context * gc,struct glx_context * old,GLXDrawable draw,GLXDrawable read)52 applegl_bind_context(struct glx_context *gc, struct glx_context *old,
53 		     GLXDrawable draw, GLXDrawable read)
54 {
55    Display *dpy = gc->psc->dpy;
56    bool error = apple_glx_make_current_context(dpy,
57 					       (old && old != &dummyContext) ? old->driContext : NULL,
58 					       gc ? gc->driContext : NULL, draw);
59 
60    apple_glx_diagnostic("%s: error %s\n", __func__, error ? "YES" : "NO");
61    if (error)
62       return 1; /* GLXBadContext is the same as Success (0) */
63 
64    apple_glapi_set_dispatch();
65 
66    return Success;
67 }
68 
69 static void
applegl_unbind_context(struct glx_context * gc,struct glx_context * new)70 applegl_unbind_context(struct glx_context *gc, struct glx_context *new)
71 {
72    Display *dpy;
73    bool error;
74 
75    /* If we don't have a context, then we have nothing to unbind */
76    if (!gc)
77       return;
78 
79    /* If we have a new context, keep this one around and remove it during bind. */
80    if (new)
81       return;
82 
83    dpy = gc->psc->dpy;
84 
85    error = apple_glx_make_current_context(dpy,
86 					  (gc != &dummyContext) ? gc->driContext : NULL,
87 					  NULL, None);
88 
89    apple_glx_diagnostic("%s: error %s\n", __func__, error ? "YES" : "NO");
90 }
91 
92 static void
applegl_wait_gl(struct glx_context * gc)93 applegl_wait_gl(struct glx_context *gc)
94 {
95    glFinish();
96 }
97 
98 static void
applegl_wait_x(struct glx_context * gc)99 applegl_wait_x(struct glx_context *gc)
100 {
101    Display *dpy = gc->psc->dpy;
102    apple_glx_waitx(dpy, gc->driContext);
103 }
104 
105 static void *
applegl_get_proc_address(const char * symbol)106 applegl_get_proc_address(const char *symbol)
107 {
108    return dlsym(apple_cgl_get_dl_handle(), symbol);
109 }
110 
111 static const struct glx_context_vtable applegl_context_vtable = {
112    applegl_destroy_context,
113    applegl_bind_context,
114    applegl_unbind_context,
115    applegl_wait_gl,
116    applegl_wait_x,
117    DRI_glXUseXFont,
118    NULL, /* bind_tex_image, */
119    NULL, /* release_tex_image, */
120    applegl_get_proc_address,
121 };
122 
123 struct glx_context *
applegl_create_context(struct glx_screen * psc,struct glx_config * config,struct glx_context * shareList,int renderType)124 applegl_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 
167 struct glx_screen_vtable applegl_screen_vtable = {
168    applegl_create_context
169 };
170 
171 _X_HIDDEN struct glx_screen *
applegl_create_screen(int screen,struct glx_display * priv)172 applegl_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
applegl_create_display(struct glx_display * glx_dpy)188 applegl_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