Debian Codegraph @

media_driver/linux/common/media_interfaces/media_interfaces.cpp

raw
1 /*
2 * Copyright (c) 2017-2022, 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 //! \file media_interfaces.cpp
24 //! \brief Helps with gen-specific factory creation.
25 //!
26
27 #include "media_interfaces_mhw.h"
28 #include "media_interfaces_codechal.h"
29 #include "media_interfaces_mmd.h"
30 #include "media_interfaces_mcpy.h"
31 #include "media_interfaces_cmhal.h"
32 #include "media_interfaces_vphal.h"
33 #include "media_interfaces_nv12top010.h"
34 #include "media_interfaces_decode_histogram.h"
35 #include "media_interfaces_hwinfo_device.h"
36
37 #include "mhw_cp_interface.h"
38 #include "mhw_mi.h"
39 #include "mhw_render_legacy.h"
40 #include "mhw_sfc.h"
41 #include "mhw_state_heap.h"
42 #include "mhw_vebox.h"
43 #include "mhw_vdbox_mfx_interface.h"
44 #include "mhw_vdbox_hcp_interface.h"
45 #include "mhw_vdbox_huc_interface.h"
46 #include "mhw_vdbox_vdenc_interface.h"
47 #include "mhw_blt_legacy.h"
48 #include "vp_pipeline.h"
49 #include "vp_platform_interface.h"
50
51 template class MediaFactory<uint32_t, MhwInterfaces>;
52 template class MediaFactory<uint32_t, MmdDevice>;
53 template class MediaFactory<uint32_t, McpyDevice>;
54 template class MediaFactory<uint32_t, CodechalDevice>;
55 template class MediaFactory<uint32_t, CMHalDevice>;
56 template class MediaFactory<uint32_t, VphalDevice>;
57 template class MediaFactory<uint32_t, Nv12ToP010Device>;
58 template class MediaFactory<uint32_t, DecodeHistogramDevice>;
59 template class MediaFactory<uint32_t, MediaInterfacesHwInfoDevice>;
60
61 typedef MediaFactory<uint32_t, MhwInterfaces> MhwFactory;
62 typedef MediaFactory<uint32_t, MmdDevice> MmdFactory;
63 typedef MediaFactory<uint32_t, McpyDevice> McpyFactory;
64 typedef MediaFactory<uint32_t, CodechalDevice> CodechalFactory;
65 typedef MediaFactory<uint32_t, CMHalDevice> CMHalFactory;
66 typedef MediaFactory<uint32_t, VphalDevice> VphalFactory;
67 typedef MediaFactory<uint32_t, Nv12ToP010Device> Nv12ToP010Factory;
68 typedef MediaFactory<uint32_t, DecodeHistogramDevice> DecodeHistogramFactory;
69 typedef MediaFactory<uint32_t, MediaInterfacesHwInfoDevice> HwInfoFactory;
70
71 VpBase* VphalDevice::CreateFactory(
72 PMOS_INTERFACE osInterface,
73 PMOS_CONTEXT osDriverContext,
74 MOS_STATUS *eStatus)
75 {
76 VpBase *vphalState = nullptr;
77 VphalDevice *vphalDevice = nullptr;
78 PLATFORM platform;
79
80 if (eStatus == nullptr)
81 {
82 VP_DEBUG_ASSERTMESSAGE("Invalid null pointer.");
83 return nullptr;
84 }
85
86 // pOsInterface not provided - use default for the current OS
87 if (osInterface == nullptr)
88 {
89 osInterface = (PMOS_INTERFACE)MOS_AllocAndZeroMemory(sizeof(MOS_INTERFACE));
90
91 if (osInterface == nullptr)
92 {
93 VP_DEBUG_ASSERTMESSAGE("Allocate OS interface failed");
94 *eStatus = MOS_STATUS_NO_SPACE;
95 return nullptr;
96 }
97
98 if (MOS_STATUS_SUCCESS != Mos_InitInterface(osInterface, osDriverContext, COMPONENT_VPCommon))
99 {
100 VP_DEBUG_ASSERTMESSAGE("Initailze OS interface failed");
101 MOS_FreeMemAndSetNull(osInterface);
102 *eStatus = MOS_STATUS_NO_SPACE;
103 return nullptr;
104 }
105
106 osInterface->bDeallocateOnExit = true;
107 }
108 else
109 // pOsInterface provided - use OS interface functions provided by DDI (OS emulation)
110 {
111 // Copy OS interface structure, save context
112 osInterface->pOsContext = (PMOS_CONTEXT)osDriverContext;
113 osInterface->bDeallocateOnExit = false;
114 }
115
116 // Initialize platform
117 osInterface->pfnGetPlatform(osInterface, &platform);
118
119 vphalDevice = VphalFactory::Create(platform.eProductFamily);
120
121 if (vphalDevice == nullptr)
122 {
123 VP_DEBUG_ASSERTMESSAGE("Failed to create MediaInterface on the given platform!");
124 if (osInterface->bDeallocateOnExit)
125 {
126 MOS_FreeMemAndSetNull(osInterface);
127 }
128 *eStatus = MOS_STATUS_NO_SPACE;
129 return nullptr;
130 }
131
132 if (vphalDevice->Initialize(osInterface, true, eStatus) != MOS_STATUS_SUCCESS)
133 {
134 VP_DEBUG_ASSERTMESSAGE("VPHal interfaces were not successfully allocated!");
135
136 // If m_vpBase has been created, osInterface should be released in VphalState::~VphalState.
137 if (osInterface->bDeallocateOnExit && nullptr == vphalDevice->m_vpBase)
138 {
139 // Deallocate OS interface structure (except if externally provided)
140 if (osInterface->pfnDestroy)
141 {
142 osInterface->pfnDestroy(osInterface, true);
143 }
144 MOS_FreeMemAndSetNull(osInterface);
145 }
146 vphalDevice->Destroy();
147 MOS_Delete(vphalDevice);
148 *eStatus = MOS_STATUS_NO_SPACE;
149 return nullptr;
150 }
151
152 vphalState = vphalDevice->m_vpBase;
153 MOS_Delete(vphalDevice);
154
155 return vphalState;
156 }
157
158 MhwInterfaces* MhwInterfaces::CreateFactory(
159 CreateParams params,
160 PMOS_INTERFACE osInterface)
161 {
162 if (osInterface == nullptr)
163 {
164 return nullptr;
165 }
166 PLATFORM platform = {};
167 osInterface->pfnGetPlatform(osInterface, &platform);
168 MhwInterfaces *mhw =nullptr;
169
170 mhw = MhwFactory::Create(platform.eProductFamily + MEDIA_EXT_FLAG);
171 if(mhw == nullptr)
172 {
173 mhw = MhwFactory::Create(platform.eProductFamily);
174 }
175
176 if (mhw == nullptr)
177 {
178 MHW_ASSERTMESSAGE("Failed to create MediaInterface on the given platform!");
179 return nullptr;
180 }
181
182 if (mhw->Initialize(params, osInterface) != MOS_STATUS_SUCCESS)
183 {
184 MHW_ASSERTMESSAGE("MHW interfaces were not successfully allocated!");
185 return nullptr;
186 }
187
188 return mhw;
189 }
190
191 void MhwInterfaces::Destroy()
192 {
193 if(m_isDestroyed)
194 {
195 return;
196 }
197 if (m_osInterface)
198 {
199 m_osInterface->pfnDeleteMhwCpInterface(m_cpInterface);
200 m_cpInterface = nullptr;
201 }
202 else
203 {
204 MHW_ASSERTMESSAGE("Failed to destroy cpInterface.");
205 }
206 MOS_Delete(m_miInterface);
207 MOS_Delete(m_renderInterface);
208 MOS_Delete(m_sfcInterface);
209 MOS_Delete(m_stateHeapInterface);
210 MOS_Delete(m_veboxInterface);
211 MOS_Delete(m_mfxInterface);
212 MOS_Delete(m_hcpInterface);
213 MOS_Delete(m_hucInterface);
214 MOS_Delete(m_vdencInterface);
215 MOS_Delete(m_bltInterface);
216 }
217
218 Codechal* CodechalDevice::CreateFactory(
219 PMOS_INTERFACE osInterface,
220 PMOS_CONTEXT osDriverContext,
221 void *standardInfo,
222 void *settings)
223 {
224 #define FAIL_DESTROY(msg) \
225 { \
226 CODECHAL_PUBLIC_ASSERTMESSAGE((msg)); \
227 if (mhwInterfaces != nullptr) \
228 { \
229 mhwInterfaces->Destroy(); \
230 } \
231 if (osInterface != nullptr && osInterface->bDeallocateOnExit) \
232 { \
233 if (osInterface->pfnDestroy != nullptr) \
234 { \
235 osInterface->pfnDestroy(osInterface, false); \
236 } \
237 MOS_FreeMemory(osInterface); \
238 } \
239 MOS_Delete(mhwInterfaces); \
240 MOS_Delete(device); \
241 return nullptr; \
242 }
243
244 #define FAIL_CHK_STATUS(stmt) \
245 { \
246 if ((stmt) != MOS_STATUS_SUCCESS) \
247 { \
248 FAIL_DESTROY("Status check failed, CodecHal creation failed!"); \
249 } \
250 }
251
252 #define FAIL_CHK_NULL(ptr) \
253 { \
254 if ((ptr) == nullptr) \
255 { \
256 FAIL_DESTROY("nullptr check failed, CodecHal creation failed!"); \
257 } \
258 }
259
260 if (osDriverContext == nullptr ||
261 standardInfo == nullptr)
262 {
263 return nullptr;
264 }
265
266 MhwInterfaces *mhwInterfaces = nullptr;
267 CodechalDevice *device = nullptr;
268
269 CODECHAL_FUNCTION CodecFunction = ((PCODECHAL_STANDARD_INFO)standardInfo)->CodecFunction;
270
271 // pOsInterface not provided - use default for the current OS
272 if (osInterface == nullptr)
273 {
274 osInterface =
275 (PMOS_INTERFACE)MOS_AllocAndZeroMemory(sizeof(MOS_INTERFACE));
276 FAIL_CHK_NULL(osInterface);
277
278 MOS_COMPONENT component = CodecHalIsDecode(CodecFunction) ? COMPONENT_Decode : COMPONENT_Encode;
279
280 osInterface->bDeallocateOnExit = true;
281 FAIL_CHK_STATUS(Mos_InitInterface(osInterface, osDriverContext, component));
282 }
283 // pOsInterface provided - use OS interface functions provided by DDI (OS emulation)
284 else
285 {
286 osInterface->bDeallocateOnExit = false;
287 }
288
289 MhwInterfaces::CreateParams params;
290 MOS_ZeroMemory(&params, sizeof(params));
291 params.Flags.m_render = true;
292 params.Flags.m_sfc = true;
293 params.Flags.m_vdboxAll = true;
294 params.Flags.m_vebox = true;
295 params.m_heapMode = (uint8_t)2;
296 params.m_isDecode = CodecHalIsDecode(CodecFunction);
297 mhwInterfaces = MhwInterfaces::CreateFactory(params, osInterface);
298 FAIL_CHK_NULL(mhwInterfaces);
299
300 PLATFORM platform = {};
301 osInterface->pfnGetPlatform(osInterface, &platform);
302 device = CodechalFactory::Create(platform.eProductFamily + MEDIA_EXT_FLAG);
303 if(device == nullptr)
304 {
305 device = CodechalFactory::Create(platform.eProductFamily);
306 }
307 FAIL_CHK_NULL(device);
308 FAIL_CHK_STATUS(device->Initialize(standardInfo, settings, mhwInterfaces, osInterface));
309 FAIL_CHK_NULL(device->m_codechalDevice);
310
311 Codechal *codechalDevice = device->m_codechalDevice;
312
313 MOS_Delete(mhwInterfaces);
314 MOS_Delete(device);
315
316 return codechalDevice;
317 }
318
319 void* MmdDevice::CreateFactory(
320 PMOS_CONTEXT osDriverContext)
321 {
322 #define MMD_FAILURE() \
323 { \
324 if (mhwInterfaces != nullptr) \
325 { \
326 mhwInterfaces->Destroy(); \
327 } \
328 MOS_Delete(mhwInterfaces); \
329 if (osInterface != nullptr) \
330 { \
331 if (osInterface->pfnDestroy) \
332 { \
333 osInterface->pfnDestroy(osInterface, false); \
334 } \
335 MOS_FreeMemory(osInterface); \
336 } \
337 MOS_Delete(device); \
338 return nullptr; \
339 }
340 MHW_FUNCTION_ENTER;
341
342 if (osDriverContext == nullptr)
343 {
344 MHW_ASSERTMESSAGE("Invalid(null) pOsDriverContext!");
345 return nullptr;
346 }
347
348 PMOS_INTERFACE osInterface = nullptr;
349 MhwInterfaces *mhwInterfaces = nullptr;
350 MmdDevice *device = nullptr;
351
352 osInterface = (PMOS_INTERFACE)MOS_AllocAndZeroMemory(sizeof(MOS_INTERFACE));
353 if (osInterface == nullptr)
354 {
355 return nullptr;
356 }
357 if (Mos_InitInterface(
358 osInterface,
359 osDriverContext,
360 COMPONENT_MEMDECOMP) != MOS_STATUS_SUCCESS)
361 {
362 MMD_FAILURE();
363 }
364
365 PLATFORM platform = {};
366 osInterface->pfnGetPlatform(osInterface, &platform);
367 device = MmdFactory::Create(platform.eProductFamily);
368 if (device == nullptr)
369 {
370 MMD_FAILURE();
371 }
372
373 mhwInterfaces = device->CreateMhwInterface(osInterface);
374 if (mhwInterfaces == nullptr)
375 {
376 MMD_FAILURE();
377 }
378
379 // transfer ownership of osinterface. No need to delete osinterface from this point.
380 device->Initialize(osInterface, mhwInterfaces);
381 if (device->m_mmdDevice == nullptr)
382 {
383 mhwInterfaces->Destroy();
384 // no need to delete osinterface becauses it's already deleted in device->Initialize
385 MOS_Delete(mhwInterfaces);
386 MOS_Delete(device);
387 return nullptr;
388 }
389
390 void *mmdDevice = device->m_mmdDevice;
391 MOS_Delete(mhwInterfaces);
392 MOS_Delete(device);
393
394 return mmdDevice;
395 }
396
397 MhwInterfaces* MmdDevice::CreateMhwInterface(PMOS_INTERFACE osInterface)
398 {
399 MhwInterfaces::CreateParams params;
400 MOS_ZeroMemory(&params, sizeof(params));
401 params.Flags.m_render = true;
402 params.m_heapMode = (uint8_t)2;
403 MhwInterfaces *mhw = MhwInterfaces::CreateFactory(params, osInterface);
404
405 return mhw;
406 }
407
408 void* McpyDevice::CreateFactory(
409 PMOS_CONTEXT osDriverContext)
410 {
411 MHW_FUNCTION_ENTER;
412
413 PMOS_INTERFACE osInterface = nullptr;
414 MhwInterfaces *mhwInterfaces = nullptr;
415 McpyDevice *device = nullptr;
416
417 auto deleterOnFailure = [&](bool deleteOsInterface){
418 if (deleteOsInterface && osInterface != nullptr)
419 {
420 if (osInterface->pfnDestroy)
421 {
422 osInterface->pfnDestroy(osInterface, false);
423 }
424 MOS_FreeMemory(osInterface);
425 }
426 MOS_Delete(device);
427 };
428
429 if (osDriverContext == nullptr)
430 {
431 MHW_ASSERTMESSAGE("Invalid(null) pOsDriverContext!");
432 return nullptr;
433 }
434
435 osInterface = (PMOS_INTERFACE)MOS_AllocAndZeroMemory(sizeof(MOS_INTERFACE));
436 if (osInterface == nullptr)
437 {
438 return nullptr;
439 }
440 if (Mos_InitInterface(
441 osInterface,
442 osDriverContext,
443 COMPONENT_MCPY) != MOS_STATUS_SUCCESS)
444 {
445 deleterOnFailure(true);
446 return nullptr;
447 }
448
449 PLATFORM platform = {};
450 osInterface->pfnGetPlatform(osInterface, &platform);
451 device = McpyFactory::Create(platform.eProductFamily);
452 if (device == nullptr)
453 {
454 deleterOnFailure(true);
455 return nullptr;
456 }
457
458 // transfer ownership of osInterface. No need to delete it if init fails
459 device->Initialize(osInterface);
460 if (device->m_mcpyDevice == nullptr)
461 {
462 deleterOnFailure(false);
463 return nullptr;
464 }
465
466 void *mcpyDevice = device->m_mcpyDevice;
467 MOS_Delete(device);
468
469 return mcpyDevice;
470 }
471
472 MhwInterfaces* McpyDevice::CreateMhwInterface(PMOS_INTERFACE osInterface)
473 {
474 MhwInterfaces::CreateParams params;
475 MOS_ZeroMemory(&params, sizeof(params));
476 params.Flags.m_render = true;
477 params.m_heapMode = (uint8_t)2;
478 MhwInterfaces *mhw = MhwInterfaces::CreateFactory(params, osInterface);
479
480 return mhw;
481 }
482
483 CodechalDecodeNV12ToP010* Nv12ToP010Device::CreateFactory(
484 PMOS_INTERFACE osInterface)
485 {
486 if (osInterface == nullptr)
487 {
488 MHW_ASSERTMESSAGE("Invalid(nullptr) osInterface!");
489 return nullptr;
490 }
491
492 PLATFORM platform = {};
493 osInterface->pfnGetPlatform(osInterface, &platform);
494 Nv12ToP010Device *device = nullptr;
495 CodechalDecodeNV12ToP010 *nv12ToP01Device = nullptr;
496 device = Nv12ToP010Factory::Create(platform.eProductFamily);
497 if (device != nullptr)
498 {
499 device->Initialize(osInterface);
500 nv12ToP01Device = device->m_nv12ToP010device;
501 }
502
503 MOS_Delete(device);
504
505 return nv12ToP01Device;
506 }
507
508 CM_HAL_GENERIC* CMHalDevice::CreateFactory(
509 CM_HAL_STATE *pCmState)
510 {
511 CMHalDevice *device = nullptr;
512 device = CMHalFactory::Create(pCmState->platform.eProductFamily);
513 if (device == nullptr)
514 {
515 return nullptr;
516 }
517 device->Initialize(pCmState);
518 if (device->m_cmhalDevice == nullptr)
519 {
520 MHW_ASSERTMESSAGE("CMHal device creation failed!");
521 MOS_Delete(device);
522 return nullptr;
523 }
524
525 CM_HAL_GENERIC *pRet = device->m_cmhalDevice;
526 MOS_Delete(device);
527 return pRet;
528 }
529
530 CodechalDecodeHistogram* DecodeHistogramDevice::CreateFactory(
531 CodechalHwInterface *hwInterface,
532 PMOS_INTERFACE osInterface)
533 {
534 if (hwInterface == nullptr || osInterface == nullptr)
535 {
536 MHW_ASSERTMESSAGE("Invalid(nullptr) hwInterface or osInterface!");
537 return nullptr;
538 }
539
540 PLATFORM platform = {};
541 osInterface->pfnGetPlatform(osInterface, &platform);
542 DecodeHistogramDevice *device = nullptr;
543 CodechalDecodeHistogram *decodeHistogramDevice = nullptr;
544 device = DecodeHistogramFactory::Create(platform.eProductFamily);
545 if (device != nullptr)
546 {
547 device->Initialize(hwInterface, osInterface);
548 decodeHistogramDevice = device->m_decodeHistogramDevice;
549 }
550
551 MOS_Delete(device);
552
553 return decodeHistogramDevice;
554 }
555
556 MediaInterfacesHwInfo* MediaInterfacesHwInfoDevice::CreateFactory(
557 PLATFORM platform, MEDIA_WA_TABLE *waTable)
558 {
559 MediaInterfacesHwInfoDevice *device = nullptr;
560 MediaInterfacesHwInfo *hwInfo = nullptr;
561
562 device = HwInfoFactory::Create(platform.eProductFamily);
563 if (device == nullptr)
564 {
565 return nullptr;
566 }
567 else
568 {
569 device->RefreshRevId(platform, waTable);
570 device->Initialize(platform);
571 }
572
573 hwInfo = MOS_New(MediaInterfacesHwInfo);
574 if (hwInfo == nullptr)
575 {
576 MHW_ASSERTMESSAGE("Invalid(nullptr) MediaInterfacesHwInfo!");
577 MOS_Delete(device);
578 return nullptr;
579 }
580
581 hwInfo->SetDeviceInfo(device->m_hwInfo.GetDeviceInfo());
582
583 MOS_Delete(device);
584
585 return hwInfo;
586 }