d99dc10287f0895950bd0087f09b1c93b688a9c3
[platform/core/uifw/dali-core.git] / dali / graphics / vulkan / vulkan-resource-cache.h
1 #ifndef DALI_GRAPHICS_VULKAN_RESOURCE_CACHE
2 #define DALI_GRAPHICS_VULKAN_RESOURCE_CACHE
3
4 /*
5  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 #ifndef VULKAN_HPP_NO_EXCEPTIONS
22 #define VULKAN_HPP_NO_EXCEPTIONS
23 #endif
24
25 // INTERNAL INCLUDES
26 #include <dali/graphics/vulkan/vulkan-types.h>
27 #include <functional>
28
29 namespace Dali
30 {
31 namespace Graphics
32 {
33 namespace Vulkan
34 {
35
36 /**
37  * Stores and manages Vulkan resources
38  */
39 class ResourceCache final
40 {
41 public:
42   /**
43    * Adds the provided buffer object to the buffer cache
44    * @param buffer The buffer object to be added to the cache
45    * @return A reference to the ResourceCache
46    */
47   ResourceCache& AddBuffer( RefCountedBuffer buffer );
48
49   /**
50    * Adds the provided image object to the image cache
51    * @param image The image object to be added to the cache
52    * @return A reference to the ResourceCache
53    */
54   ResourceCache& AddImage( RefCountedImage image );
55
56   /**
57    * Adds the provided image view object to the image view cache
58    * @param imageView The image view object to be added to the cache
59    * @return A reference to the ResourceCache
60    */
61   ResourceCache& AddImageView( RefCountedImageView imageView );
62
63   /**
64    * Adds the provided shader object to the pipeline cache
65    * @param shader The shader object to be added to the cache
66    * @return A reference to the ResourceCache
67    */
68   ResourceCache& AddShader( RefCountedShader shader );
69
70   /**
71    * Adds the provided command pool object to the command pool cache
72    * @param pool The command pool object to be added to the cache
73    * @return A reference to the ResourceCache
74    */
75   ResourceCache& AddCommandPool( RefCountedCommandPool pool );
76
77   /**
78    * Adds the provided descriptor pool object to the descriptor pool cache
79    * @param pool The descriptor pool object to be added to the cache
80    * @return A reference to the ResourceCache
81    */
82   ResourceCache& AddDescriptorPool( RefCountedDescriptorPool pool );
83
84   /**
85    * Adds the provided framebuffer object to the framebuffer cache
86    * @param framebuffer The framebuffer object to be added to the cache
87    * @return A reference to the ResourceCache
88    */
89   ResourceCache& AddFramebuffer( RefCountedFramebuffer framebuffer );
90
91   /**
92    * Adds the provided sampler object to the sampler cache
93    * @param sampler The sampler object to be added to the cache
94    * @return A reference to the ResourceCache
95    */
96   ResourceCache& AddSampler( RefCountedSampler sampler );
97
98   /**
99    * Adds the provided fence object to the fence cache
100    * @param fence The fence object to be added to the cache
101    * @return A reference to the ResourceCache
102    */
103   ResourceCache& AddFence( RefCountedFence fence );
104
105   /**
106    * Finds the buffer object using the specified Vulkan handle in the cache
107    * @param buffer The Vulkan handle of the buffer object to be found
108    * @return A Handle to the buffer object if found. An empty Handle otherwise
109    */
110   RefCountedBuffer FindBuffer( vk::Buffer buffer );
111
112   /**
113    * Finds the image object using the specified Vulkan handle in the cache
114    * @param image The Vulkan handle of the image object to be found
115    * @return A handle to the Image object if found. An empty Handle otherwise
116    */
117   RefCountedImage FindImage( vk::Image image);
118
119   /**
120    * Finds the image view object using the specified Vulkan handle
121    * @param imageView The Vulkan handle of the image view object to be found
122    * @return A handle to the ImageView object if found. An empty Handle otherwise
123    */
124   RefCountedImageView FindImageView( vk::ImageView imageView );
125
126   /**
127    * Finds the shader module using the specified Vulkan handle
128    * @param shaderModule The Vulkan handle of the shader module to be found
129    * @return A Handle to the Shader module if found. An empty Handle otherwise
130    */
131   RefCountedShader FindShader( vk::ShaderModule shaderModule );
132
133   /**
134    * Finds the CommandPool object using the specified Vulkan handle
135    * @param commandPool The Vulkan handle of the CommandPool object to be found
136    * @return A Handle to the CommandPool object if found. An empty Handle otherwise
137    */
138   RefCountedCommandPool FindCommandPool( vk::CommandPool commandPool );
139
140   /**
141    * Finds the DescriptorPool object using the specified Vulkan handle
142    * @param descriptorPool The Vulkan handle of the DescriptorPool object to be found
143    * @return A Handle to the DescriptorPool object if found. An empty Handle otherwise
144    */
145   RefCountedDescriptorPool FindDescriptorPool( vk::DescriptorPool descriptorPool );
146
147   /**
148    * Finds the Framebuffer object using the specified Vulkan handle
149    * @param framebuffer The Vulkan handle of the Framebuffer object to be found
150    * @return A Handle to the Framebuffer object if found. An empty Handle otherwise
151    */
152   RefCountedFramebuffer FindFramebuffer( vk::Framebuffer framebuffer );
153
154   /**
155    * Finds the Sampler object using the specified Vulkan handle
156    * @param sampler The Vulkan handle of the Sampler object to be found
157    * @return A Handle to the Sampler object if found. An empty Handle otherwise
158    */
159   RefCountedSampler FindSampler( vk::Sampler sampler );
160
161   /**
162    * Finds the Fence object using the specified Vulkan handle
163    * @param fence The Vulkan handle of the Fence object to be found
164    * @return A Handle to the Sampler object if found. An empty Handle otherwise
165    */
166   RefCountedFence FindFence( vk::Fence fence );
167
168   /**
169    * Removes the specified Buffer from the cache
170    * @param buffer The Buffer to be removed
171    * @return A reference to the ResourceCache
172    */
173   ResourceCache& RemoveBuffer( Buffer& buffer );
174
175   /**
176    * Removes the specified Image from the cache
177    * @param image The Image to be removed
178    * @return A reference to the ResourceCache
179    */
180   ResourceCache& RemoveImage( Image& image );
181
182   /**
183    * Removes the specified ImageView from the cache
184    * @param imageView The ImageView to be removed
185    * @return A reference to the ResourceCache
186    */
187   ResourceCache& RemoveImageView( ImageView& imageView );
188
189   /**
190    * Removes the specified Shader from the cache
191    * @param shader The Shader to be removed
192    * @return A reference to the ResourceCache
193    */
194   ResourceCache& RemoveShader( Shader& shader );
195
196   /**
197    * Removes the specified CommandPool from the cache
198    * @param commandPool The CommandPool to be removed
199    * @return A reference to the ResourceCache
200    */
201   ResourceCache& RemoveCommandPool( CommandPool& commandPool );
202
203   /**
204    * Removes the specified DescriptorPool from the cache
205    * @param descriptorPool The DescriptorPool to be removed
206    * @return A reference to the ResourceCache
207    */
208   ResourceCache& RemoveDescriptorPool( DescriptorPool& descriptorPool );
209
210   /**
211    * Removes the specified Framebuffer from the cache
212    * @param framebuffer The DescriptorPool to be removed
213    * @return A reference to the ResourceCache
214    */
215   ResourceCache& RemoveFramebuffer( Framebuffer& framebuffer );
216
217   /**
218    * Removes the specified Sampler from the cache
219    * @param sampler The Sampler to be removed
220    * @return A reference to the ResourceCache
221    */
222   ResourceCache& RemoveSampler( Sampler& sampler );
223
224   /**
225    * Removes the specified Fence from the cache
226    * @param fence The Fence to be removed
227    * @return A reference to the ResourceCache
228    */
229   ResourceCache& RemoveFence( Fence& fence );
230
231   void CollectGarbage();
232
233   void EnqueueDiscardOperation( std::function<void()> deleter );
234
235   ResourceCache() = default;
236
237   // The cache should not be copyable
238   ResourceCache( const ResourceCache& other ) = delete;
239
240   ResourceCache& operator=( const ResourceCache& other ) = delete;
241
242   // The cache should not be movable
243   ResourceCache( ResourceCache&& other ) = delete;
244
245   ResourceCache&& operator=( ResourceCache&& other ) = delete;
246
247 private:
248   std::vector< RefCountedBuffer >         mBuffers;
249   std::vector< RefCountedImage >          mImages;
250   std::vector< RefCountedImageView >      mImageViews;
251   std::vector< RefCountedShader >         mShaders;
252   std::vector< RefCountedCommandPool >    mCommandPools;
253   std::vector< RefCountedDescriptorPool > mDescriptorPools;
254   std::vector< RefCountedFramebuffer >    mFramebuffers;
255   std::vector< RefCountedSampler >        mSamplers;
256   std::vector< RefCountedFence >          mFences;
257
258   std::vector< std::function< void() > >    mDiscardQueue;
259 };
260
261 } //namespace Vulkan
262 } //namespace Graphics
263 } //namespace Dali
264
265 #endif //DALI_GRAPHICS_VULKAN_RESOURCE_CACHE