blob: 3a4d08492f220bdb44f785c9047af98a3fa86224 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Config.cpp: Implements the egl::Config class, describing the format, type
8// and size for an egl::Surface. Implements EGLConfig and related functionality.
9// [EGL 1.4] section 3.4 page 15.
10
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011#include "libEGL/Config.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000012
13#include <algorithm>
14#include <vector>
15
Jamie Madillf51639a2014-06-25 16:04:57 -040016#include "angle_gl.h"
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +000017#include <EGL/eglext.h>
daniel@transgaming.com106e1f72012-10-31 18:38:36 +000018
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000019#include "common/debug.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000020
21using namespace std;
22
23namespace egl
24{
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +000025Config::Config(rx::ConfigDesc desc, EGLint minInterval, EGLint maxInterval, EGLint texWidth, EGLint texHeight)
daniel@transgaming.com3281f972012-10-31 18:38:51 +000026 : mRenderTargetFormat(desc.renderTargetFormat), mDepthStencilFormat(desc.depthStencilFormat), mMultiSample(desc.multiSample)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000027{
jbauman@chromium.orgae345802011-03-30 22:04:25 +000028 mBindToTextureRGB = EGL_FALSE;
29 mBindToTextureRGBA = EGL_FALSE;
daniel@transgaming.com3281f972012-10-31 18:38:51 +000030 switch (desc.renderTargetFormat)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031 {
daniel@transgaming.com106e1f72012-10-31 18:38:36 +000032 case GL_RGB5_A1:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000033 mBufferSize = 16;
34 mRedSize = 5;
35 mGreenSize = 5;
36 mBlueSize = 5;
37 mAlphaSize = 1;
38 break;
Geoff Langfe28ca02013-06-04 10:10:48 -040039 case GL_BGR5_A1_ANGLEX:
40 mBufferSize = 16;
41 mRedSize = 5;
42 mGreenSize = 5;
43 mBlueSize = 5;
44 mAlphaSize = 1;
45 break;
daniel@transgaming.com106e1f72012-10-31 18:38:36 +000046 case GL_RGBA8_OES:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000047 mBufferSize = 32;
48 mRedSize = 8;
49 mGreenSize = 8;
50 mBlueSize = 8;
51 mAlphaSize = 8;
jbauman@chromium.orgae345802011-03-30 22:04:25 +000052 mBindToTextureRGBA = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000053 break;
daniel@transgaming.com106e1f72012-10-31 18:38:36 +000054 case GL_RGB565:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000055 mBufferSize = 16;
56 mRedSize = 5;
57 mGreenSize = 6;
58 mBlueSize = 5;
59 mAlphaSize = 0;
60 break;
daniel@transgaming.com106e1f72012-10-31 18:38:36 +000061 case GL_RGB8_OES:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000062 mBufferSize = 32;
63 mRedSize = 8;
64 mGreenSize = 8;
65 mBlueSize = 8;
66 mAlphaSize = 0;
jbauman@chromium.orgae345802011-03-30 22:04:25 +000067 mBindToTextureRGB = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000068 break;
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +000069 case GL_BGRA8_EXT:
70 mBufferSize = 32;
71 mRedSize = 8;
72 mGreenSize = 8;
73 mBlueSize = 8;
74 mAlphaSize = 8;
75 mBindToTextureRGBA = true;
76 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000077 default:
78 UNREACHABLE(); // Other formats should not be valid
79 }
80
81 mLuminanceSize = 0;
82 mAlphaMaskSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000083 mColorBufferType = EGL_RGB_BUFFER;
daniel@transgaming.com3281f972012-10-31 18:38:51 +000084 mConfigCaveat = (desc.fastConfig) ? EGL_NONE : EGL_SLOW_CONFIG;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000085 mConfigID = 0;
Austin Kinrossaed9f942014-10-23 14:31:03 -070086 mConformant = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000087
daniel@transgaming.com3281f972012-10-31 18:38:51 +000088 switch (desc.depthStencilFormat)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000089 {
daniel@transgaming.com106e1f72012-10-31 18:38:36 +000090 case GL_NONE:
daniel@transgaming.coma114c272011-04-22 04:18:50 +000091 mDepthSize = 0;
92 mStencilSize = 0;
93 break;
daniel@transgaming.com106e1f72012-10-31 18:38:36 +000094 case GL_DEPTH_COMPONENT32_OES:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095 mDepthSize = 32;
96 mStencilSize = 0;
97 break;
daniel@transgaming.com106e1f72012-10-31 18:38:36 +000098 case GL_DEPTH24_STENCIL8_OES:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000099 mDepthSize = 24;
100 mStencilSize = 8;
101 break;
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000102 case GL_DEPTH_COMPONENT24_OES:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000103 mDepthSize = 24;
104 mStencilSize = 0;
105 break;
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000106 case GL_DEPTH_COMPONENT16:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000107 mDepthSize = 16;
108 mStencilSize = 0;
109 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000110 default:
111 UNREACHABLE();
112 }
113
114 mLevel = 0;
115 mMatchNativePixmap = EGL_NONE;
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000116 mMaxPBufferWidth = texWidth;
117 mMaxPBufferHeight = texHeight;
118 mMaxPBufferPixels = texWidth*texHeight;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000119 mMaxSwapInterval = maxInterval;
120 mMinSwapInterval = minInterval;
121 mNativeRenderable = EGL_FALSE;
122 mNativeVisualID = 0;
123 mNativeVisualType = 0;
124 mRenderableType = EGL_OPENGL_ES2_BIT;
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000125 mSampleBuffers = desc.multiSample ? 1 : 0;
126 mSamples = desc.multiSample;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000127 mSurfaceType = EGL_PBUFFER_BIT | EGL_WINDOW_BIT | EGL_SWAP_BEHAVIOR_PRESERVED_BIT;
128 mTransparentType = EGL_NONE;
129 mTransparentRedValue = 0;
130 mTransparentGreenValue = 0;
131 mTransparentBlueValue = 0;
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000132
Austin Kinrossaed9f942014-10-23 14:31:03 -0700133 if (desc.es2Conformant)
134 {
135 mConformant = EGL_OPENGL_ES2_BIT;
136 }
137
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +0000138 if (desc.es3Capable)
139 {
140 mRenderableType |= EGL_OPENGL_ES3_BIT_KHR;
141 mConformant |= EGL_OPENGL_ES3_BIT_KHR;
142 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000143}
144
145EGLConfig Config::getHandle() const
146{
147 return (EGLConfig)(size_t)mConfigID;
148}
149
150SortConfig::SortConfig(const EGLint *attribList)
151 : mWantRed(false), mWantGreen(false), mWantBlue(false), mWantAlpha(false), mWantLuminance(false)
152{
153 scanForWantedComponents(attribList);
154}
155
156void SortConfig::scanForWantedComponents(const EGLint *attribList)
157{
158 // [EGL] section 3.4.1 page 24
159 // Sorting rule #3: by larger total number of color bits, not considering
160 // components that are 0 or don't-care.
161 for (const EGLint *attr = attribList; attr[0] != EGL_NONE; attr += 2)
162 {
163 if (attr[1] != 0 && attr[1] != EGL_DONT_CARE)
164 {
165 switch (attr[0])
166 {
167 case EGL_RED_SIZE: mWantRed = true; break;
168 case EGL_GREEN_SIZE: mWantGreen = true; break;
169 case EGL_BLUE_SIZE: mWantBlue = true; break;
170 case EGL_ALPHA_SIZE: mWantAlpha = true; break;
171 case EGL_LUMINANCE_SIZE: mWantLuminance = true; break;
172 }
173 }
174 }
175}
176
177EGLint SortConfig::wantedComponentsSize(const Config &config) const
178{
179 EGLint total = 0;
180
181 if (mWantRed) total += config.mRedSize;
182 if (mWantGreen) total += config.mGreenSize;
183 if (mWantBlue) total += config.mBlueSize;
184 if (mWantAlpha) total += config.mAlphaSize;
185 if (mWantLuminance) total += config.mLuminanceSize;
186
187 return total;
188}
189
190bool SortConfig::operator()(const Config *x, const Config *y) const
191{
192 return (*this)(*x, *y);
193}
194
195bool SortConfig::operator()(const Config &x, const Config &y) const
196{
197 #define SORT(attribute) \
198 if (x.attribute != y.attribute) \
199 { \
200 return x.attribute < y.attribute; \
201 }
202
203 META_ASSERT(EGL_NONE < EGL_SLOW_CONFIG && EGL_SLOW_CONFIG < EGL_NON_CONFORMANT_CONFIG);
204 SORT(mConfigCaveat);
205
206 META_ASSERT(EGL_RGB_BUFFER < EGL_LUMINANCE_BUFFER);
207 SORT(mColorBufferType);
208
209 // By larger total number of color bits, only considering those that are requested to be > 0.
210 EGLint xComponentsSize = wantedComponentsSize(x);
211 EGLint yComponentsSize = wantedComponentsSize(y);
212 if (xComponentsSize != yComponentsSize)
213 {
214 return xComponentsSize > yComponentsSize;
215 }
216
217 SORT(mBufferSize);
218 SORT(mSampleBuffers);
219 SORT(mSamples);
220 SORT(mDepthSize);
221 SORT(mStencilSize);
222 SORT(mAlphaMaskSize);
223 SORT(mNativeVisualType);
224 SORT(mConfigID);
225
226 #undef SORT
227
228 return false;
229}
230
231// We'd like to use SortConfig to also eliminate duplicate configs.
232// This works as long as we never have two configs with different per-RGB-component layouts,
233// but the same total.
234// 5551 and 565 are different because R+G+B is different.
235// 5551 and 555 are different because bufferSize is different.
236const EGLint ConfigSet::mSortAttribs[] =
237{
238 EGL_RED_SIZE, 1,
239 EGL_GREEN_SIZE, 1,
240 EGL_BLUE_SIZE, 1,
241 EGL_LUMINANCE_SIZE, 1,
242 // BUT NOT ALPHA
243 EGL_NONE
244};
245
246ConfigSet::ConfigSet()
247 : mSet(SortConfig(mSortAttribs))
248{
249}
250
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +0000251void ConfigSet::add(rx::ConfigDesc desc, EGLint minSwapInterval, EGLint maxSwapInterval, EGLint texWidth, EGLint texHeight)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000252{
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000253 Config config(desc, minSwapInterval, maxSwapInterval, texWidth, texHeight);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000254 mSet.insert(config);
255}
256
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000257size_t ConfigSet::size() const
258{
259 return mSet.size();
260}
261
262bool ConfigSet::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig)
263{
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000264 vector<const Config*> passed;
265 passed.reserve(mSet.size());
266
267 for (Iterator config = mSet.begin(); config != mSet.end(); config++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000268 {
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000269 bool match = true;
270 const EGLint *attribute = attribList;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000271
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000272 while (attribute[0] != EGL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000273 {
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000274 switch (attribute[0])
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000275 {
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000276 case EGL_BUFFER_SIZE: match = config->mBufferSize >= attribute[1]; break;
277 case EGL_ALPHA_SIZE: match = config->mAlphaSize >= attribute[1]; break;
278 case EGL_BLUE_SIZE: match = config->mBlueSize >= attribute[1]; break;
279 case EGL_GREEN_SIZE: match = config->mGreenSize >= attribute[1]; break;
280 case EGL_RED_SIZE: match = config->mRedSize >= attribute[1]; break;
281 case EGL_DEPTH_SIZE: match = config->mDepthSize >= attribute[1]; break;
282 case EGL_STENCIL_SIZE: match = config->mStencilSize >= attribute[1]; break;
apatrick@chromium.org9c3a3932012-01-30 20:03:32 +0000283 case EGL_CONFIG_CAVEAT: match = config->mConfigCaveat == (EGLenum) attribute[1]; break;
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000284 case EGL_CONFIG_ID: match = config->mConfigID == attribute[1]; break;
285 case EGL_LEVEL: match = config->mLevel >= attribute[1]; break;
apatrick@chromium.org9c3a3932012-01-30 20:03:32 +0000286 case EGL_NATIVE_RENDERABLE: match = config->mNativeRenderable == (EGLBoolean) attribute[1]; break;
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000287 case EGL_NATIVE_VISUAL_TYPE: match = config->mNativeVisualType == attribute[1]; break;
288 case EGL_SAMPLES: match = config->mSamples >= attribute[1]; break;
289 case EGL_SAMPLE_BUFFERS: match = config->mSampleBuffers >= attribute[1]; break;
290 case EGL_SURFACE_TYPE: match = (config->mSurfaceType & attribute[1]) == attribute[1]; break;
apatrick@chromium.org9c3a3932012-01-30 20:03:32 +0000291 case EGL_TRANSPARENT_TYPE: match = config->mTransparentType == (EGLenum) attribute[1]; break;
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000292 case EGL_TRANSPARENT_BLUE_VALUE: match = config->mTransparentBlueValue == attribute[1]; break;
293 case EGL_TRANSPARENT_GREEN_VALUE: match = config->mTransparentGreenValue == attribute[1]; break;
294 case EGL_TRANSPARENT_RED_VALUE: match = config->mTransparentRedValue == attribute[1]; break;
apatrick@chromium.org9c3a3932012-01-30 20:03:32 +0000295 case EGL_BIND_TO_TEXTURE_RGB: match = config->mBindToTextureRGB == (EGLBoolean) attribute[1]; break;
296 case EGL_BIND_TO_TEXTURE_RGBA: match = config->mBindToTextureRGBA == (EGLBoolean) attribute[1]; break;
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000297 case EGL_MIN_SWAP_INTERVAL: match = config->mMinSwapInterval == attribute[1]; break;
298 case EGL_MAX_SWAP_INTERVAL: match = config->mMaxSwapInterval == attribute[1]; break;
299 case EGL_LUMINANCE_SIZE: match = config->mLuminanceSize >= attribute[1]; break;
300 case EGL_ALPHA_MASK_SIZE: match = config->mAlphaMaskSize >= attribute[1]; break;
apatrick@chromium.org9c3a3932012-01-30 20:03:32 +0000301 case EGL_COLOR_BUFFER_TYPE: match = config->mColorBufferType == (EGLenum) attribute[1]; break;
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000302 case EGL_RENDERABLE_TYPE: match = (config->mRenderableType & attribute[1]) == attribute[1]; break;
303 case EGL_MATCH_NATIVE_PIXMAP: match = false; UNIMPLEMENTED(); break;
304 case EGL_CONFORMANT: match = (config->mConformant & attribute[1]) == attribute[1]; break;
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000305 case EGL_MAX_PBUFFER_WIDTH: match = config->mMaxPBufferWidth >= attribute[1]; break;
306 case EGL_MAX_PBUFFER_HEIGHT: match = config->mMaxPBufferHeight >= attribute[1]; break;
307 case EGL_MAX_PBUFFER_PIXELS: match = config->mMaxPBufferPixels >= attribute[1]; break;
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000308 default:
309 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310 }
311
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000312 if (!match)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000313 {
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000314 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000315 }
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000316
317 attribute += 2;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000318 }
319
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000320 if (match)
321 {
322 passed.push_back(&*config);
323 }
324 }
325
326 if (configs)
327 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000328 sort(passed.begin(), passed.end(), SortConfig(attribList));
329
330 EGLint index;
331 for (index = 0; index < configSize && index < static_cast<EGLint>(passed.size()); index++)
332 {
333 configs[index] = passed[index]->getHandle();
334 }
335
336 *numConfig = index;
337 }
338 else
339 {
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000340 *numConfig = passed.size();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000341 }
342
343 return true;
344}
345
346const egl::Config *ConfigSet::get(EGLConfig configHandle)
347{
348 for (Iterator config = mSet.begin(); config != mSet.end(); config++)
349 {
350 if (config->getHandle() == configHandle)
351 {
352 return &(*config);
353 }
354 }
355
356 return NULL;
357}
358}