blob: 92181d87c807013e18abf6478e3bfadb0d169e87 [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
39#include "../gl/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 -070042bool checkGlError(const char* op, int lineNumber);
43
44namespace android {
45namespace renderengine {
46namespace skia {
47
Ana Krulec1d12b3b2021-01-27 16:49:51 -080048using base::StringAppendF;
49
John Reck67b1e2b2020-08-26 13:17:24 -070050static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute,
51 EGLint wanted, EGLConfig* outConfig) {
52 EGLint numConfigs = -1, n = 0;
53 eglGetConfigs(dpy, nullptr, 0, &numConfigs);
54 std::vector<EGLConfig> configs(numConfigs, EGL_NO_CONFIG_KHR);
55 eglChooseConfig(dpy, attrs, configs.data(), configs.size(), &n);
56 configs.resize(n);
57
58 if (!configs.empty()) {
59 if (attribute != EGL_NONE) {
60 for (EGLConfig config : configs) {
61 EGLint value = 0;
62 eglGetConfigAttrib(dpy, config, attribute, &value);
63 if (wanted == value) {
64 *outConfig = config;
65 return NO_ERROR;
66 }
67 }
68 } else {
69 // just pick the first one
70 *outConfig = configs[0];
71 return NO_ERROR;
72 }
73 }
74
75 return NAME_NOT_FOUND;
76}
77
78static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType,
79 EGLConfig* config) {
80 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
81 // it is to be used with WIFI displays
82 status_t err;
83 EGLint wantedAttribute;
84 EGLint wantedAttributeValue;
85
86 std::vector<EGLint> attribs;
87 if (renderableType) {
88 const ui::PixelFormat pixelFormat = static_cast<ui::PixelFormat>(format);
89 const bool is1010102 = pixelFormat == ui::PixelFormat::RGBA_1010102;
90
91 // Default to 8 bits per channel.
92 const EGLint tmpAttribs[] = {
93 EGL_RENDERABLE_TYPE,
94 renderableType,
95 EGL_RECORDABLE_ANDROID,
96 EGL_TRUE,
97 EGL_SURFACE_TYPE,
98 EGL_WINDOW_BIT | EGL_PBUFFER_BIT,
99 EGL_FRAMEBUFFER_TARGET_ANDROID,
100 EGL_TRUE,
101 EGL_RED_SIZE,
102 is1010102 ? 10 : 8,
103 EGL_GREEN_SIZE,
104 is1010102 ? 10 : 8,
105 EGL_BLUE_SIZE,
106 is1010102 ? 10 : 8,
107 EGL_ALPHA_SIZE,
108 is1010102 ? 2 : 8,
109 EGL_NONE,
110 };
111 std::copy(tmpAttribs, tmpAttribs + (sizeof(tmpAttribs) / sizeof(EGLint)),
112 std::back_inserter(attribs));
113 wantedAttribute = EGL_NONE;
114 wantedAttributeValue = EGL_NONE;
115 } else {
116 // if no renderable type specified, fallback to a simplified query
117 wantedAttribute = EGL_NATIVE_VISUAL_ID;
118 wantedAttributeValue = format;
119 }
120
121 err = selectConfigForAttribute(display, attribs.data(), wantedAttribute, wantedAttributeValue,
122 config);
123 if (err == NO_ERROR) {
124 EGLint caveat;
125 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
126 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
127 }
128
129 return err;
130}
131
132std::unique_ptr<SkiaGLRenderEngine> SkiaGLRenderEngine::create(
133 const RenderEngineCreationArgs& args) {
134 // initialize EGL for the default display
135 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
136 if (!eglInitialize(display, nullptr, nullptr)) {
137 LOG_ALWAYS_FATAL("failed to initialize EGL");
138 }
139
Yiwei Zhange2650962020-12-01 23:27:58 +0000140 const auto eglVersion = eglQueryString(display, EGL_VERSION);
John Reck67b1e2b2020-08-26 13:17:24 -0700141 if (!eglVersion) {
142 checkGlError(__FUNCTION__, __LINE__);
Yiwei Zhange2650962020-12-01 23:27:58 +0000143 LOG_ALWAYS_FATAL("eglQueryString(EGL_VERSION) failed");
John Reck67b1e2b2020-08-26 13:17:24 -0700144 }
145
Yiwei Zhange2650962020-12-01 23:27:58 +0000146 const auto eglExtensions = eglQueryString(display, EGL_EXTENSIONS);
John Reck67b1e2b2020-08-26 13:17:24 -0700147 if (!eglExtensions) {
148 checkGlError(__FUNCTION__, __LINE__);
Yiwei Zhange2650962020-12-01 23:27:58 +0000149 LOG_ALWAYS_FATAL("eglQueryString(EGL_EXTENSIONS) failed");
John Reck67b1e2b2020-08-26 13:17:24 -0700150 }
151
152 auto& extensions = gl::GLExtensions::getInstance();
153 extensions.initWithEGLStrings(eglVersion, eglExtensions);
154
155 // The code assumes that ES2 or later is available if this extension is
156 // supported.
157 EGLConfig config = EGL_NO_CONFIG_KHR;
158 if (!extensions.hasNoConfigContext()) {
159 config = chooseEglConfig(display, args.pixelFormat, /*logConfig*/ true);
160 }
161
John Reck67b1e2b2020-08-26 13:17:24 -0700162 EGLContext protectedContext = EGL_NO_CONTEXT;
Alec Mourid6f09462020-12-07 11:18:17 -0800163 const std::optional<RenderEngine::ContextPriority> priority = createContextPriority(args);
John Reck67b1e2b2020-08-26 13:17:24 -0700164 if (args.enableProtectedContext && extensions.hasProtectedContent()) {
Alec Mourid6f09462020-12-07 11:18:17 -0800165 protectedContext =
166 createEglContext(display, config, nullptr, priority, Protection::PROTECTED);
John Reck67b1e2b2020-08-26 13:17:24 -0700167 ALOGE_IF(protectedContext == EGL_NO_CONTEXT, "Can't create protected context");
168 }
169
Alec Mourid6f09462020-12-07 11:18:17 -0800170 EGLContext ctxt =
171 createEglContext(display, config, protectedContext, priority, Protection::UNPROTECTED);
John Reck67b1e2b2020-08-26 13:17:24 -0700172
173 // if can't create a GL context, we can only abort.
174 LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
175
176 EGLSurface placeholder = EGL_NO_SURFACE;
177 if (!extensions.hasSurfacelessContext()) {
178 placeholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat,
179 Protection::UNPROTECTED);
180 LOG_ALWAYS_FATAL_IF(placeholder == EGL_NO_SURFACE, "can't create placeholder pbuffer");
181 }
182 EGLBoolean success = eglMakeCurrent(display, placeholder, placeholder, ctxt);
183 LOG_ALWAYS_FATAL_IF(!success, "can't make placeholder pbuffer current");
184 extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER),
185 glGetString(GL_VERSION), glGetString(GL_EXTENSIONS));
186
187 EGLSurface protectedPlaceholder = EGL_NO_SURFACE;
188 if (protectedContext != EGL_NO_CONTEXT && !extensions.hasSurfacelessContext()) {
189 protectedPlaceholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat,
190 Protection::PROTECTED);
191 ALOGE_IF(protectedPlaceholder == EGL_NO_SURFACE,
192 "can't create protected placeholder pbuffer");
193 }
194
195 // initialize the renderer while GL is current
Ian Elliott1f0911e2022-09-09 16:31:47 -0600196 std::unique_ptr<SkiaGLRenderEngine> engine(new SkiaGLRenderEngine(args, display, ctxt,
197 placeholder, protectedContext,
198 protectedPlaceholder));
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700199 engine->ensureGrContextsCreated();
John Reck67b1e2b2020-08-26 13:17:24 -0700200
201 ALOGI("OpenGL ES informations:");
202 ALOGI("vendor : %s", extensions.getVendor());
203 ALOGI("renderer : %s", extensions.getRenderer());
204 ALOGI("version : %s", extensions.getVersion());
205 ALOGI("extensions: %s", extensions.getExtensions());
206 ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
207 ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
208
209 return engine;
210}
211
212EGLConfig SkiaGLRenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
213 status_t err;
214 EGLConfig config;
215
216 // First try to get an ES3 config
217 err = selectEGLConfig(display, format, EGL_OPENGL_ES3_BIT, &config);
218 if (err != NO_ERROR) {
219 // If ES3 fails, try to get an ES2 config
220 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
221 if (err != NO_ERROR) {
222 // If ES2 still doesn't work, probably because we're on the emulator.
223 // try a simplified query
224 ALOGW("no suitable EGLConfig found, trying a simpler query");
225 err = selectEGLConfig(display, format, 0, &config);
226 if (err != NO_ERROR) {
227 // this EGL is too lame for android
228 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
229 }
230 }
231 }
232
233 if (logConfig) {
234 // print some debugging info
235 EGLint r, g, b, a;
236 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
237 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
238 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
239 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
240 ALOGI("EGL information:");
241 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
242 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
243 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
244 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
245 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
246 }
247
248 return config;
249}
250
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700251SkiaGLRenderEngine::SkiaGLRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display,
Lucas Dupind508e472020-11-04 04:32:06 +0000252 EGLContext ctxt, EGLSurface placeholder,
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700253 EGLContext protectedContext, EGLSurface protectedPlaceholder)
Alec Mouri47bcb072023-08-15 02:02:49 +0000254 : SkiaRenderEngine(args.renderEngineType, static_cast<PixelFormat>(args.pixelFormat),
255 args.supportsBackgroundBlur),
Alec Mouri0d995102021-02-24 16:53:38 -0800256 mEGLDisplay(display),
John Reck67b1e2b2020-08-26 13:17:24 -0700257 mEGLContext(ctxt),
258 mPlaceholderSurface(placeholder),
259 mProtectedEGLContext(protectedContext),
Alec Mouri47bcb072023-08-15 02:02:49 +0000260 mProtectedPlaceholderSurface(protectedPlaceholder) {}
Alec Mouric0aae732021-01-12 13:32:18 -0800261
262SkiaGLRenderEngine::~SkiaGLRenderEngine() {
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700263 finishRenderingAndAbandonContext();
Alec Mouric0aae732021-01-12 13:32:18 -0800264 if (mPlaceholderSurface != EGL_NO_SURFACE) {
265 eglDestroySurface(mEGLDisplay, mPlaceholderSurface);
266 }
267 if (mProtectedPlaceholderSurface != EGL_NO_SURFACE) {
268 eglDestroySurface(mEGLDisplay, mProtectedPlaceholderSurface);
269 }
270 if (mEGLContext != EGL_NO_CONTEXT) {
271 eglDestroyContext(mEGLDisplay, mEGLContext);
272 }
273 if (mProtectedEGLContext != EGL_NO_CONTEXT) {
274 eglDestroyContext(mEGLDisplay, mProtectedEGLContext);
275 }
276 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
277 eglTerminate(mEGLDisplay);
278 eglReleaseThread();
John Reck67b1e2b2020-08-26 13:17:24 -0700279}
280
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700281SkiaRenderEngine::Contexts SkiaGLRenderEngine::createDirectContexts(
282 const GrContextOptions& options) {
283
284 LOG_ALWAYS_FATAL_IF(isProtected(),
285 "Cannot setup contexts while already in protected mode");
286
287 sk_sp<const GrGLInterface> glInterface = GrGLMakeNativeInterface();
288
289 LOG_ALWAYS_FATAL_IF(!glInterface.get(), "GrGLMakeNativeInterface() failed");
290
291 SkiaRenderEngine::Contexts contexts;
292 contexts.first = GrDirectContext::MakeGL(glInterface, options);
293 if (supportsProtectedContentImpl()) {
294 useProtectedContextImpl(GrProtected::kYes);
295 contexts.second = GrDirectContext::MakeGL(glInterface, options);
296 useProtectedContextImpl(GrProtected::kNo);
297 }
298
299 return contexts;
300}
301
302bool SkiaGLRenderEngine::supportsProtectedContentImpl() const {
Lucas Dupind508e472020-11-04 04:32:06 +0000303 return mProtectedEGLContext != EGL_NO_CONTEXT;
304}
305
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700306bool SkiaGLRenderEngine::useProtectedContextImpl(GrProtected isProtected) {
Lucas Dupind508e472020-11-04 04:32:06 +0000307 const EGLSurface surface =
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700308 (isProtected == GrProtected::kYes) ?
309 mProtectedPlaceholderSurface : mPlaceholderSurface;
310 const EGLContext context = (isProtected == GrProtected::kYes) ?
311 mProtectedEGLContext : mEGLContext;
Alec Mouric0aae732021-01-12 13:32:18 -0800312
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700313 return eglMakeCurrent(mEGLDisplay, surface, surface, context) == EGL_TRUE;
Lucas Dupind508e472020-11-04 04:32:06 +0000314}
315
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700316void SkiaGLRenderEngine::waitFence(GrDirectContext*, base::borrowed_fd fenceFd) {
Alec Mouri4ee7b492021-08-11 10:36:55 -0700317 if (fenceFd.get() >= 0 && !waitGpuFence(fenceFd)) {
318 ATRACE_NAME("SkiaGLRenderEngine::waitFence");
319 sync_wait(fenceFd.get(), -1);
320 }
321}
322
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700323base::unique_fd SkiaGLRenderEngine::flushAndSubmit(GrDirectContext* grContext) {
324 base::unique_fd drawFence = flush();
325
326 bool requireSync = drawFence.get() < 0;
327 if (requireSync) {
328 ATRACE_BEGIN("Submit(sync=true)");
329 } else {
330 ATRACE_BEGIN("Submit(sync=false)");
331 }
332 bool success = grContext->submit(requireSync);
333 ATRACE_END();
334 if (!success) {
335 ALOGE("Failed to flush RenderEngine commands");
336 // Chances are, something illegal happened (Skia's internal GPU object
337 // doesn't exist, or the context was abandoned).
338 return drawFence;
339 }
340
341 return drawFence;
342}
343
Alec Mouri4ee7b492021-08-11 10:36:55 -0700344bool SkiaGLRenderEngine::waitGpuFence(base::borrowed_fd fenceFd) {
John Reck67b1e2b2020-08-26 13:17:24 -0700345 if (!gl::GLExtensions::getInstance().hasNativeFenceSync() ||
346 !gl::GLExtensions::getInstance().hasWaitSync()) {
347 return false;
348 }
349
Alec Mouri4ee7b492021-08-11 10:36:55 -0700350 // Duplicate the fence for passing to eglCreateSyncKHR.
351 base::unique_fd fenceDup(dup(fenceFd.get()));
352 if (fenceDup.get() < 0) {
353 ALOGE("failed to create duplicate fence fd: %d", fenceDup.get());
354 return false;
355 }
356
John Reck67b1e2b2020-08-26 13:17:24 -0700357 // release the fd and transfer the ownership to EGLSync
Alec Mouri4ee7b492021-08-11 10:36:55 -0700358 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceDup.release(), EGL_NONE};
John Reck67b1e2b2020-08-26 13:17:24 -0700359 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
360 if (sync == EGL_NO_SYNC_KHR) {
361 ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
362 return false;
363 }
364
365 // XXX: The spec draft is inconsistent as to whether this should return an
366 // EGLint or void. Ignore the return value for now, as it's not strictly
367 // needed.
368 eglWaitSyncKHR(mEGLDisplay, sync, 0);
369 EGLint error = eglGetError();
370 eglDestroySyncKHR(mEGLDisplay, sync);
371 if (error != EGL_SUCCESS) {
372 ALOGE("failed to wait for EGL native fence sync: %#x", error);
373 return false;
374 }
375
376 return true;
377}
378
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700379base::unique_fd SkiaGLRenderEngine::flush() {
Ana Krulecdfec8f52021-01-13 12:51:47 -0800380 ATRACE_CALL();
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700381 if (!gl::GLExtensions::getInstance().hasNativeFenceSync()) {
382 return base::unique_fd();
John Reckcdb4ed72021-02-04 13:39:33 -0500383 }
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700384
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700385 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
386 if (sync == EGL_NO_SYNC_KHR) {
387 ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
388 return base::unique_fd();
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500389 }
390
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700391 // native fence fd will not be populated until flush() is done.
392 glFlush();
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500393
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700394 // get the fence fd
395 base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
396 eglDestroySyncKHR(mEGLDisplay, sync);
397 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
398 ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
Derek Sollenberger7c42bef2021-02-23 13:01:39 -0500399 }
400
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700401 return fenceFd;
Lucas Dupin3f11e922020-09-22 17:31:04 -0700402}
403
John Reck67b1e2b2020-08-26 13:17:24 -0700404EGLContext SkiaGLRenderEngine::createEglContext(EGLDisplay display, EGLConfig config,
Alec Mourid6f09462020-12-07 11:18:17 -0800405 EGLContext shareContext,
406 std::optional<ContextPriority> contextPriority,
John Reck67b1e2b2020-08-26 13:17:24 -0700407 Protection protection) {
408 EGLint renderableType = 0;
409 if (config == EGL_NO_CONFIG_KHR) {
410 renderableType = EGL_OPENGL_ES3_BIT;
411 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
412 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
413 }
414 EGLint contextClientVersion = 0;
415 if (renderableType & EGL_OPENGL_ES3_BIT) {
416 contextClientVersion = 3;
417 } else if (renderableType & EGL_OPENGL_ES2_BIT) {
418 contextClientVersion = 2;
419 } else if (renderableType & EGL_OPENGL_ES_BIT) {
420 contextClientVersion = 1;
421 } else {
422 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
423 }
424
425 std::vector<EGLint> contextAttributes;
426 contextAttributes.reserve(7);
427 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
428 contextAttributes.push_back(contextClientVersion);
Alec Mourid6f09462020-12-07 11:18:17 -0800429 if (contextPriority) {
John Reck67b1e2b2020-08-26 13:17:24 -0700430 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
Alec Mourid6f09462020-12-07 11:18:17 -0800431 switch (*contextPriority) {
432 case ContextPriority::REALTIME:
433 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_REALTIME_NV);
434 break;
435 case ContextPriority::MEDIUM:
436 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_MEDIUM_IMG);
437 break;
438 case ContextPriority::LOW:
439 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LOW_IMG);
440 break;
441 case ContextPriority::HIGH:
442 default:
443 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
444 break;
445 }
John Reck67b1e2b2020-08-26 13:17:24 -0700446 }
447 if (protection == Protection::PROTECTED) {
448 contextAttributes.push_back(EGL_PROTECTED_CONTENT_EXT);
449 contextAttributes.push_back(EGL_TRUE);
450 }
451 contextAttributes.push_back(EGL_NONE);
452
453 EGLContext context = eglCreateContext(display, config, shareContext, contextAttributes.data());
454
455 if (contextClientVersion == 3 && context == EGL_NO_CONTEXT) {
456 // eglGetConfigAttrib indicated we can create GLES 3 context, but we failed, thus
457 // EGL_NO_CONTEXT so that we can abort.
458 if (config != EGL_NO_CONFIG_KHR) {
459 return context;
460 }
461 // If |config| is EGL_NO_CONFIG_KHR, we speculatively try to create GLES 3 context, so we
462 // should try to fall back to GLES 2.
463 contextAttributes[1] = 2;
464 context = eglCreateContext(display, config, shareContext, contextAttributes.data());
465 }
466
467 return context;
468}
469
Alec Mourid6f09462020-12-07 11:18:17 -0800470std::optional<RenderEngine::ContextPriority> SkiaGLRenderEngine::createContextPriority(
471 const RenderEngineCreationArgs& args) {
472 if (!gl::GLExtensions::getInstance().hasContextPriority()) {
473 return std::nullopt;
474 }
475
476 switch (args.contextPriority) {
477 case RenderEngine::ContextPriority::REALTIME:
478 if (gl::GLExtensions::getInstance().hasRealtimePriority()) {
479 return RenderEngine::ContextPriority::REALTIME;
480 } else {
481 ALOGI("Realtime priority unsupported, degrading gracefully to high priority");
482 return RenderEngine::ContextPriority::HIGH;
483 }
484 case RenderEngine::ContextPriority::HIGH:
485 case RenderEngine::ContextPriority::MEDIUM:
486 case RenderEngine::ContextPriority::LOW:
487 return args.contextPriority;
488 default:
489 return std::nullopt;
490 }
491}
492
John Reck67b1e2b2020-08-26 13:17:24 -0700493EGLSurface SkiaGLRenderEngine::createPlaceholderEglPbufferSurface(EGLDisplay display,
494 EGLConfig config, int hwcFormat,
495 Protection protection) {
496 EGLConfig placeholderConfig = config;
497 if (placeholderConfig == EGL_NO_CONFIG_KHR) {
498 placeholderConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
499 }
500 std::vector<EGLint> attributes;
501 attributes.reserve(7);
502 attributes.push_back(EGL_WIDTH);
503 attributes.push_back(1);
504 attributes.push_back(EGL_HEIGHT);
505 attributes.push_back(1);
506 if (protection == Protection::PROTECTED) {
507 attributes.push_back(EGL_PROTECTED_CONTENT_EXT);
508 attributes.push_back(EGL_TRUE);
509 }
510 attributes.push_back(EGL_NONE);
511
512 return eglCreatePbufferSurface(display, placeholderConfig, attributes.data());
513}
514
Alec Mourid6f09462020-12-07 11:18:17 -0800515int SkiaGLRenderEngine::getContextPriority() {
516 int value;
517 eglQueryContext(mEGLDisplay, mEGLContext, EGL_CONTEXT_PRIORITY_LEVEL_IMG, &value);
518 return value;
519}
520
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700521void SkiaGLRenderEngine::appendBackendSpecificInfoToDump(std::string& result) {
Ana Krulec1d12b3b2021-01-27 16:49:51 -0800522 const gl::GLExtensions& extensions = gl::GLExtensions::getInstance();
Lingfeng Yang00c1ff62022-06-02 09:19:28 -0700523 StringAppendF(&result, "\n ------------RE GLES------------\n");
Ana Krulec1d12b3b2021-01-27 16:49:51 -0800524 StringAppendF(&result, "EGL implementation : %s\n", extensions.getEGLVersion());
525 StringAppendF(&result, "%s\n", extensions.getEGLExtensions());
526 StringAppendF(&result, "GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
527 extensions.getVersion());
528 StringAppendF(&result, "%s\n", extensions.getExtensions());
Ana Krulec1d12b3b2021-01-27 16:49:51 -0800529}
530
John Reck67b1e2b2020-08-26 13:17:24 -0700531} // namespace skia
532} // namespace renderengine
Galia Peycheva6c460652020-11-03 19:42:42 +0100533} // namespace android