xref: /third_party/skia/src/gpu/vk/GrVkSemaphore.cpp (revision cb93a386)
1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2017 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "src/gpu/vk/GrVkSemaphore.h"
9cb93a386Sopenharmony_ci
10cb93a386Sopenharmony_ci#include "include/gpu/GrBackendSemaphore.h"
11cb93a386Sopenharmony_ci#include "src/gpu/vk/GrVkGpu.h"
12cb93a386Sopenharmony_ci#include "src/gpu/vk/GrVkUtil.h"
13cb93a386Sopenharmony_ci
14cb93a386Sopenharmony_ci#ifdef VK_USE_PLATFORM_WIN32_KHR
15cb93a386Sopenharmony_ci// windows wants to define this as CreateSemaphoreA or CreateSemaphoreW
16cb93a386Sopenharmony_ci#undef CreateSemaphore
17cb93a386Sopenharmony_ci#endif
18cb93a386Sopenharmony_ci
19cb93a386Sopenharmony_cistd::unique_ptr<GrVkSemaphore> GrVkSemaphore::Make(GrVkGpu* gpu, bool isOwned) {
20cb93a386Sopenharmony_ci    VkSemaphoreCreateInfo createInfo;
21cb93a386Sopenharmony_ci    memset(&createInfo, 0, sizeof(VkSemaphoreCreateInfo));
22cb93a386Sopenharmony_ci    createInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
23cb93a386Sopenharmony_ci    createInfo.pNext = nullptr;
24cb93a386Sopenharmony_ci    createInfo.flags = 0;
25cb93a386Sopenharmony_ci    VkSemaphore semaphore = VK_NULL_HANDLE;
26cb93a386Sopenharmony_ci    VkResult result;
27cb93a386Sopenharmony_ci    GR_VK_CALL_RESULT(gpu, result, CreateSemaphore(gpu->device(), &createInfo, nullptr,
28cb93a386Sopenharmony_ci                                                   &semaphore));
29cb93a386Sopenharmony_ci    if (result != VK_SUCCESS) {
30cb93a386Sopenharmony_ci        return nullptr;
31cb93a386Sopenharmony_ci    }
32cb93a386Sopenharmony_ci
33cb93a386Sopenharmony_ci    return std::unique_ptr<GrVkSemaphore>(new GrVkSemaphore(gpu, semaphore, false, false, isOwned));
34cb93a386Sopenharmony_ci}
35cb93a386Sopenharmony_ci
36cb93a386Sopenharmony_cistd::unique_ptr<GrVkSemaphore> GrVkSemaphore::MakeWrapped(GrVkGpu* gpu,
37cb93a386Sopenharmony_ci                                                          VkSemaphore semaphore,
38cb93a386Sopenharmony_ci                                                          GrSemaphoreWrapType wrapType,
39cb93a386Sopenharmony_ci                                                          GrWrapOwnership ownership) {
40cb93a386Sopenharmony_ci    if (VK_NULL_HANDLE == semaphore) {
41cb93a386Sopenharmony_ci        SkDEBUGFAIL("Trying to wrap an invalid VkSemaphore");
42cb93a386Sopenharmony_ci        return nullptr;
43cb93a386Sopenharmony_ci    }
44cb93a386Sopenharmony_ci    bool prohibitSignal = GrSemaphoreWrapType::kWillWait == wrapType;
45cb93a386Sopenharmony_ci    bool prohibitWait = GrSemaphoreWrapType::kWillSignal == wrapType;
46cb93a386Sopenharmony_ci    return std::unique_ptr<GrVkSemaphore>(new GrVkSemaphore(gpu, semaphore, prohibitSignal,
47cb93a386Sopenharmony_ci                                                            prohibitWait,
48cb93a386Sopenharmony_ci                                                            kBorrow_GrWrapOwnership != ownership));
49cb93a386Sopenharmony_ci}
50cb93a386Sopenharmony_ci
51cb93a386Sopenharmony_ciGrVkSemaphore::GrVkSemaphore(GrVkGpu* gpu, VkSemaphore semaphore, bool prohibitSignal,
52cb93a386Sopenharmony_ci                             bool prohibitWait, bool isOwned) {
53cb93a386Sopenharmony_ci    fResource = new Resource(gpu, semaphore, prohibitSignal, prohibitWait, isOwned);
54cb93a386Sopenharmony_ci}
55cb93a386Sopenharmony_ci
56cb93a386Sopenharmony_ciGrVkSemaphore::~GrVkSemaphore() {
57cb93a386Sopenharmony_ci    if (fResource) {
58cb93a386Sopenharmony_ci        fResource->unref();
59cb93a386Sopenharmony_ci    }
60cb93a386Sopenharmony_ci}
61cb93a386Sopenharmony_ci
62cb93a386Sopenharmony_civoid GrVkSemaphore::Resource::freeGPUData() const {
63cb93a386Sopenharmony_ci    if (fIsOwned) {
64cb93a386Sopenharmony_ci        GR_VK_CALL(fGpu->vkInterface(),
65cb93a386Sopenharmony_ci                   DestroySemaphore(fGpu->device(), fSemaphore, nullptr));
66cb93a386Sopenharmony_ci    }
67cb93a386Sopenharmony_ci}
68cb93a386Sopenharmony_ci
69cb93a386Sopenharmony_ciGrBackendSemaphore GrVkSemaphore::backendSemaphore() const {
70cb93a386Sopenharmony_ci    GrBackendSemaphore backendSemaphore;
71cb93a386Sopenharmony_ci    backendSemaphore.initVulkan(fResource->semaphore());
72cb93a386Sopenharmony_ci    return backendSemaphore;
73cb93a386Sopenharmony_ci}
74