blob: a11678189bad1301461aa65526d863ad532bbd57 [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
Stan Ilievaaa9e832019-09-17 14:07:23 -040019#include <EGL/eglext.h>
20#include <GLES/gl.h>
Mark Salyzyn96bf5982016-09-28 16:15:30 -070021#include <cutils/properties.h>
22#include <log/log.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040023#include <sync/sync.h>
John Reck1e510712018-04-23 08:15:03 -070024#include <utils/Trace.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040025
26#include <string>
27#include <vector>
Mark Salyzyn96bf5982016-09-28 16:15:30 -070028
Greg Danielcd558522016-11-17 13:31:40 -050029#include "Frame.h"
John Reckd04794a2015-05-08 10:04:36 -070030#include "Properties.h"
Stan Ilievaaa9e832019-09-17 14:07:23 -040031#include "utils/Color.h"
32#include "utils/StringUtils.h"
Derek Sollenberger7e044fe2016-11-07 10:57:59 -050033
John Reck3b202512014-06-23 13:13:08 -070034#define GLES_VERSION 2
35
36// Android-specific addition that is used to show when frames began in systrace
37EGLAPI void EGLAPIENTRY eglBeginFrame(EGLDisplay dpy, EGLSurface surface);
38
39namespace android {
40namespace uirenderer {
41namespace renderthread {
42
John Reck1bcacfd2017-11-03 10:12:19 -070043#define ERROR_CASE(x) \
44 case x: \
45 return #x;
John Reck3b202512014-06-23 13:13:08 -070046static const char* egl_error_str(EGLint error) {
47 switch (error) {
48 ERROR_CASE(EGL_SUCCESS)
49 ERROR_CASE(EGL_NOT_INITIALIZED)
50 ERROR_CASE(EGL_BAD_ACCESS)
51 ERROR_CASE(EGL_BAD_ALLOC)
52 ERROR_CASE(EGL_BAD_ATTRIBUTE)
53 ERROR_CASE(EGL_BAD_CONFIG)
54 ERROR_CASE(EGL_BAD_CONTEXT)
55 ERROR_CASE(EGL_BAD_CURRENT_SURFACE)
56 ERROR_CASE(EGL_BAD_DISPLAY)
57 ERROR_CASE(EGL_BAD_MATCH)
58 ERROR_CASE(EGL_BAD_NATIVE_PIXMAP)
59 ERROR_CASE(EGL_BAD_NATIVE_WINDOW)
60 ERROR_CASE(EGL_BAD_PARAMETER)
61 ERROR_CASE(EGL_BAD_SURFACE)
62 ERROR_CASE(EGL_CONTEXT_LOST)
John Reck1bcacfd2017-11-03 10:12:19 -070063 default:
64 return "Unknown error";
John Reck3b202512014-06-23 13:13:08 -070065 }
66}
sergeyv694d4992016-10-27 10:23:13 -070067const char* EglManager::eglErrorString() {
John Reck3b202512014-06-23 13:13:08 -070068 return egl_error_str(eglGetError());
69}
70
John Reck149173d2015-08-10 09:52:29 -070071static struct {
72 bool bufferAge = false;
73 bool setDamage = false;
Romain Guy26a2b972017-04-17 09:39:51 -070074 bool noConfigContext = false;
75 bool pixelFormatFloat = false;
76 bool glColorSpace = false;
Romain Guy26b6a642017-07-11 09:48:28 -070077 bool scRGB = false;
Peiyong Lin1f6aa122018-09-10 16:28:08 -070078 bool displayP3 = false;
John Reckb36bfdd2020-07-23 13:47:49 -070079 bool hdr = false;
Jorim Jaggi767e25e2018-04-04 23:07:35 +020080 bool contextPriority = false;
John Reck1e510712018-04-23 08:15:03 -070081 bool surfacelessContext = false;
Alec Mouri680414e2020-01-28 09:22:33 -080082 bool nativeFenceSync = false;
83 bool fenceSync = false;
84 bool waitSync = false;
John Reck149173d2015-08-10 09:52:29 -070085} EglExtensions;
86
John Reck1e510712018-04-23 08:15:03 -070087EglManager::EglManager()
88 : mEglDisplay(EGL_NO_DISPLAY)
Chris Craikd41c4d82015-01-05 15:51:13 -080089 , mEglConfig(nullptr)
John Reckb36bfdd2020-07-23 13:47:49 -070090 , mEglConfigF16(nullptr)
91 , mEglConfig1010102(nullptr)
John Reck3b202512014-06-23 13:13:08 -070092 , mEglContext(EGL_NO_CONTEXT)
93 , mPBufferSurface(EGL_NO_SURFACE)
Peiyong Lin3bff1352018-12-11 07:56:07 -080094 , mCurrentSurface(EGL_NO_SURFACE)
95 , mHasWideColorGamutSupport(false) {}
John Reck3b202512014-06-23 13:13:08 -070096
John Reck1e510712018-04-23 08:15:03 -070097EglManager::~EglManager() {
John Reck6104cea2019-01-10 14:37:17 -080098 if (hasEglContext()) {
99 ALOGW("~EglManager() leaked an EGL context");
100 }
John Reck1e510712018-04-23 08:15:03 -0700101}
102
John Reck3b202512014-06-23 13:13:08 -0700103void EglManager::initialize() {
104 if (hasEglContext()) return;
105
John Reckfbc8df02014-11-14 16:18:41 -0800106 ATRACE_NAME("Creating EGLContext");
107
John Reck3b202512014-06-23 13:13:08 -0700108 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
John Reck1bcacfd2017-11-03 10:12:19 -0700109 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, "Failed to get EGL_DEFAULT_DISPLAY! err=%s",
110 eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700111
112 EGLint major, minor;
113 LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE,
John Reck1bcacfd2017-11-03 10:12:19 -0700114 "Failed to initialize display %p! err=%s", mEglDisplay, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700115
John Reck848f6512018-12-03 13:26:43 -0800116 ALOGV("Initialized EGL, version %d.%d", (int)major, (int)minor);
John Reck3b202512014-06-23 13:13:08 -0700117
John Reck149173d2015-08-10 09:52:29 -0700118 initExtensions();
Season Li13d1b4a2015-07-29 17:16:19 -0700119
John Reck149173d2015-08-10 09:52:29 -0700120 // Now that extensions are loaded, pick a swap behavior
121 if (Properties::enablePartialUpdates) {
Matt Sarettb77c94a2016-12-07 12:22:37 -0500122 // An Adreno driver bug is causing rendering problems for SkiaGL with
123 // buffer age swap behavior (b/31957043). To temporarily workaround,
124 // we will use preserved swap behavior.
Derek Sollenbergerb5a12bd2017-06-14 15:23:19 -0400125 if (Properties::useBufferAge && EglExtensions.bufferAge) {
John Reck149173d2015-08-10 09:52:29 -0700126 mSwapBehavior = SwapBehavior::BufferAge;
127 } else {
128 mSwapBehavior = SwapBehavior::Preserved;
129 }
130 }
131
Romain Guy26a2b972017-04-17 09:39:51 -0700132 loadConfigs();
John Reck3b202512014-06-23 13:13:08 -0700133 createContext();
John Reckd7db4d72015-05-20 07:18:55 -0700134 createPBufferSurface();
John Reck1e510712018-04-23 08:15:03 -0700135 makeCurrent(mPBufferSurface, nullptr, /* force */ true);
Peiyong Lin3bff1352018-12-11 07:56:07 -0800136
Brian Osmane0cf5972019-01-23 10:41:20 -0500137 skcms_Matrix3x3 wideColorGamut;
138 LOG_ALWAYS_FATAL_IF(!DeviceInfo::get()->getWideColorSpace()->toXYZD50(&wideColorGamut),
139 "Could not get gamut matrix from wideColorSpace");
Peiyong Lin3bff1352018-12-11 07:56:07 -0800140 bool hasWideColorSpaceExtension = false;
Mike Reed7ac1af32020-05-26 10:50:21 -0400141 if (memcmp(&wideColorGamut, &SkNamedGamut::kDisplayP3, sizeof(wideColorGamut)) == 0) {
Peiyong Lin3bff1352018-12-11 07:56:07 -0800142 hasWideColorSpaceExtension = EglExtensions.displayP3;
Brian Osmane0cf5972019-01-23 10:41:20 -0500143 } else if (memcmp(&wideColorGamut, &SkNamedGamut::kSRGB, sizeof(wideColorGamut)) == 0) {
Peiyong Lin3bff1352018-12-11 07:56:07 -0800144 hasWideColorSpaceExtension = EglExtensions.scRGB;
145 } else {
146 LOG_ALWAYS_FATAL("Unsupported wide color space.");
147 }
John Reckb36bfdd2020-07-23 13:47:49 -0700148 mHasWideColorGamutSupport = EglExtensions.glColorSpace && hasWideColorSpaceExtension;
Peiyong Lin3bff1352018-12-11 07:56:07 -0800149}
150
151EGLConfig EglManager::load8BitsConfig(EGLDisplay display, EglManager::SwapBehavior swapBehavior) {
152 EGLint eglSwapBehavior =
153 (swapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
154 EGLint attribs[] = {EGL_RENDERABLE_TYPE,
155 EGL_OPENGL_ES2_BIT,
156 EGL_RED_SIZE,
157 8,
158 EGL_GREEN_SIZE,
159 8,
160 EGL_BLUE_SIZE,
161 8,
162 EGL_ALPHA_SIZE,
163 8,
164 EGL_DEPTH_SIZE,
165 0,
166 EGL_CONFIG_CAVEAT,
167 EGL_NONE,
168 EGL_STENCIL_SIZE,
169 STENCIL_BUFFER_SIZE,
170 EGL_SURFACE_TYPE,
171 EGL_WINDOW_BIT | eglSwapBehavior,
172 EGL_NONE};
173 EGLConfig config = EGL_NO_CONFIG_KHR;
174 EGLint numConfigs = 1;
John Reck0fa0cbc2019-04-05 16:57:46 -0700175 if (!eglChooseConfig(display, attribs, &config, numConfigs, &numConfigs) || numConfigs != 1) {
Peiyong Lin3bff1352018-12-11 07:56:07 -0800176 return EGL_NO_CONFIG_KHR;
177 }
178 return config;
179}
180
John Reckb36bfdd2020-07-23 13:47:49 -0700181EGLConfig EglManager::load1010102Config(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_RED_SIZE,
188 10,
189 EGL_GREEN_SIZE,
190 10,
191 EGL_BLUE_SIZE,
192 10,
193 EGL_ALPHA_SIZE,
194 2,
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;
204 if (!eglChooseConfig(display, attribs, &config, numConfigs, &numConfigs) || numConfigs != 1) {
205 return EGL_NO_CONFIG_KHR;
206 }
207 return config;
208}
209
Peiyong Lin3bff1352018-12-11 07:56:07 -0800210EGLConfig EglManager::loadFP16Config(EGLDisplay display, SwapBehavior swapBehavior) {
211 EGLint eglSwapBehavior =
212 (swapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
213 // If we reached this point, we have a valid swap behavior
214 EGLint attribs[] = {EGL_RENDERABLE_TYPE,
215 EGL_OPENGL_ES2_BIT,
216 EGL_COLOR_COMPONENT_TYPE_EXT,
217 EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT,
218 EGL_RED_SIZE,
219 16,
220 EGL_GREEN_SIZE,
221 16,
222 EGL_BLUE_SIZE,
223 16,
224 EGL_ALPHA_SIZE,
225 16,
226 EGL_DEPTH_SIZE,
227 0,
228 EGL_STENCIL_SIZE,
229 STENCIL_BUFFER_SIZE,
230 EGL_SURFACE_TYPE,
231 EGL_WINDOW_BIT | eglSwapBehavior,
232 EGL_NONE};
233 EGLConfig config = EGL_NO_CONFIG_KHR;
234 EGLint numConfigs = 1;
John Reck0fa0cbc2019-04-05 16:57:46 -0700235 if (!eglChooseConfig(display, attribs, &config, numConfigs, &numConfigs) || numConfigs != 1) {
Peiyong Lin3bff1352018-12-11 07:56:07 -0800236 return EGL_NO_CONFIG_KHR;
237 }
238 return config;
John Reck3b202512014-06-23 13:13:08 -0700239}
240
John Reck149173d2015-08-10 09:52:29 -0700241void EglManager::initExtensions() {
John Reck1bcacfd2017-11-03 10:12:19 -0700242 auto extensions = StringUtils::split(eglQueryString(mEglDisplay, EGL_EXTENSIONS));
Romain Guy26a2b972017-04-17 09:39:51 -0700243
John Reck8733bff2016-09-27 14:45:28 -0700244 // For our purposes we don't care if EGL_BUFFER_AGE is a result of
245 // EGL_EXT_buffer_age or EGL_KHR_partial_update as our usage is covered
246 // under EGL_KHR_partial_update and we don't need the expanded scope
247 // that EGL_EXT_buffer_age provides.
John Reck1bcacfd2017-11-03 10:12:19 -0700248 EglExtensions.bufferAge =
249 extensions.has("EGL_EXT_buffer_age") || extensions.has("EGL_KHR_partial_update");
Chris Craik6e6646c2015-09-14 15:54:12 -0700250 EglExtensions.setDamage = extensions.has("EGL_KHR_partial_update");
John Reck708b6682015-10-22 13:11:00 -0700251 LOG_ALWAYS_FATAL_IF(!extensions.has("EGL_KHR_swap_buffers_with_damage"),
John Reck1bcacfd2017-11-03 10:12:19 -0700252 "Missing required extension EGL_KHR_swap_buffers_with_damage");
Romain Guy26a2b972017-04-17 09:39:51 -0700253
254 EglExtensions.glColorSpace = extensions.has("EGL_KHR_gl_colorspace");
255 EglExtensions.noConfigContext = extensions.has("EGL_KHR_no_config_context");
256 EglExtensions.pixelFormatFloat = extensions.has("EGL_EXT_pixel_format_float");
Romain Guy26b6a642017-07-11 09:48:28 -0700257 EglExtensions.scRGB = extensions.has("EGL_EXT_gl_colorspace_scrgb");
Peiyong Lin3bff1352018-12-11 07:56:07 -0800258 EglExtensions.displayP3 = extensions.has("EGL_EXT_gl_colorspace_display_p3_passthrough");
John Reckb36bfdd2020-07-23 13:47:49 -0700259 EglExtensions.hdr = extensions.has("EGL_EXT_gl_colorspace_bt2020_pq");
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200260 EglExtensions.contextPriority = extensions.has("EGL_IMG_context_priority");
John Reck1e510712018-04-23 08:15:03 -0700261 EglExtensions.surfacelessContext = extensions.has("EGL_KHR_surfaceless_context");
Alec Mouri680414e2020-01-28 09:22:33 -0800262 EglExtensions.fenceSync = extensions.has("EGL_KHR_fence_sync");
263 EglExtensions.waitSync = extensions.has("EGL_KHR_wait_sync");
Yiwei Zhang7f430132020-08-12 15:07:51 -0700264 EglExtensions.nativeFenceSync = extensions.has("EGL_ANDROID_native_fence_sync");
John Reck149173d2015-08-10 09:52:29 -0700265}
266
John Reck3b202512014-06-23 13:13:08 -0700267bool EglManager::hasEglContext() {
268 return mEglDisplay != EGL_NO_DISPLAY;
269}
270
Romain Guy26a2b972017-04-17 09:39:51 -0700271void EglManager::loadConfigs() {
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700272 // Note: The default pixel format is RGBA_8888, when other formats are
273 // available, we should check the target pixel format and configure the
274 // attributes list properly.
Peiyong Lin3bff1352018-12-11 07:56:07 -0800275 mEglConfig = load8BitsConfig(mEglDisplay, mSwapBehavior);
276 if (mEglConfig == EGL_NO_CONFIG_KHR) {
John Reck149173d2015-08-10 09:52:29 -0700277 if (mSwapBehavior == SwapBehavior::Preserved) {
John Reck3b202512014-06-23 13:13:08 -0700278 // Try again without dirty regions enabled
John Reck149173d2015-08-10 09:52:29 -0700279 ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...");
280 mSwapBehavior = SwapBehavior::Discard;
Peiyong Lin3bff1352018-12-11 07:56:07 -0800281 mEglConfig = load8BitsConfig(mEglDisplay, mSwapBehavior);
John Reck3b202512014-06-23 13:13:08 -0700282 } else {
John Reck149173d2015-08-10 09:52:29 -0700283 // Failed to get a valid config
sergeyv694d4992016-10-27 10:23:13 -0700284 LOG_ALWAYS_FATAL("Failed to choose config, error = %s", eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700285 }
286 }
Romain Guy26a2b972017-04-17 09:39:51 -0700287
Peiyong Lin3bff1352018-12-11 07:56:07 -0800288 // When we reach this point, we have a valid swap behavior
John Reckb36bfdd2020-07-23 13:47:49 -0700289 if (EglExtensions.pixelFormatFloat) {
290 mEglConfigF16 = loadFP16Config(mEglDisplay, mSwapBehavior);
291 if (mEglConfigF16 == EGL_NO_CONFIG_KHR) {
Rob Herring74883ab2017-11-29 09:26:31 -0600292 ALOGE("Device claims wide gamut support, cannot find matching config, error = %s",
John Reck0fa0cbc2019-04-05 16:57:46 -0700293 eglErrorString());
Rob Herring74883ab2017-11-29 09:26:31 -0600294 EglExtensions.pixelFormatFloat = false;
Romain Guy26a2b972017-04-17 09:39:51 -0700295 }
John Reckb36bfdd2020-07-23 13:47:49 -0700296 }
297 mEglConfig1010102 = load1010102Config(mEglDisplay, mSwapBehavior);
298 if (mEglConfig1010102 == EGL_NO_CONFIG_KHR) {
299 ALOGW("Failed to initialize 101010-2 format, error = %s",
300 eglErrorString());
Romain Guy26a2b972017-04-17 09:39:51 -0700301 }
John Reck3b202512014-06-23 13:13:08 -0700302}
303
304void EglManager::createContext() {
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200305 std::vector<EGLint> contextAttributes;
306 contextAttributes.reserve(5);
307 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
308 contextAttributes.push_back(GLES_VERSION);
309 if (Properties::contextPriority != 0 && EglExtensions.contextPriority) {
310 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
311 contextAttributes.push_back(Properties::contextPriority);
312 }
313 contextAttributes.push_back(EGL_NONE);
John Reck1bcacfd2017-11-03 10:12:19 -0700314 mEglContext = eglCreateContext(
315 mEglDisplay, EglExtensions.noConfigContext ? ((EGLConfig) nullptr) : mEglConfig,
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200316 EGL_NO_CONTEXT, contextAttributes.data());
John Reck1bcacfd2017-11-03 10:12:19 -0700317 LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, "Failed to create context, error = %s",
318 eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700319}
320
John Reckd7db4d72015-05-20 07:18:55 -0700321void EglManager::createPBufferSurface() {
John Reck3b202512014-06-23 13:13:08 -0700322 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
John Reck1bcacfd2017-11-03 10:12:19 -0700323 "usePBufferSurface() called on uninitialized GlobalContext!");
John Reck3b202512014-06-23 13:13:08 -0700324
John Reck1e510712018-04-23 08:15:03 -0700325 if (mPBufferSurface == EGL_NO_SURFACE && !EglExtensions.surfacelessContext) {
John Reck1bcacfd2017-11-03 10:12:19 -0700326 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
John Reck3b202512014-06-23 13:13:08 -0700327 mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs);
Roman Kiryanov60736132019-08-02 14:37:53 -0700328 LOG_ALWAYS_FATAL_IF(mPBufferSurface == EGL_NO_SURFACE,
329 "Failed to create a pixel buffer display=%p, "
330 "mEglConfig=%p, error=%s",
331 mEglDisplay, mEglConfig, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700332 }
John Reck3b202512014-06-23 13:13:08 -0700333}
334
Peiyong Lin3bff1352018-12-11 07:56:07 -0800335Result<EGLSurface, EGLint> EglManager::createSurface(EGLNativeWindowType window,
336 ColorMode colorMode,
Brian Osmane0cf5972019-01-23 10:41:20 -0500337 sk_sp<SkColorSpace> colorSpace) {
John Reck1e510712018-04-23 08:15:03 -0700338 LOG_ALWAYS_FATAL_IF(!hasEglContext(), "Not initialized");
Romain Guy253f2c22016-09-28 17:34:42 -0700339
John Reckb36bfdd2020-07-23 13:47:49 -0700340 if (!mHasWideColorGamutSupport || !EglExtensions.noConfigContext) {
341 colorMode = ColorMode::Default;
342 }
Romain Guy26a2b972017-04-17 09:39:51 -0700343
344 // The color space we want to use depends on whether linear blending is turned
345 // on and whether the app has requested wide color gamut rendering. When wide
346 // color gamut rendering is off, the app simply renders in the display's native
347 // color gamut.
348 //
349 // When wide gamut rendering is off:
350 // - Blending is done by default in gamma space, which requires using a
351 // linear EGL color space (the GPU uses the color values as is)
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700352 // - If linear blending is on, we must use the non-linear EGL color space
353 // (the GPU will perform sRGB to linear and linear to SRGB conversions
354 // before and after blending)
Romain Guy26a2b972017-04-17 09:39:51 -0700355 //
356 // When wide gamut rendering is on we cannot rely on the GPU performing
357 // linear blending for us. We use two different color spaces to tag the
358 // surface appropriately for SurfaceFlinger:
Peiyong Lin3bff1352018-12-11 07:56:07 -0800359 // - Gamma blending (default) requires the use of the non-linear color space
360 // - Linear blending requires the use of the linear color space
Romain Guy26a2b972017-04-17 09:39:51 -0700361
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700362 // Not all Android targets support the EGL_GL_COLORSPACE_KHR extension
Romain Guy26a2b972017-04-17 09:39:51 -0700363 // We insert to placeholders to set EGL_GL_COLORSPACE_KHR and its value.
364 // According to section 3.4.1 of the EGL specification, the attributes
365 // list is considered empty if the first entry is EGL_NONE
John Reck1bcacfd2017-11-03 10:12:19 -0700366 EGLint attribs[] = {EGL_NONE, EGL_NONE, EGL_NONE};
Romain Guy253f2c22016-09-28 17:34:42 -0700367
John Reckb36bfdd2020-07-23 13:47:49 -0700368 EGLConfig config = mEglConfig;
369 if (DeviceInfo::get()->getWideColorType() == kRGBA_F16_SkColorType) {
370 if (mEglConfigF16 == EGL_NO_CONFIG_KHR) {
371 colorMode = ColorMode::Default;
372 } else {
373 config = mEglConfigF16;
374 }
375 }
Romain Guy26a2b972017-04-17 09:39:51 -0700376 if (EglExtensions.glColorSpace) {
377 attribs[0] = EGL_GL_COLORSPACE_KHR;
John Reckb36bfdd2020-07-23 13:47:49 -0700378 switch (colorMode) {
379 case ColorMode::Default:
380 attribs[1] = EGL_GL_COLORSPACE_LINEAR_KHR;
381 break;
382 case ColorMode::WideColorGamut: {
383 skcms_Matrix3x3 colorGamut;
384 LOG_ALWAYS_FATAL_IF(!colorSpace->toXYZD50(&colorGamut),
385 "Could not get gamut matrix from color space");
386 if (memcmp(&colorGamut, &SkNamedGamut::kDisplayP3, sizeof(colorGamut)) == 0) {
387 attribs[1] = EGL_GL_COLORSPACE_DISPLAY_P3_PASSTHROUGH_EXT;
388 } else if (memcmp(&colorGamut, &SkNamedGamut::kSRGB, sizeof(colorGamut)) == 0) {
389 attribs[1] = EGL_GL_COLORSPACE_SCRGB_EXT;
390 } else if (memcmp(&colorGamut, &SkNamedGamut::kRec2020, sizeof(colorGamut)) == 0) {
391 attribs[1] = EGL_GL_COLORSPACE_BT2020_PQ_EXT;
392 } else {
393 LOG_ALWAYS_FATAL("Unreachable: unsupported wide color space.");
394 }
395 break;
Peiyong Lin3bff1352018-12-11 07:56:07 -0800396 }
John Reckb36bfdd2020-07-23 13:47:49 -0700397 case ColorMode::Hdr:
398 config = mEglConfigF16;
399 attribs[1] = EGL_GL_COLORSPACE_BT2020_PQ_EXT;
400 break;
401 case ColorMode::Hdr10:
402 config = mEglConfig1010102;
403 attribs[1] = EGL_GL_COLORSPACE_BT2020_PQ_EXT;
404 break;
Romain Guy26a2b972017-04-17 09:39:51 -0700405 }
Romain Guy26a2b972017-04-17 09:39:51 -0700406 }
407
John Reckb36bfdd2020-07-23 13:47:49 -0700408 EGLSurface surface = eglCreateWindowSurface(mEglDisplay, config, window, attribs);
John Reck8e539ca2018-11-15 15:21:29 -0800409 if (surface == EGL_NO_SURFACE) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700410 return Error<EGLint>{eglGetError()};
John Reck8e539ca2018-11-15 15:21:29 -0800411 }
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000412
413 if (mSwapBehavior != SwapBehavior::Preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700414 LOG_ALWAYS_FATAL_IF(eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
415 EGL_BUFFER_DESTROYED) == EGL_FALSE,
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000416 "Failed to set swap behavior to destroyed for window %p, eglErr = %s",
John Reck1bcacfd2017-11-03 10:12:19 -0700417 (void*)window, eglErrorString());
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000418 }
419
John Reck3b202512014-06-23 13:13:08 -0700420 return surface;
421}
422
423void EglManager::destroySurface(EGLSurface surface) {
424 if (isCurrent(surface)) {
425 makeCurrent(EGL_NO_SURFACE);
426 }
427 if (!eglDestroySurface(mEglDisplay, surface)) {
sergeyv694d4992016-10-27 10:23:13 -0700428 ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700429 }
430}
431
432void EglManager::destroy() {
433 if (mEglDisplay == EGL_NO_DISPLAY) return;
434
John Reck3b202512014-06-23 13:13:08 -0700435 eglDestroyContext(mEglDisplay, mEglContext);
John Reck1e510712018-04-23 08:15:03 -0700436 if (mPBufferSurface != EGL_NO_SURFACE) {
437 eglDestroySurface(mEglDisplay, mPBufferSurface);
438 }
John Reck3b202512014-06-23 13:13:08 -0700439 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
440 eglTerminate(mEglDisplay);
441 eglReleaseThread();
442
443 mEglDisplay = EGL_NO_DISPLAY;
444 mEglContext = EGL_NO_CONTEXT;
445 mPBufferSurface = EGL_NO_SURFACE;
446 mCurrentSurface = EGL_NO_SURFACE;
447}
448
John Reck1e510712018-04-23 08:15:03 -0700449bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut, bool force) {
450 if (!force && isCurrent(surface)) return false;
John Reck3b202512014-06-23 13:13:08 -0700451
452 if (surface == EGL_NO_SURFACE) {
John Reckd7db4d72015-05-20 07:18:55 -0700453 // Ensure we always have a valid surface & context
454 surface = mPBufferSurface;
455 }
456 if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) {
John Reckf2dcc2a2015-07-16 09:17:59 -0700457 if (errOut) {
458 *errOut = eglGetError();
John Reck1bcacfd2017-11-03 10:12:19 -0700459 ALOGW("Failed to make current on surface %p, error=%s", (void*)surface,
460 egl_error_str(*errOut));
John Reckf2dcc2a2015-07-16 09:17:59 -0700461 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700462 LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", (void*)surface,
463 eglErrorString());
John Reckf2dcc2a2015-07-16 09:17:59 -0700464 }
John Reck3b202512014-06-23 13:13:08 -0700465 }
466 mCurrentSurface = surface;
John Recka8963062017-06-14 10:47:50 -0700467 if (Properties::disableVsync) {
468 eglSwapInterval(mEglDisplay, 0);
469 }
John Reck3b202512014-06-23 13:13:08 -0700470 return true;
471}
472
John Reck149173d2015-08-10 09:52:29 -0700473EGLint EglManager::queryBufferAge(EGLSurface surface) {
474 switch (mSwapBehavior) {
John Reck1bcacfd2017-11-03 10:12:19 -0700475 case SwapBehavior::Discard:
476 return 0;
477 case SwapBehavior::Preserved:
478 return 1;
479 case SwapBehavior::BufferAge:
480 EGLint bufferAge;
481 eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge);
482 return bufferAge;
John Reck149173d2015-08-10 09:52:29 -0700483 }
484 return 0;
485}
486
487Frame EglManager::beginFrame(EGLSurface surface) {
John Reck1bcacfd2017-11-03 10:12:19 -0700488 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, "Tried to beginFrame on EGL_NO_SURFACE!");
John Reck3b202512014-06-23 13:13:08 -0700489 makeCurrent(surface);
John Reck149173d2015-08-10 09:52:29 -0700490 Frame frame;
491 frame.mSurface = surface;
492 eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth);
493 eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight);
494 frame.mBufferAge = queryBufferAge(surface);
John Reck3b202512014-06-23 13:13:08 -0700495 eglBeginFrame(mEglDisplay, surface);
John Reck149173d2015-08-10 09:52:29 -0700496 return frame;
John Reck3b202512014-06-23 13:13:08 -0700497}
498
John Reck149173d2015-08-10 09:52:29 -0700499void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) {
500#ifdef EGL_KHR_partial_update
501 if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) {
502 EGLint rects[4];
503 frame.map(dirty, rects);
504 if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) {
505 LOG_ALWAYS_FATAL("Failed to set damage region on surface %p, error=%s",
John Reck1bcacfd2017-11-03 10:12:19 -0700506 (void*)frame.mSurface, eglErrorString());
John Reck149173d2015-08-10 09:52:29 -0700507 }
508 }
509#endif
510}
511
John Reckc96955d2016-02-26 14:56:44 -0800512bool EglManager::damageRequiresSwap() {
513 return EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge;
514}
515
John Reck149173d2015-08-10 09:52:29 -0700516bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) {
John Reck682573c2015-10-30 10:37:35 -0700517 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
John Reck55156372015-01-21 07:46:37 -0800518 ATRACE_NAME("Finishing GPU work");
519 fence();
520 }
John Reck55156372015-01-21 07:46:37 -0800521
John Recka672f6b2015-10-22 09:53:26 -0700522 EGLint rects[4];
523 frame.map(screenDirty, rects);
John Reck1bcacfd2017-11-03 10:12:19 -0700524 eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects, screenDirty.isEmpty() ? 0 : 1);
John Reckd04794a2015-05-08 10:04:36 -0700525
John Reck3b202512014-06-23 13:13:08 -0700526 EGLint err = eglGetError();
John Reck2cdbc7d2014-09-17 16:06:36 -0700527 if (CC_LIKELY(err == EGL_SUCCESS)) {
528 return true;
529 }
John Reckc2547fa2015-10-26 13:52:52 -0700530 if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) {
John Reck2cdbc7d2014-09-17 16:06:36 -0700531 // For some reason our surface was destroyed out from under us
532 // This really shouldn't happen, but if it does we can recover easily
533 // by just not trying to use the surface anymore
John Reck1bcacfd2017-11-03 10:12:19 -0700534 ALOGW("swapBuffers encountered EGL error %d on %p, halting rendering...", err,
535 frame.mSurface);
John Reck2cdbc7d2014-09-17 16:06:36 -0700536 return false;
537 }
John Reck1bcacfd2017-11-03 10:12:19 -0700538 LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering", err, egl_error_str(err));
John Reck2cdbc7d2014-09-17 16:06:36 -0700539 // Impossible to hit this, but the compiler doesn't know that
540 return false;
John Reck3b202512014-06-23 13:13:08 -0700541}
542
John Reck55156372015-01-21 07:46:37 -0800543void EglManager::fence() {
544 EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL);
John Reck1bcacfd2017-11-03 10:12:19 -0700545 eglClientWaitSyncKHR(mEglDisplay, fence, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR);
John Reck55156372015-01-21 07:46:37 -0800546 eglDestroySyncKHR(mEglDisplay, fence);
547}
548
John Reck1125d1f2014-10-23 11:02:19 -0700549bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) {
John Reck149173d2015-08-10 09:52:29 -0700550 if (mSwapBehavior != SwapBehavior::Preserved) return false;
John Reck3b202512014-06-23 13:13:08 -0700551
John Reck149173d2015-08-10 09:52:29 -0700552 bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
John Reck1bcacfd2017-11-03 10:12:19 -0700553 preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED);
John Reck149173d2015-08-10 09:52:29 -0700554 if (!preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700555 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", (void*)surface,
556 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700557 // Maybe it's already set?
558 EGLint swapBehavior;
559 if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) {
560 preserved = (swapBehavior == EGL_BUFFER_PRESERVED);
561 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700562 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", (void*)surface,
563 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700564 }
John Reck3b202512014-06-23 13:13:08 -0700565 }
John Reck1125d1f2014-10-23 11:02:19 -0700566
567 return preserved;
John Reck3b202512014-06-23 13:13:08 -0700568}
569
Stan Ilievaaa9e832019-09-17 14:07:23 -0400570static status_t waitForeverOnFence(int fence, const char* logname) {
571 ATRACE_CALL();
572 if (fence == -1) {
573 return NO_ERROR;
574 }
575 constexpr int warningTimeout = 3000;
576 int err = sync_wait(fence, warningTimeout);
577 if (err < 0 && errno == ETIME) {
578 ALOGE("%s: fence %d didn't signal in %d ms", logname, fence, warningTimeout);
579 err = sync_wait(fence, -1);
580 }
581 return err < 0 ? -errno : status_t(NO_ERROR);
582}
583
584status_t EglManager::fenceWait(int fence) {
Stan Iliev564ca3e2018-09-04 22:00:00 +0000585 if (!hasEglContext()) {
586 ALOGE("EglManager::fenceWait: EGLDisplay not initialized");
587 return INVALID_OPERATION;
588 }
589
Alec Mouri680414e2020-01-28 09:22:33 -0800590 if (EglExtensions.waitSync && EglExtensions.nativeFenceSync) {
Stan Iliev564ca3e2018-09-04 22:00:00 +0000591 // Block GPU on the fence.
592 // Create an EGLSyncKHR from the current fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400593 int fenceFd = ::dup(fence);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000594 if (fenceFd == -1) {
595 ALOGE("EglManager::fenceWait: error dup'ing fence fd: %d", errno);
596 return -errno;
597 }
John Reck0fa0cbc2019-04-05 16:57:46 -0700598 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE};
599 EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000600 if (sync == EGL_NO_SYNC_KHR) {
601 close(fenceFd);
602 ALOGE("EglManager::fenceWait: error creating EGL fence: %#x", eglGetError());
603 return UNKNOWN_ERROR;
604 }
605
606 // XXX: The spec draft is inconsistent as to whether this should
607 // return an EGLint or void. Ignore the return value for now, as
608 // it's not strictly needed.
609 eglWaitSyncKHR(mEglDisplay, sync, 0);
610 EGLint eglErr = eglGetError();
611 eglDestroySyncKHR(mEglDisplay, sync);
612 if (eglErr != EGL_SUCCESS) {
613 ALOGE("EglManager::fenceWait: error waiting for EGL fence: %#x", eglErr);
614 return UNKNOWN_ERROR;
615 }
616 } else {
617 // Block CPU on the fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400618 status_t err = waitForeverOnFence(fence, "EglManager::fenceWait");
Stan Iliev564ca3e2018-09-04 22:00:00 +0000619 if (err != NO_ERROR) {
620 ALOGE("EglManager::fenceWait: error waiting for fence: %d", err);
621 return err;
622 }
623 }
624 return OK;
625}
626
Stan Ilievaaa9e832019-09-17 14:07:23 -0400627status_t EglManager::createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence, int* nativeFence) {
628 *nativeFence = -1;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000629 if (!hasEglContext()) {
630 ALOGE("EglManager::createReleaseFence: EGLDisplay not initialized");
631 return INVALID_OPERATION;
632 }
633
Alec Mouri680414e2020-01-28 09:22:33 -0800634 if (EglExtensions.nativeFenceSync) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700635 EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000636 if (sync == EGL_NO_SYNC_KHR) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700637 ALOGE("EglManager::createReleaseFence: error creating EGL fence: %#x", eglGetError());
Stan Iliev564ca3e2018-09-04 22:00:00 +0000638 return UNKNOWN_ERROR;
639 }
640 glFlush();
641 int fenceFd = eglDupNativeFenceFDANDROID(mEglDisplay, sync);
642 eglDestroySyncKHR(mEglDisplay, sync);
643 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
644 ALOGE("EglManager::createReleaseFence: error dup'ing native fence "
John Reck0fa0cbc2019-04-05 16:57:46 -0700645 "fd: %#x",
646 eglGetError());
Stan Iliev564ca3e2018-09-04 22:00:00 +0000647 return UNKNOWN_ERROR;
648 }
Stan Ilievaaa9e832019-09-17 14:07:23 -0400649 *nativeFence = fenceFd;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000650 *eglFence = EGL_NO_SYNC_KHR;
Alec Mouri680414e2020-01-28 09:22:33 -0800651 } else if (useFenceSync && EglExtensions.fenceSync) {
Stan Iliev564ca3e2018-09-04 22:00:00 +0000652 if (*eglFence != EGL_NO_SYNC_KHR) {
653 // There is already a fence for the current slot. We need to
654 // wait on that before replacing it with another fence to
655 // ensure that all outstanding buffer accesses have completed
656 // before the producer accesses it.
657 EGLint result = eglClientWaitSyncKHR(mEglDisplay, *eglFence, 0, 1000000000);
658 if (result == EGL_FALSE) {
659 ALOGE("EglManager::createReleaseFence: error waiting for previous fence: %#x",
John Reck0fa0cbc2019-04-05 16:57:46 -0700660 eglGetError());
Stan Iliev564ca3e2018-09-04 22:00:00 +0000661 return UNKNOWN_ERROR;
662 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
663 ALOGE("EglManager::createReleaseFence: timeout waiting for previous fence");
664 return TIMED_OUT;
665 }
666 eglDestroySyncKHR(mEglDisplay, *eglFence);
667 }
668
669 // Create a fence for the outstanding accesses in the current
670 // OpenGL ES context.
671 *eglFence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, nullptr);
672 if (*eglFence == EGL_NO_SYNC_KHR) {
673 ALOGE("EglManager::createReleaseFence: error creating fence: %#x", eglGetError());
674 return UNKNOWN_ERROR;
675 }
676 glFlush();
677 }
678 return OK;
679}
680
John Reck3b202512014-06-23 13:13:08 -0700681} /* namespace renderthread */
682} /* namespace uirenderer */
683} /* namespace android */