DALi Version 2.2.11
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / public-api / loader / resource-bundle.h
1 #ifndef DALI_SCENE3D_LOADERERERERER_RESOURCE_BUNDLE_H_
2 #define DALI_SCENE3D_LOADERERERERER_RESOURCE_BUNDLE_H_
3 /*
4  * Copyright (c) 2022 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 // INTERNAL
21 #include "dali-scene3d/public-api/loader/environment-definition.h"
22 #include "dali-scene3d/public-api/loader/material-definition.h"
23 #include "dali-scene3d/public-api/loader/mesh-definition.h"
24 #include "dali-scene3d/public-api/loader/shader-definition.h"
25 #include "dali-scene3d/public-api/loader/skeleton-definition.h"
26
27 // EXTERNAL
28 #include <functional>
29 #include <memory>
30 #include "dali/public-api/common/vector-wrapper.h"
31 #include "dali/public-api/rendering/shader.h"
32 #include "dali/public-api/rendering/texture-set.h"
33
34 namespace Dali
35 {
36 namespace Scene3D
37 {
38 namespace Loader
39 {
40 /*
41  * @brief The types of resources that .dli may define.
42  */
43 struct DALI_SCENE3D_API ResourceType
44 {
45   enum Value
46   {
47     Environment,
48     Shader,
49     Mesh,
50     Material,
51   };
52
53   ResourceType() = delete;
54 };
55
56 /*
57  * @return The string value corresponding to the given resource @a 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  */
68 class DALI_SCENE3D_API ResourceBundle
69 {
70 public:
71   struct Options
72   {
73     using Type = uint8_t;
74
75     enum Value : Type
76     {
77       None        = 0,
78       ForceReload = NthBit(0), ///< Load resources [again] even if they were already loaded.
79       KeepUnused  = NthBit(1)  ///<s Don't reset handles to resources that had a 0 reference count.
80     };
81   };
82
83   using PathProvider = std::function<std::string(ResourceType::Value)>;
84
85   ResourceBundle() = default;
86
87   ResourceBundle(const ResourceBundle&) = delete;
88   ResourceBundle& operator=(const ResourceBundle&) = delete;
89
90   ResourceBundle(ResourceBundle&&) = default;
91   ResourceBundle& operator=(ResourceBundle&&) = default;
92
93   /**
94    * @return A ResourceRefCounts object with the correct number of entries for
95    *  all resource types (based on the various resource definition vectors),
96    *  with all reference counts set to 0.
97    */
98   ResourceRefCounts CreateRefCounter() const;
99
100   /**
101    * @brief Based on a ResourceRefCounts, and more specifically the reference
102    *  count of materials therein, it will calculate the reference count of
103    *  environment maps.
104    */
105   void CountEnvironmentReferences(ResourceRefCounts& refCounts) const;
106
107   /**
108    * @brief Performs the loading of all resources based on their respective
109    * reference count in @a refCounts. Resources that had a non-zero ref count will be
110    * loaded unless we already have a handle to them (OR the ForceReload option was specified).
111    * Any handles we have to resources that come in with a zero ref count will be reset,
112    * UNLESS the KeepUnused option was specified.
113    * @param[in] refCounts Reference Count that denote how many the resource is used.
114    * @param[in] pathProvider path provider for resource data.
115    * @param[in] options Option to load resource
116    * @note This method creates DALi objects like Dali::Texture, Dali::Geometry, etc.
117    */
118   void LoadResources(const ResourceRefCounts& refCounts,
119                      PathProvider             pathProvider,
120                      Options::Type            options = Options::None);
121
122   /**
123    * @brief Loads of all resources based on their respective
124    * reference count in @a refCounts. Resources that had a non-zero ref count will be
125    * loaded unless we already have a handle to them (OR the ForceReload option was specified).
126    * Any handles we have to resources that come in with a zero ref count will be reset,
127    * UNLESS the KeepUnused option was specified.
128    * @note This method don't create any of DALi objects.
129    * @param[in] refCounts Reference Count that denote how many the resource is used.
130    * @param[in] pathProvider path provider for resource data.
131    * @param[in] options Option to load resource
132    * @note This method only loads raw data from resource file, and
133    * doesn't create any of DALi objects. GenerateResources() method is required to be called
134    * after this method to create DALi objects.
135    */
136   void LoadRawResources(const ResourceRefCounts& refCounts,
137                         PathProvider             pathProvider,
138                         Options::Type            options = Options::None);
139
140   /**
141    * @brief Generates DALi objects from already loaded Raw Resources.
142    * @param[in] refCounts Reference Count that denote how many the resource is used.
143    * @param[in] options Option to load resource
144    * @note This method generates DALi objects from raw data that is already
145    * loaded by LoadRawResources method. Therefore, LoadRawResources should be called first
146    * before this method is called.
147    */
148   void GenerateResources(const ResourceRefCounts& refCounts,
149                          Options::Type            options = Options::None);
150
151 public: // DATA
152   EnvironmentDefinition::Vector mEnvironmentMaps;
153   ShaderDefinition::Vector      mShaders;
154   MeshDefinition::Vector        mMeshes;
155   MaterialDefinition::Vector    mMaterials;
156
157   SkeletonDefinition::Vector mSkeletons;
158 };
159
160 } // namespace Loader
161 } // namespace Scene3D
162 } // namespace Dali
163
164 #endif //DALI_SCENE3D_LOADERERERERER_RESOURCE_BUNDLE_H_