blob: 2a8aa8c3e5b72fd06423da0aa826af8715438076 [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
Alec Mouri49d87e52020-06-29 16:54:21 -0700241extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
242
John Reck149173d2015-08-10 09:52:29 -0700243void EglManager::initExtensions() {
John Reck1bcacfd2017-11-03 10:12:19 -0700244 auto extensions = StringUtils::split(eglQueryString(mEglDisplay, EGL_EXTENSIONS));
Alec Mouri49d87e52020-06-29 16:54:21 -0700245 auto extensionsAndroid =
246 StringUtils::split(eglQueryStringImplementationANDROID(mEglDisplay, EGL_EXTENSIONS));
Romain Guy26a2b972017-04-17 09:39:51 -0700247
John Reck8733bff2016-09-27 14:45:28 -0700248 // For our purposes we don't care if EGL_BUFFER_AGE is a result of
249 // EGL_EXT_buffer_age or EGL_KHR_partial_update as our usage is covered
250 // under EGL_KHR_partial_update and we don't need the expanded scope
251 // that EGL_EXT_buffer_age provides.
John Reck1bcacfd2017-11-03 10:12:19 -0700252 EglExtensions.bufferAge =
253 extensions.has("EGL_EXT_buffer_age") || extensions.has("EGL_KHR_partial_update");
Chris Craik6e6646c2015-09-14 15:54:12 -0700254 EglExtensions.setDamage = extensions.has("EGL_KHR_partial_update");
John Reck708b6682015-10-22 13:11:00 -0700255 LOG_ALWAYS_FATAL_IF(!extensions.has("EGL_KHR_swap_buffers_with_damage"),
John Reck1bcacfd2017-11-03 10:12:19 -0700256 "Missing required extension EGL_KHR_swap_buffers_with_damage");
Romain Guy26a2b972017-04-17 09:39:51 -0700257
258 EglExtensions.glColorSpace = extensions.has("EGL_KHR_gl_colorspace");
259 EglExtensions.noConfigContext = extensions.has("EGL_KHR_no_config_context");
260 EglExtensions.pixelFormatFloat = extensions.has("EGL_EXT_pixel_format_float");
Romain Guy26b6a642017-07-11 09:48:28 -0700261 EglExtensions.scRGB = extensions.has("EGL_EXT_gl_colorspace_scrgb");
Peiyong Lin3bff1352018-12-11 07:56:07 -0800262 EglExtensions.displayP3 = extensions.has("EGL_EXT_gl_colorspace_display_p3_passthrough");
John Reckb36bfdd2020-07-23 13:47:49 -0700263 EglExtensions.hdr = extensions.has("EGL_EXT_gl_colorspace_bt2020_pq");
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200264 EglExtensions.contextPriority = extensions.has("EGL_IMG_context_priority");
John Reck1e510712018-04-23 08:15:03 -0700265 EglExtensions.surfacelessContext = extensions.has("EGL_KHR_surfaceless_context");
Alec Mouri680414e2020-01-28 09:22:33 -0800266 EglExtensions.fenceSync = extensions.has("EGL_KHR_fence_sync");
267 EglExtensions.waitSync = extensions.has("EGL_KHR_wait_sync");
Alec Mouri49d87e52020-06-29 16:54:21 -0700268
269 // EGL_ANDROID_native_fence_sync is not exposed to applications, so access
270 // this through the private Android-specific query instead.
271 EglExtensions.nativeFenceSync = extensionsAndroid.has("EGL_ANDROID_native_fence_sync");
John Reck149173d2015-08-10 09:52:29 -0700272}
273
John Reck3b202512014-06-23 13:13:08 -0700274bool EglManager::hasEglContext() {
275 return mEglDisplay != EGL_NO_DISPLAY;
276}
277
Romain Guy26a2b972017-04-17 09:39:51 -0700278void EglManager::loadConfigs() {
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700279 // Note: The default pixel format is RGBA_8888, when other formats are
280 // available, we should check the target pixel format and configure the
281 // attributes list properly.
Peiyong Lin3bff1352018-12-11 07:56:07 -0800282 mEglConfig = load8BitsConfig(mEglDisplay, mSwapBehavior);
283 if (mEglConfig == EGL_NO_CONFIG_KHR) {
John Reck149173d2015-08-10 09:52:29 -0700284 if (mSwapBehavior == SwapBehavior::Preserved) {
John Reck3b202512014-06-23 13:13:08 -0700285 // Try again without dirty regions enabled
John Reck149173d2015-08-10 09:52:29 -0700286 ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...");
287 mSwapBehavior = SwapBehavior::Discard;
Peiyong Lin3bff1352018-12-11 07:56:07 -0800288 mEglConfig = load8BitsConfig(mEglDisplay, mSwapBehavior);
John Reck3b202512014-06-23 13:13:08 -0700289 } else {
John Reck149173d2015-08-10 09:52:29 -0700290 // Failed to get a valid config
sergeyv694d4992016-10-27 10:23:13 -0700291 LOG_ALWAYS_FATAL("Failed to choose config, error = %s", eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700292 }
293 }
Romain Guy26a2b972017-04-17 09:39:51 -0700294
Peiyong Lin3bff1352018-12-11 07:56:07 -0800295 // When we reach this point, we have a valid swap behavior
John Reckb36bfdd2020-07-23 13:47:49 -0700296 if (EglExtensions.pixelFormatFloat) {
297 mEglConfigF16 = loadFP16Config(mEglDisplay, mSwapBehavior);
298 if (mEglConfigF16 == EGL_NO_CONFIG_KHR) {
Rob Herring74883ab2017-11-29 09:26:31 -0600299 ALOGE("Device claims wide gamut support, cannot find matching config, error = %s",
John Reck0fa0cbc2019-04-05 16:57:46 -0700300 eglErrorString());
Rob Herring74883ab2017-11-29 09:26:31 -0600301 EglExtensions.pixelFormatFloat = false;
Romain Guy26a2b972017-04-17 09:39:51 -0700302 }
John Reckb36bfdd2020-07-23 13:47:49 -0700303 }
304 mEglConfig1010102 = load1010102Config(mEglDisplay, mSwapBehavior);
305 if (mEglConfig1010102 == EGL_NO_CONFIG_KHR) {
306 ALOGW("Failed to initialize 101010-2 format, error = %s",
307 eglErrorString());
Romain Guy26a2b972017-04-17 09:39:51 -0700308 }
John Reck3b202512014-06-23 13:13:08 -0700309}
310
311void EglManager::createContext() {
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200312 std::vector<EGLint> contextAttributes;
313 contextAttributes.reserve(5);
314 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
315 contextAttributes.push_back(GLES_VERSION);
316 if (Properties::contextPriority != 0 && EglExtensions.contextPriority) {
317 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
318 contextAttributes.push_back(Properties::contextPriority);
319 }
320 contextAttributes.push_back(EGL_NONE);
John Reck1bcacfd2017-11-03 10:12:19 -0700321 mEglContext = eglCreateContext(
322 mEglDisplay, EglExtensions.noConfigContext ? ((EGLConfig) nullptr) : mEglConfig,
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200323 EGL_NO_CONTEXT, contextAttributes.data());
John Reck1bcacfd2017-11-03 10:12:19 -0700324 LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, "Failed to create context, error = %s",
325 eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700326}
327
John Reckd7db4d72015-05-20 07:18:55 -0700328void EglManager::createPBufferSurface() {
John Reck3b202512014-06-23 13:13:08 -0700329 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
John Reck1bcacfd2017-11-03 10:12:19 -0700330 "usePBufferSurface() called on uninitialized GlobalContext!");
John Reck3b202512014-06-23 13:13:08 -0700331
John Reck1e510712018-04-23 08:15:03 -0700332 if (mPBufferSurface == EGL_NO_SURFACE && !EglExtensions.surfacelessContext) {
John Reck1bcacfd2017-11-03 10:12:19 -0700333 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
John Reck3b202512014-06-23 13:13:08 -0700334 mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs);
Roman Kiryanov60736132019-08-02 14:37:53 -0700335 LOG_ALWAYS_FATAL_IF(mPBufferSurface == EGL_NO_SURFACE,
336 "Failed to create a pixel buffer display=%p, "
337 "mEglConfig=%p, error=%s",
338 mEglDisplay, mEglConfig, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700339 }
John Reck3b202512014-06-23 13:13:08 -0700340}
341
Peiyong Lin3bff1352018-12-11 07:56:07 -0800342Result<EGLSurface, EGLint> EglManager::createSurface(EGLNativeWindowType window,
343 ColorMode colorMode,
Brian Osmane0cf5972019-01-23 10:41:20 -0500344 sk_sp<SkColorSpace> colorSpace) {
John Reck1e510712018-04-23 08:15:03 -0700345 LOG_ALWAYS_FATAL_IF(!hasEglContext(), "Not initialized");
Romain Guy253f2c22016-09-28 17:34:42 -0700346
John Reckb36bfdd2020-07-23 13:47:49 -0700347 if (!mHasWideColorGamutSupport || !EglExtensions.noConfigContext) {
348 colorMode = ColorMode::Default;
349 }
Romain Guy26a2b972017-04-17 09:39:51 -0700350
351 // The color space we want to use depends on whether linear blending is turned
352 // on and whether the app has requested wide color gamut rendering. When wide
353 // color gamut rendering is off, the app simply renders in the display's native
354 // color gamut.
355 //
356 // When wide gamut rendering is off:
357 // - Blending is done by default in gamma space, which requires using a
358 // linear EGL color space (the GPU uses the color values as is)
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700359 // - If linear blending is on, we must use the non-linear EGL color space
360 // (the GPU will perform sRGB to linear and linear to SRGB conversions
361 // before and after blending)
Romain Guy26a2b972017-04-17 09:39:51 -0700362 //
363 // When wide gamut rendering is on we cannot rely on the GPU performing
364 // linear blending for us. We use two different color spaces to tag the
365 // surface appropriately for SurfaceFlinger:
Peiyong Lin3bff1352018-12-11 07:56:07 -0800366 // - Gamma blending (default) requires the use of the non-linear color space
367 // - Linear blending requires the use of the linear color space
Romain Guy26a2b972017-04-17 09:39:51 -0700368
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700369 // Not all Android targets support the EGL_GL_COLORSPACE_KHR extension
Romain Guy26a2b972017-04-17 09:39:51 -0700370 // We insert to placeholders to set EGL_GL_COLORSPACE_KHR and its value.
371 // According to section 3.4.1 of the EGL specification, the attributes
372 // list is considered empty if the first entry is EGL_NONE
John Reck1bcacfd2017-11-03 10:12:19 -0700373 EGLint attribs[] = {EGL_NONE, EGL_NONE, EGL_NONE};
Romain Guy253f2c22016-09-28 17:34:42 -0700374
John Reckb36bfdd2020-07-23 13:47:49 -0700375 EGLConfig config = mEglConfig;
376 if (DeviceInfo::get()->getWideColorType() == kRGBA_F16_SkColorType) {
377 if (mEglConfigF16 == EGL_NO_CONFIG_KHR) {
378 colorMode = ColorMode::Default;
379 } else {
380 config = mEglConfigF16;
381 }
382 }
Romain Guy26a2b972017-04-17 09:39:51 -0700383 if (EglExtensions.glColorSpace) {
384 attribs[0] = EGL_GL_COLORSPACE_KHR;
John Reckb36bfdd2020-07-23 13:47:49 -0700385 switch (colorMode) {
386 case ColorMode::Default:
387 attribs[1] = EGL_GL_COLORSPACE_LINEAR_KHR;
388 break;
389 case ColorMode::WideColorGamut: {
390 skcms_Matrix3x3 colorGamut;
391 LOG_ALWAYS_FATAL_IF(!colorSpace->toXYZD50(&colorGamut),
392 "Could not get gamut matrix from color space");
393 if (memcmp(&colorGamut, &SkNamedGamut::kDisplayP3, sizeof(colorGamut)) == 0) {
394 attribs[1] = EGL_GL_COLORSPACE_DISPLAY_P3_PASSTHROUGH_EXT;
395 } else if (memcmp(&colorGamut, &SkNamedGamut::kSRGB, sizeof(colorGamut)) == 0) {
396 attribs[1] = EGL_GL_COLORSPACE_SCRGB_EXT;
397 } else if (memcmp(&colorGamut, &SkNamedGamut::kRec2020, sizeof(colorGamut)) == 0) {
398 attribs[1] = EGL_GL_COLORSPACE_BT2020_PQ_EXT;
399 } else {
400 LOG_ALWAYS_FATAL("Unreachable: unsupported wide color space.");
401 }
402 break;
Peiyong Lin3bff1352018-12-11 07:56:07 -0800403 }
John Reckb36bfdd2020-07-23 13:47:49 -0700404 case ColorMode::Hdr:
405 config = mEglConfigF16;
406 attribs[1] = EGL_GL_COLORSPACE_BT2020_PQ_EXT;
407 break;
408 case ColorMode::Hdr10:
409 config = mEglConfig1010102;
410 attribs[1] = EGL_GL_COLORSPACE_BT2020_PQ_EXT;
411 break;
Romain Guy26a2b972017-04-17 09:39:51 -0700412 }
Romain Guy26a2b972017-04-17 09:39:51 -0700413 }
414
John Reckb36bfdd2020-07-23 13:47:49 -0700415 EGLSurface surface = eglCreateWindowSurface(mEglDisplay, config, window, attribs);
John Reck8e539ca2018-11-15 15:21:29 -0800416 if (surface == EGL_NO_SURFACE) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700417 return Error<EGLint>{eglGetError()};
John Reck8e539ca2018-11-15 15:21:29 -0800418 }
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000419
420 if (mSwapBehavior != SwapBehavior::Preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700421 LOG_ALWAYS_FATAL_IF(eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
422 EGL_BUFFER_DESTROYED) == EGL_FALSE,
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000423 "Failed to set swap behavior to destroyed for window %p, eglErr = %s",
John Reck1bcacfd2017-11-03 10:12:19 -0700424 (void*)window, eglErrorString());
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000425 }
426
John Reck3b202512014-06-23 13:13:08 -0700427 return surface;
428}
429
430void EglManager::destroySurface(EGLSurface surface) {
431 if (isCurrent(surface)) {
432 makeCurrent(EGL_NO_SURFACE);
433 }
434 if (!eglDestroySurface(mEglDisplay, surface)) {
sergeyv694d4992016-10-27 10:23:13 -0700435 ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700436 }
437}
438
439void EglManager::destroy() {
440 if (mEglDisplay == EGL_NO_DISPLAY) return;
441
John Reck3b202512014-06-23 13:13:08 -0700442 eglDestroyContext(mEglDisplay, mEglContext);
John Reck1e510712018-04-23 08:15:03 -0700443 if (mPBufferSurface != EGL_NO_SURFACE) {
444 eglDestroySurface(mEglDisplay, mPBufferSurface);
445 }
John Reck3b202512014-06-23 13:13:08 -0700446 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
447 eglTerminate(mEglDisplay);
448 eglReleaseThread();
449
450 mEglDisplay = EGL_NO_DISPLAY;
451 mEglContext = EGL_NO_CONTEXT;
452 mPBufferSurface = EGL_NO_SURFACE;
453 mCurrentSurface = EGL_NO_SURFACE;
454}
455
John Reck1e510712018-04-23 08:15:03 -0700456bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut, bool force) {
457 if (!force && isCurrent(surface)) return false;
John Reck3b202512014-06-23 13:13:08 -0700458
459 if (surface == EGL_NO_SURFACE) {
John Reckd7db4d72015-05-20 07:18:55 -0700460 // Ensure we always have a valid surface & context
461 surface = mPBufferSurface;
462 }
463 if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) {
John Reckf2dcc2a2015-07-16 09:17:59 -0700464 if (errOut) {
465 *errOut = eglGetError();
John Reck1bcacfd2017-11-03 10:12:19 -0700466 ALOGW("Failed to make current on surface %p, error=%s", (void*)surface,
467 egl_error_str(*errOut));
John Reckf2dcc2a2015-07-16 09:17:59 -0700468 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700469 LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", (void*)surface,
470 eglErrorString());
John Reckf2dcc2a2015-07-16 09:17:59 -0700471 }
John Reck3b202512014-06-23 13:13:08 -0700472 }
473 mCurrentSurface = surface;
John Recka8963062017-06-14 10:47:50 -0700474 if (Properties::disableVsync) {
475 eglSwapInterval(mEglDisplay, 0);
476 }
John Reck3b202512014-06-23 13:13:08 -0700477 return true;
478}
479
John Reck149173d2015-08-10 09:52:29 -0700480EGLint EglManager::queryBufferAge(EGLSurface surface) {
481 switch (mSwapBehavior) {
John Reck1bcacfd2017-11-03 10:12:19 -0700482 case SwapBehavior::Discard:
483 return 0;
484 case SwapBehavior::Preserved:
485 return 1;
486 case SwapBehavior::BufferAge:
487 EGLint bufferAge;
488 eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge);
489 return bufferAge;
John Reck149173d2015-08-10 09:52:29 -0700490 }
491 return 0;
492}
493
494Frame EglManager::beginFrame(EGLSurface surface) {
John Reck1bcacfd2017-11-03 10:12:19 -0700495 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, "Tried to beginFrame on EGL_NO_SURFACE!");
John Reck3b202512014-06-23 13:13:08 -0700496 makeCurrent(surface);
John Reck149173d2015-08-10 09:52:29 -0700497 Frame frame;
498 frame.mSurface = surface;
499 eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth);
500 eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight);
501 frame.mBufferAge = queryBufferAge(surface);
John Reck3b202512014-06-23 13:13:08 -0700502 eglBeginFrame(mEglDisplay, surface);
John Reck149173d2015-08-10 09:52:29 -0700503 return frame;
John Reck3b202512014-06-23 13:13:08 -0700504}
505
John Reck149173d2015-08-10 09:52:29 -0700506void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) {
507#ifdef EGL_KHR_partial_update
508 if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) {
509 EGLint rects[4];
510 frame.map(dirty, rects);
511 if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) {
512 LOG_ALWAYS_FATAL("Failed to set damage region on surface %p, error=%s",
John Reck1bcacfd2017-11-03 10:12:19 -0700513 (void*)frame.mSurface, eglErrorString());
John Reck149173d2015-08-10 09:52:29 -0700514 }
515 }
516#endif
517}
518
John Reckc96955d2016-02-26 14:56:44 -0800519bool EglManager::damageRequiresSwap() {
520 return EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge;
521}
522
John Reck149173d2015-08-10 09:52:29 -0700523bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) {
John Reck682573c2015-10-30 10:37:35 -0700524 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
John Reck55156372015-01-21 07:46:37 -0800525 ATRACE_NAME("Finishing GPU work");
526 fence();
527 }
John Reck55156372015-01-21 07:46:37 -0800528
John Recka672f6b2015-10-22 09:53:26 -0700529 EGLint rects[4];
530 frame.map(screenDirty, rects);
John Reck1bcacfd2017-11-03 10:12:19 -0700531 eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects, screenDirty.isEmpty() ? 0 : 1);
John Reckd04794a2015-05-08 10:04:36 -0700532
John Reck3b202512014-06-23 13:13:08 -0700533 EGLint err = eglGetError();
John Reck2cdbc7d2014-09-17 16:06:36 -0700534 if (CC_LIKELY(err == EGL_SUCCESS)) {
535 return true;
536 }
John Reckc2547fa2015-10-26 13:52:52 -0700537 if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) {
John Reck2cdbc7d2014-09-17 16:06:36 -0700538 // For some reason our surface was destroyed out from under us
539 // This really shouldn't happen, but if it does we can recover easily
540 // by just not trying to use the surface anymore
John Reck1bcacfd2017-11-03 10:12:19 -0700541 ALOGW("swapBuffers encountered EGL error %d on %p, halting rendering...", err,
542 frame.mSurface);
John Reck2cdbc7d2014-09-17 16:06:36 -0700543 return false;
544 }
John Reck1bcacfd2017-11-03 10:12:19 -0700545 LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering", err, egl_error_str(err));
John Reck2cdbc7d2014-09-17 16:06:36 -0700546 // Impossible to hit this, but the compiler doesn't know that
547 return false;
John Reck3b202512014-06-23 13:13:08 -0700548}
549
John Reck55156372015-01-21 07:46:37 -0800550void EglManager::fence() {
551 EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL);
John Reck1bcacfd2017-11-03 10:12:19 -0700552 eglClientWaitSyncKHR(mEglDisplay, fence, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR);
John Reck55156372015-01-21 07:46:37 -0800553 eglDestroySyncKHR(mEglDisplay, fence);
554}
555
John Reck1125d1f2014-10-23 11:02:19 -0700556bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) {
John Reck149173d2015-08-10 09:52:29 -0700557 if (mSwapBehavior != SwapBehavior::Preserved) return false;
John Reck3b202512014-06-23 13:13:08 -0700558
John Reck149173d2015-08-10 09:52:29 -0700559 bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
John Reck1bcacfd2017-11-03 10:12:19 -0700560 preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED);
John Reck149173d2015-08-10 09:52:29 -0700561 if (!preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700562 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", (void*)surface,
563 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700564 // Maybe it's already set?
565 EGLint swapBehavior;
566 if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) {
567 preserved = (swapBehavior == EGL_BUFFER_PRESERVED);
568 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700569 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", (void*)surface,
570 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700571 }
John Reck3b202512014-06-23 13:13:08 -0700572 }
John Reck1125d1f2014-10-23 11:02:19 -0700573
574 return preserved;
John Reck3b202512014-06-23 13:13:08 -0700575}
576
Stan Ilievaaa9e832019-09-17 14:07:23 -0400577static status_t waitForeverOnFence(int fence, const char* logname) {
578 ATRACE_CALL();
579 if (fence == -1) {
580 return NO_ERROR;
581 }
582 constexpr int warningTimeout = 3000;
583 int err = sync_wait(fence, warningTimeout);
584 if (err < 0 && errno == ETIME) {
585 ALOGE("%s: fence %d didn't signal in %d ms", logname, fence, warningTimeout);
586 err = sync_wait(fence, -1);
587 }
588 return err < 0 ? -errno : status_t(NO_ERROR);
589}
590
591status_t EglManager::fenceWait(int fence) {
Stan Iliev564ca3e2018-09-04 22:00:00 +0000592 if (!hasEglContext()) {
593 ALOGE("EglManager::fenceWait: EGLDisplay not initialized");
594 return INVALID_OPERATION;
595 }
596
Alec Mouri680414e2020-01-28 09:22:33 -0800597 if (EglExtensions.waitSync && EglExtensions.nativeFenceSync) {
Stan Iliev564ca3e2018-09-04 22:00:00 +0000598 // Block GPU on the fence.
599 // Create an EGLSyncKHR from the current fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400600 int fenceFd = ::dup(fence);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000601 if (fenceFd == -1) {
602 ALOGE("EglManager::fenceWait: error dup'ing fence fd: %d", errno);
603 return -errno;
604 }
John Reck0fa0cbc2019-04-05 16:57:46 -0700605 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE};
606 EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000607 if (sync == EGL_NO_SYNC_KHR) {
608 close(fenceFd);
609 ALOGE("EglManager::fenceWait: error creating EGL fence: %#x", eglGetError());
610 return UNKNOWN_ERROR;
611 }
612
613 // XXX: The spec draft is inconsistent as to whether this should
614 // return an EGLint or void. Ignore the return value for now, as
615 // it's not strictly needed.
616 eglWaitSyncKHR(mEglDisplay, sync, 0);
617 EGLint eglErr = eglGetError();
618 eglDestroySyncKHR(mEglDisplay, sync);
619 if (eglErr != EGL_SUCCESS) {
620 ALOGE("EglManager::fenceWait: error waiting for EGL fence: %#x", eglErr);
621 return UNKNOWN_ERROR;
622 }
623 } else {
624 // Block CPU on the fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400625 status_t err = waitForeverOnFence(fence, "EglManager::fenceWait");
Stan Iliev564ca3e2018-09-04 22:00:00 +0000626 if (err != NO_ERROR) {
627 ALOGE("EglManager::fenceWait: error waiting for fence: %d", err);
628 return err;
629 }
630 }
631 return OK;
632}
633
Stan Ilievaaa9e832019-09-17 14:07:23 -0400634status_t EglManager::createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence, int* nativeFence) {
635 *nativeFence = -1;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000636 if (!hasEglContext()) {
637 ALOGE("EglManager::createReleaseFence: EGLDisplay not initialized");
638 return INVALID_OPERATION;
639 }
640
Alec Mouri680414e2020-01-28 09:22:33 -0800641 if (EglExtensions.nativeFenceSync) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700642 EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000643 if (sync == EGL_NO_SYNC_KHR) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700644 ALOGE("EglManager::createReleaseFence: error creating EGL fence: %#x", eglGetError());
Stan Iliev564ca3e2018-09-04 22:00:00 +0000645 return UNKNOWN_ERROR;
646 }
647 glFlush();
648 int fenceFd = eglDupNativeFenceFDANDROID(mEglDisplay, sync);
649 eglDestroySyncKHR(mEglDisplay, sync);
650 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
651 ALOGE("EglManager::createReleaseFence: error dup'ing native fence "
John Reck0fa0cbc2019-04-05 16:57:46 -0700652 "fd: %#x",
653 eglGetError());
Stan Iliev564ca3e2018-09-04 22:00:00 +0000654 return UNKNOWN_ERROR;
655 }
Stan Ilievaaa9e832019-09-17 14:07:23 -0400656 *nativeFence = fenceFd;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000657 *eglFence = EGL_NO_SYNC_KHR;
Alec Mouri680414e2020-01-28 09:22:33 -0800658 } else if (useFenceSync && EglExtensions.fenceSync) {
Stan Iliev564ca3e2018-09-04 22:00:00 +0000659 if (*eglFence != EGL_NO_SYNC_KHR) {
660 // There is already a fence for the current slot. We need to
661 // wait on that before replacing it with another fence to
662 // ensure that all outstanding buffer accesses have completed
663 // before the producer accesses it.
664 EGLint result = eglClientWaitSyncKHR(mEglDisplay, *eglFence, 0, 1000000000);
665 if (result == EGL_FALSE) {
666 ALOGE("EglManager::createReleaseFence: error waiting for previous fence: %#x",
John Reck0fa0cbc2019-04-05 16:57:46 -0700667 eglGetError());
Stan Iliev564ca3e2018-09-04 22:00:00 +0000668 return UNKNOWN_ERROR;
669 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
670 ALOGE("EglManager::createReleaseFence: timeout waiting for previous fence");
671 return TIMED_OUT;
672 }
673 eglDestroySyncKHR(mEglDisplay, *eglFence);
674 }
675
676 // Create a fence for the outstanding accesses in the current
677 // OpenGL ES context.
678 *eglFence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, nullptr);
679 if (*eglFence == EGL_NO_SYNC_KHR) {
680 ALOGE("EglManager::createReleaseFence: error creating fence: %#x", eglGetError());
681 return UNKNOWN_ERROR;
682 }
683 glFlush();
684 }
685 return OK;
686}
687
John Reck3b202512014-06-23 13:13:08 -0700688} /* namespace renderthread */
689} /* namespace uirenderer */
690} /* namespace android */