1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci Copyright (c) 2008, 2009 Apple Inc.
3bf215546Sopenharmony_ci
4bf215546Sopenharmony_ci Permission is hereby granted, free of charge, to any person
5bf215546Sopenharmony_ci obtaining a copy of this software and associated documentation files
6bf215546Sopenharmony_ci (the "Software"), to deal in the Software without restriction,
7bf215546Sopenharmony_ci including without limitation the rights to use, copy, modify, merge,
8bf215546Sopenharmony_ci publish, distribute, sublicense, and/or sell copies of the Software,
9bf215546Sopenharmony_ci and to permit persons to whom the Software is furnished to do so,
10bf215546Sopenharmony_ci subject to the following conditions:
11bf215546Sopenharmony_ci
12bf215546Sopenharmony_ci The above copyright notice and this permission notice shall be
13bf215546Sopenharmony_ci included in all copies or substantial portions of the Software.
14bf215546Sopenharmony_ci
15bf215546Sopenharmony_ci THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16bf215546Sopenharmony_ci EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17bf215546Sopenharmony_ci MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18bf215546Sopenharmony_ci NONINFRINGEMENT.  IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
19bf215546Sopenharmony_ci HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20bf215546Sopenharmony_ci WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21bf215546Sopenharmony_ci OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22bf215546Sopenharmony_ci DEALINGS IN THE SOFTWARE.
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci Except as contained in this notice, the name(s) of the above
25bf215546Sopenharmony_ci copyright holders shall not be used in advertising or otherwise to
26bf215546Sopenharmony_ci promote the sale, use or other dealings in this Software without
27bf215546Sopenharmony_ci prior written authorization.
28bf215546Sopenharmony_ci*/
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include <stdbool.h>
31bf215546Sopenharmony_ci#include <stdio.h>
32bf215546Sopenharmony_ci#include <stdlib.h>
33bf215546Sopenharmony_ci#include <assert.h>
34bf215546Sopenharmony_ci#include <GL/gl.h>
35bf215546Sopenharmony_ci#include <util/debug.h>
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci/* <rdar://problem/6953344> */
38bf215546Sopenharmony_ci#define glTexImage1D glTexImage1D_OSX
39bf215546Sopenharmony_ci#define glTexImage2D glTexImage2D_OSX
40bf215546Sopenharmony_ci#define glTexImage3D glTexImage3D_OSX
41bf215546Sopenharmony_ci#include <OpenGL/OpenGL.h>
42bf215546Sopenharmony_ci#include <OpenGL/CGLContext.h>
43bf215546Sopenharmony_ci#include <OpenGL/CGLRenderers.h>
44bf215546Sopenharmony_ci#include <OpenGL/CGLTypes.h>
45bf215546Sopenharmony_ci#undef glTexImage1D
46bf215546Sopenharmony_ci#undef glTexImage2D
47bf215546Sopenharmony_ci#undef glTexImage3D
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci#ifndef kCGLPFAOpenGLProfile
50bf215546Sopenharmony_ci#define kCGLPFAOpenGLProfile 99
51bf215546Sopenharmony_ci#endif
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci#ifndef kCGLOGLPVersion_3_2_Core
54bf215546Sopenharmony_ci#define kCGLOGLPVersion_3_2_Core 0x3200
55bf215546Sopenharmony_ci#endif
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci#include "apple_cgl.h"
58bf215546Sopenharmony_ci#include "apple_visual.h"
59bf215546Sopenharmony_ci#include "apple_glx.h"
60bf215546Sopenharmony_ci#include "glxconfig.h"
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_cienum
63bf215546Sopenharmony_ci{
64bf215546Sopenharmony_ci   MAX_ATTR = 60
65bf215546Sopenharmony_ci};
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_cistatic char __crashreporter_info_buff__[4096] = { 0 };
68bf215546Sopenharmony_cistatic const char *__crashreporter_info__ __attribute__((__used__)) =
69bf215546Sopenharmony_ci    &__crashreporter_info_buff__[0];
70bf215546Sopenharmony_ci#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
71bf215546Sopenharmony_ci// This is actually a toolchain requirement, but I'm not sure the correct check,
72bf215546Sopenharmony_ci// but it should be fine to just only include it for Leopard and later.  This line
73bf215546Sopenharmony_ci// just tells the linker to never strip this symbol (such as for space optimization)
74bf215546Sopenharmony_ci__asm__ (".desc ___crashreporter_info__, 0x10");
75bf215546Sopenharmony_ci#endif
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_civoid
78bf215546Sopenharmony_ciapple_visual_create_pfobj(CGLPixelFormatObj * pfobj, const struct glx_config * mode,
79bf215546Sopenharmony_ci                          bool * double_buffered, bool * uses_stereo,
80bf215546Sopenharmony_ci                          bool offscreen)
81bf215546Sopenharmony_ci{
82bf215546Sopenharmony_ci   CGLPixelFormatAttribute attr[MAX_ATTR];
83bf215546Sopenharmony_ci   int numattr = 0;
84bf215546Sopenharmony_ci   GLint vsref = 0;
85bf215546Sopenharmony_ci   CGLError error = 0;
86bf215546Sopenharmony_ci   bool use_core_profile = env_var_as_boolean("LIBGL_PROFILE_CORE", false);
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci   if (offscreen) {
89bf215546Sopenharmony_ci      apple_glx_diagnostic
90bf215546Sopenharmony_ci         ("offscreen rendering enabled.  Using kCGLPFAOffScreen\n");
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci      attr[numattr++] = kCGLPFAOffScreen;
93bf215546Sopenharmony_ci   }
94bf215546Sopenharmony_ci   else if (env_var_as_boolean("LIBGL_ALWAYS_SOFTWARE", false)) {
95bf215546Sopenharmony_ci      apple_glx_diagnostic
96bf215546Sopenharmony_ci         ("Software rendering requested.  Using kCGLRendererGenericFloatID.\n");
97bf215546Sopenharmony_ci      attr[numattr++] = kCGLPFARendererID;
98bf215546Sopenharmony_ci      attr[numattr++] = kCGLRendererGenericFloatID;
99bf215546Sopenharmony_ci   }
100bf215546Sopenharmony_ci   else if (env_var_as_boolean("LIBGL_ALLOW_SOFTWARE", false)) {
101bf215546Sopenharmony_ci      apple_glx_diagnostic
102bf215546Sopenharmony_ci         ("Software rendering is not being excluded.  Not using kCGLPFAAccelerated.\n");
103bf215546Sopenharmony_ci   }
104bf215546Sopenharmony_ci   else {
105bf215546Sopenharmony_ci      attr[numattr++] = kCGLPFAAccelerated;
106bf215546Sopenharmony_ci   }
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci   /*
109bf215546Sopenharmony_ci    * The program chose a config based on the fbconfigs or visuals.
110bf215546Sopenharmony_ci    * Those are based on the attributes from CGL, so we probably
111bf215546Sopenharmony_ci    * do want the closest match for the color, depth, and accum.
112bf215546Sopenharmony_ci    */
113bf215546Sopenharmony_ci   attr[numattr++] = kCGLPFAClosestPolicy;
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_ci   if (mode->stereoMode) {
116bf215546Sopenharmony_ci      attr[numattr++] = kCGLPFAStereo;
117bf215546Sopenharmony_ci      *uses_stereo = true;
118bf215546Sopenharmony_ci   }
119bf215546Sopenharmony_ci   else {
120bf215546Sopenharmony_ci      *uses_stereo = false;
121bf215546Sopenharmony_ci   }
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_ci   if (!offscreen && mode->doubleBufferMode) {
124bf215546Sopenharmony_ci      attr[numattr++] = kCGLPFADoubleBuffer;
125bf215546Sopenharmony_ci      *double_buffered = true;
126bf215546Sopenharmony_ci   }
127bf215546Sopenharmony_ci   else {
128bf215546Sopenharmony_ci      *double_buffered = false;
129bf215546Sopenharmony_ci   }
130bf215546Sopenharmony_ci
131bf215546Sopenharmony_ci   attr[numattr++] = kCGLPFAColorSize;
132bf215546Sopenharmony_ci   attr[numattr++] = mode->redBits + mode->greenBits + mode->blueBits;
133bf215546Sopenharmony_ci   attr[numattr++] = kCGLPFAAlphaSize;
134bf215546Sopenharmony_ci   attr[numattr++] = mode->alphaBits;
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci   if ((mode->accumRedBits + mode->accumGreenBits + mode->accumBlueBits) > 0) {
137bf215546Sopenharmony_ci      attr[numattr++] = kCGLPFAAccumSize;
138bf215546Sopenharmony_ci      attr[numattr++] = mode->accumRedBits + mode->accumGreenBits +
139bf215546Sopenharmony_ci         mode->accumBlueBits + mode->accumAlphaBits;
140bf215546Sopenharmony_ci   }
141bf215546Sopenharmony_ci
142bf215546Sopenharmony_ci   if (mode->depthBits > 0) {
143bf215546Sopenharmony_ci      attr[numattr++] = kCGLPFADepthSize;
144bf215546Sopenharmony_ci      attr[numattr++] = mode->depthBits;
145bf215546Sopenharmony_ci   }
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci   if (mode->stencilBits > 0) {
148bf215546Sopenharmony_ci      attr[numattr++] = kCGLPFAStencilSize;
149bf215546Sopenharmony_ci      attr[numattr++] = mode->stencilBits;
150bf215546Sopenharmony_ci   }
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_ci   if (mode->sampleBuffers > 0) {
153bf215546Sopenharmony_ci      attr[numattr++] = kCGLPFAMultisample;
154bf215546Sopenharmony_ci      attr[numattr++] = kCGLPFASampleBuffers;
155bf215546Sopenharmony_ci      attr[numattr++] = mode->sampleBuffers;
156bf215546Sopenharmony_ci      attr[numattr++] = kCGLPFASamples;
157bf215546Sopenharmony_ci      attr[numattr++] = mode->samples;
158bf215546Sopenharmony_ci   }
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_ci   /* Debugging support for Core profiles to support newer versions of OpenGL */
161bf215546Sopenharmony_ci   if (use_core_profile) {
162bf215546Sopenharmony_ci      attr[numattr++] = kCGLPFAOpenGLProfile;
163bf215546Sopenharmony_ci      attr[numattr++] = kCGLOGLPVersion_3_2_Core;
164bf215546Sopenharmony_ci   }
165bf215546Sopenharmony_ci
166bf215546Sopenharmony_ci   attr[numattr++] = 0;
167bf215546Sopenharmony_ci
168bf215546Sopenharmony_ci   assert(numattr < MAX_ATTR);
169bf215546Sopenharmony_ci
170bf215546Sopenharmony_ci   error = apple_cgl.choose_pixel_format(attr, pfobj, &vsref);
171bf215546Sopenharmony_ci
172bf215546Sopenharmony_ci   if ((error == kCGLBadAttribute || vsref == 0) && use_core_profile) {
173bf215546Sopenharmony_ci      apple_glx_diagnostic
174bf215546Sopenharmony_ci         ("Trying again without CoreProfile: error=%s, vsref=%d\n", apple_cgl.error_string(error), vsref);
175bf215546Sopenharmony_ci
176bf215546Sopenharmony_ci      if (!error)
177bf215546Sopenharmony_ci         apple_cgl.destroy_pixel_format(*pfobj);
178bf215546Sopenharmony_ci
179bf215546Sopenharmony_ci      numattr -= 3;
180bf215546Sopenharmony_ci      attr[numattr++] = 0;
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_ci      error = apple_cgl.choose_pixel_format(attr, pfobj, &vsref);
183bf215546Sopenharmony_ci   }
184bf215546Sopenharmony_ci
185bf215546Sopenharmony_ci   if (error) {
186bf215546Sopenharmony_ci      snprintf(__crashreporter_info_buff__, sizeof(__crashreporter_info_buff__),
187bf215546Sopenharmony_ci               "CGLChoosePixelFormat error: %s\n", apple_cgl.error_string(error));
188bf215546Sopenharmony_ci      fprintf(stderr, "%s", __crashreporter_info_buff__);
189bf215546Sopenharmony_ci      abort();
190bf215546Sopenharmony_ci   }
191bf215546Sopenharmony_ci
192bf215546Sopenharmony_ci   if (!*pfobj) {
193bf215546Sopenharmony_ci      snprintf(__crashreporter_info_buff__, sizeof(__crashreporter_info_buff__),
194bf215546Sopenharmony_ci               "No matching pixelformats found, perhaps try setting LIBGL_ALLOW_SOFTWARE=true\n");
195bf215546Sopenharmony_ci      fprintf(stderr, "%s", __crashreporter_info_buff__);
196bf215546Sopenharmony_ci      abort();
197bf215546Sopenharmony_ci   }
198bf215546Sopenharmony_ci}
199