SkiaVulkanPipeline.cpp revision cf2c05c652190ddc66f873192c17d193478138a1
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    return true;
85}
86
87bool SkiaVulkanPipeline::swapBuffers(const Frame& frame, bool drew,
88        const SkRect& screenDirty, FrameInfo* currentFrameInfo, bool* requireSwap) {
89
90    *requireSwap = drew;
91
92    // Even if we decided to cancel the frame, from the perspective of jank
93    // metrics the frame was swapped at this point
94    currentFrameInfo->markSwapBuffers();
95
96    if (*requireSwap) {
97        mWindowContext->swapBuffers();
98    }
99
100    mBackbuffer.reset();
101
102    return *requireSwap;
103}
104
105bool SkiaVulkanPipeline::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
106    // TODO: implement copyLayerInto for vulkan.
107    return false;
108}
109
110DeferredLayerUpdater* SkiaVulkanPipeline::createTextureLayer() {
111    Layer* layer = new Layer(mRenderThread.renderState(), 0, 0);
112    return new DeferredLayerUpdater(layer);
113}
114
115void SkiaVulkanPipeline::onStop() {
116}
117
118bool SkiaVulkanPipeline::setSurface(Surface* surface, SwapBehavior swapBehavior) {
119
120    if (mWindowContext) {
121        delete mWindowContext;
122        mWindowContext = nullptr;
123    }
124
125    if (surface) {
126        DisplayParams displayParams;
127        mWindowContext = window_context_factory::NewVulkanForAndroid(surface, displayParams);
128        if (mWindowContext) {
129            DeviceInfo::initialize(mWindowContext->getGrContext()->caps()->maxRenderTargetSize());
130        }
131    }
132
133
134    // this doesn't work for if there is more than one CanvasContext available at one time!
135    mRenderThread.setGrContext(mWindowContext ? mWindowContext->getGrContext() : nullptr);
136
137    return mWindowContext != nullptr;
138}
139
140bool SkiaVulkanPipeline::isSurfaceReady() {
141    return CC_LIKELY(mWindowContext != nullptr) && mWindowContext->isValid();
142}
143
144bool SkiaVulkanPipeline::isContextReady() {
145    return CC_LIKELY(mWindowContext != nullptr);
146}
147
148void SkiaVulkanPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) {
149    // TODO: we currently don't support OpenGL WebView's
150    DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
151    (*functor)(mode, nullptr);
152}
153
154} /* namespace skiapipeline */
155} /* namespace uirenderer */
156} /* namespace android */
157