blob: 9d3d98e2693fe3cb81354a619bbc0a7f01b8e7f8 [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>
Alec Mourib5777452020-09-28 11:32:42 -070031#include <gl/GrGLInterface.h>
Kevin Lubickd19875f2023-09-27 15:02:47 +000032#include <include/gpu/ganesh/gl/GrGLDirectContext.h>
Alec Mouricbd30932021-06-09 15:52:25 -070033#include <gui/TraceUtils.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 Mouri4ce5ec02021-01-07 17:33:21 -080036#include <utils/Trace.h>
Alec Mourib5777452020-09-28 11:32:42 -070037
38#include <cmath>
Alec Mouri4ce5ec02021-01-07 17:33:21 -080039#include <cstdint>
40#include <memory>
Alec Mouricdf6cbc2021-11-01 17:21:15 -070041#include <numeric>
Alec Mouri4ce5ec02021-01-07 17:33:21 -080042
Alec Mourie2b61c62023-08-15 19:04:54 +000043#include "GLExtensions.h"
Alec Mouri4ce5ec02021-01-07 17:33:21 -080044#include "log/log_main.h"
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -040045
John Reck67b1e2b2020-08-26 13:17:24 -070046namespace android {
47namespace renderengine {
48namespace skia {
49
Ana Krulec1d12b3b2021-01-27 16:49:51 -080050using base::StringAppendF;
51
Alec Mourie2b61c62023-08-15 19:04:54 +000052static bool checkGlError(const char* op, int lineNumber) {
53 bool errorFound = false;
54 GLint error = glGetError();
55 while (error != GL_NO_ERROR) {
56 errorFound = true;
57 error = glGetError();
58 ALOGV("after %s() (line # %d) glError (0x%x)\n", op, lineNumber, error);
59 }
60 return errorFound;
61}
62
John Reck67b1e2b2020-08-26 13:17:24 -070063static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute,
64 EGLint wanted, EGLConfig* outConfig) {
65 EGLint numConfigs = -1, n = 0;
66 eglGetConfigs(dpy, nullptr, 0, &numConfigs);
67 std::vector<EGLConfig> configs(numConfigs, EGL_NO_CONFIG_KHR);
68 eglChooseConfig(dpy, attrs, configs.data(), configs.size(), &n);
69 configs.resize(n);
70
71 if (!configs.empty()) {
72 if (attribute != EGL_NONE) {
73 for (EGLConfig config : configs) {
74 EGLint value = 0;
75 eglGetConfigAttrib(dpy, config, attribute, &value);
76 if (wanted == value) {
77 *outConfig = config;
78 return NO_ERROR;
79 }
80 }
81 } else {
82 // just pick the first one
83 *outConfig = configs[0];
84 return NO_ERROR;
85 }
86 }
87
88 return NAME_NOT_FOUND;
89}
90
91static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType,
92 EGLConfig* config) {
93 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
94 // it is to be used with WIFI displays
95 status_t err;
96 EGLint wantedAttribute;
97 EGLint wantedAttributeValue;
98
99 std::vector<EGLint> attribs;
100 if (renderableType) {
101 const ui::PixelFormat pixelFormat = static_cast<ui::PixelFormat>(format);
102 const bool is1010102 = pixelFormat == ui::PixelFormat::RGBA_1010102;
103
104 // Default to 8 bits per channel.
105 const EGLint tmpAttribs[] = {
106 EGL_RENDERABLE_TYPE,
107 renderableType,
108 EGL_RECORDABLE_ANDROID,
109 EGL_TRUE,
110 EGL_SURFACE_TYPE,
111 EGL_WINDOW_BIT | EGL_PBUFFER_BIT,
112 EGL_FRAMEBUFFER_TARGET_ANDROID,
113 EGL_TRUE,
114 EGL_RED_SIZE,
115 is1010102 ? 10 : 8,
116 EGL_GREEN_SIZE,
117 is1010102 ? 10 : 8,
118 EGL_BLUE_SIZE,
119 is1010102 ? 10 : 8,
120 EGL_ALPHA_SIZE,
121 is1010102 ? 2 : 8,
122 EGL_NONE,
123 };
124 std::copy(tmpAttribs, tmpAttribs + (sizeof(tmpAttribs) / sizeof(EGLint)),
125 std::back_inserter(attribs));
126 wantedAttribute = EGL_NONE;
127 wantedAttributeValue = EGL_NONE;
128 } else {
129 // if no renderable type specified, fallback to a simplified query
130 wantedAttribute = EGL_NATIVE_VISUAL_ID;
131 wantedAttributeValue = format;
132 }
133
134 err = selectConfigForAttribute(display, attribs.data(), wantedAttribute, wantedAttributeValue,
135 config);
136 if (err == NO_ERROR) {
137 EGLint caveat;
138 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
139 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
140 }
141
142 return err;
143}
144
145std::unique_ptr<SkiaGLRenderEngine> SkiaGLRenderEngine::create(
146 const RenderEngineCreationArgs& args) {
147 // initialize EGL for the default display
148 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
149 if (!eglInitialize(display, nullptr, nullptr)) {
150 LOG_ALWAYS_FATAL("failed to initialize EGL");
151 }
152
Yiwei Zhange2650962020-12-01 23:27:58 +0000153 const auto eglVersion = eglQueryString(display, EGL_VERSION);
John Reck67b1e2b2020-08-26 13:17:24 -0700154 if (!eglVersion) {
155 checkGlError(__FUNCTION__, __LINE__);
Yiwei Zhange2650962020-12-01 23:27:58 +0000156 LOG_ALWAYS_FATAL("eglQueryString(EGL_VERSION) failed");
John Reck67b1e2b2020-08-26 13:17:24 -0700157 }
158
Yiwei Zhange2650962020-12-01 23:27:58 +0000159 const auto eglExtensions = eglQueryString(display, EGL_EXTENSIONS);
John Reck67b1e2b2020-08-26 13:17:24 -0700160 if (!eglExtensions) {
161 checkGlError(__FUNCTION__, __LINE__);
Yiwei Zhange2650962020-12-01 23:27:58 +0000162 LOG_ALWAYS_FATAL("eglQueryString(EGL_EXTENSIONS) failed");
John Reck67b1e2b2020-08-26 13:17:24 -0700163 }
164
Alec Mourie2b61c62023-08-15 19:04:54 +0000165 auto& extensions = GLExtensions::getInstance();
John Reck67b1e2b2020-08-26 13:17:24 -0700166 extensions.initWithEGLStrings(eglVersion, eglExtensions);
167
168 // The code assumes that ES2 or later is available if this extension is
169 // supported.
170 EGLConfig config = EGL_NO_CONFIG_KHR;
171 if (!extensions.hasNoConfigContext()) {
172 config = chooseEglConfig(display, args.pixelFormat, /*logConfig*/ true);
173 }
174
John Reck67b1e2b2020-08-26 13:17:24 -0700175 EGLContext protectedContext = EGL_NO_CONTEXT;
Alec Mourid6f09462020-12-07 11:18:17 -0800176 const std::optional<RenderEngine::ContextPriority> priority = createContextPriority(args);
John Reck67b1e2b2020-08-26 13:17:24 -0700177 if (args.enableProtectedContext && extensions.hasProtectedContent()) {
Alec Mourid6f09462020-12-07 11:18:17 -0800178 protectedContext =
179 createEglContext(display, config, nullptr, priority, Protection::PROTECTED);
John Reck67b1e2b2020-08-26 13:17:24 -0700180 ALOGE_IF(protectedContext == EGL_NO_CONTEXT, "Can't create protected context");
181 }
182
Alec Mourid6f09462020-12-07 11:18:17 -0800183 EGLContext ctxt =
184 createEglContext(display, config, protectedContext, priority, Protection::UNPROTECTED);
John Reck67b1e2b2020-08-26 13:17:24 -0700185
186 // if can't create a GL context, we can only abort.
187 LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
188
189 EGLSurface placeholder = EGL_NO_SURFACE;
190 if (!extensions.hasSurfacelessContext()) {
191 placeholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat,
192 Protection::UNPROTECTED);
193 LOG_ALWAYS_FATAL_IF(placeholder == EGL_NO_SURFACE, "can't create placeholder pbuffer");
194 }
195 EGLBoolean success = eglMakeCurrent(display, placeholder, placeholder, ctxt);
196 LOG_ALWAYS_FATAL_IF(!success, "can't make placeholder pbuffer current");
197 extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER),
198 glGetString(GL_VERSION), glGetString(GL_EXTENSIONS));
199
200 EGLSurface protectedPlaceholder = EGL_NO_SURFACE;
201 if (protectedContext != EGL_NO_CONTEXT && !extensions.hasSurfacelessContext()) {
202 protectedPlaceholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat,
203 Protection::PROTECTED);
204 ALOGE_IF(protectedPlaceholder == EGL_NO_SURFACE,
205 "can't create protected placeholder pbuffer");
206 }
207
208 // initialize the renderer while GL is current
Ian Elliott1f0911e2022-09-09 16:31:47 -0600209 std::unique_ptr<SkiaGLRenderEngine> engine(new SkiaGLRenderEngine(args, display, ctxt,
210 placeholder, protectedContext,
211 protectedPlaceholder));
Nolan Scobiefc125ec2024-03-11 20:08:27 -0400212 engine->ensureContextsCreated();
John Reck67b1e2b2020-08-26 13:17:24 -0700213
214 ALOGI("OpenGL ES informations:");
215 ALOGI("vendor : %s", extensions.getVendor());
216 ALOGI("renderer : %s", extensions.getRenderer());
217 ALOGI("version : %s", extensions.getVersion());
218 ALOGI("extensions: %s", extensions.getExtensions());
219 ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
220 ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
221
222 return engine;
223}
224
225EGLConfig SkiaGLRenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
226 status_t err;
227 EGLConfig config;
228
229 // First try to get an ES3 config
230 err = selectEGLConfig(display, format, EGL_OPENGL_ES3_BIT, &config);
231 if (err != NO_ERROR) {
232 // If ES3 fails, try to get an ES2 config
233 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
234 if (err != NO_ERROR) {
235 // If ES2 still doesn't work, probably because we're on the emulator.
236 // try a simplified query
237 ALOGW("no suitable EGLConfig found, trying a simpler query");
238 err = selectEGLConfig(display, format, 0, &config);
239 if (err != NO_ERROR) {
240 // this EGL is too lame for android
Nolan Scobieefa4f042023-12-14 13:24:57 -0500241 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up"
242 " (format: %d, vendor: %s, version: %s, extensions: %s, Client"
243 " API: %s)",
244 format, eglQueryString(display, EGL_VENDOR),
245 eglQueryString(display, EGL_VERSION),
246 eglQueryString(display, EGL_EXTENSIONS),
247 eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
John Reck67b1e2b2020-08-26 13:17:24 -0700248 }
249 }
250 }
251
252 if (logConfig) {
253 // print some debugging info
254 EGLint r, g, b, a;
255 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
256 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
257 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
258 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
259 ALOGI("EGL information:");
260 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
261 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
262 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
263 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
264 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
265 }
266
267 return config;
268}
269
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700270SkiaGLRenderEngine::SkiaGLRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display,
Lucas Dupind508e472020-11-04 04:32:06 +0000271 EGLContext ctxt, EGLSurface placeholder,
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700272 EGLContext protectedContext, EGLSurface protectedPlaceholder)
Leon Scroggins III696bf932024-01-24 15:21:05 -0500273 : SkiaRenderEngine(args.threaded, static_cast<PixelFormat>(args.pixelFormat),
Robin Lee7338bd92024-04-04 14:05:07 +0000274 args.blurAlgorithm),
Alec Mouri0d995102021-02-24 16:53:38 -0800275 mEGLDisplay(display),
John Reck67b1e2b2020-08-26 13:17:24 -0700276 mEGLContext(ctxt),
277 mPlaceholderSurface(placeholder),
278 mProtectedEGLContext(protectedContext),
Alec Mouri47bcb072023-08-15 02:02:49 +0000279 mProtectedPlaceholderSurface(protectedPlaceholder) {}
Alec Mouric0aae732021-01-12 13:32:18 -0800280
281SkiaGLRenderEngine::~SkiaGLRenderEngine() {
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700282 finishRenderingAndAbandonContext();
Alec Mouric0aae732021-01-12 13:32:18 -0800283 if (mPlaceholderSurface != EGL_NO_SURFACE) {
284 eglDestroySurface(mEGLDisplay, mPlaceholderSurface);
285 }
286 if (mProtectedPlaceholderSurface != EGL_NO_SURFACE) {
287 eglDestroySurface(mEGLDisplay, mProtectedPlaceholderSurface);
288 }
289 if (mEGLContext != EGL_NO_CONTEXT) {
290 eglDestroyContext(mEGLDisplay, mEGLContext);
291 }
292 if (mProtectedEGLContext != EGL_NO_CONTEXT) {
293 eglDestroyContext(mEGLDisplay, mProtectedEGLContext);
294 }
295 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
296 eglTerminate(mEGLDisplay);
297 eglReleaseThread();
John Reck67b1e2b2020-08-26 13:17:24 -0700298}
299
Nolan Scobiefc125ec2024-03-11 20:08:27 -0400300SkiaRenderEngine::Contexts SkiaGLRenderEngine::createContexts() {
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700301 LOG_ALWAYS_FATAL_IF(isProtected(),
302 "Cannot setup contexts while already in protected mode");
303
304 sk_sp<const GrGLInterface> glInterface = GrGLMakeNativeInterface();
305
306 LOG_ALWAYS_FATAL_IF(!glInterface.get(), "GrGLMakeNativeInterface() failed");
307
308 SkiaRenderEngine::Contexts contexts;
Nolan Scobiefc125ec2024-03-11 20:08:27 -0400309 contexts.first = SkiaGpuContext::MakeGL_Ganesh(glInterface, mSkSLCacheMonitor);
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700310 if (supportsProtectedContentImpl()) {
311 useProtectedContextImpl(GrProtected::kYes);
Nolan Scobiefc125ec2024-03-11 20:08:27 -0400312 contexts.second = SkiaGpuContext::MakeGL_Ganesh(glInterface, mSkSLCacheMonitor);
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700313 useProtectedContextImpl(GrProtected::kNo);
314 }
315
316 return contexts;
317}
318
319bool SkiaGLRenderEngine::supportsProtectedContentImpl() const {
Lucas Dupind508e472020-11-04 04:32:06 +0000320 return mProtectedEGLContext != EGL_NO_CONTEXT;
321}
322
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700323bool SkiaGLRenderEngine::useProtectedContextImpl(GrProtected isProtected) {
Lucas Dupind508e472020-11-04 04:32:06 +0000324 const EGLSurface surface =
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700325 (isProtected == GrProtected::kYes) ?
326 mProtectedPlaceholderSurface : mPlaceholderSurface;
327 const EGLContext context = (isProtected == GrProtected::kYes) ?
328 mProtectedEGLContext : mEGLContext;
Alec Mouric0aae732021-01-12 13:32:18 -0800329
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700330 return eglMakeCurrent(mEGLDisplay, surface, surface, context) == EGL_TRUE;
Lucas Dupind508e472020-11-04 04:32:06 +0000331}
332
Nolan Scobiefc125ec2024-03-11 20:08:27 -0400333void SkiaGLRenderEngine::waitFence(SkiaGpuContext*, base::borrowed_fd fenceFd) {
Alec Mouri4ee7b492021-08-11 10:36:55 -0700334 if (fenceFd.get() >= 0 && !waitGpuFence(fenceFd)) {
335 ATRACE_NAME("SkiaGLRenderEngine::waitFence");
336 sync_wait(fenceFd.get(), -1);
337 }
338}
339
Nolan Scobie1e06f2d2024-03-21 14:56:38 -0400340base::unique_fd SkiaGLRenderEngine::flushAndSubmit(SkiaGpuContext* context,
341 sk_sp<SkSurface> dstSurface) {
342 sk_sp<GrDirectContext> grContext = context->grDirectContext();
343 {
344 ATRACE_NAME("flush surface");
345 grContext->flush(dstSurface.get());
346 }
347 base::unique_fd drawFence = flushGL();
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700348
349 bool requireSync = drawFence.get() < 0;
350 if (requireSync) {
351 ATRACE_BEGIN("Submit(sync=true)");
352 } else {
353 ATRACE_BEGIN("Submit(sync=false)");
354 }
Nolan Scobie1e06f2d2024-03-21 14:56:38 -0400355 bool success = grContext->submit(requireSync ? GrSyncCpu::kYes : GrSyncCpu::kNo);
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700356 ATRACE_END();
357 if (!success) {
358 ALOGE("Failed to flush RenderEngine commands");
359 // Chances are, something illegal happened (Skia's internal GPU object
360 // doesn't exist, or the context was abandoned).
361 return drawFence;
362 }
363
364 return drawFence;
365}
366
Alec Mouri4ee7b492021-08-11 10:36:55 -0700367bool SkiaGLRenderEngine::waitGpuFence(base::borrowed_fd fenceFd) {
Alec Mourie2b61c62023-08-15 19:04:54 +0000368 if (!GLExtensions::getInstance().hasNativeFenceSync() ||
369 !GLExtensions::getInstance().hasWaitSync()) {
John Reck67b1e2b2020-08-26 13:17:24 -0700370 return false;
371 }
372
Alec Mouri4ee7b492021-08-11 10:36:55 -0700373 // Duplicate the fence for passing to eglCreateSyncKHR.
374 base::unique_fd fenceDup(dup(fenceFd.get()));
375 if (fenceDup.get() < 0) {
376 ALOGE("failed to create duplicate fence fd: %d", fenceDup.get());
377 return false;
378 }
379
John Reck67b1e2b2020-08-26 13:17:24 -0700380 // release the fd and transfer the ownership to EGLSync
Alec Mouri4ee7b492021-08-11 10:36:55 -0700381 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceDup.release(), EGL_NONE};
John Reck67b1e2b2020-08-26 13:17:24 -0700382 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
383 if (sync == EGL_NO_SYNC_KHR) {
384 ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
385 return false;
386 }
387
388 // XXX: The spec draft is inconsistent as to whether this should return an
389 // EGLint or void. Ignore the return value for now, as it's not strictly
390 // needed.
391 eglWaitSyncKHR(mEGLDisplay, sync, 0);
392 EGLint error = eglGetError();
393 eglDestroySyncKHR(mEGLDisplay, sync);
394 if (error != EGL_SUCCESS) {
395 ALOGE("failed to wait for EGL native fence sync: %#x", error);
396 return false;
397 }
398
399 return true;
400}
401
Nolan Scobie1e06f2d2024-03-21 14:56:38 -0400402base::unique_fd SkiaGLRenderEngine::flushGL() {
Ana Krulecdfec8f52021-01-13 12:51:47 -0800403 ATRACE_CALL();
Alec Mourie2b61c62023-08-15 19:04:54 +0000404 if (!GLExtensions::getInstance().hasNativeFenceSync()) {
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700405 return base::unique_fd();
John Reckcdb4ed72021-02-04 13:39:33 -0500406 }
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700407
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700408 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
409 if (sync == EGL_NO_SYNC_KHR) {
410 ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
411 return base::unique_fd();
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500412 }
413
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700414 // native fence fd will not be populated until flush() is done.
415 glFlush();
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500416
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700417 // get the fence fd
418 base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
419 eglDestroySyncKHR(mEGLDisplay, sync);
420 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
421 ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
Derek Sollenberger7c42bef2021-02-23 13:01:39 -0500422 }
423
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700424 return fenceFd;
Lucas Dupin3f11e922020-09-22 17:31:04 -0700425}
426
John Reck67b1e2b2020-08-26 13:17:24 -0700427EGLContext SkiaGLRenderEngine::createEglContext(EGLDisplay display, EGLConfig config,
Alec Mourid6f09462020-12-07 11:18:17 -0800428 EGLContext shareContext,
429 std::optional<ContextPriority> contextPriority,
John Reck67b1e2b2020-08-26 13:17:24 -0700430 Protection protection) {
431 EGLint renderableType = 0;
432 if (config == EGL_NO_CONFIG_KHR) {
433 renderableType = EGL_OPENGL_ES3_BIT;
434 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
435 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
436 }
437 EGLint contextClientVersion = 0;
438 if (renderableType & EGL_OPENGL_ES3_BIT) {
439 contextClientVersion = 3;
440 } else if (renderableType & EGL_OPENGL_ES2_BIT) {
441 contextClientVersion = 2;
442 } else if (renderableType & EGL_OPENGL_ES_BIT) {
443 contextClientVersion = 1;
444 } else {
445 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
446 }
447
448 std::vector<EGLint> contextAttributes;
449 contextAttributes.reserve(7);
450 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
451 contextAttributes.push_back(contextClientVersion);
Alec Mourid6f09462020-12-07 11:18:17 -0800452 if (contextPriority) {
John Reck67b1e2b2020-08-26 13:17:24 -0700453 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
Alec Mourid6f09462020-12-07 11:18:17 -0800454 switch (*contextPriority) {
455 case ContextPriority::REALTIME:
456 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_REALTIME_NV);
457 break;
458 case ContextPriority::MEDIUM:
459 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_MEDIUM_IMG);
460 break;
461 case ContextPriority::LOW:
462 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LOW_IMG);
463 break;
464 case ContextPriority::HIGH:
465 default:
466 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
467 break;
468 }
John Reck67b1e2b2020-08-26 13:17:24 -0700469 }
470 if (protection == Protection::PROTECTED) {
471 contextAttributes.push_back(EGL_PROTECTED_CONTENT_EXT);
472 contextAttributes.push_back(EGL_TRUE);
473 }
474 contextAttributes.push_back(EGL_NONE);
475
476 EGLContext context = eglCreateContext(display, config, shareContext, contextAttributes.data());
477
478 if (contextClientVersion == 3 && context == EGL_NO_CONTEXT) {
479 // eglGetConfigAttrib indicated we can create GLES 3 context, but we failed, thus
480 // EGL_NO_CONTEXT so that we can abort.
481 if (config != EGL_NO_CONFIG_KHR) {
482 return context;
483 }
484 // If |config| is EGL_NO_CONFIG_KHR, we speculatively try to create GLES 3 context, so we
485 // should try to fall back to GLES 2.
486 contextAttributes[1] = 2;
487 context = eglCreateContext(display, config, shareContext, contextAttributes.data());
488 }
489
490 return context;
491}
492
Alec Mourid6f09462020-12-07 11:18:17 -0800493std::optional<RenderEngine::ContextPriority> SkiaGLRenderEngine::createContextPriority(
494 const RenderEngineCreationArgs& args) {
Alec Mourie2b61c62023-08-15 19:04:54 +0000495 if (!GLExtensions::getInstance().hasContextPriority()) {
Alec Mourid6f09462020-12-07 11:18:17 -0800496 return std::nullopt;
497 }
498
499 switch (args.contextPriority) {
500 case RenderEngine::ContextPriority::REALTIME:
Alec Mourie2b61c62023-08-15 19:04:54 +0000501 if (GLExtensions::getInstance().hasRealtimePriority()) {
Alec Mourid6f09462020-12-07 11:18:17 -0800502 return RenderEngine::ContextPriority::REALTIME;
503 } else {
504 ALOGI("Realtime priority unsupported, degrading gracefully to high priority");
505 return RenderEngine::ContextPriority::HIGH;
506 }
507 case RenderEngine::ContextPriority::HIGH:
508 case RenderEngine::ContextPriority::MEDIUM:
509 case RenderEngine::ContextPriority::LOW:
510 return args.contextPriority;
511 default:
512 return std::nullopt;
513 }
514}
515
John Reck67b1e2b2020-08-26 13:17:24 -0700516EGLSurface SkiaGLRenderEngine::createPlaceholderEglPbufferSurface(EGLDisplay display,
517 EGLConfig config, int hwcFormat,
518 Protection protection) {
519 EGLConfig placeholderConfig = config;
520 if (placeholderConfig == EGL_NO_CONFIG_KHR) {
521 placeholderConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
522 }
523 std::vector<EGLint> attributes;
524 attributes.reserve(7);
525 attributes.push_back(EGL_WIDTH);
526 attributes.push_back(1);
527 attributes.push_back(EGL_HEIGHT);
528 attributes.push_back(1);
529 if (protection == Protection::PROTECTED) {
530 attributes.push_back(EGL_PROTECTED_CONTENT_EXT);
531 attributes.push_back(EGL_TRUE);
532 }
533 attributes.push_back(EGL_NONE);
534
535 return eglCreatePbufferSurface(display, placeholderConfig, attributes.data());
536}
537
Alec Mourid6f09462020-12-07 11:18:17 -0800538int SkiaGLRenderEngine::getContextPriority() {
539 int value;
540 eglQueryContext(mEGLDisplay, mEGLContext, EGL_CONTEXT_PRIORITY_LEVEL_IMG, &value);
541 return value;
542}
543
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700544void SkiaGLRenderEngine::appendBackendSpecificInfoToDump(std::string& result) {
Alec Mourie2b61c62023-08-15 19:04:54 +0000545 const GLExtensions& extensions = GLExtensions::getInstance();
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700546 StringAppendF(&result, "\n ------------RE GLES------------\n");
Ana Krulec1d12b3b2021-01-27 16:49:51 -0800547 StringAppendF(&result, "EGL implementation : %s\n", extensions.getEGLVersion());
548 StringAppendF(&result, "%s\n", extensions.getEGLExtensions());
549 StringAppendF(&result, "GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
550 extensions.getVersion());
551 StringAppendF(&result, "%s\n", extensions.getExtensions());
Ana Krulec1d12b3b2021-01-27 16:49:51 -0800552}
553
John Reck67b1e2b2020-08-26 13:17:24 -0700554} // namespace skia
555} // namespace renderengine
Galia Peycheva6c460652020-11-03 19:42:42 +0100556} // namespace android