Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #include "SkiaOpenGLPipeline.h" |
| 18 | |
Alec Mouri | 3afb397 | 2022-05-27 22:03:11 +0000 | [diff] [blame] | 19 | #include <GrBackendSurface.h> |
| 20 | #include <SkBlendMode.h> |
| 21 | #include <SkImageInfo.h> |
| 22 | #include <cutils/properties.h> |
rnlee | ce9762b | 2021-05-21 15:40:53 -0700 | [diff] [blame] | 23 | #include <gui/TraceUtils.h> |
Alec Mouri | 3afb397 | 2022-05-27 22:03:11 +0000 | [diff] [blame] | 24 | #include <strings.h> |
| 25 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 26 | #include "DeferredLayerUpdater.h" |
Alec Mouri | 3afb397 | 2022-05-27 22:03:11 +0000 | [diff] [blame] | 27 | #include "FrameInfo.h" |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 28 | #include "LayerDrawable.h" |
Fedor Kudasov | 90df056 | 2019-06-19 11:41:34 +0100 | [diff] [blame] | 29 | #include "LightingInfo.h" |
Matt Sarett | cf2c05c | 2016-10-26 11:03:23 -0400 | [diff] [blame] | 30 | #include "SkiaPipeline.h" |
| 31 | #include "SkiaProfileRenderer.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 32 | #include "hwui/Bitmap.h" |
Derek Sollenberger | 28a4d99 | 2018-09-20 13:37:24 -0400 | [diff] [blame] | 33 | #include "private/hwui/DrawGlInfo.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 34 | #include "renderstate/RenderState.h" |
| 35 | #include "renderthread/EglManager.h" |
| 36 | #include "renderthread/Frame.h" |
Alec Mouri | 3afb397 | 2022-05-27 22:03:11 +0000 | [diff] [blame] | 37 | #include "renderthread/IRenderPipeline.h" |
John Reck | d9d7f12 | 2018-05-03 14:40:56 -0700 | [diff] [blame] | 38 | #include "utils/GLUtils.h" |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 39 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 40 | using namespace android::uirenderer::renderthread; |
| 41 | |
| 42 | namespace android { |
| 43 | namespace uirenderer { |
| 44 | namespace skiapipeline { |
| 45 | |
| 46 | SkiaOpenGLPipeline::SkiaOpenGLPipeline(RenderThread& thread) |
Derek Sollenberger | 5a5a648 | 2018-09-19 13:52:13 -0400 | [diff] [blame] | 47 | : SkiaPipeline(thread), mEglManager(thread.eglManager()) { |
| 48 | thread.renderState().registerContextCallback(this); |
| 49 | } |
| 50 | |
| 51 | SkiaOpenGLPipeline::~SkiaOpenGLPipeline() { |
| 52 | mRenderThread.renderState().removeContextCallback(this); |
| 53 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 54 | |
| 55 | MakeCurrentResult SkiaOpenGLPipeline::makeCurrent() { |
Derek Sollenberger | b830772 | 2022-08-30 14:24:22 -0400 | [diff] [blame] | 56 | // In case the surface was destroyed (e.g. a previous trimMemory call) we |
| 57 | // need to recreate it here. |
Nader Jawad | a352185 | 2023-01-30 20:23:46 -0800 | [diff] [blame^] | 58 | if (mHardwareBuffer) { |
| 59 | mRenderThread.requireGlContext(); |
| 60 | } else if (!isSurfaceReady() && mNativeWindow) { |
Derek Sollenberger | b830772 | 2022-08-30 14:24:22 -0400 | [diff] [blame] | 61 | setSurface(mNativeWindow.get(), mSwapBehavior); |
| 62 | } |
| 63 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 64 | EGLint error = 0; |
| 65 | if (!mEglManager.makeCurrent(mEglSurface, &error)) { |
| 66 | return MakeCurrentResult::AlreadyCurrent; |
| 67 | } |
| 68 | return error ? MakeCurrentResult::Failed : MakeCurrentResult::Succeeded; |
| 69 | } |
| 70 | |
| 71 | Frame SkiaOpenGLPipeline::getFrame() { |
Nader Jawad | a352185 | 2023-01-30 20:23:46 -0800 | [diff] [blame^] | 72 | if (mHardwareBuffer) { |
| 73 | AHardwareBuffer_Desc description; |
| 74 | AHardwareBuffer_describe(mHardwareBuffer, &description); |
| 75 | return Frame(description.width, description.height, 0); |
| 76 | } else { |
| 77 | LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE, |
| 78 | "drawRenderNode called on a context with no surface!"); |
| 79 | return mEglManager.beginFrame(mEglSurface); |
| 80 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 81 | } |
| 82 | |
Alec Mouri | 3afb397 | 2022-05-27 22:03:11 +0000 | [diff] [blame] | 83 | IRenderPipeline::DrawResult SkiaOpenGLPipeline::draw( |
| 84 | const Frame& frame, const SkRect& screenDirty, const SkRect& dirty, |
| 85 | const LightGeometry& lightGeometry, LayerUpdateQueue* layerUpdateQueue, |
| 86 | const Rect& contentDrawBounds, bool opaque, const LightInfo& lightInfo, |
Nader Jawad | a352185 | 2023-01-30 20:23:46 -0800 | [diff] [blame^] | 87 | const std::vector<sp<RenderNode>>& renderNodes, FrameInfoVisualizer* profiler, |
| 88 | const HardwareBufferRenderParams& bufferParams) { |
| 89 | if (!isCapturingSkp() && !mHardwareBuffer) { |
John Reck | 7600518 | 2021-06-09 22:43:05 -0400 | [diff] [blame] | 90 | mEglManager.damageFrame(frame, dirty); |
| 91 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 92 | |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 93 | SkColorType colorType = getSurfaceColorType(); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 94 | // setup surface for fbo0 |
Greg Daniel | ac2d232 | 2017-07-12 11:30:15 -0400 | [diff] [blame] | 95 | GrGLFramebufferInfo fboInfo; |
| 96 | fboInfo.fFBOID = 0; |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 97 | if (colorType == kRGBA_F16_SkColorType) { |
Greg Daniel | c9da8e8 | 2018-03-21 10:50:24 -0400 | [diff] [blame] | 98 | fboInfo.fFormat = GL_RGBA16F; |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 99 | } else if (colorType == kN32_SkColorType) { |
| 100 | // Note: The default preference of pixel format is RGBA_8888, when other |
| 101 | // pixel format is available, we should branch out and do more check. |
Greg Daniel | c9da8e8 | 2018-03-21 10:50:24 -0400 | [diff] [blame] | 102 | fboInfo.fFormat = GL_RGBA8; |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame] | 103 | } else if (colorType == kRGBA_1010102_SkColorType) { |
| 104 | fboInfo.fFormat = GL_RGB10_A2; |
Leon Scroggins III | e157ee8 | 2021-11-30 14:12:42 -0500 | [diff] [blame] | 105 | } else if (colorType == kAlpha_8_SkColorType) { |
| 106 | fboInfo.fFormat = GL_R8; |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 107 | } else { |
| 108 | LOG_ALWAYS_FATAL("Unsupported color type."); |
Greg Daniel | c9da8e8 | 2018-03-21 10:50:24 -0400 | [diff] [blame] | 109 | } |
Greg Daniel | ac2d232 | 2017-07-12 11:30:15 -0400 | [diff] [blame] | 110 | |
Greg Daniel | c9da8e8 | 2018-03-21 10:50:24 -0400 | [diff] [blame] | 111 | GrBackendRenderTarget backendRT(frame.width(), frame.height(), 0, STENCIL_BUFFER_SIZE, fboInfo); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 112 | |
| 113 | SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
| 114 | |
| 115 | SkASSERT(mRenderThread.getGrContext() != nullptr); |
Nader Jawad | a352185 | 2023-01-30 20:23:46 -0800 | [diff] [blame^] | 116 | sk_sp<SkSurface> surface; |
| 117 | SkMatrix preTransform; |
| 118 | if (mHardwareBuffer) { |
| 119 | surface = getBufferSkSurface(bufferParams); |
| 120 | preTransform = bufferParams.getTransform(); |
| 121 | } else { |
| 122 | surface = SkSurface::MakeFromBackendRenderTarget(mRenderThread.getGrContext(), backendRT, |
| 123 | getSurfaceOrigin(), colorType, |
| 124 | mSurfaceColorSpace, &props); |
| 125 | preTransform = SkMatrix::I(); |
| 126 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 127 | |
Nader Jawad | a352185 | 2023-01-30 20:23:46 -0800 | [diff] [blame^] | 128 | SkPoint lightCenter = preTransform.mapXY(lightGeometry.center.x, lightGeometry.center.y); |
| 129 | LightGeometry localGeometry = lightGeometry; |
| 130 | localGeometry.center.x = lightCenter.fX; |
| 131 | localGeometry.center.y = lightCenter.fY; |
| 132 | LightingInfo::updateLighting(localGeometry, lightInfo); |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 133 | renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface, |
Nader Jawad | a352185 | 2023-01-30 20:23:46 -0800 | [diff] [blame^] | 134 | preTransform); |
Matt Sarett | cf2c05c | 2016-10-26 11:03:23 -0400 | [diff] [blame] | 135 | |
| 136 | // Draw visual debugging features |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 137 | if (CC_UNLIKELY(Properties::showDirtyRegions || |
| 138 | ProfileType::None != Properties::getProfileType())) { |
Matt Sarett | cf2c05c | 2016-10-26 11:03:23 -0400 | [diff] [blame] | 139 | SkCanvas* profileCanvas = surface->getCanvas(); |
John Reck | c30836d | 2022-08-12 14:28:31 -0400 | [diff] [blame] | 140 | SkiaProfileRenderer profileRenderer(profileCanvas, frame.width(), frame.height()); |
Matt Sarett | cf2c05c | 2016-10-26 11:03:23 -0400 | [diff] [blame] | 141 | profiler->draw(profileRenderer); |
Matt Sarett | cf2c05c | 2016-10-26 11:03:23 -0400 | [diff] [blame] | 142 | } |
| 143 | |
Greg Daniel | be2803a | 2021-02-19 18:32:16 -0500 | [diff] [blame] | 144 | { |
| 145 | ATRACE_NAME("flush commands"); |
| 146 | surface->flushAndSubmit(); |
| 147 | } |
| 148 | layerUpdateQueue->clear(); |
| 149 | |
Matt Sarett | 4bda6bf | 2016-11-07 15:43:41 -0500 | [diff] [blame] | 150 | // Log memory statistics |
| 151 | if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) { |
| 152 | dumpResourceCacheUsage(); |
| 153 | } |
| 154 | |
Alec Mouri | 3afb397 | 2022-05-27 22:03:11 +0000 | [diff] [blame] | 155 | return {true, IRenderPipeline::DrawResult::kUnknownTime}; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 156 | } |
| 157 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 158 | bool SkiaOpenGLPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty, |
| 159 | FrameInfo* currentFrameInfo, bool* requireSwap) { |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 160 | GL_CHECKPOINT(LOW); |
| 161 | |
| 162 | // Even if we decided to cancel the frame, from the perspective of jank |
| 163 | // metrics the frame was swapped at this point |
| 164 | currentFrameInfo->markSwapBuffers(); |
| 165 | |
Nader Jawad | a352185 | 2023-01-30 20:23:46 -0800 | [diff] [blame^] | 166 | if (mHardwareBuffer) { |
| 167 | return false; |
| 168 | } |
| 169 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 170 | *requireSwap = drew || mEglManager.damageRequiresSwap(); |
| 171 | |
| 172 | if (*requireSwap && (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty)))) { |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | return *requireSwap; |
| 177 | } |
| 178 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 179 | DeferredLayerUpdater* SkiaOpenGLPipeline::createTextureLayer() { |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 180 | mRenderThread.requireGlContext(); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 181 | return new DeferredLayerUpdater(mRenderThread.renderState()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 182 | } |
| 183 | |
Derek Sollenberger | 5a5a648 | 2018-09-19 13:52:13 -0400 | [diff] [blame] | 184 | void SkiaOpenGLPipeline::onContextDestroyed() { |
| 185 | if (mEglSurface != EGL_NO_SURFACE) { |
| 186 | mEglManager.destroySurface(mEglSurface); |
| 187 | mEglSurface = EGL_NO_SURFACE; |
| 188 | } |
| 189 | } |
| 190 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 191 | void SkiaOpenGLPipeline::onStop() { |
| 192 | if (mEglManager.isCurrent(mEglSurface)) { |
| 193 | mEglManager.makeCurrent(EGL_NO_SURFACE); |
| 194 | } |
| 195 | } |
| 196 | |
John Reck | 8ddbc59 | 2020-05-07 16:11:18 -0700 | [diff] [blame] | 197 | bool SkiaOpenGLPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior) { |
Derek Sollenberger | b830772 | 2022-08-30 14:24:22 -0400 | [diff] [blame] | 198 | mNativeWindow = surface; |
| 199 | mSwapBehavior = swapBehavior; |
| 200 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 201 | if (mEglSurface != EGL_NO_SURFACE) { |
| 202 | mEglManager.destroySurface(mEglSurface); |
| 203 | mEglSurface = EGL_NO_SURFACE; |
| 204 | } |
| 205 | |
| 206 | if (surface) { |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 207 | mRenderThread.requireGlContext(); |
Derek Sollenberger | 1863d94 | 2020-02-05 15:41:51 -0500 | [diff] [blame] | 208 | auto newSurface = mEglManager.createSurface(surface, mColorMode, mSurfaceColorSpace); |
John Reck | 8e539ca | 2018-11-15 15:21:29 -0800 | [diff] [blame] | 209 | if (!newSurface) { |
| 210 | return false; |
| 211 | } |
| 212 | mEglSurface = newSurface.unwrap(); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | if (mEglSurface != EGL_NO_SURFACE) { |
| 216 | const bool preserveBuffer = (swapBehavior != SwapBehavior::kSwap_discardBuffer); |
Derek Sollenberger | b830772 | 2022-08-30 14:24:22 -0400 | [diff] [blame] | 217 | const bool isPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer); |
| 218 | ALOGE_IF(preserveBuffer != isPreserved, "Unable to match the desired swap behavior."); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 219 | return true; |
| 220 | } |
| 221 | |
| 222 | return false; |
| 223 | } |
| 224 | |
Nader Jawad | a352185 | 2023-01-30 20:23:46 -0800 | [diff] [blame^] | 225 | [[nodiscard]] android::base::unique_fd SkiaOpenGLPipeline::flush() { |
| 226 | int fence = -1; |
| 227 | EGLSyncKHR sync = EGL_NO_SYNC_KHR; |
| 228 | mEglManager.createReleaseFence(true, &sync, &fence); |
| 229 | // If a sync object is returned here then the device does not support native |
| 230 | // fences, we block on the returned sync and return -1 as a file descriptor |
| 231 | if (sync != EGL_NO_SYNC_KHR) { |
| 232 | EGLDisplay display = mEglManager.eglDisplay(); |
| 233 | EGLint result = eglClientWaitSyncKHR(display, sync, 0, 1000000000); |
| 234 | if (result == EGL_FALSE) { |
| 235 | ALOGE("EglManager::createReleaseFence: error waiting for previous fence: %#x", |
| 236 | eglGetError()); |
| 237 | } else if (result == EGL_TIMEOUT_EXPIRED_KHR) { |
| 238 | ALOGE("EglManager::createReleaseFence: timeout waiting for previous fence"); |
| 239 | } |
| 240 | eglDestroySyncKHR(display, sync); |
| 241 | } |
| 242 | return android::base::unique_fd(fence); |
| 243 | } |
| 244 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 245 | bool SkiaOpenGLPipeline::isSurfaceReady() { |
| 246 | return CC_UNLIKELY(mEglSurface != EGL_NO_SURFACE); |
| 247 | } |
| 248 | |
| 249 | bool SkiaOpenGLPipeline::isContextReady() { |
| 250 | return CC_LIKELY(mEglManager.hasEglContext()); |
| 251 | } |
| 252 | |
| 253 | void SkiaOpenGLPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) { |
| 254 | DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext; |
| 255 | if (thread.eglManager().hasEglContext()) { |
| 256 | mode = DrawGlInfo::kModeProcess; |
| 257 | } |
| 258 | |
| 259 | (*functor)(mode, nullptr); |
| 260 | |
| 261 | // If there's no context we don't need to reset as there's no gl state to save/restore |
| 262 | if (mode != DrawGlInfo::kModeProcessNoContext) { |
| 263 | thread.getGrContext()->resetContext(); |
| 264 | } |
| 265 | } |
| 266 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 267 | } /* namespace skiapipeline */ |
| 268 | } /* namespace uirenderer */ |
| 269 | } /* namespace android */ |