Fix ScanLineData2CacheKey, OverlayNoBlurKey, OverlayKey, CWordPaintResultKey and...
[xy_vsfilter.git] / src / subtitles / cache_manager.cpp
blobdf5032e18f82941d0d47a45cfb4b5560f26800b2
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
134 this->m_style.get().borderStyle == key.m_style.get().borderStyle
135 && fabs(this->m_style.get().outlineWidthX - key.m_style.get().outlineWidthX) < 0.000001
136 && fabs(this->m_style.get().outlineWidthY - key.m_style.get().outlineWidthY) < 0.000001
137 && fabs(this->m_style.get().fontScaleX - key.m_style.get().fontScaleX) < 0.000001
138 && fabs(this->m_style.get().fontScaleY - key.m_style.get().fontScaleY) < 0.000001
139 && fabs(this->m_style.get().fontAngleX - key.m_style.get().fontAngleX) < 0.000001
140 && fabs(this->m_style.get().fontAngleY - key.m_style.get().fontAngleY) < 0.000001
141 && fabs(this->m_style.get().fontAngleZ - key.m_style.get().fontAngleZ) < 0.000001
142 && fabs(this->m_style.get().fontShiftX - key.m_style.get().fontShiftX) < 0.000001
143 && fabs(this->m_style.get().fontShiftY - key.m_style.get().fontShiftY) < 0.000001
144 && (m_org.x==key.m_org.x) && (m_org.y==key.m_org.y)
145 && PathDataCacheKey::operator==(key); //NOTE: static_cast will call copy constructer to construct a tmp obj
148 ULONG ScanLineData2CacheKey::UpdateHashValue()
150 const STSStyle& style = m_style.get();
151 m_hash_value = __super::UpdateHashValue();
152 m_hash_value += (m_hash_value<<5);
153 m_hash_value += m_org.x;
154 m_hash_value += (m_hash_value<<5);
155 m_hash_value += m_org.y;
156 m_hash_value += (m_hash_value<<5);
157 m_hash_value += style.borderStyle;
158 m_hash_value += (m_hash_value<<5);
159 m_hash_value += ((int)style.outlineWidthX<<16) + (int)style.outlineWidthY;
160 m_hash_value += (m_hash_value<<5);
161 m_hash_value += ((int)style.fontScaleX<<16) + (int)style.fontScaleY;
162 m_hash_value += (m_hash_value<<5);
163 m_hash_value += ((int)style.fontAngleX<<20) + ((int)style.fontAngleY<<10) + ((int)style.fontAngleZ);
164 m_hash_value += (m_hash_value<<5);
165 m_hash_value += ((int)style.fontShiftX<<16) + (int)style.fontShiftY;
167 return m_hash_value;
170 //////////////////////////////////////////////////////////////////////////////////////////////
172 // OverlayNoBlurKey
174 bool OverlayNoBlurKey::operator==( const OverlayNoBlurKey& key ) const
176 //static_cast will call copy constructer to construct a tmp obj
177 //return (static_cast<ScanLineDataCacheKey>(*this)==static_cast<ScanLineDataCacheKey>(key))
178 // && (m_p.x==key.m_p.x) && (m_p.y==key.m_p.y);
179 return (m_p.x==key.m_p.x) && (m_p.y==key.m_p.y) && ScanLineData2CacheKey::operator==(key);
182 ULONG OverlayNoBlurKey::UpdateHashValue()
184 m_hash_value = __super::UpdateHashValue();
185 m_hash_value += (m_hash_value<<5);
186 m_hash_value += m_p.x;
187 m_hash_value += (m_hash_value<<5);
188 m_hash_value += m_p.y;
189 return m_hash_value;
193 //////////////////////////////////////////////////////////////////////////////////////////////
195 // OverlayKey
197 bool OverlayKey::operator==( const OverlayKey& key ) const
199 return fabs(this->m_style.get().fGaussianBlur - key.m_style.get().fGaussianBlur) < 0.000001
200 && this->m_style.get().fBlur == key.m_style.get().fBlur
201 && OverlayNoBlurKey::operator==(key);
202 //static_cast will call copy constructer to construct a tmp obj
203 //return ((CWordCacheKey)(*this)==(CWordCacheKey)key) && (m_p.x==key.m_p.x) && (m_p.y==key.m_p.y)
204 // && (m_org.x==key.m_org.x) && (m_org.y==key.m_org.y);
207 ULONG OverlayKey::UpdateHashValue()
209 m_hash_value = __super::UpdateHashValue();
210 m_hash_value += (m_hash_value<<5);
211 m_hash_value += m_style.get().fBlur;
212 m_hash_value += (m_hash_value<<5);
213 m_hash_value += hash_value(m_style.get().fGaussianBlur);
214 return m_hash_value;
218 //////////////////////////////////////////////////////////////////////////////////////////////
220 // ScanLineDataCacheKey
222 bool ScanLineDataCacheKey::operator==( const ScanLineDataCacheKey& key ) const
224 return (m_path_data && key.m_path_data) ? *m_path_data==*key.m_path_data : m_path_data==key.m_path_data;
227 ULONG ScanLineDataCacheKey::UpdateHashValue()
229 m_hash_value = PathDataTraits::Hash(*m_path_data);
230 return m_hash_value;
233 //////////////////////////////////////////////////////////////////////////////////////////////
235 // OverlayNoOffsetKey
237 bool OverlayNoOffsetKey::operator==( const OverlayNoOffsetKey& key ) const
239 return (this==&key) || ( this->m_border == key.m_border && this->m_rasterize_sub == key.m_rasterize_sub &&
240 ScanLineDataCacheKey::operator==(key) );
243 ULONG OverlayNoOffsetKey::UpdateHashValue()
245 m_hash_value = __super::UpdateHashValue();
246 m_hash_value += (m_hash_value<<5);
247 m_hash_value += m_border;
248 m_hash_value += (m_hash_value<<5);
249 m_hash_value += m_rasterize_sub;
250 return m_hash_value;
254 //////////////////////////////////////////////////////////////////////////////////////////////
256 // ClipperAlphaMaskCacheKey
258 bool ClipperAlphaMaskCacheKey::operator==( const ClipperAlphaMaskCacheKey& key ) const
260 bool result = false;
261 if (m_clipper==key.m_clipper)
263 result = true;
265 else if ( m_clipper!=NULL && key.m_clipper!=NULL )
267 const CClipper& lhs = *m_clipper;
268 const CClipper& rhs = *key.m_clipper;
269 result = (lhs.m_polygon->m_str == rhs.m_polygon->m_str
270 && fabs(lhs.m_polygon->m_scalex - rhs.m_polygon->m_scalex) < 0.000001
271 && fabs(lhs.m_polygon->m_scaley - rhs.m_polygon->m_scaley) < 0.000001
272 && lhs.m_size == rhs.m_size
273 && lhs.m_inverse == rhs.m_inverse
274 && lhs.m_effectType == rhs.m_effectType
275 && lhs.m_effect == rhs.m_effect);//fix me: unsafe code
277 return result;
280 ULONG ClipperAlphaMaskCacheKey::UpdateHashValue()
282 if(m_clipper)
283 m_hash_value = ClipperTraits::Hash(*m_clipper);
284 else
285 m_hash_value = 0;
286 return m_hash_value;
289 //////////////////////////////////////////////////////////////////////////////////////////////
291 // DrawItemHashKey
293 ULONG DrawItemHashKey::UpdateHashValue()
295 m_hash_value = m_overlay_key->GetHashValue();
296 m_hash_value += (m_hash_value<<5);
297 m_hash_value += m_clipper_key.GetHashValue();
298 m_hash_value += (m_hash_value<<5);
299 m_hash_value += hash_value(m_clip_rect);
300 m_hash_value += (m_hash_value<<5);
301 m_hash_value += m_fBorder;
302 m_hash_value += (m_hash_value<<5);
303 m_hash_value += m_fBody;
304 m_hash_value += (m_hash_value<<5);
305 m_hash_value += (m_xsub<<16) + m_ysub;
306 m_hash_value += (m_hash_value<<5);
307 m_hash_value += m_switchpts[0];//fix me: other m_switchpts elements?
308 return m_hash_value;
311 DrawItemHashKey::DrawItemHashKey( const DrawItem& draw_item)
312 : m_overlay_key( draw_item.overlay_paint_machine->GetHashKey() )
313 , m_clipper_key( draw_item.clipper->GetHashKey() )
314 , m_clip_rect(draw_item.clip_rect)
315 , m_xsub(draw_item.xsub)
316 , m_ysub(draw_item.ysub)
317 , m_fBody(draw_item.fBody)
318 , m_fBorder(draw_item.fBorder)
320 for(int i=0;i<countof(m_switchpts);i++)
321 m_switchpts[i] = draw_item.switchpts[i];
324 bool DrawItemHashKey::operator==( const DrawItemHashKey& key ) const
326 return (this==&key) || (
327 (m_clip_rect == key.m_clip_rect)==TRUE &&
328 m_xsub == key.m_xsub &&
329 m_ysub == key.m_ysub &&
330 m_fBody == key.m_fBody &&
331 m_fBorder == key.m_fBorder &&
332 !memcmp(m_switchpts, key.m_switchpts, sizeof(m_switchpts)) &&
333 *m_overlay_key==*key.m_overlay_key &&
334 m_clipper_key==key.m_clipper_key);
337 //////////////////////////////////////////////////////////////////////////////////////////////
339 // GroupedDrawItemsHashKey
341 ULONG GroupedDrawItemsHashKey::UpdateHashValue()
343 m_hash_value = hash_value(m_clip_rect);
344 ASSERT(m_key);
345 for( unsigned i=0;i<m_key->GetCount();i++)
347 m_hash_value += (m_hash_value<<5);
348 m_hash_value += m_key->GetAt(i)->GetHashValue();
350 return m_hash_value;
353 bool GroupedDrawItemsHashKey::operator==( const GroupedDrawItemsHashKey& key ) const
355 if (this==&key)
357 return true;
359 else if ( m_key->GetCount()!=key.m_key->GetCount() || m_clip_rect!=key.m_clip_rect)
361 return false;
363 else
365 for( unsigned i=0;i<m_key->GetCount();i++)
367 if( !(*m_key->GetAt(i) == *key.m_key->GetAt(i)) )
368 return false;
370 return true;
372 return true;
376 //////////////////////////////////////////////////////////////////////////////////////////////
378 // CacheManager
380 struct Caches
382 public:
383 Caches()
385 s_bitmap_cache = NULL;
386 s_clipper_alpha_mask_cache = NULL;
388 s_text_info_cache = NULL;
389 s_path_data_mru_cache = NULL;
390 s_scan_line_data_2_mru_cache = NULL;
391 s_overlay_no_blur_mru_cache = NULL;
392 s_overlay_mru_cache = NULL;
394 s_scan_line_data_mru_cache = NULL;
395 s_overlay_no_offset_mru_cache = NULL;
397 s_subpixel_variance_cache = NULL;
398 s_ass_tag_list_cache = NULL;
400 ~Caches()
402 delete s_bitmap_cache;
403 delete s_clipper_alpha_mask_cache;
405 delete s_text_info_cache;
406 delete s_path_data_mru_cache;
407 delete s_scan_line_data_2_mru_cache;
408 delete s_overlay_no_blur_mru_cache;
409 delete s_overlay_mru_cache;
411 delete s_scan_line_data_mru_cache;
412 delete s_overlay_no_offset_mru_cache;
414 delete s_subpixel_variance_cache;
415 delete s_ass_tag_list_cache;
417 public:
418 BitmapMruCache* s_bitmap_cache;
419 ClipperAlphaMaskMruCache* s_clipper_alpha_mask_cache;
421 TextInfoMruCache* s_text_info_cache;
422 AssTagListMruCache* s_ass_tag_list_cache;
424 ScanLineDataMruCache* s_scan_line_data_mru_cache;
425 OverlayNoOffsetMruCache* s_overlay_no_offset_mru_cache;
427 OverlayMruCache* s_subpixel_variance_cache;
428 OverlayMruCache* s_overlay_mru_cache;
429 OverlayNoBlurMruCache* s_overlay_no_blur_mru_cache;
430 PathDataMruCache* s_path_data_mru_cache;
431 ScanLineData2MruCache* s_scan_line_data_2_mru_cache;
434 static Caches s_caches;
436 OverlayMruCache* CacheManager::GetOverlayMruCache()
438 if(s_caches.s_overlay_mru_cache==NULL)
440 s_caches.s_overlay_mru_cache = new OverlayMruCache(OVERLAY_CACHE_ITEM_NUM);
442 return s_caches.s_overlay_mru_cache;
445 PathDataMruCache* CacheManager::GetPathDataMruCache()
447 if (s_caches.s_path_data_mru_cache==NULL)
449 s_caches.s_path_data_mru_cache = new PathDataMruCache(PATH_CACHE_ITEM_NUM);
451 return s_caches.s_path_data_mru_cache;
454 OverlayNoBlurMruCache* CacheManager::GetOverlayNoBlurMruCache()
456 if(s_caches.s_overlay_no_blur_mru_cache==NULL)
458 s_caches.s_overlay_no_blur_mru_cache = new OverlayNoBlurMruCache(OVERLAY_NO_BLUR_CACHE_ITEM_NUM);
460 return s_caches.s_overlay_no_blur_mru_cache;
463 ScanLineData2MruCache* CacheManager::GetScanLineData2MruCache()
465 if(s_caches.s_scan_line_data_2_mru_cache==NULL)
467 s_caches.s_scan_line_data_2_mru_cache = new ScanLineData2MruCache(SCAN_LINE_DATA_CACHE_ITEM_NUM);
469 return s_caches.s_scan_line_data_2_mru_cache;
472 OverlayMruCache* CacheManager::GetSubpixelVarianceCache()
474 if(s_caches.s_subpixel_variance_cache==NULL)
476 s_caches.s_subpixel_variance_cache = new OverlayMruCache(SUBPIXEL_VARIANCE_CACHE_ITEM_NUM);
478 return s_caches.s_subpixel_variance_cache;
481 ScanLineDataMruCache* CacheManager::GetScanLineDataMruCache()
483 if(s_caches.s_scan_line_data_mru_cache==NULL)
485 s_caches.s_scan_line_data_mru_cache = new ScanLineDataMruCache(SCAN_LINE_DATA_CACHE_ITEM_NUM);
487 return s_caches.s_scan_line_data_mru_cache;
490 OverlayNoOffsetMruCache* CacheManager::GetOverlayNoOffsetMruCache()
492 if(s_caches.s_overlay_no_offset_mru_cache==NULL)
494 s_caches.s_overlay_no_offset_mru_cache = new OverlayNoOffsetMruCache(OVERLAY_NO_BLUR_CACHE_ITEM_NUM);
496 return s_caches.s_overlay_no_offset_mru_cache;
499 AssTagListMruCache* CacheManager::GetAssTagListMruCache()
501 if(s_caches.s_ass_tag_list_cache==NULL)
503 s_caches.s_ass_tag_list_cache = new AssTagListMruCache(ASS_TAG_LIST_CACHE_ITEM_NUM);
505 return s_caches.s_ass_tag_list_cache;
508 TextInfoMruCache* CacheManager::GetTextInfoCache()
510 if(s_caches.s_text_info_cache==NULL)
512 s_caches.s_text_info_cache = new TextInfoMruCache(TEXT_INFO_CACHE_ITEM_NUM);
514 return s_caches.s_text_info_cache;
517 ClipperAlphaMaskMruCache* CacheManager::GetClipperAlphaMaskMruCache()
519 if(s_caches.s_clipper_alpha_mask_cache==NULL)
521 s_caches.s_clipper_alpha_mask_cache = new ClipperAlphaMaskMruCache(CLIPPER_MRU_CACHE_ITEM_NUM);
523 return s_caches.s_clipper_alpha_mask_cache;
526 BitmapMruCache* CacheManager::GetBitmapMruCache()
528 if (s_caches.s_bitmap_cache==NULL)
530 s_caches.s_bitmap_cache = new BitmapMruCache(BITMAP_MRU_CACHE_ITEM_NUM);
532 return s_caches.s_bitmap_cache;