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 | |
Brian Salomon | 62db8d5 | 2018-08-30 10:37:47 -0400 | [diff] [blame^] | 19 | sk_sp<GrVkSemaphore> GrVkSemaphore::Make(GrVkGpu* gpu, bool isOwned) { |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 20 | VkSemaphoreCreateInfo createInfo; |
Greg Daniel | 8761e0c | 2017-07-20 16:36:01 -0400 | [diff] [blame] | 21 | memset(&createInfo, 0, sizeof(VkSemaphoreCreateInfo)); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 22 | createInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; |
| 23 | createInfo.pNext = nullptr; |
| 24 | createInfo.flags = 0; |
| 25 | VkSemaphore semaphore = VK_NULL_HANDLE; |
| 26 | GR_VK_CALL_ERRCHECK(gpu->vkInterface(), |
| 27 | CreateSemaphore(gpu->device(), &createInfo, nullptr, &semaphore)); |
| 28 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 29 | return sk_sp<GrVkSemaphore>(new GrVkSemaphore(gpu, semaphore, false, false, isOwned)); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 30 | } |
| 31 | |
Brian Salomon | 62db8d5 | 2018-08-30 10:37:47 -0400 | [diff] [blame^] | 32 | sk_sp<GrVkSemaphore> GrVkSemaphore::MakeWrapped(GrVkGpu* gpu, |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 33 | VkSemaphore semaphore, |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 34 | WrapType wrapType, |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 35 | GrWrapOwnership ownership) { |
| 36 | if (VK_NULL_HANDLE == semaphore) { |
| 37 | return nullptr; |
| 38 | } |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 39 | bool prohibitSignal = WrapType::kWillWait == wrapType; |
| 40 | bool prohibitWait = WrapType::kWillSignal == wrapType; |
| 41 | return sk_sp<GrVkSemaphore>(new GrVkSemaphore(gpu, semaphore, prohibitSignal, prohibitWait, |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 42 | kBorrow_GrWrapOwnership != ownership)); |
| 43 | } |
| 44 | |
Brian Salomon | 62db8d5 | 2018-08-30 10:37:47 -0400 | [diff] [blame^] | 45 | GrVkSemaphore::GrVkSemaphore(GrVkGpu* gpu, VkSemaphore semaphore, bool prohibitSignal, |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 46 | bool prohibitWait, bool isOwned) |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 47 | : INHERITED(gpu) { |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 48 | fResource = new Resource(semaphore, prohibitSignal, prohibitWait, isOwned); |
Brian Salomon | 62db8d5 | 2018-08-30 10:37:47 -0400 | [diff] [blame^] | 49 | isOwned ? this->registerWithCache(SkBudgeted::kNo) : this->registerWithCacheWrapped(); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 50 | } |
| 51 | |
Brian Salomon | 62db8d5 | 2018-08-30 10:37:47 -0400 | [diff] [blame^] | 52 | void GrVkSemaphore::onRelease() { |
| 53 | if (fResource) { |
| 54 | fResource->unref(static_cast<const GrVkGpu*>(this->getGpu())); |
| 55 | fResource = nullptr; |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 56 | } |
Brian Salomon | 62db8d5 | 2018-08-30 10:37:47 -0400 | [diff] [blame^] | 57 | INHERITED::onRelease(); |
| 58 | } |
| 59 | |
| 60 | void GrVkSemaphore::onAbandon() { |
| 61 | if (fResource) { |
| 62 | fResource->unrefAndAbandon(); |
| 63 | fResource = nullptr; |
| 64 | } |
| 65 | INHERITED::onAbandon(); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void GrVkSemaphore::Resource::freeGPUData(const GrVkGpu* gpu) const { |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 69 | if (fIsOwned) { |
| 70 | GR_VK_CALL(gpu->vkInterface(), |
| 71 | DestroySemaphore(gpu->device(), fSemaphore, nullptr)); |
| 72 | } |
| 73 | } |
| 74 | |
Brian Salomon | 1e576e7 | 2018-08-30 10:20:38 -0400 | [diff] [blame] | 75 | GrBackendSemaphore GrVkSemaphore::backendSemaphore() const { |
| 76 | GrBackendSemaphore backendSemaphore; |
| 77 | backendSemaphore.initVulkan(fResource->semaphore()); |
| 78 | return backendSemaphore; |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 79 | } |