OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ThreadSync.h
Go to the documentation of this file.
1 /* Copyright (c) 2013-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #ifndef THREADSYNC_H
22 #define THREADSYNC_H
23 
24 #include <GLES3/gl3.h>
25 #include "AndroidPlatform.h"
26 #include <stdlib.h>
27 
28 
29 /* These indices describe the cube triangle strips, separated by degenerate triangles where necessary. */
30 static const GLubyte cubeIndices[] =
31 {
32  0, 1, 2, 3, 3, 4, 4, 5, 6, 7, 7, 8, 8, 9, 10, 11, 11, 12, 12, 13, 14, 15, 15, 16, 16, 17, 18, 19, 19, 20, 20, 21, 22, 23,
33 };
34 
35 /* Tri strips, so quads are in this order:
36  *
37  * 2 ----- 3
38  * | \ |
39  * | \ |6 - 7
40  * | \ || \ |
41  * 0 ----- 14 - 5
42  */
43 static const float cubeVertices[] =
44 {
45  /* Front. */
46  -0.5f, -0.5f, 0.5f, /* 0 */
47  0.5f, -0.5f, 0.5f, /* 1 */
48  -0.5f, 0.5f, 0.5f, /* 2 */
49  0.5f, 0.5f, 0.5f, /* 3 */
50 
51  /* Right. */
52  0.5f, -0.5f, 0.5f, /* 4 */
53  0.5f, -0.5f, -0.5f, /* 5 */
54  0.5f, 0.5f, 0.5f, /* 6 */
55  0.5f, 0.5f, -0.5f, /* 7 */
56 
57  /* Back. */
58  0.5f, -0.5f, -0.5f, /* 8 */
59  -0.5f, -0.5f, -0.5f, /* 9 */
60  0.5f, 0.5f, -0.5f, /* 10 */
61  -0.5f, 0.5f, -0.5f, /* 11 */
62 
63  /* Left. */
64  -0.5f, -0.5f, -0.5f, /* 12 */
65  -0.5f, -0.5f, 0.5f, /* 13 */
66  -0.5f, 0.5f, -0.5f, /* 14 */
67  -0.5f, 0.5f, 0.5f, /* 15 */
68 
69  /* Top. */
70  -0.5f, 0.5f, 0.5f, /* 16 */
71  0.5f, 0.5f, 0.5f, /* 17 */
72  -0.5f, 0.5f, -0.5f, /* 18 */
73  0.5f, 0.5f, -0.5f, /* 19 */
74 
75  /* Bottom. */
76  -0.5f, -0.5f, -0.5f, /* 20 */
77  0.5f, -0.5f, -0.5f, /* 21 */
78  -0.5f, -0.5f, 0.5f, /* 22 */
79  0.5f, -0.5f, 0.5f, /* 23 */
80 };
81 
82 static const float cubeTextureCoordinates[] =
83 {
84  /* Front. */
85  0.0f, 0.0f, /* 0 */
86  1.0f, 0.0f, /* 1 */
87  0.0f, 1.0f, /* 2 */
88  1.0f, 1.0f, /* 3 */
89 
90  /* Right. */
91  0.0f, 0.0f, /* 4 */
92  1.0f, 0.0f, /* 5 */
93  0.0f, 1.0f, /* 6 */
94  1.0f, 1.0f, /* 7 */
95 
96  /* Back. */
97  0.0f, 0.0f, /* 8 */
98  1.0f, 0.0f, /* 9 */
99  0.0f, 1.0f, /* 10 */
100  1.0f, 1.0f, /* 11 */
101 
102  /* Left. */
103  0.0f, 0.0f, /* 12 */
104  1.0f, 0.0f, /* 13 */
105  0.0f, 1.0f, /* 14 */
106  1.0f, 1.0f, /* 15 */
107 
108  /* Top. */
109  0.0f, 0.0f, /* 16 */
110  1.0f, 0.0f, /* 17 */
111  0.0f, 1.0f, /* 18 */
112  1.0f, 1.0f, /* 19 */
113 
114  /* Bottom. */
115  0.0f, 0.0f, /* 20 */
116  1.0f, 0.0f, /* 21 */
117  0.0f, 1.0f, /* 22 */
118  1.0f, 1.0f, /* 23 */
119 };
120 
121 static const float cubeColors[] =
122 {
123  /* Front. */
124  0.0f, 0.0f, 0.0f, 1.0f, /* 0 */
125  1.0f, 0.0f, 0.0f, 1.0f, /* 1 */
126  0.0f, 1.0f, 0.0f, 1.0f, /* 2 */
127  1.0f, 1.0f, 0.0f, 1.0f, /* 3 */
128 
129  /* Right. */
130  1.0f, 0.0f, 0.0f, 1.0f, /* 4 */
131  0.0f, 0.0f, 1.0f, 1.0f, /* 5 */
132  1.0f, 1.0f, 0.0f, 1.0f, /* 6 */
133  0.0f, 1.0f, 1.0f, 1.0f, /* 7 */
134 
135  /* Back. */
136  0.0f, 0.0f, 1.0f, 1.0f, /* 8 */
137  1.0f, 0.0f, 1.0f, 1.0f, /* 9 */
138  0.0f, 1.0f, 1.0f, 1.0f, /* 10 */
139  1.0f, 1.0f, 1.0f, 1.0f, /* 11 */
140 
141  /* Left. */
142  1.0f, 0.0f, 1.0f, 1.0f, /* 12 */
143  0.0f, 0.0f, 0.0f, 1.0f, /* 13 */
144  1.0f, 1.0f, 1.0f, 1.0f, /* 14 */
145  0.0f, 1.0f, 0.0f, 1.0f, /* 15 */
146 
147  /* Top. */
148  0.0f, 1.0f, 0.0f, 1.0f, /* 16 */
149  1.0f, 1.0f, 0.0f, 1.0f, /* 17 */
150  1.0f, 1.0f, 1.0f, 1.0f, /* 18 */
151  0.0f, 1.0f, 1.0f, 1.0f, /* 19 */
152 
153  /* Bottom. */
154  1.0f, 0.0f, 1.0f, 1.0f, /* 20 */
155  0.0f, 0.0f, 1.0f, 1.0f, /* 21 */
156  0.0f, 0.0f, 0.0f, 1.0f, /* 22 */
157  1.0f, 0.0f, 0.0f, 1.0f, /* 23 */
158 };
159 
161 {
162  /* DO NOT MODIFY. */
163  /* These attributes are in a known order and may be re-written at initialization according to application requests. */
164  EGL_SAMPLES, 4,
165 
166  EGL_ALPHA_SIZE, 0,
167 
168  EGL_RED_SIZE, 8,
169  EGL_GREEN_SIZE, 8,
170  EGL_BLUE_SIZE, 8,
171  EGL_BUFFER_SIZE, 32,
172 
173  EGL_STENCIL_SIZE, 0,
174  EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, /* This field will be completed according to application request. */
175 
176  EGL_SURFACE_TYPE, EGL_PBUFFER_BIT ,
177 
178  EGL_DEPTH_SIZE, 16,
179 
180  /* MODIFY BELOW HERE. */
181  /* If you need to add or override EGL attributes from above, do it below here. */
182 
183  EGL_NONE
184 };
185 
187 {
188  EGL_CONTEXT_CLIENT_VERSION, 3, /* GLES 3 version is requested. */
189  EGL_NONE
190 };
191 
192 /* [Pixel buffer attributes] */
194 {
195  EGL_WIDTH, 2,
196  EGL_HEIGHT, 2,
197  EGL_TEXTURE_FORMAT, EGL_TEXTURE_RGBA,
198  EGL_TEXTURE_TARGET, EGL_TEXTURE_2D,
199  EGL_NONE
200 };
201 /* [Pixel buffer attributes] */
202 
203 EGLConfig findConfig(EGLDisplay display, bool strictMatch, bool offscreen)
204 {
205  EGLConfig *configsArray = NULL;
206  EGLint numberOfConfigs = 0;
207  EGLBoolean success = EGL_FALSE;
208 
209  /* Enumerate available EGL configurations which match or exceed our required attribute list. */
210  success = eglChooseConfig(display, configAttributes, NULL, 0, &numberOfConfigs);
211  if(success != EGL_TRUE)
212  {
213  EGLint error = eglGetError();
214  LOGE("eglGetError(): %i (0x%.4x)\n", (int)error, (int)error);
215  LOGE("Failed to enumerate EGL configs at %s:%i\n", __FILE__, __LINE__);
216  exit(1);
217  }
218 
219  LOGD("Number of configs found is %d\n", numberOfConfigs);
220 
221  if (numberOfConfigs == 0)
222  {
223  LOGD("Disabling AntiAliasing to try and find a config.\n");
224  configAttributes[1] = EGL_DONT_CARE;
225  success = eglChooseConfig(display, configAttributes, NULL, 0, &numberOfConfigs);
226  if(success != EGL_TRUE)
227  {
228  EGLint error = eglGetError();
229  LOGE("eglGetError(): %i (0x%.4x)\n", (int)error, (int)error);
230  LOGE("Failed to enumerate EGL configs at %s:%i\n", __FILE__, __LINE__);
231  exit(1);
232  }
233 
234  if (numberOfConfigs == 0)
235  {
236  LOGE("No configs found with the requested attributes.\n");
237  exit(1);
238  }
239  else
240  {
241  LOGD("Configs found when antialiasing disabled.\n ");
242  }
243  }
244 
245  /* Allocate space for all EGL configs available and get them. */
246  configsArray = (EGLConfig *)calloc(numberOfConfigs, sizeof(EGLConfig));
247  if(configsArray == NULL)
248  {
249  LOGE("Out of memory at %s:%i\n", __FILE__, __LINE__);
250  exit(1);
251  }
252  success = eglChooseConfig(display, configAttributes, configsArray, numberOfConfigs, &numberOfConfigs);
253  if(success != EGL_TRUE)
254  {
255  EGLint error = eglGetError();
256  LOGE("eglGetError(): %i (0x%.4x)\n", (int)error, (int)error);
257  LOGE("Failed to enumerate EGL configs at %s:%i\n", __FILE__, __LINE__);
258  exit(1);
259  }
260 
261  bool matchFound = false;
262  int matchingConfig = -1;
263 
264  if (strictMatch)
265  {
266  /*
267  * Loop through the EGL configs to find a color depth match.
268  * Note: This is necessary, since EGL considers a higher color depth than requested to be 'better'
269  * even though this may force the driver to use a slow color conversion blitting routine.
270  */
271  EGLint redSize = configAttributes[5];
272  EGLint greenSize = configAttributes[7];
273  EGLint blueSize = configAttributes[9];
274 
275  for(int configsIndex = 0; (configsIndex < numberOfConfigs) && !matchFound; configsIndex++)
276  {
277  EGLint attributeValue = 0;
278 
279  success = eglGetConfigAttrib(display, configsArray[configsIndex], EGL_RED_SIZE, &attributeValue);
280  if(success != EGL_TRUE)
281  {
282  EGLint error = eglGetError();
283  LOGE("eglGetError(): %i (0x%.4x)\n", (int)error, (int)error);
284  LOGE("Failed to get EGL attribute at %s:%i\n", __FILE__, __LINE__);
285  exit(1);
286  }
287 
288  if(attributeValue == redSize)
289  {
290  success = eglGetConfigAttrib(display, configsArray[configsIndex], EGL_GREEN_SIZE, &attributeValue);
291  if(success != EGL_TRUE)
292  {
293  EGLint error = eglGetError();
294  LOGE("eglGetError(): %i (0x%.4x)\n", (int)error, (int)error);
295  LOGE("Failed to get EGL attribute at %s:%i\n", __FILE__, __LINE__);
296  exit(1);
297  }
298 
299  if(attributeValue == greenSize)
300  {
301  success = eglGetConfigAttrib(display, configsArray[configsIndex], EGL_BLUE_SIZE, &attributeValue);
302  if(success != EGL_TRUE)
303  {
304  EGLint error = eglGetError();
305  LOGE("eglGetError(): %i (0x%.4x)\n", (int)error, (int)error);
306  LOGE("Failed to get EGL attribute at %s:%i\n", __FILE__, __LINE__);
307  exit(1);
308  }
309 
310  if(attributeValue == blueSize)
311  {
312  if(offscreen)
313  {
314  success = eglGetConfigAttrib(display, configsArray[configsIndex], EGL_SURFACE_TYPE, &attributeValue);
315  if(success != EGL_TRUE)
316  {
317  EGLint error = eglGetError();
318  LOGE("eglGetError(): %i (0x%.4x)\n", (int)error, (int)error);
319  LOGE("Failed to get EGL attribute at %s:%i\n", __FILE__, __LINE__);
320  exit(1);
321  }
322  if(attributeValue & EGL_PBUFFER_BIT)
323  {
324  matchFound = true;
325  matchingConfig = configsIndex;
326  }
327 
328  }
329  else
330  {
331  matchFound = true;
332  matchingConfig = configsIndex;
333  }
334  }
335  }
336  }
337  }
338  }
339  else
340  {
341  /* If not strict we can use any matching config. */
342  matchingConfig = 0;
343  matchFound = true;
344  }
345 
346  if(!matchFound)
347  {
348  LOGE("Failed to find matching EGL config at %s:%i\n", __FILE__, __LINE__);
349  exit(1);
350  }
351 
352  /* Return the matching config. */
353  EGLConfig configToReturn = configsArray[matchingConfig];
354 
355  free(configsArray);
356  configsArray = NULL;
357 
358  return configToReturn;
359 }
360 
361 #endif /* THREADSYNC_H */
#define LOGD(...)
Definition: AstcTextures.h:28
static const float cubeColors[]
Definition: ThreadSync.h:121
EGLint configAttributes[]
Definition: ThreadSync.h:160
static const float cubeVertices[]
Definition: ThreadSync.h:43
EGLint contextAttributes[]
Definition: ThreadSync.h:186
static const float cubeTextureCoordinates[]
Definition: ThreadSync.h:82
#define LOGE(...)
Definition: AstcTextures.h:30
EGLConfig findConfig(EGLDisplay display, bool strictMatch, bool offscreen)
Definition: ThreadSync.h:203
static const GLubyte cubeIndices[]
Definition: ThreadSync.h:30
EGLint pBufferAttributes[]
Definition: ThreadSync.h:193