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" |
Greg Daniel | 8cd3edf | 2017-01-09 14:15:41 -0500 | [diff] [blame] | 20 | #include "GlLayer.h" |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 21 | #include "LayerDrawable.h" |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 22 | #include "renderthread/EglManager.h" |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 23 | #include "renderthread/Frame.h" |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 24 | #include "renderstate/RenderState.h" |
Matt Sarett | cf2c05c | 2016-10-26 11:03:23 -0400 | [diff] [blame] | 25 | #include "SkiaPipeline.h" |
| 26 | #include "SkiaProfileRenderer.h" |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 27 | #include "utils/TraceUtils.h" |
| 28 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 29 | #include <cutils/properties.h> |
| 30 | #include <strings.h> |
| 31 | |
| 32 | using namespace android::uirenderer::renderthread; |
| 33 | |
| 34 | namespace android { |
| 35 | namespace uirenderer { |
| 36 | namespace skiapipeline { |
| 37 | |
| 38 | SkiaOpenGLPipeline::SkiaOpenGLPipeline(RenderThread& thread) |
| 39 | : SkiaPipeline(thread) |
| 40 | , mEglManager(thread.eglManager()) { |
| 41 | } |
| 42 | |
| 43 | MakeCurrentResult SkiaOpenGLPipeline::makeCurrent() { |
| 44 | // TODO: Figure out why this workaround is needed, see b/13913604 |
| 45 | // In the meantime this matches the behavior of GLRenderer, so it is not a regression |
| 46 | EGLint error = 0; |
| 47 | if (!mEglManager.makeCurrent(mEglSurface, &error)) { |
| 48 | return MakeCurrentResult::AlreadyCurrent; |
| 49 | } |
| 50 | return error ? MakeCurrentResult::Failed : MakeCurrentResult::Succeeded; |
| 51 | } |
| 52 | |
| 53 | Frame SkiaOpenGLPipeline::getFrame() { |
| 54 | LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE, |
| 55 | "drawRenderNode called on a context with no surface!"); |
| 56 | return mEglManager.beginFrame(mEglSurface); |
| 57 | } |
| 58 | |
| 59 | bool SkiaOpenGLPipeline::draw(const Frame& frame, const SkRect& screenDirty, |
| 60 | const SkRect& dirty, |
| 61 | const FrameBuilder::LightGeometry& lightGeometry, |
| 62 | LayerUpdateQueue* layerUpdateQueue, |
| 63 | const Rect& contentDrawBounds, bool opaque, |
| 64 | const BakedOpRenderer::LightInfo& lightInfo, |
| 65 | const std::vector<sp<RenderNode>>& renderNodes, |
| 66 | FrameInfoVisualizer* profiler) { |
| 67 | |
| 68 | mEglManager.damageFrame(frame, dirty); |
| 69 | |
| 70 | // setup surface for fbo0 |
| 71 | GrBackendRenderTargetDesc renderTargetDesc; |
| 72 | renderTargetDesc.fWidth = frame.width(); |
| 73 | renderTargetDesc.fHeight = frame.height(); |
| 74 | renderTargetDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 75 | renderTargetDesc.fOrigin = kBottomLeft_GrSurfaceOrigin; |
| 76 | renderTargetDesc.fSampleCnt = 0; |
| 77 | renderTargetDesc.fStencilBits = STENCIL_BUFFER_SIZE; |
| 78 | renderTargetDesc.fRenderTargetHandle = 0; |
| 79 | |
| 80 | SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
| 81 | |
| 82 | SkASSERT(mRenderThread.getGrContext() != nullptr); |
| 83 | sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget( |
| 84 | mRenderThread.getGrContext(), renderTargetDesc, &props)); |
| 85 | |
| 86 | SkiaPipeline::updateLighting(lightGeometry, lightInfo); |
| 87 | renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, surface); |
| 88 | layerUpdateQueue->clear(); |
Matt Sarett | cf2c05c | 2016-10-26 11:03:23 -0400 | [diff] [blame] | 89 | |
| 90 | // Draw visual debugging features |
| 91 | if (CC_UNLIKELY(Properties::showDirtyRegions |
Matt Sarett | 4c9bbf4 | 2016-11-07 14:23:12 -0500 | [diff] [blame] | 92 | || ProfileType::None != Properties::getProfileType())) { |
Matt Sarett | cf2c05c | 2016-10-26 11:03:23 -0400 | [diff] [blame] | 93 | SkCanvas* profileCanvas = surface->getCanvas(); |
| 94 | SkiaProfileRenderer profileRenderer(profileCanvas); |
| 95 | profiler->draw(profileRenderer); |
| 96 | profileCanvas->flush(); |
| 97 | } |
| 98 | |
Matt Sarett | 4bda6bf | 2016-11-07 15:43:41 -0500 | [diff] [blame] | 99 | // Log memory statistics |
| 100 | if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) { |
| 101 | dumpResourceCacheUsage(); |
| 102 | } |
| 103 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 104 | return true; |
| 105 | } |
| 106 | |
| 107 | bool SkiaOpenGLPipeline::swapBuffers(const Frame& frame, bool drew, |
| 108 | const SkRect& screenDirty, FrameInfo* currentFrameInfo, bool* requireSwap) { |
| 109 | |
| 110 | GL_CHECKPOINT(LOW); |
| 111 | |
| 112 | // Even if we decided to cancel the frame, from the perspective of jank |
| 113 | // metrics the frame was swapped at this point |
| 114 | currentFrameInfo->markSwapBuffers(); |
| 115 | |
| 116 | *requireSwap = drew || mEglManager.damageRequiresSwap(); |
| 117 | |
| 118 | if (*requireSwap && (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty)))) { |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | return *requireSwap; |
| 123 | } |
| 124 | |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 125 | bool SkiaOpenGLPipeline::copyLayerInto(DeferredLayerUpdater* deferredLayer, SkBitmap* bitmap) { |
| 126 | if (!mRenderThread.getGrContext()) { |
| 127 | return false; |
| 128 | } |
| 129 | |
Chris Craik | 2f1aaf7 | 2017-02-14 13:01:42 -0800 | [diff] [blame] | 130 | // acquire most recent buffer for drawing |
| 131 | deferredLayer->updateTexImage(); |
Derek Sollenberger | c4fbada | 2016-11-07 16:05:41 -0500 | [diff] [blame] | 132 | deferredLayer->apply(); |
| 133 | |
| 134 | SkCanvas canvas(*bitmap); |
| 135 | Layer* layer = deferredLayer->backingLayer(); |
| 136 | return LayerDrawable::DrawLayer(mRenderThread.getGrContext(), &canvas, layer); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 137 | } |
| 138 | |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 139 | static Layer* createLayer(RenderState& renderState, uint32_t layerWidth, uint32_t layerHeight, |
| 140 | SkColorFilter* colorFilter, int alpha, SkBlendMode mode, bool blend) { |
| 141 | GlLayer* layer = new GlLayer(renderState, layerWidth, layerHeight, colorFilter, alpha, |
| 142 | mode, blend); |
| 143 | layer->generateTexture(); |
| 144 | return layer; |
| 145 | } |
| 146 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 147 | DeferredLayerUpdater* SkiaOpenGLPipeline::createTextureLayer() { |
| 148 | mEglManager.initialize(); |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 149 | return new DeferredLayerUpdater(mRenderThread.renderState(), createLayer, Layer::Api::OpenGL); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 150 | } |
| 151 | |
| 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, |
| 159 | ColorMode colorMode) { |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 160 | |
| 161 | if (mEglSurface != EGL_NO_SURFACE) { |
| 162 | mEglManager.destroySurface(mEglSurface); |
| 163 | mEglSurface = EGL_NO_SURFACE; |
| 164 | } |
| 165 | |
| 166 | if (surface) { |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame^] | 167 | const bool wideColorGamut = colorMode == ColorMode::WideColorGamut; |
| 168 | mEglSurface = mEglManager.createSurface(surface, wideColorGamut); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | if (mEglSurface != EGL_NO_SURFACE) { |
| 172 | const bool preserveBuffer = (swapBehavior != SwapBehavior::kSwap_discardBuffer); |
| 173 | mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer); |
| 174 | return true; |
| 175 | } |
| 176 | |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | bool SkiaOpenGLPipeline::isSurfaceReady() { |
| 181 | return CC_UNLIKELY(mEglSurface != EGL_NO_SURFACE); |
| 182 | } |
| 183 | |
| 184 | bool SkiaOpenGLPipeline::isContextReady() { |
| 185 | return CC_LIKELY(mEglManager.hasEglContext()); |
| 186 | } |
| 187 | |
| 188 | void SkiaOpenGLPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) { |
| 189 | DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext; |
| 190 | if (thread.eglManager().hasEglContext()) { |
| 191 | mode = DrawGlInfo::kModeProcess; |
| 192 | } |
| 193 | |
| 194 | (*functor)(mode, nullptr); |
| 195 | |
| 196 | // If there's no context we don't need to reset as there's no gl state to save/restore |
| 197 | if (mode != DrawGlInfo::kModeProcessNoContext) { |
| 198 | thread.getGrContext()->resetContext(); |
| 199 | } |
| 200 | } |
| 201 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 202 | } /* namespace skiapipeline */ |
| 203 | } /* namespace uirenderer */ |
| 204 | } /* namespace android */ |