blob: 383c79b27918f604e57691fe56aec984934dfd0f [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;
Nader Jawad4a6b60a2021-09-20 21:22:50 -0700149
150 auto* vendor = reinterpret_cast<const char*>(glGetString(GL_VENDOR));
151 Properties::enableRenderEffectCache = (strcmp(vendor, "Qualcomm") != 0);
Peiyong Lin3bff1352018-12-11 07:56:07 -0800152}
153
154EGLConfig EglManager::load8BitsConfig(EGLDisplay display, EglManager::SwapBehavior swapBehavior) {
155 EGLint eglSwapBehavior =
156 (swapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
157 EGLint attribs[] = {EGL_RENDERABLE_TYPE,
158 EGL_OPENGL_ES2_BIT,
159 EGL_RED_SIZE,
160 8,
161 EGL_GREEN_SIZE,
162 8,
163 EGL_BLUE_SIZE,
164 8,
165 EGL_ALPHA_SIZE,
166 8,
167 EGL_DEPTH_SIZE,
168 0,
169 EGL_CONFIG_CAVEAT,
170 EGL_NONE,
171 EGL_STENCIL_SIZE,
172 STENCIL_BUFFER_SIZE,
173 EGL_SURFACE_TYPE,
174 EGL_WINDOW_BIT | eglSwapBehavior,
175 EGL_NONE};
176 EGLConfig config = EGL_NO_CONFIG_KHR;
177 EGLint numConfigs = 1;
John Reck0fa0cbc2019-04-05 16:57:46 -0700178 if (!eglChooseConfig(display, attribs, &config, numConfigs, &numConfigs) || numConfigs != 1) {
Peiyong Lin3bff1352018-12-11 07:56:07 -0800179 return EGL_NO_CONFIG_KHR;
180 }
181 return config;
182}
183
John Reckb36bfdd2020-07-23 13:47:49 -0700184EGLConfig EglManager::load1010102Config(EGLDisplay display, SwapBehavior swapBehavior) {
185 EGLint eglSwapBehavior =
186 (swapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
187 // If we reached this point, we have a valid swap behavior
188 EGLint attribs[] = {EGL_RENDERABLE_TYPE,
189 EGL_OPENGL_ES2_BIT,
190 EGL_RED_SIZE,
191 10,
192 EGL_GREEN_SIZE,
193 10,
194 EGL_BLUE_SIZE,
195 10,
196 EGL_ALPHA_SIZE,
197 2,
198 EGL_DEPTH_SIZE,
199 0,
200 EGL_STENCIL_SIZE,
201 STENCIL_BUFFER_SIZE,
202 EGL_SURFACE_TYPE,
203 EGL_WINDOW_BIT | eglSwapBehavior,
204 EGL_NONE};
205 EGLConfig config = EGL_NO_CONFIG_KHR;
206 EGLint numConfigs = 1;
207 if (!eglChooseConfig(display, attribs, &config, numConfigs, &numConfigs) || numConfigs != 1) {
208 return EGL_NO_CONFIG_KHR;
209 }
210 return config;
211}
212
Peiyong Lin3bff1352018-12-11 07:56:07 -0800213EGLConfig EglManager::loadFP16Config(EGLDisplay display, SwapBehavior swapBehavior) {
214 EGLint eglSwapBehavior =
215 (swapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
216 // If we reached this point, we have a valid swap behavior
217 EGLint attribs[] = {EGL_RENDERABLE_TYPE,
218 EGL_OPENGL_ES2_BIT,
219 EGL_COLOR_COMPONENT_TYPE_EXT,
220 EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT,
221 EGL_RED_SIZE,
222 16,
223 EGL_GREEN_SIZE,
224 16,
225 EGL_BLUE_SIZE,
226 16,
227 EGL_ALPHA_SIZE,
228 16,
229 EGL_DEPTH_SIZE,
230 0,
231 EGL_STENCIL_SIZE,
232 STENCIL_BUFFER_SIZE,
233 EGL_SURFACE_TYPE,
234 EGL_WINDOW_BIT | eglSwapBehavior,
235 EGL_NONE};
236 EGLConfig config = EGL_NO_CONFIG_KHR;
237 EGLint numConfigs = 1;
John Reck0fa0cbc2019-04-05 16:57:46 -0700238 if (!eglChooseConfig(display, attribs, &config, numConfigs, &numConfigs) || numConfigs != 1) {
Peiyong Lin3bff1352018-12-11 07:56:07 -0800239 return EGL_NO_CONFIG_KHR;
240 }
241 return config;
John Reck3b202512014-06-23 13:13:08 -0700242}
243
John Reck149173d2015-08-10 09:52:29 -0700244void EglManager::initExtensions() {
John Reck1bcacfd2017-11-03 10:12:19 -0700245 auto extensions = StringUtils::split(eglQueryString(mEglDisplay, EGL_EXTENSIONS));
Romain Guy26a2b972017-04-17 09:39:51 -0700246
John Reck8733bff2016-09-27 14:45:28 -0700247 // For our purposes we don't care if EGL_BUFFER_AGE is a result of
248 // EGL_EXT_buffer_age or EGL_KHR_partial_update as our usage is covered
249 // under EGL_KHR_partial_update and we don't need the expanded scope
250 // that EGL_EXT_buffer_age provides.
John Reck1bcacfd2017-11-03 10:12:19 -0700251 EglExtensions.bufferAge =
252 extensions.has("EGL_EXT_buffer_age") || extensions.has("EGL_KHR_partial_update");
Chris Craik6e6646c2015-09-14 15:54:12 -0700253 EglExtensions.setDamage = extensions.has("EGL_KHR_partial_update");
John Reck708b6682015-10-22 13:11:00 -0700254 LOG_ALWAYS_FATAL_IF(!extensions.has("EGL_KHR_swap_buffers_with_damage"),
John Reck1bcacfd2017-11-03 10:12:19 -0700255 "Missing required extension EGL_KHR_swap_buffers_with_damage");
Romain Guy26a2b972017-04-17 09:39:51 -0700256
257 EglExtensions.glColorSpace = extensions.has("EGL_KHR_gl_colorspace");
258 EglExtensions.noConfigContext = extensions.has("EGL_KHR_no_config_context");
259 EglExtensions.pixelFormatFloat = extensions.has("EGL_EXT_pixel_format_float");
Romain Guy26b6a642017-07-11 09:48:28 -0700260 EglExtensions.scRGB = extensions.has("EGL_EXT_gl_colorspace_scrgb");
Peiyong Lin3bff1352018-12-11 07:56:07 -0800261 EglExtensions.displayP3 = extensions.has("EGL_EXT_gl_colorspace_display_p3_passthrough");
John Reckb36bfdd2020-07-23 13:47:49 -0700262 EglExtensions.hdr = extensions.has("EGL_EXT_gl_colorspace_bt2020_pq");
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200263 EglExtensions.contextPriority = extensions.has("EGL_IMG_context_priority");
John Reck1e510712018-04-23 08:15:03 -0700264 EglExtensions.surfacelessContext = extensions.has("EGL_KHR_surfaceless_context");
Alec Mouri680414e2020-01-28 09:22:33 -0800265 EglExtensions.fenceSync = extensions.has("EGL_KHR_fence_sync");
266 EglExtensions.waitSync = extensions.has("EGL_KHR_wait_sync");
Yiwei Zhang7f430132020-08-12 15:07:51 -0700267 EglExtensions.nativeFenceSync = extensions.has("EGL_ANDROID_native_fence_sync");
John Reck149173d2015-08-10 09:52:29 -0700268}
269
John Reck3b202512014-06-23 13:13:08 -0700270bool EglManager::hasEglContext() {
271 return mEglDisplay != EGL_NO_DISPLAY;
272}
273
Romain Guy26a2b972017-04-17 09:39:51 -0700274void EglManager::loadConfigs() {
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700275 // Note: The default pixel format is RGBA_8888, when other formats are
276 // available, we should check the target pixel format and configure the
277 // attributes list properly.
Peiyong Lin3bff1352018-12-11 07:56:07 -0800278 mEglConfig = load8BitsConfig(mEglDisplay, mSwapBehavior);
279 if (mEglConfig == EGL_NO_CONFIG_KHR) {
John Reck149173d2015-08-10 09:52:29 -0700280 if (mSwapBehavior == SwapBehavior::Preserved) {
John Reck3b202512014-06-23 13:13:08 -0700281 // Try again without dirty regions enabled
John Reck149173d2015-08-10 09:52:29 -0700282 ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...");
283 mSwapBehavior = SwapBehavior::Discard;
Peiyong Lin3bff1352018-12-11 07:56:07 -0800284 mEglConfig = load8BitsConfig(mEglDisplay, mSwapBehavior);
John Reck3b202512014-06-23 13:13:08 -0700285 } else {
John Reck149173d2015-08-10 09:52:29 -0700286 // Failed to get a valid config
sergeyv694d4992016-10-27 10:23:13 -0700287 LOG_ALWAYS_FATAL("Failed to choose config, error = %s", eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700288 }
289 }
Romain Guy26a2b972017-04-17 09:39:51 -0700290
Peiyong Lin3bff1352018-12-11 07:56:07 -0800291 // When we reach this point, we have a valid swap behavior
John Reckb36bfdd2020-07-23 13:47:49 -0700292 if (EglExtensions.pixelFormatFloat) {
293 mEglConfigF16 = loadFP16Config(mEglDisplay, mSwapBehavior);
294 if (mEglConfigF16 == EGL_NO_CONFIG_KHR) {
Rob Herring74883ab2017-11-29 09:26:31 -0600295 ALOGE("Device claims wide gamut support, cannot find matching config, error = %s",
John Reck0fa0cbc2019-04-05 16:57:46 -0700296 eglErrorString());
Rob Herring74883ab2017-11-29 09:26:31 -0600297 EglExtensions.pixelFormatFloat = false;
Romain Guy26a2b972017-04-17 09:39:51 -0700298 }
John Reckb36bfdd2020-07-23 13:47:49 -0700299 }
300 mEglConfig1010102 = load1010102Config(mEglDisplay, mSwapBehavior);
301 if (mEglConfig1010102 == EGL_NO_CONFIG_KHR) {
302 ALOGW("Failed to initialize 101010-2 format, error = %s",
303 eglErrorString());
Romain Guy26a2b972017-04-17 09:39:51 -0700304 }
John Reck3b202512014-06-23 13:13:08 -0700305}
306
307void EglManager::createContext() {
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200308 std::vector<EGLint> contextAttributes;
309 contextAttributes.reserve(5);
310 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
311 contextAttributes.push_back(GLES_VERSION);
312 if (Properties::contextPriority != 0 && EglExtensions.contextPriority) {
313 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
314 contextAttributes.push_back(Properties::contextPriority);
315 }
316 contextAttributes.push_back(EGL_NONE);
John Reck1bcacfd2017-11-03 10:12:19 -0700317 mEglContext = eglCreateContext(
318 mEglDisplay, EglExtensions.noConfigContext ? ((EGLConfig) nullptr) : mEglConfig,
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200319 EGL_NO_CONTEXT, contextAttributes.data());
John Reck1bcacfd2017-11-03 10:12:19 -0700320 LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, "Failed to create context, error = %s",
321 eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700322}
323
John Reckd7db4d72015-05-20 07:18:55 -0700324void EglManager::createPBufferSurface() {
John Reck3b202512014-06-23 13:13:08 -0700325 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
John Reck1bcacfd2017-11-03 10:12:19 -0700326 "usePBufferSurface() called on uninitialized GlobalContext!");
John Reck3b202512014-06-23 13:13:08 -0700327
John Reck1e510712018-04-23 08:15:03 -0700328 if (mPBufferSurface == EGL_NO_SURFACE && !EglExtensions.surfacelessContext) {
John Reck1bcacfd2017-11-03 10:12:19 -0700329 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
John Reck3b202512014-06-23 13:13:08 -0700330 mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs);
Roman Kiryanov60736132019-08-02 14:37:53 -0700331 LOG_ALWAYS_FATAL_IF(mPBufferSurface == EGL_NO_SURFACE,
332 "Failed to create a pixel buffer display=%p, "
333 "mEglConfig=%p, error=%s",
334 mEglDisplay, mEglConfig, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700335 }
John Reck3b202512014-06-23 13:13:08 -0700336}
337
Peiyong Lin3bff1352018-12-11 07:56:07 -0800338Result<EGLSurface, EGLint> EglManager::createSurface(EGLNativeWindowType window,
339 ColorMode colorMode,
Brian Osmane0cf5972019-01-23 10:41:20 -0500340 sk_sp<SkColorSpace> colorSpace) {
John Reck1e510712018-04-23 08:15:03 -0700341 LOG_ALWAYS_FATAL_IF(!hasEglContext(), "Not initialized");
Romain Guy253f2c22016-09-28 17:34:42 -0700342
John Reckb36bfdd2020-07-23 13:47:49 -0700343 if (!mHasWideColorGamutSupport || !EglExtensions.noConfigContext) {
344 colorMode = ColorMode::Default;
345 }
Romain Guy26a2b972017-04-17 09:39:51 -0700346
347 // The color space we want to use depends on whether linear blending is turned
348 // on and whether the app has requested wide color gamut rendering. When wide
349 // color gamut rendering is off, the app simply renders in the display's native
350 // color gamut.
351 //
352 // When wide gamut rendering is off:
353 // - Blending is done by default in gamma space, which requires using a
354 // linear EGL color space (the GPU uses the color values as is)
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700355 // - If linear blending is on, we must use the non-linear EGL color space
356 // (the GPU will perform sRGB to linear and linear to SRGB conversions
357 // before and after blending)
Romain Guy26a2b972017-04-17 09:39:51 -0700358 //
359 // When wide gamut rendering is on we cannot rely on the GPU performing
360 // linear blending for us. We use two different color spaces to tag the
361 // surface appropriately for SurfaceFlinger:
Peiyong Lin3bff1352018-12-11 07:56:07 -0800362 // - Gamma blending (default) requires the use of the non-linear color space
363 // - Linear blending requires the use of the linear color space
Romain Guy26a2b972017-04-17 09:39:51 -0700364
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700365 // Not all Android targets support the EGL_GL_COLORSPACE_KHR extension
Romain Guy26a2b972017-04-17 09:39:51 -0700366 // We insert to placeholders to set EGL_GL_COLORSPACE_KHR and its value.
367 // According to section 3.4.1 of the EGL specification, the attributes
368 // list is considered empty if the first entry is EGL_NONE
John Reck1bcacfd2017-11-03 10:12:19 -0700369 EGLint attribs[] = {EGL_NONE, EGL_NONE, EGL_NONE};
Romain Guy253f2c22016-09-28 17:34:42 -0700370
John Reckb36bfdd2020-07-23 13:47:49 -0700371 EGLConfig config = mEglConfig;
372 if (DeviceInfo::get()->getWideColorType() == kRGBA_F16_SkColorType) {
373 if (mEglConfigF16 == EGL_NO_CONFIG_KHR) {
374 colorMode = ColorMode::Default;
375 } else {
376 config = mEglConfigF16;
377 }
378 }
Romain Guy26a2b972017-04-17 09:39:51 -0700379 if (EglExtensions.glColorSpace) {
380 attribs[0] = EGL_GL_COLORSPACE_KHR;
John Reckb36bfdd2020-07-23 13:47:49 -0700381 switch (colorMode) {
382 case ColorMode::Default:
383 attribs[1] = EGL_GL_COLORSPACE_LINEAR_KHR;
384 break;
385 case ColorMode::WideColorGamut: {
386 skcms_Matrix3x3 colorGamut;
387 LOG_ALWAYS_FATAL_IF(!colorSpace->toXYZD50(&colorGamut),
388 "Could not get gamut matrix from color space");
389 if (memcmp(&colorGamut, &SkNamedGamut::kDisplayP3, sizeof(colorGamut)) == 0) {
390 attribs[1] = EGL_GL_COLORSPACE_DISPLAY_P3_PASSTHROUGH_EXT;
391 } else if (memcmp(&colorGamut, &SkNamedGamut::kSRGB, sizeof(colorGamut)) == 0) {
392 attribs[1] = EGL_GL_COLORSPACE_SCRGB_EXT;
393 } else if (memcmp(&colorGamut, &SkNamedGamut::kRec2020, sizeof(colorGamut)) == 0) {
394 attribs[1] = EGL_GL_COLORSPACE_BT2020_PQ_EXT;
395 } else {
396 LOG_ALWAYS_FATAL("Unreachable: unsupported wide color space.");
397 }
398 break;
Peiyong Lin3bff1352018-12-11 07:56:07 -0800399 }
John Reckb36bfdd2020-07-23 13:47:49 -0700400 case ColorMode::Hdr:
401 config = mEglConfigF16;
402 attribs[1] = EGL_GL_COLORSPACE_BT2020_PQ_EXT;
403 break;
404 case ColorMode::Hdr10:
405 config = mEglConfig1010102;
406 attribs[1] = EGL_GL_COLORSPACE_BT2020_PQ_EXT;
407 break;
Romain Guy26a2b972017-04-17 09:39:51 -0700408 }
Romain Guy26a2b972017-04-17 09:39:51 -0700409 }
410
John Reckb36bfdd2020-07-23 13:47:49 -0700411 EGLSurface surface = eglCreateWindowSurface(mEglDisplay, config, window, attribs);
John Reck8e539ca2018-11-15 15:21:29 -0800412 if (surface == EGL_NO_SURFACE) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700413 return Error<EGLint>{eglGetError()};
John Reck8e539ca2018-11-15 15:21:29 -0800414 }
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000415
416 if (mSwapBehavior != SwapBehavior::Preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700417 LOG_ALWAYS_FATAL_IF(eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
418 EGL_BUFFER_DESTROYED) == EGL_FALSE,
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000419 "Failed to set swap behavior to destroyed for window %p, eglErr = %s",
John Reck1bcacfd2017-11-03 10:12:19 -0700420 (void*)window, eglErrorString());
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000421 }
422
John Reck3b202512014-06-23 13:13:08 -0700423 return surface;
424}
425
426void EglManager::destroySurface(EGLSurface surface) {
427 if (isCurrent(surface)) {
428 makeCurrent(EGL_NO_SURFACE);
429 }
430 if (!eglDestroySurface(mEglDisplay, surface)) {
sergeyv694d4992016-10-27 10:23:13 -0700431 ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700432 }
433}
434
435void EglManager::destroy() {
436 if (mEglDisplay == EGL_NO_DISPLAY) return;
437
John Reck3b202512014-06-23 13:13:08 -0700438 eglDestroyContext(mEglDisplay, mEglContext);
John Reck1e510712018-04-23 08:15:03 -0700439 if (mPBufferSurface != EGL_NO_SURFACE) {
440 eglDestroySurface(mEglDisplay, mPBufferSurface);
441 }
John Reck3b202512014-06-23 13:13:08 -0700442 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
443 eglTerminate(mEglDisplay);
444 eglReleaseThread();
445
446 mEglDisplay = EGL_NO_DISPLAY;
447 mEglContext = EGL_NO_CONTEXT;
448 mPBufferSurface = EGL_NO_SURFACE;
449 mCurrentSurface = EGL_NO_SURFACE;
450}
451
John Reck1e510712018-04-23 08:15:03 -0700452bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut, bool force) {
453 if (!force && isCurrent(surface)) return false;
John Reck3b202512014-06-23 13:13:08 -0700454
455 if (surface == EGL_NO_SURFACE) {
John Reckd7db4d72015-05-20 07:18:55 -0700456 // Ensure we always have a valid surface & context
457 surface = mPBufferSurface;
458 }
459 if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) {
John Reckf2dcc2a2015-07-16 09:17:59 -0700460 if (errOut) {
461 *errOut = eglGetError();
John Reck1bcacfd2017-11-03 10:12:19 -0700462 ALOGW("Failed to make current on surface %p, error=%s", (void*)surface,
463 egl_error_str(*errOut));
John Reckf2dcc2a2015-07-16 09:17:59 -0700464 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700465 LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", (void*)surface,
466 eglErrorString());
John Reckf2dcc2a2015-07-16 09:17:59 -0700467 }
John Reck3b202512014-06-23 13:13:08 -0700468 }
469 mCurrentSurface = surface;
John Recka8963062017-06-14 10:47:50 -0700470 if (Properties::disableVsync) {
471 eglSwapInterval(mEglDisplay, 0);
472 }
John Reck3b202512014-06-23 13:13:08 -0700473 return true;
474}
475
John Reck149173d2015-08-10 09:52:29 -0700476EGLint EglManager::queryBufferAge(EGLSurface surface) {
477 switch (mSwapBehavior) {
John Reck1bcacfd2017-11-03 10:12:19 -0700478 case SwapBehavior::Discard:
479 return 0;
480 case SwapBehavior::Preserved:
481 return 1;
482 case SwapBehavior::BufferAge:
483 EGLint bufferAge;
484 eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge);
485 return bufferAge;
John Reck149173d2015-08-10 09:52:29 -0700486 }
487 return 0;
488}
489
490Frame EglManager::beginFrame(EGLSurface surface) {
John Reck1bcacfd2017-11-03 10:12:19 -0700491 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, "Tried to beginFrame on EGL_NO_SURFACE!");
John Reck3b202512014-06-23 13:13:08 -0700492 makeCurrent(surface);
John Reck149173d2015-08-10 09:52:29 -0700493 Frame frame;
494 frame.mSurface = surface;
495 eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth);
496 eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight);
497 frame.mBufferAge = queryBufferAge(surface);
John Reck3b202512014-06-23 13:13:08 -0700498 eglBeginFrame(mEglDisplay, surface);
John Reck149173d2015-08-10 09:52:29 -0700499 return frame;
John Reck3b202512014-06-23 13:13:08 -0700500}
501
John Reck149173d2015-08-10 09:52:29 -0700502void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) {
503#ifdef EGL_KHR_partial_update
504 if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) {
505 EGLint rects[4];
506 frame.map(dirty, rects);
507 if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) {
508 LOG_ALWAYS_FATAL("Failed to set damage region on surface %p, error=%s",
John Reck1bcacfd2017-11-03 10:12:19 -0700509 (void*)frame.mSurface, eglErrorString());
John Reck149173d2015-08-10 09:52:29 -0700510 }
511 }
512#endif
513}
514
John Reckc96955d2016-02-26 14:56:44 -0800515bool EglManager::damageRequiresSwap() {
516 return EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge;
517}
518
John Reck149173d2015-08-10 09:52:29 -0700519bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) {
John Reck682573c2015-10-30 10:37:35 -0700520 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
John Reck55156372015-01-21 07:46:37 -0800521 ATRACE_NAME("Finishing GPU work");
522 fence();
523 }
John Reck55156372015-01-21 07:46:37 -0800524
John Recka672f6b2015-10-22 09:53:26 -0700525 EGLint rects[4];
526 frame.map(screenDirty, rects);
John Reck1bcacfd2017-11-03 10:12:19 -0700527 eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects, screenDirty.isEmpty() ? 0 : 1);
John Reckd04794a2015-05-08 10:04:36 -0700528
John Reck3b202512014-06-23 13:13:08 -0700529 EGLint err = eglGetError();
John Reck2cdbc7d2014-09-17 16:06:36 -0700530 if (CC_LIKELY(err == EGL_SUCCESS)) {
531 return true;
532 }
John Reckc2547fa2015-10-26 13:52:52 -0700533 if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) {
John Reck2cdbc7d2014-09-17 16:06:36 -0700534 // For some reason our surface was destroyed out from under us
535 // This really shouldn't happen, but if it does we can recover easily
536 // by just not trying to use the surface anymore
John Reck1bcacfd2017-11-03 10:12:19 -0700537 ALOGW("swapBuffers encountered EGL error %d on %p, halting rendering...", err,
538 frame.mSurface);
John Reck2cdbc7d2014-09-17 16:06:36 -0700539 return false;
540 }
John Reck1bcacfd2017-11-03 10:12:19 -0700541 LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering", err, egl_error_str(err));
John Reck2cdbc7d2014-09-17 16:06:36 -0700542 // Impossible to hit this, but the compiler doesn't know that
543 return false;
John Reck3b202512014-06-23 13:13:08 -0700544}
545
John Reck55156372015-01-21 07:46:37 -0800546void EglManager::fence() {
547 EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL);
John Reck1bcacfd2017-11-03 10:12:19 -0700548 eglClientWaitSyncKHR(mEglDisplay, fence, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR);
John Reck55156372015-01-21 07:46:37 -0800549 eglDestroySyncKHR(mEglDisplay, fence);
550}
551
John Reck1125d1f2014-10-23 11:02:19 -0700552bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) {
John Reck149173d2015-08-10 09:52:29 -0700553 if (mSwapBehavior != SwapBehavior::Preserved) return false;
John Reck3b202512014-06-23 13:13:08 -0700554
John Reck149173d2015-08-10 09:52:29 -0700555 bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
John Reck1bcacfd2017-11-03 10:12:19 -0700556 preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED);
John Reck149173d2015-08-10 09:52:29 -0700557 if (!preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700558 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", (void*)surface,
559 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700560 // Maybe it's already set?
561 EGLint swapBehavior;
562 if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) {
563 preserved = (swapBehavior == EGL_BUFFER_PRESERVED);
564 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700565 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", (void*)surface,
566 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700567 }
John Reck3b202512014-06-23 13:13:08 -0700568 }
John Reck1125d1f2014-10-23 11:02:19 -0700569
570 return preserved;
John Reck3b202512014-06-23 13:13:08 -0700571}
572
Stan Ilievaaa9e832019-09-17 14:07:23 -0400573static status_t waitForeverOnFence(int fence, const char* logname) {
574 ATRACE_CALL();
575 if (fence == -1) {
576 return NO_ERROR;
577 }
578 constexpr int warningTimeout = 3000;
579 int err = sync_wait(fence, warningTimeout);
580 if (err < 0 && errno == ETIME) {
581 ALOGE("%s: fence %d didn't signal in %d ms", logname, fence, warningTimeout);
582 err = sync_wait(fence, -1);
583 }
584 return err < 0 ? -errno : status_t(NO_ERROR);
585}
586
587status_t EglManager::fenceWait(int fence) {
Stan Iliev564ca3e2018-09-04 22:00:00 +0000588 if (!hasEglContext()) {
589 ALOGE("EglManager::fenceWait: EGLDisplay not initialized");
590 return INVALID_OPERATION;
591 }
592
Alec Mouri680414e2020-01-28 09:22:33 -0800593 if (EglExtensions.waitSync && EglExtensions.nativeFenceSync) {
Stan Iliev564ca3e2018-09-04 22:00:00 +0000594 // Block GPU on the fence.
595 // Create an EGLSyncKHR from the current fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400596 int fenceFd = ::dup(fence);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000597 if (fenceFd == -1) {
598 ALOGE("EglManager::fenceWait: error dup'ing fence fd: %d", errno);
599 return -errno;
600 }
John Reck0fa0cbc2019-04-05 16:57:46 -0700601 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE};
602 EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000603 if (sync == EGL_NO_SYNC_KHR) {
604 close(fenceFd);
605 ALOGE("EglManager::fenceWait: error creating EGL fence: %#x", eglGetError());
606 return UNKNOWN_ERROR;
607 }
608
609 // XXX: The spec draft is inconsistent as to whether this should
610 // return an EGLint or void. Ignore the return value for now, as
611 // it's not strictly needed.
612 eglWaitSyncKHR(mEglDisplay, sync, 0);
613 EGLint eglErr = eglGetError();
614 eglDestroySyncKHR(mEglDisplay, sync);
615 if (eglErr != EGL_SUCCESS) {
616 ALOGE("EglManager::fenceWait: error waiting for EGL fence: %#x", eglErr);
617 return UNKNOWN_ERROR;
618 }
619 } else {
620 // Block CPU on the fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400621 status_t err = waitForeverOnFence(fence, "EglManager::fenceWait");
Stan Iliev564ca3e2018-09-04 22:00:00 +0000622 if (err != NO_ERROR) {
623 ALOGE("EglManager::fenceWait: error waiting for fence: %d", err);
624 return err;
625 }
626 }
627 return OK;
628}
629
Stan Ilievaaa9e832019-09-17 14:07:23 -0400630status_t EglManager::createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence, int* nativeFence) {
631 *nativeFence = -1;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000632 if (!hasEglContext()) {
633 ALOGE("EglManager::createReleaseFence: EGLDisplay not initialized");
634 return INVALID_OPERATION;
635 }
636
Alec Mouri680414e2020-01-28 09:22:33 -0800637 if (EglExtensions.nativeFenceSync) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700638 EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000639 if (sync == EGL_NO_SYNC_KHR) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700640 ALOGE("EglManager::createReleaseFence: error creating EGL fence: %#x", eglGetError());
Stan Iliev564ca3e2018-09-04 22:00:00 +0000641 return UNKNOWN_ERROR;
642 }
643 glFlush();
644 int fenceFd = eglDupNativeFenceFDANDROID(mEglDisplay, sync);
645 eglDestroySyncKHR(mEglDisplay, sync);
646 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
647 ALOGE("EglManager::createReleaseFence: error dup'ing native fence "
John Reck0fa0cbc2019-04-05 16:57:46 -0700648 "fd: %#x",
649 eglGetError());
Stan Iliev564ca3e2018-09-04 22:00:00 +0000650 return UNKNOWN_ERROR;
651 }
Stan Ilievaaa9e832019-09-17 14:07:23 -0400652 *nativeFence = fenceFd;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000653 *eglFence = EGL_NO_SYNC_KHR;
Alec Mouri680414e2020-01-28 09:22:33 -0800654 } else if (useFenceSync && EglExtensions.fenceSync) {
Stan Iliev564ca3e2018-09-04 22:00:00 +0000655 if (*eglFence != EGL_NO_SYNC_KHR) {
656 // There is already a fence for the current slot. We need to
657 // wait on that before replacing it with another fence to
658 // ensure that all outstanding buffer accesses have completed
659 // before the producer accesses it.
660 EGLint result = eglClientWaitSyncKHR(mEglDisplay, *eglFence, 0, 1000000000);
661 if (result == EGL_FALSE) {
662 ALOGE("EglManager::createReleaseFence: error waiting for previous fence: %#x",
John Reck0fa0cbc2019-04-05 16:57:46 -0700663 eglGetError());
Stan Iliev564ca3e2018-09-04 22:00:00 +0000664 return UNKNOWN_ERROR;
665 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
666 ALOGE("EglManager::createReleaseFence: timeout waiting for previous fence");
667 return TIMED_OUT;
668 }
669 eglDestroySyncKHR(mEglDisplay, *eglFence);
670 }
671
672 // Create a fence for the outstanding accesses in the current
673 // OpenGL ES context.
674 *eglFence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, nullptr);
675 if (*eglFence == EGL_NO_SYNC_KHR) {
676 ALOGE("EglManager::createReleaseFence: error creating fence: %#x", eglGetError());
677 return UNKNOWN_ERROR;
678 }
679 glFlush();
680 }
681 return OK;
682}
683
John Reck3b202512014-06-23 13:13:08 -0700684} /* namespace renderthread */
685} /* namespace uirenderer */
686} /* namespace android */