blob: d688b517939f0d596fc09bef7dd161ecda2f7b76 [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>
John Reck67b1e2b2020-08-26 13:17:24 -070026#include <GrContextOptions.h>
Kevin Lubicke9a6d8d2023-09-18 13:36:12 +000027#include <GrTypes.h>
Ana Krulec1d12b3b2021-01-27 16:49:51 -080028#include <android-base/stringprintf.h>
Alec Mourib5777452020-09-28 11:32:42 -070029#include <gl/GrGLInterface.h>
Kevin Lubickd19875f2023-09-27 15:02:47 +000030#include <include/gpu/ganesh/gl/GrGLDirectContext.h>
Alec Mouricbd30932021-06-09 15:52:25 -070031#include <gui/TraceUtils.h>
Alec Mouri4ce5ec02021-01-07 17:33:21 -080032#include <sync/sync.h>
Ana Krulec1d12b3b2021-01-27 16:49:51 -080033#include <ui/DebugUtils.h>
Alec Mouri4ce5ec02021-01-07 17:33:21 -080034#include <utils/Trace.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"
Alec Mouri4ce5ec02021-01-07 17:33:21 -080042#include "log/log_main.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));
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700210 engine->ensureGrContextsCreated();
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)
Alec Mouri47bcb072023-08-15 02:02:49 +0000271 : SkiaRenderEngine(args.renderEngineType, static_cast<PixelFormat>(args.pixelFormat),
272 args.supportsBackgroundBlur),
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() {
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700280 finishRenderingAndAbandonContext();
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
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700298SkiaRenderEngine::Contexts SkiaGLRenderEngine::createDirectContexts(
299 const GrContextOptions& options) {
300
301 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;
Kevin Lubickd19875f2023-09-27 15:02:47 +0000309 contexts.first = GrDirectContexts::MakeGL(glInterface, options);
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700310 if (supportsProtectedContentImpl()) {
311 useProtectedContextImpl(GrProtected::kYes);
Kevin Lubickd19875f2023-09-27 15:02:47 +0000312 contexts.second = GrDirectContexts::MakeGL(glInterface, options);
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
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700333void SkiaGLRenderEngine::waitFence(GrDirectContext*, 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
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700340base::unique_fd SkiaGLRenderEngine::flushAndSubmit(GrDirectContext* grContext) {
341 base::unique_fd drawFence = flush();
342
343 bool requireSync = drawFence.get() < 0;
344 if (requireSync) {
345 ATRACE_BEGIN("Submit(sync=true)");
346 } else {
347 ATRACE_BEGIN("Submit(sync=false)");
348 }
Kevin Lubicke9a6d8d2023-09-18 13:36:12 +0000349 bool success = grContext->submit(requireSync ? GrSyncCpu::kYes :
350 GrSyncCpu::kNo);
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700351 ATRACE_END();
352 if (!success) {
353 ALOGE("Failed to flush RenderEngine commands");
354 // Chances are, something illegal happened (Skia's internal GPU object
355 // doesn't exist, or the context was abandoned).
356 return drawFence;
357 }
358
359 return drawFence;
360}
361
Alec Mouri4ee7b492021-08-11 10:36:55 -0700362bool SkiaGLRenderEngine::waitGpuFence(base::borrowed_fd fenceFd) {
Alec Mourie2b61c62023-08-15 19:04:54 +0000363 if (!GLExtensions::getInstance().hasNativeFenceSync() ||
364 !GLExtensions::getInstance().hasWaitSync()) {
John Reck67b1e2b2020-08-26 13:17:24 -0700365 return false;
366 }
367
Alec Mouri4ee7b492021-08-11 10:36:55 -0700368 // Duplicate the fence for passing to eglCreateSyncKHR.
369 base::unique_fd fenceDup(dup(fenceFd.get()));
370 if (fenceDup.get() < 0) {
371 ALOGE("failed to create duplicate fence fd: %d", fenceDup.get());
372 return false;
373 }
374
John Reck67b1e2b2020-08-26 13:17:24 -0700375 // release the fd and transfer the ownership to EGLSync
Alec Mouri4ee7b492021-08-11 10:36:55 -0700376 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceDup.release(), EGL_NONE};
John Reck67b1e2b2020-08-26 13:17:24 -0700377 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
378 if (sync == EGL_NO_SYNC_KHR) {
379 ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
380 return false;
381 }
382
383 // XXX: The spec draft is inconsistent as to whether this should return an
384 // EGLint or void. Ignore the return value for now, as it's not strictly
385 // needed.
386 eglWaitSyncKHR(mEGLDisplay, sync, 0);
387 EGLint error = eglGetError();
388 eglDestroySyncKHR(mEGLDisplay, sync);
389 if (error != EGL_SUCCESS) {
390 ALOGE("failed to wait for EGL native fence sync: %#x", error);
391 return false;
392 }
393
394 return true;
395}
396
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700397base::unique_fd SkiaGLRenderEngine::flush() {
Ana Krulecdfec8f52021-01-13 12:51:47 -0800398 ATRACE_CALL();
Alec Mourie2b61c62023-08-15 19:04:54 +0000399 if (!GLExtensions::getInstance().hasNativeFenceSync()) {
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700400 return base::unique_fd();
John Reckcdb4ed72021-02-04 13:39:33 -0500401 }
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700402
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700403 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
404 if (sync == EGL_NO_SYNC_KHR) {
405 ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
406 return base::unique_fd();
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500407 }
408
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700409 // native fence fd will not be populated until flush() is done.
410 glFlush();
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500411
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700412 // get the fence fd
413 base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
414 eglDestroySyncKHR(mEGLDisplay, sync);
415 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
416 ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
Derek Sollenberger7c42bef2021-02-23 13:01:39 -0500417 }
418
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700419 return fenceFd;
Lucas Dupin3f11e922020-09-22 17:31:04 -0700420}
421
John Reck67b1e2b2020-08-26 13:17:24 -0700422EGLContext SkiaGLRenderEngine::createEglContext(EGLDisplay display, EGLConfig config,
Alec Mourid6f09462020-12-07 11:18:17 -0800423 EGLContext shareContext,
424 std::optional<ContextPriority> contextPriority,
John Reck67b1e2b2020-08-26 13:17:24 -0700425 Protection protection) {
426 EGLint renderableType = 0;
427 if (config == EGL_NO_CONFIG_KHR) {
428 renderableType = EGL_OPENGL_ES3_BIT;
429 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
430 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
431 }
432 EGLint contextClientVersion = 0;
433 if (renderableType & EGL_OPENGL_ES3_BIT) {
434 contextClientVersion = 3;
435 } else if (renderableType & EGL_OPENGL_ES2_BIT) {
436 contextClientVersion = 2;
437 } else if (renderableType & EGL_OPENGL_ES_BIT) {
438 contextClientVersion = 1;
439 } else {
440 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
441 }
442
443 std::vector<EGLint> contextAttributes;
444 contextAttributes.reserve(7);
445 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
446 contextAttributes.push_back(contextClientVersion);
Alec Mourid6f09462020-12-07 11:18:17 -0800447 if (contextPriority) {
John Reck67b1e2b2020-08-26 13:17:24 -0700448 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
Alec Mourid6f09462020-12-07 11:18:17 -0800449 switch (*contextPriority) {
450 case ContextPriority::REALTIME:
451 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_REALTIME_NV);
452 break;
453 case ContextPriority::MEDIUM:
454 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_MEDIUM_IMG);
455 break;
456 case ContextPriority::LOW:
457 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LOW_IMG);
458 break;
459 case ContextPriority::HIGH:
460 default:
461 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
462 break;
463 }
John Reck67b1e2b2020-08-26 13:17:24 -0700464 }
465 if (protection == Protection::PROTECTED) {
466 contextAttributes.push_back(EGL_PROTECTED_CONTENT_EXT);
467 contextAttributes.push_back(EGL_TRUE);
468 }
469 contextAttributes.push_back(EGL_NONE);
470
471 EGLContext context = eglCreateContext(display, config, shareContext, contextAttributes.data());
472
473 if (contextClientVersion == 3 && context == EGL_NO_CONTEXT) {
474 // eglGetConfigAttrib indicated we can create GLES 3 context, but we failed, thus
475 // EGL_NO_CONTEXT so that we can abort.
476 if (config != EGL_NO_CONFIG_KHR) {
477 return context;
478 }
479 // If |config| is EGL_NO_CONFIG_KHR, we speculatively try to create GLES 3 context, so we
480 // should try to fall back to GLES 2.
481 contextAttributes[1] = 2;
482 context = eglCreateContext(display, config, shareContext, contextAttributes.data());
483 }
484
485 return context;
486}
487
Alec Mourid6f09462020-12-07 11:18:17 -0800488std::optional<RenderEngine::ContextPriority> SkiaGLRenderEngine::createContextPriority(
489 const RenderEngineCreationArgs& args) {
Alec Mourie2b61c62023-08-15 19:04:54 +0000490 if (!GLExtensions::getInstance().hasContextPriority()) {
Alec Mourid6f09462020-12-07 11:18:17 -0800491 return std::nullopt;
492 }
493
494 switch (args.contextPriority) {
495 case RenderEngine::ContextPriority::REALTIME:
Alec Mourie2b61c62023-08-15 19:04:54 +0000496 if (GLExtensions::getInstance().hasRealtimePriority()) {
Alec Mourid6f09462020-12-07 11:18:17 -0800497 return RenderEngine::ContextPriority::REALTIME;
498 } else {
499 ALOGI("Realtime priority unsupported, degrading gracefully to high priority");
500 return RenderEngine::ContextPriority::HIGH;
501 }
502 case RenderEngine::ContextPriority::HIGH:
503 case RenderEngine::ContextPriority::MEDIUM:
504 case RenderEngine::ContextPriority::LOW:
505 return args.contextPriority;
506 default:
507 return std::nullopt;
508 }
509}
510
John Reck67b1e2b2020-08-26 13:17:24 -0700511EGLSurface SkiaGLRenderEngine::createPlaceholderEglPbufferSurface(EGLDisplay display,
512 EGLConfig config, int hwcFormat,
513 Protection protection) {
514 EGLConfig placeholderConfig = config;
515 if (placeholderConfig == EGL_NO_CONFIG_KHR) {
516 placeholderConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
517 }
518 std::vector<EGLint> attributes;
519 attributes.reserve(7);
520 attributes.push_back(EGL_WIDTH);
521 attributes.push_back(1);
522 attributes.push_back(EGL_HEIGHT);
523 attributes.push_back(1);
524 if (protection == Protection::PROTECTED) {
525 attributes.push_back(EGL_PROTECTED_CONTENT_EXT);
526 attributes.push_back(EGL_TRUE);
527 }
528 attributes.push_back(EGL_NONE);
529
530 return eglCreatePbufferSurface(display, placeholderConfig, attributes.data());
531}
532
Alec Mourid6f09462020-12-07 11:18:17 -0800533int SkiaGLRenderEngine::getContextPriority() {
534 int value;
535 eglQueryContext(mEGLDisplay, mEGLContext, EGL_CONTEXT_PRIORITY_LEVEL_IMG, &value);
536 return value;
537}
538
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700539void SkiaGLRenderEngine::appendBackendSpecificInfoToDump(std::string& result) {
Alec Mourie2b61c62023-08-15 19:04:54 +0000540 const GLExtensions& extensions = GLExtensions::getInstance();
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700541 StringAppendF(&result, "\n ------------RE GLES------------\n");
Ana Krulec1d12b3b2021-01-27 16:49:51 -0800542 StringAppendF(&result, "EGL implementation : %s\n", extensions.getEGLVersion());
543 StringAppendF(&result, "%s\n", extensions.getEGLExtensions());
544 StringAppendF(&result, "GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
545 extensions.getVersion());
546 StringAppendF(&result, "%s\n", extensions.getExtensions());
Ana Krulec1d12b3b2021-01-27 16:49:51 -0800547}
548
John Reck67b1e2b2020-08-26 13:17:24 -0700549} // namespace skia
550} // namespace renderengine
Galia Peycheva6c460652020-11-03 19:42:42 +0100551} // namespace android