blob: af24600ade2f80f1e45601ef377d764b7e63d6c2 [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
Nolan Scobiefc125ec2024-03-11 20:08:27 -040024#include "compat/SkiaGpuContext.h"
25
John Reck67b1e2b2020-08-26 13:17:24 -070026#include <EGL/egl.h>
27#include <EGL/eglext.h>
John Reck67b1e2b2020-08-26 13:17:24 -070028#include <GrContextOptions.h>
Kevin Lubicke9a6d8d2023-09-18 13:36:12 +000029#include <GrTypes.h>
Ana Krulec1d12b3b2021-01-27 16:49:51 -080030#include <android-base/stringprintf.h>
Vishnu Nair40d80012024-07-13 23:25:06 +000031#include <common/trace.h>
Alec Mourib5777452020-09-28 11:32:42 -070032#include <gl/GrGLInterface.h>
Kevin Lubickd19875f2023-09-27 15:02:47 +000033#include <include/gpu/ganesh/gl/GrGLDirectContext.h>
Alec Mouri4ce5ec02021-01-07 17:33:21 -080034#include <sync/sync.h>
Ana Krulec1d12b3b2021-01-27 16:49:51 -080035#include <ui/DebugUtils.h>
Alec Mourib5777452020-09-28 11:32:42 -070036
37#include <cmath>
Alec Mouri4ce5ec02021-01-07 17:33:21 -080038#include <cstdint>
39#include <memory>
Alec Mouricdf6cbc2021-11-01 17:21:15 -070040#include <numeric>
Alec Mouri4ce5ec02021-01-07 17:33:21 -080041
Alec Mourie2b61c62023-08-15 19:04:54 +000042#include "GLExtensions.h"
Alec Mouri4ce5ec02021-01-07 17:33:21 -080043#include "log/log_main.h"
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -040044
John Reck67b1e2b2020-08-26 13:17:24 -070045namespace android {
46namespace renderengine {
47namespace skia {
48
Ana Krulec1d12b3b2021-01-27 16:49:51 -080049using base::StringAppendF;
50
Alec Mourie2b61c62023-08-15 19:04:54 +000051static bool checkGlError(const char* op, int lineNumber) {
52 bool errorFound = false;
53 GLint error = glGetError();
54 while (error != GL_NO_ERROR) {
55 errorFound = true;
56 error = glGetError();
57 ALOGV("after %s() (line # %d) glError (0x%x)\n", op, lineNumber, error);
58 }
59 return errorFound;
60}
61
John Reck67b1e2b2020-08-26 13:17:24 -070062static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute,
63 EGLint wanted, EGLConfig* outConfig) {
64 EGLint numConfigs = -1, n = 0;
65 eglGetConfigs(dpy, nullptr, 0, &numConfigs);
66 std::vector<EGLConfig> configs(numConfigs, EGL_NO_CONFIG_KHR);
67 eglChooseConfig(dpy, attrs, configs.data(), configs.size(), &n);
68 configs.resize(n);
69
70 if (!configs.empty()) {
71 if (attribute != EGL_NONE) {
72 for (EGLConfig config : configs) {
73 EGLint value = 0;
74 eglGetConfigAttrib(dpy, config, attribute, &value);
75 if (wanted == value) {
76 *outConfig = config;
77 return NO_ERROR;
78 }
79 }
80 } else {
81 // just pick the first one
82 *outConfig = configs[0];
83 return NO_ERROR;
84 }
85 }
86
87 return NAME_NOT_FOUND;
88}
89
90static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType,
91 EGLConfig* config) {
92 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
93 // it is to be used with WIFI displays
94 status_t err;
95 EGLint wantedAttribute;
96 EGLint wantedAttributeValue;
97
98 std::vector<EGLint> attribs;
99 if (renderableType) {
100 const ui::PixelFormat pixelFormat = static_cast<ui::PixelFormat>(format);
101 const bool is1010102 = pixelFormat == ui::PixelFormat::RGBA_1010102;
102
103 // Default to 8 bits per channel.
104 const EGLint tmpAttribs[] = {
105 EGL_RENDERABLE_TYPE,
106 renderableType,
107 EGL_RECORDABLE_ANDROID,
108 EGL_TRUE,
109 EGL_SURFACE_TYPE,
110 EGL_WINDOW_BIT | EGL_PBUFFER_BIT,
111 EGL_FRAMEBUFFER_TARGET_ANDROID,
112 EGL_TRUE,
113 EGL_RED_SIZE,
114 is1010102 ? 10 : 8,
115 EGL_GREEN_SIZE,
116 is1010102 ? 10 : 8,
117 EGL_BLUE_SIZE,
118 is1010102 ? 10 : 8,
119 EGL_ALPHA_SIZE,
120 is1010102 ? 2 : 8,
121 EGL_NONE,
122 };
123 std::copy(tmpAttribs, tmpAttribs + (sizeof(tmpAttribs) / sizeof(EGLint)),
124 std::back_inserter(attribs));
125 wantedAttribute = EGL_NONE;
126 wantedAttributeValue = EGL_NONE;
127 } else {
128 // if no renderable type specified, fallback to a simplified query
129 wantedAttribute = EGL_NATIVE_VISUAL_ID;
130 wantedAttributeValue = format;
131 }
132
133 err = selectConfigForAttribute(display, attribs.data(), wantedAttribute, wantedAttributeValue,
134 config);
135 if (err == NO_ERROR) {
136 EGLint caveat;
137 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
138 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
139 }
140
141 return err;
142}
143
144std::unique_ptr<SkiaGLRenderEngine> SkiaGLRenderEngine::create(
145 const RenderEngineCreationArgs& args) {
146 // initialize EGL for the default display
147 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
148 if (!eglInitialize(display, nullptr, nullptr)) {
149 LOG_ALWAYS_FATAL("failed to initialize EGL");
150 }
151
Yiwei Zhange2650962020-12-01 23:27:58 +0000152 const auto eglVersion = eglQueryString(display, EGL_VERSION);
John Reck67b1e2b2020-08-26 13:17:24 -0700153 if (!eglVersion) {
154 checkGlError(__FUNCTION__, __LINE__);
Yiwei Zhange2650962020-12-01 23:27:58 +0000155 LOG_ALWAYS_FATAL("eglQueryString(EGL_VERSION) failed");
John Reck67b1e2b2020-08-26 13:17:24 -0700156 }
157
Yiwei Zhange2650962020-12-01 23:27:58 +0000158 const auto eglExtensions = eglQueryString(display, EGL_EXTENSIONS);
John Reck67b1e2b2020-08-26 13:17:24 -0700159 if (!eglExtensions) {
160 checkGlError(__FUNCTION__, __LINE__);
Yiwei Zhange2650962020-12-01 23:27:58 +0000161 LOG_ALWAYS_FATAL("eglQueryString(EGL_EXTENSIONS) failed");
John Reck67b1e2b2020-08-26 13:17:24 -0700162 }
163
Alec Mourie2b61c62023-08-15 19:04:54 +0000164 auto& extensions = GLExtensions::getInstance();
John Reck67b1e2b2020-08-26 13:17:24 -0700165 extensions.initWithEGLStrings(eglVersion, eglExtensions);
166
167 // The code assumes that ES2 or later is available if this extension is
168 // supported.
169 EGLConfig config = EGL_NO_CONFIG_KHR;
170 if (!extensions.hasNoConfigContext()) {
171 config = chooseEglConfig(display, args.pixelFormat, /*logConfig*/ true);
172 }
173
John Reck67b1e2b2020-08-26 13:17:24 -0700174 EGLContext protectedContext = EGL_NO_CONTEXT;
Alec Mourid6f09462020-12-07 11:18:17 -0800175 const std::optional<RenderEngine::ContextPriority> priority = createContextPriority(args);
John Reck67b1e2b2020-08-26 13:17:24 -0700176 if (args.enableProtectedContext && extensions.hasProtectedContent()) {
Alec Mourid6f09462020-12-07 11:18:17 -0800177 protectedContext =
178 createEglContext(display, config, nullptr, priority, Protection::PROTECTED);
John Reck67b1e2b2020-08-26 13:17:24 -0700179 ALOGE_IF(protectedContext == EGL_NO_CONTEXT, "Can't create protected context");
180 }
181
Alec Mourid6f09462020-12-07 11:18:17 -0800182 EGLContext ctxt =
183 createEglContext(display, config, protectedContext, priority, Protection::UNPROTECTED);
John Reck67b1e2b2020-08-26 13:17:24 -0700184
185 // if can't create a GL context, we can only abort.
186 LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
187
188 EGLSurface placeholder = EGL_NO_SURFACE;
189 if (!extensions.hasSurfacelessContext()) {
190 placeholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat,
191 Protection::UNPROTECTED);
192 LOG_ALWAYS_FATAL_IF(placeholder == EGL_NO_SURFACE, "can't create placeholder pbuffer");
193 }
194 EGLBoolean success = eglMakeCurrent(display, placeholder, placeholder, ctxt);
195 LOG_ALWAYS_FATAL_IF(!success, "can't make placeholder pbuffer current");
196 extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER),
197 glGetString(GL_VERSION), glGetString(GL_EXTENSIONS));
198
199 EGLSurface protectedPlaceholder = EGL_NO_SURFACE;
200 if (protectedContext != EGL_NO_CONTEXT && !extensions.hasSurfacelessContext()) {
201 protectedPlaceholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat,
202 Protection::PROTECTED);
203 ALOGE_IF(protectedPlaceholder == EGL_NO_SURFACE,
204 "can't create protected placeholder pbuffer");
205 }
206
207 // initialize the renderer while GL is current
Ian Elliott1f0911e2022-09-09 16:31:47 -0600208 std::unique_ptr<SkiaGLRenderEngine> engine(new SkiaGLRenderEngine(args, display, ctxt,
209 placeholder, protectedContext,
210 protectedPlaceholder));
Nolan Scobiefc125ec2024-03-11 20:08:27 -0400211 engine->ensureContextsCreated();
John Reck67b1e2b2020-08-26 13:17:24 -0700212
213 ALOGI("OpenGL ES informations:");
214 ALOGI("vendor : %s", extensions.getVendor());
215 ALOGI("renderer : %s", extensions.getRenderer());
216 ALOGI("version : %s", extensions.getVersion());
217 ALOGI("extensions: %s", extensions.getExtensions());
218 ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
219 ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
220
221 return engine;
222}
223
224EGLConfig SkiaGLRenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
225 status_t err;
226 EGLConfig config;
227
228 // First try to get an ES3 config
229 err = selectEGLConfig(display, format, EGL_OPENGL_ES3_BIT, &config);
230 if (err != NO_ERROR) {
231 // If ES3 fails, try to get an ES2 config
232 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
233 if (err != NO_ERROR) {
234 // If ES2 still doesn't work, probably because we're on the emulator.
235 // try a simplified query
236 ALOGW("no suitable EGLConfig found, trying a simpler query");
237 err = selectEGLConfig(display, format, 0, &config);
238 if (err != NO_ERROR) {
239 // this EGL is too lame for android
Nolan Scobieefa4f042023-12-14 13:24:57 -0500240 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up"
241 " (format: %d, vendor: %s, version: %s, extensions: %s, Client"
242 " API: %s)",
243 format, eglQueryString(display, EGL_VENDOR),
244 eglQueryString(display, EGL_VERSION),
245 eglQueryString(display, EGL_EXTENSIONS),
246 eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
John Reck67b1e2b2020-08-26 13:17:24 -0700247 }
248 }
249 }
250
251 if (logConfig) {
252 // print some debugging info
253 EGLint r, g, b, a;
254 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
255 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
256 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
257 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
258 ALOGI("EGL information:");
259 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
260 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
261 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
262 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
263 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
264 }
265
266 return config;
267}
268
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700269SkiaGLRenderEngine::SkiaGLRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display,
Lucas Dupind508e472020-11-04 04:32:06 +0000270 EGLContext ctxt, EGLSurface placeholder,
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700271 EGLContext protectedContext, EGLSurface protectedPlaceholder)
Leon Scroggins III696bf932024-01-24 15:21:05 -0500272 : SkiaRenderEngine(args.threaded, static_cast<PixelFormat>(args.pixelFormat),
Robin Lee7338bd92024-04-04 14:05:07 +0000273 args.blurAlgorithm),
Alec Mouri0d995102021-02-24 16:53:38 -0800274 mEGLDisplay(display),
John Reck67b1e2b2020-08-26 13:17:24 -0700275 mEGLContext(ctxt),
276 mPlaceholderSurface(placeholder),
277 mProtectedEGLContext(protectedContext),
Alec Mouri47bcb072023-08-15 02:02:49 +0000278 mProtectedPlaceholderSurface(protectedPlaceholder) {}
Alec Mouric0aae732021-01-12 13:32:18 -0800279
280SkiaGLRenderEngine::~SkiaGLRenderEngine() {
Nolan Scobie2526b2f2024-04-16 15:12:22 -0400281 finishRenderingAndAbandonContexts();
Alec Mouric0aae732021-01-12 13:32:18 -0800282 if (mPlaceholderSurface != EGL_NO_SURFACE) {
283 eglDestroySurface(mEGLDisplay, mPlaceholderSurface);
284 }
285 if (mProtectedPlaceholderSurface != EGL_NO_SURFACE) {
286 eglDestroySurface(mEGLDisplay, mProtectedPlaceholderSurface);
287 }
288 if (mEGLContext != EGL_NO_CONTEXT) {
289 eglDestroyContext(mEGLDisplay, mEGLContext);
290 }
291 if (mProtectedEGLContext != EGL_NO_CONTEXT) {
292 eglDestroyContext(mEGLDisplay, mProtectedEGLContext);
293 }
294 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
295 eglTerminate(mEGLDisplay);
296 eglReleaseThread();
John Reck67b1e2b2020-08-26 13:17:24 -0700297}
298
Nolan Scobiefc125ec2024-03-11 20:08:27 -0400299SkiaRenderEngine::Contexts SkiaGLRenderEngine::createContexts() {
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700300 LOG_ALWAYS_FATAL_IF(isProtected(),
301 "Cannot setup contexts while already in protected mode");
302
303 sk_sp<const GrGLInterface> glInterface = GrGLMakeNativeInterface();
304
305 LOG_ALWAYS_FATAL_IF(!glInterface.get(), "GrGLMakeNativeInterface() failed");
306
307 SkiaRenderEngine::Contexts contexts;
Nolan Scobiefc125ec2024-03-11 20:08:27 -0400308 contexts.first = SkiaGpuContext::MakeGL_Ganesh(glInterface, mSkSLCacheMonitor);
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700309 if (supportsProtectedContentImpl()) {
310 useProtectedContextImpl(GrProtected::kYes);
Nolan Scobiefc125ec2024-03-11 20:08:27 -0400311 contexts.second = SkiaGpuContext::MakeGL_Ganesh(glInterface, mSkSLCacheMonitor);
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700312 useProtectedContextImpl(GrProtected::kNo);
313 }
314
315 return contexts;
316}
317
318bool SkiaGLRenderEngine::supportsProtectedContentImpl() const {
Lucas Dupind508e472020-11-04 04:32:06 +0000319 return mProtectedEGLContext != EGL_NO_CONTEXT;
320}
321
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700322bool SkiaGLRenderEngine::useProtectedContextImpl(GrProtected isProtected) {
Lucas Dupind508e472020-11-04 04:32:06 +0000323 const EGLSurface surface =
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700324 (isProtected == GrProtected::kYes) ?
325 mProtectedPlaceholderSurface : mPlaceholderSurface;
326 const EGLContext context = (isProtected == GrProtected::kYes) ?
327 mProtectedEGLContext : mEGLContext;
Alec Mouric0aae732021-01-12 13:32:18 -0800328
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700329 return eglMakeCurrent(mEGLDisplay, surface, surface, context) == EGL_TRUE;
Lucas Dupind508e472020-11-04 04:32:06 +0000330}
331
Nolan Scobiefc125ec2024-03-11 20:08:27 -0400332void SkiaGLRenderEngine::waitFence(SkiaGpuContext*, base::borrowed_fd fenceFd) {
Alec Mouri4ee7b492021-08-11 10:36:55 -0700333 if (fenceFd.get() >= 0 && !waitGpuFence(fenceFd)) {
Vishnu Nair40d80012024-07-13 23:25:06 +0000334 SFTRACE_NAME("SkiaGLRenderEngine::waitFence");
Alec Mouri4ee7b492021-08-11 10:36:55 -0700335 sync_wait(fenceFd.get(), -1);
336 }
337}
338
Nolan Scobie1e06f2d2024-03-21 14:56:38 -0400339base::unique_fd SkiaGLRenderEngine::flushAndSubmit(SkiaGpuContext* context,
340 sk_sp<SkSurface> dstSurface) {
341 sk_sp<GrDirectContext> grContext = context->grDirectContext();
342 {
Vishnu Nair40d80012024-07-13 23:25:06 +0000343 SFTRACE_NAME("flush surface");
Nolan Scobie1e06f2d2024-03-21 14:56:38 -0400344 grContext->flush(dstSurface.get());
345 }
346 base::unique_fd drawFence = flushGL();
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700347
348 bool requireSync = drawFence.get() < 0;
349 if (requireSync) {
Vishnu Nair40d80012024-07-13 23:25:06 +0000350 SFTRACE_BEGIN("Submit(sync=true)");
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700351 } else {
Vishnu Nair40d80012024-07-13 23:25:06 +0000352 SFTRACE_BEGIN("Submit(sync=false)");
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700353 }
Nolan Scobie1e06f2d2024-03-21 14:56:38 -0400354 bool success = grContext->submit(requireSync ? GrSyncCpu::kYes : GrSyncCpu::kNo);
Vishnu Nair40d80012024-07-13 23:25:06 +0000355 SFTRACE_END();
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700356 if (!success) {
357 ALOGE("Failed to flush RenderEngine commands");
358 // Chances are, something illegal happened (Skia's internal GPU object
359 // doesn't exist, or the context was abandoned).
360 return drawFence;
361 }
362
363 return drawFence;
364}
365
Alec Mouri4ee7b492021-08-11 10:36:55 -0700366bool SkiaGLRenderEngine::waitGpuFence(base::borrowed_fd fenceFd) {
Alec Mourie2b61c62023-08-15 19:04:54 +0000367 if (!GLExtensions::getInstance().hasNativeFenceSync() ||
368 !GLExtensions::getInstance().hasWaitSync()) {
John Reck67b1e2b2020-08-26 13:17:24 -0700369 return false;
370 }
371
Alec Mouri4ee7b492021-08-11 10:36:55 -0700372 // Duplicate the fence for passing to eglCreateSyncKHR.
373 base::unique_fd fenceDup(dup(fenceFd.get()));
374 if (fenceDup.get() < 0) {
375 ALOGE("failed to create duplicate fence fd: %d", fenceDup.get());
376 return false;
377 }
378
John Reck67b1e2b2020-08-26 13:17:24 -0700379 // release the fd and transfer the ownership to EGLSync
Alec Mouri4ee7b492021-08-11 10:36:55 -0700380 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceDup.release(), EGL_NONE};
John Reck67b1e2b2020-08-26 13:17:24 -0700381 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
382 if (sync == EGL_NO_SYNC_KHR) {
383 ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
384 return false;
385 }
386
387 // XXX: The spec draft is inconsistent as to whether this should return an
388 // EGLint or void. Ignore the return value for now, as it's not strictly
389 // needed.
390 eglWaitSyncKHR(mEGLDisplay, sync, 0);
391 EGLint error = eglGetError();
392 eglDestroySyncKHR(mEGLDisplay, sync);
393 if (error != EGL_SUCCESS) {
394 ALOGE("failed to wait for EGL native fence sync: %#x", error);
395 return false;
396 }
397
398 return true;
399}
400
Nolan Scobie1e06f2d2024-03-21 14:56:38 -0400401base::unique_fd SkiaGLRenderEngine::flushGL() {
Vishnu Nair40d80012024-07-13 23:25:06 +0000402 SFTRACE_CALL();
Alec Mourie2b61c62023-08-15 19:04:54 +0000403 if (!GLExtensions::getInstance().hasNativeFenceSync()) {
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700404 return base::unique_fd();
John Reckcdb4ed72021-02-04 13:39:33 -0500405 }
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700406
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700407 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
408 if (sync == EGL_NO_SYNC_KHR) {
409 ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
410 return base::unique_fd();
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500411 }
412
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700413 // native fence fd will not be populated until flush() is done.
414 glFlush();
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500415
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700416 // get the fence fd
417 base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
418 eglDestroySyncKHR(mEGLDisplay, sync);
419 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
420 ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
Derek Sollenberger7c42bef2021-02-23 13:01:39 -0500421 }
422
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700423 return fenceFd;
Lucas Dupin3f11e922020-09-22 17:31:04 -0700424}
425
John Reck67b1e2b2020-08-26 13:17:24 -0700426EGLContext SkiaGLRenderEngine::createEglContext(EGLDisplay display, EGLConfig config,
Alec Mourid6f09462020-12-07 11:18:17 -0800427 EGLContext shareContext,
428 std::optional<ContextPriority> contextPriority,
John Reck67b1e2b2020-08-26 13:17:24 -0700429 Protection protection) {
430 EGLint renderableType = 0;
431 if (config == EGL_NO_CONFIG_KHR) {
432 renderableType = EGL_OPENGL_ES3_BIT;
433 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
434 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
435 }
436 EGLint contextClientVersion = 0;
437 if (renderableType & EGL_OPENGL_ES3_BIT) {
438 contextClientVersion = 3;
439 } else if (renderableType & EGL_OPENGL_ES2_BIT) {
440 contextClientVersion = 2;
441 } else if (renderableType & EGL_OPENGL_ES_BIT) {
442 contextClientVersion = 1;
443 } else {
444 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
445 }
446
447 std::vector<EGLint> contextAttributes;
448 contextAttributes.reserve(7);
449 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
450 contextAttributes.push_back(contextClientVersion);
Alec Mourid6f09462020-12-07 11:18:17 -0800451 if (contextPriority) {
John Reck67b1e2b2020-08-26 13:17:24 -0700452 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
Alec Mourid6f09462020-12-07 11:18:17 -0800453 switch (*contextPriority) {
454 case ContextPriority::REALTIME:
455 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_REALTIME_NV);
456 break;
457 case ContextPriority::MEDIUM:
458 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_MEDIUM_IMG);
459 break;
460 case ContextPriority::LOW:
461 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LOW_IMG);
462 break;
463 case ContextPriority::HIGH:
464 default:
465 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
466 break;
467 }
John Reck67b1e2b2020-08-26 13:17:24 -0700468 }
469 if (protection == Protection::PROTECTED) {
470 contextAttributes.push_back(EGL_PROTECTED_CONTENT_EXT);
471 contextAttributes.push_back(EGL_TRUE);
472 }
473 contextAttributes.push_back(EGL_NONE);
474
475 EGLContext context = eglCreateContext(display, config, shareContext, contextAttributes.data());
476
477 if (contextClientVersion == 3 && context == EGL_NO_CONTEXT) {
478 // eglGetConfigAttrib indicated we can create GLES 3 context, but we failed, thus
479 // EGL_NO_CONTEXT so that we can abort.
480 if (config != EGL_NO_CONFIG_KHR) {
481 return context;
482 }
483 // If |config| is EGL_NO_CONFIG_KHR, we speculatively try to create GLES 3 context, so we
484 // should try to fall back to GLES 2.
485 contextAttributes[1] = 2;
486 context = eglCreateContext(display, config, shareContext, contextAttributes.data());
487 }
488
489 return context;
490}
491
Alec Mourid6f09462020-12-07 11:18:17 -0800492std::optional<RenderEngine::ContextPriority> SkiaGLRenderEngine::createContextPriority(
493 const RenderEngineCreationArgs& args) {
Alec Mourie2b61c62023-08-15 19:04:54 +0000494 if (!GLExtensions::getInstance().hasContextPriority()) {
Alec Mourid6f09462020-12-07 11:18:17 -0800495 return std::nullopt;
496 }
497
498 switch (args.contextPriority) {
499 case RenderEngine::ContextPriority::REALTIME:
Alec Mourie2b61c62023-08-15 19:04:54 +0000500 if (GLExtensions::getInstance().hasRealtimePriority()) {
Alec Mourid6f09462020-12-07 11:18:17 -0800501 return RenderEngine::ContextPriority::REALTIME;
502 } else {
503 ALOGI("Realtime priority unsupported, degrading gracefully to high priority");
504 return RenderEngine::ContextPriority::HIGH;
505 }
506 case RenderEngine::ContextPriority::HIGH:
507 case RenderEngine::ContextPriority::MEDIUM:
508 case RenderEngine::ContextPriority::LOW:
509 return args.contextPriority;
510 default:
511 return std::nullopt;
512 }
513}
514
John Reck67b1e2b2020-08-26 13:17:24 -0700515EGLSurface SkiaGLRenderEngine::createPlaceholderEglPbufferSurface(EGLDisplay display,
516 EGLConfig config, int hwcFormat,
517 Protection protection) {
518 EGLConfig placeholderConfig = config;
519 if (placeholderConfig == EGL_NO_CONFIG_KHR) {
520 placeholderConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
521 }
522 std::vector<EGLint> attributes;
523 attributes.reserve(7);
524 attributes.push_back(EGL_WIDTH);
525 attributes.push_back(1);
526 attributes.push_back(EGL_HEIGHT);
527 attributes.push_back(1);
528 if (protection == Protection::PROTECTED) {
529 attributes.push_back(EGL_PROTECTED_CONTENT_EXT);
530 attributes.push_back(EGL_TRUE);
531 }
532 attributes.push_back(EGL_NONE);
533
534 return eglCreatePbufferSurface(display, placeholderConfig, attributes.data());
535}
536
Alec Mourid6f09462020-12-07 11:18:17 -0800537int SkiaGLRenderEngine::getContextPriority() {
538 int value;
539 eglQueryContext(mEGLDisplay, mEGLContext, EGL_CONTEXT_PRIORITY_LEVEL_IMG, &value);
540 return value;
541}
542
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700543void SkiaGLRenderEngine::appendBackendSpecificInfoToDump(std::string& result) {
Alec Mourie2b61c62023-08-15 19:04:54 +0000544 const GLExtensions& extensions = GLExtensions::getInstance();
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700545 StringAppendF(&result, "\n ------------RE GLES------------\n");
Ana Krulec1d12b3b2021-01-27 16:49:51 -0800546 StringAppendF(&result, "EGL implementation : %s\n", extensions.getEGLVersion());
547 StringAppendF(&result, "%s\n", extensions.getEGLExtensions());
548 StringAppendF(&result, "GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
549 extensions.getVersion());
550 StringAppendF(&result, "%s\n", extensions.getExtensions());
Ana Krulec1d12b3b2021-01-27 16:49:51 -0800551}
552
John Reck67b1e2b2020-08-26 13:17:24 -0700553} // namespace skia
554} // namespace renderengine
Galia Peycheva6c460652020-11-03 19:42:42 +0100555} // namespace android