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 | |
rnlee | ce9762b | 2021-05-21 15:40:53 -0700 | [diff] [blame] | 19 | #include <gui/TraceUtils.h> |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 20 | #include "DeferredLayerUpdater.h" |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 21 | #include "LayerDrawable.h" |
Fedor Kudasov | 90df056 | 2019-06-19 11:41:34 +0100 | [diff] [blame] | 22 | #include "LightingInfo.h" |
Matt Sarett | cf2c05c | 2016-10-26 11:03:23 -0400 | [diff] [blame] | 23 | #include "SkiaPipeline.h" |
| 24 | #include "SkiaProfileRenderer.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 25 | #include "hwui/Bitmap.h" |
Derek Sollenberger | 28a4d99 | 2018-09-20 13:37:24 -0400 | [diff] [blame] | 26 | #include "private/hwui/DrawGlInfo.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 27 | #include "renderstate/RenderState.h" |
| 28 | #include "renderthread/EglManager.h" |
| 29 | #include "renderthread/Frame.h" |
John Reck | d9d7f12 | 2018-05-03 14:40:56 -0700 | [diff] [blame] | 30 | #include "utils/GLUtils.h" |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 31 | |
John Reck | bdc9f1b | 2018-09-14 15:22:35 -0700 | [diff] [blame] | 32 | #include <GLES3/gl3.h> |
| 33 | |
Greg Daniel | ac2d232 | 2017-07-12 11:30:15 -0400 | [diff] [blame] | 34 | #include <GrBackendSurface.h> |
Derek Sollenberger | 2324991 | 2018-05-03 16:12:18 -0400 | [diff] [blame] | 35 | #include <SkBlendMode.h> |
John Reck | 437f6fb | 2018-05-07 08:14:14 -0700 | [diff] [blame] | 36 | #include <SkImageInfo.h> |
Greg Daniel | ac2d232 | 2017-07-12 11:30:15 -0400 | [diff] [blame] | 37 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 38 | #include <cutils/properties.h> |
| 39 | #include <strings.h> |
| 40 | |
| 41 | using namespace android::uirenderer::renderthread; |
| 42 | |
| 43 | namespace android { |
| 44 | namespace uirenderer { |
| 45 | namespace skiapipeline { |
| 46 | |
| 47 | SkiaOpenGLPipeline::SkiaOpenGLPipeline(RenderThread& thread) |
Derek Sollenberger | 5a5a648 | 2018-09-19 13:52:13 -0400 | [diff] [blame] | 48 | : SkiaPipeline(thread), mEglManager(thread.eglManager()) { |
| 49 | thread.renderState().registerContextCallback(this); |
| 50 | } |
| 51 | |
| 52 | SkiaOpenGLPipeline::~SkiaOpenGLPipeline() { |
| 53 | mRenderThread.renderState().removeContextCallback(this); |
| 54 | } |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 55 | |
| 56 | MakeCurrentResult SkiaOpenGLPipeline::makeCurrent() { |
| 57 | // TODO: Figure out why this workaround is needed, see b/13913604 |
| 58 | // In the meantime this matches the behavior of GLRenderer, so it is not a regression |
| 59 | EGLint error = 0; |
| 60 | if (!mEglManager.makeCurrent(mEglSurface, &error)) { |
| 61 | return MakeCurrentResult::AlreadyCurrent; |
| 62 | } |
| 63 | return error ? MakeCurrentResult::Failed : MakeCurrentResult::Succeeded; |
| 64 | } |
| 65 | |
| 66 | Frame SkiaOpenGLPipeline::getFrame() { |
| 67 | LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 68 | "drawRenderNode called on a context with no surface!"); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 69 | return mEglManager.beginFrame(mEglSurface); |
| 70 | } |
| 71 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 72 | bool SkiaOpenGLPipeline::draw(const Frame& frame, const SkRect& screenDirty, const SkRect& dirty, |
John Reck | d9d7f12 | 2018-05-03 14:40:56 -0700 | [diff] [blame] | 73 | const LightGeometry& lightGeometry, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 74 | LayerUpdateQueue* layerUpdateQueue, const Rect& contentDrawBounds, |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 75 | bool opaque, const LightInfo& lightInfo, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 76 | const std::vector<sp<RenderNode>>& renderNodes, |
| 77 | FrameInfoVisualizer* profiler) { |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 78 | mEglManager.damageFrame(frame, dirty); |
| 79 | |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 80 | SkColorType colorType = getSurfaceColorType(); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 81 | // setup surface for fbo0 |
Greg Daniel | ac2d232 | 2017-07-12 11:30:15 -0400 | [diff] [blame] | 82 | GrGLFramebufferInfo fboInfo; |
| 83 | fboInfo.fFBOID = 0; |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 84 | if (colorType == kRGBA_F16_SkColorType) { |
Greg Daniel | c9da8e8 | 2018-03-21 10:50:24 -0400 | [diff] [blame] | 85 | fboInfo.fFormat = GL_RGBA16F; |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 86 | } else if (colorType == kN32_SkColorType) { |
| 87 | // Note: The default preference of pixel format is RGBA_8888, when other |
| 88 | // pixel format is available, we should branch out and do more check. |
Greg Daniel | c9da8e8 | 2018-03-21 10:50:24 -0400 | [diff] [blame] | 89 | fboInfo.fFormat = GL_RGBA8; |
John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame] | 90 | } else if (colorType == kRGBA_1010102_SkColorType) { |
| 91 | fboInfo.fFormat = GL_RGB10_A2; |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 92 | } else { |
| 93 | LOG_ALWAYS_FATAL("Unsupported color type."); |
Greg Daniel | c9da8e8 | 2018-03-21 10:50:24 -0400 | [diff] [blame] | 94 | } |
Greg Daniel | ac2d232 | 2017-07-12 11:30:15 -0400 | [diff] [blame] | 95 | |
Greg Daniel | c9da8e8 | 2018-03-21 10:50:24 -0400 | [diff] [blame] | 96 | GrBackendRenderTarget backendRT(frame.width(), frame.height(), 0, STENCIL_BUFFER_SIZE, fboInfo); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 97 | |
| 98 | SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
| 99 | |
| 100 | SkASSERT(mRenderThread.getGrContext() != nullptr); |
| 101 | sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget( |
Derek Sollenberger | 25833d2 | 2019-01-14 13:55:55 -0500 | [diff] [blame] | 102 | mRenderThread.getGrContext(), backendRT, this->getSurfaceOrigin(), colorType, |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 103 | mSurfaceColorSpace, &props)); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 104 | |
Fedor Kudasov | 90df056 | 2019-06-19 11:41:34 +0100 | [diff] [blame] | 105 | LightingInfo::updateLighting(lightGeometry, lightInfo); |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 106 | renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface, |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 107 | SkMatrix::I()); |
Matt Sarett | cf2c05c | 2016-10-26 11:03:23 -0400 | [diff] [blame] | 108 | |
| 109 | // Draw visual debugging features |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 110 | if (CC_UNLIKELY(Properties::showDirtyRegions || |
| 111 | ProfileType::None != Properties::getProfileType())) { |
Matt Sarett | cf2c05c | 2016-10-26 11:03:23 -0400 | [diff] [blame] | 112 | SkCanvas* profileCanvas = surface->getCanvas(); |
| 113 | SkiaProfileRenderer profileRenderer(profileCanvas); |
| 114 | profiler->draw(profileRenderer); |
Matt Sarett | cf2c05c | 2016-10-26 11:03:23 -0400 | [diff] [blame] | 115 | } |
| 116 | |
Greg Daniel | be2803a | 2021-02-19 18:32:16 -0500 | [diff] [blame] | 117 | { |
| 118 | ATRACE_NAME("flush commands"); |
| 119 | surface->flushAndSubmit(); |
| 120 | } |
| 121 | layerUpdateQueue->clear(); |
| 122 | |
Matt Sarett | 4bda6bf | 2016-11-07 15:43:41 -0500 | [diff] [blame] | 123 | // Log memory statistics |
| 124 | if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) { |
| 125 | dumpResourceCacheUsage(); |
| 126 | } |
| 127 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 128 | return true; |
| 129 | } |
| 130 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 131 | bool SkiaOpenGLPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty, |
| 132 | FrameInfo* currentFrameInfo, bool* requireSwap) { |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 133 | GL_CHECKPOINT(LOW); |
| 134 | |
| 135 | // Even if we decided to cancel the frame, from the perspective of jank |
| 136 | // metrics the frame was swapped at this point |
| 137 | currentFrameInfo->markSwapBuffers(); |
| 138 | |
| 139 | *requireSwap = drew || mEglManager.damageRequiresSwap(); |
| 140 | |
| 141 | if (*requireSwap && (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty)))) { |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | return *requireSwap; |
| 146 | } |
| 147 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 148 | DeferredLayerUpdater* SkiaOpenGLPipeline::createTextureLayer() { |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 149 | mRenderThread.requireGlContext(); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 150 | return new DeferredLayerUpdater(mRenderThread.renderState()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 151 | } |
| 152 | |
Derek Sollenberger | 5a5a648 | 2018-09-19 13:52:13 -0400 | [diff] [blame] | 153 | void SkiaOpenGLPipeline::onContextDestroyed() { |
| 154 | if (mEglSurface != EGL_NO_SURFACE) { |
| 155 | mEglManager.destroySurface(mEglSurface); |
| 156 | mEglSurface = EGL_NO_SURFACE; |
| 157 | } |
| 158 | } |
| 159 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 160 | void SkiaOpenGLPipeline::onStop() { |
| 161 | if (mEglManager.isCurrent(mEglSurface)) { |
| 162 | mEglManager.makeCurrent(EGL_NO_SURFACE); |
| 163 | } |
| 164 | } |
| 165 | |
John Reck | 8ddbc59 | 2020-05-07 16:11:18 -0700 | [diff] [blame] | 166 | bool SkiaOpenGLPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior) { |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 167 | if (mEglSurface != EGL_NO_SURFACE) { |
| 168 | mEglManager.destroySurface(mEglSurface); |
| 169 | mEglSurface = EGL_NO_SURFACE; |
| 170 | } |
| 171 | |
| 172 | if (surface) { |
John Reck | 1e51071 | 2018-04-23 08:15:03 -0700 | [diff] [blame] | 173 | mRenderThread.requireGlContext(); |
Derek Sollenberger | 1863d94 | 2020-02-05 15:41:51 -0500 | [diff] [blame] | 174 | auto newSurface = mEglManager.createSurface(surface, mColorMode, mSurfaceColorSpace); |
John Reck | 8e539ca | 2018-11-15 15:21:29 -0800 | [diff] [blame] | 175 | if (!newSurface) { |
| 176 | return false; |
| 177 | } |
| 178 | mEglSurface = newSurface.unwrap(); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | if (mEglSurface != EGL_NO_SURFACE) { |
| 182 | const bool preserveBuffer = (swapBehavior != SwapBehavior::kSwap_discardBuffer); |
| 183 | mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer); |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | bool SkiaOpenGLPipeline::isSurfaceReady() { |
| 191 | return CC_UNLIKELY(mEglSurface != EGL_NO_SURFACE); |
| 192 | } |
| 193 | |
| 194 | bool SkiaOpenGLPipeline::isContextReady() { |
| 195 | return CC_LIKELY(mEglManager.hasEglContext()); |
| 196 | } |
| 197 | |
| 198 | void SkiaOpenGLPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) { |
| 199 | DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext; |
| 200 | if (thread.eglManager().hasEglContext()) { |
| 201 | mode = DrawGlInfo::kModeProcess; |
| 202 | } |
| 203 | |
| 204 | (*functor)(mode, nullptr); |
| 205 | |
| 206 | // If there's no context we don't need to reset as there's no gl state to save/restore |
| 207 | if (mode != DrawGlInfo::kModeProcessNoContext) { |
| 208 | thread.getGrContext()->resetContext(); |
| 209 | } |
| 210 | } |
| 211 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 212 | } /* namespace skiapipeline */ |
| 213 | } /* namespace uirenderer */ |
| 214 | } /* namespace android */ |