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