[Vulkan] Basic Vulkan backend
[platform/core/uifw/dali-core.git] / dali / graphics / vulkan / vulkan-fence.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // INTERNAL INCLUDES
19 #include <dali/graphics/vulkan/vulkan-fence.h>
20 #include <dali/graphics/vulkan/vulkan-graphics.h>
21
22 namespace Dali
23 {
24 namespace Graphics
25 {
26 namespace Vulkan
27 {
28
29 /**
30  * Class: Fence::Impl
31  *
32  */
33 struct Fence::Impl
34 {
35   Impl( Vulkan::Graphics& graphics )
36   : mGraphics( graphics )
37   {
38   }
39
40   ~Impl()
41   {
42     if( mFence )
43     {
44       mGraphics.GetDevice().destroyFence( mFence, mGraphics.GetAllocator() );
45     }
46   }
47
48   vk::Result Initialise()
49   {
50     mFence = VkAssert( mGraphics.GetDevice().createFence( vk::FenceCreateInfo{}, mGraphics.GetAllocator() ) );
51     if( mFence )
52       return vk::Result::eSuccess;
53     return vk::Result::eErrorInitializationFailed;
54   }
55
56   /**
57    *
58    * @param timeout
59    * @return
60    */
61   bool Wait( uint32_t timeout = 0u )
62   {
63     if(mFence)
64     {
65       if(timeout)
66       {
67         return mGraphics.GetDevice().waitForFences(mFence, true, timeout) == vk::Result::eSuccess;
68       }
69       else
70       {
71         timeout = 16000000;
72         while(mGraphics.GetDevice().waitForFences(mFence, true, timeout) != vk::Result::eSuccess)
73         {
74           // fixme: busy wait, bit ugly
75         }
76         return true;
77       }
78     }
79     return false;
80   }
81
82   /**
83    *
84    */
85   void Reset()
86   {
87     if(mFence)
88     {
89       mGraphics.GetDevice().resetFences(mFence);
90     }
91   }
92
93   vk::Fence GetVkFence() const
94   {
95     return mFence;
96   }
97
98   Vulkan::Graphics& mGraphics;
99   vk::Fence mFence;
100 };
101
102 /**
103  * Class: Fence
104  *
105  */
106 Handle<Fence> Fence::New( Graphics& graphics )
107 {
108   auto retval = Handle<Fence>( new Fence(graphics) );
109   if( vk::Result::eSuccess == retval->mImpl->Initialise() )
110   {
111     return retval;
112   }
113   return Handle<Fence>();
114 }
115
116 Fence::Fence(Graphics& graphics)
117 {
118   mImpl = MakeUnique<Impl>(graphics);
119 }
120
121 const Fence& Fence::ConstRef() const
122 {
123   return *this;
124 }
125
126 Fence& Fence::Ref()
127 {
128   return *this;
129 }
130
131 Fence::~Fence() = default;
132
133 bool Fence::Wait(uint32_t timeout)
134 {
135   return mImpl->Wait( timeout );
136 }
137
138 void Fence::Reset()
139 {
140   mImpl->Reset();
141 }
142
143 vk::Fence Fence::GetFence() const
144 {
145   return mImpl->GetVkFence();
146 }
147
148
149
150 } // namespace Vulkan
151 } // namespace Graphics
152 } // namespace Dali