summaryrefslogtreecommitdiff
path: root/src/glitz_agl_format.c
blob: 1b945b939620c565df5e4fed67361db41e6d1dc5 (plain)
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
 * Copyright © 2004 David Reveman, Peter Nilsson
 *
 * 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 names of
 * David Reveman and Peter Nilsson not be used in advertising or
 * publicity pertaining to distribution of the software without
 * specific, written prior permission. David Reveman and Peter Nilsson
 * makes no representations about the suitability of this software for
 * any purpose. It is provided "as is" without express or implied warranty.
 *
 * DAVID REVEMAN AND PETER NILSSON DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL DAVID REVEMAN AND
 * PETER NILSSON 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.
 *
 * Authors: David Reveman <c99drn@cs.umu.se>
 *          Peter Nilsson <c99pnn@cs.umu.se>
 */

#ifdef HAVE_CONFIG_H
#  include "../config.h"
#endif

#include "glitz_aglint.h"

#include <stdlib.h>

extern glitz_gl_proc_address_list_t _glitz_agl_gl_proc_address;

static const struct _glx_pixel_format_attrib {
  GLint attrib[20];
} pixel_format_attrib_map[] = {
  {
    {
      AGL_RGBA,
      AGL_DOUBLEBUFFER,
      AGL_RED_SIZE, 8,
      AGL_GREEN_SIZE, 8,
      AGL_BLUE_SIZE, 8,
      AGL_ALPHA_SIZE, 0,
      AGL_NO_RECOVERY,
      AGL_NONE, 0, 0, 0, 0, 0, 0, 0, 0
    }
  }, {
    {
      AGL_RGBA,
      AGL_DOUBLEBUFFER,
      AGL_RED_SIZE, 8,
      AGL_GREEN_SIZE, 8,
      AGL_BLUE_SIZE, 8,
      AGL_ALPHA_SIZE, 8,
      AGL_NO_RECOVERY,
      AGL_NONE, 0, 0, 0, 0, 0, 0, 0, 0
    }
  }, {
    {
      AGL_RGBA,
      AGL_DOUBLEBUFFER,
      AGL_RED_SIZE, 8,
      AGL_GREEN_SIZE, 8,
      AGL_BLUE_SIZE, 8,
      AGL_NO_RECOVERY,
      AGL_SAMPLE_BUFFERS_ARB, 1,
      AGL_SAMPLES_ARB, 2,
      AGL_NONE, 0, 0, 0, 0
    }
  }, {
    {
      AGL_RGBA,
      AGL_DOUBLEBUFFER,
      AGL_RED_SIZE, 8,
      AGL_GREEN_SIZE, 8,
      AGL_BLUE_SIZE, 8,
      AGL_NO_RECOVERY,
      AGL_SAMPLE_BUFFERS_ARB, 1,
      AGL_SAMPLES_ARB, 4,
      AGL_NONE, 0, 0, 0, 0
    }
  }, {
    {
      AGL_RGBA,
      AGL_DOUBLEBUFFER,
      AGL_RED_SIZE, 8,
      AGL_GREEN_SIZE, 8,
      AGL_BLUE_SIZE, 8,
      AGL_ALPHA_SIZE, 0,
      AGL_DEPTH_SIZE, 1,
      AGL_NO_RECOVERY,
      AGL_NONE, 0, 0, 0, 0, 0, 0
    }
  }, {
    {
      AGL_RGBA,
      AGL_DOUBLEBUFFER,
      AGL_RED_SIZE, 8,
      AGL_GREEN_SIZE, 8,
      AGL_BLUE_SIZE, 8,
      AGL_ALPHA_SIZE, 8,
      AGL_DEPTH_SIZE, 1,
      AGL_NO_RECOVERY,
      AGL_NONE, 0, 0, 0, 0, 0, 0
    }
  }, {
    {
      AGL_RGBA,
      AGL_DOUBLEBUFFER,
      AGL_RED_SIZE, 8,
      AGL_GREEN_SIZE, 8,
      AGL_BLUE_SIZE, 8,
      AGL_DEPTH_SIZE, 1,
      AGL_NO_RECOVERY,
      AGL_SAMPLE_BUFFERS_ARB, 1,
      AGL_SAMPLES_ARB, 2,
      AGL_NONE, 0, 0
    }
  }, {
    {
      AGL_RGBA,
      AGL_DOUBLEBUFFER,
      AGL_RED_SIZE, 8,
      AGL_GREEN_SIZE, 8,
      AGL_BLUE_SIZE, 8,
      AGL_DEPTH_SIZE, 1,
      AGL_NO_RECOVERY,
      AGL_SAMPLE_BUFFERS_ARB, 1,
      AGL_SAMPLES_ARB, 4,
      AGL_NONE, 0, 0
    }
  }, {
    {
      AGL_RGBA,
      AGL_DOUBLEBUFFER,
      AGL_RED_SIZE, 8,
      AGL_GREEN_SIZE, 8,
      AGL_BLUE_SIZE, 8,
      AGL_ALPHA_SIZE, 0,
      AGL_DEPTH_SIZE, 1,
      AGL_STENCIL_SIZE, 2,
      AGL_NO_RECOVERY,
      AGL_NONE, 0, 0, 0, 0
    }
  }, {
    {
      AGL_RGBA,
      AGL_DOUBLEBUFFER,
      AGL_RED_SIZE, 8,
      AGL_GREEN_SIZE, 8,
      AGL_BLUE_SIZE, 8,
      AGL_ALPHA_SIZE, 8,
      AGL_DEPTH_SIZE, 1,
      AGL_STENCIL_SIZE, 2,
      AGL_NO_RECOVERY,
      AGL_NONE, 0, 0, 0, 0
    }
  }, {
    {
      AGL_RGBA,
      AGL_DOUBLEBUFFER,
      AGL_RED_SIZE, 8,
      AGL_GREEN_SIZE, 8,
      AGL_BLUE_SIZE, 8,
      AGL_DEPTH_SIZE, 1,
      AGL_STENCIL_SIZE, 2,
      AGL_NO_RECOVERY,
      AGL_SAMPLE_BUFFERS_ARB, 1,
      AGL_SAMPLES_ARB, 2,
      AGL_NONE,
    }
  }, {
    {
      AGL_RGBA,
      AGL_DOUBLEBUFFER,
      AGL_RED_SIZE, 8,
      AGL_GREEN_SIZE, 8,
      AGL_BLUE_SIZE, 8,
      AGL_DEPTH_SIZE, 1,
      AGL_STENCIL_SIZE, 2,
      AGL_NO_RECOVERY,
      AGL_SAMPLE_BUFFERS_ARB, 1,
      AGL_SAMPLES_ARB, 4,
      AGL_NONE,
    }
  }, {
    {
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    }
  }
};

