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