aboutsummaryrefslogtreecommitdiff
path: root/src/waffle/cgl/cgl_config.m
blob: b4a78a6c7f2e70d973ddae84a0661dba825609a3 (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
// Copyright 2012 Intel Corporation
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// - Redistributions of source code must retain the above copyright notice, this
//   list of conditions and the following disclaimer.
//
// - Redistributions in binary form must reproduce the above copyright notice,
//   this list of conditions and the following disclaimer in the documentation
//   and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include "wcore_config_attrs.h"
#include "wcore_error.h"

#include "cgl_config.h"
#include "cgl_error.h"
#include "cgl_platform.h"

#include <AvailabilityMacros.h>

#ifndef kCGLPFAOpenGLProfile
#   define kCGLPFAOpenGLProfile     99
#   define kCGLOGLPVersion_3_2_Core 0x3200
#   define kCGLOGLPVersion_Legacy   0x1000
#endif

bool
cgl_config_destroy(struct wcore_config *wc_self)
{
    bool ok = true;

    if (wc_self == NULL)
        return ok;

    ok &= wcore_config_teardown(wc_self);
    free(cgl_config(wc_self));
    return ok;
}

/// @brief Check the values of `attrs->context_*`.
static bool
cgl_config_check_attrs(const struct cgl_platform *plat,
                       const struct wcore_config_attrs *attrs)
{
    if (attrs->context_forward_compatible) {
        // As of 2013-09-16 and up to Mac OS 10.8 Mountain Lion, CGL does not
        // support forward-compatible contexts.
        wcore_errorf(WAFFLE_ERROR_UNSUPPORTED_ON_PLATFORM,
                     "CGL does not support forward-compatible contexts");
        return false;
    }

    if (attrs->context_debug) {
        // As of 2013-09-16 and up to Mac OS 10.8 Mountain Lion, CGL does not
        // support debug contexts.
        wcore_errorf(WAFFLE_ERROR_UNSUPPORTED_ON_PLATFORM,
                     "CGL does not support debug contexts");
        return false;
    }

    // Emulate EGL_KHR_create_context, which allows the implementation to
    // return a context of the latest supported flavor that is
    // backwards-compatibile with the requested flavor.
    switch (attrs->context_api) {
        case WAFFLE_CONTEXT_OPENGL:
            if (attrs->context_profile == WAFFLE_CONTEXT_COMPATIBILITY_PROFILE) {
                wcore_errorf(WAFFLE_ERROR_UNSUPPORTED_ON_PLATFORM,
                             "CGL does not support OpenGL compatibility "
                             "profiles");
                return false;
            }

            if (wcore_config_attrs_version_gt(attrs, 32)) {
                if (plat->system_version_full >= 0x0a09) {
                    wcore_errorf(WAFFLE_ERROR_UNSUPPORTED_ON_PLATFORM,
                                 "Waffle CGL was built only with Mac OS 10.8 "
                                 "capabilities, and so cannot create an "
                                 "OpenGL > 3.2 context");
                    return false;
                }
                else {
                    wcore_errorf(WAFFLE_ERROR_UNSUPPORTED_ON_PLATFORM,
                                 "this Mac OS version cannot create an "
                                 "OpenGL > 3.2 context");
                    return false;
                }
            }
            else if (wcore_config_attrs_version_eq(attrs, 32)) {
                assert(attrs->context_profile == WAFFLE_CONTEXT_CORE_PROFILE);

                if (plat->system_version_full < 0x0a07)
                    goto error_need_os_10_7;

                return true;
            }
            else if (wcore_config_attrs_version_eq(attrs, 31)) {
                // Emulate EGL_KHR_create_context, which allows
                // implementations to promote OpenGL 3.1 to
                // OpenGL 3.2 Core Profile.
                assert(attrs->context_profile == WAFFLE_NONE);

                if (plat->system_version_full < 0x0a07)
                    goto error_need_os_10_7;

                return true;
            }
            else if (wcore_config_attrs_version_gt(attrs, 21)) {
                wcore_errorf(WAFFLE_ERROR_UNSUPPORTED_ON_PLATFORM,
                             "CGL does not support OpenGL %d.%d contexts",
                             attrs->context_major_version,
                             attrs->context_minor_version);
                return false;
            }
            else {
                assert(wcore_config_attrs_version_ge(attrs, 10));
                return true;
            }
        case WAFFLE_CONTEXT_OPENGL_ES1:
            wcore_errorf(WAFFLE_ERROR_UNSUPPORTED_ON_PLATFORM,
                         "CGL does not support OpenGL ES1");
            return false;
        case WAFFLE_CONTEXT_OPENGL_ES2:
            wcore_errorf(WAFFLE_ERROR_UNSUPPORTED_ON_PLATFORM,
                         "CGL does not support OpenGL ES2");
            return false;
        case WAFFLE_CONTEXT_OPENGL_ES3:
            wcore_errorf(WAFFLE_ERROR_UNSUPPORTED_ON_PLATFORM,
                         "CGL does not support OpenGL ES3");
            return false;
        default:
            assert(false);
            return false;
    }

error_need_os_10_7:
    wcore_errorf(WAFFLE_ERROR_UNSUPPORTED_ON_PLATFORM,
                 "Mac OS >= 10.7 is required to create an OpenGL "
                 "%d.%d context",
                 attrs->context_major_version,
                 attrs->context_minor_version);
    return false;
}

static bool
cgl_config_fill_pixel_format_attrs(
        const struct cgl_platform *plat,
        const struct wcore_config_attrs *attrs,
        CGLPixelFormatAttribute pixel_attrs[])
{
    int i = 0;

    // CGL does not have an analogue for EGL_DONT_CARE. Instead, one indicates
    // "don't care" by omitting the attribute.
    #define ADD_ATTR(name, value) \
        if ((int)(value) != WAFFLE_DONT_CARE) { \
            pixel_attrs[i++] = (name); \
            pixel_attrs[i++] = (value); \
        }

    // Emulate EGL_KHR_create_context, which allows the implementation to
    // return a context of the latest supported flavor that is
    // backwards-compatibile with the requested flavor.
    if (plat->system_version_full >= 0x0a07) {
        if (wcore_config_attrs_version_eq(attrs, 32)) {
            ADD_ATTR(kCGLPFAOpenGLProfile, (int) kCGLOGLPVersion_3_2_Core);
        }
        else if (wcore_config_attrs_version_eq(attrs, 31)) {
            ADD_ATTR(kCGLPFAOpenGLProfile, (int) kCGLOGLPVersion_3_2_Core);
        }
        else if (wcore_config_attrs_version_le(attrs, 21)) {
            ADD_ATTR(kCGLPFAOpenGLProfile, (int) kCGLOGLPVersion_Legacy);
        }
        else {
            wcore_error_internal("version=%d.%d profile=%#x",
                                 attrs->context_major_version,
                                 attrs->context_minor_version,
                                 attrs->context_profile);
            return false;
        }
    }
    else {
        // The OS doesn't recognize attribute kCGLPFAOpenGLProfile.
        assert(wcore_config_attrs_version_le(attrs, 21));
    }

    ADD_ATTR(kCGLPFAColorSize,          attrs->rgb_size);
    ADD_ATTR(kCGLPFAAlphaSize,          attrs->alpha_size);
    ADD_ATTR(kCGLPFADepthSize,          attrs->depth_size);
    ADD_ATTR(kCGLPFAStencilSize,        attrs->stencil_size);
    ADD_ATTR(kCGLPFASampleBuffers,      attrs->sample_buffers);
    ADD_ATTR(kCGLPFASamples,            attrs->samples);

    if (wcore_config_attrs_version_eq(attrs, 10))
        ADD_ATTR(kCGLPFAAccumSize,      attrs->accum_buffer);

    if (attrs->double_buffered)
        pixel_attrs[i++] = kCGLPFADoubleBuffer;

    pixel_attrs[i++] = 0;

    #undef ADD_ATTR

    return true;
}

struct wcore_config*
cgl_config_choose(struct wcore_platform *wc_plat,
                  struct wcore_display *wc_dpy,
                  const struct wcore_config_attrs *attrs)
{
    struct cgl_platform *plat = cgl_platform(wc_plat);
    struct cgl_config *self;
    bool ok = true;
    int error = 0;
    int ignore;
    CGLPixelFormatAttribute pixel_attrs[64];

    if (!cgl_config_check_attrs(plat, attrs))
        return NULL;

    self = calloc(1, sizeof(*self));
    if (!self) {
        wcore_error(WAFFLE_ERROR_BAD_ALLOC);
        return NULL;
    }

    ok = wcore_config_init(&self->wcore, wc_dpy, attrs);
    if (!ok)
        goto error;

    ok = cgl_config_fill_pixel_format_attrs(plat, attrs, pixel_attrs);
    if (!ok)
        goto error;

    error = CGLChoosePixelFormat(pixel_attrs, &self->pixel_format, &ignore);
    if (error) {
        cgl_error_failed_func("CGLChoosePixelFormat", error);
        goto error;
    }
    if (!self->pixel_format) {
        wcore_errorf(WAFFLE_ERROR_UNKNOWN,
                     "CGLChoosePixelFormat failed to find a pixel format");
        goto error;
    }

    return &self->wcore;

error:
    cgl_config_destroy(&self->wcore);
    return NULL;
}