static int
_glitz_agl_format_compare (const void *elem1,
                           const void *elem2)
{
  int i, score[2];
  glitz_format_t *format[2];
  
  format[0] = (glitz_format_t *) elem1;
  format[1] = (glitz_format_t *) elem2;
  i = score[0] = score[1] = 0;

  for (; i < 2; i++) {
    if (format[i]->red_size)
      score[i] += 10;
    if (format[i]->alpha_size)
      score[i] += 10;
    if (format[i]->stencil_size)
      score[i] += (10 + format[i]->stencil_size);
    if (format[i]->multisample.supported) 
      score[i] -= (10 - format[i]->multisample.samples);
  }
  
  return score[1] - score[0];
}

static void
_glitz_add_format (glitz_agl_thread_info_t *thread_info,
                   glitz_format_t *format)
{
  if (!glitz_format_find (thread_info->formats, thread_info->n_formats,
                          GLITZ_FORMAT_ALL_EXCEPT_ID_MASK, format, 0)) {
    int index = thread_info->n_formats++;
    
    thread_info->formats =
      realloc (thread_info->formats,
               sizeof (glitz_format_t) * thread_info->n_formats);
  
    thread_info->formats[index] = *format;
  }
}

static void
_glitz_move_out_ids (glitz_agl_thread_info_t *thread_info)
{
  int i;
  glitz_format_t *formats = thread_info->formats;
  int n_formats = thread_info->n_formats;

  thread_info->format_ids = malloc (sizeof (AGLPixelFormat) * n_formats);
  
  for (i = 0; n_formats; n_formats--, formats++) {
    thread_info->format_ids[i] = (AGLPixelFormat) formats->id;
    formats->id = i++;
  }
}

