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 std::unique_ptr<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 VkResult result;
27 GR_VK_CALL_RESULT(gpu, result, CreateSemaphore(gpu->device(), &createInfo, nullptr,
28 &semaphore));
29 if (result != VK_SUCCESS) {
30 return nullptr;
31 }
32
33 return std::unique_ptr<GrVkSemaphore>(new GrVkSemaphore(gpu, semaphore, false, false, isOwned));
34 }
35
MakeWrapped(GrVkGpu * gpu,VkSemaphore semaphore,GrSemaphoreWrapType wrapType,GrWrapOwnership ownership)36 std::unique_ptr<GrVkSemaphore> GrVkSemaphore::MakeWrapped(GrVkGpu* gpu,
37 VkSemaphore semaphore,
38 GrSemaphoreWrapType wrapType,
39 GrWrapOwnership ownership) {
40 if (VK_NULL_HANDLE == semaphore) {
41 SkDEBUGFAIL("Trying to wrap an invalid VkSemaphore");
42 return nullptr;
43 }
44 bool prohibitSignal = GrSemaphoreWrapType::kWillWait == wrapType;
45 bool prohibitWait = GrSemaphoreWrapType::kWillSignal == wrapType;
46 return std::unique_ptr<GrVkSemaphore>(new GrVkSemaphore(gpu, semaphore, prohibitSignal,
47 prohibitWait,
48 kBorrow_GrWrapOwnership != ownership));
49 }
50
GrVkSemaphore(GrVkGpu * gpu,VkSemaphore semaphore,bool prohibitSignal,bool prohibitWait,bool isOwned)51 GrVkSemaphore::GrVkSemaphore(GrVkGpu* gpu, VkSemaphore semaphore, bool prohibitSignal,
52 bool prohibitWait, bool isOwned) {
53 fResource = new Resource(gpu, semaphore, prohibitSignal, prohibitWait, isOwned);
54 }
55
~GrVkSemaphore()56 GrVkSemaphore::~GrVkSemaphore() {
57 if (fResource) {
58 fResource->unref();
59 }
60 }
61
freeGPUData() const62 void GrVkSemaphore::Resource::freeGPUData() const {
63 if (fIsOwned) {
64 GR_VK_CALL(fGpu->vkInterface(),
65 DestroySemaphore(fGpu->device(), fSemaphore, nullptr));
66 }
67 }
68
backendSemaphore() const69 GrBackendSemaphore GrVkSemaphore::backendSemaphore() const {
70 GrBackendSemaphore backendSemaphore;
71 backendSemaphore.initVulkan(fResource->semaphore());
72 return backendSemaphore;
73 }
74