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