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