xref: /third_party/mesa3d/src/glx/applegl_glx.c (revision bf215546)
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/apple_glx_context.h"
41#include "apple/apple_glx.h"
42#include "apple/apple_cgl.h"
43#include "glx_error.h"
44
45static void
46applegl_destroy_context(struct glx_context *gc)
47{
48   apple_glx_destroy_context(&gc->driContext, gc->psc->dpy);
49}
50
51static int
52applegl_bind_context(
53    struct glx_context *gc, struct glx_context *old,
54    GLXDrawable draw, GLXDrawable read)
55{
56   Display *dpy = gc->psc->dpy;
57   bool error = apple_glx_make_current_context(
58       dpy,
59       (old && old != &dummyContext) ? old->driContext : 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
71static void
72applegl_unbind_context(struct glx_context *gc, struct glx_context *new)
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   /* If we have a new context, keep this one around and remove it during bind. */
82   if (new)
83      return;
84
85   dpy = gc->psc->dpy;
86
87   error = apple_glx_make_current_context(
88       dpy,
89       (gc != &dummyContext) ? gc->driContext : NULL,
90       NULL, None);
91
92   apple_glx_diagnostic("%s: error %s\n", __func__, error ? "YES" : "NO");
93}
94
95static void
96applegl_wait_gl(struct glx_context *gc)
97{
98   glFinish();
99}
100
101static void
102applegl_wait_x(struct glx_context *gc)
103{
104   Display *dpy = gc->psc->dpy;
105   apple_glx_waitx(dpy, gc->driContext);
106}
107
108void *
109applegl_get_proc_address(const char *symbol)
110{
111   return dlsym(apple_cgl_get_dl_handle(), symbol);
112}
113
114static const struct glx_context_vtable applegl_context_vtable = {
115   .destroy             = applegl_destroy_context,
116   .bind                = applegl_bind_context,
117   .unbind              = applegl_unbind_context,
118   .wait_gl             = applegl_wait_gl,
119   .wait_x              = applegl_wait_x,
120};
121
122struct glx_context *
123applegl_create_context(
124    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   if (!config)
138      return NULL;
139
140   gc = calloc(1, sizeof(*gc));
141   if (gc == NULL)
142      return NULL;
143
144   if (!glx_context_init(gc, psc, config)) {
145      free(gc);
146      return NULL;
147   }
148
149   gc->vtable = &applegl_context_vtable;
150   gc->driContext = NULL;
151
152   /* TODO: darwin: Integrate with above to do indirect */
153   if (apple_glx_create_context(&gc->driContext, dpy, screen, config,
154                                shareList ? shareList->driContext : NULL,
155                                &errorcode, &x11error)) {
156      __glXSendError(dpy, errorcode, 0, X_GLXCreateContext, x11error);
157      gc->vtable->destroy(gc);
158      return NULL;
159   }
160
161   gc->currentContextTag = -1;
162   gc->config = config;
163   gc->isDirect = GL_TRUE;
164   gc->xid = 1; /* Just something not None, so we know when to destroy
165                 * it in MakeContextCurrent. */
166
167   return gc;
168}
169
170static const struct glx_screen_vtable applegl_screen_vtable = {
171   .create_context         = applegl_create_context,
172   .create_context_attribs = NULL,
173   .query_renderer_integer = NULL,
174   .query_renderer_string  = NULL,
175};
176
177_X_HIDDEN struct glx_screen *
178applegl_create_screen(int screen, struct glx_display * priv)
179{
180   struct glx_screen *psc;
181
182   psc = calloc(1, sizeof *psc);
183   if (psc == NULL)
184      return NULL;
185
186   glx_screen_init(psc, screen, priv);
187   psc->vtable = &applegl_screen_vtable;
188
189   return psc;
190}
191
192_X_HIDDEN int
193applegl_create_display(struct glx_display *glx_dpy)
194{
195   if(!apple_init_glx(glx_dpy->dpy))
196      return 1;
197
198   return GLXBadContext;
199}
200
201#endif
202