Fix PathDataCacheKey hash.
[xy_vsfilter.git] / src / subtitles / cache_manager.cpp
blob661e602c3b6abe65279e0b3a2ea5c2a916033670
1 /************************************************************************/
2 /* author: xy */
3 /* date: 20110926 */
4 /************************************************************************/
5 #include "StdAfx.h"
6 #include "cache_manager.h"
7 #include "draw_item.h"
8 #include "xy_overlay_paint_machine.h"
9 #include "xy_clipper_paint_machine.h"
11 ULONG PathDataTraits::Hash( const PathData& key )
13 ULONG hash = 515;
14 hash += (hash<<5);
15 hash += key.mPathPoints;
16 for (int i=0;i<key.mPathPoints;i++)
18 hash += (hash<<5);
19 hash += key.mpPathTypes[i];
21 for (int i=0;i<key.mPathPoints;i++)
23 hash += (hash<<5);
24 hash += key.mpPathPoints[i].x;
25 hash += (hash<<5);
26 hash += key.mpPathPoints[i].y;
28 return hash;
31 ULONG ClipperTraits::Hash( const CClipper& key )
33 ULONG hash = CStringElementTraits<CString>::Hash(key.m_polygon->m_str);;
34 hash += (hash<<5);
35 hash += key.m_inverse;
36 hash += (hash<<5);
37 hash += key.m_effectType;
38 hash += (hash<<5);
39 hash += key.m_size.cx;
40 hash += (hash<<5);
41 hash += key.m_size.cy;
42 hash += (hash<<5);
43 hash += hash_value(key.m_polygon->m_scalex);
44 hash += (hash<<5);
45 hash += hash_value(key.m_polygon->m_scaley);
47 for (int i=0;i<sizeof(key.m_effect.param)/sizeof(key.m_effect.param[0]);i++)
49 hash += (hash<<5);
50 hash += key.m_effect.param[i];
52 for (int i=0;i<sizeof(key.m_effect.t)/sizeof(key.m_effect.t[0]);i++)
54 hash += (hash<<5);
55 hash += key.m_effect.t[i];
57 return hash;
60 //////////////////////////////////////////////////////////////////////////////////////////////
62 // TextInfoCacheKey
64 bool TextInfoCacheKey::operator==( const TextInfoCacheKey& key ) const
66 return m_str == key.m_str
67 && static_cast<const STSStyleBase&>(m_style).operator==(key.m_style)
68 && m_style.get().fontScaleX == key.m_style.get().fontScaleX
69 && m_style.get().fontScaleY == key.m_style.get().fontScaleY
70 && m_style.get().fontSpacing == key.m_style.get().fontSpacing;
73 ULONG TextInfoCacheKey::UpdateHashValue()
75 m_hash_value = CStringElementTraits<CString>::Hash(m_str);
76 m_hash_value += (m_hash_value<<5);
77 m_hash_value += hash_value( static_cast<const STSStyleBase&>(m_style.get()) );
78 m_hash_value += (m_hash_value<<5);
79 m_hash_value += hash_value( m_style.get().fontScaleX );
80 m_hash_value += (m_hash_value<<5);
81 m_hash_value += hash_value( m_style.get().fontScaleY );
82 m_hash_value += (m_hash_value<<5);
83 m_hash_value += hash_value( m_style.get().fontSpacing );
84 return m_hash_value;
87 //////////////////////////////////////////////////////////////////////////////////////////////
89 // PathDataCacheKey
91 bool PathDataCacheKey::CompareSTSStyle( const STSStyle& lhs, const STSStyle& rhs )
93 return lhs.charSet==rhs.charSet &&
94 lhs.fontName==rhs.fontName &&
95 lhs.fontSize==rhs.fontSize &&
96 lhs.fontSpacing==rhs.fontSpacing &&
97 lhs.fontWeight==rhs.fontWeight &&
98 lhs.fItalic==rhs.fItalic &&
99 lhs.fUnderline==rhs.fUnderline &&
100 lhs.fStrikeOut==rhs.fStrikeOut;
103 ULONG PathDataCacheKey::UpdateHashValue()
105 const STSStyle& style = m_style.get();
106 m_hash_value = CStringElementTraits<CString>::Hash(m_str);
107 m_hash_value += (m_hash_value<<5);
108 m_hash_value += style.charSet;
109 m_hash_value += (m_hash_value<<5);
110 m_hash_value += CStringElementTraits<CString>::Hash(style.fontName);
111 m_hash_value += (m_hash_value<<5);
112 m_hash_value += style.fontSize;
113 m_hash_value += (m_hash_value<<5);
114 m_hash_value += style.fontSpacing;
115 m_hash_value += (m_hash_value<<5);
116 m_hash_value += style.fontWeight;
117 m_hash_value += (m_hash_value<<5);
118 m_hash_value += style.fItalic;
119 m_hash_value += (m_hash_value<<5);
120 m_hash_value += style.fUnderline;
121 m_hash_value += (m_hash_value<<5);
122 m_hash_value += style.fStrikeOut;
123 return m_hash_value;
127 //////////////////////////////////////////////////////////////////////////////////////////////
129 // ScanLineData2CacheKey
131 bool ScanLineData2CacheKey::operator==( const ScanLineData2CacheKey& key ) const
133 return //(static_cast<PathDataCacheKey>(*this)==static_cast<PathDataCacheKey>(key))
134 PathDataCacheKey::operator==(key) //static_cast will call copy constructer to construct a tmp obj
135 && this->m_style.get().borderStyle == key.m_style.get().borderStyle
136 && fabs(this->m_style.get().outlineWidthX - key.m_style.get().outlineWidthX) < 0.000001
137 && fabs(this->m_style.get().outlineWidthY - key.m_style.get().outlineWidthY) < 0.000001
138 && fabs(this->m_style.get().fontScaleX - key.m_style.get().fontScaleX) < 0.000001
139 && fabs(this->m_style.get().fontScaleY - key.m_style.get().fontScaleY) < 0.000001
140 && fabs(this->m_style.get().fontAngleX - key.m_style.get().fontAngleX) < 0.000001
141 && fabs(this->m_style.get().fontAngleY - key.m_style.get().fontAngleY) < 0.000001
142 && fabs(this->m_style.get().fontAngleZ - key.m_style.get().fontAngleZ) < 0.000001
143 && fabs(this->m_style.get().fontShiftX - key.m_style.get().fontShiftX) < 0.000001
144 && fabs(this->m_style.get().fontShiftY - key.m_style.get().fontShiftY) < 0.000001
145 && (m_org.x==key.m_org.x) && (m_org.y==key.m_org.y);
148 ULONG ScanLineData2CacheKey::UpdateHashValue()
150 m_hash_value = __super::UpdateHashValue();
151 m_hash_value += (m_hash_value<<5);
152 m_hash_value += m_org.x;
153 m_hash_value += (m_hash_value<<5);
154 m_hash_value += m_org.y;//fix me: fontScale and fontAngle are not take into account
155 return m_hash_value;
158 //////////////////////////////////////////////////////////////////////////////////////////////
160 // OverlayNoBlurKey
162 bool OverlayNoBlurKey::operator==( const OverlayNoBlurKey& key ) const
164 //static_cast will call copy constructer to construct a tmp obj
165 //return (static_cast<ScanLineDataCacheKey>(*this)==static_cast<ScanLineDataCacheKey>(key))
166 // && (m_p.x==key.m_p.x) && (m_p.y==key.m_p.y);
167 return ScanLineData2CacheKey::operator==(key) && (m_p.x==key.m_p.x) && (m_p.y==key.m_p.y);
170 ULONG OverlayNoBlurKey::UpdateHashValue()
172 m_hash_value = __super::UpdateHashValue();
173 m_hash_value += (m_hash_value<<5);
174 m_hash_value += m_p.x;
175 m_hash_value += (m_hash_value<<5);
176 m_hash_value += m_p.y;
177 return m_hash_value;
181 //////////////////////////////////////////////////////////////////////////////////////////////
183 // OverlayKey
185 bool OverlayKey::operator==( const OverlayKey& key ) const
187 return OverlayNoBlurKey::operator==(key)
188 && fabs(this->m_style.get().fGaussianBlur - key.m_style.get().fGaussianBlur) < 0.000001
189 && this->m_style.get().fBlur == key.m_style.get().fBlur;
190 //static_cast will call copy constructer to construct a tmp obj
191 //return ((CWordCacheKey)(*this)==(CWordCacheKey)key) && (m_p.x==key.m_p.x) && (m_p.y==key.m_p.y)
192 // && (m_org.x==key.m_org.x) && (m_org.y==key.m_org.y);
195 ULONG OverlayKey::UpdateHashValue()
197 m_hash_value = __super::UpdateHashValue();
198 m_hash_value += (m_hash_value<<5);
199 m_hash_value += m_style.get().fBlur;
200 m_hash_value += (m_hash_value<<5);
201 m_hash_value += hash_value(m_style.get().fGaussianBlur);
202 return m_hash_value;
206 //////////////////////////////////////////////////////////////////////////////////////////////
208 // ScanLineDataCacheKey
210 bool ScanLineDataCacheKey::operator==( const ScanLineDataCacheKey& key ) const
212 return (m_path_data && key.m_path_data) ? *m_path_data==*key.m_path_data : m_path_data==key.m_path_data;
215 ULONG ScanLineDataCacheKey::UpdateHashValue()
217 m_hash_value = PathDataTraits::Hash(*m_path_data);
218 return m_hash_value;
221 //////////////////////////////////////////////////////////////////////////////////////////////
223 // OverlayNoOffsetKey
225 bool OverlayNoOffsetKey::operator==( const OverlayNoOffsetKey& key ) const
227 return (this==&key) || ( this->m_border == key.m_border && this->m_rasterize_sub == key.m_rasterize_sub &&
228 ScanLineDataCacheKey::operator==(key) );
231 ULONG OverlayNoOffsetKey::UpdateHashValue()
233 m_hash_value = __super::UpdateHashValue();
234 m_hash_value += (m_hash_value<<5);
235 m_hash_value += m_border;
236 m_hash_value += (m_hash_value<<5);
237 m_hash_value += m_rasterize_sub;
238 return m_hash_value;
242 //////////////////////////////////////////////////////////////////////////////////////////////
244 // ClipperAlphaMaskCacheKey
246 bool ClipperAlphaMaskCacheKey::operator==( const ClipperAlphaMaskCacheKey& key ) const
248 bool result = false;
249 if (m_clipper==key.m_clipper)
251 result = true;
253 else if ( m_clipper!=NULL && key.m_clipper!=NULL )
255 const CClipper& lhs = *m_clipper;
256 const CClipper& rhs = *key.m_clipper;
257 result = (lhs.m_polygon->m_str == rhs.m_polygon->m_str
258 && fabs(lhs.m_polygon->m_scalex - rhs.m_polygon->m_scalex) < 0.000001
259 && fabs(lhs.m_polygon->m_scaley - rhs.m_polygon->m_scaley) < 0.000001
260 && lhs.m_size == rhs.m_size
261 && lhs.m_inverse == rhs.m_inverse
262 && lhs.m_effectType == rhs.m_effectType
263 && lhs.m_effect == rhs.m_effect);//fix me: unsafe code
265 return result;
268 ULONG ClipperAlphaMaskCacheKey::UpdateHashValue()
270 if(m_clipper)
271 m_hash_value = ClipperTraits::Hash(*m_clipper);
272 else
273 m_hash_value = 0;
274 return m_hash_value;
278 //////////////////////////////////////////////////////////////////////////////////////////////
280 // DrawItemHashKey
282 ULONG DrawItemHashKey::UpdateHashValue()
284 m_hash_value = m_overlay_key->GetHashValue();
285 m_hash_value += (m_hash_value<<5);
286 m_hash_value += m_clipper_key.GetHashValue();
287 m_hash_value += (m_hash_value<<5);
288 m_hash_value += hash_value(m_clip_rect);
289 m_hash_value += (m_hash_value<<5);
290 m_hash_value += m_fBorder;
291 m_hash_value += (m_hash_value<<5);
292 m_hash_value += m_fBody;
293 m_hash_value += m_switchpts[0];
294 return m_hash_value;
297 DrawItemHashKey::DrawItemHashKey( const DrawItem& draw_item)
298 : m_overlay_key( draw_item.overlay_paint_machine->GetHashKey() )
299 , m_clipper_key( draw_item.clipper->GetHashKey() )
300 , m_clip_rect(draw_item.clip_rect)
301 , m_xsub(draw_item.xsub)
302 , m_ysub(draw_item.ysub)
303 , m_fBody(draw_item.fBody)
304 , m_fBorder(draw_item.fBorder)
306 for(int i=0;i<countof(m_switchpts);i++)
307 m_switchpts[i] = draw_item.switchpts[i];
310 bool DrawItemHashKey::operator==( const DrawItemHashKey& key ) const
312 return (this==&key) || (
313 *m_overlay_key==*key.m_overlay_key &&
314 m_clipper_key==key.m_clipper_key &&
315 (m_clip_rect == key.m_clip_rect)==TRUE &&
316 m_xsub == key.m_xsub &&
317 m_ysub == key.m_ysub &&
318 m_fBody == key.m_fBody &&
319 m_fBorder == key.m_fBorder &&
320 !memcmp(m_switchpts, key.m_switchpts, sizeof(m_switchpts)));
323 //////////////////////////////////////////////////////////////////////////////////////////////
325 // GroupedDrawItemsHashKey
327 ULONG GroupedDrawItemsHashKey::UpdateHashValue()
329 m_hash_value = hash_value(m_clip_rect);
330 ASSERT(m_key);
331 for( unsigned i=0;i<m_key->GetCount();i++)
333 m_hash_value += (m_hash_value<<5);
334 m_hash_value += m_key->GetAt(i)->GetHashValue();
336 return m_hash_value;
339 bool GroupedDrawItemsHashKey::operator==( const GroupedDrawItemsHashKey& key ) const
341 if (this==&key)
343 return true;
345 else if ( m_key->GetCount()!=key.m_key->GetCount() || m_clip_rect!=key.m_clip_rect)
347 return false;
349 else
351 for( unsigned i=0;i<m_key->GetCount();i++)
353 if( !(*m_key->GetAt(i) == *key.m_key->GetAt(i)) )
354 return false;
356 return true;
358 return true;
362 //////////////////////////////////////////////////////////////////////////////////////////////
364 // CacheManager
366 struct Caches
368 public:
369 Caches()
371 s_bitmap_cache = NULL;
372 s_clipper_alpha_mask_cache = NULL;
374 s_text_info_cache = NULL;
375 s_path_data_mru_cache = NULL;
376 s_scan_line_data_2_mru_cache = NULL;
377 s_overlay_no_blur_mru_cache = NULL;
378 s_overlay_mru_cache = NULL;
380 s_scan_line_data_mru_cache = NULL;
381 s_overlay_no_offset_mru_cache = NULL;
383 s_subpixel_variance_cache = NULL;
384 s_ass_tag_list_cache = NULL;
386 ~Caches()
388 delete s_bitmap_cache;
389 delete s_clipper_alpha_mask_cache;
391 delete s_text_info_cache;
392 delete s_path_data_mru_cache;
393 delete s_scan_line_data_2_mru_cache;
394 delete s_overlay_no_blur_mru_cache;
395 delete s_overlay_mru_cache;
397 delete s_scan_line_data_mru_cache;
398 delete s_overlay_no_offset_mru_cache;
400 delete s_subpixel_variance_cache;
401 delete s_ass_tag_list_cache;
403 public:
404 BitmapMruCache* s_bitmap_cache;
405 ClipperAlphaMaskMruCache* s_clipper_alpha_mask_cache;
407 TextInfoMruCache* s_text_info_cache;
408 AssTagListMruCache* s_ass_tag_list_cache;
410 ScanLineDataMruCache* s_scan_line_data_mru_cache;
411 OverlayNoOffsetMruCache* s_overlay_no_offset_mru_cache;
413 OverlayMruCache* s_subpixel_variance_cache;
414 OverlayMruCache* s_overlay_mru_cache;
415 OverlayNoBlurMruCache* s_overlay_no_blur_mru_cache;
416 PathDataMruCache* s_path_data_mru_cache;
417 ScanLineData2MruCache* s_scan_line_data_2_mru_cache;
420 static Caches s_caches;
422 OverlayMruCache* CacheManager::GetOverlayMruCache()
424 if(s_caches.s_overlay_mru_cache==NULL)
426 s_caches.s_overlay_mru_cache = new OverlayMruCache(OVERLAY_CACHE_ITEM_NUM);
428 return s_caches.s_overlay_mru_cache;
431 PathDataMruCache* CacheManager::GetPathDataMruCache()
433 if (s_caches.s_path_data_mru_cache==NULL)
435 s_caches.s_path_data_mru_cache = new PathDataMruCache(PATH_CACHE_ITEM_NUM);
437 return s_caches.s_path_data_mru_cache;
440 OverlayNoBlurMruCache* CacheManager::GetOverlayNoBlurMruCache()
442 if(s_caches.s_overlay_no_blur_mru_cache==NULL)
444 s_caches.s_overlay_no_blur_mru_cache = new OverlayNoBlurMruCache(OVERLAY_NO_BLUR_CACHE_ITEM_NUM);
446 return s_caches.s_overlay_no_blur_mru_cache;
449 ScanLineData2MruCache* CacheManager::GetScanLineData2MruCache()
451 if(s_caches.s_scan_line_data_2_mru_cache==NULL)
453 s_caches.s_scan_line_data_2_mru_cache = new ScanLineData2MruCache(SCAN_LINE_DATA_CACHE_ITEM_NUM);
455 return s_caches.s_scan_line_data_2_mru_cache;
458 OverlayMruCache* CacheManager::GetSubpixelVarianceCache()
460 if(s_caches.s_subpixel_variance_cache==NULL)
462 s_caches.s_subpixel_variance_cache = new OverlayMruCache(SUBPIXEL_VARIANCE_CACHE_ITEM_NUM);
464 return s_caches.s_subpixel_variance_cache;
467 ScanLineDataMruCache* CacheManager::GetScanLineDataMruCache()
469 if(s_caches.s_scan_line_data_mru_cache==NULL)
471 s_caches.s_scan_line_data_mru_cache = new ScanLineDataMruCache(SCAN_LINE_DATA_CACHE_ITEM_NUM);
473 return s_caches.s_scan_line_data_mru_cache;
476 OverlayNoOffsetMruCache* CacheManager::GetOverlayNoOffsetMruCache()
478 if(s_caches.s_overlay_no_offset_mru_cache==NULL)
480 s_caches.s_overlay_no_offset_mru_cache = new OverlayNoOffsetMruCache(OVERLAY_NO_BLUR_CACHE_ITEM_NUM);
482 return s_caches.s_overlay_no_offset_mru_cache;
485 AssTagListMruCache* CacheManager::GetAssTagListMruCache()
487 if(s_caches.s_ass_tag_list_cache==NULL)
489 s_caches.s_ass_tag_list_cache = new AssTagListMruCache(ASS_TAG_LIST_CACHE_ITEM_NUM);
491 return s_caches.s_ass_tag_list_cache;
494 TextInfoMruCache* CacheManager::GetTextInfoCache()
496 if(s_caches.s_text_info_cache==NULL)
498 s_caches.s_text_info_cache = new TextInfoMruCache(TEXT_INFO_CACHE_ITEM_NUM);
500 return s_caches.s_text_info_cache;
503 ClipperAlphaMaskMruCache* CacheManager::GetClipperAlphaMaskMruCache()
505 if(s_caches.s_clipper_alpha_mask_cache==NULL)
507 s_caches.s_clipper_alpha_mask_cache = new ClipperAlphaMaskMruCache(CLIPPER_MRU_CACHE_ITEM_NUM);
509 return s_caches.s_clipper_alpha_mask_cache;
512 BitmapMruCache* CacheManager::GetBitmapMruCache()
514 if (s_caches.s_bitmap_cache==NULL)
516 s_caches.s_bitmap_cache = new BitmapMruCache(BITMAP_MRU_CACHE_ITEM_NUM);
518 return s_caches.s_bitmap_cache;