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