blob: 65ced6ad9316d7af9a6a537896236be56dd4f228 [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>
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;
Peiyong Lin1f6aa122018-09-10 16:28:08 -070079 bool displayP3 = false;
Jorim Jaggi767e25e2018-04-04 23:07:35 +020080 bool contextPriority = false;
John Reck1e510712018-04-23 08:15:03 -070081 bool surfacelessContext = false;
John Reck149173d2015-08-10 09:52:29 -070082} EglExtensions;
83
John Reck1e510712018-04-23 08:15:03 -070084EglManager::EglManager()
85 : mEglDisplay(EGL_NO_DISPLAY)
Chris Craikd41c4d82015-01-05 15:51:13 -080086 , mEglConfig(nullptr)
Romain Guy26a2b972017-04-17 09:39:51 -070087 , mEglConfigWideGamut(nullptr)
John Reck3b202512014-06-23 13:13:08 -070088 , mEglContext(EGL_NO_CONTEXT)
89 , mPBufferSurface(EGL_NO_SURFACE)
John Reck1bcacfd2017-11-03 10:12:19 -070090 , mCurrentSurface(EGL_NO_SURFACE) {}
John Reck3b202512014-06-23 13:13:08 -070091
John Reck1e510712018-04-23 08:15:03 -070092EglManager::~EglManager() {
93 destroy();
94}
95
John Reck3b202512014-06-23 13:13:08 -070096void EglManager::initialize() {
97 if (hasEglContext()) return;
98
John Reckfbc8df02014-11-14 16:18:41 -080099 ATRACE_NAME("Creating EGLContext");
100
John Reck3b202512014-06-23 13:13:08 -0700101 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
John Reck1bcacfd2017-11-03 10:12:19 -0700102 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, "Failed to get EGL_DEFAULT_DISPLAY! err=%s",
103 eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700104
105 EGLint major, minor;
106 LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE,
John Reck1bcacfd2017-11-03 10:12:19 -0700107 "Failed to initialize display %p! err=%s", mEglDisplay, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700108
109 ALOGI("Initialized EGL, version %d.%d", (int)major, (int)minor);
110
John Reck149173d2015-08-10 09:52:29 -0700111 initExtensions();
Season Li13d1b4a2015-07-29 17:16:19 -0700112
John Reck149173d2015-08-10 09:52:29 -0700113 // Now that extensions are loaded, pick a swap behavior
114 if (Properties::enablePartialUpdates) {
Matt Sarettb77c94a2016-12-07 12:22:37 -0500115 // An Adreno driver bug is causing rendering problems for SkiaGL with
116 // buffer age swap behavior (b/31957043). To temporarily workaround,
117 // we will use preserved swap behavior.
Derek Sollenbergerb5a12bd2017-06-14 15:23:19 -0400118 if (Properties::useBufferAge && EglExtensions.bufferAge) {
John Reck149173d2015-08-10 09:52:29 -0700119 mSwapBehavior = SwapBehavior::BufferAge;
120 } else {
121 mSwapBehavior = SwapBehavior::Preserved;
122 }
123 }
124
Romain Guy26a2b972017-04-17 09:39:51 -0700125 loadConfigs();
John Reck3b202512014-06-23 13:13:08 -0700126 createContext();
John Reckd7db4d72015-05-20 07:18:55 -0700127 createPBufferSurface();
John Reck1e510712018-04-23 08:15:03 -0700128 makeCurrent(mPBufferSurface, nullptr, /* force */ true);
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
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700152 EglExtensions.displayP3 = extensions.has("EGL_EXT_gl_colorspace_display_p3");
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200153 EglExtensions.contextPriority = extensions.has("EGL_IMG_context_priority");
John Reck1e510712018-04-23 08:15:03 -0700154 EglExtensions.surfacelessContext = extensions.has("EGL_KHR_surfaceless_context");
John Reck149173d2015-08-10 09:52:29 -0700155}
156
John Reck3b202512014-06-23 13:13:08 -0700157bool EglManager::hasEglContext() {
158 return mEglDisplay != EGL_NO_DISPLAY;
159}
160
Romain Guy26a2b972017-04-17 09:39:51 -0700161void EglManager::loadConfigs() {
John Reck149173d2015-08-10 09:52:29 -0700162 ALOGD("Swap behavior %d", static_cast<int>(mSwapBehavior));
John Reck1bcacfd2017-11-03 10:12:19 -0700163 EGLint swapBehavior =
164 (mSwapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700165
166 // Note: The default pixel format is RGBA_8888, when other formats are
167 // available, we should check the target pixel format and configure the
168 // attributes list properly.
John Reck1bcacfd2017-11-03 10:12:19 -0700169 EGLint attribs[] = {EGL_RENDERABLE_TYPE,
170 EGL_OPENGL_ES2_BIT,
171 EGL_RED_SIZE,
172 8,
173 EGL_GREEN_SIZE,
174 8,
175 EGL_BLUE_SIZE,
176 8,
177 EGL_ALPHA_SIZE,
178 8,
179 EGL_DEPTH_SIZE,
180 0,
181 EGL_CONFIG_CAVEAT,
182 EGL_NONE,
183 EGL_STENCIL_SIZE,
John Reck1e510712018-04-23 08:15:03 -0700184 STENCIL_BUFFER_SIZE,
John Reck1bcacfd2017-11-03 10:12:19 -0700185 EGL_SURFACE_TYPE,
186 EGL_WINDOW_BIT | swapBehavior,
187 EGL_NONE};
John Reck3b202512014-06-23 13:13:08 -0700188
Romain Guy26a2b972017-04-17 09:39:51 -0700189 EGLint numConfigs = 1;
John Reck1bcacfd2017-11-03 10:12:19 -0700190 if (!eglChooseConfig(mEglDisplay, attribs, &mEglConfig, numConfigs, &numConfigs) ||
191 numConfigs != 1) {
John Reck149173d2015-08-10 09:52:29 -0700192 if (mSwapBehavior == SwapBehavior::Preserved) {
John Reck3b202512014-06-23 13:13:08 -0700193 // Try again without dirty regions enabled
John Reck149173d2015-08-10 09:52:29 -0700194 ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...");
195 mSwapBehavior = SwapBehavior::Discard;
Romain Guy26a2b972017-04-17 09:39:51 -0700196 loadConfigs();
John Reck1bcacfd2017-11-03 10:12:19 -0700197 return; // the call to loadConfigs() we just made picks the wide gamut config
John Reck3b202512014-06-23 13:13:08 -0700198 } else {
John Reck149173d2015-08-10 09:52:29 -0700199 // Failed to get a valid config
sergeyv694d4992016-10-27 10:23:13 -0700200 LOG_ALWAYS_FATAL("Failed to choose config, error = %s", eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700201 }
202 }
Romain Guy26a2b972017-04-17 09:39:51 -0700203
204 if (EglExtensions.pixelFormatFloat) {
205 // If we reached this point, we have a valid swap behavior
John Reck1bcacfd2017-11-03 10:12:19 -0700206 EGLint attribs16F[] = {EGL_RENDERABLE_TYPE,
207 EGL_OPENGL_ES2_BIT,
208 EGL_COLOR_COMPONENT_TYPE_EXT,
209 EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT,
210 EGL_RED_SIZE,
211 16,
212 EGL_GREEN_SIZE,
213 16,
214 EGL_BLUE_SIZE,
215 16,
216 EGL_ALPHA_SIZE,
217 16,
218 EGL_DEPTH_SIZE,
219 0,
220 EGL_STENCIL_SIZE,
John Reck1e510712018-04-23 08:15:03 -0700221 STENCIL_BUFFER_SIZE,
John Reck1bcacfd2017-11-03 10:12:19 -0700222 EGL_SURFACE_TYPE,
223 EGL_WINDOW_BIT | swapBehavior,
224 EGL_NONE};
Romain Guy26a2b972017-04-17 09:39:51 -0700225
226 numConfigs = 1;
John Reck1bcacfd2017-11-03 10:12:19 -0700227 if (!eglChooseConfig(mEglDisplay, attribs16F, &mEglConfigWideGamut, numConfigs,
228 &numConfigs) ||
229 numConfigs != 1) {
Rob Herring74883ab2017-11-29 09:26:31 -0600230 ALOGE("Device claims wide gamut support, cannot find matching config, error = %s",
John Reck1a4a9812018-04-18 16:13:31 -0700231 eglErrorString());
Rob Herring74883ab2017-11-29 09:26:31 -0600232 EglExtensions.pixelFormatFloat = false;
Romain Guy26a2b972017-04-17 09:39:51 -0700233 }
234 }
John Reck3b202512014-06-23 13:13:08 -0700235}
236
237void EglManager::createContext() {
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200238 std::vector<EGLint> contextAttributes;
239 contextAttributes.reserve(5);
240 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
241 contextAttributes.push_back(GLES_VERSION);
242 if (Properties::contextPriority != 0 && EglExtensions.contextPriority) {
243 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
244 contextAttributes.push_back(Properties::contextPriority);
245 }
246 contextAttributes.push_back(EGL_NONE);
John Reck1bcacfd2017-11-03 10:12:19 -0700247 mEglContext = eglCreateContext(
248 mEglDisplay, EglExtensions.noConfigContext ? ((EGLConfig) nullptr) : mEglConfig,
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200249 EGL_NO_CONTEXT, contextAttributes.data());
John Reck1bcacfd2017-11-03 10:12:19 -0700250 LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, "Failed to create context, error = %s",
251 eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700252}
253
John Reckd7db4d72015-05-20 07:18:55 -0700254void EglManager::createPBufferSurface() {
John Reck3b202512014-06-23 13:13:08 -0700255 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
John Reck1bcacfd2017-11-03 10:12:19 -0700256 "usePBufferSurface() called on uninitialized GlobalContext!");
John Reck3b202512014-06-23 13:13:08 -0700257
John Reck1e510712018-04-23 08:15:03 -0700258 if (mPBufferSurface == EGL_NO_SURFACE && !EglExtensions.surfacelessContext) {
John Reck1bcacfd2017-11-03 10:12:19 -0700259 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
John Reck3b202512014-06-23 13:13:08 -0700260 mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs);
261 }
John Reck3b202512014-06-23 13:13:08 -0700262}
263
John Reck8e539ca2018-11-15 15:21:29 -0800264Result<EGLSurface, EGLint> EglManager::createSurface(EGLNativeWindowType window, ColorMode colorMode) {
John Reck1e510712018-04-23 08:15:03 -0700265 LOG_ALWAYS_FATAL_IF(!hasEglContext(), "Not initialized");
Romain Guy253f2c22016-09-28 17:34:42 -0700266
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700267 bool wideColorGamut = colorMode == ColorMode::WideColorGamut && EglExtensions.glColorSpace &&
268 EglExtensions.scRGB && EglExtensions.pixelFormatFloat &&
269 EglExtensions.noConfigContext;
Romain Guy26a2b972017-04-17 09:39:51 -0700270
271 // The color space we want to use depends on whether linear blending is turned
272 // on and whether the app has requested wide color gamut rendering. When wide
273 // color gamut rendering is off, the app simply renders in the display's native
274 // color gamut.
275 //
276 // When wide gamut rendering is off:
277 // - Blending is done by default in gamma space, which requires using a
278 // linear EGL color space (the GPU uses the color values as is)
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700279 // - If linear blending is on, we must use the non-linear EGL color space
280 // (the GPU will perform sRGB to linear and linear to SRGB conversions
281 // before and after blending)
Romain Guy26a2b972017-04-17 09:39:51 -0700282 //
283 // When wide gamut rendering is on we cannot rely on the GPU performing
284 // linear blending for us. We use two different color spaces to tag the
285 // surface appropriately for SurfaceFlinger:
286 // - Gamma blending (default) requires the use of the scRGB-nl color space
287 // - Linear blending requires the use of the scRGB color space
288
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700289 // Not all Android targets support the EGL_GL_COLORSPACE_KHR extension
Romain Guy26a2b972017-04-17 09:39:51 -0700290 // We insert to placeholders to set EGL_GL_COLORSPACE_KHR and its value.
291 // According to section 3.4.1 of the EGL specification, the attributes
292 // list is considered empty if the first entry is EGL_NONE
John Reck1bcacfd2017-11-03 10:12:19 -0700293 EGLint attribs[] = {EGL_NONE, EGL_NONE, EGL_NONE};
Romain Guy253f2c22016-09-28 17:34:42 -0700294
Romain Guy26a2b972017-04-17 09:39:51 -0700295 if (EglExtensions.glColorSpace) {
296 attribs[0] = EGL_GL_COLORSPACE_KHR;
297#ifdef ANDROID_ENABLE_LINEAR_BLENDING
298 if (wideColorGamut) {
299 attribs[1] = EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT;
300 } else {
Peiyong Lin189021b2018-09-27 16:41:40 -0700301 attribs[1] = EGL_GL_COLORSPACE_SRGB_KHR;
Romain Guy26a2b972017-04-17 09:39:51 -0700302 }
303#else
304 if (wideColorGamut) {
Romain Guy26b6a642017-07-11 09:48:28 -0700305 attribs[1] = EGL_GL_COLORSPACE_SCRGB_EXT;
Romain Guy26a2b972017-04-17 09:39:51 -0700306 } else {
Peiyong Lin189021b2018-09-27 16:41:40 -0700307 attribs[1] = EGL_GL_COLORSPACE_LINEAR_KHR;
Romain Guy26a2b972017-04-17 09:39:51 -0700308 }
309#endif
310 }
311
John Reck1bcacfd2017-11-03 10:12:19 -0700312 EGLSurface surface = eglCreateWindowSurface(
313 mEglDisplay, wideColorGamut ? mEglConfigWideGamut : mEglConfig, window, attribs);
John Reck8e539ca2018-11-15 15:21:29 -0800314 if (surface == EGL_NO_SURFACE) {
315 return Error<EGLint> { eglGetError() };
316 }
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000317
318 if (mSwapBehavior != SwapBehavior::Preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700319 LOG_ALWAYS_FATAL_IF(eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
320 EGL_BUFFER_DESTROYED) == EGL_FALSE,
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000321 "Failed to set swap behavior to destroyed for window %p, eglErr = %s",
John Reck1bcacfd2017-11-03 10:12:19 -0700322 (void*)window, eglErrorString());
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000323 }
324
John Reck3b202512014-06-23 13:13:08 -0700325 return surface;
326}
327
328void EglManager::destroySurface(EGLSurface surface) {
329 if (isCurrent(surface)) {
330 makeCurrent(EGL_NO_SURFACE);
331 }
332 if (!eglDestroySurface(mEglDisplay, surface)) {
sergeyv694d4992016-10-27 10:23:13 -0700333 ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700334 }
335}
336
337void EglManager::destroy() {
338 if (mEglDisplay == EGL_NO_DISPLAY) return;
339
John Reck3b202512014-06-23 13:13:08 -0700340 eglDestroyContext(mEglDisplay, mEglContext);
John Reck1e510712018-04-23 08:15:03 -0700341 if (mPBufferSurface != EGL_NO_SURFACE) {
342 eglDestroySurface(mEglDisplay, mPBufferSurface);
343 }
John Reck3b202512014-06-23 13:13:08 -0700344 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
345 eglTerminate(mEglDisplay);
346 eglReleaseThread();
347
348 mEglDisplay = EGL_NO_DISPLAY;
349 mEglContext = EGL_NO_CONTEXT;
350 mPBufferSurface = EGL_NO_SURFACE;
351 mCurrentSurface = EGL_NO_SURFACE;
352}
353
John Reck1e510712018-04-23 08:15:03 -0700354bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut, bool force) {
355 if (!force && isCurrent(surface)) return false;
John Reck3b202512014-06-23 13:13:08 -0700356
357 if (surface == EGL_NO_SURFACE) {
John Reckd7db4d72015-05-20 07:18:55 -0700358 // Ensure we always have a valid surface & context
359 surface = mPBufferSurface;
360 }
361 if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) {
John Reckf2dcc2a2015-07-16 09:17:59 -0700362 if (errOut) {
363 *errOut = eglGetError();
John Reck1bcacfd2017-11-03 10:12:19 -0700364 ALOGW("Failed to make current on surface %p, error=%s", (void*)surface,
365 egl_error_str(*errOut));
John Reckf2dcc2a2015-07-16 09:17:59 -0700366 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700367 LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", (void*)surface,
368 eglErrorString());
John Reckf2dcc2a2015-07-16 09:17:59 -0700369 }
John Reck3b202512014-06-23 13:13:08 -0700370 }
371 mCurrentSurface = surface;
John Recka8963062017-06-14 10:47:50 -0700372 if (Properties::disableVsync) {
373 eglSwapInterval(mEglDisplay, 0);
374 }
John Reck3b202512014-06-23 13:13:08 -0700375 return true;
376}
377
John Reck149173d2015-08-10 09:52:29 -0700378EGLint EglManager::queryBufferAge(EGLSurface surface) {
379 switch (mSwapBehavior) {
John Reck1bcacfd2017-11-03 10:12:19 -0700380 case SwapBehavior::Discard:
381 return 0;
382 case SwapBehavior::Preserved:
383 return 1;
384 case SwapBehavior::BufferAge:
385 EGLint bufferAge;
386 eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge);
387 return bufferAge;
John Reck149173d2015-08-10 09:52:29 -0700388 }
389 return 0;
390}
391
392Frame EglManager::beginFrame(EGLSurface surface) {
John Reck1bcacfd2017-11-03 10:12:19 -0700393 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, "Tried to beginFrame on EGL_NO_SURFACE!");
John Reck3b202512014-06-23 13:13:08 -0700394 makeCurrent(surface);
John Reck149173d2015-08-10 09:52:29 -0700395 Frame frame;
396 frame.mSurface = surface;
397 eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth);
398 eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight);
399 frame.mBufferAge = queryBufferAge(surface);
John Reck3b202512014-06-23 13:13:08 -0700400 eglBeginFrame(mEglDisplay, surface);
John Reck149173d2015-08-10 09:52:29 -0700401 return frame;
John Reck3b202512014-06-23 13:13:08 -0700402}
403
John Reck149173d2015-08-10 09:52:29 -0700404void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) {
405#ifdef EGL_KHR_partial_update
406 if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) {
407 EGLint rects[4];
408 frame.map(dirty, rects);
409 if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) {
410 LOG_ALWAYS_FATAL("Failed to set damage region on surface %p, error=%s",
John Reck1bcacfd2017-11-03 10:12:19 -0700411 (void*)frame.mSurface, eglErrorString());
John Reck149173d2015-08-10 09:52:29 -0700412 }
413 }
414#endif
415}
416
John Reckc96955d2016-02-26 14:56:44 -0800417bool EglManager::damageRequiresSwap() {
418 return EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge;
419}
420
John Reck149173d2015-08-10 09:52:29 -0700421bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) {
John Reck682573c2015-10-30 10:37:35 -0700422 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
John Reck55156372015-01-21 07:46:37 -0800423 ATRACE_NAME("Finishing GPU work");
424 fence();
425 }
John Reck55156372015-01-21 07:46:37 -0800426
John Recka672f6b2015-10-22 09:53:26 -0700427 EGLint rects[4];
428 frame.map(screenDirty, rects);
John Reck1bcacfd2017-11-03 10:12:19 -0700429 eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects, screenDirty.isEmpty() ? 0 : 1);
John Reckd04794a2015-05-08 10:04:36 -0700430
John Reck3b202512014-06-23 13:13:08 -0700431 EGLint err = eglGetError();
John Reck2cdbc7d2014-09-17 16:06:36 -0700432 if (CC_LIKELY(err == EGL_SUCCESS)) {
433 return true;
434 }
John Reckc2547fa2015-10-26 13:52:52 -0700435 if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) {
John Reck2cdbc7d2014-09-17 16:06:36 -0700436 // For some reason our surface was destroyed out from under us
437 // This really shouldn't happen, but if it does we can recover easily
438 // by just not trying to use the surface anymore
John Reck1bcacfd2017-11-03 10:12:19 -0700439 ALOGW("swapBuffers encountered EGL error %d on %p, halting rendering...", err,
440 frame.mSurface);
John Reck2cdbc7d2014-09-17 16:06:36 -0700441 return false;
442 }
John Reck1bcacfd2017-11-03 10:12:19 -0700443 LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering", err, egl_error_str(err));
John Reck2cdbc7d2014-09-17 16:06:36 -0700444 // Impossible to hit this, but the compiler doesn't know that
445 return false;
John Reck3b202512014-06-23 13:13:08 -0700446}
447
John Reck55156372015-01-21 07:46:37 -0800448void EglManager::fence() {
449 EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL);
John Reck1bcacfd2017-11-03 10:12:19 -0700450 eglClientWaitSyncKHR(mEglDisplay, fence, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR);
John Reck55156372015-01-21 07:46:37 -0800451 eglDestroySyncKHR(mEglDisplay, fence);
452}
453
John Reck1125d1f2014-10-23 11:02:19 -0700454bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) {
John Reck149173d2015-08-10 09:52:29 -0700455 if (mSwapBehavior != SwapBehavior::Preserved) return false;
John Reck3b202512014-06-23 13:13:08 -0700456
John Reck149173d2015-08-10 09:52:29 -0700457 bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
John Reck1bcacfd2017-11-03 10:12:19 -0700458 preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED);
John Reck149173d2015-08-10 09:52:29 -0700459 if (!preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700460 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", (void*)surface,
461 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700462 // Maybe it's already set?
463 EGLint swapBehavior;
464 if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) {
465 preserved = (swapBehavior == EGL_BUFFER_PRESERVED);
466 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700467 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", (void*)surface,
468 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700469 }
John Reck3b202512014-06-23 13:13:08 -0700470 }
John Reck1125d1f2014-10-23 11:02:19 -0700471
472 return preserved;
John Reck3b202512014-06-23 13:13:08 -0700473}
474
Stan Iliev564ca3e2018-09-04 22:00:00 +0000475status_t EglManager::fenceWait(sp<Fence>& fence) {
476 if (!hasEglContext()) {
477 ALOGE("EglManager::fenceWait: EGLDisplay not initialized");
478 return INVALID_OPERATION;
479 }
480
481 if (SyncFeatures::getInstance().useWaitSync() &&
482 SyncFeatures::getInstance().useNativeFenceSync()) {
483 // Block GPU on the fence.
484 // Create an EGLSyncKHR from the current fence.
485 int fenceFd = fence->dup();
486 if (fenceFd == -1) {
487 ALOGE("EglManager::fenceWait: error dup'ing fence fd: %d", errno);
488 return -errno;
489 }
490 EGLint attribs[] = {
491 EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd,
492 EGL_NONE
493 };
494 EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay,
495 EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
496 if (sync == EGL_NO_SYNC_KHR) {
497 close(fenceFd);
498 ALOGE("EglManager::fenceWait: error creating EGL fence: %#x", eglGetError());
499 return UNKNOWN_ERROR;
500 }
501
502 // XXX: The spec draft is inconsistent as to whether this should
503 // return an EGLint or void. Ignore the return value for now, as
504 // it's not strictly needed.
505 eglWaitSyncKHR(mEglDisplay, sync, 0);
506 EGLint eglErr = eglGetError();
507 eglDestroySyncKHR(mEglDisplay, sync);
508 if (eglErr != EGL_SUCCESS) {
509 ALOGE("EglManager::fenceWait: error waiting for EGL fence: %#x", eglErr);
510 return UNKNOWN_ERROR;
511 }
512 } else {
513 // Block CPU on the fence.
514 status_t err = fence->waitForever("EglManager::fenceWait");
515 if (err != NO_ERROR) {
516 ALOGE("EglManager::fenceWait: error waiting for fence: %d", err);
517 return err;
518 }
519 }
520 return OK;
521}
522
523status_t EglManager::createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence,
524 sp<Fence>& nativeFence) {
525 if (!hasEglContext()) {
526 ALOGE("EglManager::createReleaseFence: EGLDisplay not initialized");
527 return INVALID_OPERATION;
528 }
529
530 if (SyncFeatures::getInstance().useNativeFenceSync()) {
531 EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay,
532 EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
533 if (sync == EGL_NO_SYNC_KHR) {
534 ALOGE("EglManager::createReleaseFence: error creating EGL fence: %#x",
535 eglGetError());
536 return UNKNOWN_ERROR;
537 }
538 glFlush();
539 int fenceFd = eglDupNativeFenceFDANDROID(mEglDisplay, sync);
540 eglDestroySyncKHR(mEglDisplay, sync);
541 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
542 ALOGE("EglManager::createReleaseFence: error dup'ing native fence "
543 "fd: %#x", eglGetError());
544 return UNKNOWN_ERROR;
545 }
546 nativeFence = new Fence(fenceFd);
547 *eglFence = EGL_NO_SYNC_KHR;
548 } else if (useFenceSync && SyncFeatures::getInstance().useFenceSync()) {
549 if (*eglFence != EGL_NO_SYNC_KHR) {
550 // There is already a fence for the current slot. We need to
551 // wait on that before replacing it with another fence to
552 // ensure that all outstanding buffer accesses have completed
553 // before the producer accesses it.
554 EGLint result = eglClientWaitSyncKHR(mEglDisplay, *eglFence, 0, 1000000000);
555 if (result == EGL_FALSE) {
556 ALOGE("EglManager::createReleaseFence: error waiting for previous fence: %#x",
557 eglGetError());
558 return UNKNOWN_ERROR;
559 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
560 ALOGE("EglManager::createReleaseFence: timeout waiting for previous fence");
561 return TIMED_OUT;
562 }
563 eglDestroySyncKHR(mEglDisplay, *eglFence);
564 }
565
566 // Create a fence for the outstanding accesses in the current
567 // OpenGL ES context.
568 *eglFence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, nullptr);
569 if (*eglFence == EGL_NO_SYNC_KHR) {
570 ALOGE("EglManager::createReleaseFence: error creating fence: %#x", eglGetError());
571 return UNKNOWN_ERROR;
572 }
573 glFlush();
574 }
575 return OK;
576}
577
John Reck3b202512014-06-23 13:13:08 -0700578} /* namespace renderthread */
579} /* namespace uirenderer */
580} /* namespace android */