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
46 * This enum defines the outputs of the PrepareRender step, and is used
47 * by the Renderer to determine final opacity.
51 OPAQUE, ///< If the renderer should always be opaque
52 TRANSPARENT, ///< If the renderer should always be transparent
53 USE_ACTOR_COLOR ///< If the renderer should determine opacity using the actor color
67 * Set the shader effect for this material
68 * @param[in] shader The shader effect to use
70 void SetShader( Shader* shader );
73 * Add a sampler (image + sampler modes) to the material
74 * @param[in] sampler A sampler to add
76 void AddSampler( Sampler* sampler );
79 * Remove a sampler (image + sampler modes) from the material
80 * @param[in] sampler A sampler to remove
82 void RemoveSampler( Sampler* sampler );
85 * Prepare the material for rendering.
87 * Determine whether blending is enabled for this material, and store the result.
88 * @param[in] bufferIndex The current buffer index
90 void PrepareRender( BufferIndex bufferIndex );
93 * Return the blend policy ( a combination of all the different shader hints, color, samper and image properties ).
94 * This should only be called from the update thread
95 * @return The material's blend policy
97 BlendPolicy GetBlendPolicy() const;
100 * Set the blending options. This should only be called from the update thread.
101 * @param[in] updateBufferIndex The current update buffer index.
102 * @param[in] options A bitmask of blending options.
104 void SetBlendingOptions( BufferIndex updateBufferIndex, unsigned int options );
106 public: // Implementation of MaterialDataProvider
109 * @copydoc MaterialDataProvider::GetBlendColor
111 virtual const Vector4& GetBlendColor(BufferIndex bufferIndex) const;
114 * @copydoc MaterialDataProvider::GetBlendSrcFactorRgb
116 virtual BlendingFactor::Type GetBlendSrcFactorRgb(BufferIndex bufferIndex) const;
119 * @copydoc MaterialDataProvider::GetBlendSrcFactorAlpha
121 virtual BlendingFactor::Type GetBlendSrcFactorAlpha( BufferIndex bufferIndex ) const;
124 * @copydoc MaterialDataProvider::GetBlendDestFactorRgb
126 virtual BlendingFactor::Type GetBlendDestFactorRgb( BufferIndex bufferIndex ) const;
129 * @copydoc MaterialDataProvider::GetBlendDestFactorAlpha
131 virtual BlendingFactor::Type GetBlendDestFactorAlpha( BufferIndex bufferIndex ) const;
134 * @copydoc MaterialDataProvider::GetBlendEquationRgb
136 virtual BlendingEquation::Type GetBlendEquationRgb( BufferIndex bufferIndex ) const;
139 * @copydoc MaterialDataProvider::GetBlendEquationAlpha
141 virtual BlendingEquation::Type GetBlendEquationAlpha( BufferIndex bufferIndex ) const;
143 public: // Implementation of ObjectOwnerContainer template methods
145 * Connect the object to the scene graph
147 * @param[in] sceneController The scene controller - used for sending messages to render thread
148 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
150 void ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
153 * Disconnect the object from the scene graph
154 * @param[in] sceneController The scene controller - used for sending messages to render thread
155 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
157 void DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
159 public: // Implementation of ConnectionChangePropagator
161 * @copydoc ConnectionChangePropagator::AddObserver
163 void AddConnectionObserver(ConnectionChangePropagator::Observer& observer);
166 * @copydoc ConnectionChangePropagator::RemoveObserver
168 void RemoveConnectionObserver(ConnectionChangePropagator::Observer& observer);
172 * Get the shader effect of this material
173 * @return the shader effect;
175 Shader* GetShader() const;
178 * Get the samplers this material uses.
179 * @return the samplers
181 Vector<Sampler*>& GetSamplers();
183 public: // UniformMap::Observer
185 * @copydoc UniformMap::Observer::UniformMappingsChanged
187 virtual void UniformMappingsChanged( const UniformMap& mappings );
189 public: // ConnectionChangePropagator::Observer
192 * @copydoc ConnectionChangePropagator::ConnectionsChanged
194 virtual void ConnectionsChanged( PropertyOwner& owner );
197 * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
199 virtual void ConnectedUniformMapChanged( );
201 public: // PropertyOwner implementation
203 * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
205 virtual void ResetDefaultProperties( BufferIndex updateBufferIndex );
207 public: // Property data
208 AnimatableProperty<Vector4> mColor;
209 AnimatableProperty<Vector4> mBlendColor;
210 DoubleBufferedProperty<int> mFaceCullingMode;
211 DoubleBufferedProperty<int> mBlendingMode;
212 DoubleBufferedProperty<int> mBlendingOptions;
216 Vector<Sampler*> mSamplers; // Not owned
217 ConnectionChangePropagator mConnectionObservers;
218 BlendPolicy mBlendPolicy; ///< The blend policy as determined by PrepareRender
221 inline void SetShaderMessage( EventThreadServices& eventThreadServices, const Material& material, const Shader& shader )
223 typedef MessageValue1< Material, Shader* > LocalType;
225 // Reserve some memory inside the message queue
226 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
228 // Construct message in the message queue memory; note that delete should not be called on the return value
229 new (slot) LocalType( &material, &Material::SetShader, const_cast<Shader*>(&shader) );
232 inline void AddSamplerMessage( EventThreadServices& eventThreadServices, const Material& material, const Sampler& sampler )
234 typedef MessageValue1< Material, Sampler* > LocalType;
236 // Reserve some memory inside the message queue
237 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
239 // Construct message in the message queue memory; note that delete should not be called on the return value
240 new (slot) LocalType( &material, &Material::AddSampler, const_cast<Sampler*>(&sampler) );
243 inline void RemoveSamplerMessage( EventThreadServices& eventThreadServices, const Material& material, Sampler& sampler )
245 typedef MessageValue1< Material, Sampler* > LocalType;
247 // Reserve some memory inside the message queue
248 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
250 // Construct message in the message queue memory; note that delete should not be called on the return value
251 new (slot) LocalType( &material, &Material::RemoveSampler, const_cast<Sampler*>(&sampler) );
254 inline void SetBlendingOptionsMessage( EventThreadServices& eventThreadServices, const Material& material, unsigned int options )
256 typedef MessageDoubleBuffered1< Material, unsigned int > LocalType;
258 // Reserve some memory inside the message queue
259 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
261 new (slot) LocalType( &material, &Material::SetBlendingOptions, options );
265 } // namespace SceneGraph
266 } // namespace Internal
269 #endif // DALI_INTERNAL_SCENE_GRAPH_MATERIAL_H