Add model-loader to abstract gltf2-loader and dli-loader
[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) 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 // INTERNAL
21 #include <dali-scene3d/public-api/loader/buffer-definition.h>
22 #include <dali-scene3d/public-api/loader/environment-definition.h>
23 #include <dali-scene3d/public-api/loader/material-definition.h>
24 #include <dali-scene3d/public-api/loader/mesh-definition.h>
25 #include <dali-scene3d/public-api/loader/shader-definition.h>
26 #include <dali-scene3d/public-api/loader/skeleton-definition.h>
27
28 // EXTERNAL
29 #include <dali/public-api/common/vector-wrapper.h>
30 #include <dali/public-api/rendering/shader.h>
31 #include <dali/public-api/rendering/texture-set.h>
32 #include <functional>
33 #include <memory>
34
35 namespace Dali
36 {
37 namespace Scene3D
38 {
39 namespace Loader
40 {
41 /*
42  * @brief The types of resources that .dli may define.
43  */
44 struct DALI_SCENE3D_API ResourceType
45 {
46   enum Value
47   {
48     Environment,
49     Shader,
50     Mesh,
51     Material,
52   };
53
54   ResourceType() = delete;
55 };
56
57 /*
58  * @return The string value corresponding to the given resource @a type.
59  */
60 DALI_SCENE3D_API const char* GetResourceTypeName(ResourceType::Value type);
61
62 using ResourceRefCounts = std::vector<Vector<uint32_t>>;
63
64 /*
65  * @brief Stores all resource definitions along with the DALi resources that
66  *  could be created from them, directly indexible into with values from a dli
67  *  document.
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    * @return 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    */
99   ResourceRefCounts CreateRefCounter() const;
100
101   /**
102    * @brief Based on a ResourceRefCounts, and more specifically the reference
103    *  count of materials therein, it will calculate the reference count of
104    *  environment maps.
105    */
106   void CountEnvironmentReferences();
107
108   /**
109    * @brief Performs the loading of all resources based on their respective
110    * reference count in @a refCounts. Resources that had a non-zero ref count will be
111    * loaded unless we already have a handle to them (OR the ForceReload option was specified).
112    * Any handles we have to resources that come in with a zero ref count will be reset,
113    * UNLESS the KeepUnused option was specified.
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(PathProvider  pathProvider,
119                      Options::Type options = Options::None);
120
121   /**
122    * @brief Loads of all resources based on their respective
123    * reference count in @a refCounts. Resources that had a non-zero ref count will be
124    * loaded unless we already have a handle to them (OR the ForceReload option was specified).
125    * Any handles we have to resources that come in with a zero ref count will be reset,
126    * UNLESS the KeepUnused option was specified.
127    * @note This method don't create any of DALi objects.
128    * @param[in] pathProvider path provider for resource data.
129    * @param[in] options Option to load resource
130    * @note This method only loads raw data from resource file, and
131    * doesn't create any of DALi objects. GenerateResources() method is required to be called
132    * after this method to create DALi objects.
133    */
134   void LoadRawResources(PathProvider  pathProvider,
135                         Options::Type options = Options::None);
136
137   /**
138    * @brief Generates DALi objects from already loaded Raw Resources.
139    * @param[in] options Option to load resource
140    * @note This method generates DALi objects from raw data that is already
141    * loaded by LoadRawResources method. Therefore, LoadRawResources should be called first
142    * before this method is called.
143    */
144   void GenerateResources(Options::Type options = Options::None);
145
146 public: // DATA
147   ResourceRefCounts             mReferenceCounts;
148   EnvironmentDefinition::Vector mEnvironmentMaps;
149   ShaderDefinition::Vector      mShaders;
150   MeshDefinition::Vector        mMeshes;
151   MaterialDefinition::Vector    mMaterials;
152
153   SkeletonDefinition::Vector mSkeletons;
154   BufferDefinition::Vector   mBuffers;
155
156   bool mRawResourcesLoading;
157   bool mResourcesGenerating;
158
159   bool mRawResourcesLoaded;
160   bool mResourcesGenerated;
161 };
162
163 } // namespace Loader
164 } // namespace Scene3D
165 } // namespace Dali
166
167 #endif //DALI_SCENE3D_LOADERERERERER_RESOURCE_BUNDLE_H_