blob: 2143c787629b409c619089a5780de6e1dd28b0c1 [file] [log] [blame]
Mathias Agopian875d8e12013-06-07 15:35:48 -07001/*
2 * Copyright 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mark Salyzyn7823e122016-09-29 08:08:05 -070017#include <log/log.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070018#include <ui/Rect.h>
19#include <ui/Region.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070020
21#include "RenderEngine.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070022#include "GLES20RenderEngine.h"
Mathias Agopian875d8e12013-06-07 15:35:48 -070023#include "GLExtensions.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070024#include "Mesh.h"
Mathias Agopian875d8e12013-06-07 15:35:48 -070025
Fabien Sanglardc93afd52017-03-13 13:02:42 -070026#include <vector>
27#include <SurfaceFlinger.h>
28
Jiyong Park00b15b82017-08-10 20:30:56 +090029extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
Jesse Hall19e87292013-12-23 21:02:15 -080030
Mathias Agopian875d8e12013-06-07 15:35:48 -070031// ---------------------------------------------------------------------------
32namespace android {
33// ---------------------------------------------------------------------------
34
Jesse Hall19e87292013-12-23 21:02:15 -080035static bool findExtension(const char* exts, const char* name) {
36 if (!exts)
37 return false;
38 size_t len = strlen(name);
Jesse Hall05f8c702013-12-23 20:44:38 -080039
Jesse Hall19e87292013-12-23 21:02:15 -080040 const char* pos = exts;
41 while ((pos = strstr(pos, name)) != NULL) {
42 if (pos[len] == '\0' || pos[len] == ' ')
43 return true;
44 pos += len;
Mathias Agopian2185f8b2013-09-18 16:20:26 -070045 }
46
Jesse Hall19e87292013-12-23 21:02:15 -080047 return false;
48}
49
Chia-I Wud4d9c6f2017-11-09 16:55:31 -080050std::unique_ptr<RenderEngine> RenderEngine::create(int hwcFormat, uint32_t featureFlags) {
51 // initialize EGL for the default display
52 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
53 if (!eglInitialize(display, NULL, NULL)) {
54 LOG_ALWAYS_FATAL("failed to initialize EGL");
55 }
56
Jesse Hall19e87292013-12-23 21:02:15 -080057 // EGL_ANDROIDX_no_config_context is an experimental extension with no
58 // written specification. It will be replaced by something more formal.
59 // SurfaceFlinger is using it to allow a single EGLContext to render to
60 // both a 16-bit primary display framebuffer and a 32-bit virtual display
61 // framebuffer.
62 //
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -060063 // EGL_KHR_no_config_context is official extension to allow creating a
64 // context that works with any surface of a display.
65 //
Jesse Hall19e87292013-12-23 21:02:15 -080066 // The code assumes that ES2 or later is available if this extension is
67 // supported.
68 EGLConfig config = EGL_NO_CONFIG;
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -060069 if (!findExtension(eglQueryStringImplementationANDROID(display, EGL_EXTENSIONS),
70 "EGL_ANDROIDX_no_config_context") &&
71 !findExtension(eglQueryStringImplementationANDROID(display, EGL_EXTENSIONS),
72 "EGL_KHR_no_config_context")) {
Steven Thomasd7f49c52017-07-26 18:48:28 -070073 config = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
Jesse Hall19e87292013-12-23 21:02:15 -080074 }
75
76 EGLint renderableType = 0;
77 if (config == EGL_NO_CONFIG) {
78 renderableType = EGL_OPENGL_ES2_BIT;
79 } else if (!eglGetConfigAttrib(display, config,
80 EGL_RENDERABLE_TYPE, &renderableType)) {
81 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
82 }
83 EGLint contextClientVersion = 0;
Mathias Agopian2185f8b2013-09-18 16:20:26 -070084 if (renderableType & EGL_OPENGL_ES2_BIT) {
85 contextClientVersion = 2;
86 } else if (renderableType & EGL_OPENGL_ES_BIT) {
87 contextClientVersion = 1;
88 } else {
89 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
90 }
91
Fabien Sanglardc93afd52017-03-13 13:02:42 -070092 std::vector<EGLint> contextAttributes;
93 contextAttributes.reserve(6);
94 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
95 contextAttributes.push_back(contextClientVersion);
Mathias Agopian875d8e12013-06-07 15:35:48 -070096#ifdef EGL_IMG_context_priority
Fabien Sanglardc93afd52017-03-13 13:02:42 -070097 if (SurfaceFlinger::useContextPriority) {
98 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
99 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
100 }
Mathias Agopian875d8e12013-06-07 15:35:48 -0700101#endif
Fabien Sanglardc93afd52017-03-13 13:02:42 -0700102 contextAttributes.push_back(EGL_NONE);
103 contextAttributes.push_back(EGL_NONE);
104
105 EGLContext ctxt = eglCreateContext(display, config, NULL,
106 contextAttributes.data());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700107
108 // if can't create a GL context, we can only abort.
109 LOG_ALWAYS_FATAL_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
110
111
112 // now figure out what version of GL did we actually get
113 // NOTE: a dummy surface is not needed if KHR_create_context is supported
114
Jesse Hall19e87292013-12-23 21:02:15 -0800115 EGLConfig dummyConfig = config;
116 if (dummyConfig == EGL_NO_CONFIG) {
Steven Thomasd7f49c52017-07-26 18:48:28 -0700117 dummyConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
Jesse Hall19e87292013-12-23 21:02:15 -0800118 }
Mathias Agopian875d8e12013-06-07 15:35:48 -0700119 EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE, EGL_NONE };
Jesse Hall19e87292013-12-23 21:02:15 -0800120 EGLSurface dummy = eglCreatePbufferSurface(display, dummyConfig, attribs);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700121 LOG_ALWAYS_FATAL_IF(dummy==EGL_NO_SURFACE, "can't create dummy pbuffer");
122 EGLBoolean success = eglMakeCurrent(display, dummy, dummy, ctxt);
123 LOG_ALWAYS_FATAL_IF(!success, "can't make dummy pbuffer current");
124
125 GLExtensions& extensions(GLExtensions::getInstance());
126 extensions.initWithGLStrings(
127 glGetString(GL_VENDOR),
128 glGetString(GL_RENDERER),
129 glGetString(GL_VERSION),
130 glGetString(GL_EXTENSIONS));
131
132 GlesVersion version = parseGlesVersion( extensions.getVersion() );
133
134 // initialize the renderer while GL is current
135
Chia-I Wub2c76242017-11-09 17:17:07 -0800136 std::unique_ptr<RenderEngine> engine;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700137 switch (version) {
138 case GLES_VERSION_1_0:
Mathias Agopian875d8e12013-06-07 15:35:48 -0700139 case GLES_VERSION_1_1:
Fabien Sanglardefb93452016-10-04 14:02:11 -0700140 LOG_ALWAYS_FATAL("SurfaceFlinger requires OpenGL ES 2.0 minimum to run.");
Mathias Agopian875d8e12013-06-07 15:35:48 -0700141 break;
142 case GLES_VERSION_2_0:
143 case GLES_VERSION_3_0:
Chia-I Wub2c76242017-11-09 17:17:07 -0800144 engine = std::make_unique<GLES20RenderEngine>(featureFlags);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700145 break;
146 }
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800147 engine->setEGLHandles(display, config, ctxt);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700148
149 ALOGI("OpenGL ES informations:");
150 ALOGI("vendor : %s", extensions.getVendor());
151 ALOGI("renderer : %s", extensions.getRenderer());
152 ALOGI("version : %s", extensions.getVersion());
153 ALOGI("extensions: %s", extensions.getExtension());
Michael Lentine9ae79d82014-07-30 16:42:12 -0700154 ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
155 ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700156
157 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
158 eglDestroySurface(display, dummy);
159
160 return engine;
161}
162
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800163RenderEngine::RenderEngine() : mEGLDisplay(EGL_NO_DISPLAY), mEGLConfig(NULL),
164 mEGLContext(EGL_NO_CONTEXT) {
Mathias Agopian875d8e12013-06-07 15:35:48 -0700165}
166
167RenderEngine::~RenderEngine() {
Chia-I Wub01450b2017-11-09 16:55:31 -0800168 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
169 eglTerminate(mEGLDisplay);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700170}
171
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800172void RenderEngine::setEGLHandles(EGLDisplay display, EGLConfig config, EGLContext ctxt) {
173 mEGLDisplay = display;
Jesse Hall05f8c702013-12-23 20:44:38 -0800174 mEGLConfig = config;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700175 mEGLContext = ctxt;
176}
177
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800178EGLDisplay RenderEngine::getEGLDisplay() const {
179 return mEGLDisplay;
180}
181
182EGLConfig RenderEngine::getEGLConfig() const {
Jesse Hall05f8c702013-12-23 20:44:38 -0800183 return mEGLConfig;
184}
185
Chia-I Wu7f402902017-11-09 12:51:10 -0800186bool RenderEngine::setCurrentSurface(EGLSurface surface) {
187 return eglMakeCurrent(mEGLDisplay, surface, surface, mEGLContext) == EGL_TRUE;
188}
189
190void RenderEngine::resetCurrentSurface() {
191 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
192}
193
Mathias Agopian875d8e12013-06-07 15:35:48 -0700194void RenderEngine::checkErrors() const {
195 do {
196 // there could be more than one error flag
197 GLenum error = glGetError();
198 if (error == GL_NO_ERROR)
199 break;
200 ALOGE("GL error 0x%04x", int(error));
201 } while (true);
202}
203
204RenderEngine::GlesVersion RenderEngine::parseGlesVersion(const char* str) {
205 int major, minor;
206 if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) {
207 if (sscanf(str, "OpenGL ES %d.%d", &major, &minor) != 2) {
208 ALOGW("Unable to parse GL_VERSION string: \"%s\"", str);
209 return GLES_VERSION_1_0;
210 }
211 }
212
213 if (major == 1 && minor == 0) return GLES_VERSION_1_0;
214 if (major == 1 && minor >= 1) return GLES_VERSION_1_1;
215 if (major == 2 && minor >= 0) return GLES_VERSION_2_0;
216 if (major == 3 && minor >= 0) return GLES_VERSION_3_0;
217
218 ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor);
219 return GLES_VERSION_1_0;
220}
221
Mathias Agopian3f844832013-08-07 21:24:32 -0700222void RenderEngine::fillRegionWithColor(const Region& region, uint32_t height,
223 float red, float green, float blue, float alpha) {
224 size_t c;
225 Rect const* r = region.getArray(&c);
226 Mesh mesh(Mesh::TRIANGLES, c*6, 2);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700227 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Mathias Agopian3f844832013-08-07 21:24:32 -0700228 for (size_t i=0 ; i<c ; i++, r++) {
Mathias Agopian5cdc8992013-08-13 20:51:23 -0700229 position[i*6 + 0].x = r->left;
230 position[i*6 + 0].y = height - r->top;
231 position[i*6 + 1].x = r->left;
232 position[i*6 + 1].y = height - r->bottom;
233 position[i*6 + 2].x = r->right;
234 position[i*6 + 2].y = height - r->bottom;
235 position[i*6 + 3].x = r->left;
236 position[i*6 + 3].y = height - r->top;
237 position[i*6 + 4].x = r->right;
238 position[i*6 + 4].y = height - r->bottom;
239 position[i*6 + 5].x = r->right;
240 position[i*6 + 5].y = height - r->top;
Mathias Agopian3f844832013-08-07 21:24:32 -0700241 }
Mathias Agopian19733a32013-08-28 18:13:56 -0700242 setupFillWithColor(red, green, blue, alpha);
243 drawMesh(mesh);
Mathias Agopian3f844832013-08-07 21:24:32 -0700244}
245
Chia-I Wub0c041b2017-11-09 11:36:33 -0800246int RenderEngine::flush(bool wait) {
247 // Attempt to create a sync khr object that can produce a sync point. If that
248 // isn't available, create a non-dupable sync object in the fallback path and
249 // wait on it directly.
250 EGLSyncKHR sync;
251 if (!wait) {
252 EGLint syncFd = EGL_NO_NATIVE_FENCE_FD_ANDROID;
253
254 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
255 if (sync != EGL_NO_SYNC_KHR) {
256 // native fence fd will not be populated until flush() is done.
257 glFlush();
258
259 // get the sync fd
260 syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync);
261 if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
262 ALOGW("failed to dup sync khr object");
263 }
264
265 eglDestroySyncKHR(mEGLDisplay, sync);
266 }
267
268 if (syncFd != EGL_NO_NATIVE_FENCE_FD_ANDROID) {
269 return syncFd;
270 }
271 }
272
273 // fallback or explicit wait
274 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
275 if (sync != EGL_NO_SYNC_KHR) {
276 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
277 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
278 EGLint eglErr = eglGetError();
279 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
280 ALOGW("fence wait timed out");
281 } else {
282 ALOGW_IF(eglErr != EGL_SUCCESS,
283 "error waiting on EGL fence: %#x", eglErr);
284 }
285 eglDestroySyncKHR(mEGLDisplay, sync);
286 } else {
287 ALOGW("error creating EGL fence: %#x", eglGetError());
288 }
289
290 return -1;
Riley Andrews9707f4d2014-10-23 16:17:04 -0700291}
292
Mathias Agopian3f844832013-08-07 21:24:32 -0700293void RenderEngine::clearWithColor(float red, float green, float blue, float alpha) {
294 glClearColor(red, green, blue, alpha);
295 glClear(GL_COLOR_BUFFER_BIT);
296}
297
298void RenderEngine::setScissor(
299 uint32_t left, uint32_t bottom, uint32_t right, uint32_t top) {
300 glScissor(left, bottom, right, top);
301 glEnable(GL_SCISSOR_TEST);
302}
303
304void RenderEngine::disableScissor() {
305 glDisable(GL_SCISSOR_TEST);
306}
307
308void RenderEngine::genTextures(size_t count, uint32_t* names) {
309 glGenTextures(count, names);
310}
311
312void RenderEngine::deleteTextures(size_t count, uint32_t const* names) {
313 glDeleteTextures(count, names);
314}
315
Mathias Agopiand5556842013-09-19 17:08:37 -0700316void RenderEngine::readPixels(size_t l, size_t b, size_t w, size_t h, uint32_t* pixels) {
317 glReadPixels(l, b, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
318}
319
Mathias Agopian458197d2013-08-15 14:56:51 -0700320void RenderEngine::dump(String8& result) {
Chia-I Wu8601f882017-11-09 16:52:21 -0800321 result.appendFormat("EGL implementation : %s\n",
322 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
323 result.appendFormat("%s\n",
324 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
325
Mathias Agopian458197d2013-08-15 14:56:51 -0700326 const GLExtensions& extensions(GLExtensions::getInstance());
327 result.appendFormat("GLES: %s, %s, %s\n",
328 extensions.getVendor(),
329 extensions.getRenderer(),
330 extensions.getVersion());
331 result.appendFormat("%s\n", extensions.getExtension());
332}
333
Mathias Agopian3f844832013-08-07 21:24:32 -0700334// ---------------------------------------------------------------------------
335
Chia-I Wueadbaa62017-11-09 11:26:15 -0800336RenderEngine::BindNativeBufferAsFramebuffer::BindNativeBufferAsFramebuffer(
337 RenderEngine& engine, ANativeWindowBuffer* buffer) : mEngine(engine)
Mathias Agopian3f844832013-08-07 21:24:32 -0700338{
Chia-I Wueadbaa62017-11-09 11:26:15 -0800339 mImage = eglCreateImageKHR(mEngine.mEGLDisplay, EGL_NO_CONTEXT,
340 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
341 if (mImage == EGL_NO_IMAGE_KHR) {
342 mStatus = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
343 return;
344 }
345
346 mEngine.bindImageAsFramebuffer(mImage, &mTexName, &mFbName, &mStatus);
Mathias Agopian458197d2013-08-15 14:56:51 -0700347
Mathias Agopian3f844832013-08-07 21:24:32 -0700348 ALOGE_IF(mStatus != GL_FRAMEBUFFER_COMPLETE_OES,
349 "glCheckFramebufferStatusOES error %d", mStatus);
Mathias Agopian3f844832013-08-07 21:24:32 -0700350}
351
Chia-I Wueadbaa62017-11-09 11:26:15 -0800352RenderEngine::BindNativeBufferAsFramebuffer::~BindNativeBufferAsFramebuffer() {
353 if (mImage == EGL_NO_IMAGE_KHR) {
354 return;
355 }
356
Mathias Agopian3f844832013-08-07 21:24:32 -0700357 // back to main framebuffer
Mathias Agopian458197d2013-08-15 14:56:51 -0700358 mEngine.unbindFramebuffer(mTexName, mFbName);
Chia-I Wueadbaa62017-11-09 11:26:15 -0800359 eglDestroyImageKHR(mEngine.mEGLDisplay, mImage);
Mathias Agopian3f844832013-08-07 21:24:32 -0700360}
361
Chia-I Wueadbaa62017-11-09 11:26:15 -0800362status_t RenderEngine::BindNativeBufferAsFramebuffer::getStatus() const {
Mathias Agopian3f844832013-08-07 21:24:32 -0700363 return mStatus == GL_FRAMEBUFFER_COMPLETE_OES ? NO_ERROR : BAD_VALUE;
364}
365
Mathias Agopian875d8e12013-06-07 15:35:48 -0700366// ---------------------------------------------------------------------------
Jesse Hall05f8c702013-12-23 20:44:38 -0800367
368static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs,
369 EGLint attribute, EGLint wanted, EGLConfig* outConfig) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800370 EGLint numConfigs = -1, n = 0;
371 eglGetConfigs(dpy, NULL, 0, &numConfigs);
372 EGLConfig* const configs = new EGLConfig[numConfigs];
373 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
374
375 if (n) {
376 if (attribute != EGL_NONE) {
377 for (int i=0 ; i<n ; i++) {
378 EGLint value = 0;
379 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
380 if (wanted == value) {
381 *outConfig = configs[i];
382 delete [] configs;
383 return NO_ERROR;
384 }
385 }
386 } else {
387 // just pick the first one
388 *outConfig = configs[0];
389 delete [] configs;
390 return NO_ERROR;
391 }
392 }
393 delete [] configs;
394 return NAME_NOT_FOUND;
395}
396
397class EGLAttributeVector {
398 struct Attribute;
399 class Adder;
400 friend class Adder;
401 KeyedVector<Attribute, EGLint> mList;
402 struct Attribute {
Pablo Ceballos53390e12015-08-04 11:25:59 -0700403 Attribute() : v(0) {};
Chih-Hung Hsiehc4067912016-05-03 14:03:27 -0700404 explicit Attribute(EGLint v) : v(v) { }
Jesse Hall05f8c702013-12-23 20:44:38 -0800405 EGLint v;
406 bool operator < (const Attribute& other) const {
407 // this places EGL_NONE at the end
408 EGLint lhs(v);
409 EGLint rhs(other.v);
410 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
411 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
412 return lhs < rhs;
413 }
414 };
415 class Adder {
416 friend class EGLAttributeVector;
417 EGLAttributeVector& v;
418 EGLint attribute;
419 Adder(EGLAttributeVector& v, EGLint attribute)
420 : v(v), attribute(attribute) {
421 }
422 public:
423 void operator = (EGLint value) {
424 if (attribute != EGL_NONE) {
Chih-Hung Hsiehc4067912016-05-03 14:03:27 -0700425 v.mList.add(Attribute(attribute), value);
Jesse Hall05f8c702013-12-23 20:44:38 -0800426 }
427 }
428 operator EGLint () const { return v.mList[attribute]; }
429 };
430public:
431 EGLAttributeVector() {
Chih-Hung Hsiehc4067912016-05-03 14:03:27 -0700432 mList.add(Attribute(EGL_NONE), EGL_NONE);
Jesse Hall05f8c702013-12-23 20:44:38 -0800433 }
434 void remove(EGLint attribute) {
435 if (attribute != EGL_NONE) {
Chih-Hung Hsiehc4067912016-05-03 14:03:27 -0700436 mList.removeItem(Attribute(attribute));
Jesse Hall05f8c702013-12-23 20:44:38 -0800437 }
438 }
439 Adder operator [] (EGLint attribute) {
440 return Adder(*this, attribute);
441 }
442 EGLint operator [] (EGLint attribute) const {
443 return mList[attribute];
444 }
445 // cast-operator to (EGLint const*)
446 operator EGLint const* () const { return &mList.keyAt(0).v; }
447};
448
449
450static status_t selectEGLConfig(EGLDisplay display, EGLint format,
451 EGLint renderableType, EGLConfig* config) {
452 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
453 // it is to be used with WIFI displays
454 status_t err;
455 EGLint wantedAttribute;
456 EGLint wantedAttributeValue;
457
458 EGLAttributeVector attribs;
459 if (renderableType) {
460 attribs[EGL_RENDERABLE_TYPE] = renderableType;
461 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
462 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT|EGL_PBUFFER_BIT;
463 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
464 attribs[EGL_RED_SIZE] = 8;
465 attribs[EGL_GREEN_SIZE] = 8;
466 attribs[EGL_BLUE_SIZE] = 8;
neo.hee7f39722017-03-21 11:48:36 +0800467 attribs[EGL_ALPHA_SIZE] = 8;
Jesse Hall05f8c702013-12-23 20:44:38 -0800468 wantedAttribute = EGL_NONE;
469 wantedAttributeValue = EGL_NONE;
470 } else {
471 // if no renderable type specified, fallback to a simplified query
472 wantedAttribute = EGL_NATIVE_VISUAL_ID;
473 wantedAttributeValue = format;
474 }
475
476 err = selectConfigForAttribute(display, attribs,
477 wantedAttribute, wantedAttributeValue, config);
478 if (err == NO_ERROR) {
479 EGLint caveat;
480 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
481 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
482 }
483
484 return err;
485}
486
Steven Thomasd7f49c52017-07-26 18:48:28 -0700487EGLConfig RenderEngine::chooseEglConfig(EGLDisplay display, int format,
488 bool logConfig) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800489 status_t err;
490 EGLConfig config;
491
492 // First try to get an ES2 config
493 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
494 if (err != NO_ERROR) {
495 // If ES2 fails, try ES1
496 err = selectEGLConfig(display, format, EGL_OPENGL_ES_BIT, &config);
497 if (err != NO_ERROR) {
498 // still didn't work, probably because we're on the emulator...
499 // try a simplified query
500 ALOGW("no suitable EGLConfig found, trying a simpler query");
501 err = selectEGLConfig(display, format, 0, &config);
502 if (err != NO_ERROR) {
503 // this EGL is too lame for android
504 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
505 }
506 }
507 }
508
Steven Thomasd7f49c52017-07-26 18:48:28 -0700509 if (logConfig) {
510 // print some debugging info
511 EGLint r,g,b,a;
512 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
513 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
514 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
515 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
516 ALOGI("EGL information:");
517 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
518 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
519 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
520 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
521 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
522 }
Jesse Hall05f8c702013-12-23 20:44:38 -0800523
524 return config;
525}
526
Dan Stoza4e637772016-07-28 13:31:51 -0700527
528void RenderEngine::primeCache() const {
529 // Getting the ProgramCache instance causes it to prime its shader cache,
530 // which is performed in its constructor
531 ProgramCache::getInstance();
532}
533
Jesse Hall05f8c702013-12-23 20:44:38 -0800534// ---------------------------------------------------------------------------
Mathias Agopian875d8e12013-06-07 15:35:48 -0700535}; // namespace android
536// ---------------------------------------------------------------------------