blob: 53495a7d62c0009c036dd118057c03534d73c206 [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)
John Reck1bcacfd2017-11-03 10:12:19 -070045 : SkiaPipeline(thread), mVkManager(thread.vulkanManager()) {}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050046
Stan Iliev500a0c32016-10-26 10:30:09 -040047MakeCurrentResult SkiaVulkanPipeline::makeCurrent() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050048 return MakeCurrentResult::AlreadyCurrent;
Stan Iliev500a0c32016-10-26 10:30:09 -040049}
50
51Frame SkiaVulkanPipeline::getFrame() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050052 LOG_ALWAYS_FATAL_IF(mVkSurface == nullptr,
John Reck1bcacfd2017-11-03 10:12:19 -070053 "drawRenderNode called on a context with no surface!");
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050054
Stan Iliev305e13a2018-11-13 11:14:48 -050055 SkSurface* backBuffer = mVkManager.getBackbufferSurface(&mVkSurface);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050056 if (backBuffer == nullptr) {
Stan Iliev500a0c32016-10-26 10:30:09 -040057 SkDebugf("failed to get backbuffer");
58 return Frame(-1, -1, 0);
59 }
60
Greg Danielc4076782019-01-08 16:01:18 -050061 Frame frame(mVkSurface->windowWidth(), mVkSurface->windowHeight(),
62 mVkManager.getAge(mVkSurface));
Stan Iliev500a0c32016-10-26 10:30:09 -040063 return frame;
64}
65
John Reck1bcacfd2017-11-03 10:12:19 -070066bool SkiaVulkanPipeline::draw(const Frame& frame, const SkRect& screenDirty, const SkRect& dirty,
John Reckd9d7f122018-05-03 14:40:56 -070067 const LightGeometry& lightGeometry,
John Reck1bcacfd2017-11-03 10:12:19 -070068 LayerUpdateQueue* layerUpdateQueue, const Rect& contentDrawBounds,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070069 bool opaque, const LightInfo& lightInfo,
John Reck1bcacfd2017-11-03 10:12:19 -070070 const std::vector<sp<RenderNode>>& renderNodes,
71 FrameInfoVisualizer* profiler) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050072 sk_sp<SkSurface> backBuffer = mVkSurface->getBackBufferSurface();
73 if (backBuffer.get() == nullptr) {
Stan Iliev500a0c32016-10-26 10:30:09 -040074 return false;
75 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050076 SkiaPipeline::updateLighting(lightGeometry, lightInfo);
Greg Danielc4076782019-01-08 16:01:18 -050077 renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds,
78 backBuffer, mVkSurface->preTransform());
Stan Iliev14211aa2019-01-14 12:29:30 -050079 ShaderCache::get().onVkFrameFlushed(mRenderThread.getGrContext());
Stan Iliev500a0c32016-10-26 10:30:09 -040080 layerUpdateQueue->clear();
Matt Sarettcf2c05c2016-10-26 11:03:23 -040081
82 // Draw visual debugging features
John Reck1bcacfd2017-11-03 10:12:19 -070083 if (CC_UNLIKELY(Properties::showDirtyRegions ||
84 ProfileType::None != Properties::getProfileType())) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050085 SkCanvas* profileCanvas = backBuffer->getCanvas();
Matt Sarettcf2c05c2016-10-26 11:03:23 -040086 SkiaProfileRenderer profileRenderer(profileCanvas);
87 profiler->draw(profileRenderer);
88 profileCanvas->flush();
89 }
90
Matt Sarett4bda6bf2016-11-07 15:43:41 -050091 // Log memory statistics
92 if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
93 dumpResourceCacheUsage();
94 }
95
Stan Iliev500a0c32016-10-26 10:30:09 -040096 return true;
97}
98
John Reck1bcacfd2017-11-03 10:12:19 -070099bool SkiaVulkanPipeline::swapBuffers(const Frame& frame, bool drew, const SkRect& screenDirty,
100 FrameInfo* currentFrameInfo, bool* requireSwap) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400101 *requireSwap = drew;
102
103 // Even if we decided to cancel the frame, from the perspective of jank
104 // metrics the frame was swapped at this point
105 currentFrameInfo->markSwapBuffers();
106
107 if (*requireSwap) {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500108 mVkManager.swapBuffers(mVkSurface);
Stan Iliev500a0c32016-10-26 10:30:09 -0400109 }
110
Stan Iliev500a0c32016-10-26 10:30:09 -0400111 return *requireSwap;
112}
113
Stan Iliev500a0c32016-10-26 10:30:09 -0400114DeferredLayerUpdater* SkiaVulkanPipeline::createTextureLayer() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500115 mVkManager.initialize();
Greg Daniel8cd3edf2017-01-09 14:15:41 -0500116
Stan Iliev564ca3e2018-09-04 22:00:00 +0000117 return new DeferredLayerUpdater(mRenderThread.renderState());
Stan Iliev500a0c32016-10-26 10:30:09 -0400118}
119
John Reck1bcacfd2017-11-03 10:12:19 -0700120void SkiaVulkanPipeline::onStop() {}
Stan Iliev500a0c32016-10-26 10:30:09 -0400121
John Reck848f6512018-12-03 13:26:43 -0800122bool SkiaVulkanPipeline::setSurface(ANativeWindow* surface, SwapBehavior swapBehavior,
John Reck1bcacfd2017-11-03 10:12:19 -0700123 ColorMode colorMode) {
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
Peiyong Lin3bff1352018-12-11 07:56:07 -0800129 setSurfaceColorProperties(colorMode);
Stan Iliev500a0c32016-10-26 10:30:09 -0400130 if (surface) {
Peiyong Lin3bff1352018-12-11 07:56:07 -0800131 mVkSurface = mVkManager.createSurface(surface, colorMode, mSurfaceColorSpace,
132 mSurfaceColorGamut, mSurfaceColorType);
Greg Daniel031b81b2018-10-02 14:47:22 -0400133 }
134
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500135 return mVkSurface != nullptr;
Stan Iliev500a0c32016-10-26 10:30:09 -0400136}
137
138bool SkiaVulkanPipeline::isSurfaceReady() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500139 return CC_UNLIKELY(mVkSurface != nullptr);
Stan Iliev500a0c32016-10-26 10:30:09 -0400140}
141
142bool SkiaVulkanPipeline::isContextReady() {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500143 return CC_LIKELY(mVkManager.hasVkContext());
Stan Iliev500a0c32016-10-26 10:30:09 -0400144}
145
146void SkiaVulkanPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
Chris Blume5f1ac2b2018-11-05 16:10:39 -0800147 VkInteropFunctorDrawable::vkInvokeFunctor(functor);
Stan Iliev500a0c32016-10-26 10:30:09 -0400148}
149
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400150sk_sp<Bitmap> SkiaVulkanPipeline::allocateHardwareBitmap(renderthread::RenderThread& renderThread,
John Reck1bcacfd2017-11-03 10:12:19 -0700151 SkBitmap& skBitmap) {
152 // TODO: implement this function for Vulkan pipeline
153 // code below is a hack to avoid crashing because of missing HW Bitmap support
154 sp<GraphicBuffer> buffer = new GraphicBuffer(
155 skBitmap.info().width(), skBitmap.info().height(), PIXEL_FORMAT_RGBA_8888,
156 GraphicBuffer::USAGE_HW_TEXTURE | GraphicBuffer::USAGE_SW_WRITE_NEVER |
157 GraphicBuffer::USAGE_SW_READ_NEVER,
158 std::string("SkiaVulkanPipeline::allocateHardwareBitmap pid [") +
159 std::to_string(getpid()) + "]");
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400160 status_t error = buffer->initCheck();
161 if (error < 0) {
162 ALOGW("SkiaVulkanPipeline::allocateHardwareBitmap() failed in GraphicBuffer.create()");
163 return nullptr;
164 }
Derek Sollenbergere2169482018-11-20 10:57:20 -0500165 return Bitmap::createFrom(buffer, skBitmap.refColorSpace());
Stan Iliev7bc3bc62017-05-24 13:28:36 -0400166}
167
Stan Iliev500a0c32016-10-26 10:30:09 -0400168} /* namespace skiapipeline */
169} /* namespace uirenderer */
170} /* namespace android */