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