blob: 4ef7d5bccb8e50494c82820446a55a4b28292704 [file] [log] [blame]
John Reck67b1e2b2020-08-26 13:17:24 -07001/*
2 * Copyright 2020 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
17//#define LOG_NDEBUG 0
Ana Krulec70d15b1b2020-12-01 10:05:15 -080018#undef LOG_TAG
19#define LOG_TAG "RenderEngine"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Alec Mouri4ce5ec02021-01-07 17:33:21 -080022#include "SkiaGLRenderEngine.h"
John Reck67b1e2b2020-08-26 13:17:24 -070023
John Reck67b1e2b2020-08-26 13:17:24 -070024#include <EGL/egl.h>
25#include <EGL/eglext.h>
Ana Krulec1d12b3b2021-01-27 16:49:51 -080026#include <android-base/stringprintf.h>
Vishnu Nair40d80012024-07-13 23:25:06 +000027#include <common/trace.h>
Nolan Scobiebc3f3602024-08-30 13:51:37 -040028#include <include/gpu/ganesh/GrContextOptions.h>
29#include <include/gpu/ganesh/GrTypes.h>
Kevin Lubickd19875f2023-09-27 15:02:47 +000030#include <include/gpu/ganesh/gl/GrGLDirectContext.h>
Nolan Scobiebc3f3602024-08-30 13:51:37 -040031#include <include/gpu/ganesh/gl/GrGLInterface.h>
32#include <log/log_main.h>
Alec Mouri4ce5ec02021-01-07 17:33:21 -080033#include <sync/sync.h>
Ana Krulec1d12b3b2021-01-27 16:49:51 -080034#include <ui/DebugUtils.h>
Alec Mourib5777452020-09-28 11:32:42 -070035
36#include <cmath>
Alec Mouri4ce5ec02021-01-07 17:33:21 -080037#include <cstdint>
38#include <memory>
Alec Mouricdf6cbc2021-11-01 17:21:15 -070039#include <numeric>
Alec Mouri4ce5ec02021-01-07 17:33:21 -080040
Alec Mourie2b61c62023-08-15 19:04:54 +000041#include "GLExtensions.h"
Nolan Scobiebc3f3602024-08-30 13:51:37 -040042#include "compat/SkiaGpuContext.h"
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -040043
John Reck67b1e2b2020-08-26 13:17:24 -070044namespace android {
45namespace renderengine {
46namespace skia {
47
Ana Krulec1d12b3b2021-01-27 16:49:51 -080048using base::StringAppendF;
49
Alec Mourie2b61c62023-08-15 19:04:54 +000050static bool checkGlError(const char* op, int lineNumber) {
51 bool errorFound = false;
52 GLint error = glGetError();
53 while (error != GL_NO_ERROR) {
54 errorFound = true;
55 error = glGetError();
56 ALOGV("after %s() (line # %d) glError (0x%x)\n", op, lineNumber, error);
57 }
58 return errorFound;
59}
60
John Reck67b1e2b2020-08-26 13:17:24 -070061static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute,
62 EGLint wanted, EGLConfig* outConfig) {
63 EGLint numConfigs = -1, n = 0;
64 eglGetConfigs(dpy, nullptr, 0, &numConfigs);
65 std::vector<EGLConfig> configs(numConfigs, EGL_NO_CONFIG_KHR);
66 eglChooseConfig(dpy, attrs, configs.data(), configs.size(), &n);
67 configs.resize(n);
68
69 if (!configs.empty()) {
70 if (attribute != EGL_NONE) {
71 for (EGLConfig config : configs) {
72 EGLint value = 0;
73 eglGetConfigAttrib(dpy, config, attribute, &value);
74 if (wanted == value) {
75 *outConfig = config;
76 return NO_ERROR;
77 }
78 }
79 } else {
80 // just pick the first one
81 *outConfig = configs[0];
82 return NO_ERROR;
83 }
84 }
85
86 return NAME_NOT_FOUND;
87}
88
89static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType,
90 EGLConfig* config) {
91 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
92 // it is to be used with WIFI displays
93 status_t err;
94 EGLint wantedAttribute;
95 EGLint wantedAttributeValue;
96
97 std::vector<EGLint> attribs;
98 if (renderableType) {
99 const ui::PixelFormat pixelFormat = static_cast<ui::PixelFormat>(format);
100 const bool is1010102 = pixelFormat == ui::PixelFormat::RGBA_1010102;
101
102 // Default to 8 bits per channel.
103 const EGLint tmpAttribs[] = {
104 EGL_RENDERABLE_TYPE,
105 renderableType,
106 EGL_RECORDABLE_ANDROID,
107 EGL_TRUE,
108 EGL_SURFACE_TYPE,
109 EGL_WINDOW_BIT | EGL_PBUFFER_BIT,
110 EGL_FRAMEBUFFER_TARGET_ANDROID,
111 EGL_TRUE,
112 EGL_RED_SIZE,
113 is1010102 ? 10 : 8,
114 EGL_GREEN_SIZE,
115 is1010102 ? 10 : 8,
116 EGL_BLUE_SIZE,
117 is1010102 ? 10 : 8,
118 EGL_ALPHA_SIZE,
119 is1010102 ? 2 : 8,
120 EGL_NONE,
121 };
122 std::copy(tmpAttribs, tmpAttribs + (sizeof(tmpAttribs) / sizeof(EGLint)),
123 std::back_inserter(attribs));
124 wantedAttribute = EGL_NONE;
125 wantedAttributeValue = EGL_NONE;
126 } else {
127 // if no renderable type specified, fallback to a simplified query
128 wantedAttribute = EGL_NATIVE_VISUAL_ID;
129 wantedAttributeValue = format;
130 }
131
132 err = selectConfigForAttribute(display, attribs.data(), wantedAttribute, wantedAttributeValue,
133 config);
134 if (err == NO_ERROR) {
135 EGLint caveat;
136 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
137 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
138 }
139
140 return err;
141}
142
143std::unique_ptr<SkiaGLRenderEngine> SkiaGLRenderEngine::create(
144 const RenderEngineCreationArgs& args) {
145 // initialize EGL for the default display
146 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
147 if (!eglInitialize(display, nullptr, nullptr)) {
148 LOG_ALWAYS_FATAL("failed to initialize EGL");
149 }
150
Yiwei Zhange2650962020-12-01 23:27:58 +0000151 const auto eglVersion = eglQueryString(display, EGL_VERSION);
John Reck67b1e2b2020-08-26 13:17:24 -0700152 if (!eglVersion) {
153 checkGlError(__FUNCTION__, __LINE__);
Yiwei Zhange2650962020-12-01 23:27:58 +0000154 LOG_ALWAYS_FATAL("eglQueryString(EGL_VERSION) failed");
John Reck67b1e2b2020-08-26 13:17:24 -0700155 }
156
Yiwei Zhange2650962020-12-01 23:27:58 +0000157 const auto eglExtensions = eglQueryString(display, EGL_EXTENSIONS);
John Reck67b1e2b2020-08-26 13:17:24 -0700158 if (!eglExtensions) {
159 checkGlError(__FUNCTION__, __LINE__);
Yiwei Zhange2650962020-12-01 23:27:58 +0000160 LOG_ALWAYS_FATAL("eglQueryString(EGL_EXTENSIONS) failed");
John Reck67b1e2b2020-08-26 13:17:24 -0700161 }
162
Alec Mourie2b61c62023-08-15 19:04:54 +0000163 auto& extensions = GLExtensions::getInstance();
John Reck67b1e2b2020-08-26 13:17:24 -0700164 extensions.initWithEGLStrings(eglVersion, eglExtensions);
165
166 // The code assumes that ES2 or later is available if this extension is
167 // supported.
168 EGLConfig config = EGL_NO_CONFIG_KHR;
169 if (!extensions.hasNoConfigContext()) {
170 config = chooseEglConfig(display, args.pixelFormat, /*logConfig*/ true);
171 }
172
John Reck67b1e2b2020-08-26 13:17:24 -0700173 EGLContext protectedContext = EGL_NO_CONTEXT;
Alec Mourid6f09462020-12-07 11:18:17 -0800174 const std::optional<RenderEngine::ContextPriority> priority = createContextPriority(args);
John Reck67b1e2b2020-08-26 13:17:24 -0700175 if (args.enableProtectedContext && extensions.hasProtectedContent()) {
Alec Mourid6f09462020-12-07 11:18:17 -0800176 protectedContext =
177 createEglContext(display, config, nullptr, priority, Protection::PROTECTED);
John Reck67b1e2b2020-08-26 13:17:24 -0700178 ALOGE_IF(protectedContext == EGL_NO_CONTEXT, "Can't create protected context");
179 }
180
Alec Mourid6f09462020-12-07 11:18:17 -0800181 EGLContext ctxt =
182 createEglContext(display, config, protectedContext, priority, Protection::UNPROTECTED);
John Reck67b1e2b2020-08-26 13:17:24 -0700183
184 // if can't create a GL context, we can only abort.
185 LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
186
187 EGLSurface placeholder = EGL_NO_SURFACE;
188 if (!extensions.hasSurfacelessContext()) {
189 placeholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat,
190 Protection::UNPROTECTED);
191 LOG_ALWAYS_FATAL_IF(placeholder == EGL_NO_SURFACE, "can't create placeholder pbuffer");
192 }
193 EGLBoolean success = eglMakeCurrent(display, placeholder, placeholder, ctxt);
194 LOG_ALWAYS_FATAL_IF(!success, "can't make placeholder pbuffer current");
195 extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER),
196 glGetString(GL_VERSION), glGetString(GL_EXTENSIONS));
197
198 EGLSurface protectedPlaceholder = EGL_NO_SURFACE;
199 if (protectedContext != EGL_NO_CONTEXT && !extensions.hasSurfacelessContext()) {
200 protectedPlaceholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat,
201 Protection::PROTECTED);
202 ALOGE_IF(protectedPlaceholder == EGL_NO_SURFACE,
203 "can't create protected placeholder pbuffer");
204 }
205
206 // initialize the renderer while GL is current
Ian Elliott1f0911e2022-09-09 16:31:47 -0600207 std::unique_ptr<SkiaGLRenderEngine> engine(new SkiaGLRenderEngine(args, display, ctxt,
208 placeholder, protectedContext,
209 protectedPlaceholder));
Nolan Scobiefc125ec2024-03-11 20:08:27 -0400210 engine->ensureContextsCreated();
John Reck67b1e2b2020-08-26 13:17:24 -0700211
212 ALOGI("OpenGL ES informations:");
213 ALOGI("vendor : %s", extensions.getVendor());
214 ALOGI("renderer : %s", extensions.getRenderer());
215 ALOGI("version : %s", extensions.getVersion());
216 ALOGI("extensions: %s", extensions.getExtensions());
217 ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
218 ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
219
220 return engine;
221}
222
223EGLConfig SkiaGLRenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
224 status_t err;
225 EGLConfig config;
226
227 // First try to get an ES3 config
228 err = selectEGLConfig(display, format, EGL_OPENGL_ES3_BIT, &config);
229 if (err != NO_ERROR) {
230 // If ES3 fails, try to get an ES2 config
231 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
232 if (err != NO_ERROR) {
233 // If ES2 still doesn't work, probably because we're on the emulator.
234 // try a simplified query
235 ALOGW("no suitable EGLConfig found, trying a simpler query");
236 err = selectEGLConfig(display, format, 0, &config);
237 if (err != NO_ERROR) {
238 // this EGL is too lame for android
Nolan Scobieefa4f042023-12-14 13:24:57 -0500239 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up"
240 " (format: %d, vendor: %s, version: %s, extensions: %s, Client"
241 " API: %s)",
242 format, eglQueryString(display, EGL_VENDOR),
243 eglQueryString(display, EGL_VERSION),
244 eglQueryString(display, EGL_EXTENSIONS),
245 eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
John Reck67b1e2b2020-08-26 13:17:24 -0700246 }
247 }
248 }
249
250 if (logConfig) {
251 // print some debugging info
252 EGLint r, g, b, a;
253 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
254 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
255 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
256 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
257 ALOGI("EGL information:");
258 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
259 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
260 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
261 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
262 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
263 }
264
265 return config;
266}
267
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700268SkiaGLRenderEngine::SkiaGLRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display,
Lucas Dupind508e472020-11-04 04:32:06 +0000269 EGLContext ctxt, EGLSurface placeholder,
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700270 EGLContext protectedContext, EGLSurface protectedPlaceholder)
Leon Scroggins III696bf932024-01-24 15:21:05 -0500271 : SkiaRenderEngine(args.threaded, static_cast<PixelFormat>(args.pixelFormat),
Robin Lee7338bd92024-04-04 14:05:07 +0000272 args.blurAlgorithm),
Alec Mouri0d995102021-02-24 16:53:38 -0800273 mEGLDisplay(display),
John Reck67b1e2b2020-08-26 13:17:24 -0700274 mEGLContext(ctxt),
275 mPlaceholderSurface(placeholder),
276 mProtectedEGLContext(protectedContext),
Alec Mouri47bcb072023-08-15 02:02:49 +0000277 mProtectedPlaceholderSurface(protectedPlaceholder) {}
Alec Mouric0aae732021-01-12 13:32:18 -0800278
279SkiaGLRenderEngine::~SkiaGLRenderEngine() {
Nolan Scobie2526b2f2024-04-16 15:12:22 -0400280 finishRenderingAndAbandonContexts();
Alec Mouric0aae732021-01-12 13:32:18 -0800281 if (mPlaceholderSurface != EGL_NO_SURFACE) {
282 eglDestroySurface(mEGLDisplay, mPlaceholderSurface);
283 }
284 if (mProtectedPlaceholderSurface != EGL_NO_SURFACE) {
285 eglDestroySurface(mEGLDisplay, mProtectedPlaceholderSurface);
286 }
287 if (mEGLContext != EGL_NO_CONTEXT) {
288 eglDestroyContext(mEGLDisplay, mEGLContext);
289 }
290 if (mProtectedEGLContext != EGL_NO_CONTEXT) {
291 eglDestroyContext(mEGLDisplay, mProtectedEGLContext);
292 }
293 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
294 eglTerminate(mEGLDisplay);
295 eglReleaseThread();
John Reck67b1e2b2020-08-26 13:17:24 -0700296}
297
Nolan Scobiefc125ec2024-03-11 20:08:27 -0400298SkiaRenderEngine::Contexts SkiaGLRenderEngine::createContexts() {
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700299 LOG_ALWAYS_FATAL_IF(isProtected(),
300 "Cannot setup contexts while already in protected mode");
301
302 sk_sp<const GrGLInterface> glInterface = GrGLMakeNativeInterface();
303
304 LOG_ALWAYS_FATAL_IF(!glInterface.get(), "GrGLMakeNativeInterface() failed");
305
306 SkiaRenderEngine::Contexts contexts;
Nolan Scobiefc125ec2024-03-11 20:08:27 -0400307 contexts.first = SkiaGpuContext::MakeGL_Ganesh(glInterface, mSkSLCacheMonitor);
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700308 if (supportsProtectedContentImpl()) {
309 useProtectedContextImpl(GrProtected::kYes);
Nolan Scobiefc125ec2024-03-11 20:08:27 -0400310 contexts.second = SkiaGpuContext::MakeGL_Ganesh(glInterface, mSkSLCacheMonitor);
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700311 useProtectedContextImpl(GrProtected::kNo);
312 }
313
314 return contexts;
315}
316
317bool SkiaGLRenderEngine::supportsProtectedContentImpl() const {
Lucas Dupind508e472020-11-04 04:32:06 +0000318 return mProtectedEGLContext != EGL_NO_CONTEXT;
319}
320
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700321bool SkiaGLRenderEngine::useProtectedContextImpl(GrProtected isProtected) {
Lucas Dupind508e472020-11-04 04:32:06 +0000322 const EGLSurface surface =
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700323 (isProtected == GrProtected::kYes) ?
324 mProtectedPlaceholderSurface : mPlaceholderSurface;
325 const EGLContext context = (isProtected == GrProtected::kYes) ?
326 mProtectedEGLContext : mEGLContext;
Alec Mouric0aae732021-01-12 13:32:18 -0800327
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700328 return eglMakeCurrent(mEGLDisplay, surface, surface, context) == EGL_TRUE;
Lucas Dupind508e472020-11-04 04:32:06 +0000329}
330
Nolan Scobiefc125ec2024-03-11 20:08:27 -0400331void SkiaGLRenderEngine::waitFence(SkiaGpuContext*, base::borrowed_fd fenceFd) {
Alec Mouri4ee7b492021-08-11 10:36:55 -0700332 if (fenceFd.get() >= 0 && !waitGpuFence(fenceFd)) {
Vishnu Nair40d80012024-07-13 23:25:06 +0000333 SFTRACE_NAME("SkiaGLRenderEngine::waitFence");
Alec Mouri4ee7b492021-08-11 10:36:55 -0700334 sync_wait(fenceFd.get(), -1);
335 }
336}
337
Nolan Scobie1e06f2d2024-03-21 14:56:38 -0400338base::unique_fd SkiaGLRenderEngine::flushAndSubmit(SkiaGpuContext* context,
339 sk_sp<SkSurface> dstSurface) {
340 sk_sp<GrDirectContext> grContext = context->grDirectContext();
341 {
Vishnu Nair40d80012024-07-13 23:25:06 +0000342 SFTRACE_NAME("flush surface");
Nolan Scobie1e06f2d2024-03-21 14:56:38 -0400343 grContext->flush(dstSurface.get());
344 }
345 base::unique_fd drawFence = flushGL();
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700346
347 bool requireSync = drawFence.get() < 0;
348 if (requireSync) {
Vishnu Nair40d80012024-07-13 23:25:06 +0000349 SFTRACE_BEGIN("Submit(sync=true)");
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700350 } else {
Vishnu Nair40d80012024-07-13 23:25:06 +0000351 SFTRACE_BEGIN("Submit(sync=false)");
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700352 }
Nolan Scobie1e06f2d2024-03-21 14:56:38 -0400353 bool success = grContext->submit(requireSync ? GrSyncCpu::kYes : GrSyncCpu::kNo);
Vishnu Nair40d80012024-07-13 23:25:06 +0000354 SFTRACE_END();
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700355 if (!success) {
356 ALOGE("Failed to flush RenderEngine commands");
357 // Chances are, something illegal happened (Skia's internal GPU object
358 // doesn't exist, or the context was abandoned).
359 return drawFence;
360 }
361
362 return drawFence;
363}
364
Alec Mouri4ee7b492021-08-11 10:36:55 -0700365bool SkiaGLRenderEngine::waitGpuFence(base::borrowed_fd fenceFd) {
Alec Mourie2b61c62023-08-15 19:04:54 +0000366 if (!GLExtensions::getInstance().hasNativeFenceSync() ||
367 !GLExtensions::getInstance().hasWaitSync()) {
John Reck67b1e2b2020-08-26 13:17:24 -0700368 return false;
369 }
370
Alec Mouri4ee7b492021-08-11 10:36:55 -0700371 // Duplicate the fence for passing to eglCreateSyncKHR.
372 base::unique_fd fenceDup(dup(fenceFd.get()));
373 if (fenceDup.get() < 0) {
374 ALOGE("failed to create duplicate fence fd: %d", fenceDup.get());
375 return false;
376 }
377
John Reck67b1e2b2020-08-26 13:17:24 -0700378 // release the fd and transfer the ownership to EGLSync
Alec Mouri4ee7b492021-08-11 10:36:55 -0700379 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceDup.release(), EGL_NONE};
John Reck67b1e2b2020-08-26 13:17:24 -0700380 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
381 if (sync == EGL_NO_SYNC_KHR) {
382 ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
383 return false;
384 }
385
386 // XXX: The spec draft is inconsistent as to whether this should return an
387 // EGLint or void. Ignore the return value for now, as it's not strictly
388 // needed.
389 eglWaitSyncKHR(mEGLDisplay, sync, 0);
390 EGLint error = eglGetError();
391 eglDestroySyncKHR(mEGLDisplay, sync);
392 if (error != EGL_SUCCESS) {
393 ALOGE("failed to wait for EGL native fence sync: %#x", error);
394 return false;
395 }
396
397 return true;
398}
399
Nolan Scobie1e06f2d2024-03-21 14:56:38 -0400400base::unique_fd SkiaGLRenderEngine::flushGL() {
Vishnu Nair40d80012024-07-13 23:25:06 +0000401 SFTRACE_CALL();
Alec Mourie2b61c62023-08-15 19:04:54 +0000402 if (!GLExtensions::getInstance().hasNativeFenceSync()) {
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700403 return base::unique_fd();
John Reckcdb4ed72021-02-04 13:39:33 -0500404 }
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700405
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700406 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
407 if (sync == EGL_NO_SYNC_KHR) {
408 ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
409 return base::unique_fd();
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500410 }
411
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700412 // native fence fd will not be populated until flush() is done.
413 glFlush();
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500414
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700415 // get the fence fd
416 base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
417 eglDestroySyncKHR(mEGLDisplay, sync);
418 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
419 ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
Derek Sollenberger7c42bef2021-02-23 13:01:39 -0500420 }
421
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700422 return fenceFd;
Lucas Dupin3f11e922020-09-22 17:31:04 -0700423}
424
John Reck67b1e2b2020-08-26 13:17:24 -0700425EGLContext SkiaGLRenderEngine::createEglContext(EGLDisplay display, EGLConfig config,
Alec Mourid6f09462020-12-07 11:18:17 -0800426 EGLContext shareContext,
427 std::optional<ContextPriority> contextPriority,
John Reck67b1e2b2020-08-26 13:17:24 -0700428 Protection protection) {
429 EGLint renderableType = 0;
430 if (config == EGL_NO_CONFIG_KHR) {
431 renderableType = EGL_OPENGL_ES3_BIT;
432 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
433 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
434 }
435 EGLint contextClientVersion = 0;
436 if (renderableType & EGL_OPENGL_ES3_BIT) {
437 contextClientVersion = 3;
438 } else if (renderableType & EGL_OPENGL_ES2_BIT) {
439 contextClientVersion = 2;
440 } else if (renderableType & EGL_OPENGL_ES_BIT) {
441 contextClientVersion = 1;
442 } else {
443 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
444 }
445
446 std::vector<EGLint> contextAttributes;
447 contextAttributes.reserve(7);
448 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
449 contextAttributes.push_back(contextClientVersion);
Alec Mourid6f09462020-12-07 11:18:17 -0800450 if (contextPriority) {
John Reck67b1e2b2020-08-26 13:17:24 -0700451 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
Alec Mourid6f09462020-12-07 11:18:17 -0800452 switch (*contextPriority) {
453 case ContextPriority::REALTIME:
454 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_REALTIME_NV);
455 break;
456 case ContextPriority::MEDIUM:
457 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_MEDIUM_IMG);
458 break;
459 case ContextPriority::LOW:
460 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LOW_IMG);
461 break;
462 case ContextPriority::HIGH:
463 default:
464 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
465 break;
466 }
John Reck67b1e2b2020-08-26 13:17:24 -0700467 }
468 if (protection == Protection::PROTECTED) {
469 contextAttributes.push_back(EGL_PROTECTED_CONTENT_EXT);
470 contextAttributes.push_back(EGL_TRUE);
471 }
472 contextAttributes.push_back(EGL_NONE);
473
474 EGLContext context = eglCreateContext(display, config, shareContext, contextAttributes.data());
475
476 if (contextClientVersion == 3 && context == EGL_NO_CONTEXT) {
477 // eglGetConfigAttrib indicated we can create GLES 3 context, but we failed, thus
478 // EGL_NO_CONTEXT so that we can abort.
479 if (config != EGL_NO_CONFIG_KHR) {
480 return context;
481 }
482 // If |config| is EGL_NO_CONFIG_KHR, we speculatively try to create GLES 3 context, so we
483 // should try to fall back to GLES 2.
484 contextAttributes[1] = 2;
485 context = eglCreateContext(display, config, shareContext, contextAttributes.data());
486 }
487
488 return context;
489}
490
Alec Mourid6f09462020-12-07 11:18:17 -0800491std::optional<RenderEngine::ContextPriority> SkiaGLRenderEngine::createContextPriority(
492 const RenderEngineCreationArgs& args) {
Alec Mourie2b61c62023-08-15 19:04:54 +0000493 if (!GLExtensions::getInstance().hasContextPriority()) {
Alec Mourid6f09462020-12-07 11:18:17 -0800494 return std::nullopt;
495 }
496
497 switch (args.contextPriority) {
498 case RenderEngine::ContextPriority::REALTIME:
Alec Mourie2b61c62023-08-15 19:04:54 +0000499 if (GLExtensions::getInstance().hasRealtimePriority()) {
Alec Mourid6f09462020-12-07 11:18:17 -0800500 return RenderEngine::ContextPriority::REALTIME;
501 } else {
502 ALOGI("Realtime priority unsupported, degrading gracefully to high priority");
503 return RenderEngine::ContextPriority::HIGH;
504 }
505 case RenderEngine::ContextPriority::HIGH:
506 case RenderEngine::ContextPriority::MEDIUM:
507 case RenderEngine::ContextPriority::LOW:
508 return args.contextPriority;
509 default:
510 return std::nullopt;
511 }
512}
513
John Reck67b1e2b2020-08-26 13:17:24 -0700514EGLSurface SkiaGLRenderEngine::createPlaceholderEglPbufferSurface(EGLDisplay display,
515 EGLConfig config, int hwcFormat,
516 Protection protection) {
517 EGLConfig placeholderConfig = config;
518 if (placeholderConfig == EGL_NO_CONFIG_KHR) {
519 placeholderConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
520 }
521 std::vector<EGLint> attributes;
522 attributes.reserve(7);
523 attributes.push_back(EGL_WIDTH);
524 attributes.push_back(1);
525 attributes.push_back(EGL_HEIGHT);
526 attributes.push_back(1);
527 if (protection == Protection::PROTECTED) {
528 attributes.push_back(EGL_PROTECTED_CONTENT_EXT);
529 attributes.push_back(EGL_TRUE);
530 }
531 attributes.push_back(EGL_NONE);
532
533 return eglCreatePbufferSurface(display, placeholderConfig, attributes.data());
534}
535
Alec Mourid6f09462020-12-07 11:18:17 -0800536int SkiaGLRenderEngine::getContextPriority() {
537 int value;
538 eglQueryContext(mEGLDisplay, mEGLContext, EGL_CONTEXT_PRIORITY_LEVEL_IMG, &value);
539 return value;
540}
541
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700542void SkiaGLRenderEngine::appendBackendSpecificInfoToDump(std::string& result) {
Alec Mourie2b61c62023-08-15 19:04:54 +0000543 const GLExtensions& extensions = GLExtensions::getInstance();
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700544 StringAppendF(&result, "\n ------------RE GLES------------\n");
Ana Krulec1d12b3b2021-01-27 16:49:51 -0800545 StringAppendF(&result, "EGL implementation : %s\n", extensions.getEGLVersion());
546 StringAppendF(&result, "%s\n", extensions.getEGLExtensions());
547 StringAppendF(&result, "GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
548 extensions.getVersion());
549 StringAppendF(&result, "%s\n", extensions.getExtensions());
Ana Krulec1d12b3b2021-01-27 16:49:51 -0800550}
551
John Reck67b1e2b2020-08-26 13:17:24 -0700552} // namespace skia
553} // namespace renderengine
Galia Peycheva6c460652020-11-03 19:42:42 +0100554} // namespace android