blob: 212a4284a824fccaf3decb381135be6ae966e29a [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"
John Reck0fa0cbc2019-04-05 16:57:46 -070021#include "ShaderCache.h"
Fedor Kudasov90df0562019-06-19 11:41:34 +010022#include "LightingInfo.h"
Matt Sarettcf2c05c2016-10-26 11:03:23 -040023#include "SkiaPipeline.h"
24#include "SkiaProfileRenderer.h"
John Reck283bb462018-12-13 16:40:14 -080025#include "VkInteropFunctorDrawable.h"
John Reck1bcacfd2017-11-03 10:12:19 -070026#include "renderstate/RenderState.h"
27#include "renderthread/Frame.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040028
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050029#include <SkSurface.h>
Stan Iliev500a0c32016-10-26 10:30:09 -040030#include <SkTypes.h>
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050031
32#include <GrContext.h>
33#include <GrTypes.h>
34#include <vk/GrVkTypes.h>
Stan Iliev500a0c32016-10-26 10:30:09 -040035
Stan Iliev500a0c32016-10-26 10:30:09 -040036#include <cutils/properties.h>
37#include <strings.h>
38
39using namespace android::uirenderer::renderthread;
Stan Iliev500a0c32016-10-26 10:30:09 -040040
41namespace android {
42namespace uirenderer {
43namespace skiapipeline {
44
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050045SkiaVulkanPipeline::SkiaVulkanPipeline(renderthread::RenderThread& thread)
Stan Iliev90276c82019-02-03 18:01:02 -050046 : SkiaPipeline(thread), mVkManager(thread.vulkanManager()) {
47 thread.renderState().registerContextCallback(this);
48}
49
50SkiaVulkanPipeline::~SkiaVulkanPipeline() {
51 mRenderThread.renderState().removeContextCallback(this);
52}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050053
Stan Iliev500a0c32016-10-26 10:30:09 -040054MakeCurrentResult SkiaVulkanPipeline::makeCurrent() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050055 return MakeCurrentResult::AlreadyCurrent;
Stan Iliev500a0c32016-10-26 10:30:09 -040056}
57
58Frame SkiaVulkanPipeline::getFrame() {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050059 LOG_ALWAYS_FATAL_IF(mVkSurface == nullptr, "getFrame() called on a context with no surface!");
60 return mVkManager.dequeueNextBuffer(mVkSurface);
Stan Iliev500a0c32016-10-26 10:30:09 -040061}
62
John Reck1bcacfd2017-11-03 10:12:19 -070063bool SkiaVulkanPipeline::draw(const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
John Reckd9d7f122018-05-03 14:40:56 -070064 const LightGeometry& lightGeometry,
John Reck1bcacfd2017-11-03 10:12:19 -070065 LayerUpdateQueue* layerUpdateQueue, const Rect& contentDrawBounds,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070066 bool opaque, const LightInfo& lightInfo,
John Reck1bcacfd2017-11-03 10:12:19 -070067 const std::vector<sp<RenderNode>>& renderNodes,
68 FrameInfoVisualizer* profiler) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050069 sk_sp<SkSurface> backBuffer = mVkSurface->getCurrentSkSurface();
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050070 if (backBuffer.get() == nullptr) {
Stan Iliev500a0c32016-10-26 10:30:09 -040071 return false;
72 }
Fedor Kudasov90df0562019-06-19 11:41:34 +010073 LightingInfo::updateLighting(lightGeometry, lightInfo);
John Reck0fa0cbc2019-04-05 16:57:46 -070074 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, backBuffer,
75 mVkSurface->getCurrentPreTransform());
Stan Iliev14211aa2019-01-14 12:29:30 -050076 ShaderCache::get().onVkFrameFlushed(mRenderThread.getGrContext());
Stan Iliev500a0c32016-10-26 10:30:09 -040077 layerUpdateQueue->clear();
Matt Sarettcf2c05c2016-10-26 11:03:23 -040078
79 // Draw visual debugging features
John Reck1bcacfd2017-11-03 10:12:19 -070080 if (CC_UNLIKELY(Properties::showDirtyRegions ||
81 ProfileType::None != Properties::getProfileType())) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050082 SkCanvas* profileCanvas = backBuffer->getCanvas();
Matt Sarettcf2c05c2016-10-26 11:03:23 -040083 SkiaProfileRenderer profileRenderer(profileCanvas);
84 profiler->draw(profileRenderer);
85 profileCanvas->flush();
86 }
87
Matt Sarett4bda6bf2016-11-07 15:43:41 -050088 // Log memory statistics
89 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
90 dumpResourceCacheUsage();
91 }
92
Stan Iliev500a0c32016-10-26 10:30:09 -040093 return true;
94}
95
John Reck1bcacfd2017-11-03 10:12:19 -070096bool SkiaVulkanPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
97 FrameInfo* currentFrameInfo, bool* requireSwap) {
Stan Iliev500a0c32016-10-26 10:30:09 -040098 *requireSwap = drew;
99
100 // Even if we decided to cancel the frame, from the perspective of jank
101 // metrics the frame was swapped at this point
102 currentFrameInfo->markSwapBuffers();
103
104 if (*requireSwap) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500105 mVkManager.swapBuffers(mVkSurface, screenDirty);
Stan Iliev500a0c32016-10-26 10:30:09 -0400106 }
107
Stan Iliev500a0c32016-10-26 10:30:09 -0400108 return *requireSwap;
109}
110
Stan Iliev500a0c32016-10-26 10:30:09 -0400111DeferredLayerUpdater* SkiaVulkanPipeline::createTextureLayer() {
Stan Iliev981afe72019-02-13 14:24:33 -0500112 mRenderThread.requireVkContext();
Greg Daniel8cd3edf2017-01-09 14:15:41 -0500113
Stan Iliev564ca3e2018-09-04 22:00:00 +0000114 return new DeferredLayerUpdater(mRenderThread.renderState());
Stan Iliev500a0c32016-10-26 10:30:09 -0400115}
116
John Reck1bcacfd2017-11-03 10:12:19 -0700117void SkiaVulkanPipeline::onStop() {}
Stan Iliev500a0c32016-10-26 10:30:09 -0400118
John Reck8ddbc592020-05-07 16:11:18 -0700119bool SkiaVulkanPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500120 if (mVkSurface) {
121 mVkManager.destroySurface(mVkSurface);
122 mVkSurface = nullptr;
Stan Iliev500a0c32016-10-26 10:30:09 -0400123 }
124
125 if (surface) {
Stan Iliev981afe72019-02-13 14:24:33 -0500126 mRenderThread.requireVkContext();
John Reck0fa0cbc2019-04-05 16:57:46 -0700127 mVkSurface =
Derek Sollenberger1863d942020-02-05 15:41:51 -0500128 mVkManager.createSurface(surface, mColorMode, mSurfaceColorSpace, mSurfaceColorType,
John Reck8ddbc592020-05-07 16:11:18 -0700129 mRenderThread.getGrContext(), 0);
Greg Daniel031b81b2018-10-02 14:47:22 -0400130 }
131
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500132 return mVkSurface != nullptr;
Stan Iliev500a0c32016-10-26 10:30:09 -0400133}
134
135bool SkiaVulkanPipeline::isSurfaceReady() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500136 return CC_UNLIKELY(mVkSurface != nullptr);
Stan Iliev500a0c32016-10-26 10:30:09 -0400137}
138
139bool SkiaVulkanPipeline::isContextReady() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500140 return CC_LIKELY(mVkManager.hasVkContext());
Stan Iliev500a0c32016-10-26 10:30:09 -0400141}
142
143void SkiaVulkanPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
Chris Blume5f1ac2b2018-11-05 16:10:39 -0800144 VkInteropFunctorDrawable::vkInvokeFunctor(functor);
Stan Iliev500a0c32016-10-26 10:30:09 -0400145}
146
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400147sk_sp<Bitmap> SkiaVulkanPipeline::allocateHardwareBitmap(renderthread::RenderThread& renderThread,
John Reck1bcacfd2017-11-03 10:12:19 -0700148 SkBitmap& skBitmap) {
Derek Sollenberger6e35e632019-01-22 13:56:25 -0500149 LOG_ALWAYS_FATAL("Unimplemented");
150 return nullptr;
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400151}
152
Stan Iliev90276c82019-02-03 18:01:02 -0500153void SkiaVulkanPipeline::onContextDestroyed() {
154 if (mVkSurface) {
155 mVkManager.destroySurface(mVkSurface);
156 mVkSurface = nullptr;
157 }
158}
159
Stan Iliev500a0c32016-10-26 10:30:09 -0400160} /* namespace skiapipeline */
161} /* namespace uirenderer */
162} /* namespace android */