Text: Quality improvement
[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 Flora License, Version 1.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://floralicense.org/license/
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/public-api/actors/image-actor.h>
22 #include <dali/public-api/math/rect.h>
23 #include <dali/public-api/shader-effects/shader-effect.h>
24 #include <dali/internal/common/event-to-update.h>
25 #include <dali/internal/update/node-attachments/scene-graph-renderable-attachment.h>
26 #include <dali/internal/update/resources/bitmap-metadata.h>
27
28 namespace Dali
29 {
30
31 namespace Internal
32 {
33
34 // value types used by messages
35 template <> struct ParameterType< Dali::ImageActor::Style >
36 : public BasicType< Dali::ImageActor::Style > {};
37
38 namespace SceneGraph
39 {
40 class ImageRenderer;
41 class Shader;
42 class RenderQueue;
43
44 /**
45  * An attachment for rendering images in various ways.
46  */
47 class ImageAttachment : public RenderableAttachment
48 {
49 public:
50
51   typedef Dali::ImageActor::Style     Style;
52   typedef Dali::ImageActor::PixelArea PixelArea;
53
54   /**
55    * Create a new ImageAttachment.
56    * @param [in] textureId The resource ID of a texture.
57    * @return The newly allocated ImageAttachment.
58    */
59   static ImageAttachment* New( unsigned int textureId );
60
61   /**
62    * Virtual destructor
63    */
64   virtual ~ImageAttachment();
65
66   /**
67    * @copydoc RenderableAttachment::GetRenderer().
68    */
69   virtual Renderer& GetRenderer();
70
71   /**
72    * @copydoc RenderableAttachment::GetRenderer().
73    */
74   virtual const Renderer& GetRenderer() const;
75
76   /**
77    * Set the ID used to retrieve a texture from ResourceManager.
78    * @param[in] updateBufferIndex The current update buffer index.
79    * @param[in] textureId The texture ID.
80    */
81   void SetTextureId( BufferIndex updateBufferIndex, unsigned int textureId );
82
83   /**
84    * Set the area of the texture to display.
85    * @param[in] updateBufferIndex The current update buffer index.
86    * @param [in] pixelArea The area to display, in pixels relative to the top-left (0,0) of the image.
87    */
88   void SetPixelArea( BufferIndex updateBufferIndex, const PixelArea& pixelArea );
89
90   /**
91    * Query whether a pixel area has been set.
92    * @return True if a pixel area has been set.
93    */
94   bool IsPixelAreaSet() const
95   {
96     return mIsPixelAreaSet;
97   }
98
99   /**
100    * Remove any pixel areas specified with SetPixelArea; the entire image will be displayed.
101    * @pre image must be initialized.
102    */
103   void ClearPixelArea();
104
105   /**
106    * Set how the attachment is rendered; the default is STYLE_QUAD.
107    * @param [in] style The new style.
108    */
109   void SetStyle( Style style );
110
111   /**
112    * Retrieve how the attachment is rendered.
113    * @return The style.
114    */
115   Style GetStyle()
116   {
117     return mStyle;
118   }
119
120   /**
121    * Set the border; this is applied with STYLE_NINE_PATCH.
122    * @param [in] updateBufferIndex to use.
123    * @param [in] border The new border setting.
124    * @param [in] inPixels if the border is in pixels.
125    */
126   void SetBorder( BufferIndex updateBufferIndex, const Vector4& border, bool inPixels );
127
128   /**
129    * @copydoc RenderableAttachment::ShaderChanged()
130    */
131   virtual void ShaderChanged( BufferIndex updateBufferIndex );
132
133   /**
134    * @copydoc RenderableAttachment::SizeChanged()
135    */
136   virtual void SizeChanged( BufferIndex updateBufferIndex );
137
138   /**
139    * @copydoc RenderableAttachment::DoPrepareRender()
140    */
141   virtual void DoPrepareRender( BufferIndex updateBufferIndex );
142
143   /**
144    * @copydoc RenderableAttachment::IsFullyOpaque()
145    */
146   virtual bool IsFullyOpaque( BufferIndex updateBufferIndex );
147
148 protected:
149
150   /**
151    * Protected constructor
152    */
153   ImageAttachment( unsigned int textureId );
154
155 private:
156
157   /**
158    * @copydoc RenderableAttachment::ConnectToSceneGraph2().
159    */
160   virtual void ConnectToSceneGraph2( BufferIndex updateBufferIndex );
161
162   /**
163    * @copydoc RenderableAttachment::OnDestroy2().
164    */
165   virtual void OnDestroy2();
166
167   /**
168    * @copydoc RenderableAttachment::DoPrepareResources()
169    */
170   virtual bool DoPrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager );
171
172   // Helper to check whether a geometry hint was set
173   bool PreviousHintEnabled( Dali::ShaderEffect::GeometryHints hint ) const
174   {
175     return mPreviousRefreshHints & hint;
176   }
177
178   // Undefined
179   ImageAttachment(const ImageAttachment&);
180
181   // Undefined
182   ImageAttachment& operator=(const ImageAttachment& rhs);
183
184 private: // Data
185
186   ImageRenderer* mImageRenderer; ///< Raw-pointers to renderer that is owned by RenderManager
187   unsigned int mTextureId;        ///< The resource ID for a texture
188
189   // bitfields to fit in single byte
190   bool mRefreshMeshData      : 1; ///< Whether the vertex/index buffers needs regenerating
191   bool mIsPixelAreaSet       : 1; ///< Whether pixel area is set, cached for image actor to be able to ask for it
192   int  mPreviousRefreshHints : 4; ///< The shader geometry hints, when the vertex buffer was last refreshed, 4 bits is enough as there's 4 flags
193   Style mStyle               : 2; ///< rendering style, 2 bits is enough as only 2 values in the enum
194
195   BitmapMetadata  mBitmapMetadata;///< The bitmap metadata
196   Vector2 mGeometrySize;          ///< The size of the currently used geometry
197
198 };
199
200 // Messages for ImageAttachment
201
202 inline void SetTextureIdMessage( EventToUpdate& eventToUpdate, const ImageAttachment& attachment, unsigned int id )
203 {
204   typedef MessageDoubleBuffered1< ImageAttachment, unsigned int > LocalType;
205
206   // Reserve some memory inside the message queue
207   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
208
209   // Construct message in the message queue memory; note that delete should not be called on the return value
210   new (slot) LocalType( &attachment, &ImageAttachment::SetTextureId, id );
211 }
212
213 inline void SetPixelAreaMessage( EventToUpdate& eventToUpdate, const ImageAttachment& attachment, const Dali::ImageActor::PixelArea& area )
214 {
215   typedef MessageDoubleBuffered1< ImageAttachment, Dali::ImageActor::PixelArea > LocalType;
216
217   // Reserve some memory inside the message queue
218   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
219
220   // Construct message in the message queue memory; note that delete should not be called on the return value
221   new (slot) LocalType( &attachment, &ImageAttachment::SetPixelArea, area );
222 }
223
224 inline void ClearPixelAreaMessage( EventToUpdate& eventToUpdate, const ImageAttachment& attachment )
225 {
226   typedef Message< ImageAttachment > LocalType;
227
228   // Reserve some memory inside the message queue
229   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
230
231   // Construct message in the message queue memory; note that delete should not be called on the return value
232   new (slot) LocalType( &attachment, &ImageAttachment::ClearPixelArea );
233 }
234
235 inline void SetStyleMessage( EventToUpdate& eventToUpdate, const ImageAttachment& attachment, Dali::ImageActor::Style style )
236 {
237   typedef MessageValue1< ImageAttachment, Dali::ImageActor::Style > LocalType;
238
239   // Reserve some memory inside the message queue
240   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
241
242   // Construct message in the message queue memory; note that delete should not be called on the return value
243   new (slot) LocalType( &attachment, &ImageAttachment::SetStyle, style );
244 }
245
246 inline void SetNinePatchBorderMessage( EventToUpdate& eventToUpdate, const ImageAttachment& attachment, const Vector4& border, bool inPixels )
247 {
248   typedef MessageDoubleBuffered2< ImageAttachment, Vector4, bool > LocalType;
249
250   // Reserve some memory inside the message queue
251   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
252
253   // Construct message in the message queue memory; note that delete should not be called on the return value
254   new (slot) LocalType( &attachment, &ImageAttachment::SetBorder, border, inPixels );
255 }
256
257 } // namespace SceneGraph
258
259 } // namespace Internal
260
261 } // namespace Dali
262
263 #endif // __DALI_INTERNAL_SCENE_GRAPH_IMAGE_ATTACHMENT_H__