[dali_1.0.9] Merge branch 'tizen'
[platform/core/uifw/dali-core.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/common/event-to-update.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    * @copydoc RenderableAttachment::ShaderChanged()
131    */
132   virtual void ShaderChanged( BufferIndex updateBufferIndex );
133
134   /**
135    * @copydoc RenderableAttachment::SizeChanged()
136    */
137   virtual void SizeChanged( BufferIndex updateBufferIndex );
138
139   /**
140    * @copydoc RenderableAttachment::DoPrepareRender()
141    */
142   virtual void DoPrepareRender( BufferIndex updateBufferIndex );
143
144   /**
145    * @copydoc RenderableAttachment::IsFullyOpaque()
146    */
147   virtual bool IsFullyOpaque( BufferIndex updateBufferIndex );
148
149 protected:
150
151   /**
152    * Protected constructor
153    */
154   ImageAttachment( unsigned int textureId );
155
156 private:
157
158   /**
159    * @copydoc RenderableAttachment::ConnectToSceneGraph2().
160    */
161   virtual void ConnectToSceneGraph2( BufferIndex updateBufferIndex );
162
163   /**
164    * @copydoc RenderableAttachment::OnDestroy2().
165    */
166   virtual void OnDestroy2();
167
168   /**
169    * @copydoc RenderableAttachment::DoPrepareResources()
170    */
171   virtual bool DoPrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager );
172
173   // Helper to check whether a geometry hint was set
174   bool PreviousHintEnabled( Dali::ShaderEffect::GeometryHints hint ) const
175   {
176     return mPreviousRefreshHints & hint;
177   }
178
179   // Undefined
180   ImageAttachment(const ImageAttachment&);
181
182   // Undefined
183   ImageAttachment& operator=(const ImageAttachment& rhs);
184
185 private: // Data
186
187   ImageRenderer* mImageRenderer; ///< Raw-pointers to renderer that is owned by RenderManager
188   unsigned int mTextureId;        ///< The resource ID for a texture
189
190   // bitfields to fit in single byte
191   bool mRefreshMeshData      : 1; ///< Whether the vertex/index buffers needs regenerating
192   bool mIsPixelAreaSet       : 1; ///< Whether pixel area is set, cached for image actor to be able to ask for it
193   int  mPreviousRefreshHints : 4; ///< The shader geometry hints, when the vertex buffer was last refreshed, 4 bits is enough as there's 4 flags
194   Style mStyle               : 2; ///< rendering style, 2 bits is enough as only 2 values in the enum
195
196   BitmapMetadata  mBitmapMetadata;///< The bitmap metadata
197   Vector2 mGeometrySize;          ///< The size of the currently used geometry
198
199 };
200
201 // Messages for ImageAttachment
202
203 inline void SetTextureIdMessage( EventToUpdate& eventToUpdate, const ImageAttachment& attachment, unsigned int id )
204 {
205   typedef MessageDoubleBuffered1< ImageAttachment, unsigned int > LocalType;
206
207   // Reserve some memory inside the message queue
208   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
209
210   // Construct message in the message queue memory; note that delete should not be called on the return value
211   new (slot) LocalType( &attachment, &ImageAttachment::SetTextureId, id );
212 }
213
214 inline void SetPixelAreaMessage( EventToUpdate& eventToUpdate, const ImageAttachment& attachment, const Dali::ImageActor::PixelArea& area )
215 {
216   typedef MessageDoubleBuffered1< ImageAttachment, Dali::ImageActor::PixelArea > LocalType;
217
218   // Reserve some memory inside the message queue
219   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
220
221   // Construct message in the message queue memory; note that delete should not be called on the return value
222   new (slot) LocalType( &attachment, &ImageAttachment::SetPixelArea, area );
223 }
224
225 inline void ClearPixelAreaMessage( EventToUpdate& eventToUpdate, const ImageAttachment& attachment )
226 {
227   typedef Message< ImageAttachment > LocalType;
228
229   // Reserve some memory inside the message queue
230   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
231
232   // Construct message in the message queue memory; note that delete should not be called on the return value
233   new (slot) LocalType( &attachment, &ImageAttachment::ClearPixelArea );
234 }
235
236 inline void SetStyleMessage( EventToUpdate& eventToUpdate, const ImageAttachment& attachment, Dali::ImageActor::Style style )
237 {
238   typedef MessageValue1< ImageAttachment, Dali::ImageActor::Style > LocalType;
239
240   // Reserve some memory inside the message queue
241   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
242
243   // Construct message in the message queue memory; note that delete should not be called on the return value
244   new (slot) LocalType( &attachment, &ImageAttachment::SetStyle, style );
245 }
246
247 inline void SetNinePatchBorderMessage( EventToUpdate& eventToUpdate, const ImageAttachment& attachment, const Vector4& border, bool inPixels )
248 {
249   typedef MessageDoubleBuffered2< ImageAttachment, Vector4, bool > LocalType;
250
251   // Reserve some memory inside the message queue
252   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
253
254   // Construct message in the message queue memory; note that delete should not be called on the return value
255   new (slot) LocalType( &attachment, &ImageAttachment::SetBorder, border, inPixels );
256 }
257
258 } // namespace SceneGraph
259
260 } // namespace Internal
261
262 } // namespace Dali
263
264 #endif // __DALI_INTERNAL_SCENE_GRAPH_IMAGE_ATTACHMENT_H__