blob: 2053c6a34fe4b906ffe6e1de1d5b0b8b5d57eb08 [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
239 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
240 }
241 }
242 }
243
244 if (logConfig) {
245 // print some debugging info
246 EGLint r, g, b, a;
247 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
248 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
249 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
250 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
251 ALOGI("EGL information:");
252 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
253 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
254 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
255 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
256 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
257 }
258
259 return config;
260}
261
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700262SkiaGLRenderEngine::SkiaGLRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display,
Lucas Dupind508e472020-11-04 04:32:06 +0000263 EGLContext ctxt, EGLSurface placeholder,
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700264 EGLContext protectedContext, EGLSurface protectedPlaceholder)
Alec Mouri47bcb072023-08-15 02:02:49 +0000265 : SkiaRenderEngine(args.renderEngineType, static_cast<PixelFormat>(args.pixelFormat),
266 args.supportsBackgroundBlur),
Alec Mouri0d995102021-02-24 16:53:38 -0800267 mEGLDisplay(display),
John Reck67b1e2b2020-08-26 13:17:24 -0700268 mEGLContext(ctxt),
269 mPlaceholderSurface(placeholder),
270 mProtectedEGLContext(protectedContext),
Alec Mouri47bcb072023-08-15 02:02:49 +0000271 mProtectedPlaceholderSurface(protectedPlaceholder) {}
Alec Mouric0aae732021-01-12 13:32:18 -0800272
273SkiaGLRenderEngine::~SkiaGLRenderEngine() {
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700274 finishRenderingAndAbandonContext();
Alec Mouric0aae732021-01-12 13:32:18 -0800275 if (mPlaceholderSurface != EGL_NO_SURFACE) {
276 eglDestroySurface(mEGLDisplay, mPlaceholderSurface);
277 }
278 if (mProtectedPlaceholderSurface != EGL_NO_SURFACE) {
279 eglDestroySurface(mEGLDisplay, mProtectedPlaceholderSurface);
280 }
281 if (mEGLContext != EGL_NO_CONTEXT) {
282 eglDestroyContext(mEGLDisplay, mEGLContext);
283 }
284 if (mProtectedEGLContext != EGL_NO_CONTEXT) {
285 eglDestroyContext(mEGLDisplay, mProtectedEGLContext);
286 }
287 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
288 eglTerminate(mEGLDisplay);
289 eglReleaseThread();
John Reck67b1e2b2020-08-26 13:17:24 -0700290}
291
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700292SkiaRenderEngine::Contexts SkiaGLRenderEngine::createDirectContexts(
293 const GrContextOptions& options) {
294
295 LOG_ALWAYS_FATAL_IF(isProtected(),
296 "Cannot setup contexts while already in protected mode");
297
298 sk_sp<const GrGLInterface> glInterface = GrGLMakeNativeInterface();
299
300 LOG_ALWAYS_FATAL_IF(!glInterface.get(), "GrGLMakeNativeInterface() failed");
301
302 SkiaRenderEngine::Contexts contexts;
Kevin Lubickd19875f2023-09-27 15:02:47 +0000303 contexts.first = GrDirectContexts::MakeGL(glInterface, options);
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700304 if (supportsProtectedContentImpl()) {
305 useProtectedContextImpl(GrProtected::kYes);
Kevin Lubickd19875f2023-09-27 15:02:47 +0000306 contexts.second = GrDirectContexts::MakeGL(glInterface, options);
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700307 useProtectedContextImpl(GrProtected::kNo);
308 }
309
310 return contexts;
311}
312
313bool SkiaGLRenderEngine::supportsProtectedContentImpl() const {
Lucas Dupind508e472020-11-04 04:32:06 +0000314 return mProtectedEGLContext != EGL_NO_CONTEXT;
315}
316
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700317bool SkiaGLRenderEngine::useProtectedContextImpl(GrProtected isProtected) {
Lucas Dupind508e472020-11-04 04:32:06 +0000318 const EGLSurface surface =
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700319 (isProtected == GrProtected::kYes) ?
320 mProtectedPlaceholderSurface : mPlaceholderSurface;
321 const EGLContext context = (isProtected == GrProtected::kYes) ?
322 mProtectedEGLContext : mEGLContext;
Alec Mouric0aae732021-01-12 13:32:18 -0800323
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700324 return eglMakeCurrent(mEGLDisplay, surface, surface, context) == EGL_TRUE;
Lucas Dupind508e472020-11-04 04:32:06 +0000325}
326
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700327void SkiaGLRenderEngine::waitFence(GrDirectContext*, base::borrowed_fd fenceFd) {
Alec Mouri4ee7b492021-08-11 10:36:55 -0700328 if (fenceFd.get() >= 0 && !waitGpuFence(fenceFd)) {
329 ATRACE_NAME("SkiaGLRenderEngine::waitFence");
330 sync_wait(fenceFd.get(), -1);
331 }
332}
333
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700334base::unique_fd SkiaGLRenderEngine::flushAndSubmit(GrDirectContext* grContext) {
335 base::unique_fd drawFence = flush();
336
337 bool requireSync = drawFence.get() < 0;
338 if (requireSync) {
339 ATRACE_BEGIN("Submit(sync=true)");
340 } else {
341 ATRACE_BEGIN("Submit(sync=false)");
342 }
Kevin Lubicke9a6d8d2023-09-18 13:36:12 +0000343 bool success = grContext->submit(requireSync ? GrSyncCpu::kYes :
344 GrSyncCpu::kNo);
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700345 ATRACE_END();
346 if (!success) {
347 ALOGE("Failed to flush RenderEngine commands");
348 // Chances are, something illegal happened (Skia's internal GPU object
349 // doesn't exist, or the context was abandoned).
350 return drawFence;
351 }
352
353 return drawFence;
354}
355
Alec Mouri4ee7b492021-08-11 10:36:55 -0700356bool SkiaGLRenderEngine::waitGpuFence(base::borrowed_fd fenceFd) {
Alec Mourie2b61c62023-08-15 19:04:54 +0000357 if (!GLExtensions::getInstance().hasNativeFenceSync() ||
358 !GLExtensions::getInstance().hasWaitSync()) {
John Reck67b1e2b2020-08-26 13:17:24 -0700359 return false;
360 }
361
Alec Mouri4ee7b492021-08-11 10:36:55 -0700362 // Duplicate the fence for passing to eglCreateSyncKHR.
363 base::unique_fd fenceDup(dup(fenceFd.get()));
364 if (fenceDup.get() < 0) {
365 ALOGE("failed to create duplicate fence fd: %d", fenceDup.get());
366 return false;
367 }
368
John Reck67b1e2b2020-08-26 13:17:24 -0700369 // release the fd and transfer the ownership to EGLSync
Alec Mouri4ee7b492021-08-11 10:36:55 -0700370 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceDup.release(), EGL_NONE};
John Reck67b1e2b2020-08-26 13:17:24 -0700371 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
372 if (sync == EGL_NO_SYNC_KHR) {
373 ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
374 return false;
375 }
376
377 // XXX: The spec draft is inconsistent as to whether this should return an
378 // EGLint or void. Ignore the return value for now, as it's not strictly
379 // needed.
380 eglWaitSyncKHR(mEGLDisplay, sync, 0);
381 EGLint error = eglGetError();
382 eglDestroySyncKHR(mEGLDisplay, sync);
383 if (error != EGL_SUCCESS) {
384 ALOGE("failed to wait for EGL native fence sync: %#x", error);
385 return false;
386 }
387
388 return true;
389}
390
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700391base::unique_fd SkiaGLRenderEngine::flush() {
Ana Krulecdfec8f52021-01-13 12:51:47 -0800392 ATRACE_CALL();
Alec Mourie2b61c62023-08-15 19:04:54 +0000393 if (!GLExtensions::getInstance().hasNativeFenceSync()) {
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700394 return base::unique_fd();
John Reckcdb4ed72021-02-04 13:39:33 -0500395 }
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700396
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700397 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
398 if (sync == EGL_NO_SYNC_KHR) {
399 ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
400 return base::unique_fd();
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500401 }
402
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700403 // native fence fd will not be populated until flush() is done.
404 glFlush();
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500405
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700406 // get the fence fd
407 base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
408 eglDestroySyncKHR(mEGLDisplay, sync);
409 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
410 ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
Derek Sollenberger7c42bef2021-02-23 13:01:39 -0500411 }
412
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700413 return fenceFd;
Lucas Dupin3f11e922020-09-22 17:31:04 -0700414}
415
John Reck67b1e2b2020-08-26 13:17:24 -0700416EGLContext SkiaGLRenderEngine::createEglContext(EGLDisplay display, EGLConfig config,
Alec Mourid6f09462020-12-07 11:18:17 -0800417 EGLContext shareContext,
418 std::optional<ContextPriority> contextPriority,
John Reck67b1e2b2020-08-26 13:17:24 -0700419 Protection protection) {
420 EGLint renderableType = 0;
421 if (config == EGL_NO_CONFIG_KHR) {
422 renderableType = EGL_OPENGL_ES3_BIT;
423 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
424 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
425 }
426 EGLint contextClientVersion = 0;
427 if (renderableType & EGL_OPENGL_ES3_BIT) {
428 contextClientVersion = 3;
429 } else if (renderableType & EGL_OPENGL_ES2_BIT) {
430 contextClientVersion = 2;
431 } else if (renderableType & EGL_OPENGL_ES_BIT) {
432 contextClientVersion = 1;
433 } else {
434 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
435 }
436
437 std::vector<EGLint> contextAttributes;
438 contextAttributes.reserve(7);
439 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
440 contextAttributes.push_back(contextClientVersion);
Alec Mourid6f09462020-12-07 11:18:17 -0800441 if (contextPriority) {
John Reck67b1e2b2020-08-26 13:17:24 -0700442 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
Alec Mourid6f09462020-12-07 11:18:17 -0800443 switch (*contextPriority) {
444 case ContextPriority::REALTIME:
445 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_REALTIME_NV);
446 break;
447 case ContextPriority::MEDIUM:
448 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_MEDIUM_IMG);
449 break;
450 case ContextPriority::LOW:
451 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LOW_IMG);
452 break;
453 case ContextPriority::HIGH:
454 default:
455 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
456 break;
457 }
John Reck67b1e2b2020-08-26 13:17:24 -0700458 }
459 if (protection == Protection::PROTECTED) {
460 contextAttributes.push_back(EGL_PROTECTED_CONTENT_EXT);
461 contextAttributes.push_back(EGL_TRUE);
462 }
463 contextAttributes.push_back(EGL_NONE);
464
465 EGLContext context = eglCreateContext(display, config, shareContext, contextAttributes.data());
466
467 if (contextClientVersion == 3 && context == EGL_NO_CONTEXT) {
468 // eglGetConfigAttrib indicated we can create GLES 3 context, but we failed, thus
469 // EGL_NO_CONTEXT so that we can abort.
470 if (config != EGL_NO_CONFIG_KHR) {
471 return context;
472 }
473 // If |config| is EGL_NO_CONFIG_KHR, we speculatively try to create GLES 3 context, so we
474 // should try to fall back to GLES 2.
475 contextAttributes[1] = 2;
476 context = eglCreateContext(display, config, shareContext, contextAttributes.data());
477 }
478
479 return context;
480}
481
Alec Mourid6f09462020-12-07 11:18:17 -0800482std::optional<RenderEngine::ContextPriority> SkiaGLRenderEngine::createContextPriority(
483 const RenderEngineCreationArgs& args) {
Alec Mourie2b61c62023-08-15 19:04:54 +0000484 if (!GLExtensions::getInstance().hasContextPriority()) {
Alec Mourid6f09462020-12-07 11:18:17 -0800485 return std::nullopt;
486 }
487
488 switch (args.contextPriority) {
489 case RenderEngine::ContextPriority::REALTIME:
Alec Mourie2b61c62023-08-15 19:04:54 +0000490 if (GLExtensions::getInstance().hasRealtimePriority()) {
Alec Mourid6f09462020-12-07 11:18:17 -0800491 return RenderEngine::ContextPriority::REALTIME;
492 } else {
493 ALOGI("Realtime priority unsupported, degrading gracefully to high priority");
494 return RenderEngine::ContextPriority::HIGH;
495 }
496 case RenderEngine::ContextPriority::HIGH:
497 case RenderEngine::ContextPriority::MEDIUM:
498 case RenderEngine::ContextPriority::LOW:
499 return args.contextPriority;
500 default:
501 return std::nullopt;
502 }
503}
504
John Reck67b1e2b2020-08-26 13:17:24 -0700505EGLSurface SkiaGLRenderEngine::createPlaceholderEglPbufferSurface(EGLDisplay display,
506 EGLConfig config, int hwcFormat,
507 Protection protection) {
508 EGLConfig placeholderConfig = config;
509 if (placeholderConfig == EGL_NO_CONFIG_KHR) {
510 placeholderConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
511 }
512 std::vector<EGLint> attributes;
513 attributes.reserve(7);
514 attributes.push_back(EGL_WIDTH);
515 attributes.push_back(1);
516 attributes.push_back(EGL_HEIGHT);
517 attributes.push_back(1);
518 if (protection == Protection::PROTECTED) {
519 attributes.push_back(EGL_PROTECTED_CONTENT_EXT);
520 attributes.push_back(EGL_TRUE);
521 }
522 attributes.push_back(EGL_NONE);
523
524 return eglCreatePbufferSurface(display, placeholderConfig, attributes.data());
525}
526
Alec Mourid6f09462020-12-07 11:18:17 -0800527int SkiaGLRenderEngine::getContextPriority() {
528 int value;
529 eglQueryContext(mEGLDisplay, mEGLContext, EGL_CONTEXT_PRIORITY_LEVEL_IMG, &value);
530 return value;
531}
532
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700533void SkiaGLRenderEngine::appendBackendSpecificInfoToDump(std::string& result) {
Alec Mourie2b61c62023-08-15 19:04:54 +0000534 const GLExtensions& extensions = GLExtensions::getInstance();
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700535 StringAppendF(&result, "\n ------------RE GLES------------\n");
Ana Krulec1d12b3b2021-01-27 16:49:51 -0800536 StringAppendF(&result, "EGL implementation : %s\n", extensions.getEGLVersion());
537 StringAppendF(&result, "%s\n", extensions.getEGLExtensions());
538 StringAppendF(&result, "GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
539 extensions.getVersion());
540 StringAppendF(&result, "%s\n", extensions.getExtensions());
Ana Krulec1d12b3b2021-01-27 16:49:51 -0800541}
542
John Reck67b1e2b2020-08-26 13:17:24 -0700543} // namespace skia
544} // namespace renderengine
Galia Peycheva6c460652020-11-03 19:42:42 +0100545} // namespace android