blob: facf30b83b077a332d84e9168ea0916758e136df [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
444 if (DeviceInfo::get()->getWideColorType() == kRGBA_F16_SkColorType) {
445 if (mEglConfigF16 == EGL_NO_CONFIG_KHR) {
446 colorMode = ColorMode::Default;
447 } else {
John Reckb36bfdd2020-07-23 13:47:49 -0700448 config = mEglConfigF16;
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -0500449 }
450 }
451 if (EglExtensions.glColorSpace) {
452 attribs[0] = EGL_GL_COLORSPACE_KHR;
453 switch (colorMode) {
454 case ColorMode::Default:
455 attribs[1] = EGL_GL_COLORSPACE_LINEAR_KHR;
456 break;
John Reck52d45952023-04-05 15:36:15 -0400457 // We don't have an EGL colorspace for extended range P3 that's used for HDR
458 // So override it after configuring the EGL context
John Reck55887762023-01-25 16:51:18 -0500459 case ColorMode::Hdr:
John Reck0b3f3312023-01-31 16:21:28 -0500460 case ColorMode::Hdr10:
John Reck52d45952023-04-05 15:36:15 -0400461 overrideWindowDataSpaceForHdr = true;
462 attribs[1] = EGL_GL_COLORSPACE_DISPLAY_P3_PASSTHROUGH_EXT;
463 break;
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -0500464 case ColorMode::WideColorGamut: {
465 skcms_Matrix3x3 colorGamut;
466 LOG_ALWAYS_FATAL_IF(!colorSpace->toXYZD50(&colorGamut),
467 "Could not get gamut matrix from color space");
468 if (memcmp(&colorGamut, &SkNamedGamut::kDisplayP3, sizeof(colorGamut)) == 0) {
469 attribs[1] = EGL_GL_COLORSPACE_DISPLAY_P3_PASSTHROUGH_EXT;
470 } else if (memcmp(&colorGamut, &SkNamedGamut::kSRGB, sizeof(colorGamut)) == 0) {
471 attribs[1] = EGL_GL_COLORSPACE_SCRGB_EXT;
472 } else if (memcmp(&colorGamut, &SkNamedGamut::kRec2020, sizeof(colorGamut)) ==
473 0) {
474 attribs[1] = EGL_GL_COLORSPACE_BT2020_PQ_EXT;
475 } else {
476 LOG_ALWAYS_FATAL("Unreachable: unsupported wide color space.");
477 }
478 break;
479 }
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -0500480 case ColorMode::A8:
481 LOG_ALWAYS_FATAL("Unreachable: A8 doesn't use a color space");
482 break;
483 }
Romain Guy26a2b972017-04-17 09:39:51 -0700484 }
Romain Guy26a2b972017-04-17 09:39:51 -0700485 }
486
John Reckb36bfdd2020-07-23 13:47:49 -0700487 EGLSurface surface = eglCreateWindowSurface(mEglDisplay, config, window, attribs);
John Reck8e539ca2018-11-15 15:21:29 -0800488 if (surface == EGL_NO_SURFACE) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700489 return Error<EGLint>{eglGetError()};
John Reck8e539ca2018-11-15 15:21:29 -0800490 }
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000491
492 if (mSwapBehavior != SwapBehavior::Preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700493 LOG_ALWAYS_FATAL_IF(eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
494 EGL_BUFFER_DESTROYED) == EGL_FALSE,
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000495 "Failed to set swap behavior to destroyed for window %p, eglErr = %s",
John Reck1bcacfd2017-11-03 10:12:19 -0700496 (void*)window, eglErrorString());
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000497 }
498
John Reck52d45952023-04-05 15:36:15 -0400499 if (overrideWindowDataSpaceForHdr) {
500 // This relies on knowing that EGL will not re-set the dataspace after the call to
501 // eglCreateWindowSurface. Since the handling of the colorspace extension is largely
502 // implemented in libEGL in the platform, we can safely assume this is the case
John Reck4bd8d8a2023-12-08 11:27:19 -0500503 int32_t err = ANativeWindow_setBuffersDataSpace(window, P3_XRB);
John Reck52d45952023-04-05 15:36:15 -0400504 LOG_ALWAYS_FATAL_IF(err, "Failed to ANativeWindow_setBuffersDataSpace %d", err);
505 }
506
John Reck3b202512014-06-23 13:13:08 -0700507 return surface;
508}
509
510void EglManager::destroySurface(EGLSurface surface) {
511 if (isCurrent(surface)) {
512 makeCurrent(EGL_NO_SURFACE);
513 }
514 if (!eglDestroySurface(mEglDisplay, surface)) {
sergeyv694d4992016-10-27 10:23:13 -0700515 ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700516 }
517}
518
519void EglManager::destroy() {
520 if (mEglDisplay == EGL_NO_DISPLAY) return;
521
John Reck3b202512014-06-23 13:13:08 -0700522 eglDestroyContext(mEglDisplay, mEglContext);
John Reck1e510712018-04-23 08:15:03 -0700523 if (mPBufferSurface != EGL_NO_SURFACE) {
524 eglDestroySurface(mEglDisplay, mPBufferSurface);
525 }
John Reck3b202512014-06-23 13:13:08 -0700526 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
527 eglTerminate(mEglDisplay);
528 eglReleaseThread();
529
530 mEglDisplay = EGL_NO_DISPLAY;
531 mEglContext = EGL_NO_CONTEXT;
532 mPBufferSurface = EGL_NO_SURFACE;
533 mCurrentSurface = EGL_NO_SURFACE;
534}
535
John Reck1e510712018-04-23 08:15:03 -0700536bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut, bool force) {
537 if (!force && isCurrent(surface)) return false;
John Reck3b202512014-06-23 13:13:08 -0700538
539 if (surface == EGL_NO_SURFACE) {
John Reckd7db4d72015-05-20 07:18:55 -0700540 // Ensure we always have a valid surface & context
541 surface = mPBufferSurface;
542 }
543 if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) {
John Reckf2dcc2a2015-07-16 09:17:59 -0700544 if (errOut) {
545 *errOut = eglGetError();
John Reck1bcacfd2017-11-03 10:12:19 -0700546 ALOGW("Failed to make current on surface %p, error=%s", (void*)surface,
547 egl_error_str(*errOut));
John Reckf2dcc2a2015-07-16 09:17:59 -0700548 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700549 LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", (void*)surface,
550 eglErrorString());
John Reckf2dcc2a2015-07-16 09:17:59 -0700551 }
John Reck3b202512014-06-23 13:13:08 -0700552 }
553 mCurrentSurface = surface;
John Recka8963062017-06-14 10:47:50 -0700554 if (Properties::disableVsync) {
555 eglSwapInterval(mEglDisplay, 0);
556 }
John Reck3b202512014-06-23 13:13:08 -0700557 return true;
558}
559
John Reck149173d2015-08-10 09:52:29 -0700560EGLint EglManager::queryBufferAge(EGLSurface surface) {
561 switch (mSwapBehavior) {
John Reck1bcacfd2017-11-03 10:12:19 -0700562 case SwapBehavior::Discard:
563 return 0;
564 case SwapBehavior::Preserved:
565 return 1;
566 case SwapBehavior::BufferAge:
567 EGLint bufferAge;
568 eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge);
569 return bufferAge;
John Reck149173d2015-08-10 09:52:29 -0700570 }
571 return 0;
572}
573
574Frame EglManager::beginFrame(EGLSurface surface) {
John Reck1bcacfd2017-11-03 10:12:19 -0700575 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, "Tried to beginFrame on EGL_NO_SURFACE!");
John Reck3b202512014-06-23 13:13:08 -0700576 makeCurrent(surface);
John Reck149173d2015-08-10 09:52:29 -0700577 Frame frame;
578 frame.mSurface = surface;
579 eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth);
580 eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight);
581 frame.mBufferAge = queryBufferAge(surface);
John Reck3b202512014-06-23 13:13:08 -0700582 eglBeginFrame(mEglDisplay, surface);
John Reck149173d2015-08-10 09:52:29 -0700583 return frame;
John Reck3b202512014-06-23 13:13:08 -0700584}
585
John Reck149173d2015-08-10 09:52:29 -0700586void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) {
587#ifdef EGL_KHR_partial_update
588 if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) {
589 EGLint rects[4];
590 frame.map(dirty, rects);
591 if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) {
592 LOG_ALWAYS_FATAL("Failed to set damage region on surface %p, error=%s",
John Reck1bcacfd2017-11-03 10:12:19 -0700593 (void*)frame.mSurface, eglErrorString());
John Reck149173d2015-08-10 09:52:29 -0700594 }
595 }
596#endif
597}
598
John Reckc96955d2016-02-26 14:56:44 -0800599bool EglManager::damageRequiresSwap() {
600 return EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge;
601}
602
John Reck149173d2015-08-10 09:52:29 -0700603bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) {
John Reck682573c2015-10-30 10:37:35 -0700604 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
John Reck55156372015-01-21 07:46:37 -0800605 ATRACE_NAME("Finishing GPU work");
606 fence();
607 }
John Reck55156372015-01-21 07:46:37 -0800608
John Recka672f6b2015-10-22 09:53:26 -0700609 EGLint rects[4];
610 frame.map(screenDirty, rects);
John Reck1bcacfd2017-11-03 10:12:19 -0700611 eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects, screenDirty.isEmpty() ? 0 : 1);
John Reckd04794a2015-05-08 10:04:36 -0700612
John Reck3b202512014-06-23 13:13:08 -0700613 EGLint err = eglGetError();
John Reck2cdbc7d2014-09-17 16:06:36 -0700614 if (CC_LIKELY(err == EGL_SUCCESS)) {
615 return true;
616 }
John Reckc2547fa2015-10-26 13:52:52 -0700617 if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) {
John Reck2cdbc7d2014-09-17 16:06:36 -0700618 // For some reason our surface was destroyed out from under us
619 // This really shouldn't happen, but if it does we can recover easily
620 // by just not trying to use the surface anymore
John Reck1bcacfd2017-11-03 10:12:19 -0700621 ALOGW("swapBuffers encountered EGL error %d on %p, halting rendering...", err,
622 frame.mSurface);
John Reck2cdbc7d2014-09-17 16:06:36 -0700623 return false;
624 }
John Reck1bcacfd2017-11-03 10:12:19 -0700625 LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering", err, egl_error_str(err));
John Reck2cdbc7d2014-09-17 16:06:36 -0700626 // Impossible to hit this, but the compiler doesn't know that
627 return false;
John Reck3b202512014-06-23 13:13:08 -0700628}
629
John Reck55156372015-01-21 07:46:37 -0800630void EglManager::fence() {
631 EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL);
John Reck1bcacfd2017-11-03 10:12:19 -0700632 eglClientWaitSyncKHR(mEglDisplay, fence, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR);
John Reck55156372015-01-21 07:46:37 -0800633 eglDestroySyncKHR(mEglDisplay, fence);
634}
635
John Reck1125d1f2014-10-23 11:02:19 -0700636bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) {
John Reck149173d2015-08-10 09:52:29 -0700637 if (mSwapBehavior != SwapBehavior::Preserved) return false;
John Reck3b202512014-06-23 13:13:08 -0700638
John Reck149173d2015-08-10 09:52:29 -0700639 bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
John Reck1bcacfd2017-11-03 10:12:19 -0700640 preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED);
John Reck149173d2015-08-10 09:52:29 -0700641 if (!preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700642 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", (void*)surface,
643 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700644 // Maybe it's already set?
645 EGLint swapBehavior;
646 if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) {
647 preserved = (swapBehavior == EGL_BUFFER_PRESERVED);
648 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700649 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", (void*)surface,
650 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700651 }
John Reck3b202512014-06-23 13:13:08 -0700652 }
John Reck1125d1f2014-10-23 11:02:19 -0700653
654 return preserved;
John Reck3b202512014-06-23 13:13:08 -0700655}
656
Stan Ilievaaa9e832019-09-17 14:07:23 -0400657static status_t waitForeverOnFence(int fence, const char* logname) {
658 ATRACE_CALL();
659 if (fence == -1) {
660 return NO_ERROR;
661 }
662 constexpr int warningTimeout = 3000;
663 int err = sync_wait(fence, warningTimeout);
664 if (err < 0 && errno == ETIME) {
665 ALOGE("%s: fence %d didn't signal in %d ms", logname, fence, warningTimeout);
666 err = sync_wait(fence, -1);
667 }
668 return err < 0 ? -errno : status_t(NO_ERROR);
669}
670
671status_t EglManager::fenceWait(int fence) {
Stan Iliev564ca3e2018-09-04 22:00:00 +0000672 if (!hasEglContext()) {
673 ALOGE("EglManager::fenceWait: EGLDisplay not initialized");
674 return INVALID_OPERATION;
675 }
676
Alec Mouri680414e2020-01-28 09:22:33 -0800677 if (EglExtensions.waitSync && EglExtensions.nativeFenceSync) {
Stan Iliev564ca3e2018-09-04 22:00:00 +0000678 // Block GPU on the fence.
679 // Create an EGLSyncKHR from the current fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400680 int fenceFd = ::dup(fence);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000681 if (fenceFd == -1) {
682 ALOGE("EglManager::fenceWait: error dup'ing fence fd: %d", errno);
683 return -errno;
684 }
John Reck0fa0cbc2019-04-05 16:57:46 -0700685 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE};
686 EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000687 if (sync == EGL_NO_SYNC_KHR) {
688 close(fenceFd);
689 ALOGE("EglManager::fenceWait: error creating EGL fence: %#x", eglGetError());
690 return UNKNOWN_ERROR;
691 }
692
693 // XXX: The spec draft is inconsistent as to whether this should
694 // return an EGLint or void. Ignore the return value for now, as
695 // it's not strictly needed.
696 eglWaitSyncKHR(mEglDisplay, sync, 0);
697 EGLint eglErr = eglGetError();
698 eglDestroySyncKHR(mEglDisplay, sync);
699 if (eglErr != EGL_SUCCESS) {
700 ALOGE("EglManager::fenceWait: error waiting for EGL fence: %#x", eglErr);
701 return UNKNOWN_ERROR;
702 }
703 } else {
704 // Block CPU on the fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400705 status_t err = waitForeverOnFence(fence, "EglManager::fenceWait");
Stan Iliev564ca3e2018-09-04 22:00:00 +0000706 if (err != NO_ERROR) {
707 ALOGE("EglManager::fenceWait: error waiting for fence: %d", err);
708 return err;
709 }
710 }
711 return OK;
712}
713
Stan Ilievaaa9e832019-09-17 14:07:23 -0400714status_t EglManager::createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence, int* nativeFence) {
715 *nativeFence = -1;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000716 if (!hasEglContext()) {
717 ALOGE("EglManager::createReleaseFence: EGLDisplay not initialized");
718 return INVALID_OPERATION;
719 }
720
Alec Mouri680414e2020-01-28 09:22:33 -0800721 if (EglExtensions.nativeFenceSync) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700722 EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000723 if (sync == EGL_NO_SYNC_KHR) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700724 ALOGE("EglManager::createReleaseFence: error creating EGL fence: %#x", eglGetError());
Stan Iliev564ca3e2018-09-04 22:00:00 +0000725 return UNKNOWN_ERROR;
726 }
727 glFlush();
728 int fenceFd = eglDupNativeFenceFDANDROID(mEglDisplay, sync);
729 eglDestroySyncKHR(mEglDisplay, sync);
730 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
731 ALOGE("EglManager::createReleaseFence: error dup'ing native fence "
John Reck0fa0cbc2019-04-05 16:57:46 -0700732 "fd: %#x",
733 eglGetError());
Stan Iliev564ca3e2018-09-04 22:00:00 +0000734 return UNKNOWN_ERROR;
735 }
Stan Ilievaaa9e832019-09-17 14:07:23 -0400736 *nativeFence = fenceFd;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000737 *eglFence = EGL_NO_SYNC_KHR;
Alec Mouri680414e2020-01-28 09:22:33 -0800738 } else if (useFenceSync && EglExtensions.fenceSync) {
Stan Iliev564ca3e2018-09-04 22:00:00 +0000739 if (*eglFence != EGL_NO_SYNC_KHR) {
740 // There is already a fence for the current slot. We need to
741 // wait on that before replacing it with another fence to
742 // ensure that all outstanding buffer accesses have completed
743 // before the producer accesses it.
744 EGLint result = eglClientWaitSyncKHR(mEglDisplay, *eglFence, 0, 1000000000);
745 if (result == EGL_FALSE) {
746 ALOGE("EglManager::createReleaseFence: error waiting for previous fence: %#x",
John Reck0fa0cbc2019-04-05 16:57:46 -0700747 eglGetError());
Stan Iliev564ca3e2018-09-04 22:00:00 +0000748 return UNKNOWN_ERROR;
749 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
750 ALOGE("EglManager::createReleaseFence: timeout waiting for previous fence");
751 return TIMED_OUT;
752 }
753 eglDestroySyncKHR(mEglDisplay, *eglFence);
754 }
755
756 // Create a fence for the outstanding accesses in the current
757 // OpenGL ES context.
758 *eglFence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, nullptr);
759 if (*eglFence == EGL_NO_SYNC_KHR) {
760 ALOGE("EglManager::createReleaseFence: error creating fence: %#x", eglGetError());
761 return UNKNOWN_ERROR;
762 }
763 glFlush();
764 }
765 return OK;
766}
767
John Reck3b202512014-06-23 13:13:08 -0700768} /* namespace renderthread */
769} /* namespace uirenderer */
770} /* namespace android */