blob: fd0a8e06f39c872874ad1414a5d925d75547495b [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 Sollenbergerb8307722022-08-30 14:24:22 -040058 // In case the surface was destroyed (e.g. a previous trimMemory call) we
59 // need to recreate it here.
Nader Jawada3521852023-01-30 20:23:46 -080060 if (mHardwareBuffer) {
61 mRenderThread.requireVkContext();
62 } else if (!isSurfaceReady() && mNativeWindow) {
Derek Sollenbergerb8307722022-08-30 14:24:22 -040063 setSurface(mNativeWindow.get(), SwapBehavior::kSwap_default);
64 }
65 return isContextReady() ? MakeCurrentResult::AlreadyCurrent : MakeCurrentResult::Failed;
Stan Iliev500a0c32016-10-26 10:30:09 -040066}
67
68Frame SkiaVulkanPipeline::getFrame() {
Nader Jawad24617222023-03-30 12:29:01 -070069 LOG_ALWAYS_FATAL_IF(mVkSurface == nullptr, "getFrame() called on a context with no surface!");
70 return vulkanManager().dequeueNextBuffer(mVkSurface);
Stan Iliev500a0c32016-10-26 10:30:09 -040071}
72
Alec Mouri3afb3972022-05-27 22:03:11 +000073IRenderPipeline::DrawResult SkiaVulkanPipeline::draw(
74 const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
75 const LightGeometry& lightGeometry, LayerUpdateQueue* layerUpdateQueue,
76 const Rect& contentDrawBounds, bool opaque, const LightInfo& lightInfo,
Nader Jawada3521852023-01-30 20:23:46 -080077 const std::vector<sp<RenderNode>>& renderNodes, FrameInfoVisualizer* profiler,
Jiang Tiane0edc3a2024-01-09 11:52:05 +080078 const HardwareBufferRenderParams& bufferParams, std::mutex& profilerLock) {
Nader Jawada3521852023-01-30 20:23:46 -080079 sk_sp<SkSurface> backBuffer;
80 SkMatrix preTransform;
81 if (mHardwareBuffer) {
82 backBuffer = getBufferSkSurface(bufferParams);
83 preTransform = bufferParams.getTransform();
84 } else {
85 backBuffer = mVkSurface->getCurrentSkSurface();
86 preTransform = mVkSurface->getCurrentPreTransform();
87 }
88
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050089 if (backBuffer.get() == nullptr) {
John Reck5d3fac12023-11-08 23:08:10 -050090 return {false, -1, android::base::unique_fd{}};
Stan Iliev500a0c32016-10-26 10:30:09 -040091 }
Derek Sollenberger0784f242022-03-28 19:59:42 +000092
93 // update the coordinates of the global light position based on surface rotation
Nader Jawada3521852023-01-30 20:23:46 -080094 SkPoint lightCenter = preTransform.mapXY(lightGeometry.center.x, lightGeometry.center.y);
Derek Sollenberger0784f242022-03-28 19:59:42 +000095 LightGeometry localGeometry = lightGeometry;
96 localGeometry.center.x = lightCenter.fX;
97 localGeometry.center.y = lightCenter.fY;
98
99 LightingInfo::updateLighting(localGeometry, lightInfo);
John Reck0fa0cbc2019-04-05 16:57:46 -0700100 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, backBuffer,
Nader Jawada3521852023-01-30 20:23:46 -0800101 preTransform);
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400102
103 // Draw visual debugging features
John Reck1bcacfd2017-11-03 10:12:19 -0700104 if (CC_UNLIKELY(Properties::showDirtyRegions ||
105 ProfileType::None != Properties::getProfileType())) {
Jiang Tiane0edc3a2024-01-09 11:52:05 +0800106 std::scoped_lock lock(profilerLock);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500107 SkCanvas* profileCanvas = backBuffer->getCanvas();
John Reckc30836d2022-08-12 14:28:31 -0400108 SkAutoCanvasRestore saver(profileCanvas, true);
Nader Jawad5970c3e2023-11-29 12:42:39 -0800109 profileCanvas->concat(preTransform);
John Reckc30836d2022-08-12 14:28:31 -0400110 SkiaProfileRenderer profileRenderer(profileCanvas, frame.width(), frame.height());
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400111 profiler->draw(profileRenderer);
Matt Sarettcf2c05c2016-10-26 11:03:23 -0400112 }
113
John Reck5d3fac12023-11-08 23:08:10 -0500114 VulkanManager::VkDrawResult drawResult;
Greg Danielbe2803a2021-02-19 18:32:16 -0500115 {
116 ATRACE_NAME("flush commands");
John Reck5d3fac12023-11-08 23:08:10 -0500117 drawResult = vulkanManager().finishFrame(backBuffer.get());
Greg Danielbe2803a2021-02-19 18:32:16 -0500118 }
119 layerUpdateQueue->clear();
120
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500121 // Log memory statistics
122 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
123 dumpResourceCacheUsage();
124 }
125
John Reck5d3fac12023-11-08 23:08:10 -0500126 return {true, drawResult.submissionTime, std::move(drawResult.presentFence)};
Stan Iliev500a0c32016-10-26 10:30:09 -0400127}
128
John Reck5d3fac12023-11-08 23:08:10 -0500129bool SkiaVulkanPipeline::swapBuffers(const Frame& frame, IRenderPipeline::DrawResult& drawResult,
130 const SkRect& screenDirty, FrameInfo* currentFrameInfo,
131 bool* requireSwap) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400132 // Even if we decided to cancel the frame, from the perspective of jank
133 // metrics the frame was swapped at this point
134 currentFrameInfo->markSwapBuffers();
135
Nader Jawada3521852023-01-30 20:23:46 -0800136 if (mHardwareBuffer) {
137 return false;
138 }
139
John Reck5d3fac12023-11-08 23:08:10 -0500140 *requireSwap = drawResult.success;
Nader Jawada3521852023-01-30 20:23:46 -0800141
Stan Iliev500a0c32016-10-26 10:30:09 -0400142 if (*requireSwap) {
John Reck5d3fac12023-11-08 23:08:10 -0500143 vulkanManager().swapBuffers(mVkSurface, screenDirty, std::move(drawResult.presentFence));
Stan Iliev500a0c32016-10-26 10:30:09 -0400144 }
145
Stan Iliev500a0c32016-10-26 10:30:09 -0400146 return *requireSwap;
147}
148
Stan Iliev500a0c32016-10-26 10:30:09 -0400149DeferredLayerUpdater* SkiaVulkanPipeline::createTextureLayer() {
Stan Iliev981afe72019-02-13 14:24:33 -0500150 mRenderThread.requireVkContext();
Greg Daniel8cd3edf2017-01-09 14:15:41 -0500151
Stan Iliev564ca3e2018-09-04 22:00:00 +0000152 return new DeferredLayerUpdater(mRenderThread.renderState());
Stan Iliev500a0c32016-10-26 10:30:09 -0400153}
154
John Reck1bcacfd2017-11-03 10:12:19 -0700155void SkiaVulkanPipeline::onStop() {}
Stan Iliev500a0c32016-10-26 10:30:09 -0400156
Nader Jawada3521852023-01-30 20:23:46 -0800157[[nodiscard]] android::base::unique_fd SkiaVulkanPipeline::flush() {
158 int fence = -1;
159 vulkanManager().createReleaseFence(&fence, mRenderThread.getGrContext());
160 return android::base::unique_fd(fence);
161}
162
Derek Sollenbergerb8307722022-08-30 14:24:22 -0400163// We can safely ignore the swap behavior because VkManager will always operate
164// in a mode equivalent to EGLManager::SwapBehavior::kBufferAge
165bool SkiaVulkanPipeline::setSurface(ANativeWindow* surface, SwapBehavior /*swapBehavior*/) {
166 mNativeWindow = surface;
167
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500168 if (mVkSurface) {
Greg Danield2317a42021-04-08 11:39:02 -0400169 vulkanManager().destroySurface(mVkSurface);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500170 mVkSurface = nullptr;
Stan Iliev500a0c32016-10-26 10:30:09 -0400171 }
172
173 if (surface) {
Stan Iliev981afe72019-02-13 14:24:33 -0500174 mRenderThread.requireVkContext();
John Reck0fa0cbc2019-04-05 16:57:46 -0700175 mVkSurface =
Greg Danield2317a42021-04-08 11:39:02 -0400176 vulkanManager().createSurface(surface, mColorMode, mSurfaceColorSpace,
177 mSurfaceColorType, mRenderThread.getGrContext(), 0);
Greg Daniel031b81b2018-10-02 14:47:22 -0400178 }
179
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500180 return mVkSurface != nullptr;
Stan Iliev500a0c32016-10-26 10:30:09 -0400181}
182
John Reck55887762023-01-25 16:51:18 -0500183void SkiaVulkanPipeline::setTargetSdrHdrRatio(float ratio) {
184 SkiaPipeline::setTargetSdrHdrRatio(ratio);
185 if (mVkSurface) {
186 mVkSurface->setColorSpace(mSurfaceColorSpace);
187 }
188}
189
Stan Iliev500a0c32016-10-26 10:30:09 -0400190bool SkiaVulkanPipeline::isSurfaceReady() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500191 return CC_UNLIKELY(mVkSurface != nullptr);
Stan Iliev500a0c32016-10-26 10:30:09 -0400192}
193
194bool SkiaVulkanPipeline::isContextReady() {
Greg Danield2317a42021-04-08 11:39:02 -0400195 return CC_LIKELY(vulkanManager().hasVkContext());
Stan Iliev500a0c32016-10-26 10:30:09 -0400196}
197
198void SkiaVulkanPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
Chris Blume5f1ac2b2018-11-05 16:10:39 -0800199 VkInteropFunctorDrawable::vkInvokeFunctor(functor);
Stan Iliev500a0c32016-10-26 10:30:09 -0400200}
201
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400202sk_sp<Bitmap> SkiaVulkanPipeline::allocateHardwareBitmap(renderthread::RenderThread& renderThread,
John Reck1bcacfd2017-11-03 10:12:19 -0700203 SkBitmap& skBitmap) {
Derek Sollenberger6e35e632019-01-22 13:56:25 -0500204 LOG_ALWAYS_FATAL("Unimplemented");
205 return nullptr;
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400206}
207
Stan Iliev90276c82019-02-03 18:01:02 -0500208void SkiaVulkanPipeline::onContextDestroyed() {
209 if (mVkSurface) {
Greg Danield2317a42021-04-08 11:39:02 -0400210 vulkanManager().destroySurface(mVkSurface);
Stan Iliev90276c82019-02-03 18:01:02 -0500211 mVkSurface = nullptr;
212 }
213}
214
John Reckee184272023-03-21 12:29:21 -0400215const SkM44& SkiaVulkanPipeline::getPixelSnapMatrix() const {
216 return mVkSurface->getPixelSnapMatrix();
217}
218
Stan Iliev500a0c32016-10-26 10:30:09 -0400219} /* namespace skiapipeline */
220} /* namespace uirenderer */
221} /* namespace android */