2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/internal/text/rendering/atlas/text-atlas-renderer.h>
22 #include <dali/dali.h>
23 #include <dali/integration-api/debug.h>
26 #include <dali-toolkit/internal/atlas-manager/atlas-manager.h>
27 #include <dali-toolkit/internal/text/rendering/atlas/atlas-glyph-manager.h>
28 #include <dali-toolkit/internal/text/rendering/shaders/text-basic-shader.h>
29 #include <dali-toolkit/internal/text/rendering/shaders/text-bgra-shader.h>
30 #include <dali-toolkit/internal/text/rendering/shaders/text-basic-shadow-shader.h>
31 #if defined(DEBUG_ENABLED)
32 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_ATLAS_RENDERER");
36 using namespace Dali::Toolkit;
37 using namespace Dali::Toolkit::Text;
41 const float ZERO( 0.0f );
42 const float HALF( 0.5f );
43 const float ONE( 1.0f );
44 const float TWO( 2.0f );
45 const uint32_t DEFAULT_ATLAS_WIDTH = 512u;
46 const uint32_t DEFAULT_ATLAS_HEIGHT = 512u;
49 struct AtlasRenderer::Impl : public ConnectionTracker
63 FrameBufferImage mBuffer;
72 float mUnderlinePosition;
73 float mUnderlineThickness;
74 uint32_t mMeshRecordIndex;
80 Text::GlyphIndex mIndex;
86 uint32_t mNeededBlockWidth;
87 uint32_t mNeededBlockHeight;
92 mGlyphManager = AtlasGlyphManager::Get();
93 mFontClient = TextAbstraction::FontClient::Get();
94 mBasicShader = BasicShader::New();
95 mBgraShader = BgraShader::New();
96 mBasicShadowShader = BasicShadowShader::New();
99 mFace.push_back( 0 ); mFace.push_back( 2u ); mFace.push_back( 1u );
100 mFace.push_back( 1u ); mFace.push_back( 2u ); mFace.push_back( 3u );
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 )
112 AtlasManager::AtlasSlot slot;
113 std::vector< MeshRecord > meshContainer;
114 Vector< Extent > extents;
116 float currentUnderlinePosition = ZERO;
117 float currentUnderlineThickness = underlineHeight;
118 uint32_t currentBlockSize = 0;
119 FontId lastFontId = 0;
120 Style style = STYLE_NORMAL;
122 if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
124 style = STYLE_DROP_SHADOW;
127 if ( mImageIds.Size() )
129 // Unreference any currently used glyphs
133 CalculateBlocksSize( glyphs );
135 for ( uint32_t i = 0; i < glyphs.Size(); ++i )
137 GlyphInfo glyph = glyphs[ i ];
139 // No operation for white space
140 if ( glyph.width && glyph.height )
142 // Are we still using the same fontId as previous
143 if ( glyph.fontId != lastFontId )
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 ) );
151 if ( underlineHeight == ZERO )
153 currentUnderlineThickness = fontMetrics.underlineThickness;
155 // Ensure underline will be at least a pixel high
156 if ( currentUnderlineThickness < ONE )
158 currentUnderlineThickness = ONE;
162 currentUnderlineThickness = ceil( currentUnderlineThickness );
166 // Clamp the underline position at the font descender and check for ( as EFL describes it ) a broken font
167 if ( currentUnderlinePosition > descender )
169 currentUnderlinePosition = descender;
171 if ( ZERO == currentUnderlinePosition )
173 // Move offset down by one ( EFL behavior )
174 currentUnderlinePosition = ONE;
178 Vector2 position = positions[ i ];
179 MeshData newMeshData;
180 mGlyphManager.Cached( glyph.fontId, glyph.index, slot );
184 // This glyph already exists so generate mesh data plugging in our supplied position
185 mGlyphManager.GenerateMeshData( slot.mImageId, position, newMeshData );
186 mImageIds.PushBack( slot.mImageId );
191 // Select correct size for new atlas if needed....?
192 if ( lastFontId != glyph.fontId )
194 for ( uint32_t j = 0; j < mBlockSizes.size(); ++j )
196 if ( mBlockSizes[ j ].mFontId == glyph.fontId )
198 currentBlockSize = j;
199 mGlyphManager.SetNewAtlasSize( DEFAULT_ATLAS_WIDTH,
200 DEFAULT_ATLAS_HEIGHT,
201 mBlockSizes[ j ].mNeededBlockWidth,
202 mBlockSizes[ j ].mNeededBlockHeight );
207 // Create a new image for the glyph
208 BufferImage bitmap = mFontClient.CreateBitmap( glyph.fontId, glyph.index );
210 // Ensure that the next image will fit into the current block size
211 bool setSize = false;
212 if ( bitmap.GetWidth() > mBlockSizes[ currentBlockSize ].mNeededBlockWidth )
215 mBlockSizes[ currentBlockSize ].mNeededBlockWidth = bitmap.GetWidth();
217 if ( bitmap.GetHeight() > mBlockSizes[ currentBlockSize ].mNeededBlockHeight )
220 mBlockSizes[ currentBlockSize ].mNeededBlockHeight = bitmap.GetHeight();
225 mGlyphManager.SetNewAtlasSize( DEFAULT_ATLAS_WIDTH,
226 DEFAULT_ATLAS_HEIGHT,
227 mBlockSizes[ currentBlockSize ].mNeededBlockWidth,
228 mBlockSizes[ currentBlockSize ].mNeededBlockHeight );
231 // Locate a new slot for our glyph
232 mGlyphManager.Add( glyph, bitmap, slot );
234 // Generate mesh data for this quad, plugging in our supplied position
237 mGlyphManager.GenerateMeshData( slot.mImageId, position, newMeshData );
238 mImageIds.PushBack( slot.mImageId );
241 // Find an existing mesh data object to attach to ( or create a new one, if we can't find one using the same atlas)
242 StitchTextMesh( meshContainer,
246 position.y + glyph.yBearing,
247 currentUnderlinePosition,
248 currentUnderlineThickness,
250 lastFontId = glyph.fontId;
254 if ( underlineEnabled )
256 // Check to see if any of the text needs an underline
257 GenerateUnderlines( meshContainer, extents, underlineColor, textColor );
260 // For each MeshData object, create a mesh actor and add to the renderable actor
261 if ( meshContainer.size() )
263 for ( std::vector< MeshRecord >::iterator mIt = meshContainer.begin(); mIt != meshContainer.end(); ++mIt )
265 MeshActor actor = MeshActor::New( Mesh::New( mIt->mMeshData ) );
266 actor.SetColor( mIt->mColor );
268 // Ensure that text rendering is unfiltered
269 actor.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
270 if ( mIt->mIsUnderline )
272 actor.SetColorMode( USE_OWN_COLOR );
276 actor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR );
279 // Check to see what pixel format the shader should be
280 if ( mGlyphManager.GetPixelFormat( mIt->mAtlasId ) == Pixel::L8 )
282 // Create an effect if necessary
283 if ( style == STYLE_DROP_SHADOW )
285 actor.Add( GenerateShadow( *mIt, shadowOffset, shadowColor ) );
287 actor.SetShaderEffect( mBasicShader );
291 actor.SetShaderEffect( mBgraShader );
303 mActor.OffStageSignal().Connect( this, &AtlasRenderer::Impl::OffStageDisconnect );
305 #if defined(DEBUG_ENABLED)
306 Toolkit::AtlasGlyphManager::Metrics metrics = mGlyphManager.GetMetrics();
307 DALI_LOG_INFO( gLogFilter, Debug::Concise, "TextAtlasRenderer::GlyphManager::GlyphCount: %i, AtlasCount: %i, TextureMemoryUse: %iK\n",
309 metrics.mAtlasMetrics.mAtlasCount,
310 metrics.mAtlasMetrics.mTextureMemoryUsed / 1024 );
311 for ( uint32_t i = 0; i < metrics.mAtlasMetrics.mAtlasCount; ++i )
313 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Atlas [%i] %sPixels: %s Size: %ix%i, BlockSize: %ix%i, BlocksUsed: %i/%i\n",
314 i + 1, i > 8 ? "" : " ",
315 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mPixelFormat == Pixel::L8 ? "L8 " : "BGRA",
316 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mWidth,
317 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mHeight,
318 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mBlockWidth,
319 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mBlockHeight,
320 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mBlocksUsed,
321 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mTotalBlocks );
326 void StitchTextMesh( std::vector< MeshRecord >& meshContainer,
327 MeshData& newMeshData,
328 Vector< Extent >& extents,
329 const Vector4& color,
331 float underlinePosition,
332 float underlineThickness,
333 AtlasManager::AtlasSlot& slot )
337 MeshData::VertexContainer verts = newMeshData.GetVertices();
338 float left = verts[ 0 ].x;
339 float right = verts[ 1 ].x;
341 // Check to see if there's a mesh data object that references the same atlas ?
343 for ( std::vector< MeshRecord >::iterator mIt = meshContainer.begin(); mIt != meshContainer.end(); ++mIt, ++index )
345 if ( slot.mAtlasId == mIt->mAtlasId && color == mIt->mColor )
347 // Stitch the mesh to the existing mesh and adjust any extents
348 mGlyphManager.StitchMesh( mIt->mMeshData, newMeshData );
349 AdjustExtents( extents,
356 underlineThickness );
361 // No mesh data object currently exists that references this atlas, so create a new one
362 MeshRecord meshRecord;
363 meshRecord.mAtlasId = slot.mAtlasId;
364 meshRecord.mMeshData = newMeshData;
365 meshRecord.mColor = color;
366 meshRecord.mIsUnderline = false;
367 meshContainer.push_back( meshRecord );
369 // Adjust extents for this new meshrecord
370 AdjustExtents( extents,
372 meshContainer.size() - 1u,
377 underlineThickness );
382 void AdjustExtents( Vector< Extent >& extents,
383 std::vector< MeshRecord>& meshRecords,
388 float underlinePosition,
389 float underlineThickness )
391 bool foundExtent = false;
392 for ( Vector< Extent >::Iterator eIt = extents.Begin(); eIt != extents.End(); ++eIt )
394 if ( Equals( baseLine, eIt->mBaseLine ) )
397 if ( left < eIt->mLeft )
401 if ( right > eIt->mRight )
406 if ( underlinePosition > eIt->mUnderlinePosition )
408 eIt->mUnderlinePosition = underlinePosition;
410 if ( underlineThickness > eIt->mUnderlineThickness )
412 eIt->mUnderlineThickness = underlineThickness;
420 extent.mRight = right;
421 extent.mBaseLine = baseLine;
422 extent.mUnderlinePosition = underlinePosition;
423 extent.mUnderlineThickness = underlineThickness;
424 extent.mMeshRecordIndex = index;
425 extents.PushBack( extent );
429 // Unreference any glyphs that were used with this actor
430 void OffStageDisconnect( Dali::Actor actor )
437 for ( uint32_t i = 0; i < mImageIds.Size(); ++i )
439 mGlyphManager.Remove( mImageIds[ i ] );
441 mImageIds.Resize( 0 );
444 void CalculateBlocksSize( const Vector<GlyphInfo>& glyphs )
446 MaxBlockSize maxBlockSize;
447 for ( uint32_t i = 0; i < glyphs.Size(); ++i )
449 FontId fontId = glyphs[ i ].fontId;
450 bool foundFont = false;
451 for ( uint32_t j = 0; j < mBlockSizes.size(); ++j )
453 if ( mBlockSizes[ j ].mFontId == fontId )
460 FontMetrics fontMetrics;
461 mFontClient.GetFontMetrics( fontId, fontMetrics );
462 maxBlockSize.mNeededBlockWidth = static_cast< uint32_t >( fontMetrics.height );
463 maxBlockSize.mNeededBlockHeight = static_cast< uint32_t >( fontMetrics.height );
464 maxBlockSize.mFontId = fontId;
465 mBlockSizes.push_back( maxBlockSize );
470 void GenerateUnderlines( std::vector< MeshRecord>& meshRecords,
471 Vector< Extent >& extents,
472 const Vector4& underlineColor,
473 const Vector4& textColor )
475 MeshData newMeshData;
476 for ( Vector< Extent >::ConstIterator eIt = extents.Begin(); eIt != extents.End(); ++eIt )
478 MeshData::VertexContainer newVerts;
479 newVerts.reserve( 4u );
480 uint32_t index = eIt->mMeshRecordIndex;
481 Vector2 uv = mGlyphManager.GetAtlasSize( meshRecords[ index ].mAtlasId );
483 // Make sure we don't hit texture edge for single pixel texture ( filled pixel is in top left of every atlas )
484 float u = HALF / uv.x;
485 float v = HALF / uv.y;
486 float thickness = eIt->mUnderlineThickness;
487 float baseLine = eIt->mBaseLine + eIt->mUnderlinePosition - ( thickness * HALF );
488 float tlx = eIt->mLeft;
489 float brx = eIt->mRight;
491 newVerts.push_back( MeshData::Vertex( Vector3( tlx, baseLine, ZERO ),
495 newVerts.push_back( MeshData::Vertex( Vector3( brx, baseLine, ZERO ),
499 newVerts.push_back( MeshData::Vertex( Vector3( tlx, baseLine + thickness, ZERO ),
503 newVerts.push_back( MeshData::Vertex( Vector3( brx, baseLine + thickness, ZERO ),
507 newMeshData.SetVertices( newVerts );
508 newMeshData.SetFaceIndices( mFace );
510 if ( underlineColor == textColor )
512 mGlyphManager.StitchMesh( meshRecords[ index ].mMeshData, newMeshData );
517 newMeshData.SetMaterial( meshRecords[ index ].mMeshData.GetMaterial() );
518 newMeshData.SetHasNormals( true );
519 newMeshData.SetHasColor( false );
520 newMeshData.SetHasTextureCoords( true );
521 record.mMeshData = newMeshData;
522 record.mAtlasId = meshRecords[ index ].mAtlasId;
523 record.mColor = underlineColor;
524 record.mIsUnderline = true;
525 meshRecords.push_back( record );
530 MeshActor GenerateShadow( MeshRecord& meshRecord,
531 const Vector2& shadowOffset,
532 const Vector4& shadowColor )
534 // Scan vertex buffer to determine width and height of effect buffer needed
535 MeshData::VertexContainer verts = meshRecord.mMeshData.GetVertices();
536 float tlx = verts[ 0 ].x;
537 float tly = verts[ 0 ].y;
541 for ( uint32_t i = 0; i < verts.size(); ++i )
543 if ( verts[ i ].x < tlx )
547 if ( verts[ i ].y < tly )
551 if ( verts[ i ].x > brx )
555 if ( verts[ i ].y > bry )
561 float width = brx - tlx;
562 float height = bry - tly;
563 float divWidth = TWO / width;
564 float divHeight = TWO / height;
566 // Create a buffer to render to
567 meshRecord.mBuffer = FrameBufferImage::New( width, height );
569 // Create a mesh actor to contain the post-effect render
570 MeshData::VertexContainer vertices;
571 MeshData::FaceIndices face;
573 vertices.push_back( MeshData::Vertex( Vector3( tlx + shadowOffset.x, tly + shadowOffset.y, ZERO ),
577 vertices.push_back( MeshData::Vertex( Vector3( brx + shadowOffset.x, tly + shadowOffset.y, ZERO ),
578 Vector2( ONE, ZERO ),
581 vertices.push_back( MeshData::Vertex( Vector3( tlx + shadowOffset.x, bry + shadowOffset.y, ZERO ),
582 Vector2( ZERO, ONE ),
585 vertices.push_back( MeshData::Vertex( Vector3( brx + shadowOffset.x, bry + shadowOffset.y, ZERO ),
590 Material newMaterial = Material::New("effect buffer");
591 newMaterial.SetDiffuseTexture( meshRecord.mBuffer );
592 meshData.SetMaterial( newMaterial );
593 meshData.SetVertices( vertices );
594 meshData.SetFaceIndices( mFace );
595 meshData.SetHasNormals( true );
596 meshData.SetHasColor( false );
597 meshData.SetHasTextureCoords( true );
598 MeshActor actor = MeshActor::New( Mesh::New( meshData ) );
599 actor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR );
600 actor.SetShaderEffect( mBgraShader );
601 actor.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
602 actor.SetSortModifier( 0.1f ); // force behind main text
604 // Create a sub actor to render once with normalized vertex positions
605 MeshData newMeshData;
606 MeshData::VertexContainer newVerts;
607 MeshData::FaceIndices newFaces;
608 MeshData::FaceIndices faces = meshRecord.mMeshData.GetFaces();
609 for ( uint32_t i = 0; i < verts.size(); ++i )
611 MeshData::Vertex vertex = verts[ i ];
612 vertex.x = ( ( vertex.x - tlx ) * divWidth ) - ONE;
613 vertex.y = ( ( vertex.y - tly ) * divHeight ) - ONE;
614 newVerts.push_back( vertex );
617 // Reverse triangle winding order
618 uint32_t faceCount = faces.size() / 3;
619 for ( uint32_t i = 0; i < faceCount; ++i )
621 uint32_t index = i * 3;
622 newFaces.push_back( faces[ index + 2 ] );
623 newFaces.push_back( faces[ index + 1 ] );
624 newFaces.push_back( faces[ index ] );
627 newMeshData.SetMaterial( meshRecord.mMeshData.GetMaterial() );
628 newMeshData.SetVertices( newVerts );
629 newMeshData.SetFaceIndices( newFaces );
630 newMeshData.SetHasNormals( true );
631 newMeshData.SetHasColor( false );
632 newMeshData.SetHasTextureCoords( true );
634 MeshActor subActor = MeshActor::New( Mesh::New( newMeshData ) );
635 subActor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR );
636 subActor.SetColor( shadowColor );
637 subActor.SetShaderEffect( mBasicShadowShader );
638 subActor.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
640 // Create a render task to render the effect
641 RenderTask task = Stage::GetCurrent().GetRenderTaskList().CreateTask();
642 task.SetTargetFrameBuffer( meshRecord.mBuffer );
643 task.SetSourceActor( subActor );
644 task.SetClearEnabled( true );
645 task.SetClearColor( Vector4::ZERO );
646 task.SetExclusive( true );
647 task.SetRefreshRate( RenderTask::REFRESH_ONCE );
648 task.FinishedSignal().Connect( this, &AtlasRenderer::Impl::RenderComplete );
649 actor.Add( subActor );
653 void RenderComplete( RenderTask& renderTask )
655 // Disconnect and remove this single shot render task
656 renderTask.FinishedSignal().Disconnect( this, &AtlasRenderer::Impl::RenderComplete );
657 Stage::GetCurrent().GetRenderTaskList().RemoveTask( renderTask );
659 // Get the actor used for render to buffer and remove it from the parent
660 Actor renderActor = renderTask.GetSourceActor();
663 Actor parent = renderActor.GetParent();
666 parent.Remove( renderActor );
671 RenderableActor mActor; ///< The actor parent which renders the text
672 AtlasGlyphManager mGlyphManager; ///< Glyph Manager to handle upload and caching
673 Vector< uint32_t > mImageIds; ///< A list of imageIDs used by the renderer
674 TextAbstraction::FontClient mFontClient; ///> The font client used to supply glyph information
675 ShaderEffect mBasicShader; ///> Shader used to render L8 glyphs
676 ShaderEffect mBgraShader; ///> Shader used to render BGRA glyphs
677 ShaderEffect mBasicShadowShader; ///> Shader used to render drop shadow into buffer
678 std::vector< MaxBlockSize > mBlockSizes; ///> Maximum size needed to contain a glyph in a block within a new atlas
679 std::vector< MeshData::FaceIndex > mFace; ///> Face indices for a quad
682 Text::RendererPtr AtlasRenderer::New()
684 return Text::RendererPtr( new AtlasRenderer() );
687 RenderableActor AtlasRenderer::Render( Text::ViewInterface& view )
690 UnparentAndReset( mImpl->mActor );
692 Text::Length numberOfGlyphs = view.GetNumberOfGlyphs();
694 if( numberOfGlyphs > 0 )
696 Vector<GlyphInfo> glyphs;
697 glyphs.Resize( numberOfGlyphs );
699 view.GetGlyphs( &glyphs[0], 0, numberOfGlyphs );
701 std::vector<Vector2> positions;
702 positions.resize( numberOfGlyphs );
703 view.GetGlyphPositions( &positions[0], 0, numberOfGlyphs );
704 mImpl->AddGlyphs( positions,
707 view.GetShadowOffset(),
708 view.GetShadowColor(),
709 view.IsUnderlineEnabled(),
710 view.GetUnderlineColor(),
711 view.GetUnderlineHeight() );
713 return mImpl->mActor;
716 AtlasRenderer::AtlasRenderer()
722 AtlasRenderer::~AtlasRenderer()