blob: 2cc3f362e172de24585106d5624e38a152a4ed3e [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 Reck1e510712018-04-23 08:15:03 -070032#include <string>
33#include <vector>
John Reck848f6512018-12-03 13:26:43 -080034#include <system/window.h>
35#include <gui/Surface.h>
Derek Sollenberger7e044fe2016-11-07 10:57:59 -050036
John Reck3b202512014-06-23 13:13:08 -070037#define GLES_VERSION 2
38
39// Android-specific addition that is used to show when frames began in systrace
40EGLAPI void EGLAPIENTRY eglBeginFrame(EGLDisplay dpy, EGLSurface surface);
41
42namespace android {
43namespace uirenderer {
44namespace renderthread {
45
John Reck1bcacfd2017-11-03 10:12:19 -070046#define ERROR_CASE(x) \
47 case x: \
48 return #x;
John Reck3b202512014-06-23 13:13:08 -070049static const char* egl_error_str(EGLint error) {
50 switch (error) {
51 ERROR_CASE(EGL_SUCCESS)
52 ERROR_CASE(EGL_NOT_INITIALIZED)
53 ERROR_CASE(EGL_BAD_ACCESS)
54 ERROR_CASE(EGL_BAD_ALLOC)
55 ERROR_CASE(EGL_BAD_ATTRIBUTE)
56 ERROR_CASE(EGL_BAD_CONFIG)
57 ERROR_CASE(EGL_BAD_CONTEXT)
58 ERROR_CASE(EGL_BAD_CURRENT_SURFACE)
59 ERROR_CASE(EGL_BAD_DISPLAY)
60 ERROR_CASE(EGL_BAD_MATCH)
61 ERROR_CASE(EGL_BAD_NATIVE_PIXMAP)
62 ERROR_CASE(EGL_BAD_NATIVE_WINDOW)
63 ERROR_CASE(EGL_BAD_PARAMETER)
64 ERROR_CASE(EGL_BAD_SURFACE)
65 ERROR_CASE(EGL_CONTEXT_LOST)
John Reck1bcacfd2017-11-03 10:12:19 -070066 default:
67 return "Unknown error";
John Reck3b202512014-06-23 13:13:08 -070068 }
69}
sergeyv694d4992016-10-27 10:23:13 -070070const char* EglManager::eglErrorString() {
John Reck3b202512014-06-23 13:13:08 -070071 return egl_error_str(eglGetError());
72}
73
John Reck149173d2015-08-10 09:52:29 -070074static struct {
75 bool bufferAge = false;
76 bool setDamage = false;
Romain Guy26a2b972017-04-17 09:39:51 -070077 bool noConfigContext = false;
78 bool pixelFormatFloat = false;
79 bool glColorSpace = false;
Romain Guy26b6a642017-07-11 09:48:28 -070080 bool scRGB = false;
Peiyong Lin1f6aa122018-09-10 16:28:08 -070081 bool displayP3 = false;
Jorim Jaggi767e25e2018-04-04 23:07:35 +020082 bool contextPriority = false;
John Reck1e510712018-04-23 08:15:03 -070083 bool surfacelessContext = false;
John Reck149173d2015-08-10 09:52:29 -070084} EglExtensions;
85
John Reck1e510712018-04-23 08:15:03 -070086EglManager::EglManager()
87 : mEglDisplay(EGL_NO_DISPLAY)
Chris Craikd41c4d82015-01-05 15:51:13 -080088 , mEglConfig(nullptr)
Romain Guy26a2b972017-04-17 09:39:51 -070089 , mEglConfigWideGamut(nullptr)
John Reck3b202512014-06-23 13:13:08 -070090 , mEglContext(EGL_NO_CONTEXT)
91 , mPBufferSurface(EGL_NO_SURFACE)
Peiyong Lin3bff1352018-12-11 07:56:07 -080092 , mCurrentSurface(EGL_NO_SURFACE)
93 , mHasWideColorGamutSupport(false) {}
John Reck3b202512014-06-23 13:13:08 -070094
John Reck1e510712018-04-23 08:15:03 -070095EglManager::~EglManager() {
John Reck6104cea2019-01-10 14:37:17 -080096 if (hasEglContext()) {
97 ALOGW("~EglManager() leaked an EGL context");
98 }
John Reck1e510712018-04-23 08:15:03 -070099}
100
John Reck3b202512014-06-23 13:13:08 -0700101void EglManager::initialize() {
102 if (hasEglContext()) return;
103
John Reckfbc8df02014-11-14 16:18:41 -0800104 ATRACE_NAME("Creating EGLContext");
105
John Reck3b202512014-06-23 13:13:08 -0700106 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
John Reck1bcacfd2017-11-03 10:12:19 -0700107 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, "Failed to get EGL_DEFAULT_DISPLAY! err=%s",
108 eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700109
110 EGLint major, minor;
111 LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE,
John Reck1bcacfd2017-11-03 10:12:19 -0700112 "Failed to initialize display %p! err=%s", mEglDisplay, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700113
John Reck848f6512018-12-03 13:26:43 -0800114 ALOGV("Initialized EGL, version %d.%d", (int)major, (int)minor);
John Reck3b202512014-06-23 13:13:08 -0700115
John Reck149173d2015-08-10 09:52:29 -0700116 initExtensions();
Season Li13d1b4a2015-07-29 17:16:19 -0700117
John Reck149173d2015-08-10 09:52:29 -0700118 // Now that extensions are loaded, pick a swap behavior
119 if (Properties::enablePartialUpdates) {
Matt Sarettb77c94a2016-12-07 12:22:37 -0500120 // An Adreno driver bug is causing rendering problems for SkiaGL with
121 // buffer age swap behavior (b/31957043). To temporarily workaround,
122 // we will use preserved swap behavior.
Derek Sollenbergerb5a12bd2017-06-14 15:23:19 -0400123 if (Properties::useBufferAge && EglExtensions.bufferAge) {
John Reck149173d2015-08-10 09:52:29 -0700124 mSwapBehavior = SwapBehavior::BufferAge;
125 } else {
126 mSwapBehavior = SwapBehavior::Preserved;
127 }
128 }
129
Romain Guy26a2b972017-04-17 09:39:51 -0700130 loadConfigs();
John Reck3b202512014-06-23 13:13:08 -0700131 createContext();
John Reckd7db4d72015-05-20 07:18:55 -0700132 createPBufferSurface();
John Reck1e510712018-04-23 08:15:03 -0700133 makeCurrent(mPBufferSurface, nullptr, /* force */ true);
Peiyong Lin3bff1352018-12-11 07:56:07 -0800134
Brian Osmane0cf5972019-01-23 10:41:20 -0500135 skcms_Matrix3x3 wideColorGamut;
136 LOG_ALWAYS_FATAL_IF(!DeviceInfo::get()->getWideColorSpace()->toXYZD50(&wideColorGamut),
137 "Could not get gamut matrix from wideColorSpace");
Peiyong Lin3bff1352018-12-11 07:56:07 -0800138 bool hasWideColorSpaceExtension = false;
Brian Osmane0cf5972019-01-23 10:41:20 -0500139 if (memcmp(&wideColorGamut, &SkNamedGamut::kDCIP3, sizeof(wideColorGamut)) == 0) {
Peiyong Lin3bff1352018-12-11 07:56:07 -0800140 hasWideColorSpaceExtension = EglExtensions.displayP3;
Brian Osmane0cf5972019-01-23 10:41:20 -0500141 } else if (memcmp(&wideColorGamut, &SkNamedGamut::kSRGB, sizeof(wideColorGamut)) == 0) {
Peiyong Lin3bff1352018-12-11 07:56:07 -0800142 hasWideColorSpaceExtension = EglExtensions.scRGB;
143 } else {
144 LOG_ALWAYS_FATAL("Unsupported wide color space.");
145 }
146 mHasWideColorGamutSupport = EglExtensions.glColorSpace && hasWideColorSpaceExtension &&
147 mEglConfigWideGamut != EGL_NO_CONFIG_KHR;
148}
149
150EGLConfig EglManager::load8BitsConfig(EGLDisplay display, EglManager::SwapBehavior swapBehavior) {
151 EGLint eglSwapBehavior =
152 (swapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
153 EGLint attribs[] = {EGL_RENDERABLE_TYPE,
154 EGL_OPENGL_ES2_BIT,
155 EGL_RED_SIZE,
156 8,
157 EGL_GREEN_SIZE,
158 8,
159 EGL_BLUE_SIZE,
160 8,
161 EGL_ALPHA_SIZE,
162 8,
163 EGL_DEPTH_SIZE,
164 0,
165 EGL_CONFIG_CAVEAT,
166 EGL_NONE,
167 EGL_STENCIL_SIZE,
168 STENCIL_BUFFER_SIZE,
169 EGL_SURFACE_TYPE,
170 EGL_WINDOW_BIT | eglSwapBehavior,
171 EGL_NONE};
172 EGLConfig config = EGL_NO_CONFIG_KHR;
173 EGLint numConfigs = 1;
174 if (!eglChooseConfig(display, attribs, &config, numConfigs, &numConfigs) ||
175 numConfigs != 1) {
176 return EGL_NO_CONFIG_KHR;
177 }
178 return config;
179}
180
181EGLConfig EglManager::loadFP16Config(EGLDisplay display, SwapBehavior swapBehavior) {
182 EGLint eglSwapBehavior =
183 (swapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
184 // If we reached this point, we have a valid swap behavior
185 EGLint attribs[] = {EGL_RENDERABLE_TYPE,
186 EGL_OPENGL_ES2_BIT,
187 EGL_COLOR_COMPONENT_TYPE_EXT,
188 EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT,
189 EGL_RED_SIZE,
190 16,
191 EGL_GREEN_SIZE,
192 16,
193 EGL_BLUE_SIZE,
194 16,
195 EGL_ALPHA_SIZE,
196 16,
197 EGL_DEPTH_SIZE,
198 0,
199 EGL_STENCIL_SIZE,
200 STENCIL_BUFFER_SIZE,
201 EGL_SURFACE_TYPE,
202 EGL_WINDOW_BIT | eglSwapBehavior,
203 EGL_NONE};
204 EGLConfig config = EGL_NO_CONFIG_KHR;
205 EGLint numConfigs = 1;
206 if (!eglChooseConfig(display, attribs, &config, numConfigs, &numConfigs) ||
207 numConfigs != 1) {
208 return EGL_NO_CONFIG_KHR;
209 }
210 return config;
John Reck3b202512014-06-23 13:13:08 -0700211}
212
John Reck149173d2015-08-10 09:52:29 -0700213void EglManager::initExtensions() {
John Reck1bcacfd2017-11-03 10:12:19 -0700214 auto extensions = StringUtils::split(eglQueryString(mEglDisplay, EGL_EXTENSIONS));
Romain Guy26a2b972017-04-17 09:39:51 -0700215
John Reck8733bff2016-09-27 14:45:28 -0700216 // For our purposes we don't care if EGL_BUFFER_AGE is a result of
217 // EGL_EXT_buffer_age or EGL_KHR_partial_update as our usage is covered
218 // under EGL_KHR_partial_update and we don't need the expanded scope
219 // that EGL_EXT_buffer_age provides.
John Reck1bcacfd2017-11-03 10:12:19 -0700220 EglExtensions.bufferAge =
221 extensions.has("EGL_EXT_buffer_age") || extensions.has("EGL_KHR_partial_update");
Chris Craik6e6646c2015-09-14 15:54:12 -0700222 EglExtensions.setDamage = extensions.has("EGL_KHR_partial_update");
John Reck708b6682015-10-22 13:11:00 -0700223 LOG_ALWAYS_FATAL_IF(!extensions.has("EGL_KHR_swap_buffers_with_damage"),
John Reck1bcacfd2017-11-03 10:12:19 -0700224 "Missing required extension EGL_KHR_swap_buffers_with_damage");
Romain Guy26a2b972017-04-17 09:39:51 -0700225
226 EglExtensions.glColorSpace = extensions.has("EGL_KHR_gl_colorspace");
227 EglExtensions.noConfigContext = extensions.has("EGL_KHR_no_config_context");
228 EglExtensions.pixelFormatFloat = extensions.has("EGL_EXT_pixel_format_float");
Romain Guy26b6a642017-07-11 09:48:28 -0700229 EglExtensions.scRGB = extensions.has("EGL_EXT_gl_colorspace_scrgb");
Peiyong Lin3bff1352018-12-11 07:56:07 -0800230 EglExtensions.displayP3 = extensions.has("EGL_EXT_gl_colorspace_display_p3_passthrough");
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200231 EglExtensions.contextPriority = extensions.has("EGL_IMG_context_priority");
John Reck1e510712018-04-23 08:15:03 -0700232 EglExtensions.surfacelessContext = extensions.has("EGL_KHR_surfaceless_context");
John Reck149173d2015-08-10 09:52:29 -0700233}
234
John Reck3b202512014-06-23 13:13:08 -0700235bool EglManager::hasEglContext() {
236 return mEglDisplay != EGL_NO_DISPLAY;
237}
238
Romain Guy26a2b972017-04-17 09:39:51 -0700239void EglManager::loadConfigs() {
John Reck149173d2015-08-10 09:52:29 -0700240 ALOGD("Swap behavior %d", static_cast<int>(mSwapBehavior));
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700241
242 // Note: The default pixel format is RGBA_8888, when other formats are
243 // available, we should check the target pixel format and configure the
244 // attributes list properly.
Peiyong Lin3bff1352018-12-11 07:56:07 -0800245 mEglConfig = load8BitsConfig(mEglDisplay, mSwapBehavior);
246 if (mEglConfig == EGL_NO_CONFIG_KHR) {
John Reck149173d2015-08-10 09:52:29 -0700247 if (mSwapBehavior == SwapBehavior::Preserved) {
John Reck3b202512014-06-23 13:13:08 -0700248 // Try again without dirty regions enabled
John Reck149173d2015-08-10 09:52:29 -0700249 ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...");
250 mSwapBehavior = SwapBehavior::Discard;
Peiyong Lin3bff1352018-12-11 07:56:07 -0800251 ALOGD("Swap behavior %d", static_cast<int>(mSwapBehavior));
252 mEglConfig = load8BitsConfig(mEglDisplay, mSwapBehavior);
John Reck3b202512014-06-23 13:13:08 -0700253 } else {
John Reck149173d2015-08-10 09:52:29 -0700254 // Failed to get a valid config
sergeyv694d4992016-10-27 10:23:13 -0700255 LOG_ALWAYS_FATAL("Failed to choose config, error = %s", eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700256 }
257 }
Peiyong Lin3bff1352018-12-11 07:56:07 -0800258 SkColorType wideColorType = DeviceInfo::get()->getWideColorType();
Romain Guy26a2b972017-04-17 09:39:51 -0700259
Peiyong Lin3bff1352018-12-11 07:56:07 -0800260 // When we reach this point, we have a valid swap behavior
261 if (wideColorType == SkColorType::kRGBA_F16_SkColorType && EglExtensions.pixelFormatFloat) {
262 mEglConfigWideGamut = loadFP16Config(mEglDisplay, mSwapBehavior);
263 if (mEglConfigWideGamut == EGL_NO_CONFIG_KHR) {
Rob Herring74883ab2017-11-29 09:26:31 -0600264 ALOGE("Device claims wide gamut support, cannot find matching config, error = %s",
John Reck1a4a9812018-04-18 16:13:31 -0700265 eglErrorString());
Rob Herring74883ab2017-11-29 09:26:31 -0600266 EglExtensions.pixelFormatFloat = false;
Romain Guy26a2b972017-04-17 09:39:51 -0700267 }
Peiyong Lin3bff1352018-12-11 07:56:07 -0800268 } else if (wideColorType == SkColorType::kN32_SkColorType) {
269 mEglConfigWideGamut = load8BitsConfig(mEglDisplay, mSwapBehavior);
Romain Guy26a2b972017-04-17 09:39:51 -0700270 }
John Reck3b202512014-06-23 13:13:08 -0700271}
272
273void EglManager::createContext() {
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200274 std::vector<EGLint> contextAttributes;
275 contextAttributes.reserve(5);
276 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
277 contextAttributes.push_back(GLES_VERSION);
278 if (Properties::contextPriority != 0 && EglExtensions.contextPriority) {
279 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
280 contextAttributes.push_back(Properties::contextPriority);
281 }
282 contextAttributes.push_back(EGL_NONE);
John Reck1bcacfd2017-11-03 10:12:19 -0700283 mEglContext = eglCreateContext(
284 mEglDisplay, EglExtensions.noConfigContext ? ((EGLConfig) nullptr) : mEglConfig,
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200285 EGL_NO_CONTEXT, contextAttributes.data());
John Reck1bcacfd2017-11-03 10:12:19 -0700286 LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, "Failed to create context, error = %s",
287 eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700288}
289
John Reckd7db4d72015-05-20 07:18:55 -0700290void EglManager::createPBufferSurface() {
John Reck3b202512014-06-23 13:13:08 -0700291 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
John Reck1bcacfd2017-11-03 10:12:19 -0700292 "usePBufferSurface() called on uninitialized GlobalContext!");
John Reck3b202512014-06-23 13:13:08 -0700293
John Reck1e510712018-04-23 08:15:03 -0700294 if (mPBufferSurface == EGL_NO_SURFACE && !EglExtensions.surfacelessContext) {
John Reck1bcacfd2017-11-03 10:12:19 -0700295 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
John Reck3b202512014-06-23 13:13:08 -0700296 mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs);
297 }
John Reck3b202512014-06-23 13:13:08 -0700298}
299
Peiyong Lin3bff1352018-12-11 07:56:07 -0800300Result<EGLSurface, EGLint> EglManager::createSurface(EGLNativeWindowType window,
301 ColorMode colorMode,
Brian Osmane0cf5972019-01-23 10:41:20 -0500302 sk_sp<SkColorSpace> colorSpace) {
John Reck1e510712018-04-23 08:15:03 -0700303 LOG_ALWAYS_FATAL_IF(!hasEglContext(), "Not initialized");
Romain Guy253f2c22016-09-28 17:34:42 -0700304
Peiyong Lin3bff1352018-12-11 07:56:07 -0800305 bool wideColorGamut = colorMode == ColorMode::WideColorGamut && mHasWideColorGamutSupport &&
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700306 EglExtensions.noConfigContext;
Romain Guy26a2b972017-04-17 09:39:51 -0700307
308 // The color space we want to use depends on whether linear blending is turned
309 // on and whether the app has requested wide color gamut rendering. When wide
310 // color gamut rendering is off, the app simply renders in the display's native
311 // color gamut.
312 //
313 // When wide gamut rendering is off:
314 // - Blending is done by default in gamma space, which requires using a
315 // linear EGL color space (the GPU uses the color values as is)
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700316 // - If linear blending is on, we must use the non-linear EGL color space
317 // (the GPU will perform sRGB to linear and linear to SRGB conversions
318 // before and after blending)
Romain Guy26a2b972017-04-17 09:39:51 -0700319 //
320 // When wide gamut rendering is on we cannot rely on the GPU performing
321 // linear blending for us. We use two different color spaces to tag the
322 // surface appropriately for SurfaceFlinger:
Peiyong Lin3bff1352018-12-11 07:56:07 -0800323 // - Gamma blending (default) requires the use of the non-linear color space
324 // - Linear blending requires the use of the linear color space
Romain Guy26a2b972017-04-17 09:39:51 -0700325
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700326 // Not all Android targets support the EGL_GL_COLORSPACE_KHR extension
Romain Guy26a2b972017-04-17 09:39:51 -0700327 // We insert to placeholders to set EGL_GL_COLORSPACE_KHR and its value.
328 // According to section 3.4.1 of the EGL specification, the attributes
329 // list is considered empty if the first entry is EGL_NONE
John Reck1bcacfd2017-11-03 10:12:19 -0700330 EGLint attribs[] = {EGL_NONE, EGL_NONE, EGL_NONE};
Romain Guy253f2c22016-09-28 17:34:42 -0700331
Romain Guy26a2b972017-04-17 09:39:51 -0700332 if (EglExtensions.glColorSpace) {
333 attribs[0] = EGL_GL_COLORSPACE_KHR;
Romain Guy26a2b972017-04-17 09:39:51 -0700334 if (wideColorGamut) {
Brian Osmane0cf5972019-01-23 10:41:20 -0500335 skcms_Matrix3x3 colorGamut;
336 LOG_ALWAYS_FATAL_IF(!colorSpace->toXYZD50(&colorGamut),
337 "Could not get gamut matrix from color space");
338 if (memcmp(&colorGamut, &SkNamedGamut::kDCIP3, sizeof(colorGamut)) == 0) {
339 attribs[1] = EGL_GL_COLORSPACE_DISPLAY_P3_PASSTHROUGH_EXT;
340 } else if (memcmp(&colorGamut, &SkNamedGamut::kSRGB, sizeof(colorGamut)) == 0) {
341 attribs[1] = EGL_GL_COLORSPACE_SCRGB_EXT;
342 } else {
343 LOG_ALWAYS_FATAL("Unreachable: unsupported wide color space.");
Peiyong Lin3bff1352018-12-11 07:56:07 -0800344 }
Romain Guy26a2b972017-04-17 09:39:51 -0700345 } else {
Peiyong Lin189021b2018-09-27 16:41:40 -0700346 attribs[1] = EGL_GL_COLORSPACE_LINEAR_KHR;
Romain Guy26a2b972017-04-17 09:39:51 -0700347 }
Romain Guy26a2b972017-04-17 09:39:51 -0700348 }
349
John Reck1bcacfd2017-11-03 10:12:19 -0700350 EGLSurface surface = eglCreateWindowSurface(
351 mEglDisplay, wideColorGamut ? mEglConfigWideGamut : mEglConfig, window, attribs);
John Reck8e539ca2018-11-15 15:21:29 -0800352 if (surface == EGL_NO_SURFACE) {
353 return Error<EGLint> { eglGetError() };
354 }
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000355
356 if (mSwapBehavior != SwapBehavior::Preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700357 LOG_ALWAYS_FATAL_IF(eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
358 EGL_BUFFER_DESTROYED) == EGL_FALSE,
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000359 "Failed to set swap behavior to destroyed for window %p, eglErr = %s",
John Reck1bcacfd2017-11-03 10:12:19 -0700360 (void*)window, eglErrorString());
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000361 }
362
John Reck3b202512014-06-23 13:13:08 -0700363 return surface;
364}
365
366void EglManager::destroySurface(EGLSurface surface) {
367 if (isCurrent(surface)) {
368 makeCurrent(EGL_NO_SURFACE);
369 }
370 if (!eglDestroySurface(mEglDisplay, surface)) {
sergeyv694d4992016-10-27 10:23:13 -0700371 ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700372 }
373}
374
375void EglManager::destroy() {
376 if (mEglDisplay == EGL_NO_DISPLAY) return;
377
John Reck3b202512014-06-23 13:13:08 -0700378 eglDestroyContext(mEglDisplay, mEglContext);
John Reck1e510712018-04-23 08:15:03 -0700379 if (mPBufferSurface != EGL_NO_SURFACE) {
380 eglDestroySurface(mEglDisplay, mPBufferSurface);
381 }
John Reck3b202512014-06-23 13:13:08 -0700382 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
383 eglTerminate(mEglDisplay);
384 eglReleaseThread();
385
386 mEglDisplay = EGL_NO_DISPLAY;
387 mEglContext = EGL_NO_CONTEXT;
388 mPBufferSurface = EGL_NO_SURFACE;
389 mCurrentSurface = EGL_NO_SURFACE;
390}
391
John Reck1e510712018-04-23 08:15:03 -0700392bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut, bool force) {
393 if (!force && isCurrent(surface)) return false;
John Reck3b202512014-06-23 13:13:08 -0700394
395 if (surface == EGL_NO_SURFACE) {
John Reckd7db4d72015-05-20 07:18:55 -0700396 // Ensure we always have a valid surface & context
397 surface = mPBufferSurface;
398 }
399 if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) {
John Reckf2dcc2a2015-07-16 09:17:59 -0700400 if (errOut) {
401 *errOut = eglGetError();
John Reck1bcacfd2017-11-03 10:12:19 -0700402 ALOGW("Failed to make current on surface %p, error=%s", (void*)surface,
403 egl_error_str(*errOut));
John Reckf2dcc2a2015-07-16 09:17:59 -0700404 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700405 LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", (void*)surface,
406 eglErrorString());
John Reckf2dcc2a2015-07-16 09:17:59 -0700407 }
John Reck3b202512014-06-23 13:13:08 -0700408 }
409 mCurrentSurface = surface;
John Recka8963062017-06-14 10:47:50 -0700410 if (Properties::disableVsync) {
411 eglSwapInterval(mEglDisplay, 0);
412 }
John Reck3b202512014-06-23 13:13:08 -0700413 return true;
414}
415
John Reck149173d2015-08-10 09:52:29 -0700416EGLint EglManager::queryBufferAge(EGLSurface surface) {
417 switch (mSwapBehavior) {
John Reck1bcacfd2017-11-03 10:12:19 -0700418 case SwapBehavior::Discard:
419 return 0;
420 case SwapBehavior::Preserved:
421 return 1;
422 case SwapBehavior::BufferAge:
423 EGLint bufferAge;
424 eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge);
425 return bufferAge;
John Reck149173d2015-08-10 09:52:29 -0700426 }
427 return 0;
428}
429
430Frame EglManager::beginFrame(EGLSurface surface) {
John Reck1bcacfd2017-11-03 10:12:19 -0700431 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, "Tried to beginFrame on EGL_NO_SURFACE!");
John Reck3b202512014-06-23 13:13:08 -0700432 makeCurrent(surface);
John Reck149173d2015-08-10 09:52:29 -0700433 Frame frame;
434 frame.mSurface = surface;
435 eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth);
436 eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight);
437 frame.mBufferAge = queryBufferAge(surface);
John Reck3b202512014-06-23 13:13:08 -0700438 eglBeginFrame(mEglDisplay, surface);
John Reck149173d2015-08-10 09:52:29 -0700439 return frame;
John Reck3b202512014-06-23 13:13:08 -0700440}
441
John Reck149173d2015-08-10 09:52:29 -0700442void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) {
443#ifdef EGL_KHR_partial_update
444 if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) {
445 EGLint rects[4];
446 frame.map(dirty, rects);
447 if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) {
448 LOG_ALWAYS_FATAL("Failed to set damage region on surface %p, error=%s",
John Reck1bcacfd2017-11-03 10:12:19 -0700449 (void*)frame.mSurface, eglErrorString());
John Reck149173d2015-08-10 09:52:29 -0700450 }
451 }
452#endif
453}
454
John Reckc96955d2016-02-26 14:56:44 -0800455bool EglManager::damageRequiresSwap() {
456 return EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge;
457}
458
John Reck149173d2015-08-10 09:52:29 -0700459bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) {
John Reck682573c2015-10-30 10:37:35 -0700460 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
John Reck55156372015-01-21 07:46:37 -0800461 ATRACE_NAME("Finishing GPU work");
462 fence();
463 }
John Reck55156372015-01-21 07:46:37 -0800464
John Recka672f6b2015-10-22 09:53:26 -0700465 EGLint rects[4];
466 frame.map(screenDirty, rects);
John Reck1bcacfd2017-11-03 10:12:19 -0700467 eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects, screenDirty.isEmpty() ? 0 : 1);
John Reckd04794a2015-05-08 10:04:36 -0700468
John Reck3b202512014-06-23 13:13:08 -0700469 EGLint err = eglGetError();
John Reck2cdbc7d2014-09-17 16:06:36 -0700470 if (CC_LIKELY(err == EGL_SUCCESS)) {
471 return true;
472 }
John Reckc2547fa2015-10-26 13:52:52 -0700473 if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) {
John Reck2cdbc7d2014-09-17 16:06:36 -0700474 // For some reason our surface was destroyed out from under us
475 // This really shouldn't happen, but if it does we can recover easily
476 // by just not trying to use the surface anymore
John Reck1bcacfd2017-11-03 10:12:19 -0700477 ALOGW("swapBuffers encountered EGL error %d on %p, halting rendering...", err,
478 frame.mSurface);
John Reck2cdbc7d2014-09-17 16:06:36 -0700479 return false;
480 }
John Reck1bcacfd2017-11-03 10:12:19 -0700481 LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering", err, egl_error_str(err));
John Reck2cdbc7d2014-09-17 16:06:36 -0700482 // Impossible to hit this, but the compiler doesn't know that
483 return false;
John Reck3b202512014-06-23 13:13:08 -0700484}
485
John Reck55156372015-01-21 07:46:37 -0800486void EglManager::fence() {
487 EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL);
John Reck1bcacfd2017-11-03 10:12:19 -0700488 eglClientWaitSyncKHR(mEglDisplay, fence, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR);
John Reck55156372015-01-21 07:46:37 -0800489 eglDestroySyncKHR(mEglDisplay, fence);
490}
491
John Reck1125d1f2014-10-23 11:02:19 -0700492bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) {
John Reck149173d2015-08-10 09:52:29 -0700493 if (mSwapBehavior != SwapBehavior::Preserved) return false;
John Reck3b202512014-06-23 13:13:08 -0700494
John Reck149173d2015-08-10 09:52:29 -0700495 bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
John Reck1bcacfd2017-11-03 10:12:19 -0700496 preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED);
John Reck149173d2015-08-10 09:52:29 -0700497 if (!preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700498 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", (void*)surface,
499 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700500 // Maybe it's already set?
501 EGLint swapBehavior;
502 if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) {
503 preserved = (swapBehavior == EGL_BUFFER_PRESERVED);
504 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700505 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", (void*)surface,
506 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700507 }
John Reck3b202512014-06-23 13:13:08 -0700508 }
John Reck1125d1f2014-10-23 11:02:19 -0700509
510 return preserved;
John Reck3b202512014-06-23 13:13:08 -0700511}
512
Stan Iliev564ca3e2018-09-04 22:00:00 +0000513status_t EglManager::fenceWait(sp<Fence>& fence) {
514 if (!hasEglContext()) {
515 ALOGE("EglManager::fenceWait: EGLDisplay not initialized");
516 return INVALID_OPERATION;
517 }
518
519 if (SyncFeatures::getInstance().useWaitSync() &&
520 SyncFeatures::getInstance().useNativeFenceSync()) {
521 // Block GPU on the fence.
522 // Create an EGLSyncKHR from the current fence.
523 int fenceFd = fence->dup();
524 if (fenceFd == -1) {
525 ALOGE("EglManager::fenceWait: error dup'ing fence fd: %d", errno);
526 return -errno;
527 }
528 EGLint attribs[] = {
529 EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd,
530 EGL_NONE
531 };
532 EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay,
533 EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
534 if (sync == EGL_NO_SYNC_KHR) {
535 close(fenceFd);
536 ALOGE("EglManager::fenceWait: error creating EGL fence: %#x", eglGetError());
537 return UNKNOWN_ERROR;
538 }
539
540 // XXX: The spec draft is inconsistent as to whether this should
541 // return an EGLint or void. Ignore the return value for now, as
542 // it's not strictly needed.
543 eglWaitSyncKHR(mEglDisplay, sync, 0);
544 EGLint eglErr = eglGetError();
545 eglDestroySyncKHR(mEglDisplay, sync);
546 if (eglErr != EGL_SUCCESS) {
547 ALOGE("EglManager::fenceWait: error waiting for EGL fence: %#x", eglErr);
548 return UNKNOWN_ERROR;
549 }
550 } else {
551 // Block CPU on the fence.
552 status_t err = fence->waitForever("EglManager::fenceWait");
553 if (err != NO_ERROR) {
554 ALOGE("EglManager::fenceWait: error waiting for fence: %d", err);
555 return err;
556 }
557 }
558 return OK;
559}
560
561status_t EglManager::createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence,
562 sp<Fence>& nativeFence) {
563 if (!hasEglContext()) {
564 ALOGE("EglManager::createReleaseFence: EGLDisplay not initialized");
565 return INVALID_OPERATION;
566 }
567
568 if (SyncFeatures::getInstance().useNativeFenceSync()) {
569 EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay,
570 EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
571 if (sync == EGL_NO_SYNC_KHR) {
572 ALOGE("EglManager::createReleaseFence: error creating EGL fence: %#x",
573 eglGetError());
574 return UNKNOWN_ERROR;
575 }
576 glFlush();
577 int fenceFd = eglDupNativeFenceFDANDROID(mEglDisplay, sync);
578 eglDestroySyncKHR(mEglDisplay, sync);
579 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
580 ALOGE("EglManager::createReleaseFence: error dup'ing native fence "
581 "fd: %#x", eglGetError());
582 return UNKNOWN_ERROR;
583 }
584 nativeFence = new Fence(fenceFd);
585 *eglFence = EGL_NO_SYNC_KHR;
586 } else if (useFenceSync && SyncFeatures::getInstance().useFenceSync()) {
587 if (*eglFence != EGL_NO_SYNC_KHR) {
588 // There is already a fence for the current slot. We need to
589 // wait on that before replacing it with another fence to
590 // ensure that all outstanding buffer accesses have completed
591 // before the producer accesses it.
592 EGLint result = eglClientWaitSyncKHR(mEglDisplay, *eglFence, 0, 1000000000);
593 if (result == EGL_FALSE) {
594 ALOGE("EglManager::createReleaseFence: error waiting for previous fence: %#x",
595 eglGetError());
596 return UNKNOWN_ERROR;
597 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
598 ALOGE("EglManager::createReleaseFence: timeout waiting for previous fence");
599 return TIMED_OUT;
600 }
601 eglDestroySyncKHR(mEglDisplay, *eglFence);
602 }
603
604 // Create a fence for the outstanding accesses in the current
605 // OpenGL ES context.
606 *eglFence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, nullptr);
607 if (*eglFence == EGL_NO_SYNC_KHR) {
608 ALOGE("EglManager::createReleaseFence: error creating fence: %#x", eglGetError());
609 return UNKNOWN_ERROR;
610 }
611 glFlush();
612 }
613 return OK;
614}
615
John Reck3b202512014-06-23 13:13:08 -0700616} /* namespace renderthread */
617} /* namespace uirenderer */
618} /* namespace android */