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