blob: 3a49bebe7936e2f7ca2c63219ebd8cdf49720313 [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 <string>
20
Mark Salyzyn96bf5982016-09-28 16:15:30 -070021#include <cutils/properties.h>
22#include <log/log.h>
John Reck1bcacfd2017-11-03 10:12:19 -070023#include "utils/StringUtils.h"
Mark Salyzyn96bf5982016-09-28 16:15:30 -070024
John Reckd04794a2015-05-08 10:04:36 -070025#include "Caches.h"
John Reck704bed02015-11-05 09:22:17 -080026#include "DeviceInfo.h"
Greg Danielcd558522016-11-17 13:31:40 -050027#include "Frame.h"
John Reckd04794a2015-05-08 10:04:36 -070028#include "Properties.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080029#include "RenderThread.h"
Mark Salyzyn173215d2017-01-12 08:27:11 -080030#include "Texture.h"
John Reck1bcacfd2017-11-03 10:12:19 -070031#include "renderstate/RenderState.h"
Mark Salyzyn173215d2017-01-12 08:27:11 -080032
John Reck55156372015-01-21 07:46:37 -080033#include <EGL/eglext.h>
Derek Sollenberger98f75d52016-10-25 10:25:45 -040034#include <GrContextOptions.h>
35#include <gl/GrGLInterface.h>
John Reck149173d2015-08-10 09:52:29 -070036
Derek Sollenberger7e044fe2016-11-07 10:57:59 -050037#ifdef HWUI_GLES_WRAP_ENABLED
38#include "debug/GlesDriver.h"
39#endif
40
John Reck3b202512014-06-23 13:13:08 -070041#define GLES_VERSION 2
42
43// Android-specific addition that is used to show when frames began in systrace
44EGLAPI void EGLAPIENTRY eglBeginFrame(EGLDisplay dpy, EGLSurface surface);
45
46namespace android {
47namespace uirenderer {
48namespace renderthread {
49
John Reck1bcacfd2017-11-03 10:12:19 -070050#define ERROR_CASE(x) \
51 case x: \
52 return #x;
John Reck3b202512014-06-23 13:13:08 -070053static const char* egl_error_str(EGLint error) {
54 switch (error) {
55 ERROR_CASE(EGL_SUCCESS)
56 ERROR_CASE(EGL_NOT_INITIALIZED)
57 ERROR_CASE(EGL_BAD_ACCESS)
58 ERROR_CASE(EGL_BAD_ALLOC)
59 ERROR_CASE(EGL_BAD_ATTRIBUTE)
60 ERROR_CASE(EGL_BAD_CONFIG)
61 ERROR_CASE(EGL_BAD_CONTEXT)
62 ERROR_CASE(EGL_BAD_CURRENT_SURFACE)
63 ERROR_CASE(EGL_BAD_DISPLAY)
64 ERROR_CASE(EGL_BAD_MATCH)
65 ERROR_CASE(EGL_BAD_NATIVE_PIXMAP)
66 ERROR_CASE(EGL_BAD_NATIVE_WINDOW)
67 ERROR_CASE(EGL_BAD_PARAMETER)
68 ERROR_CASE(EGL_BAD_SURFACE)
69 ERROR_CASE(EGL_CONTEXT_LOST)
John Reck1bcacfd2017-11-03 10:12:19 -070070 default:
71 return "Unknown error";
John Reck3b202512014-06-23 13:13:08 -070072 }
73}
sergeyv694d4992016-10-27 10:23:13 -070074const char* EglManager::eglErrorString() {
John Reck3b202512014-06-23 13:13:08 -070075 return egl_error_str(eglGetError());
76}
77
John Reck149173d2015-08-10 09:52:29 -070078static struct {
79 bool bufferAge = false;
80 bool setDamage = false;
Romain Guy26a2b972017-04-17 09:39:51 -070081 bool noConfigContext = false;
82 bool pixelFormatFloat = false;
83 bool glColorSpace = false;
Romain Guy26b6a642017-07-11 09:48:28 -070084 bool scRGB = false;
Jorim Jaggi767e25e2018-04-04 23:07:35 +020085 bool contextPriority = false;
John Reckec100972018-04-05 16:41:41 -070086 bool presentTime = false;
John Reck149173d2015-08-10 09:52:29 -070087} EglExtensions;
88
John Reck3b202512014-06-23 13:13:08 -070089EglManager::EglManager(RenderThread& thread)
90 : mRenderThread(thread)
91 , mEglDisplay(EGL_NO_DISPLAY)
Chris Craikd41c4d82015-01-05 15:51:13 -080092 , mEglConfig(nullptr)
Romain Guy26a2b972017-04-17 09:39:51 -070093 , mEglConfigWideGamut(nullptr)
John Reck3b202512014-06-23 13:13:08 -070094 , mEglContext(EGL_NO_CONTEXT)
95 , mPBufferSurface(EGL_NO_SURFACE)
John Reck1bcacfd2017-11-03 10:12:19 -070096 , mCurrentSurface(EGL_NO_SURFACE) {}
John Reck3b202512014-06-23 13:13:08 -070097
98void 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
111 ALOGI("Initialized EGL, version %d.%d", (int)major, (int)minor);
112
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();
130 makeCurrent(mPBufferSurface);
John Reck704bed02015-11-05 09:22:17 -0800131 DeviceInfo::initialize();
John Reck3b202512014-06-23 13:13:08 -0700132 mRenderThread.renderState().onGLContextCreated();
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400133
134 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
Derek Sollenberger7e044fe2016-11-07 10:57:59 -0500135#ifdef HWUI_GLES_WRAP_ENABLED
136 debug::GlesDriver* driver = debug::GlesDriver::get();
137 sk_sp<const GrGLInterface> glInterface(driver->getSkiaInterface());
138#else
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400139 sk_sp<const GrGLInterface> glInterface(GrGLCreateNativeInterface());
Derek Sollenberger7e044fe2016-11-07 10:57:59 -0500140#endif
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400141 LOG_ALWAYS_FATAL_IF(!glInterface.get());
142
143 GrContextOptions options;
Brian Osmanda6ad832017-08-29 16:43:43 -0400144 options.fDisableDistanceFieldPaths = true;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400145 mRenderThread.cacheManager().configureContext(&options);
Greg Daniel660d6ec2017-12-08 11:44:27 -0500146 sk_sp<GrContext> grContext(GrContext::MakeGL(std::move(glInterface), options));
147 LOG_ALWAYS_FATAL_IF(!grContext.get());
148 mRenderThread.setGrContext(grContext);
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400149 }
John Reck3b202512014-06-23 13:13:08 -0700150}
151
John Reck149173d2015-08-10 09:52:29 -0700152void EglManager::initExtensions() {
John Reck1bcacfd2017-11-03 10:12:19 -0700153 auto extensions = StringUtils::split(eglQueryString(mEglDisplay, EGL_EXTENSIONS));
Romain Guy26a2b972017-04-17 09:39:51 -0700154
John Reck8733bff2016-09-27 14:45:28 -0700155 // For our purposes we don't care if EGL_BUFFER_AGE is a result of
156 // EGL_EXT_buffer_age or EGL_KHR_partial_update as our usage is covered
157 // under EGL_KHR_partial_update and we don't need the expanded scope
158 // that EGL_EXT_buffer_age provides.
John Reck1bcacfd2017-11-03 10:12:19 -0700159 EglExtensions.bufferAge =
160 extensions.has("EGL_EXT_buffer_age") || extensions.has("EGL_KHR_partial_update");
Chris Craik6e6646c2015-09-14 15:54:12 -0700161 EglExtensions.setDamage = extensions.has("EGL_KHR_partial_update");
John Reck708b6682015-10-22 13:11:00 -0700162 LOG_ALWAYS_FATAL_IF(!extensions.has("EGL_KHR_swap_buffers_with_damage"),
John Reck1bcacfd2017-11-03 10:12:19 -0700163 "Missing required extension EGL_KHR_swap_buffers_with_damage");
Romain Guy26a2b972017-04-17 09:39:51 -0700164
165 EglExtensions.glColorSpace = extensions.has("EGL_KHR_gl_colorspace");
166 EglExtensions.noConfigContext = extensions.has("EGL_KHR_no_config_context");
167 EglExtensions.pixelFormatFloat = extensions.has("EGL_EXT_pixel_format_float");
Romain Guy26b6a642017-07-11 09:48:28 -0700168#ifdef ANDROID_ENABLE_LINEAR_BLENDING
169 EglExtensions.scRGB = extensions.has("EGL_EXT_gl_colorspace_scrgb_linear");
170#else
171 EglExtensions.scRGB = extensions.has("EGL_EXT_gl_colorspace_scrgb");
172#endif
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200173 EglExtensions.contextPriority = extensions.has("EGL_IMG_context_priority");
John Reckec100972018-04-05 16:41:41 -0700174 EglExtensions.presentTime = extensions.has("EGL_ANDROID_presentation_time");
John Reck149173d2015-08-10 09:52:29 -0700175}
176
John Reck3b202512014-06-23 13:13:08 -0700177bool EglManager::hasEglContext() {
178 return mEglDisplay != EGL_NO_DISPLAY;
179}
180
Romain Guy26a2b972017-04-17 09:39:51 -0700181void EglManager::loadConfigs() {
John Reck149173d2015-08-10 09:52:29 -0700182 ALOGD("Swap behavior %d", static_cast<int>(mSwapBehavior));
John Reck1bcacfd2017-11-03 10:12:19 -0700183 EGLint swapBehavior =
184 (mSwapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
185 EGLint attribs[] = {EGL_RENDERABLE_TYPE,
186 EGL_OPENGL_ES2_BIT,
187 EGL_RED_SIZE,
188 8,
189 EGL_GREEN_SIZE,
190 8,
191 EGL_BLUE_SIZE,
192 8,
193 EGL_ALPHA_SIZE,
194 8,
195 EGL_DEPTH_SIZE,
196 0,
197 EGL_CONFIG_CAVEAT,
198 EGL_NONE,
199 EGL_STENCIL_SIZE,
200 Stencil::getStencilSize(),
201 EGL_SURFACE_TYPE,
202 EGL_WINDOW_BIT | swapBehavior,
203 EGL_NONE};
John Reck3b202512014-06-23 13:13:08 -0700204
Romain Guy26a2b972017-04-17 09:39:51 -0700205 EGLint numConfigs = 1;
John Reck1bcacfd2017-11-03 10:12:19 -0700206 if (!eglChooseConfig(mEglDisplay, attribs, &mEglConfig, numConfigs, &numConfigs) ||
207 numConfigs != 1) {
John Reck149173d2015-08-10 09:52:29 -0700208 if (mSwapBehavior == SwapBehavior::Preserved) {
John Reck3b202512014-06-23 13:13:08 -0700209 // Try again without dirty regions enabled
John Reck149173d2015-08-10 09:52:29 -0700210 ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...");
211 mSwapBehavior = SwapBehavior::Discard;
Romain Guy26a2b972017-04-17 09:39:51 -0700212 loadConfigs();
John Reck1bcacfd2017-11-03 10:12:19 -0700213 return; // the call to loadConfigs() we just made picks the wide gamut config
John Reck3b202512014-06-23 13:13:08 -0700214 } else {
John Reck149173d2015-08-10 09:52:29 -0700215 // Failed to get a valid config
sergeyv694d4992016-10-27 10:23:13 -0700216 LOG_ALWAYS_FATAL("Failed to choose config, error = %s", eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700217 }
218 }
Romain Guy26a2b972017-04-17 09:39:51 -0700219
220 if (EglExtensions.pixelFormatFloat) {
221 // If we reached this point, we have a valid swap behavior
John Reck1bcacfd2017-11-03 10:12:19 -0700222 EGLint attribs16F[] = {EGL_RENDERABLE_TYPE,
223 EGL_OPENGL_ES2_BIT,
224 EGL_COLOR_COMPONENT_TYPE_EXT,
225 EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT,
226 EGL_RED_SIZE,
227 16,
228 EGL_GREEN_SIZE,
229 16,
230 EGL_BLUE_SIZE,
231 16,
232 EGL_ALPHA_SIZE,
233 16,
234 EGL_DEPTH_SIZE,
235 0,
236 EGL_STENCIL_SIZE,
237 Stencil::getStencilSize(),
238 EGL_SURFACE_TYPE,
239 EGL_WINDOW_BIT | swapBehavior,
240 EGL_NONE};
Romain Guy26a2b972017-04-17 09:39:51 -0700241
242 numConfigs = 1;
John Reck1bcacfd2017-11-03 10:12:19 -0700243 if (!eglChooseConfig(mEglDisplay, attribs16F, &mEglConfigWideGamut, numConfigs,
244 &numConfigs) ||
245 numConfigs != 1) {
Rob Herring74883ab2017-11-29 09:26:31 -0600246 ALOGE("Device claims wide gamut support, cannot find matching config, error = %s",
John Reckec100972018-04-05 16:41:41 -0700247 eglErrorString());
Rob Herring74883ab2017-11-29 09:26:31 -0600248 EglExtensions.pixelFormatFloat = false;
Romain Guy26a2b972017-04-17 09:39:51 -0700249 }
250 }
John Reck3b202512014-06-23 13:13:08 -0700251}
252
253void EglManager::createContext() {
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200254 std::vector<EGLint> contextAttributes;
255 contextAttributes.reserve(5);
256 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
257 contextAttributes.push_back(GLES_VERSION);
258 if (Properties::contextPriority != 0 && EglExtensions.contextPriority) {
259 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
260 contextAttributes.push_back(Properties::contextPriority);
261 }
262 contextAttributes.push_back(EGL_NONE);
John Reck1bcacfd2017-11-03 10:12:19 -0700263 mEglContext = eglCreateContext(
264 mEglDisplay, EglExtensions.noConfigContext ? ((EGLConfig) nullptr) : mEglConfig,
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200265 EGL_NO_CONTEXT, contextAttributes.data());
John Reck1bcacfd2017-11-03 10:12:19 -0700266 LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, "Failed to create context, error = %s",
267 eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700268}
269
John Reckd7db4d72015-05-20 07:18:55 -0700270void EglManager::createPBufferSurface() {
John Reck3b202512014-06-23 13:13:08 -0700271 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
John Reck1bcacfd2017-11-03 10:12:19 -0700272 "usePBufferSurface() called on uninitialized GlobalContext!");
John Reck3b202512014-06-23 13:13:08 -0700273
274 if (mPBufferSurface == EGL_NO_SURFACE) {
John Reck1bcacfd2017-11-03 10:12:19 -0700275 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
John Reck3b202512014-06-23 13:13:08 -0700276 mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs);
277 }
John Reck3b202512014-06-23 13:13:08 -0700278}
279
Romain Guy26a2b972017-04-17 09:39:51 -0700280EGLSurface EglManager::createSurface(EGLNativeWindowType window, bool wideColorGamut) {
John Reck3b202512014-06-23 13:13:08 -0700281 initialize();
Romain Guy253f2c22016-09-28 17:34:42 -0700282
John Reck1bcacfd2017-11-03 10:12:19 -0700283 wideColorGamut = wideColorGamut && EglExtensions.glColorSpace && EglExtensions.scRGB &&
284 EglExtensions.pixelFormatFloat && EglExtensions.noConfigContext;
Romain Guy26a2b972017-04-17 09:39:51 -0700285
286 // The color space we want to use depends on whether linear blending is turned
287 // on and whether the app has requested wide color gamut rendering. When wide
288 // color gamut rendering is off, the app simply renders in the display's native
289 // color gamut.
290 //
291 // When wide gamut rendering is off:
292 // - Blending is done by default in gamma space, which requires using a
293 // linear EGL color space (the GPU uses the color values as is)
294 // - If linear blending is on, we must use the sRGB EGL color space (the
295 // GPU will perform sRGB to linear and linear to SRGB conversions before
296 // and after blending)
297 //
298 // When wide gamut rendering is on we cannot rely on the GPU performing
299 // linear blending for us. We use two different color spaces to tag the
300 // surface appropriately for SurfaceFlinger:
301 // - Gamma blending (default) requires the use of the scRGB-nl color space
302 // - Linear blending requires the use of the scRGB color space
303
304 // Not all Android targets support the EGL_GL_COLOR_SPACE_KHR extension
305 // We insert to placeholders to set EGL_GL_COLORSPACE_KHR and its value.
306 // According to section 3.4.1 of the EGL specification, the attributes
307 // list is considered empty if the first entry is EGL_NONE
John Reck1bcacfd2017-11-03 10:12:19 -0700308 EGLint attribs[] = {EGL_NONE, EGL_NONE, EGL_NONE};
Romain Guy253f2c22016-09-28 17:34:42 -0700309
Romain Guy26a2b972017-04-17 09:39:51 -0700310 if (EglExtensions.glColorSpace) {
311 attribs[0] = EGL_GL_COLORSPACE_KHR;
312#ifdef ANDROID_ENABLE_LINEAR_BLENDING
313 if (wideColorGamut) {
314 attribs[1] = EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT;
315 } else {
316 attribs[1] = EGL_GL_COLORSPACE_SRGB_KHR;
317 }
318#else
319 if (wideColorGamut) {
Romain Guy26b6a642017-07-11 09:48:28 -0700320 attribs[1] = EGL_GL_COLORSPACE_SCRGB_EXT;
Romain Guy26a2b972017-04-17 09:39:51 -0700321 } else {
322 attribs[1] = EGL_GL_COLORSPACE_LINEAR_KHR;
323 }
324#endif
325 }
326
John Reck1bcacfd2017-11-03 10:12:19 -0700327 EGLSurface surface = eglCreateWindowSurface(
328 mEglDisplay, wideColorGamut ? mEglConfigWideGamut : mEglConfig, window, attribs);
John Reck3b202512014-06-23 13:13:08 -0700329 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE,
John Reck1bcacfd2017-11-03 10:12:19 -0700330 "Failed to create EGLSurface for window %p, eglErr = %s", (void*)window,
331 eglErrorString());
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000332
333 if (mSwapBehavior != SwapBehavior::Preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700334 LOG_ALWAYS_FATAL_IF(eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
335 EGL_BUFFER_DESTROYED) == EGL_FALSE,
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000336 "Failed to set swap behavior to destroyed for window %p, eglErr = %s",
John Reck1bcacfd2017-11-03 10:12:19 -0700337 (void*)window, eglErrorString());
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000338 }
339
John Reck3b202512014-06-23 13:13:08 -0700340 return surface;
341}
342
343void EglManager::destroySurface(EGLSurface surface) {
344 if (isCurrent(surface)) {
345 makeCurrent(EGL_NO_SURFACE);
346 }
347 if (!eglDestroySurface(mEglDisplay, surface)) {
sergeyv694d4992016-10-27 10:23:13 -0700348 ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700349 }
350}
351
352void EglManager::destroy() {
353 if (mEglDisplay == EGL_NO_DISPLAY) return;
354
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400355 mRenderThread.setGrContext(nullptr);
Chris Craik1d477422014-08-26 17:30:15 -0700356 mRenderThread.renderState().onGLContextDestroyed();
John Reck3b202512014-06-23 13:13:08 -0700357 eglDestroyContext(mEglDisplay, mEglContext);
358 eglDestroySurface(mEglDisplay, mPBufferSurface);
359 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
360 eglTerminate(mEglDisplay);
361 eglReleaseThread();
362
363 mEglDisplay = EGL_NO_DISPLAY;
364 mEglContext = EGL_NO_CONTEXT;
365 mPBufferSurface = EGL_NO_SURFACE;
366 mCurrentSurface = EGL_NO_SURFACE;
367}
368
John Reckf2dcc2a2015-07-16 09:17:59 -0700369bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut) {
John Reck3b202512014-06-23 13:13:08 -0700370 if (isCurrent(surface)) return false;
371
372 if (surface == EGL_NO_SURFACE) {
John Reckd7db4d72015-05-20 07:18:55 -0700373 // Ensure we always have a valid surface & context
374 surface = mPBufferSurface;
375 }
376 if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) {
John Reckf2dcc2a2015-07-16 09:17:59 -0700377 if (errOut) {
378 *errOut = eglGetError();
John Reck1bcacfd2017-11-03 10:12:19 -0700379 ALOGW("Failed to make current on surface %p, error=%s", (void*)surface,
380 egl_error_str(*errOut));
John Reckf2dcc2a2015-07-16 09:17:59 -0700381 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700382 LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", (void*)surface,
383 eglErrorString());
John Reckf2dcc2a2015-07-16 09:17:59 -0700384 }
John Reck3b202512014-06-23 13:13:08 -0700385 }
386 mCurrentSurface = surface;
John Recka8963062017-06-14 10:47:50 -0700387 if (Properties::disableVsync) {
388 eglSwapInterval(mEglDisplay, 0);
389 }
John Reck3b202512014-06-23 13:13:08 -0700390 return true;
391}
392
John Reck149173d2015-08-10 09:52:29 -0700393EGLint EglManager::queryBufferAge(EGLSurface surface) {
394 switch (mSwapBehavior) {
John Reck1bcacfd2017-11-03 10:12:19 -0700395 case SwapBehavior::Discard:
396 return 0;
397 case SwapBehavior::Preserved:
398 return 1;
399 case SwapBehavior::BufferAge:
400 EGLint bufferAge;
401 eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge);
402 return bufferAge;
John Reck149173d2015-08-10 09:52:29 -0700403 }
404 return 0;
405}
406
407Frame EglManager::beginFrame(EGLSurface surface) {
John Reck1bcacfd2017-11-03 10:12:19 -0700408 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, "Tried to beginFrame on EGL_NO_SURFACE!");
John Reck3b202512014-06-23 13:13:08 -0700409 makeCurrent(surface);
John Reck149173d2015-08-10 09:52:29 -0700410 Frame frame;
411 frame.mSurface = surface;
412 eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth);
413 eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight);
414 frame.mBufferAge = queryBufferAge(surface);
John Reck3b202512014-06-23 13:13:08 -0700415 eglBeginFrame(mEglDisplay, surface);
John Reck149173d2015-08-10 09:52:29 -0700416 return frame;
John Reck3b202512014-06-23 13:13:08 -0700417}
418
John Reck149173d2015-08-10 09:52:29 -0700419void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) {
420#ifdef EGL_KHR_partial_update
421 if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) {
422 EGLint rects[4];
423 frame.map(dirty, rects);
424 if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) {
425 LOG_ALWAYS_FATAL("Failed to set damage region on surface %p, error=%s",
John Reck1bcacfd2017-11-03 10:12:19 -0700426 (void*)frame.mSurface, eglErrorString());
John Reck149173d2015-08-10 09:52:29 -0700427 }
428 }
429#endif
430}
431
John Reckc96955d2016-02-26 14:56:44 -0800432bool EglManager::damageRequiresSwap() {
433 return EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge;
434}
435
John Reck149173d2015-08-10 09:52:29 -0700436bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) {
John Reck682573c2015-10-30 10:37:35 -0700437 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
John Reck55156372015-01-21 07:46:37 -0800438 ATRACE_NAME("Finishing GPU work");
439 fence();
440 }
John Reck55156372015-01-21 07:46:37 -0800441
John Reckec100972018-04-05 16:41:41 -0700442 if (EglExtensions.presentTime && Properties::usePresentTime) {
443 if (!eglPresentationTimeANDROID(mEglDisplay, frame.mSurface, frame.mPresentTime)) {
444 LOG_ALWAYS_FATAL("Failed to set presentation time on surface %p, error=%s",
445 (void*)frame.mSurface, eglErrorString());
446 }
447 }
448
John Recka672f6b2015-10-22 09:53:26 -0700449 EGLint rects[4];
450 frame.map(screenDirty, rects);
John Reck1bcacfd2017-11-03 10:12:19 -0700451 eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects, screenDirty.isEmpty() ? 0 : 1);
John Reckd04794a2015-05-08 10:04:36 -0700452
John Reck3b202512014-06-23 13:13:08 -0700453 EGLint err = eglGetError();
John Reck2cdbc7d2014-09-17 16:06:36 -0700454 if (CC_LIKELY(err == EGL_SUCCESS)) {
455 return true;
456 }
John Reckc2547fa2015-10-26 13:52:52 -0700457 if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) {
John Reck2cdbc7d2014-09-17 16:06:36 -0700458 // For some reason our surface was destroyed out from under us
459 // This really shouldn't happen, but if it does we can recover easily
460 // by just not trying to use the surface anymore
John Reck1bcacfd2017-11-03 10:12:19 -0700461 ALOGW("swapBuffers encountered EGL error %d on %p, halting rendering...", err,
462 frame.mSurface);
John Reck2cdbc7d2014-09-17 16:06:36 -0700463 return false;
464 }
John Reck1bcacfd2017-11-03 10:12:19 -0700465 LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering", err, egl_error_str(err));
John Reck2cdbc7d2014-09-17 16:06:36 -0700466 // Impossible to hit this, but the compiler doesn't know that
467 return false;
John Reck3b202512014-06-23 13:13:08 -0700468}
469
John Reck55156372015-01-21 07:46:37 -0800470void EglManager::fence() {
471 EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL);
John Reck1bcacfd2017-11-03 10:12:19 -0700472 eglClientWaitSyncKHR(mEglDisplay, fence, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR);
John Reck55156372015-01-21 07:46:37 -0800473 eglDestroySyncKHR(mEglDisplay, fence);
474}
475
John Reck1125d1f2014-10-23 11:02:19 -0700476bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) {
John Reck149173d2015-08-10 09:52:29 -0700477 if (mSwapBehavior != SwapBehavior::Preserved) return false;
John Reck3b202512014-06-23 13:13:08 -0700478
John Reck149173d2015-08-10 09:52:29 -0700479 bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
John Reck1bcacfd2017-11-03 10:12:19 -0700480 preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED);
John Reck149173d2015-08-10 09:52:29 -0700481 if (!preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700482 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", (void*)surface,
483 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700484 // Maybe it's already set?
485 EGLint swapBehavior;
486 if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) {
487 preserved = (swapBehavior == EGL_BUFFER_PRESERVED);
488 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700489 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", (void*)surface,
490 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700491 }
John Reck3b202512014-06-23 13:13:08 -0700492 }
John Reck1125d1f2014-10-23 11:02:19 -0700493
494 return preserved;
John Reck3b202512014-06-23 13:13:08 -0700495}
496
John Reck3b202512014-06-23 13:13:08 -0700497} /* namespace renderthread */
498} /* namespace uirenderer */
499} /* namespace android */