blob: 99fd463b0660368683486407dc5ec26d9608e174 [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
rnleece9762b2021-05-21 15:40:53 -070019#include <gui/TraceUtils.h>
Stan Iliev500a0c32016-10-26 10:30:09 -040020#include "DeferredLayerUpdater.h"
Greg Danielbe2803a2021-02-19 18:32:16 -050021#include "LightingInfo.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040022#include "Readback.h"
John Reck0fa0cbc2019-04-05 16:57:46 -070023#include "ShaderCache.h"
Matt Sarettcf2c05c2016-10-26 11:03:23 -040024#include "SkiaPipeline.h"
25#include "SkiaProfileRenderer.h"
John Reck283bb462018-12-13 16:40:14 -080026#include "VkInteropFunctorDrawable.h"
John Reck1bcacfd2017-11-03 10:12:19 -070027#include "renderstate/RenderState.h"
28#include "renderthread/Frame.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
Greg Danield2317a42021-04-08 11:39:02 -040046SkiaVulkanPipeline::SkiaVulkanPipeline(renderthread::RenderThread& thread) : SkiaPipeline(thread) {
Stan Iliev90276c82019-02-03 18:01:02 -050047 thread.renderState().registerContextCallback(this);
48}
49
50SkiaVulkanPipeline::~SkiaVulkanPipeline() {
51 mRenderThread.renderState().removeContextCallback(this);
52}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050053
Greg Danield2317a42021-04-08 11:39:02 -040054VulkanManager& SkiaVulkanPipeline::vulkanManager() {
55 return mRenderThread.vulkanManager();
56}
57
Stan Iliev500a0c32016-10-26 10:30:09 -040058MakeCurrentResult SkiaVulkanPipeline::makeCurrent() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050059 return MakeCurrentResult::AlreadyCurrent;
Stan Iliev500a0c32016-10-26 10:30:09 -040060}
61
62Frame SkiaVulkanPipeline::getFrame() {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050063 LOG_ALWAYS_FATAL_IF(mVkSurface == nullptr, "getFrame() called on a context with no surface!");
Greg Danield2317a42021-04-08 11:39:02 -040064 return vulkanManager().dequeueNextBuffer(mVkSurface);
Stan Iliev500a0c32016-10-26 10:30:09 -040065}
66
John Reck1bcacfd2017-11-03 10:12:19 -070067bool SkiaVulkanPipeline::draw(const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
John Reckd9d7f122018-05-03 14:40:56 -070068 const LightGeometry& lightGeometry,
John Reck1bcacfd2017-11-03 10:12:19 -070069 LayerUpdateQueue* layerUpdateQueue, const Rect& contentDrawBounds,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070070 bool opaque, const LightInfo& lightInfo,
John Reck1bcacfd2017-11-03 10:12:19 -070071 const std::vector<sp<RenderNode>>& renderNodes,
72 FrameInfoVisualizer* profiler) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050073 sk_sp<SkSurface> backBuffer = mVkSurface->getCurrentSkSurface();
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050074 if (backBuffer.get() == nullptr) {
Stan Iliev500a0c32016-10-26 10:30:09 -040075 return false;
76 }
Derek Sollenberger0784f242022-03-28 19:59:42 +000077
78 // update the coordinates of the global light position based on surface rotation
79 SkPoint lightCenter = mVkSurface->getCurrentPreTransform().mapXY(lightGeometry.center.x,
80 lightGeometry.center.y);
81 LightGeometry localGeometry = lightGeometry;
82 localGeometry.center.x = lightCenter.fX;
83 localGeometry.center.y = lightCenter.fY;
84
85 LightingInfo::updateLighting(localGeometry, lightInfo);
John Reck0fa0cbc2019-04-05 16:57:46 -070086 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, backBuffer,
87 mVkSurface->getCurrentPreTransform());
Matt Sarettcf2c05c2016-10-26 11:03:23 -040088
89 // Draw visual debugging features
John Reck1bcacfd2017-11-03 10:12:19 -070090 if (CC_UNLIKELY(Properties::showDirtyRegions ||
91 ProfileType::None != Properties::getProfileType())) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050092 SkCanvas* profileCanvas = backBuffer->getCanvas();
Matt Sarettcf2c05c2016-10-26 11:03:23 -040093 SkiaProfileRenderer profileRenderer(profileCanvas);
94 profiler->draw(profileRenderer);
Matt Sarettcf2c05c2016-10-26 11:03:23 -040095 }
96
Greg Danielbe2803a2021-02-19 18:32:16 -050097 {
98 ATRACE_NAME("flush commands");
Greg Danield2317a42021-04-08 11:39:02 -040099 vulkanManager().finishFrame(backBuffer.get());
Greg Danielbe2803a2021-02-19 18:32:16 -0500100 }
101 layerUpdateQueue->clear();
102
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500103 // Log memory statistics
104 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
105 dumpResourceCacheUsage();
106 }
107
Stan Iliev500a0c32016-10-26 10:30:09 -0400108 return true;
109}
110
John Reck1bcacfd2017-11-03 10:12:19 -0700111bool SkiaVulkanPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
112 FrameInfo* currentFrameInfo, bool* requireSwap) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400113 *requireSwap = drew;
114
115 // Even if we decided to cancel the frame, from the perspective of jank
116 // metrics the frame was swapped at this point
117 currentFrameInfo->markSwapBuffers();
118
119 if (*requireSwap) {
Greg Danield2317a42021-04-08 11:39:02 -0400120 vulkanManager().swapBuffers(mVkSurface, screenDirty);
Stan Iliev500a0c32016-10-26 10:30:09 -0400121 }
122
Stan Iliev500a0c32016-10-26 10:30:09 -0400123 return *requireSwap;
124}
125
Stan Iliev500a0c32016-10-26 10:30:09 -0400126DeferredLayerUpdater* SkiaVulkanPipeline::createTextureLayer() {
Stan Iliev981afe72019-02-13 14:24:33 -0500127 mRenderThread.requireVkContext();
Greg Daniel8cd3edf2017-01-09 14:15:41 -0500128
Stan Iliev564ca3e2018-09-04 22:00:00 +0000129 return new DeferredLayerUpdater(mRenderThread.renderState());
Stan Iliev500a0c32016-10-26 10:30:09 -0400130}
131
John Reck1bcacfd2017-11-03 10:12:19 -0700132void SkiaVulkanPipeline::onStop() {}
Stan Iliev500a0c32016-10-26 10:30:09 -0400133
John Reck8ddbc592020-05-07 16:11:18 -0700134bool SkiaVulkanPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500135 if (mVkSurface) {
Greg Danield2317a42021-04-08 11:39:02 -0400136 vulkanManager().destroySurface(mVkSurface);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500137 mVkSurface = nullptr;
Stan Iliev500a0c32016-10-26 10:30:09 -0400138 }
139
140 if (surface) {
Stan Iliev981afe72019-02-13 14:24:33 -0500141 mRenderThread.requireVkContext();
John Reck0fa0cbc2019-04-05 16:57:46 -0700142 mVkSurface =
Greg Danield2317a42021-04-08 11:39:02 -0400143 vulkanManager().createSurface(surface, mColorMode, mSurfaceColorSpace,
144 mSurfaceColorType, mRenderThread.getGrContext(), 0);
Greg Daniel031b81b2018-10-02 14:47:22 -0400145 }
146
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500147 return mVkSurface != nullptr;
Stan Iliev500a0c32016-10-26 10:30:09 -0400148}
149
150bool SkiaVulkanPipeline::isSurfaceReady() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500151 return CC_UNLIKELY(mVkSurface != nullptr);
Stan Iliev500a0c32016-10-26 10:30:09 -0400152}
153
154bool SkiaVulkanPipeline::isContextReady() {
Greg Danield2317a42021-04-08 11:39:02 -0400155 return CC_LIKELY(vulkanManager().hasVkContext());
Stan Iliev500a0c32016-10-26 10:30:09 -0400156}
157
158void SkiaVulkanPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
Chris Blume5f1ac2b2018-11-05 16:10:39 -0800159 VkInteropFunctorDrawable::vkInvokeFunctor(functor);
Stan Iliev500a0c32016-10-26 10:30:09 -0400160}
161
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400162sk_sp<Bitmap> SkiaVulkanPipeline::allocateHardwareBitmap(renderthread::RenderThread& renderThread,
John Reck1bcacfd2017-11-03 10:12:19 -0700163 SkBitmap& skBitmap) {
Derek Sollenberger6e35e632019-01-22 13:56:25 -0500164 LOG_ALWAYS_FATAL("Unimplemented");
165 return nullptr;
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400166}
167
Stan Iliev90276c82019-02-03 18:01:02 -0500168void SkiaVulkanPipeline::onContextDestroyed() {
169 if (mVkSurface) {
Greg Danield2317a42021-04-08 11:39:02 -0400170 vulkanManager().destroySurface(mVkSurface);
Stan Iliev90276c82019-02-03 18:01:02 -0500171 mVkSurface = nullptr;
172 }
173}
174
Stan Iliev500a0c32016-10-26 10:30:09 -0400175} /* namespace skiapipeline */
176} /* namespace uirenderer */
177} /* namespace android */