blob: cc1d12bc5c9c8177d247d48de35360dae5efdc75 [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>
Alec Mouricbd30932021-06-09 15:52:25 -070030#include <gui/TraceUtils.h>
Alec Mouri4ce5ec02021-01-07 17:33:21 -080031#include <sync/sync.h>
Ana Krulec1d12b3b2021-01-27 16:49:51 -080032#include <ui/DebugUtils.h>
Alec Mouri4ce5ec02021-01-07 17:33:21 -080033#include <utils/Trace.h>
Alec Mourib5777452020-09-28 11:32:42 -070034
35#include <cmath>
Alec Mouri4ce5ec02021-01-07 17:33:21 -080036#include <cstdint>
37#include <memory>
Alec Mouricdf6cbc2021-11-01 17:21:15 -070038#include <numeric>
Alec Mouri4ce5ec02021-01-07 17:33:21 -080039
Alec Mourie2b61c62023-08-15 19:04:54 +000040#include "GLExtensions.h"
Alec Mouri4ce5ec02021-01-07 17:33:21 -080041#include "log/log_main.h"
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -040042
John Reck67b1e2b2020-08-26 13:17:24 -070043namespace android {
44namespace renderengine {
45namespace skia {
46
Ana Krulec1d12b3b2021-01-27 16:49:51 -080047using base::StringAppendF;
48
Alec Mourie2b61c62023-08-15 19:04:54 +000049static bool checkGlError(const char* op, int lineNumber) {
50 bool errorFound = false;
51 GLint error = glGetError();
52 while (error != GL_NO_ERROR) {
53 errorFound = true;
54 error = glGetError();
55 ALOGV("after %s() (line # %d) glError (0x%x)\n", op, lineNumber, error);
56 }
57 return errorFound;
58}
59
John Reck67b1e2b2020-08-26 13:17:24 -070060static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute,
61 EGLint wanted, EGLConfig* outConfig) {
62 EGLint numConfigs = -1, n = 0;
63 eglGetConfigs(dpy, nullptr, 0, &numConfigs);
64 std::vector<EGLConfig> configs(numConfigs, EGL_NO_CONFIG_KHR);
65 eglChooseConfig(dpy, attrs, configs.data(), configs.size(), &n);
66 configs.resize(n);
67
68 if (!configs.empty()) {
69 if (attribute != EGL_NONE) {
70 for (EGLConfig config : configs) {
71 EGLint value = 0;
72 eglGetConfigAttrib(dpy, config, attribute, &value);
73 if (wanted == value) {
74 *outConfig = config;
75 return NO_ERROR;
76 }
77 }
78 } else {
79 // just pick the first one
80 *outConfig = configs[0];
81 return NO_ERROR;
82 }
83 }
84
85 return NAME_NOT_FOUND;
86}
87
88static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType,
89 EGLConfig* config) {
90 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
91 // it is to be used with WIFI displays
92 status_t err;
93 EGLint wantedAttribute;
94 EGLint wantedAttributeValue;
95
96 std::vector<EGLint> attribs;
97 if (renderableType) {
98 const ui::PixelFormat pixelFormat = static_cast<ui::PixelFormat>(format);
99 const bool is1010102 = pixelFormat == ui::PixelFormat::RGBA_1010102;
100
101 // Default to 8 bits per channel.
102 const EGLint tmpAttribs[] = {
103 EGL_RENDERABLE_TYPE,
104 renderableType,
105 EGL_RECORDABLE_ANDROID,
106 EGL_TRUE,
107 EGL_SURFACE_TYPE,
108 EGL_WINDOW_BIT | EGL_PBUFFER_BIT,
109 EGL_FRAMEBUFFER_TARGET_ANDROID,
110 EGL_TRUE,
111 EGL_RED_SIZE,
112 is1010102 ? 10 : 8,
113 EGL_GREEN_SIZE,
114 is1010102 ? 10 : 8,
115 EGL_BLUE_SIZE,
116 is1010102 ? 10 : 8,
117 EGL_ALPHA_SIZE,
118 is1010102 ? 2 : 8,
119 EGL_NONE,
120 };
121 std::copy(tmpAttribs, tmpAttribs + (sizeof(tmpAttribs) / sizeof(EGLint)),
122 std::back_inserter(attribs));
123 wantedAttribute = EGL_NONE;
124 wantedAttributeValue = EGL_NONE;
125 } else {
126 // if no renderable type specified, fallback to a simplified query
127 wantedAttribute = EGL_NATIVE_VISUAL_ID;
128 wantedAttributeValue = format;
129 }
130
131 err = selectConfigForAttribute(display, attribs.data(), wantedAttribute, wantedAttributeValue,
132 config);
133 if (err == NO_ERROR) {
134 EGLint caveat;
135 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
136 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
137 }
138
139 return err;
140}
141
142std::unique_ptr<SkiaGLRenderEngine> SkiaGLRenderEngine::create(
143 const RenderEngineCreationArgs& args) {
144 // initialize EGL for the default display
145 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
146 if (!eglInitialize(display, nullptr, nullptr)) {
147 LOG_ALWAYS_FATAL("failed to initialize EGL");
148 }
149
Yiwei Zhange2650962020-12-01 23:27:58 +0000150 const auto eglVersion = eglQueryString(display, EGL_VERSION);
John Reck67b1e2b2020-08-26 13:17:24 -0700151 if (!eglVersion) {
152 checkGlError(__FUNCTION__, __LINE__);
Yiwei Zhange2650962020-12-01 23:27:58 +0000153 LOG_ALWAYS_FATAL("eglQueryString(EGL_VERSION) failed");
John Reck67b1e2b2020-08-26 13:17:24 -0700154 }
155
Yiwei Zhange2650962020-12-01 23:27:58 +0000156 const auto eglExtensions = eglQueryString(display, EGL_EXTENSIONS);
John Reck67b1e2b2020-08-26 13:17:24 -0700157 if (!eglExtensions) {
158 checkGlError(__FUNCTION__, __LINE__);
Yiwei Zhange2650962020-12-01 23:27:58 +0000159 LOG_ALWAYS_FATAL("eglQueryString(EGL_EXTENSIONS) failed");
John Reck67b1e2b2020-08-26 13:17:24 -0700160 }
161
Alec Mourie2b61c62023-08-15 19:04:54 +0000162 auto& extensions = GLExtensions::getInstance();
John Reck67b1e2b2020-08-26 13:17:24 -0700163 extensions.initWithEGLStrings(eglVersion, eglExtensions);
164
165 // The code assumes that ES2 or later is available if this extension is
166 // supported.
167 EGLConfig config = EGL_NO_CONFIG_KHR;
168 if (!extensions.hasNoConfigContext()) {
169 config = chooseEglConfig(display, args.pixelFormat, /*logConfig*/ true);
170 }
171
John Reck67b1e2b2020-08-26 13:17:24 -0700172 EGLContext protectedContext = EGL_NO_CONTEXT;
Alec Mourid6f09462020-12-07 11:18:17 -0800173 const std::optional<RenderEngine::ContextPriority> priority = createContextPriority(args);
John Reck67b1e2b2020-08-26 13:17:24 -0700174 if (args.enableProtectedContext && extensions.hasProtectedContent()) {
Alec Mourid6f09462020-12-07 11:18:17 -0800175 protectedContext =
176 createEglContext(display, config, nullptr, priority, Protection::PROTECTED);
John Reck67b1e2b2020-08-26 13:17:24 -0700177 ALOGE_IF(protectedContext == EGL_NO_CONTEXT, "Can't create protected context");
178 }
179
Alec Mourid6f09462020-12-07 11:18:17 -0800180 EGLContext ctxt =
181 createEglContext(display, config, protectedContext, priority, Protection::UNPROTECTED);
John Reck67b1e2b2020-08-26 13:17:24 -0700182
183 // if can't create a GL context, we can only abort.
184 LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
185
186 EGLSurface placeholder = EGL_NO_SURFACE;
187 if (!extensions.hasSurfacelessContext()) {
188 placeholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat,
189 Protection::UNPROTECTED);
190 LOG_ALWAYS_FATAL_IF(placeholder == EGL_NO_SURFACE, "can't create placeholder pbuffer");
191 }
192 EGLBoolean success = eglMakeCurrent(display, placeholder, placeholder, ctxt);
193 LOG_ALWAYS_FATAL_IF(!success, "can't make placeholder pbuffer current");
194 extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER),
195 glGetString(GL_VERSION), glGetString(GL_EXTENSIONS));
196
197 EGLSurface protectedPlaceholder = EGL_NO_SURFACE;
198 if (protectedContext != EGL_NO_CONTEXT && !extensions.hasSurfacelessContext()) {
199 protectedPlaceholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat,
200 Protection::PROTECTED);
201 ALOGE_IF(protectedPlaceholder == EGL_NO_SURFACE,
202 "can't create protected placeholder pbuffer");
203 }
204
205 // initialize the renderer while GL is current
Ian Elliott1f0911e2022-09-09 16:31:47 -0600206 std::unique_ptr<SkiaGLRenderEngine> engine(new SkiaGLRenderEngine(args, display, ctxt,
207 placeholder, protectedContext,
208 protectedPlaceholder));
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700209 engine->ensureGrContextsCreated();
John Reck67b1e2b2020-08-26 13:17:24 -0700210
211 ALOGI("OpenGL ES informations:");
212 ALOGI("vendor : %s", extensions.getVendor());
213 ALOGI("renderer : %s", extensions.getRenderer());
214 ALOGI("version : %s", extensions.getVersion());
215 ALOGI("extensions: %s", extensions.getExtensions());
216 ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
217 ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
218
219 return engine;
220}
221
222EGLConfig SkiaGLRenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
223 status_t err;
224 EGLConfig config;
225
226 // First try to get an ES3 config
227 err = selectEGLConfig(display, format, EGL_OPENGL_ES3_BIT, &config);
228 if (err != NO_ERROR) {
229 // If ES3 fails, try to get an ES2 config
230 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
231 if (err != NO_ERROR) {
232 // If ES2 still doesn't work, probably because we're on the emulator.
233 // try a simplified query
234 ALOGW("no suitable EGLConfig found, trying a simpler query");
235 err = selectEGLConfig(display, format, 0, &config);
236 if (err != NO_ERROR) {
237 // this EGL is too lame for android
238 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
239 }
240 }
241 }
242
243 if (logConfig) {
244 // print some debugging info
245 EGLint r, g, b, a;
246 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
247 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
248 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
249 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
250 ALOGI("EGL information:");
251 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
252 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
253 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
254 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
255 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
256 }
257
258 return config;
259}
260
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700261SkiaGLRenderEngine::SkiaGLRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display,
Lucas Dupind508e472020-11-04 04:32:06 +0000262 EGLContext ctxt, EGLSurface placeholder,
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700263 EGLContext protectedContext, EGLSurface protectedPlaceholder)
Alec Mouri47bcb072023-08-15 02:02:49 +0000264 : SkiaRenderEngine(args.renderEngineType, static_cast<PixelFormat>(args.pixelFormat),
265 args.supportsBackgroundBlur),
Alec Mouri0d995102021-02-24 16:53:38 -0800266 mEGLDisplay(display),
John Reck67b1e2b2020-08-26 13:17:24 -0700267 mEGLContext(ctxt),
268 mPlaceholderSurface(placeholder),
269 mProtectedEGLContext(protectedContext),
Alec Mouri47bcb072023-08-15 02:02:49 +0000270 mProtectedPlaceholderSurface(protectedPlaceholder) {}
Alec Mouric0aae732021-01-12 13:32:18 -0800271
272SkiaGLRenderEngine::~SkiaGLRenderEngine() {
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700273 finishRenderingAndAbandonContext();
Alec Mouric0aae732021-01-12 13:32:18 -0800274 if (mPlaceholderSurface != EGL_NO_SURFACE) {
275 eglDestroySurface(mEGLDisplay, mPlaceholderSurface);
276 }
277 if (mProtectedPlaceholderSurface != EGL_NO_SURFACE) {
278 eglDestroySurface(mEGLDisplay, mProtectedPlaceholderSurface);
279 }
280 if (mEGLContext != EGL_NO_CONTEXT) {
281 eglDestroyContext(mEGLDisplay, mEGLContext);
282 }
283 if (mProtectedEGLContext != EGL_NO_CONTEXT) {
284 eglDestroyContext(mEGLDisplay, mProtectedEGLContext);
285 }
286 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
287 eglTerminate(mEGLDisplay);
288 eglReleaseThread();
John Reck67b1e2b2020-08-26 13:17:24 -0700289}
290
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700291SkiaRenderEngine::Contexts SkiaGLRenderEngine::createDirectContexts(
292 const GrContextOptions& options) {
293
294 LOG_ALWAYS_FATAL_IF(isProtected(),
295 "Cannot setup contexts while already in protected mode");
296
297 sk_sp<const GrGLInterface> glInterface = GrGLMakeNativeInterface();
298
299 LOG_ALWAYS_FATAL_IF(!glInterface.get(), "GrGLMakeNativeInterface() failed");
300
301 SkiaRenderEngine::Contexts contexts;
302 contexts.first = GrDirectContext::MakeGL(glInterface, options);
303 if (supportsProtectedContentImpl()) {
304 useProtectedContextImpl(GrProtected::kYes);
305 contexts.second = GrDirectContext::MakeGL(glInterface, options);
306 useProtectedContextImpl(GrProtected::kNo);
307 }
308
309 return contexts;
310}
311
312bool SkiaGLRenderEngine::supportsProtectedContentImpl() const {
Lucas Dupind508e472020-11-04 04:32:06 +0000313 return mProtectedEGLContext != EGL_NO_CONTEXT;
314}
315
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700316bool SkiaGLRenderEngine::useProtectedContextImpl(GrProtected isProtected) {
Lucas Dupind508e472020-11-04 04:32:06 +0000317 const EGLSurface surface =
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700318 (isProtected == GrProtected::kYes) ?
319 mProtectedPlaceholderSurface : mPlaceholderSurface;
320 const EGLContext context = (isProtected == GrProtected::kYes) ?
321 mProtectedEGLContext : mEGLContext;
Alec Mouric0aae732021-01-12 13:32:18 -0800322
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700323 return eglMakeCurrent(mEGLDisplay, surface, surface, context) == EGL_TRUE;
Lucas Dupind508e472020-11-04 04:32:06 +0000324}
325
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700326void SkiaGLRenderEngine::waitFence(GrDirectContext*, base::borrowed_fd fenceFd) {
Alec Mouri4ee7b492021-08-11 10:36:55 -0700327 if (fenceFd.get() >= 0 && !waitGpuFence(fenceFd)) {
328 ATRACE_NAME("SkiaGLRenderEngine::waitFence");
329 sync_wait(fenceFd.get(), -1);
330 }
331}
332
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700333base::unique_fd SkiaGLRenderEngine::flushAndSubmit(GrDirectContext* grContext) {
334 base::unique_fd drawFence = flush();
335
336 bool requireSync = drawFence.get() < 0;
337 if (requireSync) {
338 ATRACE_BEGIN("Submit(sync=true)");
339 } else {
340 ATRACE_BEGIN("Submit(sync=false)");
341 }
Kevin Lubicke9a6d8d2023-09-18 13:36:12 +0000342 bool success = grContext->submit(requireSync ? GrSyncCpu::kYes :
343 GrSyncCpu::kNo);
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700344 ATRACE_END();
345 if (!success) {
346 ALOGE("Failed to flush RenderEngine commands");
347 // Chances are, something illegal happened (Skia's internal GPU object
348 // doesn't exist, or the context was abandoned).
349 return drawFence;
350 }
351
352 return drawFence;
353}
354
Alec Mouri4ee7b492021-08-11 10:36:55 -0700355bool SkiaGLRenderEngine::waitGpuFence(base::borrowed_fd fenceFd) {
Alec Mourie2b61c62023-08-15 19:04:54 +0000356 if (!GLExtensions::getInstance().hasNativeFenceSync() ||
357 !GLExtensions::getInstance().hasWaitSync()) {
John Reck67b1e2b2020-08-26 13:17:24 -0700358 return false;
359 }
360
Alec Mouri4ee7b492021-08-11 10:36:55 -0700361 // Duplicate the fence for passing to eglCreateSyncKHR.
362 base::unique_fd fenceDup(dup(fenceFd.get()));
363 if (fenceDup.get() < 0) {
364 ALOGE("failed to create duplicate fence fd: %d", fenceDup.get());
365 return false;
366 }
367
John Reck67b1e2b2020-08-26 13:17:24 -0700368 // release the fd and transfer the ownership to EGLSync
Alec Mouri4ee7b492021-08-11 10:36:55 -0700369 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceDup.release(), EGL_NONE};
John Reck67b1e2b2020-08-26 13:17:24 -0700370 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
371 if (sync == EGL_NO_SYNC_KHR) {
372 ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
373 return false;
374 }
375
376 // XXX: The spec draft is inconsistent as to whether this should return an
377 // EGLint or void. Ignore the return value for now, as it's not strictly
378 // needed.
379 eglWaitSyncKHR(mEGLDisplay, sync, 0);
380 EGLint error = eglGetError();
381 eglDestroySyncKHR(mEGLDisplay, sync);
382 if (error != EGL_SUCCESS) {
383 ALOGE("failed to wait for EGL native fence sync: %#x", error);
384 return false;
385 }
386
387 return true;
388}
389
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700390base::unique_fd SkiaGLRenderEngine::flush() {
Ana Krulecdfec8f52021-01-13 12:51:47 -0800391 ATRACE_CALL();
Alec Mourie2b61c62023-08-15 19:04:54 +0000392 if (!GLExtensions::getInstance().hasNativeFenceSync()) {
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700393 return base::unique_fd();
John Reckcdb4ed72021-02-04 13:39:33 -0500394 }
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700395
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700396 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
397 if (sync == EGL_NO_SYNC_KHR) {
398 ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
399 return base::unique_fd();
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500400 }
401
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700402 // native fence fd will not be populated until flush() is done.
403 glFlush();
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500404
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700405 // get the fence fd
406 base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
407 eglDestroySyncKHR(mEGLDisplay, sync);
408 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
409 ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
Derek Sollenberger7c42bef2021-02-23 13:01:39 -0500410 }
411
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700412 return fenceFd;
Lucas Dupin3f11e922020-09-22 17:31:04 -0700413}
414
John Reck67b1e2b2020-08-26 13:17:24 -0700415EGLContext SkiaGLRenderEngine::createEglContext(EGLDisplay display, EGLConfig config,
Alec Mourid6f09462020-12-07 11:18:17 -0800416 EGLContext shareContext,
417 std::optional<ContextPriority> contextPriority,
John Reck67b1e2b2020-08-26 13:17:24 -0700418 Protection protection) {
419 EGLint renderableType = 0;
420 if (config == EGL_NO_CONFIG_KHR) {
421 renderableType = EGL_OPENGL_ES3_BIT;
422 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
423 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
424 }
425 EGLint contextClientVersion = 0;
426 if (renderableType & EGL_OPENGL_ES3_BIT) {
427 contextClientVersion = 3;
428 } else if (renderableType & EGL_OPENGL_ES2_BIT) {
429 contextClientVersion = 2;
430 } else if (renderableType & EGL_OPENGL_ES_BIT) {
431 contextClientVersion = 1;
432 } else {
433 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
434 }
435
436 std::vector<EGLint> contextAttributes;
437 contextAttributes.reserve(7);
438 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
439 contextAttributes.push_back(contextClientVersion);
Alec Mourid6f09462020-12-07 11:18:17 -0800440 if (contextPriority) {
John Reck67b1e2b2020-08-26 13:17:24 -0700441 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
Alec Mourid6f09462020-12-07 11:18:17 -0800442 switch (*contextPriority) {
443 case ContextPriority::REALTIME:
444 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_REALTIME_NV);
445 break;
446 case ContextPriority::MEDIUM:
447 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_MEDIUM_IMG);
448 break;
449 case ContextPriority::LOW:
450 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LOW_IMG);
451 break;
452 case ContextPriority::HIGH:
453 default:
454 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
455 break;
456 }
John Reck67b1e2b2020-08-26 13:17:24 -0700457 }
458 if (protection == Protection::PROTECTED) {
459 contextAttributes.push_back(EGL_PROTECTED_CONTENT_EXT);
460 contextAttributes.push_back(EGL_TRUE);
461 }
462 contextAttributes.push_back(EGL_NONE);
463
464 EGLContext context = eglCreateContext(display, config, shareContext, contextAttributes.data());
465
466 if (contextClientVersion == 3 && context == EGL_NO_CONTEXT) {
467 // eglGetConfigAttrib indicated we can create GLES 3 context, but we failed, thus
468 // EGL_NO_CONTEXT so that we can abort.
469 if (config != EGL_NO_CONFIG_KHR) {
470 return context;
471 }
472 // If |config| is EGL_NO_CONFIG_KHR, we speculatively try to create GLES 3 context, so we
473 // should try to fall back to GLES 2.
474 contextAttributes[1] = 2;
475 context = eglCreateContext(display, config, shareContext, contextAttributes.data());
476 }
477
478 return context;
479}
480
Alec Mourid6f09462020-12-07 11:18:17 -0800481std::optional<RenderEngine::ContextPriority> SkiaGLRenderEngine::createContextPriority(
482 const RenderEngineCreationArgs& args) {
Alec Mourie2b61c62023-08-15 19:04:54 +0000483 if (!GLExtensions::getInstance().hasContextPriority()) {
Alec Mourid6f09462020-12-07 11:18:17 -0800484 return std::nullopt;
485 }
486
487 switch (args.contextPriority) {
488 case RenderEngine::ContextPriority::REALTIME:
Alec Mourie2b61c62023-08-15 19:04:54 +0000489 if (GLExtensions::getInstance().hasRealtimePriority()) {
Alec Mourid6f09462020-12-07 11:18:17 -0800490 return RenderEngine::ContextPriority::REALTIME;
491 } else {
492 ALOGI("Realtime priority unsupported, degrading gracefully to high priority");
493 return RenderEngine::ContextPriority::HIGH;
494 }
495 case RenderEngine::ContextPriority::HIGH:
496 case RenderEngine::ContextPriority::MEDIUM:
497 case RenderEngine::ContextPriority::LOW:
498 return args.contextPriority;
499 default:
500 return std::nullopt;
501 }
502}
503
John Reck67b1e2b2020-08-26 13:17:24 -0700504EGLSurface SkiaGLRenderEngine::createPlaceholderEglPbufferSurface(EGLDisplay display,
505 EGLConfig config, int hwcFormat,
506 Protection protection) {
507 EGLConfig placeholderConfig = config;
508 if (placeholderConfig == EGL_NO_CONFIG_KHR) {
509 placeholderConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
510 }
511 std::vector<EGLint> attributes;
512 attributes.reserve(7);
513 attributes.push_back(EGL_WIDTH);
514 attributes.push_back(1);
515 attributes.push_back(EGL_HEIGHT);
516 attributes.push_back(1);
517 if (protection == Protection::PROTECTED) {
518 attributes.push_back(EGL_PROTECTED_CONTENT_EXT);
519 attributes.push_back(EGL_TRUE);
520 }
521 attributes.push_back(EGL_NONE);
522
523 return eglCreatePbufferSurface(display, placeholderConfig, attributes.data());
524}
525
Alec Mourid6f09462020-12-07 11:18:17 -0800526int SkiaGLRenderEngine::getContextPriority() {
527 int value;
528 eglQueryContext(mEGLDisplay, mEGLContext, EGL_CONTEXT_PRIORITY_LEVEL_IMG, &value);
529 return value;
530}
531
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700532void SkiaGLRenderEngine::appendBackendSpecificInfoToDump(std::string& result) {
Alec Mourie2b61c62023-08-15 19:04:54 +0000533 const GLExtensions& extensions = GLExtensions::getInstance();
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700534 StringAppendF(&result, "\n ------------RE GLES------------\n");
Ana Krulec1d12b3b2021-01-27 16:49:51 -0800535 StringAppendF(&result, "EGL implementation : %s\n", extensions.getEGLVersion());
536 StringAppendF(&result, "%s\n", extensions.getEGLExtensions());
537 StringAppendF(&result, "GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
538 extensions.getVersion());
539 StringAppendF(&result, "%s\n", extensions.getExtensions());
Ana Krulec1d12b3b2021-01-27 16:49:51 -0800540}
541
John Reck67b1e2b2020-08-26 13:17:24 -0700542} // namespace skia
543} // namespace renderengine
Galia Peycheva6c460652020-11-03 19:42:42 +0100544} // namespace android