Support unrar64.dll
[xy_vsfilter.git] / src / subpic / PooledSubPic.cpp
blob85fecaa6df26ae14c1f466ca4ab9c90febfbc11f
1 #include "PooledSubPic.h"
2 #include "../subtitles/xy_malloc.h"
5 STDMETHODIMP_(void) CPooledSubPicAllocator::ReleaseItem( void* Item )
7 //DbgLog((LOG_TRACE, 3, "ReleaseItem::free:%d", _free.GetCount()));
8 CAutoLock lock(&_poolLock);
9 POSITION pos = _using.Find((CPooledSubPic*)Item);
10 if(pos!=NULL)
12 _using.RemoveAt(pos);
13 _free.AddTail((CPooledSubPic*)Item);
17 STDMETHODIMP_(void) CPooledSubPicAllocator::OnItemDestruct( void* Item )
19 //DbgLog((LOG_TRACE, 3, "OnItemDestruct::free:%d", _free.GetCount()));
20 CAutoLock lock(&_poolLock);
21 //CPooledSubPic* typedItem = (CPooledSubPic*)Item;
22 POSITION pos = _using.Find((CPooledSubPic*)Item);
23 if(pos!=NULL)
25 _using.RemoveAt(pos);
26 _capacity--;
28 ASSERT(_free.Find((CPooledSubPic*)Item)==NULL);
31 bool STDMETHODCALLTYPE CPooledSubPicAllocator::InitPool( int capacity )
33 DbgLog((LOG_TRACE, 3, "%s(%d), %s", __FILE__, __LINE__, __FUNCTION__));
34 CAutoLock lock(&_poolLock);
35 CPooledSubPic* temp;
37 if(capacity<0)
38 capacity = 0;
40 //while(capacity<_capacity)
41 while(_capacity>0)
43 if(!_free.IsEmpty())
45 temp = _free.RemoveTail();
46 _capacity--;
47 temp->_pool = NULL;
48 temp->Release();
50 else if(!_using.IsEmpty())
52 temp = _using.RemoveTail();
53 _capacity--;
54 temp->_pool = NULL;
55 temp->Release();
59 while(_capacity<capacity)
61 if(!(temp = DoAlloc()))
63 ASSERT(0);
64 return(false);
67 _free.AddTail(temp);
68 _capacity++;
69 temp->AddRef();
71 //DbgLog((LOG_TRACE, 3, "InitPool::free:%d", _free.GetCount()));
72 return true;
75 CPooledSubPicAllocator::CPooledSubPicAllocator( int alpha_blt_dst_type, SIZE maxsize, int capacity, int type/*=-1*/ )
76 :CSubPicExAllocatorImpl(maxsize, false, false)
77 , _alpha_blt_dst_type(alpha_blt_dst_type)
78 , _maxsize(maxsize)
79 , _type(type)
81 if(_type==-1)
83 switch(alpha_blt_dst_type)
85 case MSP_YUY2:
86 _type = MSP_XY_AUYV;
87 break;
88 case MSP_AYUV:
89 _type = MSP_AYUV;
90 break;
91 case MSP_IYUV:
92 case MSP_YV12:
93 case MSP_P010:
94 case MSP_P016:
95 case MSP_NV12:
96 case MSP_NV21:
97 _type = MSP_AYUV_PLANAR;
98 break;
99 default:
100 _type = MSP_RGBA;
101 break;
104 _capacity = 0;
105 InitPool(capacity);
109 bool CPooledSubPicAllocator::AllocEx( bool fStatic, ISubPicEx** ppSubPic )
111 if(!ppSubPic)
113 return(false);
116 CAutoLock lock(&_poolLock);
117 CollectUnUsedItem();
118 if(!_free.IsEmpty())
120 CPooledSubPic *item = _free.RemoveHead();
121 if(item->m_type!=_type)
123 item->_pool = NULL;
124 item->Release();
125 item = DoAlloc();
126 item->AddRef();
128 _using.AddTail(item);
129 *ppSubPic = item;
130 item->AddRef();
133 if(*ppSubPic!=NULL)
135 return true;
137 else
139 return false;
143 CPooledSubPicAllocator::~CPooledSubPicAllocator()
145 CPooledSubPic* item = NULL;
146 CAutoLock lock(&_poolLock);
147 for(POSITION pos = _free.GetHeadPosition(); pos!=NULL; )
149 item = _free.GetNext(pos);
150 if(item!=NULL)
152 item->_pool = NULL;
153 item->Release();
156 for(POSITION pos = _using.GetHeadPosition(); pos!=NULL; )
158 item = _using.GetNext(pos);
159 if(item!=NULL)
161 item->_pool = NULL;
162 item->Release();
167 STDMETHODIMP_(int) CPooledSubPicAllocator::SetSpdColorType( int color_type )
169 if(_type!=color_type)
171 m_pStatic = NULL;
172 _type = color_type;
174 return _type;
177 STDMETHODIMP_(bool) CPooledSubPicAllocator::IsSpdColorTypeSupported( int type )
179 if( (type==MSP_RGBA)
181 (type==MSP_XY_AUYV && _alpha_blt_dst_type == MSP_YUY2)
183 (type==MSP_AYUV && _alpha_blt_dst_type == MSP_AYUV)
185 (type==MSP_AYUV_PLANAR && (_alpha_blt_dst_type == MSP_IYUV ||
186 _alpha_blt_dst_type == MSP_YV12 ||
187 _alpha_blt_dst_type == MSP_P010 ||
188 _alpha_blt_dst_type == MSP_P016 ||
189 _alpha_blt_dst_type == MSP_NV12 ||
190 _alpha_blt_dst_type == MSP_NV21)) )
192 return true;
194 return false;
197 CPooledSubPic* CPooledSubPicAllocator::DoAlloc()
199 SubPicDesc spd;
200 spd.type = _type;
201 spd.w = _maxsize.cx;
202 spd.h = _maxsize.cy;
203 // spd.bpp = 32;
204 spd.bpp = (_type == MSP_AYUV_PLANAR) ? 8 : 32;
205 spd.pitch = (spd.w*spd.bpp)>>3;
207 // if(!(spd.bits = new BYTE[spd.pitch*spd.h]))
208 if(_type == MSP_AYUV_PLANAR)
210 spd.bits = xy_malloc(spd.pitch*spd.h*4);
212 else
214 spd.bits = xy_malloc(spd.pitch*spd.h);
216 if(!spd.bits)
218 ASSERT(0);
219 return(NULL);
221 CPooledSubPic* temp = NULL;
222 if(!(temp = DEBUG_NEW CPooledSubPic(spd, _alpha_blt_dst_type, this)))
224 xy_free(spd.bits);
225 ASSERT(0);
226 return(NULL);
228 return temp;
231 void CPooledSubPicAllocator::CollectUnUsedItem()
233 POSITION pos = _using.GetHeadPosition();
234 if(pos)
236 CPooledSubPic* item = _using.RemoveHead();
237 if (item->m_cRef==1)
239 _free.AddTail(item);
241 else
243 _using.AddTail(item);
248 CPooledSubPic::~CPooledSubPic()
250 if(_pool!=NULL)
251 _pool->OnItemDestruct(this);
252 xy_free(m_spd.bits);
253 m_spd.bits = NULL;