From fb9782a5294e700e45e372da0cd25165697b2e45 Mon Sep 17 00:00:00 2001 From: Craig Stout Date: Thu, 14 Dec 2017 15:20:22 -0800 Subject: [PATCH] Introduce VulkanProvider For more consistent access to base vulkan functionality owned elsewhere. --- content_handler/vulkan_surface.cc | 55 ++++++++++++---------- content_handler/vulkan_surface.h | 5 +- content_handler/vulkan_surface_pool.cc | 6 +-- content_handler/vulkan_surface_pool.h | 4 +- content_handler/vulkan_surface_producer.cc | 2 +- content_handler/vulkan_surface_producer.h | 10 +++- vulkan/vulkan_provider.h | 20 ++++++++ 7 files changed, 68 insertions(+), 34 deletions(-) create mode 100644 vulkan/vulkan_provider.h diff --git a/content_handler/vulkan_surface.cc b/content_handler/vulkan_surface.cc index 9e01b0d13d8..88c46f313b4 100644 --- a/content_handler/vulkan_surface.cc +++ b/content_handler/vulkan_surface.cc @@ -12,12 +12,12 @@ namespace flutter_runner { -VulkanSurface::VulkanSurface(vulkan::VulkanProcTable& p_vk, +VulkanSurface::VulkanSurface(vulkan::VulkanProvider& vulkan_provider, sk_sp context, sk_sp backend_context, scenic_lib::Session* session, const SkISize& size) - : vk_(p_vk), + : vulkan_provider_(vulkan_provider), backend_context_(std::move(backend_context)), session_(session) { ASSERT_IS_GPU_THREAD; @@ -94,8 +94,8 @@ vulkan::VulkanHandle VulkanSurface::SemaphoreFromEvent( .flags = 0, }; - result = VK_CALL_LOG_ERROR(vk_.CreateSemaphore( - backend_context_->fDevice, &create_info, nullptr, &semaphore)); + result = VK_CALL_LOG_ERROR(vulkan_provider_.vk().CreateSemaphore( + vulkan_provider_.vk_device(), &create_info, nullptr, &semaphore)); if (result != VK_SUCCESS) { return vulkan::VulkanHandle(); } @@ -107,15 +107,17 @@ vulkan::VulkanHandle VulkanSurface::SemaphoreFromEvent( .handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_FUCHSIA_FENCE_BIT_KHR, .handle = static_cast(semaphore_event.release())}; - result = VK_CALL_LOG_ERROR(vk_.ImportSemaphoreFuchsiaHandleKHR( - backend_context_->fDevice, &import_info)); + result = + VK_CALL_LOG_ERROR(vulkan_provider_.vk().ImportSemaphoreFuchsiaHandleKHR( + vulkan_provider_.vk_device(), &import_info)); if (result != VK_SUCCESS) { return vulkan::VulkanHandle(); } return vulkan::VulkanHandle( - semaphore, [this](VkSemaphore semaphore) { - vk_.DestroySemaphore(backend_context_->fDevice, semaphore, nullptr); + semaphore, [&vulkan_provider = vulkan_provider_](VkSemaphore semaphore) { + vulkan_provider.vk().DestroySemaphore(vulkan_provider.vk_device(), + semaphore, nullptr); }); } @@ -171,23 +173,23 @@ bool VulkanSurface::AllocateDeviceMemory(sk_sp context, { VkImage vk_image = VK_NULL_HANDLE; - if (VK_CALL_LOG_ERROR(vk_.CreateImage(backend_context_->fDevice, - &image_create_info, nullptr, - &vk_image)) != VK_SUCCESS) { + if (VK_CALL_LOG_ERROR(vulkan_provider_.vk().CreateImage( + vulkan_provider_.vk_device(), &image_create_info, nullptr, + &vk_image)) != VK_SUCCESS) { return false; } - vk_image_ = {vk_image, [this](VkImage image) { - vk_.DestroyImage(backend_context_->fDevice, image, NULL); + vk_image_ = {vk_image, + [& vulkan_provider = vulkan_provider_](VkImage image) { + vulkan_provider.vk().DestroyImage( + vulkan_provider.vk_device(), image, NULL); }}; } // Create the memory. VkMemoryRequirements memory_reqs; - vk_.GetImageMemoryRequirements(backend_context_->fDevice, // - vk_image_, // - &memory_reqs // - ); + vulkan_provider_.vk().GetImageMemoryRequirements(vulkan_provider_.vk_device(), + vk_image_, &memory_reqs); uint32_t memory_type = 0; for (; memory_type < 32; memory_type++) { @@ -205,20 +207,23 @@ bool VulkanSurface::AllocateDeviceMemory(sk_sp context, { VkDeviceMemory vk_memory = VK_NULL_HANDLE; - if (VK_CALL_LOG_ERROR(vk_.AllocateMemory(backend_context_->fDevice, - &alloc_info, NULL, &vk_memory)) != + if (VK_CALL_LOG_ERROR(vulkan_provider_.vk().AllocateMemory( + vulkan_provider_.vk_device(), &alloc_info, NULL, &vk_memory)) != VK_SUCCESS) { return false; } - vk_memory_ = {vk_memory, [this](VkDeviceMemory memory) { - vk_.FreeMemory(backend_context_->fDevice, memory, NULL); + vk_memory_ = {vk_memory, [& vulkan_provider = + vulkan_provider_](VkDeviceMemory memory) { + vulkan_provider.vk().FreeMemory(vulkan_provider.vk_device(), + memory, NULL); }}; } // Bind image memory. - if (VK_CALL_LOG_ERROR(vk_.BindImageMemory( - backend_context_->fDevice, vk_image_, vk_memory_, 0)) != VK_SUCCESS) { + if (VK_CALL_LOG_ERROR(vulkan_provider_.vk().BindImageMemory( + vulkan_provider_.vk_device(), vk_image_, vk_memory_, 0)) != + VK_SUCCESS) { return false; } @@ -229,8 +234,8 @@ bool VulkanSurface::AllocateDeviceMemory(sk_sp context, VkMemoryGetFuchsiaHandleInfoKHR get_handle_info = { VK_STRUCTURE_TYPE_MEMORY_GET_FUCHSIA_HANDLE_INFO_KHR, nullptr, vk_memory_, VK_EXTERNAL_MEMORY_HANDLE_TYPE_FUCHSIA_VMO_BIT_KHR}; - if (VK_CALL_LOG_ERROR(vk_.GetMemoryFuchsiaHandleKHR( - backend_context_->fDevice, &get_handle_info, &vmo_handle)) != + if (VK_CALL_LOG_ERROR(vulkan_provider_.vk().GetMemoryFuchsiaHandleKHR( + vulkan_provider_.vk_device(), &get_handle_info, &vmo_handle)) != VK_SUCCESS) { return false; } diff --git a/content_handler/vulkan_surface.h b/content_handler/vulkan_surface.h index 0f2fecb056f..62b20f25735 100644 --- a/content_handler/vulkan_surface.h +++ b/content_handler/vulkan_surface.h @@ -8,6 +8,7 @@ #include "flutter/flow/scene_update_context.h" #include "flutter/vulkan/vulkan_handle.h" #include "flutter/vulkan/vulkan_proc_table.h" +#include "flutter/vulkan/vulkan_provider.h" #include "lib/fsl/tasks/message_loop.h" #include "lib/fsl/tasks/message_loop_handler.h" #include "lib/fxl/macros.h" @@ -22,7 +23,7 @@ namespace flutter_runner { class VulkanSurface : public flow::SceneUpdateContext::SurfaceProducerSurface, public fsl::MessageLoopHandler { public: - VulkanSurface(vulkan::VulkanProcTable& p_vk, + VulkanSurface(vulkan::VulkanProvider& vulkan_provider, sk_sp context, sk_sp backend_context, scenic_lib::Session* session, @@ -56,7 +57,7 @@ class VulkanSurface : public flow::SceneUpdateContext::SurfaceProducerSurface, GrBackendSemaphore GetAcquireSemaphore() const; private: - vulkan::VulkanProcTable& vk_; + vulkan::VulkanProvider& vulkan_provider_; sk_sp backend_context_; scenic_lib::Session* session_; vulkan::VulkanHandle vk_image_; diff --git a/content_handler/vulkan_surface_pool.cc b/content_handler/vulkan_surface_pool.cc index 24b33fc4de1..af23ef711b7 100644 --- a/content_handler/vulkan_surface_pool.cc +++ b/content_handler/vulkan_surface_pool.cc @@ -10,11 +10,11 @@ namespace flutter_runner { -VulkanSurfacePool::VulkanSurfacePool(vulkan::VulkanProcTable& p_vk, +VulkanSurfacePool::VulkanSurfacePool(vulkan::VulkanProvider& vulkan_provider, sk_sp context, sk_sp backend_context, scenic_lib::Session* mozart_session) - : vk_(p_vk), + : vulkan_provider_(vulkan_provider), context_(std::move(context)), backend_context_(std::move(backend_context)), mozart_session_(mozart_session) {} @@ -85,7 +85,7 @@ void VulkanSurfacePool::SubmitSurface( std::unique_ptr VulkanSurfacePool::CreateSurface( const SkISize& size) { auto surface = std::make_unique( - vk_, context_, backend_context_, mozart_session_, size); + vulkan_provider_, context_, backend_context_, mozart_session_, size); if (!surface->IsValid()) { return nullptr; } diff --git a/content_handler/vulkan_surface_pool.h b/content_handler/vulkan_surface_pool.h index 561876c4e79..52dd3ed6aa4 100644 --- a/content_handler/vulkan_surface_pool.h +++ b/content_handler/vulkan_surface_pool.h @@ -16,7 +16,7 @@ class VulkanSurfacePool { static const size_t kMaxSurfacesOfSameSize = 3; static const size_t kMaxSurfaceAge = 3; - VulkanSurfacePool(vulkan::VulkanProcTable& vk, + VulkanSurfacePool(vulkan::VulkanProvider& vulkan_provider, sk_sp context, sk_sp backend_context, scenic_lib::Session* mozart_session); @@ -50,7 +50,7 @@ class VulkanSurfacePool { } }; - vulkan::VulkanProcTable& vk_; + vulkan::VulkanProvider& vulkan_provider_; sk_sp context_; sk_sp backend_context_; scenic_lib::Session* mozart_session_; diff --git a/content_handler/vulkan_surface_producer.cc b/content_handler/vulkan_surface_producer.cc index b5bb1d61cd4..809269243a5 100644 --- a/content_handler/vulkan_surface_producer.cc +++ b/content_handler/vulkan_surface_producer.cc @@ -111,7 +111,7 @@ bool VulkanSurfaceProducer::Initialize(scenic_lib::Session* mozart_session) { vulkan::kGrCacheMaxByteSize); surface_pool_ = std::make_unique( - *vk_, context_, backend_context_, mozart_session); + *this, context_, backend_context_, mozart_session); return true; } diff --git a/content_handler/vulkan_surface_producer.h b/content_handler/vulkan_surface_producer.h index da1e1718071..4baf265b3d5 100644 --- a/content_handler/vulkan_surface_producer.h +++ b/content_handler/vulkan_surface_producer.h @@ -11,6 +11,7 @@ #include "flutter/vulkan/vulkan_application.h" #include "flutter/vulkan/vulkan_device.h" #include "flutter/vulkan/vulkan_proc_table.h" +#include "flutter/vulkan/vulkan_provider.h" #include "lib/fsl/tasks/message_loop.h" #include "lib/fxl/macros.h" #include "lib/ui/scenic/client/resources.h" @@ -19,7 +20,8 @@ namespace flutter_runner { -class VulkanSurfaceProducer : public flow::SceneUpdateContext::SurfaceProducer { +class VulkanSurfaceProducer : public flow::SceneUpdateContext::SurfaceProducer, + public vulkan::VulkanProvider { public: VulkanSurfaceProducer(scenic_lib::Session* mozart_session); @@ -42,6 +44,12 @@ class VulkanSurfaceProducer : public flow::SceneUpdateContext::SurfaceProducer { surfaces); private: + // VulkanProvider + const vulkan::VulkanProcTable& vk() override { return *vk_.get(); } + const vulkan::VulkanHandle& vk_device() override { + return logical_device_->GetHandle(); + } + // Note: the order here is very important. The proctable must be destroyed // last because it contains the function pointers for VkDestroyDevice and // VkDestroyInstance. The backend context owns the VkDevice and the diff --git a/vulkan/vulkan_provider.h b/vulkan/vulkan_provider.h new file mode 100644 index 00000000000..1de597772b8 --- /dev/null +++ b/vulkan/vulkan_provider.h @@ -0,0 +1,20 @@ +// Copyright 2017 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_VULKAN_VULKAN_PROVIDER_H_ +#define FLUTTER_VULKAN_VULKAN_PROVIDER_H_ + +#include "flutter/vulkan/vulkan_handle.h" + +namespace vulkan { + +class VulkanProvider { + public: + virtual const vulkan::VulkanProcTable& vk() = 0; + virtual const vulkan::VulkanHandle& vk_device() = 0; +}; + +} // namespace vulkan + +#endif // FLUTTER_VULKAN_VULKAN_PROVIDER_H_