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