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 | |
| 17 | #include "SkiaVulkanPipeline.h" |
| 18 | |
rnlee | ce9762b | 2021-05-21 15:40:53 -0700 | [diff] [blame] | 19 | #include <gui/TraceUtils.h> |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 20 | #include "DeferredLayerUpdater.h" |
Greg Daniel | be2803a | 2021-02-19 18:32:16 -0500 | [diff] [blame] | 21 | #include "LightingInfo.h" |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 22 | #include "Readback.h" |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 23 | #include "ShaderCache.h" |
Matt Sarett | cf2c05c | 2016-10-26 11:03:23 -0400 | [diff] [blame] | 24 | #include "SkiaPipeline.h" |
| 25 | #include "SkiaProfileRenderer.h" |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 26 | #include "VkInteropFunctorDrawable.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 27 | #include "renderstate/RenderState.h" |
| 28 | #include "renderthread/Frame.h" |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 29 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 30 | #include <SkSurface.h> |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 31 | #include <SkTypes.h> |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 32 | |
Adlai Holler | 72cff42 | 2020-10-15 09:32:00 -0400 | [diff] [blame] | 33 | #include <GrDirectContext.h> |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 34 | #include <GrTypes.h> |
| 35 | #include <vk/GrVkTypes.h> |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 36 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 37 | #include <cutils/properties.h> |
| 38 | #include <strings.h> |
| 39 | |
| 40 | using namespace android::uirenderer::renderthread; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 41 | |
| 42 | namespace android { |
| 43 | namespace uirenderer { |
| 44 | namespace skiapipeline { |
| 45 | |
Greg Daniel | d2317a4 | 2021-04-08 11:39:02 -0400 | [diff] [blame] | 46 | SkiaVulkanPipeline::SkiaVulkanPipeline(renderthread::RenderThread& thread) : SkiaPipeline(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 | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 59 | return MakeCurrentResult::AlreadyCurrent; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | Frame SkiaVulkanPipeline::getFrame() { |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 63 | LOG_ALWAYS_FATAL_IF(mVkSurface == nullptr, "getFrame() called on a context with no surface!"); |
Greg Daniel | d2317a4 | 2021-04-08 11:39:02 -0400 | [diff] [blame] | 64 | return vulkanManager().dequeueNextBuffer(mVkSurface); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 65 | } |
| 66 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 67 | bool SkiaVulkanPipeline::draw(const Frame& frame, const SkRect& screenDirty, const SkRect& dirty, |
John Reck | d9d7f12 | 2018-05-03 14:40:56 -0700 | [diff] [blame] | 68 | const LightGeometry& lightGeometry, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 69 | LayerUpdateQueue* layerUpdateQueue, const Rect& contentDrawBounds, |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 70 | bool opaque, const LightInfo& lightInfo, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 71 | const std::vector<sp<RenderNode>>& renderNodes, |
| 72 | FrameInfoVisualizer* profiler) { |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 73 | sk_sp<SkSurface> backBuffer = mVkSurface->getCurrentSkSurface(); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 74 | if (backBuffer.get() == nullptr) { |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 75 | return false; |
| 76 | } |
Derek Sollenberger | 0784f24 | 2022-03-28 19:59:42 +0000 | [diff] [blame] | 77 | |
| 78 | // update the coordinates of the global light position based on surface rotation |
| 79 | SkPoint lightCenter = mVkSurface->getCurrentPreTransform().mapXY(lightGeometry.center.x, |
| 80 | lightGeometry.center.y); |
| 81 | LightGeometry localGeometry = lightGeometry; |
| 82 | localGeometry.center.x = lightCenter.fX; |
| 83 | localGeometry.center.y = lightCenter.fY; |
| 84 | |
| 85 | LightingInfo::updateLighting(localGeometry, lightInfo); |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 86 | renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, backBuffer, |
| 87 | mVkSurface->getCurrentPreTransform()); |
Matt Sarett | cf2c05c | 2016-10-26 11:03:23 -0400 | [diff] [blame] | 88 | |
| 89 | // Draw visual debugging features |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 90 | if (CC_UNLIKELY(Properties::showDirtyRegions || |
| 91 | ProfileType::None != Properties::getProfileType())) { |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 92 | SkCanvas* profileCanvas = backBuffer->getCanvas(); |
Matt Sarett | cf2c05c | 2016-10-26 11:03:23 -0400 | [diff] [blame] | 93 | SkiaProfileRenderer profileRenderer(profileCanvas); |
| 94 | profiler->draw(profileRenderer); |
Matt Sarett | cf2c05c | 2016-10-26 11:03:23 -0400 | [diff] [blame] | 95 | } |
| 96 | |
Greg Daniel | be2803a | 2021-02-19 18:32:16 -0500 | [diff] [blame] | 97 | { |
| 98 | ATRACE_NAME("flush commands"); |
Greg Daniel | d2317a4 | 2021-04-08 11:39:02 -0400 | [diff] [blame] | 99 | vulkanManager().finishFrame(backBuffer.get()); |
Greg Daniel | be2803a | 2021-02-19 18:32:16 -0500 | [diff] [blame] | 100 | } |
| 101 | layerUpdateQueue->clear(); |
| 102 | |
Matt Sarett | 4bda6bf | 2016-11-07 15:43:41 -0500 | [diff] [blame] | 103 | // Log memory statistics |
| 104 | if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) { |
| 105 | dumpResourceCacheUsage(); |
| 106 | } |
| 107 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 108 | return true; |
| 109 | } |
| 110 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 111 | bool SkiaVulkanPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty, |
| 112 | FrameInfo* currentFrameInfo, bool* requireSwap) { |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 113 | *requireSwap = drew; |
| 114 | |
| 115 | // Even if we decided to cancel the frame, from the perspective of jank |
| 116 | // metrics the frame was swapped at this point |
| 117 | currentFrameInfo->markSwapBuffers(); |
| 118 | |
| 119 | if (*requireSwap) { |
Greg Daniel | d2317a4 | 2021-04-08 11:39:02 -0400 | [diff] [blame] | 120 | vulkanManager().swapBuffers(mVkSurface, screenDirty); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 121 | } |
| 122 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 123 | return *requireSwap; |
| 124 | } |
| 125 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 126 | DeferredLayerUpdater* SkiaVulkanPipeline::createTextureLayer() { |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 127 | mRenderThread.requireVkContext(); |
Greg Daniel | 8cd3edf | 2017-01-09 14:15:41 -0500 | [diff] [blame] | 128 | |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 129 | return new DeferredLayerUpdater(mRenderThread.renderState()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 130 | } |
| 131 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 132 | void SkiaVulkanPipeline::onStop() {} |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 133 | |
John Reck | 8ddbc59 | 2020-05-07 16:11:18 -0700 | [diff] [blame] | 134 | bool SkiaVulkanPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior) { |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 135 | if (mVkSurface) { |
Greg Daniel | d2317a4 | 2021-04-08 11:39:02 -0400 | [diff] [blame] | 136 | vulkanManager().destroySurface(mVkSurface); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 137 | mVkSurface = nullptr; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | if (surface) { |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 141 | mRenderThread.requireVkContext(); |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 142 | mVkSurface = |
Greg Daniel | d2317a4 | 2021-04-08 11:39:02 -0400 | [diff] [blame] | 143 | vulkanManager().createSurface(surface, mColorMode, mSurfaceColorSpace, |
| 144 | mSurfaceColorType, mRenderThread.getGrContext(), 0); |
Greg Daniel | 031b81b | 2018-10-02 14:47:22 -0400 | [diff] [blame] | 145 | } |
| 146 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 147 | return mVkSurface != nullptr; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | bool SkiaVulkanPipeline::isSurfaceReady() { |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 151 | return CC_UNLIKELY(mVkSurface != nullptr); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | bool SkiaVulkanPipeline::isContextReady() { |
Greg Daniel | d2317a4 | 2021-04-08 11:39:02 -0400 | [diff] [blame] | 155 | return CC_LIKELY(vulkanManager().hasVkContext()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | void SkiaVulkanPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) { |
Chris Blume | 5f1ac2b | 2018-11-05 16:10:39 -0800 | [diff] [blame] | 159 | VkInteropFunctorDrawable::vkInvokeFunctor(functor); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 160 | } |
| 161 | |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 162 | sk_sp<Bitmap> SkiaVulkanPipeline::allocateHardwareBitmap(renderthread::RenderThread& renderThread, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 163 | SkBitmap& skBitmap) { |
Derek Sollenberger | 6e35e63 | 2019-01-22 13:56:25 -0500 | [diff] [blame] | 164 | LOG_ALWAYS_FATAL("Unimplemented"); |
| 165 | return nullptr; |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 166 | } |
| 167 | |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 168 | void SkiaVulkanPipeline::onContextDestroyed() { |
| 169 | if (mVkSurface) { |
Greg Daniel | d2317a4 | 2021-04-08 11:39:02 -0400 | [diff] [blame] | 170 | vulkanManager().destroySurface(mVkSurface); |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 171 | mVkSurface = nullptr; |
| 172 | } |
| 173 | } |
| 174 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 175 | } /* namespace skiapipeline */ |
| 176 | } /* namespace uirenderer */ |
| 177 | } /* namespace android */ |