1 // Copyright 2018 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef VK_FENCE_HPP_
16 #define VK_FENCE_HPP_
17
18 #include "VkObject.hpp"
19 #include "System/Synchronization.hpp"
20
21 namespace vk {
22
23 class Fence : public Object<Fence, VkFence>
24 {
25 public:
Fence(const VkFenceCreateInfo * pCreateInfo,void * mem)26 Fence(const VkFenceCreateInfo *pCreateInfo, void *mem)
27 : counted_event(std::make_shared<sw::CountedEvent>((pCreateInfo->flags & VK_FENCE_CREATE_SIGNALED_BIT) != 0))
28 {}
29
ComputeRequiredAllocationSize(const VkFenceCreateInfo * pCreateInfo)30 static size_t ComputeRequiredAllocationSize(const VkFenceCreateInfo *pCreateInfo)
31 {
32 return 0;
33 }
34
reset()35 void reset()
36 {
37 counted_event->reset();
38 }
39
complete()40 void complete()
41 {
42 counted_event->add();
43 counted_event->done();
44 }
45
getStatus()46 VkResult getStatus()
47 {
48 return counted_event->signalled() ? VK_SUCCESS : VK_NOT_READY;
49 }
50
wait()51 VkResult wait()
52 {
53 counted_event->wait();
54 return VK_SUCCESS;
55 }
56
57 template<class CLOCK, class DURATION>
wait(const std::chrono::time_point<CLOCK,DURATION> & timeout)58 VkResult wait(const std::chrono::time_point<CLOCK, DURATION> &timeout)
59 {
60 return counted_event->wait(timeout) ? VK_SUCCESS : VK_TIMEOUT;
61 }
62
getCountedEvent() const63 const std::shared_ptr<sw::CountedEvent> &getCountedEvent() const { return counted_event; };
64
65 private:
66 Fence(const Fence &) = delete;
67
68 const std::shared_ptr<sw::CountedEvent> counted_event;
69 };
70
Cast(VkFence object)71 static inline Fence *Cast(VkFence object)
72 {
73 return Fence::Cast(object);
74 }
75
76 } // namespace vk
77
78 #endif // VK_FENCE_HPP_
79