blob: a494e490aea19b4047f53ea1ee4b89c3f285b9e3 [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"
Stan Iliev500a0c32016-10-26 10:30:09 -040020#include "Readback.h"
Matt Sarettcf2c05c2016-10-26 11:03:23 -040021#include "SkiaPipeline.h"
22#include "SkiaProfileRenderer.h"
John Reck1bcacfd2017-11-03 10:12:19 -070023#include "renderstate/RenderState.h"
24#include "renderthread/Frame.h"
Chris Blume5f1ac2b2018-11-05 16:10:39 -080025#include "VkInteropFunctorDrawable.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)
John Reck1bcacfd2017-11-03 10:12:19 -070044 : SkiaPipeline(thread), mVkManager(thread.vulkanManager()) {}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050045
Stan Iliev500a0c32016-10-26 10:30:09 -040046MakeCurrentResult SkiaVulkanPipeline::makeCurrent() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050047 return MakeCurrentResult::AlreadyCurrent;
Stan Iliev500a0c32016-10-26 10:30:09 -040048}
49
50Frame SkiaVulkanPipeline::getFrame() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050051 LOG_ALWAYS_FATAL_IF(mVkSurface == nullptr,
John Reck1bcacfd2017-11-03 10:12:19 -070052 "drawRenderNode called on a context with no surface!");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050053
Stan Iliev305e13a2018-11-13 11:14:48 -050054 SkSurface* backBuffer = mVkManager.getBackbufferSurface(&mVkSurface);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050055 if (backBuffer == nullptr) {
Stan Iliev500a0c32016-10-26 10:30:09 -040056 SkDebugf("failed to get backbuffer");
57 return Frame(-1, -1, 0);
58 }
59
Greg Danielcd558522016-11-17 13:31:40 -050060 Frame frame(backBuffer->width(), backBuffer->height(), mVkManager.getAge(mVkSurface));
Stan Iliev500a0c32016-10-26 10:30:09 -040061 return frame;
62}
63
John Reck1bcacfd2017-11-03 10:12:19 -070064bool SkiaVulkanPipeline::draw(const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
John Reckd9d7f122018-05-03 14:40:56 -070065 const LightGeometry& lightGeometry,
John Reck1bcacfd2017-11-03 10:12:19 -070066 LayerUpdateQueue* layerUpdateQueue, const Rect& contentDrawBounds,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070067 bool opaque, const LightInfo& lightInfo,
John Reck1bcacfd2017-11-03 10:12:19 -070068 const std::vector<sp<RenderNode>>& renderNodes,
69 FrameInfoVisualizer* profiler) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050070 sk_sp<SkSurface> backBuffer = mVkSurface->getBackBufferSurface();
71 if (backBuffer.get() == nullptr) {
Stan Iliev500a0c32016-10-26 10:30:09 -040072 return false;
73 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050074 SkiaPipeline::updateLighting(lightGeometry, lightInfo);
Peiyong Lin1f6aa122018-09-10 16:28:08 -070075 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, backBuffer);
Stan Iliev500a0c32016-10-26 10:30:09 -040076 layerUpdateQueue->clear();
Matt Sarettcf2c05c2016-10-26 11:03:23 -040077
78 // Draw visual debugging features
John Reck1bcacfd2017-11-03 10:12:19 -070079 if (CC_UNLIKELY(Properties::showDirtyRegions ||
80 ProfileType::None != Properties::getProfileType())) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050081 SkCanvas* profileCanvas = backBuffer->getCanvas();
Matt Sarettcf2c05c2016-10-26 11:03:23 -040082 SkiaProfileRenderer profileRenderer(profileCanvas);
83 profiler->draw(profileRenderer);
84 profileCanvas->flush();
85 }
86
Matt Sarett4bda6bf2016-11-07 15:43:41 -050087 // Log memory statistics
88 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
89 dumpResourceCacheUsage();
90 }
91
Stan Iliev500a0c32016-10-26 10:30:09 -040092 return true;
93}
94
John Reck1bcacfd2017-11-03 10:12:19 -070095bool SkiaVulkanPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
96 FrameInfo* currentFrameInfo, bool* requireSwap) {
Stan Iliev500a0c32016-10-26 10:30:09 -040097 *requireSwap = drew;
98
99 // Even if we decided to cancel the frame, from the perspective of jank
100 // metrics the frame was swapped at this point
101 currentFrameInfo->markSwapBuffers();
102
103 if (*requireSwap) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500104 mVkManager.swapBuffers(mVkSurface);
Stan Iliev500a0c32016-10-26 10:30:09 -0400105 }
106
Stan Iliev500a0c32016-10-26 10:30:09 -0400107 return *requireSwap;
108}
109
Stan Iliev500a0c32016-10-26 10:30:09 -0400110DeferredLayerUpdater* SkiaVulkanPipeline::createTextureLayer() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500111 mVkManager.initialize();
Greg Daniel8cd3edf2017-01-09 14:15:41 -0500112
Stan Iliev564ca3e2018-09-04 22:00:00 +0000113 return new DeferredLayerUpdater(mRenderThread.renderState());
Stan Iliev500a0c32016-10-26 10:30:09 -0400114}
115
John Reck1bcacfd2017-11-03 10:12:19 -0700116void SkiaVulkanPipeline::onStop() {}
Stan Iliev500a0c32016-10-26 10:30:09 -0400117
Romain Guy26a2b972017-04-17 09:39:51 -0700118bool SkiaVulkanPipeline::setSurface(Surface* surface, SwapBehavior swapBehavior,
John Reck1bcacfd2017-11-03 10:12:19 -0700119 ColorMode colorMode) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500120 if (mVkSurface) {
121 mVkManager.destroySurface(mVkSurface);
122 mVkSurface = nullptr;
Stan Iliev500a0c32016-10-26 10:30:09 -0400123 }
124
125 if (surface) {
Stan Iliev79351f32018-09-19 14:23:49 -0400126 mVkSurface = mVkManager.createSurface(surface, colorMode);
Stan Iliev500a0c32016-10-26 10:30:09 -0400127 }
128
Greg Daniel031b81b2018-10-02 14:47:22 -0400129 if (colorMode == ColorMode::SRGB) {
130 mSurfaceColorType = SkColorType::kN32_SkColorType;
131 } else if (colorMode == ColorMode::WideColorGamut) {
132 mSurfaceColorType = SkColorType::kRGBA_F16_SkColorType;
133 }
134
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500135 return mVkSurface != nullptr;
Stan Iliev500a0c32016-10-26 10:30:09 -0400136}
137
138bool SkiaVulkanPipeline::isSurfaceReady() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500139 return CC_UNLIKELY(mVkSurface != nullptr);
Stan Iliev500a0c32016-10-26 10:30:09 -0400140}
141
142bool SkiaVulkanPipeline::isContextReady() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500143 return CC_LIKELY(mVkManager.hasVkContext());
Stan Iliev500a0c32016-10-26 10:30:09 -0400144}
145
146void SkiaVulkanPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
Chris Blume5f1ac2b2018-11-05 16:10:39 -0800147 VkInteropFunctorDrawable::vkInvokeFunctor(functor);
Stan Iliev500a0c32016-10-26 10:30:09 -0400148}
149
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400150sk_sp<Bitmap> SkiaVulkanPipeline::allocateHardwareBitmap(renderthread::RenderThread& renderThread,
John Reck1bcacfd2017-11-03 10:12:19 -0700151 SkBitmap& skBitmap) {
152 // TODO: implement this function for Vulkan pipeline
153 // code below is a hack to avoid crashing because of missing HW Bitmap support
154 sp<GraphicBuffer> buffer = new GraphicBuffer(
155 skBitmap.info().width(), skBitmap.info().height(), PIXEL_FORMAT_RGBA_8888,
156 GraphicBuffer::USAGE_HW_TEXTURE | GraphicBuffer::USAGE_SW_WRITE_NEVER |
157 GraphicBuffer::USAGE_SW_READ_NEVER,
158 std::string("SkiaVulkanPipeline::allocateHardwareBitmap pid [") +
159 std::to_string(getpid()) + "]");
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400160 status_t error = buffer->initCheck();
161 if (error < 0) {
162 ALOGW("SkiaVulkanPipeline::allocateHardwareBitmap() failed in GraphicBuffer.create()");
163 return nullptr;
164 }
165 return sk_sp<Bitmap>(new Bitmap(buffer.get(), skBitmap.info()));
166}
167
Stan Iliev500a0c32016-10-26 10:30:09 -0400168} /* namespace skiapipeline */
169} /* namespace uirenderer */
170} /* namespace android */