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 "src/gpu/vk/GrVkSemaphore.h"
9 
10 #include "include/gpu/GrBackendSemaphore.h"
11 #include "src/gpu/vk/GrVkGpu.h"
12 #include "src/gpu/vk/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 
Make(GrVkGpu * gpu,bool isOwned)19 sk_sp<GrVkSemaphore> GrVkSemaphore::Make(GrVkGpu* gpu, bool isOwned) {
20     VkSemaphoreCreateInfo createInfo;
21     memset(&createInfo, 0, sizeof(VkSemaphoreCreateInfo));
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 
29     return sk_sp<GrVkSemaphore>(new GrVkSemaphore(gpu, semaphore, false, false, isOwned));
30 }
31 
MakeWrapped(GrVkGpu * gpu,VkSemaphore semaphore,WrapType wrapType,GrWrapOwnership ownership)32 sk_sp<GrVkSemaphore> GrVkSemaphore::MakeWrapped(GrVkGpu* gpu,
33                                                 VkSemaphore semaphore,
34                                                 WrapType wrapType,
35                                                 GrWrapOwnership ownership) {
36     if (VK_NULL_HANDLE == semaphore) {
37         return nullptr;
38     }
39     bool prohibitSignal = WrapType::kWillWait == wrapType;
40     bool prohibitWait = WrapType::kWillSignal == wrapType;
41     return sk_sp<GrVkSemaphore>(new GrVkSemaphore(gpu, semaphore, prohibitSignal, prohibitWait,
42                                                   kBorrow_GrWrapOwnership != ownership));
43 }
44 
GrVkSemaphore(GrVkGpu * gpu,VkSemaphore semaphore,bool prohibitSignal,bool prohibitWait,bool isOwned)45 GrVkSemaphore::GrVkSemaphore(GrVkGpu* gpu, VkSemaphore semaphore, bool prohibitSignal,
46                              bool prohibitWait, bool isOwned)
47         : INHERITED(gpu) {
48     fResource = new Resource(semaphore, prohibitSignal, prohibitWait, isOwned);
49     isOwned ? this->registerWithCache(SkBudgeted::kNo)
50             : this->registerWithCacheWrapped(GrWrapCacheable::kNo);
51 }
52 
onRelease()53 void GrVkSemaphore::onRelease() {
54     if (fResource) {
55         fResource->unref(static_cast<GrVkGpu*>(this->getGpu()));
56         fResource = nullptr;
57     }
58     INHERITED::onRelease();
59 }
60 
onAbandon()61 void GrVkSemaphore::onAbandon() {
62     if (fResource) {
63         fResource->unrefAndAbandon();
64         fResource = nullptr;
65     }
66     INHERITED::onAbandon();
67 }
68 
freeGPUData(GrVkGpu * gpu) const69 void GrVkSemaphore::Resource::freeGPUData(GrVkGpu* gpu) const {
70     if (fIsOwned) {
71         GR_VK_CALL(gpu->vkInterface(),
72                    DestroySemaphore(gpu->device(), fSemaphore, nullptr));
73     }
74 }
75 
backendSemaphore() const76 GrBackendSemaphore GrVkSemaphore::backendSemaphore() const {
77     GrBackendSemaphore backendSemaphore;
78     backendSemaphore.initVulkan(fResource->semaphore());
79     return backendSemaphore;
80 }
81