xref: /aosp_15_r20/external/mesa3d/src/glx/applegl_glx.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 ([email protected])
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/apple_glx_context.h"
41 #include "apple/apple_glx.h"
42 #include "apple/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,GLXDrawable draw,GLXDrawable read)52 applegl_bind_context(
53     struct glx_context *gc,
54     GLXDrawable draw, GLXDrawable read)
55 {
56    Display *dpy = gc->psc->dpy;
57    bool error = apple_glx_make_current_context(
58        dpy,
59        NULL,
60        gc ? gc->driContext : NULL, draw);
61 
62    apple_glx_diagnostic("%s: error %s\n", __func__, error ? "YES" : "NO");
63    if (error)
64       return 1; /* GLXBadContext is the same as Success (0) */
65 
66    apple_glapi_set_dispatch();
67 
68    return Success;
69 }
70 
71 static void
applegl_unbind_context(struct glx_context * gc)72 applegl_unbind_context(struct glx_context *gc)
73 {
74    Display *dpy;
75    bool error;
76 
77    /* If we don't have a context, then we have nothing to unbind */
78    if (!gc)
79       return;
80 
81    dpy = gc->psc->dpy;
82 
83    error = apple_glx_make_current_context(
84        dpy,
85        (gc != &dummyContext) ? gc->driContext : NULL,
86        NULL, None);
87 
88    apple_glx_diagnostic("%s: error %s\n", __func__, error ? "YES" : "NO");
89 }
90 
91 static void
applegl_wait_gl(struct glx_context * gc)92 applegl_wait_gl(struct glx_context *gc)
93 {
94    glFinish();
95 }
96 
97 static void
applegl_wait_x(struct glx_context * gc)98 applegl_wait_x(struct glx_context *gc)
99 {
100    Display *dpy = gc->psc->dpy;
101    apple_glx_waitx(dpy, gc->driContext);
102 }
103 
104 void *
applegl_get_proc_address(const char * symbol)105 applegl_get_proc_address(const char *symbol)
106 {
107    return dlsym(apple_cgl_get_dl_handle(), symbol);
108 }
109 
110 static const struct glx_context_vtable applegl_context_vtable = {
111    .destroy             = applegl_destroy_context,
112    .bind                = applegl_bind_context,
113    .unbind              = applegl_unbind_context,
114    .wait_gl             = applegl_wait_gl,
115    .wait_x              = applegl_wait_x,
116 };
117 
118 struct glx_context *
applegl_create_context(struct glx_screen * psc,struct glx_config * config,struct glx_context * shareList,int renderType)119 applegl_create_context(
120     struct glx_screen *psc,
121     struct glx_config *config,
122     struct glx_context *shareList, int renderType)
123 {
124    struct glx_context *gc;
125    int errorcode;
126    bool x11error;
127    Display *dpy = psc->dpy;
128    int screen = psc->scr;
129 
130    /* TODO: Integrate this with apple_glx_create_context and make
131     * struct apple_glx_context inherit from struct glx_context. */
132 
133    if (!config)
134       return NULL;
135 
136    gc = calloc(1, sizeof(*gc));
137    if (gc == NULL)
138       return NULL;
139 
140    if (!glx_context_init(gc, psc, config)) {
141       free(gc);
142       return NULL;
143    }
144 
145    gc->vtable = &applegl_context_vtable;
146    gc->driContext = NULL;
147 
148    /* TODO: darwin: Integrate with above to do indirect */
149    if (apple_glx_create_context(&gc->driContext, dpy, screen, config,
150                                 shareList ? shareList->driContext : NULL,
151                                 &errorcode, &x11error)) {
152       __glXSendError(dpy, errorcode, 0, X_GLXCreateContext, x11error);
153       gc->vtable->destroy(gc);
154       return NULL;
155    }
156 
157    gc->currentContextTag = -1;
158    gc->config = config;
159    gc->isDirect = GL_TRUE;
160    gc->xid = 1; /* Just something not None, so we know when to destroy
161                  * it in MakeContextCurrent. */
162 
163    return gc;
164 }
165 
166 static const struct glx_screen_vtable applegl_screen_vtable = {
167    .create_context         = applegl_create_context,
168    .create_context_attribs = NULL,
169    .query_renderer_integer = NULL,
170    .query_renderer_string  = NULL,
171 };
172 
173 _X_HIDDEN struct glx_screen *
applegl_create_screen(int screen,struct glx_display * priv)174 applegl_create_screen(int screen, struct glx_display * priv)
175 {
176    struct glx_screen *psc;
177 
178    psc = calloc(1, sizeof *psc);
179    if (psc == NULL)
180       return NULL;
181 
182    glx_screen_init(psc, screen, priv);
183    psc->vtable = &applegl_screen_vtable;
184 
185    return psc;
186 }
187 
188 _X_HIDDEN int
applegl_create_display(struct glx_display * glx_dpy)189 applegl_create_display(struct glx_display *glx_dpy)
190 {
191    if(!apple_init_glx(glx_dpy->dpy))
192       return 1;
193 
194    return GLXBadContext;
195 }
196 
197 #endif
198