blob: 2ecdb6d2e9b69f18b0e0eeaa6e5a887eb226e8d4 [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.
Geoff Lang6812a552015-01-06 17:26:42 -05009// [EGL 1.5] section 3.4 page 19.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000010
Geoff Lang2b5420c2014-11-19 14:20:15 -050011#include "libANGLE/Config.h"
Geoff Lang6812a552015-01-06 17:26:42 -050012#include "libANGLE/AttributeMap.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000013
14#include <algorithm>
15#include <vector>
16
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +000017#include <EGL/eglext.h>
Jamie Madillb980c562018-11-27 11:34:27 -050018#include "angle_gl.h"
daniel@transgaming.com106e1f72012-10-31 18:38:36 +000019
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000020#include "common/debug.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000021
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000022namespace egl
23{
Geoff Lang6812a552015-01-06 17:26:42 -050024
25Config::Config()
Geoff Langf21a1082015-01-09 14:41:27 -050026 : renderTargetFormat(GL_NONE),
27 depthStencilFormat(GL_NONE),
28 bufferSize(0),
29 redSize(0),
30 greenSize(0),
31 blueSize(0),
32 luminanceSize(0),
33 alphaSize(0),
34 alphaMaskSize(0),
35 bindToTextureRGB(EGL_FALSE),
36 bindToTextureRGBA(EGL_FALSE),
37 colorBufferType(EGL_NONE),
38 configCaveat(EGL_NONE),
39 configID(0),
40 conformant(0),
41 depthSize(0),
42 level(0),
43 matchNativePixmap(EGL_FALSE),
44 maxPBufferWidth(0),
45 maxPBufferHeight(0),
46 maxPBufferPixels(0),
47 maxSwapInterval(0),
48 minSwapInterval(0),
49 nativeRenderable(EGL_FALSE),
50 nativeVisualID(0),
51 nativeVisualType(0),
52 renderableType(0),
53 sampleBuffers(0),
54 samples(0),
55 stencilSize(0),
56 surfaceType(0),
57 transparentType(EGL_NONE),
58 transparentRedValue(0),
59 transparentGreenValue(0),
Geoff Lang7f448b52015-12-16 13:31:57 -050060 transparentBlueValue(0),
Geoff Langc5a2a172017-01-13 15:55:07 -050061 optimalOrientation(0),
62 colorComponentType(EGL_COLOR_COMPONENT_TYPE_FIXED_EXT)
Jamie Madillb980c562018-11-27 11:34:27 -050063{}
Geoff Lang6812a552015-01-06 17:26:42 -050064
Jamie Madillb980c562018-11-27 11:34:27 -050065Config::~Config() {}
Jamie Madillacf2f3a2017-11-21 19:22:44 -050066
67Config::Config(const Config &other) = default;
68
69Config &Config::operator=(const Config &other) = default;
70
71ConfigSet::ConfigSet() = default;
72
73ConfigSet::ConfigSet(const ConfigSet &other) = default;
74
75ConfigSet &ConfigSet::operator=(const ConfigSet &other) = default;
76
77ConfigSet::~ConfigSet() = default;
78
Geoff Lang6812a552015-01-06 17:26:42 -050079EGLint ConfigSet::add(const Config &config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000080{
Geoff Lang6812a552015-01-06 17:26:42 -050081 // Set the config's ID to a small number that starts at 1 ([EGL 1.5] section 3.4)
Cooper Partin4d61f7e2015-08-12 10:56:50 -070082 EGLint id = static_cast<EGLint>(mConfigs.size()) + 1;
Geoff Lang6812a552015-01-06 17:26:42 -050083
84 Config copyConfig(config);
Geoff Langc223dc62015-01-09 13:10:01 -050085 copyConfig.configID = id;
Geoff Lang6812a552015-01-06 17:26:42 -050086 mConfigs.insert(std::make_pair(id, copyConfig));
87
88 return id;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000089}
90
Geoff Lang6812a552015-01-06 17:26:42 -050091const Config &ConfigSet::get(EGLint id) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000092{
Jamie Madill73edef02015-02-17 18:58:21 -050093 ASSERT(mConfigs.find(id) != mConfigs.end());
94 return mConfigs.find(id)->second;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095}
96
Geoff Lang6812a552015-01-06 17:26:42 -050097void ConfigSet::clear()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000098{
Geoff Lang6812a552015-01-06 17:26:42 -050099 mConfigs.clear();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000100}
101
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102size_t ConfigSet::size() const
103{
Geoff Lang6812a552015-01-06 17:26:42 -0500104 return mConfigs.size();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000105}
106
Geoff Lang6812a552015-01-06 17:26:42 -0500107bool ConfigSet::contains(const Config *config) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000108{
Geoff Lang6812a552015-01-06 17:26:42 -0500109 for (auto i = mConfigs.begin(); i != mConfigs.end(); i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000110 {
Geoff Lang6812a552015-01-06 17:26:42 -0500111 const Config &item = i->second;
112 if (config == &item)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000113 {
Geoff Lang6812a552015-01-06 17:26:42 -0500114 return true;
115 }
116 }
117
118 return false;
119}
120
Jamie Madillb980c562018-11-27 11:34:27 -0500121// Function object used by STL sorting routines for ordering Configs according to [EGL 1.5]
122// section 3.4.1.2 page 28.
Geoff Lang6812a552015-01-06 17:26:42 -0500123class ConfigSorter
124{
125 public:
126 explicit ConfigSorter(const AttributeMap &attributeMap)
Jamie Madill56596192015-03-19 10:23:13 -0400127 : mWantRed(false),
128 mWantGreen(false),
129 mWantBlue(false),
130 mWantAlpha(false),
131 mWantLuminance(false)
Geoff Lang6812a552015-01-06 17:26:42 -0500132 {
133 scanForWantedComponents(attributeMap);
134 }
135
Jamie Madillb980c562018-11-27 11:34:27 -0500136 bool operator()(const Config *x, const Config *y) const { return (*this)(*x, *y); }
Geoff Lang6812a552015-01-06 17:26:42 -0500137
138 bool operator()(const Config &x, const Config &y) const
139 {
Jamie Madillb980c562018-11-27 11:34:27 -0500140#define SORT(attribute) \
141 if (x.attribute != y.attribute) \
142 { \
143 return x.attribute < y.attribute; \
144 }
Geoff Lang6812a552015-01-06 17:26:42 -0500145
Jamie Madillb980c562018-11-27 11:34:27 -0500146 static_assert(EGL_NONE < EGL_SLOW_CONFIG && EGL_SLOW_CONFIG < EGL_NON_CONFORMANT_CONFIG,
147 "Unexpected EGL enum value.");
Geoff Langc223dc62015-01-09 13:10:01 -0500148 SORT(configCaveat);
Geoff Lang6812a552015-01-06 17:26:42 -0500149
Geoff Langc5a2a172017-01-13 15:55:07 -0500150 static_assert(EGL_COLOR_COMPONENT_TYPE_FIXED_EXT < EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT,
151 "Unexpected order of EGL enums.");
152 SORT(colorComponentType);
153
Geoff Langd4475812015-03-18 10:53:05 -0400154 static_assert(EGL_RGB_BUFFER < EGL_LUMINANCE_BUFFER, "Unexpected EGL enum value.");
Geoff Langc223dc62015-01-09 13:10:01 -0500155 SORT(colorBufferType);
Geoff Lang6812a552015-01-06 17:26:42 -0500156
Jamie Madillb980c562018-11-27 11:34:27 -0500157 // By larger total number of color bits, only considering those that are requested to be >
158 // 0.
Geoff Lang6812a552015-01-06 17:26:42 -0500159 EGLint xComponentsSize = wantedComponentsSize(x);
160 EGLint yComponentsSize = wantedComponentsSize(y);
161 if (xComponentsSize != yComponentsSize)
162 {
163 return xComponentsSize > yComponentsSize;
164 }
165
Geoff Langc223dc62015-01-09 13:10:01 -0500166 SORT(bufferSize);
167 SORT(sampleBuffers);
168 SORT(samples);
169 SORT(depthSize);
170 SORT(stencilSize);
171 SORT(alphaMaskSize);
172 SORT(nativeVisualType);
173 SORT(configID);
Geoff Lang6812a552015-01-06 17:26:42 -0500174
Jamie Madillb980c562018-11-27 11:34:27 -0500175#undef SORT
Geoff Lang6812a552015-01-06 17:26:42 -0500176
177 return false;
178 }
179
180 private:
Geoff Lang2648d922017-06-15 11:36:47 -0400181 static bool wantsComponent(const AttributeMap &attributeMap, EGLAttrib component)
Geoff Lang6812a552015-01-06 17:26:42 -0500182 {
183 // [EGL 1.5] section 3.4.1.2 page 30
184 // Sorting rule #3: by larger total number of color bits, not considering
185 // components that are 0 or don't-care.
Geoff Lang2648d922017-06-15 11:36:47 -0400186 EGLAttrib value = attributeMap.get(component, 0);
187 return value != 0 && value != EGL_DONT_CARE;
188 }
189
190 void scanForWantedComponents(const AttributeMap &attributeMap)
191 {
192 mWantRed = wantsComponent(attributeMap, EGL_RED_SIZE);
193 mWantGreen = wantsComponent(attributeMap, EGL_GREEN_SIZE);
194 mWantBlue = wantsComponent(attributeMap, EGL_BLUE_SIZE);
195 mWantAlpha = wantsComponent(attributeMap, EGL_ALPHA_SIZE);
196 mWantLuminance = wantsComponent(attributeMap, EGL_LUMINANCE_SIZE);
Geoff Lang6812a552015-01-06 17:26:42 -0500197 }
198
199 EGLint wantedComponentsSize(const Config &config) const
200 {
201 EGLint total = 0;
202
Jamie Madillb980c562018-11-27 11:34:27 -0500203 if (mWantRed)
204 total += config.redSize;
205 if (mWantGreen)
206 total += config.greenSize;
207 if (mWantBlue)
208 total += config.blueSize;
209 if (mWantAlpha)
210 total += config.alphaSize;
211 if (mWantLuminance)
212 total += config.luminanceSize;
Geoff Lang6812a552015-01-06 17:26:42 -0500213
214 return total;
215 }
216
217 bool mWantRed;
218 bool mWantGreen;
219 bool mWantBlue;
220 bool mWantAlpha;
221 bool mWantLuminance;
222};
223
Jamie Madillb980c562018-11-27 11:34:27 -0500224std::vector<const Config *> ConfigSet::filter(const AttributeMap &attributeMap) const
Geoff Lang6812a552015-01-06 17:26:42 -0500225{
Jamie Madillb980c562018-11-27 11:34:27 -0500226 std::vector<const Config *> result;
Geoff Lang6812a552015-01-06 17:26:42 -0500227 result.reserve(mConfigs.size());
228
229 for (auto configIter = mConfigs.begin(); configIter != mConfigs.end(); configIter++)
230 {
231 const Config &config = configIter->second;
Jamie Madillb980c562018-11-27 11:34:27 -0500232 bool match = true;
Geoff Lang6812a552015-01-06 17:26:42 -0500233
234 for (auto attribIter = attributeMap.begin(); attribIter != attributeMap.end(); attribIter++)
235 {
Ian Ewellec2c0c52016-04-05 13:46:26 -0400236 EGLAttrib attributeKey = attribIter->first;
237 EGLAttrib attributeValue = attribIter->second;
Geoff Lang6812a552015-01-06 17:26:42 -0500238
Geoff Lang638c7272017-06-13 15:50:26 -0400239 if (attributeValue == EGL_DONT_CARE)
240 {
241 continue;
242 }
243
Geoff Lang6812a552015-01-06 17:26:42 -0500244 switch (attributeKey)
245 {
Jamie Madillb980c562018-11-27 11:34:27 -0500246 case EGL_BUFFER_SIZE:
247 match = config.bufferSize >= attributeValue;
248 break;
249 case EGL_ALPHA_SIZE:
250 match = config.alphaSize >= attributeValue;
251 break;
252 case EGL_BLUE_SIZE:
253 match = config.blueSize >= attributeValue;
254 break;
255 case EGL_GREEN_SIZE:
256 match = config.greenSize >= attributeValue;
257 break;
258 case EGL_RED_SIZE:
259 match = config.redSize >= attributeValue;
260 break;
261 case EGL_DEPTH_SIZE:
262 match = config.depthSize >= attributeValue;
263 break;
264 case EGL_STENCIL_SIZE:
265 match = config.stencilSize >= attributeValue;
266 break;
267 case EGL_CONFIG_CAVEAT:
268 match = config.configCaveat == (EGLenum)attributeValue;
269 break;
270 case EGL_CONFIG_ID:
271 match = config.configID == attributeValue;
272 break;
273 case EGL_LEVEL:
274 match = config.level >= attributeValue;
275 break;
276 case EGL_NATIVE_RENDERABLE:
277 match = config.nativeRenderable == (EGLBoolean)attributeValue;
278 break;
279 case EGL_NATIVE_VISUAL_TYPE:
280 match = config.nativeVisualType == attributeValue;
281 break;
282 case EGL_SAMPLES:
283 match = config.samples >= attributeValue;
284 break;
285 case EGL_SAMPLE_BUFFERS:
286 match = config.sampleBuffers >= attributeValue;
287 break;
288 case EGL_SURFACE_TYPE:
289 match = (config.surfaceType & attributeValue) == attributeValue;
290 break;
291 case EGL_TRANSPARENT_TYPE:
292 match = config.transparentType == (EGLenum)attributeValue;
293 break;
294 case EGL_TRANSPARENT_BLUE_VALUE:
295 match = config.transparentBlueValue == attributeValue;
296 break;
297 case EGL_TRANSPARENT_GREEN_VALUE:
298 match = config.transparentGreenValue == attributeValue;
299 break;
300 case EGL_TRANSPARENT_RED_VALUE:
301 match = config.transparentRedValue == attributeValue;
302 break;
303 case EGL_BIND_TO_TEXTURE_RGB:
304 match = config.bindToTextureRGB == (EGLBoolean)attributeValue;
305 break;
306 case EGL_BIND_TO_TEXTURE_RGBA:
307 match = config.bindToTextureRGBA == (EGLBoolean)attributeValue;
308 break;
309 case EGL_MIN_SWAP_INTERVAL:
310 match = config.minSwapInterval == attributeValue;
311 break;
312 case EGL_MAX_SWAP_INTERVAL:
313 match = config.maxSwapInterval == attributeValue;
314 break;
315 case EGL_LUMINANCE_SIZE:
316 match = config.luminanceSize >= attributeValue;
317 break;
318 case EGL_ALPHA_MASK_SIZE:
319 match = config.alphaMaskSize >= attributeValue;
320 break;
321 case EGL_COLOR_BUFFER_TYPE:
322 match = config.colorBufferType == (EGLenum)attributeValue;
323 break;
324 case EGL_RENDERABLE_TYPE:
325 match = (config.renderableType & attributeValue) == attributeValue;
326 break;
327 case EGL_MATCH_NATIVE_PIXMAP:
328 match = false;
329 UNIMPLEMENTED();
330 break;
331 case EGL_CONFORMANT:
332 match = (config.conformant & attributeValue) == attributeValue;
333 break;
334 case EGL_MAX_PBUFFER_WIDTH:
335 match = config.maxPBufferWidth >= attributeValue;
336 break;
337 case EGL_MAX_PBUFFER_HEIGHT:
338 match = config.maxPBufferHeight >= attributeValue;
339 break;
340 case EGL_MAX_PBUFFER_PIXELS:
341 match = config.maxPBufferPixels >= attributeValue;
342 break;
343 case EGL_OPTIMAL_SURFACE_ORIENTATION_ANGLE:
344 match = config.optimalOrientation == attributeValue;
345 break;
346 case EGL_COLOR_COMPONENT_TYPE_EXT:
347 match = config.colorComponentType == static_cast<EGLenum>(attributeValue);
348 break;
349 default:
350 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000351 }
352
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000353 if (!match)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000354 {
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000355 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000356 }
357 }
358
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000359 if (match)
360 {
Geoff Lang6812a552015-01-06 17:26:42 -0500361 result.push_back(&config);
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000362 }
363 }
364
Geoff Lang6812a552015-01-06 17:26:42 -0500365 // Sort the result
366 std::sort(result.begin(), result.end(), ConfigSorter(attributeMap));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000367
Geoff Lang6812a552015-01-06 17:26:42 -0500368 return result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000369}
370
Geoff Lang2aaa7b42018-01-12 17:17:27 -0500371ConfigSet::ConfigMap::iterator ConfigSet::begin()
372{
373 return mConfigs.begin();
374}
375
376ConfigSet::ConfigMap::iterator ConfigSet::end()
377{
378 return mConfigs.end();
379}
Jamie Madillb980c562018-11-27 11:34:27 -0500380} // namespace egl