1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/*
2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Copyright (c) 2008, 2009 Apple Inc.
3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Permission is hereby granted, free of charge, to any person
5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org obtaining a copy of this software and associated documentation files
6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org (the "Software"), to deal in the Software without restriction,
7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org including without limitation the rights to use, copy, modify, merge,
8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org publish, distribute, sublicense, and/or sell copies of the Software,
9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org and to permit persons to whom the Software is furnished to do so,
10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org subject to the following conditions:
11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org The above copyright notice and this permission notice shall be
13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org included in all copies or substantial portions of the Software.
14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org NONINFRINGEMENT.  IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org DEALINGS IN THE SOFTWARE.
23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org Except as contained in this notice, the name(s) of the above
25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org copyright holders shall not be used in advertising or otherwise to
26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org promote the sale, use or other dealings in this Software without
27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org prior written authorization.
28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org*/
29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <stdbool.h>
31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <stdio.h>
32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <stdlib.h>
33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <assert.h>
34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <GL/gl.h>
35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/* <rdar://problem/6953344> */
37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#define glTexImage1D glTexImage1D_OSX
38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#define glTexImage2D glTexImage2D_OSX
39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#define glTexImage3D glTexImage3D_OSX
40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <OpenGL/OpenGL.h>
41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <OpenGL/CGLContext.h>
42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <OpenGL/CGLRenderers.h>
43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include <OpenGL/CGLTypes.h>
44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#undef glTexImage1D
45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#undef glTexImage2D
46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#undef glTexImage3D
47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#ifndef kCGLPFAOpenGLProfile
49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#define kCGLPFAOpenGLProfile 99
50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif
51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#ifndef kCGLOGLPVersion_3_2_Core
53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#define kCGLOGLPVersion_3_2_Core 0x3200
54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif
55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "apple_cgl.h"
57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "apple_visual.h"
58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "apple_glx.h"
59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "glxconfig.h"
60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgenum
62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   MAX_ATTR = 60
64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org};
65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid
67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgapple_visual_create_pfobj(CGLPixelFormatObj * pfobj, const struct glx_config * mode,
68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                          bool * double_buffered, bool * uses_stereo,
69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                          bool offscreen)
70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   CGLPixelFormatAttribute attr[MAX_ATTR];
72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   int numattr = 0;
73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   GLint vsref = 0;
74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   CGLError error = 0;
75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Request an OpenGL 3.2 profile if one is available */
77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if(apple_cgl.version_major > 1 || (apple_cgl.version_major == 1 && apple_cgl.version_minor >= 3)) {
78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      attr[numattr++] = kCGLPFAOpenGLProfile;
79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      attr[numattr++] = kCGLOGLPVersion_3_2_Core;
80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (offscreen) {
83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      apple_glx_diagnostic
84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         ("offscreen rendering enabled.  Using kCGLPFAOffScreen\n");
85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      attr[numattr++] = kCGLPFAOffScreen;
87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      attr[numattr++] = kCGLPFAColorSize;
88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      attr[numattr++] = 32;
89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   else if (getenv("LIBGL_ALWAYS_SOFTWARE") != NULL) {
91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      apple_glx_diagnostic
92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         ("Software rendering requested.  Using kCGLRendererGenericFloatID.\n");
93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      attr[numattr++] = kCGLPFARendererID;
94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      attr[numattr++] = kCGLRendererGenericFloatID;
95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   else if (getenv("LIBGL_ALLOW_SOFTWARE") != NULL) {
97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      apple_glx_diagnostic
98f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         ("Software rendering is not being excluded.  Not using kCGLPFAAccelerated.\n");
99f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   else {
101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      attr[numattr++] = kCGLPFAAccelerated;
102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /*
105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * The program chose a config based on the fbconfigs or visuals.
106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * Those are based on the attributes from CGL, so we probably
107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * do want the closest match for the color, depth, and accum.
108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   attr[numattr++] = kCGLPFAClosestPolicy;
110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (mode->stereoMode) {
112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      attr[numattr++] = kCGLPFAStereo;
113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      *uses_stereo = true;
114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   else {
116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      *uses_stereo = false;
117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (!offscreen && mode->doubleBufferMode) {
120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      attr[numattr++] = kCGLPFADoubleBuffer;
121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      *double_buffered = true;
122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   else {
124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      *double_buffered = false;
125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
126f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
127f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   attr[numattr++] = kCGLPFAColorSize;
128f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   attr[numattr++] = mode->redBits + mode->greenBits + mode->blueBits;
129f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   attr[numattr++] = kCGLPFAAlphaSize;
130f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   attr[numattr++] = mode->alphaBits;
131f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
132f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if ((mode->accumRedBits + mode->accumGreenBits + mode->accumBlueBits) > 0) {
133f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      attr[numattr++] = kCGLPFAAccumSize;
134f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      attr[numattr++] = mode->accumRedBits + mode->accumGreenBits +
135f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         mode->accumBlueBits + mode->accumAlphaBits;
136f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
137f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
138f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (mode->depthBits > 0) {
139f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      attr[numattr++] = kCGLPFADepthSize;
140f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      attr[numattr++] = mode->depthBits;
141f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
142f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
143f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (mode->stencilBits > 0) {
144f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      attr[numattr++] = kCGLPFAStencilSize;
145f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      attr[numattr++] = mode->stencilBits;
146f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
147f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
148f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (mode->sampleBuffers > 0) {
149f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      attr[numattr++] = kCGLPFAMultisample;
150f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      attr[numattr++] = kCGLPFASampleBuffers;
151f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      attr[numattr++] = mode->sampleBuffers;
152f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      attr[numattr++] = kCGLPFASamples;
153f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      attr[numattr++] = mode->samples;
154f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
155f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
156f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   attr[numattr++] = 0;
157f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
158f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   assert(numattr < MAX_ATTR);
159f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
160f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   error = apple_cgl.choose_pixel_format(attr, pfobj, &vsref);
161f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
162f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (error) {
163f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      fprintf(stderr, "error: %s\n", apple_cgl.error_string(error));
164f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      abort();
165f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
166f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
167