Minor fix.
[xy_vsfilter.git] / src / subtitles / cache_manager.h
blob26b73cb3910d6e86d19b621bb88509e8988419fd
1 /************************************************************************/
2 /* author: xy */
3 /* date: 20110926 */
4 /************************************************************************/
5 #ifndef __CACHE_MANAGER_H_310C134F_844C_4590_A4D2_AD30165AF10A__
6 #define __CACHE_MANAGER_H_310C134F_844C_4590_A4D2_AD30165AF10A__
8 #include "RTS.h"
9 #include "mru_cache.h"
10 #include "flyweight_base_types.h"
12 template<class CahcheKey>
13 class XyCacheKeyTraits:public CElementTraits<CahcheKey>
15 public:
16 static inline ULONG Hash(const CahcheKey& key)
18 return key.GetHashValue();
20 static inline bool CompareElements(
21 const CahcheKey& element1,
22 const CahcheKey& element2)
24 return ( (element1==element2)!=0 );
28 class TextInfoCacheKey
30 public:
31 XyFwStringW::IdType m_str_id;
32 FwSTSStyle m_style;
34 ULONG m_hash_value;
35 public:
36 bool operator==(const TextInfoCacheKey& key)const;
38 ULONG UpdateHashValue();
39 inline ULONG GetHashValue()const
41 return m_hash_value;
45 class PathDataCacheKey
47 public:
48 PathDataCacheKey(const CWord& word):m_style(word.m_style)
50 m_str_id = word.m_str.GetId();
52 PathDataCacheKey(const PathDataCacheKey& key)
53 :m_str_id(key.m_str_id)
54 ,m_style(key.m_style)
55 ,m_hash_value(key.m_hash_value){}
56 PathDataCacheKey(const FwSTSStyle& style, XyFwStringW::IdType str_id):m_style(style)
58 m_str_id = str_id;
60 bool operator==(const PathDataCacheKey& key)const
62 return m_str_id==key.m_str_id && ( m_style==key.m_style || CompareSTSStyle(m_style, key.m_style) );
64 bool operator==(const CWord& key)const
66 return m_str_id==key.m_str.GetId() && ( m_style==key.m_style || CompareSTSStyle(m_style.get(), key.m_style.get()) );
69 static bool CompareSTSStyle(const STSStyle& lhs, const STSStyle& rhs);
71 ULONG UpdateHashValue();
72 inline ULONG GetHashValue()const
74 return m_hash_value;
76 public:
77 ULONG m_hash_value;
78 protected:
79 XyFwStringW::IdType m_str_id;
80 FwSTSStyle m_style;
83 class ScanLineData2CacheKey: public PathDataCacheKey
85 public:
86 ScanLineData2CacheKey(const CWord& word, const POINT& org):PathDataCacheKey(word),m_org(org) { }
87 ScanLineData2CacheKey(const ScanLineData2CacheKey& key)
88 :PathDataCacheKey(key)
89 ,m_org(key.m_org)
90 ,m_hash_value(key.m_hash_value) { }
91 ScanLineData2CacheKey(const FwSTSStyle& style, XyFwStringW::IdType str_id, const POINT& org)
92 :PathDataCacheKey(style, str_id),m_org(org) { }
93 bool operator==(const ScanLineData2CacheKey& key)const;
95 ULONG UpdateHashValue();
96 inline ULONG GetHashValue()const
98 return m_hash_value;
100 public:
101 ULONG m_hash_value;
102 POINT m_org;
105 class OverlayNoBlurKey: public ScanLineData2CacheKey
107 public:
108 OverlayNoBlurKey(const CWord& word, const POINT& p, const POINT& org):ScanLineData2CacheKey(word,org),m_p(p) {}
109 OverlayNoBlurKey(const OverlayNoBlurKey& key):ScanLineData2CacheKey(key),m_p(key.m_p),m_hash_value(key.m_hash_value) {}
110 OverlayNoBlurKey(const FwSTSStyle& style, XyFwStringW::IdType str_id, const POINT& p, const POINT& org)
111 :ScanLineData2CacheKey(style, str_id, org),m_p(p) {}
112 bool operator==(const OverlayNoBlurKey& key)const;
114 ULONG UpdateHashValue();
115 inline ULONG GetHashValue()const
117 return m_hash_value;
119 public:
120 ULONG m_hash_value;
121 POINT m_p;
124 class OverlayKey: public OverlayNoBlurKey
126 public:
127 OverlayKey(const CWord& word, const POINT& p, const POINT& org):OverlayNoBlurKey(word, p, org) {}
128 OverlayKey(const OverlayKey& key):OverlayNoBlurKey(key), m_hash_value(key.m_hash_value) {}
130 bool operator==(const OverlayKey& key)const;
132 ULONG UpdateHashValue();
133 inline ULONG GetHashValue()const
135 return m_hash_value;
137 public:
138 ULONG m_hash_value;
141 class ScanLineDataCacheKey
143 public:
144 ScanLineDataCacheKey(const SharedPtrConstPathData& path_data):m_path_data(path_data){}
145 bool operator==(const ScanLineDataCacheKey& key)const;
147 ULONG UpdateHashValue();
148 inline ULONG GetHashValue()const
150 return m_hash_value;
152 public:
153 ULONG m_hash_value;
154 SharedPtrConstPathData m_path_data;
157 class OverlayNoOffsetKey:public ScanLineDataCacheKey
159 public:
160 OverlayNoOffsetKey(const SharedPtrConstPathData& path_data, int xsub, int ysub, int border_x, int border_y)
161 : ScanLineDataCacheKey(path_data)
162 , m_border( border_x+(border_y<<16) )
163 , m_rasterize_sub( xsub+(ysub<<16) ){}
164 bool operator==(const OverlayNoOffsetKey& key)const;
166 ULONG UpdateHashValue();
167 inline ULONG GetHashValue()const
169 return m_hash_value;
171 public:
172 ULONG m_hash_value;
174 int m_border;
175 int m_rasterize_sub;
178 class ClipperAlphaMaskCacheKey
180 public:
181 ClipperAlphaMaskCacheKey(const SharedPtrCClipper& clipper):m_clipper(clipper){}
182 bool operator==(const ClipperAlphaMaskCacheKey& key)const;
184 ULONG UpdateHashValue();
185 inline ULONG GetHashValue()const
187 return m_hash_value;
189 public:
190 ULONG m_hash_value;
192 SharedPtrCClipper m_clipper;
195 struct DrawItem;
196 class DrawItemHashKey
198 public:
199 DrawItemHashKey(const DrawItem& draw_item);
200 bool operator==(const DrawItemHashKey& hash_key) const;
202 ULONG UpdateHashValue();
203 inline ULONG GetHashValue()const
205 return m_hash_value;
207 public:
208 ULONG m_hash_value;
209 private:
210 typedef ::boost::shared_ptr<OverlayKey> SharedOverlayKey;
211 SharedOverlayKey m_overlay_key;
212 ClipperAlphaMaskCacheKey m_clipper_key;
213 CRect m_clip_rect;
214 int m_xsub;
215 int m_ysub;
216 DWORD m_switchpts[6];
217 bool m_fBody;
218 bool m_fBorder;
221 class GroupedDrawItemsHashKey
223 public:
224 bool operator==(const GroupedDrawItemsHashKey& key) const;
226 ULONG UpdateHashValue();
227 inline ULONG GetHashValue()const
229 return m_hash_value;
231 public:
232 ULONG m_hash_value;
233 private:
234 typedef ::boost::shared_ptr<DrawItemHashKey> PKey;
235 typedef CAtlArray<PKey> Keys;
236 typedef ::boost::shared_ptr<Keys> PKeys;
238 PKeys m_key;
239 CRect m_clip_rect;
241 friend struct GroupedDrawItems;
244 class PathDataTraits:public CElementTraits<PathData>
246 public:
247 static ULONG Hash(const PathData& key);
250 class ClipperTraits:public CElementTraits<CClipper>
252 public:
253 static ULONG Hash(const CClipper& key);
256 typedef EnhancedXyMru<
257 TextInfoCacheKey,
258 CText::SharedPtrTextInfo,
259 XyCacheKeyTraits<TextInfoCacheKey>
260 > TextInfoMruCache;
262 typedef EnhancedXyMru<
263 CStringW,
264 CRenderedTextSubtitle::SharedPtrConstAssTagList,
265 CStringElementTraits<CStringW>
266 > AssTagListMruCache;
268 typedef EnhancedXyMru<PathDataCacheKey, SharedPtrConstPathData, XyCacheKeyTraits<PathDataCacheKey>> PathDataMruCache;
270 typedef EnhancedXyMru<ScanLineData2CacheKey, SharedPtrConstScanLineData2, XyCacheKeyTraits<ScanLineData2CacheKey>> ScanLineData2MruCache;
272 typedef EnhancedXyMru<OverlayNoBlurKey, SharedPtrOverlay, XyCacheKeyTraits<OverlayNoBlurKey>> OverlayNoBlurMruCache;
274 typedef EnhancedXyMru<OverlayKey, SharedPtrOverlay, XyCacheKeyTraits<OverlayKey>> OverlayMruCache;
276 typedef EnhancedXyMru<ScanLineDataCacheKey, SharedPtrConstScanLineData, XyCacheKeyTraits<ScanLineDataCacheKey>> ScanLineDataMruCache;
278 typedef EnhancedXyMru<OverlayNoOffsetKey, OverlayNoBlurKey, XyCacheKeyTraits<OverlayNoOffsetKey>> OverlayNoOffsetMruCache;
280 typedef EnhancedXyMru<ClipperAlphaMaskCacheKey, SharedPtrGrayImage2, XyCacheKeyTraits<ClipperAlphaMaskCacheKey>> ClipperAlphaMaskMruCache;
282 class XyBitmap;
283 typedef ::boost::shared_ptr<XyBitmap> SharedPtrXyBitmap;
284 typedef EnhancedXyMru<std::size_t, SharedPtrXyBitmap> BitmapMruCache;
286 class CacheManager
288 public:
289 static const int BITMAP_MRU_CACHE_ITEM_NUM = 64;//for test only
290 static const int CLIPPER_MRU_CACHE_ITEM_NUM = 48;
292 static const int TEXT_INFO_CACHE_ITEM_NUM = 2048;
293 static const int ASS_TAG_LIST_CACHE_ITEM_NUM = 256;
294 static const int SUBPIXEL_VARIANCE_CACHE_ITEM_NUM = 256;
295 static const int OVERLAY_CACHE_ITEM_NUM = 256;
297 static const int OVERLAY_NO_BLUR_CACHE_ITEM_NUM = 256;
298 static const int SCAN_LINE_DATA_CACHE_ITEM_NUM = 512;
299 static const int PATH_CACHE_ITEM_NUM = 512;
300 static const int WORD_CACHE_ITEM_NUM = 512;
302 static BitmapMruCache* GetBitmapMruCache();
304 static ClipperAlphaMaskMruCache* GetClipperAlphaMaskMruCache();
305 static TextInfoMruCache* GetTextInfoCache();
306 static AssTagListMruCache* GetAssTagListMruCache();
308 static ScanLineDataMruCache* GetScanLineDataMruCache();
309 static OverlayNoOffsetMruCache* GetOverlayNoOffsetMruCache();
311 static OverlayMruCache* GetSubpixelVarianceCache();
312 static OverlayMruCache* GetOverlayMruCache();
313 static OverlayNoBlurMruCache* GetOverlayNoBlurMruCache();
314 static ScanLineData2MruCache* GetScanLineData2MruCache();
315 static PathDataMruCache* GetPathDataMruCache();
319 typedef XyFlyWeight<GroupedDrawItemsHashKey, CacheManager::BITMAP_MRU_CACHE_ITEM_NUM, XyCacheKeyTraits<GroupedDrawItemsHashKey>> XyFwGroupedDrawItemsHashKey;
321 #endif // end of __CACHE_MANAGER_H_310C134F_844C_4590_A4D2_AD30165AF10A__