blob: 1bd943f4c21db980dcd41db5a8a49e7487b52b6c [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"
Greg Danielbe2803a2021-02-19 18:32:16 -050020#include "LightingInfo.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040021#include "Readback.h"
John Reck0fa0cbc2019-04-05 16:57:46 -070022#include "ShaderCache.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"
Greg Danielbe2803a2021-02-19 18:32:16 -050028#include "utils/TraceUtils.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040029
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050030#include <SkSurface.h>
Stan Iliev500a0c32016-10-26 10:30:09 -040031#include <SkTypes.h>
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050032
Adlai Holler72cff422020-10-15 09:32:00 -040033#include <GrDirectContext.h>
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050034#include <GrTypes.h>
35#include <vk/GrVkTypes.h>
Stan Iliev500a0c32016-10-26 10:30:09 -040036
Stan Iliev500a0c32016-10-26 10:30:09 -040037#include <cutils/properties.h>
38#include <strings.h>
39
40using namespace android::uirenderer::renderthread;
Stan Iliev500a0c32016-10-26 10:30:09 -040041
42namespace android {
43namespace uirenderer {
44namespace skiapipeline {
45
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050046SkiaVulkanPipeline::SkiaVulkanPipeline(renderthread::RenderThread& thread)
Stan Iliev90276c82019-02-03 18:01:02 -050047 : SkiaPipeline(thread), mVkManager(thread.vulkanManager()) {
48 thread.renderState().registerContextCallback(this);
49}
50
51SkiaVulkanPipeline::~SkiaVulkanPipeline() {
52 mRenderThread.renderState().removeContextCallback(this);
53}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050054
Stan Iliev500a0c32016-10-26 10:30:09 -040055MakeCurrentResult SkiaVulkanPipeline::makeCurrent() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050056 return MakeCurrentResult::AlreadyCurrent;
Stan Iliev500a0c32016-10-26 10:30:09 -040057}
58
59Frame SkiaVulkanPipeline::getFrame() {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050060 LOG_ALWAYS_FATAL_IF(mVkSurface == nullptr, "getFrame() called on a context with no surface!");
61 return mVkManager.dequeueNextBuffer(mVkSurface);
Stan Iliev500a0c32016-10-26 10:30:09 -040062}
63
John Reck1bcacfd2017-11-03 10:12:19 -070064bool SkiaVulkanPipeline::draw(const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
John Reckd9d7f122018-05-03 14:40:56 -070065 const LightGeometry& lightGeometry,
John Reck1bcacfd2017-11-03 10:12:19 -070066 LayerUpdateQueue* layerUpdateQueue, const Rect& contentDrawBounds,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070067 bool opaque, const LightInfo& lightInfo,
John Reck1bcacfd2017-11-03 10:12:19 -070068 const std::vector<sp<RenderNode>>& renderNodes,
69 FrameInfoVisualizer* profiler) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050070 sk_sp<SkSurface> backBuffer = mVkSurface->getCurrentSkSurface();
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050071 if (backBuffer.get() == nullptr) {
Stan Iliev500a0c32016-10-26 10:30:09 -040072 return false;
73 }
Fedor Kudasov90df0562019-06-19 11:41:34 +010074 LightingInfo::updateLighting(lightGeometry, lightInfo);
John Reck0fa0cbc2019-04-05 16:57:46 -070075 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, backBuffer,
76 mVkSurface->getCurrentPreTransform());
Matt Sarettcf2c05c2016-10-26 11:03:23 -040077
78 // Draw visual debugging features
John Reck1bcacfd2017-11-03 10:12:19 -070079 if (CC_UNLIKELY(Properties::showDirtyRegions ||
80 ProfileType::None != Properties::getProfileType())) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050081 SkCanvas* profileCanvas = backBuffer->getCanvas();
Matt Sarettcf2c05c2016-10-26 11:03:23 -040082 SkiaProfileRenderer profileRenderer(profileCanvas);
83 profiler->draw(profileRenderer);
Matt Sarettcf2c05c2016-10-26 11:03:23 -040084 }
85
Greg Danielbe2803a2021-02-19 18:32:16 -050086 {
87 ATRACE_NAME("flush commands");
88 mVkManager.finishFrame(backBuffer.get());
89 }
90 layerUpdateQueue->clear();
91
Matt Sarett4bda6bf2016-11-07 15:43:41 -050092 // Log memory statistics
93 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
94 dumpResourceCacheUsage();
95 }
96
Stan Iliev500a0c32016-10-26 10:30:09 -040097 return true;
98}
99
John Reck1bcacfd2017-11-03 10:12:19 -0700100bool SkiaVulkanPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
101 FrameInfo* currentFrameInfo, bool* requireSwap) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400102 *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 Sollenbergera19b71a2019-02-15 16:36:30 -0500109 mVkManager.swapBuffers(mVkSurface, screenDirty);
Stan Iliev500a0c32016-10-26 10:30:09 -0400110 }
111
Stan Iliev500a0c32016-10-26 10:30:09 -0400112 return *requireSwap;
113}
114
Stan Iliev500a0c32016-10-26 10:30:09 -0400115DeferredLayerUpdater* SkiaVulkanPipeline::createTextureLayer() {
Stan Iliev981afe72019-02-13 14:24:33 -0500116 mRenderThread.requireVkContext();
Greg Daniel8cd3edf2017-01-09 14:15:41 -0500117
Stan Iliev564ca3e2018-09-04 22:00:00 +0000118 return new DeferredLayerUpdater(mRenderThread.renderState());
Stan Iliev500a0c32016-10-26 10:30:09 -0400119}
120
John Reck1bcacfd2017-11-03 10:12:19 -0700121void SkiaVulkanPipeline::onStop() {}
Stan Iliev500a0c32016-10-26 10:30:09 -0400122
John Reck8ddbc592020-05-07 16:11:18 -0700123bool SkiaVulkanPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500124 if (mVkSurface) {
125 mVkManager.destroySurface(mVkSurface);
126 mVkSurface = nullptr;
Stan Iliev500a0c32016-10-26 10:30:09 -0400127 }
128
129 if (surface) {
Stan Iliev981afe72019-02-13 14:24:33 -0500130 mRenderThread.requireVkContext();
John Reck0fa0cbc2019-04-05 16:57:46 -0700131 mVkSurface =
Derek Sollenberger1863d942020-02-05 15:41:51 -0500132 mVkManager.createSurface(surface, mColorMode, mSurfaceColorSpace, mSurfaceColorType,
John Reck8ddbc592020-05-07 16:11:18 -0700133 mRenderThread.getGrContext(), 0);
Greg Daniel031b81b2018-10-02 14:47:22 -0400134 }
135
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500136 return mVkSurface != nullptr;
Stan Iliev500a0c32016-10-26 10:30:09 -0400137}
138
139bool SkiaVulkanPipeline::isSurfaceReady() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500140 return CC_UNLIKELY(mVkSurface != nullptr);
Stan Iliev500a0c32016-10-26 10:30:09 -0400141}
142
143bool SkiaVulkanPipeline::isContextReady() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500144 return CC_LIKELY(mVkManager.hasVkContext());
Stan Iliev500a0c32016-10-26 10:30:09 -0400145}
146
147void SkiaVulkanPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
Chris Blume5f1ac2b2018-11-05 16:10:39 -0800148 VkInteropFunctorDrawable::vkInvokeFunctor(functor);
Stan Iliev500a0c32016-10-26 10:30:09 -0400149}
150
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400151sk_sp<Bitmap> SkiaVulkanPipeline::allocateHardwareBitmap(renderthread::RenderThread& renderThread,
John Reck1bcacfd2017-11-03 10:12:19 -0700152 SkBitmap& skBitmap) {
Derek Sollenberger6e35e632019-01-22 13:56:25 -0500153 LOG_ALWAYS_FATAL("Unimplemented");
154 return nullptr;
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400155}
156
Stan Iliev90276c82019-02-03 18:01:02 -0500157void SkiaVulkanPipeline::onContextDestroyed() {
158 if (mVkSurface) {
159 mVkManager.destroySurface(mVkSurface);
160 mVkSurface = nullptr;
161 }
162}
163
Stan Iliev500a0c32016-10-26 10:30:09 -0400164} /* namespace skiapipeline */
165} /* namespace uirenderer */
166} /* namespace android */