blob: 0b8b83873f509af9d04237f3669aaaf4a7063ddb [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 {
Lloyd Pique144e1162017-12-20 16:44:52 -080040namespace RE {
Mathias Agopian875d8e12013-06-07 15:35:48 -070041// ---------------------------------------------------------------------------
42
Lloyd Pique144e1162017-12-20 16:44:52 -080043RenderEngine::~RenderEngine() = default;
44
45namespace impl {
46
Chia-I Wud4d9c6f2017-11-09 16:55:31 -080047std::unique_ptr<RenderEngine> RenderEngine::create(int hwcFormat, uint32_t featureFlags) {
48 // initialize EGL for the default display
49 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Peiyong Lin566a3b42018-01-09 18:22:43 -080050 if (!eglInitialize(display, nullptr, nullptr)) {
Chia-I Wud4d9c6f2017-11-09 16:55:31 -080051 LOG_ALWAYS_FATAL("failed to initialize EGL");
52 }
53
Peiyong Lin566a3b42018-01-09 18:22:43 -080054 GLExtensions& extensions = GLExtensions::getInstance();
Chia-I Wu767d7c92017-11-30 13:22:48 -080055 extensions.initWithEGLStrings(eglQueryStringImplementationANDROID(display, EGL_VERSION),
56 eglQueryStringImplementationANDROID(display, EGL_EXTENSIONS));
57
Jesse Hall19e87292013-12-23 21:02:15 -080058 // The code assumes that ES2 or later is available if this extension is
59 // supported.
60 EGLConfig config = EGL_NO_CONFIG;
Chia-I Wu767d7c92017-11-30 13:22:48 -080061 if (!extensions.hasNoConfigContext()) {
Steven Thomasd7f49c52017-07-26 18:48:28 -070062 config = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
Jesse Hall19e87292013-12-23 21:02:15 -080063 }
64
65 EGLint renderableType = 0;
66 if (config == EGL_NO_CONFIG) {
67 renderableType = EGL_OPENGL_ES2_BIT;
Chia-I Wub027f802017-11-29 14:00:52 -080068 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
Jesse Hall19e87292013-12-23 21:02:15 -080069 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
70 }
71 EGLint contextClientVersion = 0;
Mathias Agopian2185f8b2013-09-18 16:20:26 -070072 if (renderableType & EGL_OPENGL_ES2_BIT) {
73 contextClientVersion = 2;
74 } else if (renderableType & EGL_OPENGL_ES_BIT) {
75 contextClientVersion = 1;
76 } else {
77 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
78 }
79
Fabien Sanglardc93afd52017-03-13 13:02:42 -070080 std::vector<EGLint> contextAttributes;
81 contextAttributes.reserve(6);
82 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
83 contextAttributes.push_back(contextClientVersion);
Jorim Jaggi5b15ef62018-01-17 18:31:39 +010084 bool useContextPriority = overrideUseContextPriorityFromConfig(extensions.hasContextPriority());
85 if (useContextPriority) {
Fabien Sanglardc93afd52017-03-13 13:02:42 -070086 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
87 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
88 }
Fabien Sanglardc93afd52017-03-13 13:02:42 -070089 contextAttributes.push_back(EGL_NONE);
90
Peiyong Lin566a3b42018-01-09 18:22:43 -080091 EGLContext ctxt = eglCreateContext(display, config, nullptr, contextAttributes.data());
Mathias Agopian875d8e12013-06-07 15:35:48 -070092
93 // if can't create a GL context, we can only abort.
Chia-I Wub027f802017-11-29 14:00:52 -080094 LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
Mathias Agopian875d8e12013-06-07 15:35:48 -070095
96 // now figure out what version of GL did we actually get
97 // NOTE: a dummy surface is not needed if KHR_create_context is supported
98
Jesse Hall19e87292013-12-23 21:02:15 -080099 EGLConfig dummyConfig = config;
100 if (dummyConfig == EGL_NO_CONFIG) {
Steven Thomasd7f49c52017-07-26 18:48:28 -0700101 dummyConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
Jesse Hall19e87292013-12-23 21:02:15 -0800102 }
Chia-I Wub027f802017-11-29 14:00:52 -0800103 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE, EGL_NONE};
Jesse Hall19e87292013-12-23 21:02:15 -0800104 EGLSurface dummy = eglCreatePbufferSurface(display, dummyConfig, attribs);
Chia-I Wub027f802017-11-29 14:00:52 -0800105 LOG_ALWAYS_FATAL_IF(dummy == EGL_NO_SURFACE, "can't create dummy pbuffer");
Mathias Agopian875d8e12013-06-07 15:35:48 -0700106 EGLBoolean success = eglMakeCurrent(display, dummy, dummy, ctxt);
107 LOG_ALWAYS_FATAL_IF(!success, "can't make dummy pbuffer current");
108
Chia-I Wub027f802017-11-29 14:00:52 -0800109 extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER),
110 glGetString(GL_VERSION), glGetString(GL_EXTENSIONS));
Mathias Agopian875d8e12013-06-07 15:35:48 -0700111
Chia-I Wub027f802017-11-29 14:00:52 -0800112 GlesVersion version = parseGlesVersion(extensions.getVersion());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700113
114 // initialize the renderer while GL is current
115
Chia-I Wub2c76242017-11-09 17:17:07 -0800116 std::unique_ptr<RenderEngine> engine;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700117 switch (version) {
Chia-I Wub027f802017-11-29 14:00:52 -0800118 case GLES_VERSION_1_0:
119 case GLES_VERSION_1_1:
120 LOG_ALWAYS_FATAL("SurfaceFlinger requires OpenGL ES 2.0 minimum to run.");
121 break;
122 case GLES_VERSION_2_0:
123 case GLES_VERSION_3_0:
124 engine = std::make_unique<GLES20RenderEngine>(featureFlags);
125 break;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700126 }
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800127 engine->setEGLHandles(display, config, ctxt);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700128
129 ALOGI("OpenGL ES informations:");
130 ALOGI("vendor : %s", extensions.getVendor());
131 ALOGI("renderer : %s", extensions.getRenderer());
132 ALOGI("version : %s", extensions.getVersion());
Chia-I Wu767d7c92017-11-30 13:22:48 -0800133 ALOGI("extensions: %s", extensions.getExtensions());
Michael Lentine9ae79d82014-07-30 16:42:12 -0700134 ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
135 ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700136
137 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
138 eglDestroySurface(display, dummy);
139
140 return engine;
141}
142
Jorim Jaggi5b15ef62018-01-17 18:31:39 +0100143bool RenderEngine::overrideUseContextPriorityFromConfig(bool useContextPriority) {
144 OptionalBool ret;
Lloyd Pique144e1162017-12-20 16:44:52 -0800145 ISurfaceFlingerConfigs::getService()->useContextPriority([&ret](OptionalBool b) { ret = b; });
Jorim Jaggi5b15ef62018-01-17 18:31:39 +0100146 if (ret.specified) {
147 return ret.value;
148 } else {
149 return useContextPriority;
150 }
151}
152
Chia-I Wu93e14df2018-06-04 10:10:17 -0700153RenderEngine::RenderEngine(uint32_t featureFlags)
154 : mEGLDisplay(EGL_NO_DISPLAY),
155 mEGLConfig(nullptr),
156 mEGLContext(EGL_NO_CONTEXT),
157 mFeatureFlags(featureFlags) {}
Mathias Agopian875d8e12013-06-07 15:35:48 -0700158
159RenderEngine::~RenderEngine() {
Chia-I Wub01450b2017-11-09 16:55:31 -0800160 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
161 eglTerminate(mEGLDisplay);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700162}
163
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800164void RenderEngine::setEGLHandles(EGLDisplay display, EGLConfig config, EGLContext ctxt) {
165 mEGLDisplay = display;
Jesse Hall05f8c702013-12-23 20:44:38 -0800166 mEGLConfig = config;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700167 mEGLContext = ctxt;
168}
169
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800170EGLDisplay RenderEngine::getEGLDisplay() const {
171 return mEGLDisplay;
172}
173
174EGLConfig RenderEngine::getEGLConfig() const {
Jesse Hall05f8c702013-12-23 20:44:38 -0800175 return mEGLConfig;
176}
177
Chia-I Wu9f2db772017-11-30 21:06:50 -0800178bool RenderEngine::isCurrent() const {
179 return mEGLDisplay == eglGetCurrentDisplay() && mEGLContext == eglGetCurrentContext();
180}
181
Lloyd Pique144e1162017-12-20 16:44:52 -0800182std::unique_ptr<RE::Surface> RenderEngine::createSurface() {
183 return std::make_unique<Surface>(*this);
184}
185
186std::unique_ptr<RE::Image> RenderEngine::createImage() {
187 return std::make_unique<Image>(*this);
188}
189
190bool RenderEngine::setCurrentSurface(const android::RE::Surface& surface) {
191 // Note: RE::Surface is an abstract interface. This implementation only ever
192 // creates RE::impl::Surface's, so it is safe to just cast to the actual
193 // type.
194 return setCurrentSurface(static_cast<const android::RE::impl::Surface&>(surface));
195}
196
197bool RenderEngine::setCurrentSurface(const android::RE::impl::Surface& surface) {
Chia-I Wuf846a352017-11-10 09:22:52 -0800198 bool success = true;
199 EGLSurface eglSurface = surface.getEGLSurface();
200 if (eglSurface != eglGetCurrentSurface(EGL_DRAW)) {
201 success = eglMakeCurrent(mEGLDisplay, eglSurface, eglSurface, mEGLContext) == EGL_TRUE;
202 if (success && surface.getAsync()) {
203 eglSwapInterval(mEGLDisplay, 0);
204 }
205 }
206
207 return success;
Chia-I Wu7f402902017-11-09 12:51:10 -0800208}
209
210void RenderEngine::resetCurrentSurface() {
211 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
212}
213
Chia-I Wu767fcf72017-11-30 22:07:38 -0800214base::unique_fd RenderEngine::flush() {
215 if (!GLExtensions::getInstance().hasNativeFenceSync()) {
216 return base::unique_fd();
217 }
218
Peiyong Lin566a3b42018-01-09 18:22:43 -0800219 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
Chia-I Wu767fcf72017-11-30 22:07:38 -0800220 if (sync == EGL_NO_SYNC_KHR) {
221 ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
222 return base::unique_fd();
223 }
224
225 // native fence fd will not be populated until flush() is done.
226 glFlush();
227
228 // get the fence fd
229 base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
230 eglDestroySyncKHR(mEGLDisplay, sync);
231 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
232 ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
233 }
234
235 return fenceFd;
236}
237
238bool RenderEngine::finish() {
239 if (!GLExtensions::getInstance().hasFenceSync()) {
240 ALOGW("no synchronization support");
241 return false;
242 }
243
Peiyong Lin566a3b42018-01-09 18:22:43 -0800244 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, nullptr);
Chia-I Wu767fcf72017-11-30 22:07:38 -0800245 if (sync == EGL_NO_SYNC_KHR) {
246 ALOGW("failed to create EGL fence sync: %#x", eglGetError());
247 return false;
248 }
249
250 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
251 2000000000 /*2 sec*/);
252 EGLint error = eglGetError();
253 eglDestroySyncKHR(mEGLDisplay, sync);
254 if (result != EGL_CONDITION_SATISFIED_KHR) {
255 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
256 ALOGW("fence wait timed out");
257 } else {
258 ALOGW("error waiting on EGL fence: %#x", error);
259 }
260 return false;
261 }
262
263 return true;
264}
265
266bool RenderEngine::waitFence(base::unique_fd fenceFd) {
267 if (!GLExtensions::getInstance().hasNativeFenceSync() ||
268 !GLExtensions::getInstance().hasWaitSync()) {
269 return false;
270 }
271
272 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE};
273 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
274 if (sync == EGL_NO_SYNC_KHR) {
275 ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
276 return false;
277 }
278
279 // fenceFd is now owned by EGLSync
280 (void)fenceFd.release();
281
282 // XXX: The spec draft is inconsistent as to whether this should return an
283 // EGLint or void. Ignore the return value for now, as it's not strictly
284 // needed.
285 eglWaitSyncKHR(mEGLDisplay, sync, 0);
286 EGLint error = eglGetError();
287 eglDestroySyncKHR(mEGLDisplay, sync);
288 if (error != EGL_SUCCESS) {
289 ALOGE("failed to wait for EGL native fence sync: %#x", error);
290 return false;
291 }
292
293 return true;
294}
295
Mathias Agopian875d8e12013-06-07 15:35:48 -0700296void RenderEngine::checkErrors() const {
297 do {
298 // there could be more than one error flag
299 GLenum error = glGetError();
Chia-I Wub027f802017-11-29 14:00:52 -0800300 if (error == GL_NO_ERROR) break;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700301 ALOGE("GL error 0x%04x", int(error));
302 } while (true);
303}
304
305RenderEngine::GlesVersion RenderEngine::parseGlesVersion(const char* str) {
306 int major, minor;
307 if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) {
308 if (sscanf(str, "OpenGL ES %d.%d", &major, &minor) != 2) {
309 ALOGW("Unable to parse GL_VERSION string: \"%s\"", str);
310 return GLES_VERSION_1_0;
311 }
312 }
313
314 if (major == 1 && minor == 0) return GLES_VERSION_1_0;
315 if (major == 1 && minor >= 1) return GLES_VERSION_1_1;
316 if (major == 2 && minor >= 0) return GLES_VERSION_2_0;
317 if (major == 3 && minor >= 0) return GLES_VERSION_3_0;
318
319 ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor);
320 return GLES_VERSION_1_0;
321}
322
Chia-I Wub027f802017-11-29 14:00:52 -0800323void RenderEngine::fillRegionWithColor(const Region& region, uint32_t height, float red,
324 float green, float blue, float alpha) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700325 size_t c;
326 Rect const* r = region.getArray(&c);
Chia-I Wub027f802017-11-29 14:00:52 -0800327 Mesh mesh(Mesh::TRIANGLES, c * 6, 2);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700328 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Chia-I Wub027f802017-11-29 14:00:52 -0800329 for (size_t i = 0; i < c; i++, r++) {
330 position[i * 6 + 0].x = r->left;
331 position[i * 6 + 0].y = height - r->top;
332 position[i * 6 + 1].x = r->left;
333 position[i * 6 + 1].y = height - r->bottom;
334 position[i * 6 + 2].x = r->right;
335 position[i * 6 + 2].y = height - r->bottom;
336 position[i * 6 + 3].x = r->left;
337 position[i * 6 + 3].y = height - r->top;
338 position[i * 6 + 4].x = r->right;
339 position[i * 6 + 4].y = height - r->bottom;
340 position[i * 6 + 5].x = r->right;
341 position[i * 6 + 5].y = height - r->top;
Mathias Agopian3f844832013-08-07 21:24:32 -0700342 }
Mathias Agopian19733a32013-08-28 18:13:56 -0700343 setupFillWithColor(red, green, blue, alpha);
344 drawMesh(mesh);
Mathias Agopian3f844832013-08-07 21:24:32 -0700345}
346
347void RenderEngine::clearWithColor(float red, float green, float blue, float alpha) {
348 glClearColor(red, green, blue, alpha);
349 glClear(GL_COLOR_BUFFER_BIT);
350}
351
Chia-I Wub027f802017-11-29 14:00:52 -0800352void RenderEngine::setScissor(uint32_t left, uint32_t bottom, uint32_t right, uint32_t top) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700353 glScissor(left, bottom, right, top);
354 glEnable(GL_SCISSOR_TEST);
355}
356
357void RenderEngine::disableScissor() {
358 glDisable(GL_SCISSOR_TEST);
359}
360
361void RenderEngine::genTextures(size_t count, uint32_t* names) {
362 glGenTextures(count, names);
363}
364
365void RenderEngine::deleteTextures(size_t count, uint32_t const* names) {
366 glDeleteTextures(count, names);
367}
368
Lloyd Pique144e1162017-12-20 16:44:52 -0800369void RenderEngine::bindExternalTextureImage(uint32_t texName, const android::RE::Image& image) {
370 // Note: RE::Image is an abstract interface. This implementation only ever
371 // creates RE::impl::Image's, so it is safe to just cast to the actual type.
372 return bindExternalTextureImage(texName, static_cast<const android::RE::impl::Image&>(image));
373}
374
375void RenderEngine::bindExternalTextureImage(uint32_t texName,
376 const android::RE::impl::Image& image) {
Chia-I Wu401ef832017-12-01 10:52:22 -0800377 const GLenum target = GL_TEXTURE_EXTERNAL_OES;
378
379 glBindTexture(target, texName);
380 if (image.getEGLImage() != EGL_NO_IMAGE_KHR) {
381 glEGLImageTargetTexture2DOES(target, static_cast<GLeglImageOES>(image.getEGLImage()));
382 }
383}
384
Mathias Agopiand5556842013-09-19 17:08:37 -0700385void RenderEngine::readPixels(size_t l, size_t b, size_t w, size_t h, uint32_t* pixels) {
386 glReadPixels(l, b, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
387}
388
Mathias Agopian458197d2013-08-15 14:56:51 -0700389void RenderEngine::dump(String8& result) {
Peiyong Lin566a3b42018-01-09 18:22:43 -0800390 const GLExtensions& extensions = GLExtensions::getInstance();
Chia-I Wu767d7c92017-11-30 13:22:48 -0800391
392 result.appendFormat("EGL implementation : %s\n", extensions.getEGLVersion());
393 result.appendFormat("%s\n", extensions.getEGLExtensions());
394
Chia-I Wub027f802017-11-29 14:00:52 -0800395 result.appendFormat("GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
396 extensions.getVersion());
Chia-I Wu767d7c92017-11-30 13:22:48 -0800397 result.appendFormat("%s\n", extensions.getExtensions());
Mathias Agopian458197d2013-08-15 14:56:51 -0700398}
399
Mathias Agopian3f844832013-08-07 21:24:32 -0700400// ---------------------------------------------------------------------------
401
Lloyd Pique144e1162017-12-20 16:44:52 -0800402void RenderEngine::bindNativeBufferAsFrameBuffer(ANativeWindowBuffer* buffer,
403 RE::BindNativeBufferAsFramebuffer* bindHelper) {
404 bindHelper->mImage = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
405 buffer, nullptr);
406 if (bindHelper->mImage == EGL_NO_IMAGE_KHR) {
407 bindHelper->mStatus = NO_MEMORY;
Chia-I Wueadbaa62017-11-09 11:26:15 -0800408 return;
409 }
410
Lloyd Pique144e1162017-12-20 16:44:52 -0800411 uint32_t glStatus;
412 bindImageAsFramebuffer(bindHelper->mImage, &bindHelper->mTexName, &bindHelper->mFbName,
413 &glStatus);
Mathias Agopian458197d2013-08-15 14:56:51 -0700414
Lloyd Pique144e1162017-12-20 16:44:52 -0800415 ALOGE_IF(glStatus != GL_FRAMEBUFFER_COMPLETE_OES, "glCheckFramebufferStatusOES error %d",
416 glStatus);
417
418 bindHelper->mStatus = glStatus == GL_FRAMEBUFFER_COMPLETE_OES ? NO_ERROR : BAD_VALUE;
Mathias Agopian3f844832013-08-07 21:24:32 -0700419}
420
Lloyd Pique144e1162017-12-20 16:44:52 -0800421void RenderEngine::unbindNativeBufferAsFrameBuffer(RE::BindNativeBufferAsFramebuffer* bindHelper) {
422 if (bindHelper->mImage == EGL_NO_IMAGE_KHR) {
Chia-I Wueadbaa62017-11-09 11:26:15 -0800423 return;
424 }
425
Mathias Agopian3f844832013-08-07 21:24:32 -0700426 // back to main framebuffer
Lloyd Pique144e1162017-12-20 16:44:52 -0800427 unbindFramebuffer(bindHelper->mTexName, bindHelper->mFbName);
428 eglDestroyImageKHR(mEGLDisplay, bindHelper->mImage);
Dan Stozaa277ccb2018-05-31 15:28:00 -0700429
430 // Workaround for b/77935566 to force the EGL driver to release the
431 // screenshot buffer
432 setScissor(0, 0, 0, 0);
433 clearWithColor(0.0, 0.0, 0.0, 0.0);
434 disableScissor();
Mathias Agopian3f844832013-08-07 21:24:32 -0700435}
436
Mathias Agopian875d8e12013-06-07 15:35:48 -0700437// ---------------------------------------------------------------------------
Jesse Hall05f8c702013-12-23 20:44:38 -0800438
Chia-I Wub027f802017-11-29 14:00:52 -0800439static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute,
440 EGLint wanted, EGLConfig* outConfig) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800441 EGLint numConfigs = -1, n = 0;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800442 eglGetConfigs(dpy, nullptr, 0, &numConfigs);
Jesse Hall05f8c702013-12-23 20:44:38 -0800443 EGLConfig* const configs = new EGLConfig[numConfigs];
444 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
445
446 if (n) {
447 if (attribute != EGL_NONE) {
Chia-I Wub027f802017-11-29 14:00:52 -0800448 for (int i = 0; i < n; i++) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800449 EGLint value = 0;
450 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
451 if (wanted == value) {
452 *outConfig = configs[i];
Chia-I Wub027f802017-11-29 14:00:52 -0800453 delete[] configs;
Jesse Hall05f8c702013-12-23 20:44:38 -0800454 return NO_ERROR;
455 }
456 }
457 } else {
458 // just pick the first one
459 *outConfig = configs[0];
Chia-I Wub027f802017-11-29 14:00:52 -0800460 delete[] configs;
Jesse Hall05f8c702013-12-23 20:44:38 -0800461 return NO_ERROR;
462 }
463 }
Chia-I Wub027f802017-11-29 14:00:52 -0800464 delete[] configs;
Jesse Hall05f8c702013-12-23 20:44:38 -0800465 return NAME_NOT_FOUND;
466}
467
468class EGLAttributeVector {
469 struct Attribute;
470 class Adder;
471 friend class Adder;
472 KeyedVector<Attribute, EGLint> mList;
473 struct Attribute {
Chia-I Wub027f802017-11-29 14:00:52 -0800474 Attribute() : v(0){};
475 explicit Attribute(EGLint v) : v(v) {}
Jesse Hall05f8c702013-12-23 20:44:38 -0800476 EGLint v;
Chia-I Wub027f802017-11-29 14:00:52 -0800477 bool operator<(const Attribute& other) const {
Jesse Hall05f8c702013-12-23 20:44:38 -0800478 // this places EGL_NONE at the end
479 EGLint lhs(v);
480 EGLint rhs(other.v);
481 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
482 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
483 return lhs < rhs;
484 }
485 };
486 class Adder {
487 friend class EGLAttributeVector;
488 EGLAttributeVector& v;
489 EGLint attribute;
Chia-I Wub027f802017-11-29 14:00:52 -0800490 Adder(EGLAttributeVector& v, EGLint attribute) : v(v), attribute(attribute) {}
491
Jesse Hall05f8c702013-12-23 20:44:38 -0800492 public:
Chia-I Wub027f802017-11-29 14:00:52 -0800493 void operator=(EGLint value) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800494 if (attribute != EGL_NONE) {
Chih-Hung Hsiehc4067912016-05-03 14:03:27 -0700495 v.mList.add(Attribute(attribute), value);
Jesse Hall05f8c702013-12-23 20:44:38 -0800496 }
497 }
Chia-I Wub027f802017-11-29 14:00:52 -0800498 operator EGLint() const { return v.mList[attribute]; }
Jesse Hall05f8c702013-12-23 20:44:38 -0800499 };
Chia-I Wub027f802017-11-29 14:00:52 -0800500
Jesse Hall05f8c702013-12-23 20:44:38 -0800501public:
Chia-I Wub027f802017-11-29 14:00:52 -0800502 EGLAttributeVector() { mList.add(Attribute(EGL_NONE), EGL_NONE); }
Jesse Hall05f8c702013-12-23 20:44:38 -0800503 void remove(EGLint attribute) {
504 if (attribute != EGL_NONE) {
Chih-Hung Hsiehc4067912016-05-03 14:03:27 -0700505 mList.removeItem(Attribute(attribute));
Jesse Hall05f8c702013-12-23 20:44:38 -0800506 }
507 }
Chia-I Wub027f802017-11-29 14:00:52 -0800508 Adder operator[](EGLint attribute) { return Adder(*this, attribute); }
509 EGLint operator[](EGLint attribute) const { return mList[attribute]; }
Jesse Hall05f8c702013-12-23 20:44:38 -0800510 // cast-operator to (EGLint const*)
Chia-I Wub027f802017-11-29 14:00:52 -0800511 operator EGLint const*() const { return &mList.keyAt(0).v; }
Jesse Hall05f8c702013-12-23 20:44:38 -0800512};
513
Chia-I Wub027f802017-11-29 14:00:52 -0800514static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType,
515 EGLConfig* config) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800516 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
517 // it is to be used with WIFI displays
518 status_t err;
519 EGLint wantedAttribute;
520 EGLint wantedAttributeValue;
521
522 EGLAttributeVector attribs;
523 if (renderableType) {
Chia-I Wub027f802017-11-29 14:00:52 -0800524 attribs[EGL_RENDERABLE_TYPE] = renderableType;
525 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
526 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
Jesse Hall05f8c702013-12-23 20:44:38 -0800527 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
Chia-I Wub027f802017-11-29 14:00:52 -0800528 attribs[EGL_RED_SIZE] = 8;
529 attribs[EGL_GREEN_SIZE] = 8;
530 attribs[EGL_BLUE_SIZE] = 8;
531 attribs[EGL_ALPHA_SIZE] = 8;
532 wantedAttribute = EGL_NONE;
533 wantedAttributeValue = EGL_NONE;
Jesse Hall05f8c702013-12-23 20:44:38 -0800534 } else {
535 // if no renderable type specified, fallback to a simplified query
Chia-I Wub027f802017-11-29 14:00:52 -0800536 wantedAttribute = EGL_NATIVE_VISUAL_ID;
537 wantedAttributeValue = format;
Jesse Hall05f8c702013-12-23 20:44:38 -0800538 }
539
Chia-I Wub027f802017-11-29 14:00:52 -0800540 err = selectConfigForAttribute(display, attribs, wantedAttribute, wantedAttributeValue, config);
Jesse Hall05f8c702013-12-23 20:44:38 -0800541 if (err == NO_ERROR) {
542 EGLint caveat;
543 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
544 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
545 }
546
547 return err;
548}
549
Chia-I Wub027f802017-11-29 14:00:52 -0800550EGLConfig RenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800551 status_t err;
552 EGLConfig config;
553
554 // First try to get an ES2 config
555 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
556 if (err != NO_ERROR) {
557 // If ES2 fails, try ES1
558 err = selectEGLConfig(display, format, EGL_OPENGL_ES_BIT, &config);
559 if (err != NO_ERROR) {
560 // still didn't work, probably because we're on the emulator...
561 // try a simplified query
562 ALOGW("no suitable EGLConfig found, trying a simpler query");
563 err = selectEGLConfig(display, format, 0, &config);
564 if (err != NO_ERROR) {
565 // this EGL is too lame for android
566 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
567 }
568 }
569 }
570
Steven Thomasd7f49c52017-07-26 18:48:28 -0700571 if (logConfig) {
572 // print some debugging info
Chia-I Wub027f802017-11-29 14:00:52 -0800573 EGLint r, g, b, a;
574 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
Steven Thomasd7f49c52017-07-26 18:48:28 -0700575 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
Chia-I Wub027f802017-11-29 14:00:52 -0800576 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
Steven Thomasd7f49c52017-07-26 18:48:28 -0700577 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
578 ALOGI("EGL information:");
579 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
580 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
581 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
Chia-I Wub027f802017-11-29 14:00:52 -0800582 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
Steven Thomasd7f49c52017-07-26 18:48:28 -0700583 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
584 }
Jesse Hall05f8c702013-12-23 20:44:38 -0800585
586 return config;
587}
588
Dan Stoza4e637772016-07-28 13:31:51 -0700589void RenderEngine::primeCache() const {
Chia-I Wu93e14df2018-06-04 10:10:17 -0700590 ProgramCache::getInstance().primeCache(mFeatureFlags & WIDE_COLOR_SUPPORT);
Dan Stoza4e637772016-07-28 13:31:51 -0700591}
592
Jesse Hall05f8c702013-12-23 20:44:38 -0800593// ---------------------------------------------------------------------------
Lloyd Pique144e1162017-12-20 16:44:52 -0800594
595} // namespace impl
596} // namespace RE
597} // namespace android