blob: 883ae26fe31eaa4aac38612be091138f71344c18 [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
Mathias Agopian3f844832013-08-07 21:24:32 -070021#include "GLES20RenderEngine.h"
Mathias Agopian875d8e12013-06-07 15:35:48 -070022#include "GLExtensions.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070023#include "Mesh.h"
Chia-I Wub027f802017-11-29 14:00:52 -080024#include "RenderEngine.h"
Mathias Agopian875d8e12013-06-07 15:35:48 -070025
Fabien Sanglardc93afd52017-03-13 13:02:42 -070026#include <SurfaceFlinger.h>
Chia-I Wub027f802017-11-29 14:00:52 -080027#include <vector>
Fabien Sanglardc93afd52017-03-13 13:02:42 -070028
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) {
Chia-I Wub027f802017-11-29 14:00:52 -080036 if (!exts) return false;
Jesse Hall19e87292013-12-23 21:02:15 -080037 size_t len = strlen(name);
Jesse Hall05f8c702013-12-23 20:44:38 -080038
Jesse Hall19e87292013-12-23 21:02:15 -080039 const char* pos = exts;
40 while ((pos = strstr(pos, name)) != NULL) {
Chia-I Wub027f802017-11-29 14:00:52 -080041 if (pos[len] == '\0' || pos[len] == ' ') return true;
Jesse Hall19e87292013-12-23 21:02:15 -080042 pos += len;
Mathias Agopian2185f8b2013-09-18 16:20:26 -070043 }
44
Jesse Hall19e87292013-12-23 21:02:15 -080045 return false;
46}
47
Chia-I Wud4d9c6f2017-11-09 16:55:31 -080048std::unique_ptr<RenderEngine> RenderEngine::create(int hwcFormat, uint32_t featureFlags) {
49 // initialize EGL for the default display
50 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
51 if (!eglInitialize(display, NULL, NULL)) {
52 LOG_ALWAYS_FATAL("failed to initialize EGL");
53 }
54
Jesse Hall19e87292013-12-23 21:02:15 -080055 // EGL_ANDROIDX_no_config_context is an experimental extension with no
56 // written specification. It will be replaced by something more formal.
57 // SurfaceFlinger is using it to allow a single EGLContext to render to
58 // both a 16-bit primary display framebuffer and a 32-bit virtual display
59 // framebuffer.
60 //
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -060061 // EGL_KHR_no_config_context is official extension to allow creating a
62 // context that works with any surface of a display.
63 //
Jesse Hall19e87292013-12-23 21:02:15 -080064 // The code assumes that ES2 or later is available if this extension is
65 // supported.
66 EGLConfig config = EGL_NO_CONFIG;
Courtney Goeltzenleuchter0ebaac32017-04-13 12:17:03 -060067 if (!findExtension(eglQueryStringImplementationANDROID(display, EGL_EXTENSIONS),
68 "EGL_ANDROIDX_no_config_context") &&
69 !findExtension(eglQueryStringImplementationANDROID(display, EGL_EXTENSIONS),
70 "EGL_KHR_no_config_context")) {
Steven Thomasd7f49c52017-07-26 18:48:28 -070071 config = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
Jesse Hall19e87292013-12-23 21:02:15 -080072 }
73
74 EGLint renderableType = 0;
75 if (config == EGL_NO_CONFIG) {
76 renderableType = EGL_OPENGL_ES2_BIT;
Chia-I Wub027f802017-11-29 14:00:52 -080077 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
Jesse Hall19e87292013-12-23 21:02:15 -080078 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
79 }
80 EGLint contextClientVersion = 0;
Mathias Agopian2185f8b2013-09-18 16:20:26 -070081 if (renderableType & EGL_OPENGL_ES2_BIT) {
82 contextClientVersion = 2;
83 } else if (renderableType & EGL_OPENGL_ES_BIT) {
84 contextClientVersion = 1;
85 } else {
86 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
87 }
88
Fabien Sanglardc93afd52017-03-13 13:02:42 -070089 std::vector<EGLint> contextAttributes;
90 contextAttributes.reserve(6);
91 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
92 contextAttributes.push_back(contextClientVersion);
Mathias Agopian875d8e12013-06-07 15:35:48 -070093#ifdef EGL_IMG_context_priority
Fabien Sanglardc93afd52017-03-13 13:02:42 -070094 if (SurfaceFlinger::useContextPriority) {
95 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
96 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
97 }
Mathias Agopian875d8e12013-06-07 15:35:48 -070098#endif
Fabien Sanglardc93afd52017-03-13 13:02:42 -070099 contextAttributes.push_back(EGL_NONE);
100 contextAttributes.push_back(EGL_NONE);
101
Chia-I Wub027f802017-11-29 14:00:52 -0800102 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes.data());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700103
104 // if can't create a GL context, we can only abort.
Chia-I Wub027f802017-11-29 14:00:52 -0800105 LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
Mathias Agopian875d8e12013-06-07 15:35:48 -0700106
107 // now figure out what version of GL did we actually get
108 // NOTE: a dummy surface is not needed if KHR_create_context is supported
109
Jesse Hall19e87292013-12-23 21:02:15 -0800110 EGLConfig dummyConfig = config;
111 if (dummyConfig == EGL_NO_CONFIG) {
Steven Thomasd7f49c52017-07-26 18:48:28 -0700112 dummyConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
Jesse Hall19e87292013-12-23 21:02:15 -0800113 }
Chia-I Wub027f802017-11-29 14:00:52 -0800114 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE, EGL_NONE};
Jesse Hall19e87292013-12-23 21:02:15 -0800115 EGLSurface dummy = eglCreatePbufferSurface(display, dummyConfig, attribs);
Chia-I Wub027f802017-11-29 14:00:52 -0800116 LOG_ALWAYS_FATAL_IF(dummy == EGL_NO_SURFACE, "can't create dummy pbuffer");
Mathias Agopian875d8e12013-06-07 15:35:48 -0700117 EGLBoolean success = eglMakeCurrent(display, dummy, dummy, ctxt);
118 LOG_ALWAYS_FATAL_IF(!success, "can't make dummy pbuffer current");
119
120 GLExtensions& extensions(GLExtensions::getInstance());
Chia-I Wub027f802017-11-29 14:00:52 -0800121 extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER),
122 glGetString(GL_VERSION), glGetString(GL_EXTENSIONS));
Mathias Agopian875d8e12013-06-07 15:35:48 -0700123
Chia-I Wub027f802017-11-29 14:00:52 -0800124 GlesVersion version = parseGlesVersion(extensions.getVersion());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700125
126 // initialize the renderer while GL is current
127
Chia-I Wub2c76242017-11-09 17:17:07 -0800128 std::unique_ptr<RenderEngine> engine;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700129 switch (version) {
Chia-I Wub027f802017-11-29 14:00:52 -0800130 case GLES_VERSION_1_0:
131 case GLES_VERSION_1_1:
132 LOG_ALWAYS_FATAL("SurfaceFlinger requires OpenGL ES 2.0 minimum to run.");
133 break;
134 case GLES_VERSION_2_0:
135 case GLES_VERSION_3_0:
136 engine = std::make_unique<GLES20RenderEngine>(featureFlags);
137 break;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700138 }
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800139 engine->setEGLHandles(display, config, ctxt);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700140
141 ALOGI("OpenGL ES informations:");
142 ALOGI("vendor : %s", extensions.getVendor());
143 ALOGI("renderer : %s", extensions.getRenderer());
144 ALOGI("version : %s", extensions.getVersion());
145 ALOGI("extensions: %s", extensions.getExtension());
Michael Lentine9ae79d82014-07-30 16:42:12 -0700146 ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
147 ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700148
149 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
150 eglDestroySurface(display, dummy);
151
152 return engine;
153}
154
Chia-I Wub027f802017-11-29 14:00:52 -0800155RenderEngine::RenderEngine()
156 : mEGLDisplay(EGL_NO_DISPLAY), mEGLConfig(NULL), mEGLContext(EGL_NO_CONTEXT) {}
Mathias Agopian875d8e12013-06-07 15:35:48 -0700157
158RenderEngine::~RenderEngine() {
Chia-I Wub01450b2017-11-09 16:55:31 -0800159 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
160 eglTerminate(mEGLDisplay);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700161}
162
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800163void RenderEngine::setEGLHandles(EGLDisplay display, EGLConfig config, EGLContext ctxt) {
164 mEGLDisplay = display;
Jesse Hall05f8c702013-12-23 20:44:38 -0800165 mEGLConfig = config;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700166 mEGLContext = ctxt;
167}
168
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800169EGLDisplay RenderEngine::getEGLDisplay() const {
170 return mEGLDisplay;
171}
172
173EGLConfig RenderEngine::getEGLConfig() const {
Jesse Hall05f8c702013-12-23 20:44:38 -0800174 return mEGLConfig;
175}
176
Chia-I Wuf846a352017-11-10 09:22:52 -0800177bool RenderEngine::setCurrentSurface(const RE::Surface& surface) {
178 bool success = true;
179 EGLSurface eglSurface = surface.getEGLSurface();
180 if (eglSurface != eglGetCurrentSurface(EGL_DRAW)) {
181 success = eglMakeCurrent(mEGLDisplay, eglSurface, eglSurface, mEGLContext) == EGL_TRUE;
182 if (success && surface.getAsync()) {
183 eglSwapInterval(mEGLDisplay, 0);
184 }
185 }
186
187 return success;
Chia-I Wu7f402902017-11-09 12:51:10 -0800188}
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();
Chia-I Wub027f802017-11-29 14:00:52 -0800198 if (error == GL_NO_ERROR) break;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700199 ALOGE("GL error 0x%04x", int(error));
200 } while (true);
201}
202
203RenderEngine::GlesVersion RenderEngine::parseGlesVersion(const char* str) {
204 int major, minor;
205 if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) {
206 if (sscanf(str, "OpenGL ES %d.%d", &major, &minor) != 2) {
207 ALOGW("Unable to parse GL_VERSION string: \"%s\"", str);
208 return GLES_VERSION_1_0;
209 }
210 }
211
212 if (major == 1 && minor == 0) return GLES_VERSION_1_0;
213 if (major == 1 && minor >= 1) return GLES_VERSION_1_1;
214 if (major == 2 && minor >= 0) return GLES_VERSION_2_0;
215 if (major == 3 && minor >= 0) return GLES_VERSION_3_0;
216
217 ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor);
218 return GLES_VERSION_1_0;
219}
220
Chia-I Wub027f802017-11-29 14:00:52 -0800221void RenderEngine::fillRegionWithColor(const Region& region, uint32_t height, float red,
222 float green, float blue, float alpha) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700223 size_t c;
224 Rect const* r = region.getArray(&c);
Chia-I Wub027f802017-11-29 14:00:52 -0800225 Mesh mesh(Mesh::TRIANGLES, c * 6, 2);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700226 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Chia-I Wub027f802017-11-29 14:00:52 -0800227 for (size_t i = 0; i < c; i++, r++) {
228 position[i * 6 + 0].x = r->left;
229 position[i * 6 + 0].y = height - r->top;
230 position[i * 6 + 1].x = r->left;
231 position[i * 6 + 1].y = height - r->bottom;
232 position[i * 6 + 2].x = r->right;
233 position[i * 6 + 2].y = height - r->bottom;
234 position[i * 6 + 3].x = r->left;
235 position[i * 6 + 3].y = height - r->top;
236 position[i * 6 + 4].x = r->right;
237 position[i * 6 + 4].y = height - r->bottom;
238 position[i * 6 + 5].x = r->right;
239 position[i * 6 + 5].y = height - r->top;
Mathias Agopian3f844832013-08-07 21:24:32 -0700240 }
Mathias Agopian19733a32013-08-28 18:13:56 -0700241 setupFillWithColor(red, green, blue, alpha);
242 drawMesh(mesh);
Mathias Agopian3f844832013-08-07 21:24:32 -0700243}
244
Chia-I Wub0c041b2017-11-09 11:36:33 -0800245int RenderEngine::flush(bool wait) {
246 // Attempt to create a sync khr object that can produce a sync point. If that
247 // isn't available, create a non-dupable sync object in the fallback path and
248 // wait on it directly.
249 EGLSyncKHR sync;
250 if (!wait) {
251 EGLint syncFd = EGL_NO_NATIVE_FENCE_FD_ANDROID;
252
253 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
254 if (sync != EGL_NO_SYNC_KHR) {
255 // native fence fd will not be populated until flush() is done.
256 glFlush();
257
258 // get the sync fd
259 syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync);
260 if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
261 ALOGW("failed to dup sync khr object");
262 }
263
264 eglDestroySyncKHR(mEGLDisplay, sync);
265 }
266
267 if (syncFd != EGL_NO_NATIVE_FENCE_FD_ANDROID) {
268 return syncFd;
269 }
270 }
271
272 // fallback or explicit wait
273 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
274 if (sync != EGL_NO_SYNC_KHR) {
Chia-I Wub027f802017-11-29 14:00:52 -0800275 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
276 2000000000 /*2 sec*/);
Chia-I Wub0c041b2017-11-09 11:36:33 -0800277 EGLint eglErr = eglGetError();
278 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
279 ALOGW("fence wait timed out");
280 } else {
Chia-I Wub027f802017-11-29 14:00:52 -0800281 ALOGW_IF(eglErr != EGL_SUCCESS, "error waiting on EGL fence: %#x", eglErr);
Chia-I Wub0c041b2017-11-09 11:36:33 -0800282 }
283 eglDestroySyncKHR(mEGLDisplay, sync);
284 } else {
285 ALOGW("error creating EGL fence: %#x", eglGetError());
286 }
287
288 return -1;
Riley Andrews9707f4d2014-10-23 16:17:04 -0700289}
290
Mathias Agopian3f844832013-08-07 21:24:32 -0700291void RenderEngine::clearWithColor(float red, float green, float blue, float alpha) {
292 glClearColor(red, green, blue, alpha);
293 glClear(GL_COLOR_BUFFER_BIT);
294}
295
Chia-I Wub027f802017-11-29 14:00:52 -0800296void RenderEngine::setScissor(uint32_t left, uint32_t bottom, uint32_t right, uint32_t top) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700297 glScissor(left, bottom, right, top);
298 glEnable(GL_SCISSOR_TEST);
299}
300
301void RenderEngine::disableScissor() {
302 glDisable(GL_SCISSOR_TEST);
303}
304
305void RenderEngine::genTextures(size_t count, uint32_t* names) {
306 glGenTextures(count, names);
307}
308
309void RenderEngine::deleteTextures(size_t count, uint32_t const* names) {
310 glDeleteTextures(count, names);
311}
312
Mathias Agopiand5556842013-09-19 17:08:37 -0700313void RenderEngine::readPixels(size_t l, size_t b, size_t w, size_t h, uint32_t* pixels) {
314 glReadPixels(l, b, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
315}
316
Mathias Agopian458197d2013-08-15 14:56:51 -0700317void RenderEngine::dump(String8& result) {
Chia-I Wu8601f882017-11-09 16:52:21 -0800318 result.appendFormat("EGL implementation : %s\n",
Chia-I Wub027f802017-11-29 14:00:52 -0800319 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
320 result.appendFormat("%s\n", eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Chia-I Wu8601f882017-11-09 16:52:21 -0800321
Mathias Agopian458197d2013-08-15 14:56:51 -0700322 const GLExtensions& extensions(GLExtensions::getInstance());
Chia-I Wub027f802017-11-29 14:00:52 -0800323 result.appendFormat("GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
324 extensions.getVersion());
Mathias Agopian458197d2013-08-15 14:56:51 -0700325 result.appendFormat("%s\n", extensions.getExtension());
326}
327
Mathias Agopian3f844832013-08-07 21:24:32 -0700328// ---------------------------------------------------------------------------
329
Chia-I Wueadbaa62017-11-09 11:26:15 -0800330RenderEngine::BindNativeBufferAsFramebuffer::BindNativeBufferAsFramebuffer(
Chia-I Wub027f802017-11-29 14:00:52 -0800331 RenderEngine& engine, ANativeWindowBuffer* buffer)
332 : mEngine(engine) {
333 mImage = eglCreateImageKHR(mEngine.mEGLDisplay, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
334 buffer, NULL);
Chia-I Wueadbaa62017-11-09 11:26:15 -0800335 if (mImage == EGL_NO_IMAGE_KHR) {
336 mStatus = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
337 return;
338 }
339
340 mEngine.bindImageAsFramebuffer(mImage, &mTexName, &mFbName, &mStatus);
Mathias Agopian458197d2013-08-15 14:56:51 -0700341
Chia-I Wub027f802017-11-29 14:00:52 -0800342 ALOGE_IF(mStatus != GL_FRAMEBUFFER_COMPLETE_OES, "glCheckFramebufferStatusOES error %d",
343 mStatus);
Mathias Agopian3f844832013-08-07 21:24:32 -0700344}
345
Chia-I Wueadbaa62017-11-09 11:26:15 -0800346RenderEngine::BindNativeBufferAsFramebuffer::~BindNativeBufferAsFramebuffer() {
347 if (mImage == EGL_NO_IMAGE_KHR) {
348 return;
349 }
350
Mathias Agopian3f844832013-08-07 21:24:32 -0700351 // back to main framebuffer
Mathias Agopian458197d2013-08-15 14:56:51 -0700352 mEngine.unbindFramebuffer(mTexName, mFbName);
Chia-I Wueadbaa62017-11-09 11:26:15 -0800353 eglDestroyImageKHR(mEngine.mEGLDisplay, mImage);
Mathias Agopian3f844832013-08-07 21:24:32 -0700354}
355
Chia-I Wueadbaa62017-11-09 11:26:15 -0800356status_t RenderEngine::BindNativeBufferAsFramebuffer::getStatus() const {
Mathias Agopian3f844832013-08-07 21:24:32 -0700357 return mStatus == GL_FRAMEBUFFER_COMPLETE_OES ? NO_ERROR : BAD_VALUE;
358}
359
Mathias Agopian875d8e12013-06-07 15:35:48 -0700360// ---------------------------------------------------------------------------
Jesse Hall05f8c702013-12-23 20:44:38 -0800361
Chia-I Wub027f802017-11-29 14:00:52 -0800362static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute,
363 EGLint wanted, EGLConfig* outConfig) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800364 EGLint numConfigs = -1, n = 0;
365 eglGetConfigs(dpy, NULL, 0, &numConfigs);
366 EGLConfig* const configs = new EGLConfig[numConfigs];
367 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
368
369 if (n) {
370 if (attribute != EGL_NONE) {
Chia-I Wub027f802017-11-29 14:00:52 -0800371 for (int i = 0; i < n; i++) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800372 EGLint value = 0;
373 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
374 if (wanted == value) {
375 *outConfig = configs[i];
Chia-I Wub027f802017-11-29 14:00:52 -0800376 delete[] configs;
Jesse Hall05f8c702013-12-23 20:44:38 -0800377 return NO_ERROR;
378 }
379 }
380 } else {
381 // just pick the first one
382 *outConfig = configs[0];
Chia-I Wub027f802017-11-29 14:00:52 -0800383 delete[] configs;
Jesse Hall05f8c702013-12-23 20:44:38 -0800384 return NO_ERROR;
385 }
386 }
Chia-I Wub027f802017-11-29 14:00:52 -0800387 delete[] configs;
Jesse Hall05f8c702013-12-23 20:44:38 -0800388 return NAME_NOT_FOUND;
389}
390
391class EGLAttributeVector {
392 struct Attribute;
393 class Adder;
394 friend class Adder;
395 KeyedVector<Attribute, EGLint> mList;
396 struct Attribute {
Chia-I Wub027f802017-11-29 14:00:52 -0800397 Attribute() : v(0){};
398 explicit Attribute(EGLint v) : v(v) {}
Jesse Hall05f8c702013-12-23 20:44:38 -0800399 EGLint v;
Chia-I Wub027f802017-11-29 14:00:52 -0800400 bool operator<(const Attribute& other) const {
Jesse Hall05f8c702013-12-23 20:44:38 -0800401 // this places EGL_NONE at the end
402 EGLint lhs(v);
403 EGLint rhs(other.v);
404 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
405 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
406 return lhs < rhs;
407 }
408 };
409 class Adder {
410 friend class EGLAttributeVector;
411 EGLAttributeVector& v;
412 EGLint attribute;
Chia-I Wub027f802017-11-29 14:00:52 -0800413 Adder(EGLAttributeVector& v, EGLint attribute) : v(v), attribute(attribute) {}
414
Jesse Hall05f8c702013-12-23 20:44:38 -0800415 public:
Chia-I Wub027f802017-11-29 14:00:52 -0800416 void operator=(EGLint value) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800417 if (attribute != EGL_NONE) {
Chih-Hung Hsiehc4067912016-05-03 14:03:27 -0700418 v.mList.add(Attribute(attribute), value);
Jesse Hall05f8c702013-12-23 20:44:38 -0800419 }
420 }
Chia-I Wub027f802017-11-29 14:00:52 -0800421 operator EGLint() const { return v.mList[attribute]; }
Jesse Hall05f8c702013-12-23 20:44:38 -0800422 };
Chia-I Wub027f802017-11-29 14:00:52 -0800423
Jesse Hall05f8c702013-12-23 20:44:38 -0800424public:
Chia-I Wub027f802017-11-29 14:00:52 -0800425 EGLAttributeVector() { mList.add(Attribute(EGL_NONE), EGL_NONE); }
Jesse Hall05f8c702013-12-23 20:44:38 -0800426 void remove(EGLint attribute) {
427 if (attribute != EGL_NONE) {
Chih-Hung Hsiehc4067912016-05-03 14:03:27 -0700428 mList.removeItem(Attribute(attribute));
Jesse Hall05f8c702013-12-23 20:44:38 -0800429 }
430 }
Chia-I Wub027f802017-11-29 14:00:52 -0800431 Adder operator[](EGLint attribute) { return Adder(*this, attribute); }
432 EGLint operator[](EGLint attribute) const { return mList[attribute]; }
Jesse Hall05f8c702013-12-23 20:44:38 -0800433 // cast-operator to (EGLint const*)
Chia-I Wub027f802017-11-29 14:00:52 -0800434 operator EGLint const*() const { return &mList.keyAt(0).v; }
Jesse Hall05f8c702013-12-23 20:44:38 -0800435};
436
Chia-I Wub027f802017-11-29 14:00:52 -0800437static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType,
438 EGLConfig* config) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800439 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
440 // it is to be used with WIFI displays
441 status_t err;
442 EGLint wantedAttribute;
443 EGLint wantedAttributeValue;
444
445 EGLAttributeVector attribs;
446 if (renderableType) {
Chia-I Wub027f802017-11-29 14:00:52 -0800447 attribs[EGL_RENDERABLE_TYPE] = renderableType;
448 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
449 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
Jesse Hall05f8c702013-12-23 20:44:38 -0800450 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
Chia-I Wub027f802017-11-29 14:00:52 -0800451 attribs[EGL_RED_SIZE] = 8;
452 attribs[EGL_GREEN_SIZE] = 8;
453 attribs[EGL_BLUE_SIZE] = 8;
454 attribs[EGL_ALPHA_SIZE] = 8;
455 wantedAttribute = EGL_NONE;
456 wantedAttributeValue = EGL_NONE;
Jesse Hall05f8c702013-12-23 20:44:38 -0800457 } else {
458 // if no renderable type specified, fallback to a simplified query
Chia-I Wub027f802017-11-29 14:00:52 -0800459 wantedAttribute = EGL_NATIVE_VISUAL_ID;
460 wantedAttributeValue = format;
Jesse Hall05f8c702013-12-23 20:44:38 -0800461 }
462
Chia-I Wub027f802017-11-29 14:00:52 -0800463 err = selectConfigForAttribute(display, attribs, wantedAttribute, wantedAttributeValue, config);
Jesse Hall05f8c702013-12-23 20:44:38 -0800464 if (err == NO_ERROR) {
465 EGLint caveat;
466 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
467 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
468 }
469
470 return err;
471}
472
Chia-I Wub027f802017-11-29 14:00:52 -0800473EGLConfig RenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800474 status_t err;
475 EGLConfig config;
476
477 // First try to get an ES2 config
478 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
479 if (err != NO_ERROR) {
480 // If ES2 fails, try ES1
481 err = selectEGLConfig(display, format, EGL_OPENGL_ES_BIT, &config);
482 if (err != NO_ERROR) {
483 // still didn't work, probably because we're on the emulator...
484 // try a simplified query
485 ALOGW("no suitable EGLConfig found, trying a simpler query");
486 err = selectEGLConfig(display, format, 0, &config);
487 if (err != NO_ERROR) {
488 // this EGL is too lame for android
489 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
490 }
491 }
492 }
493
Steven Thomasd7f49c52017-07-26 18:48:28 -0700494 if (logConfig) {
495 // print some debugging info
Chia-I Wub027f802017-11-29 14:00:52 -0800496 EGLint r, g, b, a;
497 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
Steven Thomasd7f49c52017-07-26 18:48:28 -0700498 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
Chia-I Wub027f802017-11-29 14:00:52 -0800499 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
Steven Thomasd7f49c52017-07-26 18:48:28 -0700500 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
501 ALOGI("EGL information:");
502 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
503 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
504 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
Chia-I Wub027f802017-11-29 14:00:52 -0800505 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
Steven Thomasd7f49c52017-07-26 18:48:28 -0700506 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
507 }
Jesse Hall05f8c702013-12-23 20:44:38 -0800508
509 return config;
510}
511
Dan Stoza4e637772016-07-28 13:31:51 -0700512void RenderEngine::primeCache() const {
513 // Getting the ProgramCache instance causes it to prime its shader cache,
514 // which is performed in its constructor
515 ProgramCache::getInstance();
516}
517
Jesse Hall05f8c702013-12-23 20:44:38 -0800518// ---------------------------------------------------------------------------
Mathias Agopian875d8e12013-06-07 15:35:48 -0700519}; // namespace android
520// ---------------------------------------------------------------------------