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 "SkiaVulkanPipeline.h"
18 
19 #include "DeferredLayerUpdater.h"
20 #include "renderthread/Frame.h"
21 #include "Readback.h"
22 #include "renderstate/RenderState.h"
23 #include "SkiaPipeline.h"
24 #include "SkiaProfileRenderer.h"
25 #include "VkLayer.h"
26 
27 #include <SkSurface.h>
28 #include <SkTypes.h>
29 
30 #include <GrContext.h>
31 #include <GrTypes.h>
32 #include <vk/GrVkTypes.h>
33 
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 
SkiaVulkanPipeline(renderthread::RenderThread & thread)43 SkiaVulkanPipeline::SkiaVulkanPipeline(renderthread::RenderThread& thread)
44         : SkiaPipeline(thread)
45         , mVkManager(thread.vulkanManager()) {}
46 
makeCurrent()47 MakeCurrentResult SkiaVulkanPipeline::makeCurrent() {
48     return MakeCurrentResult::AlreadyCurrent;
49 }
50 
getFrame()51 Frame SkiaVulkanPipeline::getFrame() {
52     LOG_ALWAYS_FATAL_IF(mVkSurface == nullptr,
53                 "drawRenderNode called on a context with no surface!");
54 
55     SkSurface* backBuffer = mVkManager.getBackbufferSurface(mVkSurface);
56     if (backBuffer == nullptr) {
57         SkDebugf("failed to get backbuffer");
58         return Frame(-1, -1, 0);
59     }
60 
61     Frame frame(backBuffer->width(), backBuffer->height(), mVkManager.getAge(mVkSurface));
62     return frame;
63 }
64 
draw(const Frame & frame,const SkRect & screenDirty,const SkRect & dirty,const FrameBuilder::LightGeometry & lightGeometry,LayerUpdateQueue * layerUpdateQueue,const Rect & contentDrawBounds,bool opaque,const BakedOpRenderer::LightInfo & lightInfo,const std::vector<sp<RenderNode>> & renderNodes,FrameInfoVisualizer * profiler)65 bool SkiaVulkanPipeline::draw(const Frame& frame, const SkRect& screenDirty,
66         const SkRect& dirty,
67         const FrameBuilder::LightGeometry& lightGeometry,
68         LayerUpdateQueue* layerUpdateQueue,
69         const Rect& contentDrawBounds, bool opaque,
70         const BakedOpRenderer::LightInfo& lightInfo,
71         const std::vector<sp<RenderNode>>& renderNodes,
72         FrameInfoVisualizer* profiler) {
73 
74     sk_sp<SkSurface> backBuffer = mVkSurface->getBackBufferSurface();
75     if (backBuffer.get() == nullptr) {
76         return false;
77     }
78     SkiaPipeline::updateLighting(lightGeometry, lightInfo);
79     renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, backBuffer);
80     layerUpdateQueue->clear();
81 
82     // Draw visual debugging features
83     if (CC_UNLIKELY(Properties::showDirtyRegions
84             || ProfileType::None != Properties::getProfileType())) {
85         SkCanvas* profileCanvas = backBuffer->getCanvas();
86         SkiaProfileRenderer profileRenderer(profileCanvas);
87         profiler->draw(profileRenderer);
88         profileCanvas->flush();
89     }
90 
91     // Log memory statistics
92     if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
93         dumpResourceCacheUsage();
94     }
95 
96     return true;
97 }
98 
swapBuffers(const Frame & frame,bool drew,const SkRect & screenDirty,FrameInfo * currentFrameInfo,bool * requireSwap)99 bool SkiaVulkanPipeline::swapBuffers(const Frame& frame, bool drew,
100         const SkRect& screenDirty, FrameInfo* currentFrameInfo, bool* requireSwap) {
101 
102     *requireSwap = drew;
103 
104     // Even if we decided to cancel the frame, from the perspective of jank
105     // metrics the frame was swapped at this point
106     currentFrameInfo->markSwapBuffers();
107 
108     if (*requireSwap) {
109         mVkManager.swapBuffers(mVkSurface);
110     }
111 
112     return *requireSwap;
113 }
114 
copyLayerInto(DeferredLayerUpdater * layer,SkBitmap * bitmap)115 bool SkiaVulkanPipeline::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
116     // TODO: implement copyLayerInto for vulkan.
117     return false;
118 }
119 
createLayer(RenderState & renderState,uint32_t layerWidth,uint32_t layerHeight,SkColorFilter * colorFilter,int alpha,SkBlendMode mode,bool blend)120 static Layer* createLayer(RenderState& renderState, uint32_t layerWidth, uint32_t layerHeight,
121         SkColorFilter* colorFilter, int alpha, SkBlendMode mode, bool blend) {
122     return new VkLayer(renderState, layerWidth, layerHeight, colorFilter, alpha, mode, blend);
123 }
124 
createTextureLayer()125 DeferredLayerUpdater* SkiaVulkanPipeline::createTextureLayer() {
126     mVkManager.initialize();
127 
128     return new DeferredLayerUpdater(mRenderThread.renderState(), createLayer, Layer::Api::Vulkan);
129 }
130 
onStop()131 void SkiaVulkanPipeline::onStop() {
132 }
133 
setSurface(Surface * surface,SwapBehavior swapBehavior)134 bool SkiaVulkanPipeline::setSurface(Surface* surface, SwapBehavior swapBehavior) {
135     if (mVkSurface) {
136         mVkManager.destroySurface(mVkSurface);
137         mVkSurface = nullptr;
138     }
139 
140     if (surface) {
141         mVkSurface = mVkManager.createSurface(surface);
142     }
143 
144     return mVkSurface != nullptr;
145 }
146 
isSurfaceReady()147 bool SkiaVulkanPipeline::isSurfaceReady() {
148     return CC_UNLIKELY(mVkSurface != nullptr);
149 }
150 
isContextReady()151 bool SkiaVulkanPipeline::isContextReady() {
152     return CC_LIKELY(mVkManager.hasVkContext());
153 }
154 
invokeFunctor(const RenderThread & thread,Functor * functor)155 void SkiaVulkanPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
156     // TODO: we currently don't support OpenGL WebView's
157     DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
158     (*functor)(mode, nullptr);
159 }
160 
161 } /* namespace skiapipeline */
162 } /* namespace uirenderer */
163 } /* namespace android */
164