d94c1e0fb16c0ed6b1a766cce971fb90bd86397c
[mesa.git] / src / glx / apple / apple_glx.c
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
41 static bool initialized = false;
42 static int dri_event_base = 0;
43
44 const GLuint __glXDefaultPixelStore[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 1 };
45
46 static bool diagnostic = false;
47
48 void
49 apple_glx_diagnostic(const char *fmt, ...)
50 {
51 va_list vl;
52
53 if (diagnostic) {
54 fprintf(stderr, "DIAG: ");
55
56 va_start(vl, fmt);
57 vfprintf(stderr, fmt, vl);
58 va_end(vl);
59 }
60 }
61
62 int
63 apple_get_dri_event_base(void)
64 {
65 if (!initialized) {
66 fprintf(stderr,
67 "error: dri_event_base called before apple_init_glx!\n");
68 abort();
69 }
70 return dri_event_base;
71 }
72
73 static void
74 surface_notify_handler(Display * dpy, unsigned int uid, int kind)
75 {
76
77 switch (kind) {
78 case AppleDRISurfaceNotifyDestroyed:
79 apple_glx_diagnostic("%s: surface destroyed %u\n", __func__, uid);
80 apple_glx_surface_destroy(uid);
81 break;
82
83 case AppleDRISurfaceNotifyChanged:{
84 int updated;
85
86 updated = apple_glx_context_surface_changed(uid, pthread_self());
87
88 apple_glx_diagnostic("surface notify updated %d\n", updated);
89 }
90 break;
91
92 default:
93 fprintf(stderr, "unhandled kind of event: %d in %s\n", kind, __func__);
94 }
95 }
96
97 xp_client_id
98 apple_glx_get_client_id(void)
99 {
100 static xp_client_id id;
101
102 if (0 == id) {
103 if ((XP_Success != xp_init(XP_IN_BACKGROUND)) ||
104 (Success != xp_get_client_id(&id))) {
105 return 0;
106 }
107 }
108
109 return id;
110 }
111
112 /* Return true if an error occured. */
113 bool
114 apple_init_glx(Display * dpy)
115 {
116 int eventBase, errorBase;
117 int major, minor, patch;
118
119 if (!XAppleDRIQueryExtension(dpy, &eventBase, &errorBase))
120 return true;
121
122 if (!XAppleDRIQueryVersion(dpy, &major, &minor, &patch))
123 return true;
124
125 if (initialized)
126 return false;
127
128 if (getenv("LIBGL_DIAGNOSTIC")) {
129 printf("initializing libGL in %s\n", __func__);
130 diagnostic = true;
131 }
132
133 apple_cgl_init();
134 (void) apple_glx_get_client_id();
135
136 XAppleDRISetSurfaceNotifyHandler(surface_notify_handler);
137
138 /* This should really be per display. */
139 dri_event_base = eventBase;
140 initialized = true;
141
142 return false;
143 }
144
145 void
146 apple_glx_swap_buffers(void *ptr)
147 {
148 struct apple_glx_context *ac = ptr;
149
150 /* This may not be needed with CGLFlushDrawable: */
151 glFlush();
152 apple_cgl.flush_drawable(ac->context_obj);
153 }
154
155 void
156 apple_glx_waitx(Display * dpy, void *ptr)
157 {
158 struct apple_private_context *ac = ptr;
159
160 (void) ac;
161
162 glFlush();
163 glFinish();
164 XSync(dpy, False);
165 }