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,bool wideColorGamut,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, bool wideColorGamut,
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, wideColorGamut,
80 contentDrawBounds, backBuffer);
81 layerUpdateQueue->clear();
82
83 // Draw visual debugging features
84 if (CC_UNLIKELY(Properties::showDirtyRegions
85 || ProfileType::None != Properties::getProfileType())) {
86 SkCanvas* profileCanvas = backBuffer->getCanvas();
87 SkiaProfileRenderer profileRenderer(profileCanvas);
88 profiler->draw(profileRenderer);
89 profileCanvas->flush();
90 }
91
92 // Log memory statistics
93 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
94 dumpResourceCacheUsage();
95 }
96
97 return true;
98 }
99
swapBuffers(const Frame & frame,bool drew,const SkRect & screenDirty,FrameInfo * currentFrameInfo,bool * requireSwap)100 bool SkiaVulkanPipeline::swapBuffers(const Frame& frame, bool drew,
101 const SkRect& screenDirty, FrameInfo* currentFrameInfo, bool* requireSwap) {
102
103 *requireSwap = drew;
104
105 // Even if we decided to cancel the frame, from the perspective of jank
106 // metrics the frame was swapped at this point
107 currentFrameInfo->markSwapBuffers();
108
109 if (*requireSwap) {
110 mVkManager.swapBuffers(mVkSurface);
111 }
112
113 return *requireSwap;
114 }
115
copyLayerInto(DeferredLayerUpdater * layer,SkBitmap * bitmap)116 bool SkiaVulkanPipeline::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
117 // TODO: implement copyLayerInto for vulkan.
118 return false;
119 }
120
createLayer(RenderState & renderState,uint32_t layerWidth,uint32_t layerHeight,SkColorFilter * colorFilter,int alpha,SkBlendMode mode,bool blend)121 static Layer* createLayer(RenderState& renderState, uint32_t layerWidth, uint32_t layerHeight,
122 SkColorFilter* colorFilter, int alpha, SkBlendMode mode, bool blend) {
123 return new VkLayer(renderState, layerWidth, layerHeight, colorFilter, alpha, mode, blend);
124 }
125
createTextureLayer()126 DeferredLayerUpdater* SkiaVulkanPipeline::createTextureLayer() {
127 mVkManager.initialize();
128
129 return new DeferredLayerUpdater(mRenderThread.renderState(), createLayer, Layer::Api::Vulkan);
130 }
131
onStop()132 void SkiaVulkanPipeline::onStop() {
133 }
134
setSurface(Surface * surface,SwapBehavior swapBehavior,ColorMode colorMode)135 bool SkiaVulkanPipeline::setSurface(Surface* surface, SwapBehavior swapBehavior,
136 ColorMode colorMode) {
137 if (mVkSurface) {
138 mVkManager.destroySurface(mVkSurface);
139 mVkSurface = nullptr;
140 }
141
142 if (surface) {
143 // TODO: handle color mode
144 mVkSurface = mVkManager.createSurface(surface);
145 }
146
147 return mVkSurface != nullptr;
148 }
149
isSurfaceReady()150 bool SkiaVulkanPipeline::isSurfaceReady() {
151 return CC_UNLIKELY(mVkSurface != nullptr);
152 }
153
isContextReady()154 bool SkiaVulkanPipeline::isContextReady() {
155 return CC_LIKELY(mVkManager.hasVkContext());
156 }
157
invokeFunctor(const RenderThread & thread,Functor * functor)158 void SkiaVulkanPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
159 // TODO: we currently don't support OpenGL WebView's
160 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
161 (*functor)(mode, nullptr);
162 }
163
allocateHardwareBitmap(renderthread::RenderThread & renderThread,SkBitmap & skBitmap)164 sk_sp<Bitmap> SkiaVulkanPipeline::allocateHardwareBitmap(renderthread::RenderThread& renderThread,
165 SkBitmap& skBitmap) {
166 //TODO: implement this function for Vulkan pipeline
167 //code below is a hack to avoid crashing because of missing HW Bitmap support
168 sp<GraphicBuffer> buffer = new GraphicBuffer(skBitmap.info().width(), skBitmap.info().height(),
169 PIXEL_FORMAT_RGBA_8888,
170 GraphicBuffer::USAGE_HW_TEXTURE |
171 GraphicBuffer::USAGE_SW_WRITE_NEVER |
172 GraphicBuffer::USAGE_SW_READ_NEVER,
173 std::string("SkiaVulkanPipeline::allocateHardwareBitmap pid [")
174 + std::to_string(getpid()) + "]");
175 status_t error = buffer->initCheck();
176 if (error < 0) {
177 ALOGW("SkiaVulkanPipeline::allocateHardwareBitmap() failed in GraphicBuffer.create()");
178 return nullptr;
179 }
180 return sk_sp<Bitmap>(new Bitmap(buffer.get(), skBitmap.info()));
181 }
182
183 } /* namespace skiapipeline */
184 } /* namespace uirenderer */
185 } /* namespace android */
186