[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / public-api / loader / resource-bundle.h
1 #ifndef DALI_SCENE3D_LOADERERERER_RESOURCE_BUNDLE_H_
2 #define DALI_SCENE3D_LOADERERERER_RESOURCE_BUNDLE_H_
3 /*
4  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 // EXTERNAL INCLUDES
21 #include <dali/public-api/common/vector-wrapper.h>
22 #include <dali/public-api/rendering/shader.h>
23 #include <dali/public-api/rendering/texture-set.h>
24 #include <functional>
25 #include <memory>
26
27 // INTERNAL INCLUDES
28 #include <dali-scene3d/public-api/loader/buffer-definition.h>
29 #include <dali-scene3d/public-api/loader/environment-definition.h>
30 #include <dali-scene3d/public-api/loader/material-definition.h>
31 #include <dali-scene3d/public-api/loader/mesh-definition.h>
32 #include <dali-scene3d/public-api/loader/shader-definition.h>
33 #include <dali-scene3d/public-api/loader/skeleton-definition.h>
34
35 namespace Dali::Scene3D::Loader
36 {
37 /**
38  * @brief The types of resources that .dli may define.
39  * @SINCE_2_0.7
40  */
41 struct DALI_SCENE3D_API ResourceType
42 {
43   enum Value
44   {
45     Environment,
46     Shader,
47     Mesh,
48     Material,
49   };
50
51   ResourceType() = delete;
52 };
53
54 /**
55  * @brief The string value corresponding to the given resource @a type.
56  * @SINCE_2_0.7
57  * @return The string value for type.
58  */
59 DALI_SCENE3D_API const char* GetResourceTypeName(ResourceType::Value type);
60
61 using ResourceRefCounts = std::vector<Vector<uint32_t>>;
62
63 /**
64  * @brief Stores all resource definitions along with the DALi resources that
65  *  could be created from them, directly indexible into with values from a dli
66  *  document.
67  * @SINCE_2_0.7
68  */
69 class DALI_SCENE3D_API ResourceBundle
70 {
71 public:
72   struct Options
73   {
74     using Type = uint8_t;
75
76     enum Value : Type
77     {
78       None        = 0,
79       ForceReload = NthBit(0), ///< Load resources [again] even if they were already loaded.
80       KeepUnused  = NthBit(1)  ///<s Don't reset handles to resources that had a 0 reference count.
81     };
82   };
83
84   using PathProvider = std::function<std::string(ResourceType::Value)>;
85
86   ResourceBundle();
87
88   ResourceBundle(const ResourceBundle&) = delete;
89   ResourceBundle& operator=(const ResourceBundle&) = delete;
90
91   ResourceBundle(ResourceBundle&&) = default;
92   ResourceBundle& operator=(ResourceBundle&&) = default;
93
94   /**
95    * @brief A ResourceRefCounts object with the correct number of entries for
96    *  all resource types (based on the various resource definition vectors),
97    *  with all reference counts set to 0.
98    * @SINCE_2_0.7
99    * @return A ResourceRefCounts object.
100    */
101   ResourceRefCounts CreateRefCounter() const;
102
103   /**
104    * @brief Based on a ResourceRefCounts, and more specifically the reference
105    *  count of materials therein, it will calculate the reference count of
106    *  environment maps.
107    * @SINCE_2_0.7
108    */
109   void CountEnvironmentReferences();
110
111   /**
112    * @brief Performs the loading of all resources based on their respective reference count in @a refCounts.
113    *
114    * Resources that had a non-zero ref count will be loaded unless we already have a handle to them
115    * (OR the ForceReload option was specified).
116    * Any handles we have to resources that come in with a zero ref count will be reset,
117    * UNLESS the KeepUnused option was specified.
118    *
119    * @SINCE_2_0.7
120    * @param[in] pathProvider path provider for resource data.
121    * @param[in] options Option to load resource
122    * @note This method creates DALi objects like Dali::Texture, Dali::Geometry, etc.
123    */
124   void LoadResources(PathProvider  pathProvider,
125                      Options::Type options = Options::None);
126
127   /**
128    * @brief Loads of all resources based on their respective reference count in @a refCounts.
129    *
130    * Resources that had a non-zero ref count will be loaded unless we already have a handle to them
131    * (OR the ForceReload option was specified).
132    * Any handles we have to resources that come in with a zero ref count will be reset,
133    * UNLESS the KeepUnused option was specified.
134    *
135    * @SINCE_2_2.9
136    * @note This method don't create any of DALi objects.
137    * @param[in] pathProvider path provider for resource data.
138    * @param[in] options Option to load resource
139    * @note This method only loads raw data from resource file, and
140    * doesn't create any of DALi objects. GenerateResources() method is required to be called
141    * after this method to create DALi objects.
142    */
143   void LoadRawResources(PathProvider  pathProvider,
144                         Options::Type options = Options::None);
145
146   /**
147    * @brief Generates DALi objects from already loaded Raw Resources.
148    * @SINCE_2_2.9
149    * @param[in] options Option to load resource
150    * @note This method generates DALi objects from raw data that is already
151    * loaded by LoadRawResources method. Therefore, LoadRawResources should be called first
152    * before this method is called.
153    */
154   void GenerateResources(Options::Type options = Options::None);
155
156 public: // DATA
157   ResourceRefCounts             mReferenceCounts;
158   EnvironmentDefinition::Vector mEnvironmentMaps;
159   ShaderDefinition::Vector      mShaders;
160   MeshDefinition::Vector        mMeshes;
161   MaterialDefinition::Vector    mMaterials;
162
163   SkeletonDefinition::Vector mSkeletons;
164   BufferDefinition::Vector   mBuffers;
165
166   bool mRawResourcesLoading;
167   bool mResourcesGenerating;
168
169   bool mRawResourcesLoaded;
170   bool mResourcesGenerated;
171 };
172
173 } // namespace Dali::Scene3D::Loader
174
175 #endif //DALI_SCENE3D_LOADERERERER_RESOURCE_BUNDLE_H_