Refactored EventToUpdate into EventThreadServices
[platform/core/uifw/dali-core.git] / dali / internal / update / node-attachments / scene-graph-text-attachment.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_TEXT_ATTACHMENT_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_TEXT_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/owner-pointer.h>
23 #include <dali/internal/event/common/event-thread-services.h>
24 #include <dali/internal/update/resources/resource-manager.h>
25 #include <dali/internal/update/node-attachments/scene-graph-renderable-attachment.h>
26
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32
33 class TextParameters;
34
35 namespace SceneGraph
36 {
37
38 class TextRenderer;
39 class Shader;
40 class RenderQueue;
41
42 /**
43  * An attachment for rendering text from a font atlas.
44  */
45 class TextAttachment : public RenderableAttachment
46 {
47 public:
48
49   /**
50    * Construct a new TextAttachment.
51    * @return a new TextAttachment
52    */
53   static TextAttachment* New();
54
55   /**
56    * Virtual destructor
57    */
58   virtual ~TextAttachment();
59
60   /**
61    * @copydoc RenderableAttachment::GetRenderer().
62    */
63   virtual Renderer& GetRenderer();
64
65   /**
66    * @copydoc RenderableAttachment::GetRenderer().
67    */
68   virtual const Renderer& GetRenderer() const;
69
70   /**
71    * Set the text vertex buffer; ownership is passed to the renderer.
72    * This is to avoid duplicate data being held in the text-attachment and the scene-graph-text-attachment.
73    * @param[in] updateBufferIndex The current update buffer index.
74    * @param[in] vertexBuffer vertex buffer
75    */
76   void SetTextVertexBuffer( BufferIndex updateBufferIndex, TextVertexBuffer* vertexBuffer );
77
78   /**
79    * Set the pixel size of the font, this is passed to the renderer
80    * @param updateBufferIndex The current update buffer index.
81    * @param pixelSize The new pixel size
82    */
83   void SetTextFontSize( BufferIndex updateBufferIndex, float pixelSize );
84
85   /**
86    * Sets the gradient start point, the gradient end point and the color associated with the gradient end point.
87    * @param[in] updateBufferIndex The current update buffer index.
88    * @param[in] color The gradient color (end-point color)
89    * @param[in] startPoint The relative position of the gradient start point.
90    * @param[in] endPoint The relative position of the gradient end point.
91    */
92   void SetGradient( BufferIndex updateBufferIndex, const Vector4& color, const Vector2& startPoint, const Vector2& endPoint );
93
94   /**
95    * Set the text color.
96    * @param[in] updateBufferIndex The current update buffer index.
97    * @param[in] color The text color.
98    */
99   void SetTextColor( BufferIndex updateBufferIndex, const Vector4& color );
100
101   /**
102    * Set text outlining.
103    * @param[in] updateBufferIndex The current update buffer index.
104    * @param[in] enable Set to true to enable text outlining.
105    * @param[in] color Outline color.
106    * @param[in] thickness Thickness of outline.
107    */
108   void SetOutline( BufferIndex updateBufferIndex, bool enable, const Vector4& color, const Vector2& thickness );
109
110   /**
111    * Set text glow.
112    * @param[in] updateBufferIndex The current update buffer index.
113    * @param[in] enable Set to true to enable text outer glow.
114    * @param[in] color Glow color.
115    * @param[in] intensity Determines the amount of glow around text.
116    */
117   void SetGlow( BufferIndex updateBufferIndex, bool enable, const Vector4& color, float intensity );
118
119   /**
120    * Set text shadow.
121    * @param[in] updateBufferIndex The current update buffer index.
122    * @param[in] enable  Set to true to enable text drop shadow.
123    * @param[in] color   Shadow color.
124    * @param[in] offset  Offset in pixels. To avoid cropping of the drop shadow limit the offset to PointSize / 3.5.
125    * @param[in] size    Size of shadow in pixels. 0 means the shadow is the same size as the text.
126    */
127   void SetDropShadow( BufferIndex updateBufferIndex, bool enable, const Vector4& color, const Vector2& offset, const float size );
128
129   /**
130    * Set soft edge smoothing.
131    * @param[in] updateBufferIndex The current update buffer index.
132    * @param[in] smoothEdge Specify the distance field value for the center of the text edge.
133    */
134   void SetSmoothEdge( BufferIndex updateBufferIndex, float smoothEdge );
135
136   /**
137    * @copydoc RenderableAttachment::ShaderChanged()
138    */
139   virtual void ShaderChanged( BufferIndex updateBufferIndex );
140
141   /**
142    * @copydoc RenderableAttachment::SizeChanged()
143    */
144   virtual void SizeChanged( BufferIndex updateBufferIndex );
145
146   /**
147    * @copydoc RenderableAttachment::DoPrepareRender()
148    */
149   virtual void DoPrepareRender( BufferIndex updateBufferIndex );
150
151   /**
152    * @copydoc RenderableAttachment::IsFullyOpaque()
153    */
154   virtual bool IsFullyOpaque( BufferIndex updateBufferIndex )
155   {
156     return false; // false since drawing text requires alpha
157   }
158
159   /**
160    * @copydoc RenderableAttachment::GetScaleForSize()
161    */
162   virtual void DoGetScaleForSize( const Vector3& nodeSize, Vector3& scaling );
163
164 protected:
165
166   /**
167    * Protected constructor. See New()
168    */
169   TextAttachment();
170
171 private:
172
173   /**
174    * @copydoc RenderableAttachment::ConnectToSceneGraph2().
175    */
176   virtual void ConnectToSceneGraph2( BufferIndex updateBufferIndex );
177
178   /**
179    * @copydoc RenderableAttachment::OnDestroy2().
180    */
181   virtual void OnDestroy2();
182
183   /**
184    * @copydoc RenderableAttachment::DoPrepareResources()
185    */
186   virtual bool DoPrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager );
187
188   // Undefined
189   TextAttachment(const TextAttachment&);
190
191   // Undefined
192   TextAttachment& operator=(const TextAttachment& rhs);
193
194 private:
195
196   TextRenderer* mTextRenderer;    ///< Raw-pointers to renderer that is owned by RenderManager
197   Vector2 mGeometrySize;          ///< used for geometry scaling
198   Vector2 mGeometryScaling;       ///< used for geometry scaling
199
200   unsigned int mAtlasId;          ///< atlas Id from last vertex buffer
201
202 };
203
204 inline void SetTextVertexBufferMessage( EventThreadServices& eventThreadServices, const TextAttachment& attachment, TextVertexBuffer& buffer)
205 {
206   typedef MessageDoubleBuffered1< TextAttachment, OwnerPointer< TextVertexBuffer> > LocalType;
207
208   // Reserve some memory inside the message queue
209   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
210
211   // Construct message in the message queue memory; note that delete should not be called on the return value
212   new (slot) LocalType( &attachment, &TextAttachment::SetTextVertexBuffer, &buffer );
213 }
214
215 inline void SetTextFontSizeMessage( EventThreadServices& eventThreadServices, const TextAttachment& attachment, float pixelSize )
216 {
217   typedef MessageDoubleBuffered1< TextAttachment, float > LocalType;
218
219   // Reserve some memory inside the message queue
220   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
221
222   // Construct message in the message queue memory; note that delete should not be called on the return value
223   new (slot) LocalType( &attachment, &TextAttachment::SetTextFontSize, pixelSize );
224 }
225
226 inline void SetGradientMessage( EventThreadServices& eventThreadServices, const TextAttachment& attachment, const Vector4& color, const Vector2& startPoint, const Vector2& endPoint )
227 {
228   typedef MessageDoubleBuffered3< TextAttachment, Vector4, Vector2, Vector2 > LocalType;
229
230   // Reserve some memory inside the message queue
231   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
232
233   // Construct message in the message queue memory; note that delete should not be called on the return value
234   new (slot) LocalType( &attachment, &TextAttachment::SetGradient, color, startPoint, endPoint );
235 }
236
237 inline void SetTextColorMessage( EventThreadServices& eventThreadServices, const TextAttachment& attachment, const Vector4& color )
238 {
239   typedef MessageDoubleBuffered1< TextAttachment, Vector4 > LocalType;
240
241   // Reserve some memory inside the message queue
242   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
243
244   // Construct message in the message queue memory; note that delete should not be called on the return value
245   new (slot) LocalType( &attachment, &TextAttachment::SetTextColor, color );
246 }
247
248 inline void SetOutlineMessage( EventThreadServices& eventThreadServices, const TextAttachment& attachment, bool enable, const Vector4& color, const Vector2& params )
249 {
250   typedef MessageDoubleBuffered3< TextAttachment, bool, Vector4, Vector2 > LocalType;
251
252   // Reserve some memory inside the message queue
253   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
254
255   // Construct message in the message queue memory; note that delete should not be called on the return value
256   new (slot) LocalType( &attachment, &TextAttachment::SetOutline, enable, color, params );
257 }
258
259 inline void SetGlowMessage( EventThreadServices& eventThreadServices, const TextAttachment& attachment, bool enable, const Vector4& color, float params )
260 {
261   typedef MessageDoubleBuffered3< TextAttachment, bool, Vector4, float > LocalType;
262
263   // Reserve some memory inside the message queue
264   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
265
266   // Construct message in the message queue memory; note that delete should not be called on the return value
267   new (slot) LocalType( &attachment, &TextAttachment::SetGlow, enable, color, params );
268 }
269
270 inline void SetDropShadowMessage( EventThreadServices& eventThreadServices, const TextAttachment& attachment, bool enable, const Vector4& color, const Vector2& offset, const float size )
271 {
272   typedef MessageDoubleBuffered4< TextAttachment, bool, Vector4, Vector2, float > LocalType;
273
274   // Reserve some memory inside the message queue
275   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
276
277   // Construct message in the message queue memory; note that delete should not be called on the return value
278   new (slot) LocalType( &attachment, &TextAttachment::SetDropShadow, enable, color, offset, size );
279 }
280
281 inline void SetSmoothEdgeMessage( EventThreadServices& eventThreadServices, const TextAttachment& attachment, float params )
282 {
283   typedef MessageDoubleBuffered1< TextAttachment, float > LocalType;
284
285   // Reserve some memory inside the message queue
286   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
287
288   // Construct message in the message queue memory; note that delete should not be called on the return value
289   new (slot) LocalType( &attachment, &TextAttachment::SetSmoothEdge, params );
290 }
291
292 } // namespace SceneGraph
293
294 } // namespace Internal
295
296 } // namespace Dali
297
298 #endif // __DALI_INTERNAL_SCENE_GRAPH_TEXT_ATTACHMENT_H__