blob: edde6d3e05c0a4094b2b6ca12ad82d82c73dc20a [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)
Stan Iliev90276c82019-02-03 18:01:02 -050045 : SkiaPipeline(thread), mVkManager(thread.vulkanManager()) {
46 thread.renderState().registerContextCallback(this);
47}
48
49SkiaVulkanPipeline::~SkiaVulkanPipeline() {
50 mRenderThread.renderState().removeContextCallback(this);
51}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050052
Stan Iliev500a0c32016-10-26 10:30:09 -040053MakeCurrentResult SkiaVulkanPipeline::makeCurrent() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050054 return MakeCurrentResult::AlreadyCurrent;
Stan Iliev500a0c32016-10-26 10:30:09 -040055}
56
57Frame SkiaVulkanPipeline::getFrame() {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050058 LOG_ALWAYS_FATAL_IF(mVkSurface == nullptr, "getFrame() called on a context with no surface!");
59 return mVkManager.dequeueNextBuffer(mVkSurface);
Stan Iliev500a0c32016-10-26 10:30:09 -040060}
61
John Reck1bcacfd2017-11-03 10:12:19 -070062bool SkiaVulkanPipeline::draw(const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
John Reckd9d7f122018-05-03 14:40:56 -070063 const LightGeometry& lightGeometry,
John Reck1bcacfd2017-11-03 10:12:19 -070064 LayerUpdateQueue* layerUpdateQueue, const Rect& contentDrawBounds,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070065 bool opaque, const LightInfo& lightInfo,
John Reck1bcacfd2017-11-03 10:12:19 -070066 const std::vector<sp<RenderNode>>& renderNodes,
67 FrameInfoVisualizer* profiler) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050068 sk_sp<SkSurface> backBuffer = mVkSurface->getCurrentSkSurface();
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050069 if (backBuffer.get() == nullptr) {
Stan Iliev500a0c32016-10-26 10:30:09 -040070 return false;
71 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050072 SkiaPipeline::updateLighting(lightGeometry, lightInfo);
Greg Danielc4076782019-01-08 16:01:18 -050073 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds,
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050074 backBuffer, mVkSurface->getCurrentPreTransform());
Stan Iliev14211aa2019-01-14 12:29:30 -050075 ShaderCache::get().onVkFrameFlushed(mRenderThread.getGrContext());
Stan Iliev500a0c32016-10-26 10:30:09 -040076 layerUpdateQueue->clear();
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);
84 profileCanvas->flush();
85 }
86
Matt Sarett4bda6bf2016-11-07 15:43:41 -050087 // Log memory statistics
88 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
89 dumpResourceCacheUsage();
90 }
91
Stan Iliev500a0c32016-10-26 10:30:09 -040092 return true;
93}
94
John Reck1bcacfd2017-11-03 10:12:19 -070095bool SkiaVulkanPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
96 FrameInfo* currentFrameInfo, bool* requireSwap) {
Stan Iliev500a0c32016-10-26 10:30:09 -040097 *requireSwap = drew;
98
99 // Even if we decided to cancel the frame, from the perspective of jank
100 // metrics the frame was swapped at this point
101 currentFrameInfo->markSwapBuffers();
102
103 if (*requireSwap) {
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500104 mVkManager.swapBuffers(mVkSurface, screenDirty);
Stan Iliev500a0c32016-10-26 10:30:09 -0400105 }
106
Stan Iliev500a0c32016-10-26 10:30:09 -0400107 return *requireSwap;
108}
109
Stan Iliev500a0c32016-10-26 10:30:09 -0400110DeferredLayerUpdater* SkiaVulkanPipeline::createTextureLayer() {
Stan Iliev981afe72019-02-13 14:24:33 -0500111 mRenderThread.requireVkContext();
Greg Daniel8cd3edf2017-01-09 14:15:41 -0500112
Stan Iliev564ca3e2018-09-04 22:00:00 +0000113 return new DeferredLayerUpdater(mRenderThread.renderState());
Stan Iliev500a0c32016-10-26 10:30:09 -0400114}
115
John Reck1bcacfd2017-11-03 10:12:19 -0700116void SkiaVulkanPipeline::onStop() {}
Stan Iliev500a0c32016-10-26 10:30:09 -0400117
John Reck848f6512018-12-03 13:26:43 -0800118bool SkiaVulkanPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior,
John Reck1bcacfd2017-11-03 10:12:19 -0700119 ColorMode colorMode) {
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
Peiyong Lin3bff1352018-12-11 07:56:07 -0800125 setSurfaceColorProperties(colorMode);
Stan Iliev500a0c32016-10-26 10:30:09 -0400126 if (surface) {
Stan Iliev981afe72019-02-13 14:24:33 -0500127 mRenderThread.requireVkContext();
Peiyong Lin3bff1352018-12-11 07:56:07 -0800128 mVkSurface = mVkManager.createSurface(surface, colorMode, mSurfaceColorSpace,
Stan Iliev981afe72019-02-13 14:24:33 -0500129 mSurfaceColorType, mRenderThread.getGrContext());
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 */