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