blob: b9aae9839bdc3543fbfc4be55f276506fe1d90d4 [file] [log] [blame]
Stan Iliev500a0c32016-10-26 10:30:09 -04001/*
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 Iliev500a0c32016-10-26 10:30:09 -040020#include "Readback.h"
Matt Sarettcf2c05c2016-10-26 11:03:23 -040021#include "SkiaPipeline.h"
22#include "SkiaProfileRenderer.h"
John Reck283bb462018-12-13 16:40:14 -080023#include "VkInteropFunctorDrawable.h"
John Reck1bcacfd2017-11-03 10:12:19 -070024#include "renderstate/RenderState.h"
25#include "renderthread/Frame.h"
Stan Iliev14211aa2019-01-14 12:29:30 -050026#include "ShaderCache.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040027
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050028#include <SkSurface.h>
Stan Iliev500a0c32016-10-26 10:30:09 -040029#include <SkTypes.h>
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050030
31#include <GrContext.h>
32#include <GrTypes.h>
33#include <vk/GrVkTypes.h>
Stan Iliev500a0c32016-10-26 10:30:09 -040034
Stan Iliev500a0c32016-10-26 10:30:09 -040035#include <cutils/properties.h>
36#include <strings.h>
37
38using namespace android::uirenderer::renderthread;
Stan Iliev500a0c32016-10-26 10:30:09 -040039
40namespace android {
41namespace uirenderer {
42namespace skiapipeline {
43
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050044SkiaVulkanPipeline::SkiaVulkanPipeline(renderthread::RenderThread& thread)
John Reck1bcacfd2017-11-03 10:12:19 -070045 : SkiaPipeline(thread), mVkManager(thread.vulkanManager()) {}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050046
Stan Iliev500a0c32016-10-26 10:30:09 -040047MakeCurrentResult SkiaVulkanPipeline::makeCurrent() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050048 return MakeCurrentResult::AlreadyCurrent;
Stan Iliev500a0c32016-10-26 10:30:09 -040049}
50
51Frame SkiaVulkanPipeline::getFrame() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050052 LOG_ALWAYS_FATAL_IF(mVkSurface == nullptr,
John Reck1bcacfd2017-11-03 10:12:19 -070053 "drawRenderNode called on a context with no surface!");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050054
Stan Iliev305e13a2018-11-13 11:14:48 -050055 SkSurface* backBuffer = mVkManager.getBackbufferSurface(&mVkSurface);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050056 if (backBuffer == nullptr) {
Stan Iliev500a0c32016-10-26 10:30:09 -040057 SkDebugf("failed to get backbuffer");
58 return Frame(-1, -1, 0);
59 }
60
Greg Danielcd558522016-11-17 13:31:40 -050061 Frame frame(backBuffer->width(), backBuffer->height(), mVkManager.getAge(mVkSurface));
Stan Iliev500a0c32016-10-26 10:30:09 -040062 return frame;
63}
64
John Reck1bcacfd2017-11-03 10:12:19 -070065bool SkiaVulkanPipeline::draw(const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
John Reckd9d7f122018-05-03 14:40:56 -070066 const LightGeometry& lightGeometry,
John Reck1bcacfd2017-11-03 10:12:19 -070067 LayerUpdateQueue* layerUpdateQueue, const Rect& contentDrawBounds,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070068 bool opaque, const LightInfo& lightInfo,
John Reck1bcacfd2017-11-03 10:12:19 -070069 const std::vector<sp<RenderNode>>& renderNodes,
70 FrameInfoVisualizer* profiler) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050071 sk_sp<SkSurface> backBuffer = mVkSurface->getBackBufferSurface();
72 if (backBuffer.get() == nullptr) {
Stan Iliev500a0c32016-10-26 10:30:09 -040073 return false;
74 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050075 SkiaPipeline::updateLighting(lightGeometry, lightInfo);
Peiyong Lin1f6aa122018-09-10 16:28:08 -070076 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, backBuffer);
Stan Iliev14211aa2019-01-14 12:29:30 -050077 ShaderCache::get().onVkFrameFlushed(mRenderThread.getGrContext());
Stan Iliev500a0c32016-10-26 10:30:09 -040078 layerUpdateQueue->clear();
Matt Sarettcf2c05c2016-10-26 11:03:23 -040079
80 // Draw visual debugging features
John Reck1bcacfd2017-11-03 10:12:19 -070081 if (CC_UNLIKELY(Properties::showDirtyRegions ||
82 ProfileType::None != Properties::getProfileType())) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050083 SkCanvas* profileCanvas = backBuffer->getCanvas();
Matt Sarettcf2c05c2016-10-26 11:03:23 -040084 SkiaProfileRenderer profileRenderer(profileCanvas);
85 profiler->draw(profileRenderer);
86 profileCanvas->flush();
87 }
88
Matt Sarett4bda6bf2016-11-07 15:43:41 -050089 // Log memory statistics
90 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
91 dumpResourceCacheUsage();
92 }
93
Stan Iliev500a0c32016-10-26 10:30:09 -040094 return true;
95}
96
John Reck1bcacfd2017-11-03 10:12:19 -070097bool SkiaVulkanPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
98 FrameInfo* currentFrameInfo, bool* requireSwap) {
Stan Iliev500a0c32016-10-26 10:30:09 -040099 *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 Sollenberger0e3cba32016-11-09 11:58:36 -0500106 mVkManager.swapBuffers(mVkSurface);
Stan Iliev500a0c32016-10-26 10:30:09 -0400107 }
108
Stan Iliev500a0c32016-10-26 10:30:09 -0400109 return *requireSwap;
110}
111
Stan Iliev500a0c32016-10-26 10:30:09 -0400112DeferredLayerUpdater* SkiaVulkanPipeline::createTextureLayer() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500113 mVkManager.initialize();
Greg Daniel8cd3edf2017-01-09 14:15:41 -0500114
Stan Iliev564ca3e2018-09-04 22:00:00 +0000115 return new DeferredLayerUpdater(mRenderThread.renderState());
Stan Iliev500a0c32016-10-26 10:30:09 -0400116}
117
John Reck1bcacfd2017-11-03 10:12:19 -0700118void SkiaVulkanPipeline::onStop() {}
Stan Iliev500a0c32016-10-26 10:30:09 -0400119
John Reck848f6512018-12-03 13:26:43 -0800120bool SkiaVulkanPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior,
John Reck1bcacfd2017-11-03 10:12:19 -0700121 ColorMode colorMode) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500122 if (mVkSurface) {
123 mVkManager.destroySurface(mVkSurface);
124 mVkSurface = nullptr;
Stan Iliev500a0c32016-10-26 10:30:09 -0400125 }
126
Peiyong Lin3bff1352018-12-11 07:56:07 -0800127 setSurfaceColorProperties(colorMode);
Stan Iliev500a0c32016-10-26 10:30:09 -0400128 if (surface) {
Peiyong Lin3bff1352018-12-11 07:56:07 -0800129 mVkSurface = mVkManager.createSurface(surface, colorMode, mSurfaceColorSpace,
130 mSurfaceColorGamut, mSurfaceColorType);
Greg Daniel031b81b2018-10-02 14:47:22 -0400131 }
132
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500133 return mVkSurface != nullptr;
Stan Iliev500a0c32016-10-26 10:30:09 -0400134}
135
136bool SkiaVulkanPipeline::isSurfaceReady() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500137 return CC_UNLIKELY(mVkSurface != nullptr);
Stan Iliev500a0c32016-10-26 10:30:09 -0400138}
139
140bool SkiaVulkanPipeline::isContextReady() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500141 return CC_LIKELY(mVkManager.hasVkContext());
Stan Iliev500a0c32016-10-26 10:30:09 -0400142}
143
144void SkiaVulkanPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
Chris Blume5f1ac2b2018-11-05 16:10:39 -0800145 VkInteropFunctorDrawable::vkInvokeFunctor(functor);
Stan Iliev500a0c32016-10-26 10:30:09 -0400146}
147
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400148sk_sp<Bitmap> SkiaVulkanPipeline::allocateHardwareBitmap(renderthread::RenderThread& renderThread,
John Reck1bcacfd2017-11-03 10:12:19 -0700149 SkBitmap& skBitmap) {
150 // TODO: implement this function for Vulkan pipeline
151 // code below is a hack to avoid crashing because of missing HW Bitmap support
152 sp<GraphicBuffer> buffer = new GraphicBuffer(
153 skBitmap.info().width(), skBitmap.info().height(), PIXEL_FORMAT_RGBA_8888,
154 GraphicBuffer::USAGE_HW_TEXTURE | GraphicBuffer::USAGE_SW_WRITE_NEVER |
155 GraphicBuffer::USAGE_SW_READ_NEVER,
156 std::string("SkiaVulkanPipeline::allocateHardwareBitmap pid [") +
157 std::to_string(getpid()) + "]");
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400158 status_t error = buffer->initCheck();
159 if (error < 0) {
160 ALOGW("SkiaVulkanPipeline::allocateHardwareBitmap() failed in GraphicBuffer.create()");
161 return nullptr;
162 }
Derek Sollenbergere2169482018-11-20 10:57:20 -0500163 return Bitmap::createFrom(buffer, skBitmap.refColorSpace());
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400164}
165
Stan Iliev500a0c32016-10-26 10:30:09 -0400166} /* namespace skiapipeline */
167} /* namespace uirenderer */
168} /* namespace android */