blob: 5b87e1013baf8d47072f4a1f30b52fbd9f53cd8d [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;
John Reck149173d2015-08-10 09:52:29 -070085} EglExtensions;
86
John Reck3b202512014-06-23 13:13:08 -070087EglManager::EglManager(RenderThread& thread)
88 : mRenderThread(thread)
89 , mEglDisplay(EGL_NO_DISPLAY)
Chris Craikd41c4d82015-01-05 15:51:13 -080090 , mEglConfig(nullptr)
Romain Guy26a2b972017-04-17 09:39:51 -070091 , mEglConfigWideGamut(nullptr)
John Reck3b202512014-06-23 13:13:08 -070092 , mEglContext(EGL_NO_CONTEXT)
93 , mPBufferSurface(EGL_NO_SURFACE)
John Reck1bcacfd2017-11-03 10:12:19 -070094 , mCurrentSurface(EGL_NO_SURFACE) {}
John Reck3b202512014-06-23 13:13:08 -070095
96void 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();
128 makeCurrent(mPBufferSurface);
John Reck704bed02015-11-05 09:22:17 -0800129 DeviceInfo::initialize();
John Reck3b202512014-06-23 13:13:08 -0700130 mRenderThread.renderState().onGLContextCreated();
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400131
132 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
Derek Sollenberger7e044fe2016-11-07 10:57:59 -0500133#ifdef HWUI_GLES_WRAP_ENABLED
134 debug::GlesDriver* driver = debug::GlesDriver::get();
135 sk_sp<const GrGLInterface> glInterface(driver->getSkiaInterface());
136#else
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400137 sk_sp<const GrGLInterface> glInterface(GrGLCreateNativeInterface());
Derek Sollenberger7e044fe2016-11-07 10:57:59 -0500138#endif
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400139 LOG_ALWAYS_FATAL_IF(!glInterface.get());
140
141 GrContextOptions options;
Brian Osmanda6ad832017-08-29 16:43:43 -0400142 options.fDisableDistanceFieldPaths = true;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400143 mRenderThread.cacheManager().configureContext(&options);
Greg Daniel660d6ec2017-12-08 11:44:27 -0500144 sk_sp<GrContext> grContext(GrContext::MakeGL(std::move(glInterface), options));
145 LOG_ALWAYS_FATAL_IF(!grContext.get());
146 mRenderThread.setGrContext(grContext);
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400147 }
John Reck3b202512014-06-23 13:13:08 -0700148}
149
John Reck149173d2015-08-10 09:52:29 -0700150void EglManager::initExtensions() {
John Reck1bcacfd2017-11-03 10:12:19 -0700151 auto extensions = StringUtils::split(eglQueryString(mEglDisplay, EGL_EXTENSIONS));
Romain Guy26a2b972017-04-17 09:39:51 -0700152
John Reck8733bff2016-09-27 14:45:28 -0700153 // For our purposes we don't care if EGL_BUFFER_AGE is a result of
154 // EGL_EXT_buffer_age or EGL_KHR_partial_update as our usage is covered
155 // under EGL_KHR_partial_update and we don't need the expanded scope
156 // that EGL_EXT_buffer_age provides.
John Reck1bcacfd2017-11-03 10:12:19 -0700157 EglExtensions.bufferAge =
158 extensions.has("EGL_EXT_buffer_age") || extensions.has("EGL_KHR_partial_update");
Chris Craik6e6646c2015-09-14 15:54:12 -0700159 EglExtensions.setDamage = extensions.has("EGL_KHR_partial_update");
John Reck708b6682015-10-22 13:11:00 -0700160 LOG_ALWAYS_FATAL_IF(!extensions.has("EGL_KHR_swap_buffers_with_damage"),
John Reck1bcacfd2017-11-03 10:12:19 -0700161 "Missing required extension EGL_KHR_swap_buffers_with_damage");
Romain Guy26a2b972017-04-17 09:39:51 -0700162
163 EglExtensions.glColorSpace = extensions.has("EGL_KHR_gl_colorspace");
164 EglExtensions.noConfigContext = extensions.has("EGL_KHR_no_config_context");
165 EglExtensions.pixelFormatFloat = extensions.has("EGL_EXT_pixel_format_float");
Romain Guy26b6a642017-07-11 09:48:28 -0700166#ifdef ANDROID_ENABLE_LINEAR_BLENDING
167 EglExtensions.scRGB = extensions.has("EGL_EXT_gl_colorspace_scrgb_linear");
168#else
169 EglExtensions.scRGB = extensions.has("EGL_EXT_gl_colorspace_scrgb");
170#endif
John Reck149173d2015-08-10 09:52:29 -0700171}
172
John Reck3b202512014-06-23 13:13:08 -0700173bool EglManager::hasEglContext() {
174 return mEglDisplay != EGL_NO_DISPLAY;
175}
176
Romain Guy26a2b972017-04-17 09:39:51 -0700177void EglManager::loadConfigs() {
John Reck149173d2015-08-10 09:52:29 -0700178 ALOGD("Swap behavior %d", static_cast<int>(mSwapBehavior));
John Reck1bcacfd2017-11-03 10:12:19 -0700179 EGLint swapBehavior =
180 (mSwapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
181 EGLint attribs[] = {EGL_RENDERABLE_TYPE,
182 EGL_OPENGL_ES2_BIT,
183 EGL_RED_SIZE,
184 8,
185 EGL_GREEN_SIZE,
186 8,
187 EGL_BLUE_SIZE,
188 8,
189 EGL_ALPHA_SIZE,
190 8,
191 EGL_DEPTH_SIZE,
192 0,
193 EGL_CONFIG_CAVEAT,
194 EGL_NONE,
195 EGL_STENCIL_SIZE,
196 Stencil::getStencilSize(),
197 EGL_SURFACE_TYPE,
198 EGL_WINDOW_BIT | swapBehavior,
199 EGL_NONE};
John Reck3b202512014-06-23 13:13:08 -0700200
Romain Guy26a2b972017-04-17 09:39:51 -0700201 EGLint numConfigs = 1;
John Reck1bcacfd2017-11-03 10:12:19 -0700202 if (!eglChooseConfig(mEglDisplay, attribs, &mEglConfig, numConfigs, &numConfigs) ||
203 numConfigs != 1) {
John Reck149173d2015-08-10 09:52:29 -0700204 if (mSwapBehavior == SwapBehavior::Preserved) {
John Reck3b202512014-06-23 13:13:08 -0700205 // Try again without dirty regions enabled
John Reck149173d2015-08-10 09:52:29 -0700206 ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...");
207 mSwapBehavior = SwapBehavior::Discard;
Romain Guy26a2b972017-04-17 09:39:51 -0700208 loadConfigs();
John Reck1bcacfd2017-11-03 10:12:19 -0700209 return; // the call to loadConfigs() we just made picks the wide gamut config
John Reck3b202512014-06-23 13:13:08 -0700210 } else {
John Reck149173d2015-08-10 09:52:29 -0700211 // Failed to get a valid config
sergeyv694d4992016-10-27 10:23:13 -0700212 LOG_ALWAYS_FATAL("Failed to choose config, error = %s", eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700213 }
214 }
Romain Guy26a2b972017-04-17 09:39:51 -0700215
216 if (EglExtensions.pixelFormatFloat) {
217 // If we reached this point, we have a valid swap behavior
John Reck1bcacfd2017-11-03 10:12:19 -0700218 EGLint attribs16F[] = {EGL_RENDERABLE_TYPE,
219 EGL_OPENGL_ES2_BIT,
220 EGL_COLOR_COMPONENT_TYPE_EXT,
221 EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT,
222 EGL_RED_SIZE,
223 16,
224 EGL_GREEN_SIZE,
225 16,
226 EGL_BLUE_SIZE,
227 16,
228 EGL_ALPHA_SIZE,
229 16,
230 EGL_DEPTH_SIZE,
231 0,
232 EGL_STENCIL_SIZE,
233 Stencil::getStencilSize(),
234 EGL_SURFACE_TYPE,
235 EGL_WINDOW_BIT | swapBehavior,
236 EGL_NONE};
Romain Guy26a2b972017-04-17 09:39:51 -0700237
238 numConfigs = 1;
John Reck1bcacfd2017-11-03 10:12:19 -0700239 if (!eglChooseConfig(mEglDisplay, attribs16F, &mEglConfigWideGamut, numConfigs,
240 &numConfigs) ||
241 numConfigs != 1) {
Rob Herring74883ab2017-11-29 09:26:31 -0600242 ALOGE("Device claims wide gamut support, cannot find matching config, error = %s",
Romain Guy26a2b972017-04-17 09:39:51 -0700243 eglErrorString());
Rob Herring74883ab2017-11-29 09:26:31 -0600244 EglExtensions.pixelFormatFloat = false;
Romain Guy26a2b972017-04-17 09:39:51 -0700245 }
246 }
John Reck3b202512014-06-23 13:13:08 -0700247}
248
249void EglManager::createContext() {
John Reck1bcacfd2017-11-03 10:12:19 -0700250 EGLint attribs[] = {EGL_CONTEXT_CLIENT_VERSION, GLES_VERSION, EGL_NONE};
251 mEglContext = eglCreateContext(
252 mEglDisplay, EglExtensions.noConfigContext ? ((EGLConfig) nullptr) : mEglConfig,
Romain Guy26a2b972017-04-17 09:39:51 -0700253 EGL_NO_CONTEXT, attribs);
John Reck1bcacfd2017-11-03 10:12:19 -0700254 LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, "Failed to create context, error = %s",
255 eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700256}
257
John Reckd7db4d72015-05-20 07:18:55 -0700258void EglManager::createPBufferSurface() {
John Reck3b202512014-06-23 13:13:08 -0700259 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
John Reck1bcacfd2017-11-03 10:12:19 -0700260 "usePBufferSurface() called on uninitialized GlobalContext!");
John Reck3b202512014-06-23 13:13:08 -0700261
262 if (mPBufferSurface == EGL_NO_SURFACE) {
John Reck1bcacfd2017-11-03 10:12:19 -0700263 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
John Reck3b202512014-06-23 13:13:08 -0700264 mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs);
265 }
John Reck3b202512014-06-23 13:13:08 -0700266}
267
Romain Guy26a2b972017-04-17 09:39:51 -0700268EGLSurface EglManager::createSurface(EGLNativeWindowType window, bool wideColorGamut) {
John Reck3b202512014-06-23 13:13:08 -0700269 initialize();
Romain Guy253f2c22016-09-28 17:34:42 -0700270
John Reck1bcacfd2017-11-03 10:12:19 -0700271 wideColorGamut = wideColorGamut && EglExtensions.glColorSpace && EglExtensions.scRGB &&
272 EglExtensions.pixelFormatFloat && EglExtensions.noConfigContext;
Romain Guy26a2b972017-04-17 09:39:51 -0700273
274 // The color space we want to use depends on whether linear blending is turned
275 // on and whether the app has requested wide color gamut rendering. When wide
276 // color gamut rendering is off, the app simply renders in the display's native
277 // color gamut.
278 //
279 // When wide gamut rendering is off:
280 // - Blending is done by default in gamma space, which requires using a
281 // linear EGL color space (the GPU uses the color values as is)
282 // - If linear blending is on, we must use the sRGB EGL color space (the
283 // GPU will perform sRGB to linear and linear to SRGB conversions before
284 // and after blending)
285 //
286 // When wide gamut rendering is on we cannot rely on the GPU performing
287 // linear blending for us. We use two different color spaces to tag the
288 // surface appropriately for SurfaceFlinger:
289 // - Gamma blending (default) requires the use of the scRGB-nl color space
290 // - Linear blending requires the use of the scRGB color space
291
292 // Not all Android targets support the EGL_GL_COLOR_SPACE_KHR extension
293 // We insert to placeholders to set EGL_GL_COLORSPACE_KHR and its value.
294 // According to section 3.4.1 of the EGL specification, the attributes
295 // list is considered empty if the first entry is EGL_NONE
John Reck1bcacfd2017-11-03 10:12:19 -0700296 EGLint attribs[] = {EGL_NONE, EGL_NONE, EGL_NONE};
Romain Guy253f2c22016-09-28 17:34:42 -0700297
Romain Guy26a2b972017-04-17 09:39:51 -0700298 if (EglExtensions.glColorSpace) {
299 attribs[0] = EGL_GL_COLORSPACE_KHR;
300#ifdef ANDROID_ENABLE_LINEAR_BLENDING
301 if (wideColorGamut) {
302 attribs[1] = EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT;
303 } else {
304 attribs[1] = EGL_GL_COLORSPACE_SRGB_KHR;
305 }
306#else
307 if (wideColorGamut) {
Romain Guy26b6a642017-07-11 09:48:28 -0700308 attribs[1] = EGL_GL_COLORSPACE_SCRGB_EXT;
Romain Guy26a2b972017-04-17 09:39:51 -0700309 } else {
310 attribs[1] = EGL_GL_COLORSPACE_LINEAR_KHR;
311 }
312#endif
313 }
314
John Reck1bcacfd2017-11-03 10:12:19 -0700315 EGLSurface surface = eglCreateWindowSurface(
316 mEglDisplay, wideColorGamut ? mEglConfigWideGamut : mEglConfig, window, attribs);
John Reck3b202512014-06-23 13:13:08 -0700317 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE,
John Reck1bcacfd2017-11-03 10:12:19 -0700318 "Failed to create EGLSurface for window %p, eglErr = %s", (void*)window,
319 eglErrorString());
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000320
321 if (mSwapBehavior != SwapBehavior::Preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700322 LOG_ALWAYS_FATAL_IF(eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
323 EGL_BUFFER_DESTROYED) == EGL_FALSE,
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000324 "Failed to set swap behavior to destroyed for window %p, eglErr = %s",
John Reck1bcacfd2017-11-03 10:12:19 -0700325 (void*)window, eglErrorString());
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000326 }
327
John Reck3b202512014-06-23 13:13:08 -0700328 return surface;
329}
330
331void EglManager::destroySurface(EGLSurface surface) {
332 if (isCurrent(surface)) {
333 makeCurrent(EGL_NO_SURFACE);
334 }
335 if (!eglDestroySurface(mEglDisplay, surface)) {
sergeyv694d4992016-10-27 10:23:13 -0700336 ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700337 }
338}
339
340void EglManager::destroy() {
341 if (mEglDisplay == EGL_NO_DISPLAY) return;
342
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400343 mRenderThread.setGrContext(nullptr);
Chris Craik1d477422014-08-26 17:30:15 -0700344 mRenderThread.renderState().onGLContextDestroyed();
John Reck3b202512014-06-23 13:13:08 -0700345 eglDestroyContext(mEglDisplay, mEglContext);
346 eglDestroySurface(mEglDisplay, mPBufferSurface);
347 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
348 eglTerminate(mEglDisplay);
349 eglReleaseThread();
350
351 mEglDisplay = EGL_NO_DISPLAY;
352 mEglContext = EGL_NO_CONTEXT;
353 mPBufferSurface = EGL_NO_SURFACE;
354 mCurrentSurface = EGL_NO_SURFACE;
355}
356
John Reckf2dcc2a2015-07-16 09:17:59 -0700357bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut) {
John Reck3b202512014-06-23 13:13:08 -0700358 if (isCurrent(surface)) return false;
359
360 if (surface == EGL_NO_SURFACE) {
John Reckd7db4d72015-05-20 07:18:55 -0700361 // Ensure we always have a valid surface & context
362 surface = mPBufferSurface;
363 }
364 if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) {
John Reckf2dcc2a2015-07-16 09:17:59 -0700365 if (errOut) {
366 *errOut = eglGetError();
John Reck1bcacfd2017-11-03 10:12:19 -0700367 ALOGW("Failed to make current on surface %p, error=%s", (void*)surface,
368 egl_error_str(*errOut));
John Reckf2dcc2a2015-07-16 09:17:59 -0700369 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700370 LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", (void*)surface,
371 eglErrorString());
John Reckf2dcc2a2015-07-16 09:17:59 -0700372 }
John Reck3b202512014-06-23 13:13:08 -0700373 }
374 mCurrentSurface = surface;
John Recka8963062017-06-14 10:47:50 -0700375 if (Properties::disableVsync) {
376 eglSwapInterval(mEglDisplay, 0);
377 }
John Reck3b202512014-06-23 13:13:08 -0700378 return true;
379}
380
John Reck149173d2015-08-10 09:52:29 -0700381EGLint EglManager::queryBufferAge(EGLSurface surface) {
382 switch (mSwapBehavior) {
John Reck1bcacfd2017-11-03 10:12:19 -0700383 case SwapBehavior::Discard:
384 return 0;
385 case SwapBehavior::Preserved:
386 return 1;
387 case SwapBehavior::BufferAge:
388 EGLint bufferAge;
389 eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge);
390 return bufferAge;
John Reck149173d2015-08-10 09:52:29 -0700391 }
392 return 0;
393}
394
395Frame EglManager::beginFrame(EGLSurface surface) {
John Reck1bcacfd2017-11-03 10:12:19 -0700396 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, "Tried to beginFrame on EGL_NO_SURFACE!");
John Reck3b202512014-06-23 13:13:08 -0700397 makeCurrent(surface);
John Reck149173d2015-08-10 09:52:29 -0700398 Frame frame;
399 frame.mSurface = surface;
400 eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth);
401 eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight);
402 frame.mBufferAge = queryBufferAge(surface);
John Reck3b202512014-06-23 13:13:08 -0700403 eglBeginFrame(mEglDisplay, surface);
John Reck149173d2015-08-10 09:52:29 -0700404 return frame;
John Reck3b202512014-06-23 13:13:08 -0700405}
406
John Reck149173d2015-08-10 09:52:29 -0700407void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) {
408#ifdef EGL_KHR_partial_update
409 if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) {
410 EGLint rects[4];
411 frame.map(dirty, rects);
412 if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) {
413 LOG_ALWAYS_FATAL("Failed to set damage region on surface %p, error=%s",
John Reck1bcacfd2017-11-03 10:12:19 -0700414 (void*)frame.mSurface, eglErrorString());
John Reck149173d2015-08-10 09:52:29 -0700415 }
416 }
417#endif
418}
419
John Reckc96955d2016-02-26 14:56:44 -0800420bool EglManager::damageRequiresSwap() {
421 return EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge;
422}
423
John Reck149173d2015-08-10 09:52:29 -0700424bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) {
John Reck682573c2015-10-30 10:37:35 -0700425 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
John Reck55156372015-01-21 07:46:37 -0800426 ATRACE_NAME("Finishing GPU work");
427 fence();
428 }
John Reck55156372015-01-21 07:46:37 -0800429
John Recka672f6b2015-10-22 09:53:26 -0700430 EGLint rects[4];
431 frame.map(screenDirty, rects);
John Reck1bcacfd2017-11-03 10:12:19 -0700432 eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects, screenDirty.isEmpty() ? 0 : 1);
John Reckd04794a2015-05-08 10:04:36 -0700433
John Reck3b202512014-06-23 13:13:08 -0700434 EGLint err = eglGetError();
John Reck2cdbc7d2014-09-17 16:06:36 -0700435 if (CC_LIKELY(err == EGL_SUCCESS)) {
436 return true;
437 }
John Reckc2547fa2015-10-26 13:52:52 -0700438 if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) {
John Reck2cdbc7d2014-09-17 16:06:36 -0700439 // For some reason our surface was destroyed out from under us
440 // This really shouldn't happen, but if it does we can recover easily
441 // by just not trying to use the surface anymore
John Reck1bcacfd2017-11-03 10:12:19 -0700442 ALOGW("swapBuffers encountered EGL error %d on %p, halting rendering...", err,
443 frame.mSurface);
John Reck2cdbc7d2014-09-17 16:06:36 -0700444 return false;
445 }
John Reck1bcacfd2017-11-03 10:12:19 -0700446 LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering", err, egl_error_str(err));
John Reck2cdbc7d2014-09-17 16:06:36 -0700447 // Impossible to hit this, but the compiler doesn't know that
448 return false;
John Reck3b202512014-06-23 13:13:08 -0700449}
450
John Reck55156372015-01-21 07:46:37 -0800451void EglManager::fence() {
452 EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL);
John Reck1bcacfd2017-11-03 10:12:19 -0700453 eglClientWaitSyncKHR(mEglDisplay, fence, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR);
John Reck55156372015-01-21 07:46:37 -0800454 eglDestroySyncKHR(mEglDisplay, fence);
455}
456
John Reck1125d1f2014-10-23 11:02:19 -0700457bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) {
John Reck149173d2015-08-10 09:52:29 -0700458 if (mSwapBehavior != SwapBehavior::Preserved) return false;
John Reck3b202512014-06-23 13:13:08 -0700459
John Reck149173d2015-08-10 09:52:29 -0700460 bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
John Reck1bcacfd2017-11-03 10:12:19 -0700461 preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED);
John Reck149173d2015-08-10 09:52:29 -0700462 if (!preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700463 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", (void*)surface,
464 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700465 // Maybe it's already set?
466 EGLint swapBehavior;
467 if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) {
468 preserved = (swapBehavior == EGL_BUFFER_PRESERVED);
469 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700470 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", (void*)surface,
471 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700472 }
John Reck3b202512014-06-23 13:13:08 -0700473 }
John Reck1125d1f2014-10-23 11:02:19 -0700474
475 return preserved;
John Reck3b202512014-06-23 13:13:08 -0700476}
477
John Reck3b202512014-06-23 13:13:08 -0700478} /* namespace renderthread */
479} /* namespace uirenderer */
480} /* namespace android */