blob: d511df3a69aeda5c23d80efe58bbcdb64b9d3dcf [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
Jamie Madillf51639a2014-06-25 16:04:57 -040017#include "angle_gl.h"
shannon.woods%transgaming.com@gtempaccount.comdcf33d52013-04-13 03:33:11 +000018#include <EGL/eglext.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),
61 optimalOrientation(0)
Geoff Lang6812a552015-01-06 17:26:42 -050062{
63}
64
Geoff Lang6812a552015-01-06 17:26:42 -050065EGLint ConfigSet::add(const Config &config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000066{
Geoff Lang6812a552015-01-06 17:26:42 -050067 // 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 -070068 EGLint id = static_cast<EGLint>(mConfigs.size()) + 1;
Geoff Lang6812a552015-01-06 17:26:42 -050069
70 Config copyConfig(config);
Geoff Langc223dc62015-01-09 13:10:01 -050071 copyConfig.configID = id;
Geoff Lang6812a552015-01-06 17:26:42 -050072 mConfigs.insert(std::make_pair(id, copyConfig));
73
74 return id;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000075}
76
Geoff Lang6812a552015-01-06 17:26:42 -050077const Config &ConfigSet::get(EGLint id) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000078{
Jamie Madill73edef02015-02-17 18:58:21 -050079 ASSERT(mConfigs.find(id) != mConfigs.end());
80 return mConfigs.find(id)->second;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000081}
82
Geoff Lang6812a552015-01-06 17:26:42 -050083void ConfigSet::clear()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000084{
Geoff Lang6812a552015-01-06 17:26:42 -050085 mConfigs.clear();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000086}
87
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000088size_t ConfigSet::size() const
89{
Geoff Lang6812a552015-01-06 17:26:42 -050090 return mConfigs.size();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000091}
92
Geoff Lang6812a552015-01-06 17:26:42 -050093bool ConfigSet::contains(const Config *config) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000094{
Geoff Lang6812a552015-01-06 17:26:42 -050095 for (auto i = mConfigs.begin(); i != mConfigs.end(); i++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000096 {
Geoff Lang6812a552015-01-06 17:26:42 -050097 const Config &item = i->second;
98 if (config == &item)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000099 {
Geoff Lang6812a552015-01-06 17:26:42 -0500100 return true;
101 }
102 }
103
104 return false;
105}
106
107// Function object used by STL sorting routines for ordering Configs according to [EGL 1.5] section 3.4.1.2 page 28.
108class ConfigSorter
109{
110 public:
111 explicit ConfigSorter(const AttributeMap &attributeMap)
Jamie Madill56596192015-03-19 10:23:13 -0400112 : mWantRed(false),
113 mWantGreen(false),
114 mWantBlue(false),
115 mWantAlpha(false),
116 mWantLuminance(false)
Geoff Lang6812a552015-01-06 17:26:42 -0500117 {
118 scanForWantedComponents(attributeMap);
119 }
120
121 bool operator()(const Config *x, const Config *y) const
122 {
123 return (*this)(*x, *y);
124 }
125
126 bool operator()(const Config &x, const Config &y) const
127 {
128 #define SORT(attribute) \
129 if (x.attribute != y.attribute) \
130 { \
131 return x.attribute < y.attribute; \
132 }
133
Geoff Langd4475812015-03-18 10:53:05 -0400134 static_assert(EGL_NONE < EGL_SLOW_CONFIG && EGL_SLOW_CONFIG < EGL_NON_CONFORMANT_CONFIG, "Unexpected EGL enum value.");
Geoff Langc223dc62015-01-09 13:10:01 -0500135 SORT(configCaveat);
Geoff Lang6812a552015-01-06 17:26:42 -0500136
Geoff Langd4475812015-03-18 10:53:05 -0400137 static_assert(EGL_RGB_BUFFER < EGL_LUMINANCE_BUFFER, "Unexpected EGL enum value.");
Geoff Langc223dc62015-01-09 13:10:01 -0500138 SORT(colorBufferType);
Geoff Lang6812a552015-01-06 17:26:42 -0500139
140 // By larger total number of color bits, only considering those that are requested to be > 0.
141 EGLint xComponentsSize = wantedComponentsSize(x);
142 EGLint yComponentsSize = wantedComponentsSize(y);
143 if (xComponentsSize != yComponentsSize)
144 {
145 return xComponentsSize > yComponentsSize;
146 }
147
Geoff Langc223dc62015-01-09 13:10:01 -0500148 SORT(bufferSize);
149 SORT(sampleBuffers);
150 SORT(samples);
151 SORT(depthSize);
152 SORT(stencilSize);
153 SORT(alphaMaskSize);
154 SORT(nativeVisualType);
155 SORT(configID);
Geoff Lang6812a552015-01-06 17:26:42 -0500156
157 #undef SORT
158
159 return false;
160 }
161
162 private:
163 void scanForWantedComponents(const AttributeMap &attributeMap)
164 {
165 // [EGL 1.5] section 3.4.1.2 page 30
166 // Sorting rule #3: by larger total number of color bits, not considering
167 // components that are 0 or don't-care.
168 for (auto attribIter = attributeMap.begin(); attribIter != attributeMap.end(); attribIter++)
169 {
170 EGLint attributeKey = attribIter->first;
171 EGLint attributeValue = attribIter->second;
172 if (attributeKey != 0 && attributeValue != EGL_DONT_CARE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000173 {
Geoff Lang6812a552015-01-06 17:26:42 -0500174 switch (attributeKey)
175 {
176 case EGL_RED_SIZE: mWantRed = true; break;
177 case EGL_GREEN_SIZE: mWantGreen = true; break;
178 case EGL_BLUE_SIZE: mWantBlue = true; break;
179 case EGL_ALPHA_SIZE: mWantAlpha = true; break;
180 case EGL_LUMINANCE_SIZE: mWantLuminance = true; break;
181 }
182 }
183 }
184 }
185
186 EGLint wantedComponentsSize(const Config &config) const
187 {
188 EGLint total = 0;
189
Geoff Langc223dc62015-01-09 13:10:01 -0500190 if (mWantRed) total += config.redSize;
191 if (mWantGreen) total += config.greenSize;
192 if (mWantBlue) total += config.blueSize;
193 if (mWantAlpha) total += config.alphaSize;
194 if (mWantLuminance) total += config.luminanceSize;
Geoff Lang6812a552015-01-06 17:26:42 -0500195
196 return total;
197 }
198
199 bool mWantRed;
200 bool mWantGreen;
201 bool mWantBlue;
202 bool mWantAlpha;
203 bool mWantLuminance;
204};
205
206std::vector<const Config*> ConfigSet::filter(const AttributeMap &attributeMap) const
207{
208 std::vector<const Config*> result;
209 result.reserve(mConfigs.size());
210
211 for (auto configIter = mConfigs.begin(); configIter != mConfigs.end(); configIter++)
212 {
213 const Config &config = configIter->second;
214 bool match = true;
215
216 for (auto attribIter = attributeMap.begin(); attribIter != attributeMap.end(); attribIter++)
217 {
218 EGLint attributeKey = attribIter->first;
219 EGLint attributeValue = attribIter->second;
220
221 switch (attributeKey)
222 {
Geoff Langc223dc62015-01-09 13:10:01 -0500223 case EGL_BUFFER_SIZE: match = config.bufferSize >= attributeValue; break;
224 case EGL_ALPHA_SIZE: match = config.alphaSize >= attributeValue; break;
225 case EGL_BLUE_SIZE: match = config.blueSize >= attributeValue; break;
226 case EGL_GREEN_SIZE: match = config.greenSize >= attributeValue; break;
227 case EGL_RED_SIZE: match = config.redSize >= attributeValue; break;
228 case EGL_DEPTH_SIZE: match = config.depthSize >= attributeValue; break;
229 case EGL_STENCIL_SIZE: match = config.stencilSize >= attributeValue; break;
230 case EGL_CONFIG_CAVEAT: match = config.configCaveat == (EGLenum)attributeValue; break;
231 case EGL_CONFIG_ID: match = config.configID == attributeValue; break;
232 case EGL_LEVEL: match = config.level >= attributeValue; break;
233 case EGL_NATIVE_RENDERABLE: match = config.nativeRenderable == (EGLBoolean)attributeValue; break;
234 case EGL_NATIVE_VISUAL_TYPE: match = config.nativeVisualType == attributeValue; break;
235 case EGL_SAMPLES: match = config.samples >= attributeValue; break;
236 case EGL_SAMPLE_BUFFERS: match = config.sampleBuffers >= attributeValue; break;
237 case EGL_SURFACE_TYPE: match = (config.surfaceType & attributeValue) == attributeValue; break;
238 case EGL_TRANSPARENT_TYPE: match = config.transparentType == (EGLenum)attributeValue; break;
239 case EGL_TRANSPARENT_BLUE_VALUE: match = config.transparentBlueValue == attributeValue; break;
240 case EGL_TRANSPARENT_GREEN_VALUE: match = config.transparentGreenValue == attributeValue; break;
241 case EGL_TRANSPARENT_RED_VALUE: match = config.transparentRedValue == attributeValue; break;
242 case EGL_BIND_TO_TEXTURE_RGB: match = config.bindToTextureRGB == (EGLBoolean)attributeValue; break;
243 case EGL_BIND_TO_TEXTURE_RGBA: match = config.bindToTextureRGBA == (EGLBoolean)attributeValue; break;
244 case EGL_MIN_SWAP_INTERVAL: match = config.minSwapInterval == attributeValue; break;
245 case EGL_MAX_SWAP_INTERVAL: match = config.maxSwapInterval == attributeValue; break;
246 case EGL_LUMINANCE_SIZE: match = config.luminanceSize >= attributeValue; break;
247 case EGL_ALPHA_MASK_SIZE: match = config.alphaMaskSize >= attributeValue; break;
248 case EGL_COLOR_BUFFER_TYPE: match = config.colorBufferType == (EGLenum)attributeValue; break;
249 case EGL_RENDERABLE_TYPE: match = (config.renderableType & attributeValue) == attributeValue; break;
250 case EGL_MATCH_NATIVE_PIXMAP: match = false; UNIMPLEMENTED(); break;
251 case EGL_CONFORMANT: match = (config.conformant & attributeValue) == attributeValue; break;
252 case EGL_MAX_PBUFFER_WIDTH: match = config.maxPBufferWidth >= attributeValue; break;
253 case EGL_MAX_PBUFFER_HEIGHT: match = config.maxPBufferHeight >= attributeValue; break;
254 case EGL_MAX_PBUFFER_PIXELS: match = config.maxPBufferPixels >= attributeValue; break;
Geoff Lang7f448b52015-12-16 13:31:57 -0500255 case EGL_OPTIMAL_SURFACE_ORIENTATION_ANGLE:
256 match = config.optimalOrientation == attributeValue;
257 break;
Geoff Lang6812a552015-01-06 17:26:42 -0500258 default: UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000259 }
260
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000261 if (!match)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000262 {
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000263 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000264 }
265 }
266
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000267 if (match)
268 {
Geoff Lang6812a552015-01-06 17:26:42 -0500269 result.push_back(&config);
daniel@transgaming.com6a94b972010-05-13 02:02:34 +0000270 }
271 }
272
Geoff Lang6812a552015-01-06 17:26:42 -0500273 // Sort the result
274 std::sort(result.begin(), result.end(), ConfigSorter(attributeMap));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000275
Geoff Lang6812a552015-01-06 17:26:42 -0500276 return result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000277}
278
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000279}