1 //
2 // Copyright 2002 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.5] section 3.4 page 19.
10
11 #include "libANGLE/Config.h"
12 #include "libANGLE/AttributeMap.h"
13
14 #include <algorithm>
15 #include <vector>
16
17 #include <EGL/eglext.h>
18 #include "angle_gl.h"
19
20 #include "common/debug.h"
21
22 namespace egl
23 {
24
Config()25 Config::Config()
26 : 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_RGB_BUFFER),
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),
60 transparentBlueValue(0),
61 optimalOrientation(0),
62 colorComponentType(EGL_COLOR_COMPONENT_TYPE_FIXED_EXT),
63 recordable(EGL_FALSE)
64 {}
65
~Config()66 Config::~Config() {}
67
68 Config::Config(const Config &other) = default;
69
70 Config &Config::operator=(const Config &other) = default;
71
72 ConfigSet::ConfigSet() = default;
73
74 ConfigSet::ConfigSet(const ConfigSet &other) = default;
75
76 ConfigSet &ConfigSet::operator=(const ConfigSet &other) = default;
77
78 ConfigSet::~ConfigSet() = default;
79
add(const Config & config)80 EGLint ConfigSet::add(const Config &config)
81 {
82 // Set the config's ID to a small number that starts at 1 ([EGL 1.5] section 3.4)
83 EGLint id = static_cast<EGLint>(mConfigs.size()) + 1;
84
85 Config copyConfig(config);
86 copyConfig.configID = id;
87 mConfigs.insert(std::make_pair(id, copyConfig));
88
89 return id;
90 }
91
get(EGLint id) const92 const Config &ConfigSet::get(EGLint id) const
93 {
94 ASSERT(mConfigs.find(id) != mConfigs.end());
95 return mConfigs.find(id)->second;
96 }
97
clear()98 void ConfigSet::clear()
99 {
100 mConfigs.clear();
101 }
102
size() const103 size_t ConfigSet::size() const
104 {
105 return mConfigs.size();
106 }
107
contains(const Config * config) const108 bool ConfigSet::contains(const Config *config) const
109 {
110 for (auto i = mConfigs.begin(); i != mConfigs.end(); i++)
111 {
112 const Config &item = i->second;
113 if (config == &item)
114 {
115 return true;
116 }
117 }
118
119 return false;
120 }
121
122 // Function object used by STL sorting routines for ordering Configs according to [EGL 1.5]
123 // section 3.4.1.2 page 28.
124 class ConfigSorter
125 {
126 public:
ConfigSorter(const AttributeMap & attributeMap)127 explicit ConfigSorter(const AttributeMap &attributeMap)
128 : mWantRed(false),
129 mWantGreen(false),
130 mWantBlue(false),
131 mWantAlpha(false),
132 mWantLuminance(false)
133 {
134 scanForWantedComponents(attributeMap);
135 }
136
operator ()(const Config * x,const Config * y) const137 bool operator()(const Config *x, const Config *y) const { return (*this)(*x, *y); }
138
operator ()(const Config & x,const Config & y) const139 bool operator()(const Config &x, const Config &y) const
140 {
141 #define SORT(attribute) \
142 do \
143 { \
144 if (x.attribute != y.attribute) \
145 return x.attribute < y.attribute; \
146 } while (0)
147
148 static_assert(EGL_NONE < EGL_SLOW_CONFIG && EGL_SLOW_CONFIG < EGL_NON_CONFORMANT_CONFIG,
149 "Unexpected EGL enum value.");
150 SORT(configCaveat);
151
152 static_assert(EGL_COLOR_COMPONENT_TYPE_FIXED_EXT < EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT,
153 "Unexpected order of EGL enums.");
154 SORT(colorComponentType);
155
156 static_assert(EGL_RGB_BUFFER < EGL_LUMINANCE_BUFFER, "Unexpected EGL enum value.");
157 SORT(colorBufferType);
158
159 // By larger total number of color bits, only considering those that are requested to be >
160 // 0.
161 EGLint xComponentsSize = wantedComponentsSize(x);
162 EGLint yComponentsSize = wantedComponentsSize(y);
163 if (xComponentsSize != yComponentsSize)
164 {
165 return xComponentsSize > yComponentsSize;
166 }
167
168 SORT(bufferSize);
169 SORT(sampleBuffers);
170 SORT(samples);
171 SORT(depthSize);
172 SORT(stencilSize);
173 SORT(alphaMaskSize);
174 SORT(nativeVisualType);
175 SORT(configID);
176
177 #undef SORT
178
179 return false;
180 }
181
182 private:
wantsComponent(const AttributeMap & attributeMap,EGLAttrib component)183 static bool wantsComponent(const AttributeMap &attributeMap, EGLAttrib component)
184 {
185 // [EGL 1.5] section 3.4.1.2 page 30
186 // Sorting rule #3: by larger total number of color bits, not considering
187 // components that are 0 or don't-care.
188 EGLAttrib value = attributeMap.get(component, 0);
189 return value != 0 && value != EGL_DONT_CARE;
190 }
191
scanForWantedComponents(const AttributeMap & attributeMap)192 void scanForWantedComponents(const AttributeMap &attributeMap)
193 {
194 mWantRed = wantsComponent(attributeMap, EGL_RED_SIZE);
195 mWantGreen = wantsComponent(attributeMap, EGL_GREEN_SIZE);
196 mWantBlue = wantsComponent(attributeMap, EGL_BLUE_SIZE);
197 mWantAlpha = wantsComponent(attributeMap, EGL_ALPHA_SIZE);
198 mWantLuminance = wantsComponent(attributeMap, EGL_LUMINANCE_SIZE);
199 }
200
wantedComponentsSize(const Config & config) const201 EGLint wantedComponentsSize(const Config &config) const
202 {
203 EGLint total = 0;
204
205 if (mWantRed)
206 total += config.redSize;
207 if (mWantGreen)
208 total += config.greenSize;
209 if (mWantBlue)
210 total += config.blueSize;
211 if (mWantAlpha)
212 total += config.alphaSize;
213 if (mWantLuminance)
214 total += config.luminanceSize;
215
216 return total;
217 }
218
219 bool mWantRed;
220 bool mWantGreen;
221 bool mWantBlue;
222 bool mWantAlpha;
223 bool mWantLuminance;
224 };
225
filter(const AttributeMap & attributeMap) const226 std::vector<const Config *> ConfigSet::filter(const AttributeMap &attributeMap) const
227 {
228 std::vector<const Config *> result;
229 result.reserve(mConfigs.size());
230
231 for (auto configIter = mConfigs.begin(); configIter != mConfigs.end(); configIter++)
232 {
233 const Config &config = configIter->second;
234 bool match = true;
235
236 for (auto attribIter = attributeMap.begin(); attribIter != attributeMap.end(); attribIter++)
237 {
238 EGLAttrib attributeKey = attribIter->first;
239 EGLAttrib attributeValue = attribIter->second;
240
241 if (attributeValue == EGL_DONT_CARE)
242 {
243 continue;
244 }
245
246 switch (attributeKey)
247 {
248 case EGL_BUFFER_SIZE:
249 match = config.bufferSize >= attributeValue;
250 break;
251 case EGL_ALPHA_SIZE:
252 match = config.alphaSize >= attributeValue;
253 break;
254 case EGL_BLUE_SIZE:
255 match = config.blueSize >= attributeValue;
256 break;
257 case EGL_GREEN_SIZE:
258 match = config.greenSize >= attributeValue;
259 break;
260 case EGL_RED_SIZE:
261 match = config.redSize >= attributeValue;
262 break;
263 case EGL_DEPTH_SIZE:
264 match = config.depthSize >= attributeValue;
265 break;
266 case EGL_STENCIL_SIZE:
267 match = config.stencilSize >= attributeValue;
268 break;
269 case EGL_CONFIG_CAVEAT:
270 match = config.configCaveat == static_cast<EGLenum>(attributeValue);
271 break;
272 case EGL_CONFIG_ID:
273 match = config.configID == attributeValue;
274 break;
275 case EGL_LEVEL:
276 match = config.level == attributeValue;
277 break;
278 case EGL_NATIVE_RENDERABLE:
279 match = config.nativeRenderable == static_cast<EGLBoolean>(attributeValue);
280 break;
281 case EGL_NATIVE_VISUAL_TYPE:
282 match = config.nativeVisualType == attributeValue;
283 break;
284 case EGL_SAMPLES:
285 match = config.samples >= attributeValue;
286 break;
287 case EGL_SAMPLE_BUFFERS:
288 match = config.sampleBuffers >= attributeValue;
289 break;
290 case EGL_SURFACE_TYPE:
291 match = (config.surfaceType & attributeValue) == attributeValue;
292 break;
293 case EGL_TRANSPARENT_TYPE:
294 match = config.transparentType == static_cast<EGLenum>(attributeValue);
295 break;
296 case EGL_TRANSPARENT_BLUE_VALUE:
297 if (attributeMap.get(EGL_TRANSPARENT_TYPE, EGL_NONE) != EGL_NONE)
298 {
299 match = config.transparentBlueValue == attributeValue;
300 }
301 break;
302 case EGL_TRANSPARENT_GREEN_VALUE:
303 if (attributeMap.get(EGL_TRANSPARENT_TYPE, EGL_NONE) != EGL_NONE)
304 {
305 match = config.transparentGreenValue == attributeValue;
306 }
307 break;
308 case EGL_TRANSPARENT_RED_VALUE:
309 if (attributeMap.get(EGL_TRANSPARENT_TYPE, EGL_NONE) != EGL_NONE)
310 {
311 match = config.transparentRedValue == attributeValue;
312 }
313 break;
314 case EGL_BIND_TO_TEXTURE_RGB:
315 match = config.bindToTextureRGB == static_cast<EGLBoolean>(attributeValue);
316 break;
317 case EGL_BIND_TO_TEXTURE_RGBA:
318 match = config.bindToTextureRGBA == static_cast<EGLBoolean>(attributeValue);
319 break;
320 case EGL_MIN_SWAP_INTERVAL:
321 match = config.minSwapInterval == attributeValue;
322 break;
323 case EGL_MAX_SWAP_INTERVAL:
324 match = config.maxSwapInterval == attributeValue;
325 break;
326 case EGL_LUMINANCE_SIZE:
327 match = config.luminanceSize >= attributeValue;
328 break;
329 case EGL_ALPHA_MASK_SIZE:
330 match = config.alphaMaskSize >= attributeValue;
331 break;
332 case EGL_COLOR_BUFFER_TYPE:
333 match = config.colorBufferType == static_cast<EGLenum>(attributeValue);
334 break;
335 case EGL_RENDERABLE_TYPE:
336 match = (config.renderableType & attributeValue) == attributeValue;
337 break;
338 case EGL_MATCH_NATIVE_PIXMAP:
339 match = false;
340 UNIMPLEMENTED();
341 break;
342 case EGL_CONFORMANT:
343 match = (config.conformant & attributeValue) == attributeValue;
344 break;
345 case EGL_MAX_PBUFFER_WIDTH:
346 match = config.maxPBufferWidth >= attributeValue;
347 break;
348 case EGL_MAX_PBUFFER_HEIGHT:
349 match = config.maxPBufferHeight >= attributeValue;
350 break;
351 case EGL_MAX_PBUFFER_PIXELS:
352 match = config.maxPBufferPixels >= attributeValue;
353 break;
354 case EGL_OPTIMAL_SURFACE_ORIENTATION_ANGLE:
355 match = config.optimalOrientation == attributeValue;
356 break;
357 case EGL_COLOR_COMPONENT_TYPE_EXT:
358 match = config.colorComponentType == static_cast<EGLenum>(attributeValue);
359 break;
360 case EGL_RECORDABLE_ANDROID:
361 match = config.recordable == static_cast<EGLBoolean>(attributeValue);
362 break;
363 default:
364 UNREACHABLE();
365 }
366
367 if (!match)
368 {
369 break;
370 }
371 }
372
373 if (match)
374 {
375 result.push_back(&config);
376 }
377 }
378
379 // Sort the result
380 std::sort(result.begin(), result.end(), ConfigSorter(attributeMap));
381
382 return result;
383 }
384
begin()385 ConfigSet::ConfigMap::iterator ConfigSet::begin()
386 {
387 return mConfigs.begin();
388 }
389
end()390 ConfigSet::ConfigMap::iterator ConfigSet::end()
391 {
392 return mConfigs.end();
393 }
394 } // namespace egl
395