1 #ifndef DALI_INTERNAL_SCENE_GRAPH_MATERIAL_H
2 #define DALI_INTERNAL_SCENE_GRAPH_MATERIAL_H
5 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
39 class ConnectionObserver;
40 class SceneController;
42 class Material : public PropertyOwner, public MaterialDataProvider, public UniformMap::Observer, public ConnectionChangePropagator::Observer
56 * Set the shader effect for this material
57 * @param[in] shader The shader effect to use
59 void SetShader( Shader* shader );
62 * Add a sampler (image + sampler modes) to the material
63 * @param[in] sampler A sampler to add
65 void AddSampler( Sampler* sampler );
68 * Remove a sampler (image + sampler modes) from the material
69 * @param[in] sampler A sampler to remove
71 void RemoveSampler( Sampler* sampler );
74 * Prepare the material for rendering.
76 * Determine whether blending is enabled for this material, and store the result.
77 * @param[in] bufferIndex The current buffer index
79 void PrepareRender( BufferIndex bufferIndex );
82 * Return true if the material requires blending
83 * @return true if the material requires blending
85 bool GetBlendingEnabled( BufferIndex bufferIndex ) const;
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.
92 void SetBlendingOptions( BufferIndex updateBufferIndex, unsigned int options );
94 public: // Implementation of MaterialDataProvider
97 * @copydoc MaterialDataProvider::GetBlendColor
99 virtual const Vector4& GetBlendColor(BufferIndex bufferIndex) const;
102 * @copydoc MaterialDataProvider::GetBlendSrcFactorRgb
104 virtual BlendingFactor::Type GetBlendSrcFactorRgb(BufferIndex bufferIndex) const;
107 * @copydoc MaterialDataProvider::GetBlendSrcFactorAlpha
109 virtual BlendingFactor::Type GetBlendSrcFactorAlpha( BufferIndex bufferIndex ) const;
112 * @copydoc MaterialDataProvider::GetBlendDestFactorRgb
114 virtual BlendingFactor::Type GetBlendDestFactorRgb( BufferIndex bufferIndex ) const;
117 * @copydoc MaterialDataProvider::GetBlendDestFactorAlpha
119 virtual BlendingFactor::Type GetBlendDestFactorAlpha( BufferIndex bufferIndex ) const;
122 * @copydoc MaterialDataProvider::GetBlendEquationRgb
124 virtual BlendingEquation::Type GetBlendEquationRgb( BufferIndex bufferIndex ) const;
127 * @copydoc MaterialDataProvider::GetBlendEquationAlpha
129 virtual BlendingEquation::Type GetBlendEquationAlpha( BufferIndex bufferIndex ) const;
131 public: // Implementation of ObjectOwnerContainer template methods
133 * Connect the object to the scene graph
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
138 void ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
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
145 void DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
147 public: // Implementation of ConnectionChangePropagator
149 * @copydoc ConnectionChangePropagator::AddObserver
151 void AddConnectionObserver(ConnectionChangePropagator::Observer& observer);
154 * @copydoc ConnectionChangePropagator::RemoveObserver
156 void RemoveConnectionObserver(ConnectionChangePropagator::Observer& observer);
160 * Get the shader effect of this material
161 * @return the shader effect;
163 Shader* GetShader() const;
166 * Get the samplers this material uses.
167 * @return the samplers
169 Vector<Sampler*>& GetSamplers();
171 public: // UniformMap::Observer
173 * @copydoc UniformMap::Observer::UniformMappingsChanged
175 virtual void UniformMappingsChanged( const UniformMap& mappings );
177 public: // ConnectionChangePropagator::Observer
180 * @copydoc ConnectionChangePropagator::ConnectionsChanged
182 virtual void ConnectionsChanged( PropertyOwner& owner );
185 * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
187 virtual void ConnectedUniformMapChanged( );
189 public: // PropertyOwner implementation
191 * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
193 virtual void ResetDefaultProperties( BufferIndex updateBufferIndex );
195 public: // Property data
196 AnimatableProperty<Vector4> mColor;
197 AnimatableProperty<Vector4> mBlendColor;
198 DoubleBufferedProperty<int> mFaceCullingMode;
199 DoubleBufferedProperty<int> mBlendingMode;
200 DoubleBufferedProperty<int> mBlendingOptions;
204 Vector<Sampler*> mSamplers; // Not owned
205 ConnectionChangePropagator mConnectionObservers;
206 DoubleBuffered<int> mBlendingEnabled; // The output of the current blending mode and sampler properties
209 inline void SetShaderMessage( EventThreadServices& eventThreadServices, const Material& material, const Shader& shader )
211 typedef MessageValue1< Material, Shader* > LocalType;
213 // Reserve some memory inside the message queue
214 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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) );
220 inline void AddSamplerMessage( EventThreadServices& eventThreadServices, const Material& material, const Sampler& sampler )
222 typedef MessageValue1< Material, Sampler* > LocalType;
224 // Reserve some memory inside the message queue
225 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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) );
231 inline void RemoveSamplerMessage( EventThreadServices& eventThreadServices, const Material& material, Sampler& sampler )
233 typedef MessageValue1< Material, Sampler* > LocalType;
235 // Reserve some memory inside the message queue
236 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
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) );
242 inline void SetBlendingOptionsMessage( EventThreadServices& eventThreadServices, const Material& material, unsigned int options )
244 typedef MessageDoubleBuffered1< Material, unsigned int > LocalType;
246 // Reserve some memory inside the message queue
247 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
249 new (slot) LocalType( &material, &Material::SetBlendingOptions, options );
253 } // namespace SceneGraph
254 } // namespace Internal
257 #endif // DALI_INTERNAL_SCENE_GRAPH_MATERIAL_H