blob: 30a3fc5ac93faab8f93e4971b5c6224603e9fd15 [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 Danielbe2803a2021-02-19 18:32:16 -050020#include "LightingInfo.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040021#include "Readback.h"
John Reck0fa0cbc2019-04-05 16:57:46 -070022#include "ShaderCache.h"
Matt Sarettcf2c05c2016-10-26 11:03:23 -040023#include "SkiaPipeline.h"
24#include "SkiaProfileRenderer.h"
John Reck283bb462018-12-13 16:40:14 -080025#include "VkInteropFunctorDrawable.h"
John Reck1bcacfd2017-11-03 10:12:19 -070026#include "renderstate/RenderState.h"
27#include "renderthread/Frame.h"
Greg Danielbe2803a2021-02-19 18:32:16 -050028#include "utils/TraceUtils.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040029
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050030#include <SkSurface.h>
Stan Iliev500a0c32016-10-26 10:30:09 -040031#include <SkTypes.h>
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050032
Adlai Holler72cff422020-10-15 09:32:00 -040033#include <GrDirectContext.h>
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050034#include <GrTypes.h>
35#include <vk/GrVkTypes.h>
Stan Iliev500a0c32016-10-26 10:30:09 -040036
Stan Iliev500a0c32016-10-26 10:30:09 -040037#include <cutils/properties.h>
38#include <strings.h>
39
40using namespace android::uirenderer::renderthread;
Stan Iliev500a0c32016-10-26 10:30:09 -040041
42namespace android {
43namespace uirenderer {
44namespace skiapipeline {
45
Greg Danield2317a42021-04-08 11:39:02 -040046SkiaVulkanPipeline::SkiaVulkanPipeline(renderthread::RenderThread& thread) : SkiaPipeline(thread) {
Stan Iliev90276c82019-02-03 18:01:02 -050047 thread.renderState().registerContextCallback(this);
48}
49
50SkiaVulkanPipeline::~SkiaVulkanPipeline() {
51 mRenderThread.renderState().removeContextCallback(this);
52}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050053
Greg Danield2317a42021-04-08 11:39:02 -040054VulkanManager& SkiaVulkanPipeline::vulkanManager() {
55 return mRenderThread.vulkanManager();
56}
57
Stan Iliev500a0c32016-10-26 10:30:09 -040058MakeCurrentResult SkiaVulkanPipeline::makeCurrent() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050059 return MakeCurrentResult::AlreadyCurrent;
Stan Iliev500a0c32016-10-26 10:30:09 -040060}
61
62Frame SkiaVulkanPipeline::getFrame() {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050063 LOG_ALWAYS_FATAL_IF(mVkSurface == nullptr, "getFrame() called on a context with no surface!");
Greg Danield2317a42021-04-08 11:39:02 -040064 return vulkanManager().dequeueNextBuffer(mVkSurface);
Stan Iliev500a0c32016-10-26 10:30:09 -040065}
66
John Reck1bcacfd2017-11-03 10:12:19 -070067bool SkiaVulkanPipeline::draw(const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
John Reckd9d7f122018-05-03 14:40:56 -070068 const LightGeometry& lightGeometry,
John Reck1bcacfd2017-11-03 10:12:19 -070069 LayerUpdateQueue* layerUpdateQueue, const Rect& contentDrawBounds,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070070 bool opaque, const LightInfo& lightInfo,
John Reck1bcacfd2017-11-03 10:12:19 -070071 const std::vector<sp<RenderNode>>& renderNodes,
72 FrameInfoVisualizer* profiler) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050073 sk_sp<SkSurface> backBuffer = mVkSurface->getCurrentSkSurface();
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050074 if (backBuffer.get() == nullptr) {
Stan Iliev500a0c32016-10-26 10:30:09 -040075 return false;
76 }
Fedor Kudasov90df0562019-06-19 11:41:34 +010077 LightingInfo::updateLighting(lightGeometry, lightInfo);
John Reck0fa0cbc2019-04-05 16:57:46 -070078 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, backBuffer,
79 mVkSurface->getCurrentPreTransform());
Matt Sarettcf2c05c2016-10-26 11:03:23 -040080
81 // Draw visual debugging features
John Reck1bcacfd2017-11-03 10:12:19 -070082 if (CC_UNLIKELY(Properties::showDirtyRegions ||
83 ProfileType::None != Properties::getProfileType())) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050084 SkCanvas* profileCanvas = backBuffer->getCanvas();
Matt Sarettcf2c05c2016-10-26 11:03:23 -040085 SkiaProfileRenderer profileRenderer(profileCanvas);
86 profiler->draw(profileRenderer);
Matt Sarettcf2c05c2016-10-26 11:03:23 -040087 }
88
Greg Danielbe2803a2021-02-19 18:32:16 -050089 {
90 ATRACE_NAME("flush commands");
Greg Danield2317a42021-04-08 11:39:02 -040091 vulkanManager().finishFrame(backBuffer.get());
Greg Danielbe2803a2021-02-19 18:32:16 -050092 }
93 layerUpdateQueue->clear();
94
Matt Sarett4bda6bf2016-11-07 15:43:41 -050095 // Log memory statistics
96 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
97 dumpResourceCacheUsage();
98 }
99
Stan Iliev500a0c32016-10-26 10:30:09 -0400100 return true;
101}
102
John Reck1bcacfd2017-11-03 10:12:19 -0700103bool SkiaVulkanPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
104 FrameInfo* currentFrameInfo, bool* requireSwap) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400105 *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) {
Greg Danield2317a42021-04-08 11:39:02 -0400112 vulkanManager().swapBuffers(mVkSurface, screenDirty);
Stan Iliev500a0c32016-10-26 10:30:09 -0400113 }
114
Stan Iliev500a0c32016-10-26 10:30:09 -0400115 return *requireSwap;
116}
117
Stan Iliev500a0c32016-10-26 10:30:09 -0400118DeferredLayerUpdater* SkiaVulkanPipeline::createTextureLayer() {
Stan Iliev981afe72019-02-13 14:24:33 -0500119 mRenderThread.requireVkContext();
Greg Daniel8cd3edf2017-01-09 14:15:41 -0500120
Stan Iliev564ca3e2018-09-04 22:00:00 +0000121 return new DeferredLayerUpdater(mRenderThread.renderState());
Stan Iliev500a0c32016-10-26 10:30:09 -0400122}
123
John Reck1bcacfd2017-11-03 10:12:19 -0700124void SkiaVulkanPipeline::onStop() {}
Stan Iliev500a0c32016-10-26 10:30:09 -0400125
John Reck8ddbc592020-05-07 16:11:18 -0700126bool SkiaVulkanPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500127 if (mVkSurface) {
Greg Danield2317a42021-04-08 11:39:02 -0400128 vulkanManager().destroySurface(mVkSurface);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500129 mVkSurface = nullptr;
Stan Iliev500a0c32016-10-26 10:30:09 -0400130 }
131
132 if (surface) {
Stan Iliev981afe72019-02-13 14:24:33 -0500133 mRenderThread.requireVkContext();
John Reck0fa0cbc2019-04-05 16:57:46 -0700134 mVkSurface =
Greg Danield2317a42021-04-08 11:39:02 -0400135 vulkanManager().createSurface(surface, mColorMode, mSurfaceColorSpace,
136 mSurfaceColorType, mRenderThread.getGrContext(), 0);
Greg Daniel031b81b2018-10-02 14:47:22 -0400137 }
138
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500139 return mVkSurface != nullptr;
Stan Iliev500a0c32016-10-26 10:30:09 -0400140}
141
142bool SkiaVulkanPipeline::isSurfaceReady() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500143 return CC_UNLIKELY(mVkSurface != nullptr);
Stan Iliev500a0c32016-10-26 10:30:09 -0400144}
145
146bool SkiaVulkanPipeline::isContextReady() {
Greg Danield2317a42021-04-08 11:39:02 -0400147 return CC_LIKELY(vulkanManager().hasVkContext());
Stan Iliev500a0c32016-10-26 10:30:09 -0400148}
149
150void SkiaVulkanPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
Chris Blume5f1ac2b2018-11-05 16:10:39 -0800151 VkInteropFunctorDrawable::vkInvokeFunctor(functor);
Stan Iliev500a0c32016-10-26 10:30:09 -0400152}
153
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400154sk_sp<Bitmap> SkiaVulkanPipeline::allocateHardwareBitmap(renderthread::RenderThread& renderThread,
John Reck1bcacfd2017-11-03 10:12:19 -0700155 SkBitmap& skBitmap) {
Derek Sollenberger6e35e632019-01-22 13:56:25 -0500156 LOG_ALWAYS_FATAL("Unimplemented");
157 return nullptr;
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400158}
159
Stan Iliev90276c82019-02-03 18:01:02 -0500160void SkiaVulkanPipeline::onContextDestroyed() {
161 if (mVkSurface) {
Greg Danield2317a42021-04-08 11:39:02 -0400162 vulkanManager().destroySurface(mVkSurface);
Stan Iliev90276c82019-02-03 18:01:02 -0500163 mVkSurface = nullptr;
164 }
165}
166
Stan Iliev500a0c32016-10-26 10:30:09 -0400167} /* namespace skiapipeline */
168} /* namespace uirenderer */
169} /* namespace android */