Tizen 2.4.0 rev3 SDK Public Release
[framework/graphics/dali.git] / dali / internal / update / node-attachments / scene-graph-image-attachment.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_IMAGE_ATTACHMENT_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_IMAGE_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/public-api/actors/image-actor.h>
23 #include <dali/public-api/math/rect.h>
24 #include <dali/public-api/shader-effects/shader-effect.h>
25 #include <dali/internal/event/common/event-thread-services.h>
26 #include <dali/internal/update/node-attachments/scene-graph-renderable-attachment.h>
27 #include <dali/internal/update/resources/bitmap-metadata.h>
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 // value types used by messages
36 template <> struct ParameterType< Dali::ImageActor::Style >
37 : public BasicType< Dali::ImageActor::Style > {};
38
39 namespace SceneGraph
40 {
41 class ImageRenderer;
42 class Shader;
43 class RenderQueue;
44
45 /**
46  * An attachment for rendering images in various ways.
47  */
48 class ImageAttachment : public RenderableAttachment
49 {
50 public:
51
52   typedef Dali::ImageActor::Style     Style;
53   typedef Dali::ImageActor::PixelArea PixelArea;
54
55   /**
56    * Create a new ImageAttachment.
57    * @param [in] textureId The resource ID of a texture.
58    * @return The newly allocated ImageAttachment.
59    */
60   static ImageAttachment* New( unsigned int textureId );
61
62   /**
63    * Virtual destructor
64    */
65   virtual ~ImageAttachment();
66
67   /**
68    * @copydoc RenderableAttachment::GetRenderer().
69    */
70   virtual Renderer& GetRenderer();
71
72   /**
73    * @copydoc RenderableAttachment::GetRenderer().
74    */
75   virtual const Renderer& GetRenderer() const;
76
77   /**
78    * Set the ID used to retrieve a texture from ResourceManager.
79    * @param[in] updateBufferIndex The current update buffer index.
80    * @param[in] textureId The texture ID.
81    */
82   void SetTextureId( BufferIndex updateBufferIndex, unsigned int textureId );
83
84   /**
85    * Set the area of the texture to display.
86    * @param[in] updateBufferIndex The current update buffer index.
87    * @param [in] pixelArea The area to display, in pixels relative to the top-left (0,0) of the image.
88    */
89   void SetPixelArea( BufferIndex updateBufferIndex, const PixelArea& pixelArea );
90
91   /**
92    * Query whether a pixel area has been set.
93    * @return True if a pixel area has been set.
94    */
95   bool IsPixelAreaSet() const
96   {
97     return mIsPixelAreaSet;
98   }
99
100   /**
101    * Remove any pixel areas specified with SetPixelArea; the entire image will be displayed.
102    * @pre image must be initialized.
103    */
104   void ClearPixelArea();
105
106   /**
107    * Set how the attachment is rendered; the default is STYLE_QUAD.
108    * @param [in] style The new style.
109    */
110   void SetStyle( Style style );
111
112   /**
113    * Retrieve how the attachment is rendered.
114    * @return The style.
115    */
116   Style GetStyle()
117   {
118     return mStyle;
119   }
120
121   /**
122    * Set the border; this is applied with STYLE_NINE_PATCH.
123    * @param [in] updateBufferIndex to use.
124    * @param [in] border The new border setting.
125    * @param [in] inPixels if the border is in pixels.
126    */
127   void SetBorder( BufferIndex updateBufferIndex, const Vector4& border, bool inPixels );
128
129   /**
130    * Set the blending options. This should only be called from the update-thread.
131    * @param[in] updateBufferIndex The current update buffer index.
132    * @param[in] options A bitmask of blending options.
133    */
134   void SetBlendingOptions( BufferIndex updateBufferIndex, unsigned int options );
135
136   /**
137    * Set the blend-color. This should only be called from the update-thread.
138    * @param[in] updateBufferIndex The current update buffer index.
139    * @param[in] color The new blend-color.
140    */
141   void SetBlendColor( BufferIndex updateBufferIndex, const Vector4& color );
142
143   /**
144    * Set the face-culling mode.
145    * @param[in] updateBufferIndex The current update buffer index.
146    * @param[in] mode The face-culling mode.
147    */
148   void SetCullFace( BufferIndex updateBufferIndex, CullFaceMode mode );
149
150   /**
151    * Set the sampler used to render the texture for this renderable.
152    * @param[in] updateBufferIndex The current update buffer index.
153    * @param[in] samplerBitfield The image sampler packed options to set.
154    */
155   void SetSampler( BufferIndex updateBufferIndex, unsigned int samplerBitfield );
156
157   /**
158    * Apply a shader on the renderable
159    * @param[in] updateBufferIndex The current update buffer index.
160    * @param[in] shader to apply.
161    */
162   void ApplyShader( BufferIndex updateBufferIndex, Shader* shader );
163
164   /**
165    * Remove the shader from the renderable
166    * @param[in] updateBufferIndex The current update buffer index.
167    */
168   void RemoveShader( BufferIndex updateBufferIndex );
169
170   /**
171    * Called to notify that the shader might have been changed
172    * The implementation should recalculate geometry and scale based on the
173    * hints from the new shader
174    * @param[in] updateBufferIndex The current update buffer index.
175    * @return Return true if the geometry changed.
176    */
177   virtual void ShaderChanged( BufferIndex updateBufferIndex );
178
179   /**
180    * @copydoc RenderableAttachment::SizeChanged()
181    */
182   virtual void SizeChanged( BufferIndex updateBufferIndex );
183
184   /**
185    * @copydoc RenderableAttachment::DoPrepareRender()
186    */
187   virtual void DoPrepareRender( BufferIndex updateBufferIndex );
188
189   /**
190    * @copydoc RenderableAttachment::IsFullyOpaque()
191    */
192   virtual bool IsFullyOpaque( BufferIndex updateBufferIndex );
193
194 protected:
195
196   /**
197    * Protected constructor
198    */
199   ImageAttachment( unsigned int textureId );
200
201 private:
202
203   /**
204    * Sends the cull-face mode to the renderer
205    * @param updateBufferIndex for the message buffer
206    */
207   void SendCullFaceChangeMessage( BufferIndex updateBufferIndex );
208
209   /**
210    * Sends the shader to the renderer
211    * @param updateBufferIndex for the message buffer
212    */
213   void SendShaderChangeMessage( BufferIndex updateBufferIndex );
214
215   /**
216    * @copydoc RenderableAttachment::Initialize2().
217    */
218   virtual void Initialize2( BufferIndex updateBufferIndex );
219
220   /**
221    * @copydoc RenderableAttachment::OnDestroy2().
222    */
223   virtual void OnDestroy2();
224
225   /**
226    * @copydoc NodeAttachment::ConnectedToSceneGraph()
227    */
228   virtual void ConnectedToSceneGraph();
229
230   /**
231    * @copydoc NodeAttachment::DisconnectedFromSceneGraph()
232    */
233   virtual void DisconnectedFromSceneGraph();
234
235   /**
236    * @copydoc RenderableAttachment::DoPrepareResources()
237    */
238   virtual bool DoPrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager );
239
240   // Helper to check whether a geometry hint was set
241   bool PreviousHintEnabled( Dali::ShaderEffect::GeometryHints hint ) const
242   {
243     return mPreviousRefreshHints & hint;
244   }
245
246   // Undefined
247   ImageAttachment(const ImageAttachment&);
248
249   // Undefined
250   ImageAttachment& operator=(const ImageAttachment& rhs);
251
252 private: // Data
253
254   ImageRenderer* mImageRenderer; ///< Raw-pointers to renderer that is owned by RenderManager
255   unsigned int mTextureId;        ///< The resource ID for a texture
256
257   // bitfields to fit in single byte
258   bool mRefreshMeshData      : 1; ///< Whether the vertex/index buffers needs regenerating
259   bool mIsPixelAreaSet       : 1; ///< Whether pixel area is set, cached for image actor to be able to ask for it
260   int  mPreviousRefreshHints : 4; ///< The shader geometry hints, when the vertex buffer was last refreshed, 4 bits is enough as there's 4 flags
261   Style mStyle               : 2; ///< rendering style, 2 bits is enough as only 2 values in the enum
262   CullFaceMode mCullFaceMode : 3; ///< Cullface mode, 3 bits is enough for 4 values
263   bool mUseBlend             : 1; ///< True if the attachment & renderer should be considered opaque for sorting and blending.
264
265   BitmapMetadata  mBitmapMetadata;///< The bitmap metadata
266   Vector2 mGeometrySize;          ///< The size of the currently used geometry
267
268 };
269
270 // Messages for ImageAttachment
271
272 inline void SetTextureIdMessage( EventThreadServices& eventThreadServices, const ImageAttachment& attachment, unsigned int id )
273 {
274   typedef MessageDoubleBuffered1< ImageAttachment, unsigned int > LocalType;
275
276   // Reserve some memory inside the message queue
277   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
278
279   // Construct message in the message queue memory; note that delete should not be called on the return value
280   new (slot) LocalType( &attachment, &ImageAttachment::SetTextureId, id );
281 }
282
283 inline void SetPixelAreaMessage( EventThreadServices& eventThreadServices, const ImageAttachment& attachment, const Dali::ImageActor::PixelArea& area )
284 {
285   typedef MessageDoubleBuffered1< ImageAttachment, Dali::ImageActor::PixelArea > LocalType;
286
287   // Reserve some memory inside the message queue
288   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
289
290   // Construct message in the message queue memory; note that delete should not be called on the return value
291   new (slot) LocalType( &attachment, &ImageAttachment::SetPixelArea, area );
292 }
293
294 inline void ClearPixelAreaMessage( EventThreadServices& eventThreadServices, const ImageAttachment& attachment )
295 {
296   typedef Message< ImageAttachment > LocalType;
297
298   // Reserve some memory inside the message queue
299   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
300
301   // Construct message in the message queue memory; note that delete should not be called on the return value
302   new (slot) LocalType( &attachment, &ImageAttachment::ClearPixelArea );
303 }
304
305 inline void SetStyleMessage( EventThreadServices& eventThreadServices, const ImageAttachment& attachment, Dali::ImageActor::Style style )
306 {
307   typedef MessageValue1< ImageAttachment, Dali::ImageActor::Style > LocalType;
308
309   // Reserve some memory inside the message queue
310   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
311
312   // Construct message in the message queue memory; note that delete should not be called on the return value
313   new (slot) LocalType( &attachment, &ImageAttachment::SetStyle, style );
314 }
315
316 inline void SetNinePatchBorderMessage( EventThreadServices& eventThreadServices, const ImageAttachment& attachment, const Vector4& border, bool inPixels )
317 {
318   typedef MessageDoubleBuffered2< ImageAttachment, Vector4, bool > LocalType;
319
320   // Reserve some memory inside the message queue
321   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
322
323   // Construct message in the message queue memory; note that delete should not be called on the return value
324   new (slot) LocalType( &attachment, &ImageAttachment::SetBorder, border, inPixels );
325 }
326
327 inline void SetSortModifierMessage( EventThreadServices& eventThreadServices, const ImageAttachment& attachment, float modifier )
328 {
329   typedef MessageValue1< ImageAttachment, float > LocalType;
330
331   // Reserve some memory inside the message queue
332   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
333
334   // Construct message in the message queue memory; note that delete should not be called on the return value
335   new (slot) LocalType( &attachment, &ImageAttachment::SetSortModifier, modifier );
336 }
337
338 inline void SetCullFaceMessage( EventThreadServices& eventThreadServices, const ImageAttachment& attachment, CullFaceMode mode )
339 {
340   typedef MessageDoubleBuffered1< ImageAttachment, CullFaceMode > LocalType;
341
342   // Reserve some memory inside the message queue
343   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
344
345   // Construct message in the message queue memory; note that delete should not be called on the return value
346   new (slot) LocalType( &attachment, &ImageAttachment::SetCullFace, mode );
347 }
348
349 inline void SetBlendingOptionsMessage( EventThreadServices& eventThreadServices, const ImageAttachment& attachment, unsigned int options )
350 {
351   typedef MessageDoubleBuffered1< ImageAttachment, unsigned int > LocalType;
352
353   // Reserve some memory inside the message queue
354   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
355
356   new (slot) LocalType( &attachment, &ImageAttachment::SetBlendingOptions, options );
357 }
358
359 inline void SetBlendColorMessage( EventThreadServices& eventThreadServices, const ImageAttachment& attachment, const Vector4& color )
360 {
361   typedef MessageDoubleBuffered1< ImageAttachment, Vector4 > LocalType;
362
363   // Reserve some memory inside the message queue
364   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
365
366   new (slot) LocalType( &attachment, &ImageAttachment::SetBlendColor, color );
367 }
368
369 inline void SetSamplerMessage( EventThreadServices& eventThreadServices, const ImageAttachment& attachment, unsigned int samplerBitfield )
370 {
371   typedef MessageDoubleBuffered1< ImageAttachment, unsigned int > LocalType;
372
373   // Reserve some memory inside the message queue
374   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
375
376   // Construct message in the message queue memory; note that delete should not be called on the return value
377   new (slot) LocalType( &attachment, &ImageAttachment::SetSampler, samplerBitfield );
378 }
379
380 inline void ApplyShaderMessage( EventThreadServices& eventThreadServices, const ImageAttachment& attachment, const Shader& constShader )
381 {
382   // Update thread can edit the object
383   Shader& shader = const_cast< Shader& >( constShader );
384
385   typedef MessageDoubleBuffered1< ImageAttachment, Shader* > LocalType;
386
387   // Reserve some memory inside the message queue
388   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
389
390   // Construct message in the message queue memory; note that delete should not be called on the return value
391   new (slot) LocalType( &attachment, &ImageAttachment::ApplyShader, &shader );
392 }
393
394 inline void RemoveShaderMessage( EventThreadServices& eventThreadServices, const ImageAttachment& attachment )
395 {
396   typedef MessageDoubleBuffered0< ImageAttachment > LocalType;
397
398   // Reserve some memory inside the message queue
399   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
400
401   // Construct message in the message queue memory; note that delete should not be called on the return value
402   new (slot) LocalType( &attachment, &ImageAttachment::RemoveShader );
403 }
404
405 } // namespace SceneGraph
406
407 } // namespace Internal
408
409 } // namespace Dali
410
411 #endif // __DALI_INTERNAL_SCENE_GRAPH_IMAGE_ATTACHMENT_H__