blob: 8230dfd44f9a80fc89ac7b2ab34a3f5025477063 [file] [log] [blame]
John Reck3b202512014-06-23 13:13:08 -07001/*
2 * Copyright (C) 2014 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
John Reck3b202512014-06-23 13:13:08 -070017#include "EglManager.h"
18
Mark Salyzyn96bf5982016-09-28 16:15:30 -070019#include <cutils/properties.h>
20#include <log/log.h>
Stan Iliev564ca3e2018-09-04 22:00:00 +000021#include <private/gui/SyncFeatures.h>
John Reck1e510712018-04-23 08:15:03 -070022#include <utils/Trace.h>
Peiyong Lin1f6aa122018-09-10 16:28:08 -070023#include "utils/Color.h"
John Reck1bcacfd2017-11-03 10:12:19 -070024#include "utils/StringUtils.h"
Mark Salyzyn96bf5982016-09-28 16:15:30 -070025
Greg Danielcd558522016-11-17 13:31:40 -050026#include "Frame.h"
John Reckd04794a2015-05-08 10:04:36 -070027#include "Properties.h"
Mark Salyzyn173215d2017-01-12 08:27:11 -080028
John Reck55156372015-01-21 07:46:37 -080029#include <EGL/eglext.h>
John Reckbdc9f1b2018-09-14 15:22:35 -070030#include <GLES/gl.h>
John Reck149173d2015-08-10 09:52:29 -070031
John Reck1e510712018-04-23 08:15:03 -070032#include <string>
33#include <vector>
John Reck650bd9a2018-11-26 09:52:20 -080034#include <system/window.h>
35#include <gui/Surface.h>
Derek Sollenberger7e044fe2016-11-07 10:57:59 -050036
John Reck3b202512014-06-23 13:13:08 -070037#define GLES_VERSION 2
38
39// Android-specific addition that is used to show when frames began in systrace
40EGLAPI void EGLAPIENTRY eglBeginFrame(EGLDisplay dpy, EGLSurface surface);
41
42namespace android {
43namespace uirenderer {
44namespace renderthread {
45
John Reck1bcacfd2017-11-03 10:12:19 -070046#define ERROR_CASE(x) \
47 case x: \
48 return #x;
John Reck3b202512014-06-23 13:13:08 -070049static const char* egl_error_str(EGLint error) {
50 switch (error) {
51 ERROR_CASE(EGL_SUCCESS)
52 ERROR_CASE(EGL_NOT_INITIALIZED)
53 ERROR_CASE(EGL_BAD_ACCESS)
54 ERROR_CASE(EGL_BAD_ALLOC)
55 ERROR_CASE(EGL_BAD_ATTRIBUTE)
56 ERROR_CASE(EGL_BAD_CONFIG)
57 ERROR_CASE(EGL_BAD_CONTEXT)
58 ERROR_CASE(EGL_BAD_CURRENT_SURFACE)
59 ERROR_CASE(EGL_BAD_DISPLAY)
60 ERROR_CASE(EGL_BAD_MATCH)
61 ERROR_CASE(EGL_BAD_NATIVE_PIXMAP)
62 ERROR_CASE(EGL_BAD_NATIVE_WINDOW)
63 ERROR_CASE(EGL_BAD_PARAMETER)
64 ERROR_CASE(EGL_BAD_SURFACE)
65 ERROR_CASE(EGL_CONTEXT_LOST)
John Reck1bcacfd2017-11-03 10:12:19 -070066 default:
67 return "Unknown error";
John Reck3b202512014-06-23 13:13:08 -070068 }
69}
sergeyv694d4992016-10-27 10:23:13 -070070const char* EglManager::eglErrorString() {
John Reck3b202512014-06-23 13:13:08 -070071 return egl_error_str(eglGetError());
72}
73
John Reck149173d2015-08-10 09:52:29 -070074static struct {
75 bool bufferAge = false;
76 bool setDamage = false;
Romain Guy26a2b972017-04-17 09:39:51 -070077 bool noConfigContext = false;
78 bool pixelFormatFloat = false;
79 bool glColorSpace = false;
Romain Guy26b6a642017-07-11 09:48:28 -070080 bool scRGB = false;
Peiyong Lin1f6aa122018-09-10 16:28:08 -070081 bool displayP3 = false;
Jorim Jaggi767e25e2018-04-04 23:07:35 +020082 bool contextPriority = false;
John Reck1e510712018-04-23 08:15:03 -070083 bool surfacelessContext = false;
John Reck149173d2015-08-10 09:52:29 -070084} EglExtensions;
85
John Reck1e510712018-04-23 08:15:03 -070086EglManager::EglManager()
87 : mEglDisplay(EGL_NO_DISPLAY)
Chris Craikd41c4d82015-01-05 15:51:13 -080088 , mEglConfig(nullptr)
Romain Guy26a2b972017-04-17 09:39:51 -070089 , mEglConfigWideGamut(nullptr)
John Reck3b202512014-06-23 13:13:08 -070090 , mEglContext(EGL_NO_CONTEXT)
91 , mPBufferSurface(EGL_NO_SURFACE)
John Reck1bcacfd2017-11-03 10:12:19 -070092 , mCurrentSurface(EGL_NO_SURFACE) {}
John Reck3b202512014-06-23 13:13:08 -070093
John Reck1e510712018-04-23 08:15:03 -070094EglManager::~EglManager() {
95 destroy();
96}
97
John Reck3b202512014-06-23 13:13:08 -070098void EglManager::initialize() {
99 if (hasEglContext()) return;
100
John Reckfbc8df02014-11-14 16:18:41 -0800101 ATRACE_NAME("Creating EGLContext");
102
John Reck3b202512014-06-23 13:13:08 -0700103 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
John Reck1bcacfd2017-11-03 10:12:19 -0700104 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, "Failed to get EGL_DEFAULT_DISPLAY! err=%s",
105 eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700106
107 EGLint major, minor;
108 LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE,
John Reck1bcacfd2017-11-03 10:12:19 -0700109 "Failed to initialize display %p! err=%s", mEglDisplay, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700110
John Reck650bd9a2018-11-26 09:52:20 -0800111 ALOGV("Initialized EGL, version %d.%d", (int)major, (int)minor);
John Reck3b202512014-06-23 13:13:08 -0700112
John Reck149173d2015-08-10 09:52:29 -0700113 initExtensions();
Season Li13d1b4a2015-07-29 17:16:19 -0700114
John Reck149173d2015-08-10 09:52:29 -0700115 // Now that extensions are loaded, pick a swap behavior
116 if (Properties::enablePartialUpdates) {
Matt Sarettb77c94a2016-12-07 12:22:37 -0500117 // An Adreno driver bug is causing rendering problems for SkiaGL with
118 // buffer age swap behavior (b/31957043). To temporarily workaround,
119 // we will use preserved swap behavior.
Derek Sollenbergerb5a12bd2017-06-14 15:23:19 -0400120 if (Properties::useBufferAge && EglExtensions.bufferAge) {
John Reck149173d2015-08-10 09:52:29 -0700121 mSwapBehavior = SwapBehavior::BufferAge;
122 } else {
123 mSwapBehavior = SwapBehavior::Preserved;
124 }
125 }
126
Romain Guy26a2b972017-04-17 09:39:51 -0700127 loadConfigs();
John Reck3b202512014-06-23 13:13:08 -0700128 createContext();
John Reckd7db4d72015-05-20 07:18:55 -0700129 createPBufferSurface();
John Reck1e510712018-04-23 08:15:03 -0700130 makeCurrent(mPBufferSurface, nullptr, /* force */ true);
John Reck3b202512014-06-23 13:13:08 -0700131}
132
John Reck149173d2015-08-10 09:52:29 -0700133void EglManager::initExtensions() {
John Reck1bcacfd2017-11-03 10:12:19 -0700134 auto extensions = StringUtils::split(eglQueryString(mEglDisplay, EGL_EXTENSIONS));
Romain Guy26a2b972017-04-17 09:39:51 -0700135
John Reck8733bff2016-09-27 14:45:28 -0700136 // For our purposes we don't care if EGL_BUFFER_AGE is a result of
137 // EGL_EXT_buffer_age or EGL_KHR_partial_update as our usage is covered
138 // under EGL_KHR_partial_update and we don't need the expanded scope
139 // that EGL_EXT_buffer_age provides.
John Reck1bcacfd2017-11-03 10:12:19 -0700140 EglExtensions.bufferAge =
141 extensions.has("EGL_EXT_buffer_age") || extensions.has("EGL_KHR_partial_update");
Chris Craik6e6646c2015-09-14 15:54:12 -0700142 EglExtensions.setDamage = extensions.has("EGL_KHR_partial_update");
John Reck708b6682015-10-22 13:11:00 -0700143 LOG_ALWAYS_FATAL_IF(!extensions.has("EGL_KHR_swap_buffers_with_damage"),
John Reck1bcacfd2017-11-03 10:12:19 -0700144 "Missing required extension EGL_KHR_swap_buffers_with_damage");
Romain Guy26a2b972017-04-17 09:39:51 -0700145
146 EglExtensions.glColorSpace = extensions.has("EGL_KHR_gl_colorspace");
147 EglExtensions.noConfigContext = extensions.has("EGL_KHR_no_config_context");
148 EglExtensions.pixelFormatFloat = extensions.has("EGL_EXT_pixel_format_float");
Romain Guy26b6a642017-07-11 09:48:28 -0700149#ifdef ANDROID_ENABLE_LINEAR_BLENDING
150 EglExtensions.scRGB = extensions.has("EGL_EXT_gl_colorspace_scrgb_linear");
151#else
152 EglExtensions.scRGB = extensions.has("EGL_EXT_gl_colorspace_scrgb");
153#endif
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700154 EglExtensions.displayP3 = extensions.has("EGL_EXT_gl_colorspace_display_p3");
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200155 EglExtensions.contextPriority = extensions.has("EGL_IMG_context_priority");
John Reck1e510712018-04-23 08:15:03 -0700156 EglExtensions.surfacelessContext = extensions.has("EGL_KHR_surfaceless_context");
John Reck149173d2015-08-10 09:52:29 -0700157}
158
John Reck3b202512014-06-23 13:13:08 -0700159bool EglManager::hasEglContext() {
160 return mEglDisplay != EGL_NO_DISPLAY;
161}
162
Romain Guy26a2b972017-04-17 09:39:51 -0700163void EglManager::loadConfigs() {
John Reck149173d2015-08-10 09:52:29 -0700164 ALOGD("Swap behavior %d", static_cast<int>(mSwapBehavior));
John Reck1bcacfd2017-11-03 10:12:19 -0700165 EGLint swapBehavior =
166 (mSwapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700167
168 // Note: The default pixel format is RGBA_8888, when other formats are
169 // available, we should check the target pixel format and configure the
170 // attributes list properly.
John Reck1bcacfd2017-11-03 10:12:19 -0700171 EGLint attribs[] = {EGL_RENDERABLE_TYPE,
172 EGL_OPENGL_ES2_BIT,
173 EGL_RED_SIZE,
174 8,
175 EGL_GREEN_SIZE,
176 8,
177 EGL_BLUE_SIZE,
178 8,
179 EGL_ALPHA_SIZE,
180 8,
181 EGL_DEPTH_SIZE,
182 0,
183 EGL_CONFIG_CAVEAT,
184 EGL_NONE,
185 EGL_STENCIL_SIZE,
John Reck1e510712018-04-23 08:15:03 -0700186 STENCIL_BUFFER_SIZE,
John Reck1bcacfd2017-11-03 10:12:19 -0700187 EGL_SURFACE_TYPE,
188 EGL_WINDOW_BIT | swapBehavior,
189 EGL_NONE};
John Reck3b202512014-06-23 13:13:08 -0700190
Romain Guy26a2b972017-04-17 09:39:51 -0700191 EGLint numConfigs = 1;
John Reck1bcacfd2017-11-03 10:12:19 -0700192 if (!eglChooseConfig(mEglDisplay, attribs, &mEglConfig, numConfigs, &numConfigs) ||
193 numConfigs != 1) {
John Reck149173d2015-08-10 09:52:29 -0700194 if (mSwapBehavior == SwapBehavior::Preserved) {
John Reck3b202512014-06-23 13:13:08 -0700195 // Try again without dirty regions enabled
John Reck149173d2015-08-10 09:52:29 -0700196 ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...");
197 mSwapBehavior = SwapBehavior::Discard;
Romain Guy26a2b972017-04-17 09:39:51 -0700198 loadConfigs();
John Reck1bcacfd2017-11-03 10:12:19 -0700199 return; // the call to loadConfigs() we just made picks the wide gamut config
John Reck3b202512014-06-23 13:13:08 -0700200 } else {
John Reck149173d2015-08-10 09:52:29 -0700201 // Failed to get a valid config
sergeyv694d4992016-10-27 10:23:13 -0700202 LOG_ALWAYS_FATAL("Failed to choose config, error = %s", eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700203 }
204 }
Romain Guy26a2b972017-04-17 09:39:51 -0700205
206 if (EglExtensions.pixelFormatFloat) {
207 // If we reached this point, we have a valid swap behavior
John Reck1bcacfd2017-11-03 10:12:19 -0700208 EGLint attribs16F[] = {EGL_RENDERABLE_TYPE,
209 EGL_OPENGL_ES2_BIT,
210 EGL_COLOR_COMPONENT_TYPE_EXT,
211 EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT,
212 EGL_RED_SIZE,
213 16,
214 EGL_GREEN_SIZE,
215 16,
216 EGL_BLUE_SIZE,
217 16,
218 EGL_ALPHA_SIZE,
219 16,
220 EGL_DEPTH_SIZE,
221 0,
222 EGL_STENCIL_SIZE,
John Reck1e510712018-04-23 08:15:03 -0700223 STENCIL_BUFFER_SIZE,
John Reck1bcacfd2017-11-03 10:12:19 -0700224 EGL_SURFACE_TYPE,
225 EGL_WINDOW_BIT | swapBehavior,
226 EGL_NONE};
Romain Guy26a2b972017-04-17 09:39:51 -0700227
228 numConfigs = 1;
John Reck1bcacfd2017-11-03 10:12:19 -0700229 if (!eglChooseConfig(mEglDisplay, attribs16F, &mEglConfigWideGamut, numConfigs,
230 &numConfigs) ||
231 numConfigs != 1) {
Rob Herring74883ab2017-11-29 09:26:31 -0600232 ALOGE("Device claims wide gamut support, cannot find matching config, error = %s",
John Reck1a4a9812018-04-18 16:13:31 -0700233 eglErrorString());
Rob Herring74883ab2017-11-29 09:26:31 -0600234 EglExtensions.pixelFormatFloat = false;
Romain Guy26a2b972017-04-17 09:39:51 -0700235 }
236 }
John Reck3b202512014-06-23 13:13:08 -0700237}
238
239void EglManager::createContext() {
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200240 std::vector<EGLint> contextAttributes;
241 contextAttributes.reserve(5);
242 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
243 contextAttributes.push_back(GLES_VERSION);
244 if (Properties::contextPriority != 0 && EglExtensions.contextPriority) {
245 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
246 contextAttributes.push_back(Properties::contextPriority);
247 }
248 contextAttributes.push_back(EGL_NONE);
John Reck1bcacfd2017-11-03 10:12:19 -0700249 mEglContext = eglCreateContext(
250 mEglDisplay, EglExtensions.noConfigContext ? ((EGLConfig) nullptr) : mEglConfig,
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200251 EGL_NO_CONTEXT, contextAttributes.data());
John Reck1bcacfd2017-11-03 10:12:19 -0700252 LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, "Failed to create context, error = %s",
253 eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700254}
255
John Reckd7db4d72015-05-20 07:18:55 -0700256void EglManager::createPBufferSurface() {
John Reck3b202512014-06-23 13:13:08 -0700257 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
John Reck1bcacfd2017-11-03 10:12:19 -0700258 "usePBufferSurface() called on uninitialized GlobalContext!");
John Reck3b202512014-06-23 13:13:08 -0700259
John Reck1e510712018-04-23 08:15:03 -0700260 if (mPBufferSurface == EGL_NO_SURFACE && !EglExtensions.surfacelessContext) {
John Reck1bcacfd2017-11-03 10:12:19 -0700261 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
John Reck3b202512014-06-23 13:13:08 -0700262 mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs);
263 }
John Reck3b202512014-06-23 13:13:08 -0700264}
265
John Reck8e539ca2018-11-15 15:21:29 -0800266Result<EGLSurface, EGLint> EglManager::createSurface(EGLNativeWindowType window, ColorMode colorMode) {
John Reck1e510712018-04-23 08:15:03 -0700267 LOG_ALWAYS_FATAL_IF(!hasEglContext(), "Not initialized");
Romain Guy253f2c22016-09-28 17:34:42 -0700268
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700269 bool wideColorGamut = colorMode == ColorMode::WideColorGamut && EglExtensions.glColorSpace &&
270 EglExtensions.scRGB && EglExtensions.pixelFormatFloat &&
271 EglExtensions.noConfigContext;
Romain Guy26a2b972017-04-17 09:39:51 -0700272
273 // The color space we want to use depends on whether linear blending is turned
274 // on and whether the app has requested wide color gamut rendering. When wide
275 // color gamut rendering is off, the app simply renders in the display's native
276 // color gamut.
277 //
278 // When wide gamut rendering is off:
279 // - Blending is done by default in gamma space, which requires using a
280 // linear EGL color space (the GPU uses the color values as is)
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700281 // - If linear blending is on, we must use the non-linear EGL color space
282 // (the GPU will perform sRGB to linear and linear to SRGB conversions
283 // before and after blending)
Romain Guy26a2b972017-04-17 09:39:51 -0700284 //
285 // When wide gamut rendering is on we cannot rely on the GPU performing
286 // linear blending for us. We use two different color spaces to tag the
287 // surface appropriately for SurfaceFlinger:
288 // - Gamma blending (default) requires the use of the scRGB-nl color space
289 // - Linear blending requires the use of the scRGB color space
290
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700291 // Not all Android targets support the EGL_GL_COLORSPACE_KHR extension
Romain Guy26a2b972017-04-17 09:39:51 -0700292 // We insert to placeholders to set EGL_GL_COLORSPACE_KHR and its value.
293 // According to section 3.4.1 of the EGL specification, the attributes
294 // list is considered empty if the first entry is EGL_NONE
John Reck1bcacfd2017-11-03 10:12:19 -0700295 EGLint attribs[] = {EGL_NONE, EGL_NONE, EGL_NONE};
Romain Guy253f2c22016-09-28 17:34:42 -0700296
Romain Guy26a2b972017-04-17 09:39:51 -0700297 if (EglExtensions.glColorSpace) {
298 attribs[0] = EGL_GL_COLORSPACE_KHR;
299#ifdef ANDROID_ENABLE_LINEAR_BLENDING
300 if (wideColorGamut) {
301 attribs[1] = EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT;
302 } else {
Peiyong Lin189021b2018-09-27 16:41:40 -0700303 attribs[1] = EGL_GL_COLORSPACE_SRGB_KHR;
Romain Guy26a2b972017-04-17 09:39:51 -0700304 }
305#else
306 if (wideColorGamut) {
Romain Guy26b6a642017-07-11 09:48:28 -0700307 attribs[1] = EGL_GL_COLORSPACE_SCRGB_EXT;
Romain Guy26a2b972017-04-17 09:39:51 -0700308 } else {
Peiyong Lin189021b2018-09-27 16:41:40 -0700309 attribs[1] = EGL_GL_COLORSPACE_LINEAR_KHR;
Romain Guy26a2b972017-04-17 09:39:51 -0700310 }
311#endif
312 }
313
John Reck1bcacfd2017-11-03 10:12:19 -0700314 EGLSurface surface = eglCreateWindowSurface(
315 mEglDisplay, wideColorGamut ? mEglConfigWideGamut : mEglConfig, window, attribs);
John Reck8e539ca2018-11-15 15:21:29 -0800316 if (surface == EGL_NO_SURFACE) {
317 return Error<EGLint> { eglGetError() };
318 }
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000319
320 if (mSwapBehavior != SwapBehavior::Preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700321 LOG_ALWAYS_FATAL_IF(eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
322 EGL_BUFFER_DESTROYED) == EGL_FALSE,
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000323 "Failed to set swap behavior to destroyed for window %p, eglErr = %s",
John Reck1bcacfd2017-11-03 10:12:19 -0700324 (void*)window, eglErrorString());
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000325 }
326
John Reck3b202512014-06-23 13:13:08 -0700327 return surface;
328}
329
330void EglManager::destroySurface(EGLSurface surface) {
331 if (isCurrent(surface)) {
332 makeCurrent(EGL_NO_SURFACE);
333 }
334 if (!eglDestroySurface(mEglDisplay, surface)) {
sergeyv694d4992016-10-27 10:23:13 -0700335 ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700336 }
337}
338
339void EglManager::destroy() {
340 if (mEglDisplay == EGL_NO_DISPLAY) return;
341
John Reck3b202512014-06-23 13:13:08 -0700342 eglDestroyContext(mEglDisplay, mEglContext);
John Reck1e510712018-04-23 08:15:03 -0700343 if (mPBufferSurface != EGL_NO_SURFACE) {
344 eglDestroySurface(mEglDisplay, mPBufferSurface);
345 }
John Reck3b202512014-06-23 13:13:08 -0700346 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
347 eglTerminate(mEglDisplay);
348 eglReleaseThread();
349
350 mEglDisplay = EGL_NO_DISPLAY;
351 mEglContext = EGL_NO_CONTEXT;
352 mPBufferSurface = EGL_NO_SURFACE;
353 mCurrentSurface = EGL_NO_SURFACE;
354}
355
John Reck1e510712018-04-23 08:15:03 -0700356bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut, bool force) {
357 if (!force && isCurrent(surface)) return false;
John Reck3b202512014-06-23 13:13:08 -0700358
359 if (surface == EGL_NO_SURFACE) {
John Reckd7db4d72015-05-20 07:18:55 -0700360 // Ensure we always have a valid surface & context
361 surface = mPBufferSurface;
362 }
363 if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) {
John Reckf2dcc2a2015-07-16 09:17:59 -0700364 if (errOut) {
365 *errOut = eglGetError();
John Reck1bcacfd2017-11-03 10:12:19 -0700366 ALOGW("Failed to make current on surface %p, error=%s", (void*)surface,
367 egl_error_str(*errOut));
John Reckf2dcc2a2015-07-16 09:17:59 -0700368 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700369 LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", (void*)surface,
370 eglErrorString());
John Reckf2dcc2a2015-07-16 09:17:59 -0700371 }
John Reck3b202512014-06-23 13:13:08 -0700372 }
373 mCurrentSurface = surface;
John Recka8963062017-06-14 10:47:50 -0700374 if (Properties::disableVsync) {
375 eglSwapInterval(mEglDisplay, 0);
376 }
John Reck3b202512014-06-23 13:13:08 -0700377 return true;
378}
379
John Reck149173d2015-08-10 09:52:29 -0700380EGLint EglManager::queryBufferAge(EGLSurface surface) {
381 switch (mSwapBehavior) {
John Reck1bcacfd2017-11-03 10:12:19 -0700382 case SwapBehavior::Discard:
383 return 0;
384 case SwapBehavior::Preserved:
385 return 1;
386 case SwapBehavior::BufferAge:
387 EGLint bufferAge;
388 eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge);
389 return bufferAge;
John Reck149173d2015-08-10 09:52:29 -0700390 }
391 return 0;
392}
393
394Frame EglManager::beginFrame(EGLSurface surface) {
John Reck1bcacfd2017-11-03 10:12:19 -0700395 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, "Tried to beginFrame on EGL_NO_SURFACE!");
John Reck3b202512014-06-23 13:13:08 -0700396 makeCurrent(surface);
John Reck149173d2015-08-10 09:52:29 -0700397 Frame frame;
398 frame.mSurface = surface;
399 eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth);
400 eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight);
401 frame.mBufferAge = queryBufferAge(surface);
John Reck3b202512014-06-23 13:13:08 -0700402 eglBeginFrame(mEglDisplay, surface);
John Reck149173d2015-08-10 09:52:29 -0700403 return frame;
John Reck3b202512014-06-23 13:13:08 -0700404}
405
John Reck149173d2015-08-10 09:52:29 -0700406void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) {
407#ifdef EGL_KHR_partial_update
408 if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) {
409 EGLint rects[4];
410 frame.map(dirty, rects);
411 if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) {
412 LOG_ALWAYS_FATAL("Failed to set damage region on surface %p, error=%s",
John Reck1bcacfd2017-11-03 10:12:19 -0700413 (void*)frame.mSurface, eglErrorString());
John Reck149173d2015-08-10 09:52:29 -0700414 }
415 }
416#endif
417}
418
John Reckc96955d2016-02-26 14:56:44 -0800419bool EglManager::damageRequiresSwap() {
420 return EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge;
421}
422
John Reck149173d2015-08-10 09:52:29 -0700423bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) {
John Reck682573c2015-10-30 10:37:35 -0700424 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
John Reck55156372015-01-21 07:46:37 -0800425 ATRACE_NAME("Finishing GPU work");
426 fence();
427 }
John Reck55156372015-01-21 07:46:37 -0800428
John Recka672f6b2015-10-22 09:53:26 -0700429 EGLint rects[4];
430 frame.map(screenDirty, rects);
John Reck1bcacfd2017-11-03 10:12:19 -0700431 eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects, screenDirty.isEmpty() ? 0 : 1);
John Reckd04794a2015-05-08 10:04:36 -0700432
John Reck3b202512014-06-23 13:13:08 -0700433 EGLint err = eglGetError();
John Reck2cdbc7d2014-09-17 16:06:36 -0700434 if (CC_LIKELY(err == EGL_SUCCESS)) {
435 return true;
436 }
John Reckc2547fa2015-10-26 13:52:52 -0700437 if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) {
John Reck2cdbc7d2014-09-17 16:06:36 -0700438 // For some reason our surface was destroyed out from under us
439 // This really shouldn't happen, but if it does we can recover easily
440 // by just not trying to use the surface anymore
John Reck1bcacfd2017-11-03 10:12:19 -0700441 ALOGW("swapBuffers encountered EGL error %d on %p, halting rendering...", err,
442 frame.mSurface);
John Reck2cdbc7d2014-09-17 16:06:36 -0700443 return false;
444 }
John Reck1bcacfd2017-11-03 10:12:19 -0700445 LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering", err, egl_error_str(err));
John Reck2cdbc7d2014-09-17 16:06:36 -0700446 // Impossible to hit this, but the compiler doesn't know that
447 return false;
John Reck3b202512014-06-23 13:13:08 -0700448}
449
John Reck55156372015-01-21 07:46:37 -0800450void EglManager::fence() {
451 EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL);
John Reck1bcacfd2017-11-03 10:12:19 -0700452 eglClientWaitSyncKHR(mEglDisplay, fence, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR);
John Reck55156372015-01-21 07:46:37 -0800453 eglDestroySyncKHR(mEglDisplay, fence);
454}
455
John Reck1125d1f2014-10-23 11:02:19 -0700456bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) {
John Reck149173d2015-08-10 09:52:29 -0700457 if (mSwapBehavior != SwapBehavior::Preserved) return false;
John Reck3b202512014-06-23 13:13:08 -0700458
John Reck149173d2015-08-10 09:52:29 -0700459 bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
John Reck1bcacfd2017-11-03 10:12:19 -0700460 preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED);
John Reck149173d2015-08-10 09:52:29 -0700461 if (!preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700462 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", (void*)surface,
463 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700464 // Maybe it's already set?
465 EGLint swapBehavior;
466 if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) {
467 preserved = (swapBehavior == EGL_BUFFER_PRESERVED);
468 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700469 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", (void*)surface,
470 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700471 }
John Reck3b202512014-06-23 13:13:08 -0700472 }
John Reck1125d1f2014-10-23 11:02:19 -0700473
474 return preserved;
John Reck3b202512014-06-23 13:13:08 -0700475}
476
Stan Iliev564ca3e2018-09-04 22:00:00 +0000477status_t EglManager::fenceWait(sp<Fence>& fence) {
478 if (!hasEglContext()) {
479 ALOGE("EglManager::fenceWait: EGLDisplay not initialized");
480 return INVALID_OPERATION;
481 }
482
483 if (SyncFeatures::getInstance().useWaitSync() &&
484 SyncFeatures::getInstance().useNativeFenceSync()) {
485 // Block GPU on the fence.
486 // Create an EGLSyncKHR from the current fence.
487 int fenceFd = fence->dup();
488 if (fenceFd == -1) {
489 ALOGE("EglManager::fenceWait: error dup'ing fence fd: %d", errno);
490 return -errno;
491 }
492 EGLint attribs[] = {
493 EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd,
494 EGL_NONE
495 };
496 EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay,
497 EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
498 if (sync == EGL_NO_SYNC_KHR) {
499 close(fenceFd);
500 ALOGE("EglManager::fenceWait: error creating EGL fence: %#x", eglGetError());
501 return UNKNOWN_ERROR;
502 }
503
504 // XXX: The spec draft is inconsistent as to whether this should
505 // return an EGLint or void. Ignore the return value for now, as
506 // it's not strictly needed.
507 eglWaitSyncKHR(mEglDisplay, sync, 0);
508 EGLint eglErr = eglGetError();
509 eglDestroySyncKHR(mEglDisplay, sync);
510 if (eglErr != EGL_SUCCESS) {
511 ALOGE("EglManager::fenceWait: error waiting for EGL fence: %#x", eglErr);
512 return UNKNOWN_ERROR;
513 }
514 } else {
515 // Block CPU on the fence.
516 status_t err = fence->waitForever("EglManager::fenceWait");
517 if (err != NO_ERROR) {
518 ALOGE("EglManager::fenceWait: error waiting for fence: %d", err);
519 return err;
520 }
521 }
522 return OK;
523}
524
525status_t EglManager::createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence,
526 sp<Fence>& nativeFence) {
527 if (!hasEglContext()) {
528 ALOGE("EglManager::createReleaseFence: EGLDisplay not initialized");
529 return INVALID_OPERATION;
530 }
531
532 if (SyncFeatures::getInstance().useNativeFenceSync()) {
533 EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay,
534 EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
535 if (sync == EGL_NO_SYNC_KHR) {
536 ALOGE("EglManager::createReleaseFence: error creating EGL fence: %#x",
537 eglGetError());
538 return UNKNOWN_ERROR;
539 }
540 glFlush();
541 int fenceFd = eglDupNativeFenceFDANDROID(mEglDisplay, sync);
542 eglDestroySyncKHR(mEglDisplay, sync);
543 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
544 ALOGE("EglManager::createReleaseFence: error dup'ing native fence "
545 "fd: %#x", eglGetError());
546 return UNKNOWN_ERROR;
547 }
548 nativeFence = new Fence(fenceFd);
549 *eglFence = EGL_NO_SYNC_KHR;
550 } else if (useFenceSync && SyncFeatures::getInstance().useFenceSync()) {
551 if (*eglFence != EGL_NO_SYNC_KHR) {
552 // There is already a fence for the current slot. We need to
553 // wait on that before replacing it with another fence to
554 // ensure that all outstanding buffer accesses have completed
555 // before the producer accesses it.
556 EGLint result = eglClientWaitSyncKHR(mEglDisplay, *eglFence, 0, 1000000000);
557 if (result == EGL_FALSE) {
558 ALOGE("EglManager::createReleaseFence: error waiting for previous fence: %#x",
559 eglGetError());
560 return UNKNOWN_ERROR;
561 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
562 ALOGE("EglManager::createReleaseFence: timeout waiting for previous fence");
563 return TIMED_OUT;
564 }
565 eglDestroySyncKHR(mEglDisplay, *eglFence);
566 }
567
568 // Create a fence for the outstanding accesses in the current
569 // OpenGL ES context.
570 *eglFence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, nullptr);
571 if (*eglFence == EGL_NO_SYNC_KHR) {
572 ALOGE("EglManager::createReleaseFence: error creating fence: %#x", eglGetError());
573 return UNKNOWN_ERROR;
574 }
575 glFlush();
576 }
577 return OK;
578}
579
John Reck3b202512014-06-23 13:13:08 -0700580} /* namespace renderthread */
581} /* namespace uirenderer */
582} /* namespace android */