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