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