blob: 179b79003c69d9199d5397202475deb5b2b28a59 [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"
Chia-I Wu401ef832017-12-01 10:52:22 -080023#include "Image.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070024#include "Mesh.h"
Chia-I Wub027f802017-11-29 14:00:52 -080025#include "RenderEngine.h"
Mathias Agopian875d8e12013-06-07 15:35:48 -070026
Fabien Sanglardc93afd52017-03-13 13:02:42 -070027#include <SurfaceFlinger.h>
Chia-I Wub027f802017-11-29 14:00:52 -080028#include <vector>
Fabien Sanglardc93afd52017-03-13 13:02:42 -070029
Jiyong Park00b15b82017-08-10 20:30:56 +090030extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
Jesse Hall19e87292013-12-23 21:02:15 -080031
Mathias Agopian875d8e12013-06-07 15:35:48 -070032// ---------------------------------------------------------------------------
33namespace android {
34// ---------------------------------------------------------------------------
35
Chia-I Wud4d9c6f2017-11-09 16:55:31 -080036std::unique_ptr<RenderEngine> RenderEngine::create(int hwcFormat, uint32_t featureFlags) {
37 // initialize EGL for the default display
38 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Peiyong Lin566a3b42018-01-09 18:22:43 -080039 if (!eglInitialize(display, nullptr, nullptr)) {
Chia-I Wud4d9c6f2017-11-09 16:55:31 -080040 LOG_ALWAYS_FATAL("failed to initialize EGL");
41 }
42
Peiyong Lin566a3b42018-01-09 18:22:43 -080043 GLExtensions& extensions = GLExtensions::getInstance();
Chia-I Wu767d7c92017-11-30 13:22:48 -080044 extensions.initWithEGLStrings(eglQueryStringImplementationANDROID(display, EGL_VERSION),
45 eglQueryStringImplementationANDROID(display, EGL_EXTENSIONS));
46
Jesse Hall19e87292013-12-23 21:02:15 -080047 // The code assumes that ES2 or later is available if this extension is
48 // supported.
49 EGLConfig config = EGL_NO_CONFIG;
Chia-I Wu767d7c92017-11-30 13:22:48 -080050 if (!extensions.hasNoConfigContext()) {
Steven Thomasd7f49c52017-07-26 18:48:28 -070051 config = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
Jesse Hall19e87292013-12-23 21:02:15 -080052 }
53
54 EGLint renderableType = 0;
55 if (config == EGL_NO_CONFIG) {
56 renderableType = EGL_OPENGL_ES2_BIT;
Chia-I Wub027f802017-11-29 14:00:52 -080057 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
Jesse Hall19e87292013-12-23 21:02:15 -080058 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
59 }
60 EGLint contextClientVersion = 0;
Mathias Agopian2185f8b2013-09-18 16:20:26 -070061 if (renderableType & EGL_OPENGL_ES2_BIT) {
62 contextClientVersion = 2;
63 } else if (renderableType & EGL_OPENGL_ES_BIT) {
64 contextClientVersion = 1;
65 } else {
66 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
67 }
68
Fabien Sanglardc93afd52017-03-13 13:02:42 -070069 std::vector<EGLint> contextAttributes;
70 contextAttributes.reserve(6);
71 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
72 contextAttributes.push_back(contextClientVersion);
Mathias Agopian875d8e12013-06-07 15:35:48 -070073#ifdef EGL_IMG_context_priority
Fabien Sanglardc93afd52017-03-13 13:02:42 -070074 if (SurfaceFlinger::useContextPriority) {
75 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
76 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
77 }
Mathias Agopian875d8e12013-06-07 15:35:48 -070078#endif
Fabien Sanglardc93afd52017-03-13 13:02:42 -070079 contextAttributes.push_back(EGL_NONE);
80 contextAttributes.push_back(EGL_NONE);
81
Peiyong Lin566a3b42018-01-09 18:22:43 -080082 EGLContext ctxt = eglCreateContext(display, config, nullptr, contextAttributes.data());
Mathias Agopian875d8e12013-06-07 15:35:48 -070083
84 // if can't create a GL context, we can only abort.
Chia-I Wub027f802017-11-29 14:00:52 -080085 LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
Mathias Agopian875d8e12013-06-07 15:35:48 -070086
87 // now figure out what version of GL did we actually get
88 // NOTE: a dummy surface is not needed if KHR_create_context is supported
89
Jesse Hall19e87292013-12-23 21:02:15 -080090 EGLConfig dummyConfig = config;
91 if (dummyConfig == EGL_NO_CONFIG) {
Steven Thomasd7f49c52017-07-26 18:48:28 -070092 dummyConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
Jesse Hall19e87292013-12-23 21:02:15 -080093 }
Chia-I Wub027f802017-11-29 14:00:52 -080094 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE, EGL_NONE};
Jesse Hall19e87292013-12-23 21:02:15 -080095 EGLSurface dummy = eglCreatePbufferSurface(display, dummyConfig, attribs);
Chia-I Wub027f802017-11-29 14:00:52 -080096 LOG_ALWAYS_FATAL_IF(dummy == EGL_NO_SURFACE, "can't create dummy pbuffer");
Mathias Agopian875d8e12013-06-07 15:35:48 -070097 EGLBoolean success = eglMakeCurrent(display, dummy, dummy, ctxt);
98 LOG_ALWAYS_FATAL_IF(!success, "can't make dummy pbuffer current");
99
Chia-I Wub027f802017-11-29 14:00:52 -0800100 extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER),
101 glGetString(GL_VERSION), glGetString(GL_EXTENSIONS));
Mathias Agopian875d8e12013-06-07 15:35:48 -0700102
Chia-I Wub027f802017-11-29 14:00:52 -0800103 GlesVersion version = parseGlesVersion(extensions.getVersion());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700104
105 // initialize the renderer while GL is current
106
Chia-I Wub2c76242017-11-09 17:17:07 -0800107 std::unique_ptr<RenderEngine> engine;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700108 switch (version) {
Chia-I Wub027f802017-11-29 14:00:52 -0800109 case GLES_VERSION_1_0:
110 case GLES_VERSION_1_1:
111 LOG_ALWAYS_FATAL("SurfaceFlinger requires OpenGL ES 2.0 minimum to run.");
112 break;
113 case GLES_VERSION_2_0:
114 case GLES_VERSION_3_0:
115 engine = std::make_unique<GLES20RenderEngine>(featureFlags);
116 break;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700117 }
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800118 engine->setEGLHandles(display, config, ctxt);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700119
120 ALOGI("OpenGL ES informations:");
121 ALOGI("vendor : %s", extensions.getVendor());
122 ALOGI("renderer : %s", extensions.getRenderer());
123 ALOGI("version : %s", extensions.getVersion());
Chia-I Wu767d7c92017-11-30 13:22:48 -0800124 ALOGI("extensions: %s", extensions.getExtensions());
Michael Lentine9ae79d82014-07-30 16:42:12 -0700125 ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
126 ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700127
128 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
129 eglDestroySurface(display, dummy);
130
131 return engine;
132}
133
Chia-I Wub027f802017-11-29 14:00:52 -0800134RenderEngine::RenderEngine()
Peiyong Lin566a3b42018-01-09 18:22:43 -0800135 : mEGLDisplay(EGL_NO_DISPLAY), mEGLConfig(nullptr), mEGLContext(EGL_NO_CONTEXT) {}
Mathias Agopian875d8e12013-06-07 15:35:48 -0700136
137RenderEngine::~RenderEngine() {
Chia-I Wub01450b2017-11-09 16:55:31 -0800138 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
139 eglTerminate(mEGLDisplay);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700140}
141
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800142void RenderEngine::setEGLHandles(EGLDisplay display, EGLConfig config, EGLContext ctxt) {
143 mEGLDisplay = display;
Jesse Hall05f8c702013-12-23 20:44:38 -0800144 mEGLConfig = config;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700145 mEGLContext = ctxt;
146}
147
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800148EGLDisplay RenderEngine::getEGLDisplay() const {
149 return mEGLDisplay;
150}
151
152EGLConfig RenderEngine::getEGLConfig() const {
Jesse Hall05f8c702013-12-23 20:44:38 -0800153 return mEGLConfig;
154}
155
Chia-I Wu401ef832017-12-01 10:52:22 -0800156bool RenderEngine::supportsImageCrop() const {
157 return GLExtensions::getInstance().hasImageCrop();
158}
159
Chia-I Wu9f2db772017-11-30 21:06:50 -0800160bool RenderEngine::isCurrent() const {
161 return mEGLDisplay == eglGetCurrentDisplay() && mEGLContext == eglGetCurrentContext();
162}
163
Chia-I Wuf846a352017-11-10 09:22:52 -0800164bool RenderEngine::setCurrentSurface(const RE::Surface& surface) {
165 bool success = true;
166 EGLSurface eglSurface = surface.getEGLSurface();
167 if (eglSurface != eglGetCurrentSurface(EGL_DRAW)) {
168 success = eglMakeCurrent(mEGLDisplay, eglSurface, eglSurface, mEGLContext) == EGL_TRUE;
169 if (success && surface.getAsync()) {
170 eglSwapInterval(mEGLDisplay, 0);
171 }
172 }
173
174 return success;
Chia-I Wu7f402902017-11-09 12:51:10 -0800175}
176
177void RenderEngine::resetCurrentSurface() {
178 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
179}
180
Chia-I Wu767fcf72017-11-30 22:07:38 -0800181base::unique_fd RenderEngine::flush() {
182 if (!GLExtensions::getInstance().hasNativeFenceSync()) {
183 return base::unique_fd();
184 }
185
Peiyong Lin566a3b42018-01-09 18:22:43 -0800186 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
Chia-I Wu767fcf72017-11-30 22:07:38 -0800187 if (sync == EGL_NO_SYNC_KHR) {
188 ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
189 return base::unique_fd();
190 }
191
192 // native fence fd will not be populated until flush() is done.
193 glFlush();
194
195 // get the fence fd
196 base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
197 eglDestroySyncKHR(mEGLDisplay, sync);
198 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
199 ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
200 }
201
202 return fenceFd;
203}
204
205bool RenderEngine::finish() {
206 if (!GLExtensions::getInstance().hasFenceSync()) {
207 ALOGW("no synchronization support");
208 return false;
209 }
210
Peiyong Lin566a3b42018-01-09 18:22:43 -0800211 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, nullptr);
Chia-I Wu767fcf72017-11-30 22:07:38 -0800212 if (sync == EGL_NO_SYNC_KHR) {
213 ALOGW("failed to create EGL fence sync: %#x", eglGetError());
214 return false;
215 }
216
217 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
218 2000000000 /*2 sec*/);
219 EGLint error = eglGetError();
220 eglDestroySyncKHR(mEGLDisplay, sync);
221 if (result != EGL_CONDITION_SATISFIED_KHR) {
222 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
223 ALOGW("fence wait timed out");
224 } else {
225 ALOGW("error waiting on EGL fence: %#x", error);
226 }
227 return false;
228 }
229
230 return true;
231}
232
233bool RenderEngine::waitFence(base::unique_fd fenceFd) {
234 if (!GLExtensions::getInstance().hasNativeFenceSync() ||
235 !GLExtensions::getInstance().hasWaitSync()) {
236 return false;
237 }
238
239 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE};
240 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
241 if (sync == EGL_NO_SYNC_KHR) {
242 ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
243 return false;
244 }
245
246 // fenceFd is now owned by EGLSync
247 (void)fenceFd.release();
248
249 // XXX: The spec draft is inconsistent as to whether this should return an
250 // EGLint or void. Ignore the return value for now, as it's not strictly
251 // needed.
252 eglWaitSyncKHR(mEGLDisplay, sync, 0);
253 EGLint error = eglGetError();
254 eglDestroySyncKHR(mEGLDisplay, sync);
255 if (error != EGL_SUCCESS) {
256 ALOGE("failed to wait for EGL native fence sync: %#x", error);
257 return false;
258 }
259
260 return true;
261}
262
Mathias Agopian875d8e12013-06-07 15:35:48 -0700263void RenderEngine::checkErrors() const {
264 do {
265 // there could be more than one error flag
266 GLenum error = glGetError();
Chia-I Wub027f802017-11-29 14:00:52 -0800267 if (error == GL_NO_ERROR) break;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700268 ALOGE("GL error 0x%04x", int(error));
269 } while (true);
270}
271
272RenderEngine::GlesVersion RenderEngine::parseGlesVersion(const char* str) {
273 int major, minor;
274 if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) {
275 if (sscanf(str, "OpenGL ES %d.%d", &major, &minor) != 2) {
276 ALOGW("Unable to parse GL_VERSION string: \"%s\"", str);
277 return GLES_VERSION_1_0;
278 }
279 }
280
281 if (major == 1 && minor == 0) return GLES_VERSION_1_0;
282 if (major == 1 && minor >= 1) return GLES_VERSION_1_1;
283 if (major == 2 && minor >= 0) return GLES_VERSION_2_0;
284 if (major == 3 && minor >= 0) return GLES_VERSION_3_0;
285
286 ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor);
287 return GLES_VERSION_1_0;
288}
289
Chia-I Wub027f802017-11-29 14:00:52 -0800290void RenderEngine::fillRegionWithColor(const Region& region, uint32_t height, float red,
291 float green, float blue, float alpha) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700292 size_t c;
293 Rect const* r = region.getArray(&c);
Chia-I Wub027f802017-11-29 14:00:52 -0800294 Mesh mesh(Mesh::TRIANGLES, c * 6, 2);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700295 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Chia-I Wub027f802017-11-29 14:00:52 -0800296 for (size_t i = 0; i < c; i++, r++) {
297 position[i * 6 + 0].x = r->left;
298 position[i * 6 + 0].y = height - r->top;
299 position[i * 6 + 1].x = r->left;
300 position[i * 6 + 1].y = height - r->bottom;
301 position[i * 6 + 2].x = r->right;
302 position[i * 6 + 2].y = height - r->bottom;
303 position[i * 6 + 3].x = r->left;
304 position[i * 6 + 3].y = height - r->top;
305 position[i * 6 + 4].x = r->right;
306 position[i * 6 + 4].y = height - r->bottom;
307 position[i * 6 + 5].x = r->right;
308 position[i * 6 + 5].y = height - r->top;
Mathias Agopian3f844832013-08-07 21:24:32 -0700309 }
Mathias Agopian19733a32013-08-28 18:13:56 -0700310 setupFillWithColor(red, green, blue, alpha);
311 drawMesh(mesh);
Mathias Agopian3f844832013-08-07 21:24:32 -0700312}
313
314void RenderEngine::clearWithColor(float red, float green, float blue, float alpha) {
315 glClearColor(red, green, blue, alpha);
316 glClear(GL_COLOR_BUFFER_BIT);
317}
318
Chia-I Wub027f802017-11-29 14:00:52 -0800319void RenderEngine::setScissor(uint32_t left, uint32_t bottom, uint32_t right, uint32_t top) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700320 glScissor(left, bottom, right, top);
321 glEnable(GL_SCISSOR_TEST);
322}
323
324void RenderEngine::disableScissor() {
325 glDisable(GL_SCISSOR_TEST);
326}
327
328void RenderEngine::genTextures(size_t count, uint32_t* names) {
329 glGenTextures(count, names);
330}
331
332void RenderEngine::deleteTextures(size_t count, uint32_t const* names) {
333 glDeleteTextures(count, names);
334}
335
Chia-I Wu401ef832017-12-01 10:52:22 -0800336void RenderEngine::bindExternalTextureImage(uint32_t texName, const RE::Image& image) {
337 const GLenum target = GL_TEXTURE_EXTERNAL_OES;
338
339 glBindTexture(target, texName);
340 if (image.getEGLImage() != EGL_NO_IMAGE_KHR) {
341 glEGLImageTargetTexture2DOES(target, static_cast<GLeglImageOES>(image.getEGLImage()));
342 }
343}
344
Mathias Agopiand5556842013-09-19 17:08:37 -0700345void RenderEngine::readPixels(size_t l, size_t b, size_t w, size_t h, uint32_t* pixels) {
346 glReadPixels(l, b, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
347}
348
Mathias Agopian458197d2013-08-15 14:56:51 -0700349void RenderEngine::dump(String8& result) {
Peiyong Lin566a3b42018-01-09 18:22:43 -0800350 const GLExtensions& extensions = GLExtensions::getInstance();
Chia-I Wu767d7c92017-11-30 13:22:48 -0800351
352 result.appendFormat("EGL implementation : %s\n", extensions.getEGLVersion());
353 result.appendFormat("%s\n", extensions.getEGLExtensions());
354
Chia-I Wub027f802017-11-29 14:00:52 -0800355 result.appendFormat("GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
356 extensions.getVersion());
Chia-I Wu767d7c92017-11-30 13:22:48 -0800357 result.appendFormat("%s\n", extensions.getExtensions());
Mathias Agopian458197d2013-08-15 14:56:51 -0700358}
359
Mathias Agopian3f844832013-08-07 21:24:32 -0700360// ---------------------------------------------------------------------------
361
Chia-I Wueadbaa62017-11-09 11:26:15 -0800362RenderEngine::BindNativeBufferAsFramebuffer::BindNativeBufferAsFramebuffer(
Chia-I Wub027f802017-11-29 14:00:52 -0800363 RenderEngine& engine, ANativeWindowBuffer* buffer)
364 : mEngine(engine) {
365 mImage = eglCreateImageKHR(mEngine.mEGLDisplay, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
Peiyong Lin566a3b42018-01-09 18:22:43 -0800366 buffer, nullptr);
Chia-I Wueadbaa62017-11-09 11:26:15 -0800367 if (mImage == EGL_NO_IMAGE_KHR) {
368 mStatus = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
369 return;
370 }
371
372 mEngine.bindImageAsFramebuffer(mImage, &mTexName, &mFbName, &mStatus);
Mathias Agopian458197d2013-08-15 14:56:51 -0700373
Chia-I Wub027f802017-11-29 14:00:52 -0800374 ALOGE_IF(mStatus != GL_FRAMEBUFFER_COMPLETE_OES, "glCheckFramebufferStatusOES error %d",
375 mStatus);
Mathias Agopian3f844832013-08-07 21:24:32 -0700376}
377
Chia-I Wueadbaa62017-11-09 11:26:15 -0800378RenderEngine::BindNativeBufferAsFramebuffer::~BindNativeBufferAsFramebuffer() {
379 if (mImage == EGL_NO_IMAGE_KHR) {
380 return;
381 }
382
Mathias Agopian3f844832013-08-07 21:24:32 -0700383 // back to main framebuffer
Mathias Agopian458197d2013-08-15 14:56:51 -0700384 mEngine.unbindFramebuffer(mTexName, mFbName);
Chia-I Wueadbaa62017-11-09 11:26:15 -0800385 eglDestroyImageKHR(mEngine.mEGLDisplay, mImage);
Mathias Agopian3f844832013-08-07 21:24:32 -0700386}
387
Chia-I Wueadbaa62017-11-09 11:26:15 -0800388status_t RenderEngine::BindNativeBufferAsFramebuffer::getStatus() const {
Mathias Agopian3f844832013-08-07 21:24:32 -0700389 return mStatus == GL_FRAMEBUFFER_COMPLETE_OES ? NO_ERROR : BAD_VALUE;
390}
391
Mathias Agopian875d8e12013-06-07 15:35:48 -0700392// ---------------------------------------------------------------------------
Jesse Hall05f8c702013-12-23 20:44:38 -0800393
Chia-I Wub027f802017-11-29 14:00:52 -0800394static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute,
395 EGLint wanted, EGLConfig* outConfig) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800396 EGLint numConfigs = -1, n = 0;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800397 eglGetConfigs(dpy, nullptr, 0, &numConfigs);
Jesse Hall05f8c702013-12-23 20:44:38 -0800398 EGLConfig* const configs = new EGLConfig[numConfigs];
399 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
400
401 if (n) {
402 if (attribute != EGL_NONE) {
Chia-I Wub027f802017-11-29 14:00:52 -0800403 for (int i = 0; i < n; i++) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800404 EGLint value = 0;
405 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
406 if (wanted == value) {
407 *outConfig = configs[i];
Chia-I Wub027f802017-11-29 14:00:52 -0800408 delete[] configs;
Jesse Hall05f8c702013-12-23 20:44:38 -0800409 return NO_ERROR;
410 }
411 }
412 } else {
413 // just pick the first one
414 *outConfig = configs[0];
Chia-I Wub027f802017-11-29 14:00:52 -0800415 delete[] configs;
Jesse Hall05f8c702013-12-23 20:44:38 -0800416 return NO_ERROR;
417 }
418 }
Chia-I Wub027f802017-11-29 14:00:52 -0800419 delete[] configs;
Jesse Hall05f8c702013-12-23 20:44:38 -0800420 return NAME_NOT_FOUND;
421}
422
423class EGLAttributeVector {
424 struct Attribute;
425 class Adder;
426 friend class Adder;
427 KeyedVector<Attribute, EGLint> mList;
428 struct Attribute {
Chia-I Wub027f802017-11-29 14:00:52 -0800429 Attribute() : v(0){};
430 explicit Attribute(EGLint v) : v(v) {}
Jesse Hall05f8c702013-12-23 20:44:38 -0800431 EGLint v;
Chia-I Wub027f802017-11-29 14:00:52 -0800432 bool operator<(const Attribute& other) const {
Jesse Hall05f8c702013-12-23 20:44:38 -0800433 // this places EGL_NONE at the end
434 EGLint lhs(v);
435 EGLint rhs(other.v);
436 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
437 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
438 return lhs < rhs;
439 }
440 };
441 class Adder {
442 friend class EGLAttributeVector;
443 EGLAttributeVector& v;
444 EGLint attribute;
Chia-I Wub027f802017-11-29 14:00:52 -0800445 Adder(EGLAttributeVector& v, EGLint attribute) : v(v), attribute(attribute) {}
446
Jesse Hall05f8c702013-12-23 20:44:38 -0800447 public:
Chia-I Wub027f802017-11-29 14:00:52 -0800448 void operator=(EGLint value) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800449 if (attribute != EGL_NONE) {
Chih-Hung Hsiehc4067912016-05-03 14:03:27 -0700450 v.mList.add(Attribute(attribute), value);
Jesse Hall05f8c702013-12-23 20:44:38 -0800451 }
452 }
Chia-I Wub027f802017-11-29 14:00:52 -0800453 operator EGLint() const { return v.mList[attribute]; }
Jesse Hall05f8c702013-12-23 20:44:38 -0800454 };
Chia-I Wub027f802017-11-29 14:00:52 -0800455
Jesse Hall05f8c702013-12-23 20:44:38 -0800456public:
Chia-I Wub027f802017-11-29 14:00:52 -0800457 EGLAttributeVector() { mList.add(Attribute(EGL_NONE), EGL_NONE); }
Jesse Hall05f8c702013-12-23 20:44:38 -0800458 void remove(EGLint attribute) {
459 if (attribute != EGL_NONE) {
Chih-Hung Hsiehc4067912016-05-03 14:03:27 -0700460 mList.removeItem(Attribute(attribute));
Jesse Hall05f8c702013-12-23 20:44:38 -0800461 }
462 }
Chia-I Wub027f802017-11-29 14:00:52 -0800463 Adder operator[](EGLint attribute) { return Adder(*this, attribute); }
464 EGLint operator[](EGLint attribute) const { return mList[attribute]; }
Jesse Hall05f8c702013-12-23 20:44:38 -0800465 // cast-operator to (EGLint const*)
Chia-I Wub027f802017-11-29 14:00:52 -0800466 operator EGLint const*() const { return &mList.keyAt(0).v; }
Jesse Hall05f8c702013-12-23 20:44:38 -0800467};
468
Chia-I Wub027f802017-11-29 14:00:52 -0800469static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType,
470 EGLConfig* config) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800471 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
472 // it is to be used with WIFI displays
473 status_t err;
474 EGLint wantedAttribute;
475 EGLint wantedAttributeValue;
476
477 EGLAttributeVector attribs;
478 if (renderableType) {
Chia-I Wub027f802017-11-29 14:00:52 -0800479 attribs[EGL_RENDERABLE_TYPE] = renderableType;
480 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
481 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
Jesse Hall05f8c702013-12-23 20:44:38 -0800482 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
Chia-I Wub027f802017-11-29 14:00:52 -0800483 attribs[EGL_RED_SIZE] = 8;
484 attribs[EGL_GREEN_SIZE] = 8;
485 attribs[EGL_BLUE_SIZE] = 8;
486 attribs[EGL_ALPHA_SIZE] = 8;
487 wantedAttribute = EGL_NONE;
488 wantedAttributeValue = EGL_NONE;
Jesse Hall05f8c702013-12-23 20:44:38 -0800489 } else {
490 // if no renderable type specified, fallback to a simplified query
Chia-I Wub027f802017-11-29 14:00:52 -0800491 wantedAttribute = EGL_NATIVE_VISUAL_ID;
492 wantedAttributeValue = format;
Jesse Hall05f8c702013-12-23 20:44:38 -0800493 }
494
Chia-I Wub027f802017-11-29 14:00:52 -0800495 err = selectConfigForAttribute(display, attribs, wantedAttribute, wantedAttributeValue, config);
Jesse Hall05f8c702013-12-23 20:44:38 -0800496 if (err == NO_ERROR) {
497 EGLint caveat;
498 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
499 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
500 }
501
502 return err;
503}
504
Chia-I Wub027f802017-11-29 14:00:52 -0800505EGLConfig RenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800506 status_t err;
507 EGLConfig config;
508
509 // First try to get an ES2 config
510 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
511 if (err != NO_ERROR) {
512 // If ES2 fails, try ES1
513 err = selectEGLConfig(display, format, EGL_OPENGL_ES_BIT, &config);
514 if (err != NO_ERROR) {
515 // still didn't work, probably because we're on the emulator...
516 // try a simplified query
517 ALOGW("no suitable EGLConfig found, trying a simpler query");
518 err = selectEGLConfig(display, format, 0, &config);
519 if (err != NO_ERROR) {
520 // this EGL is too lame for android
521 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
522 }
523 }
524 }
525
Steven Thomasd7f49c52017-07-26 18:48:28 -0700526 if (logConfig) {
527 // print some debugging info
Chia-I Wub027f802017-11-29 14:00:52 -0800528 EGLint r, g, b, a;
529 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
Steven Thomasd7f49c52017-07-26 18:48:28 -0700530 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
Chia-I Wub027f802017-11-29 14:00:52 -0800531 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
Steven Thomasd7f49c52017-07-26 18:48:28 -0700532 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
533 ALOGI("EGL information:");
534 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
535 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
536 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
Chia-I Wub027f802017-11-29 14:00:52 -0800537 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
Steven Thomasd7f49c52017-07-26 18:48:28 -0700538 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
539 }
Jesse Hall05f8c702013-12-23 20:44:38 -0800540
541 return config;
542}
543
Dan Stoza4e637772016-07-28 13:31:51 -0700544void RenderEngine::primeCache() const {
545 // Getting the ProgramCache instance causes it to prime its shader cache,
546 // which is performed in its constructor
547 ProgramCache::getInstance();
548}
549
Jesse Hall05f8c702013-12-23 20:44:38 -0800550// ---------------------------------------------------------------------------
Mathias Agopian875d8e12013-06-07 15:35:48 -0700551}; // namespace android
552// ---------------------------------------------------------------------------