blob: 4f5f180643f35b742245ea1200479ce8a9126e6c [file] [log] [blame]
John Reck3b202512014-06-23 13:13:08 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
John Reck3b202512014-06-23 13:13:08 -070017#include "EglManager.h"
18
Mark Salyzyn96bf5982016-09-28 16:15:30 -070019#include <cutils/properties.h>
20#include <log/log.h>
Stan Iliev564ca3e2018-09-04 22:00:00 +000021#include <private/gui/SyncFeatures.h>
John Reck1e510712018-04-23 08:15:03 -070022#include <utils/Trace.h>
Peiyong Lin1f6aa122018-09-10 16:28:08 -070023#include "utils/Color.h"
John Reck1bcacfd2017-11-03 10:12:19 -070024#include "utils/StringUtils.h"
Mark Salyzyn96bf5982016-09-28 16:15:30 -070025
Greg Danielcd558522016-11-17 13:31:40 -050026#include "Frame.h"
John Reckd04794a2015-05-08 10:04:36 -070027#include "Properties.h"
Mark Salyzyn173215d2017-01-12 08:27:11 -080028
John Reck55156372015-01-21 07:46:37 -080029#include <EGL/eglext.h>
John Reckbdc9f1b2018-09-14 15:22:35 -070030#include <GLES/gl.h>
John Reck149173d2015-08-10 09:52:29 -070031
John Reck0fa0cbc2019-04-05 16:57:46 -070032#include <system/window.h>
John Reck1e510712018-04-23 08:15:03 -070033#include <string>
34#include <vector>
Derek Sollenberger7e044fe2016-11-07 10:57:59 -050035
John Reck3b202512014-06-23 13:13:08 -070036#define GLES_VERSION 2
37
38// Android-specific addition that is used to show when frames began in systrace
39EGLAPI void EGLAPIENTRY eglBeginFrame(EGLDisplay dpy, EGLSurface surface);
40
41namespace android {
42namespace uirenderer {
43namespace renderthread {
44
John Reck1bcacfd2017-11-03 10:12:19 -070045#define ERROR_CASE(x) \
46 case x: \
47 return #x;
John Reck3b202512014-06-23 13:13:08 -070048static const char* egl_error_str(EGLint error) {
49 switch (error) {
50 ERROR_CASE(EGL_SUCCESS)
51 ERROR_CASE(EGL_NOT_INITIALIZED)
52 ERROR_CASE(EGL_BAD_ACCESS)
53 ERROR_CASE(EGL_BAD_ALLOC)
54 ERROR_CASE(EGL_BAD_ATTRIBUTE)
55 ERROR_CASE(EGL_BAD_CONFIG)
56 ERROR_CASE(EGL_BAD_CONTEXT)
57 ERROR_CASE(EGL_BAD_CURRENT_SURFACE)
58 ERROR_CASE(EGL_BAD_DISPLAY)
59 ERROR_CASE(EGL_BAD_MATCH)
60 ERROR_CASE(EGL_BAD_NATIVE_PIXMAP)
61 ERROR_CASE(EGL_BAD_NATIVE_WINDOW)
62 ERROR_CASE(EGL_BAD_PARAMETER)
63 ERROR_CASE(EGL_BAD_SURFACE)
64 ERROR_CASE(EGL_CONTEXT_LOST)
John Reck1bcacfd2017-11-03 10:12:19 -070065 default:
66 return "Unknown error";
John Reck3b202512014-06-23 13:13:08 -070067 }
68}
sergeyv694d4992016-10-27 10:23:13 -070069const char* EglManager::eglErrorString() {
John Reck3b202512014-06-23 13:13:08 -070070 return egl_error_str(eglGetError());
71}
72
John Reck149173d2015-08-10 09:52:29 -070073static struct {
74 bool bufferAge = false;
75 bool setDamage = false;
Romain Guy26a2b972017-04-17 09:39:51 -070076 bool noConfigContext = false;
77 bool pixelFormatFloat = false;
78 bool glColorSpace = false;
Romain Guy26b6a642017-07-11 09:48:28 -070079 bool scRGB = false;
Peiyong Lin1f6aa122018-09-10 16:28:08 -070080 bool displayP3 = false;
Jorim Jaggi767e25e2018-04-04 23:07:35 +020081 bool contextPriority = false;
John Reck1e510712018-04-23 08:15:03 -070082 bool surfacelessContext = false;
John Reck149173d2015-08-10 09:52:29 -070083} EglExtensions;
84
John Reck1e510712018-04-23 08:15:03 -070085EglManager::EglManager()
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)
Peiyong Lin3bff1352018-12-11 07:56:07 -080091 , mCurrentSurface(EGL_NO_SURFACE)
92 , mHasWideColorGamutSupport(false) {}
John Reck3b202512014-06-23 13:13:08 -070093
John Reck1e510712018-04-23 08:15:03 -070094EglManager::~EglManager() {
John Reck6104cea2019-01-10 14:37:17 -080095 if (hasEglContext()) {
96 ALOGW("~EglManager() leaked an EGL context");
97 }
John Reck1e510712018-04-23 08:15:03 -070098}
99
John Reck3b202512014-06-23 13:13:08 -0700100void EglManager::initialize() {
101 if (hasEglContext()) return;
102
John Reckfbc8df02014-11-14 16:18:41 -0800103 ATRACE_NAME("Creating EGLContext");
104
John Reck3b202512014-06-23 13:13:08 -0700105 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
John Reck1bcacfd2017-11-03 10:12:19 -0700106 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, "Failed to get EGL_DEFAULT_DISPLAY! err=%s",
107 eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700108
109 EGLint major, minor;
110 LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE,
John Reck1bcacfd2017-11-03 10:12:19 -0700111 "Failed to initialize display %p! err=%s", mEglDisplay, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700112
John Reck848f6512018-12-03 13:26:43 -0800113 ALOGV("Initialized EGL, version %d.%d", (int)major, (int)minor);
John Reck3b202512014-06-23 13:13:08 -0700114
John Reck149173d2015-08-10 09:52:29 -0700115 initExtensions();
Season Li13d1b4a2015-07-29 17:16:19 -0700116
John Reck149173d2015-08-10 09:52:29 -0700117 // Now that extensions are loaded, pick a swap behavior
118 if (Properties::enablePartialUpdates) {
Matt Sarettb77c94a2016-12-07 12:22:37 -0500119 // An Adreno driver bug is causing rendering problems for SkiaGL with
120 // buffer age swap behavior (b/31957043). To temporarily workaround,
121 // we will use preserved swap behavior.
Derek Sollenbergerb5a12bd2017-06-14 15:23:19 -0400122 if (Properties::useBufferAge && EglExtensions.bufferAge) {
John Reck149173d2015-08-10 09:52:29 -0700123 mSwapBehavior = SwapBehavior::BufferAge;
124 } else {
125 mSwapBehavior = SwapBehavior::Preserved;
126 }
127 }
128
Romain Guy26a2b972017-04-17 09:39:51 -0700129 loadConfigs();
John Reck3b202512014-06-23 13:13:08 -0700130 createContext();
John Reckd7db4d72015-05-20 07:18:55 -0700131 createPBufferSurface();
John Reck1e510712018-04-23 08:15:03 -0700132 makeCurrent(mPBufferSurface, nullptr, /* force */ true);
Peiyong Lin3bff1352018-12-11 07:56:07 -0800133
Brian Osmane0cf5972019-01-23 10:41:20 -0500134 skcms_Matrix3x3 wideColorGamut;
135 LOG_ALWAYS_FATAL_IF(!DeviceInfo::get()->getWideColorSpace()->toXYZD50(&wideColorGamut),
136 "Could not get gamut matrix from wideColorSpace");
Peiyong Lin3bff1352018-12-11 07:56:07 -0800137 bool hasWideColorSpaceExtension = false;
Brian Osmane0cf5972019-01-23 10:41:20 -0500138 if (memcmp(&wideColorGamut, &SkNamedGamut::kDCIP3, sizeof(wideColorGamut)) == 0) {
Peiyong Lin3bff1352018-12-11 07:56:07 -0800139 hasWideColorSpaceExtension = EglExtensions.displayP3;
Brian Osmane0cf5972019-01-23 10:41:20 -0500140 } else if (memcmp(&wideColorGamut, &SkNamedGamut::kSRGB, sizeof(wideColorGamut)) == 0) {
Peiyong Lin3bff1352018-12-11 07:56:07 -0800141 hasWideColorSpaceExtension = EglExtensions.scRGB;
142 } else {
143 LOG_ALWAYS_FATAL("Unsupported wide color space.");
144 }
145 mHasWideColorGamutSupport = EglExtensions.glColorSpace && hasWideColorSpaceExtension &&
146 mEglConfigWideGamut != EGL_NO_CONFIG_KHR;
147}
148
149EGLConfig EglManager::load8BitsConfig(EGLDisplay display, EglManager::SwapBehavior swapBehavior) {
150 EGLint eglSwapBehavior =
151 (swapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
152 EGLint attribs[] = {EGL_RENDERABLE_TYPE,
153 EGL_OPENGL_ES2_BIT,
154 EGL_RED_SIZE,
155 8,
156 EGL_GREEN_SIZE,
157 8,
158 EGL_BLUE_SIZE,
159 8,
160 EGL_ALPHA_SIZE,
161 8,
162 EGL_DEPTH_SIZE,
163 0,
164 EGL_CONFIG_CAVEAT,
165 EGL_NONE,
166 EGL_STENCIL_SIZE,
167 STENCIL_BUFFER_SIZE,
168 EGL_SURFACE_TYPE,
169 EGL_WINDOW_BIT | eglSwapBehavior,
170 EGL_NONE};
171 EGLConfig config = EGL_NO_CONFIG_KHR;
172 EGLint numConfigs = 1;
John Reck0fa0cbc2019-04-05 16:57:46 -0700173 if (!eglChooseConfig(display, attribs, &config, numConfigs, &numConfigs) || numConfigs != 1) {
Peiyong Lin3bff1352018-12-11 07:56:07 -0800174 return EGL_NO_CONFIG_KHR;
175 }
176 return config;
177}
178
179EGLConfig EglManager::loadFP16Config(EGLDisplay display, SwapBehavior swapBehavior) {
180 EGLint eglSwapBehavior =
181 (swapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
182 // If we reached this point, we have a valid swap behavior
183 EGLint attribs[] = {EGL_RENDERABLE_TYPE,
184 EGL_OPENGL_ES2_BIT,
185 EGL_COLOR_COMPONENT_TYPE_EXT,
186 EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT,
187 EGL_RED_SIZE,
188 16,
189 EGL_GREEN_SIZE,
190 16,
191 EGL_BLUE_SIZE,
192 16,
193 EGL_ALPHA_SIZE,
194 16,
195 EGL_DEPTH_SIZE,
196 0,
197 EGL_STENCIL_SIZE,
198 STENCIL_BUFFER_SIZE,
199 EGL_SURFACE_TYPE,
200 EGL_WINDOW_BIT | eglSwapBehavior,
201 EGL_NONE};
202 EGLConfig config = EGL_NO_CONFIG_KHR;
203 EGLint numConfigs = 1;
John Reck0fa0cbc2019-04-05 16:57:46 -0700204 if (!eglChooseConfig(display, attribs, &config, numConfigs, &numConfigs) || numConfigs != 1) {
Peiyong Lin3bff1352018-12-11 07:56:07 -0800205 return EGL_NO_CONFIG_KHR;
206 }
207 return config;
John Reck3b202512014-06-23 13:13:08 -0700208}
209
John Reck149173d2015-08-10 09:52:29 -0700210void EglManager::initExtensions() {
John Reck1bcacfd2017-11-03 10:12:19 -0700211 auto extensions = StringUtils::split(eglQueryString(mEglDisplay, EGL_EXTENSIONS));
Romain Guy26a2b972017-04-17 09:39:51 -0700212
John Reck8733bff2016-09-27 14:45:28 -0700213 // For our purposes we don't care if EGL_BUFFER_AGE is a result of
214 // EGL_EXT_buffer_age or EGL_KHR_partial_update as our usage is covered
215 // under EGL_KHR_partial_update and we don't need the expanded scope
216 // that EGL_EXT_buffer_age provides.
John Reck1bcacfd2017-11-03 10:12:19 -0700217 EglExtensions.bufferAge =
218 extensions.has("EGL_EXT_buffer_age") || extensions.has("EGL_KHR_partial_update");
Chris Craik6e6646c2015-09-14 15:54:12 -0700219 EglExtensions.setDamage = extensions.has("EGL_KHR_partial_update");
John Reck708b6682015-10-22 13:11:00 -0700220 LOG_ALWAYS_FATAL_IF(!extensions.has("EGL_KHR_swap_buffers_with_damage"),
John Reck1bcacfd2017-11-03 10:12:19 -0700221 "Missing required extension EGL_KHR_swap_buffers_with_damage");
Romain Guy26a2b972017-04-17 09:39:51 -0700222
223 EglExtensions.glColorSpace = extensions.has("EGL_KHR_gl_colorspace");
224 EglExtensions.noConfigContext = extensions.has("EGL_KHR_no_config_context");
225 EglExtensions.pixelFormatFloat = extensions.has("EGL_EXT_pixel_format_float");
Romain Guy26b6a642017-07-11 09:48:28 -0700226 EglExtensions.scRGB = extensions.has("EGL_EXT_gl_colorspace_scrgb");
Peiyong Lin3bff1352018-12-11 07:56:07 -0800227 EglExtensions.displayP3 = extensions.has("EGL_EXT_gl_colorspace_display_p3_passthrough");
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200228 EglExtensions.contextPriority = extensions.has("EGL_IMG_context_priority");
John Reck1e510712018-04-23 08:15:03 -0700229 EglExtensions.surfacelessContext = extensions.has("EGL_KHR_surfaceless_context");
John Reck149173d2015-08-10 09:52:29 -0700230}
231
John Reck3b202512014-06-23 13:13:08 -0700232bool EglManager::hasEglContext() {
233 return mEglDisplay != EGL_NO_DISPLAY;
234}
235
Romain Guy26a2b972017-04-17 09:39:51 -0700236void EglManager::loadConfigs() {
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700237 // Note: The default pixel format is RGBA_8888, when other formats are
238 // available, we should check the target pixel format and configure the
239 // attributes list properly.
Peiyong Lin3bff1352018-12-11 07:56:07 -0800240 mEglConfig = load8BitsConfig(mEglDisplay, mSwapBehavior);
241 if (mEglConfig == EGL_NO_CONFIG_KHR) {
John Reck149173d2015-08-10 09:52:29 -0700242 if (mSwapBehavior == SwapBehavior::Preserved) {
John Reck3b202512014-06-23 13:13:08 -0700243 // Try again without dirty regions enabled
John Reck149173d2015-08-10 09:52:29 -0700244 ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...");
245 mSwapBehavior = SwapBehavior::Discard;
Peiyong Lin3bff1352018-12-11 07:56:07 -0800246 mEglConfig = load8BitsConfig(mEglDisplay, mSwapBehavior);
John Reck3b202512014-06-23 13:13:08 -0700247 } else {
John Reck149173d2015-08-10 09:52:29 -0700248 // Failed to get a valid config
sergeyv694d4992016-10-27 10:23:13 -0700249 LOG_ALWAYS_FATAL("Failed to choose config, error = %s", eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700250 }
251 }
Peiyong Lin3bff1352018-12-11 07:56:07 -0800252 SkColorType wideColorType = DeviceInfo::get()->getWideColorType();
Romain Guy26a2b972017-04-17 09:39:51 -0700253
Peiyong Lin3bff1352018-12-11 07:56:07 -0800254 // When we reach this point, we have a valid swap behavior
255 if (wideColorType == SkColorType::kRGBA_F16_SkColorType && EglExtensions.pixelFormatFloat) {
256 mEglConfigWideGamut = loadFP16Config(mEglDisplay, mSwapBehavior);
257 if (mEglConfigWideGamut == EGL_NO_CONFIG_KHR) {
Rob Herring74883ab2017-11-29 09:26:31 -0600258 ALOGE("Device claims wide gamut support, cannot find matching config, error = %s",
John Reck0fa0cbc2019-04-05 16:57:46 -0700259 eglErrorString());
Rob Herring74883ab2017-11-29 09:26:31 -0600260 EglExtensions.pixelFormatFloat = false;
Romain Guy26a2b972017-04-17 09:39:51 -0700261 }
Peiyong Lin3bff1352018-12-11 07:56:07 -0800262 } else if (wideColorType == SkColorType::kN32_SkColorType) {
263 mEglConfigWideGamut = load8BitsConfig(mEglDisplay, mSwapBehavior);
Romain Guy26a2b972017-04-17 09:39:51 -0700264 }
John Reck3b202512014-06-23 13:13:08 -0700265}
266
267void EglManager::createContext() {
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200268 std::vector<EGLint> contextAttributes;
269 contextAttributes.reserve(5);
270 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
271 contextAttributes.push_back(GLES_VERSION);
272 if (Properties::contextPriority != 0 && EglExtensions.contextPriority) {
273 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
274 contextAttributes.push_back(Properties::contextPriority);
275 }
276 contextAttributes.push_back(EGL_NONE);
John Reck1bcacfd2017-11-03 10:12:19 -0700277 mEglContext = eglCreateContext(
278 mEglDisplay, EglExtensions.noConfigContext ? ((EGLConfig) nullptr) : mEglConfig,
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200279 EGL_NO_CONTEXT, contextAttributes.data());
John Reck1bcacfd2017-11-03 10:12:19 -0700280 LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, "Failed to create context, error = %s",
281 eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700282}
283
John Reckd7db4d72015-05-20 07:18:55 -0700284void EglManager::createPBufferSurface() {
John Reck3b202512014-06-23 13:13:08 -0700285 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
John Reck1bcacfd2017-11-03 10:12:19 -0700286 "usePBufferSurface() called on uninitialized GlobalContext!");
John Reck3b202512014-06-23 13:13:08 -0700287
John Reck1e510712018-04-23 08:15:03 -0700288 if (mPBufferSurface == EGL_NO_SURFACE && !EglExtensions.surfacelessContext) {
John Reck1bcacfd2017-11-03 10:12:19 -0700289 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
John Reck3b202512014-06-23 13:13:08 -0700290 mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs);
291 }
John Reck3b202512014-06-23 13:13:08 -0700292}
293
Peiyong Lin3bff1352018-12-11 07:56:07 -0800294Result<EGLSurface, EGLint> EglManager::createSurface(EGLNativeWindowType window,
295 ColorMode colorMode,
Brian Osmane0cf5972019-01-23 10:41:20 -0500296 sk_sp<SkColorSpace> colorSpace) {
John Reck1e510712018-04-23 08:15:03 -0700297 LOG_ALWAYS_FATAL_IF(!hasEglContext(), "Not initialized");
Romain Guy253f2c22016-09-28 17:34:42 -0700298
Peiyong Lin3bff1352018-12-11 07:56:07 -0800299 bool wideColorGamut = colorMode == ColorMode::WideColorGamut && mHasWideColorGamutSupport &&
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700300 EglExtensions.noConfigContext;
Romain Guy26a2b972017-04-17 09:39:51 -0700301
302 // The color space we want to use depends on whether linear blending is turned
303 // on and whether the app has requested wide color gamut rendering. When wide
304 // color gamut rendering is off, the app simply renders in the display's native
305 // color gamut.
306 //
307 // When wide gamut rendering is off:
308 // - Blending is done by default in gamma space, which requires using a
309 // linear EGL color space (the GPU uses the color values as is)
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700310 // - If linear blending is on, we must use the non-linear EGL color space
311 // (the GPU will perform sRGB to linear and linear to SRGB conversions
312 // before and after blending)
Romain Guy26a2b972017-04-17 09:39:51 -0700313 //
314 // When wide gamut rendering is on we cannot rely on the GPU performing
315 // linear blending for us. We use two different color spaces to tag the
316 // surface appropriately for SurfaceFlinger:
Peiyong Lin3bff1352018-12-11 07:56:07 -0800317 // - Gamma blending (default) requires the use of the non-linear color space
318 // - Linear blending requires the use of the linear color space
Romain Guy26a2b972017-04-17 09:39:51 -0700319
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700320 // Not all Android targets support the EGL_GL_COLORSPACE_KHR extension
Romain Guy26a2b972017-04-17 09:39:51 -0700321 // We insert to placeholders to set EGL_GL_COLORSPACE_KHR and its value.
322 // According to section 3.4.1 of the EGL specification, the attributes
323 // list is considered empty if the first entry is EGL_NONE
John Reck1bcacfd2017-11-03 10:12:19 -0700324 EGLint attribs[] = {EGL_NONE, EGL_NONE, EGL_NONE};
Romain Guy253f2c22016-09-28 17:34:42 -0700325
Romain Guy26a2b972017-04-17 09:39:51 -0700326 if (EglExtensions.glColorSpace) {
327 attribs[0] = EGL_GL_COLORSPACE_KHR;
Romain Guy26a2b972017-04-17 09:39:51 -0700328 if (wideColorGamut) {
Brian Osmane0cf5972019-01-23 10:41:20 -0500329 skcms_Matrix3x3 colorGamut;
330 LOG_ALWAYS_FATAL_IF(!colorSpace->toXYZD50(&colorGamut),
331 "Could not get gamut matrix from color space");
332 if (memcmp(&colorGamut, &SkNamedGamut::kDCIP3, sizeof(colorGamut)) == 0) {
333 attribs[1] = EGL_GL_COLORSPACE_DISPLAY_P3_PASSTHROUGH_EXT;
334 } else if (memcmp(&colorGamut, &SkNamedGamut::kSRGB, sizeof(colorGamut)) == 0) {
335 attribs[1] = EGL_GL_COLORSPACE_SCRGB_EXT;
336 } else {
337 LOG_ALWAYS_FATAL("Unreachable: unsupported wide color space.");
Peiyong Lin3bff1352018-12-11 07:56:07 -0800338 }
Romain Guy26a2b972017-04-17 09:39:51 -0700339 } else {
Peiyong Lin189021b2018-09-27 16:41:40 -0700340 attribs[1] = EGL_GL_COLORSPACE_LINEAR_KHR;
Romain Guy26a2b972017-04-17 09:39:51 -0700341 }
Romain Guy26a2b972017-04-17 09:39:51 -0700342 }
343
John Reck1bcacfd2017-11-03 10:12:19 -0700344 EGLSurface surface = eglCreateWindowSurface(
345 mEglDisplay, wideColorGamut ? mEglConfigWideGamut : mEglConfig, window, attribs);
John Reck8e539ca2018-11-15 15:21:29 -0800346 if (surface == EGL_NO_SURFACE) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700347 return Error<EGLint>{eglGetError()};
John Reck8e539ca2018-11-15 15:21:29 -0800348 }
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000349
350 if (mSwapBehavior != SwapBehavior::Preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700351 LOG_ALWAYS_FATAL_IF(eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
352 EGL_BUFFER_DESTROYED) == EGL_FALSE,
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000353 "Failed to set swap behavior to destroyed for window %p, eglErr = %s",
John Reck1bcacfd2017-11-03 10:12:19 -0700354 (void*)window, eglErrorString());
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000355 }
356
John Reck3b202512014-06-23 13:13:08 -0700357 return surface;
358}
359
360void EglManager::destroySurface(EGLSurface surface) {
361 if (isCurrent(surface)) {
362 makeCurrent(EGL_NO_SURFACE);
363 }
364 if (!eglDestroySurface(mEglDisplay, surface)) {
sergeyv694d4992016-10-27 10:23:13 -0700365 ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700366 }
367}
368
369void EglManager::destroy() {
370 if (mEglDisplay == EGL_NO_DISPLAY) return;
371
John Reck3b202512014-06-23 13:13:08 -0700372 eglDestroyContext(mEglDisplay, mEglContext);
John Reck1e510712018-04-23 08:15:03 -0700373 if (mPBufferSurface != EGL_NO_SURFACE) {
374 eglDestroySurface(mEglDisplay, mPBufferSurface);
375 }
John Reck3b202512014-06-23 13:13:08 -0700376 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
377 eglTerminate(mEglDisplay);
378 eglReleaseThread();
379
380 mEglDisplay = EGL_NO_DISPLAY;
381 mEglContext = EGL_NO_CONTEXT;
382 mPBufferSurface = EGL_NO_SURFACE;
383 mCurrentSurface = EGL_NO_SURFACE;
384}
385
John Reck1e510712018-04-23 08:15:03 -0700386bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut, bool force) {
387 if (!force && isCurrent(surface)) return false;
John Reck3b202512014-06-23 13:13:08 -0700388
389 if (surface == EGL_NO_SURFACE) {
John Reckd7db4d72015-05-20 07:18:55 -0700390 // Ensure we always have a valid surface & context
391 surface = mPBufferSurface;
392 }
393 if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) {
John Reckf2dcc2a2015-07-16 09:17:59 -0700394 if (errOut) {
395 *errOut = eglGetError();
John Reck1bcacfd2017-11-03 10:12:19 -0700396 ALOGW("Failed to make current on surface %p, error=%s", (void*)surface,
397 egl_error_str(*errOut));
John Reckf2dcc2a2015-07-16 09:17:59 -0700398 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700399 LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", (void*)surface,
400 eglErrorString());
John Reckf2dcc2a2015-07-16 09:17:59 -0700401 }
John Reck3b202512014-06-23 13:13:08 -0700402 }
403 mCurrentSurface = surface;
John Recka8963062017-06-14 10:47:50 -0700404 if (Properties::disableVsync) {
405 eglSwapInterval(mEglDisplay, 0);
406 }
John Reck3b202512014-06-23 13:13:08 -0700407 return true;
408}
409
John Reck149173d2015-08-10 09:52:29 -0700410EGLint EglManager::queryBufferAge(EGLSurface surface) {
411 switch (mSwapBehavior) {
John Reck1bcacfd2017-11-03 10:12:19 -0700412 case SwapBehavior::Discard:
413 return 0;
414 case SwapBehavior::Preserved:
415 return 1;
416 case SwapBehavior::BufferAge:
417 EGLint bufferAge;
418 eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge);
419 return bufferAge;
John Reck149173d2015-08-10 09:52:29 -0700420 }
421 return 0;
422}
423
424Frame EglManager::beginFrame(EGLSurface surface) {
John Reck1bcacfd2017-11-03 10:12:19 -0700425 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, "Tried to beginFrame on EGL_NO_SURFACE!");
John Reck3b202512014-06-23 13:13:08 -0700426 makeCurrent(surface);
John Reck149173d2015-08-10 09:52:29 -0700427 Frame frame;
428 frame.mSurface = surface;
429 eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth);
430 eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight);
431 frame.mBufferAge = queryBufferAge(surface);
John Reck3b202512014-06-23 13:13:08 -0700432 eglBeginFrame(mEglDisplay, surface);
John Reck149173d2015-08-10 09:52:29 -0700433 return frame;
John Reck3b202512014-06-23 13:13:08 -0700434}
435
John Reck149173d2015-08-10 09:52:29 -0700436void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) {
437#ifdef EGL_KHR_partial_update
438 if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) {
439 EGLint rects[4];
440 frame.map(dirty, rects);
441 if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) {
442 LOG_ALWAYS_FATAL("Failed to set damage region on surface %p, error=%s",
John Reck1bcacfd2017-11-03 10:12:19 -0700443 (void*)frame.mSurface, eglErrorString());
John Reck149173d2015-08-10 09:52:29 -0700444 }
445 }
446#endif
447}
448
John Reckc96955d2016-02-26 14:56:44 -0800449bool EglManager::damageRequiresSwap() {
450 return EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge;
451}
452
John Reck149173d2015-08-10 09:52:29 -0700453bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) {
John Reck682573c2015-10-30 10:37:35 -0700454 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
John Reck55156372015-01-21 07:46:37 -0800455 ATRACE_NAME("Finishing GPU work");
456 fence();
457 }
John Reck55156372015-01-21 07:46:37 -0800458
John Recka672f6b2015-10-22 09:53:26 -0700459 EGLint rects[4];
460 frame.map(screenDirty, rects);
John Reck1bcacfd2017-11-03 10:12:19 -0700461 eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects, screenDirty.isEmpty() ? 0 : 1);
John Reckd04794a2015-05-08 10:04:36 -0700462
John Reck3b202512014-06-23 13:13:08 -0700463 EGLint err = eglGetError();
John Reck2cdbc7d2014-09-17 16:06:36 -0700464 if (CC_LIKELY(err == EGL_SUCCESS)) {
465 return true;
466 }
John Reckc2547fa2015-10-26 13:52:52 -0700467 if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) {
John Reck2cdbc7d2014-09-17 16:06:36 -0700468 // For some reason our surface was destroyed out from under us
469 // This really shouldn't happen, but if it does we can recover easily
470 // by just not trying to use the surface anymore
John Reck1bcacfd2017-11-03 10:12:19 -0700471 ALOGW("swapBuffers encountered EGL error %d on %p, halting rendering...", err,
472 frame.mSurface);
John Reck2cdbc7d2014-09-17 16:06:36 -0700473 return false;
474 }
John Reck1bcacfd2017-11-03 10:12:19 -0700475 LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering", err, egl_error_str(err));
John Reck2cdbc7d2014-09-17 16:06:36 -0700476 // Impossible to hit this, but the compiler doesn't know that
477 return false;
John Reck3b202512014-06-23 13:13:08 -0700478}
479
John Reck55156372015-01-21 07:46:37 -0800480void EglManager::fence() {
481 EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL);
John Reck1bcacfd2017-11-03 10:12:19 -0700482 eglClientWaitSyncKHR(mEglDisplay, fence, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR);
John Reck55156372015-01-21 07:46:37 -0800483 eglDestroySyncKHR(mEglDisplay, fence);
484}
485
John Reck1125d1f2014-10-23 11:02:19 -0700486bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) {
John Reck149173d2015-08-10 09:52:29 -0700487 if (mSwapBehavior != SwapBehavior::Preserved) return false;
John Reck3b202512014-06-23 13:13:08 -0700488
John Reck149173d2015-08-10 09:52:29 -0700489 bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
John Reck1bcacfd2017-11-03 10:12:19 -0700490 preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED);
John Reck149173d2015-08-10 09:52:29 -0700491 if (!preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700492 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", (void*)surface,
493 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700494 // Maybe it's already set?
495 EGLint swapBehavior;
496 if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) {
497 preserved = (swapBehavior == EGL_BUFFER_PRESERVED);
498 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700499 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", (void*)surface,
500 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700501 }
John Reck3b202512014-06-23 13:13:08 -0700502 }
John Reck1125d1f2014-10-23 11:02:19 -0700503
504 return preserved;
John Reck3b202512014-06-23 13:13:08 -0700505}
506
Stan Iliev564ca3e2018-09-04 22:00:00 +0000507status_t EglManager::fenceWait(sp<Fence>& fence) {
508 if (!hasEglContext()) {
509 ALOGE("EglManager::fenceWait: EGLDisplay not initialized");
510 return INVALID_OPERATION;
511 }
512
513 if (SyncFeatures::getInstance().useWaitSync() &&
514 SyncFeatures::getInstance().useNativeFenceSync()) {
515 // Block GPU on the fence.
516 // Create an EGLSyncKHR from the current fence.
517 int fenceFd = fence->dup();
518 if (fenceFd == -1) {
519 ALOGE("EglManager::fenceWait: error dup'ing fence fd: %d", errno);
520 return -errno;
521 }
John Reck0fa0cbc2019-04-05 16:57:46 -0700522 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE};
523 EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000524 if (sync == EGL_NO_SYNC_KHR) {
525 close(fenceFd);
526 ALOGE("EglManager::fenceWait: error creating EGL fence: %#x", eglGetError());
527 return UNKNOWN_ERROR;
528 }
529
530 // XXX: The spec draft is inconsistent as to whether this should
531 // return an EGLint or void. Ignore the return value for now, as
532 // it's not strictly needed.
533 eglWaitSyncKHR(mEglDisplay, sync, 0);
534 EGLint eglErr = eglGetError();
535 eglDestroySyncKHR(mEglDisplay, sync);
536 if (eglErr != EGL_SUCCESS) {
537 ALOGE("EglManager::fenceWait: error waiting for EGL fence: %#x", eglErr);
538 return UNKNOWN_ERROR;
539 }
540 } else {
541 // Block CPU on the fence.
542 status_t err = fence->waitForever("EglManager::fenceWait");
543 if (err != NO_ERROR) {
544 ALOGE("EglManager::fenceWait: error waiting for fence: %d", err);
545 return err;
546 }
547 }
548 return OK;
549}
550
551status_t EglManager::createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence,
John Reck0fa0cbc2019-04-05 16:57:46 -0700552 sp<Fence>& nativeFence) {
Stan Iliev564ca3e2018-09-04 22:00:00 +0000553 if (!hasEglContext()) {
554 ALOGE("EglManager::createReleaseFence: EGLDisplay not initialized");
555 return INVALID_OPERATION;
556 }
557
558 if (SyncFeatures::getInstance().useNativeFenceSync()) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700559 EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000560 if (sync == EGL_NO_SYNC_KHR) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700561 ALOGE("EglManager::createReleaseFence: error creating EGL fence: %#x", eglGetError());
Stan Iliev564ca3e2018-09-04 22:00:00 +0000562 return UNKNOWN_ERROR;
563 }
564 glFlush();
565 int fenceFd = eglDupNativeFenceFDANDROID(mEglDisplay, sync);
566 eglDestroySyncKHR(mEglDisplay, sync);
567 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
568 ALOGE("EglManager::createReleaseFence: error dup'ing native fence "
John Reck0fa0cbc2019-04-05 16:57:46 -0700569 "fd: %#x",
570 eglGetError());
Stan Iliev564ca3e2018-09-04 22:00:00 +0000571 return UNKNOWN_ERROR;
572 }
573 nativeFence = new Fence(fenceFd);
574 *eglFence = EGL_NO_SYNC_KHR;
575 } else if (useFenceSync && SyncFeatures::getInstance().useFenceSync()) {
576 if (*eglFence != EGL_NO_SYNC_KHR) {
577 // There is already a fence for the current slot. We need to
578 // wait on that before replacing it with another fence to
579 // ensure that all outstanding buffer accesses have completed
580 // before the producer accesses it.
581 EGLint result = eglClientWaitSyncKHR(mEglDisplay, *eglFence, 0, 1000000000);
582 if (result == EGL_FALSE) {
583 ALOGE("EglManager::createReleaseFence: error waiting for previous fence: %#x",
John Reck0fa0cbc2019-04-05 16:57:46 -0700584 eglGetError());
Stan Iliev564ca3e2018-09-04 22:00:00 +0000585 return UNKNOWN_ERROR;
586 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
587 ALOGE("EglManager::createReleaseFence: timeout waiting for previous fence");
588 return TIMED_OUT;
589 }
590 eglDestroySyncKHR(mEglDisplay, *eglFence);
591 }
592
593 // Create a fence for the outstanding accesses in the current
594 // OpenGL ES context.
595 *eglFence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, nullptr);
596 if (*eglFence == EGL_NO_SYNC_KHR) {
597 ALOGE("EglManager::createReleaseFence: error creating fence: %#x", eglGetError());
598 return UNKNOWN_ERROR;
599 }
600 glFlush();
601 }
602 return OK;
603}
604
John Reck3b202512014-06-23 13:13:08 -0700605} /* namespace renderthread */
606} /* namespace uirenderer */
607} /* namespace android */