Merge "Add WebView SetTtsFocus" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-scene-loader / public-api / scene-definition.h
1 #ifndef DALI_SCENE_LOADER_SCENE_DEFINITION_H_
2 #define DALI_SCENE_LOADER_SCENE_DEFINITION_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 INCLUDES
21 #include "dali-scene-loader/public-api/customization.h"
22 #include "dali-scene-loader/public-api/node-definition.h"
23 #include "dali-scene-loader/public-api/string-callback.h"
24 #include "dali-scene-loader/public-api/utils.h"
25
26 // EXTERNAL INCLUDES
27 #include <memory>
28 #include <string>
29 #include "dali/public-api/actors/actor.h"
30 #include "dali/public-api/math/matrix.h"
31 #include "dali/public-api/math/quaternion.h"
32 #include "dali/public-api/math/vector4.h"
33
34 namespace Dali
35 {
36 namespace SceneLoader
37 {
38 class MatrixStack;
39
40 /*
41  * @brief Intermediate representation of a scene with functionality required to
42  *  create DALi objects (Actors, Renderers) from it.
43  */
44 class DALI_SCENE_LOADER_API SceneDefinition
45 {
46 public: // TYPES
47   using NodePredicate     = std::function<bool(const NodeDefinition&)>;
48   using NodeConsumer      = std::function<void(NodeDefinition&)>;
49   using ConstNodeConsumer = std::function<void(const NodeDefinition&)>;
50
51 public: // METHODS
52   SceneDefinition();
53   SceneDefinition(SceneDefinition&& other);
54   ~SceneDefinition();
55
56   /*
57    * @brief Registers a scene root node.
58    * @return The index of the scene root node *registration*.
59    */
60   Index AddRootNode(Index iNode);
61
62   /*
63    * @return the list of scene root node IDs in the order of their loading.
64    */
65   const std::vector<Index>& GetRoots() const;
66
67   /*
68    * @brief Removes scene root registration at the given index @a iRoot.
69    * @note @a iRoot is the index of the registration (i.e. into the vector returned by GetRoots()),
70    *  not of the node.
71    */
72   void RemoveRootNode(Index iRoot);
73
74   /*
75    * @return The number of node( definition)s in the scene.
76    */
77   uint32_t GetNodeCount() const;
78
79   /*
80    * @return Const pointer to the node (definition) at the given index.
81    */
82   const NodeDefinition* GetNode(Index iNode) const;
83
84   /*
85    * @return Pointer to the node (definition) at the given index.
86    */
87   NodeDefinition* GetNode(Index iNode);
88
89   /*
90    * @brief Traverses the scene starting from the node at the given index into
91    *  nodes, using the given customization @a choices and the visitor @a v.
92    */
93   void Visit(Index iNode, const Customization::Choices& choices, NodeDefinition::IVisitor& v);
94
95   /*
96    * @brief Traverses the scene starting from the node at the given index into
97    *  nodes, using the given customization @a choices and the visitor @a v.
98    */
99   void Visit(Index iNode, const Customization::Choices& choices, NodeDefinition::IConstVisitor& v) const;
100
101   /*
102    * @brief Counts the references to meshes, shaders, materials that nodes in
103    *  the scene are holding, writing the results into @a refCounts.
104    * @note @a refCounts' entries must have the correct size. Use ResourceBundle::GetRefCounter().
105    */
106   void CountResourceRefs(Index iNode, const Customization::Choices& choices, ResourceRefCounts& refCounts) const;
107
108   /*
109    * @brief Given a bundle of @a resources that are loaded, and customization
110    *  @a choices, this method traverses the scene, creating the actors and renderers
111    *  from node definitions.
112    * @return Handle to the root actor.
113    */
114   Actor CreateNodes(Index iNode, const Customization::Choices& choices, NodeDefinition::CreateParams& params) const;
115
116   /*
117    * @brief Creates / update a registry of mappings from customization tags to
118    *  a lists of names of customizable nodes under each tag, and the number of
119    *  options. If @a outMissingChoices was specified, each tag that it encounters
120    *  in the scene but not in @a choices, will be registered on it with the default
121    *  choice of 0.
122    */
123   void GetCustomizationOptions(const Customization::Choices& choices,
124                                Customization::Map&           outCustomizationOptions,
125                                Customization::Choices*       outMissingChoices) const;
126
127   /*
128    * @brief Attempts to add @a nodeDef to the end of nodes, and its index to the end of
129    *  its parent's list of children (if iParent != NodeDefinition::INVALID_PARENT).
130    * @return If the operation was successful - which requires nodeDef->name to be unique -
131    *  a pointer to the stored node definition; nullptr otherwise.
132    */
133   NodeDefinition* AddNode(std::unique_ptr<NodeDefinition>&& nodeDef);
134
135   /*
136    * @brief Moves the node to some other parent and / or to a different index.
137    * @return Whether the operation was successful.
138    * @note This is currently breaking an assumption of never having a child of a node at a lower
139    *  index as that of the node itself, due to the fact that we're only changing parent ids (and
140    *  entries into the vector of children of node definitions), to save the complication of having
141    *  to move about, and offset indices to, everything past the reparented node. This should be
142    *  sufficient AT LEAST AS LONG AS we recreate the SceneDefinition when loading the scene; if
143    *  we ever needed to serialize it, we should ensure correct ordering of nodes.
144    */
145   bool ReparentNode(const std::string& name, const std::string& newParentName, Index siblingOrder);
146
147   /*
148    * @brief Removes a node with the given name, including all of its children, and updating
149    *  the indices on all remaining node definitions.
150    * @return Whether the operation was successful.
151    */
152   bool RemoveNode(const std::string& name);
153
154   /*
155    * @brief Builds the model matrix stack for the node at the given @a index.
156    * @note It only pushes new matrices; does not require the stack to be empty (or cares if it was not).
157    */
158   void GetNodeModelStack(Index index, MatrixStack& model) const;
159
160   /*
161    * @brief Attempts to find the definition of a node with the given @a name. Only upon
162    *  success, and if @a outIndex is non-null, the index of the node is written to it.
163    * @return Pointer to the node definition; nullptr if not found.
164    * @note No ownership transfer.
165    */
166   NodeDefinition* FindNode(const std::string& name, Index* outIndex = nullptr);
167
168   /*
169    * @brief Attempts to find the definition of a node with the given @a name. Only upon
170    *  success, and if @a outIndex is non-null, the index of the node is written to it.
171    * @return Pointer to the node definition; nullptr if not found.
172    * @note No ownership transfer.
173    */
174   const NodeDefinition* FindNode(const std::string& name, Index* outIndex = nullptr) const;
175
176   /*
177    * @return The index of the given NodeDefinition@ a node, or -1 if the node definition
178    *  was not found.
179    */
180   Index FindNodeIndex(const NodeDefinition& node) const;
181
182   /*
183    * @brief Calls @a consumer with up to @a limit NodeDefinitions that evaluate to true
184    *  with @a predicate.
185    * @note A @a limit value of 0 means no limit.
186    */
187   void FindNodes(NodePredicate predicate, NodeConsumer consumer, unsigned int limit = 0);
188
189   /*
190    * @brief Calls @a consumer with up to @a limit NodeDefinitions that evaluate to true
191    *  with @a predicate.
192    * @note A @a limit value of 0 means no limit.
193    */
194   void FindNodes(NodePredicate predicate, ConstNodeConsumer consumer, unsigned int limit = 0) const;
195
196   /*
197    * @brief Applies constraints from the given requests.
198    */
199   void ApplyConstraints(Actor&                           root,
200                         std::vector<ConstraintRequest>&& constrainables,
201                         StringCallback                   onError = DefaultErrorCallback) const;
202
203   /*
204    * @brief Sets up joint matrix properties and constraints on actors that are involved in skeletal
205    *  animation (i.e. those that are between (inclusive) the lower and upper bounds of any skeleton),
206    *  to ensure the correct update of meshes skinned to these skeletons.
207    * @param iRoot The index of the scene root node. Skeletons that aren't descendants of this node
208    *  will be ignored.
209    * @param skeletons The list of skeletons that require setting up.
210    * @param rootActor The Actor corresponding to the root node, which will be used to locate
211    *  other actors.
212    */
213   void ConfigureSkeletonJoints(uint32_t iRoot, const SkeletonDefinition::Vector& skeletons, Actor rootActor) const;
214
215   /*
216    * @brief Ensures that there is no overlap between shaders used by nodes that have
217    *  meshes skinned to different skeletons.
218    */
219   void EnsureUniqueSkinningShaderInstances(ResourceBundle& resources) const;
220
221   /*
222    * @brief Performs the configuration of the given skinning shaders with the given skeleton
223    *   This means that the absolute transforms of the joints are calculated and set as one of
224    *   the uniforms in the mat4 @b uBone array (in depth first traversal). Further, the following
225    *   are created:<br />
226    *   - a @b jointMatrix property on each joint Actor;<br />
227    *   - constraint from the Actor's local position and rotation (and if it has a @e joint
228    *     parent, the jointMatrix of the parent) to its @b jointMatrix property;<br />
229    *   - a constraint from the the Actor's @b jointMatrix property to the related entry in
230    *     the shader's @b uBone property;<br />
231    *   This ensures the automatic update of the skeletal animation, should any of the joints'
232    *   transform changes, by whatever means.
233    * @return The success of the operations. Error messages will be posted to the optional
234    *   @a onError callback.
235    * @note A maximum of SkinningDetails::MAX_JOINTS joints per skeleton are supported at the moment.
236    * @note Even if multiple skinned meshes use the same skinning shader, the correct number
237    *   of separate instances need to be declared in the .dli to avoid clashing uniform
238    *   definitions and constraints.
239    */
240   void ConfigureSkinningShaders(const ResourceBundle&                             resources,
241                                 Actor                                             root,
242                                 std::vector<SkinningShaderConfigurationRequest>&& requests) const;
243
244   /*
245    * @brief Ensures there is no two meshes with blend shapes sharing the same shader.
246    */
247   void EnsureUniqueBlendShapeShaderInstances(ResourceBundle& resources) const;
248
249   /**
250    * @brief Performs the configuration of the given blend shapes.
251    *
252    * For each node with blend shapes it registers into the actor the weights properties for each morph target
253    * and some needed uniforms into the shader.
254    * 
255    * @param[in] root The root actor.
256    * @param[in] requests The requests to configure blend shapes.
257    * @param[in] resources The resources bundle. Meshes need to be accessed to configure the blend shapes.
258    * @param[in] onError The error callback.
259    */
260   bool ConfigureBlendshapeShaders(const ResourceBundle&                               resources,
261                                   Actor                                               root,
262                                   std::vector<BlendshapeShaderConfigurationRequest>&& requests,
263                                   StringCallback                                      onError = DefaultErrorCallback) const;
264
265   SceneDefinition& operator=(SceneDefinition&& other);
266
267 private: // METHODS
268   bool FindNode(const std::string& name, std::unique_ptr<NodeDefinition>** result);
269
270 private:                                               // DATA
271   std::vector<std::unique_ptr<NodeDefinition>> mNodes; // size unknown up front (may discard nodes).
272   std::vector<Index>                           mRootNodeIds;
273 };
274
275 } // namespace SceneLoader
276 } // namespace Dali
277
278 #endif //DALI_SCENE_LOADER_SCENE_DEFINITION_H_