static void
_glitz_agl_add_texture_format (glitz_format_t *texture_format, void *ptr)
{
  glitz_agl_thread_info_t *thread_info = (glitz_agl_thread_info_t *) ptr;
  glitz_format_t format;

  format = *texture_format;
  format.id = 0;

  _glitz_add_format (thread_info, &format);
}

void
glitz_agl_query_formats (glitz_agl_thread_info_t *thread_info)
{
  glitz_format_t format;
  AGLPixelFormat pixel_format;
  int i = 0;

  for (i = 0; *(pixel_format_attrib_map[i].attrib); i++) {
    GLint value;

    pixel_format = aglChoosePixelFormat (NULL, 0,
                                         pixel_format_attrib_map[i].attrib);

    /* Stereo is not supported yet */
    if (!(aglDescribePixelFormat (pixel_format, AGL_STEREO, &value)) ||
        value) {
      aglDestroyPixelFormat (pixel_format);
      continue;
    }

    aglDescribePixelFormat (pixel_format, AGL_DOUBLEBUFFER, &value);
    format.doublebuffer = (value)? 1: 0;

    /* We don't support single buffering in MacOS X */
    if (!format.doublebuffer) {
      aglDestroyPixelFormat (pixel_format);
      continue;
    }

    format.id = (unsigned long int) pixel_format;

    format.read.onscreen = format.draw.onscreen = 1;
    if (thread_info->agl_feature_mask & GLITZ_AGL_FEATURE_PBUFFER_MASK)
      format.read.offscreen = format.draw.offscreen = 1;
    else
      format.read.offscreen = format.draw.offscreen = 0;
      
    aglDescribePixelFormat (pixel_format, AGL_RED_SIZE, &value);
    format.red_size = (unsigned short) value;
    aglDescribePixelFormat (pixel_format, AGL_GREEN_SIZE, &value);
    format.green_size = (unsigned short) value;
    aglDescribePixelFormat (pixel_format, AGL_BLUE_SIZE, &value);
    format.blue_size = (unsigned short) value;
    aglDescribePixelFormat (pixel_format, AGL_ALPHA_SIZE, &value);
    format.alpha_size = (unsigned short) value;
    aglDescribePixelFormat (pixel_format, AGL_DEPTH_SIZE, &value);
    format.depth_size = (unsigned short) value;
    aglDescribePixelFormat (pixel_format, AGL_STENCIL_SIZE, &value);
    format.stencil_size = (unsigned short) value;
    
    if (thread_info->feature_mask & GLITZ_FEATURE_MULTISAMPLE_MASK) {
      aglDescribePixelFormat (pixel_format, AGL_SAMPLE_BUFFERS_ARB, &value);
      format.multisample.supported = (value)? 1: 0;
      aglDescribePixelFormat (pixel_format, AGL_SAMPLES_ARB, &value);
      format.multisample.samples = (unsigned short) value;

      if (format.multisample.supported) {
        if (!(thread_info->feature_mask &
              GLITZ_FEATURE_OFFSCREEN_MULTISAMPLE_MASK))
          format.read.offscreen = format.draw.offscreen = 0;
      }
    } else {
      format.multisample.supported = 0;
      format.multisample.samples = 0;
    }

    if (format.red_size || format.green_size || format.blue_size ||
        format.alpha_size)
      _glitz_add_format (thread_info, &format);
  }

  qsort (thread_info->formats, thread_info->n_formats,
         sizeof (glitz_format_t), _glitz_agl_format_compare);

  glitz_format_for_each_texture_format (&_glitz_agl_add_texture_format,
                                        &_glitz_agl_gl_proc_address,
                                        (void *) thread_info);
  
  _glitz_move_out_ids (thread_info);
}

glitz_format_t *
glitz_agl_find_format (unsigned long mask,
                       const glitz_format_t *templ,
                       int count)
{
  glitz_agl_thread_info_t *thread_info = glitz_agl_thread_info_get ();

  return glitz_format_find (thread_info->formats, thread_info->n_formats,
                            mask, templ, count);
}
slim_hidden_def(glitz_agl_find_format);

glitz_format_t *
glitz_agl_find_standard_format (glitz_format_name_t format_name)
{
  glitz_agl_thread_info_t *thread_info = glitz_agl_thread_info_get ();
  
  return
    glitz_format_find_standard (thread_info->formats, thread_info->n_formats,
                                format_name);
}
slim_hidden_def(glitz_agl_find_standard_format);