apple_glx.c revision 0594cf70883b64692ba617d85f4f9b4e636e5c2b
1/*
2 Copyright (c) 2008, 2009 Apple Inc.
3
4 Permission is hereby granted, free of charge, to any person
5 obtaining a copy of this software and associated documentation files
6 (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge,
8 publish, distribute, sublicense, and/or sell copies of the Software,
9 and to permit persons to whom the Software is furnished to do so,
10 subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be
13 included in all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 NONINFRINGEMENT.  IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
19 HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23
24 Except as contained in this notice, the name(s) of the above
25 copyright holders shall not be used in advertising or otherwise to
26 promote the sale, use or other dealings in this Software without
27 prior written authorization.
28*/
29
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <assert.h>
34#include <stdarg.h>
35#include <dlfcn.h>
36#include "appledri.h"
37#include "apple_glx.h"
38#include "apple_glx_context.h"
39#include "apple_cgl.h"
40#include "apple_xgl_api.h"
41
42static bool initialized = false;
43static int dri_event_base = 0;
44
45const GLuint __glXDefaultPixelStore[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 1 };
46
47#ifndef OPENGL_LIB_PATH
48#define OPENGL_LIB_PATH "/System/Library/Frameworks/OpenGL.framework/Libraries/libGL.dylib"
49#endif
50
51static void *libgl_handle = NULL;
52
53static bool diagnostic = false;
54
55void
56apple_glx_diagnostic(const char *fmt, ...)
57{
58   va_list vl;
59
60   if (diagnostic) {
61      fprintf(stderr, "DIAG: ");
62
63      va_start(vl, fmt);
64      vfprintf(stderr, fmt, vl);
65      va_end(vl);
66   }
67}
68
69int
70apple_get_dri_event_base(void)
71{
72   if (!initialized) {
73      fprintf(stderr,
74              "error: dri_event_base called before apple_init_glx!\n");
75      abort();
76   }
77   return dri_event_base;
78}
79
80static void
81surface_notify_handler(Display * dpy, unsigned int uid, int kind)
82{
83
84   switch (kind) {
85   case AppleDRISurfaceNotifyDestroyed:
86      apple_glx_diagnostic("%s: surface destroyed %u\n", __func__, uid);
87      apple_glx_surface_destroy(uid);
88      break;
89
90   case AppleDRISurfaceNotifyChanged:{
91         int updated;
92
93         updated = apple_glx_context_surface_changed(uid, pthread_self());
94
95         apple_glx_diagnostic("surface notify updated %d\n", updated);
96      }
97      break;
98
99   default:
100      fprintf(stderr, "unhandled kind of event: %d in %s\n", kind, __func__);
101   }
102}
103
104xp_client_id
105apple_glx_get_client_id(void)
106{
107   static xp_client_id id;
108
109   if (0 == id) {
110      if ((XP_Success != xp_init(XP_IN_BACKGROUND)) ||
111          (Success != xp_get_client_id(&id))) {
112         return 0;
113      }
114   }
115
116   return id;
117}
118
119/* Return true if an error occured. */
120bool
121apple_init_glx(Display * dpy)
122{
123   int eventBase, errorBase;
124   int major, minor, patch;
125
126   if (!XAppleDRIQueryExtension(dpy, &eventBase, &errorBase))
127      return true;
128
129   if (!XAppleDRIQueryVersion(dpy, &major, &minor, &patch))
130      return true;
131
132   if (initialized)
133      return false;
134
135   if (getenv("LIBGL_DIAGNOSTIC")) {
136      printf("initializing libGL in %s\n", __func__);
137      diagnostic = true;
138   }
139
140   apple_cgl_init();
141   apple_xgl_init_direct();
142   libgl_handle = dlopen(OPENGL_LIB_PATH, RTLD_LAZY);
143   (void) apple_glx_get_client_id();
144
145   XAppleDRISetSurfaceNotifyHandler(surface_notify_handler);
146
147   /* This should really be per display. */
148   dri_event_base = eventBase;
149   initialized = true;
150
151   return false;
152}
153
154void
155apple_glx_swap_buffers(void *ptr)
156{
157   struct apple_glx_context *ac = ptr;
158
159   /* This may not be needed with CGLFlushDrawable: */
160   glFlush();
161   apple_cgl.flush_drawable(ac->context_obj);
162}
163
164void *
165apple_glx_get_proc_address(const GLubyte * procname)
166{
167   size_t len;
168   void *h, *s;
169   char *pname = (char *) procname;
170
171   assert(NULL != procname);
172   len = strlen(pname);
173
174   if (len < 3) {
175      return NULL;
176   }
177
178   if ((pname != strstr(pname, "glX")) && (pname != strstr(pname, "gl"))) {
179      fprintf(stderr,
180              "warning: get proc address request is not for a gl or glX function");
181      return NULL;
182   }
183
184   /* Search using the default symbols first. */
185   (void) dlerror();            /*drain dlerror */
186   h = dlopen(NULL, RTLD_NOW);
187   if (NULL == h) {
188      fprintf(stderr, "warning: get proc address: %s\n", dlerror());
189      return NULL;
190   }
191
192   s = dlsym(h, pname);
193
194   if (NULL == s) {
195      /* Try the libGL.dylib from the OpenGL.framework. */
196      s = dlsym(libgl_handle, pname);
197   }
198
199   dlclose(h);
200
201   return s;
202}
203
204void
205apple_glx_waitx(Display * dpy, void *ptr)
206{
207   struct apple_private_context *ac = ptr;
208
209   (void) ac;
210
211   glFlush();
212   glFinish();
213   XSync(dpy, False);
214}
215