blob: 0685812b87f337506ca86ffa7f02fede9005adf7 [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>
John Reck1bcacfd2017-11-03 10:12:19 -070023#include "utils/StringUtils.h"
Mark Salyzyn96bf5982016-09-28 16:15:30 -070024
John Reck704bed02015-11-05 09:22:17 -080025#include "DeviceInfo.h"
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>
Derek Sollenberger7e044fe2016-11-07 10:57:59 -050034
John Reck3b202512014-06-23 13:13:08 -070035#define GLES_VERSION 2
36
37// Android-specific addition that is used to show when frames began in systrace
38EGLAPI void EGLAPIENTRY eglBeginFrame(EGLDisplay dpy, EGLSurface surface);
39
40namespace android {
41namespace uirenderer {
42namespace renderthread {
43
John Reck1bcacfd2017-11-03 10:12:19 -070044#define ERROR_CASE(x) \
45 case x: \
46 return #x;
John Reck3b202512014-06-23 13:13:08 -070047static const char* egl_error_str(EGLint error) {
48 switch (error) {
49 ERROR_CASE(EGL_SUCCESS)
50 ERROR_CASE(EGL_NOT_INITIALIZED)
51 ERROR_CASE(EGL_BAD_ACCESS)
52 ERROR_CASE(EGL_BAD_ALLOC)
53 ERROR_CASE(EGL_BAD_ATTRIBUTE)
54 ERROR_CASE(EGL_BAD_CONFIG)
55 ERROR_CASE(EGL_BAD_CONTEXT)
56 ERROR_CASE(EGL_BAD_CURRENT_SURFACE)
57 ERROR_CASE(EGL_BAD_DISPLAY)
58 ERROR_CASE(EGL_BAD_MATCH)
59 ERROR_CASE(EGL_BAD_NATIVE_PIXMAP)
60 ERROR_CASE(EGL_BAD_NATIVE_WINDOW)
61 ERROR_CASE(EGL_BAD_PARAMETER)
62 ERROR_CASE(EGL_BAD_SURFACE)
63 ERROR_CASE(EGL_CONTEXT_LOST)
John Reck1bcacfd2017-11-03 10:12:19 -070064 default:
65 return "Unknown error";
John Reck3b202512014-06-23 13:13:08 -070066 }
67}
sergeyv694d4992016-10-27 10:23:13 -070068const char* EglManager::eglErrorString() {
John Reck3b202512014-06-23 13:13:08 -070069 return egl_error_str(eglGetError());
70}
71
John Reck149173d2015-08-10 09:52:29 -070072static struct {
73 bool bufferAge = false;
74 bool setDamage = false;
Romain Guy26a2b972017-04-17 09:39:51 -070075 bool noConfigContext = false;
76 bool pixelFormatFloat = false;
77 bool glColorSpace = false;
Romain Guy26b6a642017-07-11 09:48:28 -070078 bool scRGB = false;
Jorim Jaggi767e25e2018-04-04 23:07:35 +020079 bool contextPriority = false;
John Reck1e510712018-04-23 08:15:03 -070080 bool surfacelessContext = false;
John Reck149173d2015-08-10 09:52:29 -070081} EglExtensions;
82
John Reck1e510712018-04-23 08:15:03 -070083EglManager::EglManager()
84 : mEglDisplay(EGL_NO_DISPLAY)
Chris Craikd41c4d82015-01-05 15:51:13 -080085 , mEglConfig(nullptr)
Romain Guy26a2b972017-04-17 09:39:51 -070086 , mEglConfigWideGamut(nullptr)
John Reck3b202512014-06-23 13:13:08 -070087 , mEglContext(EGL_NO_CONTEXT)
88 , mPBufferSurface(EGL_NO_SURFACE)
John Reck1bcacfd2017-11-03 10:12:19 -070089 , mCurrentSurface(EGL_NO_SURFACE) {}
John Reck3b202512014-06-23 13:13:08 -070090
John Reck1e510712018-04-23 08:15:03 -070091EglManager::~EglManager() {
92 destroy();
93}
94
John Reck3b202512014-06-23 13:13:08 -070095void EglManager::initialize() {
96 if (hasEglContext()) return;
97
John Reckfbc8df02014-11-14 16:18:41 -080098 ATRACE_NAME("Creating EGLContext");
99
John Reck3b202512014-06-23 13:13:08 -0700100 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
John Reck1bcacfd2017-11-03 10:12:19 -0700101 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, "Failed to get EGL_DEFAULT_DISPLAY! err=%s",
102 eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700103
104 EGLint major, minor;
105 LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE,
John Reck1bcacfd2017-11-03 10:12:19 -0700106 "Failed to initialize display %p! err=%s", mEglDisplay, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700107
108 ALOGI("Initialized EGL, version %d.%d", (int)major, (int)minor);
109
John Reck149173d2015-08-10 09:52:29 -0700110 initExtensions();
Season Li13d1b4a2015-07-29 17:16:19 -0700111
John Reck149173d2015-08-10 09:52:29 -0700112 // Now that extensions are loaded, pick a swap behavior
113 if (Properties::enablePartialUpdates) {
Matt Sarettb77c94a2016-12-07 12:22:37 -0500114 // An Adreno driver bug is causing rendering problems for SkiaGL with
115 // buffer age swap behavior (b/31957043). To temporarily workaround,
116 // we will use preserved swap behavior.
Derek Sollenbergerb5a12bd2017-06-14 15:23:19 -0400117 if (Properties::useBufferAge && EglExtensions.bufferAge) {
John Reck149173d2015-08-10 09:52:29 -0700118 mSwapBehavior = SwapBehavior::BufferAge;
119 } else {
120 mSwapBehavior = SwapBehavior::Preserved;
121 }
122 }
123
Romain Guy26a2b972017-04-17 09:39:51 -0700124 loadConfigs();
John Reck3b202512014-06-23 13:13:08 -0700125 createContext();
John Reckd7db4d72015-05-20 07:18:55 -0700126 createPBufferSurface();
John Reck1e510712018-04-23 08:15:03 -0700127 makeCurrent(mPBufferSurface, nullptr, /* force */ true);
John Reck704bed02015-11-05 09:22:17 -0800128 DeviceInfo::initialize();
John Reck3b202512014-06-23 13:13:08 -0700129}
130
John Reck149173d2015-08-10 09:52:29 -0700131void EglManager::initExtensions() {
John Reck1bcacfd2017-11-03 10:12:19 -0700132 auto extensions = StringUtils::split(eglQueryString(mEglDisplay, EGL_EXTENSIONS));
Romain Guy26a2b972017-04-17 09:39:51 -0700133
John Reck8733bff2016-09-27 14:45:28 -0700134 // For our purposes we don't care if EGL_BUFFER_AGE is a result of
135 // EGL_EXT_buffer_age or EGL_KHR_partial_update as our usage is covered
136 // under EGL_KHR_partial_update and we don't need the expanded scope
137 // that EGL_EXT_buffer_age provides.
John Reck1bcacfd2017-11-03 10:12:19 -0700138 EglExtensions.bufferAge =
139 extensions.has("EGL_EXT_buffer_age") || extensions.has("EGL_KHR_partial_update");
Chris Craik6e6646c2015-09-14 15:54:12 -0700140 EglExtensions.setDamage = extensions.has("EGL_KHR_partial_update");
John Reck708b6682015-10-22 13:11:00 -0700141 LOG_ALWAYS_FATAL_IF(!extensions.has("EGL_KHR_swap_buffers_with_damage"),
John Reck1bcacfd2017-11-03 10:12:19 -0700142 "Missing required extension EGL_KHR_swap_buffers_with_damage");
Romain Guy26a2b972017-04-17 09:39:51 -0700143
144 EglExtensions.glColorSpace = extensions.has("EGL_KHR_gl_colorspace");
145 EglExtensions.noConfigContext = extensions.has("EGL_KHR_no_config_context");
146 EglExtensions.pixelFormatFloat = extensions.has("EGL_EXT_pixel_format_float");
Romain Guy26b6a642017-07-11 09:48:28 -0700147#ifdef ANDROID_ENABLE_LINEAR_BLENDING
148 EglExtensions.scRGB = extensions.has("EGL_EXT_gl_colorspace_scrgb_linear");
149#else
150 EglExtensions.scRGB = extensions.has("EGL_EXT_gl_colorspace_scrgb");
151#endif
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200152 EglExtensions.contextPriority = extensions.has("EGL_IMG_context_priority");
John Reck1e510712018-04-23 08:15:03 -0700153 EglExtensions.surfacelessContext = extensions.has("EGL_KHR_surfaceless_context");
John Reck149173d2015-08-10 09:52:29 -0700154}
155
John Reck3b202512014-06-23 13:13:08 -0700156bool EglManager::hasEglContext() {
157 return mEglDisplay != EGL_NO_DISPLAY;
158}
159
Romain Guy26a2b972017-04-17 09:39:51 -0700160void EglManager::loadConfigs() {
John Reck149173d2015-08-10 09:52:29 -0700161 ALOGD("Swap behavior %d", static_cast<int>(mSwapBehavior));
John Reck1bcacfd2017-11-03 10:12:19 -0700162 EGLint swapBehavior =
163 (mSwapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
164 EGLint attribs[] = {EGL_RENDERABLE_TYPE,
165 EGL_OPENGL_ES2_BIT,
166 EGL_RED_SIZE,
167 8,
168 EGL_GREEN_SIZE,
169 8,
170 EGL_BLUE_SIZE,
171 8,
172 EGL_ALPHA_SIZE,
173 8,
174 EGL_DEPTH_SIZE,
175 0,
176 EGL_CONFIG_CAVEAT,
177 EGL_NONE,
178 EGL_STENCIL_SIZE,
John Reck1e510712018-04-23 08:15:03 -0700179 STENCIL_BUFFER_SIZE,
John Reck1bcacfd2017-11-03 10:12:19 -0700180 EGL_SURFACE_TYPE,
181 EGL_WINDOW_BIT | swapBehavior,
182 EGL_NONE};
John Reck3b202512014-06-23 13:13:08 -0700183
Romain Guy26a2b972017-04-17 09:39:51 -0700184 EGLint numConfigs = 1;
John Reck1bcacfd2017-11-03 10:12:19 -0700185 if (!eglChooseConfig(mEglDisplay, attribs, &mEglConfig, numConfigs, &numConfigs) ||
186 numConfigs != 1) {
John Reck149173d2015-08-10 09:52:29 -0700187 if (mSwapBehavior == SwapBehavior::Preserved) {
John Reck3b202512014-06-23 13:13:08 -0700188 // Try again without dirty regions enabled
John Reck149173d2015-08-10 09:52:29 -0700189 ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...");
190 mSwapBehavior = SwapBehavior::Discard;
Romain Guy26a2b972017-04-17 09:39:51 -0700191 loadConfigs();
John Reck1bcacfd2017-11-03 10:12:19 -0700192 return; // the call to loadConfigs() we just made picks the wide gamut config
John Reck3b202512014-06-23 13:13:08 -0700193 } else {
John Reck149173d2015-08-10 09:52:29 -0700194 // Failed to get a valid config
sergeyv694d4992016-10-27 10:23:13 -0700195 LOG_ALWAYS_FATAL("Failed to choose config, error = %s", eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700196 }
197 }
Romain Guy26a2b972017-04-17 09:39:51 -0700198
199 if (EglExtensions.pixelFormatFloat) {
200 // If we reached this point, we have a valid swap behavior
John Reck1bcacfd2017-11-03 10:12:19 -0700201 EGLint attribs16F[] = {EGL_RENDERABLE_TYPE,
202 EGL_OPENGL_ES2_BIT,
203 EGL_COLOR_COMPONENT_TYPE_EXT,
204 EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT,
205 EGL_RED_SIZE,
206 16,
207 EGL_GREEN_SIZE,
208 16,
209 EGL_BLUE_SIZE,
210 16,
211 EGL_ALPHA_SIZE,
212 16,
213 EGL_DEPTH_SIZE,
214 0,
215 EGL_STENCIL_SIZE,
John Reck1e510712018-04-23 08:15:03 -0700216 STENCIL_BUFFER_SIZE,
John Reck1bcacfd2017-11-03 10:12:19 -0700217 EGL_SURFACE_TYPE,
218 EGL_WINDOW_BIT | swapBehavior,
219 EGL_NONE};
Romain Guy26a2b972017-04-17 09:39:51 -0700220
221 numConfigs = 1;
John Reck1bcacfd2017-11-03 10:12:19 -0700222 if (!eglChooseConfig(mEglDisplay, attribs16F, &mEglConfigWideGamut, numConfigs,
223 &numConfigs) ||
224 numConfigs != 1) {
Rob Herring74883ab2017-11-29 09:26:31 -0600225 ALOGE("Device claims wide gamut support, cannot find matching config, error = %s",
John Reck1a4a9812018-04-18 16:13:31 -0700226 eglErrorString());
Rob Herring74883ab2017-11-29 09:26:31 -0600227 EglExtensions.pixelFormatFloat = false;
Romain Guy26a2b972017-04-17 09:39:51 -0700228 }
229 }
John Reck3b202512014-06-23 13:13:08 -0700230}
231
232void EglManager::createContext() {
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200233 std::vector<EGLint> contextAttributes;
234 contextAttributes.reserve(5);
235 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
236 contextAttributes.push_back(GLES_VERSION);
237 if (Properties::contextPriority != 0 && EglExtensions.contextPriority) {
238 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
239 contextAttributes.push_back(Properties::contextPriority);
240 }
241 contextAttributes.push_back(EGL_NONE);
John Reck1bcacfd2017-11-03 10:12:19 -0700242 mEglContext = eglCreateContext(
243 mEglDisplay, EglExtensions.noConfigContext ? ((EGLConfig) nullptr) : mEglConfig,
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200244 EGL_NO_CONTEXT, contextAttributes.data());
John Reck1bcacfd2017-11-03 10:12:19 -0700245 LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, "Failed to create context, error = %s",
246 eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700247}
248
John Reckd7db4d72015-05-20 07:18:55 -0700249void EglManager::createPBufferSurface() {
John Reck3b202512014-06-23 13:13:08 -0700250 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
John Reck1bcacfd2017-11-03 10:12:19 -0700251 "usePBufferSurface() called on uninitialized GlobalContext!");
John Reck3b202512014-06-23 13:13:08 -0700252
John Reck1e510712018-04-23 08:15:03 -0700253 if (mPBufferSurface == EGL_NO_SURFACE && !EglExtensions.surfacelessContext) {
John Reck1bcacfd2017-11-03 10:12:19 -0700254 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
John Reck3b202512014-06-23 13:13:08 -0700255 mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs);
256 }
John Reck3b202512014-06-23 13:13:08 -0700257}
258
Romain Guy26a2b972017-04-17 09:39:51 -0700259EGLSurface EglManager::createSurface(EGLNativeWindowType window, bool wideColorGamut) {
John Reck1e510712018-04-23 08:15:03 -0700260 LOG_ALWAYS_FATAL_IF(!hasEglContext(), "Not initialized");
Romain Guy253f2c22016-09-28 17:34:42 -0700261
John Reck1bcacfd2017-11-03 10:12:19 -0700262 wideColorGamut = wideColorGamut && EglExtensions.glColorSpace && EglExtensions.scRGB &&
263 EglExtensions.pixelFormatFloat && EglExtensions.noConfigContext;
Romain Guy26a2b972017-04-17 09:39:51 -0700264
265 // The color space we want to use depends on whether linear blending is turned
266 // on and whether the app has requested wide color gamut rendering. When wide
267 // color gamut rendering is off, the app simply renders in the display's native
268 // color gamut.
269 //
270 // When wide gamut rendering is off:
271 // - Blending is done by default in gamma space, which requires using a
272 // linear EGL color space (the GPU uses the color values as is)
273 // - If linear blending is on, we must use the sRGB EGL color space (the
274 // GPU will perform sRGB to linear and linear to SRGB conversions before
275 // and after blending)
276 //
277 // When wide gamut rendering is on we cannot rely on the GPU performing
278 // linear blending for us. We use two different color spaces to tag the
279 // surface appropriately for SurfaceFlinger:
280 // - Gamma blending (default) requires the use of the scRGB-nl color space
281 // - Linear blending requires the use of the scRGB color space
282
283 // Not all Android targets support the EGL_GL_COLOR_SPACE_KHR extension
284 // We insert to placeholders to set EGL_GL_COLORSPACE_KHR and its value.
285 // According to section 3.4.1 of the EGL specification, the attributes
286 // list is considered empty if the first entry is EGL_NONE
John Reck1bcacfd2017-11-03 10:12:19 -0700287 EGLint attribs[] = {EGL_NONE, EGL_NONE, EGL_NONE};
Romain Guy253f2c22016-09-28 17:34:42 -0700288
Romain Guy26a2b972017-04-17 09:39:51 -0700289 if (EglExtensions.glColorSpace) {
290 attribs[0] = EGL_GL_COLORSPACE_KHR;
291#ifdef ANDROID_ENABLE_LINEAR_BLENDING
292 if (wideColorGamut) {
293 attribs[1] = EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT;
294 } else {
295 attribs[1] = EGL_GL_COLORSPACE_SRGB_KHR;
296 }
297#else
298 if (wideColorGamut) {
Romain Guy26b6a642017-07-11 09:48:28 -0700299 attribs[1] = EGL_GL_COLORSPACE_SCRGB_EXT;
Romain Guy26a2b972017-04-17 09:39:51 -0700300 } else {
301 attribs[1] = EGL_GL_COLORSPACE_LINEAR_KHR;
302 }
303#endif
304 }
305
John Reck1bcacfd2017-11-03 10:12:19 -0700306 EGLSurface surface = eglCreateWindowSurface(
307 mEglDisplay, wideColorGamut ? mEglConfigWideGamut : mEglConfig, window, attribs);
John Reck3b202512014-06-23 13:13:08 -0700308 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE,
John Reck1bcacfd2017-11-03 10:12:19 -0700309 "Failed to create EGLSurface for window %p, eglErr = %s", (void*)window,
310 eglErrorString());
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000311
312 if (mSwapBehavior != SwapBehavior::Preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700313 LOG_ALWAYS_FATAL_IF(eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
314 EGL_BUFFER_DESTROYED) == EGL_FALSE,
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000315 "Failed to set swap behavior to destroyed for window %p, eglErr = %s",
John Reck1bcacfd2017-11-03 10:12:19 -0700316 (void*)window, eglErrorString());
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000317 }
318
John Reck3b202512014-06-23 13:13:08 -0700319 return surface;
320}
321
322void EglManager::destroySurface(EGLSurface surface) {
323 if (isCurrent(surface)) {
324 makeCurrent(EGL_NO_SURFACE);
325 }
326 if (!eglDestroySurface(mEglDisplay, surface)) {
sergeyv694d4992016-10-27 10:23:13 -0700327 ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700328 }
329}
330
331void EglManager::destroy() {
332 if (mEglDisplay == EGL_NO_DISPLAY) return;
333
John Reck3b202512014-06-23 13:13:08 -0700334 eglDestroyContext(mEglDisplay, mEglContext);
John Reck1e510712018-04-23 08:15:03 -0700335 if (mPBufferSurface != EGL_NO_SURFACE) {
336 eglDestroySurface(mEglDisplay, mPBufferSurface);
337 }
John Reck3b202512014-06-23 13:13:08 -0700338 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
339 eglTerminate(mEglDisplay);
340 eglReleaseThread();
341
342 mEglDisplay = EGL_NO_DISPLAY;
343 mEglContext = EGL_NO_CONTEXT;
344 mPBufferSurface = EGL_NO_SURFACE;
345 mCurrentSurface = EGL_NO_SURFACE;
346}
347
John Reck1e510712018-04-23 08:15:03 -0700348bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut, bool force) {
349 if (!force && isCurrent(surface)) return false;
John Reck3b202512014-06-23 13:13:08 -0700350
351 if (surface == EGL_NO_SURFACE) {
John Reckd7db4d72015-05-20 07:18:55 -0700352 // Ensure we always have a valid surface & context
353 surface = mPBufferSurface;
354 }
355 if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) {
John Reckf2dcc2a2015-07-16 09:17:59 -0700356 if (errOut) {
357 *errOut = eglGetError();
John Reck1bcacfd2017-11-03 10:12:19 -0700358 ALOGW("Failed to make current on surface %p, error=%s", (void*)surface,
359 egl_error_str(*errOut));
John Reckf2dcc2a2015-07-16 09:17:59 -0700360 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700361 LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", (void*)surface,
362 eglErrorString());
John Reckf2dcc2a2015-07-16 09:17:59 -0700363 }
John Reck3b202512014-06-23 13:13:08 -0700364 }
365 mCurrentSurface = surface;
John Recka8963062017-06-14 10:47:50 -0700366 if (Properties::disableVsync) {
367 eglSwapInterval(mEglDisplay, 0);
368 }
John Reck3b202512014-06-23 13:13:08 -0700369 return true;
370}
371
John Reck149173d2015-08-10 09:52:29 -0700372EGLint EglManager::queryBufferAge(EGLSurface surface) {
373 switch (mSwapBehavior) {
John Reck1bcacfd2017-11-03 10:12:19 -0700374 case SwapBehavior::Discard:
375 return 0;
376 case SwapBehavior::Preserved:
377 return 1;
378 case SwapBehavior::BufferAge:
379 EGLint bufferAge;
380 eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge);
381 return bufferAge;
John Reck149173d2015-08-10 09:52:29 -0700382 }
383 return 0;
384}
385
386Frame EglManager::beginFrame(EGLSurface surface) {
John Reck1bcacfd2017-11-03 10:12:19 -0700387 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, "Tried to beginFrame on EGL_NO_SURFACE!");
John Reck3b202512014-06-23 13:13:08 -0700388 makeCurrent(surface);
John Reck149173d2015-08-10 09:52:29 -0700389 Frame frame;
390 frame.mSurface = surface;
391 eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth);
392 eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight);
393 frame.mBufferAge = queryBufferAge(surface);
John Reck3b202512014-06-23 13:13:08 -0700394 eglBeginFrame(mEglDisplay, surface);
John Reck149173d2015-08-10 09:52:29 -0700395 return frame;
John Reck3b202512014-06-23 13:13:08 -0700396}
397
John Reck149173d2015-08-10 09:52:29 -0700398void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) {
399#ifdef EGL_KHR_partial_update
400 if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) {
401 EGLint rects[4];
402 frame.map(dirty, rects);
403 if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) {
404 LOG_ALWAYS_FATAL("Failed to set damage region on surface %p, error=%s",
John Reck1bcacfd2017-11-03 10:12:19 -0700405 (void*)frame.mSurface, eglErrorString());
John Reck149173d2015-08-10 09:52:29 -0700406 }
407 }
408#endif
409}
410
John Reckc96955d2016-02-26 14:56:44 -0800411bool EglManager::damageRequiresSwap() {
412 return EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge;
413}
414
John Reck149173d2015-08-10 09:52:29 -0700415bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) {
John Reck682573c2015-10-30 10:37:35 -0700416 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
John Reck55156372015-01-21 07:46:37 -0800417 ATRACE_NAME("Finishing GPU work");
418 fence();
419 }
John Reck55156372015-01-21 07:46:37 -0800420
John Recka672f6b2015-10-22 09:53:26 -0700421 EGLint rects[4];
422 frame.map(screenDirty, rects);
John Reck1bcacfd2017-11-03 10:12:19 -0700423 eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects, screenDirty.isEmpty() ? 0 : 1);
John Reckd04794a2015-05-08 10:04:36 -0700424
John Reck3b202512014-06-23 13:13:08 -0700425 EGLint err = eglGetError();
John Reck2cdbc7d2014-09-17 16:06:36 -0700426 if (CC_LIKELY(err == EGL_SUCCESS)) {
427 return true;
428 }
John Reckc2547fa2015-10-26 13:52:52 -0700429 if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) {
John Reck2cdbc7d2014-09-17 16:06:36 -0700430 // For some reason our surface was destroyed out from under us
431 // This really shouldn't happen, but if it does we can recover easily
432 // by just not trying to use the surface anymore
John Reck1bcacfd2017-11-03 10:12:19 -0700433 ALOGW("swapBuffers encountered EGL error %d on %p, halting rendering...", err,
434 frame.mSurface);
John Reck2cdbc7d2014-09-17 16:06:36 -0700435 return false;
436 }
John Reck1bcacfd2017-11-03 10:12:19 -0700437 LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering", err, egl_error_str(err));
John Reck2cdbc7d2014-09-17 16:06:36 -0700438 // Impossible to hit this, but the compiler doesn't know that
439 return false;
John Reck3b202512014-06-23 13:13:08 -0700440}
441
John Reck55156372015-01-21 07:46:37 -0800442void EglManager::fence() {
443 EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL);
John Reck1bcacfd2017-11-03 10:12:19 -0700444 eglClientWaitSyncKHR(mEglDisplay, fence, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR);
John Reck55156372015-01-21 07:46:37 -0800445 eglDestroySyncKHR(mEglDisplay, fence);
446}
447
John Reck1125d1f2014-10-23 11:02:19 -0700448bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) {
John Reck149173d2015-08-10 09:52:29 -0700449 if (mSwapBehavior != SwapBehavior::Preserved) return false;
John Reck3b202512014-06-23 13:13:08 -0700450
John Reck149173d2015-08-10 09:52:29 -0700451 bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
John Reck1bcacfd2017-11-03 10:12:19 -0700452 preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED);
John Reck149173d2015-08-10 09:52:29 -0700453 if (!preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700454 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", (void*)surface,
455 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700456 // Maybe it's already set?
457 EGLint swapBehavior;
458 if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) {
459 preserved = (swapBehavior == EGL_BUFFER_PRESERVED);
460 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700461 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", (void*)surface,
462 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700463 }
John Reck3b202512014-06-23 13:13:08 -0700464 }
John Reck1125d1f2014-10-23 11:02:19 -0700465
466 return preserved;
John Reck3b202512014-06-23 13:13:08 -0700467}
468
Stan Iliev564ca3e2018-09-04 22:00:00 +0000469status_t EglManager::fenceWait(sp<Fence>& fence) {
470 if (!hasEglContext()) {
471 ALOGE("EglManager::fenceWait: EGLDisplay not initialized");
472 return INVALID_OPERATION;
473 }
474
475 if (SyncFeatures::getInstance().useWaitSync() &&
476 SyncFeatures::getInstance().useNativeFenceSync()) {
477 // Block GPU on the fence.
478 // Create an EGLSyncKHR from the current fence.
479 int fenceFd = fence->dup();
480 if (fenceFd == -1) {
481 ALOGE("EglManager::fenceWait: error dup'ing fence fd: %d", errno);
482 return -errno;
483 }
484 EGLint attribs[] = {
485 EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd,
486 EGL_NONE
487 };
488 EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay,
489 EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
490 if (sync == EGL_NO_SYNC_KHR) {
491 close(fenceFd);
492 ALOGE("EglManager::fenceWait: error creating EGL fence: %#x", eglGetError());
493 return UNKNOWN_ERROR;
494 }
495
496 // XXX: The spec draft is inconsistent as to whether this should
497 // return an EGLint or void. Ignore the return value for now, as
498 // it's not strictly needed.
499 eglWaitSyncKHR(mEglDisplay, sync, 0);
500 EGLint eglErr = eglGetError();
501 eglDestroySyncKHR(mEglDisplay, sync);
502 if (eglErr != EGL_SUCCESS) {
503 ALOGE("EglManager::fenceWait: error waiting for EGL fence: %#x", eglErr);
504 return UNKNOWN_ERROR;
505 }
506 } else {
507 // Block CPU on the fence.
508 status_t err = fence->waitForever("EglManager::fenceWait");
509 if (err != NO_ERROR) {
510 ALOGE("EglManager::fenceWait: error waiting for fence: %d", err);
511 return err;
512 }
513 }
514 return OK;
515}
516
517status_t EglManager::createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence,
518 sp<Fence>& nativeFence) {
519 if (!hasEglContext()) {
520 ALOGE("EglManager::createReleaseFence: EGLDisplay not initialized");
521 return INVALID_OPERATION;
522 }
523
524 if (SyncFeatures::getInstance().useNativeFenceSync()) {
525 EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay,
526 EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
527 if (sync == EGL_NO_SYNC_KHR) {
528 ALOGE("EglManager::createReleaseFence: error creating EGL fence: %#x",
529 eglGetError());
530 return UNKNOWN_ERROR;
531 }
532 glFlush();
533 int fenceFd = eglDupNativeFenceFDANDROID(mEglDisplay, sync);
534 eglDestroySyncKHR(mEglDisplay, sync);
535 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
536 ALOGE("EglManager::createReleaseFence: error dup'ing native fence "
537 "fd: %#x", eglGetError());
538 return UNKNOWN_ERROR;
539 }
540 nativeFence = new Fence(fenceFd);
541 *eglFence = EGL_NO_SYNC_KHR;
542 } else if (useFenceSync && SyncFeatures::getInstance().useFenceSync()) {
543 if (*eglFence != EGL_NO_SYNC_KHR) {
544 // There is already a fence for the current slot. We need to
545 // wait on that before replacing it with another fence to
546 // ensure that all outstanding buffer accesses have completed
547 // before the producer accesses it.
548 EGLint result = eglClientWaitSyncKHR(mEglDisplay, *eglFence, 0, 1000000000);
549 if (result == EGL_FALSE) {
550 ALOGE("EglManager::createReleaseFence: error waiting for previous fence: %#x",
551 eglGetError());
552 return UNKNOWN_ERROR;
553 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
554 ALOGE("EglManager::createReleaseFence: timeout waiting for previous fence");
555 return TIMED_OUT;
556 }
557 eglDestroySyncKHR(mEglDisplay, *eglFence);
558 }
559
560 // Create a fence for the outstanding accesses in the current
561 // OpenGL ES context.
562 *eglFence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, nullptr);
563 if (*eglFence == EGL_NO_SYNC_KHR) {
564 ALOGE("EglManager::createReleaseFence: error creating fence: %#x", eglGetError());
565 return UNKNOWN_ERROR;
566 }
567 glFlush();
568 }
569 return OK;
570}
571
John Reck3b202512014-06-23 13:13:08 -0700572} /* namespace renderthread */
573} /* namespace uirenderer */
574} /* namespace android */