[dali_2.0.4] 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 #include <dali/internal/common/const-string.h>
28
29 namespace Dali
30 {
31 namespace Internal
32 {
33 class PropertyInputImpl;
34
35 namespace SceneGraph
36 {
37
38 /**
39  * The uniform map is used to map a uniform name to a property value.
40  */
41 class UniformPropertyMapping
42 {
43 public:
44   /**
45    * Constructor
46    */
47   UniformPropertyMapping(ConstString theUniformName, const PropertyInputImpl* thePropertyPtr)
48   : propertyPtr(thePropertyPtr),
49     uniformName(theUniformName)
50   {
51   }
52
53   UniformPropertyMapping() = default;
54
55   const PropertyInputImpl* propertyPtr{nullptr};
56   ConstString uniformName;
57 };
58
59 /**
60  * The UniformMap class is used to map uniform names to property values. It is available
61  * in all of the classes responsible for rendering:
62  * Actor, Renderer, Geometry, TextureSet, Shader.
63  *
64  * It can be observed for changes to the mapping table.
65  */
66 class UniformMap
67 {
68 public:
69   using SizeType = uint32_t;
70
71   class Observer
72   {
73   public:
74
75     /**
76      * Inform observer that uniform mappings have been changed
77      * @param mappings
78      */
79     virtual void UniformMappingsChanged(const UniformMap& mappings) = 0;
80
81   protected:
82
83     /**
84      * Virtual destructor, no deletion through this interface
85      */
86     virtual ~Observer() = default;
87   };
88
89   /**
90    * Add an observer that watches for changes in the mappings
91    */
92   void AddObserver( Observer& observer );
93
94   /**
95    * Remove an observer
96    */
97   void RemoveObserver( Observer& observer );
98
99   /**
100    * Add a map to the mappings table.
101    */
102   void Add(UniformPropertyMapping map);
103
104   /**
105    * Remove a map from the mappings table
106    */
107   void Remove( ConstString uniformName );
108
109   /**
110    * Find a property given the uniform name.
111    * @return The address of the property if it's in the map, or NULL otherwise.
112    */
113   const PropertyInputImpl* Find( ConstString uniformName );
114
115   /**
116    * Get the count of uniforms in the map
117    * @return The number of uniform mappings
118    */
119   SizeType Count() const;
120
121   /**
122    * @pre index must be in the range 0 :: Count()-1
123    * @param[in] index The index of the element to fetch
124    * @return reference to the element in the map
125    */
126   const UniformPropertyMapping& operator[]( SizeType index ) const;
127
128 private:
129   /**
130    * Helper to call the observers when the mappings have changed
131    */
132   void MappingChanged();
133
134 private:
135   using UniformMapContainer = Dali::Vector<UniformPropertyMapping>;
136   using UniformMapIter      = UniformMapContainer::Iterator;
137   using Observers           = Dali::Vector<Observer*>;
138   using ObserversIter       = Observers::Iterator;
139
140   UniformMapContainer mUniformMaps; // Owner container of uniform maps
141
142   Observers mObservers;
143 };
144
145
146 } // namespace SceneGraph
147 } // namespace Internal
148 } // namespace Dali
149
150 #endif // DALI_INTERNAL_SCENE_GRAPH_UNIFORM_MAP_H