Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
Jerome Gaillard | bea67ce | 2024-03-28 14:21:16 +0000 | [diff] [blame] | 17 | #include "pipeline/skia/SkiaVulkanPipeline.h" |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 18 | |
Alec Mouri | 3afb397 | 2022-05-27 22:03:11 +0000 | [diff] [blame] | 19 | #include <SkSurface.h> |
| 20 | #include <SkTypes.h> |
| 21 | #include <cutils/properties.h> |
rnlee | ce9762b | 2021-05-21 15:40:53 -0700 | [diff] [blame] | 22 | #include <gui/TraceUtils.h> |
Nolan Scobie | 572d801 | 2024-08-30 13:43:34 -0400 | [diff] [blame] | 23 | #include <include/gpu/ganesh/GrDirectContext.h> |
| 24 | #include <include/gpu/ganesh/GrTypes.h> |
| 25 | #include <include/gpu/ganesh/vk/GrVkTypes.h> |
Alec Mouri | 3afb397 | 2022-05-27 22:03:11 +0000 | [diff] [blame] | 26 | #include <strings.h> |
Alec Mouri | 3afb397 | 2022-05-27 22:03:11 +0000 | [diff] [blame] | 27 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 28 | #include "DeferredLayerUpdater.h" |
Greg Daniel | be2803a | 2021-02-19 18:32:16 -0500 | [diff] [blame] | 29 | #include "LightingInfo.h" |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 30 | #include "Readback.h" |
Jerome Gaillard | bea67ce | 2024-03-28 14:21:16 +0000 | [diff] [blame] | 31 | #include "pipeline/skia/ShaderCache.h" |
| 32 | #include "pipeline/skia/SkiaGpuPipeline.h" |
| 33 | #include "pipeline/skia/SkiaProfileRenderer.h" |
| 34 | #include "pipeline/skia/VkInteropFunctorDrawable.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 35 | #include "renderstate/RenderState.h" |
| 36 | #include "renderthread/Frame.h" |
Alec Mouri | 3afb397 | 2022-05-27 22:03:11 +0000 | [diff] [blame] | 37 | #include "renderthread/IRenderPipeline.h" |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 38 | |
| 39 | using namespace android::uirenderer::renderthread; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 40 | |
| 41 | namespace android { |
| 42 | namespace uirenderer { |
| 43 | namespace skiapipeline { |
| 44 | |
Jerome Gaillard | bea67ce | 2024-03-28 14:21:16 +0000 | [diff] [blame] | 45 | SkiaVulkanPipeline::SkiaVulkanPipeline(renderthread::RenderThread& thread) |
| 46 | : SkiaGpuPipeline(thread) { |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 47 | thread.renderState().registerContextCallback(this); |
| 48 | } |
| 49 | |
| 50 | SkiaVulkanPipeline::~SkiaVulkanPipeline() { |
| 51 | mRenderThread.renderState().removeContextCallback(this); |
| 52 | } |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 53 | |
Greg Daniel | d2317a4 | 2021-04-08 11:39:02 -0400 | [diff] [blame] | 54 | VulkanManager& SkiaVulkanPipeline::vulkanManager() { |
| 55 | return mRenderThread.vulkanManager(); |
| 56 | } |
| 57 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 58 | MakeCurrentResult SkiaVulkanPipeline::makeCurrent() { |
Derek Sollenberger | b830772 | 2022-08-30 14:24:22 -0400 | [diff] [blame] | 59 | // In case the surface was destroyed (e.g. a previous trimMemory call) we |
| 60 | // need to recreate it here. |
Nader Jawad | a352185 | 2023-01-30 20:23:46 -0800 | [diff] [blame] | 61 | if (mHardwareBuffer) { |
| 62 | mRenderThread.requireVkContext(); |
| 63 | } else if (!isSurfaceReady() && mNativeWindow) { |
Derek Sollenberger | b830772 | 2022-08-30 14:24:22 -0400 | [diff] [blame] | 64 | setSurface(mNativeWindow.get(), SwapBehavior::kSwap_default); |
| 65 | } |
| 66 | return isContextReady() ? MakeCurrentResult::AlreadyCurrent : MakeCurrentResult::Failed; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | Frame SkiaVulkanPipeline::getFrame() { |
Nader Jawad | 2461722 | 2023-03-30 12:29:01 -0700 | [diff] [blame] | 70 | LOG_ALWAYS_FATAL_IF(mVkSurface == nullptr, "getFrame() called on a context with no surface!"); |
| 71 | return vulkanManager().dequeueNextBuffer(mVkSurface); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 72 | } |
| 73 | |
Alec Mouri | 3afb397 | 2022-05-27 22:03:11 +0000 | [diff] [blame] | 74 | IRenderPipeline::DrawResult SkiaVulkanPipeline::draw( |
| 75 | const Frame& frame, const SkRect& screenDirty, const SkRect& dirty, |
| 76 | const LightGeometry& lightGeometry, LayerUpdateQueue* layerUpdateQueue, |
| 77 | const Rect& contentDrawBounds, bool opaque, const LightInfo& lightInfo, |
Nader Jawad | a352185 | 2023-01-30 20:23:46 -0800 | [diff] [blame] | 78 | const std::vector<sp<RenderNode>>& renderNodes, FrameInfoVisualizer* profiler, |
Jiang Tian | e0edc3a | 2024-01-09 11:52:05 +0800 | [diff] [blame] | 79 | const HardwareBufferRenderParams& bufferParams, std::mutex& profilerLock) { |
Nader Jawad | a352185 | 2023-01-30 20:23:46 -0800 | [diff] [blame] | 80 | sk_sp<SkSurface> backBuffer; |
| 81 | SkMatrix preTransform; |
| 82 | if (mHardwareBuffer) { |
| 83 | backBuffer = getBufferSkSurface(bufferParams); |
| 84 | preTransform = bufferParams.getTransform(); |
| 85 | } else { |
| 86 | backBuffer = mVkSurface->getCurrentSkSurface(); |
| 87 | preTransform = mVkSurface->getCurrentPreTransform(); |
| 88 | } |
| 89 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 90 | if (backBuffer.get() == nullptr) { |
John Reck | 5d3fac1 | 2023-11-08 23:08:10 -0500 | [diff] [blame] | 91 | return {false, -1, android::base::unique_fd{}}; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 92 | } |
Derek Sollenberger | 0784f240 | 2022-03-28 19:59:42 +0000 | [diff] [blame] | 93 | |
| 94 | // update the coordinates of the global light position based on surface rotation |
Nader Jawad | a352185 | 2023-01-30 20:23:46 -0800 | [diff] [blame] | 95 | SkPoint lightCenter = preTransform.mapXY(lightGeometry.center.x, lightGeometry.center.y); |
Derek Sollenberger | 0784f240 | 2022-03-28 19:59:42 +0000 | [diff] [blame] | 96 | LightGeometry localGeometry = lightGeometry; |
| 97 | localGeometry.center.x = lightCenter.fX; |
| 98 | localGeometry.center.y = lightCenter.fY; |
| 99 | |
| 100 | LightingInfo::updateLighting(localGeometry, lightInfo); |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 101 | renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, backBuffer, |
Nader Jawad | a352185 | 2023-01-30 20:23:46 -0800 | [diff] [blame] | 102 | preTransform); |
Matt Sarett | cf2c05c | 2016-10-26 11:03:23 -0400 | [diff] [blame] | 103 | |
| 104 | // Draw visual debugging features |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 105 | if (CC_UNLIKELY(Properties::showDirtyRegions || |
| 106 | ProfileType::None != Properties::getProfileType())) { |
Jiang Tian | e0edc3a | 2024-01-09 11:52:05 +0800 | [diff] [blame] | 107 | std::scoped_lock lock(profilerLock); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 108 | SkCanvas* profileCanvas = backBuffer->getCanvas(); |
John Reck | c30836d | 2022-08-12 14:28:31 -0400 | [diff] [blame] | 109 | SkAutoCanvasRestore saver(profileCanvas, true); |
Nader Jawad | 5970c3e | 2023-11-29 12:42:39 -0800 | [diff] [blame] | 110 | profileCanvas->concat(preTransform); |
John Reck | c30836d | 2022-08-12 14:28:31 -0400 | [diff] [blame] | 111 | SkiaProfileRenderer profileRenderer(profileCanvas, frame.width(), frame.height()); |
Matt Sarett | cf2c05c | 2016-10-26 11:03:23 -0400 | [diff] [blame] | 112 | profiler->draw(profileRenderer); |
Matt Sarett | cf2c05c | 2016-10-26 11:03:23 -0400 | [diff] [blame] | 113 | } |
| 114 | |
John Reck | 5d3fac1 | 2023-11-08 23:08:10 -0500 | [diff] [blame] | 115 | VulkanManager::VkDrawResult drawResult; |
Greg Daniel | be2803a | 2021-02-19 18:32:16 -0500 | [diff] [blame] | 116 | { |
| 117 | ATRACE_NAME("flush commands"); |
John Reck | 5d3fac1 | 2023-11-08 23:08:10 -0500 | [diff] [blame] | 118 | drawResult = vulkanManager().finishFrame(backBuffer.get()); |
Greg Daniel | be2803a | 2021-02-19 18:32:16 -0500 | [diff] [blame] | 119 | } |
| 120 | layerUpdateQueue->clear(); |
| 121 | |
Matt Sarett | 4bda6bf | 2016-11-07 15:43:41 -0500 | [diff] [blame] | 122 | // Log memory statistics |
| 123 | if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) { |
| 124 | dumpResourceCacheUsage(); |
| 125 | } |
| 126 | |
John Reck | 5d3fac1 | 2023-11-08 23:08:10 -0500 | [diff] [blame] | 127 | return {true, drawResult.submissionTime, std::move(drawResult.presentFence)}; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 128 | } |
| 129 | |
John Reck | 5d3fac1 | 2023-11-08 23:08:10 -0500 | [diff] [blame] | 130 | bool SkiaVulkanPipeline::swapBuffers(const Frame& frame, IRenderPipeline::DrawResult& drawResult, |
| 131 | const SkRect& screenDirty, FrameInfo* currentFrameInfo, |
| 132 | bool* requireSwap) { |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 133 | // Even if we decided to cancel the frame, from the perspective of jank |
| 134 | // metrics the frame was swapped at this point |
| 135 | currentFrameInfo->markSwapBuffers(); |
| 136 | |
Nader Jawad | a352185 | 2023-01-30 20:23:46 -0800 | [diff] [blame] | 137 | if (mHardwareBuffer) { |
| 138 | return false; |
| 139 | } |
| 140 | |
John Reck | 5d3fac1 | 2023-11-08 23:08:10 -0500 | [diff] [blame] | 141 | *requireSwap = drawResult.success; |
Nader Jawad | a352185 | 2023-01-30 20:23:46 -0800 | [diff] [blame] | 142 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 143 | if (*requireSwap) { |
John Reck | 5d3fac1 | 2023-11-08 23:08:10 -0500 | [diff] [blame] | 144 | vulkanManager().swapBuffers(mVkSurface, screenDirty, std::move(drawResult.presentFence)); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 145 | } |
| 146 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 147 | return *requireSwap; |
| 148 | } |
| 149 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 150 | DeferredLayerUpdater* SkiaVulkanPipeline::createTextureLayer() { |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 151 | mRenderThread.requireVkContext(); |
Greg Daniel | 8cd3edf | 2017-01-09 14:15:41 -0500 | [diff] [blame] | 152 | |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 153 | return new DeferredLayerUpdater(mRenderThread.renderState()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 154 | } |
| 155 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 156 | void SkiaVulkanPipeline::onStop() {} |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 157 | |
Nader Jawad | a352185 | 2023-01-30 20:23:46 -0800 | [diff] [blame] | 158 | [[nodiscard]] android::base::unique_fd SkiaVulkanPipeline::flush() { |
| 159 | int fence = -1; |
| 160 | vulkanManager().createReleaseFence(&fence, mRenderThread.getGrContext()); |
| 161 | return android::base::unique_fd(fence); |
| 162 | } |
| 163 | |
Derek Sollenberger | b830772 | 2022-08-30 14:24:22 -0400 | [diff] [blame] | 164 | // We can safely ignore the swap behavior because VkManager will always operate |
| 165 | // in a mode equivalent to EGLManager::SwapBehavior::kBufferAge |
| 166 | bool SkiaVulkanPipeline::setSurface(ANativeWindow* surface, SwapBehavior /*swapBehavior*/) { |
| 167 | mNativeWindow = surface; |
| 168 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 169 | if (mVkSurface) { |
Greg Daniel | d2317a4 | 2021-04-08 11:39:02 -0400 | [diff] [blame] | 170 | vulkanManager().destroySurface(mVkSurface); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 171 | mVkSurface = nullptr; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | if (surface) { |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 175 | mRenderThread.requireVkContext(); |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 176 | mVkSurface = |
Greg Daniel | d2317a4 | 2021-04-08 11:39:02 -0400 | [diff] [blame] | 177 | vulkanManager().createSurface(surface, mColorMode, mSurfaceColorSpace, |
| 178 | mSurfaceColorType, mRenderThread.getGrContext(), 0); |
Greg Daniel | 031b81b | 2018-10-02 14:47:22 -0400 | [diff] [blame] | 179 | } |
| 180 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 181 | return mVkSurface != nullptr; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 182 | } |
| 183 | |
John Reck | 5588776 | 2023-01-25 16:51:18 -0500 | [diff] [blame] | 184 | void SkiaVulkanPipeline::setTargetSdrHdrRatio(float ratio) { |
| 185 | SkiaPipeline::setTargetSdrHdrRatio(ratio); |
| 186 | if (mVkSurface) { |
| 187 | mVkSurface->setColorSpace(mSurfaceColorSpace); |
| 188 | } |
| 189 | } |
| 190 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 191 | bool SkiaVulkanPipeline::isSurfaceReady() { |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 192 | return CC_UNLIKELY(mVkSurface != nullptr); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | bool SkiaVulkanPipeline::isContextReady() { |
Greg Daniel | d2317a4 | 2021-04-08 11:39:02 -0400 | [diff] [blame] | 196 | return CC_LIKELY(vulkanManager().hasVkContext()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void SkiaVulkanPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) { |
Chris Blume | 5f1ac2b | 2018-11-05 16:10:39 -0800 | [diff] [blame] | 200 | VkInteropFunctorDrawable::vkInvokeFunctor(functor); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 201 | } |
| 202 | |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 203 | sk_sp<Bitmap> SkiaVulkanPipeline::allocateHardwareBitmap(renderthread::RenderThread& renderThread, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 204 | SkBitmap& skBitmap) { |
Derek Sollenberger | 6e35e63 | 2019-01-22 13:56:25 -0500 | [diff] [blame] | 205 | LOG_ALWAYS_FATAL("Unimplemented"); |
| 206 | return nullptr; |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 207 | } |
| 208 | |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 209 | void SkiaVulkanPipeline::onContextDestroyed() { |
| 210 | if (mVkSurface) { |
Greg Daniel | d2317a4 | 2021-04-08 11:39:02 -0400 | [diff] [blame] | 211 | vulkanManager().destroySurface(mVkSurface); |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 212 | mVkSurface = nullptr; |
| 213 | } |
| 214 | } |
| 215 | |
John Reck | ee18427 | 2023-03-21 12:29:21 -0400 | [diff] [blame] | 216 | const SkM44& SkiaVulkanPipeline::getPixelSnapMatrix() const { |
| 217 | return mVkSurface->getPixelSnapMatrix(); |
| 218 | } |
| 219 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 220 | } /* namespace skiapipeline */ |
| 221 | } /* namespace uirenderer */ |
| 222 | } /* namespace android */ |