blob: d6240e7064cfcb2d9021108b82c3023f6f499667 [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
21#include "utils/StringUtils.h"
22#include <cutils/properties.h>
23#include <log/log.h>
24
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"
John Reckd04794a2015-05-08 10:04:36 -070030#include "renderstate/RenderState.h"
Mark Salyzyn173215d2017-01-12 08:27:11 -080031#include "Texture.h"
32
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
50#define ERROR_CASE(x) case x: return #x;
51static const char* egl_error_str(EGLint error) {
52 switch (error) {
53 ERROR_CASE(EGL_SUCCESS)
54 ERROR_CASE(EGL_NOT_INITIALIZED)
55 ERROR_CASE(EGL_BAD_ACCESS)
56 ERROR_CASE(EGL_BAD_ALLOC)
57 ERROR_CASE(EGL_BAD_ATTRIBUTE)
58 ERROR_CASE(EGL_BAD_CONFIG)
59 ERROR_CASE(EGL_BAD_CONTEXT)
60 ERROR_CASE(EGL_BAD_CURRENT_SURFACE)
61 ERROR_CASE(EGL_BAD_DISPLAY)
62 ERROR_CASE(EGL_BAD_MATCH)
63 ERROR_CASE(EGL_BAD_NATIVE_PIXMAP)
64 ERROR_CASE(EGL_BAD_NATIVE_WINDOW)
65 ERROR_CASE(EGL_BAD_PARAMETER)
66 ERROR_CASE(EGL_BAD_SURFACE)
67 ERROR_CASE(EGL_CONTEXT_LOST)
68 default:
69 return "Unknown error";
70 }
71}
sergeyv694d4992016-10-27 10:23:13 -070072const char* EglManager::eglErrorString() {
John Reck3b202512014-06-23 13:13:08 -070073 return egl_error_str(eglGetError());
74}
75
John Reck149173d2015-08-10 09:52:29 -070076static struct {
77 bool bufferAge = false;
78 bool setDamage = false;
Romain Guy26a2b972017-04-17 09:39:51 -070079 bool noConfigContext = false;
80 bool pixelFormatFloat = false;
81 bool glColorSpace = false;
John Reck149173d2015-08-10 09:52:29 -070082} EglExtensions;
83
John Reck3b202512014-06-23 13:13:08 -070084EglManager::EglManager(RenderThread& thread)
85 : mRenderThread(thread)
86 , mEglDisplay(EGL_NO_DISPLAY)
Chris Craikd41c4d82015-01-05 15:51:13 -080087 , mEglConfig(nullptr)
Romain Guy26a2b972017-04-17 09:39:51 -070088 , mEglConfigWideGamut(nullptr)
John Reck3b202512014-06-23 13:13:08 -070089 , mEglContext(EGL_NO_CONTEXT)
90 , mPBufferSurface(EGL_NO_SURFACE)
Romain Guy253f2c22016-09-28 17:34:42 -070091 , mCurrentSurface(EGL_NO_SURFACE) {
John Reck3b202512014-06-23 13:13:08 -070092}
93
94void EglManager::initialize() {
95 if (hasEglContext()) return;
96
John Reckfbc8df02014-11-14 16:18:41 -080097 ATRACE_NAME("Creating EGLContext");
98
John Reck3b202512014-06-23 13:13:08 -070099 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
100 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
sergeyv694d4992016-10-27 10:23:13 -0700101 "Failed to get EGL_DEFAULT_DISPLAY! err=%s", eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700102
103 EGLint major, minor;
104 LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE,
sergeyv694d4992016-10-27 10:23:13 -0700105 "Failed to initialize display %p! err=%s", mEglDisplay, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700106
107 ALOGI("Initialized EGL, version %d.%d", (int)major, (int)minor);
108
John Reck149173d2015-08-10 09:52:29 -0700109 initExtensions();
Season Li13d1b4a2015-07-29 17:16:19 -0700110
John Reck149173d2015-08-10 09:52:29 -0700111 // Now that extensions are loaded, pick a swap behavior
112 if (Properties::enablePartialUpdates) {
Matt Sarettb77c94a2016-12-07 12:22:37 -0500113 // An Adreno driver bug is causing rendering problems for SkiaGL with
114 // buffer age swap behavior (b/31957043). To temporarily workaround,
115 // we will use preserved swap behavior.
116 if (Properties::useBufferAge && EglExtensions.bufferAge && !Properties::isSkiaEnabled()) {
John Reck149173d2015-08-10 09:52:29 -0700117 mSwapBehavior = SwapBehavior::BufferAge;
118 } else {
119 mSwapBehavior = SwapBehavior::Preserved;
120 }
121 }
122
Romain Guy26a2b972017-04-17 09:39:51 -0700123 loadConfigs();
John Reck3b202512014-06-23 13:13:08 -0700124 createContext();
John Reckd7db4d72015-05-20 07:18:55 -0700125 createPBufferSurface();
126 makeCurrent(mPBufferSurface);
John Reck704bed02015-11-05 09:22:17 -0800127 DeviceInfo::initialize();
John Reck3b202512014-06-23 13:13:08 -0700128 mRenderThread.renderState().onGLContextCreated();
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400129
130 if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL) {
Derek Sollenberger7e044fe2016-11-07 10:57:59 -0500131#ifdef HWUI_GLES_WRAP_ENABLED
132 debug::GlesDriver* driver = debug::GlesDriver::get();
133 sk_sp<const GrGLInterface> glInterface(driver->getSkiaInterface());
134#else
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400135 sk_sp<const GrGLInterface> glInterface(GrGLCreateNativeInterface());
Derek Sollenberger7e044fe2016-11-07 10:57:59 -0500136#endif
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400137 LOG_ALWAYS_FATAL_IF(!glInterface.get());
138
139 GrContextOptions options;
Derek Sollenbergerab61fb82017-02-23 07:48:11 -0500140 options.fGpuPathRenderers &= ~GrContextOptions::GpuPathRenderers::kDistanceField;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400141 mRenderThread.cacheManager().configureContext(&options);
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400142 mRenderThread.setGrContext(GrContext::Create(GrBackend::kOpenGL_GrBackend,
143 (GrBackendContext)glInterface.get(), options));
144 }
John Reck3b202512014-06-23 13:13:08 -0700145}
146
John Reck149173d2015-08-10 09:52:29 -0700147void EglManager::initExtensions() {
John Reck704bed02015-11-05 09:22:17 -0800148 auto extensions = StringUtils::split(
149 eglQueryString(mEglDisplay, EGL_EXTENSIONS));
Romain Guy26a2b972017-04-17 09:39:51 -0700150
John Reck8733bff2016-09-27 14:45:28 -0700151 // For our purposes we don't care if EGL_BUFFER_AGE is a result of
152 // EGL_EXT_buffer_age or EGL_KHR_partial_update as our usage is covered
153 // under EGL_KHR_partial_update and we don't need the expanded scope
154 // that EGL_EXT_buffer_age provides.
155 EglExtensions.bufferAge = extensions.has("EGL_EXT_buffer_age")
156 || extensions.has("EGL_KHR_partial_update");
Chris Craik6e6646c2015-09-14 15:54:12 -0700157 EglExtensions.setDamage = extensions.has("EGL_KHR_partial_update");
John Reck708b6682015-10-22 13:11:00 -0700158 LOG_ALWAYS_FATAL_IF(!extensions.has("EGL_KHR_swap_buffers_with_damage"),
159 "Missing required extension EGL_KHR_swap_buffers_with_damage");
Romain Guy26a2b972017-04-17 09:39:51 -0700160
161 EglExtensions.glColorSpace = extensions.has("EGL_KHR_gl_colorspace");
162 EglExtensions.noConfigContext = extensions.has("EGL_KHR_no_config_context");
163 EglExtensions.pixelFormatFloat = extensions.has("EGL_EXT_pixel_format_float");
John Reck149173d2015-08-10 09:52:29 -0700164}
165
John Reck3b202512014-06-23 13:13:08 -0700166bool EglManager::hasEglContext() {
167 return mEglDisplay != EGL_NO_DISPLAY;
168}
169
Romain Guy26a2b972017-04-17 09:39:51 -0700170void EglManager::loadConfigs() {
John Reck149173d2015-08-10 09:52:29 -0700171 ALOGD("Swap behavior %d", static_cast<int>(mSwapBehavior));
172 EGLint swapBehavior = (mSwapBehavior == SwapBehavior::Preserved)
173 ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
John Reck3b202512014-06-23 13:13:08 -0700174 EGLint attribs[] = {
175 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
176 EGL_RED_SIZE, 8,
177 EGL_GREEN_SIZE, 8,
178 EGL_BLUE_SIZE, 8,
179 EGL_ALPHA_SIZE, 8,
180 EGL_DEPTH_SIZE, 0,
181 EGL_CONFIG_CAVEAT, EGL_NONE,
182 EGL_STENCIL_SIZE, Stencil::getStencilSize(),
183 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | swapBehavior,
184 EGL_NONE
185 };
186
Romain Guy26a2b972017-04-17 09:39:51 -0700187 EGLint numConfigs = 1;
188 if (!eglChooseConfig(mEglDisplay, attribs, &mEglConfig, numConfigs, &numConfigs)
189 || numConfigs != 1) {
John Reck149173d2015-08-10 09:52:29 -0700190 if (mSwapBehavior == SwapBehavior::Preserved) {
John Reck3b202512014-06-23 13:13:08 -0700191 // Try again without dirty regions enabled
John Reck149173d2015-08-10 09:52:29 -0700192 ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...");
193 mSwapBehavior = SwapBehavior::Discard;
Romain Guy26a2b972017-04-17 09:39:51 -0700194 loadConfigs();
195 return; // the call to loadConfigs() we just made picks the wide gamut config
John Reck3b202512014-06-23 13:13:08 -0700196 } else {
John Reck149173d2015-08-10 09:52:29 -0700197 // Failed to get a valid config
sergeyv694d4992016-10-27 10:23:13 -0700198 LOG_ALWAYS_FATAL("Failed to choose config, error = %s", eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700199 }
200 }
Romain Guy26a2b972017-04-17 09:39:51 -0700201
202 if (EglExtensions.pixelFormatFloat) {
203 // If we reached this point, we have a valid swap behavior
204 EGLint attribs16F[] = {
205 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
206 EGL_COLOR_COMPONENT_TYPE_EXT, EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT,
207 EGL_RED_SIZE, 16,
208 EGL_GREEN_SIZE, 16,
209 EGL_BLUE_SIZE, 16,
210 EGL_ALPHA_SIZE, 16,
211 EGL_DEPTH_SIZE, 0,
212 EGL_STENCIL_SIZE, Stencil::getStencilSize(),
213 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | swapBehavior,
214 EGL_NONE
215 };
216
217 numConfigs = 1;
218 if (!eglChooseConfig(mEglDisplay, attribs16F, &mEglConfigWideGamut, numConfigs, &numConfigs)
219 || numConfigs != 1) {
220 LOG_ALWAYS_FATAL(
221 "Device claims wide gamut support, cannot find matching config, error = %s",
222 eglErrorString());
223 }
224 }
John Reck3b202512014-06-23 13:13:08 -0700225}
226
227void EglManager::createContext() {
John Reck682573c2015-10-30 10:37:35 -0700228 EGLint attribs[] = {
229 EGL_CONTEXT_CLIENT_VERSION, GLES_VERSION,
230 EGL_NONE
231 };
Romain Guy26a2b972017-04-17 09:39:51 -0700232 mEglContext = eglCreateContext(mEglDisplay,
233 EglExtensions.noConfigContext ? ((EGLConfig) nullptr) : mEglConfig,
234 EGL_NO_CONTEXT, attribs);
John Reck3b202512014-06-23 13:13:08 -0700235 LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT,
sergeyv694d4992016-10-27 10:23:13 -0700236 "Failed to create context, error = %s", eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700237}
238
John Reckd7db4d72015-05-20 07:18:55 -0700239void EglManager::createPBufferSurface() {
John Reck3b202512014-06-23 13:13:08 -0700240 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
241 "usePBufferSurface() called on uninitialized GlobalContext!");
242
243 if (mPBufferSurface == EGL_NO_SURFACE) {
244 EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
245 mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs);
246 }
John Reck3b202512014-06-23 13:13:08 -0700247}
248
Romain Guy26a2b972017-04-17 09:39:51 -0700249EGLSurface EglManager::createSurface(EGLNativeWindowType window, bool wideColorGamut) {
John Reck3b202512014-06-23 13:13:08 -0700250 initialize();
Romain Guy253f2c22016-09-28 17:34:42 -0700251
Romain Guy26a2b972017-04-17 09:39:51 -0700252 wideColorGamut = wideColorGamut && EglExtensions.glColorSpace
253 && EglExtensions.pixelFormatFloat && EglExtensions.noConfigContext;
254
255 // The color space we want to use depends on whether linear blending is turned
256 // on and whether the app has requested wide color gamut rendering. When wide
257 // color gamut rendering is off, the app simply renders in the display's native
258 // color gamut.
259 //
260 // When wide gamut rendering is off:
261 // - Blending is done by default in gamma space, which requires using a
262 // linear EGL color space (the GPU uses the color values as is)
263 // - If linear blending is on, we must use the sRGB EGL color space (the
264 // GPU will perform sRGB to linear and linear to SRGB conversions before
265 // and after blending)
266 //
267 // When wide gamut rendering is on we cannot rely on the GPU performing
268 // linear blending for us. We use two different color spaces to tag the
269 // surface appropriately for SurfaceFlinger:
270 // - Gamma blending (default) requires the use of the scRGB-nl color space
271 // - Linear blending requires the use of the scRGB color space
272
273 // Not all Android targets support the EGL_GL_COLOR_SPACE_KHR extension
274 // We insert to placeholders to set EGL_GL_COLORSPACE_KHR and its value.
275 // According to section 3.4.1 of the EGL specification, the attributes
276 // list is considered empty if the first entry is EGL_NONE
Romain Guy253f2c22016-09-28 17:34:42 -0700277 EGLint attribs[] = {
Romain Guy26a2b972017-04-17 09:39:51 -0700278 EGL_NONE, EGL_NONE,
Romain Guy253f2c22016-09-28 17:34:42 -0700279 EGL_NONE
280 };
281
Romain Guy26a2b972017-04-17 09:39:51 -0700282 if (EglExtensions.glColorSpace) {
283 attribs[0] = EGL_GL_COLORSPACE_KHR;
284#ifdef ANDROID_ENABLE_LINEAR_BLENDING
285 if (wideColorGamut) {
286 attribs[1] = EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT;
287 } else {
288 attribs[1] = EGL_GL_COLORSPACE_SRGB_KHR;
289 }
290#else
291 if (wideColorGamut) {
292 // TODO: this should be using scRGB-nl, not scRGB, we need an extension for this
293 // TODO: in the meantime SurfaceFlinger just assumes that scRGB is scRGB-nl
294 attribs[1] = EGL_GL_COLORSPACE_SCRGB_LINEAR_EXT;
295 } else {
296 attribs[1] = EGL_GL_COLORSPACE_LINEAR_KHR;
297 }
298#endif
299 }
300
301 EGLSurface surface = eglCreateWindowSurface(mEglDisplay,
302 wideColorGamut ? mEglConfigWideGamut : mEglConfig, window, attribs);
John Reck3b202512014-06-23 13:13:08 -0700303 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE,
304 "Failed to create EGLSurface for window %p, eglErr = %s",
sergeyv694d4992016-10-27 10:23:13 -0700305 (void*) window, eglErrorString());
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000306
307 if (mSwapBehavior != SwapBehavior::Preserved) {
308 LOG_ALWAYS_FATAL_IF(eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED) == EGL_FALSE,
309 "Failed to set swap behavior to destroyed for window %p, eglErr = %s",
sergeyv694d4992016-10-27 10:23:13 -0700310 (void*) window, eglErrorString());
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000311 }
312
John Reck3b202512014-06-23 13:13:08 -0700313 return surface;
314}
315
316void EglManager::destroySurface(EGLSurface surface) {
317 if (isCurrent(surface)) {
318 makeCurrent(EGL_NO_SURFACE);
319 }
320 if (!eglDestroySurface(mEglDisplay, surface)) {
sergeyv694d4992016-10-27 10:23:13 -0700321 ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700322 }
323}
324
325void EglManager::destroy() {
326 if (mEglDisplay == EGL_NO_DISPLAY) return;
327
Derek Sollenberger98f75d52016-10-25 10:25:45 -0400328 mRenderThread.setGrContext(nullptr);
Chris Craik1d477422014-08-26 17:30:15 -0700329 mRenderThread.renderState().onGLContextDestroyed();
John Reck3b202512014-06-23 13:13:08 -0700330 eglDestroyContext(mEglDisplay, mEglContext);
331 eglDestroySurface(mEglDisplay, mPBufferSurface);
332 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
333 eglTerminate(mEglDisplay);
334 eglReleaseThread();
335
336 mEglDisplay = EGL_NO_DISPLAY;
337 mEglContext = EGL_NO_CONTEXT;
338 mPBufferSurface = EGL_NO_SURFACE;
339 mCurrentSurface = EGL_NO_SURFACE;
340}
341
John Reckf2dcc2a2015-07-16 09:17:59 -0700342bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut) {
John Reck3b202512014-06-23 13:13:08 -0700343 if (isCurrent(surface)) return false;
344
345 if (surface == EGL_NO_SURFACE) {
John Reckd7db4d72015-05-20 07:18:55 -0700346 // Ensure we always have a valid surface & context
347 surface = mPBufferSurface;
348 }
349 if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) {
John Reckf2dcc2a2015-07-16 09:17:59 -0700350 if (errOut) {
351 *errOut = eglGetError();
352 ALOGW("Failed to make current on surface %p, error=%s",
353 (void*)surface, egl_error_str(*errOut));
354 } else {
355 LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s",
sergeyv694d4992016-10-27 10:23:13 -0700356 (void*)surface, eglErrorString());
John Reckf2dcc2a2015-07-16 09:17:59 -0700357 }
John Reck3b202512014-06-23 13:13:08 -0700358 }
359 mCurrentSurface = surface;
John Recka8963062017-06-14 10:47:50 -0700360 if (Properties::disableVsync) {
361 eglSwapInterval(mEglDisplay, 0);
362 }
John Reck3b202512014-06-23 13:13:08 -0700363 return true;
364}
365
John Reck149173d2015-08-10 09:52:29 -0700366EGLint EglManager::queryBufferAge(EGLSurface surface) {
367 switch (mSwapBehavior) {
368 case SwapBehavior::Discard:
369 return 0;
370 case SwapBehavior::Preserved:
371 return 1;
372 case SwapBehavior::BufferAge:
373 EGLint bufferAge;
374 eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge);
375 return bufferAge;
376 }
377 return 0;
378}
379
380Frame EglManager::beginFrame(EGLSurface surface) {
John Reck3b202512014-06-23 13:13:08 -0700381 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE,
382 "Tried to beginFrame on EGL_NO_SURFACE!");
383 makeCurrent(surface);
John Reck149173d2015-08-10 09:52:29 -0700384 Frame frame;
385 frame.mSurface = surface;
386 eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth);
387 eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight);
388 frame.mBufferAge = queryBufferAge(surface);
John Reck3b202512014-06-23 13:13:08 -0700389 eglBeginFrame(mEglDisplay, surface);
John Reck149173d2015-08-10 09:52:29 -0700390 return frame;
John Reck3b202512014-06-23 13:13:08 -0700391}
392
John Reck149173d2015-08-10 09:52:29 -0700393void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) {
394#ifdef EGL_KHR_partial_update
395 if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) {
396 EGLint rects[4];
397 frame.map(dirty, rects);
398 if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) {
399 LOG_ALWAYS_FATAL("Failed to set damage region on surface %p, error=%s",
sergeyv694d4992016-10-27 10:23:13 -0700400 (void*)frame.mSurface, eglErrorString());
John Reck149173d2015-08-10 09:52:29 -0700401 }
402 }
403#endif
404}
405
John Reckc96955d2016-02-26 14:56:44 -0800406bool EglManager::damageRequiresSwap() {
407 return EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge;
408}
409
John Reck149173d2015-08-10 09:52:29 -0700410bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) {
John Reck55156372015-01-21 07:46:37 -0800411
John Reck682573c2015-10-30 10:37:35 -0700412 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
John Reck55156372015-01-21 07:46:37 -0800413 ATRACE_NAME("Finishing GPU work");
414 fence();
415 }
John Reck55156372015-01-21 07:46:37 -0800416
John Recka672f6b2015-10-22 09:53:26 -0700417 EGLint rects[4];
418 frame.map(screenDirty, rects);
419 eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects,
420 screenDirty.isEmpty() ? 0 : 1);
John Reckd04794a2015-05-08 10:04:36 -0700421
John Reck3b202512014-06-23 13:13:08 -0700422 EGLint err = eglGetError();
John Reck2cdbc7d2014-09-17 16:06:36 -0700423 if (CC_LIKELY(err == EGL_SUCCESS)) {
424 return true;
425 }
John Reckc2547fa2015-10-26 13:52:52 -0700426 if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) {
John Reck2cdbc7d2014-09-17 16:06:36 -0700427 // For some reason our surface was destroyed out from under us
428 // This really shouldn't happen, but if it does we can recover easily
429 // by just not trying to use the surface anymore
John Reckd1ddcf12016-02-05 15:59:55 -0800430 ALOGW("swapBuffers encountered EGL error %d on %p, halting rendering...",
431 err, frame.mSurface);
John Reck2cdbc7d2014-09-17 16:06:36 -0700432 return false;
433 }
434 LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering",
435 err, egl_error_str(err));
436 // 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);
442 eglClientWaitSyncKHR(mEglDisplay, fence,
443 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR);
444 eglDestroySyncKHR(mEglDisplay, fence);
445}
446
John Reck1125d1f2014-10-23 11:02:19 -0700447bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) {
John Reck149173d2015-08-10 09:52:29 -0700448 if (mSwapBehavior != SwapBehavior::Preserved) return false;
John Reck3b202512014-06-23 13:13:08 -0700449
John Reck149173d2015-08-10 09:52:29 -0700450 bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
451 preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED);
452 if (!preserved) {
453 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s",
sergeyv694d4992016-10-27 10:23:13 -0700454 (void*) surface, eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700455 // Maybe it's already set?
456 EGLint swapBehavior;
457 if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) {
458 preserved = (swapBehavior == EGL_BUFFER_PRESERVED);
459 } else {
460 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p",
sergeyv694d4992016-10-27 10:23:13 -0700461 (void*) surface, eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700462 }
John Reck3b202512014-06-23 13:13:08 -0700463 }
John Reck1125d1f2014-10-23 11:02:19 -0700464
465 return preserved;
John Reck3b202512014-06-23 13:13:08 -0700466}
467
John Reck3b202512014-06-23 13:13:08 -0700468} /* namespace renderthread */
469} /* namespace uirenderer */
470} /* namespace android */