Support glTF extention: KHR_texture_transform
[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  */
40 struct DALI_SCENE3D_API ResourceType
41 {
42   enum Value
43   {
44     Environment,
45     Shader,
46     Mesh,
47     Material,
48   };
49
50   ResourceType() = delete;
51 };
52
53 /*
54  * @return The string value corresponding to the given resource @a type.
55  */
56 DALI_SCENE3D_API const char* GetResourceTypeName(ResourceType::Value type);
57
58 using ResourceRefCounts = std::vector<Vector<uint32_t>>;
59
60 /*
61  * @brief Stores all resource definitions along with the DALi resources that
62  *  could be created from them, directly indexible into with values from a dli
63  *  document.
64  */
65 class DALI_SCENE3D_API ResourceBundle
66 {
67 public:
68   struct Options
69   {
70     using Type = uint8_t;
71
72     enum Value : Type
73     {
74       None        = 0,
75       ForceReload = NthBit(0), ///< Load resources [again] even if they were already loaded.
76       KeepUnused  = NthBit(1)  ///<s Don't reset handles to resources that had a 0 reference count.
77     };
78   };
79
80   using PathProvider = std::function<std::string(ResourceType::Value)>;
81
82   ResourceBundle();
83
84   ResourceBundle(const ResourceBundle&) = delete;
85   ResourceBundle& operator=(const ResourceBundle&) = delete;
86
87   ResourceBundle(ResourceBundle&&) = default;
88   ResourceBundle& operator=(ResourceBundle&&) = default;
89
90   /**
91    * @return A ResourceRefCounts object with the correct number of entries for
92    *  all resource types (based on the various resource definition vectors),
93    *  with all reference counts set to 0.
94    */
95   ResourceRefCounts CreateRefCounter() const;
96
97   /**
98    * @brief Based on a ResourceRefCounts, and more specifically the reference
99    *  count of materials therein, it will calculate the reference count of
100    *  environment maps.
101    */
102   void CountEnvironmentReferences();
103
104   /**
105    * @brief Performs the loading of all resources based on their respective
106    * reference count in @a refCounts. Resources that had a non-zero ref count will be
107    * loaded unless we already have a handle to them (OR the ForceReload option was specified).
108    * Any handles we have to resources that come in with a zero ref count will be reset,
109    * UNLESS the KeepUnused option was specified.
110    * @param[in] pathProvider path provider for resource data.
111    * @param[in] options Option to load resource
112    * @note This method creates DALi objects like Dali::Texture, Dali::Geometry, etc.
113    */
114   void LoadResources(PathProvider  pathProvider,
115                      Options::Type options = Options::None);
116
117   /**
118    * @brief Loads of all resources based on their respective
119    * reference count in @a refCounts. Resources that had a non-zero ref count will be
120    * loaded unless we already have a handle to them (OR the ForceReload option was specified).
121    * Any handles we have to resources that come in with a zero ref count will be reset,
122    * UNLESS the KeepUnused option was specified.
123    * @note This method don't create any of DALi objects.
124    * @param[in] pathProvider path provider for resource data.
125    * @param[in] options Option to load resource
126    * @note This method only loads raw data from resource file, and
127    * doesn't create any of DALi objects. GenerateResources() method is required to be called
128    * after this method to create DALi objects.
129    */
130   void LoadRawResources(PathProvider  pathProvider,
131                         Options::Type options = Options::None);
132
133   /**
134    * @brief Generates DALi objects from already loaded Raw Resources.
135    * @param[in] options Option to load resource
136    * @note This method generates DALi objects from raw data that is already
137    * loaded by LoadRawResources method. Therefore, LoadRawResources should be called first
138    * before this method is called.
139    */
140   void GenerateResources(Options::Type options = Options::None);
141
142 public: // DATA
143   ResourceRefCounts             mReferenceCounts;
144   EnvironmentDefinition::Vector mEnvironmentMaps;
145   ShaderDefinition::Vector      mShaders;
146   MeshDefinition::Vector        mMeshes;
147   MaterialDefinition::Vector    mMaterials;
148
149   SkeletonDefinition::Vector mSkeletons;
150   BufferDefinition::Vector   mBuffers;
151
152   bool mRawResourcesLoading;
153   bool mResourcesGenerating;
154
155   bool mRawResourcesLoaded;
156   bool mResourcesGenerated;
157 };
158
159 } // namespace Dali::Scene3D::Loader
160
161 #endif //DALI_SCENE3D_LOADERERERER_RESOURCE_BUNDLE_H_