1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
/*
* Copyright © 2004 David Reveman
*
* Permission to use, copy, modify, distribute, and sell this software
* and its documentation for any purpose is hereby granted without
* fee, provided that the above copyright notice appear in all copies
* and that both that copyright notice and this permission notice
* appear in supporting documentation, and that the name of
* David Reveman not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior permission.
* David Reveman makes no representations about the suitability of this
* software for any purpose. It is provided "as is" without express or
* implied warranty.
*
* DAVID REVEMAN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
* NO EVENT SHALL DAVID REVEMAN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Author: David Reveman <davidr@novell.com>
*/
#ifdef HAVE_CONFIG_H
# include "../config.h"
#endif
#include "glitz_aglint.h"
extern glitz_gl_proc_address_list_t _glitz_agl_gl_proc_address;
static CFBundleRef
_glitz_agl_get_bundle (const char *name)
{
CFBundleRef bundle = 0;
FSRefParam ref_param;
unsigned char framework_name[256];
framework_name[0] = strlen (name);
strcpy (&framework_name[1], name);
memset (&ref_param, 0, sizeof (ref_param));
if (FindFolder (kSystemDomain,
kFrameworksFolderType,
kDontCreateFolder,
&ref_param.ioVRefNum,
&ref_param.ioDirID) == noErr) {
FSRef ref;
memset (&ref, 0, sizeof (ref));
ref_param.ioNamePtr = framework_name;
ref_param.newRef = &ref;
if (PBMakeFSRefSync (&ref_param) == noErr) {
CFURLRef url;
url = CFURLCreateFromFSRef (kCFAllocatorDefault, &ref);
if (url) {
bundle = CFBundleCreate (kCFAllocatorDefault, url);
CFRelease (url);
if (!CFBundleLoadExecutable (bundle)) {
CFRelease (bundle);
return (CFBundleRef) 0;
}
}
}
}
return bundle;
}
static void
_glitz_agl_release_bundle (CFBundleRef bundle)
{
if (bundle) {
CFBundleUnloadExecutable (bundle);
CFRelease (bundle);
}
}
static glitz_function_pointer_t
_glitz_agl_get_proc_address (const char *name, void *closure)
{
glitz_function_pointer_t address = NULL;
CFBundleRef bundle = (CFBundleRef) closure;
CFStringRef str;
if (bundle) {
str = CFStringCreateWithCString (kCFAllocatorDefault, name,
kCFStringEncodingMacRoman);
address = CFBundleGetFunctionPointerForName (bundle, str);
CFRelease (str);
}
return address;
}
glitz_agl_context_t *
glitz_agl_context_get (glitz_agl_thread_info_t *thread_info,
glitz_drawable_format_t *format)
{
glitz_agl_context_t *context;
glitz_agl_context_t **contexts = thread_info->contexts;
int index, n_contexts = thread_info->n_contexts;
for (; n_contexts; n_contexts--, contexts++)
if ((*contexts)->id == format->id)
return *contexts;
index = thread_info->n_contexts++;
thread_info->contexts =
realloc (thread_info->contexts,
sizeof (glitz_agl_context_t *) * thread_info->n_contexts);
if (!thread_info->contexts)
return NULL;
context = malloc (sizeof (glitz_agl_context_t));
if (!context)
return NULL;
thread_info->contexts[index] = context;
context->context =
aglCreateContext (thread_info->pixel_formats[format->id],
thread_info->root_context);
if (!context->context) {
free (context);
return NULL;
}
context->id = format->id;
context->pbuffer = 0;
if (!thread_info->root_context)
thread_info->root_context = context->context;
memcpy (&context->backend.gl,
&_glitz_agl_gl_proc_address,
sizeof (glitz_gl_proc_address_list_t));
context->backend.create_pbuffer = glitz_agl_create_pbuffer;
context->backend.destroy = glitz_agl_destroy;
context->backend.push_current = glitz_agl_push_current;
context->backend.pop_current = glitz_agl_pop_current;
context->backend.swap_buffers = glitz_agl_swap_buffers;
context->backend.make_current_read = glitz_agl_make_current_read;
context->backend.drawable_formats = thread_info->formats;
context->backend.n_drawable_formats = thread_info->n_formats;
context->backend.texture_formats = NULL;
context->backend.formats = NULL;
context->backend.n_formats = 0;
context->backend.program_map = &thread_info->program_map;
context->backend.feature_mask = 0;
context->initialized = 0;
return context;
}
void
glitz_agl_context_destroy (glitz_agl_thread_info_t *thread_info,
glitz_agl_context_t *context)
{
if (context->backend.formats)
free (context->backend.formats);
if (context->backend.texture_formats)
free (context->backend.texture_formats);
aglDestroyContext (context->context);
free (context);
}
static void
_glitz_agl_context_initialize (glitz_agl_thread_info_t *thread_info,
glitz_agl_context_t *context)
{
CFBundleRef bundle;
bundle = _glitz_agl_get_bundle ("OpenGL.framework");
glitz_backend_init (&context->backend,
_glitz_agl_get_proc_address,
(void *) bundle);
_glitz_agl_release_bundle (bundle);
context->backend.gl.get_integer_v (GLITZ_GL_MAX_VIEWPORT_DIMS,
context->max_viewport_dims);
glitz_initiate_state (&_glitz_agl_gl_proc_address);
context->initialized = 1;
}
static void
_glitz_agl_context_make_current (glitz_agl_drawable_t *drawable,
glitz_bool_t flush)
{
if (flush)
glFlush ();
if (drawable->pbuffer) {
aglSetPBuffer (drawable->context->context, drawable->pbuffer, 0, 0,
aglGetVirtualScreen (drawable->context->context));
drawable->context->pbuffer = 1;
} else {
if (drawable->context->pbuffer) {
aglSetDrawable (drawable->context->context, NULL);
drawable->context->pbuffer = 0;
}
aglSetDrawable (drawable->context->context, drawable->drawable);
}
aglSetCurrentContext (drawable->context->context);
drawable->base.update_all = 1;
if (!drawable->context->initialized)
_glitz_agl_context_initialize (drawable->thread_info, drawable->context);
}
static void
_glitz_agl_context_update (glitz_agl_drawable_t *drawable,
glitz_constraint_t constraint)
{
AGLContext context;
switch (constraint) {
case GLITZ_NONE:
break;
case GLITZ_ANY_CONTEXT_CURRENT:
context = aglGetCurrentContext ();
if (context == (AGLContext) 0)
_glitz_agl_context_make_current (drawable, 0);
break;
case GLITZ_CONTEXT_CURRENT:
context = aglGetCurrentContext ();
if (context != drawable->context->context)
_glitz_agl_context_make_current (drawable, (context)? 1: 0);
break;
case GLITZ_DRAWABLE_CURRENT:
context = aglGetCurrentContext ();
if (context != drawable->context->context) {
_glitz_agl_context_make_current (drawable, (context)? 1: 0);
} else {
if (drawable->pbuffer) {
AGLPbuffer pbuffer;
GLuint unused;
aglGetPBuffer (drawable->context->context, &pbuffer,
&unused, &unused, &unused);
if (pbuffer != drawable->pbuffer)
_glitz_agl_context_make_current (drawable, (context)? 1: 0);
} else if (drawable->drawable) {
if (aglGetDrawable (drawable->context->context) != drawable->drawable)
_glitz_agl_context_make_current (drawable, (context)? 1: 0);
}
}
break;
}
}
void
glitz_agl_push_current (void *abstract_drawable,
glitz_surface_t *surface,
glitz_constraint_t constraint)
{
glitz_agl_drawable_t *drawable = (glitz_agl_drawable_t *) abstract_drawable;
glitz_agl_context_info_t *context_info;
int index;
index = drawable->thread_info->context_stack_size++;
context_info = &drawable->thread_info->context_stack[index];
context_info->drawable = drawable;
context_info->surface = surface;
context_info->constraint = constraint;
_glitz_agl_context_update (context_info->drawable, constraint);
}
glitz_surface_t *
glitz_agl_pop_current (void *abstract_drawable)
{
glitz_agl_drawable_t *drawable = (glitz_agl_drawable_t *) abstract_drawable;
glitz_agl_context_info_t *context_info = NULL;
int index;
drawable->thread_info->context_stack_size--;
index = drawable->thread_info->context_stack_size - 1;
context_info = &drawable->thread_info->context_stack[index];
if (context_info->drawable)
_glitz_agl_context_update (context_info->drawable,
context_info->constraint);
if (context_info->constraint == GLITZ_DRAWABLE_CURRENT)
return context_info->surface;
return NULL;
}
|