Support unrar64.dll
[xy_vsfilter.git] / src / subtitles / cache_manager.h
blobe3c707e1e6550a6f963cf9e91c8822f52946ec51
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 const CPolygon *tmp = dynamic_cast<const CPolygon *>(&word);
51 if (tmp)
53 m_scalex = tmp->m_scalex;
54 m_scaley = tmp->m_scaley;
56 else
58 m_scalex = 0;
59 m_scaley = 0;
61 m_str_id = word.m_str.GetId();
63 PathDataCacheKey(const PathDataCacheKey& key)
64 :m_str_id(key.m_str_id)
65 ,m_scalex(key.m_scalex)
66 ,m_scaley(key.m_scaley)
67 ,m_style(key.m_style)
68 ,m_hash_value(key.m_hash_value){}
69 bool operator==(const PathDataCacheKey& key)const
71 return m_str_id==key.m_str_id
72 && fabs(m_scalex-key.m_scalex)<0.000001
73 && fabs(m_scaley-key.m_scaley)<0.000001
74 && ( m_style==key.m_style || CompareSTSStyle(m_style, key.m_style) );
76 bool operator==(const CWord& key)const
78 return operator==(PathDataCacheKey(key));
81 static bool CompareSTSStyle(const STSStyle& lhs, const STSStyle& rhs);
83 ULONG UpdateHashValue();
84 inline ULONG GetHashValue()const
86 return m_hash_value;
88 public:
89 ULONG m_hash_value;
90 protected:
91 double m_scalex, m_scaley;//for CPolygon
92 XyFwStringW::IdType m_str_id;
93 FwSTSStyle m_style;
96 class ScanLineData2CacheKey: public PathDataCacheKey
98 public:
99 ScanLineData2CacheKey(const CWord& word, const POINT& org):PathDataCacheKey(word),m_org(org) { }
100 ScanLineData2CacheKey(const ScanLineData2CacheKey& key)
101 :PathDataCacheKey(key)
102 ,m_org(key.m_org)
103 ,m_hash_value(key.m_hash_value) { }
105 bool operator==(const ScanLineData2CacheKey& key)const;
107 ULONG UpdateHashValue();
108 inline ULONG GetHashValue()const
110 return m_hash_value;
112 public:
113 ULONG m_hash_value;
114 POINT m_org;
117 class OverlayNoBlurKey: public ScanLineData2CacheKey
119 public:
120 OverlayNoBlurKey(const CWord& word, const POINT& p, const POINT& org):ScanLineData2CacheKey(word,org),m_p(p) {}
121 OverlayNoBlurKey(const OverlayNoBlurKey& key):ScanLineData2CacheKey(key),m_p(key.m_p),m_hash_value(key.m_hash_value) {}
123 bool operator==(const OverlayNoBlurKey& key)const;
125 ULONG UpdateHashValue();
126 inline ULONG GetHashValue()const
128 return m_hash_value;
130 public:
131 ULONG m_hash_value;
132 POINT m_p;
135 class OverlayKey: public OverlayNoBlurKey
137 public:
138 OverlayKey(const CWord& word, const POINT& p, const POINT& org):OverlayNoBlurKey(word, p, org) {}
139 OverlayKey(const OverlayKey& key):OverlayNoBlurKey(key), m_hash_value(key.m_hash_value) {}
141 bool operator==(const OverlayKey& key)const;
143 ULONG UpdateHashValue();
144 inline ULONG GetHashValue()const
146 return m_hash_value;
148 public:
149 ULONG m_hash_value;
152 class ScanLineDataCacheKey
154 public:
155 ScanLineDataCacheKey(const SharedPtrConstPathData& path_data):m_path_data(path_data){}
156 bool operator==(const ScanLineDataCacheKey& key)const;
158 ULONG UpdateHashValue();
159 inline ULONG GetHashValue()const
161 return m_hash_value;
163 public:
164 ULONG m_hash_value;
165 SharedPtrConstPathData m_path_data;
168 class OverlayNoOffsetKey:public ScanLineDataCacheKey
170 public:
171 OverlayNoOffsetKey(const SharedPtrConstPathData& path_data, int xsub, int ysub, int border_x, int border_y)
172 : ScanLineDataCacheKey(path_data)
173 , m_border( border_x+(border_y<<16) )
174 , m_rasterize_sub( xsub+(ysub<<16) ){}
175 bool operator==(const OverlayNoOffsetKey& key)const;
177 ULONG UpdateHashValue();
178 inline ULONG GetHashValue()const
180 return m_hash_value;
182 public:
183 ULONG m_hash_value;
185 int m_border;
186 int m_rasterize_sub;
189 class ClipperAlphaMaskCacheKey
191 public:
192 ClipperAlphaMaskCacheKey(const SharedPtrCClipper& clipper):m_clipper(clipper){}
193 bool operator==(const ClipperAlphaMaskCacheKey& key)const;
195 ULONG UpdateHashValue();
196 inline ULONG GetHashValue()const
198 return m_hash_value;
200 public:
201 ULONG m_hash_value;
203 SharedPtrCClipper m_clipper;
206 struct DrawItem;
207 class DrawItemHashKey
209 public:
210 DrawItemHashKey(const DrawItem& draw_item);
211 bool operator==(const DrawItemHashKey& hash_key) const;
213 ULONG UpdateHashValue();
214 inline ULONG GetHashValue()const
216 return m_hash_value;
218 public:
219 ULONG m_hash_value;
220 private:
221 typedef ::boost::shared_ptr<OverlayKey> SharedOverlayKey;
222 SharedOverlayKey m_overlay_key;
223 ClipperAlphaMaskCacheKey m_clipper_key;
224 CRect m_clip_rect;
225 int m_xsub;
226 int m_ysub;
227 DWORD m_switchpts[6];
228 bool m_fBody;
229 bool m_fBorder;
232 class GroupedDrawItemsHashKey
234 public:
235 bool operator==(const GroupedDrawItemsHashKey& key) const;
237 ULONG UpdateHashValue();
238 inline ULONG GetHashValue()const
240 return m_hash_value;
242 public:
243 ULONG m_hash_value;
244 private:
245 typedef ::boost::shared_ptr<DrawItemHashKey> PKey;
246 typedef CAtlArray<PKey> Keys;
247 typedef ::boost::shared_ptr<Keys> PKeys;
249 PKeys m_key;
250 CRect m_clip_rect;
252 friend struct GroupedDrawItems;
255 class PathDataTraits:public CElementTraits<PathData>
257 public:
258 static ULONG Hash(const PathData& key);
261 class ClipperTraits:public CElementTraits<CClipper>
263 public:
264 static ULONG Hash(const CClipper& key);
267 typedef EnhancedXyMru<
268 TextInfoCacheKey,
269 CText::SharedPtrTextInfo,
270 XyCacheKeyTraits<TextInfoCacheKey>
271 > TextInfoMruCache;
273 typedef EnhancedXyMru<
274 CStringW,
275 CRenderedTextSubtitle::SharedPtrConstAssTagList,
276 CStringElementTraits<CStringW>
277 > AssTagListMruCache;
279 typedef EnhancedXyMru<PathDataCacheKey, SharedPtrConstPathData, XyCacheKeyTraits<PathDataCacheKey>> PathDataMruCache;
281 typedef EnhancedXyMru<ScanLineData2CacheKey, SharedPtrConstScanLineData2, XyCacheKeyTraits<ScanLineData2CacheKey>> ScanLineData2MruCache;
283 typedef EnhancedXyMru<OverlayNoBlurKey, SharedPtrOverlay, XyCacheKeyTraits<OverlayNoBlurKey>> OverlayNoBlurMruCache;
285 typedef EnhancedXyMru<OverlayKey, SharedPtrOverlay, XyCacheKeyTraits<OverlayKey>> OverlayMruCache;
287 typedef EnhancedXyMru<ScanLineDataCacheKey, SharedPtrConstScanLineData, XyCacheKeyTraits<ScanLineDataCacheKey>> ScanLineDataMruCache;
289 typedef EnhancedXyMru<OverlayNoOffsetKey, OverlayNoBlurKey, XyCacheKeyTraits<OverlayNoOffsetKey>> OverlayNoOffsetMruCache;
291 typedef EnhancedXyMru<ClipperAlphaMaskCacheKey, SharedPtrGrayImage2, XyCacheKeyTraits<ClipperAlphaMaskCacheKey>> ClipperAlphaMaskMruCache;
293 class XyBitmap;
294 typedef ::boost::shared_ptr<XyBitmap> SharedPtrXyBitmap;
295 typedef EnhancedXyMru<std::size_t, SharedPtrXyBitmap> BitmapMruCache;
297 class CacheManager
299 public:
300 static const int BITMAP_MRU_CACHE_ITEM_NUM = 64;//for test only
301 static const int CLIPPER_MRU_CACHE_ITEM_NUM = 48;
303 static const int TEXT_INFO_CACHE_ITEM_NUM = 2048;
304 static const int ASS_TAG_LIST_CACHE_ITEM_NUM = 2048;
305 static const int SUBPIXEL_VARIANCE_CACHE_ITEM_NUM = 256;
306 static const int OVERLAY_CACHE_ITEM_NUM = 256;
308 static const int OVERLAY_NO_BLUR_CACHE_ITEM_NUM = 256;
309 static const int SCAN_LINE_DATA_CACHE_ITEM_NUM = 512;
310 static const int PATH_CACHE_ITEM_NUM = 512;
311 static const int WORD_CACHE_ITEM_NUM = 512;
313 static BitmapMruCache* GetBitmapMruCache();
315 static ClipperAlphaMaskMruCache* GetClipperAlphaMaskMruCache();
316 static TextInfoMruCache* GetTextInfoCache();
317 static AssTagListMruCache* GetAssTagListMruCache();
319 static ScanLineDataMruCache* GetScanLineDataMruCache();
320 static OverlayNoOffsetMruCache* GetOverlayNoOffsetMruCache();
322 static OverlayMruCache* GetSubpixelVarianceCache();
323 static OverlayMruCache* GetOverlayMruCache();
324 static OverlayNoBlurMruCache* GetOverlayNoBlurMruCache();
325 static ScanLineData2MruCache* GetScanLineData2MruCache();
326 static PathDataMruCache* GetPathDataMruCache();
330 typedef XyFlyWeight<GroupedDrawItemsHashKey, CacheManager::BITMAP_MRU_CACHE_ITEM_NUM, XyCacheKeyTraits<GroupedDrawItemsHashKey>> XyFwGroupedDrawItemsHashKey;
332 #endif // end of __CACHE_MANAGER_H_310C134F_844C_4590_A4D2_AD30165AF10A__