blob: cd21822df5b1658191b6982382f25962c56d80bd [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>
John Reck1e510712018-04-23 08:15:03 -070021#include <utils/Trace.h>
John Reck1bcacfd2017-11-03 10:12:19 -070022#include "utils/StringUtils.h"
Mark Salyzyn96bf5982016-09-28 16:15:30 -070023
John Reck704bed02015-11-05 09:22:17 -080024#include "DeviceInfo.h"
Greg Danielcd558522016-11-17 13:31:40 -050025#include "Frame.h"
John Reckd04794a2015-05-08 10:04:36 -070026#include "Properties.h"
Mark Salyzyn173215d2017-01-12 08:27:11 -080027
John Reck55156372015-01-21 07:46:37 -080028#include <EGL/eglext.h>
John Reck149173d2015-08-10 09:52:29 -070029
John Reck1e510712018-04-23 08:15:03 -070030#include <string>
31#include <vector>
Derek Sollenberger7e044fe2016-11-07 10:57:59 -050032
John Reck3b202512014-06-23 13:13:08 -070033#define GLES_VERSION 2
34
35// Android-specific addition that is used to show when frames began in systrace
36EGLAPI void EGLAPIENTRY eglBeginFrame(EGLDisplay dpy, EGLSurface surface);
37
38namespace android {
39namespace uirenderer {
40namespace renderthread {
41
John Reck1bcacfd2017-11-03 10:12:19 -070042#define ERROR_CASE(x) \
43 case x: \
44 return #x;
John Reck3b202512014-06-23 13:13:08 -070045static const char* egl_error_str(EGLint error) {
46 switch (error) {
47 ERROR_CASE(EGL_SUCCESS)
48 ERROR_CASE(EGL_NOT_INITIALIZED)
49 ERROR_CASE(EGL_BAD_ACCESS)
50 ERROR_CASE(EGL_BAD_ALLOC)
51 ERROR_CASE(EGL_BAD_ATTRIBUTE)
52 ERROR_CASE(EGL_BAD_CONFIG)
53 ERROR_CASE(EGL_BAD_CONTEXT)
54 ERROR_CASE(EGL_BAD_CURRENT_SURFACE)
55 ERROR_CASE(EGL_BAD_DISPLAY)
56 ERROR_CASE(EGL_BAD_MATCH)
57 ERROR_CASE(EGL_BAD_NATIVE_PIXMAP)
58 ERROR_CASE(EGL_BAD_NATIVE_WINDOW)
59 ERROR_CASE(EGL_BAD_PARAMETER)
60 ERROR_CASE(EGL_BAD_SURFACE)
61 ERROR_CASE(EGL_CONTEXT_LOST)
John Reck1bcacfd2017-11-03 10:12:19 -070062 default:
63 return "Unknown error";
John Reck3b202512014-06-23 13:13:08 -070064 }
65}
sergeyv694d4992016-10-27 10:23:13 -070066const char* EglManager::eglErrorString() {
John Reck3b202512014-06-23 13:13:08 -070067 return egl_error_str(eglGetError());
68}
69
John Reck149173d2015-08-10 09:52:29 -070070static struct {
71 bool bufferAge = false;
72 bool setDamage = false;
Romain Guy26a2b972017-04-17 09:39:51 -070073 bool noConfigContext = false;
74 bool pixelFormatFloat = false;
75 bool glColorSpace = false;
Romain Guy26b6a642017-07-11 09:48:28 -070076 bool scRGB = false;
Jorim Jaggi767e25e2018-04-04 23:07:35 +020077 bool contextPriority = false;
John Reck1e510712018-04-23 08:15:03 -070078 bool surfacelessContext = false;
John Reck149173d2015-08-10 09:52:29 -070079} EglExtensions;
80
John Reck1e510712018-04-23 08:15:03 -070081EglManager::EglManager()
82 : mEglDisplay(EGL_NO_DISPLAY)
Chris Craikd41c4d82015-01-05 15:51:13 -080083 , mEglConfig(nullptr)
Romain Guy26a2b972017-04-17 09:39:51 -070084 , mEglConfigWideGamut(nullptr)
John Reck3b202512014-06-23 13:13:08 -070085 , mEglContext(EGL_NO_CONTEXT)
86 , mPBufferSurface(EGL_NO_SURFACE)
John Reck1bcacfd2017-11-03 10:12:19 -070087 , mCurrentSurface(EGL_NO_SURFACE) {}
John Reck3b202512014-06-23 13:13:08 -070088
John Reck1e510712018-04-23 08:15:03 -070089EglManager::~EglManager() {
90 destroy();
91}
92
John Reck3b202512014-06-23 13:13:08 -070093void EglManager::initialize() {
94 if (hasEglContext()) return;
95
John Reckfbc8df02014-11-14 16:18:41 -080096 ATRACE_NAME("Creating EGLContext");
97
John Reck3b202512014-06-23 13:13:08 -070098 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
John Reck1bcacfd2017-11-03 10:12:19 -070099 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, "Failed to get EGL_DEFAULT_DISPLAY! err=%s",
100 eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700101
102 EGLint major, minor;
103 LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE,
John Reck1bcacfd2017-11-03 10:12:19 -0700104 "Failed to initialize display %p! err=%s", mEglDisplay, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700105
106 ALOGI("Initialized EGL, version %d.%d", (int)major, (int)minor);
107
John Reck149173d2015-08-10 09:52:29 -0700108 initExtensions();
Season Li13d1b4a2015-07-29 17:16:19 -0700109
John Reck149173d2015-08-10 09:52:29 -0700110 // Now that extensions are loaded, pick a swap behavior
111 if (Properties::enablePartialUpdates) {
Matt Sarettb77c94a2016-12-07 12:22:37 -0500112 // An Adreno driver bug is causing rendering problems for SkiaGL with
113 // buffer age swap behavior (b/31957043). To temporarily workaround,
114 // we will use preserved swap behavior.
Derek Sollenbergerb5a12bd2017-06-14 15:23:19 -0400115 if (Properties::useBufferAge && EglExtensions.bufferAge) {
John Reck149173d2015-08-10 09:52:29 -0700116 mSwapBehavior = SwapBehavior::BufferAge;
117 } else {
118 mSwapBehavior = SwapBehavior::Preserved;
119 }
120 }
121
Romain Guy26a2b972017-04-17 09:39:51 -0700122 loadConfigs();
John Reck3b202512014-06-23 13:13:08 -0700123 createContext();
John Reckd7db4d72015-05-20 07:18:55 -0700124 createPBufferSurface();
John Reck1e510712018-04-23 08:15:03 -0700125 makeCurrent(mPBufferSurface, nullptr, /* force */ true);
John Reck704bed02015-11-05 09:22:17 -0800126 DeviceInfo::initialize();
John Reck3b202512014-06-23 13:13:08 -0700127}
128
John Reck149173d2015-08-10 09:52:29 -0700129void EglManager::initExtensions() {
John Reck1bcacfd2017-11-03 10:12:19 -0700130 auto extensions = StringUtils::split(eglQueryString(mEglDisplay, EGL_EXTENSIONS));
Romain Guy26a2b972017-04-17 09:39:51 -0700131
John Reck8733bff2016-09-27 14:45:28 -0700132 // For our purposes we don't care if EGL_BUFFER_AGE is a result of
133 // EGL_EXT_buffer_age or EGL_KHR_partial_update as our usage is covered
134 // under EGL_KHR_partial_update and we don't need the expanded scope
135 // that EGL_EXT_buffer_age provides.
John Reck1bcacfd2017-11-03 10:12:19 -0700136 EglExtensions.bufferAge =
137 extensions.has("EGL_EXT_buffer_age") || extensions.has("EGL_KHR_partial_update");
Chris Craik6e6646c2015-09-14 15:54:12 -0700138 EglExtensions.setDamage = extensions.has("EGL_KHR_partial_update");
John Reck708b6682015-10-22 13:11:00 -0700139 LOG_ALWAYS_FATAL_IF(!extensions.has("EGL_KHR_swap_buffers_with_damage"),
John Reck1bcacfd2017-11-03 10:12:19 -0700140 "Missing required extension EGL_KHR_swap_buffers_with_damage");
Romain Guy26a2b972017-04-17 09:39:51 -0700141
142 EglExtensions.glColorSpace = extensions.has("EGL_KHR_gl_colorspace");
143 EglExtensions.noConfigContext = extensions.has("EGL_KHR_no_config_context");
144 EglExtensions.pixelFormatFloat = extensions.has("EGL_EXT_pixel_format_float");
Romain Guy26b6a642017-07-11 09:48:28 -0700145#ifdef ANDROID_ENABLE_LINEAR_BLENDING
146 EglExtensions.scRGB = extensions.has("EGL_EXT_gl_colorspace_scrgb_linear");
147#else
148 EglExtensions.scRGB = extensions.has("EGL_EXT_gl_colorspace_scrgb");
149#endif
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200150 EglExtensions.contextPriority = extensions.has("EGL_IMG_context_priority");
John Reck1e510712018-04-23 08:15:03 -0700151 EglExtensions.surfacelessContext = extensions.has("EGL_KHR_surfaceless_context");
John Reck149173d2015-08-10 09:52:29 -0700152}
153
John Reck3b202512014-06-23 13:13:08 -0700154bool EglManager::hasEglContext() {
155 return mEglDisplay != EGL_NO_DISPLAY;
156}
157
Romain Guy26a2b972017-04-17 09:39:51 -0700158void EglManager::loadConfigs() {
John Reck149173d2015-08-10 09:52:29 -0700159 ALOGD("Swap behavior %d", static_cast<int>(mSwapBehavior));
John Reck1bcacfd2017-11-03 10:12:19 -0700160 EGLint swapBehavior =
161 (mSwapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
162 EGLint attribs[] = {EGL_RENDERABLE_TYPE,
163 EGL_OPENGL_ES2_BIT,
164 EGL_RED_SIZE,
165 8,
166 EGL_GREEN_SIZE,
167 8,
168 EGL_BLUE_SIZE,
169 8,
170 EGL_ALPHA_SIZE,
171 8,
172 EGL_DEPTH_SIZE,
173 0,
174 EGL_CONFIG_CAVEAT,
175 EGL_NONE,
176 EGL_STENCIL_SIZE,
John Reck1e510712018-04-23 08:15:03 -0700177 STENCIL_BUFFER_SIZE,
John Reck1bcacfd2017-11-03 10:12:19 -0700178 EGL_SURFACE_TYPE,
179 EGL_WINDOW_BIT | swapBehavior,
180 EGL_NONE};
John Reck3b202512014-06-23 13:13:08 -0700181
Romain Guy26a2b972017-04-17 09:39:51 -0700182 EGLint numConfigs = 1;
John Reck1bcacfd2017-11-03 10:12:19 -0700183 if (!eglChooseConfig(mEglDisplay, attribs, &mEglConfig, numConfigs, &numConfigs) ||
184 numConfigs != 1) {
John Reck149173d2015-08-10 09:52:29 -0700185 if (mSwapBehavior == SwapBehavior::Preserved) {
John Reck3b202512014-06-23 13:13:08 -0700186 // Try again without dirty regions enabled
John Reck149173d2015-08-10 09:52:29 -0700187 ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...");
188 mSwapBehavior = SwapBehavior::Discard;
Romain Guy26a2b972017-04-17 09:39:51 -0700189 loadConfigs();
John Reck1bcacfd2017-11-03 10:12:19 -0700190 return; // the call to loadConfigs() we just made picks the wide gamut config
John Reck3b202512014-06-23 13:13:08 -0700191 } else {
John Reck149173d2015-08-10 09:52:29 -0700192 // Failed to get a valid config
sergeyv694d4992016-10-27 10:23:13 -0700193 LOG_ALWAYS_FATAL("Failed to choose config, error = %s", eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700194 }
195 }
Romain Guy26a2b972017-04-17 09:39:51 -0700196
197 if (EglExtensions.pixelFormatFloat) {
198 // If we reached this point, we have a valid swap behavior
John Reck1bcacfd2017-11-03 10:12:19 -0700199 EGLint attribs16F[] = {EGL_RENDERABLE_TYPE,
200 EGL_OPENGL_ES2_BIT,
201 EGL_COLOR_COMPONENT_TYPE_EXT,
202 EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT,
203 EGL_RED_SIZE,
204 16,
205 EGL_GREEN_SIZE,
206 16,
207 EGL_BLUE_SIZE,
208 16,
209 EGL_ALPHA_SIZE,
210 16,
211 EGL_DEPTH_SIZE,
212 0,
213 EGL_STENCIL_SIZE,
John Reck1e510712018-04-23 08:15:03 -0700214 STENCIL_BUFFER_SIZE,
John Reck1bcacfd2017-11-03 10:12:19 -0700215 EGL_SURFACE_TYPE,
216 EGL_WINDOW_BIT | swapBehavior,
217 EGL_NONE};
Romain Guy26a2b972017-04-17 09:39:51 -0700218
219 numConfigs = 1;
John Reck1bcacfd2017-11-03 10:12:19 -0700220 if (!eglChooseConfig(mEglDisplay, attribs16F, &mEglConfigWideGamut, numConfigs,
221 &numConfigs) ||
222 numConfigs != 1) {
Rob Herring74883ab2017-11-29 09:26:31 -0600223 ALOGE("Device claims wide gamut support, cannot find matching config, error = %s",
John Reck1a4a9812018-04-18 16:13:31 -0700224 eglErrorString());
Rob Herring74883ab2017-11-29 09:26:31 -0600225 EglExtensions.pixelFormatFloat = false;
Romain Guy26a2b972017-04-17 09:39:51 -0700226 }
227 }
John Reck3b202512014-06-23 13:13:08 -0700228}
229
230void EglManager::createContext() {
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200231 std::vector<EGLint> contextAttributes;
232 contextAttributes.reserve(5);
233 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
234 contextAttributes.push_back(GLES_VERSION);
235 if (Properties::contextPriority != 0 && EglExtensions.contextPriority) {
236 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
237 contextAttributes.push_back(Properties::contextPriority);
238 }
239 contextAttributes.push_back(EGL_NONE);
John Reck1bcacfd2017-11-03 10:12:19 -0700240 mEglContext = eglCreateContext(
241 mEglDisplay, EglExtensions.noConfigContext ? ((EGLConfig) nullptr) : mEglConfig,
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200242 EGL_NO_CONTEXT, contextAttributes.data());
John Reck1bcacfd2017-11-03 10:12:19 -0700243 LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, "Failed to create context, error = %s",
244 eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700245}
246
John Reckd7db4d72015-05-20 07:18:55 -0700247void EglManager::createPBufferSurface() {
John Reck3b202512014-06-23 13:13:08 -0700248 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
John Reck1bcacfd2017-11-03 10:12:19 -0700249 "usePBufferSurface() called on uninitialized GlobalContext!");
John Reck3b202512014-06-23 13:13:08 -0700250
John Reck1e510712018-04-23 08:15:03 -0700251 if (mPBufferSurface == EGL_NO_SURFACE && !EglExtensions.surfacelessContext) {
John Reck1bcacfd2017-11-03 10:12:19 -0700252 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
John Reck3b202512014-06-23 13:13:08 -0700253 mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs);
254 }
John Reck3b202512014-06-23 13:13:08 -0700255}
256
Romain Guy26a2b972017-04-17 09:39:51 -0700257EGLSurface EglManager::createSurface(EGLNativeWindowType window, bool wideColorGamut) {
John Reck1e510712018-04-23 08:15:03 -0700258 LOG_ALWAYS_FATAL_IF(!hasEglContext(), "Not initialized");
Romain Guy253f2c22016-09-28 17:34:42 -0700259
John Reck1bcacfd2017-11-03 10:12:19 -0700260 wideColorGamut = wideColorGamut && EglExtensions.glColorSpace && EglExtensions.scRGB &&
261 EglExtensions.pixelFormatFloat && EglExtensions.noConfigContext;
Romain Guy26a2b972017-04-17 09:39:51 -0700262
263 // The color space we want to use depends on whether linear blending is turned
264 // on and whether the app has requested wide color gamut rendering. When wide
265 // color gamut rendering is off, the app simply renders in the display's native
266 // color gamut.
267 //
268 // When wide gamut rendering is off:
269 // - Blending is done by default in gamma space, which requires using a
270 // linear EGL color space (the GPU uses the color values as is)
271 // - If linear blending is on, we must use the sRGB EGL color space (the
272 // GPU will perform sRGB to linear and linear to SRGB conversions before
273 // and after blending)
274 //
275 // When wide gamut rendering is on we cannot rely on the GPU performing
276 // linear blending for us. We use two different color spaces to tag the
277 // surface appropriately for SurfaceFlinger:
278 // - Gamma blending (default) requires the use of the scRGB-nl color space
279 // - Linear blending requires the use of the scRGB color space
280
281 // Not all Android targets support the EGL_GL_COLOR_SPACE_KHR extension
282 // We insert to placeholders to set EGL_GL_COLORSPACE_KHR and its value.
283 // According to section 3.4.1 of the EGL specification, the attributes
284 // list is considered empty if the first entry is EGL_NONE
John Reck1bcacfd2017-11-03 10:12:19 -0700285 EGLint attribs[] = {EGL_NONE, EGL_NONE, EGL_NONE};
Romain Guy253f2c22016-09-28 17:34:42 -0700286
Romain Guy26a2b972017-04-17 09:39:51 -0700287 if (EglExtensions.glColorSpace) {
288 attribs[0] = EGL_GL_COLORSPACE_KHR;
289#ifdef ANDROID_ENABLE_LINEAR_BLENDING
290 if (wideColorGamut) {
291 attribs[1] = EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT;
292 } else {
293 attribs[1] = EGL_GL_COLORSPACE_SRGB_KHR;
294 }
295#else
296 if (wideColorGamut) {
Romain Guy26b6a642017-07-11 09:48:28 -0700297 attribs[1] = EGL_GL_COLORSPACE_SCRGB_EXT;
Romain Guy26a2b972017-04-17 09:39:51 -0700298 } else {
299 attribs[1] = EGL_GL_COLORSPACE_LINEAR_KHR;
300 }
301#endif
302 }
303
John Reck1bcacfd2017-11-03 10:12:19 -0700304 EGLSurface surface = eglCreateWindowSurface(
305 mEglDisplay, wideColorGamut ? mEglConfigWideGamut : mEglConfig, window, attribs);
John Reck3b202512014-06-23 13:13:08 -0700306 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE,
John Reck1bcacfd2017-11-03 10:12:19 -0700307 "Failed to create EGLSurface for window %p, eglErr = %s", (void*)window,
308 eglErrorString());
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000309
310 if (mSwapBehavior != SwapBehavior::Preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700311 LOG_ALWAYS_FATAL_IF(eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
312 EGL_BUFFER_DESTROYED) == EGL_FALSE,
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000313 "Failed to set swap behavior to destroyed for window %p, eglErr = %s",
John Reck1bcacfd2017-11-03 10:12:19 -0700314 (void*)window, eglErrorString());
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000315 }
316
John Reck3b202512014-06-23 13:13:08 -0700317 return surface;
318}
319
320void EglManager::destroySurface(EGLSurface surface) {
321 if (isCurrent(surface)) {
322 makeCurrent(EGL_NO_SURFACE);
323 }
324 if (!eglDestroySurface(mEglDisplay, surface)) {
sergeyv694d4992016-10-27 10:23:13 -0700325 ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700326 }
327}
328
329void EglManager::destroy() {
330 if (mEglDisplay == EGL_NO_DISPLAY) return;
331
John Reck3b202512014-06-23 13:13:08 -0700332 eglDestroyContext(mEglDisplay, mEglContext);
John Reck1e510712018-04-23 08:15:03 -0700333 if (mPBufferSurface != EGL_NO_SURFACE) {
334 eglDestroySurface(mEglDisplay, mPBufferSurface);
335 }
John Reck3b202512014-06-23 13:13:08 -0700336 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
337 eglTerminate(mEglDisplay);
338 eglReleaseThread();
339
340 mEglDisplay = EGL_NO_DISPLAY;
341 mEglContext = EGL_NO_CONTEXT;
342 mPBufferSurface = EGL_NO_SURFACE;
343 mCurrentSurface = EGL_NO_SURFACE;
344}
345
John Reck1e510712018-04-23 08:15:03 -0700346bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut, bool force) {
347 if (!force && isCurrent(surface)) return false;
John Reck3b202512014-06-23 13:13:08 -0700348
349 if (surface == EGL_NO_SURFACE) {
John Reckd7db4d72015-05-20 07:18:55 -0700350 // Ensure we always have a valid surface & context
351 surface = mPBufferSurface;
352 }
353 if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) {
John Reckf2dcc2a2015-07-16 09:17:59 -0700354 if (errOut) {
355 *errOut = eglGetError();
John Reck1bcacfd2017-11-03 10:12:19 -0700356 ALOGW("Failed to make current on surface %p, error=%s", (void*)surface,
357 egl_error_str(*errOut));
John Reckf2dcc2a2015-07-16 09:17:59 -0700358 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700359 LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", (void*)surface,
360 eglErrorString());
John Reckf2dcc2a2015-07-16 09:17:59 -0700361 }
John Reck3b202512014-06-23 13:13:08 -0700362 }
363 mCurrentSurface = surface;
John Recka8963062017-06-14 10:47:50 -0700364 if (Properties::disableVsync) {
365 eglSwapInterval(mEglDisplay, 0);
366 }
John Reck3b202512014-06-23 13:13:08 -0700367 return true;
368}
369
John Reck149173d2015-08-10 09:52:29 -0700370EGLint EglManager::queryBufferAge(EGLSurface surface) {
371 switch (mSwapBehavior) {
John Reck1bcacfd2017-11-03 10:12:19 -0700372 case SwapBehavior::Discard:
373 return 0;
374 case SwapBehavior::Preserved:
375 return 1;
376 case SwapBehavior::BufferAge:
377 EGLint bufferAge;
378 eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge);
379 return bufferAge;
John Reck149173d2015-08-10 09:52:29 -0700380 }
381 return 0;
382}
383
384Frame EglManager::beginFrame(EGLSurface surface) {
John Reck1bcacfd2017-11-03 10:12:19 -0700385 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, "Tried to beginFrame on EGL_NO_SURFACE!");
John Reck3b202512014-06-23 13:13:08 -0700386 makeCurrent(surface);
John Reck149173d2015-08-10 09:52:29 -0700387 Frame frame;
388 frame.mSurface = surface;
389 eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth);
390 eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight);
391 frame.mBufferAge = queryBufferAge(surface);
John Reck3b202512014-06-23 13:13:08 -0700392 eglBeginFrame(mEglDisplay, surface);
John Reck149173d2015-08-10 09:52:29 -0700393 return frame;
John Reck3b202512014-06-23 13:13:08 -0700394}
395
John Reck149173d2015-08-10 09:52:29 -0700396void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) {
397#ifdef EGL_KHR_partial_update
398 if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) {
399 EGLint rects[4];
400 frame.map(dirty, rects);
401 if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) {
402 LOG_ALWAYS_FATAL("Failed to set damage region on surface %p, error=%s",
John Reck1bcacfd2017-11-03 10:12:19 -0700403 (void*)frame.mSurface, eglErrorString());
John Reck149173d2015-08-10 09:52:29 -0700404 }
405 }
406#endif
407}
408
John Reckc96955d2016-02-26 14:56:44 -0800409bool EglManager::damageRequiresSwap() {
410 return EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge;
411}
412
John Reck149173d2015-08-10 09:52:29 -0700413bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) {
John Reck682573c2015-10-30 10:37:35 -0700414 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
John Reck55156372015-01-21 07:46:37 -0800415 ATRACE_NAME("Finishing GPU work");
416 fence();
417 }
John Reck55156372015-01-21 07:46:37 -0800418
John Recka672f6b2015-10-22 09:53:26 -0700419 EGLint rects[4];
420 frame.map(screenDirty, rects);
John Reck1bcacfd2017-11-03 10:12:19 -0700421 eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects, screenDirty.isEmpty() ? 0 : 1);
John Reckd04794a2015-05-08 10:04:36 -0700422
John Reck3b202512014-06-23 13:13:08 -0700423 EGLint err = eglGetError();
John Reck2cdbc7d2014-09-17 16:06:36 -0700424 if (CC_LIKELY(err == EGL_SUCCESS)) {
425 return true;
426 }
John Reckc2547fa2015-10-26 13:52:52 -0700427 if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) {
John Reck2cdbc7d2014-09-17 16:06:36 -0700428 // For some reason our surface was destroyed out from under us
429 // This really shouldn't happen, but if it does we can recover easily
430 // by just not trying to use the surface anymore
John Reck1bcacfd2017-11-03 10:12:19 -0700431 ALOGW("swapBuffers encountered EGL error %d on %p, halting rendering...", err,
432 frame.mSurface);
John Reck2cdbc7d2014-09-17 16:06:36 -0700433 return false;
434 }
John Reck1bcacfd2017-11-03 10:12:19 -0700435 LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering", err, egl_error_str(err));
John Reck2cdbc7d2014-09-17 16:06:36 -0700436 // Impossible to hit this, but the compiler doesn't know that
437 return false;
John Reck3b202512014-06-23 13:13:08 -0700438}
439
John Reck55156372015-01-21 07:46:37 -0800440void EglManager::fence() {
441 EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL);
John Reck1bcacfd2017-11-03 10:12:19 -0700442 eglClientWaitSyncKHR(mEglDisplay, fence, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR);
John Reck55156372015-01-21 07:46:37 -0800443 eglDestroySyncKHR(mEglDisplay, fence);
444}
445
John Reck1125d1f2014-10-23 11:02:19 -0700446bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) {
John Reck149173d2015-08-10 09:52:29 -0700447 if (mSwapBehavior != SwapBehavior::Preserved) return false;
John Reck3b202512014-06-23 13:13:08 -0700448
John Reck149173d2015-08-10 09:52:29 -0700449 bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
John Reck1bcacfd2017-11-03 10:12:19 -0700450 preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED);
John Reck149173d2015-08-10 09:52:29 -0700451 if (!preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700452 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", (void*)surface,
453 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700454 // Maybe it's already set?
455 EGLint swapBehavior;
456 if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) {
457 preserved = (swapBehavior == EGL_BUFFER_PRESERVED);
458 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700459 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", (void*)surface,
460 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700461 }
John Reck3b202512014-06-23 13:13:08 -0700462 }
John Reck1125d1f2014-10-23 11:02:19 -0700463
464 return preserved;
John Reck3b202512014-06-23 13:13:08 -0700465}
466
John Reck3b202512014-06-23 13:13:08 -0700467} /* namespace renderthread */
468} /* namespace uirenderer */
469} /* namespace android */