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