[dali_2.1.1] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene-loader / public-api / resource-bundle.h
1 #ifndef DALI_SCENE_LOADER_RESOURCE_BUNDLE_H_
2 #define DALI_SCENE_LOADER_RESOURCE_BUNDLE_H_
3 /*
4  * Copyright (c) 2021 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-scene-loader/public-api/environment-definition.h"
22 #include "dali-scene-loader/public-api/material-definition.h"
23 #include "dali-scene-loader/public-api/mesh-definition.h"
24 #include "dali-scene-loader/public-api/shader-definition.h"
25 #include "dali-scene-loader/public-api/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 SceneLoader
37 {
38 /*
39  * @brief The types of resources that .dli may define.
40  */
41 struct DALI_SCENE_LOADER_API ResourceType
42 {
43   enum Value
44   {
45     Environment,
46     Shader,
47     Mesh,
48     Material,
49   };
50
51   ResourceType() = delete;
52 };
53
54 /*
55  * @return The string value corresponding to the given resource @a type.
56  */
57 DALI_SCENE_LOADER_API const char* GetResourceTypeName(ResourceType::Value type);
58
59 using ResourceRefCounts = std::vector<Vector<uint32_t>>;
60
61 /*
62  * @brief Stores all resource definitions along with the DALi resources that
63  *  could be created from them, directly indexible into with values from a dli
64  *  document.
65  */
66 class DALI_SCENE_LOADER_API ResourceBundle
67 {
68 public:
69   struct Options
70   {
71     using Type = uint8_t;
72
73     enum Value : Type
74     {
75       None        = 0,
76       ForceReload = NthBit(0), ///< Load resources [again] even if they were already loaded.
77       KeepUnused  = NthBit(1)  ///<s Don't reset handles to resources that had a 0 reference count.
78     };
79   };
80
81   using PathProvider = std::function<std::string(ResourceType::Value)>;
82
83   ResourceBundle() = default;
84
85   ResourceBundle(const ResourceBundle&) = delete;
86   ResourceBundle& operator=(const ResourceBundle&) = delete;
87
88   ResourceBundle(ResourceBundle&&) = default;
89   ResourceBundle& operator=(ResourceBundle&&) = default;
90
91   /*
92    * @return A ResourceRefCounts object with the correct number of entries for
93    *  all resource types (based on the various resource definition vectors),
94    *  with all reference counts set to 0.
95    */
96   ResourceRefCounts CreateRefCounter() const;
97
98   /*
99    * @brief Based on a ResourceRefCounts, and more specifically the reference
100    *  count of materials therein, it will calculate the reference count of
101    *  environment maps.
102    */
103   void CountEnvironmentReferences(ResourceRefCounts& refCounts) const;
104
105   /*
106    * @brief Performs the loading of all resources based on their respective
107    *  reference count in @a refCounts. Resources that had a non-zero ref count will be
108    *  loaded unless we already have a handle to them (OR the ForceReload option was specified).
109    *  Any handles we have to resources that come in with a zero ref count will be reset,
110    *  UNLESS the KeepUnused option was specified.
111    */
112   void LoadResources(const ResourceRefCounts& refCounts,
113                      PathProvider             pathProvider,
114                      Options::Type            options = Options::None);
115
116 public: // DATA
117   EnvironmentDefinition::Vector mEnvironmentMaps;
118   ShaderDefinition::Vector      mShaders;
119   MeshDefinition::Vector        mMeshes;
120   MaterialDefinition::Vector    mMaterials;
121
122   SkeletonDefinition::Vector mSkeletons;
123 };
124
125 } // namespace SceneLoader
126 } // namespace Dali
127
128 #endif //DALI_SCENE_LOADER_RESOURCE_BUNDLE_H_