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