2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://floralicense.org/license/
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FGrp_FontCache.cpp
20 * @brief This is the cpp file for _FontCache class.
27 #include <FBaseErrors.h>
29 #include <FBaseSysLog.h>
31 #include "FGrp_FontCache.h"
32 #include "FGrp_FontRsrcManager.h"
35 #define CONVERT_INTEGER_TO_26_6_FIXED_POINT(value) (value * 64)
36 #define __pBspCache ((_BspTree<unsigned long, void*>*)__pCache)
42 const unsigned long _CACHE_MEMORY_SIZE = 3 * 1024 * 1024;
43 const unsigned long _MAX_CACHABLE_ITEM_SIZE = _CACHE_MEMORY_SIZE / 10;
46 _MakeId(unsigned long character, int size, int style, unsigned long& out)
48 int fixedSize = size >> 5;
50 if (((character >> 22) > 0) || ((fixedSize >> 8) > 0) || ((style >> 2) > 0))
55 out = (character << 10) | ((fixedSize & 0xFF) << 2) | (style & 0x3);
60 template <typename Type>
61 inline unsigned long _MakeIndex(Type id, int level);
65 _MakeIndex<unsigned long>(unsigned long id, int level)
67 typedef unsigned long TBase;
69 TBase index = (id & (0x80808080 >> level)) >> ((sizeof(TBase) * 2 - 1) - level);
71 index |= (index >> 7);
72 index |= (index >> 14);
73 index |= (index >> 21);
75 return (index & 0x0000000F);
78 template <typename TId, typename TData>
84 MAX_LEVEL = sizeof(TId) * 2
88 _BspTree(int level = 0)
92 for (int i = 0; i < MAX_CHILD; i++)
103 bool Add(TId id, TData data)
108 // collision should not be occured.
113 unsigned long index = __GetIndex(id);
115 if (__child[index] == null)
117 __child[index] = new (std::nothrow) _BspTree<TId, TData>(__level + 1);
118 if (__child[index] == null)
124 return __child[index]->Add(id, data);
127 TData Find(TId id) const
134 unsigned long index = __GetIndex(id);
138 return __child[index]->Find(id);
154 for (int i = 0; i < MAX_CHILD; i++)
160 Tizen::Graphics::_FontRsrcManager::GetInstance().GetMemoryManager().Free(__data);
165 bool __IsEdge(void) const
167 return (__level >= MAX_LEVEL);
170 inline unsigned long __GetIndex(TId id) const
172 return _MakeIndex(id, __level);
175 bool __Remove(TId id, TData& found)
183 unsigned long index = __GetIndex(id);
187 if (__child[index]->__Remove(id, found))
189 delete __child[index];
190 __child[index] = null;
192 unsigned long check = 0;
194 for (int i = 0; i < MAX_CHILD; i++)
196 check |= (unsigned long)__child[i];
208 _BspTree<TId, TData>* __child[MAX_CHILD];
213 namespace Tizen { namespace Graphics
223 virtual ~_FontCacheImpl(void)
227 void Add(unsigned long id)
229 __leastRecentlyUsedKeyList.push_back(id);
232 void Remove(unsigned long id)
234 __leastRecentlyUsedKeyList.remove(id);
237 void RemoveFirst(void)
239 __leastRecentlyUsedKeyList.pop_front();
244 __leastRecentlyUsedKeyList.clear();
247 unsigned long GetFirst(void)
249 return __leastRecentlyUsedKeyList.front();
254 return __leastRecentlyUsedKeyList.empty();
258 //_BspTree<unsigned long, void*> bspTree;
259 std::list<unsigned long> __leastRecentlyUsedKeyList;
262 _FontCache::_FontCache(void)
263 : __pCache((void*) new (std::nothrow) _BspTree<unsigned long, void*>())
265 __pFontCacheImpl = new (std::nothrow) _FontCacheImpl;
268 _FontCache::~_FontCache(void)
270 delete reinterpret_cast<_BspTree<unsigned long, void*>*>(__pCache);
273 delete __pFontCacheImpl;
274 __pFontCacheImpl = null;
278 _FontCache::IsValid(void) const
280 return (this->__pCache && this->__pFontCacheImpl);
284 _FontCache::Find(unsigned long character, int size, int style, _IFont::Glyph** pOut)
286 unsigned long id = 0;
287 bool rtn = _MakeId(character, size, style, id);
294 _IFont::Glyph* p = (_IFont::Glyph*)__pBspCache->Find(id);
308 _FontCache::Add(unsigned long character, int size, int style, _IFont::Glyph* pGlyph)
310 unsigned long bufferSize = 0;
311 unsigned long id = 0;
312 bool rtn = _MakeId(character, size, style, id);
315 // Requested glyph is not cachable
319 bufferSize = sizeof(_IFont::Glyph) + (pGlyph->image.bytesPerLine * pGlyph->image.height);
320 _IFont::Glyph* pGlyphCache = (_IFont::Glyph*) _FontRsrcManager::GetInstance().GetMemoryManager().Alloc(bufferSize);
321 if (pGlyphCache == null)
324 bool rtn = _FontRsrcManager::GetInstance().RemoveMemory(bufferSize);
331 pGlyphCache = (_IFont::Glyph*) _FontRsrcManager::GetInstance().GetMemoryManager().Alloc(bufferSize);
333 if (pGlyphCache == null)
339 memcpy(pGlyphCache, pGlyph, sizeof(_IFont::Glyph));
340 pGlyphCache->image.pBitmap = (unsigned char*)((pGlyphCache) + 1);
341 memcpy(pGlyphCache->image.pBitmap, pGlyph->image.pBitmap, pGlyph->image.bytesPerLine * pGlyph->image.height);
343 if (__pBspCache->Add(id, pGlyphCache))
345 __pFontCacheImpl->Add(id);
355 _FontCache::Remove(unsigned long character, int size, int style)
357 unsigned long id = 0;
358 bool rtn = _MakeId(character, size, style, id);
364 __pBspCache->Remove(id);
365 __pFontCacheImpl->Remove(id);
369 _FontCache::RemoveFirst(void)
371 unsigned long id = 0;
373 id = __pFontCacheImpl->GetFirst();
375 __pBspCache->Remove(id);
376 __pFontCacheImpl->RemoveFirst();
380 _FontCache::IsEmpty(void)
382 return __pFontCacheImpl->IsEmpty();
386 _FontCache::Cleanup(void)
388 _BspTree<unsigned long, void*>* p = reinterpret_cast<_BspTree<unsigned long, void*>*>(__pCache);
391 new (p) _BspTree<unsigned long, void*>();
393 __pFontCacheImpl->RemoveAll();
399 _FontCache::__DiscardVictim(unsigned long requestedSize)
405 _FontCache::__RemoveId(unsigned long id)
407 _IFont::Glyph* pCachedGlyph = null;
409 pCachedGlyph = (_IFont::Glyph*)__pBspCache->Find(id);
411 if (pCachedGlyph != null)
413 __pBspCache->Remove(id);
423 }} // Tizen::Graphics