blob: e253ad596e61b7238c8fc1603aad48bf63099c54 [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>
Ana Krulec1d12b3b2021-01-27 16:49:51 -080027#include <android-base/stringprintf.h>
Alec Mourib5777452020-09-28 11:32:42 -070028#include <gl/GrGLInterface.h>
Alec Mouricbd30932021-06-09 15:52:25 -070029#include <gui/TraceUtils.h>
Alec Mouri4ce5ec02021-01-07 17:33:21 -080030#include <sync/sync.h>
Ana Krulec1d12b3b2021-01-27 16:49:51 -080031#include <ui/DebugUtils.h>
Alec Mouri4ce5ec02021-01-07 17:33:21 -080032#include <utils/Trace.h>
Alec Mourib5777452020-09-28 11:32:42 -070033
34#include <cmath>
Alec Mouri4ce5ec02021-01-07 17:33:21 -080035#include <cstdint>
36#include <memory>
Alec Mouricdf6cbc2021-11-01 17:21:15 -070037#include <numeric>
Alec Mouri4ce5ec02021-01-07 17:33:21 -080038
Alec Mourie2b61c62023-08-15 19:04:54 +000039#include "GLExtensions.h"
Alec Mouri4ce5ec02021-01-07 17:33:21 -080040#include "log/log_main.h"
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -040041
John Reck67b1e2b2020-08-26 13:17:24 -070042namespace android {
43namespace renderengine {
44namespace skia {
45
Ana Krulec1d12b3b2021-01-27 16:49:51 -080046using base::StringAppendF;
47
Alec Mourie2b61c62023-08-15 19:04:54 +000048static bool checkGlError(const char* op, int lineNumber) {
49 bool errorFound = false;
50 GLint error = glGetError();
51 while (error != GL_NO_ERROR) {
52 errorFound = true;
53 error = glGetError();
54 ALOGV("after %s() (line # %d) glError (0x%x)\n", op, lineNumber, error);
55 }
56 return errorFound;
57}
58
John Reck67b1e2b2020-08-26 13:17:24 -070059static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute,
60 EGLint wanted, EGLConfig* outConfig) {
61 EGLint numConfigs = -1, n = 0;
62 eglGetConfigs(dpy, nullptr, 0, &numConfigs);
63 std::vector<EGLConfig> configs(numConfigs, EGL_NO_CONFIG_KHR);
64 eglChooseConfig(dpy, attrs, configs.data(), configs.size(), &n);
65 configs.resize(n);
66
67 if (!configs.empty()) {
68 if (attribute != EGL_NONE) {
69 for (EGLConfig config : configs) {
70 EGLint value = 0;
71 eglGetConfigAttrib(dpy, config, attribute, &value);
72 if (wanted == value) {
73 *outConfig = config;
74 return NO_ERROR;
75 }
76 }
77 } else {
78 // just pick the first one
79 *outConfig = configs[0];
80 return NO_ERROR;
81 }
82 }
83
84 return NAME_NOT_FOUND;
85}
86
87static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType,
88 EGLConfig* config) {
89 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
90 // it is to be used with WIFI displays
91 status_t err;
92 EGLint wantedAttribute;
93 EGLint wantedAttributeValue;
94
95 std::vector<EGLint> attribs;
96 if (renderableType) {
97 const ui::PixelFormat pixelFormat = static_cast<ui::PixelFormat>(format);
98 const bool is1010102 = pixelFormat == ui::PixelFormat::RGBA_1010102;
99
100 // Default to 8 bits per channel.
101 const EGLint tmpAttribs[] = {
102 EGL_RENDERABLE_TYPE,
103 renderableType,
104 EGL_RECORDABLE_ANDROID,
105 EGL_TRUE,
106 EGL_SURFACE_TYPE,
107 EGL_WINDOW_BIT | EGL_PBUFFER_BIT,
108 EGL_FRAMEBUFFER_TARGET_ANDROID,
109 EGL_TRUE,
110 EGL_RED_SIZE,
111 is1010102 ? 10 : 8,
112 EGL_GREEN_SIZE,
113 is1010102 ? 10 : 8,
114 EGL_BLUE_SIZE,
115 is1010102 ? 10 : 8,
116 EGL_ALPHA_SIZE,
117 is1010102 ? 2 : 8,
118 EGL_NONE,
119 };
120 std::copy(tmpAttribs, tmpAttribs + (sizeof(tmpAttribs) / sizeof(EGLint)),
121 std::back_inserter(attribs));
122 wantedAttribute = EGL_NONE;
123 wantedAttributeValue = EGL_NONE;
124 } else {
125 // if no renderable type specified, fallback to a simplified query
126 wantedAttribute = EGL_NATIVE_VISUAL_ID;
127 wantedAttributeValue = format;
128 }
129
130 err = selectConfigForAttribute(display, attribs.data(), wantedAttribute, wantedAttributeValue,
131 config);
132 if (err == NO_ERROR) {
133 EGLint caveat;
134 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
135 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
136 }
137
138 return err;
139}
140
141std::unique_ptr<SkiaGLRenderEngine> SkiaGLRenderEngine::create(
142 const RenderEngineCreationArgs& args) {
143 // initialize EGL for the default display
144 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
145 if (!eglInitialize(display, nullptr, nullptr)) {
146 LOG_ALWAYS_FATAL("failed to initialize EGL");
147 }
148
Yiwei Zhange2650962020-12-01 23:27:58 +0000149 const auto eglVersion = eglQueryString(display, EGL_VERSION);
John Reck67b1e2b2020-08-26 13:17:24 -0700150 if (!eglVersion) {
151 checkGlError(__FUNCTION__, __LINE__);
Yiwei Zhange2650962020-12-01 23:27:58 +0000152 LOG_ALWAYS_FATAL("eglQueryString(EGL_VERSION) failed");
John Reck67b1e2b2020-08-26 13:17:24 -0700153 }
154
Yiwei Zhange2650962020-12-01 23:27:58 +0000155 const auto eglExtensions = eglQueryString(display, EGL_EXTENSIONS);
John Reck67b1e2b2020-08-26 13:17:24 -0700156 if (!eglExtensions) {
157 checkGlError(__FUNCTION__, __LINE__);
Yiwei Zhange2650962020-12-01 23:27:58 +0000158 LOG_ALWAYS_FATAL("eglQueryString(EGL_EXTENSIONS) failed");
John Reck67b1e2b2020-08-26 13:17:24 -0700159 }
160
Alec Mourie2b61c62023-08-15 19:04:54 +0000161 auto& extensions = GLExtensions::getInstance();
John Reck67b1e2b2020-08-26 13:17:24 -0700162 extensions.initWithEGLStrings(eglVersion, eglExtensions);
163
164 // The code assumes that ES2 or later is available if this extension is
165 // supported.
166 EGLConfig config = EGL_NO_CONFIG_KHR;
167 if (!extensions.hasNoConfigContext()) {
168 config = chooseEglConfig(display, args.pixelFormat, /*logConfig*/ true);
169 }
170
John Reck67b1e2b2020-08-26 13:17:24 -0700171 EGLContext protectedContext = EGL_NO_CONTEXT;
Alec Mourid6f09462020-12-07 11:18:17 -0800172 const std::optional<RenderEngine::ContextPriority> priority = createContextPriority(args);
John Reck67b1e2b2020-08-26 13:17:24 -0700173 if (args.enableProtectedContext && extensions.hasProtectedContent()) {
Alec Mourid6f09462020-12-07 11:18:17 -0800174 protectedContext =
175 createEglContext(display, config, nullptr, priority, Protection::PROTECTED);
John Reck67b1e2b2020-08-26 13:17:24 -0700176 ALOGE_IF(protectedContext == EGL_NO_CONTEXT, "Can't create protected context");
177 }
178
Alec Mourid6f09462020-12-07 11:18:17 -0800179 EGLContext ctxt =
180 createEglContext(display, config, protectedContext, priority, Protection::UNPROTECTED);
John Reck67b1e2b2020-08-26 13:17:24 -0700181
182 // if can't create a GL context, we can only abort.
183 LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
184
185 EGLSurface placeholder = EGL_NO_SURFACE;
186 if (!extensions.hasSurfacelessContext()) {
187 placeholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat,
188 Protection::UNPROTECTED);
189 LOG_ALWAYS_FATAL_IF(placeholder == EGL_NO_SURFACE, "can't create placeholder pbuffer");
190 }
191 EGLBoolean success = eglMakeCurrent(display, placeholder, placeholder, ctxt);
192 LOG_ALWAYS_FATAL_IF(!success, "can't make placeholder pbuffer current");
193 extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER),
194 glGetString(GL_VERSION), glGetString(GL_EXTENSIONS));
195
196 EGLSurface protectedPlaceholder = EGL_NO_SURFACE;
197 if (protectedContext != EGL_NO_CONTEXT && !extensions.hasSurfacelessContext()) {
198 protectedPlaceholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat,
199 Protection::PROTECTED);
200 ALOGE_IF(protectedPlaceholder == EGL_NO_SURFACE,
201 "can't create protected placeholder pbuffer");
202 }
203
204 // initialize the renderer while GL is current
Ian Elliott1f0911e2022-09-09 16:31:47 -0600205 std::unique_ptr<SkiaGLRenderEngine> engine(new SkiaGLRenderEngine(args, display, ctxt,
206 placeholder, protectedContext,
207 protectedPlaceholder));
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700208 engine->ensureGrContextsCreated();
John Reck67b1e2b2020-08-26 13:17:24 -0700209
210 ALOGI("OpenGL ES informations:");
211 ALOGI("vendor : %s", extensions.getVendor());
212 ALOGI("renderer : %s", extensions.getRenderer());
213 ALOGI("version : %s", extensions.getVersion());
214 ALOGI("extensions: %s", extensions.getExtensions());
215 ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
216 ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
217
218 return engine;
219}
220
221EGLConfig SkiaGLRenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
222 status_t err;
223 EGLConfig config;
224
225 // First try to get an ES3 config
226 err = selectEGLConfig(display, format, EGL_OPENGL_ES3_BIT, &config);
227 if (err != NO_ERROR) {
228 // If ES3 fails, try to get an ES2 config
229 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
230 if (err != NO_ERROR) {
231 // If ES2 still doesn't work, probably because we're on the emulator.
232 // try a simplified query
233 ALOGW("no suitable EGLConfig found, trying a simpler query");
234 err = selectEGLConfig(display, format, 0, &config);
235 if (err != NO_ERROR) {
236 // this EGL is too lame for android
237 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
238 }
239 }
240 }
241
242 if (logConfig) {
243 // print some debugging info
244 EGLint r, g, b, a;
245 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
246 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
247 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
248 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
249 ALOGI("EGL information:");
250 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
251 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
252 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
253 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
254 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
255 }
256
257 return config;
258}
259
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700260SkiaGLRenderEngine::SkiaGLRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display,
Lucas Dupind508e472020-11-04 04:32:06 +0000261 EGLContext ctxt, EGLSurface placeholder,
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700262 EGLContext protectedContext, EGLSurface protectedPlaceholder)
Alec Mouri47bcb072023-08-15 02:02:49 +0000263 : SkiaRenderEngine(args.renderEngineType, static_cast<PixelFormat>(args.pixelFormat),
264 args.supportsBackgroundBlur),
Alec Mouri0d995102021-02-24 16:53:38 -0800265 mEGLDisplay(display),
John Reck67b1e2b2020-08-26 13:17:24 -0700266 mEGLContext(ctxt),
267 mPlaceholderSurface(placeholder),
268 mProtectedEGLContext(protectedContext),
Alec Mouri47bcb072023-08-15 02:02:49 +0000269 mProtectedPlaceholderSurface(protectedPlaceholder) {}
Alec Mouric0aae732021-01-12 13:32:18 -0800270
271SkiaGLRenderEngine::~SkiaGLRenderEngine() {
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700272 finishRenderingAndAbandonContext();
Alec Mouric0aae732021-01-12 13:32:18 -0800273 if (mPlaceholderSurface != EGL_NO_SURFACE) {
274 eglDestroySurface(mEGLDisplay, mPlaceholderSurface);
275 }
276 if (mProtectedPlaceholderSurface != EGL_NO_SURFACE) {
277 eglDestroySurface(mEGLDisplay, mProtectedPlaceholderSurface);
278 }
279 if (mEGLContext != EGL_NO_CONTEXT) {
280 eglDestroyContext(mEGLDisplay, mEGLContext);
281 }
282 if (mProtectedEGLContext != EGL_NO_CONTEXT) {
283 eglDestroyContext(mEGLDisplay, mProtectedEGLContext);
284 }
285 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
286 eglTerminate(mEGLDisplay);
287 eglReleaseThread();
John Reck67b1e2b2020-08-26 13:17:24 -0700288}
289
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700290SkiaRenderEngine::Contexts SkiaGLRenderEngine::createDirectContexts(
291 const GrContextOptions& options) {
292
293 LOG_ALWAYS_FATAL_IF(isProtected(),
294 "Cannot setup contexts while already in protected mode");
295
296 sk_sp<const GrGLInterface> glInterface = GrGLMakeNativeInterface();
297
298 LOG_ALWAYS_FATAL_IF(!glInterface.get(), "GrGLMakeNativeInterface() failed");
299
300 SkiaRenderEngine::Contexts contexts;
301 contexts.first = GrDirectContext::MakeGL(glInterface, options);
302 if (supportsProtectedContentImpl()) {
303 useProtectedContextImpl(GrProtected::kYes);
304 contexts.second = GrDirectContext::MakeGL(glInterface, options);
305 useProtectedContextImpl(GrProtected::kNo);
306 }
307
308 return contexts;
309}
310
311bool SkiaGLRenderEngine::supportsProtectedContentImpl() const {
Lucas Dupind508e472020-11-04 04:32:06 +0000312 return mProtectedEGLContext != EGL_NO_CONTEXT;
313}
314
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700315bool SkiaGLRenderEngine::useProtectedContextImpl(GrProtected isProtected) {
Lucas Dupind508e472020-11-04 04:32:06 +0000316 const EGLSurface surface =
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700317 (isProtected == GrProtected::kYes) ?
318 mProtectedPlaceholderSurface : mPlaceholderSurface;
319 const EGLContext context = (isProtected == GrProtected::kYes) ?
320 mProtectedEGLContext : mEGLContext;
Alec Mouric0aae732021-01-12 13:32:18 -0800321
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700322 return eglMakeCurrent(mEGLDisplay, surface, surface, context) == EGL_TRUE;
Lucas Dupind508e472020-11-04 04:32:06 +0000323}
324
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700325void SkiaGLRenderEngine::waitFence(GrDirectContext*, base::borrowed_fd fenceFd) {
Alec Mouri4ee7b492021-08-11 10:36:55 -0700326 if (fenceFd.get() >= 0 && !waitGpuFence(fenceFd)) {
327 ATRACE_NAME("SkiaGLRenderEngine::waitFence");
328 sync_wait(fenceFd.get(), -1);
329 }
330}
331
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700332base::unique_fd SkiaGLRenderEngine::flushAndSubmit(GrDirectContext* grContext) {
333 base::unique_fd drawFence = flush();
334
335 bool requireSync = drawFence.get() < 0;
336 if (requireSync) {
337 ATRACE_BEGIN("Submit(sync=true)");
338 } else {
339 ATRACE_BEGIN("Submit(sync=false)");
340 }
341 bool success = grContext->submit(requireSync);
342 ATRACE_END();
343 if (!success) {
344 ALOGE("Failed to flush RenderEngine commands");
345 // Chances are, something illegal happened (Skia's internal GPU object
346 // doesn't exist, or the context was abandoned).
347 return drawFence;
348 }
349
350 return drawFence;
351}
352
Alec Mouri4ee7b492021-08-11 10:36:55 -0700353bool SkiaGLRenderEngine::waitGpuFence(base::borrowed_fd fenceFd) {
Alec Mourie2b61c62023-08-15 19:04:54 +0000354 if (!GLExtensions::getInstance().hasNativeFenceSync() ||
355 !GLExtensions::getInstance().hasWaitSync()) {
John Reck67b1e2b2020-08-26 13:17:24 -0700356 return false;
357 }
358
Alec Mouri4ee7b492021-08-11 10:36:55 -0700359 // Duplicate the fence for passing to eglCreateSyncKHR.
360 base::unique_fd fenceDup(dup(fenceFd.get()));
361 if (fenceDup.get() < 0) {
362 ALOGE("failed to create duplicate fence fd: %d", fenceDup.get());
363 return false;
364 }
365
John Reck67b1e2b2020-08-26 13:17:24 -0700366 // release the fd and transfer the ownership to EGLSync
Alec Mouri4ee7b492021-08-11 10:36:55 -0700367 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceDup.release(), EGL_NONE};
John Reck67b1e2b2020-08-26 13:17:24 -0700368 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
369 if (sync == EGL_NO_SYNC_KHR) {
370 ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
371 return false;
372 }
373
374 // XXX: The spec draft is inconsistent as to whether this should return an
375 // EGLint or void. Ignore the return value for now, as it's not strictly
376 // needed.
377 eglWaitSyncKHR(mEGLDisplay, sync, 0);
378 EGLint error = eglGetError();
379 eglDestroySyncKHR(mEGLDisplay, sync);
380 if (error != EGL_SUCCESS) {
381 ALOGE("failed to wait for EGL native fence sync: %#x", error);
382 return false;
383 }
384
385 return true;
386}
387
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700388base::unique_fd SkiaGLRenderEngine::flush() {
Ana Krulecdfec8f52021-01-13 12:51:47 -0800389 ATRACE_CALL();
Alec Mourie2b61c62023-08-15 19:04:54 +0000390 if (!GLExtensions::getInstance().hasNativeFenceSync()) {
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700391 return base::unique_fd();
John Reckcdb4ed72021-02-04 13:39:33 -0500392 }
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700393
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700394 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
395 if (sync == EGL_NO_SYNC_KHR) {
396 ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
397 return base::unique_fd();
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500398 }
399
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700400 // native fence fd will not be populated until flush() is done.
401 glFlush();
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500402
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700403 // get the fence fd
404 base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
405 eglDestroySyncKHR(mEGLDisplay, sync);
406 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
407 ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
Derek Sollenberger7c42bef2021-02-23 13:01:39 -0500408 }
409
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700410 return fenceFd;
Lucas Dupin3f11e922020-09-22 17:31:04 -0700411}
412
John Reck67b1e2b2020-08-26 13:17:24 -0700413EGLContext SkiaGLRenderEngine::createEglContext(EGLDisplay display, EGLConfig config,
Alec Mourid6f09462020-12-07 11:18:17 -0800414 EGLContext shareContext,
415 std::optional<ContextPriority> contextPriority,
John Reck67b1e2b2020-08-26 13:17:24 -0700416 Protection protection) {
417 EGLint renderableType = 0;
418 if (config == EGL_NO_CONFIG_KHR) {
419 renderableType = EGL_OPENGL_ES3_BIT;
420 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
421 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
422 }
423 EGLint contextClientVersion = 0;
424 if (renderableType & EGL_OPENGL_ES3_BIT) {
425 contextClientVersion = 3;
426 } else if (renderableType & EGL_OPENGL_ES2_BIT) {
427 contextClientVersion = 2;
428 } else if (renderableType & EGL_OPENGL_ES_BIT) {
429 contextClientVersion = 1;
430 } else {
431 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
432 }
433
434 std::vector<EGLint> contextAttributes;
435 contextAttributes.reserve(7);
436 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
437 contextAttributes.push_back(contextClientVersion);
Alec Mourid6f09462020-12-07 11:18:17 -0800438 if (contextPriority) {
John Reck67b1e2b2020-08-26 13:17:24 -0700439 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
Alec Mourid6f09462020-12-07 11:18:17 -0800440 switch (*contextPriority) {
441 case ContextPriority::REALTIME:
442 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_REALTIME_NV);
443 break;
444 case ContextPriority::MEDIUM:
445 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_MEDIUM_IMG);
446 break;
447 case ContextPriority::LOW:
448 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LOW_IMG);
449 break;
450 case ContextPriority::HIGH:
451 default:
452 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
453 break;
454 }
John Reck67b1e2b2020-08-26 13:17:24 -0700455 }
456 if (protection == Protection::PROTECTED) {
457 contextAttributes.push_back(EGL_PROTECTED_CONTENT_EXT);
458 contextAttributes.push_back(EGL_TRUE);
459 }
460 contextAttributes.push_back(EGL_NONE);
461
462 EGLContext context = eglCreateContext(display, config, shareContext, contextAttributes.data());
463
464 if (contextClientVersion == 3 && context == EGL_NO_CONTEXT) {
465 // eglGetConfigAttrib indicated we can create GLES 3 context, but we failed, thus
466 // EGL_NO_CONTEXT so that we can abort.
467 if (config != EGL_NO_CONFIG_KHR) {
468 return context;
469 }
470 // If |config| is EGL_NO_CONFIG_KHR, we speculatively try to create GLES 3 context, so we
471 // should try to fall back to GLES 2.
472 contextAttributes[1] = 2;
473 context = eglCreateContext(display, config, shareContext, contextAttributes.data());
474 }
475
476 return context;
477}
478
Alec Mourid6f09462020-12-07 11:18:17 -0800479std::optional<RenderEngine::ContextPriority> SkiaGLRenderEngine::createContextPriority(
480 const RenderEngineCreationArgs& args) {
Alec Mourie2b61c62023-08-15 19:04:54 +0000481 if (!GLExtensions::getInstance().hasContextPriority()) {
Alec Mourid6f09462020-12-07 11:18:17 -0800482 return std::nullopt;
483 }
484
485 switch (args.contextPriority) {
486 case RenderEngine::ContextPriority::REALTIME:
Alec Mourie2b61c62023-08-15 19:04:54 +0000487 if (GLExtensions::getInstance().hasRealtimePriority()) {
Alec Mourid6f09462020-12-07 11:18:17 -0800488 return RenderEngine::ContextPriority::REALTIME;
489 } else {
490 ALOGI("Realtime priority unsupported, degrading gracefully to high priority");
491 return RenderEngine::ContextPriority::HIGH;
492 }
493 case RenderEngine::ContextPriority::HIGH:
494 case RenderEngine::ContextPriority::MEDIUM:
495 case RenderEngine::ContextPriority::LOW:
496 return args.contextPriority;
497 default:
498 return std::nullopt;
499 }
500}
501
John Reck67b1e2b2020-08-26 13:17:24 -0700502EGLSurface SkiaGLRenderEngine::createPlaceholderEglPbufferSurface(EGLDisplay display,
503 EGLConfig config, int hwcFormat,
504 Protection protection) {
505 EGLConfig placeholderConfig = config;
506 if (placeholderConfig == EGL_NO_CONFIG_KHR) {
507 placeholderConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
508 }
509 std::vector<EGLint> attributes;
510 attributes.reserve(7);
511 attributes.push_back(EGL_WIDTH);
512 attributes.push_back(1);
513 attributes.push_back(EGL_HEIGHT);
514 attributes.push_back(1);
515 if (protection == Protection::PROTECTED) {
516 attributes.push_back(EGL_PROTECTED_CONTENT_EXT);
517 attributes.push_back(EGL_TRUE);
518 }
519 attributes.push_back(EGL_NONE);
520
521 return eglCreatePbufferSurface(display, placeholderConfig, attributes.data());
522}
523
Alec Mourid6f09462020-12-07 11:18:17 -0800524int SkiaGLRenderEngine::getContextPriority() {
525 int value;
526 eglQueryContext(mEGLDisplay, mEGLContext, EGL_CONTEXT_PRIORITY_LEVEL_IMG, &value);
527 return value;
528}
529
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700530void SkiaGLRenderEngine::appendBackendSpecificInfoToDump(std::string& result) {
Alec Mourie2b61c62023-08-15 19:04:54 +0000531 const GLExtensions& extensions = GLExtensions::getInstance();
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700532 StringAppendF(&result, "\n ------------RE GLES------------\n");
Ana Krulec1d12b3b2021-01-27 16:49:51 -0800533 StringAppendF(&result, "EGL implementation : %s\n", extensions.getEGLVersion());
534 StringAppendF(&result, "%s\n", extensions.getEGLExtensions());
535 StringAppendF(&result, "GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
536 extensions.getVersion());
537 StringAppendF(&result, "%s\n", extensions.getExtensions());
Ana Krulec1d12b3b2021-01-27 16:49:51 -0800538}
539
John Reck67b1e2b2020-08-26 13:17:24 -0700540} // namespace skia
541} // namespace renderengine
Galia Peycheva6c460652020-11-03 19:42:42 +0100542} // namespace android