blob: c7d7a17a23ebc9ecc51c53bd4760e62011b1199c [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"
Nader Jawad086645d2021-09-24 13:42:47 -070031#include "RenderEffectCapabilityQuery.h"
Stan Ilievaaa9e832019-09-17 14:07:23 -040032#include "utils/Color.h"
33#include "utils/StringUtils.h"
Derek Sollenberger7e044fe2016-11-07 10:57:59 -050034
John Reck3b202512014-06-23 13:13:08 -070035#define GLES_VERSION 2
36
37// Android-specific addition that is used to show when frames began in systrace
38EGLAPI void EGLAPIENTRY eglBeginFrame(EGLDisplay dpy, EGLSurface surface);
39
40namespace android {
41namespace uirenderer {
42namespace renderthread {
43
John Reck1bcacfd2017-11-03 10:12:19 -070044#define ERROR_CASE(x) \
45 case x: \
46 return #x;
John Reck3b202512014-06-23 13:13:08 -070047static const char* egl_error_str(EGLint error) {
48 switch (error) {
49 ERROR_CASE(EGL_SUCCESS)
50 ERROR_CASE(EGL_NOT_INITIALIZED)
51 ERROR_CASE(EGL_BAD_ACCESS)
52 ERROR_CASE(EGL_BAD_ALLOC)
53 ERROR_CASE(EGL_BAD_ATTRIBUTE)
54 ERROR_CASE(EGL_BAD_CONFIG)
55 ERROR_CASE(EGL_BAD_CONTEXT)
56 ERROR_CASE(EGL_BAD_CURRENT_SURFACE)
57 ERROR_CASE(EGL_BAD_DISPLAY)
58 ERROR_CASE(EGL_BAD_MATCH)
59 ERROR_CASE(EGL_BAD_NATIVE_PIXMAP)
60 ERROR_CASE(EGL_BAD_NATIVE_WINDOW)
61 ERROR_CASE(EGL_BAD_PARAMETER)
62 ERROR_CASE(EGL_BAD_SURFACE)
63 ERROR_CASE(EGL_CONTEXT_LOST)
John Reck1bcacfd2017-11-03 10:12:19 -070064 default:
65 return "Unknown error";
John Reck3b202512014-06-23 13:13:08 -070066 }
67}
sergeyv694d4992016-10-27 10:23:13 -070068const char* EglManager::eglErrorString() {
John Reck3b202512014-06-23 13:13:08 -070069 return egl_error_str(eglGetError());
70}
71
John Reck149173d2015-08-10 09:52:29 -070072static struct {
73 bool bufferAge = false;
74 bool setDamage = false;
Romain Guy26a2b972017-04-17 09:39:51 -070075 bool noConfigContext = false;
76 bool pixelFormatFloat = false;
77 bool glColorSpace = false;
Romain Guy26b6a642017-07-11 09:48:28 -070078 bool scRGB = false;
Peiyong Lin1f6aa122018-09-10 16:28:08 -070079 bool displayP3 = false;
John Reckb36bfdd2020-07-23 13:47:49 -070080 bool hdr = false;
Jorim Jaggi767e25e2018-04-04 23:07:35 +020081 bool contextPriority = false;
John Reck1e510712018-04-23 08:15:03 -070082 bool surfacelessContext = false;
Alec Mouri680414e2020-01-28 09:22:33 -080083 bool nativeFenceSync = false;
84 bool fenceSync = false;
85 bool waitSync = false;
John Reck149173d2015-08-10 09:52:29 -070086} EglExtensions;
87
John Reck1e510712018-04-23 08:15:03 -070088EglManager::EglManager()
89 : mEglDisplay(EGL_NO_DISPLAY)
Chris Craikd41c4d82015-01-05 15:51:13 -080090 , mEglConfig(nullptr)
John Reckb36bfdd2020-07-23 13:47:49 -070091 , mEglConfigF16(nullptr)
92 , mEglConfig1010102(nullptr)
John Reck3b202512014-06-23 13:13:08 -070093 , mEglContext(EGL_NO_CONTEXT)
94 , mPBufferSurface(EGL_NO_SURFACE)
Peiyong Lin3bff1352018-12-11 07:56:07 -080095 , mCurrentSurface(EGL_NO_SURFACE)
96 , mHasWideColorGamutSupport(false) {}
John Reck3b202512014-06-23 13:13:08 -070097
John Reck1e510712018-04-23 08:15:03 -070098EglManager::~EglManager() {
John Reck6104cea2019-01-10 14:37:17 -080099 if (hasEglContext()) {
100 ALOGW("~EglManager() leaked an EGL context");
101 }
John Reck1e510712018-04-23 08:15:03 -0700102}
103
John Reck3b202512014-06-23 13:13:08 -0700104void EglManager::initialize() {
105 if (hasEglContext()) return;
106
John Reckfbc8df02014-11-14 16:18:41 -0800107 ATRACE_NAME("Creating EGLContext");
108
John Reck3b202512014-06-23 13:13:08 -0700109 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
John Reck1bcacfd2017-11-03 10:12:19 -0700110 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, "Failed to get EGL_DEFAULT_DISPLAY! err=%s",
111 eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700112
113 EGLint major, minor;
114 LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE,
John Reck1bcacfd2017-11-03 10:12:19 -0700115 "Failed to initialize display %p! err=%s", mEglDisplay, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700116
John Reck848f6512018-12-03 13:26:43 -0800117 ALOGV("Initialized EGL, version %d.%d", (int)major, (int)minor);
John Reck3b202512014-06-23 13:13:08 -0700118
John Reck149173d2015-08-10 09:52:29 -0700119 initExtensions();
Season Li13d1b4a2015-07-29 17:16:19 -0700120
John Reck149173d2015-08-10 09:52:29 -0700121 // Now that extensions are loaded, pick a swap behavior
122 if (Properties::enablePartialUpdates) {
Matt Sarettb77c94a2016-12-07 12:22:37 -0500123 // An Adreno driver bug is causing rendering problems for SkiaGL with
124 // buffer age swap behavior (b/31957043). To temporarily workaround,
125 // we will use preserved swap behavior.
Derek Sollenbergerb5a12bd2017-06-14 15:23:19 -0400126 if (Properties::useBufferAge && EglExtensions.bufferAge) {
John Reck149173d2015-08-10 09:52:29 -0700127 mSwapBehavior = SwapBehavior::BufferAge;
128 } else {
129 mSwapBehavior = SwapBehavior::Preserved;
130 }
131 }
132
Romain Guy26a2b972017-04-17 09:39:51 -0700133 loadConfigs();
John Reck3b202512014-06-23 13:13:08 -0700134 createContext();
John Reckd7db4d72015-05-20 07:18:55 -0700135 createPBufferSurface();
John Reck1e510712018-04-23 08:15:03 -0700136 makeCurrent(mPBufferSurface, nullptr, /* force */ true);
Peiyong Lin3bff1352018-12-11 07:56:07 -0800137
Brian Osmane0cf5972019-01-23 10:41:20 -0500138 skcms_Matrix3x3 wideColorGamut;
139 LOG_ALWAYS_FATAL_IF(!DeviceInfo::get()->getWideColorSpace()->toXYZD50(&wideColorGamut),
140 "Could not get gamut matrix from wideColorSpace");
Peiyong Lin3bff1352018-12-11 07:56:07 -0800141 bool hasWideColorSpaceExtension = false;
Mike Reed7ac1af32020-05-26 10:50:21 -0400142 if (memcmp(&wideColorGamut, &SkNamedGamut::kDisplayP3, sizeof(wideColorGamut)) == 0) {
Peiyong Lin3bff1352018-12-11 07:56:07 -0800143 hasWideColorSpaceExtension = EglExtensions.displayP3;
Brian Osmane0cf5972019-01-23 10:41:20 -0500144 } else if (memcmp(&wideColorGamut, &SkNamedGamut::kSRGB, sizeof(wideColorGamut)) == 0) {
Peiyong Lin3bff1352018-12-11 07:56:07 -0800145 hasWideColorSpaceExtension = EglExtensions.scRGB;
146 } else {
147 LOG_ALWAYS_FATAL("Unsupported wide color space.");
148 }
John Reckb36bfdd2020-07-23 13:47:49 -0700149 mHasWideColorGamutSupport = EglExtensions.glColorSpace && hasWideColorSpaceExtension;
Nader Jawad4a6b60a2021-09-20 21:22:50 -0700150
151 auto* vendor = reinterpret_cast<const char*>(glGetString(GL_VENDOR));
Nader Jawad086645d2021-09-24 13:42:47 -0700152 auto* version = reinterpret_cast<const char*>(glGetString(GL_VERSION));
153 Properties::enableRenderEffectCache = supportsRenderEffectCache(
154 vendor, version);
155 ALOGV("RenderEffectCache supported %d on driver version %s",
156 Properties::enableRenderEffectCache, version);
Peiyong Lin3bff1352018-12-11 07:56:07 -0800157}
158
159EGLConfig EglManager::load8BitsConfig(EGLDisplay display, EglManager::SwapBehavior swapBehavior) {
160 EGLint eglSwapBehavior =
161 (swapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
162 EGLint attribs[] = {EGL_RENDERABLE_TYPE,
163 EGL_OPENGL_ES2_BIT,
164 EGL_RED_SIZE,
165 8,
166 EGL_GREEN_SIZE,
167 8,
168 EGL_BLUE_SIZE,
169 8,
170 EGL_ALPHA_SIZE,
171 8,
172 EGL_DEPTH_SIZE,
173 0,
174 EGL_CONFIG_CAVEAT,
175 EGL_NONE,
176 EGL_STENCIL_SIZE,
177 STENCIL_BUFFER_SIZE,
178 EGL_SURFACE_TYPE,
179 EGL_WINDOW_BIT | eglSwapBehavior,
180 EGL_NONE};
181 EGLConfig config = EGL_NO_CONFIG_KHR;
182 EGLint numConfigs = 1;
John Reck0fa0cbc2019-04-05 16:57:46 -0700183 if (!eglChooseConfig(display, attribs, &config, numConfigs, &numConfigs) || numConfigs != 1) {
Peiyong Lin3bff1352018-12-11 07:56:07 -0800184 return EGL_NO_CONFIG_KHR;
185 }
186 return config;
187}
188
John Reckb36bfdd2020-07-23 13:47:49 -0700189EGLConfig EglManager::load1010102Config(EGLDisplay display, SwapBehavior swapBehavior) {
190 EGLint eglSwapBehavior =
191 (swapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
192 // If we reached this point, we have a valid swap behavior
193 EGLint attribs[] = {EGL_RENDERABLE_TYPE,
194 EGL_OPENGL_ES2_BIT,
195 EGL_RED_SIZE,
196 10,
197 EGL_GREEN_SIZE,
198 10,
199 EGL_BLUE_SIZE,
200 10,
201 EGL_ALPHA_SIZE,
202 2,
203 EGL_DEPTH_SIZE,
204 0,
205 EGL_STENCIL_SIZE,
206 STENCIL_BUFFER_SIZE,
207 EGL_SURFACE_TYPE,
208 EGL_WINDOW_BIT | eglSwapBehavior,
209 EGL_NONE};
210 EGLConfig config = EGL_NO_CONFIG_KHR;
211 EGLint numConfigs = 1;
212 if (!eglChooseConfig(display, attribs, &config, numConfigs, &numConfigs) || numConfigs != 1) {
213 return EGL_NO_CONFIG_KHR;
214 }
215 return config;
216}
217
Peiyong Lin3bff1352018-12-11 07:56:07 -0800218EGLConfig EglManager::loadFP16Config(EGLDisplay display, SwapBehavior swapBehavior) {
219 EGLint eglSwapBehavior =
220 (swapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
221 // If we reached this point, we have a valid swap behavior
222 EGLint attribs[] = {EGL_RENDERABLE_TYPE,
223 EGL_OPENGL_ES2_BIT,
224 EGL_COLOR_COMPONENT_TYPE_EXT,
225 EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT,
226 EGL_RED_SIZE,
227 16,
228 EGL_GREEN_SIZE,
229 16,
230 EGL_BLUE_SIZE,
231 16,
232 EGL_ALPHA_SIZE,
233 16,
234 EGL_DEPTH_SIZE,
235 0,
236 EGL_STENCIL_SIZE,
237 STENCIL_BUFFER_SIZE,
238 EGL_SURFACE_TYPE,
239 EGL_WINDOW_BIT | eglSwapBehavior,
240 EGL_NONE};
241 EGLConfig config = EGL_NO_CONFIG_KHR;
242 EGLint numConfigs = 1;
John Reck0fa0cbc2019-04-05 16:57:46 -0700243 if (!eglChooseConfig(display, attribs, &config, numConfigs, &numConfigs) || numConfigs != 1) {
Peiyong Lin3bff1352018-12-11 07:56:07 -0800244 return EGL_NO_CONFIG_KHR;
245 }
246 return config;
John Reck3b202512014-06-23 13:13:08 -0700247}
248
John Reck149173d2015-08-10 09:52:29 -0700249void EglManager::initExtensions() {
John Reck1bcacfd2017-11-03 10:12:19 -0700250 auto extensions = StringUtils::split(eglQueryString(mEglDisplay, EGL_EXTENSIONS));
Romain Guy26a2b972017-04-17 09:39:51 -0700251
John Reck8733bff2016-09-27 14:45:28 -0700252 // For our purposes we don't care if EGL_BUFFER_AGE is a result of
253 // EGL_EXT_buffer_age or EGL_KHR_partial_update as our usage is covered
254 // under EGL_KHR_partial_update and we don't need the expanded scope
255 // that EGL_EXT_buffer_age provides.
John Reck1bcacfd2017-11-03 10:12:19 -0700256 EglExtensions.bufferAge =
257 extensions.has("EGL_EXT_buffer_age") || extensions.has("EGL_KHR_partial_update");
Chris Craik6e6646c2015-09-14 15:54:12 -0700258 EglExtensions.setDamage = extensions.has("EGL_KHR_partial_update");
John Reck708b6682015-10-22 13:11:00 -0700259 LOG_ALWAYS_FATAL_IF(!extensions.has("EGL_KHR_swap_buffers_with_damage"),
John Reck1bcacfd2017-11-03 10:12:19 -0700260 "Missing required extension EGL_KHR_swap_buffers_with_damage");
Romain Guy26a2b972017-04-17 09:39:51 -0700261
262 EglExtensions.glColorSpace = extensions.has("EGL_KHR_gl_colorspace");
263 EglExtensions.noConfigContext = extensions.has("EGL_KHR_no_config_context");
264 EglExtensions.pixelFormatFloat = extensions.has("EGL_EXT_pixel_format_float");
Romain Guy26b6a642017-07-11 09:48:28 -0700265 EglExtensions.scRGB = extensions.has("EGL_EXT_gl_colorspace_scrgb");
Peiyong Lin3bff1352018-12-11 07:56:07 -0800266 EglExtensions.displayP3 = extensions.has("EGL_EXT_gl_colorspace_display_p3_passthrough");
John Reckb36bfdd2020-07-23 13:47:49 -0700267 EglExtensions.hdr = extensions.has("EGL_EXT_gl_colorspace_bt2020_pq");
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200268 EglExtensions.contextPriority = extensions.has("EGL_IMG_context_priority");
John Reck1e510712018-04-23 08:15:03 -0700269 EglExtensions.surfacelessContext = extensions.has("EGL_KHR_surfaceless_context");
Alec Mouri680414e2020-01-28 09:22:33 -0800270 EglExtensions.fenceSync = extensions.has("EGL_KHR_fence_sync");
271 EglExtensions.waitSync = extensions.has("EGL_KHR_wait_sync");
Yiwei Zhang7f430132020-08-12 15:07:51 -0700272 EglExtensions.nativeFenceSync = extensions.has("EGL_ANDROID_native_fence_sync");
John Reck149173d2015-08-10 09:52:29 -0700273}
274
John Reck3b202512014-06-23 13:13:08 -0700275bool EglManager::hasEglContext() {
276 return mEglDisplay != EGL_NO_DISPLAY;
277}
278
Romain Guy26a2b972017-04-17 09:39:51 -0700279void EglManager::loadConfigs() {
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700280 // Note: The default pixel format is RGBA_8888, when other formats are
281 // available, we should check the target pixel format and configure the
282 // attributes list properly.
Peiyong Lin3bff1352018-12-11 07:56:07 -0800283 mEglConfig = load8BitsConfig(mEglDisplay, mSwapBehavior);
284 if (mEglConfig == EGL_NO_CONFIG_KHR) {
John Reck149173d2015-08-10 09:52:29 -0700285 if (mSwapBehavior == SwapBehavior::Preserved) {
John Reck3b202512014-06-23 13:13:08 -0700286 // Try again without dirty regions enabled
John Reck149173d2015-08-10 09:52:29 -0700287 ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...");
288 mSwapBehavior = SwapBehavior::Discard;
Peiyong Lin3bff1352018-12-11 07:56:07 -0800289 mEglConfig = load8BitsConfig(mEglDisplay, mSwapBehavior);
John Reck3b202512014-06-23 13:13:08 -0700290 } else {
John Reck149173d2015-08-10 09:52:29 -0700291 // Failed to get a valid config
sergeyv694d4992016-10-27 10:23:13 -0700292 LOG_ALWAYS_FATAL("Failed to choose config, error = %s", eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700293 }
294 }
Romain Guy26a2b972017-04-17 09:39:51 -0700295
Peiyong Lin3bff1352018-12-11 07:56:07 -0800296 // When we reach this point, we have a valid swap behavior
John Reckb36bfdd2020-07-23 13:47:49 -0700297 if (EglExtensions.pixelFormatFloat) {
298 mEglConfigF16 = loadFP16Config(mEglDisplay, mSwapBehavior);
299 if (mEglConfigF16 == EGL_NO_CONFIG_KHR) {
Rob Herring74883ab2017-11-29 09:26:31 -0600300 ALOGE("Device claims wide gamut support, cannot find matching config, error = %s",
John Reck0fa0cbc2019-04-05 16:57:46 -0700301 eglErrorString());
Rob Herring74883ab2017-11-29 09:26:31 -0600302 EglExtensions.pixelFormatFloat = false;
Romain Guy26a2b972017-04-17 09:39:51 -0700303 }
John Reckb36bfdd2020-07-23 13:47:49 -0700304 }
305 mEglConfig1010102 = load1010102Config(mEglDisplay, mSwapBehavior);
306 if (mEglConfig1010102 == EGL_NO_CONFIG_KHR) {
307 ALOGW("Failed to initialize 101010-2 format, error = %s",
308 eglErrorString());
Romain Guy26a2b972017-04-17 09:39:51 -0700309 }
John Reck3b202512014-06-23 13:13:08 -0700310}
311
312void EglManager::createContext() {
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200313 std::vector<EGLint> contextAttributes;
314 contextAttributes.reserve(5);
315 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
316 contextAttributes.push_back(GLES_VERSION);
317 if (Properties::contextPriority != 0 && EglExtensions.contextPriority) {
318 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
319 contextAttributes.push_back(Properties::contextPriority);
320 }
321 contextAttributes.push_back(EGL_NONE);
John Reck1bcacfd2017-11-03 10:12:19 -0700322 mEglContext = eglCreateContext(
323 mEglDisplay, EglExtensions.noConfigContext ? ((EGLConfig) nullptr) : mEglConfig,
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200324 EGL_NO_CONTEXT, contextAttributes.data());
John Reck1bcacfd2017-11-03 10:12:19 -0700325 LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, "Failed to create context, error = %s",
326 eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700327}
328
John Reckd7db4d72015-05-20 07:18:55 -0700329void EglManager::createPBufferSurface() {
John Reck3b202512014-06-23 13:13:08 -0700330 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
John Reck1bcacfd2017-11-03 10:12:19 -0700331 "usePBufferSurface() called on uninitialized GlobalContext!");
John Reck3b202512014-06-23 13:13:08 -0700332
John Reck1e510712018-04-23 08:15:03 -0700333 if (mPBufferSurface == EGL_NO_SURFACE && !EglExtensions.surfacelessContext) {
John Reck1bcacfd2017-11-03 10:12:19 -0700334 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
John Reck3b202512014-06-23 13:13:08 -0700335 mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs);
Roman Kiryanov60736132019-08-02 14:37:53 -0700336 LOG_ALWAYS_FATAL_IF(mPBufferSurface == EGL_NO_SURFACE,
337 "Failed to create a pixel buffer display=%p, "
338 "mEglConfig=%p, error=%s",
339 mEglDisplay, mEglConfig, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700340 }
John Reck3b202512014-06-23 13:13:08 -0700341}
342
Peiyong Lin3bff1352018-12-11 07:56:07 -0800343Result<EGLSurface, EGLint> EglManager::createSurface(EGLNativeWindowType window,
344 ColorMode colorMode,
Brian Osmane0cf5972019-01-23 10:41:20 -0500345 sk_sp<SkColorSpace> colorSpace) {
John Reck1e510712018-04-23 08:15:03 -0700346 LOG_ALWAYS_FATAL_IF(!hasEglContext(), "Not initialized");
Romain Guy253f2c22016-09-28 17:34:42 -0700347
John Reckb36bfdd2020-07-23 13:47:49 -0700348 if (!mHasWideColorGamutSupport || !EglExtensions.noConfigContext) {
349 colorMode = ColorMode::Default;
350 }
Romain Guy26a2b972017-04-17 09:39:51 -0700351
352 // The color space we want to use depends on whether linear blending is turned
353 // on and whether the app has requested wide color gamut rendering. When wide
354 // color gamut rendering is off, the app simply renders in the display's native
355 // color gamut.
356 //
357 // When wide gamut rendering is off:
358 // - Blending is done by default in gamma space, which requires using a
359 // linear EGL color space (the GPU uses the color values as is)
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700360 // - If linear blending is on, we must use the non-linear EGL color space
361 // (the GPU will perform sRGB to linear and linear to SRGB conversions
362 // before and after blending)
Romain Guy26a2b972017-04-17 09:39:51 -0700363 //
364 // When wide gamut rendering is on we cannot rely on the GPU performing
365 // linear blending for us. We use two different color spaces to tag the
366 // surface appropriately for SurfaceFlinger:
Peiyong Lin3bff1352018-12-11 07:56:07 -0800367 // - Gamma blending (default) requires the use of the non-linear color space
368 // - Linear blending requires the use of the linear color space
Romain Guy26a2b972017-04-17 09:39:51 -0700369
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700370 // Not all Android targets support the EGL_GL_COLORSPACE_KHR extension
Romain Guy26a2b972017-04-17 09:39:51 -0700371 // We insert to placeholders to set EGL_GL_COLORSPACE_KHR and its value.
372 // According to section 3.4.1 of the EGL specification, the attributes
373 // list is considered empty if the first entry is EGL_NONE
John Reck1bcacfd2017-11-03 10:12:19 -0700374 EGLint attribs[] = {EGL_NONE, EGL_NONE, EGL_NONE};
Romain Guy253f2c22016-09-28 17:34:42 -0700375
John Reckb36bfdd2020-07-23 13:47:49 -0700376 EGLConfig config = mEglConfig;
377 if (DeviceInfo::get()->getWideColorType() == kRGBA_F16_SkColorType) {
378 if (mEglConfigF16 == EGL_NO_CONFIG_KHR) {
379 colorMode = ColorMode::Default;
380 } else {
381 config = mEglConfigF16;
382 }
383 }
Romain Guy26a2b972017-04-17 09:39:51 -0700384 if (EglExtensions.glColorSpace) {
385 attribs[0] = EGL_GL_COLORSPACE_KHR;
John Reckb36bfdd2020-07-23 13:47:49 -0700386 switch (colorMode) {
387 case ColorMode::Default:
388 attribs[1] = EGL_GL_COLORSPACE_LINEAR_KHR;
389 break;
390 case ColorMode::WideColorGamut: {
391 skcms_Matrix3x3 colorGamut;
392 LOG_ALWAYS_FATAL_IF(!colorSpace->toXYZD50(&colorGamut),
393 "Could not get gamut matrix from color space");
394 if (memcmp(&colorGamut, &SkNamedGamut::kDisplayP3, sizeof(colorGamut)) == 0) {
395 attribs[1] = EGL_GL_COLORSPACE_DISPLAY_P3_PASSTHROUGH_EXT;
396 } else if (memcmp(&colorGamut, &SkNamedGamut::kSRGB, sizeof(colorGamut)) == 0) {
397 attribs[1] = EGL_GL_COLORSPACE_SCRGB_EXT;
398 } else if (memcmp(&colorGamut, &SkNamedGamut::kRec2020, sizeof(colorGamut)) == 0) {
399 attribs[1] = EGL_GL_COLORSPACE_BT2020_PQ_EXT;
400 } else {
401 LOG_ALWAYS_FATAL("Unreachable: unsupported wide color space.");
402 }
403 break;
Peiyong Lin3bff1352018-12-11 07:56:07 -0800404 }
John Reckb36bfdd2020-07-23 13:47:49 -0700405 case ColorMode::Hdr:
406 config = mEglConfigF16;
407 attribs[1] = EGL_GL_COLORSPACE_BT2020_PQ_EXT;
408 break;
409 case ColorMode::Hdr10:
410 config = mEglConfig1010102;
411 attribs[1] = EGL_GL_COLORSPACE_BT2020_PQ_EXT;
412 break;
Romain Guy26a2b972017-04-17 09:39:51 -0700413 }
Romain Guy26a2b972017-04-17 09:39:51 -0700414 }
415
John Reckb36bfdd2020-07-23 13:47:49 -0700416 EGLSurface surface = eglCreateWindowSurface(mEglDisplay, config, window, attribs);
John Reck8e539ca2018-11-15 15:21:29 -0800417 if (surface == EGL_NO_SURFACE) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700418 return Error<EGLint>{eglGetError()};
John Reck8e539ca2018-11-15 15:21:29 -0800419 }
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000420
421 if (mSwapBehavior != SwapBehavior::Preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700422 LOG_ALWAYS_FATAL_IF(eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
423 EGL_BUFFER_DESTROYED) == EGL_FALSE,
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000424 "Failed to set swap behavior to destroyed for window %p, eglErr = %s",
John Reck1bcacfd2017-11-03 10:12:19 -0700425 (void*)window, eglErrorString());
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000426 }
427
John Reck3b202512014-06-23 13:13:08 -0700428 return surface;
429}
430
431void EglManager::destroySurface(EGLSurface surface) {
432 if (isCurrent(surface)) {
433 makeCurrent(EGL_NO_SURFACE);
434 }
435 if (!eglDestroySurface(mEglDisplay, surface)) {
sergeyv694d4992016-10-27 10:23:13 -0700436 ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700437 }
438}
439
440void EglManager::destroy() {
441 if (mEglDisplay == EGL_NO_DISPLAY) return;
442
John Reck3b202512014-06-23 13:13:08 -0700443 eglDestroyContext(mEglDisplay, mEglContext);
John Reck1e510712018-04-23 08:15:03 -0700444 if (mPBufferSurface != EGL_NO_SURFACE) {
445 eglDestroySurface(mEglDisplay, mPBufferSurface);
446 }
John Reck3b202512014-06-23 13:13:08 -0700447 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
448 eglTerminate(mEglDisplay);
449 eglReleaseThread();
450
451 mEglDisplay = EGL_NO_DISPLAY;
452 mEglContext = EGL_NO_CONTEXT;
453 mPBufferSurface = EGL_NO_SURFACE;
454 mCurrentSurface = EGL_NO_SURFACE;
455}
456
John Reck1e510712018-04-23 08:15:03 -0700457bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut, bool force) {
458 if (!force && isCurrent(surface)) return false;
John Reck3b202512014-06-23 13:13:08 -0700459
460 if (surface == EGL_NO_SURFACE) {
John Reckd7db4d72015-05-20 07:18:55 -0700461 // Ensure we always have a valid surface & context
462 surface = mPBufferSurface;
463 }
464 if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) {
John Reckf2dcc2a2015-07-16 09:17:59 -0700465 if (errOut) {
466 *errOut = eglGetError();
John Reck1bcacfd2017-11-03 10:12:19 -0700467 ALOGW("Failed to make current on surface %p, error=%s", (void*)surface,
468 egl_error_str(*errOut));
John Reckf2dcc2a2015-07-16 09:17:59 -0700469 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700470 LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", (void*)surface,
471 eglErrorString());
John Reckf2dcc2a2015-07-16 09:17:59 -0700472 }
John Reck3b202512014-06-23 13:13:08 -0700473 }
474 mCurrentSurface = surface;
John Recka8963062017-06-14 10:47:50 -0700475 if (Properties::disableVsync) {
476 eglSwapInterval(mEglDisplay, 0);
477 }
John Reck3b202512014-06-23 13:13:08 -0700478 return true;
479}
480
John Reck149173d2015-08-10 09:52:29 -0700481EGLint EglManager::queryBufferAge(EGLSurface surface) {
482 switch (mSwapBehavior) {
John Reck1bcacfd2017-11-03 10:12:19 -0700483 case SwapBehavior::Discard:
484 return 0;
485 case SwapBehavior::Preserved:
486 return 1;
487 case SwapBehavior::BufferAge:
488 EGLint bufferAge;
489 eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge);
490 return bufferAge;
John Reck149173d2015-08-10 09:52:29 -0700491 }
492 return 0;
493}
494
495Frame EglManager::beginFrame(EGLSurface surface) {
John Reck1bcacfd2017-11-03 10:12:19 -0700496 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, "Tried to beginFrame on EGL_NO_SURFACE!");
John Reck3b202512014-06-23 13:13:08 -0700497 makeCurrent(surface);
John Reck149173d2015-08-10 09:52:29 -0700498 Frame frame;
499 frame.mSurface = surface;
500 eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth);
501 eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight);
502 frame.mBufferAge = queryBufferAge(surface);
John Reck3b202512014-06-23 13:13:08 -0700503 eglBeginFrame(mEglDisplay, surface);
John Reck149173d2015-08-10 09:52:29 -0700504 return frame;
John Reck3b202512014-06-23 13:13:08 -0700505}
506
John Reck149173d2015-08-10 09:52:29 -0700507void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) {
508#ifdef EGL_KHR_partial_update
509 if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) {
510 EGLint rects[4];
511 frame.map(dirty, rects);
512 if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) {
513 LOG_ALWAYS_FATAL("Failed to set damage region on surface %p, error=%s",
John Reck1bcacfd2017-11-03 10:12:19 -0700514 (void*)frame.mSurface, eglErrorString());
John Reck149173d2015-08-10 09:52:29 -0700515 }
516 }
517#endif
518}
519
John Reckc96955d2016-02-26 14:56:44 -0800520bool EglManager::damageRequiresSwap() {
521 return EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge;
522}
523
John Reck149173d2015-08-10 09:52:29 -0700524bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) {
John Reck682573c2015-10-30 10:37:35 -0700525 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
John Reck55156372015-01-21 07:46:37 -0800526 ATRACE_NAME("Finishing GPU work");
527 fence();
528 }
John Reck55156372015-01-21 07:46:37 -0800529
John Recka672f6b2015-10-22 09:53:26 -0700530 EGLint rects[4];
531 frame.map(screenDirty, rects);
John Reck1bcacfd2017-11-03 10:12:19 -0700532 eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects, screenDirty.isEmpty() ? 0 : 1);
John Reckd04794a2015-05-08 10:04:36 -0700533
John Reck3b202512014-06-23 13:13:08 -0700534 EGLint err = eglGetError();
John Reck2cdbc7d2014-09-17 16:06:36 -0700535 if (CC_LIKELY(err == EGL_SUCCESS)) {
536 return true;
537 }
John Reckc2547fa2015-10-26 13:52:52 -0700538 if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) {
John Reck2cdbc7d2014-09-17 16:06:36 -0700539 // For some reason our surface was destroyed out from under us
540 // This really shouldn't happen, but if it does we can recover easily
541 // by just not trying to use the surface anymore
John Reck1bcacfd2017-11-03 10:12:19 -0700542 ALOGW("swapBuffers encountered EGL error %d on %p, halting rendering...", err,
543 frame.mSurface);
John Reck2cdbc7d2014-09-17 16:06:36 -0700544 return false;
545 }
John Reck1bcacfd2017-11-03 10:12:19 -0700546 LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering", err, egl_error_str(err));
John Reck2cdbc7d2014-09-17 16:06:36 -0700547 // Impossible to hit this, but the compiler doesn't know that
548 return false;
John Reck3b202512014-06-23 13:13:08 -0700549}
550
John Reck55156372015-01-21 07:46:37 -0800551void EglManager::fence() {
552 EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL);
John Reck1bcacfd2017-11-03 10:12:19 -0700553 eglClientWaitSyncKHR(mEglDisplay, fence, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR);
John Reck55156372015-01-21 07:46:37 -0800554 eglDestroySyncKHR(mEglDisplay, fence);
555}
556
John Reck1125d1f2014-10-23 11:02:19 -0700557bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) {
John Reck149173d2015-08-10 09:52:29 -0700558 if (mSwapBehavior != SwapBehavior::Preserved) return false;
John Reck3b202512014-06-23 13:13:08 -0700559
John Reck149173d2015-08-10 09:52:29 -0700560 bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
John Reck1bcacfd2017-11-03 10:12:19 -0700561 preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED);
John Reck149173d2015-08-10 09:52:29 -0700562 if (!preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700563 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", (void*)surface,
564 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700565 // Maybe it's already set?
566 EGLint swapBehavior;
567 if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) {
568 preserved = (swapBehavior == EGL_BUFFER_PRESERVED);
569 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700570 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", (void*)surface,
571 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700572 }
John Reck3b202512014-06-23 13:13:08 -0700573 }
John Reck1125d1f2014-10-23 11:02:19 -0700574
575 return preserved;
John Reck3b202512014-06-23 13:13:08 -0700576}
577
Stan Ilievaaa9e832019-09-17 14:07:23 -0400578static status_t waitForeverOnFence(int fence, const char* logname) {
579 ATRACE_CALL();
580 if (fence == -1) {
581 return NO_ERROR;
582 }
583 constexpr int warningTimeout = 3000;
584 int err = sync_wait(fence, warningTimeout);
585 if (err < 0 && errno == ETIME) {
586 ALOGE("%s: fence %d didn't signal in %d ms", logname, fence, warningTimeout);
587 err = sync_wait(fence, -1);
588 }
589 return err < 0 ? -errno : status_t(NO_ERROR);
590}
591
592status_t EglManager::fenceWait(int fence) {
Stan Iliev564ca3e2018-09-04 22:00:00 +0000593 if (!hasEglContext()) {
594 ALOGE("EglManager::fenceWait: EGLDisplay not initialized");
595 return INVALID_OPERATION;
596 }
597
Alec Mouri680414e2020-01-28 09:22:33 -0800598 if (EglExtensions.waitSync && EglExtensions.nativeFenceSync) {
Stan Iliev564ca3e2018-09-04 22:00:00 +0000599 // Block GPU on the fence.
600 // Create an EGLSyncKHR from the current fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400601 int fenceFd = ::dup(fence);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000602 if (fenceFd == -1) {
603 ALOGE("EglManager::fenceWait: error dup'ing fence fd: %d", errno);
604 return -errno;
605 }
John Reck0fa0cbc2019-04-05 16:57:46 -0700606 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE};
607 EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000608 if (sync == EGL_NO_SYNC_KHR) {
609 close(fenceFd);
610 ALOGE("EglManager::fenceWait: error creating EGL fence: %#x", eglGetError());
611 return UNKNOWN_ERROR;
612 }
613
614 // XXX: The spec draft is inconsistent as to whether this should
615 // return an EGLint or void. Ignore the return value for now, as
616 // it's not strictly needed.
617 eglWaitSyncKHR(mEglDisplay, sync, 0);
618 EGLint eglErr = eglGetError();
619 eglDestroySyncKHR(mEglDisplay, sync);
620 if (eglErr != EGL_SUCCESS) {
621 ALOGE("EglManager::fenceWait: error waiting for EGL fence: %#x", eglErr);
622 return UNKNOWN_ERROR;
623 }
624 } else {
625 // Block CPU on the fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400626 status_t err = waitForeverOnFence(fence, "EglManager::fenceWait");
Stan Iliev564ca3e2018-09-04 22:00:00 +0000627 if (err != NO_ERROR) {
628 ALOGE("EglManager::fenceWait: error waiting for fence: %d", err);
629 return err;
630 }
631 }
632 return OK;
633}
634
Stan Ilievaaa9e832019-09-17 14:07:23 -0400635status_t EglManager::createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence, int* nativeFence) {
636 *nativeFence = -1;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000637 if (!hasEglContext()) {
638 ALOGE("EglManager::createReleaseFence: EGLDisplay not initialized");
639 return INVALID_OPERATION;
640 }
641
Alec Mouri680414e2020-01-28 09:22:33 -0800642 if (EglExtensions.nativeFenceSync) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700643 EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000644 if (sync == EGL_NO_SYNC_KHR) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700645 ALOGE("EglManager::createReleaseFence: error creating EGL fence: %#x", eglGetError());
Stan Iliev564ca3e2018-09-04 22:00:00 +0000646 return UNKNOWN_ERROR;
647 }
648 glFlush();
649 int fenceFd = eglDupNativeFenceFDANDROID(mEglDisplay, sync);
650 eglDestroySyncKHR(mEglDisplay, sync);
651 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
652 ALOGE("EglManager::createReleaseFence: error dup'ing native fence "
John Reck0fa0cbc2019-04-05 16:57:46 -0700653 "fd: %#x",
654 eglGetError());
Stan Iliev564ca3e2018-09-04 22:00:00 +0000655 return UNKNOWN_ERROR;
656 }
Stan Ilievaaa9e832019-09-17 14:07:23 -0400657 *nativeFence = fenceFd;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000658 *eglFence = EGL_NO_SYNC_KHR;
Alec Mouri680414e2020-01-28 09:22:33 -0800659 } else if (useFenceSync && EglExtensions.fenceSync) {
Stan Iliev564ca3e2018-09-04 22:00:00 +0000660 if (*eglFence != EGL_NO_SYNC_KHR) {
661 // There is already a fence for the current slot. We need to
662 // wait on that before replacing it with another fence to
663 // ensure that all outstanding buffer accesses have completed
664 // before the producer accesses it.
665 EGLint result = eglClientWaitSyncKHR(mEglDisplay, *eglFence, 0, 1000000000);
666 if (result == EGL_FALSE) {
667 ALOGE("EglManager::createReleaseFence: error waiting for previous fence: %#x",
John Reck0fa0cbc2019-04-05 16:57:46 -0700668 eglGetError());
Stan Iliev564ca3e2018-09-04 22:00:00 +0000669 return UNKNOWN_ERROR;
670 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
671 ALOGE("EglManager::createReleaseFence: timeout waiting for previous fence");
672 return TIMED_OUT;
673 }
674 eglDestroySyncKHR(mEglDisplay, *eglFence);
675 }
676
677 // Create a fence for the outstanding accesses in the current
678 // OpenGL ES context.
679 *eglFence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, nullptr);
680 if (*eglFence == EGL_NO_SYNC_KHR) {
681 ALOGE("EglManager::createReleaseFence: error creating fence: %#x", eglGetError());
682 return UNKNOWN_ERROR;
683 }
684 glFlush();
685 }
686 return OK;
687}
688
John Reck3b202512014-06-23 13:13:08 -0700689} /* namespace renderthread */
690} /* namespace uirenderer */
691} /* namespace android */