blob: 5488cb6f4f121e74e27e30fc3736129dc35d034b [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
daniel@transgaming.com106e1f72012-10-31 18:38:36 +000016#include <GLES2/gl2.h>
17#include <GLES2/gl2ext.h>
18
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;
daniel@transgaming.com106e1f72012-10-31 18:38:36 +000039 case GL_RGBA8_OES:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000040 mBufferSize = 32;
41 mRedSize = 8;
42 mGreenSize = 8;
43 mBlueSize = 8;
44 mAlphaSize = 8;
jbauman@chromium.orgae345802011-03-30 22:04:25 +000045 mBindToTextureRGBA = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000046 break;
daniel@transgaming.com106e1f72012-10-31 18:38:36 +000047 case GL_RGB565:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000048 mBufferSize = 16;
49 mRedSize = 5;
50 mGreenSize = 6;
51 mBlueSize = 5;
52 mAlphaSize = 0;
53 break;
daniel@transgaming.com106e1f72012-10-31 18:38:36 +000054 case GL_RGB8_OES:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000055 mBufferSize = 32;
56 mRedSize = 8;
57 mGreenSize = 8;
58 mBlueSize = 8;
59 mAlphaSize = 0;
jbauman@chromium.orgae345802011-03-30 22:04:25 +000060 mBindToTextureRGB = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000061 break;
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +000062 case GL_BGRA8_EXT:
63 mBufferSize = 32;
64 mRedSize = 8;
65 mGreenSize = 8;
66 mBlueSize = 8;
67 mAlphaSize = 8;
68 mBindToTextureRGBA = true;
69 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000070 default:
71 UNREACHABLE(); // Other formats should not be valid
72 }
73
74 mLuminanceSize = 0;
75 mAlphaMaskSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000076 mColorBufferType = EGL_RGB_BUFFER;
daniel@transgaming.com3281f972012-10-31 18:38:51 +000077 mConfigCaveat = (desc.fastConfig) ? EGL_NONE : EGL_SLOW_CONFIG;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000078 mConfigID = 0;
79 mConformant = EGL_OPENGL_ES2_BIT;
80
daniel@transgaming.com3281f972012-10-31 18:38:51 +000081 switch (desc.depthStencilFormat)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000082 {
daniel@transgaming.com106e1f72012-10-31 18:38:36 +000083 case GL_NONE:
daniel@transgaming.coma114c272011-04-22 04:18:50 +000084 mDepthSize = 0;
85 mStencilSize = 0;
86 break;
daniel@transgaming.com106e1f72012-10-31 18:38:36 +000087 case GL_DEPTH_COMPONENT32_OES:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000088 mDepthSize = 32;
89 mStencilSize = 0;
90 break;
daniel@transgaming.com106e1f72012-10-31 18:38:36 +000091 case GL_DEPTH24_STENCIL8_OES:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000092 mDepthSize = 24;
93 mStencilSize = 8;
94 break;
daniel@transgaming.com106e1f72012-10-31 18:38:36 +000095 case GL_DEPTH_COMPONENT24_OES:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000096 mDepthSize = 24;
97 mStencilSize = 0;
98 break;
daniel@transgaming.com106e1f72012-10-31 18:38:36 +000099 case GL_DEPTH_COMPONENT16:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000100 mDepthSize = 16;
101 mStencilSize = 0;
102 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000103 default:
104 UNREACHABLE();
105 }
106
107 mLevel = 0;
108 mMatchNativePixmap = EGL_NONE;
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000109 mMaxPBufferWidth = texWidth;
110 mMaxPBufferHeight = texHeight;
111 mMaxPBufferPixels = texWidth*texHeight;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000112 mMaxSwapInterval = maxInterval;
113 mMinSwapInterval = minInterval;
114 mNativeRenderable = EGL_FALSE;
115 mNativeVisualID = 0;
116 mNativeVisualType = 0;
117 mRenderableType = EGL_OPENGL_ES2_BIT;
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000118 mSampleBuffers = desc.multiSample ? 1 : 0;
119 mSamples = desc.multiSample;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000120 mSurfaceType = EGL_PBUFFER_BIT | EGL_WINDOW_BIT | EGL_SWAP_BEHAVIOR_PRESERVED_BIT;
121 mTransparentType = EGL_NONE;
122 mTransparentRedValue = 0;
123 mTransparentGreenValue = 0;
124 mTransparentBlueValue = 0;
125}
126
127EGLConfig Config::getHandle() const
128{
129 return (EGLConfig)(size_t)mConfigID;
130}
131
132SortConfig::SortConfig(const EGLint *attribList)
133 : mWantRed(false), mWantGreen(false), mWantBlue(false), mWantAlpha(false), mWantLuminance(false)
134{
135 scanForWantedComponents(attribList);
136}
137
138void SortConfig::scanForWantedComponents(const EGLint *attribList)
139{
140 // [EGL] section 3.4.1 page 24
141 // Sorting rule #3: by larger total number of color bits, not considering
142 // components that are 0 or don't-care.
143 for (const EGLint *attr = attribList; attr[0] != EGL_NONE; attr += 2)
144 {
145 if (attr[1] != 0 && attr[1] != EGL_DONT_CARE)
146 {
147 switch (attr[0])
148 {
149 case EGL_RED_SIZE: mWantRed = true; break;
150 case EGL_GREEN_SIZE: mWantGreen = true; break;
151 case EGL_BLUE_SIZE: mWantBlue = true; break;
152 case EGL_ALPHA_SIZE: mWantAlpha = true; break;
153 case EGL_LUMINANCE_SIZE: mWantLuminance = true; break;
154 }
155 }
156 }
157}
158
159EGLint SortConfig::wantedComponentsSize(const Config &config) const
160{
161 EGLint total = 0;
162
163 if (mWantRed) total += config.mRedSize;
164 if (mWantGreen) total += config.mGreenSize;
165 if (mWantBlue) total += config.mBlueSize;
166 if (mWantAlpha) total += config.mAlphaSize;
167 if (mWantLuminance) total += config.mLuminanceSize;
168
169 return total;
170}
171
172bool SortConfig::operator()(const Config *x, const Config *y) const
173{
174 return (*this)(*x, *y);
175}
176
177bool SortConfig::operator()(const Config &x, const Config &y) const
178{
179 #define SORT(attribute) \
180 if (x.attribute != y.attribute) \
181 { \
182 return x.attribute < y.attribute; \
183 }
184
185 META_ASSERT(EGL_NONE < EGL_SLOW_CONFIG && EGL_SLOW_CONFIG < EGL_NON_CONFORMANT_CONFIG);
186 SORT(mConfigCaveat);
187
188 META_ASSERT(EGL_RGB_BUFFER < EGL_LUMINANCE_BUFFER);
189 SORT(mColorBufferType);
190
191 // By larger total number of color bits, only considering those that are requested to be > 0.
192 EGLint xComponentsSize = wantedComponentsSize(x);
193 EGLint yComponentsSize = wantedComponentsSize(y);
194 if (xComponentsSize != yComponentsSize)
195 {
196 return xComponentsSize > yComponentsSize;
197 }
198
199 SORT(mBufferSize);
200 SORT(mSampleBuffers);
201 SORT(mSamples);
202 SORT(mDepthSize);
203 SORT(mStencilSize);
204 SORT(mAlphaMaskSize);
205 SORT(mNativeVisualType);
206 SORT(mConfigID);
207
208 #undef SORT
209
210 return false;
211}
212
213// We'd like to use SortConfig to also eliminate duplicate configs.
214// This works as long as we never have two configs with different per-RGB-component layouts,
215// but the same total.
216// 5551 and 565 are different because R+G+B is different.
217// 5551 and 555 are different because bufferSize is different.
218const EGLint ConfigSet::mSortAttribs[] =
219{
220 EGL_RED_SIZE, 1,
221 EGL_GREEN_SIZE, 1,
222 EGL_BLUE_SIZE, 1,
223 EGL_LUMINANCE_SIZE, 1,
224 // BUT NOT ALPHA
225 EGL_NONE
226};
227
228ConfigSet::ConfigSet()
229 : mSet(SortConfig(mSortAttribs))
230{
231}
232
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +0000233void ConfigSet::add(rx::ConfigDesc desc, EGLint minSwapInterval, EGLint maxSwapInterval, EGLint texWidth, EGLint texHeight)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000234{
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000235 Config config(desc, minSwapInterval, maxSwapInterval, texWidth, texHeight);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000236 mSet.insert(config);
237}
238
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000239size_t ConfigSet::size() const
240{
241 return mSet.size();
242}
243
244bool ConfigSet::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig)
245{
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000246 vector<const Config*> passed;
247 passed.reserve(mSet.size());
248
249 for (Iterator config = mSet.begin(); config != mSet.end(); config++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000250 {
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000251 bool match = true;
252 const EGLint *attribute = attribList;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000253
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000254 while (attribute[0] != EGL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000255 {
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000256 switch (attribute[0])
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000257 {
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000258 case EGL_BUFFER_SIZE: match = config->mBufferSize >= attribute[1]; break;
259 case EGL_ALPHA_SIZE: match = config->mAlphaSize >= attribute[1]; break;
260 case EGL_BLUE_SIZE: match = config->mBlueSize >= attribute[1]; break;
261 case EGL_GREEN_SIZE: match = config->mGreenSize >= attribute[1]; break;
262 case EGL_RED_SIZE: match = config->mRedSize >= attribute[1]; break;
263 case EGL_DEPTH_SIZE: match = config->mDepthSize >= attribute[1]; break;
264 case EGL_STENCIL_SIZE: match = config->mStencilSize >= attribute[1]; break;
apatrick@chromium.org9c3a3932012-01-30 20:03:32 +0000265 case EGL_CONFIG_CAVEAT: match = config->mConfigCaveat == (EGLenum) attribute[1]; break;
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000266 case EGL_CONFIG_ID: match = config->mConfigID == attribute[1]; break;
267 case EGL_LEVEL: match = config->mLevel >= attribute[1]; break;
apatrick@chromium.org9c3a3932012-01-30 20:03:32 +0000268 case EGL_NATIVE_RENDERABLE: match = config->mNativeRenderable == (EGLBoolean) attribute[1]; break;
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000269 case EGL_NATIVE_VISUAL_TYPE: match = config->mNativeVisualType == attribute[1]; break;
270 case EGL_SAMPLES: match = config->mSamples >= attribute[1]; break;
271 case EGL_SAMPLE_BUFFERS: match = config->mSampleBuffers >= attribute[1]; break;
272 case EGL_SURFACE_TYPE: match = (config->mSurfaceType & attribute[1]) == attribute[1]; break;
apatrick@chromium.org9c3a3932012-01-30 20:03:32 +0000273 case EGL_TRANSPARENT_TYPE: match = config->mTransparentType == (EGLenum) attribute[1]; break;
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000274 case EGL_TRANSPARENT_BLUE_VALUE: match = config->mTransparentBlueValue == attribute[1]; break;
275 case EGL_TRANSPARENT_GREEN_VALUE: match = config->mTransparentGreenValue == attribute[1]; break;
276 case EGL_TRANSPARENT_RED_VALUE: match = config->mTransparentRedValue == attribute[1]; break;
apatrick@chromium.org9c3a3932012-01-30 20:03:32 +0000277 case EGL_BIND_TO_TEXTURE_RGB: match = config->mBindToTextureRGB == (EGLBoolean) attribute[1]; break;
278 case EGL_BIND_TO_TEXTURE_RGBA: match = config->mBindToTextureRGBA == (EGLBoolean) attribute[1]; break;
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000279 case EGL_MIN_SWAP_INTERVAL: match = config->mMinSwapInterval == attribute[1]; break;
280 case EGL_MAX_SWAP_INTERVAL: match = config->mMaxSwapInterval == attribute[1]; break;
281 case EGL_LUMINANCE_SIZE: match = config->mLuminanceSize >= attribute[1]; break;
282 case EGL_ALPHA_MASK_SIZE: match = config->mAlphaMaskSize >= attribute[1]; break;
apatrick@chromium.org9c3a3932012-01-30 20:03:32 +0000283 case EGL_COLOR_BUFFER_TYPE: match = config->mColorBufferType == (EGLenum) attribute[1]; break;
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000284 case EGL_RENDERABLE_TYPE: match = (config->mRenderableType & attribute[1]) == attribute[1]; break;
285 case EGL_MATCH_NATIVE_PIXMAP: match = false; UNIMPLEMENTED(); break;
286 case EGL_CONFORMANT: match = (config->mConformant & attribute[1]) == attribute[1]; break;
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000287 case EGL_MAX_PBUFFER_WIDTH: match = config->mMaxPBufferWidth >= attribute[1]; break;
288 case EGL_MAX_PBUFFER_HEIGHT: match = config->mMaxPBufferHeight >= attribute[1]; break;
289 case EGL_MAX_PBUFFER_PIXELS: match = config->mMaxPBufferPixels >= attribute[1]; break;
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000290 default:
291 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000292 }
293
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000294 if (!match)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000295 {
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000296 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000297 }
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000298
299 attribute += 2;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000300 }
301
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000302 if (match)
303 {
304 passed.push_back(&*config);
305 }
306 }
307
308 if (configs)
309 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310 sort(passed.begin(), passed.end(), SortConfig(attribList));
311
312 EGLint index;
313 for (index = 0; index < configSize && index < static_cast<EGLint>(passed.size()); index++)
314 {
315 configs[index] = passed[index]->getHandle();
316 }
317
318 *numConfig = index;
319 }
320 else
321 {
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000322 *numConfig = passed.size();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000323 }
324
325 return true;
326}
327
328const egl::Config *ConfigSet::get(EGLConfig configHandle)
329{
330 for (Iterator config = mSet.begin(); config != mSet.end(); config++)
331 {
332 if (config->getHandle() == configHandle)
333 {
334 return &(*config);
335 }
336 }
337
338 return NULL;
339}
340}