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