blob: 2904dfe76f40789ff6c06e4ebdb79a89d3cbfb76 [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
John Reck4bd8d8a2023-12-08 11:27:19 -050040static constexpr auto P3_XRB = static_cast<android_dataspace>(
41 ADATASPACE_STANDARD_DCI_P3 | ADATASPACE_TRANSFER_SRGB | ADATASPACE_RANGE_EXTENDED);
42
John Reck3b202512014-06-23 13:13:08 -070043namespace android {
44namespace uirenderer {
45namespace renderthread {
46
John Reck1bcacfd2017-11-03 10:12:19 -070047#define ERROR_CASE(x) \
48 case x: \
49 return #x;
John Reck3b202512014-06-23 13:13:08 -070050static const char* egl_error_str(EGLint error) {
51 switch (error) {
52 ERROR_CASE(EGL_SUCCESS)
53 ERROR_CASE(EGL_NOT_INITIALIZED)
54 ERROR_CASE(EGL_BAD_ACCESS)
55 ERROR_CASE(EGL_BAD_ALLOC)
56 ERROR_CASE(EGL_BAD_ATTRIBUTE)
57 ERROR_CASE(EGL_BAD_CONFIG)
58 ERROR_CASE(EGL_BAD_CONTEXT)
59 ERROR_CASE(EGL_BAD_CURRENT_SURFACE)
60 ERROR_CASE(EGL_BAD_DISPLAY)
61 ERROR_CASE(EGL_BAD_MATCH)
62 ERROR_CASE(EGL_BAD_NATIVE_PIXMAP)
63 ERROR_CASE(EGL_BAD_NATIVE_WINDOW)
64 ERROR_CASE(EGL_BAD_PARAMETER)
65 ERROR_CASE(EGL_BAD_SURFACE)
66 ERROR_CASE(EGL_CONTEXT_LOST)
John Reck1bcacfd2017-11-03 10:12:19 -070067 default:
68 return "Unknown error";
John Reck3b202512014-06-23 13:13:08 -070069 }
70}
sergeyv694d4992016-10-27 10:23:13 -070071const char* EglManager::eglErrorString() {
John Reck3b202512014-06-23 13:13:08 -070072 return egl_error_str(eglGetError());
73}
74
John Reck149173d2015-08-10 09:52:29 -070075static struct {
76 bool bufferAge = false;
77 bool setDamage = false;
Romain Guy26a2b972017-04-17 09:39:51 -070078 bool noConfigContext = false;
79 bool pixelFormatFloat = false;
80 bool glColorSpace = false;
Romain Guy26b6a642017-07-11 09:48:28 -070081 bool scRGB = false;
Peiyong Lin1f6aa122018-09-10 16:28:08 -070082 bool displayP3 = false;
John Reckb36bfdd2020-07-23 13:47:49 -070083 bool hdr = false;
Jorim Jaggi767e25e2018-04-04 23:07:35 +020084 bool contextPriority = false;
John Reck1e510712018-04-23 08:15:03 -070085 bool surfacelessContext = false;
Alec Mouri680414e2020-01-28 09:22:33 -080086 bool nativeFenceSync = false;
87 bool fenceSync = false;
88 bool waitSync = false;
John Reck149173d2015-08-10 09:52:29 -070089} EglExtensions;
90
John Reck1e510712018-04-23 08:15:03 -070091EglManager::EglManager()
92 : mEglDisplay(EGL_NO_DISPLAY)
Chris Craikd41c4d82015-01-05 15:51:13 -080093 , mEglConfig(nullptr)
John Reckb36bfdd2020-07-23 13:47:49 -070094 , mEglConfigF16(nullptr)
95 , mEglConfig1010102(nullptr)
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -050096 , mEglConfigA8(nullptr)
John Reck3b202512014-06-23 13:13:08 -070097 , mEglContext(EGL_NO_CONTEXT)
98 , mPBufferSurface(EGL_NO_SURFACE)
Peiyong Lin3bff1352018-12-11 07:56:07 -080099 , mCurrentSurface(EGL_NO_SURFACE)
100 , mHasWideColorGamutSupport(false) {}
John Reck3b202512014-06-23 13:13:08 -0700101
John Reck1e510712018-04-23 08:15:03 -0700102EglManager::~EglManager() {
John Reck6104cea2019-01-10 14:37:17 -0800103 if (hasEglContext()) {
104 ALOGW("~EglManager() leaked an EGL context");
105 }
John Reck1e510712018-04-23 08:15:03 -0700106}
107
John Reck3b202512014-06-23 13:13:08 -0700108void EglManager::initialize() {
109 if (hasEglContext()) return;
110
John Reckfbc8df02014-11-14 16:18:41 -0800111 ATRACE_NAME("Creating EGLContext");
112
John Reck3b202512014-06-23 13:13:08 -0700113 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
John Reck1bcacfd2017-11-03 10:12:19 -0700114 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, "Failed to get EGL_DEFAULT_DISPLAY! err=%s",
115 eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700116
117 EGLint major, minor;
118 LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE,
John Reck1bcacfd2017-11-03 10:12:19 -0700119 "Failed to initialize display %p! err=%s", mEglDisplay, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700120
John Reck848f6512018-12-03 13:26:43 -0800121 ALOGV("Initialized EGL, version %d.%d", (int)major, (int)minor);
John Reck3b202512014-06-23 13:13:08 -0700122
John Reck149173d2015-08-10 09:52:29 -0700123 initExtensions();
Season Li13d1b4a2015-07-29 17:16:19 -0700124
John Reck149173d2015-08-10 09:52:29 -0700125 // Now that extensions are loaded, pick a swap behavior
126 if (Properties::enablePartialUpdates) {
Matt Sarettb77c94a2016-12-07 12:22:37 -0500127 // An Adreno driver bug is causing rendering problems for SkiaGL with
128 // buffer age swap behavior (b/31957043). To temporarily workaround,
129 // we will use preserved swap behavior.
Derek Sollenbergerb5a12bd2017-06-14 15:23:19 -0400130 if (Properties::useBufferAge && EglExtensions.bufferAge) {
John Reck149173d2015-08-10 09:52:29 -0700131 mSwapBehavior = SwapBehavior::BufferAge;
132 } else {
133 mSwapBehavior = SwapBehavior::Preserved;
134 }
135 }
136
Romain Guy26a2b972017-04-17 09:39:51 -0700137 loadConfigs();
John Reck3b202512014-06-23 13:13:08 -0700138 createContext();
John Reckd7db4d72015-05-20 07:18:55 -0700139 createPBufferSurface();
John Reck1e510712018-04-23 08:15:03 -0700140 makeCurrent(mPBufferSurface, nullptr, /* force */ true);
Peiyong Lin3bff1352018-12-11 07:56:07 -0800141
Brian Osmane0cf5972019-01-23 10:41:20 -0500142 skcms_Matrix3x3 wideColorGamut;
143 LOG_ALWAYS_FATAL_IF(!DeviceInfo::get()->getWideColorSpace()->toXYZD50(&wideColorGamut),
144 "Could not get gamut matrix from wideColorSpace");
Peiyong Lin3bff1352018-12-11 07:56:07 -0800145 bool hasWideColorSpaceExtension = false;
Mike Reed7ac1af32020-05-26 10:50:21 -0400146 if (memcmp(&wideColorGamut, &SkNamedGamut::kDisplayP3, sizeof(wideColorGamut)) == 0) {
Peiyong Lin3bff1352018-12-11 07:56:07 -0800147 hasWideColorSpaceExtension = EglExtensions.displayP3;
Brian Osmane0cf5972019-01-23 10:41:20 -0500148 } else if (memcmp(&wideColorGamut, &SkNamedGamut::kSRGB, sizeof(wideColorGamut)) == 0) {
Peiyong Lin3bff1352018-12-11 07:56:07 -0800149 hasWideColorSpaceExtension = EglExtensions.scRGB;
150 } else {
151 LOG_ALWAYS_FATAL("Unsupported wide color space.");
152 }
John Reckb36bfdd2020-07-23 13:47:49 -0700153 mHasWideColorGamutSupport = EglExtensions.glColorSpace && hasWideColorSpaceExtension;
Nader Jawad4a6b60a2021-09-20 21:22:50 -0700154
155 auto* vendor = reinterpret_cast<const char*>(glGetString(GL_VENDOR));
Nader Jawad086645d2021-09-24 13:42:47 -0700156 auto* version = reinterpret_cast<const char*>(glGetString(GL_VERSION));
157 Properties::enableRenderEffectCache = supportsRenderEffectCache(
158 vendor, version);
159 ALOGV("RenderEffectCache supported %d on driver version %s",
160 Properties::enableRenderEffectCache, version);
Peiyong Lin3bff1352018-12-11 07:56:07 -0800161}
162
163EGLConfig EglManager::load8BitsConfig(EGLDisplay display, EglManager::SwapBehavior swapBehavior) {
164 EGLint eglSwapBehavior =
165 (swapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
166 EGLint attribs[] = {EGL_RENDERABLE_TYPE,
167 EGL_OPENGL_ES2_BIT,
168 EGL_RED_SIZE,
169 8,
170 EGL_GREEN_SIZE,
171 8,
172 EGL_BLUE_SIZE,
173 8,
174 EGL_ALPHA_SIZE,
175 8,
176 EGL_DEPTH_SIZE,
177 0,
178 EGL_CONFIG_CAVEAT,
179 EGL_NONE,
180 EGL_STENCIL_SIZE,
181 STENCIL_BUFFER_SIZE,
182 EGL_SURFACE_TYPE,
183 EGL_WINDOW_BIT | eglSwapBehavior,
184 EGL_NONE};
185 EGLConfig config = EGL_NO_CONFIG_KHR;
186 EGLint numConfigs = 1;
John Reck0fa0cbc2019-04-05 16:57:46 -0700187 if (!eglChooseConfig(display, attribs, &config, numConfigs, &numConfigs) || numConfigs != 1) {
Peiyong Lin3bff1352018-12-11 07:56:07 -0800188 return EGL_NO_CONFIG_KHR;
189 }
190 return config;
191}
192
John Reckb36bfdd2020-07-23 13:47:49 -0700193EGLConfig EglManager::load1010102Config(EGLDisplay display, SwapBehavior swapBehavior) {
194 EGLint eglSwapBehavior =
195 (swapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
196 // If we reached this point, we have a valid swap behavior
197 EGLint attribs[] = {EGL_RENDERABLE_TYPE,
198 EGL_OPENGL_ES2_BIT,
199 EGL_RED_SIZE,
200 10,
201 EGL_GREEN_SIZE,
202 10,
203 EGL_BLUE_SIZE,
204 10,
205 EGL_ALPHA_SIZE,
206 2,
207 EGL_DEPTH_SIZE,
208 0,
209 EGL_STENCIL_SIZE,
210 STENCIL_BUFFER_SIZE,
211 EGL_SURFACE_TYPE,
212 EGL_WINDOW_BIT | eglSwapBehavior,
213 EGL_NONE};
214 EGLConfig config = EGL_NO_CONFIG_KHR;
215 EGLint numConfigs = 1;
216 if (!eglChooseConfig(display, attribs, &config, numConfigs, &numConfigs) || numConfigs != 1) {
217 return EGL_NO_CONFIG_KHR;
218 }
219 return config;
220}
221
Peiyong Lin3bff1352018-12-11 07:56:07 -0800222EGLConfig EglManager::loadFP16Config(EGLDisplay display, SwapBehavior swapBehavior) {
223 EGLint eglSwapBehavior =
224 (swapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
225 // If we reached this point, we have a valid swap behavior
226 EGLint attribs[] = {EGL_RENDERABLE_TYPE,
227 EGL_OPENGL_ES2_BIT,
228 EGL_COLOR_COMPONENT_TYPE_EXT,
229 EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT,
230 EGL_RED_SIZE,
231 16,
232 EGL_GREEN_SIZE,
233 16,
234 EGL_BLUE_SIZE,
235 16,
236 EGL_ALPHA_SIZE,
237 16,
238 EGL_DEPTH_SIZE,
239 0,
240 EGL_STENCIL_SIZE,
241 STENCIL_BUFFER_SIZE,
242 EGL_SURFACE_TYPE,
243 EGL_WINDOW_BIT | eglSwapBehavior,
244 EGL_NONE};
245 EGLConfig config = EGL_NO_CONFIG_KHR;
246 EGLint numConfigs = 1;
John Reck0fa0cbc2019-04-05 16:57:46 -0700247 if (!eglChooseConfig(display, attribs, &config, numConfigs, &numConfigs) || numConfigs != 1) {
Peiyong Lin3bff1352018-12-11 07:56:07 -0800248 return EGL_NO_CONFIG_KHR;
249 }
250 return config;
John Reck3b202512014-06-23 13:13:08 -0700251}
252
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -0500253EGLConfig EglManager::loadA8Config(EGLDisplay display, EglManager::SwapBehavior swapBehavior) {
254 EGLint eglSwapBehavior =
255 (swapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
256 EGLint attribs[] = {EGL_RENDERABLE_TYPE,
257 EGL_OPENGL_ES2_BIT,
258 EGL_RED_SIZE,
259 8,
260 EGL_GREEN_SIZE,
261 0,
262 EGL_BLUE_SIZE,
263 0,
264 EGL_ALPHA_SIZE,
265 0,
266 EGL_DEPTH_SIZE,
267 0,
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -0500268 EGL_SURFACE_TYPE,
269 EGL_WINDOW_BIT | eglSwapBehavior,
270 EGL_NONE};
271 EGLint numConfigs = 1;
272 if (!eglChooseConfig(display, attribs, nullptr, numConfigs, &numConfigs)) {
273 return EGL_NO_CONFIG_KHR;
274 }
275
276 std::vector<EGLConfig> configs(numConfigs, EGL_NO_CONFIG_KHR);
277 if (!eglChooseConfig(display, attribs, configs.data(), numConfigs, &numConfigs)) {
278 return EGL_NO_CONFIG_KHR;
279 }
280
281 // The component sizes passed to eglChooseConfig are minimums, so configs
282 // contains entries that exceed them. Choose one that matches the sizes
283 // exactly.
284 for (EGLConfig config : configs) {
285 EGLint r{0}, g{0}, b{0}, a{0};
286 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
287 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
288 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
289 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
290 if (8 == r && 0 == g && 0 == b && 0 == a) {
291 return config;
292 }
293 }
294 return EGL_NO_CONFIG_KHR;
295}
296
John Reck149173d2015-08-10 09:52:29 -0700297void EglManager::initExtensions() {
John Reck1bcacfd2017-11-03 10:12:19 -0700298 auto extensions = StringUtils::split(eglQueryString(mEglDisplay, EGL_EXTENSIONS));
Romain Guy26a2b972017-04-17 09:39:51 -0700299
John Reck8733bff2016-09-27 14:45:28 -0700300 // For our purposes we don't care if EGL_BUFFER_AGE is a result of
301 // EGL_EXT_buffer_age or EGL_KHR_partial_update as our usage is covered
302 // under EGL_KHR_partial_update and we don't need the expanded scope
303 // that EGL_EXT_buffer_age provides.
John Reck1bcacfd2017-11-03 10:12:19 -0700304 EglExtensions.bufferAge =
305 extensions.has("EGL_EXT_buffer_age") || extensions.has("EGL_KHR_partial_update");
Chris Craik6e6646c2015-09-14 15:54:12 -0700306 EglExtensions.setDamage = extensions.has("EGL_KHR_partial_update");
John Reck708b6682015-10-22 13:11:00 -0700307 LOG_ALWAYS_FATAL_IF(!extensions.has("EGL_KHR_swap_buffers_with_damage"),
John Reck1bcacfd2017-11-03 10:12:19 -0700308 "Missing required extension EGL_KHR_swap_buffers_with_damage");
Romain Guy26a2b972017-04-17 09:39:51 -0700309
310 EglExtensions.glColorSpace = extensions.has("EGL_KHR_gl_colorspace");
311 EglExtensions.noConfigContext = extensions.has("EGL_KHR_no_config_context");
312 EglExtensions.pixelFormatFloat = extensions.has("EGL_EXT_pixel_format_float");
Romain Guy26b6a642017-07-11 09:48:28 -0700313 EglExtensions.scRGB = extensions.has("EGL_EXT_gl_colorspace_scrgb");
Peiyong Lin3bff1352018-12-11 07:56:07 -0800314 EglExtensions.displayP3 = extensions.has("EGL_EXT_gl_colorspace_display_p3_passthrough");
John Reckb36bfdd2020-07-23 13:47:49 -0700315 EglExtensions.hdr = extensions.has("EGL_EXT_gl_colorspace_bt2020_pq");
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200316 EglExtensions.contextPriority = extensions.has("EGL_IMG_context_priority");
John Reck1e510712018-04-23 08:15:03 -0700317 EglExtensions.surfacelessContext = extensions.has("EGL_KHR_surfaceless_context");
Alec Mouri680414e2020-01-28 09:22:33 -0800318 EglExtensions.fenceSync = extensions.has("EGL_KHR_fence_sync");
319 EglExtensions.waitSync = extensions.has("EGL_KHR_wait_sync");
Yiwei Zhang7f430132020-08-12 15:07:51 -0700320 EglExtensions.nativeFenceSync = extensions.has("EGL_ANDROID_native_fence_sync");
John Reck149173d2015-08-10 09:52:29 -0700321}
322
John Reck3b202512014-06-23 13:13:08 -0700323bool EglManager::hasEglContext() {
324 return mEglDisplay != EGL_NO_DISPLAY;
325}
326
Romain Guy26a2b972017-04-17 09:39:51 -0700327void EglManager::loadConfigs() {
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700328 // Note: The default pixel format is RGBA_8888, when other formats are
329 // available, we should check the target pixel format and configure the
330 // attributes list properly.
Peiyong Lin3bff1352018-12-11 07:56:07 -0800331 mEglConfig = load8BitsConfig(mEglDisplay, mSwapBehavior);
332 if (mEglConfig == EGL_NO_CONFIG_KHR) {
John Reck149173d2015-08-10 09:52:29 -0700333 if (mSwapBehavior == SwapBehavior::Preserved) {
John Reck3b202512014-06-23 13:13:08 -0700334 // Try again without dirty regions enabled
John Reck149173d2015-08-10 09:52:29 -0700335 ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...");
336 mSwapBehavior = SwapBehavior::Discard;
Peiyong Lin3bff1352018-12-11 07:56:07 -0800337 mEglConfig = load8BitsConfig(mEglDisplay, mSwapBehavior);
John Reck3b202512014-06-23 13:13:08 -0700338 } else {
John Reck149173d2015-08-10 09:52:29 -0700339 // Failed to get a valid config
sergeyv694d4992016-10-27 10:23:13 -0700340 LOG_ALWAYS_FATAL("Failed to choose config, error = %s", eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700341 }
342 }
Romain Guy26a2b972017-04-17 09:39:51 -0700343
Peiyong Lin3bff1352018-12-11 07:56:07 -0800344 // When we reach this point, we have a valid swap behavior
John Reckb36bfdd2020-07-23 13:47:49 -0700345 if (EglExtensions.pixelFormatFloat) {
346 mEglConfigF16 = loadFP16Config(mEglDisplay, mSwapBehavior);
347 if (mEglConfigF16 == EGL_NO_CONFIG_KHR) {
Rob Herring74883ab2017-11-29 09:26:31 -0600348 ALOGE("Device claims wide gamut support, cannot find matching config, error = %s",
John Reck0fa0cbc2019-04-05 16:57:46 -0700349 eglErrorString());
Rob Herring74883ab2017-11-29 09:26:31 -0600350 EglExtensions.pixelFormatFloat = false;
Romain Guy26a2b972017-04-17 09:39:51 -0700351 }
John Reckb36bfdd2020-07-23 13:47:49 -0700352 }
353 mEglConfig1010102 = load1010102Config(mEglDisplay, mSwapBehavior);
354 if (mEglConfig1010102 == EGL_NO_CONFIG_KHR) {
355 ALOGW("Failed to initialize 101010-2 format, error = %s",
356 eglErrorString());
Romain Guy26a2b972017-04-17 09:39:51 -0700357 }
John Reck3b202512014-06-23 13:13:08 -0700358}
359
360void EglManager::createContext() {
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200361 std::vector<EGLint> contextAttributes;
362 contextAttributes.reserve(5);
363 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
364 contextAttributes.push_back(GLES_VERSION);
365 if (Properties::contextPriority != 0 && EglExtensions.contextPriority) {
366 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
367 contextAttributes.push_back(Properties::contextPriority);
368 }
369 contextAttributes.push_back(EGL_NONE);
John Reck1bcacfd2017-11-03 10:12:19 -0700370 mEglContext = eglCreateContext(
371 mEglDisplay, EglExtensions.noConfigContext ? ((EGLConfig) nullptr) : mEglConfig,
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200372 EGL_NO_CONTEXT, contextAttributes.data());
John Reck1bcacfd2017-11-03 10:12:19 -0700373 LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, "Failed to create context, error = %s",
374 eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700375}
376
John Reckd7db4d72015-05-20 07:18:55 -0700377void EglManager::createPBufferSurface() {
John Reck3b202512014-06-23 13:13:08 -0700378 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
John Reck1bcacfd2017-11-03 10:12:19 -0700379 "usePBufferSurface() called on uninitialized GlobalContext!");
John Reck3b202512014-06-23 13:13:08 -0700380
John Reck1e510712018-04-23 08:15:03 -0700381 if (mPBufferSurface == EGL_NO_SURFACE && !EglExtensions.surfacelessContext) {
John Reck1bcacfd2017-11-03 10:12:19 -0700382 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
John Reck3b202512014-06-23 13:13:08 -0700383 mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs);
Roman Kiryanov60736132019-08-02 14:37:53 -0700384 LOG_ALWAYS_FATAL_IF(mPBufferSurface == EGL_NO_SURFACE,
385 "Failed to create a pixel buffer display=%p, "
386 "mEglConfig=%p, error=%s",
387 mEglDisplay, mEglConfig, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700388 }
John Reck3b202512014-06-23 13:13:08 -0700389}
390
Peiyong Lin3bff1352018-12-11 07:56:07 -0800391Result<EGLSurface, EGLint> EglManager::createSurface(EGLNativeWindowType window,
392 ColorMode colorMode,
Brian Osmane0cf5972019-01-23 10:41:20 -0500393 sk_sp<SkColorSpace> colorSpace) {
John Reck1e510712018-04-23 08:15:03 -0700394 LOG_ALWAYS_FATAL_IF(!hasEglContext(), "Not initialized");
Romain Guy253f2c22016-09-28 17:34:42 -0700395
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -0500396 if (!EglExtensions.noConfigContext) {
397 // The caller shouldn't use A8 if we cannot switch modes.
398 LOG_ALWAYS_FATAL_IF(colorMode == ColorMode::A8,
399 "Cannot use A8 without EGL_KHR_no_config_context!");
400
401 // Cannot switch modes without EGL_KHR_no_config_context.
John Reckb36bfdd2020-07-23 13:47:49 -0700402 colorMode = ColorMode::Default;
403 }
Romain Guy26a2b972017-04-17 09:39:51 -0700404 // The color space we want to use depends on whether linear blending is turned
405 // on and whether the app has requested wide color gamut rendering. When wide
406 // color gamut rendering is off, the app simply renders in the display's native
407 // color gamut.
408 //
409 // When wide gamut rendering is off:
410 // - Blending is done by default in gamma space, which requires using a
411 // linear EGL color space (the GPU uses the color values as is)
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700412 // - If linear blending is on, we must use the non-linear EGL color space
413 // (the GPU will perform sRGB to linear and linear to SRGB conversions
414 // before and after blending)
Romain Guy26a2b972017-04-17 09:39:51 -0700415 //
416 // When wide gamut rendering is on we cannot rely on the GPU performing
417 // linear blending for us. We use two different color spaces to tag the
418 // surface appropriately for SurfaceFlinger:
Peiyong Lin3bff1352018-12-11 07:56:07 -0800419 // - Gamma blending (default) requires the use of the non-linear color space
420 // - Linear blending requires the use of the linear color space
Romain Guy26a2b972017-04-17 09:39:51 -0700421
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700422 // Not all Android targets support the EGL_GL_COLORSPACE_KHR extension
Romain Guy26a2b972017-04-17 09:39:51 -0700423 // We insert to placeholders to set EGL_GL_COLORSPACE_KHR and its value.
424 // According to section 3.4.1 of the EGL specification, the attributes
425 // list is considered empty if the first entry is EGL_NONE
John Reck1bcacfd2017-11-03 10:12:19 -0700426 EGLint attribs[] = {EGL_NONE, EGL_NONE, EGL_NONE};
Romain Guy253f2c22016-09-28 17:34:42 -0700427
John Reckb36bfdd2020-07-23 13:47:49 -0700428 EGLConfig config = mEglConfig;
John Reck52d45952023-04-05 15:36:15 -0400429 bool overrideWindowDataSpaceForHdr = false;
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -0500430 if (colorMode == ColorMode::A8) {
431 // A8 doesn't use a color space
Leon Scroggins IIIe040b292021-12-14 17:22:25 -0500432 if (!mEglConfigA8) {
433 mEglConfigA8 = loadA8Config(mEglDisplay, mSwapBehavior);
434 LOG_ALWAYS_FATAL_IF(!mEglConfigA8,
435 "Requested ColorMode::A8, but EGL lacks support! error = %s",
436 eglErrorString());
437 }
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -0500438 config = mEglConfigA8;
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -0500439 } else {
440 if (!mHasWideColorGamutSupport) {
John Reckb36bfdd2020-07-23 13:47:49 -0700441 colorMode = ColorMode::Default;
John Reckb36bfdd2020-07-23 13:47:49 -0700442 }
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -0500443
Alec Mouri22ab7f32023-09-06 02:11:56 +0000444 // TODO: maybe we want to get rid of the WCG check if overlay properties just works?
445 const bool canUseFp16 = DeviceInfo::get()->isSupportFp16ForHdr() ||
446 DeviceInfo::get()->getWideColorType() == kRGBA_F16_SkColorType;
447
448 if (canUseFp16) {
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -0500449 if (mEglConfigF16 == EGL_NO_CONFIG_KHR) {
450 colorMode = ColorMode::Default;
451 } else {
John Reckb36bfdd2020-07-23 13:47:49 -0700452 config = mEglConfigF16;
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -0500453 }
454 }
Alec Mouri22ab7f32023-09-06 02:11:56 +0000455
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -0500456 if (EglExtensions.glColorSpace) {
457 attribs[0] = EGL_GL_COLORSPACE_KHR;
458 switch (colorMode) {
459 case ColorMode::Default:
460 attribs[1] = EGL_GL_COLORSPACE_LINEAR_KHR;
461 break;
Alec Mouri22ab7f32023-09-06 02:11:56 +0000462 case ColorMode::Hdr:
463 if (canUseFp16) {
464 attribs[1] = EGL_GL_COLORSPACE_SCRGB_EXT;
465 break;
466 // No fp16 support so fallthrough to HDR10
467 }
John Reck52d45952023-04-05 15:36:15 -0400468 // We don't have an EGL colorspace for extended range P3 that's used for HDR
469 // So override it after configuring the EGL context
John Reck0b3f3312023-01-31 16:21:28 -0500470 case ColorMode::Hdr10:
John Reck52d45952023-04-05 15:36:15 -0400471 overrideWindowDataSpaceForHdr = true;
472 attribs[1] = EGL_GL_COLORSPACE_DISPLAY_P3_PASSTHROUGH_EXT;
473 break;
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -0500474 case ColorMode::WideColorGamut: {
475 skcms_Matrix3x3 colorGamut;
476 LOG_ALWAYS_FATAL_IF(!colorSpace->toXYZD50(&colorGamut),
477 "Could not get gamut matrix from color space");
478 if (memcmp(&colorGamut, &SkNamedGamut::kDisplayP3, sizeof(colorGamut)) == 0) {
479 attribs[1] = EGL_GL_COLORSPACE_DISPLAY_P3_PASSTHROUGH_EXT;
480 } else if (memcmp(&colorGamut, &SkNamedGamut::kSRGB, sizeof(colorGamut)) == 0) {
481 attribs[1] = EGL_GL_COLORSPACE_SCRGB_EXT;
482 } else if (memcmp(&colorGamut, &SkNamedGamut::kRec2020, sizeof(colorGamut)) ==
483 0) {
484 attribs[1] = EGL_GL_COLORSPACE_BT2020_PQ_EXT;
485 } else {
486 LOG_ALWAYS_FATAL("Unreachable: unsupported wide color space.");
487 }
488 break;
489 }
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -0500490 case ColorMode::A8:
491 LOG_ALWAYS_FATAL("Unreachable: A8 doesn't use a color space");
492 break;
493 }
Romain Guy26a2b972017-04-17 09:39:51 -0700494 }
Romain Guy26a2b972017-04-17 09:39:51 -0700495 }
496
John Reckb36bfdd2020-07-23 13:47:49 -0700497 EGLSurface surface = eglCreateWindowSurface(mEglDisplay, config, window, attribs);
John Reck8e539ca2018-11-15 15:21:29 -0800498 if (surface == EGL_NO_SURFACE) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700499 return Error<EGLint>{eglGetError()};
John Reck8e539ca2018-11-15 15:21:29 -0800500 }
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000501
502 if (mSwapBehavior != SwapBehavior::Preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700503 LOG_ALWAYS_FATAL_IF(eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
504 EGL_BUFFER_DESTROYED) == EGL_FALSE,
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000505 "Failed to set swap behavior to destroyed for window %p, eglErr = %s",
John Reck1bcacfd2017-11-03 10:12:19 -0700506 (void*)window, eglErrorString());
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000507 }
508
John Reck52d45952023-04-05 15:36:15 -0400509 if (overrideWindowDataSpaceForHdr) {
510 // This relies on knowing that EGL will not re-set the dataspace after the call to
511 // eglCreateWindowSurface. Since the handling of the colorspace extension is largely
512 // implemented in libEGL in the platform, we can safely assume this is the case
John Reck4bd8d8a2023-12-08 11:27:19 -0500513 int32_t err = ANativeWindow_setBuffersDataSpace(window, P3_XRB);
John Reck52d45952023-04-05 15:36:15 -0400514 LOG_ALWAYS_FATAL_IF(err, "Failed to ANativeWindow_setBuffersDataSpace %d", err);
515 }
516
John Reck3b202512014-06-23 13:13:08 -0700517 return surface;
518}
519
520void EglManager::destroySurface(EGLSurface surface) {
521 if (isCurrent(surface)) {
522 makeCurrent(EGL_NO_SURFACE);
523 }
524 if (!eglDestroySurface(mEglDisplay, surface)) {
sergeyv694d4992016-10-27 10:23:13 -0700525 ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700526 }
527}
528
529void EglManager::destroy() {
530 if (mEglDisplay == EGL_NO_DISPLAY) return;
531
John Reck3b202512014-06-23 13:13:08 -0700532 eglDestroyContext(mEglDisplay, mEglContext);
John Reck1e510712018-04-23 08:15:03 -0700533 if (mPBufferSurface != EGL_NO_SURFACE) {
534 eglDestroySurface(mEglDisplay, mPBufferSurface);
535 }
John Reck3b202512014-06-23 13:13:08 -0700536 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
537 eglTerminate(mEglDisplay);
538 eglReleaseThread();
539
540 mEglDisplay = EGL_NO_DISPLAY;
541 mEglContext = EGL_NO_CONTEXT;
542 mPBufferSurface = EGL_NO_SURFACE;
543 mCurrentSurface = EGL_NO_SURFACE;
544}
545
John Reck1e510712018-04-23 08:15:03 -0700546bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut, bool force) {
547 if (!force && isCurrent(surface)) return false;
John Reck3b202512014-06-23 13:13:08 -0700548
549 if (surface == EGL_NO_SURFACE) {
John Reckd7db4d72015-05-20 07:18:55 -0700550 // Ensure we always have a valid surface & context
551 surface = mPBufferSurface;
552 }
553 if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) {
John Reckf2dcc2a2015-07-16 09:17:59 -0700554 if (errOut) {
555 *errOut = eglGetError();
John Reck1bcacfd2017-11-03 10:12:19 -0700556 ALOGW("Failed to make current on surface %p, error=%s", (void*)surface,
557 egl_error_str(*errOut));
John Reckf2dcc2a2015-07-16 09:17:59 -0700558 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700559 LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", (void*)surface,
560 eglErrorString());
John Reckf2dcc2a2015-07-16 09:17:59 -0700561 }
John Reck3b202512014-06-23 13:13:08 -0700562 }
563 mCurrentSurface = surface;
John Recka8963062017-06-14 10:47:50 -0700564 if (Properties::disableVsync) {
565 eglSwapInterval(mEglDisplay, 0);
566 }
John Reck3b202512014-06-23 13:13:08 -0700567 return true;
568}
569
John Reck149173d2015-08-10 09:52:29 -0700570EGLint EglManager::queryBufferAge(EGLSurface surface) {
571 switch (mSwapBehavior) {
John Reck1bcacfd2017-11-03 10:12:19 -0700572 case SwapBehavior::Discard:
573 return 0;
574 case SwapBehavior::Preserved:
575 return 1;
576 case SwapBehavior::BufferAge:
577 EGLint bufferAge;
578 eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge);
579 return bufferAge;
John Reck149173d2015-08-10 09:52:29 -0700580 }
581 return 0;
582}
583
584Frame EglManager::beginFrame(EGLSurface surface) {
John Reck1bcacfd2017-11-03 10:12:19 -0700585 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, "Tried to beginFrame on EGL_NO_SURFACE!");
John Reck3b202512014-06-23 13:13:08 -0700586 makeCurrent(surface);
John Reck149173d2015-08-10 09:52:29 -0700587 Frame frame;
588 frame.mSurface = surface;
589 eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth);
590 eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight);
591 frame.mBufferAge = queryBufferAge(surface);
John Reck3b202512014-06-23 13:13:08 -0700592 eglBeginFrame(mEglDisplay, surface);
John Reck149173d2015-08-10 09:52:29 -0700593 return frame;
John Reck3b202512014-06-23 13:13:08 -0700594}
595
John Reck149173d2015-08-10 09:52:29 -0700596void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) {
597#ifdef EGL_KHR_partial_update
598 if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) {
599 EGLint rects[4];
600 frame.map(dirty, rects);
601 if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) {
602 LOG_ALWAYS_FATAL("Failed to set damage region on surface %p, error=%s",
John Reck1bcacfd2017-11-03 10:12:19 -0700603 (void*)frame.mSurface, eglErrorString());
John Reck149173d2015-08-10 09:52:29 -0700604 }
605 }
606#endif
607}
608
John Reckc96955d2016-02-26 14:56:44 -0800609bool EglManager::damageRequiresSwap() {
610 return EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge;
611}
612
John Reck149173d2015-08-10 09:52:29 -0700613bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) {
John Reck682573c2015-10-30 10:37:35 -0700614 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
John Reck55156372015-01-21 07:46:37 -0800615 ATRACE_NAME("Finishing GPU work");
616 fence();
617 }
John Reck55156372015-01-21 07:46:37 -0800618
John Recka672f6b2015-10-22 09:53:26 -0700619 EGLint rects[4];
620 frame.map(screenDirty, rects);
John Reck1bcacfd2017-11-03 10:12:19 -0700621 eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects, screenDirty.isEmpty() ? 0 : 1);
John Reckd04794a2015-05-08 10:04:36 -0700622
John Reck3b202512014-06-23 13:13:08 -0700623 EGLint err = eglGetError();
John Reck2cdbc7d2014-09-17 16:06:36 -0700624 if (CC_LIKELY(err == EGL_SUCCESS)) {
625 return true;
626 }
John Reckc2547fa2015-10-26 13:52:52 -0700627 if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) {
John Reck2cdbc7d2014-09-17 16:06:36 -0700628 // For some reason our surface was destroyed out from under us
629 // This really shouldn't happen, but if it does we can recover easily
630 // by just not trying to use the surface anymore
John Reck1bcacfd2017-11-03 10:12:19 -0700631 ALOGW("swapBuffers encountered EGL error %d on %p, halting rendering...", err,
632 frame.mSurface);
John Reck2cdbc7d2014-09-17 16:06:36 -0700633 return false;
634 }
John Reck1bcacfd2017-11-03 10:12:19 -0700635 LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering", err, egl_error_str(err));
John Reck2cdbc7d2014-09-17 16:06:36 -0700636 // Impossible to hit this, but the compiler doesn't know that
637 return false;
John Reck3b202512014-06-23 13:13:08 -0700638}
639
John Reck55156372015-01-21 07:46:37 -0800640void EglManager::fence() {
641 EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL);
John Reck1bcacfd2017-11-03 10:12:19 -0700642 eglClientWaitSyncKHR(mEglDisplay, fence, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR);
John Reck55156372015-01-21 07:46:37 -0800643 eglDestroySyncKHR(mEglDisplay, fence);
644}
645
John Reck1125d1f2014-10-23 11:02:19 -0700646bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) {
John Reck149173d2015-08-10 09:52:29 -0700647 if (mSwapBehavior != SwapBehavior::Preserved) return false;
John Reck3b202512014-06-23 13:13:08 -0700648
John Reck149173d2015-08-10 09:52:29 -0700649 bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
John Reck1bcacfd2017-11-03 10:12:19 -0700650 preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED);
John Reck149173d2015-08-10 09:52:29 -0700651 if (!preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700652 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", (void*)surface,
653 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700654 // Maybe it's already set?
655 EGLint swapBehavior;
656 if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) {
657 preserved = (swapBehavior == EGL_BUFFER_PRESERVED);
658 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700659 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", (void*)surface,
660 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700661 }
John Reck3b202512014-06-23 13:13:08 -0700662 }
John Reck1125d1f2014-10-23 11:02:19 -0700663
664 return preserved;
John Reck3b202512014-06-23 13:13:08 -0700665}
666
Stan Ilievaaa9e832019-09-17 14:07:23 -0400667static status_t waitForeverOnFence(int fence, const char* logname) {
668 ATRACE_CALL();
669 if (fence == -1) {
670 return NO_ERROR;
671 }
672 constexpr int warningTimeout = 3000;
673 int err = sync_wait(fence, warningTimeout);
674 if (err < 0 && errno == ETIME) {
675 ALOGE("%s: fence %d didn't signal in %d ms", logname, fence, warningTimeout);
676 err = sync_wait(fence, -1);
677 }
678 return err < 0 ? -errno : status_t(NO_ERROR);
679}
680
681status_t EglManager::fenceWait(int fence) {
Stan Iliev564ca3e2018-09-04 22:00:00 +0000682 if (!hasEglContext()) {
683 ALOGE("EglManager::fenceWait: EGLDisplay not initialized");
684 return INVALID_OPERATION;
685 }
686
Alec Mouri680414e2020-01-28 09:22:33 -0800687 if (EglExtensions.waitSync && EglExtensions.nativeFenceSync) {
Stan Iliev564ca3e2018-09-04 22:00:00 +0000688 // Block GPU on the fence.
689 // Create an EGLSyncKHR from the current fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400690 int fenceFd = ::dup(fence);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000691 if (fenceFd == -1) {
692 ALOGE("EglManager::fenceWait: error dup'ing fence fd: %d", errno);
693 return -errno;
694 }
John Reck0fa0cbc2019-04-05 16:57:46 -0700695 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE};
696 EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000697 if (sync == EGL_NO_SYNC_KHR) {
698 close(fenceFd);
699 ALOGE("EglManager::fenceWait: error creating EGL fence: %#x", eglGetError());
700 return UNKNOWN_ERROR;
701 }
702
703 // XXX: The spec draft is inconsistent as to whether this should
704 // return an EGLint or void. Ignore the return value for now, as
705 // it's not strictly needed.
706 eglWaitSyncKHR(mEglDisplay, sync, 0);
707 EGLint eglErr = eglGetError();
708 eglDestroySyncKHR(mEglDisplay, sync);
709 if (eglErr != EGL_SUCCESS) {
710 ALOGE("EglManager::fenceWait: error waiting for EGL fence: %#x", eglErr);
711 return UNKNOWN_ERROR;
712 }
713 } else {
714 // Block CPU on the fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400715 status_t err = waitForeverOnFence(fence, "EglManager::fenceWait");
Stan Iliev564ca3e2018-09-04 22:00:00 +0000716 if (err != NO_ERROR) {
717 ALOGE("EglManager::fenceWait: error waiting for fence: %d", err);
718 return err;
719 }
720 }
721 return OK;
722}
723
Stan Ilievaaa9e832019-09-17 14:07:23 -0400724status_t EglManager::createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence, int* nativeFence) {
725 *nativeFence = -1;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000726 if (!hasEglContext()) {
727 ALOGE("EglManager::createReleaseFence: EGLDisplay not initialized");
728 return INVALID_OPERATION;
729 }
730
Alec Mouri680414e2020-01-28 09:22:33 -0800731 if (EglExtensions.nativeFenceSync) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700732 EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000733 if (sync == EGL_NO_SYNC_KHR) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700734 ALOGE("EglManager::createReleaseFence: error creating EGL fence: %#x", eglGetError());
Stan Iliev564ca3e2018-09-04 22:00:00 +0000735 return UNKNOWN_ERROR;
736 }
737 glFlush();
738 int fenceFd = eglDupNativeFenceFDANDROID(mEglDisplay, sync);
739 eglDestroySyncKHR(mEglDisplay, sync);
740 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
741 ALOGE("EglManager::createReleaseFence: error dup'ing native fence "
John Reck0fa0cbc2019-04-05 16:57:46 -0700742 "fd: %#x",
743 eglGetError());
Stan Iliev564ca3e2018-09-04 22:00:00 +0000744 return UNKNOWN_ERROR;
745 }
Stan Ilievaaa9e832019-09-17 14:07:23 -0400746 *nativeFence = fenceFd;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000747 *eglFence = EGL_NO_SYNC_KHR;
Alec Mouri680414e2020-01-28 09:22:33 -0800748 } else if (useFenceSync && EglExtensions.fenceSync) {
Stan Iliev564ca3e2018-09-04 22:00:00 +0000749 if (*eglFence != EGL_NO_SYNC_KHR) {
750 // There is already a fence for the current slot. We need to
751 // wait on that before replacing it with another fence to
752 // ensure that all outstanding buffer accesses have completed
753 // before the producer accesses it.
754 EGLint result = eglClientWaitSyncKHR(mEglDisplay, *eglFence, 0, 1000000000);
755 if (result == EGL_FALSE) {
756 ALOGE("EglManager::createReleaseFence: error waiting for previous fence: %#x",
John Reck0fa0cbc2019-04-05 16:57:46 -0700757 eglGetError());
Stan Iliev564ca3e2018-09-04 22:00:00 +0000758 return UNKNOWN_ERROR;
759 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
760 ALOGE("EglManager::createReleaseFence: timeout waiting for previous fence");
761 return TIMED_OUT;
762 }
763 eglDestroySyncKHR(mEglDisplay, *eglFence);
764 }
765
766 // Create a fence for the outstanding accesses in the current
767 // OpenGL ES context.
768 *eglFence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, nullptr);
769 if (*eglFence == EGL_NO_SYNC_KHR) {
770 ALOGE("EglManager::createReleaseFence: error creating fence: %#x", eglGetError());
771 return UNKNOWN_ERROR;
772 }
773 glFlush();
774 }
775 return OK;
776}
777
John Reck3b202512014-06-23 13:13:08 -0700778} /* namespace renderthread */
779} /* namespace uirenderer */
780} /* namespace android */