[dali_2.0.3] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / update / common / uniform-map.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_UNIFORM_MAP_H
2 #define DALI_INTERNAL_SCENE_GRAPH_UNIFORM_MAP_H
3
4 /*
5  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 // EXTERNAL INCLUDES
21 #include <string>
22 #include <cstdint> // uint32_t
23
24 // INTERNAL INCLUDES
25 #include <dali/devel-api/common/hash.h>
26 #include <dali/devel-api/common/owner-container.h>
27
28 namespace Dali
29 {
30 namespace Internal
31 {
32 class PropertyInputImpl;
33
34 namespace SceneGraph
35 {
36
37 /**
38  * The uniform map is used to map a uniform name to a property value.
39  */
40 class UniformPropertyMapping
41 {
42 public:
43   using Hash = unsigned long;
44
45   /**
46    * Constructor
47    */
48   UniformPropertyMapping(std::string theUniformName, const PropertyInputImpl* thePropertyPtr)
49   : propertyPtr(thePropertyPtr),
50     uniformName(std::move(theUniformName)),
51     uniformNameHash(Dali::CalculateHash(theUniformName))
52   {
53   }
54
55   UniformPropertyMapping()
56   : propertyPtr( nullptr ),
57     uniformName( "" ),
58     uniformNameHash( 0 )
59   {
60   }
61
62
63   const PropertyInputImpl* propertyPtr;
64   std::string uniformName;
65   Hash uniformNameHash;
66 };
67
68 /**
69  * The UniformMap class is used to map uniform names to property values. It is available
70  * in all of the classes responsible for rendering:
71  * Actor, Renderer, Geometry, TextureSet, Shader.
72  *
73  * It can be observed for changes to the mapping table.
74  */
75 class UniformMap
76 {
77 public:
78   using SizeType = uint32_t;
79
80   class Observer
81   {
82   public:
83
84     /**
85      * Inform observer that uniform mappings have been changed
86      * @param mappings
87      */
88     virtual void UniformMappingsChanged(const UniformMap& mappings) = 0;
89
90   protected:
91
92     /**
93      * Virtual destructor, no deletion through this interface
94      */
95     virtual ~Observer() = default;
96   };
97
98   /**
99    * Constructor
100    */
101   UniformMap();
102
103   /**
104    * Destructor
105    */
106   ~UniformMap();
107
108   /**
109    * Add an observer that watches for changes in the mappings
110    */
111   void AddObserver( Observer& observer );
112
113   /**
114    * Remove an observer
115    */
116   void RemoveObserver( Observer& observer );
117
118   /**
119    * Add a map to the mappings table.
120    */
121   void Add( UniformPropertyMapping* map );
122
123   /**
124    * Remove a map from the mappings table
125    */
126   void Remove( const std::string& uniformName );
127
128   /**
129    * Find a property given the uniform name.
130    * @return The address of the property if it's in the map, or NULL otherwise.
131    */
132   const PropertyInputImpl* Find( const std::string& uniformName );
133
134   /**
135    * Get the count of uniforms in the map
136    * @return The number of uniform mappings
137    */
138   SizeType Count() const;
139
140   /**
141    * @pre index must be in the range 0 :: Count()-1
142    * @param[in] index The index of the element to fetch
143    * @return reference to the element in the map
144    */
145   const UniformPropertyMapping& operator[]( SizeType index ) const;
146
147 private:
148   /**
149    * Helper to call the observers when the mappings have changed
150    */
151   void MappingChanged();
152
153 private:
154   using UniformMapContainer = OwnerContainer<UniformPropertyMapping*>;
155   using UniformMapIter      = UniformMapContainer::Iterator;
156   using Observers           = Dali::Vector<Observer*>;
157   using ObserversIter       = Observers::Iterator;
158
159   UniformMapContainer mUniformMaps; // Owner container of uniform maps
160
161   Observers mObservers;
162 };
163
164
165 } // namespace SceneGraph
166 } // namespace Internal
167 } // namespace Dali
168
169 #endif // DALI_INTERNAL_SCENE_GRAPH_UNIFORM_MAP_H