1 /*
2 * Copyright (c) 2019-2020, Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22 
23 //!
24 //! \file     vp_obj_factories.cpp
25 //! \brief    Factories for vp object creation.
26 //!
27 #include "vp_obj_factories.h"
28 #include "sw_filter_handle.h"
29 using namespace vp;
30 
31 /****************************************************************************************************/
32 /*                                      HwFilterPipeFactory                                         */
33 /****************************************************************************************************/
34 
HwFilterPipeFactory(VpInterface & vpInterface)35 HwFilterPipeFactory::HwFilterPipeFactory(VpInterface &vpInterface) : m_allocator(vpInterface)
36 {
37 }
38 
~HwFilterPipeFactory()39 HwFilterPipeFactory::~HwFilterPipeFactory()
40 {
41 }
42 
Create(SwFilterPipe & swfilterPipe,Policy & policy,HwFilterPipe * & pHwFilterPipe)43 MOS_STATUS HwFilterPipeFactory::Create(SwFilterPipe &swfilterPipe,
44     Policy &policy, HwFilterPipe *&pHwFilterPipe)
45 {
46     VP_FUNC_CALL();
47 
48     pHwFilterPipe = m_allocator.Create();
49 
50     VP_PUBLIC_CHK_NULL_RETURN(pHwFilterPipe);
51     MOS_STATUS status = pHwFilterPipe->Initialize(swfilterPipe, policy);
52     if (MOS_FAILED(status))
53     {
54         Destory(pHwFilterPipe);
55     }
56     return status;
57 }
58 
Destory(HwFilterPipe * & pHwfilterPipe)59 MOS_STATUS HwFilterPipeFactory::Destory(HwFilterPipe *&pHwfilterPipe)
60 {
61     VP_FUNC_CALL();
62 
63     return m_allocator.Destory(pHwfilterPipe);
64 }
65 
66 /****************************************************************************************************/
67 /*                                      HwFilterFactory                                             */
68 /****************************************************************************************************/
69 
HwFilterFactory(VpInterface & vpInterface)70 HwFilterFactory::HwFilterFactory(VpInterface &vpInterface) : m_allocatorVebox(vpInterface), m_allocatorVeboxSfc(vpInterface), m_allocatorRender(vpInterface)
71 {
72 }
73 
~HwFilterFactory()74 HwFilterFactory::~HwFilterFactory()
75 {
76 }
77 
Create(HW_FILTER_PARAMS & param)78 HwFilter *HwFilterFactory::Create(HW_FILTER_PARAMS &param)
79 {
80     VP_FUNC_CALL();
81 
82     HwFilter *p = nullptr;
83     switch (param.Type)
84     {
85     case EngineTypeVebox:
86         p = m_allocatorVebox.Create();
87         break;
88     case EngineTypeVeboxSfc:
89         p = m_allocatorVeboxSfc.Create();
90         break;
91     case EngineTypeRender:
92         p = m_allocatorRender.Create();
93         break;
94     default:
95         return nullptr;
96         break;
97     }
98     if (p)
99     {
100         if (MOS_FAILED(p->Initialize(param)))
101         {
102             Destory(p);
103             return nullptr;
104         }
105     }
106     return p;
107 }
108 
Destory(HwFilter * & pHwFilter)109 void HwFilterFactory::Destory(HwFilter *&pHwFilter)
110 {
111     VP_FUNC_CALL();
112 
113     if (pHwFilter)
114     {
115         switch (pHwFilter->GetEngineType())
116         {
117         case EngineTypeVebox:
118         {
119             HwFilterVebox *p = dynamic_cast<HwFilterVebox*>(pHwFilter);
120             if (p)
121             {
122                 m_allocatorVebox.Destory(p);
123                 pHwFilter = nullptr;
124             }
125             else
126             {
127                 VP_PUBLIC_ASSERTMESSAGE("Invalid engine type for hwFilter!");
128                 MOS_Delete(pHwFilter);
129             }
130             break;
131         }
132         case EngineTypeVeboxSfc:
133         {
134             HwFilterVeboxSfc *p = dynamic_cast<HwFilterVeboxSfc*>(pHwFilter);
135             if (p)
136             {
137                 m_allocatorVeboxSfc.Destory(p);
138                 pHwFilter = nullptr;
139             }
140             else
141             {
142                 VP_PUBLIC_ASSERTMESSAGE("Invalid engine type for hwFilter!");
143                 MOS_Delete(pHwFilter);
144             }
145             break;
146         }
147         case EngineTypeRender:
148         {
149             HwFilterRender *p = dynamic_cast<HwFilterRender*>(pHwFilter);
150             if (p)
151             {
152                 m_allocatorRender.Destory(p);
153                 pHwFilter = nullptr;
154             }
155             else
156             {
157                 VP_PUBLIC_ASSERTMESSAGE("Invalid engine type for hwFilter!");
158                 MOS_Delete(pHwFilter);
159             }
160             break;
161         }
162         default:
163             MOS_Delete(pHwFilter);
164             return;
165         }
166         pHwFilter = nullptr;
167     }
168 }
169 
170 /****************************************************************************************************/
171 /*                                      SwFilterPipeFactory                                         */
172 /****************************************************************************************************/
173 
SwFilterPipeFactory(VpInterface & vpInterface)174 SwFilterPipeFactory::SwFilterPipeFactory(VpInterface &vpInterface) :
175     m_allocator(vpInterface),
176     m_vpInterface(vpInterface)
177 {
178 }
179 
~SwFilterPipeFactory()180 SwFilterPipeFactory::~SwFilterPipeFactory()
181 {
182 }
183 
GetPipeCountForProcessing(VP_PIPELINE_PARAMS & params)184 int SwFilterPipeFactory::GetPipeCountForProcessing(VP_PIPELINE_PARAMS &params)
185 {
186     VP_FUNC_CALL();
187 
188     int pipeCnt = 1;
189     int featureCnt = 0;
190     auto featureHander = *m_vpInterface.GetSwFilterHandlerMap();
191     for (auto &handler : featureHander)
192     {
193         int cnt = handler.second->GetPipeCountForProcessing(params);
194         if (cnt > 1)
195         {
196             pipeCnt = cnt;
197             featureCnt++;
198         }
199     }
200     if (featureCnt > 1)
201     {
202         VP_PUBLIC_ASSERTMESSAGE("Invalid usage, only support 1 feature for multi-pipe.");
203         return 0;
204     }
205     return pipeCnt;
206 }
207 
Update(VP_PIPELINE_PARAMS & params,int index)208 MOS_STATUS SwFilterPipeFactory::Update(VP_PIPELINE_PARAMS &params, int index)
209 {
210     VP_FUNC_CALL();
211 
212     auto featureHander = *m_vpInterface.GetSwFilterHandlerMap();
213     for (auto &handler : featureHander)
214     {
215         VP_PUBLIC_CHK_STATUS_RETURN(handler.second->UpdateParamsForProcessing(params, index));
216     }
217     return MOS_STATUS_SUCCESS;
218 }
219 
Create(PVP_PIPELINE_PARAMS params,std::vector<SwFilterPipe * > & swFilterPipe,VpPipelineParamFactory * pipelineParamFactory)220 MOS_STATUS SwFilterPipeFactory::Create(PVP_PIPELINE_PARAMS params, std::vector<SwFilterPipe *> &swFilterPipe, VpPipelineParamFactory *pipelineParamFactory)
221 {
222     VP_FUNC_CALL();
223 
224     VP_PUBLIC_CHK_NULL_RETURN(params);
225     VP_PUBLIC_CHK_NULL_RETURN(pipelineParamFactory);
226     int pipeCnt = GetPipeCountForProcessing(*params);
227     if (pipeCnt == 0)
228     {
229         return MOS_STATUS_INVALID_PARAMETER;
230     }
231 
232     MT_LOG1(MT_VP_HAL_SWWFILTER, MT_NORMAL, MT_VP_HAL_PIPE_CNT, pipeCnt);
233 
234     for (int index = 0; index < pipeCnt; index++)
235     {
236         VP_PIPELINE_PARAMS *tempParams = pipelineParamFactory->Clone(params);
237         VP_PUBLIC_CHK_NULL_RETURN(tempParams);
238         VP_PUBLIC_CHK_STATUS_RETURN(Update(*tempParams, index));
239 
240         SwFilterPipe *pipe = m_allocator.Create();
241         if (!pipe)
242         {
243             pipelineParamFactory->Destroy(tempParams);
244             VP_PUBLIC_CHK_STATUS_RETURN(MOS_STATUS_NULL_POINTER);
245         }
246 
247         FeatureRule featureRule;
248         MOS_STATUS status = pipe->Initialize(*tempParams, featureRule);
249 
250         VP_PUBLIC_CHK_STATUS_RETURN(pipelineParamFactory->Destroy(tempParams));
251 
252         if (MOS_FAILED(status))
253         {
254             m_allocator.Destory(pipe);
255             return status;
256         }
257 
258         swFilterPipe.push_back(pipe);
259     }
260 
261     return MOS_STATUS_SUCCESS;
262 }
263 
Create(VEBOX_SFC_PARAMS * params,std::vector<SwFilterPipe * > & swFilterPipe)264 MOS_STATUS SwFilterPipeFactory::Create(VEBOX_SFC_PARAMS *params, std::vector<SwFilterPipe*> &swFilterPipe)
265 {
266     VP_FUNC_CALL();
267 
268     VP_PUBLIC_CHK_NULL_RETURN(params);
269     SwFilterPipe *pipe = m_allocator.Create();
270     VP_PUBLIC_CHK_NULL_RETURN(pipe);
271 
272     MOS_STATUS status = pipe->Initialize(*params);
273 
274     if (MOS_FAILED(status))
275     {
276         m_allocator.Destory(pipe);
277         return status;
278     }
279 
280     swFilterPipe.push_back(pipe);
281     return status;
282 }
283 
Create(SwFilterPipe * & swFilterPipe)284 MOS_STATUS SwFilterPipeFactory::Create(SwFilterPipe *&swFilterPipe)
285 {
286     VP_FUNC_CALL();
287 
288     swFilterPipe = m_allocator.Create();
289     VP_PUBLIC_CHK_NULL_RETURN(swFilterPipe);
290 
291     return MOS_STATUS_SUCCESS;
292 }
293 
Destory(SwFilterPipe * & swFilterPipe)294 void SwFilterPipeFactory::Destory(SwFilterPipe *&swFilterPipe)
295 {
296     VP_FUNC_CALL();
297 
298     m_allocator.Destory(swFilterPipe);
299 }
300