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