xref: /aosp_15_r20/frameworks/native/libs/renderengine/skia/SkiaGLRenderEngine.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright 2020 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker //#define LOG_NDEBUG 0
18*38e8c45fSAndroid Build Coastguard Worker #undef LOG_TAG
19*38e8c45fSAndroid Build Coastguard Worker #define LOG_TAG "RenderEngine"
20*38e8c45fSAndroid Build Coastguard Worker #define ATRACE_TAG ATRACE_TAG_GRAPHICS
21*38e8c45fSAndroid Build Coastguard Worker 
22*38e8c45fSAndroid Build Coastguard Worker #include "SkiaGLRenderEngine.h"
23*38e8c45fSAndroid Build Coastguard Worker 
24*38e8c45fSAndroid Build Coastguard Worker #include <EGL/egl.h>
25*38e8c45fSAndroid Build Coastguard Worker #include <EGL/eglext.h>
26*38e8c45fSAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
27*38e8c45fSAndroid Build Coastguard Worker #include <common/trace.h>
28*38e8c45fSAndroid Build Coastguard Worker #include <include/gpu/ganesh/GrContextOptions.h>
29*38e8c45fSAndroid Build Coastguard Worker #include <include/gpu/ganesh/GrTypes.h>
30*38e8c45fSAndroid Build Coastguard Worker #include <include/gpu/ganesh/gl/GrGLDirectContext.h>
31*38e8c45fSAndroid Build Coastguard Worker #include <include/gpu/ganesh/gl/GrGLInterface.h>
32*38e8c45fSAndroid Build Coastguard Worker #include <log/log_main.h>
33*38e8c45fSAndroid Build Coastguard Worker #include <sync/sync.h>
34*38e8c45fSAndroid Build Coastguard Worker #include <ui/DebugUtils.h>
35*38e8c45fSAndroid Build Coastguard Worker 
36*38e8c45fSAndroid Build Coastguard Worker #include <cmath>
37*38e8c45fSAndroid Build Coastguard Worker #include <cstdint>
38*38e8c45fSAndroid Build Coastguard Worker #include <memory>
39*38e8c45fSAndroid Build Coastguard Worker #include <numeric>
40*38e8c45fSAndroid Build Coastguard Worker 
41*38e8c45fSAndroid Build Coastguard Worker #include "GLExtensions.h"
42*38e8c45fSAndroid Build Coastguard Worker #include "compat/SkiaGpuContext.h"
43*38e8c45fSAndroid Build Coastguard Worker 
44*38e8c45fSAndroid Build Coastguard Worker namespace android {
45*38e8c45fSAndroid Build Coastguard Worker namespace renderengine {
46*38e8c45fSAndroid Build Coastguard Worker namespace skia {
47*38e8c45fSAndroid Build Coastguard Worker 
48*38e8c45fSAndroid Build Coastguard Worker using base::StringAppendF;
49*38e8c45fSAndroid Build Coastguard Worker 
checkGlError(const char * op,int lineNumber)50*38e8c45fSAndroid Build Coastguard Worker static bool checkGlError(const char* op, int lineNumber) {
51*38e8c45fSAndroid Build Coastguard Worker     bool errorFound = false;
52*38e8c45fSAndroid Build Coastguard Worker     GLint error = glGetError();
53*38e8c45fSAndroid Build Coastguard Worker     while (error != GL_NO_ERROR) {
54*38e8c45fSAndroid Build Coastguard Worker         errorFound = true;
55*38e8c45fSAndroid Build Coastguard Worker         error = glGetError();
56*38e8c45fSAndroid Build Coastguard Worker         ALOGV("after %s() (line # %d) glError (0x%x)\n", op, lineNumber, error);
57*38e8c45fSAndroid Build Coastguard Worker     }
58*38e8c45fSAndroid Build Coastguard Worker     return errorFound;
59*38e8c45fSAndroid Build Coastguard Worker }
60*38e8c45fSAndroid Build Coastguard Worker 
selectConfigForAttribute(EGLDisplay dpy,EGLint const * attrs,EGLint attribute,EGLint wanted,EGLConfig * outConfig)61*38e8c45fSAndroid Build Coastguard Worker static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute,
62*38e8c45fSAndroid Build Coastguard Worker                                          EGLint wanted, EGLConfig* outConfig) {
63*38e8c45fSAndroid Build Coastguard Worker     EGLint numConfigs = -1, n = 0;
64*38e8c45fSAndroid Build Coastguard Worker     eglGetConfigs(dpy, nullptr, 0, &numConfigs);
65*38e8c45fSAndroid Build Coastguard Worker     std::vector<EGLConfig> configs(numConfigs, EGL_NO_CONFIG_KHR);
66*38e8c45fSAndroid Build Coastguard Worker     eglChooseConfig(dpy, attrs, configs.data(), configs.size(), &n);
67*38e8c45fSAndroid Build Coastguard Worker     configs.resize(n);
68*38e8c45fSAndroid Build Coastguard Worker 
69*38e8c45fSAndroid Build Coastguard Worker     if (!configs.empty()) {
70*38e8c45fSAndroid Build Coastguard Worker         if (attribute != EGL_NONE) {
71*38e8c45fSAndroid Build Coastguard Worker             for (EGLConfig config : configs) {
72*38e8c45fSAndroid Build Coastguard Worker                 EGLint value = 0;
73*38e8c45fSAndroid Build Coastguard Worker                 eglGetConfigAttrib(dpy, config, attribute, &value);
74*38e8c45fSAndroid Build Coastguard Worker                 if (wanted == value) {
75*38e8c45fSAndroid Build Coastguard Worker                     *outConfig = config;
76*38e8c45fSAndroid Build Coastguard Worker                     return NO_ERROR;
77*38e8c45fSAndroid Build Coastguard Worker                 }
78*38e8c45fSAndroid Build Coastguard Worker             }
79*38e8c45fSAndroid Build Coastguard Worker         } else {
80*38e8c45fSAndroid Build Coastguard Worker             // just pick the first one
81*38e8c45fSAndroid Build Coastguard Worker             *outConfig = configs[0];
82*38e8c45fSAndroid Build Coastguard Worker             return NO_ERROR;
83*38e8c45fSAndroid Build Coastguard Worker         }
84*38e8c45fSAndroid Build Coastguard Worker     }
85*38e8c45fSAndroid Build Coastguard Worker 
86*38e8c45fSAndroid Build Coastguard Worker     return NAME_NOT_FOUND;
87*38e8c45fSAndroid Build Coastguard Worker }
88*38e8c45fSAndroid Build Coastguard Worker 
selectEGLConfig(EGLDisplay display,EGLint format,EGLint renderableType,EGLConfig * config)89*38e8c45fSAndroid Build Coastguard Worker static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType,
90*38e8c45fSAndroid Build Coastguard Worker                                 EGLConfig* config) {
91*38e8c45fSAndroid Build Coastguard Worker     // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
92*38e8c45fSAndroid Build Coastguard Worker     // it is to be used with WIFI displays
93*38e8c45fSAndroid Build Coastguard Worker     status_t err;
94*38e8c45fSAndroid Build Coastguard Worker     EGLint wantedAttribute;
95*38e8c45fSAndroid Build Coastguard Worker     EGLint wantedAttributeValue;
96*38e8c45fSAndroid Build Coastguard Worker 
97*38e8c45fSAndroid Build Coastguard Worker     std::vector<EGLint> attribs;
98*38e8c45fSAndroid Build Coastguard Worker     if (renderableType) {
99*38e8c45fSAndroid Build Coastguard Worker         const ui::PixelFormat pixelFormat = static_cast<ui::PixelFormat>(format);
100*38e8c45fSAndroid Build Coastguard Worker         const bool is1010102 = pixelFormat == ui::PixelFormat::RGBA_1010102;
101*38e8c45fSAndroid Build Coastguard Worker 
102*38e8c45fSAndroid Build Coastguard Worker         // Default to 8 bits per channel.
103*38e8c45fSAndroid Build Coastguard Worker         const EGLint tmpAttribs[] = {
104*38e8c45fSAndroid Build Coastguard Worker                 EGL_RENDERABLE_TYPE,
105*38e8c45fSAndroid Build Coastguard Worker                 renderableType,
106*38e8c45fSAndroid Build Coastguard Worker                 EGL_RECORDABLE_ANDROID,
107*38e8c45fSAndroid Build Coastguard Worker                 EGL_TRUE,
108*38e8c45fSAndroid Build Coastguard Worker                 EGL_SURFACE_TYPE,
109*38e8c45fSAndroid Build Coastguard Worker                 EGL_WINDOW_BIT | EGL_PBUFFER_BIT,
110*38e8c45fSAndroid Build Coastguard Worker                 EGL_FRAMEBUFFER_TARGET_ANDROID,
111*38e8c45fSAndroid Build Coastguard Worker                 EGL_TRUE,
112*38e8c45fSAndroid Build Coastguard Worker                 EGL_RED_SIZE,
113*38e8c45fSAndroid Build Coastguard Worker                 is1010102 ? 10 : 8,
114*38e8c45fSAndroid Build Coastguard Worker                 EGL_GREEN_SIZE,
115*38e8c45fSAndroid Build Coastguard Worker                 is1010102 ? 10 : 8,
116*38e8c45fSAndroid Build Coastguard Worker                 EGL_BLUE_SIZE,
117*38e8c45fSAndroid Build Coastguard Worker                 is1010102 ? 10 : 8,
118*38e8c45fSAndroid Build Coastguard Worker                 EGL_ALPHA_SIZE,
119*38e8c45fSAndroid Build Coastguard Worker                 is1010102 ? 2 : 8,
120*38e8c45fSAndroid Build Coastguard Worker                 EGL_NONE,
121*38e8c45fSAndroid Build Coastguard Worker         };
122*38e8c45fSAndroid Build Coastguard Worker         std::copy(tmpAttribs, tmpAttribs + (sizeof(tmpAttribs) / sizeof(EGLint)),
123*38e8c45fSAndroid Build Coastguard Worker                   std::back_inserter(attribs));
124*38e8c45fSAndroid Build Coastguard Worker         wantedAttribute = EGL_NONE;
125*38e8c45fSAndroid Build Coastguard Worker         wantedAttributeValue = EGL_NONE;
126*38e8c45fSAndroid Build Coastguard Worker     } else {
127*38e8c45fSAndroid Build Coastguard Worker         // if no renderable type specified, fallback to a simplified query
128*38e8c45fSAndroid Build Coastguard Worker         wantedAttribute = EGL_NATIVE_VISUAL_ID;
129*38e8c45fSAndroid Build Coastguard Worker         wantedAttributeValue = format;
130*38e8c45fSAndroid Build Coastguard Worker     }
131*38e8c45fSAndroid Build Coastguard Worker 
132*38e8c45fSAndroid Build Coastguard Worker     err = selectConfigForAttribute(display, attribs.data(), wantedAttribute, wantedAttributeValue,
133*38e8c45fSAndroid Build Coastguard Worker                                    config);
134*38e8c45fSAndroid Build Coastguard Worker     if (err == NO_ERROR) {
135*38e8c45fSAndroid Build Coastguard Worker         EGLint caveat;
136*38e8c45fSAndroid Build Coastguard Worker         if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
137*38e8c45fSAndroid Build Coastguard Worker             ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
138*38e8c45fSAndroid Build Coastguard Worker     }
139*38e8c45fSAndroid Build Coastguard Worker 
140*38e8c45fSAndroid Build Coastguard Worker     return err;
141*38e8c45fSAndroid Build Coastguard Worker }
142*38e8c45fSAndroid Build Coastguard Worker 
create(const RenderEngineCreationArgs & args)143*38e8c45fSAndroid Build Coastguard Worker std::unique_ptr<SkiaGLRenderEngine> SkiaGLRenderEngine::create(
144*38e8c45fSAndroid Build Coastguard Worker         const RenderEngineCreationArgs& args) {
145*38e8c45fSAndroid Build Coastguard Worker     // initialize EGL for the default display
146*38e8c45fSAndroid Build Coastguard Worker     EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
147*38e8c45fSAndroid Build Coastguard Worker     if (!eglInitialize(display, nullptr, nullptr)) {
148*38e8c45fSAndroid Build Coastguard Worker         LOG_ALWAYS_FATAL("failed to initialize EGL");
149*38e8c45fSAndroid Build Coastguard Worker     }
150*38e8c45fSAndroid Build Coastguard Worker 
151*38e8c45fSAndroid Build Coastguard Worker     const auto eglVersion = eglQueryString(display, EGL_VERSION);
152*38e8c45fSAndroid Build Coastguard Worker     if (!eglVersion) {
153*38e8c45fSAndroid Build Coastguard Worker         checkGlError(__FUNCTION__, __LINE__);
154*38e8c45fSAndroid Build Coastguard Worker         LOG_ALWAYS_FATAL("eglQueryString(EGL_VERSION) failed");
155*38e8c45fSAndroid Build Coastguard Worker     }
156*38e8c45fSAndroid Build Coastguard Worker 
157*38e8c45fSAndroid Build Coastguard Worker     const auto eglExtensions = eglQueryString(display, EGL_EXTENSIONS);
158*38e8c45fSAndroid Build Coastguard Worker     if (!eglExtensions) {
159*38e8c45fSAndroid Build Coastguard Worker         checkGlError(__FUNCTION__, __LINE__);
160*38e8c45fSAndroid Build Coastguard Worker         LOG_ALWAYS_FATAL("eglQueryString(EGL_EXTENSIONS) failed");
161*38e8c45fSAndroid Build Coastguard Worker     }
162*38e8c45fSAndroid Build Coastguard Worker 
163*38e8c45fSAndroid Build Coastguard Worker     auto& extensions = GLExtensions::getInstance();
164*38e8c45fSAndroid Build Coastguard Worker     extensions.initWithEGLStrings(eglVersion, eglExtensions);
165*38e8c45fSAndroid Build Coastguard Worker 
166*38e8c45fSAndroid Build Coastguard Worker     // The code assumes that ES2 or later is available if this extension is
167*38e8c45fSAndroid Build Coastguard Worker     // supported.
168*38e8c45fSAndroid Build Coastguard Worker     EGLConfig config = EGL_NO_CONFIG_KHR;
169*38e8c45fSAndroid Build Coastguard Worker     if (!extensions.hasNoConfigContext()) {
170*38e8c45fSAndroid Build Coastguard Worker         config = chooseEglConfig(display, args.pixelFormat, /*logConfig*/ true);
171*38e8c45fSAndroid Build Coastguard Worker     }
172*38e8c45fSAndroid Build Coastguard Worker 
173*38e8c45fSAndroid Build Coastguard Worker     EGLContext protectedContext = EGL_NO_CONTEXT;
174*38e8c45fSAndroid Build Coastguard Worker     const std::optional<RenderEngine::ContextPriority> priority = createContextPriority(args);
175*38e8c45fSAndroid Build Coastguard Worker     if (args.enableProtectedContext && extensions.hasProtectedContent()) {
176*38e8c45fSAndroid Build Coastguard Worker         protectedContext =
177*38e8c45fSAndroid Build Coastguard Worker                 createEglContext(display, config, nullptr, priority, Protection::PROTECTED);
178*38e8c45fSAndroid Build Coastguard Worker         ALOGE_IF(protectedContext == EGL_NO_CONTEXT, "Can't create protected context");
179*38e8c45fSAndroid Build Coastguard Worker     }
180*38e8c45fSAndroid Build Coastguard Worker 
181*38e8c45fSAndroid Build Coastguard Worker     EGLContext ctxt =
182*38e8c45fSAndroid Build Coastguard Worker             createEglContext(display, config, protectedContext, priority, Protection::UNPROTECTED);
183*38e8c45fSAndroid Build Coastguard Worker 
184*38e8c45fSAndroid Build Coastguard Worker     // if can't create a GL context, we can only abort.
185*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
186*38e8c45fSAndroid Build Coastguard Worker 
187*38e8c45fSAndroid Build Coastguard Worker     EGLSurface placeholder = EGL_NO_SURFACE;
188*38e8c45fSAndroid Build Coastguard Worker     if (!extensions.hasSurfacelessContext()) {
189*38e8c45fSAndroid Build Coastguard Worker         placeholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat,
190*38e8c45fSAndroid Build Coastguard Worker                                                          Protection::UNPROTECTED);
191*38e8c45fSAndroid Build Coastguard Worker         LOG_ALWAYS_FATAL_IF(placeholder == EGL_NO_SURFACE, "can't create placeholder pbuffer");
192*38e8c45fSAndroid Build Coastguard Worker     }
193*38e8c45fSAndroid Build Coastguard Worker     EGLBoolean success = eglMakeCurrent(display, placeholder, placeholder, ctxt);
194*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(!success, "can't make placeholder pbuffer current");
195*38e8c45fSAndroid Build Coastguard Worker     extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER),
196*38e8c45fSAndroid Build Coastguard Worker                                  glGetString(GL_VERSION), glGetString(GL_EXTENSIONS));
197*38e8c45fSAndroid Build Coastguard Worker 
198*38e8c45fSAndroid Build Coastguard Worker     EGLSurface protectedPlaceholder = EGL_NO_SURFACE;
199*38e8c45fSAndroid Build Coastguard Worker     if (protectedContext != EGL_NO_CONTEXT && !extensions.hasSurfacelessContext()) {
200*38e8c45fSAndroid Build Coastguard Worker         protectedPlaceholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat,
201*38e8c45fSAndroid Build Coastguard Worker                                                                   Protection::PROTECTED);
202*38e8c45fSAndroid Build Coastguard Worker         ALOGE_IF(protectedPlaceholder == EGL_NO_SURFACE,
203*38e8c45fSAndroid Build Coastguard Worker                  "can't create protected placeholder pbuffer");
204*38e8c45fSAndroid Build Coastguard Worker     }
205*38e8c45fSAndroid Build Coastguard Worker 
206*38e8c45fSAndroid Build Coastguard Worker     // initialize the renderer while GL is current
207*38e8c45fSAndroid Build Coastguard Worker     std::unique_ptr<SkiaGLRenderEngine> engine(new SkiaGLRenderEngine(args, display, ctxt,
208*38e8c45fSAndroid Build Coastguard Worker                                                                       placeholder, protectedContext,
209*38e8c45fSAndroid Build Coastguard Worker                                                                       protectedPlaceholder));
210*38e8c45fSAndroid Build Coastguard Worker     engine->ensureContextsCreated();
211*38e8c45fSAndroid Build Coastguard Worker 
212*38e8c45fSAndroid Build Coastguard Worker     ALOGI("OpenGL ES informations:");
213*38e8c45fSAndroid Build Coastguard Worker     ALOGI("vendor    : %s", extensions.getVendor());
214*38e8c45fSAndroid Build Coastguard Worker     ALOGI("renderer  : %s", extensions.getRenderer());
215*38e8c45fSAndroid Build Coastguard Worker     ALOGI("version   : %s", extensions.getVersion());
216*38e8c45fSAndroid Build Coastguard Worker     ALOGI("extensions: %s", extensions.getExtensions());
217*38e8c45fSAndroid Build Coastguard Worker     ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
218*38e8c45fSAndroid Build Coastguard Worker     ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
219*38e8c45fSAndroid Build Coastguard Worker 
220*38e8c45fSAndroid Build Coastguard Worker     return engine;
221*38e8c45fSAndroid Build Coastguard Worker }
222*38e8c45fSAndroid Build Coastguard Worker 
chooseEglConfig(EGLDisplay display,int format,bool logConfig)223*38e8c45fSAndroid Build Coastguard Worker EGLConfig SkiaGLRenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
224*38e8c45fSAndroid Build Coastguard Worker     status_t err;
225*38e8c45fSAndroid Build Coastguard Worker     EGLConfig config;
226*38e8c45fSAndroid Build Coastguard Worker 
227*38e8c45fSAndroid Build Coastguard Worker     // First try to get an ES3 config
228*38e8c45fSAndroid Build Coastguard Worker     err = selectEGLConfig(display, format, EGL_OPENGL_ES3_BIT, &config);
229*38e8c45fSAndroid Build Coastguard Worker     if (err != NO_ERROR) {
230*38e8c45fSAndroid Build Coastguard Worker         // If ES3 fails, try to get an ES2 config
231*38e8c45fSAndroid Build Coastguard Worker         err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
232*38e8c45fSAndroid Build Coastguard Worker         if (err != NO_ERROR) {
233*38e8c45fSAndroid Build Coastguard Worker             // If ES2 still doesn't work, probably because we're on the emulator.
234*38e8c45fSAndroid Build Coastguard Worker             // try a simplified query
235*38e8c45fSAndroid Build Coastguard Worker             ALOGW("no suitable EGLConfig found, trying a simpler query");
236*38e8c45fSAndroid Build Coastguard Worker             err = selectEGLConfig(display, format, 0, &config);
237*38e8c45fSAndroid Build Coastguard Worker             if (err != NO_ERROR) {
238*38e8c45fSAndroid Build Coastguard Worker                 // this EGL is too lame for android
239*38e8c45fSAndroid Build Coastguard Worker                 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up"
240*38e8c45fSAndroid Build Coastguard Worker                                  " (format: %d, vendor: %s, version: %s, extensions: %s, Client"
241*38e8c45fSAndroid Build Coastguard Worker                                  " API: %s)",
242*38e8c45fSAndroid Build Coastguard Worker                                  format, eglQueryString(display, EGL_VENDOR),
243*38e8c45fSAndroid Build Coastguard Worker                                  eglQueryString(display, EGL_VERSION),
244*38e8c45fSAndroid Build Coastguard Worker                                  eglQueryString(display, EGL_EXTENSIONS),
245*38e8c45fSAndroid Build Coastguard Worker                                  eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
246*38e8c45fSAndroid Build Coastguard Worker             }
247*38e8c45fSAndroid Build Coastguard Worker         }
248*38e8c45fSAndroid Build Coastguard Worker     }
249*38e8c45fSAndroid Build Coastguard Worker 
250*38e8c45fSAndroid Build Coastguard Worker     if (logConfig) {
251*38e8c45fSAndroid Build Coastguard Worker         // print some debugging info
252*38e8c45fSAndroid Build Coastguard Worker         EGLint r, g, b, a;
253*38e8c45fSAndroid Build Coastguard Worker         eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
254*38e8c45fSAndroid Build Coastguard Worker         eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
255*38e8c45fSAndroid Build Coastguard Worker         eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
256*38e8c45fSAndroid Build Coastguard Worker         eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
257*38e8c45fSAndroid Build Coastguard Worker         ALOGI("EGL information:");
258*38e8c45fSAndroid Build Coastguard Worker         ALOGI("vendor    : %s", eglQueryString(display, EGL_VENDOR));
259*38e8c45fSAndroid Build Coastguard Worker         ALOGI("version   : %s", eglQueryString(display, EGL_VERSION));
260*38e8c45fSAndroid Build Coastguard Worker         ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
261*38e8c45fSAndroid Build Coastguard Worker         ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
262*38e8c45fSAndroid Build Coastguard Worker         ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
263*38e8c45fSAndroid Build Coastguard Worker     }
264*38e8c45fSAndroid Build Coastguard Worker 
265*38e8c45fSAndroid Build Coastguard Worker     return config;
266*38e8c45fSAndroid Build Coastguard Worker }
267*38e8c45fSAndroid Build Coastguard Worker 
SkiaGLRenderEngine(const RenderEngineCreationArgs & args,EGLDisplay display,EGLContext ctxt,EGLSurface placeholder,EGLContext protectedContext,EGLSurface protectedPlaceholder)268*38e8c45fSAndroid Build Coastguard Worker SkiaGLRenderEngine::SkiaGLRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display,
269*38e8c45fSAndroid Build Coastguard Worker                                        EGLContext ctxt, EGLSurface placeholder,
270*38e8c45fSAndroid Build Coastguard Worker                                        EGLContext protectedContext, EGLSurface protectedPlaceholder)
271*38e8c45fSAndroid Build Coastguard Worker       : SkiaRenderEngine(args.threaded, static_cast<PixelFormat>(args.pixelFormat),
272*38e8c45fSAndroid Build Coastguard Worker                          args.blurAlgorithm),
273*38e8c45fSAndroid Build Coastguard Worker         mEGLDisplay(display),
274*38e8c45fSAndroid Build Coastguard Worker         mEGLContext(ctxt),
275*38e8c45fSAndroid Build Coastguard Worker         mPlaceholderSurface(placeholder),
276*38e8c45fSAndroid Build Coastguard Worker         mProtectedEGLContext(protectedContext),
277*38e8c45fSAndroid Build Coastguard Worker         mProtectedPlaceholderSurface(protectedPlaceholder) {}
278*38e8c45fSAndroid Build Coastguard Worker 
~SkiaGLRenderEngine()279*38e8c45fSAndroid Build Coastguard Worker SkiaGLRenderEngine::~SkiaGLRenderEngine() {
280*38e8c45fSAndroid Build Coastguard Worker     finishRenderingAndAbandonContexts();
281*38e8c45fSAndroid Build Coastguard Worker     if (mPlaceholderSurface != EGL_NO_SURFACE) {
282*38e8c45fSAndroid Build Coastguard Worker         eglDestroySurface(mEGLDisplay, mPlaceholderSurface);
283*38e8c45fSAndroid Build Coastguard Worker     }
284*38e8c45fSAndroid Build Coastguard Worker     if (mProtectedPlaceholderSurface != EGL_NO_SURFACE) {
285*38e8c45fSAndroid Build Coastguard Worker         eglDestroySurface(mEGLDisplay, mProtectedPlaceholderSurface);
286*38e8c45fSAndroid Build Coastguard Worker     }
287*38e8c45fSAndroid Build Coastguard Worker     if (mEGLContext != EGL_NO_CONTEXT) {
288*38e8c45fSAndroid Build Coastguard Worker         eglDestroyContext(mEGLDisplay, mEGLContext);
289*38e8c45fSAndroid Build Coastguard Worker     }
290*38e8c45fSAndroid Build Coastguard Worker     if (mProtectedEGLContext != EGL_NO_CONTEXT) {
291*38e8c45fSAndroid Build Coastguard Worker         eglDestroyContext(mEGLDisplay, mProtectedEGLContext);
292*38e8c45fSAndroid Build Coastguard Worker     }
293*38e8c45fSAndroid Build Coastguard Worker     eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
294*38e8c45fSAndroid Build Coastguard Worker     eglTerminate(mEGLDisplay);
295*38e8c45fSAndroid Build Coastguard Worker     eglReleaseThread();
296*38e8c45fSAndroid Build Coastguard Worker }
297*38e8c45fSAndroid Build Coastguard Worker 
createContexts()298*38e8c45fSAndroid Build Coastguard Worker SkiaRenderEngine::Contexts SkiaGLRenderEngine::createContexts() {
299*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(isProtected(),
300*38e8c45fSAndroid Build Coastguard Worker                         "Cannot setup contexts while already in protected mode");
301*38e8c45fSAndroid Build Coastguard Worker 
302*38e8c45fSAndroid Build Coastguard Worker     sk_sp<const GrGLInterface> glInterface = GrGLMakeNativeInterface();
303*38e8c45fSAndroid Build Coastguard Worker 
304*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(!glInterface.get(), "GrGLMakeNativeInterface() failed");
305*38e8c45fSAndroid Build Coastguard Worker 
306*38e8c45fSAndroid Build Coastguard Worker     SkiaRenderEngine::Contexts contexts;
307*38e8c45fSAndroid Build Coastguard Worker     contexts.first = SkiaGpuContext::MakeGL_Ganesh(glInterface, mSkSLCacheMonitor);
308*38e8c45fSAndroid Build Coastguard Worker     if (supportsProtectedContentImpl()) {
309*38e8c45fSAndroid Build Coastguard Worker         useProtectedContextImpl(GrProtected::kYes);
310*38e8c45fSAndroid Build Coastguard Worker         contexts.second = SkiaGpuContext::MakeGL_Ganesh(glInterface, mSkSLCacheMonitor);
311*38e8c45fSAndroid Build Coastguard Worker         useProtectedContextImpl(GrProtected::kNo);
312*38e8c45fSAndroid Build Coastguard Worker     }
313*38e8c45fSAndroid Build Coastguard Worker 
314*38e8c45fSAndroid Build Coastguard Worker     return contexts;
315*38e8c45fSAndroid Build Coastguard Worker }
316*38e8c45fSAndroid Build Coastguard Worker 
supportsProtectedContentImpl() const317*38e8c45fSAndroid Build Coastguard Worker bool SkiaGLRenderEngine::supportsProtectedContentImpl() const {
318*38e8c45fSAndroid Build Coastguard Worker     return mProtectedEGLContext != EGL_NO_CONTEXT;
319*38e8c45fSAndroid Build Coastguard Worker }
320*38e8c45fSAndroid Build Coastguard Worker 
useProtectedContextImpl(GrProtected isProtected)321*38e8c45fSAndroid Build Coastguard Worker bool SkiaGLRenderEngine::useProtectedContextImpl(GrProtected isProtected) {
322*38e8c45fSAndroid Build Coastguard Worker     const EGLSurface surface =
323*38e8c45fSAndroid Build Coastguard Worker         (isProtected == GrProtected::kYes) ?
324*38e8c45fSAndroid Build Coastguard Worker         mProtectedPlaceholderSurface : mPlaceholderSurface;
325*38e8c45fSAndroid Build Coastguard Worker     const EGLContext context = (isProtected == GrProtected::kYes) ?
326*38e8c45fSAndroid Build Coastguard Worker         mProtectedEGLContext : mEGLContext;
327*38e8c45fSAndroid Build Coastguard Worker 
328*38e8c45fSAndroid Build Coastguard Worker     return eglMakeCurrent(mEGLDisplay, surface, surface, context) == EGL_TRUE;
329*38e8c45fSAndroid Build Coastguard Worker }
330*38e8c45fSAndroid Build Coastguard Worker 
waitFence(SkiaGpuContext *,base::borrowed_fd fenceFd)331*38e8c45fSAndroid Build Coastguard Worker void SkiaGLRenderEngine::waitFence(SkiaGpuContext*, base::borrowed_fd fenceFd) {
332*38e8c45fSAndroid Build Coastguard Worker     if (fenceFd.get() >= 0 && !waitGpuFence(fenceFd)) {
333*38e8c45fSAndroid Build Coastguard Worker         SFTRACE_NAME("SkiaGLRenderEngine::waitFence");
334*38e8c45fSAndroid Build Coastguard Worker         sync_wait(fenceFd.get(), -1);
335*38e8c45fSAndroid Build Coastguard Worker     }
336*38e8c45fSAndroid Build Coastguard Worker }
337*38e8c45fSAndroid Build Coastguard Worker 
flushAndSubmit(SkiaGpuContext * context,sk_sp<SkSurface> dstSurface)338*38e8c45fSAndroid Build Coastguard Worker base::unique_fd SkiaGLRenderEngine::flushAndSubmit(SkiaGpuContext* context,
339*38e8c45fSAndroid Build Coastguard Worker                                                    sk_sp<SkSurface> dstSurface) {
340*38e8c45fSAndroid Build Coastguard Worker     sk_sp<GrDirectContext> grContext = context->grDirectContext();
341*38e8c45fSAndroid Build Coastguard Worker     {
342*38e8c45fSAndroid Build Coastguard Worker         SFTRACE_NAME("flush surface");
343*38e8c45fSAndroid Build Coastguard Worker         grContext->flush(dstSurface.get());
344*38e8c45fSAndroid Build Coastguard Worker     }
345*38e8c45fSAndroid Build Coastguard Worker     base::unique_fd drawFence = flushGL();
346*38e8c45fSAndroid Build Coastguard Worker 
347*38e8c45fSAndroid Build Coastguard Worker     bool requireSync = drawFence.get() < 0;
348*38e8c45fSAndroid Build Coastguard Worker     if (requireSync) {
349*38e8c45fSAndroid Build Coastguard Worker         SFTRACE_BEGIN("Submit(sync=true)");
350*38e8c45fSAndroid Build Coastguard Worker     } else {
351*38e8c45fSAndroid Build Coastguard Worker         SFTRACE_BEGIN("Submit(sync=false)");
352*38e8c45fSAndroid Build Coastguard Worker     }
353*38e8c45fSAndroid Build Coastguard Worker     bool success = grContext->submit(requireSync ? GrSyncCpu::kYes : GrSyncCpu::kNo);
354*38e8c45fSAndroid Build Coastguard Worker     SFTRACE_END();
355*38e8c45fSAndroid Build Coastguard Worker     if (!success) {
356*38e8c45fSAndroid Build Coastguard Worker         ALOGE("Failed to flush RenderEngine commands");
357*38e8c45fSAndroid Build Coastguard Worker         // Chances are, something illegal happened (Skia's internal GPU object
358*38e8c45fSAndroid Build Coastguard Worker         // doesn't exist, or the context was abandoned).
359*38e8c45fSAndroid Build Coastguard Worker         return drawFence;
360*38e8c45fSAndroid Build Coastguard Worker     }
361*38e8c45fSAndroid Build Coastguard Worker 
362*38e8c45fSAndroid Build Coastguard Worker     return drawFence;
363*38e8c45fSAndroid Build Coastguard Worker }
364*38e8c45fSAndroid Build Coastguard Worker 
waitGpuFence(base::borrowed_fd fenceFd)365*38e8c45fSAndroid Build Coastguard Worker bool SkiaGLRenderEngine::waitGpuFence(base::borrowed_fd fenceFd) {
366*38e8c45fSAndroid Build Coastguard Worker     if (!GLExtensions::getInstance().hasNativeFenceSync() ||
367*38e8c45fSAndroid Build Coastguard Worker         !GLExtensions::getInstance().hasWaitSync()) {
368*38e8c45fSAndroid Build Coastguard Worker         return false;
369*38e8c45fSAndroid Build Coastguard Worker     }
370*38e8c45fSAndroid Build Coastguard Worker 
371*38e8c45fSAndroid Build Coastguard Worker     // Duplicate the fence for passing to eglCreateSyncKHR.
372*38e8c45fSAndroid Build Coastguard Worker     base::unique_fd fenceDup(dup(fenceFd.get()));
373*38e8c45fSAndroid Build Coastguard Worker     if (fenceDup.get() < 0) {
374*38e8c45fSAndroid Build Coastguard Worker         ALOGE("failed to create duplicate fence fd: %d", fenceDup.get());
375*38e8c45fSAndroid Build Coastguard Worker         return false;
376*38e8c45fSAndroid Build Coastguard Worker     }
377*38e8c45fSAndroid Build Coastguard Worker 
378*38e8c45fSAndroid Build Coastguard Worker     // release the fd and transfer the ownership to EGLSync
379*38e8c45fSAndroid Build Coastguard Worker     EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceDup.release(), EGL_NONE};
380*38e8c45fSAndroid Build Coastguard Worker     EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
381*38e8c45fSAndroid Build Coastguard Worker     if (sync == EGL_NO_SYNC_KHR) {
382*38e8c45fSAndroid Build Coastguard Worker         ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
383*38e8c45fSAndroid Build Coastguard Worker         return false;
384*38e8c45fSAndroid Build Coastguard Worker     }
385*38e8c45fSAndroid Build Coastguard Worker 
386*38e8c45fSAndroid Build Coastguard Worker     // XXX: The spec draft is inconsistent as to whether this should return an
387*38e8c45fSAndroid Build Coastguard Worker     // EGLint or void.  Ignore the return value for now, as it's not strictly
388*38e8c45fSAndroid Build Coastguard Worker     // needed.
389*38e8c45fSAndroid Build Coastguard Worker     eglWaitSyncKHR(mEGLDisplay, sync, 0);
390*38e8c45fSAndroid Build Coastguard Worker     EGLint error = eglGetError();
391*38e8c45fSAndroid Build Coastguard Worker     eglDestroySyncKHR(mEGLDisplay, sync);
392*38e8c45fSAndroid Build Coastguard Worker     if (error != EGL_SUCCESS) {
393*38e8c45fSAndroid Build Coastguard Worker         ALOGE("failed to wait for EGL native fence sync: %#x", error);
394*38e8c45fSAndroid Build Coastguard Worker         return false;
395*38e8c45fSAndroid Build Coastguard Worker     }
396*38e8c45fSAndroid Build Coastguard Worker 
397*38e8c45fSAndroid Build Coastguard Worker     return true;
398*38e8c45fSAndroid Build Coastguard Worker }
399*38e8c45fSAndroid Build Coastguard Worker 
flushGL()400*38e8c45fSAndroid Build Coastguard Worker base::unique_fd SkiaGLRenderEngine::flushGL() {
401*38e8c45fSAndroid Build Coastguard Worker     SFTRACE_CALL();
402*38e8c45fSAndroid Build Coastguard Worker     if (!GLExtensions::getInstance().hasNativeFenceSync()) {
403*38e8c45fSAndroid Build Coastguard Worker         return base::unique_fd();
404*38e8c45fSAndroid Build Coastguard Worker     }
405*38e8c45fSAndroid Build Coastguard Worker 
406*38e8c45fSAndroid Build Coastguard Worker     EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
407*38e8c45fSAndroid Build Coastguard Worker     if (sync == EGL_NO_SYNC_KHR) {
408*38e8c45fSAndroid Build Coastguard Worker         ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
409*38e8c45fSAndroid Build Coastguard Worker         return base::unique_fd();
410*38e8c45fSAndroid Build Coastguard Worker     }
411*38e8c45fSAndroid Build Coastguard Worker 
412*38e8c45fSAndroid Build Coastguard Worker     // native fence fd will not be populated until flush() is done.
413*38e8c45fSAndroid Build Coastguard Worker     glFlush();
414*38e8c45fSAndroid Build Coastguard Worker 
415*38e8c45fSAndroid Build Coastguard Worker     // get the fence fd
416*38e8c45fSAndroid Build Coastguard Worker     base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
417*38e8c45fSAndroid Build Coastguard Worker     eglDestroySyncKHR(mEGLDisplay, sync);
418*38e8c45fSAndroid Build Coastguard Worker     if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
419*38e8c45fSAndroid Build Coastguard Worker         ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
420*38e8c45fSAndroid Build Coastguard Worker     }
421*38e8c45fSAndroid Build Coastguard Worker 
422*38e8c45fSAndroid Build Coastguard Worker     return fenceFd;
423*38e8c45fSAndroid Build Coastguard Worker }
424*38e8c45fSAndroid Build Coastguard Worker 
createEglContext(EGLDisplay display,EGLConfig config,EGLContext shareContext,std::optional<ContextPriority> contextPriority,Protection protection)425*38e8c45fSAndroid Build Coastguard Worker EGLContext SkiaGLRenderEngine::createEglContext(EGLDisplay display, EGLConfig config,
426*38e8c45fSAndroid Build Coastguard Worker                                                 EGLContext shareContext,
427*38e8c45fSAndroid Build Coastguard Worker                                                 std::optional<ContextPriority> contextPriority,
428*38e8c45fSAndroid Build Coastguard Worker                                                 Protection protection) {
429*38e8c45fSAndroid Build Coastguard Worker     EGLint renderableType = 0;
430*38e8c45fSAndroid Build Coastguard Worker     if (config == EGL_NO_CONFIG_KHR) {
431*38e8c45fSAndroid Build Coastguard Worker         renderableType = EGL_OPENGL_ES3_BIT;
432*38e8c45fSAndroid Build Coastguard Worker     } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
433*38e8c45fSAndroid Build Coastguard Worker         LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
434*38e8c45fSAndroid Build Coastguard Worker     }
435*38e8c45fSAndroid Build Coastguard Worker     EGLint contextClientVersion = 0;
436*38e8c45fSAndroid Build Coastguard Worker     if (renderableType & EGL_OPENGL_ES3_BIT) {
437*38e8c45fSAndroid Build Coastguard Worker         contextClientVersion = 3;
438*38e8c45fSAndroid Build Coastguard Worker     } else if (renderableType & EGL_OPENGL_ES2_BIT) {
439*38e8c45fSAndroid Build Coastguard Worker         contextClientVersion = 2;
440*38e8c45fSAndroid Build Coastguard Worker     } else if (renderableType & EGL_OPENGL_ES_BIT) {
441*38e8c45fSAndroid Build Coastguard Worker         contextClientVersion = 1;
442*38e8c45fSAndroid Build Coastguard Worker     } else {
443*38e8c45fSAndroid Build Coastguard Worker         LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
444*38e8c45fSAndroid Build Coastguard Worker     }
445*38e8c45fSAndroid Build Coastguard Worker 
446*38e8c45fSAndroid Build Coastguard Worker     std::vector<EGLint> contextAttributes;
447*38e8c45fSAndroid Build Coastguard Worker     contextAttributes.reserve(7);
448*38e8c45fSAndroid Build Coastguard Worker     contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
449*38e8c45fSAndroid Build Coastguard Worker     contextAttributes.push_back(contextClientVersion);
450*38e8c45fSAndroid Build Coastguard Worker     if (contextPriority) {
451*38e8c45fSAndroid Build Coastguard Worker         contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
452*38e8c45fSAndroid Build Coastguard Worker         switch (*contextPriority) {
453*38e8c45fSAndroid Build Coastguard Worker             case ContextPriority::REALTIME:
454*38e8c45fSAndroid Build Coastguard Worker                 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_REALTIME_NV);
455*38e8c45fSAndroid Build Coastguard Worker                 break;
456*38e8c45fSAndroid Build Coastguard Worker             case ContextPriority::MEDIUM:
457*38e8c45fSAndroid Build Coastguard Worker                 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_MEDIUM_IMG);
458*38e8c45fSAndroid Build Coastguard Worker                 break;
459*38e8c45fSAndroid Build Coastguard Worker             case ContextPriority::LOW:
460*38e8c45fSAndroid Build Coastguard Worker                 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LOW_IMG);
461*38e8c45fSAndroid Build Coastguard Worker                 break;
462*38e8c45fSAndroid Build Coastguard Worker             case ContextPriority::HIGH:
463*38e8c45fSAndroid Build Coastguard Worker             default:
464*38e8c45fSAndroid Build Coastguard Worker                 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
465*38e8c45fSAndroid Build Coastguard Worker                 break;
466*38e8c45fSAndroid Build Coastguard Worker         }
467*38e8c45fSAndroid Build Coastguard Worker     }
468*38e8c45fSAndroid Build Coastguard Worker     if (protection == Protection::PROTECTED) {
469*38e8c45fSAndroid Build Coastguard Worker         contextAttributes.push_back(EGL_PROTECTED_CONTENT_EXT);
470*38e8c45fSAndroid Build Coastguard Worker         contextAttributes.push_back(EGL_TRUE);
471*38e8c45fSAndroid Build Coastguard Worker     }
472*38e8c45fSAndroid Build Coastguard Worker     contextAttributes.push_back(EGL_NONE);
473*38e8c45fSAndroid Build Coastguard Worker 
474*38e8c45fSAndroid Build Coastguard Worker     EGLContext context = eglCreateContext(display, config, shareContext, contextAttributes.data());
475*38e8c45fSAndroid Build Coastguard Worker 
476*38e8c45fSAndroid Build Coastguard Worker     if (contextClientVersion == 3 && context == EGL_NO_CONTEXT) {
477*38e8c45fSAndroid Build Coastguard Worker         // eglGetConfigAttrib indicated we can create GLES 3 context, but we failed, thus
478*38e8c45fSAndroid Build Coastguard Worker         // EGL_NO_CONTEXT so that we can abort.
479*38e8c45fSAndroid Build Coastguard Worker         if (config != EGL_NO_CONFIG_KHR) {
480*38e8c45fSAndroid Build Coastguard Worker             return context;
481*38e8c45fSAndroid Build Coastguard Worker         }
482*38e8c45fSAndroid Build Coastguard Worker         // If |config| is EGL_NO_CONFIG_KHR, we speculatively try to create GLES 3 context, so we
483*38e8c45fSAndroid Build Coastguard Worker         // should try to fall back to GLES 2.
484*38e8c45fSAndroid Build Coastguard Worker         contextAttributes[1] = 2;
485*38e8c45fSAndroid Build Coastguard Worker         context = eglCreateContext(display, config, shareContext, contextAttributes.data());
486*38e8c45fSAndroid Build Coastguard Worker     }
487*38e8c45fSAndroid Build Coastguard Worker 
488*38e8c45fSAndroid Build Coastguard Worker     return context;
489*38e8c45fSAndroid Build Coastguard Worker }
490*38e8c45fSAndroid Build Coastguard Worker 
createContextPriority(const RenderEngineCreationArgs & args)491*38e8c45fSAndroid Build Coastguard Worker std::optional<RenderEngine::ContextPriority> SkiaGLRenderEngine::createContextPriority(
492*38e8c45fSAndroid Build Coastguard Worker         const RenderEngineCreationArgs& args) {
493*38e8c45fSAndroid Build Coastguard Worker     if (!GLExtensions::getInstance().hasContextPriority()) {
494*38e8c45fSAndroid Build Coastguard Worker         return std::nullopt;
495*38e8c45fSAndroid Build Coastguard Worker     }
496*38e8c45fSAndroid Build Coastguard Worker 
497*38e8c45fSAndroid Build Coastguard Worker     switch (args.contextPriority) {
498*38e8c45fSAndroid Build Coastguard Worker         case RenderEngine::ContextPriority::REALTIME:
499*38e8c45fSAndroid Build Coastguard Worker             if (GLExtensions::getInstance().hasRealtimePriority()) {
500*38e8c45fSAndroid Build Coastguard Worker                 return RenderEngine::ContextPriority::REALTIME;
501*38e8c45fSAndroid Build Coastguard Worker             } else {
502*38e8c45fSAndroid Build Coastguard Worker                 ALOGI("Realtime priority unsupported, degrading gracefully to high priority");
503*38e8c45fSAndroid Build Coastguard Worker                 return RenderEngine::ContextPriority::HIGH;
504*38e8c45fSAndroid Build Coastguard Worker             }
505*38e8c45fSAndroid Build Coastguard Worker         case RenderEngine::ContextPriority::HIGH:
506*38e8c45fSAndroid Build Coastguard Worker         case RenderEngine::ContextPriority::MEDIUM:
507*38e8c45fSAndroid Build Coastguard Worker         case RenderEngine::ContextPriority::LOW:
508*38e8c45fSAndroid Build Coastguard Worker             return args.contextPriority;
509*38e8c45fSAndroid Build Coastguard Worker         default:
510*38e8c45fSAndroid Build Coastguard Worker             return std::nullopt;
511*38e8c45fSAndroid Build Coastguard Worker     }
512*38e8c45fSAndroid Build Coastguard Worker }
513*38e8c45fSAndroid Build Coastguard Worker 
createPlaceholderEglPbufferSurface(EGLDisplay display,EGLConfig config,int hwcFormat,Protection protection)514*38e8c45fSAndroid Build Coastguard Worker EGLSurface SkiaGLRenderEngine::createPlaceholderEglPbufferSurface(EGLDisplay display,
515*38e8c45fSAndroid Build Coastguard Worker                                                                   EGLConfig config, int hwcFormat,
516*38e8c45fSAndroid Build Coastguard Worker                                                                   Protection protection) {
517*38e8c45fSAndroid Build Coastguard Worker     EGLConfig placeholderConfig = config;
518*38e8c45fSAndroid Build Coastguard Worker     if (placeholderConfig == EGL_NO_CONFIG_KHR) {
519*38e8c45fSAndroid Build Coastguard Worker         placeholderConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
520*38e8c45fSAndroid Build Coastguard Worker     }
521*38e8c45fSAndroid Build Coastguard Worker     std::vector<EGLint> attributes;
522*38e8c45fSAndroid Build Coastguard Worker     attributes.reserve(7);
523*38e8c45fSAndroid Build Coastguard Worker     attributes.push_back(EGL_WIDTH);
524*38e8c45fSAndroid Build Coastguard Worker     attributes.push_back(1);
525*38e8c45fSAndroid Build Coastguard Worker     attributes.push_back(EGL_HEIGHT);
526*38e8c45fSAndroid Build Coastguard Worker     attributes.push_back(1);
527*38e8c45fSAndroid Build Coastguard Worker     if (protection == Protection::PROTECTED) {
528*38e8c45fSAndroid Build Coastguard Worker         attributes.push_back(EGL_PROTECTED_CONTENT_EXT);
529*38e8c45fSAndroid Build Coastguard Worker         attributes.push_back(EGL_TRUE);
530*38e8c45fSAndroid Build Coastguard Worker     }
531*38e8c45fSAndroid Build Coastguard Worker     attributes.push_back(EGL_NONE);
532*38e8c45fSAndroid Build Coastguard Worker 
533*38e8c45fSAndroid Build Coastguard Worker     return eglCreatePbufferSurface(display, placeholderConfig, attributes.data());
534*38e8c45fSAndroid Build Coastguard Worker }
535*38e8c45fSAndroid Build Coastguard Worker 
getContextPriority()536*38e8c45fSAndroid Build Coastguard Worker int SkiaGLRenderEngine::getContextPriority() {
537*38e8c45fSAndroid Build Coastguard Worker     int value;
538*38e8c45fSAndroid Build Coastguard Worker     eglQueryContext(mEGLDisplay, mEGLContext, EGL_CONTEXT_PRIORITY_LEVEL_IMG, &value);
539*38e8c45fSAndroid Build Coastguard Worker     return value;
540*38e8c45fSAndroid Build Coastguard Worker }
541*38e8c45fSAndroid Build Coastguard Worker 
appendBackendSpecificInfoToDump(std::string & result)542*38e8c45fSAndroid Build Coastguard Worker void SkiaGLRenderEngine::appendBackendSpecificInfoToDump(std::string& result) {
543*38e8c45fSAndroid Build Coastguard Worker     const GLExtensions& extensions = GLExtensions::getInstance();
544*38e8c45fSAndroid Build Coastguard Worker     StringAppendF(&result, "\n ------------RE GLES (Ganesh)------------\n");
545*38e8c45fSAndroid Build Coastguard Worker     StringAppendF(&result, "EGL implementation : %s\n", extensions.getEGLVersion());
546*38e8c45fSAndroid Build Coastguard Worker     StringAppendF(&result, "%s\n", extensions.getEGLExtensions());
547*38e8c45fSAndroid Build Coastguard Worker     StringAppendF(&result, "GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
548*38e8c45fSAndroid Build Coastguard Worker                   extensions.getVersion());
549*38e8c45fSAndroid Build Coastguard Worker     StringAppendF(&result, "%s\n", extensions.getExtensions());
550*38e8c45fSAndroid Build Coastguard Worker }
551*38e8c45fSAndroid Build Coastguard Worker 
552*38e8c45fSAndroid Build Coastguard Worker } // namespace skia
553*38e8c45fSAndroid Build Coastguard Worker } // namespace renderengine
554*38e8c45fSAndroid Build Coastguard Worker } // namespace android
555