SkiaVulkanPipeline.cpp revision 4bda6bfaa6b8cb775f18f2453720d05f4cb29152
1500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev/* 2500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev * Copyright (C) 2016 The Android Open Source Project 3500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev * 4500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev * Licensed under the Apache License, Version 2.0 (the "License"); 5500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev * you may not use this file except in compliance with the License. 6500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev * You may obtain a copy of the License at 7500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev * 8500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev * http://www.apache.org/licenses/LICENSE-2.0 9500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev * 10500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev * Unless required by applicable law or agreed to in writing, software 11500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev * distributed under the License is distributed on an "AS IS" BASIS, 12500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev * See the License for the specific language governing permissions and 14500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev * limitations under the License. 15500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev */ 16500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 17500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include "SkiaVulkanPipeline.h" 18500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 19500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include "DeferredLayerUpdater.h" 20500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include "renderthread/EglManager.h" // needed for Frame 21500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include "Readback.h" 22500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include "renderstate/RenderState.h" 23cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett#include "SkiaPipeline.h" 24cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett#include "SkiaProfileRenderer.h" 25500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 26500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include <SkTypes.h> 27500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include <WindowContextFactory_android.h> 28500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include <VulkanWindowContext.h> 29500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 30500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include <android/native_window.h> 31500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include <cutils/properties.h> 32500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev#include <strings.h> 33500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 34500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievusing namespace android::uirenderer::renderthread; 35500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievusing namespace sk_app; 36500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 37500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievnamespace android { 38500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievnamespace uirenderer { 39500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievnamespace skiapipeline { 40500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 41500a0c30d4dcd012218c3e44a62926a1c34a259fStan IlievMakeCurrentResult SkiaVulkanPipeline::makeCurrent() { 42500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev return (mWindowContext != nullptr) ? 43500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev MakeCurrentResult::AlreadyCurrent : MakeCurrentResult::Failed; 44500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev} 45500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 46500a0c30d4dcd012218c3e44a62926a1c34a259fStan IlievFrame SkiaVulkanPipeline::getFrame() { 47500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev LOG_ALWAYS_FATAL_IF(mWindowContext == nullptr, "Tried to draw into null vulkan context!"); 48500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev mBackbuffer = mWindowContext->getBackbufferSurface(); 49500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev if (mBackbuffer.get() == nullptr) { 50500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev // try recreating the context? 51500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev SkDebugf("failed to get backbuffer"); 52500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev return Frame(-1, -1, 0); 53500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev } 54500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 55500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev // TODO: support buffer age if Vulkan API can do it 56500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev Frame frame(mBackbuffer->width(), mBackbuffer->height(), 0); 57500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev return frame; 58500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev} 59500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 60500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievbool SkiaVulkanPipeline::draw(const Frame& frame, const SkRect& screenDirty, 61500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev const SkRect& dirty, 62500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev const FrameBuilder::LightGeometry& lightGeometry, 63500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev LayerUpdateQueue* layerUpdateQueue, 64500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev const Rect& contentDrawBounds, bool opaque, 65500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev const BakedOpRenderer::LightInfo& lightInfo, 66500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev const std::vector<sp<RenderNode>>& renderNodes, 67500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev FrameInfoVisualizer* profiler) { 68500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 69500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev if (mBackbuffer.get() == nullptr) { 70500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev return false; 71500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev } 72500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev renderFrame(*layerUpdateQueue, dirty, renderNodes, opaque, contentDrawBounds, mBackbuffer); 73500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev layerUpdateQueue->clear(); 74cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett 75cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett // Draw visual debugging features 76cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett if (CC_UNLIKELY(Properties::showDirtyRegions 77cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett || ProfileType::None == Properties::getProfileType())) { 78cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett SkCanvas* profileCanvas = mBackbuffer->getCanvas(); 79cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett SkiaProfileRenderer profileRenderer(profileCanvas); 80cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett profiler->draw(profileRenderer); 81cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett profileCanvas->flush(); 82cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett } 83cf2c05c652190ddc66f873192c17d193478138a1Matt Sarett 844bda6bfaa6b8cb775f18f2453720d05f4cb29152Matt Sarett // Log memory statistics 854bda6bfaa6b8cb775f18f2453720d05f4cb29152Matt Sarett if (CC_UNLIKELY(Properties::debugLevel != kDebugDisabled)) { 864bda6bfaa6b8cb775f18f2453720d05f4cb29152Matt Sarett dumpResourceCacheUsage(); 874bda6bfaa6b8cb775f18f2453720d05f4cb29152Matt Sarett } 884bda6bfaa6b8cb775f18f2453720d05f4cb29152Matt Sarett 89500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev return true; 90500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev} 91500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 92500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievbool SkiaVulkanPipeline::swapBuffers(const Frame& frame, bool drew, 93500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev const SkRect& screenDirty, FrameInfo* currentFrameInfo, bool* requireSwap) { 94500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 95500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev *requireSwap = drew; 96500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 97500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev // Even if we decided to cancel the frame, from the perspective of jank 98500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev // metrics the frame was swapped at this point 99500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev currentFrameInfo->markSwapBuffers(); 100500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 101500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev if (*requireSwap) { 102500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev mWindowContext->swapBuffers(); 103500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev } 104500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 105500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev mBackbuffer.reset(); 106500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 107500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev return *requireSwap; 108500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev} 109500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 110500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievbool SkiaVulkanPipeline::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) { 111500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev // TODO: implement copyLayerInto for vulkan. 112500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev return false; 113500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev} 114500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 115500a0c30d4dcd012218c3e44a62926a1c34a259fStan IlievDeferredLayerUpdater* SkiaVulkanPipeline::createTextureLayer() { 116500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev Layer* layer = new Layer(mRenderThread.renderState(), 0, 0); 117500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev return new DeferredLayerUpdater(layer); 118500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev} 119500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 120500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievvoid SkiaVulkanPipeline::onStop() { 121500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev} 122500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 123500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievbool SkiaVulkanPipeline::setSurface(Surface* surface, SwapBehavior swapBehavior) { 124500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 125500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev if (mWindowContext) { 126500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev delete mWindowContext; 127500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev mWindowContext = nullptr; 128500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev } 129500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 130500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev if (surface) { 131500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev DisplayParams displayParams; 132500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev mWindowContext = window_context_factory::NewVulkanForAndroid(surface, displayParams); 133500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev if (mWindowContext) { 134500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev DeviceInfo::initialize(mWindowContext->getGrContext()->caps()->maxRenderTargetSize()); 135500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev } 136500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev } 137500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 138500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 139500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev // this doesn't work for if there is more than one CanvasContext available at one time! 140500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev mRenderThread.setGrContext(mWindowContext ? mWindowContext->getGrContext() : nullptr); 141500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 142500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev return mWindowContext != nullptr; 143500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev} 144500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 145500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievbool SkiaVulkanPipeline::isSurfaceReady() { 146500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev return CC_LIKELY(mWindowContext != nullptr) && mWindowContext->isValid(); 147500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev} 148500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 149500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievbool SkiaVulkanPipeline::isContextReady() { 150500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev return CC_LIKELY(mWindowContext != nullptr); 151500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev} 152500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 153500a0c30d4dcd012218c3e44a62926a1c34a259fStan Ilievvoid SkiaVulkanPipeline::invokeFunctor(const RenderThread& thread, Functor* functor) { 154500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev // TODO: we currently don't support OpenGL WebView's 155500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext; 156500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev (*functor)(mode, nullptr); 157500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev} 158500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev 159500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev} /* namespace skiapipeline */ 160500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev} /* namespace uirenderer */ 161500a0c30d4dcd012218c3e44a62926a1c34a259fStan Iliev} /* namespace android */ 162