blob: 9656f6ea323bf387b5b5c693b606b23cebbac805 [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
shannon.woods%transgaming.com@gtempaccount.comf26ddae2013-04-13 03:29:13 +000016#include <GLES3/gl3.h>
17#include <GLES3/gl3ext.h>
daniel@transgaming.com106e1f72012-10-31 18:38:36 +000018#include <GLES2/gl2.h>
19#include <GLES2/gl2ext.h>
20
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000021#include "common/debug.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000022
23using namespace std;
24
25namespace egl
26{
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +000027Config::Config(rx::ConfigDesc desc, EGLint minInterval, EGLint maxInterval, EGLint texWidth, EGLint texHeight)
daniel@transgaming.com3281f972012-10-31 18:38:51 +000028 : mRenderTargetFormat(desc.renderTargetFormat), mDepthStencilFormat(desc.depthStencilFormat), mMultiSample(desc.multiSample)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000029{
jbauman@chromium.orgae345802011-03-30 22:04:25 +000030 mBindToTextureRGB = EGL_FALSE;
31 mBindToTextureRGBA = EGL_FALSE;
daniel@transgaming.com3281f972012-10-31 18:38:51 +000032 switch (desc.renderTargetFormat)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000033 {
daniel@transgaming.com106e1f72012-10-31 18:38:36 +000034 case GL_RGB5_A1:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000035 mBufferSize = 16;
36 mRedSize = 5;
37 mGreenSize = 5;
38 mBlueSize = 5;
39 mAlphaSize = 1;
40 break;
daniel@transgaming.com106e1f72012-10-31 18:38:36 +000041 case GL_RGBA8_OES:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000042 mBufferSize = 32;
43 mRedSize = 8;
44 mGreenSize = 8;
45 mBlueSize = 8;
46 mAlphaSize = 8;
jbauman@chromium.orgae345802011-03-30 22:04:25 +000047 mBindToTextureRGBA = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000048 break;
daniel@transgaming.com106e1f72012-10-31 18:38:36 +000049 case GL_RGB565:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000050 mBufferSize = 16;
51 mRedSize = 5;
52 mGreenSize = 6;
53 mBlueSize = 5;
54 mAlphaSize = 0;
55 break;
daniel@transgaming.com106e1f72012-10-31 18:38:36 +000056 case GL_RGB8_OES:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000057 mBufferSize = 32;
58 mRedSize = 8;
59 mGreenSize = 8;
60 mBlueSize = 8;
61 mAlphaSize = 0;
jbauman@chromium.orgae345802011-03-30 22:04:25 +000062 mBindToTextureRGB = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000063 break;
shannon.woods@transgaming.comc60c5212013-01-25 21:54:01 +000064 case GL_BGRA8_EXT:
65 mBufferSize = 32;
66 mRedSize = 8;
67 mGreenSize = 8;
68 mBlueSize = 8;
69 mAlphaSize = 8;
70 mBindToTextureRGBA = true;
71 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000072 default:
73 UNREACHABLE(); // Other formats should not be valid
74 }
75
76 mLuminanceSize = 0;
77 mAlphaMaskSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000078 mColorBufferType = EGL_RGB_BUFFER;
daniel@transgaming.com3281f972012-10-31 18:38:51 +000079 mConfigCaveat = (desc.fastConfig) ? EGL_NONE : EGL_SLOW_CONFIG;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000080 mConfigID = 0;
81 mConformant = EGL_OPENGL_ES2_BIT;
82
daniel@transgaming.com3281f972012-10-31 18:38:51 +000083 switch (desc.depthStencilFormat)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000084 {
daniel@transgaming.com106e1f72012-10-31 18:38:36 +000085 case GL_NONE:
daniel@transgaming.coma114c272011-04-22 04:18:50 +000086 mDepthSize = 0;
87 mStencilSize = 0;
88 break;
daniel@transgaming.com106e1f72012-10-31 18:38:36 +000089 case GL_DEPTH_COMPONENT32_OES:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000090 mDepthSize = 32;
91 mStencilSize = 0;
92 break;
daniel@transgaming.com106e1f72012-10-31 18:38:36 +000093 case GL_DEPTH24_STENCIL8_OES:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000094 mDepthSize = 24;
95 mStencilSize = 8;
96 break;
daniel@transgaming.com106e1f72012-10-31 18:38:36 +000097 case GL_DEPTH_COMPONENT24_OES:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000098 mDepthSize = 24;
99 mStencilSize = 0;
100 break;
daniel@transgaming.com106e1f72012-10-31 18:38:36 +0000101 case GL_DEPTH_COMPONENT16:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102 mDepthSize = 16;
103 mStencilSize = 0;
104 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000105 default:
106 UNREACHABLE();
107 }
108
109 mLevel = 0;
110 mMatchNativePixmap = EGL_NONE;
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000111 mMaxPBufferWidth = texWidth;
112 mMaxPBufferHeight = texHeight;
113 mMaxPBufferPixels = texWidth*texHeight;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000114 mMaxSwapInterval = maxInterval;
115 mMinSwapInterval = minInterval;
116 mNativeRenderable = EGL_FALSE;
117 mNativeVisualID = 0;
118 mNativeVisualType = 0;
119 mRenderableType = EGL_OPENGL_ES2_BIT;
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000120 mSampleBuffers = desc.multiSample ? 1 : 0;
121 mSamples = desc.multiSample;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000122 mSurfaceType = EGL_PBUFFER_BIT | EGL_WINDOW_BIT | EGL_SWAP_BEHAVIOR_PRESERVED_BIT;
123 mTransparentType = EGL_NONE;
124 mTransparentRedValue = 0;
125 mTransparentGreenValue = 0;
126 mTransparentBlueValue = 0;
127}
128
129EGLConfig Config::getHandle() const
130{
131 return (EGLConfig)(size_t)mConfigID;
132}
133
134SortConfig::SortConfig(const EGLint *attribList)
135 : mWantRed(false), mWantGreen(false), mWantBlue(false), mWantAlpha(false), mWantLuminance(false)
136{
137 scanForWantedComponents(attribList);
138}
139
140void SortConfig::scanForWantedComponents(const EGLint *attribList)
141{
142 // [EGL] section 3.4.1 page 24
143 // Sorting rule #3: by larger total number of color bits, not considering
144 // components that are 0 or don't-care.
145 for (const EGLint *attr = attribList; attr[0] != EGL_NONE; attr += 2)
146 {
147 if (attr[1] != 0 && attr[1] != EGL_DONT_CARE)
148 {
149 switch (attr[0])
150 {
151 case EGL_RED_SIZE: mWantRed = true; break;
152 case EGL_GREEN_SIZE: mWantGreen = true; break;
153 case EGL_BLUE_SIZE: mWantBlue = true; break;
154 case EGL_ALPHA_SIZE: mWantAlpha = true; break;
155 case EGL_LUMINANCE_SIZE: mWantLuminance = true; break;
156 }
157 }
158 }
159}
160
161EGLint SortConfig::wantedComponentsSize(const Config &config) const
162{
163 EGLint total = 0;
164
165 if (mWantRed) total += config.mRedSize;
166 if (mWantGreen) total += config.mGreenSize;
167 if (mWantBlue) total += config.mBlueSize;
168 if (mWantAlpha) total += config.mAlphaSize;
169 if (mWantLuminance) total += config.mLuminanceSize;
170
171 return total;
172}
173
174bool SortConfig::operator()(const Config *x, const Config *y) const
175{
176 return (*this)(*x, *y);
177}
178
179bool SortConfig::operator()(const Config &x, const Config &y) const
180{
181 #define SORT(attribute) \
182 if (x.attribute != y.attribute) \
183 { \
184 return x.attribute < y.attribute; \
185 }
186
187 META_ASSERT(EGL_NONE < EGL_SLOW_CONFIG && EGL_SLOW_CONFIG < EGL_NON_CONFORMANT_CONFIG);
188 SORT(mConfigCaveat);
189
190 META_ASSERT(EGL_RGB_BUFFER < EGL_LUMINANCE_BUFFER);
191 SORT(mColorBufferType);
192
193 // By larger total number of color bits, only considering those that are requested to be > 0.
194 EGLint xComponentsSize = wantedComponentsSize(x);
195 EGLint yComponentsSize = wantedComponentsSize(y);
196 if (xComponentsSize != yComponentsSize)
197 {
198 return xComponentsSize > yComponentsSize;
199 }
200
201 SORT(mBufferSize);
202 SORT(mSampleBuffers);
203 SORT(mSamples);
204 SORT(mDepthSize);
205 SORT(mStencilSize);
206 SORT(mAlphaMaskSize);
207 SORT(mNativeVisualType);
208 SORT(mConfigID);
209
210 #undef SORT
211
212 return false;
213}
214
215// We'd like to use SortConfig to also eliminate duplicate configs.
216// This works as long as we never have two configs with different per-RGB-component layouts,
217// but the same total.
218// 5551 and 565 are different because R+G+B is different.
219// 5551 and 555 are different because bufferSize is different.
220const EGLint ConfigSet::mSortAttribs[] =
221{
222 EGL_RED_SIZE, 1,
223 EGL_GREEN_SIZE, 1,
224 EGL_BLUE_SIZE, 1,
225 EGL_LUMINANCE_SIZE, 1,
226 // BUT NOT ALPHA
227 EGL_NONE
228};
229
230ConfigSet::ConfigSet()
231 : mSet(SortConfig(mSortAttribs))
232{
233}
234
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +0000235void ConfigSet::add(rx::ConfigDesc desc, EGLint minSwapInterval, EGLint maxSwapInterval, EGLint texWidth, EGLint texHeight)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000236{
daniel@transgaming.com3281f972012-10-31 18:38:51 +0000237 Config config(desc, minSwapInterval, maxSwapInterval, texWidth, texHeight);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000238 mSet.insert(config);
239}
240
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000241size_t ConfigSet::size() const
242{
243 return mSet.size();
244}
245
246bool ConfigSet::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig)
247{
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000248 vector<const Config*> passed;
249 passed.reserve(mSet.size());
250
251 for (Iterator config = mSet.begin(); config != mSet.end(); config++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000252 {
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000253 bool match = true;
254 const EGLint *attribute = attribList;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000255
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000256 while (attribute[0] != EGL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000257 {
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000258 switch (attribute[0])
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000259 {
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000260 case EGL_BUFFER_SIZE: match = config->mBufferSize >= attribute[1]; break;
261 case EGL_ALPHA_SIZE: match = config->mAlphaSize >= attribute[1]; break;
262 case EGL_BLUE_SIZE: match = config->mBlueSize >= attribute[1]; break;
263 case EGL_GREEN_SIZE: match = config->mGreenSize >= attribute[1]; break;
264 case EGL_RED_SIZE: match = config->mRedSize >= attribute[1]; break;
265 case EGL_DEPTH_SIZE: match = config->mDepthSize >= attribute[1]; break;
266 case EGL_STENCIL_SIZE: match = config->mStencilSize >= attribute[1]; break;
apatrick@chromium.org9c3a3932012-01-30 20:03:32 +0000267 case EGL_CONFIG_CAVEAT: match = config->mConfigCaveat == (EGLenum) attribute[1]; break;
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000268 case EGL_CONFIG_ID: match = config->mConfigID == attribute[1]; break;
269 case EGL_LEVEL: match = config->mLevel >= attribute[1]; break;
apatrick@chromium.org9c3a3932012-01-30 20:03:32 +0000270 case EGL_NATIVE_RENDERABLE: match = config->mNativeRenderable == (EGLBoolean) attribute[1]; break;
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000271 case EGL_NATIVE_VISUAL_TYPE: match = config->mNativeVisualType == attribute[1]; break;
272 case EGL_SAMPLES: match = config->mSamples >= attribute[1]; break;
273 case EGL_SAMPLE_BUFFERS: match = config->mSampleBuffers >= attribute[1]; break;
274 case EGL_SURFACE_TYPE: match = (config->mSurfaceType & attribute[1]) == attribute[1]; break;
apatrick@chromium.org9c3a3932012-01-30 20:03:32 +0000275 case EGL_TRANSPARENT_TYPE: match = config->mTransparentType == (EGLenum) attribute[1]; break;
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000276 case EGL_TRANSPARENT_BLUE_VALUE: match = config->mTransparentBlueValue == attribute[1]; break;
277 case EGL_TRANSPARENT_GREEN_VALUE: match = config->mTransparentGreenValue == attribute[1]; break;
278 case EGL_TRANSPARENT_RED_VALUE: match = config->mTransparentRedValue == attribute[1]; break;
apatrick@chromium.org9c3a3932012-01-30 20:03:32 +0000279 case EGL_BIND_TO_TEXTURE_RGB: match = config->mBindToTextureRGB == (EGLBoolean) attribute[1]; break;
280 case EGL_BIND_TO_TEXTURE_RGBA: match = config->mBindToTextureRGBA == (EGLBoolean) attribute[1]; break;
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000281 case EGL_MIN_SWAP_INTERVAL: match = config->mMinSwapInterval == attribute[1]; break;
282 case EGL_MAX_SWAP_INTERVAL: match = config->mMaxSwapInterval == attribute[1]; break;
283 case EGL_LUMINANCE_SIZE: match = config->mLuminanceSize >= attribute[1]; break;
284 case EGL_ALPHA_MASK_SIZE: match = config->mAlphaMaskSize >= attribute[1]; break;
apatrick@chromium.org9c3a3932012-01-30 20:03:32 +0000285 case EGL_COLOR_BUFFER_TYPE: match = config->mColorBufferType == (EGLenum) attribute[1]; break;
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000286 case EGL_RENDERABLE_TYPE: match = (config->mRenderableType & attribute[1]) == attribute[1]; break;
287 case EGL_MATCH_NATIVE_PIXMAP: match = false; UNIMPLEMENTED(); break;
288 case EGL_CONFORMANT: match = (config->mConformant & attribute[1]) == attribute[1]; break;
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000289 case EGL_MAX_PBUFFER_WIDTH: match = config->mMaxPBufferWidth >= attribute[1]; break;
290 case EGL_MAX_PBUFFER_HEIGHT: match = config->mMaxPBufferHeight >= attribute[1]; break;
291 case EGL_MAX_PBUFFER_PIXELS: match = config->mMaxPBufferPixels >= attribute[1]; break;
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000292 default:
293 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000294 }
295
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000296 if (!match)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000297 {
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000298 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000299 }
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000300
301 attribute += 2;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000302 }
303
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000304 if (match)
305 {
306 passed.push_back(&*config);
307 }
308 }
309
310 if (configs)
311 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000312 sort(passed.begin(), passed.end(), SortConfig(attribList));
313
314 EGLint index;
315 for (index = 0; index < configSize && index < static_cast<EGLint>(passed.size()); index++)
316 {
317 configs[index] = passed[index]->getHandle();
318 }
319
320 *numConfig = index;
321 }
322 else
323 {
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000324 *numConfig = passed.size();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000325 }
326
327 return true;
328}
329
330const egl::Config *ConfigSet::get(EGLConfig configHandle)
331{
332 for (Iterator config = mSet.begin(); config != mSet.end(); config++)
333 {
334 if (config->getHandle() == configHandle)
335 {
336 return &(*config);
337 }
338 }
339
340 return NULL;
341}
342}