blob: 87cffb52c150dbb67bb698fb61edbcad13aec863 [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 Sollenberger0e3cba32016-11-09 11:58:36 -050058 LOG_ALWAYS_FATAL_IF(mVkSurface == nullptr,
John Reck1bcacfd2017-11-03 10:12:19 -070059 "drawRenderNode called on a context with no surface!");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050060
Stan Iliev305e13a2018-11-13 11:14:48 -050061 SkSurface* backBuffer = mVkManager.getBackbufferSurface(&mVkSurface);
Stan Iliev90276c82019-02-03 18:01:02 -050062 LOG_ALWAYS_FATAL_IF(mVkSurface == nullptr,
63 "drawRenderNode called on a context with an invalid surface");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050064 if (backBuffer == nullptr) {
Stan Iliev500a0c32016-10-26 10:30:09 -040065 SkDebugf("failed to get backbuffer");
66 return Frame(-1, -1, 0);
67 }
68
Greg Danielc4076782019-01-08 16:01:18 -050069 Frame frame(mVkSurface->windowWidth(), mVkSurface->windowHeight(),
70 mVkManager.getAge(mVkSurface));
Stan Iliev500a0c32016-10-26 10:30:09 -040071 return frame;
72}
73
John Reck1bcacfd2017-11-03 10:12:19 -070074bool SkiaVulkanPipeline::draw(const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
John Reckd9d7f122018-05-03 14:40:56 -070075 const LightGeometry& lightGeometry,
John Reck1bcacfd2017-11-03 10:12:19 -070076 LayerUpdateQueue* layerUpdateQueue, const Rect& contentDrawBounds,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070077 bool opaque, const LightInfo& lightInfo,
John Reck1bcacfd2017-11-03 10:12:19 -070078 const std::vector<sp<RenderNode>>& renderNodes,
79 FrameInfoVisualizer* profiler) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050080 sk_sp<SkSurface> backBuffer = mVkSurface->getBackBufferSurface();
81 if (backBuffer.get() == nullptr) {
Stan Iliev500a0c32016-10-26 10:30:09 -040082 return false;
83 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050084 SkiaPipeline::updateLighting(lightGeometry, lightInfo);
Greg Danielc4076782019-01-08 16:01:18 -050085 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds,
86 backBuffer, mVkSurface->preTransform());
Stan Iliev14211aa2019-01-14 12:29:30 -050087 ShaderCache::get().onVkFrameFlushed(mRenderThread.getGrContext());
Stan Iliev500a0c32016-10-26 10:30:09 -040088 layerUpdateQueue->clear();
Matt Sarettcf2c05c2016-10-26 11:03:23 -040089
90 // Draw visual debugging features
John Reck1bcacfd2017-11-03 10:12:19 -070091 if (CC_UNLIKELY(Properties::showDirtyRegions ||
92 ProfileType::None != Properties::getProfileType())) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050093 SkCanvas* profileCanvas = backBuffer->getCanvas();
Matt Sarettcf2c05c2016-10-26 11:03:23 -040094 SkiaProfileRenderer profileRenderer(profileCanvas);
95 profiler->draw(profileRenderer);
96 profileCanvas->flush();
97 }
98
Matt Sarett4bda6bf2016-11-07 15:43:41 -050099 // Log memory statistics
100 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
101 dumpResourceCacheUsage();
102 }
103
Stan Iliev500a0c32016-10-26 10:30:09 -0400104 return true;
105}
106
John Reck1bcacfd2017-11-03 10:12:19 -0700107bool SkiaVulkanPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
108 FrameInfo* currentFrameInfo, bool* requireSwap) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400109 *requireSwap = drew;
110
111 // Even if we decided to cancel the frame, from the perspective of jank
112 // metrics the frame was swapped at this point
113 currentFrameInfo->markSwapBuffers();
114
115 if (*requireSwap) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500116 mVkManager.swapBuffers(mVkSurface);
Stan Iliev500a0c32016-10-26 10:30:09 -0400117 }
118
Stan Iliev500a0c32016-10-26 10:30:09 -0400119 return *requireSwap;
120}
121
Stan Iliev500a0c32016-10-26 10:30:09 -0400122DeferredLayerUpdater* SkiaVulkanPipeline::createTextureLayer() {
Stan Iliev981afe72019-02-13 14:24:33 -0500123 mRenderThread.requireVkContext();
Greg Daniel8cd3edf2017-01-09 14:15:41 -0500124
Stan Iliev564ca3e2018-09-04 22:00:00 +0000125 return new DeferredLayerUpdater(mRenderThread.renderState());
Stan Iliev500a0c32016-10-26 10:30:09 -0400126}
127
John Reck1bcacfd2017-11-03 10:12:19 -0700128void SkiaVulkanPipeline::onStop() {}
Stan Iliev500a0c32016-10-26 10:30:09 -0400129
John Reck848f6512018-12-03 13:26:43 -0800130bool SkiaVulkanPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior,
John Reck1bcacfd2017-11-03 10:12:19 -0700131 ColorMode colorMode) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500132 if (mVkSurface) {
133 mVkManager.destroySurface(mVkSurface);
134 mVkSurface = nullptr;
Stan Iliev500a0c32016-10-26 10:30:09 -0400135 }
136
Peiyong Lin3bff1352018-12-11 07:56:07 -0800137 setSurfaceColorProperties(colorMode);
Stan Iliev500a0c32016-10-26 10:30:09 -0400138 if (surface) {
Stan Iliev981afe72019-02-13 14:24:33 -0500139 mRenderThread.requireVkContext();
Peiyong Lin3bff1352018-12-11 07:56:07 -0800140 mVkSurface = mVkManager.createSurface(surface, colorMode, mSurfaceColorSpace,
Stan Iliev981afe72019-02-13 14:24:33 -0500141 mSurfaceColorType, mRenderThread.getGrContext());
Greg Daniel031b81b2018-10-02 14:47:22 -0400142 }
143
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500144 return mVkSurface != nullptr;
Stan Iliev500a0c32016-10-26 10:30:09 -0400145}
146
147bool SkiaVulkanPipeline::isSurfaceReady() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500148 return CC_UNLIKELY(mVkSurface != nullptr);
Stan Iliev500a0c32016-10-26 10:30:09 -0400149}
150
151bool SkiaVulkanPipeline::isContextReady() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500152 return CC_LIKELY(mVkManager.hasVkContext());
Stan Iliev500a0c32016-10-26 10:30:09 -0400153}
154
155void SkiaVulkanPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
Chris Blume5f1ac2b2018-11-05 16:10:39 -0800156 VkInteropFunctorDrawable::vkInvokeFunctor(functor);
Stan Iliev500a0c32016-10-26 10:30:09 -0400157}
158
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400159sk_sp<Bitmap> SkiaVulkanPipeline::allocateHardwareBitmap(renderthread::RenderThread& renderThread,
John Reck1bcacfd2017-11-03 10:12:19 -0700160 SkBitmap& skBitmap) {
Derek Sollenberger6e35e632019-01-22 13:56:25 -0500161 LOG_ALWAYS_FATAL("Unimplemented");
162 return nullptr;
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400163}
164
Stan Iliev90276c82019-02-03 18:01:02 -0500165void SkiaVulkanPipeline::onContextDestroyed() {
166 if (mVkSurface) {
167 mVkManager.destroySurface(mVkSurface);
168 mVkSurface = nullptr;
169 }
170}
171
Stan Iliev500a0c32016-10-26 10:30:09 -0400172} /* namespace skiapipeline */
173} /* namespace uirenderer */
174} /* namespace android */