blob: 22016edd8525016c6210ee2f3f2d6dc87812c277 [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
Jorim Jaggi5b15ef62018-01-17 18:31:39 +010030#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
31#include <configstore/Utils.h>
32
33using namespace android::hardware::configstore;
34using namespace android::hardware::configstore::V1_0;
35
Jiyong Park00b15b82017-08-10 20:30:56 +090036extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
Jesse Hall19e87292013-12-23 21:02:15 -080037
Mathias Agopian875d8e12013-06-07 15:35:48 -070038// ---------------------------------------------------------------------------
39namespace android {
40// ---------------------------------------------------------------------------
41
Chia-I Wud4d9c6f2017-11-09 16:55:31 -080042std::unique_ptr<RenderEngine> RenderEngine::create(int hwcFormat, uint32_t featureFlags) {
43 // initialize EGL for the default display
44 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Peiyong Lin566a3b42018-01-09 18:22:43 -080045 if (!eglInitialize(display, nullptr, nullptr)) {
Chia-I Wud4d9c6f2017-11-09 16:55:31 -080046 LOG_ALWAYS_FATAL("failed to initialize EGL");
47 }
48
Peiyong Lin566a3b42018-01-09 18:22:43 -080049 GLExtensions& extensions = GLExtensions::getInstance();
Chia-I Wu767d7c92017-11-30 13:22:48 -080050 extensions.initWithEGLStrings(eglQueryStringImplementationANDROID(display, EGL_VERSION),
51 eglQueryStringImplementationANDROID(display, EGL_EXTENSIONS));
52
Jesse Hall19e87292013-12-23 21:02:15 -080053 // The code assumes that ES2 or later is available if this extension is
54 // supported.
55 EGLConfig config = EGL_NO_CONFIG;
Chia-I Wu767d7c92017-11-30 13:22:48 -080056 if (!extensions.hasNoConfigContext()) {
Steven Thomasd7f49c52017-07-26 18:48:28 -070057 config = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
Jesse Hall19e87292013-12-23 21:02:15 -080058 }
59
60 EGLint renderableType = 0;
61 if (config == EGL_NO_CONFIG) {
62 renderableType = EGL_OPENGL_ES2_BIT;
Chia-I Wub027f802017-11-29 14:00:52 -080063 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
Jesse Hall19e87292013-12-23 21:02:15 -080064 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
65 }
66 EGLint contextClientVersion = 0;
Mathias Agopian2185f8b2013-09-18 16:20:26 -070067 if (renderableType & EGL_OPENGL_ES2_BIT) {
68 contextClientVersion = 2;
69 } else if (renderableType & EGL_OPENGL_ES_BIT) {
70 contextClientVersion = 1;
71 } else {
72 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
73 }
74
Fabien Sanglardc93afd52017-03-13 13:02:42 -070075 std::vector<EGLint> contextAttributes;
76 contextAttributes.reserve(6);
77 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
78 contextAttributes.push_back(contextClientVersion);
Jorim Jaggi5b15ef62018-01-17 18:31:39 +010079 bool useContextPriority = overrideUseContextPriorityFromConfig(extensions.hasContextPriority());
80 if (useContextPriority) {
Fabien Sanglardc93afd52017-03-13 13:02:42 -070081 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
82 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
83 }
Fabien Sanglardc93afd52017-03-13 13:02:42 -070084 contextAttributes.push_back(EGL_NONE);
85
Peiyong Lin566a3b42018-01-09 18:22:43 -080086 EGLContext ctxt = eglCreateContext(display, config, nullptr, contextAttributes.data());
Mathias Agopian875d8e12013-06-07 15:35:48 -070087
88 // if can't create a GL context, we can only abort.
Chia-I Wub027f802017-11-29 14:00:52 -080089 LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
Mathias Agopian875d8e12013-06-07 15:35:48 -070090
91 // now figure out what version of GL did we actually get
92 // NOTE: a dummy surface is not needed if KHR_create_context is supported
93
Jesse Hall19e87292013-12-23 21:02:15 -080094 EGLConfig dummyConfig = config;
95 if (dummyConfig == EGL_NO_CONFIG) {
Steven Thomasd7f49c52017-07-26 18:48:28 -070096 dummyConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
Jesse Hall19e87292013-12-23 21:02:15 -080097 }
Chia-I Wub027f802017-11-29 14:00:52 -080098 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE, EGL_NONE};
Jesse Hall19e87292013-12-23 21:02:15 -080099 EGLSurface dummy = eglCreatePbufferSurface(display, dummyConfig, attribs);
Chia-I Wub027f802017-11-29 14:00:52 -0800100 LOG_ALWAYS_FATAL_IF(dummy == EGL_NO_SURFACE, "can't create dummy pbuffer");
Mathias Agopian875d8e12013-06-07 15:35:48 -0700101 EGLBoolean success = eglMakeCurrent(display, dummy, dummy, ctxt);
102 LOG_ALWAYS_FATAL_IF(!success, "can't make dummy pbuffer current");
103
Chia-I Wub027f802017-11-29 14:00:52 -0800104 extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER),
105 glGetString(GL_VERSION), glGetString(GL_EXTENSIONS));
Mathias Agopian875d8e12013-06-07 15:35:48 -0700106
Chia-I Wub027f802017-11-29 14:00:52 -0800107 GlesVersion version = parseGlesVersion(extensions.getVersion());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700108
109 // initialize the renderer while GL is current
110
Chia-I Wub2c76242017-11-09 17:17:07 -0800111 std::unique_ptr<RenderEngine> engine;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700112 switch (version) {
Chia-I Wub027f802017-11-29 14:00:52 -0800113 case GLES_VERSION_1_0:
114 case GLES_VERSION_1_1:
115 LOG_ALWAYS_FATAL("SurfaceFlinger requires OpenGL ES 2.0 minimum to run.");
116 break;
117 case GLES_VERSION_2_0:
118 case GLES_VERSION_3_0:
119 engine = std::make_unique<GLES20RenderEngine>(featureFlags);
120 break;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700121 }
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800122 engine->setEGLHandles(display, config, ctxt);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700123
124 ALOGI("OpenGL ES informations:");
125 ALOGI("vendor : %s", extensions.getVendor());
126 ALOGI("renderer : %s", extensions.getRenderer());
127 ALOGI("version : %s", extensions.getVersion());
Chia-I Wu767d7c92017-11-30 13:22:48 -0800128 ALOGI("extensions: %s", extensions.getExtensions());
Michael Lentine9ae79d82014-07-30 16:42:12 -0700129 ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
130 ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700131
132 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
133 eglDestroySurface(display, dummy);
134
135 return engine;
136}
137
Jorim Jaggi5b15ef62018-01-17 18:31:39 +0100138bool RenderEngine::overrideUseContextPriorityFromConfig(bool useContextPriority) {
139 OptionalBool ret;
140 ISurfaceFlingerConfigs::getService()->useContextPriority([&ret](OptionalBool b) {
141 ret = b;
142 });
143 if (ret.specified) {
144 return ret.value;
145 } else {
146 return useContextPriority;
147 }
148}
149
Chia-I Wub027f802017-11-29 14:00:52 -0800150RenderEngine::RenderEngine()
Peiyong Lin566a3b42018-01-09 18:22:43 -0800151 : mEGLDisplay(EGL_NO_DISPLAY), mEGLConfig(nullptr), mEGLContext(EGL_NO_CONTEXT) {}
Mathias Agopian875d8e12013-06-07 15:35:48 -0700152
153RenderEngine::~RenderEngine() {
Chia-I Wub01450b2017-11-09 16:55:31 -0800154 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
155 eglTerminate(mEGLDisplay);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700156}
157
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800158void RenderEngine::setEGLHandles(EGLDisplay display, EGLConfig config, EGLContext ctxt) {
159 mEGLDisplay = display;
Jesse Hall05f8c702013-12-23 20:44:38 -0800160 mEGLConfig = config;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700161 mEGLContext = ctxt;
162}
163
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800164EGLDisplay RenderEngine::getEGLDisplay() const {
165 return mEGLDisplay;
166}
167
168EGLConfig RenderEngine::getEGLConfig() const {
Jesse Hall05f8c702013-12-23 20:44:38 -0800169 return mEGLConfig;
170}
171
Chia-I Wu401ef832017-12-01 10:52:22 -0800172bool RenderEngine::supportsImageCrop() const {
173 return GLExtensions::getInstance().hasImageCrop();
174}
175
Chia-I Wu9f2db772017-11-30 21:06:50 -0800176bool RenderEngine::isCurrent() const {
177 return mEGLDisplay == eglGetCurrentDisplay() && mEGLContext == eglGetCurrentContext();
178}
179
Chia-I Wuf846a352017-11-10 09:22:52 -0800180bool RenderEngine::setCurrentSurface(const RE::Surface& surface) {
181 bool success = true;
182 EGLSurface eglSurface = surface.getEGLSurface();
183 if (eglSurface != eglGetCurrentSurface(EGL_DRAW)) {
184 success = eglMakeCurrent(mEGLDisplay, eglSurface, eglSurface, mEGLContext) == EGL_TRUE;
185 if (success && surface.getAsync()) {
186 eglSwapInterval(mEGLDisplay, 0);
187 }
188 }
189
190 return success;
Chia-I Wu7f402902017-11-09 12:51:10 -0800191}
192
193void RenderEngine::resetCurrentSurface() {
194 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
195}
196
Chia-I Wu767fcf72017-11-30 22:07:38 -0800197base::unique_fd RenderEngine::flush() {
198 if (!GLExtensions::getInstance().hasNativeFenceSync()) {
199 return base::unique_fd();
200 }
201
Peiyong Lin566a3b42018-01-09 18:22:43 -0800202 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
Chia-I Wu767fcf72017-11-30 22:07:38 -0800203 if (sync == EGL_NO_SYNC_KHR) {
204 ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
205 return base::unique_fd();
206 }
207
208 // native fence fd will not be populated until flush() is done.
209 glFlush();
210
211 // get the fence fd
212 base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
213 eglDestroySyncKHR(mEGLDisplay, sync);
214 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
215 ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
216 }
217
218 return fenceFd;
219}
220
221bool RenderEngine::finish() {
222 if (!GLExtensions::getInstance().hasFenceSync()) {
223 ALOGW("no synchronization support");
224 return false;
225 }
226
Peiyong Lin566a3b42018-01-09 18:22:43 -0800227 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, nullptr);
Chia-I Wu767fcf72017-11-30 22:07:38 -0800228 if (sync == EGL_NO_SYNC_KHR) {
229 ALOGW("failed to create EGL fence sync: %#x", eglGetError());
230 return false;
231 }
232
233 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
234 2000000000 /*2 sec*/);
235 EGLint error = eglGetError();
236 eglDestroySyncKHR(mEGLDisplay, sync);
237 if (result != EGL_CONDITION_SATISFIED_KHR) {
238 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
239 ALOGW("fence wait timed out");
240 } else {
241 ALOGW("error waiting on EGL fence: %#x", error);
242 }
243 return false;
244 }
245
246 return true;
247}
248
249bool RenderEngine::waitFence(base::unique_fd fenceFd) {
250 if (!GLExtensions::getInstance().hasNativeFenceSync() ||
251 !GLExtensions::getInstance().hasWaitSync()) {
252 return false;
253 }
254
255 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE};
256 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
257 if (sync == EGL_NO_SYNC_KHR) {
258 ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
259 return false;
260 }
261
262 // fenceFd is now owned by EGLSync
263 (void)fenceFd.release();
264
265 // XXX: The spec draft is inconsistent as to whether this should return an
266 // EGLint or void. Ignore the return value for now, as it's not strictly
267 // needed.
268 eglWaitSyncKHR(mEGLDisplay, sync, 0);
269 EGLint error = eglGetError();
270 eglDestroySyncKHR(mEGLDisplay, sync);
271 if (error != EGL_SUCCESS) {
272 ALOGE("failed to wait for EGL native fence sync: %#x", error);
273 return false;
274 }
275
276 return true;
277}
278
Mathias Agopian875d8e12013-06-07 15:35:48 -0700279void RenderEngine::checkErrors() const {
280 do {
281 // there could be more than one error flag
282 GLenum error = glGetError();
Chia-I Wub027f802017-11-29 14:00:52 -0800283 if (error == GL_NO_ERROR) break;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700284 ALOGE("GL error 0x%04x", int(error));
285 } while (true);
286}
287
288RenderEngine::GlesVersion RenderEngine::parseGlesVersion(const char* str) {
289 int major, minor;
290 if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) {
291 if (sscanf(str, "OpenGL ES %d.%d", &major, &minor) != 2) {
292 ALOGW("Unable to parse GL_VERSION string: \"%s\"", str);
293 return GLES_VERSION_1_0;
294 }
295 }
296
297 if (major == 1 && minor == 0) return GLES_VERSION_1_0;
298 if (major == 1 && minor >= 1) return GLES_VERSION_1_1;
299 if (major == 2 && minor >= 0) return GLES_VERSION_2_0;
300 if (major == 3 && minor >= 0) return GLES_VERSION_3_0;
301
302 ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor);
303 return GLES_VERSION_1_0;
304}
305
Chia-I Wub027f802017-11-29 14:00:52 -0800306void RenderEngine::fillRegionWithColor(const Region& region, uint32_t height, float red,
307 float green, float blue, float alpha) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700308 size_t c;
309 Rect const* r = region.getArray(&c);
Chia-I Wub027f802017-11-29 14:00:52 -0800310 Mesh mesh(Mesh::TRIANGLES, c * 6, 2);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700311 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Chia-I Wub027f802017-11-29 14:00:52 -0800312 for (size_t i = 0; i < c; i++, r++) {
313 position[i * 6 + 0].x = r->left;
314 position[i * 6 + 0].y = height - r->top;
315 position[i * 6 + 1].x = r->left;
316 position[i * 6 + 1].y = height - r->bottom;
317 position[i * 6 + 2].x = r->right;
318 position[i * 6 + 2].y = height - r->bottom;
319 position[i * 6 + 3].x = r->left;
320 position[i * 6 + 3].y = height - r->top;
321 position[i * 6 + 4].x = r->right;
322 position[i * 6 + 4].y = height - r->bottom;
323 position[i * 6 + 5].x = r->right;
324 position[i * 6 + 5].y = height - r->top;
Mathias Agopian3f844832013-08-07 21:24:32 -0700325 }
Mathias Agopian19733a32013-08-28 18:13:56 -0700326 setupFillWithColor(red, green, blue, alpha);
327 drawMesh(mesh);
Mathias Agopian3f844832013-08-07 21:24:32 -0700328}
329
330void RenderEngine::clearWithColor(float red, float green, float blue, float alpha) {
331 glClearColor(red, green, blue, alpha);
332 glClear(GL_COLOR_BUFFER_BIT);
333}
334
Chia-I Wub027f802017-11-29 14:00:52 -0800335void RenderEngine::setScissor(uint32_t left, uint32_t bottom, uint32_t right, uint32_t top) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700336 glScissor(left, bottom, right, top);
337 glEnable(GL_SCISSOR_TEST);
338}
339
340void RenderEngine::disableScissor() {
341 glDisable(GL_SCISSOR_TEST);
342}
343
344void RenderEngine::genTextures(size_t count, uint32_t* names) {
345 glGenTextures(count, names);
346}
347
348void RenderEngine::deleteTextures(size_t count, uint32_t const* names) {
349 glDeleteTextures(count, names);
350}
351
Chia-I Wu401ef832017-12-01 10:52:22 -0800352void RenderEngine::bindExternalTextureImage(uint32_t texName, const RE::Image& image) {
353 const GLenum target = GL_TEXTURE_EXTERNAL_OES;
354
355 glBindTexture(target, texName);
356 if (image.getEGLImage() != EGL_NO_IMAGE_KHR) {
357 glEGLImageTargetTexture2DOES(target, static_cast<GLeglImageOES>(image.getEGLImage()));
358 }
359}
360
Mathias Agopiand5556842013-09-19 17:08:37 -0700361void RenderEngine::readPixels(size_t l, size_t b, size_t w, size_t h, uint32_t* pixels) {
362 glReadPixels(l, b, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
363}
364
Mathias Agopian458197d2013-08-15 14:56:51 -0700365void RenderEngine::dump(String8& result) {
Peiyong Lin566a3b42018-01-09 18:22:43 -0800366 const GLExtensions& extensions = GLExtensions::getInstance();
Chia-I Wu767d7c92017-11-30 13:22:48 -0800367
368 result.appendFormat("EGL implementation : %s\n", extensions.getEGLVersion());
369 result.appendFormat("%s\n", extensions.getEGLExtensions());
370
Chia-I Wub027f802017-11-29 14:00:52 -0800371 result.appendFormat("GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
372 extensions.getVersion());
Chia-I Wu767d7c92017-11-30 13:22:48 -0800373 result.appendFormat("%s\n", extensions.getExtensions());
Mathias Agopian458197d2013-08-15 14:56:51 -0700374}
375
Mathias Agopian3f844832013-08-07 21:24:32 -0700376// ---------------------------------------------------------------------------
377
Chia-I Wueadbaa62017-11-09 11:26:15 -0800378RenderEngine::BindNativeBufferAsFramebuffer::BindNativeBufferAsFramebuffer(
Chia-I Wub027f802017-11-29 14:00:52 -0800379 RenderEngine& engine, ANativeWindowBuffer* buffer)
380 : mEngine(engine) {
381 mImage = eglCreateImageKHR(mEngine.mEGLDisplay, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
Peiyong Lin566a3b42018-01-09 18:22:43 -0800382 buffer, nullptr);
Chia-I Wueadbaa62017-11-09 11:26:15 -0800383 if (mImage == EGL_NO_IMAGE_KHR) {
384 mStatus = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
385 return;
386 }
387
388 mEngine.bindImageAsFramebuffer(mImage, &mTexName, &mFbName, &mStatus);
Mathias Agopian458197d2013-08-15 14:56:51 -0700389
Chia-I Wub027f802017-11-29 14:00:52 -0800390 ALOGE_IF(mStatus != GL_FRAMEBUFFER_COMPLETE_OES, "glCheckFramebufferStatusOES error %d",
391 mStatus);
Mathias Agopian3f844832013-08-07 21:24:32 -0700392}
393
Chia-I Wueadbaa62017-11-09 11:26:15 -0800394RenderEngine::BindNativeBufferAsFramebuffer::~BindNativeBufferAsFramebuffer() {
395 if (mImage == EGL_NO_IMAGE_KHR) {
396 return;
397 }
398
Mathias Agopian3f844832013-08-07 21:24:32 -0700399 // back to main framebuffer
Mathias Agopian458197d2013-08-15 14:56:51 -0700400 mEngine.unbindFramebuffer(mTexName, mFbName);
Chia-I Wueadbaa62017-11-09 11:26:15 -0800401 eglDestroyImageKHR(mEngine.mEGLDisplay, mImage);
Mathias Agopian3f844832013-08-07 21:24:32 -0700402}
403
Chia-I Wueadbaa62017-11-09 11:26:15 -0800404status_t RenderEngine::BindNativeBufferAsFramebuffer::getStatus() const {
Mathias Agopian3f844832013-08-07 21:24:32 -0700405 return mStatus == GL_FRAMEBUFFER_COMPLETE_OES ? NO_ERROR : BAD_VALUE;
406}
407
Mathias Agopian875d8e12013-06-07 15:35:48 -0700408// ---------------------------------------------------------------------------
Jesse Hall05f8c702013-12-23 20:44:38 -0800409
Chia-I Wub027f802017-11-29 14:00:52 -0800410static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute,
411 EGLint wanted, EGLConfig* outConfig) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800412 EGLint numConfigs = -1, n = 0;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800413 eglGetConfigs(dpy, nullptr, 0, &numConfigs);
Jesse Hall05f8c702013-12-23 20:44:38 -0800414 EGLConfig* const configs = new EGLConfig[numConfigs];
415 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
416
417 if (n) {
418 if (attribute != EGL_NONE) {
Chia-I Wub027f802017-11-29 14:00:52 -0800419 for (int i = 0; i < n; i++) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800420 EGLint value = 0;
421 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
422 if (wanted == value) {
423 *outConfig = configs[i];
Chia-I Wub027f802017-11-29 14:00:52 -0800424 delete[] configs;
Jesse Hall05f8c702013-12-23 20:44:38 -0800425 return NO_ERROR;
426 }
427 }
428 } else {
429 // just pick the first one
430 *outConfig = configs[0];
Chia-I Wub027f802017-11-29 14:00:52 -0800431 delete[] configs;
Jesse Hall05f8c702013-12-23 20:44:38 -0800432 return NO_ERROR;
433 }
434 }
Chia-I Wub027f802017-11-29 14:00:52 -0800435 delete[] configs;
Jesse Hall05f8c702013-12-23 20:44:38 -0800436 return NAME_NOT_FOUND;
437}
438
439class EGLAttributeVector {
440 struct Attribute;
441 class Adder;
442 friend class Adder;
443 KeyedVector<Attribute, EGLint> mList;
444 struct Attribute {
Chia-I Wub027f802017-11-29 14:00:52 -0800445 Attribute() : v(0){};
446 explicit Attribute(EGLint v) : v(v) {}
Jesse Hall05f8c702013-12-23 20:44:38 -0800447 EGLint v;
Chia-I Wub027f802017-11-29 14:00:52 -0800448 bool operator<(const Attribute& other) const {
Jesse Hall05f8c702013-12-23 20:44:38 -0800449 // this places EGL_NONE at the end
450 EGLint lhs(v);
451 EGLint rhs(other.v);
452 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
453 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
454 return lhs < rhs;
455 }
456 };
457 class Adder {
458 friend class EGLAttributeVector;
459 EGLAttributeVector& v;
460 EGLint attribute;
Chia-I Wub027f802017-11-29 14:00:52 -0800461 Adder(EGLAttributeVector& v, EGLint attribute) : v(v), attribute(attribute) {}
462
Jesse Hall05f8c702013-12-23 20:44:38 -0800463 public:
Chia-I Wub027f802017-11-29 14:00:52 -0800464 void operator=(EGLint value) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800465 if (attribute != EGL_NONE) {
Chih-Hung Hsiehc4067912016-05-03 14:03:27 -0700466 v.mList.add(Attribute(attribute), value);
Jesse Hall05f8c702013-12-23 20:44:38 -0800467 }
468 }
Chia-I Wub027f802017-11-29 14:00:52 -0800469 operator EGLint() const { return v.mList[attribute]; }
Jesse Hall05f8c702013-12-23 20:44:38 -0800470 };
Chia-I Wub027f802017-11-29 14:00:52 -0800471
Jesse Hall05f8c702013-12-23 20:44:38 -0800472public:
Chia-I Wub027f802017-11-29 14:00:52 -0800473 EGLAttributeVector() { mList.add(Attribute(EGL_NONE), EGL_NONE); }
Jesse Hall05f8c702013-12-23 20:44:38 -0800474 void remove(EGLint attribute) {
475 if (attribute != EGL_NONE) {
Chih-Hung Hsiehc4067912016-05-03 14:03:27 -0700476 mList.removeItem(Attribute(attribute));
Jesse Hall05f8c702013-12-23 20:44:38 -0800477 }
478 }
Chia-I Wub027f802017-11-29 14:00:52 -0800479 Adder operator[](EGLint attribute) { return Adder(*this, attribute); }
480 EGLint operator[](EGLint attribute) const { return mList[attribute]; }
Jesse Hall05f8c702013-12-23 20:44:38 -0800481 // cast-operator to (EGLint const*)
Chia-I Wub027f802017-11-29 14:00:52 -0800482 operator EGLint const*() const { return &mList.keyAt(0).v; }
Jesse Hall05f8c702013-12-23 20:44:38 -0800483};
484
Chia-I Wub027f802017-11-29 14:00:52 -0800485static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType,
486 EGLConfig* config) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800487 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
488 // it is to be used with WIFI displays
489 status_t err;
490 EGLint wantedAttribute;
491 EGLint wantedAttributeValue;
492
493 EGLAttributeVector attribs;
494 if (renderableType) {
Chia-I Wub027f802017-11-29 14:00:52 -0800495 attribs[EGL_RENDERABLE_TYPE] = renderableType;
496 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
497 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
Jesse Hall05f8c702013-12-23 20:44:38 -0800498 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
Chia-I Wub027f802017-11-29 14:00:52 -0800499 attribs[EGL_RED_SIZE] = 8;
500 attribs[EGL_GREEN_SIZE] = 8;
501 attribs[EGL_BLUE_SIZE] = 8;
502 attribs[EGL_ALPHA_SIZE] = 8;
503 wantedAttribute = EGL_NONE;
504 wantedAttributeValue = EGL_NONE;
Jesse Hall05f8c702013-12-23 20:44:38 -0800505 } else {
506 // if no renderable type specified, fallback to a simplified query
Chia-I Wub027f802017-11-29 14:00:52 -0800507 wantedAttribute = EGL_NATIVE_VISUAL_ID;
508 wantedAttributeValue = format;
Jesse Hall05f8c702013-12-23 20:44:38 -0800509 }
510
Chia-I Wub027f802017-11-29 14:00:52 -0800511 err = selectConfigForAttribute(display, attribs, wantedAttribute, wantedAttributeValue, config);
Jesse Hall05f8c702013-12-23 20:44:38 -0800512 if (err == NO_ERROR) {
513 EGLint caveat;
514 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
515 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
516 }
517
518 return err;
519}
520
Chia-I Wub027f802017-11-29 14:00:52 -0800521EGLConfig RenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800522 status_t err;
523 EGLConfig config;
524
525 // First try to get an ES2 config
526 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
527 if (err != NO_ERROR) {
528 // If ES2 fails, try ES1
529 err = selectEGLConfig(display, format, EGL_OPENGL_ES_BIT, &config);
530 if (err != NO_ERROR) {
531 // still didn't work, probably because we're on the emulator...
532 // try a simplified query
533 ALOGW("no suitable EGLConfig found, trying a simpler query");
534 err = selectEGLConfig(display, format, 0, &config);
535 if (err != NO_ERROR) {
536 // this EGL is too lame for android
537 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
538 }
539 }
540 }
541
Steven Thomasd7f49c52017-07-26 18:48:28 -0700542 if (logConfig) {
543 // print some debugging info
Chia-I Wub027f802017-11-29 14:00:52 -0800544 EGLint r, g, b, a;
545 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
Steven Thomasd7f49c52017-07-26 18:48:28 -0700546 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
Chia-I Wub027f802017-11-29 14:00:52 -0800547 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
Steven Thomasd7f49c52017-07-26 18:48:28 -0700548 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
549 ALOGI("EGL information:");
550 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
551 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
552 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
Chia-I Wub027f802017-11-29 14:00:52 -0800553 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
Steven Thomasd7f49c52017-07-26 18:48:28 -0700554 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
555 }
Jesse Hall05f8c702013-12-23 20:44:38 -0800556
557 return config;
558}
559
Dan Stoza4e637772016-07-28 13:31:51 -0700560void RenderEngine::primeCache() const {
561 // Getting the ProgramCache instance causes it to prime its shader cache,
562 // which is performed in its constructor
563 ProgramCache::getInstance();
564}
565
Jesse Hall05f8c702013-12-23 20:44:38 -0800566// ---------------------------------------------------------------------------
Mathias Agopian875d8e12013-06-07 15:35:48 -0700567}; // namespace android
568// ---------------------------------------------------------------------------