[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / common / model-cache-manager.h
1 #ifndef DALI_SCENE3D_MODEL_CACHE_MANAGER_H
2 #define DALI_SCENE3D_MODEL_CACHE_MANAGER_H
3
4 /*
5  * Copyright (c) 2023 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 // EXTERNAL INCLUDES
22 #include <dali/public-api/object/base-handle.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-scene3d/public-api/loader/load-result.h>
26
27 namespace Dali
28 {
29 namespace Scene3D
30 {
31 namespace Internal
32 {
33 class ModelCacheManager;
34
35 /**
36  * A singleton class to manage the cache of 3D models so that the resources of the same model
37  * are only loaded once and kept in the cache. The cached resources will be reused when the
38  * same model is loaded multiple times.
39  */
40 class ModelCacheManager : public Dali::BaseHandle
41 {
42 public:
43   /**
44    * @brief Creates a ModelCacheManager handle.
45    *
46    * Calling member functions with an uninitialised handle is not allowed.
47    */
48   ModelCacheManager();
49
50   /**
51    * @brief Destructor
52    *
53    * This is non-virtual since derived Handle types must not contain data or virtual methods.
54    */
55   ~ModelCacheManager();
56
57   /**
58    * @brief Create or retrieve the ModelCacheManager singleton.
59    *
60    * @return A handle to the ModelCacheManager.
61    */
62   static ModelCacheManager Get();
63
64   /**
65    * @brief Retrieves the load result for the model with the given URI.
66    * If there is no existing load result for the given model, a new one will be created.
67    * @param[in] modelUri The unique model URI with its absolute path.
68    * @return A reference to the model's load result.
69    */
70   Dali::Scene3D::Loader::LoadResult GetModelLoadResult(std::string modelUri);
71
72   /**
73    * @brief Lock the mutex object to synchronize the scene loading of the model
74    * with the given URI between multiple threads.
75    * It will be used when we need to avoid multiple threads access to same cache.
76    *
77    * @param[in] modelUri The unique model RUI with its absolute path.
78    * @post UnlockModelLoadScene() should be called before call this API.
79    */
80   void LockModelLoadScene(std::string modelUri);
81
82   /**
83    * @brief Unlock the mutex object to synchronize the scene loading of the model
84    * with the given URI between multiple threads.
85    * It will be used when we need to avoid multiple threads access to same cache.
86    *
87    * @param[in] modelUri The unique model URI with its absolute path.
88    * @pre LockModelLoadScene() should be called before call this API.
89    */
90   void UnlockModelLoadScene(std::string modelUri);
91
92   /**
93    * @brief Retrieves the reference count of the cache for the model with the given URI.
94    * @param[in] modelUri The unique model URI with its absolute path.
95    * @return The reference count of the cache.
96    */
97   uint32_t GetModelCacheRefCount(std::string modelUri);
98
99   /**
100    * @brief Reference the cache of the model with the given URI.
101    * This will increment the reference count of the load result by 1.
102    * @param[in] modelUri The model URI.
103    */
104   void ReferenceModelCache(std::string modelUri);
105
106   /**
107    * @brief Unreference the cache of the model with the given URI.
108    * This will decrement the reference count of the load result by 1.
109    * When the reference count becomes zero, the model will be removed from the cache and all
110    * its resources will be deleted.
111    * @param[in] modelUri The model URI.
112    */
113   void UnreferenceModelCache(std::string modelUri);
114
115   /**
116    * @brief Retrieves whether the scene of the model with the given URI has been loaded.
117    * @param[in] modelUri The unique model URI with its absolute path.
118    * @return whether the scene of the model has been loaded. This will be true if the scene
119    * has been loaded for once.
120    */
121   bool IsSceneLoaded(std::string modelUri);
122
123   /**
124    * @brief Sets whether the scene of the model with the given URI has been loaded.
125    * @param[in] modelUri The unique model URI with its absolute path.
126    * @param[in] isSceneLoaded Whether the scene of the model has been loaded.
127    */
128   void SetSceneLoaded(std::string modelUri, bool isSceneLoaded);
129
130   /**
131    * @brief Retrieves whether the scene loading of the model with the given URI is in progress.
132    * @param[in] modelUri The unique model URI with its absolute path.
133    * @return whether the scene loading of the model is in progress.
134    */
135   bool IsSceneLoading(std::string modelUri);
136
137   /**
138    * @brief Sets whether the scene loading of the model with the given URI is in progress.
139    * @param[in] modelUri The unique model URI with its absolute path.
140    * @param[in] isSceneLoading Whether the scene loading of the model is in progress.
141    */
142   void SetSceneLoading(std::string modelUri, bool isSceneLoading);
143
144 public:
145   // Default copy and move operator
146   ModelCacheManager(const ModelCacheManager& rhs) = default;
147   ModelCacheManager(ModelCacheManager&& rhs)      = default;
148   ModelCacheManager& operator=(const ModelCacheManager& rhs) = default;
149   ModelCacheManager& operator=(ModelCacheManager&& rhs) noexcept = default;
150
151 private:
152   class Impl;
153   explicit DALI_INTERNAL ModelCacheManager(ModelCacheManager::Impl* impl);
154 };
155
156 } // namespace Internal
157
158 } // namespace Scene3D
159
160 } // namespace Dali
161
162 #endif // DALI_SCENE3D_MODEL_CACHE_MANAGER_H