blob: 5f2eee4523fc45aa42004b01c86c8b8401edbe4a [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"
Stan Iliev500a0c32016-10-26 10:30:09 -040025
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050026#include <SkSurface.h>
Stan Iliev500a0c32016-10-26 10:30:09 -040027#include <SkTypes.h>
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050028
29#include <GrContext.h>
30#include <GrTypes.h>
31#include <vk/GrVkTypes.h>
Stan Iliev500a0c32016-10-26 10:30:09 -040032
Stan Iliev500a0c32016-10-26 10:30:09 -040033#include <cutils/properties.h>
34#include <strings.h>
35
36using namespace android::uirenderer::renderthread;
Stan Iliev500a0c32016-10-26 10:30:09 -040037
38namespace android {
39namespace uirenderer {
40namespace skiapipeline {
41
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050042SkiaVulkanPipeline::SkiaVulkanPipeline(renderthread::RenderThread& thread)
John Reck1bcacfd2017-11-03 10:12:19 -070043 : SkiaPipeline(thread), mVkManager(thread.vulkanManager()) {}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050044
Stan Iliev500a0c32016-10-26 10:30:09 -040045MakeCurrentResult SkiaVulkanPipeline::makeCurrent() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050046 return MakeCurrentResult::AlreadyCurrent;
Stan Iliev500a0c32016-10-26 10:30:09 -040047}
48
49Frame SkiaVulkanPipeline::getFrame() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050050 LOG_ALWAYS_FATAL_IF(mVkSurface == nullptr,
John Reck1bcacfd2017-11-03 10:12:19 -070051 "drawRenderNode called on a context with no surface!");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050052
53 SkSurface* backBuffer = mVkManager.getBackbufferSurface(mVkSurface);
54 if (backBuffer == nullptr) {
Stan Iliev500a0c32016-10-26 10:30:09 -040055 SkDebugf("failed to get backbuffer");
56 return Frame(-1, -1, 0);
57 }
58
Greg Danielcd558522016-11-17 13:31:40 -050059 Frame frame(backBuffer->width(), backBuffer->height(), mVkManager.getAge(mVkSurface));
Stan Iliev500a0c32016-10-26 10:30:09 -040060 return frame;
61}
62
John Reck1bcacfd2017-11-03 10:12:19 -070063bool SkiaVulkanPipeline::draw(const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
John Reckd9d7f122018-05-03 14:40:56 -070064 const LightGeometry& lightGeometry,
John Reck1bcacfd2017-11-03 10:12:19 -070065 LayerUpdateQueue* layerUpdateQueue, const Rect& contentDrawBounds,
66 bool opaque, bool wideColorGamut,
John Reckd9d7f122018-05-03 14:40:56 -070067 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);
John Reck1bcacfd2017-11-03 10:12:19 -070075 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, wideColorGamut, contentDrawBounds,
76 backBuffer);
Stan Iliev500a0c32016-10-26 10:30:09 -040077 layerUpdateQueue->clear();
Matt Sarettcf2c05c2016-10-26 11:03:23 -040078
79 // Draw visual debugging features
John Reck1bcacfd2017-11-03 10:12:19 -070080 if (CC_UNLIKELY(Properties::showDirtyRegions ||
81 ProfileType::None != Properties::getProfileType())) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050082 SkCanvas* profileCanvas = backBuffer->getCanvas();
Matt Sarettcf2c05c2016-10-26 11:03:23 -040083 SkiaProfileRenderer profileRenderer(profileCanvas);
84 profiler->draw(profileRenderer);
85 profileCanvas->flush();
86 }
87
Matt Sarett4bda6bf2016-11-07 15:43:41 -050088 // Log memory statistics
89 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
90 dumpResourceCacheUsage();
91 }
92
Stan Iliev500a0c32016-10-26 10:30:09 -040093 return true;
94}
95
John Reck1bcacfd2017-11-03 10:12:19 -070096bool SkiaVulkanPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
97 FrameInfo* currentFrameInfo, bool* requireSwap) {
Stan Iliev500a0c32016-10-26 10:30:09 -040098 *requireSwap = drew;
99
100 // Even if we decided to cancel the frame, from the perspective of jank
101 // metrics the frame was swapped at this point
102 currentFrameInfo->markSwapBuffers();
103
104 if (*requireSwap) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500105 mVkManager.swapBuffers(mVkSurface);
Stan Iliev500a0c32016-10-26 10:30:09 -0400106 }
107
Stan Iliev500a0c32016-10-26 10:30:09 -0400108 return *requireSwap;
109}
110
111bool SkiaVulkanPipeline::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
112 // TODO: implement copyLayerInto for vulkan.
113 return false;
114}
115
116DeferredLayerUpdater* SkiaVulkanPipeline::createTextureLayer() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500117 mVkManager.initialize();
Greg Daniel8cd3edf2017-01-09 14:15:41 -0500118
Stan Iliev85f90962018-08-31 18:35:06 +0000119 return new DeferredLayerUpdater(mRenderThread.renderState());
Stan Iliev500a0c32016-10-26 10:30:09 -0400120}
121
John Reck1bcacfd2017-11-03 10:12:19 -0700122void SkiaVulkanPipeline::onStop() {}
Stan Iliev500a0c32016-10-26 10:30:09 -0400123
Romain Guy26a2b972017-04-17 09:39:51 -0700124bool SkiaVulkanPipeline::setSurface(Surface* surface, SwapBehavior swapBehavior,
John Reck1bcacfd2017-11-03 10:12:19 -0700125 ColorMode colorMode) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500126 if (mVkSurface) {
127 mVkManager.destroySurface(mVkSurface);
128 mVkSurface = nullptr;
Stan Iliev500a0c32016-10-26 10:30:09 -0400129 }
130
131 if (surface) {
Romain Guy26a2b972017-04-17 09:39:51 -0700132 // TODO: handle color mode
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500133 mVkSurface = mVkManager.createSurface(surface);
Stan Iliev500a0c32016-10-26 10:30:09 -0400134 }
135
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500136 return mVkSurface != nullptr;
Stan Iliev500a0c32016-10-26 10:30:09 -0400137}
138
139bool SkiaVulkanPipeline::isSurfaceReady() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500140 return CC_UNLIKELY(mVkSurface != nullptr);
Stan Iliev500a0c32016-10-26 10:30:09 -0400141}
142
143bool SkiaVulkanPipeline::isContextReady() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500144 return CC_LIKELY(mVkManager.hasVkContext());
Stan Iliev500a0c32016-10-26 10:30:09 -0400145}
146
147void SkiaVulkanPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
148 // TODO: we currently don't support OpenGL WebView's
149 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
150 (*functor)(mode, nullptr);
151}
152
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400153sk_sp<Bitmap> SkiaVulkanPipeline::allocateHardwareBitmap(renderthread::RenderThread& renderThread,
John Reck1bcacfd2017-11-03 10:12:19 -0700154 SkBitmap& skBitmap) {
155 // TODO: implement this function for Vulkan pipeline
156 // code below is a hack to avoid crashing because of missing HW Bitmap support
157 sp<GraphicBuffer> buffer = new GraphicBuffer(
158 skBitmap.info().width(), skBitmap.info().height(), PIXEL_FORMAT_RGBA_8888,
159 GraphicBuffer::USAGE_HW_TEXTURE | GraphicBuffer::USAGE_SW_WRITE_NEVER |
160 GraphicBuffer::USAGE_SW_READ_NEVER,
161 std::string("SkiaVulkanPipeline::allocateHardwareBitmap pid [") +
162 std::to_string(getpid()) + "]");
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400163 status_t error = buffer->initCheck();
164 if (error < 0) {
165 ALOGW("SkiaVulkanPipeline::allocateHardwareBitmap() failed in GraphicBuffer.create()");
166 return nullptr;
167 }
168 return sk_sp<Bitmap>(new Bitmap(buffer.get(), skBitmap.info()));
169}
170
Stan Iliev500a0c32016-10-26 10:30:09 -0400171} /* namespace skiapipeline */
172} /* namespace uirenderer */
173} /* namespace android */