blob: fdc41a95f0b570d9f6fe4e5aa65d7b61a180eb82 [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;
86 mConformant = EGL_OPENGL_ES2_BIT;
87
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
133 if (desc.es3Capable)
134 {
135 mRenderableType |= EGL_OPENGL_ES3_BIT_KHR;
136 mConformant |= EGL_OPENGL_ES3_BIT_KHR;
137 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000138}
139
140EGLConfig Config::getHandle() const
141{
142 return (EGLConfig)(size_t)mConfigID;
143}
144
145SortConfig::SortConfig(const EGLint *attribList)
146 : mWantRed(false), mWantGreen(false), mWantBlue(false), mWantAlpha(false), mWantLuminance(false)
147{
148 scanForWantedComponents(attribList);
149}
150
151void SortConfig::scanForWantedComponents(const EGLint *attribList)
152{
153 // [EGL] section 3.4.1 page 24
154 // Sorting rule #3: by larger total number of color bits, not considering
155 // components that are 0 or don't-care.
156 for (const EGLint *attr = attribList; attr[0] != EGL_NONE; attr += 2)
157 {
158 if (attr[1] != 0 && attr[1] != EGL_DONT_CARE)
159 {
160 switch (attr[0])
161 {
162 case EGL_RED_SIZE: mWantRed = true; break;
163 case EGL_GREEN_SIZE: mWantGreen = true; break;
164 case EGL_BLUE_SIZE: mWantBlue = true; break;
165 case EGL_ALPHA_SIZE: mWantAlpha = true; break;
166 case EGL_LUMINANCE_SIZE: mWantLuminance = true; break;
167 }
168 }
169 }
170}
171
172EGLint SortConfig::wantedComponentsSize(const Config &config) const
173{
174 EGLint total = 0;
175
176 if (mWantRed) total += config.mRedSize;
177 if (mWantGreen) total += config.mGreenSize;
178 if (mWantBlue) total += config.mBlueSize;
179 if (mWantAlpha) total += config.mAlphaSize;
180 if (mWantLuminance) total += config.mLuminanceSize;
181
182 return total;
183}
184
185bool SortConfig::operator()(const Config *x, const Config *y) const
186{
187 return (*this)(*x, *y);
188}
189
190bool SortConfig::operator()(const Config &x, const Config &y) const
191{
192 #define SORT(attribute) \
193 if (x.attribute != y.attribute) \
194 { \
195 return x.attribute < y.attribute; \
196 }
197
198 META_ASSERT(EGL_NONE < EGL_SLOW_CONFIG && EGL_SLOW_CONFIG < EGL_NON_CONFORMANT_CONFIG);
199 SORT(mConfigCaveat);
200
201 META_ASSERT(EGL_RGB_BUFFER < EGL_LUMINANCE_BUFFER);
202 SORT(mColorBufferType);
203
204 // By larger total number of color bits, only considering those that are requested to be > 0.
205 EGLint xComponentsSize = wantedComponentsSize(x);
206 EGLint yComponentsSize = wantedComponentsSize(y);
207 if (xComponentsSize != yComponentsSize)
208 {
209 return xComponentsSize > yComponentsSize;
210 }
211
212 SORT(mBufferSize);
213 SORT(mSampleBuffers);
214 SORT(mSamples);
215 SORT(mDepthSize);
216 SORT(mStencilSize);
217 SORT(mAlphaMaskSize);
218 SORT(mNativeVisualType);
219 SORT(mConfigID);
220
221 #undef SORT
222
223 return false;
224}
225
226// We'd like to use SortConfig to also eliminate duplicate configs.
227// This works as long as we never have two configs with different per-RGB-component layouts,
228// but the same total.
229// 5551 and 565 are different because R+G+B is different.
230// 5551 and 555 are different because bufferSize is different.
231const EGLint ConfigSet::mSortAttribs[] =
232{
233 EGL_RED_SIZE, 1,
234 EGL_GREEN_SIZE, 1,
235 EGL_BLUE_SIZE, 1,
236 EGL_LUMINANCE_SIZE, 1,
237 // BUT NOT ALPHA
238 EGL_NONE
239};
240
241ConfigSet::ConfigSet()
242 : mSet(SortConfig(mSortAttribs))
243{
244}
245
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +0000246void ConfigSet::add(rx::ConfigDesc desc, EGLint minSwapInterval, EGLint maxSwapInterval, EGLint texWidth, EGLint texHeight)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000247{
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000248 Config config(desc, minSwapInterval, maxSwapInterval, texWidth, texHeight);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000249 mSet.insert(config);
250}
251
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000252size_t ConfigSet::size() const
253{
254 return mSet.size();
255}
256
257bool ConfigSet::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig)
258{
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000259 vector<const Config*> passed;
260 passed.reserve(mSet.size());
261
262 for (Iterator config = mSet.begin(); config != mSet.end(); config++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000263 {
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000264 bool match = true;
265 const EGLint *attribute = attribList;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000266
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000267 while (attribute[0] != EGL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000268 {
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000269 switch (attribute[0])
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000270 {
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000271 case EGL_BUFFER_SIZE: match = config->mBufferSize >= attribute[1]; break;
272 case EGL_ALPHA_SIZE: match = config->mAlphaSize >= attribute[1]; break;
273 case EGL_BLUE_SIZE: match = config->mBlueSize >= attribute[1]; break;
274 case EGL_GREEN_SIZE: match = config->mGreenSize >= attribute[1]; break;
275 case EGL_RED_SIZE: match = config->mRedSize >= attribute[1]; break;
276 case EGL_DEPTH_SIZE: match = config->mDepthSize >= attribute[1]; break;
277 case EGL_STENCIL_SIZE: match = config->mStencilSize >= attribute[1]; break;
apatrick@chromium.org9c3a3932012-01-30 20:03:32 +0000278 case EGL_CONFIG_CAVEAT: match = config->mConfigCaveat == (EGLenum) attribute[1]; break;
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000279 case EGL_CONFIG_ID: match = config->mConfigID == attribute[1]; break;
280 case EGL_LEVEL: match = config->mLevel >= attribute[1]; break;
apatrick@chromium.org9c3a3932012-01-30 20:03:32 +0000281 case EGL_NATIVE_RENDERABLE: match = config->mNativeRenderable == (EGLBoolean) attribute[1]; break;
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000282 case EGL_NATIVE_VISUAL_TYPE: match = config->mNativeVisualType == attribute[1]; break;
283 case EGL_SAMPLES: match = config->mSamples >= attribute[1]; break;
284 case EGL_SAMPLE_BUFFERS: match = config->mSampleBuffers >= attribute[1]; break;
285 case EGL_SURFACE_TYPE: match = (config->mSurfaceType & attribute[1]) == attribute[1]; break;
apatrick@chromium.org9c3a3932012-01-30 20:03:32 +0000286 case EGL_TRANSPARENT_TYPE: match = config->mTransparentType == (EGLenum) attribute[1]; break;
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000287 case EGL_TRANSPARENT_BLUE_VALUE: match = config->mTransparentBlueValue == attribute[1]; break;
288 case EGL_TRANSPARENT_GREEN_VALUE: match = config->mTransparentGreenValue == attribute[1]; break;
289 case EGL_TRANSPARENT_RED_VALUE: match = config->mTransparentRedValue == attribute[1]; break;
apatrick@chromium.org9c3a3932012-01-30 20:03:32 +0000290 case EGL_BIND_TO_TEXTURE_RGB: match = config->mBindToTextureRGB == (EGLBoolean) attribute[1]; break;
291 case EGL_BIND_TO_TEXTURE_RGBA: match = config->mBindToTextureRGBA == (EGLBoolean) attribute[1]; break;
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000292 case EGL_MIN_SWAP_INTERVAL: match = config->mMinSwapInterval == attribute[1]; break;
293 case EGL_MAX_SWAP_INTERVAL: match = config->mMaxSwapInterval == attribute[1]; break;
294 case EGL_LUMINANCE_SIZE: match = config->mLuminanceSize >= attribute[1]; break;
295 case EGL_ALPHA_MASK_SIZE: match = config->mAlphaMaskSize >= attribute[1]; break;
apatrick@chromium.org9c3a3932012-01-30 20:03:32 +0000296 case EGL_COLOR_BUFFER_TYPE: match = config->mColorBufferType == (EGLenum) attribute[1]; break;
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000297 case EGL_RENDERABLE_TYPE: match = (config->mRenderableType & attribute[1]) == attribute[1]; break;
298 case EGL_MATCH_NATIVE_PIXMAP: match = false; UNIMPLEMENTED(); break;
299 case EGL_CONFORMANT: match = (config->mConformant & attribute[1]) == attribute[1]; break;
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000300 case EGL_MAX_PBUFFER_WIDTH: match = config->mMaxPBufferWidth >= attribute[1]; break;
301 case EGL_MAX_PBUFFER_HEIGHT: match = config->mMaxPBufferHeight >= attribute[1]; break;
302 case EGL_MAX_PBUFFER_PIXELS: match = config->mMaxPBufferPixels >= attribute[1]; break;
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000303 default:
304 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305 }
306
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000307 if (!match)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308 {
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000309 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310 }
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000311
312 attribute += 2;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000313 }
314
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000315 if (match)
316 {
317 passed.push_back(&*config);
318 }
319 }
320
321 if (configs)
322 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000323 sort(passed.begin(), passed.end(), SortConfig(attribList));
324
325 EGLint index;
326 for (index = 0; index < configSize && index < static_cast<EGLint>(passed.size()); index++)
327 {
328 configs[index] = passed[index]->getHandle();
329 }
330
331 *numConfig = index;
332 }
333 else
334 {
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000335 *numConfig = passed.size();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000336 }
337
338 return true;
339}
340
341const egl::Config *ConfigSet::get(EGLConfig configHandle)
342{
343 for (Iterator config = mSet.begin(); config != mSet.end(); config++)
344 {
345 if (config->getHandle() == configHandle)
346 {
347 return &(*config);
348 }
349 }
350
351 return NULL;
352}
353}