Refactored EventToUpdate into EventThreadServices
[platform/core/uifw/dali-core.git] / dali / internal / update / node-attachments / scene-graph-renderable-attachment.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_RENDERABLE_ATTACHMENT_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_RENDERABLE_ATTACHMENT_H__
3
4 /*
5  * Copyright (c) 2014 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
21 // INTERNAL INCLUDES
22 #include <dali/internal/common/blending-options.h>
23 #include <dali/internal/common/internal-constants.h>
24 #include <dali/internal/common/type-abstraction-enums.h>
25 #include <dali/internal/event/common/event-thread-services.h>
26 #include <dali/internal/update/controllers/scene-controller.h>
27 #include <dali/internal/update/nodes/node.h>
28 #include <dali/internal/update/node-attachments/node-attachment.h>
29 #include <dali/internal/update/resources/resource-manager-declarations.h>
30 #include <dali/internal/render/renderers/scene-graph-renderer-declarations.h>
31
32 namespace Dali
33 {
34
35 namespace Internal
36 {
37 class ResourceManager;
38 class ResourceTracker;
39
40 namespace SceneGraph
41 {
42 class Renderer;
43 class Shader;
44
45 /**
46  * RenderableAttachments are responsible for preparing textures, meshes, matrices etc. during the Update.
47  * These resources are then passed to a renderer, for use in the next Render.
48  */
49 class RenderableAttachment : public NodeAttachment
50 {
51 public: // API
52
53   /**
54    * Set the sort-modifier for the attachment.
55    * @param[in] modifier The depth-sort modifier.
56    */
57   void SetSortModifier(float modifier);
58
59   /**
60    * Retrieve the sort-modifier for the attachment.
61    * @return The sort-modifier.
62    */
63   float GetSortModifier() const
64   {
65     // inlined as its called a lot when sorting transparent renderers
66     return mSortModifier;
67   }
68
69   /**
70    * @See Dali::RenderableActor::SetBlendMode().
71    */
72   void SetBlendingMode( BlendingMode::Type mode );
73
74   /**
75    * @copydoc Dali::RenderableActor::GetBlendMode().
76    */
77   BlendingMode::Type GetBlendingMode() const;
78
79   /**
80    * Check if the blending mode has changed - if it has, send message to renderer
81    * @param[in] updateBufferIndex The current update buffer index.
82    * @param[in] useBlending True if the renderer should use blending option
83    */
84   void ChangeBlending( BufferIndex updateBufferIndex, bool useBlending );
85
86   /**
87    * Set the blending options. This should only be called from the update-thread.
88    * @param[in] updateBufferIndex The current update buffer index.
89    * @param[in] options A bitmask of blending options.
90    */
91   void SetBlendingOptions( BufferIndex updateBufferIndex, unsigned int options );
92
93   /**
94    * Set the blend-color. This should only be called from the update-thread.
95    * @param[in] updateBufferIndex The current update buffer index.
96    * @param[in] color The new blend-color.
97    */
98   void SetBlendColor( BufferIndex updateBufferIndex, const Vector4& color );
99
100   /**
101    * Set the face-culling mode.
102    * @param[in] updateBufferIndex The current update buffer index.
103    * @param[in] mode The face-culling mode.
104    */
105   void SetCullFace( BufferIndex updateBufferIndex, CullFaceMode mode );
106
107   /**
108    * Set the sampler used to render the texture for this renderable.
109    * @param[in] updateBufferIndex The current update buffer index.
110    * @param[in] samplerBitfield The image sampler packed options to set.
111    */
112   void SetSampler( BufferIndex updateBufferIndex, unsigned int samplerBitfield );
113
114   /**
115    * Flag to check if any geometry scaling is needed, inlined as called from update algorithm often
116    * @return true if the derived renderable uses geometry scaling
117    */
118   bool UsesGeometryScaling() const
119   {
120     return mUsesGeometryScaling;
121   }
122
123   /**
124    * Triggers scale for size update. GetScaleForSize will be called in this frame
125    */
126   void SetRecalculateScaleForSize();
127
128   /**
129    * Returns the scaling dirty flag, inlined as called from update algorithm often
130    * @return if scale for size is dirty, i.e. scaling has changed
131    */
132   bool IsScaleForSizeDirty() const
133   {
134     return mScaleForSizeDirty;
135   }
136
137   /**
138    * Retrieve scale-for-size for given node size
139    * Clears the scale for size flag
140    * @param[in] nodeSize to scale to
141    * @param[out] scaling factors
142    */
143   void GetScaleForSize( const Vector3& nodeSize, Vector3& scaling );
144
145   /**
146    * Apply a shader on the renderable
147    * @param[in] updateBufferIndex The current update buffer index.
148    * @param[in] shader to apply.
149    */
150   void ApplyShader( BufferIndex updateBufferIndex, Shader* shader );
151
152   /**
153    * Remove the shader from the renderable
154    * @param[in] updateBufferIndex The current update buffer index.
155    */
156   void RemoveShader( BufferIndex updateBufferIndex );
157
158 public: // For use during in the update algorithm only
159
160   /**
161    * TODO this method should not be virtual but because mesh attachment is a mess, it needs to be
162    * considered visible regardless of its size... need to remove geometry scaling to fix this!!!
163    * @param[in] updateBufferIndex The current update buffer index.
164    * @return visible tells if this renderer can be potentially seen
165    */
166   virtual bool ResolveVisibility( BufferIndex updateBufferIndex );
167
168   /**
169    * if this renderable actor has visible size and color
170    * @return true if you can potentially see this actor
171    */
172   bool HasVisibleSizeAndColor() const
173   {
174     return mHasSizeAndColorFlag;
175   }
176
177   /**
178    * Check whether the attachment has been marked as ready to render
179    * @param[out] ready TRUE if the attachment has resources to render
180    * @param[out] complete TRUE if the attachment's resources are complete
181    * (e.g. image has finished loading, framebuffer is ready to render, native image
182    * framebuffer has been rendered)
183    */
184   void GetReadyAndComplete(bool& ready, bool& complete) const;
185
186   /**
187    * Query whether the attachment has blending enabled.
188    * @param[in] updateBufferIndex The current update buffer index.
189    * @return true if blending is enabled, false otherwise.
190    */
191   bool IsBlendingOn( BufferIndex updateBufferIndex );
192
193   /**
194    * Prepare the object for rendering.
195    * This is called by the UpdateManager when an object is due to be rendered in the current frame.
196    * @param[in] updateBufferIndex The current update buffer index.
197    */
198   void PrepareRender( BufferIndex updateBufferIndex );
199
200 public: // API for derived classes
201
202   /**
203    * Retrieve a Renderer used by this attachment; implemented in derived classes.
204    * @note The first Renderer is the main renderer for the attachment, and
205    * should always exist during the lifetime of the RenderableAttachment.
206    * @return A Renderer.
207    */
208   virtual Renderer& GetRenderer() = 0;
209
210   /**
211    * Retrieve a Renderer used by this attachment.
212    * @note The first Renderer is the main renderer for the attachment, and
213    * should always exist during the lifetime of the RenderableAttachment.
214    * Other renderers are for effects such as shadows and reflections.
215    * @return A Renderer.
216    */
217   virtual const Renderer& GetRenderer() const = 0;
218
219   /**
220    * Prepare the object resources.
221    * This must be called by the UpdateManager before calling PrepareRender, for each frame.
222    * @param[in] updateBufferIndex The current update buffer index.
223    * @param[in] resourceManager The resource manager.
224    */
225   void PrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager );
226
227   /**
228    * If the resource is being tracked, then follow it. ( Further ready tests will use this
229    * list ) Otherwise, if it's not complete, set mHasUntrackedResources.
230    * @param[in] The resource id
231    */
232   void FollowTracker( Integration::ResourceId id );
233
234   /**
235    * @copydoc RenderableAttachment::PrepareRender()
236    */
237   virtual void DoPrepareRender( BufferIndex updateBufferIndex ) = 0;
238
239   /**
240    * Query whether the attachment is fully opaque.
241    * @param[in] updateBufferIndex The current update buffer index.
242    * @return True if fully opaque.
243    */
244   virtual bool IsFullyOpaque( BufferIndex updateBufferIndex ) = 0;
245
246   /**
247    * Called to notify that the shader might have been changed
248    * The implementation should recalculate geometry and scale based on the
249    * hints from the new shader
250    * @param[in] updateBufferIndex The current update buffer index.
251    * @return Return true if the geometry changed.
252    */
253   virtual void ShaderChanged( BufferIndex updateBufferIndex ) = 0;
254
255   /**
256    * Called to notify that the size has been changed
257    * The implementation may tell the renderer to recalculate geometry and scale based on the new size
258    * @param[in] updateBufferIndex The current update buffer index.
259    */
260   virtual void SizeChanged( BufferIndex updateBufferIndex ) = 0;
261
262   /**
263    * Chained from NodeAttachment::ConnectToSceneGraph()
264    */
265   virtual void ConnectToSceneGraph2( BufferIndex updateBufferIndex ) = 0;
266
267   /**
268    * Chained from NodeAttachment::OnDestroy()
269    */
270   virtual void OnDestroy2() = 0;
271
272   /**
273    * Retrieve the scale-for-size for given node size. Default implementation returns Vector3::ZERO
274    * @param[in] nodeSize to scale to
275    * @param[out] scaling factors
276    */
277   virtual void DoGetScaleForSize( const Vector3& nodeSize, Vector3& scaling );
278
279 protected:
280
281   /**
282    * Protected constructor; only base classes can be instantiated.
283    * @param usesGeometryScaling should be false if the derived class does not need geometry scaling
284    */
285   RenderableAttachment( bool usesGeometryScaling );
286
287   /**
288    * Virtual destructor, no deletion through this interface
289    */
290   virtual ~RenderableAttachment();
291
292 private: // From NodeAttachment
293
294   /**
295    * @copydoc NodeAttachment::ConnectToSceneGraph().
296    */
297   virtual void ConnectToSceneGraph( SceneController& sceneController, BufferIndex updateBufferIndex );
298
299   /**
300    * @copydoc NodeAttachment::DisconnectFromSceneGraph().
301    */
302   virtual void OnDestroy();
303
304   /**
305    * @copydoc NodeAttachment::GetRenderable()
306    */
307   virtual RenderableAttachment* GetRenderable();
308
309 private:
310
311   /**
312    * Prepare the object resources.
313    * This must be called by the UpdateManager before calling PrepareRender, for each frame.
314    * @param[in] updateBufferIndex The current buffer index.
315    * @param[in] resourceManager The resource manager.
316    * @return True if resources are ready, false will prevent PrepareRender being called for this attachment.
317    */
318   virtual bool DoPrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager ) = 0;
319
320   /**
321    * Sends the shader to the renderer
322    * @param updateBufferIndex for the message buffer
323    */
324   void SendShaderChangeMessage( BufferIndex updateBufferIndex );
325
326   // Undefined
327   RenderableAttachment( const RenderableAttachment& );
328
329   // Undefined
330   RenderableAttachment& operator=( const RenderableAttachment& rhs );
331
332 protected:
333
334   SceneController* mSceneController;   ///< Used for initializing renderers whilst attached
335   Shader*          mShader;            ///< A pointer to the shader
336
337   Dali::Vector< Integration::ResourceId > mTrackedResources; ///< Filled during PrepareResources if there are uncomplete, tracked resources.
338
339   float mSortModifier;
340
341   BlendingMode::Type mBlendingMode:2;  ///< Whether blending is used to render the renderable attachment. 2 bits is enough for 3 values
342
343   bool mUsesGeometryScaling:1;         ///< True if the derived renderer uses scaling.
344   bool mScaleForSizeDirty:1;           ///< True if mScaleForSize has changed in the current frame.
345   bool mUseBlend:1;                    ///< True if the attachment & renderer should be considered opaque for sorting and blending.
346   bool mHasSizeAndColorFlag:1;         ///< Set during the update algorithm to tell whether this renderer can potentially be seen
347   bool mResourcesReady:1;              ///< Set during the Update algorithm; true if the attachment has resources ready for the current frame.
348   bool mFinishedResourceAcquisition:1; ///< Set during DoPrepareResources; true if ready & all resource acquisition has finished (successfully or otherwise)
349   bool mHasUntrackedResources:1;       ///< Set during PrepareResources, true if have tried to follow untracked resources
350   CullFaceMode mCullFaceMode:3;        ///< Cullface mode, 3 bits is enough for 4 values
351
352 };
353
354 // Messages for RenderableAttachment
355
356 inline void SetSortModifierMessage( EventThreadServices& eventThreadServices, const RenderableAttachment& attachment, float modifier )
357 {
358   typedef MessageValue1< RenderableAttachment, float > LocalType;
359
360   // Reserve some memory inside the message queue
361   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
362
363   // Construct message in the message queue memory; note that delete should not be called on the return value
364   new (slot) LocalType( &attachment, &RenderableAttachment::SetSortModifier, modifier );
365 }
366
367 inline void SetCullFaceMessage( EventThreadServices& eventThreadServices, const RenderableAttachment& attachment, CullFaceMode mode )
368 {
369   typedef MessageDoubleBuffered1< RenderableAttachment, CullFaceMode > LocalType;
370
371   // Reserve some memory inside the message queue
372   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
373
374   // Construct message in the message queue memory; note that delete should not be called on the return value
375   new (slot) LocalType( &attachment, &RenderableAttachment::SetCullFace, mode );
376 }
377
378 inline void SetBlendingModeMessage( EventThreadServices& eventThreadServices, const RenderableAttachment& attachment, BlendingMode::Type mode )
379 {
380   typedef MessageValue1< RenderableAttachment, BlendingMode::Type > LocalType;
381
382   // Reserve some memory inside the message queue
383   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
384
385   new (slot) LocalType( &attachment, &RenderableAttachment::SetBlendingMode, mode );
386 }
387
388 inline void SetBlendingOptionsMessage( EventThreadServices& eventThreadServices, const RenderableAttachment& attachment, unsigned int options )
389 {
390   typedef MessageDoubleBuffered1< RenderableAttachment, unsigned int > LocalType;
391
392   // Reserve some memory inside the message queue
393   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
394
395   new (slot) LocalType( &attachment, &RenderableAttachment::SetBlendingOptions, options );
396 }
397
398 inline void SetBlendColorMessage( EventThreadServices& eventThreadServices, const RenderableAttachment& attachment, const Vector4& color )
399 {
400   typedef MessageDoubleBuffered1< RenderableAttachment, Vector4 > LocalType;
401
402   // Reserve some memory inside the message queue
403   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
404
405   new (slot) LocalType( &attachment, &RenderableAttachment::SetBlendColor, color );
406 }
407
408 inline void SetSamplerMessage( EventThreadServices& eventThreadServices, const RenderableAttachment& attachment, unsigned int samplerBitfield )
409 {
410   typedef MessageDoubleBuffered1< RenderableAttachment, unsigned int > LocalType;
411
412   // Reserve some memory inside the message queue
413   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
414
415   // Construct message in the message queue memory; note that delete should not be called on the return value
416   new (slot) LocalType( &attachment, &RenderableAttachment::SetSampler, samplerBitfield );
417 }
418
419 inline void ApplyShaderMessage( EventThreadServices& eventThreadServices, const RenderableAttachment& attachment, const Shader& constShader )
420 {
421   // Update thread can edit the object
422   Shader& shader = const_cast< Shader& >( constShader );
423
424   typedef MessageDoubleBuffered1< RenderableAttachment, Shader* > LocalType;
425
426   // Reserve some memory inside the message queue
427   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
428
429   // Construct message in the message queue memory; note that delete should not be called on the return value
430   new (slot) LocalType( &attachment, &RenderableAttachment::ApplyShader, &shader );
431 }
432
433 inline void RemoveShaderMessage( EventThreadServices& eventThreadServices, const RenderableAttachment& attachment )
434 {
435   typedef MessageDoubleBuffered0< RenderableAttachment > LocalType;
436
437   // Reserve some memory inside the message queue
438   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
439
440   // Construct message in the message queue memory; note that delete should not be called on the return value
441   new (slot) LocalType( &attachment, &RenderableAttachment::RemoveShader );
442 }
443
444 } // namespace SceneGraph
445
446 } // namespace Internal
447
448 } // namespace Dali
449
450 #endif // __DALI_INTERNAL_SCENE_GRAPH_RENDERABLE_ATTACHMENT_H__