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 <gui/TraceUtils.h>
20 #include "DeferredLayerUpdater.h"
21 #include "LightingInfo.h"
22 #include "Readback.h"
23 #include "ShaderCache.h"
24 #include "SkiaPipeline.h"
25 #include "SkiaProfileRenderer.h"
26 #include "VkInteropFunctorDrawable.h"
27 #include "renderstate/RenderState.h"
28 #include "renderthread/Frame.h"
29
30 #include <SkSurface.h>
31 #include <SkTypes.h>
32
33 #include <GrDirectContext.h>
34 #include <GrTypes.h>
35 #include <vk/GrVkTypes.h>
36
37 #include <cutils/properties.h>
38 #include <strings.h>
39
40 using namespace android::uirenderer::renderthread;
41
42 namespace android {
43 namespace uirenderer {
44 namespace skiapipeline {
45
SkiaVulkanPipeline(renderthread::RenderThread & thread)46 SkiaVulkanPipeline::SkiaVulkanPipeline(renderthread::RenderThread& thread) : SkiaPipeline(thread) {
47 thread.renderState().registerContextCallback(this);
48 }
49
~SkiaVulkanPipeline()50 SkiaVulkanPipeline::~SkiaVulkanPipeline() {
51 mRenderThread.renderState().removeContextCallback(this);
52 }
53
vulkanManager()54 VulkanManager& SkiaVulkanPipeline::vulkanManager() {
55 return mRenderThread.vulkanManager();
56 }
57
makeCurrent()58 MakeCurrentResult SkiaVulkanPipeline::makeCurrent() {
59 return MakeCurrentResult::AlreadyCurrent;
60 }
61
getFrame()62 Frame SkiaVulkanPipeline::getFrame() {
63 LOG_ALWAYS_FATAL_IF(mVkSurface == nullptr, "getFrame() called on a context with no surface!");
64 return vulkanManager().dequeueNextBuffer(mVkSurface);
65 }
66
draw(const Frame & frame,const SkRect & screenDirty,const SkRect & dirty,const LightGeometry & lightGeometry,LayerUpdateQueue * layerUpdateQueue,const Rect & contentDrawBounds,bool opaque,const LightInfo & lightInfo,const std::vector<sp<RenderNode>> & renderNodes,FrameInfoVisualizer * profiler)67 bool SkiaVulkanPipeline::draw(const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
68 const LightGeometry& lightGeometry,
69 LayerUpdateQueue* layerUpdateQueue, const Rect& contentDrawBounds,
70 bool opaque, const LightInfo& lightInfo,
71 const std::vector<sp<RenderNode>>& renderNodes,
72 FrameInfoVisualizer* profiler) {
73 sk_sp<SkSurface> backBuffer = mVkSurface->getCurrentSkSurface();
74 if (backBuffer.get() == nullptr) {
75 return false;
76 }
77 LightingInfo::updateLighting(lightGeometry, lightInfo);
78 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, backBuffer,
79 mVkSurface->getCurrentPreTransform());
80
81 // Draw visual debugging features
82 if (CC_UNLIKELY(Properties::showDirtyRegions ||
83 ProfileType::None != Properties::getProfileType())) {
84 SkCanvas* profileCanvas = backBuffer->getCanvas();
85 SkiaProfileRenderer profileRenderer(profileCanvas);
86 profiler->draw(profileRenderer);
87 }
88
89 {
90 ATRACE_NAME("flush commands");
91 vulkanManager().finishFrame(backBuffer.get());
92 }
93 layerUpdateQueue->clear();
94
95 // Log memory statistics
96 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
97 dumpResourceCacheUsage();
98 }
99
100 return true;
101 }
102
swapBuffers(const Frame & frame,bool drew,const SkRect & screenDirty,FrameInfo * currentFrameInfo,bool * requireSwap)103 bool SkiaVulkanPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
104 FrameInfo* currentFrameInfo, bool* requireSwap) {
105 *requireSwap = drew;
106
107 // Even if we decided to cancel the frame, from the perspective of jank
108 // metrics the frame was swapped at this point
109 currentFrameInfo->markSwapBuffers();
110
111 if (*requireSwap) {
112 vulkanManager().swapBuffers(mVkSurface, screenDirty);
113 }
114
115 return *requireSwap;
116 }
117
createTextureLayer()118 DeferredLayerUpdater* SkiaVulkanPipeline::createTextureLayer() {
119 mRenderThread.requireVkContext();
120
121 return new DeferredLayerUpdater(mRenderThread.renderState());
122 }
123
onStop()124 void SkiaVulkanPipeline::onStop() {}
125
setSurface(ANativeWindow * surface,SwapBehavior swapBehavior)126 bool SkiaVulkanPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior) {
127 if (mVkSurface) {
128 vulkanManager().destroySurface(mVkSurface);
129 mVkSurface = nullptr;
130 }
131
132 if (surface) {
133 mRenderThread.requireVkContext();
134 mVkSurface =
135 vulkanManager().createSurface(surface, mColorMode, mSurfaceColorSpace,
136 mSurfaceColorType, mRenderThread.getGrContext(), 0);
137 }
138
139 return mVkSurface != nullptr;
140 }
141
isSurfaceReady()142 bool SkiaVulkanPipeline::isSurfaceReady() {
143 return CC_UNLIKELY(mVkSurface != nullptr);
144 }
145
isContextReady()146 bool SkiaVulkanPipeline::isContextReady() {
147 return CC_LIKELY(vulkanManager().hasVkContext());
148 }
149
invokeFunctor(const RenderThread & thread,Functor * functor)150 void SkiaVulkanPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
151 VkInteropFunctorDrawable::vkInvokeFunctor(functor);
152 }
153
allocateHardwareBitmap(renderthread::RenderThread & renderThread,SkBitmap & skBitmap)154 sk_sp<Bitmap> SkiaVulkanPipeline::allocateHardwareBitmap(renderthread::RenderThread& renderThread,
155 SkBitmap& skBitmap) {
156 LOG_ALWAYS_FATAL("Unimplemented");
157 return nullptr;
158 }
159
onContextDestroyed()160 void SkiaVulkanPipeline::onContextDestroyed() {
161 if (mVkSurface) {
162 vulkanManager().destroySurface(mVkSurface);
163 mVkSurface = nullptr;
164 }
165 }
166
167 } /* namespace skiapipeline */
168 } /* namespace uirenderer */
169 } /* namespace android */
170