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 | #ifndef GrVkSemaphore_DEFINED |
| 9 | #define GrVkSemaphore_DEFINED |
| 10 | |
| 11 | #include "GrSemaphore.h" |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 12 | |
| 13 | #include "GrResourceProvider.h" |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 14 | #include "GrVkResource.h" |
| 15 | |
| 16 | #include "vk/GrVkTypes.h" |
| 17 | |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 18 | class GrBackendSemaphore; |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 19 | class GrVkGpu; |
| 20 | |
| 21 | class GrVkSemaphore : public GrSemaphore { |
| 22 | public: |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 23 | static sk_sp<GrVkSemaphore> Make(const GrVkGpu* gpu, bool isOwned); |
| 24 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 25 | using WrapType = GrResourceProvider::SemaphoreWrapType; |
| 26 | |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 27 | static sk_sp<GrVkSemaphore> MakeWrapped(const GrVkGpu* gpu, |
| 28 | VkSemaphore semaphore, |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 29 | WrapType wrapType, |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 30 | GrWrapOwnership); |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 31 | |
| 32 | ~GrVkSemaphore() override; |
| 33 | |
| 34 | class Resource : public GrVkResource { |
| 35 | public: |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 36 | Resource(VkSemaphore semaphore, bool prohibitSignal, bool prohibitWait, bool isOwned) |
| 37 | : INHERITED() |
| 38 | , fSemaphore(semaphore) |
| 39 | , fHasBeenSubmittedToQueueForSignal(prohibitSignal) |
| 40 | , fHasBeenSubmittedToQueueForWait(prohibitWait) |
| 41 | , fIsOwned(isOwned) {} |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 42 | |
| 43 | ~Resource() override {} |
| 44 | |
| 45 | VkSemaphore semaphore() const { return fSemaphore; } |
| 46 | |
Greg Daniel | 21580ba | 2018-06-26 11:26:44 -0400 | [diff] [blame^] | 47 | static void AcquireMutex() { GetMutex()->acquire(); } |
| 48 | static void ReleaseMutex() { GetMutex()->release(); } |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 49 | |
| 50 | bool shouldSignal() const { |
| 51 | return !fHasBeenSubmittedToQueueForSignal; |
| 52 | } |
| 53 | bool shouldWait() const { |
| 54 | return !fHasBeenSubmittedToQueueForWait; |
| 55 | } |
| 56 | |
| 57 | void markAsSignaled() { |
Greg Daniel | 21580ba | 2018-06-26 11:26:44 -0400 | [diff] [blame^] | 58 | GetMutex()->assertHeld(); |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 59 | fHasBeenSubmittedToQueueForSignal = true; |
| 60 | } |
| 61 | void markAsWaited() { |
Greg Daniel | 21580ba | 2018-06-26 11:26:44 -0400 | [diff] [blame^] | 62 | GetMutex()->assertHeld(); |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 63 | fHasBeenSubmittedToQueueForWait = true; |
| 64 | } |
| 65 | |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 66 | #ifdef SK_TRACE_VK_RESOURCES |
| 67 | void dumpInfo() const override { |
| 68 | SkDebugf("GrVkSemaphore: %d (%d refs)\n", fSemaphore, this->getRefCnt()); |
| 69 | } |
| 70 | #endif |
| 71 | private: |
| 72 | void freeGPUData(const GrVkGpu* gpu) const override; |
| 73 | |
Greg Daniel | 21580ba | 2018-06-26 11:26:44 -0400 | [diff] [blame^] | 74 | static SkMutex* GetMutex() { |
| 75 | static SkMutex kMutex; |
| 76 | return &kMutex; |
| 77 | } |
| 78 | |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 79 | VkSemaphore fSemaphore; |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 80 | bool fHasBeenSubmittedToQueueForSignal; |
| 81 | bool fHasBeenSubmittedToQueueForWait; |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 82 | bool fIsOwned; |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 83 | |
| 84 | typedef GrVkResource INHERITED; |
| 85 | }; |
| 86 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 87 | Resource* getResource() { return fResource; } |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 88 | |
| 89 | private: |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 90 | GrVkSemaphore(const GrVkGpu* gpu, VkSemaphore semaphore, bool prohibitSignal, bool prohibitWait, |
| 91 | bool isOwned); |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 92 | |
| 93 | void setBackendSemaphore(GrBackendSemaphore*) const override; |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 94 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 95 | Resource* fResource; |
Greg Daniel | 6be3523 | 2017-03-01 17:01:09 -0500 | [diff] [blame] | 96 | |
| 97 | typedef GrSemaphore INHERITED; |
| 98 | }; |
| 99 | |
| 100 | #endif |