xref: /aosp_15_r20/external/mesa3d/src/glx/applegl_glx.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1*61046927SAndroid Build Coastguard Worker /*
2*61046927SAndroid Build Coastguard Worker  * Copyright © 2010 Intel Corporation
3*61046927SAndroid Build Coastguard Worker  * Copyright © 2011 Apple Inc.
4*61046927SAndroid Build Coastguard Worker  *
5*61046927SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining a
6*61046927SAndroid Build Coastguard Worker  * copy of this software and associated documentation files (the "Soft-
7*61046927SAndroid Build Coastguard Worker  * ware"), to deal in the Software without restriction, including without
8*61046927SAndroid Build Coastguard Worker  * limitation the rights to use, copy, modify, merge, publish, distribute,
9*61046927SAndroid Build Coastguard Worker  * and/or sell copies of the Software, and to permit persons to whom the
10*61046927SAndroid Build Coastguard Worker  * Software is furnished to do so, provided that the above copyright
11*61046927SAndroid Build Coastguard Worker  * notice(s) and this permission notice appear in all copies of the Soft-
12*61046927SAndroid Build Coastguard Worker  * ware and that both the above copyright notice(s) and this permission
13*61046927SAndroid Build Coastguard Worker  * notice appear in supporting documentation.
14*61046927SAndroid Build Coastguard Worker  *
15*61046927SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16*61046927SAndroid Build Coastguard Worker  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
17*61046927SAndroid Build Coastguard Worker  * ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY
18*61046927SAndroid Build Coastguard Worker  * RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN
19*61046927SAndroid Build Coastguard Worker  * THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSE-
20*61046927SAndroid Build Coastguard Worker  * QUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21*61046927SAndroid Build Coastguard Worker  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22*61046927SAndroid Build Coastguard Worker  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFOR-
23*61046927SAndroid Build Coastguard Worker  * MANCE OF THIS SOFTWARE.
24*61046927SAndroid Build Coastguard Worker  *
25*61046927SAndroid Build Coastguard Worker  * Except as contained in this notice, the name of a copyright holder shall
26*61046927SAndroid Build Coastguard Worker  * not be used in advertising or otherwise to promote the sale, use or
27*61046927SAndroid Build Coastguard Worker  * other dealings in this Software without prior written authorization of
28*61046927SAndroid Build Coastguard Worker  * the copyright holder.
29*61046927SAndroid Build Coastguard Worker  *
30*61046927SAndroid Build Coastguard Worker  * Authors:
31*61046927SAndroid Build Coastguard Worker  *   Kristian Høgsberg ([email protected])
32*61046927SAndroid Build Coastguard Worker  */
33*61046927SAndroid Build Coastguard Worker 
34*61046927SAndroid Build Coastguard Worker #if defined(GLX_USE_APPLEGL)
35*61046927SAndroid Build Coastguard Worker 
36*61046927SAndroid Build Coastguard Worker #include <stdbool.h>
37*61046927SAndroid Build Coastguard Worker #include <dlfcn.h>
38*61046927SAndroid Build Coastguard Worker 
39*61046927SAndroid Build Coastguard Worker #include "glxclient.h"
40*61046927SAndroid Build Coastguard Worker #include "apple/apple_glx_context.h"
41*61046927SAndroid Build Coastguard Worker #include "apple/apple_glx.h"
42*61046927SAndroid Build Coastguard Worker #include "apple/apple_cgl.h"
43*61046927SAndroid Build Coastguard Worker #include "glx_error.h"
44*61046927SAndroid Build Coastguard Worker 
45*61046927SAndroid Build Coastguard Worker static void
applegl_destroy_context(struct glx_context * gc)46*61046927SAndroid Build Coastguard Worker applegl_destroy_context(struct glx_context *gc)
47*61046927SAndroid Build Coastguard Worker {
48*61046927SAndroid Build Coastguard Worker    apple_glx_destroy_context(&gc->driContext, gc->psc->dpy);
49*61046927SAndroid Build Coastguard Worker }
50*61046927SAndroid Build Coastguard Worker 
51*61046927SAndroid Build Coastguard Worker static int
applegl_bind_context(struct glx_context * gc,GLXDrawable draw,GLXDrawable read)52*61046927SAndroid Build Coastguard Worker applegl_bind_context(
53*61046927SAndroid Build Coastguard Worker     struct glx_context *gc,
54*61046927SAndroid Build Coastguard Worker     GLXDrawable draw, GLXDrawable read)
55*61046927SAndroid Build Coastguard Worker {
56*61046927SAndroid Build Coastguard Worker    Display *dpy = gc->psc->dpy;
57*61046927SAndroid Build Coastguard Worker    bool error = apple_glx_make_current_context(
58*61046927SAndroid Build Coastguard Worker        dpy,
59*61046927SAndroid Build Coastguard Worker        NULL,
60*61046927SAndroid Build Coastguard Worker        gc ? gc->driContext : NULL, draw);
61*61046927SAndroid Build Coastguard Worker 
62*61046927SAndroid Build Coastguard Worker    apple_glx_diagnostic("%s: error %s\n", __func__, error ? "YES" : "NO");
63*61046927SAndroid Build Coastguard Worker    if (error)
64*61046927SAndroid Build Coastguard Worker       return 1; /* GLXBadContext is the same as Success (0) */
65*61046927SAndroid Build Coastguard Worker 
66*61046927SAndroid Build Coastguard Worker    apple_glapi_set_dispatch();
67*61046927SAndroid Build Coastguard Worker 
68*61046927SAndroid Build Coastguard Worker    return Success;
69*61046927SAndroid Build Coastguard Worker }
70*61046927SAndroid Build Coastguard Worker 
71*61046927SAndroid Build Coastguard Worker static void
applegl_unbind_context(struct glx_context * gc)72*61046927SAndroid Build Coastguard Worker applegl_unbind_context(struct glx_context *gc)
73*61046927SAndroid Build Coastguard Worker {
74*61046927SAndroid Build Coastguard Worker    Display *dpy;
75*61046927SAndroid Build Coastguard Worker    bool error;
76*61046927SAndroid Build Coastguard Worker 
77*61046927SAndroid Build Coastguard Worker    /* If we don't have a context, then we have nothing to unbind */
78*61046927SAndroid Build Coastguard Worker    if (!gc)
79*61046927SAndroid Build Coastguard Worker       return;
80*61046927SAndroid Build Coastguard Worker 
81*61046927SAndroid Build Coastguard Worker    dpy = gc->psc->dpy;
82*61046927SAndroid Build Coastguard Worker 
83*61046927SAndroid Build Coastguard Worker    error = apple_glx_make_current_context(
84*61046927SAndroid Build Coastguard Worker        dpy,
85*61046927SAndroid Build Coastguard Worker        (gc != &dummyContext) ? gc->driContext : NULL,
86*61046927SAndroid Build Coastguard Worker        NULL, None);
87*61046927SAndroid Build Coastguard Worker 
88*61046927SAndroid Build Coastguard Worker    apple_glx_diagnostic("%s: error %s\n", __func__, error ? "YES" : "NO");
89*61046927SAndroid Build Coastguard Worker }
90*61046927SAndroid Build Coastguard Worker 
91*61046927SAndroid Build Coastguard Worker static void
applegl_wait_gl(struct glx_context * gc)92*61046927SAndroid Build Coastguard Worker applegl_wait_gl(struct glx_context *gc)
93*61046927SAndroid Build Coastguard Worker {
94*61046927SAndroid Build Coastguard Worker    glFinish();
95*61046927SAndroid Build Coastguard Worker }
96*61046927SAndroid Build Coastguard Worker 
97*61046927SAndroid Build Coastguard Worker static void
applegl_wait_x(struct glx_context * gc)98*61046927SAndroid Build Coastguard Worker applegl_wait_x(struct glx_context *gc)
99*61046927SAndroid Build Coastguard Worker {
100*61046927SAndroid Build Coastguard Worker    Display *dpy = gc->psc->dpy;
101*61046927SAndroid Build Coastguard Worker    apple_glx_waitx(dpy, gc->driContext);
102*61046927SAndroid Build Coastguard Worker }
103*61046927SAndroid Build Coastguard Worker 
104*61046927SAndroid Build Coastguard Worker void *
applegl_get_proc_address(const char * symbol)105*61046927SAndroid Build Coastguard Worker applegl_get_proc_address(const char *symbol)
106*61046927SAndroid Build Coastguard Worker {
107*61046927SAndroid Build Coastguard Worker    return dlsym(apple_cgl_get_dl_handle(), symbol);
108*61046927SAndroid Build Coastguard Worker }
109*61046927SAndroid Build Coastguard Worker 
110*61046927SAndroid Build Coastguard Worker static const struct glx_context_vtable applegl_context_vtable = {
111*61046927SAndroid Build Coastguard Worker    .destroy             = applegl_destroy_context,
112*61046927SAndroid Build Coastguard Worker    .bind                = applegl_bind_context,
113*61046927SAndroid Build Coastguard Worker    .unbind              = applegl_unbind_context,
114*61046927SAndroid Build Coastguard Worker    .wait_gl             = applegl_wait_gl,
115*61046927SAndroid Build Coastguard Worker    .wait_x              = applegl_wait_x,
116*61046927SAndroid Build Coastguard Worker };
117*61046927SAndroid Build Coastguard Worker 
118*61046927SAndroid Build Coastguard Worker struct glx_context *
applegl_create_context(struct glx_screen * psc,struct glx_config * config,struct glx_context * shareList,int renderType)119*61046927SAndroid Build Coastguard Worker applegl_create_context(
120*61046927SAndroid Build Coastguard Worker     struct glx_screen *psc,
121*61046927SAndroid Build Coastguard Worker     struct glx_config *config,
122*61046927SAndroid Build Coastguard Worker     struct glx_context *shareList, int renderType)
123*61046927SAndroid Build Coastguard Worker {
124*61046927SAndroid Build Coastguard Worker    struct glx_context *gc;
125*61046927SAndroid Build Coastguard Worker    int errorcode;
126*61046927SAndroid Build Coastguard Worker    bool x11error;
127*61046927SAndroid Build Coastguard Worker    Display *dpy = psc->dpy;
128*61046927SAndroid Build Coastguard Worker    int screen = psc->scr;
129*61046927SAndroid Build Coastguard Worker 
130*61046927SAndroid Build Coastguard Worker    /* TODO: Integrate this with apple_glx_create_context and make
131*61046927SAndroid Build Coastguard Worker     * struct apple_glx_context inherit from struct glx_context. */
132*61046927SAndroid Build Coastguard Worker 
133*61046927SAndroid Build Coastguard Worker    if (!config)
134*61046927SAndroid Build Coastguard Worker       return NULL;
135*61046927SAndroid Build Coastguard Worker 
136*61046927SAndroid Build Coastguard Worker    gc = calloc(1, sizeof(*gc));
137*61046927SAndroid Build Coastguard Worker    if (gc == NULL)
138*61046927SAndroid Build Coastguard Worker       return NULL;
139*61046927SAndroid Build Coastguard Worker 
140*61046927SAndroid Build Coastguard Worker    if (!glx_context_init(gc, psc, config)) {
141*61046927SAndroid Build Coastguard Worker       free(gc);
142*61046927SAndroid Build Coastguard Worker       return NULL;
143*61046927SAndroid Build Coastguard Worker    }
144*61046927SAndroid Build Coastguard Worker 
145*61046927SAndroid Build Coastguard Worker    gc->vtable = &applegl_context_vtable;
146*61046927SAndroid Build Coastguard Worker    gc->driContext = NULL;
147*61046927SAndroid Build Coastguard Worker 
148*61046927SAndroid Build Coastguard Worker    /* TODO: darwin: Integrate with above to do indirect */
149*61046927SAndroid Build Coastguard Worker    if (apple_glx_create_context(&gc->driContext, dpy, screen, config,
150*61046927SAndroid Build Coastguard Worker                                 shareList ? shareList->driContext : NULL,
151*61046927SAndroid Build Coastguard Worker                                 &errorcode, &x11error)) {
152*61046927SAndroid Build Coastguard Worker       __glXSendError(dpy, errorcode, 0, X_GLXCreateContext, x11error);
153*61046927SAndroid Build Coastguard Worker       gc->vtable->destroy(gc);
154*61046927SAndroid Build Coastguard Worker       return NULL;
155*61046927SAndroid Build Coastguard Worker    }
156*61046927SAndroid Build Coastguard Worker 
157*61046927SAndroid Build Coastguard Worker    gc->currentContextTag = -1;
158*61046927SAndroid Build Coastguard Worker    gc->config = config;
159*61046927SAndroid Build Coastguard Worker    gc->isDirect = GL_TRUE;
160*61046927SAndroid Build Coastguard Worker    gc->xid = 1; /* Just something not None, so we know when to destroy
161*61046927SAndroid Build Coastguard Worker                  * it in MakeContextCurrent. */
162*61046927SAndroid Build Coastguard Worker 
163*61046927SAndroid Build Coastguard Worker    return gc;
164*61046927SAndroid Build Coastguard Worker }
165*61046927SAndroid Build Coastguard Worker 
166*61046927SAndroid Build Coastguard Worker static const struct glx_screen_vtable applegl_screen_vtable = {
167*61046927SAndroid Build Coastguard Worker    .create_context         = applegl_create_context,
168*61046927SAndroid Build Coastguard Worker    .create_context_attribs = NULL,
169*61046927SAndroid Build Coastguard Worker    .query_renderer_integer = NULL,
170*61046927SAndroid Build Coastguard Worker    .query_renderer_string  = NULL,
171*61046927SAndroid Build Coastguard Worker };
172*61046927SAndroid Build Coastguard Worker 
173*61046927SAndroid Build Coastguard Worker _X_HIDDEN struct glx_screen *
applegl_create_screen(int screen,struct glx_display * priv)174*61046927SAndroid Build Coastguard Worker applegl_create_screen(int screen, struct glx_display * priv)
175*61046927SAndroid Build Coastguard Worker {
176*61046927SAndroid Build Coastguard Worker    struct glx_screen *psc;
177*61046927SAndroid Build Coastguard Worker 
178*61046927SAndroid Build Coastguard Worker    psc = calloc(1, sizeof *psc);
179*61046927SAndroid Build Coastguard Worker    if (psc == NULL)
180*61046927SAndroid Build Coastguard Worker       return NULL;
181*61046927SAndroid Build Coastguard Worker 
182*61046927SAndroid Build Coastguard Worker    glx_screen_init(psc, screen, priv);
183*61046927SAndroid Build Coastguard Worker    psc->vtable = &applegl_screen_vtable;
184*61046927SAndroid Build Coastguard Worker 
185*61046927SAndroid Build Coastguard Worker    return psc;
186*61046927SAndroid Build Coastguard Worker }
187*61046927SAndroid Build Coastguard Worker 
188*61046927SAndroid Build Coastguard Worker _X_HIDDEN int
applegl_create_display(struct glx_display * glx_dpy)189*61046927SAndroid Build Coastguard Worker applegl_create_display(struct glx_display *glx_dpy)
190*61046927SAndroid Build Coastguard Worker {
191*61046927SAndroid Build Coastguard Worker    if(!apple_init_glx(glx_dpy->dpy))
192*61046927SAndroid Build Coastguard Worker       return 1;
193*61046927SAndroid Build Coastguard Worker 
194*61046927SAndroid Build Coastguard Worker    return GLXBadContext;
195*61046927SAndroid Build Coastguard Worker }
196*61046927SAndroid Build Coastguard Worker 
197*61046927SAndroid Build Coastguard Worker #endif
198