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