aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/vk/GrVkSemaphore.cpp
blob: 4a23c43eea0322ea18dfdb3cc74dac843c73606c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
 * Copyright 2017 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "GrVkSemaphore.h"

#include "GrBackendSemaphore.h"
#include "GrVkGpu.h"
#include "GrVkUtil.h"

#ifdef VK_USE_PLATFORM_WIN32_KHR
// windows wants to define this as CreateSemaphoreA or CreateSemaphoreW
#undef CreateSemaphore
#endif

SkMutex GrVkSemaphore::Resource::gMutex;

sk_sp<GrVkSemaphore> GrVkSemaphore::Make(const GrVkGpu* gpu, bool isOwned) {
    VkSemaphoreCreateInfo createInfo;
    memset(&createInfo, 0, sizeof(VkSemaphoreCreateInfo));
    createInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
    createInfo.pNext = nullptr;
    createInfo.flags = 0;
    VkSemaphore semaphore = VK_NULL_HANDLE;
    GR_VK_CALL_ERRCHECK(gpu->vkInterface(),
                        CreateSemaphore(gpu->device(), &createInfo, nullptr, &semaphore));

    return sk_sp<GrVkSemaphore>(new GrVkSemaphore(gpu, semaphore, false, false, isOwned));
}

sk_sp<GrVkSemaphore> GrVkSemaphore::MakeWrapped(const GrVkGpu* gpu,
                                                VkSemaphore semaphore,
                                                WrapType wrapType,
                                                GrWrapOwnership ownership) {
    if (VK_NULL_HANDLE == semaphore) {
        return nullptr;
    }
    bool prohibitSignal = WrapType::kWillWait == wrapType;
    bool prohibitWait = WrapType::kWillSignal == wrapType;
    return sk_sp<GrVkSemaphore>(new GrVkSemaphore(gpu, semaphore, prohibitSignal, prohibitWait,
                                                  kBorrow_GrWrapOwnership != ownership));
}

GrVkSemaphore::GrVkSemaphore(const GrVkGpu* gpu, VkSemaphore semaphore, bool prohibitSignal,
                             bool prohibitWait, bool isOwned)
        : INHERITED(gpu) {
    fResource = new Resource(semaphore, prohibitSignal, prohibitWait, isOwned);
}

GrVkSemaphore::~GrVkSemaphore() {
    if (fGpu) {
        fResource->unref(static_cast<const GrVkGpu*>(fGpu));
    } else {
        fResource->unrefAndAbandon();
    }
}

void GrVkSemaphore::Resource::freeGPUData(const GrVkGpu* gpu) const {
    if (fIsOwned) {
        GR_VK_CALL(gpu->vkInterface(),
                   DestroySemaphore(gpu->device(), fSemaphore, nullptr));
    }
}

void GrVkSemaphore::setBackendSemaphore(GrBackendSemaphore* backendSemaphore) const {
    backendSemaphore->initVulkan(fResource->semaphore());
}