Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GrVkSemaphore.h" |
| 9 | |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 10 | #include "GrBackendSemaphore.h" |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 11 | #include "GrVkGpu.h" |
| 12 | #include "GrVkUtil.h" |
| 13 | |
| 14 | #ifdef VK_USE_PLATFORM_WIN32_KHR |
| 15 | // windows wants to define this as CreateSemaphoreA or CreateSemaphoreW |
| 16 | #undef CreateSemaphore |
| 17 | #endif |
| 18 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame^] | 19 | SkMutex GrVkSemaphore::Resource::gMutex; |
| 20 | |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 21 | sk_sp<GrVkSemaphore> GrVkSemaphore::Make(const GrVkGpu* gpu, bool isOwned) { |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 22 | VkSemaphoreCreateInfo createInfo; |
Greg Daniel | 8761e0c | 2017-07-20 16:36:01 -0400 | [diff] [blame] | 23 | memset(&createInfo, 0, sizeof(VkSemaphoreCreateInfo)); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 24 | createInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; |
| 25 | createInfo.pNext = nullptr; |
| 26 | createInfo.flags = 0; |
| 27 | VkSemaphore semaphore = VK_NULL_HANDLE; |
| 28 | GR_VK_CALL_ERRCHECK(gpu->vkInterface(), |
| 29 | CreateSemaphore(gpu->device(), &createInfo, nullptr, &semaphore)); |
| 30 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame^] | 31 | return sk_sp<GrVkSemaphore>(new GrVkSemaphore(gpu, semaphore, false, false, isOwned)); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 32 | } |
| 33 | |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 34 | sk_sp<GrVkSemaphore> GrVkSemaphore::MakeWrapped(const GrVkGpu* gpu, |
| 35 | VkSemaphore semaphore, |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame^] | 36 | WrapType wrapType, |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 37 | GrWrapOwnership ownership) { |
| 38 | if (VK_NULL_HANDLE == semaphore) { |
| 39 | return nullptr; |
| 40 | } |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame^] | 41 | bool prohibitSignal = WrapType::kWillWait == wrapType; |
| 42 | bool prohibitWait = WrapType::kWillSignal == wrapType; |
| 43 | return sk_sp<GrVkSemaphore>(new GrVkSemaphore(gpu, semaphore, prohibitSignal, prohibitWait, |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 44 | kBorrow_GrWrapOwnership != ownership)); |
| 45 | } |
| 46 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame^] | 47 | GrVkSemaphore::GrVkSemaphore(const GrVkGpu* gpu, VkSemaphore semaphore, bool prohibitSignal, |
| 48 | bool prohibitWait, bool isOwned) |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 49 | : INHERITED(gpu) { |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame^] | 50 | fResource = new Resource(semaphore, prohibitSignal, prohibitWait, isOwned); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | GrVkSemaphore::~GrVkSemaphore() { |
| 54 | if (fGpu) { |
| 55 | fResource->unref(static_cast<const GrVkGpu*>(fGpu)); |
| 56 | } else { |
| 57 | fResource->unrefAndAbandon(); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void GrVkSemaphore::Resource::freeGPUData(const GrVkGpu* gpu) const { |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 62 | if (fIsOwned) { |
| 63 | GR_VK_CALL(gpu->vkInterface(), |
| 64 | DestroySemaphore(gpu->device(), fSemaphore, nullptr)); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void GrVkSemaphore::setBackendSemaphore(GrBackendSemaphore* backendSemaphore) const { |
| 69 | backendSemaphore->initVulkan(fResource->semaphore()); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 70 | } |
| 71 | |