Added sampler properties, test cases.
[platform/core/uifw/dali-core.git] / dali / internal / update / effects / scene-graph-material.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_MATERIAL_H
2 #define DALI_INTERNAL_SCENE_GRAPH_MATERIAL_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/public-api/actors/renderable-actor.h> // For CullFaceMode
21 #include <dali/internal/common/buffer-index.h>
22 #include <dali/internal/common/blending-options.h>
23 #include <dali/internal/event/common/event-thread-services.h>
24 #include <dali/internal/update/common/animatable-property.h>
25 #include <dali/internal/update/common/double-buffered-property.h>
26 #include <dali/internal/update/common/property-owner.h>
27 #include <dali/internal/update/common/scene-graph-connection-observers.h>
28 #include <dali/internal/update/common/uniform-map.h>
29 #include <dali/internal/render/data-providers/render-data-provider.h>
30
31 namespace Dali
32 {
33 namespace Internal
34 {
35 namespace SceneGraph
36 {
37 class Sampler;
38 class Shader;
39 class ConnectionObserver;
40 class SceneController;
41
42 class Material : public PropertyOwner, public MaterialDataProvider, public UniformMap::Observer, public ConnectionObservers::Observer
43 {
44 public:
45   /**
46    * Constructor
47    */
48   Material();
49
50   /**
51    * Destructor
52    */
53   virtual ~Material();
54
55   /**
56    * Set the shader effect for this material
57    * @param[in] shader The shader effect to use
58    */
59   void SetShader( const Shader* shader );
60
61   /**
62    * Add a sampler (image + sampler modes) to the material
63    * @param[in] sampler A sampler to add
64    */
65   void AddSampler( const Sampler* sampler );
66
67   /**
68    * Remove a sampler (image + sampler modes) from the material
69    * @param[in] sampler A sampler to remove
70    */
71   void RemoveSampler( const Sampler* sampler );
72
73   /**
74    * Connect the object to the scene graph
75    *
76    * @param[in] sceneController The scene controller - used for sending messages to render thread
77    * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
78    */
79   void ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
80
81   /**
82    * Disconnect the object from the scene graph
83    * @param[in] sceneController The scene controller - used for sending messages to render thread
84    * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
85    */
86   void DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
87
88   /**
89    * @copydoc ConnectionObservers::AddObserver
90    */
91   void AddConnectionObserver(ConnectionObservers::Observer& observer);
92
93   /**
94    * @copydoc ConnectionObservers::RemoveObserver
95    */
96   void RemoveConnectionObserver(ConnectionObservers::Observer& observer);
97
98 public: // MaterialDataProvider implementation
99   /**
100    * Get the shader effect of this material
101    * @return the shader effect;
102    */
103   virtual Shader* GetShader() const;
104
105   /**
106    * Get the samplers this material uses.
107    * @return the samplers
108    */
109   virtual const RenderDataProvider::Samplers& GetSamplers() const;
110
111 public: // UniformMap::Observer
112   /**
113    * @copydoc UniformMap::Observer::UniformMappingsChanged
114    */
115   virtual void UniformMappingsChanged( const UniformMap& mappings );
116
117 public: // ConnectionObserver::Observer
118
119   /**
120    * @copydoc ConnectionObservers::ConnectionsChanged
121    */
122   virtual void ConnectionsChanged( PropertyOwner& owner );
123
124   /**
125    * @copydoc ConnectionObservers::ConnectedUniformMapChanged
126    */
127   virtual void ConnectedUniformMapChanged( );
128
129 public: // PropertyOwner implementation
130   /**
131    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
132    */
133   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex );
134
135 public: // Property data
136   AnimatableProperty<Vector4> mColor;
137   AnimatableProperty<Vector4> mBlendColor;
138   DoubleBufferedProperty<int> mFaceCullingMode;
139
140 private:
141   const Shader* mShader;
142   RenderDataProvider::Samplers mSamplers; // Not owned
143   ConnectionObservers mConnectionObservers;
144
145   // @todo MESH_REWORK add property values for cull face mode, blending options, blend color
146   // Add getters/setters?
147 };
148
149 inline void SetShaderMessage( EventThreadServices& eventThreadServices, const Material& material, const Shader& shader )
150 {
151   typedef MessageValue1< Material, const Shader* > LocalType;
152
153   // Reserve some memory inside the message queue
154   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
155
156   // Construct message in the message queue memory; note that delete should not be called on the return value
157   new (slot) LocalType( &material, &Material::SetShader, &shader );
158 }
159
160 inline void AddSamplerMessage( EventThreadServices& eventThreadServices, const Material& material, const Sampler& sampler )
161 {
162   typedef MessageValue1< Material, const Sampler* > LocalType;
163
164   // Reserve some memory inside the message queue
165   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
166
167   // Construct message in the message queue memory; note that delete should not be called on the return value
168   new (slot) LocalType( &material, &Material::AddSampler, &sampler );
169 }
170
171 inline void RemoveSamplerMessage( EventThreadServices& eventThreadServices, const Material& material, const Sampler& sampler )
172 {
173   typedef MessageValue1< Material, const Sampler* > LocalType;
174
175   // Reserve some memory inside the message queue
176   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
177
178   // Construct message in the message queue memory; note that delete should not be called on the return value
179   new (slot) LocalType( &material, &Material::RemoveSampler, &sampler );
180 }
181
182 } // namespace SceneGraph
183 } // namespace Internal
184 } // namespace Dali
185
186 #endif //  DALI_INTERNAL_SCENE_GRAPH_MATERIAL_H