Merge "Removed unused CullFace features for ImageActor" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / update / rendering / 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 // INTERNAL INCLUDES
21 #include <dali/devel-api/rendering/material.h>
22 #include <dali/internal/common/buffer-index.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/property-owner.h>
26 #include <dali/internal/update/common/scene-graph-connection-change-propagator.h>
27 #include <dali/internal/update/common/uniform-map.h>
28 #include <dali/internal/update/resources/resource-manager-declarations.h>
29
30 namespace Dali
31 {
32 namespace Internal
33 {
34 class ResourceManager;
35
36 namespace Render
37 {
38 class Sampler;
39 }
40 namespace SceneGraph
41 {
42 class Sampler;
43 class Shader;
44 class ConnectionObserver;
45 class SceneController;
46
47 class Material : public PropertyOwner, public UniformMap::Observer, public ConnectionChangePropagator::Observer
48 {
49 public:
50
51   /**
52    * Construct a new Material.
53    */
54   static Material* New();
55
56   /**
57    * Destructor
58    */
59   virtual ~Material();
60
61   /**
62    * Overriden delete operator
63    * Deletes the material from its global memory pool
64    */
65   void operator delete( void* ptr );
66
67   /**
68    * Prepare material, check texture loading status, opacity etc
69    * @param resourceManager for checking texture details and loading status
70    */
71   void Prepare( const ResourceManager& resourceManager );
72
73   /**
74    * Set the shader effect for this material
75    * @param[in] shader The shader effect to use
76    */
77   void SetShader( Shader* shader );
78
79   /**
80    * Get the shader effect of this material
81    * @return the shader effect;
82    */
83   Shader* GetShader() const;
84
85   /**
86    * Adds a new texture to be used by the material
87    * @param[in] image The image used by the texture
88    * @param[in] uniformName The uniform name of the texture
89    * @param[in] sampler Sampling parameters. If not provided the default sampling parameters will be used
90    */
91   void AddTexture( const std::string& name, ResourceId id, Render::Sampler* sampler );
92
93   /**
94    * Removes a texture from the material
95    * @param[in] index The index of the texture in the array of textures
96    */
97   void RemoveTexture( size_t index );
98
99   /**
100    * Sets the image to be used by a given texture
101    * @param[in] index The index of the texture in the array of textures
102    * @param[in] image The new image
103    */
104   void SetTextureImage( size_t index, ResourceId id );
105
106   /**
107    * Set the sampler used by a given texture
108    * @param[in] index The index of the texture in the array of textures
109    * @param[in] sampler The new sampler
110    */
111   void SetTextureSampler( size_t index, Render::Sampler* sampler);
112
113   /**
114    * Set the uniform name of a given texture
115    * @param[in] index The index of the texture in the array of textures
116    * @param[in] uniformName The new uniform name
117    */
118   void SetTextureUniformName( size_t index, const std::string& uniformName );
119
120   /**
121    * Return whether the textures or the shader require the opacity to be translucent
122    * @return The material's blend policy
123    */
124   bool IsTranslucent() const;
125
126   /**
127    * Get the material resource status
128    * Note, we need two values as it's possible that some resource failed to load
129    * in which case resourcesReady is false (the material is not good to be rendered)
130    * but finishedResourceAcquisition if true as there is no more loading going on
131    * @param[out] resourcesReady if the material is ready to be rendered
132    * @param[out] finishedResourceAcquisition if
133    */
134   void GetResourcesStatus( bool& resourcesReady, bool& finishedResourceAcquisition );
135
136
137 public: // Implementation of ObjectOwnerContainer template methods
138
139   /**
140    * Connect the object to the scene graph
141    *
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 ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
146
147   /**
148    * Disconnect the object from the scene graph
149    * @param[in] sceneController The scene controller - used for sending messages to render thread
150    * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
151    */
152   void DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
153
154 public: // Implementation of ConnectionChangePropagator
155
156   /**
157    * @copydoc ConnectionChangePropagator::AddObserver
158    */
159   void AddConnectionObserver(ConnectionChangePropagator::Observer& observer);
160
161   /**
162    * @copydoc ConnectionChangePropagator::RemoveObserver
163    */
164   void RemoveConnectionObserver(ConnectionChangePropagator::Observer& observer);
165
166 public:
167
168   /**
169    * Get the ResourceId of a texture used by the material
170    * @param[in] index The index of the texture in the textures array
171    * @return the ResourceId
172    */
173   ResourceId GetTextureId( size_t index )
174   {
175     return mTextureId[index];
176   }
177
178   /**
179    * Get the uniform name of a texture used by the material
180    * @param[in] index The index of the texture in the textures array
181    * @return the uniform name
182    */
183   const std::string& GetTextureUniformName( size_t index )
184   {
185     return mUniformName[index];
186   }
187
188   /**
189    * Get the sampler of a texture used by the material
190    * @param[in] index The index of the texture in the textures array
191    * @return the sampler used by the texture
192    */
193   Render::Sampler* GetTextureSampler( size_t index )
194   {
195     return mSamplers[index];
196   }
197
198   /**
199    * Get the number of textures used by the material
200    * @return The number of textures
201    */
202   size_t GetTextureCount()
203   {
204     return mTextureId.Size();
205   }
206
207 public: // UniformMap::Observer
208   /**
209    * @copydoc UniformMap::Observer::UniformMappingsChanged
210    */
211   virtual void UniformMappingsChanged( const UniformMap& mappings );
212
213 public: // ConnectionChangePropagator::Observer
214
215   /**
216    * @copydoc ConnectionChangePropagator::ConnectionsChanged
217    */
218   virtual void ConnectionsChanged( PropertyOwner& owner );
219
220   /**
221    * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
222    */
223   virtual void ConnectedUniformMapChanged();
224
225 private:
226
227   /**
228    * Protected constructor; See also Material::New()
229    */
230    Material();
231
232 private: // Data
233
234   Shader*                         mShader;
235   Vector< Render::Sampler* >      mSamplers; // Not owned
236   Vector< ResourceId >            mTextureId;
237   std::vector< std::string >      mUniformName;
238   ConnectionChangePropagator      mConnectionObservers;
239   bool                            mResourcesReady; ///< if the material is ready to be rendered
240   bool                            mFinishedResourceAcquisition; ///< if resource loading is completed
241   bool                            mMaterialChanged; ///< if the material has changed since the last frame
242   bool                            mIsTranslucent; ///< if the textures or the shader require the opacity to be translucent
243 };
244
245 inline void SetShaderMessage( EventThreadServices& eventThreadServices, const Material& material, Shader& shader )
246 {
247   typedef MessageValue1< Material, Shader* > LocalType;
248
249   // Reserve some memory inside the message queue
250   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
251
252   // Construct message in the message queue memory; note that delete should not be called on the return value
253   new (slot) LocalType( &material, &Material::SetShader, &shader );
254 }
255
256 inline void AddTextureMessage( EventThreadServices& eventThreadServices, const Material& material, const std::string& uniformName, ResourceId id, Render::Sampler* sampler )
257 {
258   typedef MessageValue3< Material, std::string, ResourceId, Render::Sampler* > LocalType;
259
260   // Reserve some memory inside the message queue
261   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
262
263   // Construct message in the message queue memory; note that delete should not be called on the return value
264   new (slot) LocalType( &material, &Material::AddTexture, uniformName, id, sampler );
265 }
266
267 inline void RemoveTextureMessage( EventThreadServices& eventThreadServices, const Material& material, size_t index )
268 {
269   typedef MessageValue1< Material, size_t > LocalType;
270
271   // Reserve some memory inside the message queue
272   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
273
274   // Construct message in the message queue memory; note that delete should not be called on the return value
275   new (slot) LocalType( &material, &Material::RemoveTexture, index );
276 }
277
278 inline void SetTextureImageMessage( EventThreadServices& eventThreadServices, const Material& material, size_t index, ResourceId id )
279 {
280   typedef MessageValue2< Material, size_t, ResourceId > LocalType;
281
282   // Reserve some memory inside the message queue
283   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
284
285   // Construct message in the message queue memory; note that delete should not be called on the return value
286   new (slot) LocalType( &material, &Material::SetTextureImage, index, id );
287 }
288
289 inline void SetTextureSamplerMessage( EventThreadServices& eventThreadServices, const Material& material, size_t index, Render::Sampler* sampler )
290 {
291   typedef MessageValue2< Material, size_t, Render::Sampler* > LocalType;
292
293   // Reserve some memory inside the message queue
294   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
295
296   // Construct message in the message queue memory; note that delete should not be called on the return value
297   new (slot) LocalType( &material, &Material::SetTextureSampler, index, sampler );
298 }
299
300 inline void SetTextureUniformNameMessage( EventThreadServices& eventThreadServices, const Material& material, size_t index, const std::string& uniformName )
301 {
302   typedef MessageValue2< Material, size_t, std::string > LocalType;
303
304   // Reserve some memory inside the message queue
305   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
306
307   // Construct message in the message queue memory; note that delete should not be called on the return value
308   new (slot) LocalType( &material, &Material::SetTextureUniformName, index, uniformName );
309 }
310
311 } // namespace SceneGraph
312
313 } // namespace Internal
314
315 } // namespace Dali
316
317 #endif //  DALI_INTERNAL_SCENE_GRAPH_MATERIAL_H