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" |
| 20 | #include "renderthread/EglManager.h" |
| 21 | #include "renderstate/RenderState.h" |
| 22 | #include "Readback.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 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 98 | return true; |
| 99 | } |
| 100 | |
| 101 | bool SkiaOpenGLPipeline::swapBuffers(const Frame& frame, bool drew, |
| 102 | const SkRect& screenDirty, FrameInfo* currentFrameInfo, bool* requireSwap) { |
| 103 | |
| 104 | GL_CHECKPOINT(LOW); |
| 105 | |
| 106 | // Even if we decided to cancel the frame, from the perspective of jank |
| 107 | // metrics the frame was swapped at this point |
| 108 | currentFrameInfo->markSwapBuffers(); |
| 109 | |
| 110 | *requireSwap = drew || mEglManager.damageRequiresSwap(); |
| 111 | |
| 112 | if (*requireSwap && (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty)))) { |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | return *requireSwap; |
| 117 | } |
| 118 | |
| 119 | bool SkiaOpenGLPipeline::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) { |
| 120 | layer->apply(); |
| 121 | return Readback::copyTextureLayerInto(mRenderThread, *(layer->backingLayer()), bitmap) |
| 122 | == CopyResult::Success; |
| 123 | } |
| 124 | |
| 125 | DeferredLayerUpdater* SkiaOpenGLPipeline::createTextureLayer() { |
| 126 | mEglManager.initialize(); |
| 127 | Layer* layer = new Layer(mRenderThread.renderState(), 0, 0); |
| 128 | layer->generateTexture(); |
| 129 | return new DeferredLayerUpdater(layer); |
| 130 | } |
| 131 | |
| 132 | void SkiaOpenGLPipeline::onStop() { |
| 133 | if (mEglManager.isCurrent(mEglSurface)) { |
| 134 | mEglManager.makeCurrent(EGL_NO_SURFACE); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | bool SkiaOpenGLPipeline::setSurface(Surface* surface, SwapBehavior swapBehavior) { |
| 139 | |
| 140 | if (mEglSurface != EGL_NO_SURFACE) { |
| 141 | mEglManager.destroySurface(mEglSurface); |
| 142 | mEglSurface = EGL_NO_SURFACE; |
| 143 | } |
| 144 | |
| 145 | if (surface) { |
| 146 | mEglSurface = mEglManager.createSurface(surface); |
| 147 | } |
| 148 | |
| 149 | if (mEglSurface != EGL_NO_SURFACE) { |
| 150 | const bool preserveBuffer = (swapBehavior != SwapBehavior::kSwap_discardBuffer); |
| 151 | mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer); |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | bool SkiaOpenGLPipeline::isSurfaceReady() { |
| 159 | return CC_UNLIKELY(mEglSurface != EGL_NO_SURFACE); |
| 160 | } |
| 161 | |
| 162 | bool SkiaOpenGLPipeline::isContextReady() { |
| 163 | return CC_LIKELY(mEglManager.hasEglContext()); |
| 164 | } |
| 165 | |
| 166 | void SkiaOpenGLPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) { |
| 167 | DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext; |
| 168 | if (thread.eglManager().hasEglContext()) { |
| 169 | mode = DrawGlInfo::kModeProcess; |
| 170 | } |
| 171 | |
| 172 | (*functor)(mode, nullptr); |
| 173 | |
| 174 | // If there's no context we don't need to reset as there's no gl state to save/restore |
| 175 | if (mode != DrawGlInfo::kModeProcessNoContext) { |
| 176 | thread.getGrContext()->resetContext(); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | } /* namespace skiapipeline */ |
| 181 | } /* namespace uirenderer */ |
| 182 | } /* namespace android */ |