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>
24 #include <dali/public-api/text-abstraction/text-abstraction.h>
27 #include <dali-toolkit/internal/atlas-manager/atlas-manager.h>
28 #include <dali-toolkit/internal/text/line-run.h>
29 #include <dali-toolkit/internal/text/rendering/atlas/atlas-glyph-manager.h>
30 #include <dali-toolkit/internal/text/rendering/shaders/text-basic-shader.h>
31 #include <dali-toolkit/internal/text/rendering/shaders/text-bgra-shader.h>
32 #include <dali-toolkit/internal/text/rendering/shaders/text-basic-shadow-shader.h>
35 using namespace Dali::Toolkit;
36 using namespace Dali::Toolkit::Text;
40 #if defined(DEBUG_ENABLED)
41 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::Concise, true, "LOG_TEXT_RENDERING");
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;
52 struct AtlasRenderer::Impl : public ConnectionTracker
66 FrameBufferImage mBuffer;
75 float mUnderlinePosition;
76 float mUnderlineThickness;
77 uint32_t mMeshRecordIndex;
83 Text::GlyphIndex mIndex;
89 uint32_t mNeededBlockWidth;
90 uint32_t mNeededBlockHeight;
95 mGlyphManager = AtlasGlyphManager::Get();
96 mFontClient = TextAbstraction::FontClient::Get();
97 mBasicShader = BasicShader::New();
98 mBgraShader = BgraShader::New();
99 mBasicShadowShader = BasicShadowShader::New();
102 mFace.push_back( 0 ); mFace.push_back( 2u ); mFace.push_back( 1u );
103 mFace.push_back( 1u ); mFace.push_back( 2u ); mFace.push_back( 3u );
106 void AddGlyphs( const std::vector<Vector2>& positions,
107 const Vector<GlyphInfo>& glyphs,
108 const Vector4& textColor,
109 const Vector2& shadowOffset,
110 const Vector4& shadowColor,
111 bool underlineEnabled,
112 const Vector4& underlineColor,
113 float underlineHeight )
115 AtlasManager::AtlasSlot slot;
116 std::vector< MeshRecord > meshContainer;
117 Vector< Extent > extents;
119 float currentUnderlinePosition = ZERO;
120 float currentUnderlineThickness = underlineHeight;
121 uint32_t currentBlockSize = 0;
122 FontId lastFontId = 0;
123 Style style = STYLE_NORMAL;
125 if ( fabsf( shadowOffset.x ) > Math::MACHINE_EPSILON_1 || fabsf( shadowOffset.y ) > Math::MACHINE_EPSILON_1 )
127 style = STYLE_DROP_SHADOW;
130 if ( mImageIds.Size() )
132 // Unreference any currently used glyphs
136 CalculateBlocksSize( glyphs );
138 for( uint32_t i = 0, glyphSize = glyphs.Size(); i < glyphSize; ++i )
140 const GlyphInfo& glyph = glyphs[ i ];
142 // No operation for white space
143 if ( glyph.width && glyph.height )
145 // Are we still using the same fontId as previous
146 if ( glyph.fontId != lastFontId )
148 // We need to fetch fresh font underline metrics
149 FontMetrics fontMetrics;
150 mFontClient.GetFontMetrics( glyph.fontId, fontMetrics );
151 currentUnderlinePosition = ceil( fabsf( fontMetrics.underlinePosition ) );
152 float descender = ceil( fabsf( fontMetrics.descender ) );
154 if ( underlineHeight == ZERO )
156 currentUnderlineThickness = fontMetrics.underlineThickness;
158 // Ensure underline will be at least a pixel high
159 if ( currentUnderlineThickness < ONE )
161 currentUnderlineThickness = ONE;
165 currentUnderlineThickness = ceil( currentUnderlineThickness );
169 // Clamp the underline position at the font descender and check for ( as EFL describes it ) a broken font
170 if ( currentUnderlinePosition > descender )
172 currentUnderlinePosition = descender;
174 if ( ZERO == currentUnderlinePosition )
176 // Move offset down by one ( EFL behavior )
177 currentUnderlinePosition = ONE;
181 const Vector2& position = positions[ i ];
182 MeshData newMeshData;
183 mGlyphManager.Cached( glyph.fontId, glyph.index, slot );
187 // This glyph already exists so generate mesh data plugging in our supplied position
188 mGlyphManager.GenerateMeshData( slot.mImageId, position, newMeshData );
189 mImageIds.PushBack( slot.mImageId );
194 // Select correct size for new atlas if needed....?
195 if ( lastFontId != glyph.fontId )
197 for ( uint32_t j = 0; j < mBlockSizes.size(); ++j )
199 if ( mBlockSizes[ j ].mFontId == glyph.fontId )
201 currentBlockSize = j;
202 mGlyphManager.SetNewAtlasSize( DEFAULT_ATLAS_WIDTH,
203 DEFAULT_ATLAS_HEIGHT,
204 mBlockSizes[ j ].mNeededBlockWidth,
205 mBlockSizes[ j ].mNeededBlockHeight );
210 // Create a new image for the glyph
211 BufferImage bitmap = mFontClient.CreateBitmap( glyph.fontId, glyph.index );
214 // Ensure that the next image will fit into the current block size
215 bool setSize = false;
216 if ( bitmap.GetWidth() > mBlockSizes[ currentBlockSize ].mNeededBlockWidth )
219 mBlockSizes[ currentBlockSize ].mNeededBlockWidth = bitmap.GetWidth();
221 if ( bitmap.GetHeight() > mBlockSizes[ currentBlockSize ].mNeededBlockHeight )
224 mBlockSizes[ currentBlockSize ].mNeededBlockHeight = bitmap.GetHeight();
229 mGlyphManager.SetNewAtlasSize( DEFAULT_ATLAS_WIDTH,
230 DEFAULT_ATLAS_HEIGHT,
231 mBlockSizes[ currentBlockSize ].mNeededBlockWidth,
232 mBlockSizes[ currentBlockSize ].mNeededBlockHeight );
235 // Locate a new slot for our glyph
236 mGlyphManager.Add( glyph, bitmap, slot );
238 // Generate mesh data for this quad, plugging in our supplied position
241 mGlyphManager.GenerateMeshData( slot.mImageId, position, newMeshData );
242 mImageIds.PushBack( slot.mImageId );
246 // Find an existing mesh data object to attach to ( or create a new one, if we can't find one using the same atlas)
247 StitchTextMesh( meshContainer,
251 position.y + glyph.yBearing,
252 currentUnderlinePosition,
253 currentUnderlineThickness,
255 lastFontId = glyph.fontId;
259 if ( underlineEnabled )
261 // Check to see if any of the text needs an underline
262 GenerateUnderlines( meshContainer, extents, underlineColor, textColor );
265 // For each MeshData object, create a mesh actor and add to the renderable actor
266 if ( meshContainer.size() )
268 for ( std::vector< MeshRecord >::iterator mIt = meshContainer.begin(); mIt != meshContainer.end(); ++mIt )
270 MeshActor actor = MeshActor::New( Mesh::New( mIt->mMeshData ) );
271 actor.SetColor( mIt->mColor );
273 // Ensure that text rendering is unfiltered
274 actor.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
275 if ( mIt->mIsUnderline )
277 actor.SetColorMode( USE_OWN_COLOR );
281 actor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR );
284 // Check to see what pixel format the shader should be
285 if ( mGlyphManager.GetPixelFormat( mIt->mAtlasId ) == Pixel::L8 )
287 // Create an effect if necessary
288 if ( style == STYLE_DROP_SHADOW )
290 actor.Add( GenerateShadow( *mIt, shadowOffset, shadowColor ) );
292 actor.SetShaderEffect( mBasicShader );
296 actor.SetShaderEffect( mBgraShader );
308 mActor.OffStageSignal().Connect( this, &AtlasRenderer::Impl::OffStageDisconnect );
310 #if defined(DEBUG_ENABLED)
311 Toolkit::AtlasGlyphManager::Metrics metrics = mGlyphManager.GetMetrics();
312 DALI_LOG_INFO( gLogFilter, Debug::General, "TextAtlasRenderer::GlyphManager::GlyphCount: %i, AtlasCount: %i, TextureMemoryUse: %iK\n",
314 metrics.mAtlasMetrics.mAtlasCount,
315 metrics.mAtlasMetrics.mTextureMemoryUsed / 1024 );
316 for ( uint32_t i = 0; i < metrics.mAtlasMetrics.mAtlasCount; ++i )
318 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Atlas [%i] %sPixels: %s Size: %ix%i, BlockSize: %ix%i, BlocksUsed: %i/%i\n",
319 i + 1, i > 8 ? "" : " ",
320 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mPixelFormat == Pixel::L8 ? "L8 " : "BGRA",
321 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mWidth,
322 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mHeight,
323 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mBlockWidth,
324 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mSize.mBlockHeight,
325 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mBlocksUsed,
326 metrics.mAtlasMetrics.mAtlasMetrics[ i ].mTotalBlocks );
331 void StitchTextMesh( std::vector< MeshRecord >& meshContainer,
332 MeshData& newMeshData,
333 Vector< Extent >& extents,
334 const Vector4& color,
336 float underlinePosition,
337 float underlineThickness,
338 AtlasManager::AtlasSlot& slot )
342 MeshData::VertexContainer verts = newMeshData.GetVertices();
343 float left = verts[ 0 ].x;
344 float right = verts[ 1 ].x;
346 // Check to see if there's a mesh data object that references the same atlas ?
348 for ( std::vector< MeshRecord >::iterator mIt = meshContainer.begin(); mIt != meshContainer.end(); ++mIt, ++index )
350 if ( slot.mAtlasId == mIt->mAtlasId && color == mIt->mColor )
352 // Stitch the mesh to the existing mesh and adjust any extents
353 mGlyphManager.StitchMesh( mIt->mMeshData, newMeshData );
354 AdjustExtents( extents,
361 underlineThickness );
366 // No mesh data object currently exists that references this atlas, so create a new one
367 MeshRecord meshRecord;
368 meshRecord.mAtlasId = slot.mAtlasId;
369 meshRecord.mMeshData = newMeshData;
370 meshRecord.mColor = color;
371 meshRecord.mIsUnderline = false;
372 meshContainer.push_back( meshRecord );
374 // Adjust extents for this new meshrecord
375 AdjustExtents( extents,
377 meshContainer.size() - 1u,
382 underlineThickness );
387 void AdjustExtents( Vector< Extent >& extents,
388 std::vector< MeshRecord>& meshRecords,
393 float underlinePosition,
394 float underlineThickness )
396 bool foundExtent = false;
397 for ( Vector< Extent >::Iterator eIt = extents.Begin(); eIt != extents.End(); ++eIt )
399 if ( Equals( baseLine, eIt->mBaseLine ) )
402 if ( left < eIt->mLeft )
406 if ( right > eIt->mRight )
411 if ( underlinePosition > eIt->mUnderlinePosition )
413 eIt->mUnderlinePosition = underlinePosition;
415 if ( underlineThickness > eIt->mUnderlineThickness )
417 eIt->mUnderlineThickness = underlineThickness;
425 extent.mRight = right;
426 extent.mBaseLine = baseLine;
427 extent.mUnderlinePosition = underlinePosition;
428 extent.mUnderlineThickness = underlineThickness;
429 extent.mMeshRecordIndex = index;
430 extents.PushBack( extent );
434 // Unreference any glyphs that were used with this actor
435 void OffStageDisconnect( Dali::Actor actor )
442 for ( uint32_t i = 0; i < mImageIds.Size(); ++i )
444 mGlyphManager.Remove( mImageIds[ i ] );
446 mImageIds.Resize( 0 );
449 void CalculateBlocksSize( const Vector<GlyphInfo>& glyphs )
451 MaxBlockSize maxBlockSize;
452 for ( uint32_t i = 0; i < glyphs.Size(); ++i )
454 FontId fontId = glyphs[ i ].fontId;
455 bool foundFont = false;
456 for ( uint32_t j = 0; j < mBlockSizes.size(); ++j )
458 if ( mBlockSizes[ j ].mFontId == fontId )
465 FontMetrics fontMetrics;
466 mFontClient.GetFontMetrics( fontId, fontMetrics );
467 maxBlockSize.mNeededBlockWidth = static_cast< uint32_t >( fontMetrics.height );
468 maxBlockSize.mNeededBlockHeight = static_cast< uint32_t >( fontMetrics.height );
469 maxBlockSize.mFontId = fontId;
470 mBlockSizes.push_back( maxBlockSize );
475 void GenerateUnderlines( std::vector< MeshRecord>& meshRecords,
476 Vector< Extent >& extents,
477 const Vector4& underlineColor,
478 const Vector4& textColor )
480 MeshData newMeshData;
481 for ( Vector< Extent >::ConstIterator eIt = extents.Begin(); eIt != extents.End(); ++eIt )
483 MeshData::VertexContainer newVerts;
484 newVerts.reserve( 4u );
485 uint32_t index = eIt->mMeshRecordIndex;
486 Vector2 uv = mGlyphManager.GetAtlasSize( meshRecords[ index ].mAtlasId );
488 // Make sure we don't hit texture edge for single pixel texture ( filled pixel is in top left of every atlas )
489 float u = HALF / uv.x;
490 float v = HALF / uv.y;
491 float thickness = eIt->mUnderlineThickness;
492 float baseLine = eIt->mBaseLine + eIt->mUnderlinePosition - ( thickness * HALF );
493 float tlx = eIt->mLeft;
494 float brx = eIt->mRight;
496 newVerts.push_back( MeshData::Vertex( Vector3( tlx, baseLine, ZERO ),
500 newVerts.push_back( MeshData::Vertex( Vector3( brx, baseLine, ZERO ),
504 newVerts.push_back( MeshData::Vertex( Vector3( tlx, baseLine + thickness, ZERO ),
508 newVerts.push_back( MeshData::Vertex( Vector3( brx, baseLine + thickness, ZERO ),
512 newMeshData.SetVertices( newVerts );
513 newMeshData.SetFaceIndices( mFace );
515 if ( underlineColor == textColor )
517 mGlyphManager.StitchMesh( meshRecords[ index ].mMeshData, newMeshData );
522 newMeshData.SetMaterial( meshRecords[ index ].mMeshData.GetMaterial() );
523 newMeshData.SetHasNormals( true );
524 newMeshData.SetHasColor( false );
525 newMeshData.SetHasTextureCoords( true );
526 record.mMeshData = newMeshData;
527 record.mAtlasId = meshRecords[ index ].mAtlasId;
528 record.mColor = underlineColor;
529 record.mIsUnderline = true;
530 meshRecords.push_back( record );
535 MeshActor GenerateShadow( MeshRecord& meshRecord,
536 const Vector2& shadowOffset,
537 const Vector4& shadowColor )
539 // Scan vertex buffer to determine width and height of effect buffer needed
540 MeshData::VertexContainer verts = meshRecord.mMeshData.GetVertices();
541 float tlx = verts[ 0 ].x;
542 float tly = verts[ 0 ].y;
546 for ( uint32_t i = 0; i < verts.size(); ++i )
548 if ( verts[ i ].x < tlx )
552 if ( verts[ i ].y < tly )
556 if ( verts[ i ].x > brx )
560 if ( verts[ i ].y > bry )
566 float width = brx - tlx;
567 float height = bry - tly;
568 float divWidth = TWO / width;
569 float divHeight = TWO / height;
571 // Create a buffer to render to
572 meshRecord.mBuffer = FrameBufferImage::New( width, height );
574 // Create a mesh actor to contain the post-effect render
575 MeshData::VertexContainer vertices;
576 MeshData::FaceIndices face;
578 vertices.push_back( MeshData::Vertex( Vector3( tlx + shadowOffset.x, tly + shadowOffset.y, ZERO ),
582 vertices.push_back( MeshData::Vertex( Vector3( brx + shadowOffset.x, tly + shadowOffset.y, ZERO ),
583 Vector2( ONE, ZERO ),
586 vertices.push_back( MeshData::Vertex( Vector3( tlx + shadowOffset.x, bry + shadowOffset.y, ZERO ),
587 Vector2( ZERO, ONE ),
590 vertices.push_back( MeshData::Vertex( Vector3( brx + shadowOffset.x, bry + shadowOffset.y, ZERO ),
595 Material newMaterial = Material::New("effect buffer");
596 newMaterial.SetDiffuseTexture( meshRecord.mBuffer );
597 meshData.SetMaterial( newMaterial );
598 meshData.SetVertices( vertices );
599 meshData.SetFaceIndices( mFace );
600 meshData.SetHasNormals( true );
601 meshData.SetHasColor( false );
602 meshData.SetHasTextureCoords( true );
603 MeshActor actor = MeshActor::New( Mesh::New( meshData ) );
604 actor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR );
605 actor.SetShaderEffect( mBgraShader );
606 actor.SetFilterMode( FilterMode::LINEAR, FilterMode::LINEAR );
607 actor.SetSortModifier( 0.1f ); // force behind main text
609 // Create a sub actor to render once with normalized vertex positions
610 MeshData newMeshData;
611 MeshData::VertexContainer newVerts;
612 MeshData::FaceIndices newFaces;
613 MeshData::FaceIndices faces = meshRecord.mMeshData.GetFaces();
614 for ( uint32_t i = 0; i < verts.size(); ++i )
616 MeshData::Vertex vertex = verts[ i ];
617 vertex.x = ( ( vertex.x - tlx ) * divWidth ) - ONE;
618 vertex.y = ( ( vertex.y - tly ) * divHeight ) - ONE;
619 newVerts.push_back( vertex );
622 // Reverse triangle winding order
623 uint32_t faceCount = faces.size() / 3;
624 for ( uint32_t i = 0; i < faceCount; ++i )
626 uint32_t index = i * 3;
627 newFaces.push_back( faces[ index + 2 ] );
628 newFaces.push_back( faces[ index + 1 ] );
629 newFaces.push_back( faces[ index ] );
632 newMeshData.SetMaterial( meshRecord.mMeshData.GetMaterial() );
633 newMeshData.SetVertices( newVerts );
634 newMeshData.SetFaceIndices( newFaces );
635 newMeshData.SetHasNormals( true );
636 newMeshData.SetHasColor( false );
637 newMeshData.SetHasTextureCoords( true );
639 MeshActor subActor = MeshActor::New( Mesh::New( newMeshData ) );
640 subActor.SetColorMode( USE_OWN_MULTIPLY_PARENT_COLOR );
641 subActor.SetColor( shadowColor );
642 subActor.SetShaderEffect( mBasicShadowShader );
643 subActor.SetFilterMode( FilterMode::NEAREST, FilterMode::NEAREST );
645 // Create a render task to render the effect
646 RenderTask task = Stage::GetCurrent().GetRenderTaskList().CreateTask();
647 task.SetTargetFrameBuffer( meshRecord.mBuffer );
648 task.SetSourceActor( subActor );
649 task.SetClearEnabled( true );
650 task.SetClearColor( Vector4::ZERO );
651 task.SetExclusive( true );
652 task.SetRefreshRate( RenderTask::REFRESH_ONCE );
653 task.FinishedSignal().Connect( this, &AtlasRenderer::Impl::RenderComplete );
654 actor.Add( subActor );
658 void RenderComplete( RenderTask& renderTask )
660 // Disconnect and remove this single shot render task
661 renderTask.FinishedSignal().Disconnect( this, &AtlasRenderer::Impl::RenderComplete );
662 Stage::GetCurrent().GetRenderTaskList().RemoveTask( renderTask );
664 // Get the actor used for render to buffer and remove it from the parent
665 Actor renderActor = renderTask.GetSourceActor();
668 Actor parent = renderActor.GetParent();
671 parent.Remove( renderActor );
676 RenderableActor mActor; ///< The actor parent which renders the text
677 AtlasGlyphManager mGlyphManager; ///< Glyph Manager to handle upload and caching
678 Vector< uint32_t > mImageIds; ///< A list of imageIDs used by the renderer
679 TextAbstraction::FontClient mFontClient; ///> The font client used to supply glyph information
680 ShaderEffect mBasicShader; ///> Shader used to render L8 glyphs
681 ShaderEffect mBgraShader; ///> Shader used to render BGRA glyphs
682 ShaderEffect mBasicShadowShader; ///> Shader used to render drop shadow into buffer
683 std::vector< MaxBlockSize > mBlockSizes; ///> Maximum size needed to contain a glyph in a block within a new atlas
684 std::vector< MeshData::FaceIndex > mFace; ///> Face indices for a quad
687 Text::RendererPtr AtlasRenderer::New()
689 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Text::AtlasRenderer::New()\n" );
691 return Text::RendererPtr( new AtlasRenderer() );
694 RenderableActor AtlasRenderer::Render( Text::ViewInterface& view )
696 UnparentAndReset( mImpl->mActor );
698 Length numberOfGlyphs = view.GetNumberOfGlyphs();
700 if( numberOfGlyphs > 0u )
702 Vector<GlyphInfo> glyphs;
703 glyphs.Resize( numberOfGlyphs );
705 std::vector<Vector2> positions;
706 positions.resize( numberOfGlyphs );
708 numberOfGlyphs = view.GetGlyphs( glyphs.Begin(),
712 glyphs.Resize( numberOfGlyphs );
713 positions.resize( numberOfGlyphs );
715 mImpl->AddGlyphs( positions,
718 view.GetShadowOffset(),
719 view.GetShadowColor(),
720 view.IsUnderlineEnabled(),
721 view.GetUnderlineColor(),
722 view.GetUnderlineHeight() );
725 return mImpl->mActor;
728 AtlasRenderer::AtlasRenderer()
734 AtlasRenderer::~AtlasRenderer()