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" |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 20 | #include "Readback.h" |
Matt Sarett | cf2c05c | 2016-10-26 11:03:23 -0400 | [diff] [blame] | 21 | #include "SkiaPipeline.h" |
| 22 | #include "SkiaProfileRenderer.h" |
Greg Daniel | 8cd3edf | 2017-01-09 14:15:41 -0500 | [diff] [blame] | 23 | #include "VkLayer.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 24 | #include "renderstate/RenderState.h" |
| 25 | #include "renderthread/Frame.h" |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 26 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 27 | #include <SkSurface.h> |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 28 | #include <SkTypes.h> |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 29 | |
| 30 | #include <GrContext.h> |
| 31 | #include <GrTypes.h> |
| 32 | #include <vk/GrVkTypes.h> |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 33 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 34 | #include <cutils/properties.h> |
| 35 | #include <strings.h> |
| 36 | |
| 37 | using namespace android::uirenderer::renderthread; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 38 | |
| 39 | namespace android { |
| 40 | namespace uirenderer { |
| 41 | namespace skiapipeline { |
| 42 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 43 | SkiaVulkanPipeline::SkiaVulkanPipeline(renderthread::RenderThread& thread) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 44 | : SkiaPipeline(thread), mVkManager(thread.vulkanManager()) {} |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 45 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 46 | MakeCurrentResult SkiaVulkanPipeline::makeCurrent() { |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 47 | return MakeCurrentResult::AlreadyCurrent; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | Frame SkiaVulkanPipeline::getFrame() { |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 51 | LOG_ALWAYS_FATAL_IF(mVkSurface == nullptr, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 52 | "drawRenderNode called on a context with no surface!"); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 53 | |
| 54 | SkSurface* backBuffer = mVkManager.getBackbufferSurface(mVkSurface); |
| 55 | if (backBuffer == nullptr) { |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 56 | SkDebugf("failed to get backbuffer"); |
| 57 | return Frame(-1, -1, 0); |
| 58 | } |
| 59 | |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 60 | Frame frame(backBuffer->width(), backBuffer->height(), mVkManager.getAge(mVkSurface)); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 61 | return frame; |
| 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, |
| 65 | const FrameBuilder::LightGeometry& lightGeometry, |
| 66 | LayerUpdateQueue* layerUpdateQueue, const Rect& contentDrawBounds, |
| 67 | bool opaque, bool wideColorGamut, |
| 68 | const BakedOpRenderer::LightInfo& lightInfo, |
| 69 | const std::vector<sp<RenderNode>>& renderNodes, |
| 70 | FrameInfoVisualizer* profiler) { |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 71 | sk_sp<SkSurface> backBuffer = mVkSurface->getBackBufferSurface(); |
| 72 | if (backBuffer.get() == nullptr) { |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 73 | return false; |
| 74 | } |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 75 | SkiaPipeline::updateLighting(lightGeometry, lightInfo); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 76 | renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, wideColorGamut, contentDrawBounds, |
| 77 | backBuffer); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 78 | layerUpdateQueue->clear(); |
Matt Sarett | cf2c05c | 2016-10-26 11:03:23 -0400 | [diff] [blame] | 79 | |
| 80 | // Draw visual debugging features |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 81 | if (CC_UNLIKELY(Properties::showDirtyRegions || |
| 82 | ProfileType::None != Properties::getProfileType())) { |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 83 | SkCanvas* profileCanvas = backBuffer->getCanvas(); |
Matt Sarett | cf2c05c | 2016-10-26 11:03:23 -0400 | [diff] [blame] | 84 | SkiaProfileRenderer profileRenderer(profileCanvas); |
| 85 | profiler->draw(profileRenderer); |
| 86 | profileCanvas->flush(); |
| 87 | } |
| 88 | |
Matt Sarett | 4bda6bf | 2016-11-07 15:43:41 -0500 | [diff] [blame] | 89 | // Log memory statistics |
| 90 | if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) { |
| 91 | dumpResourceCacheUsage(); |
| 92 | } |
| 93 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 94 | return true; |
| 95 | } |
| 96 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 97 | bool SkiaVulkanPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty, |
| 98 | FrameInfo* currentFrameInfo, bool* requireSwap) { |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 99 | *requireSwap = drew; |
| 100 | |
| 101 | // Even if we decided to cancel the frame, from the perspective of jank |
| 102 | // metrics the frame was swapped at this point |
| 103 | currentFrameInfo->markSwapBuffers(); |
| 104 | |
| 105 | if (*requireSwap) { |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 106 | mVkManager.swapBuffers(mVkSurface); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 107 | } |
| 108 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 109 | return *requireSwap; |
| 110 | } |
| 111 | |
| 112 | bool SkiaVulkanPipeline::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) { |
| 113 | // TODO: implement copyLayerInto for vulkan. |
| 114 | return false; |
| 115 | } |
| 116 | |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 117 | static Layer* createLayer(RenderState& renderState, uint32_t layerWidth, uint32_t layerHeight, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 118 | SkColorFilter* colorFilter, int alpha, SkBlendMode mode, bool blend) { |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 119 | return new VkLayer(renderState, layerWidth, layerHeight, colorFilter, alpha, mode, blend); |
| 120 | } |
| 121 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 122 | DeferredLayerUpdater* SkiaVulkanPipeline::createTextureLayer() { |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 123 | mVkManager.initialize(); |
Greg Daniel | 8cd3edf | 2017-01-09 14:15:41 -0500 | [diff] [blame] | 124 | |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 125 | return new DeferredLayerUpdater(mRenderThread.renderState(), createLayer, Layer::Api::Vulkan); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 126 | } |
| 127 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 128 | void SkiaVulkanPipeline::onStop() {} |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 129 | |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 130 | bool SkiaVulkanPipeline::setSurface(Surface* surface, SwapBehavior swapBehavior, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 131 | ColorMode colorMode) { |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 132 | if (mVkSurface) { |
| 133 | mVkManager.destroySurface(mVkSurface); |
| 134 | mVkSurface = nullptr; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | if (surface) { |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 138 | // TODO: handle color mode |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 139 | mVkSurface = mVkManager.createSurface(surface); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 140 | } |
| 141 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 142 | return mVkSurface != nullptr; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | bool SkiaVulkanPipeline::isSurfaceReady() { |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 146 | return CC_UNLIKELY(mVkSurface != nullptr); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | bool SkiaVulkanPipeline::isContextReady() { |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 150 | return CC_LIKELY(mVkManager.hasVkContext()); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void SkiaVulkanPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) { |
| 154 | // TODO: we currently don't support OpenGL WebView's |
| 155 | DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext; |
| 156 | (*functor)(mode, nullptr); |
| 157 | } |
| 158 | |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 159 | sk_sp<Bitmap> SkiaVulkanPipeline::allocateHardwareBitmap(renderthread::RenderThread& renderThread, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 160 | SkBitmap& skBitmap) { |
| 161 | // TODO: implement this function for Vulkan pipeline |
| 162 | // code below is a hack to avoid crashing because of missing HW Bitmap support |
| 163 | sp<GraphicBuffer> buffer = new GraphicBuffer( |
| 164 | skBitmap.info().width(), skBitmap.info().height(), PIXEL_FORMAT_RGBA_8888, |
| 165 | GraphicBuffer::USAGE_HW_TEXTURE | GraphicBuffer::USAGE_SW_WRITE_NEVER | |
| 166 | GraphicBuffer::USAGE_SW_READ_NEVER, |
| 167 | std::string("SkiaVulkanPipeline::allocateHardwareBitmap pid [") + |
| 168 | std::to_string(getpid()) + "]"); |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 169 | status_t error = buffer->initCheck(); |
| 170 | if (error < 0) { |
| 171 | ALOGW("SkiaVulkanPipeline::allocateHardwareBitmap() failed in GraphicBuffer.create()"); |
| 172 | return nullptr; |
| 173 | } |
| 174 | return sk_sp<Bitmap>(new Bitmap(buffer.get(), skBitmap.info())); |
| 175 | } |
| 176 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 177 | } /* namespace skiapipeline */ |
| 178 | } /* namespace uirenderer */ |
| 179 | } /* namespace android */ |