Improved reference caching in AtlasGlyphManager for text changes in an existing Text...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / rendering / atlas / atlas-glyph-manager-impl.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 // CLASS HEADER
18 #include <dali-toolkit/internal/text/rendering/atlas/atlas-glyph-manager-impl.h>
19
20 // EXTERNAL INCLUDES
21 #include <dali/public-api/actors/image-actor.h>
22 #include <dali/public-api/common/stage.h>
23
24 namespace Dali
25 {
26
27 namespace Toolkit
28 {
29
30 namespace Internal
31 {
32
33 //#define DISPLAY_ATLAS
34
35 AtlasGlyphManager::AtlasGlyphManager()
36 : mCount( 0 )
37 {
38   mAtlasManager = Dali::Toolkit::AtlasManager::New();
39 }
40
41 AtlasGlyphManager::~AtlasGlyphManager()
42 {
43   // Clear up any remaining references
44   for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
45         fontGlyphRecordIt != mFontGlyphRecords.end();
46         ++fontGlyphRecordIt )
47   {
48     for ( Vector< GlyphRecordEntry >::Iterator glyphRecordEntryIt = fontGlyphRecordIt->mGlyphRecords.Begin();
49           glyphRecordEntryIt != fontGlyphRecordIt->mGlyphRecords.End();
50           ++glyphRecordEntryIt )
51     {
52       mAtlasManager.Remove( glyphRecordEntryIt->mImageId );
53     }
54   }
55 }
56
57 AtlasGlyphManagerPtr AtlasGlyphManager::New()
58 {
59   AtlasGlyphManagerPtr internal = new AtlasGlyphManager();
60   return internal;
61 }
62
63 void AtlasGlyphManager::Add( Text::FontId fontId,
64                              const Text::GlyphInfo& glyph,
65                              const BufferImage& bitmap,
66                              Dali::Toolkit::AtlasManager::AtlasSlot& slot )
67 {
68   mAtlasManager.Add( bitmap, slot );
69
70   GlyphRecordEntry record;
71   record.mIndex = glyph.index;
72   record.mImageId = slot.mImageId;
73   record.mCount = 1;
74
75   // Have glyph records been created for this fontId ?
76   bool foundGlyph = false;
77   for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
78         fontGlyphRecordIt != mFontGlyphRecords.end(); ++fontGlyphRecordIt )
79   {
80     if ( fontGlyphRecordIt->mFontId == fontId )
81     {
82       fontGlyphRecordIt->mGlyphRecords.PushBack( record );
83       foundGlyph = true;
84       break;
85     }
86   }
87
88   if ( !foundGlyph )
89   {
90     // We need to add a new font entry
91     FontGlyphRecord fontGlyphRecord;
92     fontGlyphRecord.mFontId = fontId;
93     fontGlyphRecord.mGlyphRecords.PushBack( record );
94     mFontGlyphRecords.push_back( fontGlyphRecord );
95   }
96
97 #ifdef DISPLAY_ATLAS
98   {
99     uint32_t atlasCount = mAtlasManager.GetAtlasCount();
100     if ( atlasCount > mCount )
101     {
102       for ( uint32_t i = 0; i < atlasCount; ++i )
103       {
104         ImageActor actor = ImageActor::New( mAtlasManager.GetAtlasContainer( i + 1u ) );
105         actor.SetParentOrigin( Vector3( 0.5f, 0.25f + ( static_cast< float >( i ) * 0.25f ), 0.5f ) );
106         actor.SetAnchorPoint( AnchorPoint::CENTER );
107         actor.SetSize( 256.0f, 256.0f );
108         Stage::GetCurrent().Add( actor );
109       }
110     }
111     mCount = atlasCount;
112   }
113 #endif
114 }
115
116 void AtlasGlyphManager::GenerateMeshData( uint32_t imageId,
117                                           const Vector2& position,
118                                           MeshData& meshData )
119 {
120   // Generate mesh data and tell Atlas Manager not to handle reference counting ( we'll do it )
121   mAtlasManager.GenerateMeshData( imageId, position, meshData, false );
122 }
123
124 void AtlasGlyphManager::StitchMesh( MeshData& first,
125                                     const MeshData& second )
126 {
127   mAtlasManager.StitchMesh( first, second );
128 }
129
130 bool AtlasGlyphManager::Cached( Text::FontId fontId,
131                                 uint32_t index,
132                                 Dali::Toolkit::AtlasManager::AtlasSlot& slot )
133 {
134   for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
135         fontGlyphRecordIt != mFontGlyphRecords.end();
136         ++fontGlyphRecordIt )
137   {
138     if ( fontGlyphRecordIt->mFontId == fontId )
139     {
140       for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
141             glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
142             ++glyphRecordIt )
143       {
144         if ( glyphRecordIt->mIndex == index )
145         {
146           slot.mImageId = glyphRecordIt->mImageId;
147           slot.mAtlasId = mAtlasManager.GetAtlas( slot.mImageId );
148           return true;
149         }
150       }
151     }
152   }
153   slot.mImageId = 0;
154   return false;
155 }
156
157 Vector2 AtlasGlyphManager::GetAtlasSize( uint32_t atlasId )
158 {
159   Toolkit::AtlasManager::AtlasSize size = mAtlasManager.GetAtlasSize( atlasId );
160   return Vector2( static_cast< float >( size.mWidth ), static_cast< float >( size.mHeight ) );
161 }
162
163 void AtlasGlyphManager::SetNewAtlasSize( uint32_t width, uint32_t height, uint32_t blockWidth, uint32_t blockHeight )
164 {
165   Toolkit::AtlasManager::AtlasSize size;
166   size.mWidth = width;
167   size.mHeight = height;
168   size.mBlockWidth = blockWidth;
169   size.mBlockHeight = blockHeight;
170   mAtlasManager.SetNewAtlasSize( size );
171 }
172
173 Pixel::Format AtlasGlyphManager::GetPixelFormat( uint32_t atlasId )
174 {
175   return mAtlasManager.GetPixelFormat( atlasId );
176 }
177
178 const Toolkit::AtlasGlyphManager::Metrics& AtlasGlyphManager::GetMetrics()
179 {
180   mMetrics.mGlyphCount = mFontGlyphRecords.size();
181   mAtlasManager.GetMetrics( mMetrics.mAtlasMetrics );
182   return mMetrics;
183 }
184
185 void AtlasGlyphManager::AdjustReferenceCount( Text::FontId fontId, uint32_t imageId, int32_t delta )
186 {
187   for ( std::vector< FontGlyphRecord >::iterator fontGlyphRecordIt = mFontGlyphRecords.begin();
188         fontGlyphRecordIt != mFontGlyphRecords.end();
189         ++fontGlyphRecordIt )
190   {
191     if ( fontGlyphRecordIt->mFontId == fontId )
192     {
193       for ( Vector< GlyphRecordEntry >::Iterator glyphRecordIt = fontGlyphRecordIt->mGlyphRecords.Begin();
194             glyphRecordIt != fontGlyphRecordIt->mGlyphRecords.End();
195             ++glyphRecordIt )
196       {
197         if ( glyphRecordIt->mImageId == imageId )
198         {
199           glyphRecordIt->mCount += delta;
200           if ( !glyphRecordIt->mCount )
201           {
202             mAtlasManager.Remove( glyphRecordIt->mImageId );
203             fontGlyphRecordIt->mGlyphRecords.Remove( glyphRecordIt );
204           }
205           return;
206         }
207       }
208     }
209   }
210 }
211
212 } // namespace Internal
213
214 } // namespace Toolkit
215
216 } // namespace Dali