148433d528dd374ba12cdba79b870cde99c47a68
[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-change-propagator.h>
28 #include <dali/internal/update/common/uniform-map.h>
29 #include <dali/internal/render/data-providers/material-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 ConnectionChangePropagator::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( 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( 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( Sampler* sampler );
72
73   /**
74    * Prepare the material for rendering.
75    *
76    * Determine whether blending is enabled for this material, and store the result.
77    * @param[in] bufferIndex The current buffer index
78    */
79   void PrepareRender( BufferIndex bufferIndex );
80
81   /**
82    * Return true if the material requires blending
83    * @return true if the material requires blending
84    */
85   bool GetBlendingEnabled( BufferIndex bufferIndex ) const;
86
87   /**
88    * Set the blending options. This should only be called from the update-thread.
89    * @param[in] updateBufferIndex The current update buffer index.
90    * @param[in] options A bitmask of blending options.
91    */
92   void SetBlendingOptions( BufferIndex updateBufferIndex, unsigned int options );
93
94 public: // Implementation of MaterialDataProvider
95
96   /**
97    * @copydoc MaterialDataProvider::GetBlendColor
98    */
99   virtual const Vector4& GetBlendColor(BufferIndex bufferIndex) const;
100
101   /**
102    * @copydoc MaterialDataProvider::GetBlendSrcFactorRgb
103    */
104   virtual BlendingFactor::Type GetBlendSrcFactorRgb(BufferIndex bufferIndex) const;
105
106   /**
107    * @copydoc MaterialDataProvider::GetBlendSrcFactorAlpha
108    */
109   virtual BlendingFactor::Type GetBlendSrcFactorAlpha( BufferIndex bufferIndex ) const;
110
111   /**
112    * @copydoc MaterialDataProvider::GetBlendDestFactorRgb
113    */
114   virtual BlendingFactor::Type GetBlendDestFactorRgb( BufferIndex bufferIndex ) const;
115
116   /**
117    * @copydoc MaterialDataProvider::GetBlendDestFactorAlpha
118    */
119   virtual BlendingFactor::Type GetBlendDestFactorAlpha( BufferIndex bufferIndex ) const;
120
121   /**
122    * @copydoc MaterialDataProvider::GetBlendEquationRgb
123    */
124   virtual BlendingEquation::Type GetBlendEquationRgb( BufferIndex bufferIndex ) const;
125
126   /**
127    * @copydoc MaterialDataProvider::GetBlendEquationAlpha
128    */
129   virtual BlendingEquation::Type GetBlendEquationAlpha( BufferIndex bufferIndex ) const;
130
131 public: // Implementation of ObjectOwnerContainer template methods
132   /**
133    * Connect the object to the scene graph
134    *
135    * @param[in] sceneController The scene controller - used for sending messages to render thread
136    * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
137    */
138   void ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
139
140   /**
141    * Disconnect the object from the scene graph
142    * @param[in] sceneController The scene controller - used for sending messages to render thread
143    * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
144    */
145   void DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
146
147 public: // Implementation of ConnectionChangePropagator
148   /**
149    * @copydoc ConnectionChangePropagator::AddObserver
150    */
151   void AddConnectionObserver(ConnectionChangePropagator::Observer& observer);
152
153   /**
154    * @copydoc ConnectionChangePropagator::RemoveObserver
155    */
156   void RemoveConnectionObserver(ConnectionChangePropagator::Observer& observer);
157
158 public:
159   /**
160    * Get the shader effect of this material
161    * @return the shader effect;
162    */
163   Shader* GetShader() const;
164
165   /**
166    * Get the samplers this material uses.
167    * @return the samplers
168    */
169   Vector<Sampler*>& GetSamplers();
170
171 public: // UniformMap::Observer
172   /**
173    * @copydoc UniformMap::Observer::UniformMappingsChanged
174    */
175   virtual void UniformMappingsChanged( const UniformMap& mappings );
176
177 public: // ConnectionChangePropagator::Observer
178
179   /**
180    * @copydoc ConnectionChangePropagator::ConnectionsChanged
181    */
182   virtual void ConnectionsChanged( PropertyOwner& owner );
183
184   /**
185    * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
186    */
187   virtual void ConnectedUniformMapChanged( );
188
189 public: // PropertyOwner implementation
190   /**
191    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
192    */
193   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex );
194
195 public: // Property data
196   AnimatableProperty<Vector4> mColor;
197   AnimatableProperty<Vector4> mBlendColor;
198   DoubleBufferedProperty<int> mFaceCullingMode;
199   DoubleBufferedProperty<int> mBlendingMode;
200   DoubleBufferedProperty<int> mBlendingOptions;
201
202 private:
203   Shader* mShader;
204   Vector<Sampler*> mSamplers; // Not owned
205   ConnectionChangePropagator mConnectionObservers;
206   DoubleBuffered<int> mBlendingEnabled; // The output of the current blending mode and sampler properties
207 };
208
209 inline void SetShaderMessage( EventThreadServices& eventThreadServices, const Material& material, const Shader& shader )
210 {
211   typedef MessageValue1< Material, Shader* > LocalType;
212
213   // Reserve some memory inside the message queue
214   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
215
216   // Construct message in the message queue memory; note that delete should not be called on the return value
217   new (slot) LocalType( &material, &Material::SetShader, const_cast<Shader*>(&shader) );
218 }
219
220 inline void AddSamplerMessage( EventThreadServices& eventThreadServices, const Material& material, const Sampler& sampler )
221 {
222   typedef MessageValue1< Material, Sampler* > LocalType;
223
224   // Reserve some memory inside the message queue
225   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
226
227   // Construct message in the message queue memory; note that delete should not be called on the return value
228   new (slot) LocalType( &material, &Material::AddSampler, const_cast<Sampler*>(&sampler) );
229 }
230
231 inline void RemoveSamplerMessage( EventThreadServices& eventThreadServices, const Material& material, Sampler& sampler )
232 {
233   typedef MessageValue1< Material, Sampler* > LocalType;
234
235   // Reserve some memory inside the message queue
236   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
237
238   // Construct message in the message queue memory; note that delete should not be called on the return value
239   new (slot) LocalType( &material, &Material::RemoveSampler, const_cast<Sampler*>(&sampler) );
240 }
241
242 inline void SetBlendingOptionsMessage( EventThreadServices& eventThreadServices, const Material& material, unsigned int options )
243 {
244   typedef MessageDoubleBuffered1< Material, unsigned int > LocalType;
245
246   // Reserve some memory inside the message queue
247   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
248
249   new (slot) LocalType( &material, &Material::SetBlendingOptions, options );
250 }
251
252
253 } // namespace SceneGraph
254 } // namespace Internal
255 } // namespace Dali
256
257 #endif //  DALI_INTERNAL_SCENE_GRAPH_MATERIAL_H