blob: 6c3541ee24f6a017518e0aeea55714edf28d4fe8 [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)
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -050093 , mEglConfigA8(nullptr)
John Reck3b202512014-06-23 13:13:08 -070094 , mEglContext(EGL_NO_CONTEXT)
95 , mPBufferSurface(EGL_NO_SURFACE)
Peiyong Lin3bff1352018-12-11 07:56:07 -080096 , mCurrentSurface(EGL_NO_SURFACE)
97 , mHasWideColorGamutSupport(false) {}
John Reck3b202512014-06-23 13:13:08 -070098
John Reck1e510712018-04-23 08:15:03 -070099EglManager::~EglManager() {
John Reck6104cea2019-01-10 14:37:17 -0800100 if (hasEglContext()) {
101 ALOGW("~EglManager() leaked an EGL context");
102 }
John Reck1e510712018-04-23 08:15:03 -0700103}
104
John Reck3b202512014-06-23 13:13:08 -0700105void EglManager::initialize() {
106 if (hasEglContext()) return;
107
John Reckfbc8df02014-11-14 16:18:41 -0800108 ATRACE_NAME("Creating EGLContext");
109
John Reck3b202512014-06-23 13:13:08 -0700110 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
John Reck1bcacfd2017-11-03 10:12:19 -0700111 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, "Failed to get EGL_DEFAULT_DISPLAY! err=%s",
112 eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700113
114 EGLint major, minor;
115 LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE,
John Reck1bcacfd2017-11-03 10:12:19 -0700116 "Failed to initialize display %p! err=%s", mEglDisplay, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700117
John Reck848f6512018-12-03 13:26:43 -0800118 ALOGV("Initialized EGL, version %d.%d", (int)major, (int)minor);
John Reck3b202512014-06-23 13:13:08 -0700119
John Reck149173d2015-08-10 09:52:29 -0700120 initExtensions();
Season Li13d1b4a2015-07-29 17:16:19 -0700121
John Reck149173d2015-08-10 09:52:29 -0700122 // Now that extensions are loaded, pick a swap behavior
123 if (Properties::enablePartialUpdates) {
Matt Sarettb77c94a2016-12-07 12:22:37 -0500124 // An Adreno driver bug is causing rendering problems for SkiaGL with
125 // buffer age swap behavior (b/31957043). To temporarily workaround,
126 // we will use preserved swap behavior.
Derek Sollenbergerb5a12bd2017-06-14 15:23:19 -0400127 if (Properties::useBufferAge && EglExtensions.bufferAge) {
John Reck149173d2015-08-10 09:52:29 -0700128 mSwapBehavior = SwapBehavior::BufferAge;
129 } else {
130 mSwapBehavior = SwapBehavior::Preserved;
131 }
132 }
133
Romain Guy26a2b972017-04-17 09:39:51 -0700134 loadConfigs();
John Reck3b202512014-06-23 13:13:08 -0700135 createContext();
John Reckd7db4d72015-05-20 07:18:55 -0700136 createPBufferSurface();
John Reck1e510712018-04-23 08:15:03 -0700137 makeCurrent(mPBufferSurface, nullptr, /* force */ true);
Peiyong Lin3bff1352018-12-11 07:56:07 -0800138
Brian Osmane0cf5972019-01-23 10:41:20 -0500139 skcms_Matrix3x3 wideColorGamut;
140 LOG_ALWAYS_FATAL_IF(!DeviceInfo::get()->getWideColorSpace()->toXYZD50(&wideColorGamut),
141 "Could not get gamut matrix from wideColorSpace");
Peiyong Lin3bff1352018-12-11 07:56:07 -0800142 bool hasWideColorSpaceExtension = false;
Mike Reed7ac1af32020-05-26 10:50:21 -0400143 if (memcmp(&wideColorGamut, &SkNamedGamut::kDisplayP3, sizeof(wideColorGamut)) == 0) {
Peiyong Lin3bff1352018-12-11 07:56:07 -0800144 hasWideColorSpaceExtension = EglExtensions.displayP3;
Brian Osmane0cf5972019-01-23 10:41:20 -0500145 } else if (memcmp(&wideColorGamut, &SkNamedGamut::kSRGB, sizeof(wideColorGamut)) == 0) {
Peiyong Lin3bff1352018-12-11 07:56:07 -0800146 hasWideColorSpaceExtension = EglExtensions.scRGB;
147 } else {
148 LOG_ALWAYS_FATAL("Unsupported wide color space.");
149 }
John Reckb36bfdd2020-07-23 13:47:49 -0700150 mHasWideColorGamutSupport = EglExtensions.glColorSpace && hasWideColorSpaceExtension;
Nader Jawad4a6b60a2021-09-20 21:22:50 -0700151
152 auto* vendor = reinterpret_cast<const char*>(glGetString(GL_VENDOR));
Nader Jawad086645d2021-09-24 13:42:47 -0700153 auto* version = reinterpret_cast<const char*>(glGetString(GL_VERSION));
154 Properties::enableRenderEffectCache = supportsRenderEffectCache(
155 vendor, version);
156 ALOGV("RenderEffectCache supported %d on driver version %s",
157 Properties::enableRenderEffectCache, version);
Peiyong Lin3bff1352018-12-11 07:56:07 -0800158}
159
160EGLConfig EglManager::load8BitsConfig(EGLDisplay display, EglManager::SwapBehavior swapBehavior) {
161 EGLint eglSwapBehavior =
162 (swapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
163 EGLint attribs[] = {EGL_RENDERABLE_TYPE,
164 EGL_OPENGL_ES2_BIT,
165 EGL_RED_SIZE,
166 8,
167 EGL_GREEN_SIZE,
168 8,
169 EGL_BLUE_SIZE,
170 8,
171 EGL_ALPHA_SIZE,
172 8,
173 EGL_DEPTH_SIZE,
174 0,
175 EGL_CONFIG_CAVEAT,
176 EGL_NONE,
177 EGL_STENCIL_SIZE,
178 STENCIL_BUFFER_SIZE,
179 EGL_SURFACE_TYPE,
180 EGL_WINDOW_BIT | eglSwapBehavior,
181 EGL_NONE};
182 EGLConfig config = EGL_NO_CONFIG_KHR;
183 EGLint numConfigs = 1;
John Reck0fa0cbc2019-04-05 16:57:46 -0700184 if (!eglChooseConfig(display, attribs, &config, numConfigs, &numConfigs) || numConfigs != 1) {
Peiyong Lin3bff1352018-12-11 07:56:07 -0800185 return EGL_NO_CONFIG_KHR;
186 }
187 return config;
188}
189
John Reckb36bfdd2020-07-23 13:47:49 -0700190EGLConfig EglManager::load1010102Config(EGLDisplay display, SwapBehavior swapBehavior) {
191 EGLint eglSwapBehavior =
192 (swapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
193 // If we reached this point, we have a valid swap behavior
194 EGLint attribs[] = {EGL_RENDERABLE_TYPE,
195 EGL_OPENGL_ES2_BIT,
196 EGL_RED_SIZE,
197 10,
198 EGL_GREEN_SIZE,
199 10,
200 EGL_BLUE_SIZE,
201 10,
202 EGL_ALPHA_SIZE,
203 2,
204 EGL_DEPTH_SIZE,
205 0,
206 EGL_STENCIL_SIZE,
207 STENCIL_BUFFER_SIZE,
208 EGL_SURFACE_TYPE,
209 EGL_WINDOW_BIT | eglSwapBehavior,
210 EGL_NONE};
211 EGLConfig config = EGL_NO_CONFIG_KHR;
212 EGLint numConfigs = 1;
213 if (!eglChooseConfig(display, attribs, &config, numConfigs, &numConfigs) || numConfigs != 1) {
214 return EGL_NO_CONFIG_KHR;
215 }
216 return config;
217}
218
Peiyong Lin3bff1352018-12-11 07:56:07 -0800219EGLConfig EglManager::loadFP16Config(EGLDisplay display, SwapBehavior swapBehavior) {
220 EGLint eglSwapBehavior =
221 (swapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
222 // If we reached this point, we have a valid swap behavior
223 EGLint attribs[] = {EGL_RENDERABLE_TYPE,
224 EGL_OPENGL_ES2_BIT,
225 EGL_COLOR_COMPONENT_TYPE_EXT,
226 EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT,
227 EGL_RED_SIZE,
228 16,
229 EGL_GREEN_SIZE,
230 16,
231 EGL_BLUE_SIZE,
232 16,
233 EGL_ALPHA_SIZE,
234 16,
235 EGL_DEPTH_SIZE,
236 0,
237 EGL_STENCIL_SIZE,
238 STENCIL_BUFFER_SIZE,
239 EGL_SURFACE_TYPE,
240 EGL_WINDOW_BIT | eglSwapBehavior,
241 EGL_NONE};
242 EGLConfig config = EGL_NO_CONFIG_KHR;
243 EGLint numConfigs = 1;
John Reck0fa0cbc2019-04-05 16:57:46 -0700244 if (!eglChooseConfig(display, attribs, &config, numConfigs, &numConfigs) || numConfigs != 1) {
Peiyong Lin3bff1352018-12-11 07:56:07 -0800245 return EGL_NO_CONFIG_KHR;
246 }
247 return config;
John Reck3b202512014-06-23 13:13:08 -0700248}
249
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -0500250EGLConfig EglManager::loadA8Config(EGLDisplay display, EglManager::SwapBehavior swapBehavior) {
251 EGLint eglSwapBehavior =
252 (swapBehavior == SwapBehavior::Preserved) ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0;
253 EGLint attribs[] = {EGL_RENDERABLE_TYPE,
254 EGL_OPENGL_ES2_BIT,
255 EGL_RED_SIZE,
256 8,
257 EGL_GREEN_SIZE,
258 0,
259 EGL_BLUE_SIZE,
260 0,
261 EGL_ALPHA_SIZE,
262 0,
263 EGL_DEPTH_SIZE,
264 0,
265 EGL_STENCIL_SIZE,
266 STENCIL_BUFFER_SIZE,
267 EGL_SURFACE_TYPE,
268 EGL_WINDOW_BIT | eglSwapBehavior,
269 EGL_NONE};
270 EGLint numConfigs = 1;
271 if (!eglChooseConfig(display, attribs, nullptr, numConfigs, &numConfigs)) {
272 return EGL_NO_CONFIG_KHR;
273 }
274
275 std::vector<EGLConfig> configs(numConfigs, EGL_NO_CONFIG_KHR);
276 if (!eglChooseConfig(display, attribs, configs.data(), numConfigs, &numConfigs)) {
277 return EGL_NO_CONFIG_KHR;
278 }
279
280 // The component sizes passed to eglChooseConfig are minimums, so configs
281 // contains entries that exceed them. Choose one that matches the sizes
282 // exactly.
283 for (EGLConfig config : configs) {
284 EGLint r{0}, g{0}, b{0}, a{0};
285 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
286 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
287 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
288 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
289 if (8 == r && 0 == g && 0 == b && 0 == a) {
290 return config;
291 }
292 }
293 return EGL_NO_CONFIG_KHR;
294}
295
John Reck149173d2015-08-10 09:52:29 -0700296void EglManager::initExtensions() {
John Reck1bcacfd2017-11-03 10:12:19 -0700297 auto extensions = StringUtils::split(eglQueryString(mEglDisplay, EGL_EXTENSIONS));
Romain Guy26a2b972017-04-17 09:39:51 -0700298
John Reck8733bff2016-09-27 14:45:28 -0700299 // For our purposes we don't care if EGL_BUFFER_AGE is a result of
300 // EGL_EXT_buffer_age or EGL_KHR_partial_update as our usage is covered
301 // under EGL_KHR_partial_update and we don't need the expanded scope
302 // that EGL_EXT_buffer_age provides.
John Reck1bcacfd2017-11-03 10:12:19 -0700303 EglExtensions.bufferAge =
304 extensions.has("EGL_EXT_buffer_age") || extensions.has("EGL_KHR_partial_update");
Chris Craik6e6646c2015-09-14 15:54:12 -0700305 EglExtensions.setDamage = extensions.has("EGL_KHR_partial_update");
John Reck708b6682015-10-22 13:11:00 -0700306 LOG_ALWAYS_FATAL_IF(!extensions.has("EGL_KHR_swap_buffers_with_damage"),
John Reck1bcacfd2017-11-03 10:12:19 -0700307 "Missing required extension EGL_KHR_swap_buffers_with_damage");
Romain Guy26a2b972017-04-17 09:39:51 -0700308
309 EglExtensions.glColorSpace = extensions.has("EGL_KHR_gl_colorspace");
310 EglExtensions.noConfigContext = extensions.has("EGL_KHR_no_config_context");
311 EglExtensions.pixelFormatFloat = extensions.has("EGL_EXT_pixel_format_float");
Romain Guy26b6a642017-07-11 09:48:28 -0700312 EglExtensions.scRGB = extensions.has("EGL_EXT_gl_colorspace_scrgb");
Peiyong Lin3bff1352018-12-11 07:56:07 -0800313 EglExtensions.displayP3 = extensions.has("EGL_EXT_gl_colorspace_display_p3_passthrough");
John Reckb36bfdd2020-07-23 13:47:49 -0700314 EglExtensions.hdr = extensions.has("EGL_EXT_gl_colorspace_bt2020_pq");
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200315 EglExtensions.contextPriority = extensions.has("EGL_IMG_context_priority");
John Reck1e510712018-04-23 08:15:03 -0700316 EglExtensions.surfacelessContext = extensions.has("EGL_KHR_surfaceless_context");
Alec Mouri680414e2020-01-28 09:22:33 -0800317 EglExtensions.fenceSync = extensions.has("EGL_KHR_fence_sync");
318 EglExtensions.waitSync = extensions.has("EGL_KHR_wait_sync");
Yiwei Zhang7f430132020-08-12 15:07:51 -0700319 EglExtensions.nativeFenceSync = extensions.has("EGL_ANDROID_native_fence_sync");
John Reck149173d2015-08-10 09:52:29 -0700320}
321
John Reck3b202512014-06-23 13:13:08 -0700322bool EglManager::hasEglContext() {
323 return mEglDisplay != EGL_NO_DISPLAY;
324}
325
Romain Guy26a2b972017-04-17 09:39:51 -0700326void EglManager::loadConfigs() {
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700327 // Note: The default pixel format is RGBA_8888, when other formats are
328 // available, we should check the target pixel format and configure the
329 // attributes list properly.
Peiyong Lin3bff1352018-12-11 07:56:07 -0800330 mEglConfig = load8BitsConfig(mEglDisplay, mSwapBehavior);
331 if (mEglConfig == EGL_NO_CONFIG_KHR) {
John Reck149173d2015-08-10 09:52:29 -0700332 if (mSwapBehavior == SwapBehavior::Preserved) {
John Reck3b202512014-06-23 13:13:08 -0700333 // Try again without dirty regions enabled
John Reck149173d2015-08-10 09:52:29 -0700334 ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...");
335 mSwapBehavior = SwapBehavior::Discard;
Peiyong Lin3bff1352018-12-11 07:56:07 -0800336 mEglConfig = load8BitsConfig(mEglDisplay, mSwapBehavior);
John Reck3b202512014-06-23 13:13:08 -0700337 } else {
John Reck149173d2015-08-10 09:52:29 -0700338 // Failed to get a valid config
sergeyv694d4992016-10-27 10:23:13 -0700339 LOG_ALWAYS_FATAL("Failed to choose config, error = %s", eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700340 }
341 }
Romain Guy26a2b972017-04-17 09:39:51 -0700342
Peiyong Lin3bff1352018-12-11 07:56:07 -0800343 // When we reach this point, we have a valid swap behavior
John Reckb36bfdd2020-07-23 13:47:49 -0700344 if (EglExtensions.pixelFormatFloat) {
345 mEglConfigF16 = loadFP16Config(mEglDisplay, mSwapBehavior);
346 if (mEglConfigF16 == EGL_NO_CONFIG_KHR) {
Rob Herring74883ab2017-11-29 09:26:31 -0600347 ALOGE("Device claims wide gamut support, cannot find matching config, error = %s",
John Reck0fa0cbc2019-04-05 16:57:46 -0700348 eglErrorString());
Rob Herring74883ab2017-11-29 09:26:31 -0600349 EglExtensions.pixelFormatFloat = false;
Romain Guy26a2b972017-04-17 09:39:51 -0700350 }
John Reckb36bfdd2020-07-23 13:47:49 -0700351 }
352 mEglConfig1010102 = load1010102Config(mEglDisplay, mSwapBehavior);
353 if (mEglConfig1010102 == EGL_NO_CONFIG_KHR) {
354 ALOGW("Failed to initialize 101010-2 format, error = %s",
355 eglErrorString());
Romain Guy26a2b972017-04-17 09:39:51 -0700356 }
John Reck3b202512014-06-23 13:13:08 -0700357}
358
359void EglManager::createContext() {
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200360 std::vector<EGLint> contextAttributes;
361 contextAttributes.reserve(5);
362 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
363 contextAttributes.push_back(GLES_VERSION);
364 if (Properties::contextPriority != 0 && EglExtensions.contextPriority) {
365 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
366 contextAttributes.push_back(Properties::contextPriority);
367 }
368 contextAttributes.push_back(EGL_NONE);
John Reck1bcacfd2017-11-03 10:12:19 -0700369 mEglContext = eglCreateContext(
370 mEglDisplay, EglExtensions.noConfigContext ? ((EGLConfig) nullptr) : mEglConfig,
Jorim Jaggi767e25e2018-04-04 23:07:35 +0200371 EGL_NO_CONTEXT, contextAttributes.data());
John Reck1bcacfd2017-11-03 10:12:19 -0700372 LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, "Failed to create context, error = %s",
373 eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700374}
375
John Reckd7db4d72015-05-20 07:18:55 -0700376void EglManager::createPBufferSurface() {
John Reck3b202512014-06-23 13:13:08 -0700377 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY,
John Reck1bcacfd2017-11-03 10:12:19 -0700378 "usePBufferSurface() called on uninitialized GlobalContext!");
John Reck3b202512014-06-23 13:13:08 -0700379
John Reck1e510712018-04-23 08:15:03 -0700380 if (mPBufferSurface == EGL_NO_SURFACE && !EglExtensions.surfacelessContext) {
John Reck1bcacfd2017-11-03 10:12:19 -0700381 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE};
John Reck3b202512014-06-23 13:13:08 -0700382 mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs);
Roman Kiryanov60736132019-08-02 14:37:53 -0700383 LOG_ALWAYS_FATAL_IF(mPBufferSurface == EGL_NO_SURFACE,
384 "Failed to create a pixel buffer display=%p, "
385 "mEglConfig=%p, error=%s",
386 mEglDisplay, mEglConfig, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700387 }
John Reck3b202512014-06-23 13:13:08 -0700388}
389
Peiyong Lin3bff1352018-12-11 07:56:07 -0800390Result<EGLSurface, EGLint> EglManager::createSurface(EGLNativeWindowType window,
391 ColorMode colorMode,
Brian Osmane0cf5972019-01-23 10:41:20 -0500392 sk_sp<SkColorSpace> colorSpace) {
John Reck1e510712018-04-23 08:15:03 -0700393 LOG_ALWAYS_FATAL_IF(!hasEglContext(), "Not initialized");
Romain Guy253f2c22016-09-28 17:34:42 -0700394
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -0500395 if (!EglExtensions.noConfigContext) {
396 // The caller shouldn't use A8 if we cannot switch modes.
397 LOG_ALWAYS_FATAL_IF(colorMode == ColorMode::A8,
398 "Cannot use A8 without EGL_KHR_no_config_context!");
399
400 // Cannot switch modes without EGL_KHR_no_config_context.
John Reckb36bfdd2020-07-23 13:47:49 -0700401 colorMode = ColorMode::Default;
402 }
Romain Guy26a2b972017-04-17 09:39:51 -0700403 // The color space we want to use depends on whether linear blending is turned
404 // on and whether the app has requested wide color gamut rendering. When wide
405 // color gamut rendering is off, the app simply renders in the display's native
406 // color gamut.
407 //
408 // When wide gamut rendering is off:
409 // - Blending is done by default in gamma space, which requires using a
410 // linear EGL color space (the GPU uses the color values as is)
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700411 // - If linear blending is on, we must use the non-linear EGL color space
412 // (the GPU will perform sRGB to linear and linear to SRGB conversions
413 // before and after blending)
Romain Guy26a2b972017-04-17 09:39:51 -0700414 //
415 // When wide gamut rendering is on we cannot rely on the GPU performing
416 // linear blending for us. We use two different color spaces to tag the
417 // surface appropriately for SurfaceFlinger:
Peiyong Lin3bff1352018-12-11 07:56:07 -0800418 // - Gamma blending (default) requires the use of the non-linear color space
419 // - Linear blending requires the use of the linear color space
Romain Guy26a2b972017-04-17 09:39:51 -0700420
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700421 // Not all Android targets support the EGL_GL_COLORSPACE_KHR extension
Romain Guy26a2b972017-04-17 09:39:51 -0700422 // We insert to placeholders to set EGL_GL_COLORSPACE_KHR and its value.
423 // According to section 3.4.1 of the EGL specification, the attributes
424 // list is considered empty if the first entry is EGL_NONE
John Reck1bcacfd2017-11-03 10:12:19 -0700425 EGLint attribs[] = {EGL_NONE, EGL_NONE, EGL_NONE};
Romain Guy253f2c22016-09-28 17:34:42 -0700426
John Reckb36bfdd2020-07-23 13:47:49 -0700427 EGLConfig config = mEglConfig;
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -0500428 if (colorMode == ColorMode::A8) {
429 // A8 doesn't use a color space
Leon Scroggins IIIe040b292021-12-14 17:22:25 -0500430 if (!mEglConfigA8) {
431 mEglConfigA8 = loadA8Config(mEglDisplay, mSwapBehavior);
432 LOG_ALWAYS_FATAL_IF(!mEglConfigA8,
433 "Requested ColorMode::A8, but EGL lacks support! error = %s",
434 eglErrorString());
435 }
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -0500436 config = mEglConfigA8;
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -0500437 } else {
438 if (!mHasWideColorGamutSupport) {
John Reckb36bfdd2020-07-23 13:47:49 -0700439 colorMode = ColorMode::Default;
John Reckb36bfdd2020-07-23 13:47:49 -0700440 }
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -0500441
442 if (DeviceInfo::get()->getWideColorType() == kRGBA_F16_SkColorType) {
443 if (mEglConfigF16 == EGL_NO_CONFIG_KHR) {
444 colorMode = ColorMode::Default;
445 } else {
John Reckb36bfdd2020-07-23 13:47:49 -0700446 config = mEglConfigF16;
Leon Scroggins IIIe157ee82021-11-30 14:12:42 -0500447 }
448 }
449 if (EglExtensions.glColorSpace) {
450 attribs[0] = EGL_GL_COLORSPACE_KHR;
451 switch (colorMode) {
452 case ColorMode::Default:
453 attribs[1] = EGL_GL_COLORSPACE_LINEAR_KHR;
454 break;
455 case ColorMode::WideColorGamut: {
456 skcms_Matrix3x3 colorGamut;
457 LOG_ALWAYS_FATAL_IF(!colorSpace->toXYZD50(&colorGamut),
458 "Could not get gamut matrix from color space");
459 if (memcmp(&colorGamut, &SkNamedGamut::kDisplayP3, sizeof(colorGamut)) == 0) {
460 attribs[1] = EGL_GL_COLORSPACE_DISPLAY_P3_PASSTHROUGH_EXT;
461 } else if (memcmp(&colorGamut, &SkNamedGamut::kSRGB, sizeof(colorGamut)) == 0) {
462 attribs[1] = EGL_GL_COLORSPACE_SCRGB_EXT;
463 } else if (memcmp(&colorGamut, &SkNamedGamut::kRec2020, sizeof(colorGamut)) ==
464 0) {
465 attribs[1] = EGL_GL_COLORSPACE_BT2020_PQ_EXT;
466 } else {
467 LOG_ALWAYS_FATAL("Unreachable: unsupported wide color space.");
468 }
469 break;
470 }
471 case ColorMode::Hdr:
472 config = mEglConfigF16;
473 attribs[1] = EGL_GL_COLORSPACE_BT2020_PQ_EXT;
474 break;
475 case ColorMode::Hdr10:
476 config = mEglConfig1010102;
477 attribs[1] = EGL_GL_COLORSPACE_BT2020_PQ_EXT;
478 break;
479 case ColorMode::A8:
480 LOG_ALWAYS_FATAL("Unreachable: A8 doesn't use a color space");
481 break;
482 }
Romain Guy26a2b972017-04-17 09:39:51 -0700483 }
Romain Guy26a2b972017-04-17 09:39:51 -0700484 }
485
John Reckb36bfdd2020-07-23 13:47:49 -0700486 EGLSurface surface = eglCreateWindowSurface(mEglDisplay, config, window, attribs);
John Reck8e539ca2018-11-15 15:21:29 -0800487 if (surface == EGL_NO_SURFACE) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700488 return Error<EGLint>{eglGetError()};
John Reck8e539ca2018-11-15 15:21:29 -0800489 }
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000490
491 if (mSwapBehavior != SwapBehavior::Preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700492 LOG_ALWAYS_FATAL_IF(eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
493 EGL_BUFFER_DESTROYED) == EGL_FALSE,
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000494 "Failed to set swap behavior to destroyed for window %p, eglErr = %s",
John Reck1bcacfd2017-11-03 10:12:19 -0700495 (void*)window, eglErrorString());
Christian Poetzsch9a878a62016-02-05 14:22:01 +0000496 }
497
John Reck3b202512014-06-23 13:13:08 -0700498 return surface;
499}
500
501void EglManager::destroySurface(EGLSurface surface) {
502 if (isCurrent(surface)) {
503 makeCurrent(EGL_NO_SURFACE);
504 }
505 if (!eglDestroySurface(mEglDisplay, surface)) {
sergeyv694d4992016-10-27 10:23:13 -0700506 ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, eglErrorString());
John Reck3b202512014-06-23 13:13:08 -0700507 }
508}
509
510void EglManager::destroy() {
511 if (mEglDisplay == EGL_NO_DISPLAY) return;
512
John Reck3b202512014-06-23 13:13:08 -0700513 eglDestroyContext(mEglDisplay, mEglContext);
John Reck1e510712018-04-23 08:15:03 -0700514 if (mPBufferSurface != EGL_NO_SURFACE) {
515 eglDestroySurface(mEglDisplay, mPBufferSurface);
516 }
John Reck3b202512014-06-23 13:13:08 -0700517 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
518 eglTerminate(mEglDisplay);
519 eglReleaseThread();
520
521 mEglDisplay = EGL_NO_DISPLAY;
522 mEglContext = EGL_NO_CONTEXT;
523 mPBufferSurface = EGL_NO_SURFACE;
524 mCurrentSurface = EGL_NO_SURFACE;
525}
526
John Reck1e510712018-04-23 08:15:03 -0700527bool EglManager::makeCurrent(EGLSurface surface, EGLint* errOut, bool force) {
528 if (!force && isCurrent(surface)) return false;
John Reck3b202512014-06-23 13:13:08 -0700529
530 if (surface == EGL_NO_SURFACE) {
John Reckd7db4d72015-05-20 07:18:55 -0700531 // Ensure we always have a valid surface & context
532 surface = mPBufferSurface;
533 }
534 if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) {
John Reckf2dcc2a2015-07-16 09:17:59 -0700535 if (errOut) {
536 *errOut = eglGetError();
John Reck1bcacfd2017-11-03 10:12:19 -0700537 ALOGW("Failed to make current on surface %p, error=%s", (void*)surface,
538 egl_error_str(*errOut));
John Reckf2dcc2a2015-07-16 09:17:59 -0700539 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700540 LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", (void*)surface,
541 eglErrorString());
John Reckf2dcc2a2015-07-16 09:17:59 -0700542 }
John Reck3b202512014-06-23 13:13:08 -0700543 }
544 mCurrentSurface = surface;
John Recka8963062017-06-14 10:47:50 -0700545 if (Properties::disableVsync) {
546 eglSwapInterval(mEglDisplay, 0);
547 }
John Reck3b202512014-06-23 13:13:08 -0700548 return true;
549}
550
John Reck149173d2015-08-10 09:52:29 -0700551EGLint EglManager::queryBufferAge(EGLSurface surface) {
552 switch (mSwapBehavior) {
John Reck1bcacfd2017-11-03 10:12:19 -0700553 case SwapBehavior::Discard:
554 return 0;
555 case SwapBehavior::Preserved:
556 return 1;
557 case SwapBehavior::BufferAge:
558 EGLint bufferAge;
559 eglQuerySurface(mEglDisplay, surface, EGL_BUFFER_AGE_EXT, &bufferAge);
560 return bufferAge;
John Reck149173d2015-08-10 09:52:29 -0700561 }
562 return 0;
563}
564
565Frame EglManager::beginFrame(EGLSurface surface) {
John Reck1bcacfd2017-11-03 10:12:19 -0700566 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, "Tried to beginFrame on EGL_NO_SURFACE!");
John Reck3b202512014-06-23 13:13:08 -0700567 makeCurrent(surface);
John Reck149173d2015-08-10 09:52:29 -0700568 Frame frame;
569 frame.mSurface = surface;
570 eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, &frame.mWidth);
571 eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, &frame.mHeight);
572 frame.mBufferAge = queryBufferAge(surface);
John Reck3b202512014-06-23 13:13:08 -0700573 eglBeginFrame(mEglDisplay, surface);
John Reck149173d2015-08-10 09:52:29 -0700574 return frame;
John Reck3b202512014-06-23 13:13:08 -0700575}
576
John Reck149173d2015-08-10 09:52:29 -0700577void EglManager::damageFrame(const Frame& frame, const SkRect& dirty) {
578#ifdef EGL_KHR_partial_update
579 if (EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge) {
580 EGLint rects[4];
581 frame.map(dirty, rects);
582 if (!eglSetDamageRegionKHR(mEglDisplay, frame.mSurface, rects, 1)) {
583 LOG_ALWAYS_FATAL("Failed to set damage region on surface %p, error=%s",
John Reck1bcacfd2017-11-03 10:12:19 -0700584 (void*)frame.mSurface, eglErrorString());
John Reck149173d2015-08-10 09:52:29 -0700585 }
586 }
587#endif
588}
589
John Reckc96955d2016-02-26 14:56:44 -0800590bool EglManager::damageRequiresSwap() {
591 return EglExtensions.setDamage && mSwapBehavior == SwapBehavior::BufferAge;
592}
593
John Reck149173d2015-08-10 09:52:29 -0700594bool EglManager::swapBuffers(const Frame& frame, const SkRect& screenDirty) {
John Reck682573c2015-10-30 10:37:35 -0700595 if (CC_UNLIKELY(Properties::waitForGpuCompletion)) {
John Reck55156372015-01-21 07:46:37 -0800596 ATRACE_NAME("Finishing GPU work");
597 fence();
598 }
John Reck55156372015-01-21 07:46:37 -0800599
John Recka672f6b2015-10-22 09:53:26 -0700600 EGLint rects[4];
601 frame.map(screenDirty, rects);
John Reck1bcacfd2017-11-03 10:12:19 -0700602 eglSwapBuffersWithDamageKHR(mEglDisplay, frame.mSurface, rects, screenDirty.isEmpty() ? 0 : 1);
John Reckd04794a2015-05-08 10:04:36 -0700603
John Reck3b202512014-06-23 13:13:08 -0700604 EGLint err = eglGetError();
John Reck2cdbc7d2014-09-17 16:06:36 -0700605 if (CC_LIKELY(err == EGL_SUCCESS)) {
606 return true;
607 }
John Reckc2547fa2015-10-26 13:52:52 -0700608 if (err == EGL_BAD_SURFACE || err == EGL_BAD_NATIVE_WINDOW) {
John Reck2cdbc7d2014-09-17 16:06:36 -0700609 // For some reason our surface was destroyed out from under us
610 // This really shouldn't happen, but if it does we can recover easily
611 // by just not trying to use the surface anymore
John Reck1bcacfd2017-11-03 10:12:19 -0700612 ALOGW("swapBuffers encountered EGL error %d on %p, halting rendering...", err,
613 frame.mSurface);
John Reck2cdbc7d2014-09-17 16:06:36 -0700614 return false;
615 }
John Reck1bcacfd2017-11-03 10:12:19 -0700616 LOG_ALWAYS_FATAL("Encountered EGL error %d %s during rendering", err, egl_error_str(err));
John Reck2cdbc7d2014-09-17 16:06:36 -0700617 // Impossible to hit this, but the compiler doesn't know that
618 return false;
John Reck3b202512014-06-23 13:13:08 -0700619}
620
John Reck55156372015-01-21 07:46:37 -0800621void EglManager::fence() {
622 EGLSyncKHR fence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, NULL);
John Reck1bcacfd2017-11-03 10:12:19 -0700623 eglClientWaitSyncKHR(mEglDisplay, fence, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR);
John Reck55156372015-01-21 07:46:37 -0800624 eglDestroySyncKHR(mEglDisplay, fence);
625}
626
John Reck1125d1f2014-10-23 11:02:19 -0700627bool EglManager::setPreserveBuffer(EGLSurface surface, bool preserve) {
John Reck149173d2015-08-10 09:52:29 -0700628 if (mSwapBehavior != SwapBehavior::Preserved) return false;
John Reck3b202512014-06-23 13:13:08 -0700629
John Reck149173d2015-08-10 09:52:29 -0700630 bool preserved = eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR,
John Reck1bcacfd2017-11-03 10:12:19 -0700631 preserve ? EGL_BUFFER_PRESERVED : EGL_BUFFER_DESTROYED);
John Reck149173d2015-08-10 09:52:29 -0700632 if (!preserved) {
John Reck1bcacfd2017-11-03 10:12:19 -0700633 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", (void*)surface,
634 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700635 // Maybe it's already set?
636 EGLint swapBehavior;
637 if (eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &swapBehavior)) {
638 preserved = (swapBehavior == EGL_BUFFER_PRESERVED);
639 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700640 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", (void*)surface,
641 eglErrorString());
John Reck1125d1f2014-10-23 11:02:19 -0700642 }
John Reck3b202512014-06-23 13:13:08 -0700643 }
John Reck1125d1f2014-10-23 11:02:19 -0700644
645 return preserved;
John Reck3b202512014-06-23 13:13:08 -0700646}
647
Stan Ilievaaa9e832019-09-17 14:07:23 -0400648static status_t waitForeverOnFence(int fence, const char* logname) {
649 ATRACE_CALL();
650 if (fence == -1) {
651 return NO_ERROR;
652 }
653 constexpr int warningTimeout = 3000;
654 int err = sync_wait(fence, warningTimeout);
655 if (err < 0 && errno == ETIME) {
656 ALOGE("%s: fence %d didn't signal in %d ms", logname, fence, warningTimeout);
657 err = sync_wait(fence, -1);
658 }
659 return err < 0 ? -errno : status_t(NO_ERROR);
660}
661
662status_t EglManager::fenceWait(int fence) {
Stan Iliev564ca3e2018-09-04 22:00:00 +0000663 if (!hasEglContext()) {
664 ALOGE("EglManager::fenceWait: EGLDisplay not initialized");
665 return INVALID_OPERATION;
666 }
667
Alec Mouri680414e2020-01-28 09:22:33 -0800668 if (EglExtensions.waitSync && EglExtensions.nativeFenceSync) {
Stan Iliev564ca3e2018-09-04 22:00:00 +0000669 // Block GPU on the fence.
670 // Create an EGLSyncKHR from the current fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400671 int fenceFd = ::dup(fence);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000672 if (fenceFd == -1) {
673 ALOGE("EglManager::fenceWait: error dup'ing fence fd: %d", errno);
674 return -errno;
675 }
John Reck0fa0cbc2019-04-05 16:57:46 -0700676 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE};
677 EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000678 if (sync == EGL_NO_SYNC_KHR) {
679 close(fenceFd);
680 ALOGE("EglManager::fenceWait: error creating EGL fence: %#x", eglGetError());
681 return UNKNOWN_ERROR;
682 }
683
684 // XXX: The spec draft is inconsistent as to whether this should
685 // return an EGLint or void. Ignore the return value for now, as
686 // it's not strictly needed.
687 eglWaitSyncKHR(mEglDisplay, sync, 0);
688 EGLint eglErr = eglGetError();
689 eglDestroySyncKHR(mEglDisplay, sync);
690 if (eglErr != EGL_SUCCESS) {
691 ALOGE("EglManager::fenceWait: error waiting for EGL fence: %#x", eglErr);
692 return UNKNOWN_ERROR;
693 }
694 } else {
695 // Block CPU on the fence.
Stan Ilievaaa9e832019-09-17 14:07:23 -0400696 status_t err = waitForeverOnFence(fence, "EglManager::fenceWait");
Stan Iliev564ca3e2018-09-04 22:00:00 +0000697 if (err != NO_ERROR) {
698 ALOGE("EglManager::fenceWait: error waiting for fence: %d", err);
699 return err;
700 }
701 }
702 return OK;
703}
704
Stan Ilievaaa9e832019-09-17 14:07:23 -0400705status_t EglManager::createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence, int* nativeFence) {
706 *nativeFence = -1;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000707 if (!hasEglContext()) {
708 ALOGE("EglManager::createReleaseFence: EGLDisplay not initialized");
709 return INVALID_OPERATION;
710 }
711
Alec Mouri680414e2020-01-28 09:22:33 -0800712 if (EglExtensions.nativeFenceSync) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700713 EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000714 if (sync == EGL_NO_SYNC_KHR) {
John Reck0fa0cbc2019-04-05 16:57:46 -0700715 ALOGE("EglManager::createReleaseFence: error creating EGL fence: %#x", eglGetError());
Stan Iliev564ca3e2018-09-04 22:00:00 +0000716 return UNKNOWN_ERROR;
717 }
718 glFlush();
719 int fenceFd = eglDupNativeFenceFDANDROID(mEglDisplay, sync);
720 eglDestroySyncKHR(mEglDisplay, sync);
721 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
722 ALOGE("EglManager::createReleaseFence: error dup'ing native fence "
John Reck0fa0cbc2019-04-05 16:57:46 -0700723 "fd: %#x",
724 eglGetError());
Stan Iliev564ca3e2018-09-04 22:00:00 +0000725 return UNKNOWN_ERROR;
726 }
Stan Ilievaaa9e832019-09-17 14:07:23 -0400727 *nativeFence = fenceFd;
Stan Iliev564ca3e2018-09-04 22:00:00 +0000728 *eglFence = EGL_NO_SYNC_KHR;
Alec Mouri680414e2020-01-28 09:22:33 -0800729 } else if (useFenceSync && EglExtensions.fenceSync) {
Stan Iliev564ca3e2018-09-04 22:00:00 +0000730 if (*eglFence != EGL_NO_SYNC_KHR) {
731 // There is already a fence for the current slot. We need to
732 // wait on that before replacing it with another fence to
733 // ensure that all outstanding buffer accesses have completed
734 // before the producer accesses it.
735 EGLint result = eglClientWaitSyncKHR(mEglDisplay, *eglFence, 0, 1000000000);
736 if (result == EGL_FALSE) {
737 ALOGE("EglManager::createReleaseFence: error waiting for previous fence: %#x",
John Reck0fa0cbc2019-04-05 16:57:46 -0700738 eglGetError());
Stan Iliev564ca3e2018-09-04 22:00:00 +0000739 return UNKNOWN_ERROR;
740 } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
741 ALOGE("EglManager::createReleaseFence: timeout waiting for previous fence");
742 return TIMED_OUT;
743 }
744 eglDestroySyncKHR(mEglDisplay, *eglFence);
745 }
746
747 // Create a fence for the outstanding accesses in the current
748 // OpenGL ES context.
749 *eglFence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, nullptr);
750 if (*eglFence == EGL_NO_SYNC_KHR) {
751 ALOGE("EglManager::createReleaseFence: error creating fence: %#x", eglGetError());
752 return UNKNOWN_ERROR;
753 }
754 glFlush();
755 }
756 return OK;
757}
758
John Reck3b202512014-06-23 13:13:08 -0700759} /* namespace renderthread */
760} /* namespace uirenderer */
761} /* namespace android */