blob: e1ef71f7d3abc1a9f90e8ab405347970f7129f28 [file] [log] [blame]
Stan Iliev500a0c32016-10-26 10:30:09 -04001/*
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"
Greg Danielcd558522016-11-17 13:31:40 -050020#include "renderthread/Frame.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040021#include "Readback.h"
22#include "renderstate/RenderState.h"
Matt Sarettcf2c05c2016-10-26 11:03:23 -040023#include "SkiaPipeline.h"
24#include "SkiaProfileRenderer.h"
Greg Daniel8cd3edf2017-01-09 14:15:41 -050025#include "VkLayer.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040026
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050027#include <SkSurface.h>
Stan Iliev500a0c32016-10-26 10:30:09 -040028#include <SkTypes.h>
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050029
30#include <GrContext.h>
31#include <GrTypes.h>
32#include <vk/GrVkTypes.h>
Stan Iliev500a0c32016-10-26 10:30:09 -040033
Stan Iliev500a0c32016-10-26 10:30:09 -040034#include <cutils/properties.h>
35#include <strings.h>
36
37using namespace android::uirenderer::renderthread;
Stan Iliev500a0c32016-10-26 10:30:09 -040038
39namespace android {
40namespace uirenderer {
41namespace skiapipeline {
42
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050043SkiaVulkanPipeline::SkiaVulkanPipeline(renderthread::RenderThread& thread)
44 : SkiaPipeline(thread)
45 , mVkManager(thread.vulkanManager()) {}
46
Stan Iliev500a0c32016-10-26 10:30:09 -040047MakeCurrentResult SkiaVulkanPipeline::makeCurrent() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050048 return MakeCurrentResult::AlreadyCurrent;
Stan Iliev500a0c32016-10-26 10:30:09 -040049}
50
51Frame SkiaVulkanPipeline::getFrame() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050052 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) {
Stan Iliev500a0c32016-10-26 10:30:09 -040057 SkDebugf("failed to get backbuffer");
58 return Frame(-1, -1, 0);
59 }
60
Greg Danielcd558522016-11-17 13:31:40 -050061 Frame frame(backBuffer->width(), backBuffer->height(), mVkManager.getAge(mVkSurface));
Stan Iliev500a0c32016-10-26 10:30:09 -040062 return frame;
63}
64
65bool SkiaVulkanPipeline::draw(const Frame& frame, const SkRect& screenDirty,
66 const SkRect& dirty,
67 const FrameBuilder::LightGeometry& lightGeometry,
68 LayerUpdateQueue* layerUpdateQueue,
Romain Guy07ae5052017-06-13 18:25:32 -070069 const Rect& contentDrawBounds, bool opaque, bool wideColorGamut,
Stan Iliev500a0c32016-10-26 10:30:09 -040070 const BakedOpRenderer::LightInfo& lightInfo,
71 const std::vector<sp<RenderNode>>& renderNodes,
72 FrameInfoVisualizer* profiler) {
73
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050074 sk_sp<SkSurface> backBuffer = mVkSurface->getBackBufferSurface();
75 if (backBuffer.get() == nullptr) {
Stan Iliev500a0c32016-10-26 10:30:09 -040076 return false;
77 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050078 SkiaPipeline::updateLighting(lightGeometry, lightInfo);
Romain Guy07ae5052017-06-13 18:25:32 -070079 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, wideColorGamut,
80 contentDrawBounds, backBuffer);
Stan Iliev500a0c32016-10-26 10:30:09 -040081 layerUpdateQueue->clear();
Matt Sarettcf2c05c2016-10-26 11:03:23 -040082
83 // Draw visual debugging features
84 if (CC_UNLIKELY(Properties::showDirtyRegions
Matt Sarett4c9bbf42016-11-07 14:23:12 -050085 || ProfileType::None != Properties::getProfileType())) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050086 SkCanvas* profileCanvas = backBuffer->getCanvas();
Matt Sarettcf2c05c2016-10-26 11:03:23 -040087 SkiaProfileRenderer profileRenderer(profileCanvas);
88 profiler->draw(profileRenderer);
89 profileCanvas->flush();
90 }
91
Matt Sarett4bda6bf2016-11-07 15:43:41 -050092 // Log memory statistics
93 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
94 dumpResourceCacheUsage();
95 }
96
Stan Iliev500a0c32016-10-26 10:30:09 -040097 return true;
98}
99
100bool 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) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500110 mVkManager.swapBuffers(mVkSurface);
Stan Iliev500a0c32016-10-26 10:30:09 -0400111 }
112
Stan Iliev500a0c32016-10-26 10:30:09 -0400113 return *requireSwap;
114}
115
116bool SkiaVulkanPipeline::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
117 // TODO: implement copyLayerInto for vulkan.
118 return false;
119}
120
sergeyv3e9999b2017-01-19 15:37:02 -0800121static 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
Stan Iliev500a0c32016-10-26 10:30:09 -0400126DeferredLayerUpdater* SkiaVulkanPipeline::createTextureLayer() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500127 mVkManager.initialize();
Greg Daniel8cd3edf2017-01-09 14:15:41 -0500128
sergeyv3e9999b2017-01-19 15:37:02 -0800129 return new DeferredLayerUpdater(mRenderThread.renderState(), createLayer, Layer::Api::Vulkan);
Stan Iliev500a0c32016-10-26 10:30:09 -0400130}
131
132void SkiaVulkanPipeline::onStop() {
133}
134
Romain Guy26a2b972017-04-17 09:39:51 -0700135bool SkiaVulkanPipeline::setSurface(Surface* surface, SwapBehavior swapBehavior,
136 ColorMode colorMode) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500137 if (mVkSurface) {
138 mVkManager.destroySurface(mVkSurface);
139 mVkSurface = nullptr;
Stan Iliev500a0c32016-10-26 10:30:09 -0400140 }
141
142 if (surface) {
Romain Guy26a2b972017-04-17 09:39:51 -0700143 // TODO: handle color mode
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500144 mVkSurface = mVkManager.createSurface(surface);
Stan Iliev500a0c32016-10-26 10:30:09 -0400145 }
146
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500147 return mVkSurface != nullptr;
Stan Iliev500a0c32016-10-26 10:30:09 -0400148}
149
150bool SkiaVulkanPipeline::isSurfaceReady() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500151 return CC_UNLIKELY(mVkSurface != nullptr);
Stan Iliev500a0c32016-10-26 10:30:09 -0400152}
153
154bool SkiaVulkanPipeline::isContextReady() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500155 return CC_LIKELY(mVkManager.hasVkContext());
Stan Iliev500a0c32016-10-26 10:30:09 -0400156}
157
158void 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
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400164sk_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
Stan Iliev500a0c32016-10-26 10:30:09 -0400183} /* namespace skiapipeline */
184} /* namespace uirenderer */
185} /* namespace android */