blob: 905d46e58014396ed93f73700c5424c9a382ddbe [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
Alec Mouri3afb3972022-05-27 22:03:11 +000019#include <GrDirectContext.h>
20#include <GrTypes.h>
21#include <SkSurface.h>
22#include <SkTypes.h>
23#include <cutils/properties.h>
rnleece9762b2021-05-21 15:40:53 -070024#include <gui/TraceUtils.h>
Alec Mouri3afb3972022-05-27 22:03:11 +000025#include <strings.h>
26#include <vk/GrVkTypes.h>
27
Stan Iliev500a0c32016-10-26 10:30:09 -040028#include "DeferredLayerUpdater.h"
Greg Danielbe2803a2021-02-19 18:32:16 -050029#include "LightingInfo.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040030#include "Readback.h"
John Reck0fa0cbc2019-04-05 16:57:46 -070031#include "ShaderCache.h"
Matt Sarettcf2c05c2016-10-26 11:03:23 -040032#include "SkiaPipeline.h"
33#include "SkiaProfileRenderer.h"
John Reck283bb462018-12-13 16:40:14 -080034#include "VkInteropFunctorDrawable.h"
John Reck1bcacfd2017-11-03 10:12:19 -070035#include "renderstate/RenderState.h"
36#include "renderthread/Frame.h"
Alec Mouri3afb3972022-05-27 22:03:11 +000037#include "renderthread/IRenderPipeline.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040038
39using namespace android::uirenderer::renderthread;
Stan Iliev500a0c32016-10-26 10:30:09 -040040
41namespace android {
42namespace uirenderer {
43namespace skiapipeline {
44
Greg Danield2317a42021-04-08 11:39:02 -040045SkiaVulkanPipeline::SkiaVulkanPipeline(renderthread::RenderThread& thread) : SkiaPipeline(thread) {
Stan Iliev90276c82019-02-03 18:01:02 -050046 thread.renderState().registerContextCallback(this);
47}
48
49SkiaVulkanPipeline::~SkiaVulkanPipeline() {
50 mRenderThread.renderState().removeContextCallback(this);
51}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050052
Greg Danield2317a42021-04-08 11:39:02 -040053VulkanManager& SkiaVulkanPipeline::vulkanManager() {
54 return mRenderThread.vulkanManager();
55}
56
Stan Iliev500a0c32016-10-26 10:30:09 -040057MakeCurrentResult SkiaVulkanPipeline::makeCurrent() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050058 return MakeCurrentResult::AlreadyCurrent;
Stan Iliev500a0c32016-10-26 10:30:09 -040059}
60
61Frame SkiaVulkanPipeline::getFrame() {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050062 LOG_ALWAYS_FATAL_IF(mVkSurface == nullptr, "getFrame() called on a context with no surface!");
Greg Danield2317a42021-04-08 11:39:02 -040063 return vulkanManager().dequeueNextBuffer(mVkSurface);
Stan Iliev500a0c32016-10-26 10:30:09 -040064}
65
Alec Mouri3afb3972022-05-27 22:03:11 +000066IRenderPipeline::DrawResult SkiaVulkanPipeline::draw(
67 const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
68 const LightGeometry& lightGeometry, LayerUpdateQueue* layerUpdateQueue,
69 const Rect& contentDrawBounds, bool opaque, const LightInfo& lightInfo,
70 const std::vector<sp<RenderNode>>& renderNodes, FrameInfoVisualizer* profiler) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050071 sk_sp<SkSurface> backBuffer = mVkSurface->getCurrentSkSurface();
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050072 if (backBuffer.get() == nullptr) {
Alec Mouri3afb3972022-05-27 22:03:11 +000073 return {false, -1};
Stan Iliev500a0c32016-10-26 10:30:09 -040074 }
Derek Sollenberger0784f242022-03-28 19:59:42 +000075
76 // update the coordinates of the global light position based on surface rotation
77 SkPoint lightCenter = mVkSurface->getCurrentPreTransform().mapXY(lightGeometry.center.x,
78 lightGeometry.center.y);
79 LightGeometry localGeometry = lightGeometry;
80 localGeometry.center.x = lightCenter.fX;
81 localGeometry.center.y = lightCenter.fY;
82
83 LightingInfo::updateLighting(localGeometry, lightInfo);
John Reck0fa0cbc2019-04-05 16:57:46 -070084 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, backBuffer,
85 mVkSurface->getCurrentPreTransform());
Matt Sarettcf2c05c2016-10-26 11:03:23 -040086
87 // Draw visual debugging features
John Reck1bcacfd2017-11-03 10:12:19 -070088 if (CC_UNLIKELY(Properties::showDirtyRegions ||
89 ProfileType::None != Properties::getProfileType())) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050090 SkCanvas* profileCanvas = backBuffer->getCanvas();
Matt Sarettcf2c05c2016-10-26 11:03:23 -040091 SkiaProfileRenderer profileRenderer(profileCanvas);
92 profiler->draw(profileRenderer);
Matt Sarettcf2c05c2016-10-26 11:03:23 -040093 }
94
Alec Mouri3afb3972022-05-27 22:03:11 +000095 nsecs_t submissionTime = IRenderPipeline::DrawResult::kUnknownTime;
Greg Danielbe2803a2021-02-19 18:32:16 -050096 {
97 ATRACE_NAME("flush commands");
Alec Mouri3afb3972022-05-27 22:03:11 +000098 submissionTime = vulkanManager().finishFrame(backBuffer.get());
Greg Danielbe2803a2021-02-19 18:32:16 -050099 }
100 layerUpdateQueue->clear();
101
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500102 // Log memory statistics
103 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
104 dumpResourceCacheUsage();
105 }
106
Alec Mouri3afb3972022-05-27 22:03:11 +0000107 return {true, submissionTime};
Stan Iliev500a0c32016-10-26 10:30:09 -0400108}
109
John Reck1bcacfd2017-11-03 10:12:19 -0700110bool SkiaVulkanPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
111 FrameInfo* currentFrameInfo, bool* requireSwap) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400112 *requireSwap = drew;
113
114 // Even if we decided to cancel the frame, from the perspective of jank
115 // metrics the frame was swapped at this point
116 currentFrameInfo->markSwapBuffers();
117
118 if (*requireSwap) {
Greg Danield2317a42021-04-08 11:39:02 -0400119 vulkanManager().swapBuffers(mVkSurface, screenDirty);
Stan Iliev500a0c32016-10-26 10:30:09 -0400120 }
121
Stan Iliev500a0c32016-10-26 10:30:09 -0400122 return *requireSwap;
123}
124
Stan Iliev500a0c32016-10-26 10:30:09 -0400125DeferredLayerUpdater* SkiaVulkanPipeline::createTextureLayer() {
Stan Iliev981afe72019-02-13 14:24:33 -0500126 mRenderThread.requireVkContext();
Greg Daniel8cd3edf2017-01-09 14:15:41 -0500127
Stan Iliev564ca3e2018-09-04 22:00:00 +0000128 return new DeferredLayerUpdater(mRenderThread.renderState());
Stan Iliev500a0c32016-10-26 10:30:09 -0400129}
130
John Reck1bcacfd2017-11-03 10:12:19 -0700131void SkiaVulkanPipeline::onStop() {}
Stan Iliev500a0c32016-10-26 10:30:09 -0400132
John Reck8ddbc592020-05-07 16:11:18 -0700133bool SkiaVulkanPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500134 if (mVkSurface) {
Greg Danield2317a42021-04-08 11:39:02 -0400135 vulkanManager().destroySurface(mVkSurface);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500136 mVkSurface = nullptr;
Stan Iliev500a0c32016-10-26 10:30:09 -0400137 }
138
139 if (surface) {
Stan Iliev981afe72019-02-13 14:24:33 -0500140 mRenderThread.requireVkContext();
John Reck0fa0cbc2019-04-05 16:57:46 -0700141 mVkSurface =
Greg Danield2317a42021-04-08 11:39:02 -0400142 vulkanManager().createSurface(surface, mColorMode, mSurfaceColorSpace,
143 mSurfaceColorType, mRenderThread.getGrContext(), 0);
Greg Daniel031b81b2018-10-02 14:47:22 -0400144 }
145
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500146 return mVkSurface != nullptr;
Stan Iliev500a0c32016-10-26 10:30:09 -0400147}
148
149bool SkiaVulkanPipeline::isSurfaceReady() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500150 return CC_UNLIKELY(mVkSurface != nullptr);
Stan Iliev500a0c32016-10-26 10:30:09 -0400151}
152
153bool SkiaVulkanPipeline::isContextReady() {
Greg Danield2317a42021-04-08 11:39:02 -0400154 return CC_LIKELY(vulkanManager().hasVkContext());
Stan Iliev500a0c32016-10-26 10:30:09 -0400155}
156
157void SkiaVulkanPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
Chris Blume5f1ac2b2018-11-05 16:10:39 -0800158 VkInteropFunctorDrawable::vkInvokeFunctor(functor);
Stan Iliev500a0c32016-10-26 10:30:09 -0400159}
160
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400161sk_sp<Bitmap> SkiaVulkanPipeline::allocateHardwareBitmap(renderthread::RenderThread& renderThread,
John Reck1bcacfd2017-11-03 10:12:19 -0700162 SkBitmap& skBitmap) {
Derek Sollenberger6e35e632019-01-22 13:56:25 -0500163 LOG_ALWAYS_FATAL("Unimplemented");
164 return nullptr;
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400165}
166
Stan Iliev90276c82019-02-03 18:01:02 -0500167void SkiaVulkanPipeline::onContextDestroyed() {
168 if (mVkSurface) {
Greg Danield2317a42021-04-08 11:39:02 -0400169 vulkanManager().destroySurface(mVkSurface);
Stan Iliev90276c82019-02-03 18:01:02 -0500170 mVkSurface = nullptr;
171 }
172}
173
Stan Iliev500a0c32016-10-26 10:30:09 -0400174} /* namespace skiapipeline */
175} /* namespace uirenderer */
176} /* namespace android */