From 4ba28a734e4fce0b083bcb7bf460b536acce33f4 Mon Sep 17 00:00:00 2001 From: "yuzhuohuang@qq.com" Date: Tue, 25 Oct 2011 08:17:37 +0800 Subject: [PATCH] Refactor cache key a bit. --- src/subtitles/RTS.cpp | 28 +++++++++++------------ src/subtitles/RTS.h | 4 +++- src/subtitles/STS.h | 13 +---------- src/subtitles/cache_manager.cpp | 44 ++++++++++++++++++++---------------- src/subtitles/cache_manager.h | 17 +++++--------- src/subtitles/flyweight_base_types.h | 17 +++++++++++--- 6 files changed, 61 insertions(+), 62 deletions(-) diff --git a/src/subtitles/RTS.cpp b/src/subtitles/RTS.cpp index 16dc630..9c465aa 100644 --- a/src/subtitles/RTS.cpp +++ b/src/subtitles/RTS.cpp @@ -182,19 +182,19 @@ void CWord::Paint( SharedPtrCWord word, const CPoint& p, const CPoint& org, Over if(!word->m_str || overlay_list==NULL) return; CPoint psub = SubpixelPositionControler::GetGlobalControler().GetSubpixel(p); - CPoint trans_org = org - p; - OverlayKey overlay_key(*word, psub, trans_org); + CPoint trans_org = org - p; bool need_transform = word->NeedTransform(); if(!need_transform) { - overlay_key.m_org.x=0; - overlay_key.m_org.y=0; - } + trans_org.x=0; + trans_org.y=0; + } + OverlayKey overlay_key(*word, psub, trans_org); OverlayMruCache* overlay_cache = CacheManager::GetOverlayMruCache(); OverlayMruCache::hashed_cache_const_iterator iter = overlay_cache->hash_find(overlay_key); if(iter==overlay_cache->hash_end()) { - word->DoPaint(psub, trans_org, &(overlay_list->overlay)); + word->DoPaint(psub, trans_org, &(overlay_list->overlay), overlay_key); OverlayMruItem item(overlay_key, overlay_list->overlay); CacheManager::GetOverlayMruCache()->update_cache(item); } @@ -217,14 +217,12 @@ void CWord::Paint( SharedPtrCWord word, const CPoint& p, const CPoint& org, Over } } -void CWord::DoPaint(const CPoint& psub, const CPoint& trans_org, SharedPtrOverlay* overlay) +void CWord::DoPaint(const CPoint& psub, const CPoint& trans_org, SharedPtrOverlay* overlay, const OverlayKey& key) { overlay->reset(new Overlay()); - OverlayNoBlurKey overlay_no_blur_key(*this, psub, trans_org); - OverlayNoBlurMruCache* overlay_no_blur_cache = CacheManager::GetOverlayNoBlurMruCache(); - OverlayNoBlurMruCache::hashed_cache_const_iterator iter = overlay_no_blur_cache->hash_find(overlay_no_blur_key); + OverlayNoBlurMruCache::hashed_cache_const_iterator iter = overlay_no_blur_cache->hash_find(key); SharedPtrOverlay raterize_result; if(iter==overlay_no_blur_cache->hash_end()) @@ -233,20 +231,20 @@ void CWord::DoPaint(const CPoint& psub, const CPoint& trans_org, SharedPtrOverla SharedPtrScanLineData scan_line_data; ScanLineDataMruCache* scan_line_data_cache = CacheManager::GetScanLineDataMruCache(); - ScanLineDataMruCache::hashed_cache_const_iterator iter = scan_line_data_cache->hash_find(overlay_no_blur_key); + ScanLineDataMruCache::hashed_cache_const_iterator iter = scan_line_data_cache->hash_find(key); if(iter==scan_line_data_cache->hash_end()) { //get outline path, if not cached, create it and cache a copy, else copy from cache SharedPtrPathData path_data(new PathData()); PathDataMruCache* path_data_cache = CacheManager::GetPathDataMruCache(); - PathDataMruCache::hashed_cache_const_iterator iter = path_data_cache->hash_find(overlay_no_blur_key); + PathDataMruCache::hashed_cache_const_iterator iter = path_data_cache->hash_find(key); if(iter==path_data_cache->hash_end()) { if(!CreatePath(path_data)) return; SharedPtrPathData data(new PathData()); *data = *path_data;//important! copy not ref - PathDataMruItem item(overlay_no_blur_key, data); + PathDataMruItem item(key, data); path_data_cache->update_cache(item); } else @@ -268,7 +266,7 @@ void CWord::DoPaint(const CPoint& psub, const CPoint& trans_org, SharedPtrOverla { if(!CreateOpaqueBox()) return; } - ScanLineDataMruItem item(overlay_no_blur_key, scan_line_data); + ScanLineDataMruItem item(key, scan_line_data); scan_line_data_cache->update_cache(item); } else @@ -277,7 +275,7 @@ void CWord::DoPaint(const CPoint& psub, const CPoint& trans_org, SharedPtrOverla } if(!Rasterizer::Rasterize(*scan_line_data, psub.x, psub.y, raterize_result)) return; - OverlayNoBlurMruItem item(overlay_no_blur_key, raterize_result); + OverlayNoBlurMruItem item(key, raterize_result); overlay_no_blur_cache->update_cache(item); } else diff --git a/src/subtitles/RTS.h b/src/subtitles/RTS.h index e9e20e5..bd7d493 100644 --- a/src/subtitles/RTS.h +++ b/src/subtitles/RTS.h @@ -66,6 +66,8 @@ class CWord; typedef CWord* PCWord; typedef ::boost::shared_ptr SharedPtrCWord; typedef ::boost::shared_ptr SharedPtrCPolygon; + +class OverlayKey; class CWord { bool NeedTransform(); @@ -82,7 +84,7 @@ protected: virtual bool CreatePath(SharedPtrPathData path_data) = 0; - void DoPaint(const CPoint& p, const CPoint& org, SharedPtrOverlay* overlay); + void DoPaint(const CPoint& p, const CPoint& org, SharedPtrOverlay* overlay, const OverlayKey& key); public: bool m_fWhiteSpaceChar, m_fLineBreak; diff --git a/src/subtitles/STS.h b/src/subtitles/STS.h index f23fe0a..068a3da 100644 --- a/src/subtitles/STS.h +++ b/src/subtitles/STS.h @@ -71,20 +71,9 @@ public: typedef ::boost::flyweights::flyweight FwSTSStyle; //for FwSTSStyle -static inline std::size_t hash_value(const double& d) -{ - std::size_t hash = 515; - const int32_t* tmp = reinterpret_cast(&d); - hash += (hash<<5); - hash += *(tmp++); - hash += (hash<<5); - hash += *(tmp++); - return hash; -} - static inline std::size_t hash_value(const STSStyle& s) { - //Todo: fix me + //Todo: fix me std::size_t hash = CStringElementTraits::Hash(s.fontName); hash = (hash<<5) + (hash) + s.colors[0]; hash = (hash<<5) + (hash) + s.colors[2]; diff --git a/src/subtitles/cache_manager.cpp b/src/subtitles/cache_manager.cpp index 29d10db..688025c 100644 --- a/src/subtitles/cache_manager.cpp +++ b/src/subtitles/cache_manager.cpp @@ -14,16 +14,6 @@ std::size_t hash_value(const CWordCacheKey& key) { return( CStringElementTraits::Hash(key.m_str) ); } - -std::size_t hash_value(const OverlayKey& key) -{ - size_t hash = CStringElementTraits::Hash(key.m_str); - hash += (hash<<5); - hash += key.m_p.x; - hash += (hash<<5); - hash += key.m_p.y; - return hash; -} std::size_t hash_value( const PathDataCacheKey& key ) { @@ -51,6 +41,16 @@ std::size_t hash_value( const OverlayNoBlurKey& key ) hash += key.m_p.y; return hash; } + +std::size_t hash_value(const OverlayKey& key) +{ + size_t hash = hash_value(static_cast(key)); + hash += (hash<<5); + hash += key.m_style.get().fBlur; + hash += (hash<<5); + hash += hash_value(key.m_style.get().fGaussianBlur); + return hash; +} CWordCacheKey::CWordCacheKey( const CWord& word ) { @@ -144,6 +144,20 @@ bool OverlayNoBlurKey::operator==( const OverlayNoBlurKey& key ) const ////////////////////////////////////////////////////////////////////////////////////////////// +// OverlayKey + +bool OverlayKey::operator==( const OverlayKey& key ) const +{ + return OverlayNoBlurKey::operator==(key) + && fabs(this->m_style.get().fGaussianBlur - key.m_style.get().fGaussianBlur) < 0.000001 + && this->m_style.get().fBlur == key.m_style.get().fBlur; + //static_cast will call copy constructer to construct a tmp obj + //return ((CWordCacheKey)(*this)==(CWordCacheKey)key) && (m_p.x==key.m_p.x) && (m_p.y==key.m_p.y) + // && (m_org.x==key.m_org.x) && (m_org.y==key.m_org.y); +} + +////////////////////////////////////////////////////////////////////////////////////////////// + // CacheManager CWordMruCache* CacheManager::s_word_mru_cache = NULL; @@ -196,13 +210,3 @@ ScanLineDataMruCache* CacheManager::GetScanLineDataMruCache() } return s_scan_line_data_mru_cache; } - - -bool OverlayKey::operator==( const OverlayKey& key ) const -{ - return CWordCacheKey::operator==(key) && (m_p.x==key.m_p.x) && (m_p.y==key.m_p.y) - && (m_org.x==key.m_org.x) && (m_org.y==key.m_org.y); - //static_cast will call copy constructer to construct a tmp obj - //return ((CWordCacheKey)(*this)==(CWordCacheKey)key) && (m_p.x==key.m_p.x) && (m_p.y==key.m_p.y) - // && (m_org.x==key.m_org.x) && (m_org.y==key.m_org.y); -} diff --git a/src/subtitles/cache_manager.h b/src/subtitles/cache_manager.h index 6b66cda..db9f023 100644 --- a/src/subtitles/cache_manager.h +++ b/src/subtitles/cache_manager.h @@ -68,20 +68,15 @@ public: POINT m_p; }; -class OverlayKey: public CWordCacheKey +class OverlayKey: public OverlayNoBlurKey { public: - OverlayKey(const CWord& word, const POINT& p, const POINT& org):CWordCacheKey(word),m_p(p),m_org(org) { } - OverlayKey(const OverlayKey& key):CWordCacheKey(key),m_p(key.m_p),m_org(key.m_org) { } - OverlayKey(const FwSTSStyle& style, const CStringW& str, int ktype, int kstart, int kend, const POINT& p, const POINT& org) - :CWordCacheKey(style, str, ktype, kstart, kend),m_p(p), m_org(org) { } - bool operator==(const OverlayKey& key)const; - bool CompareTo(const CWord& word, const POINT& p, const POINT& org)const - { - return ((CWordCacheKey)(*this)==word) && (m_p.x==p.x) && (m_p.y==p.y) && (m_org.x==org.x) && (m_org.y==org.y); - } + OverlayKey(const CWord& word, const POINT& p, const POINT& org):OverlayNoBlurKey(word, p, org) { } + OverlayKey(const OverlayKey& key):OverlayNoBlurKey(key){ } - POINT m_p, m_org; + bool operator==(const OverlayKey& key)const; + + friend std::size_t hash_value(const OverlayKey& key); }; std::size_t hash_value(const CWord& key); diff --git a/src/subtitles/flyweight_base_types.h b/src/subtitles/flyweight_base_types.h index 198575c..f08a156 100644 --- a/src/subtitles/flyweight_base_types.h +++ b/src/subtitles/flyweight_base_types.h @@ -8,17 +8,28 @@ typedef ::boost::flyweights::flyweight FwRect; -inline std::size_t hash_value(const CStringW& s) +static inline std::size_t hash_value(const double& d) +{ + std::size_t hash = 515; + const int32_t* tmp = reinterpret_cast(&d); + hash += (hash<<5); + hash += *(tmp++); + hash += (hash<<5); + hash += *(tmp++); + return hash; +} + +static inline std::size_t hash_value(const CStringW& s) { return CStringElementTraits::Hash(s); } -inline std::size_t hash_value(const CStringA& s) +static inline std::size_t hash_value(const CStringA& s) { return CStringElementTraits::Hash(s); } -inline std::size_t hash_value(const CRect& s) +static inline std::size_t hash_value(const CRect& s) { std::size_t hash = 515; hash += (hash<<5); -- 2.11.4.GIT