Move new mesh API to devel-api.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / rendering / atlas / text-atlas-renderer.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/text/rendering/atlas/text-atlas-renderer.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/dali.h>
23 #include <dali/devel-api/object/property-buffer.h>
24 #include <dali/devel-api/rendering/geometry.h>
25 #include <dali/devel-api/rendering/renderer.h>
26 #include <dali/devel-api/rendering/sampler.h>
27 #include <dali/devel-api/rendering/shader.h>
28 #include <dali/devel-api/text-abstraction/font-client.h>
29 #include <dali/integration-api/debug.h>
30
31 // INTERNAL INCLUDES
32 #include <dali-toolkit/public-api/controls/control-depth-index-ranges.h>
33 #include <dali-toolkit/internal/text/rendering/atlas/atlas-glyph-manager.h>
34
35 using namespace Dali;
36 using namespace Dali::Toolkit;
37 using namespace Dali::Toolkit::Text;
38
39 namespace
40 {
41 #if defined(DEBUG_ENABLED)
42   Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_RENDERING");
43 #endif
44
45 const float ZERO( 0.0f );
46 const float HALF( 0.5f );
47 const float ONE( 1.0f );
48 const float TWO( 2.0f );
49 const uint32_t DEFAULT_ATLAS_WIDTH = 512u;
50 const uint32_t DEFAULT_ATLAS_HEIGHT = 512u;
51 }
52 struct AtlasRenderer::Impl : public ConnectionTracker
53 {
54
55   enum Style
56   {
57     STYLE_NORMAL,
58     STYLE_DROP_SHADOW
59   };
60
61   struct MeshRecord
62   {
63     Vector4 mColor;
64     uint32_t mAtlasId;
65     AtlasManager::Mesh2D mMesh;
66     FrameBufferImage mBuffer;
67     bool mIsUnderline;
68   };
69
70   struct Extent
71   {
72     float mBaseLine;
73     float mLeft;
74     float mRight;
75     float mUnderlinePosition;
76     float mUnderlineThickness;
77     uint32_t mMeshRecordIndex;
78   };
79
80   struct AtlasRecord
81   {
82     uint32_t mImageId;
83     Text::GlyphIndex mIndex;
84   };
85
86   struct MaxBlockSize
87   {
88     FontId mFontId;
89     uint32_t mNeededBlockWidth;
90     uint32_t mNeededBlockHeight;
91   };
92
93   Impl()
94   {
95     mGlyphManager = AtlasGlyphManager::Get();
96     mFontClient = TextAbstraction::FontClient::Get();
97
98     mQuadVertexFormat[ "aPosition" ] = Property::VECTOR2;
99     mQuadVertexFormat[ "aTexCoord" ] = Property::VECTOR2;
100     mQuadIndexFormat[ "indices" ] = Property::UNSIGNED_INTEGER;
101   }
102
103   void AddGlyphs( const std::vector<Vector2>& positions,
104                   const Vector<GlyphInfo>& glyphs,
105                   const Vector4& textColor,
106                   const Vector2& shadowOffset,
107                   const Vector4& shadowColor,
108                   bool underlineEnabled,
109                   const Vector4& underlineColor,
110                   float underlineHeight )
111   {
112     AtlasManager::AtlasSlot slot;
113     std::vector< MeshRecord > meshContainer;
114     Vector< Extent > extents;
115
116     float currentUnderlinePosition = ZERO;
117     float currentUnderlineThickness = underlineHeight;
118     uint32_t currentBlockSize = 0;
119     FontId lastFontId = 0;
120     Style style = STYLE_NORMAL;
121
122     if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
123     {
124       style = STYLE_DROP_SHADOW;
125     }
126
127     if ( mImageIds.Size() )
128     {
129       // Unreference any currently used glyphs
130       RemoveText();
131     }
132
133     CalculateBlocksSize( glyphs );
134
135     for( uint32_t i = 0, glyphSize = glyphs.Size(); i < glyphSize; ++i )
136     {
137       const GlyphInfo& glyph = glyphs[ i ];
138
139       // No operation for white space
140       if ( glyph.width && glyph.height )
141       {
142         // Are we still using the same fontId as previous
143         if ( glyph.fontId != lastFontId )
144         {
145           // We need to fetch fresh font underline metrics
146           FontMetrics fontMetrics;
147           mFontClient.GetFontMetrics( glyph.fontId, fontMetrics );
148           currentUnderlinePosition = ceil( fabsf( fontMetrics.underlinePosition ) );
149           float descender = ceil( fabsf( fontMetrics.descender ) );
150
151           if ( underlineHeight == ZERO )
152           {
153             currentUnderlineThickness = fontMetrics.underlineThickness;
154
155             // Ensure underline will be at least a pixel high
156             if ( currentUnderlineThickness < ONE )
157             {
158               currentUnderlineThickness = ONE;
159             }
160             else
161             {
162               currentUnderlineThickness = ceil( currentUnderlineThickness );
163             }
164           }
165
166           // Clamp the underline position at the font descender and check for ( as EFL describes it ) a broken font
167           if ( currentUnderlinePosition > descender )
168           {
169             currentUnderlinePosition = descender;
170           }
171           if ( ZERO == currentUnderlinePosition )
172           {
173             // Move offset down by one ( EFL behavior )
174             currentUnderlinePosition = ONE;
175           }
176         }
177
178         const Vector2& position = positions[ i ];
179         AtlasManager::Mesh2D newMesh;
180         mGlyphManager.Cached( glyph.fontId, glyph.index, slot );
181
182         if ( slot.mImageId )
183         {
184           // This glyph already exists so generate mesh data plugging in our supplied position
185           mGlyphManager.GenerateMeshData( slot.mImageId, position, newMesh );
186           mImageIds.PushBack( slot.mImageId );
187         }
188         else
189         {
190
191           // Select correct size for new atlas if needed....?
192           if ( lastFontId != glyph.fontId )
193           {
194             for ( uint32_t j = 0; j < mBlockSizes.size(); ++j )
195             {
196               if ( mBlockSizes[ j ].mFontId == glyph.fontId )
197               {
198                 currentBlockSize = j;
199                 mGlyphManager.SetNewAtlasSize( DEFAULT_ATLAS_WIDTH,
200                                                DEFAULT_ATLAS_HEIGHT,
201                                                mBlockSizes[ j ].mNeededBlockWidth,
202                                                mBlockSizes[ j ].mNeededBlockHeight );
203               }
204             }
205           }
206
207           // Create a new image for the glyph
208           BufferImage bitmap = mFontClient.CreateBitmap( glyph.fontId, glyph.index );
209           if ( bitmap )
210           {
211             // Ensure that the next image will fit into the current block size
212             bool setSize = false;
213             if ( bitmap.GetWidth() > mBlockSizes[ currentBlockSize ].mNeededBlockWidth )
214             {
215               setSize = true;
216               mBlockSizes[ currentBlockSize ].mNeededBlockWidth = bitmap.GetWidth();
217             }
218             if ( bitmap.GetHeight() > mBlockSizes[ currentBlockSize ].mNeededBlockHeight )
219             {
220               setSize = true;
221               mBlockSizes[ currentBlockSize ].mNeededBlockHeight = bitmap.GetHeight();
222             }
223
224             if ( setSize )
225             {
226               mGlyphManager.SetNewAtlasSize( DEFAULT_ATLAS_WIDTH,
227                                              DEFAULT_ATLAS_HEIGHT,
228                                              mBlockSizes[ currentBlockSize ].mNeededBlockWidth,
229                                              mBlockSizes[ currentBlockSize ].mNeededBlockHeight );
230             }
231
232             // Locate a new slot for our glyph
233             mGlyphManager.Add( glyph, bitmap, slot );
234
235             // Generate mesh data for this quad, plugging in our supplied position
236             if ( slot.mImageId )
237             {
238               mGlyphManager.GenerateMeshData( slot.mImageId, position, newMesh );
239               mImageIds.PushBack( slot.mImageId );
240             }
241           }
242         }
243         // Find an existing mesh data object to attach to ( or create a new one, if we can't find one using the same atlas)
244         StitchTextMesh( meshContainer,
245                         newMesh,
246                         extents,
247                         textColor,
248                         position.y + glyph.yBearing,
249                         currentUnderlinePosition,
250                         currentUnderlineThickness,
251                         slot );
252        lastFontId = glyph.fontId;
253       }
254     }
255
256     if ( underlineEnabled )
257     {
258       // Check to see if any of the text needs an underline
259       GenerateUnderlines( meshContainer, extents, underlineColor, textColor );
260     }
261
262     // For each MeshData object, create a mesh actor and add to the renderable actor
263     if ( meshContainer.size() )
264     {
265       for ( std::vector< MeshRecord >::iterator mIt = meshContainer.begin(); mIt != meshContainer.end(); ++mIt )
266       {
267         Actor actor = CreateMeshActor( *mIt );
268
269         // Create an effect if necessary
270         if ( style == STYLE_DROP_SHADOW )
271         {
272           actor.Add( GenerateShadow( *mIt, shadowOffset, shadowColor ) );
273         }
274
275         if ( mActor )
276         {
277           mActor.Add( actor );
278         }
279         else
280         {
281           mActor = actor;
282         }
283       }
284       mActor.OffStageSignal().Connect( this, &AtlasRenderer::Impl::OffStageDisconnect );
285     }
286 #if defined(DEBUG_ENABLED)
287     Toolkit::AtlasGlyphManager::Metrics metrics = mGlyphManager.GetMetrics();
288     DALI_LOG_INFO( gLogFilter, Debug::General, "TextAtlasRenderer::GlyphManager::GlyphCount: %i, AtlasCount: %i, TextureMemoryUse: %iK\n",
289                                                 metrics.mGlyphCount,
290                                                 metrics.mAtlasMetrics.mAtlasCount,
291                                                 metrics.mAtlasMetrics.mTextureMemoryUsed / 1024 );
292     for ( uint32_t i = 0; i < metrics.mAtlasMetrics.mAtlasCount; ++i )
293     {
294       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Atlas [%i] %sPixels: %s Size: %ix%i, BlockSize: %ix%i, BlocksUsed: %i/%i\n",
295                                                  i + 1, i > 8 ? "" : " ",
296                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mPixelFormat == Pixel::L8 ? "L8  " : "BGRA",
297                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mWidth,
298                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mHeight,
299                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mBlockWidth,
300                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mBlockHeight,
301                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mBlocksUsed,
302                                                  metrics.mAtlasMetrics.mAtlasMetrics[ i ].mTotalBlocks );
303     }
304 #endif
305   }
306
307   Actor CreateMeshActor( const MeshRecord& meshRecord )
308   {
309     PropertyBuffer quadVertices = PropertyBuffer::New( mQuadVertexFormat, meshRecord.mMesh.mVertices.Size() );
310     PropertyBuffer quadIndices = PropertyBuffer::New( mQuadIndexFormat, meshRecord.mMesh.mIndices.Size() );
311     quadVertices.SetData( const_cast< AtlasManager::Vertex2D* >( &meshRecord.mMesh.mVertices[ 0 ] ) );
312     quadIndices.SetData( const_cast< unsigned int* >( &meshRecord.mMesh.mIndices[ 0 ] ) );
313
314     Geometry quadGeometry = Geometry::New();
315     quadGeometry.AddVertexBuffer( quadVertices );
316     quadGeometry.SetIndexBuffer( quadIndices );
317
318     Material material = mGlyphManager.GetMaterial( meshRecord.mAtlasId );
319     Dali::Renderer renderer = Dali::Renderer::New( quadGeometry, material );
320     Actor actor = Actor::New();
321     actor.AddRenderer( renderer );
322     actor.SetSize( 1.0f, 1.0f );
323     actor.SetColor( meshRecord.mColor );
324
325     if ( meshRecord.mIsUnderline )
326     {
327       actor.SetColorMode( USE_OWN_COLOR );
328     }
329     else
330     {
331       actor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR );
332     }
333     return actor;
334   }
335
336   void StitchTextMesh( std::vector< MeshRecord >& meshContainer,
337                        AtlasManager::Mesh2D& newMesh,
338                        Vector< Extent >& extents,
339                        const Vector4& color,
340                        float baseLine,
341                        float underlinePosition,
342                        float underlineThickness,
343                        AtlasManager::AtlasSlot& slot )
344   {
345     if ( slot.mImageId )
346     {
347       float left = newMesh.mVertices[ 0 ].mPosition.x;
348       float right = newMesh.mVertices[ 1 ].mPosition.x;
349
350       // Check to see if there's a mesh data object that references the same atlas ?
351       uint32_t index = 0;
352       for ( std::vector< MeshRecord >::iterator mIt = meshContainer.begin(); mIt != meshContainer.end(); ++mIt, ++index )
353       {
354         if ( slot.mAtlasId == mIt->mAtlasId && color == mIt->mColor )
355         {
356           // Stitch the mesh to the existing mesh and adjust any extents
357           mGlyphManager.StitchMesh( mIt->mMesh, newMesh );
358           AdjustExtents( extents,
359                          meshContainer,
360                          index,
361                          left,
362                          right,
363                          baseLine,
364                          underlinePosition,
365                          underlineThickness );
366           return;
367         }
368       }
369
370       // No mesh data object currently exists that references this atlas, so create a new one
371       MeshRecord meshRecord;
372       meshRecord.mAtlasId = slot.mAtlasId;
373       meshRecord.mMesh = newMesh;
374       meshRecord.mColor = color;
375       meshRecord.mIsUnderline = false;
376       meshContainer.push_back( meshRecord );
377
378       // Adjust extents for this new meshrecord
379       AdjustExtents( extents,
380                      meshContainer,
381                      meshContainer.size() - 1u,
382                      left,
383                      right,
384                      baseLine,
385                      underlinePosition,
386                      underlineThickness );
387
388     }
389   }
390
391   void AdjustExtents( Vector< Extent >& extents,
392                       std::vector< MeshRecord>& meshRecords,
393                       uint32_t index,
394                       float left,
395                       float right,
396                       float baseLine,
397                       float underlinePosition,
398                       float underlineThickness )
399   {
400     bool foundExtent = false;
401     for ( Vector< Extent >::Iterator eIt = extents.Begin(); eIt != extents.End(); ++eIt )
402     {
403       if ( Equals( baseLine, eIt->mBaseLine ) )
404       {
405         foundExtent = true;
406         if ( left < eIt->mLeft )
407         {
408           eIt->mLeft = left;
409         }
410         if ( right > eIt->mRight  )
411         {
412           eIt->mRight = right;
413         }
414
415         if ( underlinePosition > eIt->mUnderlinePosition )
416         {
417           eIt->mUnderlinePosition = underlinePosition;
418         }
419         if ( underlineThickness > eIt->mUnderlineThickness )
420         {
421           eIt->mUnderlineThickness = underlineThickness;
422         }
423       }
424     }
425     if ( !foundExtent )
426     {
427       Extent extent;
428       extent.mLeft = left;
429       extent.mRight = right;
430       extent.mBaseLine = baseLine;
431       extent.mUnderlinePosition = underlinePosition;
432       extent.mUnderlineThickness = underlineThickness;
433       extent.mMeshRecordIndex = index;
434       extents.PushBack( extent );
435     }
436   }
437
438   // Unreference any glyphs that were used with this actor
439   void OffStageDisconnect( Dali::Actor actor )
440   {
441     RemoveText();
442   }
443
444   void RemoveText()
445   {
446     for ( uint32_t i = 0; i < mImageIds.Size(); ++i )
447     {
448       mGlyphManager.Remove( mImageIds[ i ] );
449     }
450     mImageIds.Resize( 0 );
451   }
452
453   void CalculateBlocksSize( const Vector<GlyphInfo>& glyphs )
454   {
455     MaxBlockSize maxBlockSize;
456     for ( uint32_t i = 0; i < glyphs.Size(); ++i )
457     {
458       FontId fontId = glyphs[ i ].fontId;
459       bool foundFont = false;
460       for ( uint32_t j = 0; j < mBlockSizes.size(); ++j )
461       {
462         if ( mBlockSizes[ j ].mFontId == fontId )
463         {
464           foundFont = true;
465         }
466       }
467       if ( !foundFont )
468       {
469         FontMetrics fontMetrics;
470         mFontClient.GetFontMetrics( fontId, fontMetrics );
471         maxBlockSize.mNeededBlockWidth = static_cast< uint32_t >( fontMetrics.height );
472         maxBlockSize.mNeededBlockHeight = static_cast< uint32_t >( fontMetrics.height );
473         maxBlockSize.mFontId = fontId;
474         mBlockSizes.push_back( maxBlockSize );
475       }
476     }
477   }
478
479   void GenerateUnderlines( std::vector< MeshRecord>& meshRecords,
480                            Vector< Extent >& extents,
481                            const Vector4& underlineColor,
482                            const Vector4& textColor )
483   {
484     AtlasManager::Mesh2D newMesh;
485     unsigned short faceIndex = 0;
486     for ( Vector< Extent >::ConstIterator eIt = extents.Begin(); eIt != extents.End(); ++eIt )
487     {
488       AtlasManager::Vertex2D vert;
489       uint32_t index = eIt->mMeshRecordIndex;
490       Vector2 uv = mGlyphManager.GetAtlasSize( meshRecords[ index ].mAtlasId );
491
492       // Make sure we don't hit texture edge for single pixel texture ( filled pixel is in top left of every atlas )
493       float u = HALF / uv.x;
494       float v = HALF / uv.y;
495       float thickness = eIt->mUnderlineThickness;
496       float baseLine = eIt->mBaseLine + eIt->mUnderlinePosition - ( thickness * HALF );
497       float tlx = eIt->mLeft;
498       float brx = eIt->mRight;
499
500       vert.mPosition.x = tlx;
501       vert.mPosition.y = baseLine;
502       vert.mTexCoords.x = ZERO;
503       vert.mTexCoords.y = ZERO;
504       newMesh.mVertices.PushBack( vert );
505
506       vert.mPosition.x = brx;
507       vert.mPosition.y = baseLine;
508       vert.mTexCoords.x = u;
509       newMesh.mVertices.PushBack( vert );
510
511       vert.mPosition.x = tlx;
512       vert.mPosition.y = baseLine + thickness;
513       vert.mTexCoords.x = ZERO;
514       vert.mTexCoords.y = v;
515       newMesh.mVertices.PushBack( vert );
516
517       vert.mPosition.x = brx;
518       vert.mPosition.y = baseLine + thickness;
519       vert.mTexCoords.x = u;
520       newMesh.mVertices.PushBack( vert );
521
522       // Six indices in counter clockwise winding
523       newMesh.mIndices.PushBack( faceIndex + 1u );
524       newMesh.mIndices.PushBack( faceIndex );
525       newMesh.mIndices.PushBack( faceIndex + 2u );
526       newMesh.mIndices.PushBack( faceIndex + 2u );
527       newMesh.mIndices.PushBack( faceIndex + 3u );
528       newMesh.mIndices.PushBack( faceIndex + 1u );
529       faceIndex += 4;
530
531       if ( underlineColor == textColor )
532       {
533         mGlyphManager.StitchMesh( meshRecords[ index ].mMesh, newMesh );
534       }
535       else
536       {
537         MeshRecord record;
538         record.mMesh = newMesh;
539         record.mAtlasId = meshRecords[ index ].mAtlasId;
540         record.mColor = underlineColor;
541         record.mIsUnderline = true;
542         meshRecords.push_back( record );
543       }
544     }
545   }
546
547   Actor GenerateShadow( MeshRecord& meshRecord,
548                         const Vector2& shadowOffset,
549                         const Vector4& shadowColor )
550   {
551     // Scan vertex buffer to determine width and height of effect buffer needed
552     const Vector< AtlasManager::Vertex2D >& verts = meshRecord.mMesh.mVertices;
553     float tlx = verts[ 0 ].mPosition.x;
554     float tly = verts[ 0 ].mPosition.y;
555     float brx = ZERO;
556     float bry = ZERO;
557
558     for ( uint32_t i = 0; i < verts.Size(); ++i )
559     {
560       if ( verts[ i ].mPosition.x < tlx )
561       {
562         tlx = verts[ i ].mPosition.x;
563       }
564       if ( verts[ i ].mPosition.y < tly )
565       {
566         tly = verts[ i ].mPosition.y;
567       }
568       if ( verts[ i ].mPosition.x > brx )
569       {
570         brx = verts[ i ].mPosition.x;
571       }
572       if ( verts[ i ].mPosition.y > bry )
573       {
574         bry = verts[ i ].mPosition.y;
575       }
576     }
577
578     float width = brx - tlx;
579     float height = bry - tly;
580     float divWidth = TWO / width;
581     float divHeight = TWO / height;
582
583     // Create a buffer to render to
584     meshRecord.mBuffer = FrameBufferImage::New( width, height );
585
586     // We will render a quad into this buffer
587     unsigned int indices[ 6 ] = { 1, 0, 2, 2, 3, 1 };
588     PropertyBuffer quadVertices = PropertyBuffer::New( mQuadVertexFormat, 4u );
589     PropertyBuffer quadIndices = PropertyBuffer::New( mQuadIndexFormat, sizeof(indices)/sizeof(indices[0]) );
590
591     AtlasManager::Vertex2D vertices[ 4 ] = {
592     { Vector2( tlx + shadowOffset.x, tly + shadowOffset.y ), Vector2( ZERO, ZERO ) },
593     { Vector2( brx + shadowOffset.x, tly + shadowOffset.y ), Vector2( ONE, ZERO ) },
594     { Vector2( tlx + shadowOffset.x, bry + shadowOffset.y ), Vector2( ZERO, ONE ) },
595     { Vector2( brx + shadowOffset.x, bry + shadowOffset.y ), Vector2( ONE, ONE ) } };
596
597     quadVertices.SetData( vertices );
598     quadIndices.SetData( indices );
599
600     Geometry quadGeometry = Geometry::New();
601     quadGeometry.AddVertexBuffer( quadVertices );
602     quadGeometry.SetIndexBuffer( quadIndices );
603
604     Sampler sampler = Sampler::New( meshRecord.mBuffer, "sTexture" );
605     Material material = Material::New( mGlyphManager.GetEffectBufferShader() );
606     material.AddSampler( sampler );
607
608     Dali::Renderer renderer = Dali::Renderer::New( quadGeometry, material );
609
610     // Ensure shadow is behind the text...
611     renderer.SetDepthIndex( CONTENT_DEPTH_INDEX - 1 );
612     Actor actor = Actor::New();
613     actor.AddRenderer( renderer );
614     actor.SetSize( 1.0f, 1.0f );
615
616     // Create a sub actor to render the source with normalized vertex positions
617     Vector< AtlasManager::Vertex2D > normVertexList;
618     for ( uint32_t i = 0; i < verts.Size(); ++i )
619     {
620       AtlasManager::Vertex2D vertex = verts[ i ];
621       vertex.mPosition.x = ( ( vertex.mPosition.x - tlx ) * divWidth ) - ONE;
622       vertex.mPosition.y = ( ( vertex.mPosition.y - tly ) * divHeight ) - ONE;
623       normVertexList.PushBack( vertex );
624     }
625
626     PropertyBuffer normVertices = PropertyBuffer::New( mQuadVertexFormat, normVertexList.Size() );
627     PropertyBuffer normIndices = PropertyBuffer::New( mQuadIndexFormat, meshRecord.mMesh.mIndices.Size() );
628     normVertices.SetData( const_cast< AtlasManager::Vertex2D* >( &normVertexList[ 0 ] ) );
629     normIndices.SetData( const_cast< unsigned int* >( &meshRecord.mMesh.mIndices[ 0 ] ) );
630
631     Geometry normGeometry = Geometry::New();
632     normGeometry.AddVertexBuffer( normVertices );
633     normGeometry.SetIndexBuffer( normIndices );
634
635     Material normMaterial = Material::New( mGlyphManager.GetGlyphShadowShader() );
636     Sampler normSampler =  mGlyphManager.GetSampler( meshRecord.mAtlasId );
637     normMaterial.AddSampler( normSampler );
638     Dali::Renderer normRenderer = Dali::Renderer::New( normGeometry, normMaterial );
639     Actor subActor = Actor::New();
640     subActor.AddRenderer( normRenderer );
641     subActor.SetSize( 1.0f, 1.0f );
642     subActor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR );
643     subActor.SetColor( shadowColor );
644
645     // Create a render task to render the effect
646     RenderTask task = Stage::GetCurrent().GetRenderTaskList().CreateTask();
647     task.SetTargetFrameBuffer( meshRecord.mBuffer );
648     task.SetSourceActor( subActor );
649     task.SetClearEnabled( true );
650     task.SetClearColor( Vector4::ZERO );
651     task.SetExclusive( true );
652     task.SetRefreshRate( RenderTask::REFRESH_ONCE );
653     task.FinishedSignal().Connect( this, &AtlasRenderer::Impl::RenderComplete );
654     actor.Add( subActor );
655
656     return actor;
657   }
658
659   void RenderComplete( RenderTask& renderTask )
660   {
661     // Disconnect and remove this single shot render task
662     renderTask.FinishedSignal().Disconnect( this, &AtlasRenderer::Impl::RenderComplete );
663     Stage::GetCurrent().GetRenderTaskList().RemoveTask( renderTask );
664
665     // Get the actor used for render to buffer and remove it from the parent
666     Actor renderActor = renderTask.GetSourceActor();
667     if ( renderActor )
668     {
669       Actor parent = renderActor.GetParent();
670       if ( parent )
671       {
672         parent.Remove( renderActor );
673       }
674     }
675   }
676
677   Actor mActor;                                       ///< The actor parent which renders the text
678   AtlasGlyphManager mGlyphManager;                    ///< Glyph Manager to handle upload and caching
679   Vector< uint32_t > mImageIds;                       ///< A list of imageIDs used by the renderer
680   TextAbstraction::FontClient mFontClient;            ///> The font client used to supply glyph information
681   std::vector< MaxBlockSize > mBlockSizes;            ///> Maximum size needed to contain a glyph in a block within a new atlas
682   std::vector< uint32_t > mFace;                      ///> Face indices for a quad
683   Property::Map mQuadVertexFormat;
684   Property::Map mQuadIndexFormat;
685 };
686
687 Text::RendererPtr AtlasRenderer::New()
688 {
689   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Text::AtlasRenderer::New()\n" );
690
691   return Text::RendererPtr( new AtlasRenderer() );
692 }
693
694 Actor AtlasRenderer::Render( Text::ViewInterface& view )
695 {
696   UnparentAndReset( mImpl->mActor );
697
698   Length numberOfGlyphs = view.GetNumberOfGlyphs();
699
700   if( numberOfGlyphs > 0u )
701   {
702     Vector<GlyphInfo> glyphs;
703     glyphs.Resize( numberOfGlyphs );
704
705     std::vector<Vector2> positions;
706     positions.resize( numberOfGlyphs );
707
708     numberOfGlyphs = view.GetGlyphs( glyphs.Begin(),
709                                      &positions[0],
710                                      0u,
711                                      numberOfGlyphs );
712     glyphs.Resize( numberOfGlyphs );
713     positions.resize( numberOfGlyphs );
714
715     mImpl->AddGlyphs( positions,
716                       glyphs,
717                       view.GetTextColor(),
718                       view.GetShadowOffset(),
719                       view.GetShadowColor(),
720                       view.IsUnderlineEnabled(),
721                       view.GetUnderlineColor(),
722                       view.GetUnderlineHeight() );
723   }
724
725   return mImpl->mActor;
726 }
727
728 AtlasRenderer::AtlasRenderer()
729 {
730   mImpl = new Impl();
731
732 }
733
734 AtlasRenderer::~AtlasRenderer()
735 {
736   delete mImpl;
737 }