Skip to content

Commit

Permalink
Introduce VulkanProvider
Browse files Browse the repository at this point in the history
For more consistent access to base vulkan functionality owned
elsewhere.
  • Loading branch information
cdotstout committed Jan 27, 2018
1 parent 521c73d commit fb9782a
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 34 deletions.
55 changes: 30 additions & 25 deletions content_handler/vulkan_surface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

namespace flutter_runner {

VulkanSurface::VulkanSurface(vulkan::VulkanProcTable& p_vk,
VulkanSurface::VulkanSurface(vulkan::VulkanProvider& vulkan_provider,
sk_sp<GrContext> context,
sk_sp<GrVkBackendContext> 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;
Expand Down Expand Up @@ -94,8 +94,8 @@ vulkan::VulkanHandle<VkSemaphore> 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<VkSemaphore>();
}
Expand All @@ -107,15 +107,17 @@ vulkan::VulkanHandle<VkSemaphore> VulkanSurface::SemaphoreFromEvent(
.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_FUCHSIA_FENCE_BIT_KHR,
.handle = static_cast<uint32_t>(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<VkSemaphore>();
}

return vulkan::VulkanHandle<VkSemaphore>(
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);
});
}

Expand Down Expand Up @@ -171,23 +173,23 @@ bool VulkanSurface::AllocateDeviceMemory(sk_sp<GrContext> 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++) {
Expand All @@ -205,20 +207,23 @@ bool VulkanSurface::AllocateDeviceMemory(sk_sp<GrContext> 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;
}

Expand All @@ -229,8 +234,8 @@ bool VulkanSurface::AllocateDeviceMemory(sk_sp<GrContext> 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;
}
Expand Down
5 changes: 3 additions & 2 deletions content_handler/vulkan_surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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<GrContext> context,
sk_sp<GrVkBackendContext> backend_context,
scenic_lib::Session* session,
Expand Down Expand Up @@ -56,7 +57,7 @@ class VulkanSurface : public flow::SceneUpdateContext::SurfaceProducerSurface,
GrBackendSemaphore GetAcquireSemaphore() const;

private:
vulkan::VulkanProcTable& vk_;
vulkan::VulkanProvider& vulkan_provider_;
sk_sp<GrVkBackendContext> backend_context_;
scenic_lib::Session* session_;
vulkan::VulkanHandle<VkImage> vk_image_;
Expand Down
6 changes: 3 additions & 3 deletions content_handler/vulkan_surface_pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

namespace flutter_runner {

VulkanSurfacePool::VulkanSurfacePool(vulkan::VulkanProcTable& p_vk,
VulkanSurfacePool::VulkanSurfacePool(vulkan::VulkanProvider& vulkan_provider,
sk_sp<GrContext> context,
sk_sp<GrVkBackendContext> 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) {}
Expand Down Expand Up @@ -85,7 +85,7 @@ void VulkanSurfacePool::SubmitSurface(
std::unique_ptr<VulkanSurface> VulkanSurfacePool::CreateSurface(
const SkISize& size) {
auto surface = std::make_unique<VulkanSurface>(
vk_, context_, backend_context_, mozart_session_, size);
vulkan_provider_, context_, backend_context_, mozart_session_, size);
if (!surface->IsValid()) {
return nullptr;
}
Expand Down
4 changes: 2 additions & 2 deletions content_handler/vulkan_surface_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<GrContext> context,
sk_sp<GrVkBackendContext> backend_context,
scenic_lib::Session* mozart_session);
Expand Down Expand Up @@ -50,7 +50,7 @@ class VulkanSurfacePool {
}
};

vulkan::VulkanProcTable& vk_;
vulkan::VulkanProvider& vulkan_provider_;
sk_sp<GrContext> context_;
sk_sp<GrVkBackendContext> backend_context_;
scenic_lib::Session* mozart_session_;
Expand Down
2 changes: 1 addition & 1 deletion content_handler/vulkan_surface_producer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ bool VulkanSurfaceProducer::Initialize(scenic_lib::Session* mozart_session) {
vulkan::kGrCacheMaxByteSize);

surface_pool_ = std::make_unique<VulkanSurfacePool>(
*vk_, context_, backend_context_, mozart_session);
*this, context_, backend_context_, mozart_session);

return true;
}
Expand Down
10 changes: 9 additions & 1 deletion content_handler/vulkan_surface_producer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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);

Expand All @@ -42,6 +44,12 @@ class VulkanSurfaceProducer : public flow::SceneUpdateContext::SurfaceProducer {
surfaces);

private:
// VulkanProvider
const vulkan::VulkanProcTable& vk() override { return *vk_.get(); }
const vulkan::VulkanHandle<VkDevice>& 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
Expand Down
20 changes: 20 additions & 0 deletions vulkan/vulkan_provider.h
Original file line number Diff line number Diff line change
@@ -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<VkDevice>& vk_device() = 0;
};

} // namespace vulkan

#endif // FLUTTER_VULKAN_VULKAN_PROVIDER_H_

0 comments on commit fb9782a

Please sign in to comment.