SkiaVulkanPipeline.cpp revision 0e3cba31460e0698def0310003b7d291f1174afa
1/*
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"
20#include "renderthread/EglManager.h" // needed for Frame
21#include "Readback.h"
22#include "renderstate/RenderState.h"
23#include "SkiaPipeline.h"
24#include "SkiaProfileRenderer.h"
25
26#include <SkSurface.h>
27#include <SkTypes.h>
28
29#include <GrContext.h>
30#include <GrTypes.h>
31#include <vk/GrVkTypes.h>
32
33#include <android/native_window.h>
34#include <cutils/properties.h>
35#include <strings.h>
36
37using namespace android::uirenderer::renderthread;
38
39namespace android {
40namespace uirenderer {
41namespace skiapipeline {
42
43SkiaVulkanPipeline::SkiaVulkanPipeline(renderthread::RenderThread& thread)
44        : SkiaPipeline(thread)
45        , mVkManager(thread.vulkanManager()) {}
46
47MakeCurrentResult SkiaVulkanPipeline::makeCurrent() {
48    return MakeCurrentResult::AlreadyCurrent;
49}
50
51Frame SkiaVulkanPipeline::getFrame() {
52    LOG_ALWAYS_FATAL_IF(mVkSurface == nullptr,
53                "drawRenderNode called on a context with no surface!");
54
55    SkSurface* backBuffer = mVkManager.getBackbufferSurface(mVkSurface);
56    if (backBuffer == nullptr) {
57        SkDebugf("failed to get backbuffer");
58        return Frame(-1, -1, 0);
59    }
60
61    // TODO: support buffer age if Vulkan API can do it
62    Frame frame(backBuffer->width(), backBuffer->height(), 0);
63    return frame;
64}
65
66bool SkiaVulkanPipeline::draw(const Frame& frame, const SkRect& screenDirty,
67        const SkRect& dirty,
68        const FrameBuilder::LightGeometry& lightGeometry,
69        LayerUpdateQueue* layerUpdateQueue,
70        const Rect& contentDrawBounds, bool opaque,
71        const BakedOpRenderer::LightInfo& lightInfo,
72        const std::vector<sp<RenderNode>>& renderNodes,
73        FrameInfoVisualizer* profiler) {
74
75    sk_sp<SkSurface> backBuffer = mVkSurface->getBackBufferSurface();
76    if (backBuffer.get() == nullptr) {
77        return false;
78    }
79    SkiaPipeline::updateLighting(lightGeometry, lightInfo);
80    renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, backBuffer);
81    layerUpdateQueue->clear();
82
83    // Draw visual debugging features
84    if (CC_UNLIKELY(Properties::showDirtyRegions
85            || ProfileType::None != Properties::getProfileType())) {
86        SkCanvas* profileCanvas = backBuffer->getCanvas();
87        SkiaProfileRenderer profileRenderer(profileCanvas);
88        profiler->draw(profileRenderer);
89        profileCanvas->flush();
90    }
91
92    // Log memory statistics
93    if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) {
94        dumpResourceCacheUsage();
95    }
96
97    return true;
98}
99
100bool SkiaVulkanPipeline::swapBuffers(const Frame& frame, bool drew,
101        const SkRect& screenDirty, FrameInfo* currentFrameInfo, bool* requireSwap) {
102
103    *requireSwap = drew;
104
105    // Even if we decided to cancel the frame, from the perspective of jank
106    // metrics the frame was swapped at this point
107    currentFrameInfo->markSwapBuffers();
108
109    if (*requireSwap) {
110        mVkManager.swapBuffers(mVkSurface);
111    }
112
113    return *requireSwap;
114}
115
116bool SkiaVulkanPipeline::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
117    // TODO: implement copyLayerInto for vulkan.
118    return false;
119}
120
121DeferredLayerUpdater* SkiaVulkanPipeline::createTextureLayer() {
122    mVkManager.initialize();
123    Layer* layer = new Layer(mRenderThread.renderState(), 0, 0);
124    return new DeferredLayerUpdater(layer);
125}
126
127void SkiaVulkanPipeline::onStop() {
128}
129
130bool SkiaVulkanPipeline::setSurface(Surface* surface, SwapBehavior swapBehavior) {
131    if (mVkSurface) {
132        mVkManager.destroySurface(mVkSurface);
133        mVkSurface = nullptr;
134    }
135
136    if (surface) {
137        mVkSurface = mVkManager.createSurface(surface);
138    }
139
140    return mVkSurface != nullptr;
141}
142
143bool SkiaVulkanPipeline::isSurfaceReady() {
144    return CC_UNLIKELY(mVkSurface != nullptr);
145}
146
147bool SkiaVulkanPipeline::isContextReady() {
148    return CC_LIKELY(mVkManager.hasVkContext());
149}
150
151void SkiaVulkanPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
152    // TODO: we currently don't support OpenGL WebView's
153    DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
154    (*functor)(mode, nullptr);
155}
156
157} /* namespace skiapipeline */
158} /* namespace uirenderer */
159} /* namespace android */
160