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