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"
33 #include "util/FGrp_UtilType.h"
35 #define __pBspCache ((_BspTree<unsigned long, void*>*)__pCache)
42 _MakeId(unsigned long character, Tizen::Graphics::_Util::FixedPoint26_6 size, int style, unsigned long& out)
44 int fixedSize = size >> 5;
46 if (((character >> 22) > 0) || ((fixedSize >> 8) > 0) || ((style >> 2) > 0))
51 out = (character << 10) | ((fixedSize & 0xFF) << 2) | (style & 0x3);
56 template <typename Type>
57 inline unsigned long _MakeIndex(Type id, int level);
61 _MakeIndex<unsigned long>(unsigned long id, int level)
63 typedef unsigned long TBase;
65 TBase index = (id & (0x80808080 >> level)) >> ((sizeof(TBase) * 2 - 1) - level);
67 index |= (index >> 7);
68 index |= (index >> 14);
69 index |= (index >> 21);
71 return (index & 0x0000000F);
74 template <typename TId, typename TData>
80 MAX_LEVEL = sizeof(TId) * 2
84 _BspTree(int level = 0)
88 for (int i = 0; i < MAX_CHILD; i++)
99 bool Add(TId id, TData data)
104 // collision should not be occured.
109 unsigned long index = __GetIndex(id);
111 if (__child[index] == null)
113 __child[index] = new (std::nothrow) _BspTree<TId, TData>(__level + 1);
114 if (__child[index] == null)
120 return __child[index]->Add(id, data);
123 TData Find(TId id) const
130 unsigned long index = __GetIndex(id);
134 return __child[index]->Find(id);
150 for (int i = 0; i < MAX_CHILD; i++)
156 Tizen::Graphics::_FontRsrcManager::GetInstance().GetMemoryManager().Free(__data);
161 bool __IsEdge(void) const
163 return (__level >= MAX_LEVEL);
166 inline unsigned long __GetIndex(TId id) const
168 return _MakeIndex(id, __level);
171 bool __Remove(TId id, TData& found)
179 unsigned long index = __GetIndex(id);
183 if (__child[index]->__Remove(id, found))
185 delete __child[index];
186 __child[index] = null;
188 unsigned long check = 0;
190 for (int i = 0; i < MAX_CHILD; i++)
192 check |= (unsigned long)__child[i];
204 _BspTree<TId, TData>* __child[MAX_CHILD];
209 namespace Tizen { namespace Graphics
219 virtual ~_FontCacheImpl(void)
223 void Add(unsigned long id)
225 __leastRecentlyUsedKeyList.push_back(id);
228 void Remove(unsigned long id)
230 __leastRecentlyUsedKeyList.remove(id);
233 void RemoveFirst(void)
235 __leastRecentlyUsedKeyList.pop_front();
240 __leastRecentlyUsedKeyList.clear();
243 unsigned long GetFirst(void)
245 return __leastRecentlyUsedKeyList.front();
250 return __leastRecentlyUsedKeyList.empty();
254 //_BspTree<unsigned long, void*> bspTree;
255 std::list<unsigned long> __leastRecentlyUsedKeyList;
258 _FontCache::_FontCache(void)
259 : __pCache((void*) new (std::nothrow) _BspTree<unsigned long, void*>())
261 __pFontCacheImpl = new (std::nothrow) _FontCacheImpl;
264 _FontCache::~_FontCache(void)
266 delete reinterpret_cast<_BspTree<unsigned long, void*>*>(__pCache);
269 delete __pFontCacheImpl;
270 __pFontCacheImpl = null;
274 _FontCache::IsValid(void) const
276 return (this->__pCache && this->__pFontCacheImpl);
280 _FontCache::Find(unsigned long character, _Util::FixedPoint26_6 size, int style, _IFont::Glyph** pOut)
282 unsigned long id = 0;
283 bool rtn = _MakeId(character, size, style, id);
290 _IFont::Glyph* p = (_IFont::Glyph*)__pBspCache->Find(id);
304 _FontCache::Add(unsigned long character, _Util::FixedPoint26_6 size, int style, _IFont::Glyph* pGlyph)
306 unsigned long bufferSize = 0;
307 unsigned long id = 0;
308 bool rtn = _MakeId(character, size, style, id);
311 // Requested glyph is not cachable
315 bufferSize = sizeof(_IFont::Glyph) + (pGlyph->image.bytesPerLine * pGlyph->image.height);
316 _IFont::Glyph* pGlyphCache = (_IFont::Glyph*) _FontRsrcManager::GetInstance().GetMemoryManager().Alloc(bufferSize);
317 if (pGlyphCache == null)
320 bool rtn = _FontRsrcManager::GetInstance().RemoveMemory(bufferSize);
327 pGlyphCache = (_IFont::Glyph*) _FontRsrcManager::GetInstance().GetMemoryManager().Alloc(bufferSize);
329 if (pGlyphCache == null)
335 memcpy(pGlyphCache, pGlyph, sizeof(_IFont::Glyph));
336 pGlyphCache->image.pBitmap = (unsigned char*)((pGlyphCache) + 1);
337 memcpy(pGlyphCache->image.pBitmap, pGlyph->image.pBitmap, pGlyph->image.bytesPerLine * pGlyph->image.height);
339 if (__pBspCache->Add(id, pGlyphCache))
341 __pFontCacheImpl->Add(id);
351 _FontCache::Remove(unsigned long character, _Util::FixedPoint26_6 size, int style)
353 unsigned long id = 0;
354 bool rtn = _MakeId(character, size, style, id);
360 __pBspCache->Remove(id);
361 __pFontCacheImpl->Remove(id);
365 _FontCache::RemoveFirst(void)
367 unsigned long id = 0;
369 id = __pFontCacheImpl->GetFirst();
371 __pBspCache->Remove(id);
372 __pFontCacheImpl->RemoveFirst();
376 _FontCache::IsEmpty(void)
378 return __pFontCacheImpl->IsEmpty();
382 _FontCache::Cleanup(void)
384 _BspTree<unsigned long, void*>* p = reinterpret_cast<_BspTree<unsigned long, void*>*>(__pCache);
387 new (p) _BspTree<unsigned long, void*>();
389 __pFontCacheImpl->RemoveAll();
395 _FontCache::__DiscardVictim(unsigned long requestedSize)
401 _FontCache::__RemoveId(unsigned long id)
403 _IFont::Glyph* pCachedGlyph = null;
405 pCachedGlyph = (_IFont::Glyph*)__pBspCache->Find(id);
407 if (pCachedGlyph != null)
409 __pBspCache->Remove(id);
419 }} // Tizen::Graphics