Skip to content

Commit

Permalink
Account for GrVkBackendContext assuming ownership of the VkInstance a…
Browse files Browse the repository at this point in the history
…nd VkDevice. (flutter#3604)
  • Loading branch information
chinmaygarde authored Apr 18, 2017
1 parent ab189d7 commit ad64948
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 3 deletions.
6 changes: 5 additions & 1 deletion vulkan/vulkan_application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,14 @@ uint32_t VulkanApplication::GetAPIVersion() const {
return api_version_;
}

const VulkanHandle<VkInstance>& VulkanApplication::GetInstance() {
const VulkanHandle<VkInstance>& VulkanApplication::GetInstance() const {
return instance_;
}

void VulkanApplication::ReleaseInstanceOwnership() {
instance_.ReleaseOwnership();
}

std::vector<VkPhysicalDevice> VulkanApplication::GetPhysicalDevices() const {
if (!IsValid()) {
return {};
Expand Down
4 changes: 3 additions & 1 deletion vulkan/vulkan_application.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ class VulkanApplication {

uint32_t GetAPIVersion() const;

const VulkanHandle<VkInstance>& GetInstance();
const VulkanHandle<VkInstance>& GetInstance() const;

void ReleaseInstanceOwnership();

std::unique_ptr<VulkanDevice> AcquireFirstCompatibleLogicalDevice() const;

Expand Down
4 changes: 4 additions & 0 deletions vulkan/vulkan_device.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ const VulkanHandle<VkDevice>& VulkanDevice::GetHandle() const {
return device_;
}

void VulkanDevice::ReleaseDeviceOwnership() {
device_.ReleaseOwnership();
}

const VulkanHandle<VkPhysicalDevice>& VulkanDevice::GetPhysicalDeviceHandle()
const {
return physical_device_;
Expand Down
2 changes: 2 additions & 0 deletions vulkan/vulkan_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class VulkanDevice {

uint32_t GetGraphicsQueueIndex() const;

void ReleaseDeviceOwnership();

FTL_WARN_UNUSED_RESULT
bool GetSurfaceCapabilities(const VulkanSurface& surface,
VkSurfaceCapabilitiesKHR* capabilities) const;
Expand Down
5 changes: 5 additions & 0 deletions vulkan/vulkan_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class VulkanHandle {

operator Handle() const { return handle_; }

/// Relinquish responsibility of collecting the underlying handle when this
/// object is collected. It is the responsibility of the caller to ensure that
/// the lifetime of the handle extends past the lifetime of this object.
void ReleaseOwnership() { disposer_ = nullptr; }

private:
Handle handle_;
Disposer disposer_;
Expand Down
5 changes: 5 additions & 0 deletions vulkan/vulkan_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ sk_sp<GrVkBackendContext> VulkanWindow::CreateSkiaBackendContext() {
return nullptr;
}

// The Skia backend context takes ownership of the device and the instance.
// Make sure we release our ownership now.
logical_device_->ReleaseDeviceOwnership();
application_->ReleaseInstanceOwnership();

auto context = sk_make_sp<GrVkBackendContext>();
context->fInstance = application_->GetInstance();
context->fPhysicalDevice = logical_device_->GetPhysicalDeviceHandle();
Expand Down
4 changes: 3 additions & 1 deletion vulkan/vulkan_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ class VulkanWindow {
private:
bool valid_;
ftl::RefPtr<VulkanProcTable> vk;
// Note: The order of objects here is important because the
// GrVkBackendContext assumes ownership of the device and instance handles.
sk_sp<GrVkBackendContext> skia_vk_backend_context_;
std::unique_ptr<VulkanApplication> application_;
std::unique_ptr<VulkanDevice> logical_device_;
std::unique_ptr<VulkanSurface> surface_;
std::unique_ptr<VulkanSwapchain> swapchain_;
sk_sp<GrVkBackendContext> skia_vk_backend_context_;
sk_sp<GrContext> skia_gr_context_;

bool CreateSkiaGrContext();
Expand Down

0 comments on commit ad64948

Please sign in to comment.