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