Debian Codegraph @

media_driver/linux/common/ddi/media_libva.cpp

raw
1 /*
2 * Copyright (c) 2009-2023, 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_libva.cpp
24 //! \brief libva(and its extension) interface implementaion.
25 //!
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <fcntl.h>
31 #include <sys/mman.h>
32 #include <sys/ioctl.h>
33 #include <unistd.h>
34
35 #if !defined(ANDROID) && defined(X11_FOUND)
36 #include <X11/Xutil.h>
37 #endif
38
39 #include <linux/fb.h>
40
41 #include "media_libva.h"
42
43 #include "media_libva_util.h"
44 #include "media_libva_decoder.h"
45 #include "media_libva_encoder.h"
46 #if !defined(ANDROID) && defined(X11_FOUND)
47 #include "media_libva_putsurface_linux.h"
48 #endif
49 #include "media_libva_vp.h"
50 #include "media_ddi_prot.h"
51
52 #include "mos_os.h"
53 #include "mos_context.h"
54 #include "mos_cmdbufmgr.h"
55 #include "mos_gpucontextmgr.h"
56
57 #include "hwinfo_linux.h"
58 #include "mediamemdecomp.h"
59 #include "mos_solo_generic.h"
60 #include "media_libva_caps.h"
61 #include "media_interfaces_mmd.h"
62 #include "media_interfaces_mcpy.h"
63 #include "media_user_settings_mgr.h"
64 #include "media_factory.h"
65 #include "mos_interface.h"
66 #include "drm_fourcc.h"
67 #include "media_libva_apo_decision.h"
68 #include "mos_oca_interface_specific.h"
69
70 #include "media_libva_interface.h"
71 #include "media_libva_interface_next.h"
72 #include "media_interfaces_hwinfo_device.h"
73 #include "media_libva_caps_next.h"
74
75 #define BO_BUSY_TIMEOUT_LIMIT 100
76
77 #ifdef __cplusplus
78 extern "C" {
79 #endif
80 extern VAStatus DdiDestroyContextCM (VADriverContextP vaDrvCtx, VAContextID vaCtxID);
81 #ifdef __cplusplus
82 }
83 #endif
84
85 extern template class MediaFactory<uint32_t, MhwInterfaces>;
86
87 VAProcFilterType vp_supported_filters[DDI_VP_MAX_NUM_FILTERS] = {
88 VAProcFilterNoiseReduction,
89 VAProcFilterDeinterlacing,
90 VAProcFilterSharpening,
91 VAProcFilterColorBalance,
92 VAProcFilterSkinToneEnhancement,
93 VAProcFilterTotalColorCorrection,
94 VAProcFilterHVSNoiseReduction,
95 VAProcFilterHighDynamicRangeToneMapping
96 #if VA_CHECK_VERSION(1, 12, 0)
97 , VAProcFilter3DLUT
98 #endif
99 };
100
101 VAProcColorStandardType vp_input_color_std[DDI_VP_NUM_INPUT_COLOR_STD] = {
102 VAProcColorStandardBT601,
103 VAProcColorStandardBT709,
104 VAProcColorStandardSRGB,
105 VAProcColorStandardSTRGB,
106 VAProcColorStandardBT2020,
107 VAProcColorStandardExplicit
108 };
109
110 VAProcColorStandardType vp_output_color_std[DDI_VP_NUM_OUT_COLOR_STD] = {
111 VAProcColorStandardBT601,
112 VAProcColorStandardBT709,
113 VAProcColorStandardSRGB,
114 VAProcColorStandardSTRGB,
115 VAProcColorStandardBT2020,
116 VAProcColorStandardExplicit
117 };
118
119 // Making this API public since media_libva_vp.c calls this
120 VAStatus DdiMedia_MapBuffer (
121 VADriverContextP ctx,
122 VABufferID buf_id, /* in */
123 void **pbuf /* out */
124 );
125
126 VAStatus DdiMedia_UnmapBuffer (
127 VADriverContextP ctx,
128 VABufferID buf_id /* in */
129 );
130
131 VAStatus DdiMedia_DestroyImage (
132 VADriverContextP ctx,
133 VAImageID image
134 );
135
136 static PDDI_MEDIA_CONTEXT DdiMedia_CreateMediaDriverContext()
137 {
138 PDDI_MEDIA_CONTEXT mediaCtx;
139
140 mediaCtx = MOS_New(DDI_MEDIA_CONTEXT);
141
142 return mediaCtx;
143 }
144
145 // refine this for decoder later
146 static bool DdiMedia_ReleaseSliceControlBuffer(
147 uint32_t ctxType,
148 void *ctx,
149 DDI_MEDIA_BUFFER *buf)
150 {
151 DDI_UNUSED(buf);
152
153 switch (ctxType)
154 {
155 case DDI_MEDIA_CONTEXT_TYPE_DECODER:
156 {
157 PDDI_DECODE_CONTEXT decCtx;
158
159 decCtx = DdiDecode_GetDecContextFromPVOID(ctx);
160 DDI_CODEC_COM_BUFFER_MGR *bufMgr = &(decCtx->BufMgr);
161
162 switch (decCtx->wMode)
163 {
164 case CODECHAL_DECODE_MODE_AVCVLD:
165 if(decCtx->bShortFormatInUse)
166 {
167 if(bufMgr->Codec_Param.Codec_Param_H264.pVASliceParaBufH264Base == nullptr)
168 {
169 return false;
170 }
171 }
172 else
173 {
174 if(bufMgr->Codec_Param.Codec_Param_H264.pVASliceParaBufH264 == nullptr)
175 {
176 return false;
177 }
178 }
179 break;
180 case CODECHAL_DECODE_MODE_MPEG2VLD:
181 if(bufMgr->Codec_Param.Codec_Param_MPEG2.pVASliceParaBufMPEG2 == nullptr)
182 {
183 return false;
184 }
185 break;
186 case CODECHAL_DECODE_MODE_VC1VLD:
187 if(bufMgr->Codec_Param.Codec_Param_VC1.pVASliceParaBufVC1 == nullptr)
188 {
189 return false;
190 }
191 break;
192 case CODECHAL_DECODE_MODE_JPEG:
193 if(bufMgr->Codec_Param.Codec_Param_JPEG.pVASliceParaBufJPEG == nullptr)
194 {
195 return false;
196 }
197 break;
198 case CODECHAL_DECODE_MODE_VP8VLD:
199 if(bufMgr->Codec_Param.Codec_Param_VP8.pVASliceParaBufVP8 == nullptr)
200 {
201 return false;
202 }
203 break;
204 case CODECHAL_DECODE_MODE_HEVCVLD:
205 if(decCtx->bShortFormatInUse)
206 {
207 if(bufMgr->Codec_Param.Codec_Param_HEVC.pVASliceParaBufBaseHEVC == nullptr)
208 {
209 return false;
210 }
211 }
212 else
213 {
214 if(bufMgr->Codec_Param.Codec_Param_HEVC.pVASliceParaBufHEVC == nullptr &&
215 bufMgr->Codec_Param.Codec_Param_HEVC.pVASliceParaBufHEVCRext == nullptr)
216 {
217 return false;
218 }
219 }
220 break;
221 case CODECHAL_DECODE_MODE_VP9VLD:
222 if(bufMgr->Codec_Param.Codec_Param_VP9.pVASliceParaBufVP9 == nullptr)
223 {
224 return false;
225 }
226 break;
227 default:
228 return false;
229 }
230 break;
231 }
232 case DDI_MEDIA_CONTEXT_TYPE_ENCODER:
233 break;
234 default:
235 break;
236 }
237
238 return true;
239 }
240
241 static bool DdiMedia_ReleaseBpBuffer(
242 DDI_CODEC_COM_BUFFER_MGR *bufMgr,
243 DDI_MEDIA_BUFFER *buf)
244 {
245 DDI_UNUSED(bufMgr);
246 DDI_UNUSED(buf);
247 return true;
248 }
249
250 static bool DdiMedia_ReleaseBsBuffer(
251 DDI_CODEC_COM_BUFFER_MGR *bufMgr,
252 DDI_MEDIA_BUFFER *buf)
253 {
254 if ((bufMgr == nullptr) || (buf == nullptr))
255 {
256 return true;
257 }
258
259 if (buf->format == Media_Format_CPU)
260 {
261 for(uint32_t i = 0; i < bufMgr->dwNumSliceData; i++)
262 {
263 if(bufMgr->pSliceData[i].pBaseAddress == buf->pData)
264 {
265 DdiMediaUtil_FreeBuffer(buf);
266 bufMgr->pSliceData[i].pBaseAddress = nullptr;
267 if (bufMgr->pSliceData[i].pMappedGPUBuffer != nullptr)
268 {
269 DdiMediaUtil_UnlockBuffer(bufMgr->pSliceData[i].pMappedGPUBuffer);
270 if (bufMgr->pSliceData[i].pMappedGPUBuffer->bMapped == false)
271 {
272 DdiMediaUtil_FreeBuffer(bufMgr->pSliceData[i].pMappedGPUBuffer);
273 MOS_FreeMemory(bufMgr->pSliceData[i].pMappedGPUBuffer);
274 }
275 }
276 MOS_ZeroMemory((void*)(&(bufMgr->pSliceData[i])), sizeof(bufMgr->pSliceData[0]));
277 bufMgr->dwNumSliceData --;
278 return true;
279 }
280 }
281 return false;
282 }
283
284 return true;
285 }
286
287 static uint32_t DdiMedia_CreateRenderTarget(
288 PDDI_MEDIA_CONTEXT mediaDrvCtx,
289 DDI_MEDIA_FORMAT mediaFormat,
290 uint32_t width,
291 uint32_t height,
292 DDI_MEDIA_SURFACE_DESCRIPTOR *surfDesc,
293 uint32_t surfaceUsageHint,
294 int memType
295 )
296 {
297 DdiMediaUtil_LockMutex(&mediaDrvCtx->SurfaceMutex);
298
299 PDDI_MEDIA_SURFACE_HEAP_ELEMENT surfaceElement = DdiMediaUtil_AllocPMediaSurfaceFromHeap(mediaDrvCtx->pSurfaceHeap);
300 if (nullptr == surfaceElement)
301 {
302 DdiMediaUtil_UnLockMutex(&mediaDrvCtx->SurfaceMutex);
303 return VA_INVALID_ID;
304 }
305
306 surfaceElement->pSurface = (DDI_MEDIA_SURFACE *)MOS_AllocAndZeroMemory(sizeof(DDI_MEDIA_SURFACE));
307 if (nullptr == surfaceElement->pSurface)
308 {
309 DdiMediaUtil_ReleasePMediaSurfaceFromHeap(mediaDrvCtx->pSurfaceHeap, surfaceElement->uiVaSurfaceID);
310 DdiMediaUtil_UnLockMutex(&mediaDrvCtx->SurfaceMutex);
311 return VA_INVALID_ID;
312 }
313
314 surfaceElement->pSurface->pMediaCtx = mediaDrvCtx;
315 surfaceElement->pSurface->iWidth = width;
316 surfaceElement->pSurface->iHeight = height;
317 surfaceElement->pSurface->pSurfDesc = surfDesc;
318 surfaceElement->pSurface->format = mediaFormat;
319 surfaceElement->pSurface->uiLockedBufID = VA_INVALID_ID;
320 surfaceElement->pSurface->uiLockedImageID = VA_INVALID_ID;
321 surfaceElement->pSurface->surfaceUsageHint= surfaceUsageHint;
322 surfaceElement->pSurface->memType = memType;
323
324 if(DdiMediaUtil_CreateSurface(surfaceElement->pSurface, mediaDrvCtx)!= VA_STATUS_SUCCESS)
325 {
326 MOS_FreeMemory(surfaceElement->pSurface);
327 DdiMediaUtil_ReleasePMediaSurfaceFromHeap(mediaDrvCtx->pSurfaceHeap, surfaceElement->uiVaSurfaceID);
328 DdiMediaUtil_UnLockMutex(&mediaDrvCtx->SurfaceMutex);
329 return VA_INVALID_ID;
330 }
331
332 mediaDrvCtx->uiNumSurfaces++;
333 uint32_t surfaceID = surfaceElement->uiVaSurfaceID;
334 DdiMediaUtil_UnLockMutex(&mediaDrvCtx->SurfaceMutex);
335 return surfaceID;
336 }
337
338 VAStatus
339 DdiMedia_HybridQueryBufferAttributes (
340 VADisplay dpy,
341 VAContextID context,
342 VABufferType bufferType,
343 void *outputData,
344 uint32_t *outputDataLen
345 )
346 {
347 return VA_STATUS_ERROR_UNIMPLEMENTED;
348 }
349
350 //!
351 //! \brief Set Frame ID
352 //!
353 //! \param [in] dpy
354 //! VA display
355 //! \param [in] surface
356 //! VA surface ID
357 //! \param [in] frame_id
358 //! Frame ID
359 //!
360 //! \return VAStatus
361 //! VA_STATUS_SUCCESS if success, else fail reason
362 //!
363 VAStatus DdiMedia_SetFrameID(
364 VADisplay dpy,
365 VASurfaceID surface,
366 uint32_t frame_id
367 )
368 {
369 VADriverContextP ctx = (((VADisplayContextP)dpy)->pDriverContext);
370 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
371
372 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
373 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
374
375 DDI_MEDIA_SURFACE *mediaSurface = DdiMedia_GetSurfaceFromVASurfaceID(mediaCtx, surface);
376 DDI_CHK_NULL(mediaSurface, "nullptr mediaSurface.", VA_STATUS_ERROR_INVALID_PARAMETER);
377
378 mediaSurface->frame_idx = frame_id;
379
380 return VA_STATUS_SUCCESS;
381 }
382
383 //!
384 //! \brief Convert media format to OS format
385 //!
386 //! \param [in] format
387 //! Ddi media format
388 //!
389 //! \return Os format if call sucesss,else
390 //! VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT if fail
391 //!
392 int32_t DdiMedia_MediaFormatToOsFormat(DDI_MEDIA_FORMAT format)
393 {
394 switch (format)
395 {
396 case Media_Format_X8R8G8B8:
397 return VA_FOURCC_XRGB;
398 case Media_Format_X8B8G8R8:
399 return VA_FOURCC_XBGR;
400 case Media_Format_A8B8G8R8:
401 return VA_FOURCC_ABGR;
402 case Media_Format_R10G10B10A2:
403 return VA_FOURCC_A2B10G10R10;
404 case Media_Format_R8G8B8A8:
405 return VA_FOURCC_RGBA;
406 case Media_Format_A8R8G8B8:
407 return VA_FOURCC_ARGB;
408 case Media_Format_B10G10R10A2:
409 return VA_FOURCC_A2R10G10B10;
410 case Media_Format_R10G10B10X2:
411 return VA_FOURCC_X2B10G10R10;
412 case Media_Format_B10G10R10X2:
413 return VA_FOURCC_X2R10G10B10;
414 case Media_Format_R5G6B5:
415 return VA_FOURCC_R5G6B5;
416 case Media_Format_R8G8B8:
417 return VA_FOURCC_R8G8B8;
418 case Media_Format_NV12:
419 return VA_FOURCC_NV12;
420 case Media_Format_NV21:
421 return VA_FOURCC_NV21;
422 case Media_Format_YUY2:
423 return VA_FOURCC_YUY2;
424 case Media_Format_UYVY:
425 return VA_FOURCC_UYVY;
426 case Media_Format_YV12:
427 return VA_FOURCC_YV12;
428 case Media_Format_IYUV:
429 return VA_FOURCC_IYUV;
430 case Media_Format_I420:
431 return VA_FOURCC_I420;
432 case Media_Format_400P:
433 return VA_FOURCC('4','0','0','P');
434 case Media_Format_IMC3:
435 return VA_FOURCC_IMC3;
436 case Media_Format_422H:
437 return VA_FOURCC_422H;
438 case Media_Format_422V:
439 return VA_FOURCC_422V;
440 case Media_Format_411P:
441 return VA_FOURCC_411P;
442 case Media_Format_444P:
443 return VA_FOURCC_444P;
444 case Media_Format_RGBP:
445 return VA_FOURCC_RGBP;
446 case Media_Format_BGRP:
447 return VA_FOURCC_BGRP;
448 case Media_Format_Buffer:
449 return VA_FOURCC_P208;
450 case Media_Format_P010:
451 return VA_FOURCC_P010;
452 case Media_Format_P012:
453 return VA_FOURCC_P012;
454 case Media_Format_P016:
455 return VA_FOURCC_P016;
456 case Media_Format_Y210:
457 return VA_FOURCC_Y210;
458 #if VA_CHECK_VERSION(1, 9, 0)
459 case Media_Format_Y212:
460 return VA_FOURCC_Y212;
461 #endif
462 case Media_Format_Y216:
463 return VA_FOURCC_Y216;
464 case Media_Format_AYUV:
465 return VA_FOURCC_AYUV;
466 #if VA_CHECK_VERSION(1, 13, 0)
467 case Media_Format_XYUV:
468 return VA_FOURCC_XYUV;
469 #endif
470 case Media_Format_Y410:
471 return VA_FOURCC_Y410;
472 #if VA_CHECK_VERSION(1, 9, 0)
473 case Media_Format_Y412:
474 return VA_FOURCC_Y412;
475 #endif
476 case Media_Format_Y416:
477 return VA_FOURCC_Y416;
478 case Media_Format_Y8:
479 return VA_FOURCC_Y8;
480 case Media_Format_Y16S:
481 return VA_FOURCC_Y16;
482 case Media_Format_Y16U:
483 return VA_FOURCC_Y16;
484 case Media_Format_VYUY:
485 return VA_FOURCC_VYUY;
486 case Media_Format_YVYU:
487 return VA_FOURCC_YVYU;
488 case Media_Format_A16R16G16B16:
489 return VA_FOURCC_ARGB64;
490 case Media_Format_A16B16G16R16:
491 return VA_FOURCC_ABGR64;
492
493 default:
494 return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
495 }
496 }
497
498 //!
499 //! \brief Convert Os format to media format
500 //!
501 //! \param [in] fourcc
502 //! FourCC
503 //! \param [in] rtformatType
504 //! Rt format type
505 //!
506 //! \return DDI_MEDIA_FORMAT
507 //! Ddi media format
508 //!
509 DDI_MEDIA_FORMAT DdiMedia_OsFormatToMediaFormat(int32_t fourcc, int32_t rtformatType)
510 {
511 switch (fourcc)
512 {
513 case VA_FOURCC_A2R10G10B10:
514 return Media_Format_B10G10R10A2;
515 case VA_FOURCC_A2B10G10R10:
516 return Media_Format_R10G10B10A2;
517 case VA_FOURCC_X2R10G10B10:
518 return Media_Format_B10G10R10X2;
519 case VA_FOURCC_X2B10G10R10:
520 return Media_Format_R10G10B10X2;
521 case VA_FOURCC_BGRA:
522 case VA_FOURCC_ARGB:
523 #ifdef VA_RT_FORMAT_RGB32_10BPP
524 if(VA_RT_FORMAT_RGB32_10BPP == rtformatType)
525 {
526 return Media_Format_B10G10R10A2;
527 }
528 #endif
529 return Media_Format_A8R8G8B8;
530 case VA_FOURCC_RGBA:
531 #ifdef VA_RT_FORMAT_RGB32_10BPP
532 if(VA_RT_FORMAT_RGB32_10BPP == rtformatType)
533 {
534 return Media_Format_R10G10B10A2;
535 }
536 #endif
537 return Media_Format_R8G8B8A8;
538 case VA_FOURCC_ABGR:
539 #ifdef VA_RT_FORMAT_RGB32_10BPP
540 if(VA_RT_FORMAT_RGB32_10BPP == rtformatType)
541 {
542 return Media_Format_R10G10B10A2;
543 }
544 #endif
545 return Media_Format_A8B8G8R8;
546 case VA_FOURCC_BGRX:
547 case VA_FOURCC_XRGB:
548 return Media_Format_X8R8G8B8;
549 case VA_FOURCC_XBGR:
550 case VA_FOURCC_RGBX:
551 return Media_Format_X8B8G8R8;
552 case VA_FOURCC_R5G6B5:
553 return Media_Format_R5G6B5;
554 case VA_FOURCC_R8G8B8:
555 return Media_Format_R8G8B8;
556 case VA_FOURCC_NV12:
557 return Media_Format_NV12;
558 case VA_FOURCC_NV21:
559 return Media_Format_NV21;
560 case VA_FOURCC_YUY2:
561 return Media_Format_YUY2;
562 case VA_FOURCC_UYVY:
563 return Media_Format_UYVY;
564 case VA_FOURCC_YV12:
565 return Media_Format_YV12;
566 case VA_FOURCC_IYUV:
567 return Media_Format_IYUV;
568 case VA_FOURCC_I420:
569 return Media_Format_I420;
570 case VA_FOURCC_422H:
571 return Media_Format_422H;
572 case VA_FOURCC_422V:
573 return Media_Format_422V;
574 case VA_FOURCC('4','0','0','P'):
575 case VA_FOURCC_Y800:
576 return Media_Format_400P;
577 case VA_FOURCC_411P:
578 return Media_Format_411P;
579 case VA_FOURCC_IMC3:
580 return Media_Format_IMC3;
581 case VA_FOURCC_444P:
582 return Media_Format_444P;
583 case VA_FOURCC_BGRP:
584 return Media_Format_BGRP;
585 case VA_FOURCC_RGBP:
586 return Media_Format_RGBP;
587 case VA_FOURCC_P208:
588 return Media_Format_Buffer;
589 case VA_FOURCC_P010:
590 return Media_Format_P010;
591 case VA_FOURCC_P012:
592 return Media_Format_P012;
593 case VA_FOURCC_P016:
594 return Media_Format_P016;
595 case VA_FOURCC_Y210:
596 return Media_Format_Y210;
597 #if VA_CHECK_VERSION(1, 9, 0)
598 case VA_FOURCC_Y212:
599 return Media_Format_Y212;
600 #endif
601 case VA_FOURCC_Y216:
602 return Media_Format_Y216;
603 case VA_FOURCC_AYUV:
604 return Media_Format_AYUV;
605 #if VA_CHECK_VERSION(1, 13, 0)
606 case VA_FOURCC_XYUV:
607 return Media_Format_XYUV;
608 #endif
609 case VA_FOURCC_Y410:
610 return Media_Format_Y410;
611 #if VA_CHECK_VERSION(1, 9, 0)
612 case VA_FOURCC_Y412:
613 return Media_Format_Y412;
614 #endif
615 case VA_FOURCC_Y416:
616 return Media_Format_Y416;
617 case VA_FOURCC_Y8:
618 return Media_Format_Y8;
619 case VA_FOURCC_Y16:
620 return Media_Format_Y16S;
621 case VA_FOURCC_VYUY:
622 return Media_Format_VYUY;
623 case VA_FOURCC_YVYU:
624 return Media_Format_YVYU;
625 case VA_FOURCC_ARGB64:
626 return Media_Format_A16R16G16B16;
627 case VA_FOURCC_ABGR64:
628 return Media_Format_A16B16G16R16;
629
630 default:
631 return Media_Format_Count;
632 }
633 }
634
635 static VAStatus DdiMedia_GetChromaPitchHeight(
636 uint32_t fourcc,
637 uint32_t pitch,
638 uint32_t height,
639 uint32_t *chromaPitch,
640 uint32_t *chromaHeight)
641 {
642 DDI_CHK_NULL(chromaPitch, "nullptr chromaPitch", VA_STATUS_ERROR_INVALID_PARAMETER);
643 DDI_CHK_NULL(chromaHeight, "nullptr chromaHeight", VA_STATUS_ERROR_INVALID_PARAMETER);
644
645 switch(fourcc)
646 {
647 case VA_FOURCC_NV12:
648 case VA_FOURCC_P010:
649 case VA_FOURCC_P012:
650 case VA_FOURCC_P016:
651 *chromaHeight = MOS_ALIGN_CEIL(height, 2) / 2;
652 *chromaPitch = pitch;
653 break;
654 case VA_FOURCC_I420:
655 case VA_FOURCC_YV12:
656 *chromaHeight = MOS_ALIGN_CEIL(height, 2) / 2;
657 *chromaPitch = MOS_ALIGN_CEIL(pitch, 2) / 2;
658 break;
659 case VA_FOURCC_411P:
660 case VA_FOURCC_422H:
661 case VA_FOURCC_444P:
662 case VA_FOURCC_RGBP:
663 *chromaHeight = height;
664 *chromaPitch = pitch;
665 break;
666 case VA_FOURCC_422V:
667 case VA_FOURCC_IMC3:
668 *chromaHeight = MOS_ALIGN_CEIL(height, 2) / 2;
669 *chromaPitch = pitch;
670 break;
671 default:
672 *chromaPitch = 0;
673 *chromaHeight = 0;
674 }
675
676 return VA_STATUS_SUCCESS;
677 }
678
679
680 #if !defined(ANDROID) && defined(X11_FOUND)
681
682 #define X11_LIB_NAME "libX11.so.6"
683
684 /*
685 * Close opened libX11.so lib, free related function table.
686 */
687 static void DdiMedia_DestroyX11Connection(
688 PDDI_MEDIA_CONTEXT mediaCtx
689 )
690 {
691 if (nullptr == mediaCtx || nullptr == mediaCtx->X11FuncTable)
692 {
693 return;
694 }
695
696 MosUtilities::MosFreeLibrary(mediaCtx->X11FuncTable->pX11LibHandle);
697 MOS_FreeMemory(mediaCtx->X11FuncTable);
698 mediaCtx->X11FuncTable = nullptr;
699
700 return;
701 }
702
703 /*
704 * dlopen libX11.so, setup the function table, which is used by
705 * DdiCodec_PutSurface (Linux) so far.
706 */
707 static VAStatus DdiMedia_ConnectX11(
708 PDDI_MEDIA_CONTEXT mediaCtx
709 )
710 {
711 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
712
713 mediaCtx->X11FuncTable = (PDDI_X11_FUNC_TABLE)MOS_AllocAndZeroMemory(sizeof(DDI_X11_FUNC_TABLE));
714 DDI_CHK_NULL(mediaCtx->X11FuncTable, "Allocation Failed for X11FuncTable", VA_STATUS_ERROR_ALLOCATION_FAILED);
715
716 HMODULE h_module = nullptr;
717 MOS_STATUS mos_status = MosUtilities::MosLoadLibrary(X11_LIB_NAME, &h_module);
718 if (MOS_STATUS_SUCCESS != mos_status || nullptr == h_module)
719 {
720 DdiMedia_DestroyX11Connection(mediaCtx);
721 return VA_STATUS_ERROR_OPERATION_FAILED;
722 }
723
724 mediaCtx->X11FuncTable->pX11LibHandle = h_module;
725
726 mediaCtx->X11FuncTable->pfnXCreateGC =
727 MosUtilities::MosGetProcAddress(h_module, "XCreateGC");
728 mediaCtx->X11FuncTable->pfnXFreeGC =
729 MosUtilities::MosGetProcAddress(h_module, "XFreeGC");
730 mediaCtx->X11FuncTable->pfnXCreateImage =
731 MosUtilities::MosGetProcAddress(h_module, "XCreateImage");
732 mediaCtx->X11FuncTable->pfnXDestroyImage =
733 MosUtilities::MosGetProcAddress(h_module, "XDestroyImage");
734 mediaCtx->X11FuncTable->pfnXPutImage =
735 MosUtilities::MosGetProcAddress(h_module, "XPutImage");
736
737 if (nullptr == mediaCtx->X11FuncTable->pfnXCreateGC ||
738 nullptr == mediaCtx->X11FuncTable->pfnXFreeGC ||
739 nullptr == mediaCtx->X11FuncTable->pfnXCreateImage ||
740 nullptr == mediaCtx->X11FuncTable->pfnXDestroyImage ||
741 nullptr == mediaCtx->X11FuncTable->pfnXPutImage)
742 {
743 DdiMedia_DestroyX11Connection(mediaCtx);
744 return VA_STATUS_ERROR_OPERATION_FAILED;
745 }
746
747 return VA_STATUS_SUCCESS;
748 }
749 #endif
750
751 /////////////////////////////////////////////////////////////////////////////
752 //! \Free allocated surfaceheap elements
753 //! \params
754 //! [in] PDDI_MEDIA_CONTEXT
755 //! [out] none
756 //! \returns
757 /////////////////////////////////////////////////////////////////////////////
758 static void DdiMedia_FreeSurfaceHeapElements(PDDI_MEDIA_CONTEXT mediaCtx)
759 {
760 if (nullptr == mediaCtx)
761 return;
762 PDDI_MEDIA_HEAP surfaceHeap = mediaCtx->pSurfaceHeap;
763
764 if (nullptr == surfaceHeap)
765 return;
766
767 PDDI_MEDIA_SURFACE_HEAP_ELEMENT mediaSurfaceHeapBase = (PDDI_MEDIA_SURFACE_HEAP_ELEMENT)surfaceHeap->pHeapBase;
768 if (nullptr == mediaSurfaceHeapBase)
769 return;
770
771 int32_t surfaceNums = mediaCtx->uiNumSurfaces;
772 for (int32_t elementId = 0; surfaceNums > 0 && elementId < surfaceHeap->uiAllocatedHeapElements; elementId++)
773 {
774 PDDI_MEDIA_SURFACE_HEAP_ELEMENT mediaSurfaceHeapElmt = &mediaSurfaceHeapBase[elementId];
775 if (nullptr == mediaSurfaceHeapElmt->pSurface)
776 continue;
777
778 DdiMediaUtil_FreeSurface(mediaSurfaceHeapElmt->pSurface);
779 MOS_FreeMemory(mediaSurfaceHeapElmt->pSurface);
780 DdiMediaUtil_ReleasePMediaSurfaceFromHeap(surfaceHeap,mediaSurfaceHeapElmt->uiVaSurfaceID);
781 surfaceNums--;
782 mediaCtx->uiNumSurfaces--;
783 }
784 }
785
786 /////////////////////////////////////////////////////////////////////////////
787 //! \Free allocated bufferheap elements
788 //! \params
789 //! [in] VADriverContextP
790 //! [out] none
791 //! \returns
792 /////////////////////////////////////////////////////////////////////////////
793 static void DdiMedia_FreeBufferHeapElements(VADriverContextP ctx)
794 {
795 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
796 if (nullptr == mediaCtx)
797 return;
798
799 PDDI_MEDIA_HEAP bufferHeap = mediaCtx->pBufferHeap;
800 if (nullptr == bufferHeap)
801 return;
802
803 PDDI_MEDIA_BUFFER_HEAP_ELEMENT mediaBufferHeapBase = (PDDI_MEDIA_BUFFER_HEAP_ELEMENT)bufferHeap->pHeapBase;
804 if (nullptr == mediaBufferHeapBase)
805 return;
806
807 int32_t bufNums = mediaCtx->uiNumBufs;
808 for (int32_t elementId = 0; bufNums > 0 && elementId < bufferHeap->uiAllocatedHeapElements; ++elementId)
809 {
810 PDDI_MEDIA_BUFFER_HEAP_ELEMENT mediaBufferHeapElmt = &mediaBufferHeapBase[elementId];
811 if (nullptr == mediaBufferHeapElmt->pBuffer)
812 continue;
813 //Note: uiNumBufs will recount in DdiMedia_DestroyBuffer
814 DdiMedia_DestroyBuffer(ctx,mediaBufferHeapElmt->uiVaBufferID);
815 //Ensure the non-empty buffer to be destroyed.
816 bufNums--;
817 }
818 }
819
820 /////////////////////////////////////////////////////////////////////////////
821 //! \Free allocated Imageheap elements
822 //! \params
823 //! [in] VADriverContextP
824 //! [out] none
825 //! \returns
826 /////////////////////////////////////////////////////////////////////////////
827 static void DdiMedia_FreeImageHeapElements(VADriverContextP ctx)
828 {
829 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
830 if (nullptr == mediaCtx)
831 return;
832
833 PDDI_MEDIA_HEAP imageHeap = mediaCtx->pImageHeap;
834 if (nullptr == imageHeap)
835 return;
836
837 PDDI_MEDIA_IMAGE_HEAP_ELEMENT mediaImageHeapBase = (PDDI_MEDIA_IMAGE_HEAP_ELEMENT)imageHeap->pHeapBase;
838 if (nullptr == mediaImageHeapBase)
839 return;
840
841 int32_t imageNums = mediaCtx->uiNumImages;
842 for (int32_t elementId = 0; imageNums > 0 && elementId < imageHeap->uiAllocatedHeapElements; ++elementId)
843 {
844 PDDI_MEDIA_IMAGE_HEAP_ELEMENT mediaImageHeapElmt = &mediaImageHeapBase[elementId];
845 if (nullptr == mediaImageHeapElmt->pImage)
846 continue;
847 //Note: uiNumImages will recount in DdiMedia_DestroyImage
848 DdiMedia_DestroyImage(ctx,mediaImageHeapElmt->uiVaImageID);
849 imageNums--;
850 }
851 }
852
853 /////////////////////////////////////////////////////////////////////////////
854 //! \Execute free allocated bufferheap elements for FreeContextHeapElements function
855 //! \params
856 //! [in] VADriverContextP
857 //! [in] PDDI_MEDIA_HEAP
858 //! [out] none
859 //! \returns
860 /////////////////////////////////////////////////////////////////////////////
861 static void DdiMedia_FreeContextHeap(VADriverContextP ctx, PDDI_MEDIA_HEAP contextHeap,int32_t vaContextOffset, int32_t ctxNums)
862 {
863 PDDI_MEDIA_VACONTEXT_HEAP_ELEMENT mediaContextHeapBase = (PDDI_MEDIA_VACONTEXT_HEAP_ELEMENT)contextHeap->pHeapBase;
864 if (nullptr == mediaContextHeapBase)
865 return;
866
867 for (int32_t elementId = 0; ctxNums > 0 && elementId < contextHeap->uiAllocatedHeapElements; ++elementId)
868 {
869 PDDI_MEDIA_VACONTEXT_HEAP_ELEMENT mediaContextHeapElmt = &mediaContextHeapBase[elementId];
870 if (nullptr == mediaContextHeapElmt->pVaContext)
871 continue;
872 VAContextID vaCtxID = (VAContextID)(mediaContextHeapElmt->uiVaContextID + vaContextOffset);
873 DdiMedia_DestroyContext(ctx,vaCtxID);
874 ctxNums--;
875 }
876
877 }
878
879 /////////////////////////////////////////////////////////////////////////////
880 //! \Free allocated contextheap elements
881 //! \params
882 //! [in] VADriverContextP
883 //! [out] none
884 //! \returns
885 /////////////////////////////////////////////////////////////////////////////
886 static void DdiMedia_FreeContextHeapElements(VADriverContextP ctx)
887 {
888 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
889 if (nullptr == mediaCtx)
890 return;
891
892 //Free EncoderContext
893 PDDI_MEDIA_HEAP encoderContextHeap = mediaCtx->pEncoderCtxHeap;
894 int32_t encCtxNums = mediaCtx->uiNumEncoders;
895 if (nullptr != encoderContextHeap)
896 DdiMedia_FreeContextHeap(ctx,encoderContextHeap,DDI_MEDIA_VACONTEXTID_OFFSET_ENCODER,encCtxNums);
897
898 //Free DecoderContext
899 PDDI_MEDIA_HEAP decoderContextHeap = mediaCtx->pDecoderCtxHeap;
900 int32_t decCtxNums = mediaCtx->uiNumDecoders;
901 if (nullptr != decoderContextHeap)
902 DdiMedia_FreeContextHeap(ctx,decoderContextHeap,DDI_MEDIA_VACONTEXTID_OFFSET_DECODER,decCtxNums);
903
904 //Free VpContext
905 PDDI_MEDIA_HEAP vpContextHeap = mediaCtx->pVpCtxHeap;
906 int32_t vpctxNums = mediaCtx->uiNumVPs;
907 if (nullptr != vpContextHeap)
908 DdiMedia_FreeContextHeap(ctx,vpContextHeap,DDI_MEDIA_VACONTEXTID_OFFSET_VP,vpctxNums);
909
910 //Free ProtContext
911 PDDI_MEDIA_HEAP protContextHeap = mediaCtx->pProtCtxHeap;
912 int32_t protCtxNums = mediaCtx->uiNumProts;
913 if (nullptr != protContextHeap)
914 DdiMedia_FreeProtectedSessionHeap(ctx,protContextHeap,DDI_MEDIA_VACONTEXTID_OFFSET_PROT,protCtxNums);
915
916 //Free MfeContext
917 PDDI_MEDIA_HEAP mfeContextHeap = mediaCtx->pMfeCtxHeap;
918 int32_t mfeCtxNums = mediaCtx->uiNumMfes;
919 if (nullptr != mfeContextHeap)
920 DdiMedia_FreeContextHeap(ctx, mfeContextHeap, DDI_MEDIA_VACONTEXTID_OFFSET_MFE, mfeCtxNums);
921
922 // Free media memory decompression data structure
923 if (!mediaCtx->m_apoMosEnabled && mediaCtx->pMediaMemDecompState)
924 {
925 MediaMemDecompBaseState *mediaMemCompState =
926 static_cast<MediaMemDecompBaseState*>(mediaCtx->pMediaMemDecompState);
927 MOS_Delete(mediaMemCompState);
928 }
929 mediaCtx->pMediaMemDecompState = nullptr;
930 }
931
932 /////////////////////////////////////////////////////////////////////////////
933 //! \Free allocated ContextCM elements
934 //! \params
935 //! [in] VADriverContextP
936 //! [out] none
937 //! \returns
938 /////////////////////////////////////////////////////////////////////////////
939 static void DdiMedia_FreeContextCMElements(VADriverContextP ctx)
940 {
941 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
942 if (nullptr == mediaCtx)
943 return;
944
945 int32_t cmnums = mediaCtx->uiNumCMs;
946 for (int32_t elementId = 0; elementId < cmnums; elementId++)
947 {
948 VAContextID vaCtxID = elementId + DDI_MEDIA_VACONTEXTID_OFFSET_CM;
949 DdiDestroyContextCM(ctx,vaCtxID);
950 }
951 }
952
953 //!
954 //! \brief Get VA image from VA image ID
955 //!
956 //! \param [in] mediaCtx
957 //! Pointer to ddi media context
958 //! \param [in] imageID
959 //! VA image ID
960 //!
961 //! \return VAImage*
962 //! Pointer to VAImage
963 //!
964 VAImage* DdiMedia_GetVAImageFromVAImageID (PDDI_MEDIA_CONTEXT mediaCtx, VAImageID imageID)
965 {
966 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", nullptr);
967
968 uint32_t i = (uint32_t)imageID;
969 DDI_CHK_LESS(i, mediaCtx->pImageHeap->uiAllocatedHeapElements, "invalid image id", nullptr);
970 DdiMediaUtil_LockMutex(&mediaCtx->ImageMutex);
971 PDDI_MEDIA_IMAGE_HEAP_ELEMENT imageElement = (PDDI_MEDIA_IMAGE_HEAP_ELEMENT)mediaCtx->pImageHeap->pHeapBase;
972 imageElement += i;
973 VAImage *vaImage = imageElement->pImage;
974 DdiMediaUtil_UnLockMutex(&mediaCtx->ImageMutex);
975
976 return vaImage;
977 }
978
979 //!
980 //! \brief Get ctx from VA buffer ID
981 //!
982 //! \param [in] mediaCtx
983 //! pddi media context
984 //! \param [in] bufferID
985 //! VA Buffer ID
986 //!
987 //! \return void*
988 //! Pointer to buffer heap element context
989 //!
990 void* DdiMedia_GetCtxFromVABufferID (PDDI_MEDIA_CONTEXT mediaCtx, VABufferID bufferID)
991 {
992 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", nullptr);
993
994 uint32_t i = (uint32_t)bufferID;
995 DDI_CHK_LESS(i, mediaCtx->pBufferHeap->uiAllocatedHeapElements, "invalid buffer id", nullptr);
996 DdiMediaUtil_LockMutex(&mediaCtx->BufferMutex);
997 PDDI_MEDIA_BUFFER_HEAP_ELEMENT bufHeapElement = (PDDI_MEDIA_BUFFER_HEAP_ELEMENT)mediaCtx->pBufferHeap->pHeapBase;
998 bufHeapElement += i;
999 void *temp = bufHeapElement->pCtx;
1000 DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex);
1001
1002 return temp;
1003 }
1004
1005 //!
1006 //! \brief Get ctx type from VA buffer ID
1007 //!
1008 //! \param [in] mediaCtx
1009 //! Pointer to ddi media context
1010 //! \param [in] bufferID
1011 //! VA buffer ID
1012 //!
1013 //! \return uint32_t
1014 //1 Context type
1015 //!
1016 uint32_t DdiMedia_GetCtxTypeFromVABufferID (PDDI_MEDIA_CONTEXT mediaCtx, VABufferID bufferID)
1017 {
1018 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", DDI_MEDIA_CONTEXT_TYPE_NONE);
1019
1020 uint32_t i = (uint32_t)bufferID;
1021 DDI_CHK_LESS(i, mediaCtx->pBufferHeap->uiAllocatedHeapElements, "invalid buffer id", DDI_MEDIA_CONTEXT_TYPE_NONE);
1022 DdiMediaUtil_LockMutex(&mediaCtx->BufferMutex);
1023 PDDI_MEDIA_BUFFER_HEAP_ELEMENT bufHeapElement = (PDDI_MEDIA_BUFFER_HEAP_ELEMENT)mediaCtx->pBufferHeap->pHeapBase;
1024 bufHeapElement += i;
1025 uint32_t ctxType = bufHeapElement->uiCtxType;
1026 DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex);
1027
1028 return ctxType;
1029
1030 }
1031
1032 //!
1033 //! \brief Destroy image from VA image ID
1034 //!
1035 //! \param [in] mediaCtx
1036 //! Pointer to ddi media context
1037 //! \param [in] imageID
1038 //! VA image ID
1039 //!
1040 //! \return bool
1041 //! True if destroy image from VA image ID, else fail
1042 //!
1043 bool DdiMedia_DestroyImageFromVAImageID (PDDI_MEDIA_CONTEXT mediaCtx, VAImageID imageID)
1044 {
1045 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", false);
1046
1047 DdiMediaUtil_LockMutex(&mediaCtx->ImageMutex);
1048 DdiMediaUtil_ReleasePVAImageFromHeap(mediaCtx->pImageHeap, (uint32_t)imageID);
1049 mediaCtx->uiNumImages--;
1050 DdiMediaUtil_UnLockMutex(&mediaCtx->ImageMutex);
1051 return true;
1052 }
1053
1054 //!
1055 //! \brief Decompress internal media memory
1056 //!
1057 //! \param [in] mosCtx
1058 //! Pointer to mos context
1059 //! \param [in] osResource
1060 //! Pointer mos resource
1061 //!
1062 void DdiMedia_MediaMemoryDecompressInternal(PMOS_CONTEXT mosCtx, PMOS_RESOURCE osResource)
1063 {
1064 DDI_CHK_NULL(mosCtx, "nullptr mosCtx",);
1065 DDI_CHK_NULL(osResource, "nullptr osResource",);
1066 DDI_ASSERT(osResource);
1067
1068 MediaMemDecompBaseState *mediaMemDecompState = static_cast<MediaMemDecompBaseState*>(*mosCtx->ppMediaMemDecompState);
1069
1070 if (mosCtx->m_apoMosEnabled && !mediaMemDecompState)
1071 {
1072 DDI_CHK_NULL(mediaMemDecompState, "nullptr mediaMemDecompState", );
1073 }
1074
1075 if (!mediaMemDecompState)
1076 {
1077 mediaMemDecompState = static_cast<MediaMemDecompBaseState*>(MmdDevice::CreateFactory(mosCtx));
1078 *mosCtx->ppMediaMemDecompState = mediaMemDecompState;
1079 }
1080
1081 if (mediaMemDecompState)
1082 {
1083 mediaMemDecompState->MemoryDecompress(osResource);
1084 }
1085 else
1086 {
1087 DDI_ASSERTMESSAGE("Invalid memory decompression state.");
1088 }
1089 }
1090
1091 //!
1092 //! \brief copy internal media surface to another surface
1093 //!
1094 //! \param [in] mosCtx
1095 //! Pointer to mos context
1096 //! \param [in] inputOsResource
1097 //! Pointer input mos resource
1098 //! \param [in] outputOsResource
1099 //! Pointer output mos resource
1100 //! \param [in] boutputcompressed
1101 //! output can be compressed or not
1102 //!
1103 void DdiMedia_MediaMemoryCopyInternal(PMOS_CONTEXT mosCtx, PMOS_RESOURCE inputOsResource, PMOS_RESOURCE outputOsResource, bool boutputcompressed)
1104 {
1105 DDI_CHK_NULL(mosCtx, "nullptr mosCtx",);
1106 DDI_CHK_NULL(inputOsResource, "nullptr input osResource",);
1107 DDI_CHK_NULL(outputOsResource, "nullptr output osResource",);
1108 DDI_ASSERT(inputOsResource);
1109 DDI_ASSERT(outputOsResource);
1110
1111 MediaMemDecompBaseState *mediaMemDecompState = static_cast<MediaMemDecompBaseState*>(*mosCtx->ppMediaMemDecompState);
1112
1113 if (mosCtx->m_apoMosEnabled && !mediaMemDecompState)
1114 {
1115 DDI_CHK_NULL(mediaMemDecompState, "nullptr mediaMemDecompState", );
1116 }
1117
1118 if (!mediaMemDecompState)
1119 {
1120 mediaMemDecompState = static_cast<MediaMemDecompBaseState*>(MmdDevice::CreateFactory(mosCtx));
1121 *mosCtx->ppMediaMemDecompState = mediaMemDecompState;
1122 }
1123
1124 if (mediaMemDecompState)
1125 {
1126 mediaMemDecompState->MediaMemoryCopy(inputOsResource, outputOsResource, boutputcompressed);
1127 }
1128 else
1129 {
1130 DDI_ASSERTMESSAGE("Invalid memory decompression state.");
1131 }
1132 }
1133
1134 //!
1135 //! \brief copy internal media surface/buffer to another surface/buffer
1136 //!
1137 //! \param [in] mosCtx
1138 //! Pointer to mos context
1139 //! \param [in] inputOsResource
1140 //! Pointer input mos resource
1141 //! \param [in] outputOsResource
1142 //! Pointer output mos resource
1143 //! \param [in] boutputcompressed
1144 //! output can be compressed or not
1145 //! \param [in] copyWidth
1146 //! The 2D surface Width
1147 //! \param [in] copyHeight
1148 //! The 2D surface height
1149 //! \param [in] copyInputOffset
1150 //! The offset of copied surface from
1151 //! \param [in] copyOutputOffset
1152 //! The offset of copied to
1153 //!
1154 void DdiMedia_MediaMemoryCopy2DInternal(PMOS_CONTEXT mosCtx, PMOS_RESOURCE inputOsResource, PMOS_RESOURCE outputOsResource, uint32_t copyWidth, uint32_t copyHeight, uint32_t copyInputOffset, uint32_t copyOutputOffset, uint32_t bpp, bool boutputcompressed)
1155 {
1156 DDI_CHK_NULL(mosCtx, "nullptr mosCtx",);
1157 DDI_CHK_NULL(inputOsResource, "nullptr input osResource",);
1158 DDI_CHK_NULL(outputOsResource, "nullptr output osResource",);
1159 DDI_ASSERT(inputOsResource);
1160 DDI_ASSERT(outputOsResource);
1161
1162 MediaMemDecompBaseState *mediaMemDecompState = static_cast<MediaMemDecompBaseState*>(*mosCtx->ppMediaMemDecompState);
1163
1164 if (mosCtx->m_apoMosEnabled && !mediaMemDecompState)
1165 {
1166 DDI_CHK_NULL(mediaMemDecompState, "nullptr mediaMemDecompState", );
1167 }
1168
1169 if (!mediaMemDecompState)
1170 {
1171 mediaMemDecompState = static_cast<MediaMemDecompBaseState*>(MmdDevice::CreateFactory(mosCtx));
1172 *mosCtx->ppMediaMemDecompState = mediaMemDecompState;
1173 }
1174
1175 if (mediaMemDecompState)
1176 {
1177 mediaMemDecompState->MediaMemoryCopy2D(
1178 inputOsResource,
1179 outputOsResource,
1180 copyWidth,
1181 copyHeight,
1182 copyInputOffset,
1183 copyOutputOffset,
1184 bpp,
1185 boutputcompressed);
1186 }
1187 else
1188 {
1189 DDI_ASSERTMESSAGE("Invalid memory decompression state.");
1190 }
1191 }
1192
1193 //!
1194 //! \brief Tile/Linear format conversion for media surface/buffer
1195 //!
1196 //! \param [in] mosCtx
1197 //! Pointer to mos context
1198 //! \param [in] inputOsResource
1199 //! Pointer input mos resource
1200 //! \param [in] outputOsResource
1201 //! Pointer output mos resource
1202 //! \param [in] copyWidth
1203 //! The 2D surface Width
1204 //! \param [in] copyHeight
1205 //! The 2D surface height
1206 //! \param [in] copyInputOffset
1207 //! The offset of copied surface from
1208 //! \param [in] copyOutputOffset
1209 //! The offset of copied to
1210 //! \param [in] isTileToLinear
1211 //! Convertion direction, true: tile->linear, false: linear->tile
1212 //! \param [in] outputCompressed
1213 //! output can be compressed or not
1214 //!
1215 VAStatus DdiMedia_MediaMemoryTileConvertInternal(
1216 PMOS_CONTEXT mosCtx,
1217 PMOS_RESOURCE inputOsResource,
1218 PMOS_RESOURCE outputOsResource,
1219 uint32_t copyWidth,
1220 uint32_t copyHeight,
1221 uint32_t copyInputOffset,
1222 uint32_t copyOutputOffset,
1223 bool isTileToLinear,
1224 bool outputCompressed)
1225 {
1226 DDI_CHK_NULL(mosCtx, "nullptr mosCtx", VA_STATUS_ERROR_INVALID_PARAMETER);
1227 DDI_CHK_NULL(inputOsResource, "nullptr input osResource", VA_STATUS_ERROR_INVALID_PARAMETER);
1228 DDI_CHK_NULL(outputOsResource, "nullptr output osResource", VA_STATUS_ERROR_INVALID_PARAMETER);
1229 DDI_ASSERT(inputOsResource);
1230 DDI_ASSERT(outputOsResource);
1231
1232 VAStatus vaStatus = VA_STATUS_SUCCESS;
1233
1234 MediaMemDecompBaseState *mediaMemDecompState = static_cast<MediaMemDecompBaseState*>(*mosCtx->ppMediaMemDecompState);
1235
1236 if (mosCtx->m_apoMosEnabled && !mediaMemDecompState)
1237 {
1238 DDI_CHK_NULL(mediaMemDecompState, "nullptr mediaMemDecompState", VA_STATUS_ERROR_INVALID_PARAMETER);
1239 }
1240
1241 if (!mediaMemDecompState)
1242 {
1243 mediaMemDecompState = static_cast<MediaMemDecompBaseState*>(MmdDevice::CreateFactory(mosCtx));
1244 *mosCtx->ppMediaMemDecompState = mediaMemDecompState;
1245 }
1246
1247 DDI_CHK_NULL(mediaMemDecompState, "Invalid memory decompression state", VA_STATUS_ERROR_INVALID_PARAMETER);
1248
1249 MOS_STATUS mosStatus = mediaMemDecompState->MediaMemoryTileConvert(
1250 inputOsResource,
1251 outputOsResource,
1252 copyWidth,
1253 copyHeight,
1254 copyInputOffset,
1255 copyOutputOffset,
1256 isTileToLinear,
1257 outputCompressed);
1258 if (mosStatus != MOS_STATUS_SUCCESS)
1259 {
1260 vaStatus = VA_STATUS_ERROR_UNKNOWN;
1261 }
1262
1263 return vaStatus;
1264 }
1265
1266 //!
1267 //! \brief Decompress a compressed surface.
1268 //!
1269 //! \param [in] mediaCtx
1270 //! Pointer to ddi media context
1271 //! \param [in] mediaSurface
1272 //! Ddi media surface
1273 //!
1274 //! \return VAStatus
1275 //! VA_STATUS_SUCCESS if success, else fail reason
1276 //!
1277 VAStatus DdiMedia_MediaMemoryDecompress(PDDI_MEDIA_CONTEXT mediaCtx, DDI_MEDIA_SURFACE *mediaSurface)
1278 {
1279 DDI_CHK_NULL(mediaCtx, "Null mediaCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
1280 DDI_CHK_NULL(mediaSurface, "nullptr mediaSurface", VA_STATUS_ERROR_INVALID_PARAMETER);
1281 DDI_CHK_NULL(mediaSurface->pGmmResourceInfo, "nullptr mediaSurface->pGmmResourceInfo", VA_STATUS_ERROR_INVALID_PARAMETER);
1282
1283 VAStatus vaStatus = VA_STATUS_SUCCESS;
1284 GMM_RESOURCE_FLAG GmmFlags;
1285
1286 MOS_ZeroMemory(&GmmFlags, sizeof(GmmFlags));
1287 GmmFlags = mediaSurface->pGmmResourceInfo->GetResFlags();
1288
1289 if (((GmmFlags.Gpu.MMC ||
1290 GmmFlags.Gpu.CCS) &&
1291 GmmFlags.Info.MediaCompressed) ||
1292 mediaSurface->pGmmResourceInfo->IsMediaMemoryCompressed(0))
1293 {
1294 MOS_CONTEXT mosCtx = {};
1295 MOS_RESOURCE surface;
1296 DdiCpInterface *pCpDdiInterface;
1297
1298 MOS_ZeroMemory(&surface, sizeof(surface));
1299
1300 mosCtx.bufmgr = mediaCtx->pDrmBufMgr;
1301 mosCtx.m_gpuContextMgr = mediaCtx->m_gpuContextMgr;
1302 mosCtx.m_cmdBufMgr = mediaCtx->m_cmdBufMgr;
1303 mosCtx.fd = mediaCtx->fd;
1304 mosCtx.iDeviceId = mediaCtx->iDeviceId;
1305 mosCtx.m_skuTable = mediaCtx->SkuTable;
1306 mosCtx.m_waTable = mediaCtx->WaTable;
1307 mosCtx.m_gtSystemInfo = *mediaCtx->pGtSystemInfo;
1308 mosCtx.m_platform = mediaCtx->platform;
1309
1310 mosCtx.ppMediaMemDecompState = &mediaCtx->pMediaMemDecompState;
1311 mosCtx.pfnMemoryDecompress = mediaCtx->pfnMemoryDecompress;
1312 mosCtx.pfnMediaMemoryCopy = mediaCtx->pfnMediaMemoryCopy;
1313 mosCtx.pfnMediaMemoryCopy2D = mediaCtx->pfnMediaMemoryCopy2D;
1314 mosCtx.m_gtSystemInfo = *mediaCtx->pGtSystemInfo;
1315 mosCtx.m_auxTableMgr = mediaCtx->m_auxTableMgr;
1316 mosCtx.pGmmClientContext = mediaCtx->pGmmClientContext;
1317
1318 mosCtx.m_osDeviceContext = mediaCtx->m_osDeviceContext;
1319 mosCtx.m_apoMosEnabled = mediaCtx->m_apoMosEnabled;
1320 mosCtx.m_userSettingPtr = mediaCtx->m_userSettingPtr;
1321
1322 pCpDdiInterface = Create_DdiCpInterface(mosCtx);
1323
1324 if (nullptr == pCpDdiInterface)
1325 {
1326 vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
1327 }
1328 else
1329 {
1330 DdiMediaUtil_LockMutex(&mediaCtx->SurfaceMutex);
1331 DdiMediaUtil_LockMutex(&mediaCtx->MemDecompMutex);
1332
1333 DdiMedia_MediaSurfaceToMosResource(mediaSurface, &surface);
1334 DdiMedia_MediaMemoryDecompressInternal(&mosCtx, &surface);
1335
1336 DdiMediaUtil_UnLockMutex(&mediaCtx->MemDecompMutex);
1337 DdiMediaUtil_UnLockMutex(&mediaCtx->SurfaceMutex);
1338
1339 if (pCpDdiInterface)
1340 {
1341 Delete_DdiCpInterface(pCpDdiInterface);
1342 pCpDdiInterface = NULL;
1343 }
1344 }
1345 }
1346
1347 return vaStatus;
1348 }
1349
1350 /*
1351 * Initialize the library
1352 */
1353
1354 // Global mutex
1355 MEDIA_MUTEX_T GlobalMutex = MEDIA_MUTEX_INITIALIZER;
1356
1357 //!
1358 //! \brief Initialize
1359 //!
1360 //! \param [in] mediaCtx
1361 //! Pointer to DDI media driver context
1362 //!
1363 //! \return VAStatus
1364 //! VA_STATUS_SUCCESS if success, else fail reason
1365 //!
1366 static VAStatus DdiMedia_HeapInitialize(
1367 PDDI_MEDIA_CONTEXT mediaCtx)
1368 {
1369 DDI_CHK_NULL(mediaCtx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
1370
1371 // Heap initialization here
1372 mediaCtx->pSurfaceHeap = (DDI_MEDIA_HEAP *)MOS_AllocAndZeroMemory(sizeof(DDI_MEDIA_HEAP));
1373 DDI_CHK_NULL(mediaCtx->pSurfaceHeap, "nullptr pSurfaceHeap", VA_STATUS_ERROR_ALLOCATION_FAILED);
1374 mediaCtx->pSurfaceHeap->uiHeapElementSize = sizeof(DDI_MEDIA_SURFACE_HEAP_ELEMENT);
1375
1376 mediaCtx->pBufferHeap = (DDI_MEDIA_HEAP *)MOS_AllocAndZeroMemory(sizeof(DDI_MEDIA_HEAP));
1377 DDI_CHK_NULL(mediaCtx->pBufferHeap, "nullptr BufferHeap", VA_STATUS_ERROR_ALLOCATION_FAILED);
1378 mediaCtx->pBufferHeap->uiHeapElementSize = sizeof(DDI_MEDIA_BUFFER_HEAP_ELEMENT);
1379
1380 mediaCtx->pImageHeap = (DDI_MEDIA_HEAP *)MOS_AllocAndZeroMemory(sizeof(DDI_MEDIA_HEAP));
1381 DDI_CHK_NULL(mediaCtx->pImageHeap, "nullptr ImageHeap", VA_STATUS_ERROR_ALLOCATION_FAILED);
1382 mediaCtx->pImageHeap->uiHeapElementSize = sizeof(DDI_MEDIA_IMAGE_HEAP_ELEMENT);
1383
1384 mediaCtx->pDecoderCtxHeap = (DDI_MEDIA_HEAP *)MOS_AllocAndZeroMemory(sizeof(DDI_MEDIA_HEAP));
1385 DDI_CHK_NULL(mediaCtx->pDecoderCtxHeap, "nullptr DecoderCtxHeap", VA_STATUS_ERROR_ALLOCATION_FAILED);
1386 mediaCtx->pDecoderCtxHeap->uiHeapElementSize = sizeof(DDI_MEDIA_VACONTEXT_HEAP_ELEMENT);
1387
1388 mediaCtx->pEncoderCtxHeap = (DDI_MEDIA_HEAP *)MOS_AllocAndZeroMemory(sizeof(DDI_MEDIA_HEAP));
1389 DDI_CHK_NULL(mediaCtx->pEncoderCtxHeap, "nullptr EncoderCtxHeap", VA_STATUS_ERROR_ALLOCATION_FAILED);
1390 mediaCtx->pEncoderCtxHeap->uiHeapElementSize = sizeof(DDI_MEDIA_VACONTEXT_HEAP_ELEMENT);
1391
1392 mediaCtx->pVpCtxHeap = (DDI_MEDIA_HEAP *)MOS_AllocAndZeroMemory(sizeof(DDI_MEDIA_HEAP));
1393 DDI_CHK_NULL(mediaCtx->pVpCtxHeap, "nullptr VpCtxHeap", VA_STATUS_ERROR_ALLOCATION_FAILED);
1394 mediaCtx->pVpCtxHeap->uiHeapElementSize = sizeof(DDI_MEDIA_VACONTEXT_HEAP_ELEMENT);
1395
1396 mediaCtx->pProtCtxHeap = (DDI_MEDIA_HEAP *)MOS_AllocAndZeroMemory(sizeof(DDI_MEDIA_HEAP));
1397 DDI_CHK_NULL(mediaCtx->pProtCtxHeap, "nullptr pProtCtxHeap", VA_STATUS_ERROR_ALLOCATION_FAILED);
1398 mediaCtx->pProtCtxHeap->uiHeapElementSize = sizeof(DDI_MEDIA_VACONTEXT_HEAP_ELEMENT);
1399
1400 mediaCtx->pCmCtxHeap = (DDI_MEDIA_HEAP *)MOS_AllocAndZeroMemory(sizeof(DDI_MEDIA_HEAP));
1401 DDI_CHK_NULL(mediaCtx->pCmCtxHeap, "nullptr CmCtxHeap", VA_STATUS_ERROR_ALLOCATION_FAILED);
1402 mediaCtx->pCmCtxHeap->uiHeapElementSize = sizeof(DDI_MEDIA_VACONTEXT_HEAP_ELEMENT);
1403
1404 mediaCtx->pMfeCtxHeap = (DDI_MEDIA_HEAP *)MOS_AllocAndZeroMemory(sizeof(DDI_MEDIA_HEAP));
1405 DDI_CHK_NULL(mediaCtx->pMfeCtxHeap, "nullptr MfeCtxHeap", VA_STATUS_ERROR_ALLOCATION_FAILED);
1406 mediaCtx->pMfeCtxHeap->uiHeapElementSize = sizeof(DDI_MEDIA_VACONTEXT_HEAP_ELEMENT);
1407
1408 // init the mutexs
1409 DdiMediaUtil_InitMutex(&mediaCtx->SurfaceMutex);
1410 DdiMediaUtil_InitMutex(&mediaCtx->BufferMutex);
1411 DdiMediaUtil_InitMutex(&mediaCtx->ImageMutex);
1412 DdiMediaUtil_InitMutex(&mediaCtx->DecoderMutex);
1413 DdiMediaUtil_InitMutex(&mediaCtx->EncoderMutex);
1414 DdiMediaUtil_InitMutex(&mediaCtx->VpMutex);
1415 DdiMediaUtil_InitMutex(&mediaCtx->ProtMutex);
1416 DdiMediaUtil_InitMutex(&mediaCtx->CmMutex);
1417 DdiMediaUtil_InitMutex(&mediaCtx->MfeMutex);
1418
1419 return VA_STATUS_SUCCESS;
1420 }
1421
1422 //!
1423 //! \brief Initialize
1424 //!
1425 //! \param [in] mediaCtx
1426 //! Pointer to DDI media driver context
1427 //!
1428 //! \return VAStatus
1429 //! VA_STATUS_SUCCESS if success, else fail reason
1430 //!
1431 static VAStatus DdiMedia_HeapDestroy(
1432 PDDI_MEDIA_CONTEXT mediaCtx)
1433 {
1434 DDI_CHK_NULL(mediaCtx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
1435 // destroy heaps
1436 MOS_FreeMemory(mediaCtx->pSurfaceHeap->pHeapBase);
1437 MOS_FreeMemory(mediaCtx->pSurfaceHeap);
1438
1439 MOS_FreeMemory(mediaCtx->pBufferHeap->pHeapBase);
1440 MOS_FreeMemory(mediaCtx->pBufferHeap);
1441
1442 MOS_FreeMemory(mediaCtx->pImageHeap->pHeapBase);
1443 MOS_FreeMemory(mediaCtx->pImageHeap);
1444
1445 MOS_FreeMemory(mediaCtx->pDecoderCtxHeap->pHeapBase);
1446 MOS_FreeMemory(mediaCtx->pDecoderCtxHeap);
1447
1448 MOS_FreeMemory(mediaCtx->pEncoderCtxHeap->pHeapBase);
1449 MOS_FreeMemory(mediaCtx->pEncoderCtxHeap);
1450
1451 MOS_FreeMemory(mediaCtx->pVpCtxHeap->pHeapBase);
1452 MOS_FreeMemory(mediaCtx->pVpCtxHeap);
1453
1454 MOS_FreeMemory(mediaCtx->pProtCtxHeap->pHeapBase);
1455 MOS_FreeMemory(mediaCtx->pProtCtxHeap);
1456
1457 MOS_FreeMemory(mediaCtx->pCmCtxHeap->pHeapBase);
1458 MOS_FreeMemory(mediaCtx->pCmCtxHeap);
1459
1460 MOS_FreeMemory(mediaCtx->pMfeCtxHeap->pHeapBase);
1461 MOS_FreeMemory(mediaCtx->pMfeCtxHeap);
1462 // destroy the mutexs
1463 DdiMediaUtil_DestroyMutex(&mediaCtx->SurfaceMutex);
1464 DdiMediaUtil_DestroyMutex(&mediaCtx->BufferMutex);
1465 DdiMediaUtil_DestroyMutex(&mediaCtx->ImageMutex);
1466 DdiMediaUtil_DestroyMutex(&mediaCtx->DecoderMutex);
1467 DdiMediaUtil_DestroyMutex(&mediaCtx->EncoderMutex);
1468 DdiMediaUtil_DestroyMutex(&mediaCtx->VpMutex);
1469 DdiMediaUtil_DestroyMutex(&mediaCtx->ProtMutex);
1470 DdiMediaUtil_DestroyMutex(&mediaCtx->CmMutex);
1471 DdiMediaUtil_DestroyMutex(&mediaCtx->MfeMutex);
1472
1473 //resource checking
1474 if (mediaCtx->uiNumSurfaces != 0)
1475 {
1476 DDI_ASSERTMESSAGE("APP does not destroy all the surfaces.");
1477 }
1478 if (mediaCtx->uiNumBufs != 0)
1479 {
1480 DDI_ASSERTMESSAGE("APP does not destroy all the buffers.");
1481 }
1482 if (mediaCtx->uiNumImages != 0)
1483 {
1484 DDI_ASSERTMESSAGE("APP does not destroy all the images.");
1485 }
1486 if (mediaCtx->uiNumDecoders != 0)
1487 {
1488 DDI_ASSERTMESSAGE("APP does not destroy all the decoders.");
1489 }
1490 if (mediaCtx->uiNumEncoders != 0)
1491 {
1492 DDI_ASSERTMESSAGE("APP does not destroy all the encoders.");
1493 }
1494 if (mediaCtx->uiNumVPs != 0)
1495 {
1496 DDI_ASSERTMESSAGE("APP does not destroy all the VPs.");
1497 }
1498 if (mediaCtx->uiNumProts != 0)
1499 {
1500 DDI_ASSERTMESSAGE("APP does not destroy all the Prots.");
1501 }
1502 if (mediaCtx->uiNumCMs != 0)
1503 {
1504 DDI_ASSERTMESSAGE("APP does not destroy all the CMs.");
1505 }
1506 return VA_STATUS_SUCCESS;
1507 }
1508
1509 //!
1510 //! \brief Free for media context
1511 //!
1512 //! \param [in] mediaCtx
1513 //! Pointer to ddi media context
1514 //!
1515 void FreeForMediaContext(PDDI_MEDIA_CONTEXT mediaCtx)
1516 {
1517 DdiMediaUtil_UnLockMutex(&GlobalMutex);
1518
1519 if (mediaCtx)
1520 {
1521 mediaCtx->SkuTable.reset();
1522 mediaCtx->WaTable.reset();
1523 MOS_FreeMemory(mediaCtx->pSurfaceHeap);
1524 MOS_FreeMemory(mediaCtx->pBufferHeap);
1525 MOS_FreeMemory(mediaCtx->pImageHeap);
1526 MOS_FreeMemory(mediaCtx->pDecoderCtxHeap);
1527 MOS_FreeMemory(mediaCtx->pEncoderCtxHeap);
1528 MOS_FreeMemory(mediaCtx->pVpCtxHeap);
1529 MOS_FreeMemory(mediaCtx->pProtCtxHeap);
1530 MOS_FreeMemory(mediaCtx->pMfeCtxHeap);
1531 mediaCtx->m_userSettingPtr.reset();
1532 MOS_Delete(mediaCtx);
1533 }
1534
1535 return;
1536 }
1537
1538 void DestroyMediaContextMutex(PDDI_MEDIA_CONTEXT mediaCtx)
1539 {
1540 // destroy the mutexs
1541 DdiMediaUtil_DestroyMutex(&mediaCtx->SurfaceMutex);
1542 DdiMediaUtil_DestroyMutex(&mediaCtx->BufferMutex);
1543 DdiMediaUtil_DestroyMutex(&mediaCtx->ImageMutex);
1544 DdiMediaUtil_DestroyMutex(&mediaCtx->DecoderMutex);
1545 DdiMediaUtil_DestroyMutex(&mediaCtx->EncoderMutex);
1546 DdiMediaUtil_DestroyMutex(&mediaCtx->VpMutex);
1547 DdiMediaUtil_DestroyMutex(&mediaCtx->CmMutex);
1548 DdiMediaUtil_DestroyMutex(&mediaCtx->MfeMutex);
1549 #if !defined(ANDROID) && defined(X11_FOUND)
1550 DdiMediaUtil_DestroyMutex(&mediaCtx->PutSurfaceRenderMutex);
1551 DdiMediaUtil_DestroyMutex(&mediaCtx->PutSurfaceSwapBufferMutex);
1552 #endif
1553 return;
1554 }
1555
1556 VAStatus DdiMedia_GetDeviceFD (
1557 VADriverContextP ctx,
1558 int32_t *pDevicefd
1559 )
1560 {
1561 struct drm_state *pDRMState = (struct drm_state *)ctx->drm_state;
1562 DDI_CHK_NULL(pDRMState, "nullptr pDRMState", VA_STATUS_ERROR_INVALID_CONTEXT);
1563 DDI_CHK_NULL(pDevicefd, "nullptr pDevicefd", VA_STATUS_ERROR_INVALID_CONTEXT);
1564
1565 // If libva failes to open the graphics card, try to open it again within Media Driver
1566 if(pDRMState->fd < 0)
1567 {
1568 DDI_ASSERTMESSAGE("DDI:LIBVA Wrapper doesn't pass file descriptor for graphics adaptor, trying to open the graphics... ");
1569 pDRMState->fd = DdiMediaUtil_OpenGraphicsAdaptor((char *)DEVICE_NAME);
1570 if (pDRMState->fd < 0) {
1571 DDI_ASSERTMESSAGE("DDI: Still failed to open the graphic adaptor, return failure");
1572 return VA_STATUS_ERROR_INVALID_PARAMETER;
1573 }
1574 }
1575 *pDevicefd = pDRMState->fd;
1576
1577 return VA_STATUS_SUCCESS;
1578 }
1579
1580 VAStatus DdiMedia_CleanUpSoftlet(PDDI_MEDIA_CONTEXT mediaCtx)
1581 {
1582 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
1583
1584 if (mediaCtx->m_capsNext)
1585 {
1586 MOS_Delete(mediaCtx->m_capsNext);
1587 mediaCtx->m_capsNext = nullptr;
1588 }
1589
1590 MediaLibvaInterfaceNext::ReleaseCompList(mediaCtx);
1591 if(mediaCtx->m_hwInfo)
1592 {
1593 MOS_Delete(mediaCtx->m_hwInfo);
1594 mediaCtx->m_hwInfo = nullptr;
1595 }
1596
1597 return VA_STATUS_SUCCESS;
1598 }
1599
1600 VAStatus DdiMedia__InitializeSoftlet(
1601 PDDI_MEDIA_CONTEXT mediaCtx,
1602 bool apoDdiEnabled)
1603 {
1604 VAStatus status = VA_STATUS_SUCCESS;
1605
1606 if (nullptr == mediaCtx)
1607 {
1608 FreeForMediaContext(mediaCtx);
1609 return VA_STATUS_ERROR_ALLOCATION_FAILED;
1610 }
1611
1612 do
1613 {
1614 if(apoDdiEnabled)
1615 {
1616 mediaCtx->m_hwInfo = MediaInterfacesHwInfoDevice::CreateFactory(mediaCtx->platform);
1617 if (nullptr == mediaCtx->m_hwInfo)
1618 {
1619 DDI_ASSERTMESSAGE("Unregister hwinfo platform.");
1620 status = VA_STATUS_ERROR_ALLOCATION_FAILED;
1621 break;
1622 }
1623
1624 mediaCtx->m_capsNext = MediaLibvaCapsNext::CreateCaps(mediaCtx);
1625 if (!mediaCtx->m_capsNext)
1626 {
1627 DDI_ASSERTMESSAGE("Caps next create failed. Not supported GFX device.");
1628 status = VA_STATUS_ERROR_ALLOCATION_FAILED;
1629 break;
1630 }
1631
1632 if(mediaCtx->m_capsNext->Init() != VA_STATUS_SUCCESS)
1633 {
1634 DDI_ASSERTMESSAGE("Caps next init failed.");
1635 status = VA_STATUS_ERROR_ALLOCATION_FAILED;
1636 break;
1637 }
1638
1639 if (MediaLibvaInterfaceNext::InitCompList(mediaCtx) != VA_STATUS_SUCCESS)
1640 {
1641 DDI_ASSERTMESSAGE("Init CompList failed. Not supported GFX device.");
1642 status = VA_STATUS_ERROR_ALLOCATION_FAILED;
1643 break;
1644 }
1645 }
1646 } while(false);
1647
1648 if (status != VA_STATUS_SUCCESS)
1649 {
1650 DdiMedia_CleanUp(mediaCtx);
1651 DestroyMediaContextMutex(mediaCtx);
1652 FreeForMediaContext(mediaCtx);
1653 }
1654
1655 return status;
1656 }
1657
1658 VAStatus DdiMedia__Initialize (
1659 VADriverContextP ctx,
1660 int32_t *major_version,
1661 int32_t *minor_version
1662 )
1663 {
1664 #if !defined(ANDROID) && defined(X11_FOUND)
1665 // ATRACE code in <cutils/trace.h> started from KitKat, version = 440
1666 // ENABLE_TRACE is defined only for eng build so release build won't leak perf data
1667 // thus trace code enabled only on KitKat (and newer) && eng build
1668 #if ANDROID_VERSION > 439 && defined(ENABLE_ATRACE)
1669 char switch_value[PROPERTY_VALUE_MAX];
1670
1671 property_get("debug.DdiCodec_.umd", switch_value, "0");
1672 atrace_switch = atoi(switch_value);
1673 #endif
1674 #endif
1675
1676 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
1677
1678 VAStatus status = VA_STATUS_SUCCESS;
1679 bool apoDdiEnabled = false;
1680 int32_t devicefd = 0;
1681 if(DdiMedia_GetDeviceFD(ctx, &devicefd) != VA_STATUS_SUCCESS)
1682 {
1683 DDI_ASSERTMESSAGE("Unable to get device FD");
1684 return VA_STATUS_ERROR_ALLOCATION_FAILED;
1685 }
1686
1687 status = DdiMedia_InitMediaContext(ctx, devicefd, major_version, minor_version, apoDdiEnabled);
1688 if(status != VA_STATUS_SUCCESS)
1689 {
1690 DDI_ASSERTMESSAGE("Failed to init media context");
1691 return status;
1692 }
1693
1694 if(!apoDdiEnabled)
1695 {
1696 if(DdiMedia_LoadFuncion(ctx) != VA_STATUS_SUCCESS)
1697 {
1698 DDI_ASSERTMESSAGE("Failed to load function pointer");
1699 return VA_STATUS_ERROR_ALLOCATION_FAILED;
1700 }
1701 }
1702 else
1703 {
1704 if(MediaLibvaInterface::LoadFunction(ctx) != VA_STATUS_SUCCESS)
1705 {
1706 DDI_ASSERTMESSAGE("Failed to load function pointer");
1707 return VA_STATUS_ERROR_ALLOCATION_FAILED;
1708 }
1709 }
1710
1711 return status;
1712 }
1713
1714 VAStatus DdiMedia_InitMediaContext (
1715 VADriverContextP ctx,
1716 int32_t devicefd,
1717 int32_t *major_version,
1718 int32_t *minor_version,
1719 bool &apoDdiEnabled
1720 )
1721 {
1722 DDI_FUNCTION_ENTER();
1723
1724 if(major_version)
1725 {
1726 *major_version = VA_MAJOR_VERSION;
1727 }
1728
1729 if(minor_version)
1730 {
1731 *minor_version = VA_MINOR_VERSION;
1732 }
1733
1734 DdiMediaUtil_LockMutex(&GlobalMutex);
1735 // media context is already created, return directly to support multiple entry
1736 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
1737 if(mediaCtx)
1738 {
1739 FreeForMediaContext(mediaCtx);
1740 }
1741
1742 mediaCtx = DdiMedia_CreateMediaDriverContext();
1743 if (nullptr == mediaCtx)
1744 {
1745 FreeForMediaContext(mediaCtx);
1746 return VA_STATUS_ERROR_ALLOCATION_FAILED;
1747 }
1748 mediaCtx->uiRef++;
1749 ctx->pDriverData = (void *)mediaCtx;
1750 mediaCtx->fd = devicefd;
1751
1752 mediaCtx->m_userSettingPtr = std::make_shared<MediaUserSetting::MediaUserSetting>();
1753
1754 MOS_CONTEXT mosCtx = {};
1755 mosCtx.fd = mediaCtx->fd;
1756 mosCtx.m_userSettingPtr = mediaCtx->m_userSettingPtr;
1757
1758 MosInterface::InitOsUtilities(&mosCtx);
1759 mediaCtx->m_apoMosEnabled = SetupApoMosSwitch(devicefd, mediaCtx->m_userSettingPtr);
1760
1761 mediaCtx->pfnMemoryDecompress = DdiMedia_MediaMemoryDecompressInternal;
1762 mediaCtx->pfnMediaMemoryCopy = DdiMedia_MediaMemoryCopyInternal;
1763 mediaCtx->pfnMediaMemoryCopy2D = DdiMedia_MediaMemoryCopy2DInternal;
1764 mediaCtx->pfnMediaMemoryTileConvert = DdiMedia_MediaMemoryTileConvertInternal;
1765
1766 mediaCtx->modularizedGpuCtxEnabled = true;
1767
1768 if (mediaCtx->m_apoMosEnabled)
1769 {
1770 mosCtx.fd = mediaCtx->fd;
1771 mosCtx.m_apoMosEnabled = mediaCtx->m_apoMosEnabled;
1772
1773 MosOcaInterfaceSpecific::InitInterface(&mosCtx);
1774
1775 mediaCtx->pGtSystemInfo = (MEDIA_SYSTEM_INFO *)MOS_AllocAndZeroMemory(sizeof(MEDIA_SYSTEM_INFO));
1776 if (nullptr == mediaCtx->pGtSystemInfo)
1777 {
1778 FreeForMediaContext(mediaCtx);
1779 return VA_STATUS_ERROR_ALLOCATION_FAILED;
1780 }
1781
1782 if (MosInterface::CreateOsDeviceContext(&mosCtx, &mediaCtx->m_osDeviceContext) != MOS_STATUS_SUCCESS)
1783 {
1784 DDI_ASSERTMESSAGE("Unable to create MOS device context.");
1785 FreeForMediaContext(mediaCtx);
1786 return VA_STATUS_ERROR_OPERATION_FAILED;
1787 }
1788 mediaCtx->pDrmBufMgr = mosCtx.bufmgr;
1789 mediaCtx->iDeviceId = mosCtx.iDeviceId;
1790 mediaCtx->SkuTable = mosCtx.m_skuTable;
1791 mediaCtx->WaTable = mosCtx.m_waTable;
1792 *mediaCtx->pGtSystemInfo = mosCtx.m_gtSystemInfo;
1793 mediaCtx->platform = mosCtx.m_platform;
1794 mediaCtx->m_auxTableMgr = mosCtx.m_auxTableMgr;
1795 mediaCtx->pGmmClientContext = mosCtx.pGmmClientContext;
1796 mediaCtx->m_useSwSwizzling = mosCtx.bUseSwSwizzling;
1797 mediaCtx->m_tileYFlag = mosCtx.bTileYFlag;
1798 mediaCtx->bIsAtomSOC = mosCtx.bIsAtomSOC;
1799 mediaCtx->perfData = mosCtx.pPerfData;
1800
1801 if (mosCtx.ppMediaMemDecompState == nullptr)
1802 {
1803 DDI_ASSERTMESSAGE("media decomp state is null.");
1804 FreeForMediaContext(mediaCtx);
1805 return VA_STATUS_ERROR_OPERATION_FAILED;
1806 }
1807 mediaCtx->pMediaMemDecompState = *mosCtx.ppMediaMemDecompState;
1808
1809 mediaCtx->pMediaCopyState = *mosCtx.ppMediaCopyState;
1810 }
1811 else if (mediaCtx->modularizedGpuCtxEnabled)
1812 {
1813 mediaCtx->pDrmBufMgr = mos_bufmgr_gem_init(mediaCtx->fd, DDI_CODEC_BATCH_BUFFER_SIZE);
1814 if (nullptr == mediaCtx->pDrmBufMgr)
1815 {
1816 DDI_ASSERTMESSAGE("DDI:No able to allocate buffer manager, fd=0x%d", mediaCtx->fd);
1817 FreeForMediaContext(mediaCtx);
1818 return VA_STATUS_ERROR_INVALID_PARAMETER;
1819 }
1820 mos_bufmgr_enable_reuse(mediaCtx->pDrmBufMgr);
1821
1822 //Latency reducation:replace HWGetDeviceID to get device using ioctl from drm.
1823 mediaCtx->iDeviceId = mos_bufmgr_get_devid(mediaCtx->pDrmBufMgr);
1824
1825 //TO--DO, apo set it to FALSE by default, to remove the logic in apo mos controlled by it????
1826 mediaCtx->bIsAtomSOC = IS_ATOMSOC(mediaCtx->iDeviceId);
1827
1828 MEDIA_FEATURE_TABLE *skuTable = &mediaCtx->SkuTable;
1829 MEDIA_WA_TABLE * waTable = &mediaCtx->WaTable;
1830 skuTable->reset();
1831 waTable->reset();
1832 // get Sku/Wa tables and platform information
1833 PLATFORM platform = {};
1834 // Allocate memory for Media System Info
1835 mediaCtx->pGtSystemInfo = (MEDIA_SYSTEM_INFO *)MOS_AllocAndZeroMemory(sizeof(MEDIA_SYSTEM_INFO));
1836 if (nullptr == mediaCtx->pGtSystemInfo)
1837 {
1838 FreeForMediaContext(mediaCtx);
1839 return VA_STATUS_ERROR_ALLOCATION_FAILED;
1840 }
1841 MOS_STATUS eStatus = HWInfo_GetGfxInfo(mediaCtx->fd, mediaCtx->pDrmBufMgr, &platform, skuTable, waTable, mediaCtx->pGtSystemInfo, mediaCtx->m_userSettingPtr);
1842 if (MOS_STATUS_SUCCESS != eStatus)
1843 {
1844 DDI_ASSERTMESSAGE("Fatal error - unsuccesfull Sku/Wa/GtSystemInfo initialization");
1845 FreeForMediaContext(mediaCtx);
1846 return VA_STATUS_ERROR_OPERATION_FAILED;
1847 }
1848 mediaCtx->platform = platform;
1849
1850 MosUtilities::MosTraceSetupInfo(
1851 (VA_MAJOR_VERSION << 16) | VA_MINOR_VERSION,
1852 platform.eProductFamily,
1853 platform.eRenderCoreFamily,
1854 (platform.usRevId << 16) | platform.usDeviceID);
1855
1856 //TO--DO, gen12+ still need this wa????, not porting yet
1857 if (MEDIA_IS_SKU(skuTable, FtrEnableMediaKernels) == 0)
1858 {
1859 MEDIA_WR_WA(waTable, WaHucStreamoutOnlyDisable, 0);
1860 }
1861
1862 GMM_SKU_FEATURE_TABLE gmmSkuTable;
1863 memset(&gmmSkuTable, 0, sizeof(gmmSkuTable));
1864
1865 GMM_WA_TABLE gmmWaTable;
1866 memset(&gmmWaTable, 0, sizeof(gmmWaTable));
1867
1868 GMM_GT_SYSTEM_INFO gmmGtInfo;
1869 memset(&gmmGtInfo, 0, sizeof(gmmGtInfo));
1870
1871 GMM_ADAPTER_BDF gmmAdapterBDF;
1872 memset(&gmmAdapterBDF, 0, sizeof(gmmAdapterBDF));
1873
1874 eStatus = HWInfo_GetGmmInfo(mediaCtx->pDrmBufMgr, &gmmSkuTable, &gmmWaTable, &gmmGtInfo);
1875 if (MOS_STATUS_SUCCESS != eStatus)
1876 {
1877 DDI_ASSERTMESSAGE("Fatal error - unsuccesfull Gmm Sku/Wa/GtSystemInfo initialization");
1878 FreeForMediaContext(mediaCtx);
1879 return VA_STATUS_ERROR_OPERATION_FAILED;
1880 }
1881
1882 eStatus = Mos_Solo_DdiInitializeDeviceId(
1883 (void *)mediaCtx->pDrmBufMgr,
1884 &mediaCtx->SkuTable,
1885 &mediaCtx->WaTable,
1886 &gmmSkuTable,
1887 &gmmWaTable,
1888 &gmmGtInfo,
1889 &mediaCtx->iDeviceId,
1890 &mediaCtx->fd,
1891 &mediaCtx->platform,
1892 mediaCtx->m_userSettingPtr);
1893 if (eStatus != MOS_STATUS_SUCCESS)
1894 {
1895 FreeForMediaContext(mediaCtx);
1896 return VA_STATUS_ERROR_OPERATION_FAILED;
1897 }
1898
1899 // fill in the mos context struct as input to initialize m_osContext
1900 mosCtx.bufmgr = mediaCtx->pDrmBufMgr;
1901 mosCtx.fd = mediaCtx->fd;
1902 mosCtx.iDeviceId = mediaCtx->iDeviceId;
1903 mosCtx.m_skuTable = mediaCtx->SkuTable;
1904 mosCtx.m_waTable = mediaCtx->WaTable;
1905 mosCtx.m_gtSystemInfo = *mediaCtx->pGtSystemInfo;
1906 mosCtx.m_platform = mediaCtx->platform;
1907 mosCtx.ppMediaMemDecompState = &mediaCtx->pMediaMemDecompState;
1908 mosCtx.pfnMemoryDecompress = mediaCtx->pfnMemoryDecompress;
1909 mosCtx.pfnMediaMemoryCopy = mediaCtx->pfnMediaMemoryCopy;
1910 mosCtx.pfnMediaMemoryCopy2D = mediaCtx->pfnMediaMemoryCopy2D;
1911 mosCtx.ppMediaCopyState = &mediaCtx->pMediaCopyState;
1912
1913 eStatus = MosInterface::GetAdapterBDF(&mosCtx, &gmmAdapterBDF);
1914 if (MOS_STATUS_SUCCESS != eStatus)
1915 {
1916 DDI_ASSERTMESSAGE("Fatal error - unsuccesfull Gmm Adapter BDF initialization");
1917 FreeForMediaContext(mediaCtx);
1918 return VA_STATUS_ERROR_OPERATION_FAILED;
1919 }
1920
1921 // Initialize Gmm context
1922 GMM_INIT_IN_ARGS gmmInitAgrs = {};
1923 GMM_INIT_OUT_ARGS gmmOutArgs = {};
1924 gmmInitAgrs.Platform = mediaCtx->platform;
1925 gmmInitAgrs.pSkuTable = &gmmSkuTable;
1926 gmmInitAgrs.pWaTable = &gmmWaTable;
1927 gmmInitAgrs.pGtSysInfo = &gmmGtInfo;
1928 gmmInitAgrs.FileDescriptor = gmmAdapterBDF.Data;
1929 gmmInitAgrs.ClientType = (GMM_CLIENT)GMM_LIBVA_LINUX;
1930
1931 GMM_STATUS status = InitializeGmm(&gmmInitAgrs, &gmmOutArgs);
1932 if (status != GMM_SUCCESS)
1933 {
1934 DDI_ASSERTMESSAGE("InitializeGmm fail.");
1935 FreeForMediaContext(mediaCtx);
1936 return VA_STATUS_ERROR_OPERATION_FAILED;
1937 }
1938 mediaCtx->pGmmClientContext = gmmOutArgs.pGmmClientContext;
1939
1940 // Create GMM page table manager
1941 mediaCtx->m_auxTableMgr = AuxTableMgr::CreateAuxTableMgr(mediaCtx->pDrmBufMgr, &mediaCtx->SkuTable, mediaCtx->pGmmClientContext);
1942
1943 bool bSimulationEnable = false;
1944 #if (_DEBUG || _RELEASE_INTERNAL)
1945 ReadUserSettingForDebug(
1946 mediaCtx->m_userSettingPtr,
1947 bSimulationEnable,
1948 __MEDIA_USER_FEATURE_VALUE_SIM_ENABLE,
1949 MediaUserSetting::Group::Device);
1950 #endif
1951
1952 mediaCtx->m_useSwSwizzling = bSimulationEnable || MEDIA_IS_SKU(&mediaCtx->SkuTable, FtrUseSwSwizzling);
1953 mediaCtx->m_tileYFlag = MEDIA_IS_SKU(&mediaCtx->SkuTable, FtrTileY);
1954
1955 mediaCtx->m_osContext = OsContext::GetOsContextObject();
1956 if (mediaCtx->m_osContext == nullptr)
1957 {
1958 MOS_OS_ASSERTMESSAGE("Unable to get the active OS context.");
1959 FreeForMediaContext(mediaCtx);
1960 return VA_STATUS_ERROR_OPERATION_FAILED;
1961 }
1962
1963 mosCtx.m_auxTableMgr = mediaCtx->m_auxTableMgr;
1964 mosCtx.pGmmClientContext = mediaCtx->pGmmClientContext;
1965
1966 eStatus = mediaCtx->m_osContext->Init(&mosCtx);
1967 if (MOS_STATUS_SUCCESS != eStatus)
1968 {
1969 MOS_OS_ASSERTMESSAGE("Unable to initialize OS context.");
1970 FreeForMediaContext(mediaCtx);
1971 return VA_STATUS_ERROR_OPERATION_FAILED;
1972 }
1973
1974 // Prepare the command buffer manager
1975 mediaCtx->m_cmdBufMgr = CmdBufMgr::GetObject();
1976 if (mediaCtx->m_cmdBufMgr == nullptr)
1977 {
1978 MOS_OS_ASSERTMESSAGE(" nullptr returned by CmdBufMgr::GetObject");
1979 FreeForMediaContext(mediaCtx);
1980 return VA_STATUS_ERROR_OPERATION_FAILED;
1981 }
1982
1983 MOS_STATUS ret = mediaCtx->m_cmdBufMgr->Initialize(mediaCtx->m_osContext, COMMAND_BUFFER_SIZE/2);
1984 if (ret != MOS_STATUS_SUCCESS)
1985 {
1986 MOS_OS_ASSERTMESSAGE(" cmdBufMgr Initialization failed");
1987 FreeForMediaContext(mediaCtx);
1988 return VA_STATUS_ERROR_OPERATION_FAILED;
1989 }
1990
1991 // Prepare the gpu Context manager
1992 mediaCtx->m_gpuContextMgr = GpuContextMgr::GetObject(mediaCtx->pGtSystemInfo, mediaCtx->m_osContext);
1993 if (mediaCtx->m_gpuContextMgr == nullptr)
1994 {
1995 MOS_OS_ASSERTMESSAGE(" nullptr returned by GpuContextMgr::GetObject");
1996 FreeForMediaContext(mediaCtx);
1997 return VA_STATUS_ERROR_OPERATION_FAILED;
1998 }
1999 }
2000 else
2001 {
2002 MOS_OS_ASSERTMESSAGE(" Invalid mos module state");
2003 FreeForMediaContext(mediaCtx);
2004 return VA_STATUS_ERROR_INVALID_PARAMETER;
2005 }
2006
2007 if (DdiMedia_HeapInitialize(mediaCtx) != VA_STATUS_SUCCESS)
2008 {
2009 DestroyMediaContextMutex(mediaCtx);
2010 FreeForMediaContext(mediaCtx);
2011 return VA_STATUS_ERROR_ALLOCATION_FAILED;
2012 }
2013
2014 //Caps need platform and sku table, especially in MediaLibvaCapsCp::IsDecEncryptionSupported
2015 apoDdiEnabled = MediaLibvaApoDecision::InitDdiApoState(devicefd, mediaCtx->m_userSettingPtr);
2016 mediaCtx->m_apoDdiEnabled = apoDdiEnabled;
2017 if(mediaCtx->m_apoDdiEnabled)
2018 {
2019 if (DdiMedia__InitializeSoftlet(mediaCtx, apoDdiEnabled) != VA_STATUS_SUCCESS)
2020 {
2021 DDI_ASSERTMESSAGE("Softlet initialize failed");
2022 return VA_STATUS_ERROR_ALLOCATION_FAILED;
2023 }
2024 ctx->max_image_formats = mediaCtx->m_capsNext->GetImageFormatsMaxNum();
2025 }
2026 else
2027 {
2028 mediaCtx->m_caps = MediaLibvaCaps::CreateMediaLibvaCaps(mediaCtx);
2029 if (!mediaCtx->m_caps)
2030 {
2031 DDI_ASSERTMESSAGE("Caps create failed. Not supported GFX device.");
2032 DestroyMediaContextMutex(mediaCtx);
2033 FreeForMediaContext(mediaCtx);
2034 return VA_STATUS_ERROR_ALLOCATION_FAILED;
2035 }
2036
2037 if (mediaCtx->m_caps->Init() != VA_STATUS_SUCCESS)
2038 {
2039 DDI_ASSERTMESSAGE("Caps init failed. Not supported GFX device.");
2040 DdiMedia_CleanUp(mediaCtx);
2041 DestroyMediaContextMutex(mediaCtx);
2042 FreeForMediaContext(mediaCtx);
2043 return VA_STATUS_ERROR_ALLOCATION_FAILED;
2044 }
2045 ctx->max_image_formats = mediaCtx->m_caps->GetImageFormatsMaxNum();
2046 }
2047
2048 #if !defined(ANDROID) && defined(X11_FOUND)
2049 DdiMediaUtil_InitMutex(&mediaCtx->PutSurfaceRenderMutex);
2050 DdiMediaUtil_InitMutex(&mediaCtx->PutSurfaceSwapBufferMutex);
2051
2052 // try to open X11 lib, if fail, assume no X11 environment
2053 if (VA_STATUS_SUCCESS != DdiMedia_ConnectX11(mediaCtx))
2054 {
2055 // assume no X11 environment. In current implementation,
2056 // PutSurface (Linux) needs X11 support, so just replace
2057 // it with a dummy version. DdiCodec_PutSurfaceDummy() will
2058 // return VA_STATUS_ERROR_UNIMPLEMENTED directly.
2059 ctx->vtable->vaPutSurface = NULL;
2060 }
2061 output_dri_init(ctx);
2062 #endif
2063
2064 DdiMediaUtil_SetMediaResetEnableFlag(mediaCtx);
2065
2066 DdiMediaUtil_UnLockMutex(&GlobalMutex);
2067
2068 return VA_STATUS_SUCCESS;
2069 }
2070
2071 VAStatus DdiMedia_LoadFuncion (VADriverContextP ctx)
2072 {
2073 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
2074
2075 struct VADriverVTable *pVTable = DDI_CODEC_GET_VTABLE(ctx);
2076 DDI_CHK_NULL(pVTable, "nullptr pVTable", VA_STATUS_ERROR_INVALID_CONTEXT);
2077
2078 struct VADriverVTableVPP *pVTableVpp = DDI_CODEC_GET_VTABLE_VPP(ctx);
2079 DDI_CHK_NULL(pVTableVpp, "nullptr pVTableVpp", VA_STATUS_ERROR_INVALID_CONTEXT);
2080
2081 #if VA_CHECK_VERSION(1,11,0)
2082 struct VADriverVTableProt *pVTableProt = DDI_CODEC_GET_VTABLE_PROT(ctx);
2083 DDI_CHK_NULL(pVTableProt, "nullptr pVTableProt", VA_STATUS_ERROR_INVALID_CONTEXT);
2084 #endif
2085
2086 ctx->version_major = VA_MAJOR_VERSION;
2087 ctx->version_minor = VA_MINOR_VERSION;
2088 ctx->max_profiles = DDI_CODEC_GEN_MAX_PROFILES;
2089 ctx->max_entrypoints = DDI_CODEC_GEN_MAX_ENTRYPOINTS;
2090 ctx->max_attributes = (int32_t)VAConfigAttribTypeMax;
2091 ctx->max_subpic_formats = DDI_CODEC_GEN_MAX_SUBPIC_FORMATS;
2092 ctx->max_display_attributes = DDI_MEDIA_GEN_MAX_DISPLAY_ATTRIBUTES;
2093 ctx->str_vendor = DDI_CODEC_GEN_STR_VENDOR;
2094 ctx->vtable_tpi = nullptr;
2095
2096 pVTable->vaTerminate = DdiMedia_Terminate;
2097 pVTable->vaQueryConfigEntrypoints = DdiMedia_QueryConfigEntrypoints;
2098 pVTable->vaQueryConfigProfiles = DdiMedia_QueryConfigProfiles;
2099 pVTable->vaQueryConfigAttributes = DdiMedia_QueryConfigAttributes;
2100 pVTable->vaCreateConfig = DdiMedia_CreateConfig;
2101 pVTable->vaDestroyConfig = DdiMedia_DestroyConfig;
2102 pVTable->vaGetConfigAttributes = DdiMedia_GetConfigAttributes;
2103
2104 pVTable->vaCreateSurfaces = DdiMedia_CreateSurfaces;
2105 pVTable->vaDestroySurfaces = DdiMedia_DestroySurfaces;
2106 pVTable->vaCreateSurfaces2 = DdiMedia_CreateSurfaces2;
2107
2108 pVTable->vaCreateContext = DdiMedia_CreateContext;
2109 pVTable->vaDestroyContext = DdiMedia_DestroyContext;
2110 pVTable->vaCreateBuffer = DdiMedia_CreateBuffer;
2111 pVTable->vaBufferSetNumElements = DdiMedia_BufferSetNumElements;
2112 pVTable->vaMapBuffer = DdiMedia_MapBuffer;
2113 pVTable->vaUnmapBuffer = DdiMedia_UnmapBuffer;
2114 pVTable->vaDestroyBuffer = DdiMedia_DestroyBuffer;
2115 pVTable->vaBeginPicture = DdiMedia_BeginPicture;
2116 pVTable->vaRenderPicture = DdiMedia_RenderPicture;
2117 pVTable->vaEndPicture = DdiMedia_EndPicture;
2118 pVTable->vaSyncSurface = DdiMedia_SyncSurface;
2119 #if VA_CHECK_VERSION(1, 9, 0)
2120 pVTable->vaSyncSurface2 = DdiMedia_SyncSurface2;
2121 pVTable->vaSyncBuffer = DdiMedia_SyncBuffer;
2122 #endif
2123 pVTable->vaQuerySurfaceStatus = DdiMedia_QuerySurfaceStatus;
2124 pVTable->vaQuerySurfaceError = DdiMedia_QuerySurfaceError;
2125 pVTable->vaQuerySurfaceAttributes = DdiMedia_QuerySurfaceAttributes;
2126 pVTable->vaPutSurface = DdiMedia_PutSurface;
2127 pVTable->vaQueryImageFormats = DdiMedia_QueryImageFormats;
2128
2129 pVTable->vaCreateImage = DdiMedia_CreateImage;
2130 pVTable->vaDeriveImage = DdiMedia_DeriveImage;
2131 pVTable->vaDestroyImage = DdiMedia_DestroyImage;
2132 pVTable->vaSetImagePalette = DdiMedia_SetImagePalette;
2133 pVTable->vaGetImage = DdiMedia_GetImage;
2134 pVTable->vaPutImage = DdiMedia_PutImage;
2135 pVTable->vaQuerySubpictureFormats = DdiMedia_QuerySubpictureFormats;
2136 pVTable->vaCreateSubpicture = DdiMedia_CreateSubpicture;
2137 pVTable->vaDestroySubpicture = DdiMedia_DestroySubpicture;
2138 pVTable->vaSetSubpictureImage = DdiMedia_SetSubpictureImage;
2139 pVTable->vaSetSubpictureChromakey = DdiMedia_SetSubpictureChromakey;
2140 pVTable->vaSetSubpictureGlobalAlpha = DdiMedia_SetSubpictureGlobalAlpha;
2141 pVTable->vaAssociateSubpicture = DdiMedia_AssociateSubpicture;
2142 pVTable->vaDeassociateSubpicture = DdiMedia_DeassociateSubpicture;
2143 pVTable->vaQueryDisplayAttributes = DdiMedia_QueryDisplayAttributes;
2144 pVTable->vaGetDisplayAttributes = DdiMedia_GetDisplayAttributes;
2145 pVTable->vaSetDisplayAttributes = DdiMedia_SetDisplayAttributes;
2146 pVTable->vaQueryProcessingRate = DdiMedia_QueryProcessingRate;
2147 #if VA_CHECK_VERSION(1,10,0)
2148 pVTable->vaCopy = DdiMedia_Copy;
2149 #endif
2150
2151 // vaTrace
2152 pVTable->vaBufferInfo = DdiMedia_BufferInfo;
2153 pVTable->vaLockSurface = DdiMedia_LockSurface;
2154 pVTable->vaUnlockSurface = DdiMedia_UnlockSurface;
2155
2156 pVTableVpp->vaQueryVideoProcFilters = DdiMedia_QueryVideoProcFilters;
2157 pVTableVpp->vaQueryVideoProcFilterCaps = DdiMedia_QueryVideoProcFilterCaps;
2158 pVTableVpp->vaQueryVideoProcPipelineCaps = DdiMedia_QueryVideoProcPipelineCaps;
2159
2160 #if VA_CHECK_VERSION(1,11,0)
2161 pVTableProt->vaCreateProtectedSession = DdiMediaProtected::DdiMedia_CreateProtectedSession;
2162 pVTableProt->vaDestroyProtectedSession = DdiMediaProtected::DdiMedia_DestroyProtectedSession;
2163 pVTableProt->vaAttachProtectedSession = DdiMediaProtected::DdiMedia_AttachProtectedSession;
2164 pVTableProt->vaDetachProtectedSession = DdiMediaProtected::DdiMedia_DetachProtectedSession;
2165 pVTableProt->vaProtectedSessionExecute = DdiMediaProtected::DdiMedia_ProtectedSessionExecute;
2166 #endif
2167
2168 //pVTable->vaSetSurfaceAttributes = DdiMedia_SetSurfaceAttributes;
2169 pVTable->vaGetSurfaceAttributes = DdiMedia_GetSurfaceAttributes;
2170 //Export PRIMEFD/FLINK to application for buffer sharing with OpenCL/GL
2171 pVTable->vaAcquireBufferHandle = DdiMedia_AcquireBufferHandle;
2172 pVTable->vaReleaseBufferHandle = DdiMedia_ReleaseBufferHandle;
2173 pVTable->vaExportSurfaceHandle = DdiMedia_ExportSurfaceHandle;
2174 #ifndef ANDROID
2175 pVTable->vaCreateMFContext = DdiMedia_CreateMfeContextInternal;
2176 pVTable->vaMFAddContext = DdiMedia_AddContextInternal;
2177 pVTable->vaMFReleaseContext = DdiMedia_ReleaseContextInternal;
2178 pVTable->vaMFSubmit = DdiEncode_MfeSubmit;
2179 #endif
2180 return VA_STATUS_SUCCESS;
2181 }
2182
2183 VAStatus DdiMedia_CleanUp (PDDI_MEDIA_CONTEXT mediaCtx)
2184 {
2185 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
2186
2187 if (mediaCtx->m_caps)
2188 {
2189 MOS_Delete(mediaCtx->m_caps);
2190 mediaCtx->m_caps = nullptr;
2191 }
2192
2193 DdiMedia_CleanUpSoftlet(mediaCtx);
2194 return VA_STATUS_SUCCESS;
2195 }
2196
2197 VAStatus DdiMedia_Terminate (
2198 VADriverContextP ctx
2199 )
2200 {
2201 DDI_FUNCTION_ENTER();
2202 MOS_CONTEXT mosCtx = {};
2203
2204 #if CLASS_TRACE
2205 ClassTrace::Dump("#In DdiMedia_Terminate", "temp/class_trace.log");
2206 #endif
2207 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
2208
2209 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
2210 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
2211
2212 DdiMediaUtil_LockMutex(&GlobalMutex);
2213
2214 #if !defined(ANDROID) && defined(X11_FOUND)
2215 DdiMedia_DestroyX11Connection(mediaCtx);
2216 DdiMediaUtil_DestroyMutex(&mediaCtx->PutSurfaceRenderMutex);
2217 DdiMediaUtil_DestroyMutex(&mediaCtx->PutSurfaceSwapBufferMutex);
2218
2219 if (mediaCtx->m_caps
2220 || mediaCtx->m_capsNext
2221 )
2222 {
2223 if (mediaCtx->dri_output != nullptr) {
2224 if (mediaCtx->dri_output->handle)
2225 dso_close(mediaCtx->dri_output->handle);
2226
2227 free(mediaCtx->dri_output);
2228 mediaCtx->dri_output = nullptr;
2229 }
2230 }
2231 #endif
2232
2233 DdiMedia_CleanUp(mediaCtx);
2234
2235 //destory resources
2236 DdiMedia_FreeSurfaceHeapElements(mediaCtx);
2237 DdiMedia_FreeBufferHeapElements(ctx);
2238 DdiMedia_FreeImageHeapElements(ctx);
2239 DdiMedia_FreeContextHeapElements(ctx);
2240 DdiMedia_FreeContextCMElements(ctx);
2241
2242 DdiMedia_HeapDestroy(mediaCtx);
2243 DdiMediaProtected::FreeInstances();
2244
2245 mosCtx.fd = mediaCtx->fd;
2246 mosCtx.m_userSettingPtr = mediaCtx->m_userSettingPtr;
2247
2248 if (mediaCtx->m_apoMosEnabled)
2249 {
2250 MosInterface::DestroyOsDeviceContext(mediaCtx->m_osDeviceContext);
2251 mediaCtx->m_osDeviceContext = MOS_INVALID_HANDLE;
2252 MOS_FreeMemory(mediaCtx->pGtSystemInfo);
2253 MosOcaInterfaceSpecific::UninitInterface();
2254 MosInterface::CloseOsUtilities(&mosCtx);
2255 }
2256 else if (mediaCtx->modularizedGpuCtxEnabled)
2257 {
2258 if (mediaCtx->m_auxTableMgr != nullptr)
2259 {
2260 MOS_Delete(mediaCtx->m_auxTableMgr);
2261 mediaCtx->m_auxTableMgr = nullptr;
2262 }
2263
2264 if (mediaCtx->m_gpuContextMgr)
2265 {
2266 mediaCtx->m_gpuContextMgr->CleanUp();
2267 MOS_Delete(mediaCtx->m_gpuContextMgr);
2268 }
2269
2270 if (mediaCtx->m_cmdBufMgr)
2271 {
2272 mediaCtx->m_cmdBufMgr->CleanUp();
2273 MOS_Delete(mediaCtx->m_cmdBufMgr);
2274 }
2275
2276 if (mediaCtx->m_osContext)
2277 {
2278 mediaCtx->m_osContext->CleanUp();
2279 MOS_Delete(mediaCtx->m_osContext);
2280 }
2281
2282 // destroy libdrm buffer manager
2283 mos_bufmgr_destroy(mediaCtx->pDrmBufMgr);
2284
2285 // Destroy memory allocated to store Media System Info
2286 MOS_FreeMemory(mediaCtx->pGtSystemInfo);
2287 // Free GMM memory.
2288 GMM_INIT_OUT_ARGS gmmOutArgs = {};
2289 gmmOutArgs.pGmmClientContext = mediaCtx->pGmmClientContext;
2290 GmmAdapterDestroy(&gmmOutArgs);
2291 mediaCtx->pGmmClientContext = nullptr;
2292 MosUtilities::MosUtilitiesClose(mediaCtx->m_userSettingPtr);
2293 }
2294
2295 mediaCtx->m_userSettingPtr.reset();
2296
2297 if (mediaCtx->uiRef > 1)
2298 {
2299 mediaCtx->uiRef--;
2300 DdiMediaUtil_UnLockMutex(&GlobalMutex);
2301
2302 return VA_STATUS_SUCCESS;
2303 }
2304 mediaCtx->SkuTable.reset();
2305 mediaCtx->WaTable.reset();
2306
2307 // release media driver context, ctx creation is behind the mos_utilities_init
2308 // If free earilier than MOS_utilities_close, memnja count error.
2309 MOS_Delete(mediaCtx);
2310 mediaCtx = nullptr;
2311 ctx->pDriverData = nullptr;
2312
2313 DdiMediaUtil_UnLockMutex(&GlobalMutex);
2314
2315 return VA_STATUS_SUCCESS;
2316 }
2317
2318 VAStatus DdiMedia_QueryConfigEntrypoints(
2319 VADriverContextP ctx,
2320 VAProfile profile,
2321 VAEntrypoint *entrypoint_list,
2322 int32_t *num_entrypoints
2323 )
2324 {
2325 DDI_FUNCTION_ENTER();
2326
2327 PERF_UTILITY_START_ONCE("First Frame Time", PERF_MOS, PERF_LEVEL_DDI);
2328
2329 DDI_CHK_NULL(ctx, "nullptr Ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
2330 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
2331 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
2332 DDI_CHK_NULL(mediaCtx->m_caps, "nullptr m_caps", VA_STATUS_ERROR_INVALID_CONTEXT);
2333
2334 DDI_CHK_NULL(entrypoint_list, "nullptr entrypoint_list", VA_STATUS_ERROR_INVALID_PARAMETER);
2335 DDI_CHK_NULL(num_entrypoints, "nullptr num_entrypoints", VA_STATUS_ERROR_INVALID_PARAMETER);
2336
2337 return mediaCtx->m_caps->QueryConfigEntrypoints(profile,
2338 entrypoint_list, num_entrypoints);
2339 }
2340
2341 VAStatus DdiMedia_QueryConfigProfiles (
2342 VADriverContextP ctx,
2343 VAProfile *profile_list,
2344 int32_t *num_profiles
2345 )
2346 {
2347 DDI_FUNCTION_ENTER();
2348
2349 DDI_CHK_NULL(ctx, "nullptr Ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
2350 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
2351
2352 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
2353 DDI_CHK_NULL(mediaCtx->m_caps, "nullptr m_caps", VA_STATUS_ERROR_INVALID_CONTEXT);
2354 DDI_CHK_NULL(profile_list, "nullptr profile_list", VA_STATUS_ERROR_INVALID_PARAMETER);
2355 DDI_CHK_NULL(num_profiles, "nullptr num_profiles", VA_STATUS_ERROR_INVALID_PARAMETER);
2356
2357 return mediaCtx->m_caps->QueryConfigProfiles(profile_list, num_profiles);
2358 }
2359
2360 VAStatus DdiMedia_QueryConfigAttributes (
2361 VADriverContextP ctx,
2362 VAConfigID config_id,
2363 VAProfile *profile,
2364 VAEntrypoint *entrypoint,
2365 VAConfigAttrib *attrib_list,
2366 int32_t *num_attribs
2367 )
2368 {
2369 DDI_FUNCTION_ENTER();
2370
2371 DDI_CHK_NULL(profile, "nullptr profile", VA_STATUS_ERROR_INVALID_PARAMETER);
2372 DDI_CHK_NULL(entrypoint, "nullptr entrypoint", VA_STATUS_ERROR_INVALID_PARAMETER);
2373 DDI_CHK_NULL(ctx, "nullptr Ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
2374 DDI_CHK_NULL(num_attribs, "nullptr num_attribs", VA_STATUS_ERROR_INVALID_PARAMETER);
2375
2376 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
2377 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
2378 DDI_CHK_NULL(mediaCtx->m_caps, "nullptr m_caps", VA_STATUS_ERROR_INVALID_CONTEXT);
2379
2380 return mediaCtx->m_caps->QueryConfigAttributes(
2381 config_id, profile, entrypoint, attrib_list, num_attribs);
2382 }
2383
2384 VAStatus DdiMedia_CreateConfig (
2385 VADriverContextP ctx,
2386 VAProfile profile,
2387 VAEntrypoint entrypoint,
2388 VAConfigAttrib *attrib_list,
2389 int32_t num_attribs,
2390 VAConfigID *config_id
2391 )
2392 {
2393 DDI_FUNCTION_ENTER();
2394
2395 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
2396 DDI_CHK_NULL(config_id, "nullptr config_id", VA_STATUS_ERROR_INVALID_PARAMETER);
2397
2398 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
2399 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
2400 DDI_CHK_NULL(mediaCtx->m_caps, "nullptr m_caps", VA_STATUS_ERROR_INVALID_CONTEXT);
2401
2402 return mediaCtx->m_caps->CreateConfig(
2403 profile, entrypoint, attrib_list, num_attribs, config_id);
2404 }
2405
2406 VAStatus DdiMedia_DestroyConfig (
2407 VADriverContextP ctx,
2408 VAConfigID config_id
2409 )
2410 {
2411 DDI_FUNCTION_ENTER();
2412
2413 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
2414
2415 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
2416 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
2417 DDI_CHK_NULL(mediaCtx->m_caps, "nullptr m_caps", VA_STATUS_ERROR_INVALID_CONTEXT);
2418
2419 return mediaCtx->m_caps->DestroyConfig(config_id);
2420 }
2421
2422 VAStatus DdiMedia_GetConfigAttributes(
2423 VADriverContextP ctx,
2424 VAProfile profile,
2425 VAEntrypoint entrypoint,
2426 VAConfigAttrib *attrib_list,
2427 int32_t num_attribs
2428 )
2429 {
2430 DDI_FUNCTION_ENTER();
2431
2432 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
2433
2434 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
2435 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
2436 DDI_CHK_NULL(mediaCtx->m_caps, "nullptr m_caps", VA_STATUS_ERROR_INVALID_CONTEXT);
2437
2438 return mediaCtx->m_caps->GetConfigAttributes(
2439 profile, entrypoint, attrib_list, num_attribs);
2440 }
2441
2442 VAStatus DdiMedia_CreateSurfaces (
2443 VADriverContextP ctx,
2444 int32_t width,
2445 int32_t height,
2446 int32_t format,
2447 int32_t num_surfaces,
2448 VASurfaceID *surfaces
2449 )
2450 {
2451 DDI_FUNCTION_ENTER();
2452 return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
2453 }
2454
2455 VAStatus DdiMedia_DestroySurfaces (
2456 VADriverContextP ctx,
2457 VASurfaceID *surfaces,
2458 int32_t num_surfaces
2459 )
2460 {
2461 DDI_FUNCTION_ENTER();
2462
2463 DDI_CHK_NULL (ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
2464 DDI_CHK_LARGER(num_surfaces, 0, "Invalid num_surfaces", VA_STATUS_ERROR_INVALID_PARAMETER);
2465 DDI_CHK_NULL (surfaces, "nullptr surfaces", VA_STATUS_ERROR_INVALID_PARAMETER);
2466 MOS_TraceEventExt(EVENT_VA_FREE_SURFACE, EVENT_TYPE_START, &num_surfaces, sizeof(int32_t), surfaces, num_surfaces*sizeof(VAGenericID));
2467
2468 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
2469 DDI_CHK_NULL (mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
2470 DDI_CHK_NULL (mediaCtx->pSurfaceHeap, "nullptr mediaCtx->pSurfaceHeap", VA_STATUS_ERROR_INVALID_CONTEXT);
2471
2472 PDDI_MEDIA_SURFACE surface = nullptr;
2473 for(int32_t i = 0; i < num_surfaces; i++)
2474 {
2475 DDI_CHK_LESS((uint32_t)surfaces[i], mediaCtx->pSurfaceHeap->uiAllocatedHeapElements, "Invalid surfaces", VA_STATUS_ERROR_INVALID_SURFACE);
2476 surface = DdiMedia_GetSurfaceFromVASurfaceID(mediaCtx, surfaces[i]);
2477 DDI_CHK_NULL(surface, "nullptr surface", VA_STATUS_ERROR_INVALID_SURFACE);
2478 if(surface->pCurrentFrameSemaphore)
2479 {
2480 DdiMediaUtil_WaitSemaphore(surface->pCurrentFrameSemaphore);
2481 DdiMediaUtil_PostSemaphore(surface->pCurrentFrameSemaphore);
2482 }
2483 if(surface->pReferenceFrameSemaphore)
2484 {
2485 DdiMediaUtil_WaitSemaphore(surface->pReferenceFrameSemaphore);
2486 DdiMediaUtil_PostSemaphore(surface->pReferenceFrameSemaphore);
2487 }
2488 }
2489
2490 for(int32_t i = 0; i < num_surfaces; i++)
2491 {
2492 DDI_CHK_LESS((uint32_t)surfaces[i], mediaCtx->pSurfaceHeap->uiAllocatedHeapElements, "Invalid surfaces", VA_STATUS_ERROR_INVALID_SURFACE);
2493 surface = DdiMedia_GetSurfaceFromVASurfaceID(mediaCtx, surfaces[i]);
2494 DDI_CHK_NULL(surface, "nullptr surface", VA_STATUS_ERROR_INVALID_SURFACE);
2495 if(surface->pCurrentFrameSemaphore)
2496 {
2497 DdiMediaUtil_DestroySemaphore(surface->pCurrentFrameSemaphore);
2498 surface->pCurrentFrameSemaphore = nullptr;
2499 }
2500
2501 if(surface->pReferenceFrameSemaphore)
2502 {
2503 DdiMediaUtil_DestroySemaphore(surface->pReferenceFrameSemaphore);
2504 surface->pReferenceFrameSemaphore = nullptr;
2505 }
2506
2507 DdiMediaUtil_UnRegisterRTSurfaces(ctx, surface);
2508
2509 if (surface->pMediaCtx && surface->pMediaCtx->m_auxTableMgr)
2510 {
2511 uint64_t freq = 1, countStart = 0, countCur = 0;
2512 MosUtilities::MosQueryPerformanceFrequency(&freq);
2513 uint64_t countTimeout = freq * BO_BUSY_TIMEOUT_LIMIT / 1000;
2514 MOS_TraceEventExt(EVENT_VA_SYNC, EVENT_TYPE_START, nullptr, 0, nullptr, 0);
2515 MosUtilities::MosQueryPerformanceCounter(&countStart);
2516 while (1)
2517 {
2518 uint32_t timeout_NS = 10;
2519 int ret = mos_bo_wait(surface->bo, timeout_NS);
2520 MosUtilities::MosQueryPerformanceCounter(&countCur);
2521 if(ret == 0 || countCur - countStart > countTimeout)
2522 {
2523 break;
2524 }
2525 }
2526 MOS_TraceEventExt(EVENT_VA_SYNC, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
2527 }
2528
2529 DdiMediaUtil_LockMutex(&mediaCtx->SurfaceMutex);
2530 DdiMediaUtil_FreeSurface(surface);
2531 MOS_FreeMemory(surface);
2532 DdiMediaUtil_ReleasePMediaSurfaceFromHeap(mediaCtx->pSurfaceHeap, (uint32_t)surfaces[i]);
2533 mediaCtx->uiNumSurfaces--;
2534 DdiMediaUtil_UnLockMutex(&mediaCtx->SurfaceMutex);
2535 }
2536
2537 MOS_TraceEventExt(EVENT_VA_FREE_SURFACE, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
2538 return VA_STATUS_SUCCESS;
2539 }
2540
2541 VAStatus DdiMedia_CreateSurfaces2(
2542 VADriverContextP ctx,
2543 uint32_t format,
2544 uint32_t width,
2545 uint32_t height,
2546 VASurfaceID *surfaces,
2547 uint32_t num_surfaces,
2548 VASurfaceAttrib *attrib_list,
2549 uint32_t num_attribs
2550 )
2551 {
2552 DDI_FUNCTION_ENTER();
2553 uint32_t event[] = {width, height, format};
2554 MOS_TraceEventExt(EVENT_VA_SURFACE, EVENT_TYPE_START, event, sizeof(event), nullptr, 0);
2555
2556 DDI_CHK_NULL (ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
2557 DDI_CHK_LARGER(num_surfaces, 0, "Invalid num_surfaces", VA_STATUS_ERROR_INVALID_PARAMETER);
2558 DDI_CHK_NULL (surfaces, "nullptr surfaces", VA_STATUS_ERROR_INVALID_PARAMETER);
2559 DDI_CHK_LARGER(width, 0, "Invalid width", VA_STATUS_ERROR_INVALID_PARAMETER);
2560 DDI_CHK_LARGER(height, 0, "Invalid height", VA_STATUS_ERROR_INVALID_PARAMETER);
2561
2562 if(num_attribs > 0)
2563 {
2564 DDI_CHK_NULL(attrib_list, "nullptr attrib_list", VA_STATUS_ERROR_INVALID_PARAMETER);
2565 }
2566
2567 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
2568 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
2569
2570 int32_t expected_fourcc = VA_FOURCC_NV12;
2571 switch(format)
2572 {
2573 case VA_RT_FORMAT_YUV420:
2574 expected_fourcc = VA_FOURCC_NV12;
2575 break;
2576 case VA_RT_FORMAT_YUV420_12:
2577 expected_fourcc = VA_FOURCC_P012;
2578 break;
2579 case VA_RT_FORMAT_YUV422:
2580 expected_fourcc = VA_FOURCC_YUY2;
2581 break;
2582 case VA_RT_FORMAT_YUV422_10:
2583 expected_fourcc = VA_FOURCC_Y210;
2584 break;
2585 case VA_RT_FORMAT_YUV422_12:
2586 #if VA_CHECK_VERSION(1, 9, 0)
2587 expected_fourcc = VA_FOURCC_Y212;
2588 #else
2589 expected_fourcc = VA_FOURCC_Y216;
2590 #endif
2591 break;
2592 case VA_RT_FORMAT_YUV444:
2593 expected_fourcc = VA_FOURCC_444P;
2594 break;
2595 case VA_RT_FORMAT_YUV444_10:
2596 expected_fourcc = VA_FOURCC_Y410;
2597 break;
2598 case VA_RT_FORMAT_YUV444_12:
2599 #if VA_CHECK_VERSION(1, 9, 0)
2600 expected_fourcc = VA_FOURCC_Y412;
2601 #else
2602 expected_fourcc = VA_FOURCC_Y416;
2603 #endif
2604 break;
2605 case VA_RT_FORMAT_YUV411:
2606 expected_fourcc = VA_FOURCC_411P;
2607 break;
2608 case VA_RT_FORMAT_YUV400:
2609 expected_fourcc = VA_FOURCC('4','0','0','P');
2610 break;
2611 case VA_RT_FORMAT_YUV420_10BPP:
2612 expected_fourcc = VA_FOURCC_P010;
2613 break;
2614 case VA_RT_FORMAT_RGB16:
2615 expected_fourcc = VA_FOURCC_R5G6B5;
2616 break;
2617 case VA_RT_FORMAT_RGB32:
2618 expected_fourcc = VA_FOURCC_BGRA;
2619 break;
2620 case VA_RT_FORMAT_RGBP:
2621 expected_fourcc = VA_FOURCC_RGBP;
2622 break;
2623 #ifdef VA_RT_FORMAT_RGB32_10BPP
2624 case VA_RT_FORMAT_RGB32_10BPP:
2625 expected_fourcc = VA_FOURCC_BGRA;
2626 break;
2627 #endif
2628 #if 1 //added for having MDF sanity test pass, will be removed after MDF formal patch checked in
2629 case VA_FOURCC_NV12:
2630 expected_fourcc = VA_FOURCC_NV12;
2631 break;
2632 case VA_FOURCC_NV21:
2633 expected_fourcc = VA_FOURCC_NV21;
2634 break;
2635 case VA_FOURCC_ABGR:
2636 expected_fourcc = VA_FOURCC_ABGR;
2637 break;
2638 case VA_FOURCC_ARGB:
2639 expected_fourcc = VA_FOURCC_ARGB;
2640 break;
2641 case VA_FOURCC_XBGR:
2642 expected_fourcc = VA_FOURCC_XBGR;
2643 break;
2644 case VA_FOURCC_XRGB:
2645 expected_fourcc = VA_FOURCC_XRGB;
2646 break;
2647 case VA_FOURCC_R5G6B5:
2648 expected_fourcc = VA_FOURCC_R5G6B5;
2649 break;
2650 case VA_FOURCC_R8G8B8:
2651 expected_fourcc = VA_FOURCC_R8G8B8;
2652 break;
2653 case VA_FOURCC_YUY2:
2654 expected_fourcc = VA_FOURCC_YUY2;
2655 break;
2656 case VA_FOURCC_YV12:
2657 expected_fourcc = VA_FOURCC_YV12;
2658 break;
2659 case VA_FOURCC_422H:
2660 expected_fourcc = VA_FOURCC_422H;
2661 break;
2662 case VA_FOURCC_422V:
2663 expected_fourcc = VA_FOURCC_422V;
2664 break;
2665 case VA_FOURCC_P208:
2666 expected_fourcc = VA_FOURCC_P208;
2667 break;
2668 case VA_FOURCC_P010:
2669 expected_fourcc = VA_FOURCC_P010;
2670 break;
2671 case VA_FOURCC_P012:
2672 expected_fourcc = VA_FOURCC_P012;
2673 break;
2674 case VA_FOURCC_P016:
2675 expected_fourcc = VA_FOURCC_P016;
2676 break;
2677 case VA_FOURCC_Y210:
2678 expected_fourcc = VA_FOURCC_Y210;
2679 break;
2680 #if VA_CHECK_VERSION(1, 9, 0)
2681 case VA_FOURCC_Y212:
2682 expected_fourcc = VA_FOURCC_Y212;
2683 break;
2684 #endif
2685 case VA_FOURCC_Y216:
2686 expected_fourcc = VA_FOURCC_Y216;
2687 break;
2688 case VA_FOURCC_AYUV:
2689 expected_fourcc = VA_FOURCC_AYUV;
2690 break;
2691 #if VA_CHECK_VERSION(1, 13, 0)
2692 case VA_FOURCC_XYUV:
2693 expected_fourcc = VA_FOURCC_XYUV;
2694 break;
2695 #endif
2696 case VA_FOURCC_Y410:
2697 expected_fourcc = VA_FOURCC_Y410;
2698 break;
2699 #if VA_CHECK_VERSION(1, 9, 0)
2700 case VA_FOURCC_Y412:
2701 expected_fourcc = VA_FOURCC_Y412;
2702 break;
2703 #endif
2704 case VA_FOURCC_Y416:
2705 expected_fourcc = VA_FOURCC_Y416;
2706 break;
2707 case VA_FOURCC_I420:
2708 expected_fourcc = VA_FOURCC_I420;
2709 break;
2710 case VA_FOURCC_UYVY:
2711 expected_fourcc = VA_FOURCC_UYVY;
2712 break;
2713 #endif
2714 default:
2715 DDI_ASSERTMESSAGE("Invalid VAConfigAttribRTFormat: 0x%x. Please uses the format defined in libva/va.h", format);
2716 return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
2717 }
2718
2719 VASurfaceAttribExternalBuffers externalBufDescripor;
2720 VADRMPRIMESurfaceDescriptor drmPrimeSurfaceDescriptor;
2721 MOS_ZeroMemory(&externalBufDescripor, sizeof(VASurfaceAttribExternalBuffers));
2722 MOS_ZeroMemory(&drmPrimeSurfaceDescriptor, sizeof(VADRMPRIMESurfaceDescriptor));
2723 #if VA_CHECK_VERSION(1, 21, 0)
2724 VADRMPRIME3SurfaceDescriptor drmPrime3SurfaceDescriptor;
2725 MOS_ZeroMemory(&drmPrime3SurfaceDescriptor, sizeof(VADRMPRIME3SurfaceDescriptor));
2726 #endif
2727
2728 int32_t memTypeFlag = VA_SURFACE_ATTRIB_MEM_TYPE_VA;
2729 int32_t descFlag = 0;
2730 uint32_t surfaceUsageHint = VA_SURFACE_ATTRIB_USAGE_HINT_GENERIC;
2731 bool surfDescProvided = false;
2732 bool surfIsUserPtr = false;
2733
2734 for (uint32_t i = 0; i < num_attribs && attrib_list; i++)
2735 {
2736 if (attrib_list[i].flags & VA_SURFACE_ATTRIB_SETTABLE)
2737 {
2738 switch (attrib_list[i].type)
2739 {
2740 case VASurfaceAttribUsageHint:
2741 DDI_ASSERT(attrib_list[i].value.type == VAGenericValueTypeInteger);
2742 surfaceUsageHint = attrib_list[i].value.value.i;
2743 break;
2744 case VASurfaceAttribPixelFormat:
2745 DDI_ASSERT(attrib_list[i].value.type == VAGenericValueTypeInteger);
2746 expected_fourcc = attrib_list[i].value.value.i;
2747 break;
2748 case VASurfaceAttribMemoryType:
2749 DDI_ASSERT(attrib_list[i].value.type == VAGenericValueTypeInteger);
2750 if ((attrib_list[i].value.value.i == VA_SURFACE_ATTRIB_MEM_TYPE_VA) ||
2751 (attrib_list[i].value.value.i == VA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM) ||
2752 (attrib_list[i].value.value.i == VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME) ||
2753 (attrib_list[i].value.value.i == VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2) ||
2754 #if VA_CHECK_VERSION(1, 21, 0)
2755 (attrib_list[i].value.value.i == VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_3) ||
2756 #endif
2757 (attrib_list[i].value.value.i == VA_SURFACE_ATTRIB_MEM_TYPE_USER_PTR))
2758 {
2759 memTypeFlag = attrib_list[i].value.value.i;
2760 surfIsUserPtr = (attrib_list[i].value.value.i == VA_SURFACE_ATTRIB_MEM_TYPE_USER_PTR);
2761 }
2762 else
2763 {
2764 DDI_ASSERTMESSAGE("Not supported external buffer type.");
2765 return VA_STATUS_ERROR_INVALID_PARAMETER;
2766 }
2767 break;
2768 case (VASurfaceAttribType)VASurfaceAttribExternalBufferDescriptor:
2769 DDI_ASSERT(attrib_list[i].value.type == VAGenericValueTypePointer);
2770 if( nullptr == attrib_list[i].value.value.p )
2771 {
2772 DDI_ASSERTMESSAGE("Invalid VASurfaceAttribExternalBuffers used.");
2773 //remove the check for libva-utils conformance test, need libva-utils change cases
2774 //after libva-utils fix the case, return VA_STATUS_ERROR_INVALID_PARAMETER;
2775 break;
2776 }
2777 surfDescProvided = true;
2778 if (memTypeFlag == VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2)
2779 {
2780 MOS_SecureMemcpy(&drmPrimeSurfaceDescriptor, sizeof(VADRMPRIMESurfaceDescriptor), attrib_list[i].value.value.p, sizeof(VADRMPRIMESurfaceDescriptor));
2781 expected_fourcc = drmPrimeSurfaceDescriptor.fourcc;
2782 width = drmPrimeSurfaceDescriptor.width;
2783 height = drmPrimeSurfaceDescriptor.height;
2784 }
2785 #if VA_CHECK_VERSION(1, 21, 0)
2786 else if (memTypeFlag == VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_3 )
2787 {
2788 MosUtilities::MosSecureMemcpy(&drmPrime3SurfaceDescriptor, sizeof(VADRMPRIME3SurfaceDescriptor), attrib_list[i].value.value.p, sizeof(VADRMPRIME3SurfaceDescriptor));
2789 expected_fourcc = drmPrime3SurfaceDescriptor.fourcc;
2790 width = drmPrime3SurfaceDescriptor.width;
2791 height = drmPrime3SurfaceDescriptor.height;
2792 drmPrimeSurfaceDescriptor = *((VADRMPRIMESurfaceDescriptor *)&drmPrime3SurfaceDescriptor);
2793 descFlag = drmPrime3SurfaceDescriptor.flags;
2794 }
2795 #endif
2796 else
2797 {
2798 MOS_SecureMemcpy(&externalBufDescripor, sizeof(VASurfaceAttribExternalBuffers), attrib_list[i].value.value.p, sizeof(VASurfaceAttribExternalBuffers));
2799
2800 expected_fourcc = externalBufDescripor.pixel_format;
2801 width = externalBufDescripor.width;
2802 height = externalBufDescripor.height;
2803 // the following code is for backward compatible and it will be removed in the future
2804 // new implemention should use VASurfaceAttribMemoryType attrib and set its value to VA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM
2805 if ((externalBufDescripor.flags & VA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM )||
2806 (externalBufDescripor.flags & VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME)||
2807 (externalBufDescripor.flags & VA_SURFACE_EXTBUF_DESC_PROTECTED)||
2808 (externalBufDescripor.flags & VA_SURFACE_EXTBUF_DESC_ENABLE_TILING))
2809 {
2810 if (externalBufDescripor.flags & VA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM)
2811 {
2812 memTypeFlag = VA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM;
2813 }
2814 else if (externalBufDescripor.flags & VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME)
2815 {
2816 memTypeFlag = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME;
2817 }
2818
2819 descFlag = (externalBufDescripor.flags & ~(VA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM | VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME));
2820 surfIsUserPtr = false;
2821 }
2822 }
2823
2824 break;
2825 default:
2826 DDI_ASSERTMESSAGE("Unsupported type.");
2827 break;
2828 }
2829 }
2830 }
2831
2832 DDI_MEDIA_FORMAT mediaFmt = DdiMedia_OsFormatToMediaFormat(expected_fourcc,format);
2833 if (mediaFmt == Media_Format_Count)
2834 {
2835 DDI_ASSERTMESSAGE("DDI: unsupported surface type in DdiMedia_CreateSurfaces2.");
2836 return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
2837 }
2838
2839 for (uint32_t i = 0; i < num_surfaces; i++)
2840 {
2841 PDDI_MEDIA_SURFACE_DESCRIPTOR surfDesc = nullptr;
2842 MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
2843
2844 if (surfDescProvided == true)
2845 {
2846 surfDesc = (PDDI_MEDIA_SURFACE_DESCRIPTOR)MOS_AllocAndZeroMemory(sizeof(DDI_MEDIA_SURFACE_DESCRIPTOR));
2847 if (surfDesc == nullptr)
2848 {
2849 return VA_STATUS_ERROR_ALLOCATION_FAILED;
2850 }
2851 surfDesc->uiFlags = descFlag;
2852 surfDesc->uiVaMemType = memTypeFlag;
2853
2854 if (
2855 #if VA_CHECK_VERSION(1, 21, 0)
2856 memTypeFlag == VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_3 ||
2857 #endif
2858 memTypeFlag == VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2)
2859 {
2860 surfDesc->ulBuffer = drmPrimeSurfaceDescriptor.objects[0].fd;
2861 surfDesc->modifier = drmPrimeSurfaceDescriptor.objects[0].drm_format_modifier;
2862 surfDesc->uiSize = drmPrimeSurfaceDescriptor.objects[0].size;
2863 surfDesc->uiBuffserSize = drmPrimeSurfaceDescriptor.objects[0].size;
2864 if(drmPrimeSurfaceDescriptor.num_layers > 1)
2865 {
2866 surfDesc->uiPlanes = drmPrimeSurfaceDescriptor.num_layers;
2867 for (uint32_t k = 0; k < surfDesc->uiPlanes; k ++)
2868 {
2869 surfDesc->uiPitches[k] = drmPrimeSurfaceDescriptor.layers[k].pitch[0];
2870 surfDesc->uiOffsets[k] = drmPrimeSurfaceDescriptor.layers[k].offset[0];
2871 }
2872 }
2873 else
2874 {
2875 surfDesc->uiPlanes = drmPrimeSurfaceDescriptor.layers[0].num_planes;
2876 for(uint32_t k = 0; k < surfDesc->uiPlanes; k ++)
2877 {
2878 surfDesc->uiPitches[k] = drmPrimeSurfaceDescriptor.layers[0].pitch[k];
2879 surfDesc->uiOffsets[k] = drmPrimeSurfaceDescriptor.layers[0].offset[k];
2880 }
2881 }
2882 }
2883 else if (memTypeFlag != VA_SURFACE_ATTRIB_MEM_TYPE_VA) {
2884 surfDesc->uiPlanes = externalBufDescripor.num_planes;
2885 surfDesc->ulBuffer = externalBufDescripor.buffers[i];
2886 surfDesc->uiSize = externalBufDescripor.data_size;
2887 surfDesc->uiBuffserSize = externalBufDescripor.data_size;
2888
2889 eStatus = MOS_SecureMemcpy(surfDesc->uiPitches, sizeof(surfDesc->uiPitches), externalBufDescripor.pitches, sizeof(externalBufDescripor.pitches));
2890 if (eStatus != MOS_STATUS_SUCCESS)
2891 {
2892 DDI_VERBOSEMESSAGE("DDI:Failed to copy surface buffer data!");
2893 MOS_FreeMemAndSetNull(surfDesc);
2894 return VA_STATUS_ERROR_OPERATION_FAILED;
2895 }
2896 eStatus = MOS_SecureMemcpy(surfDesc->uiOffsets, sizeof(surfDesc->uiOffsets), externalBufDescripor.offsets, sizeof(externalBufDescripor.offsets));
2897 if (eStatus != MOS_STATUS_SUCCESS)
2898 {
2899 DDI_VERBOSEMESSAGE("DDI:Failed to copy surface buffer data!");
2900 MOS_FreeMemAndSetNull(surfDesc);
2901 return VA_STATUS_ERROR_OPERATION_FAILED;
2902 }
2903
2904 if( surfIsUserPtr )
2905 {
2906 surfDesc->uiTile = TILING_NONE;
2907 if (surfDesc->ulBuffer % 4096 != 0)
2908 {
2909 MOS_FreeMemory(surfDesc);
2910 DDI_VERBOSEMESSAGE("Buffer Address is invalid");
2911 return VA_STATUS_ERROR_INVALID_PARAMETER;
2912 }
2913 }
2914 }
2915 }
2916 VASurfaceID vaSurfaceID = (VASurfaceID)DdiMedia_CreateRenderTarget(mediaCtx, mediaFmt, width, height, surfDesc, surfaceUsageHint, MOS_MEMPOOL_VIDEOMEMORY);
2917 if (VA_INVALID_ID != vaSurfaceID)
2918 {
2919 surfaces[i] = vaSurfaceID;
2920 }
2921 else
2922 {
2923 // here to release the successful allocated surfaces?
2924 if( nullptr != surfDesc )
2925 {
2926 MOS_FreeMemory(surfDesc);
2927 }
2928 return VA_STATUS_ERROR_ALLOCATION_FAILED;
2929 }
2930 }
2931
2932 MOS_TraceEventExt(EVENT_VA_SURFACE, EVENT_TYPE_END, &num_surfaces, sizeof(uint32_t), surfaces, num_surfaces*sizeof(VAGenericID));
2933 return VA_STATUS_SUCCESS;
2934 }
2935
2936 VAStatus DdiMedia_CreateMfeContextInternal(
2937 VADriverContextP ctx,
2938 VAMFContextID *mfe_context
2939 )
2940 {
2941 DDI_FUNCTION_ENTER();
2942
2943 PDDI_MEDIA_CONTEXT mediaDrvCtx = DdiMedia_GetMediaContext(ctx);
2944 DDI_CHK_NULL(mediaDrvCtx, "nullptr pMediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
2945
2946 DDI_CHK_NULL(mfe_context, "nullptr mfe_context", VA_STATUS_ERROR_INVALID_PARAMETER);
2947 *mfe_context = DDI_MEDIA_INVALID_VACONTEXTID;
2948
2949 if (!mediaDrvCtx->m_caps->IsMfeSupportedOnPlatform(mediaDrvCtx->platform))
2950 {
2951 DDI_VERBOSEMESSAGE("MFE is not supported on the platform!");
2952 return VA_STATUS_ERROR_UNIMPLEMENTED;
2953 }
2954
2955 PDDI_ENCODE_MFE_CONTEXT encodeMfeContext = (PDDI_ENCODE_MFE_CONTEXT)MOS_AllocAndZeroMemory(sizeof(DDI_ENCODE_MFE_CONTEXT));
2956 if (nullptr == encodeMfeContext)
2957 {
2958 MOS_FreeMemory(encodeMfeContext);
2959 return VA_STATUS_ERROR_ALLOCATION_FAILED;
2960 }
2961
2962 DdiMediaUtil_LockMutex(&mediaDrvCtx->MfeMutex);
2963 PDDI_MEDIA_VACONTEXT_HEAP_ELEMENT vaContextHeapElmt = DdiMediaUtil_AllocPVAContextFromHeap(mediaDrvCtx->pMfeCtxHeap);
2964 if (nullptr == vaContextHeapElmt)
2965 {
2966 DdiMediaUtil_UnLockMutex(&mediaDrvCtx->MfeMutex);
2967 MOS_FreeMemory(encodeMfeContext);
2968 return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
2969 }
2970
2971 vaContextHeapElmt->pVaContext = (void*)encodeMfeContext;
2972 mediaDrvCtx->uiNumMfes++;
2973 *mfe_context = (VAMFContextID)(vaContextHeapElmt->uiVaContextID + DDI_MEDIA_VACONTEXTID_OFFSET_MFE);
2974 DdiMediaUtil_UnLockMutex(&mediaDrvCtx->MfeMutex);
2975
2976 // Create shared state, which is used by all the sub contexts
2977 MfeSharedState *mfeEncodeSharedState = (MfeSharedState*)MOS_AllocAndZeroMemory(sizeof(MfeSharedState));
2978 if (nullptr == mfeEncodeSharedState)
2979 {
2980 MOS_FreeMemory(encodeMfeContext);
2981 return VA_STATUS_ERROR_ALLOCATION_FAILED;
2982 }
2983
2984 encodeMfeContext->mfeEncodeSharedState = mfeEncodeSharedState;
2985
2986 DdiMediaUtil_InitMutex(&encodeMfeContext->encodeMfeMutex);
2987
2988 return VA_STATUS_SUCCESS;
2989 }
2990
2991 static VAStatus DdiMedia_DestoryMfeContext (
2992 VADriverContextP ctx,
2993 VAMFContextID mfe_context
2994 )
2995 {
2996 DDI_FUNCTION_ENTER();
2997
2998 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
2999 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
3000
3001 uint32_t ctxType = DDI_MEDIA_CONTEXT_TYPE_NONE;
3002 PDDI_ENCODE_MFE_CONTEXT encodeMfeContext = (PDDI_ENCODE_MFE_CONTEXT)DdiMedia_GetContextFromContextID(ctx, mfe_context, &ctxType);
3003 DDI_CHK_NULL(encodeMfeContext, "nullptr encodeMfeContext", VA_STATUS_ERROR_INVALID_CONTEXT);
3004
3005 // Release std::vector memory
3006 encodeMfeContext->pDdiEncodeContexts.clear();
3007 encodeMfeContext->pDdiEncodeContexts.shrink_to_fit();
3008
3009 encodeMfeContext->mfeEncodeSharedState->encoders.clear();
3010 encodeMfeContext->mfeEncodeSharedState->encoders.shrink_to_fit();
3011
3012 DdiMediaUtil_DestroyMutex(&encodeMfeContext->encodeMfeMutex);
3013 MOS_FreeMemory(encodeMfeContext->mfeEncodeSharedState);
3014 MOS_FreeMemory(encodeMfeContext);
3015
3016 DdiMediaUtil_LockMutex(&mediaCtx->MfeMutex);
3017 DdiMediaUtil_ReleasePVAContextFromHeap(mediaCtx->pMfeCtxHeap, (mfe_context & DDI_MEDIA_MASK_VACONTEXTID));
3018 mediaCtx->uiNumMfes--;
3019 DdiMediaUtil_UnLockMutex(&mediaCtx->MfeMutex);
3020 return VA_STATUS_SUCCESS;
3021 }
3022
3023 VAStatus DdiMedia_AddContextInternal(
3024 VADriverContextP ctx,
3025 VAContextID context,
3026 VAMFContextID mfe_context
3027 )
3028 {
3029 DDI_FUNCTION_ENTER();
3030
3031 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
3032 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
3033
3034 uint32_t ctxType = DDI_MEDIA_CONTEXT_TYPE_NONE;
3035 PDDI_ENCODE_MFE_CONTEXT encodeMfeContext = (PDDI_ENCODE_MFE_CONTEXT)DdiMedia_GetContextFromContextID(ctx, mfe_context, &ctxType);
3036 DDI_CHK_NULL(encodeMfeContext, "nullptr encodeMfeContext", VA_STATUS_ERROR_INVALID_CONTEXT);
3037
3038 if (ctxType != DDI_MEDIA_CONTEXT_TYPE_MFE)
3039 {
3040 return VA_STATUS_ERROR_OPERATION_FAILED;
3041 }
3042
3043 PDDI_ENCODE_CONTEXT encodeContext = DdiEncode_GetEncContextFromContextID(ctx, context);
3044 DDI_CHK_NULL(encodeContext, "nullptr encodeContext", VA_STATUS_ERROR_INVALID_CONTEXT);
3045
3046 CodechalEncoderState *encoder = dynamic_cast<CodechalEncoderState *>(encodeContext->pCodecHal);
3047 DDI_CHK_NULL(encoder, "nullptr codechal encoder", VA_STATUS_ERROR_INVALID_CONTEXT);
3048
3049 if (!mediaCtx->m_caps->IsMfeSupportedEntrypoint(encodeContext->vaEntrypoint))
3050 {
3051 return VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT;
3052 }
3053
3054 if (!mediaCtx->m_caps->IsMfeSupportedProfile(encodeContext->vaProfile))
3055 {
3056 return VA_STATUS_ERROR_UNSUPPORTED_PROFILE;
3057 }
3058
3059 DdiMediaUtil_LockMutex(&encodeMfeContext->encodeMfeMutex);
3060 encodeMfeContext->pDdiEncodeContexts.push_back(encodeContext);
3061
3062 if (encodeMfeContext->currentStreamId == 0)
3063 {
3064 encodeMfeContext->isFEI = (encodeContext->vaEntrypoint == VAEntrypointFEI) ? true : false;
3065 }
3066
3067 //MFE cannot support legacy and FEI together
3068 if ((encodeContext->vaEntrypoint != VAEntrypointFEI && encodeMfeContext->isFEI) ||
3069 (encodeContext->vaEntrypoint == VAEntrypointFEI && !encodeMfeContext->isFEI))
3070 {
3071 DdiMediaUtil_UnLockMutex(&encodeMfeContext->encodeMfeMutex);
3072 return VA_STATUS_ERROR_INVALID_CONTEXT;
3073 }
3074
3075 encoder->m_mfeEnabled = true;
3076 // Assign one unique id to this sub context/stream
3077 encoder->m_mfeEncodeParams.streamId = encodeMfeContext->currentStreamId;
3078
3079 MOS_STATUS eStatus = encoder->SetMfeSharedState(encodeMfeContext->mfeEncodeSharedState);
3080 if (eStatus != MOS_STATUS_SUCCESS)
3081 {
3082 DDI_ASSERTMESSAGE(
3083 "Failed to set MFE Shared State for encoder #%d",
3084 encodeMfeContext->currentStreamId);
3085
3086 encoder->m_mfeEnabled = false;
3087
3088 DdiMediaUtil_UnLockMutex(&encodeMfeContext->encodeMfeMutex);
3089 return VA_STATUS_ERROR_OPERATION_FAILED;
3090 }
3091
3092 encodeMfeContext->currentStreamId++;
3093 DdiMediaUtil_UnLockMutex(&encodeMfeContext->encodeMfeMutex);
3094 return VA_STATUS_SUCCESS;
3095 }
3096
3097 VAStatus DdiMedia_ReleaseContextInternal(
3098 VADriverContextP ctx,
3099 VAContextID context,
3100 VAMFContextID mfe_context
3101 )
3102 {
3103 DDI_FUNCTION_ENTER();
3104
3105 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
3106 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
3107
3108 uint32_t ctxType = DDI_MEDIA_CONTEXT_TYPE_NONE;
3109 PDDI_ENCODE_MFE_CONTEXT encodeMfeContext = (PDDI_ENCODE_MFE_CONTEXT)DdiMedia_GetContextFromContextID(ctx, mfe_context, &ctxType);
3110 DDI_CHK_NULL(encodeMfeContext, "nullptr encodeMfeContext", VA_STATUS_ERROR_INVALID_CONTEXT);
3111
3112 if (ctxType != DDI_MEDIA_CONTEXT_TYPE_MFE ||
3113 encodeMfeContext->pDdiEncodeContexts.size() == 0)
3114 {
3115 return VA_STATUS_ERROR_OPERATION_FAILED;
3116 }
3117
3118 PDDI_ENCODE_CONTEXT encodeContext = DdiEncode_GetEncContextFromContextID(ctx, context);
3119 DDI_CHK_NULL(encodeMfeContext, "nullptr encodeContext", VA_STATUS_ERROR_INVALID_CONTEXT);
3120
3121 bool contextErased = false;
3122 DdiMediaUtil_LockMutex(&encodeMfeContext->encodeMfeMutex);
3123 for (uint32_t i = 0; i < encodeMfeContext->pDdiEncodeContexts.size(); i++)
3124 {
3125 if (encodeMfeContext->pDdiEncodeContexts[i] == encodeContext)
3126 {
3127 encodeMfeContext->pDdiEncodeContexts.erase(encodeMfeContext->pDdiEncodeContexts.begin() + i);
3128 contextErased = true;
3129 break;
3130 }
3131 }
3132
3133 if (!contextErased)
3134 {
3135 DdiMediaUtil_UnLockMutex(&encodeMfeContext->encodeMfeMutex);
3136 return VA_STATUS_ERROR_OPERATION_FAILED;
3137 }
3138
3139 DdiMediaUtil_UnLockMutex(&encodeMfeContext->encodeMfeMutex);
3140
3141 return VA_STATUS_SUCCESS;
3142 }
3143
3144 VAStatus DdiMedia_CreateContext (
3145 VADriverContextP ctx,
3146 VAConfigID config_id,
3147 int32_t picture_width,
3148 int32_t picture_height,
3149 int32_t flag,
3150 VASurfaceID *render_targets,
3151 int32_t num_render_targets,
3152 VAContextID *context
3153 )
3154 {
3155 DDI_FUNCTION_ENTER();
3156
3157 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
3158 DDI_CHK_NULL(context, "nullptr context", VA_STATUS_ERROR_INVALID_PARAMETER);
3159
3160 PDDI_MEDIA_CONTEXT mediaDrvCtx = DdiMedia_GetMediaContext(ctx);
3161 DDI_CHK_NULL(mediaDrvCtx, "nullptr mediaDrvCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
3162
3163 if(num_render_targets > 0)
3164 {
3165 DDI_CHK_NULL(render_targets, "nullptr render_targets", VA_STATUS_ERROR_INVALID_PARAMETER);
3166 DDI_CHK_NULL(mediaDrvCtx->pSurfaceHeap, "nullptr mediaDrvCtx->pSurfaceHeap", VA_STATUS_ERROR_INVALID_CONTEXT);
3167 for(int32_t i = 0; i < num_render_targets; i++)
3168 {
3169 uint32_t surfaceId = (uint32_t)render_targets[i];
3170 DDI_CHK_LESS(surfaceId, mediaDrvCtx->pSurfaceHeap->uiAllocatedHeapElements, "Invalid Surface", VA_STATUS_ERROR_INVALID_SURFACE);
3171 }
3172 }
3173
3174 VAStatus vaStatus = VA_STATUS_SUCCESS;
3175 if(mediaDrvCtx->m_caps->IsDecConfigId(config_id))
3176 {
3177 vaStatus = DdiDecode_CreateContext(ctx, config_id - DDI_CODEC_GEN_CONFIG_ATTRIBUTES_DEC_BASE, picture_width, picture_height, flag, render_targets, num_render_targets, context);
3178 }
3179 else if(mediaDrvCtx->m_caps->IsEncConfigId(config_id))
3180 {
3181 vaStatus = DdiEncode_CreateContext(ctx, config_id - DDI_CODEC_GEN_CONFIG_ATTRIBUTES_ENC_BASE, picture_width, picture_height, flag, render_targets, num_render_targets, context);
3182 }
3183 else if(mediaDrvCtx->m_caps->IsVpConfigId(config_id))
3184 {
3185 vaStatus = DdiVp_CreateContext(ctx, config_id - DDI_VP_GEN_CONFIG_ATTRIBUTES_BASE, picture_width, picture_height, flag, render_targets, num_render_targets, context);
3186 }
3187 else
3188 {
3189 DDI_ASSERTMESSAGE("DDI: Invalid config_id");
3190 vaStatus = VA_STATUS_ERROR_INVALID_CONFIG;
3191 }
3192
3193 return vaStatus;
3194 }
3195
3196 VAStatus DdiMedia_DestroyContext (
3197 VADriverContextP ctx,
3198 VAContextID context
3199 )
3200 {
3201 DDI_FUNCTION_ENTER();
3202
3203 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
3204
3205 uint32_t ctxType = DDI_MEDIA_CONTEXT_TYPE_NONE;
3206 void *ctxPtr = DdiMedia_GetContextFromContextID(ctx, context, &ctxType);
3207
3208 switch (ctxType)
3209 {
3210 case DDI_MEDIA_CONTEXT_TYPE_DECODER:
3211 return DdiDecode_DestroyContext(ctx, context);
3212 case DDI_MEDIA_CONTEXT_TYPE_ENCODER:
3213 return DdiEncode_DestroyContext(ctx, context);
3214 case DDI_MEDIA_CONTEXT_TYPE_VP:
3215 return DdiVp_DestroyContext(ctx, context);
3216 case DDI_MEDIA_CONTEXT_TYPE_MFE:
3217 return DdiMedia_DestoryMfeContext(ctx, context);
3218 default:
3219 DDI_ASSERTMESSAGE("DDI: unsupported context in DdiCodec_DestroyContext.");
3220 return VA_STATUS_ERROR_INVALID_CONTEXT;
3221 }
3222 }
3223
3224 VAStatus DdiMedia_CreateBuffer (
3225 VADriverContextP ctx,
3226 VAContextID context,
3227 VABufferType type,
3228 uint32_t size,
3229 uint32_t num_elements,
3230 void *data,
3231 VABufferID *bufId
3232 )
3233 {
3234 DDI_FUNCTION_ENTER();
3235 int32_t event[] = {size, num_elements, type};
3236 MOS_TraceEventExt(EVENT_VA_BUFFER, EVENT_TYPE_START, event, sizeof(event), nullptr, 0);
3237
3238 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
3239 DDI_CHK_NULL(bufId, "nullptr buf_id", VA_STATUS_ERROR_INVALID_PARAMETER);
3240 DDI_CHK_LARGER(size, 0, "Invalid size", VA_STATUS_ERROR_INVALID_PARAMETER);
3241
3242 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
3243 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
3244
3245 uint32_t ctxType = DDI_MEDIA_CONTEXT_TYPE_NONE;
3246 void *ctxPtr = DdiMedia_GetContextFromContextID(ctx, context, &ctxType);
3247 DDI_CHK_NULL(ctxPtr, "nullptr ctxPtr", VA_STATUS_ERROR_INVALID_CONTEXT);
3248
3249 *bufId = VA_INVALID_ID;
3250
3251 DdiMediaUtil_LockMutex(&mediaCtx->BufferMutex);
3252 VAStatus va = VA_STATUS_SUCCESS;
3253 switch (ctxType)
3254 {
3255 case DDI_MEDIA_CONTEXT_TYPE_DECODER:
3256 va = DdiDecode_CreateBuffer(ctx, DdiDecode_GetDecContextFromPVOID(ctxPtr), type, size, num_elements, data, bufId);
3257 break;
3258 case DDI_MEDIA_CONTEXT_TYPE_ENCODER:
3259 va = DdiEncode_CreateBuffer(ctx, context, type, size, num_elements, data, bufId);
3260 break;
3261 case DDI_MEDIA_CONTEXT_TYPE_VP:
3262 va = DdiVp_CreateBuffer(ctx, ctxPtr, type, size, num_elements, data, bufId);
3263 break;
3264 case DDI_MEDIA_CONTEXT_TYPE_PROTECTED:
3265 va = DdiMediaProtected::DdiMedia_ProtectedSessionCreateBuffer(ctx, context, type, size, num_elements, data, bufId);
3266 break;
3267 default:
3268 va = VA_STATUS_ERROR_INVALID_CONTEXT;
3269 }
3270
3271 DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex);
3272 MOS_TraceEventExt(EVENT_VA_BUFFER, EVENT_TYPE_END, bufId, sizeof(bufId), nullptr, 0);
3273 return va;
3274 }
3275
3276 VAStatus DdiMedia_BufferSetNumElements (
3277 VADriverContextP ctx,
3278 VABufferID buf_id,
3279 uint32_t num_elements
3280 )
3281 {
3282 DDI_FUNCTION_ENTER();
3283
3284 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
3285
3286 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
3287 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
3288
3289 DDI_CHK_NULL(mediaCtx->pBufferHeap, "nullptr mediaCtx->pBufferHeap", VA_STATUS_ERROR_INVALID_CONTEXT);
3290 DDI_CHK_LESS((uint32_t)buf_id, mediaCtx->pBufferHeap->uiAllocatedHeapElements, "Invalid buf_id", VA_STATUS_ERROR_INVALID_BUFFER);
3291
3292 DDI_MEDIA_BUFFER *buf = DdiMedia_GetBufferFromVABufferID(mediaCtx, buf_id);
3293 DDI_CHK_NULL(buf, "Invalid buffer.", VA_STATUS_ERROR_INVALID_BUFFER);
3294
3295 // only for VASliceParameterBufferType of buffer, the number of elements can be greater than 1
3296 if(buf->uiType != VASliceParameterBufferType &&
3297 num_elements > 1)
3298 {
3299 return VA_STATUS_ERROR_INVALID_PARAMETER;
3300 }
3301
3302 if(buf->uiType == VASliceParameterBufferType &&
3303 buf->uiNumElements < num_elements)
3304 {
3305 MOS_FreeMemory(buf->pData);
3306 buf->iSize = buf->iSize / buf->uiNumElements;
3307 buf->pData = (uint8_t*)MOS_AllocAndZeroMemory(buf->iSize * num_elements);
3308 buf->iSize = buf->iSize * num_elements;
3309 }
3310
3311 return VA_STATUS_SUCCESS;
3312 }
3313
3314 //!
3315 //! \brief Map data store of the buffer into the client's address space
3316 //! vaCreateBuffer() needs to be called with "data" set to nullptr before
3317 //! calling vaMapBuffer()
3318 //!
3319 //! \param [in] ctx
3320 //! Pointer to VA driver context
3321 //! \param [in] buf_id
3322 //! VA buffer ID
3323 //! \param [out] pbuf
3324 //! Pointer to buffer
3325 //! \param [in] flag
3326 //! Flag
3327 //!
3328 //! \return VAStatus
3329 //! VA_STATUS_SUCCESS if success, else fail reason
3330 //!
3331 VAStatus DdiMedia_MapBufferInternal (
3332 VADriverContextP ctx,
3333 VABufferID buf_id,
3334 void **pbuf,
3335 uint32_t flag
3336 )
3337 {
3338 DDI_FUNCTION_ENTER();
3339 MOS_TraceEventExt(EVENT_VA_MAP, EVENT_TYPE_START, &buf_id, sizeof(buf_id), &flag, sizeof(flag));
3340
3341 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
3342 DDI_CHK_NULL(pbuf, "nullptr pbuf", VA_STATUS_ERROR_INVALID_PARAMETER);
3343
3344 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
3345 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
3346
3347 DDI_CHK_NULL(mediaCtx->pBufferHeap, "nullptr mediaCtx->pBufferHeap", VA_STATUS_ERROR_INVALID_CONTEXT);
3348 DDI_CHK_LESS((uint32_t)buf_id, mediaCtx->pBufferHeap->uiAllocatedHeapElements, "Invalid bufferId", VA_STATUS_ERROR_INVALID_CONTEXT);
3349
3350 DDI_MEDIA_BUFFER *buf = DdiMedia_GetBufferFromVABufferID(mediaCtx, buf_id);
3351 DDI_CHK_NULL(buf, "nullptr buf", VA_STATUS_ERROR_INVALID_BUFFER);
3352
3353 // The context is nullptr when the buffer is created from DdiMedia_DeriveImage
3354 // So doesn't need to check the context for all cases
3355 // Only check the context in dec/enc mode
3356 VAStatus vaStatus = VA_STATUS_SUCCESS;
3357 uint32_t ctxType = DdiMedia_GetCtxTypeFromVABufferID(mediaCtx, buf_id);
3358 void *ctxPtr = nullptr;
3359 DDI_CODEC_COM_BUFFER_MGR *bufMgr = nullptr;
3360 PDDI_ENCODE_CONTEXT encCtx = nullptr;
3361 PDDI_DECODE_CONTEXT decCtx = nullptr;
3362 switch (ctxType)
3363 {
3364 case DDI_MEDIA_CONTEXT_TYPE_VP:
3365 case DDI_MEDIA_CONTEXT_TYPE_PROTECTED:
3366 break;
3367 case DDI_MEDIA_CONTEXT_TYPE_DECODER:
3368 ctxPtr = DdiMedia_GetCtxFromVABufferID(mediaCtx, buf_id);
3369 DDI_CHK_NULL(ctxPtr, "nullptr ctxPtr", VA_STATUS_ERROR_INVALID_CONTEXT);
3370
3371 decCtx = DdiDecode_GetDecContextFromPVOID(ctxPtr);
3372 bufMgr = &(decCtx->BufMgr);
3373 break;
3374 case DDI_MEDIA_CONTEXT_TYPE_ENCODER:
3375 ctxPtr = DdiMedia_GetCtxFromVABufferID(mediaCtx, buf_id);
3376 DDI_CHK_NULL(ctxPtr, "nullptr ctxPtr", VA_STATUS_ERROR_INVALID_CONTEXT);
3377
3378 encCtx = DdiEncode_GetEncContextFromPVOID(ctxPtr);
3379 DDI_CHK_NULL(encCtx, "nullptr encCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
3380 bufMgr = &(encCtx->BufMgr);
3381 break;
3382 case DDI_MEDIA_CONTEXT_TYPE_MEDIA:
3383 break;
3384 default:
3385 return VA_STATUS_ERROR_INVALID_BUFFER;
3386 }
3387
3388 MOS_TraceEventExt(EVENT_VA_MAP, EVENT_TYPE_INFO, &ctxType, sizeof(ctxType), &buf->uiType, sizeof(uint32_t));
3389 switch ((int32_t)buf->uiType)
3390 {
3391 case VASliceDataBufferType:
3392 case VAProtectedSliceDataBufferType:
3393 case VABitPlaneBufferType:
3394 *pbuf = (void *)(buf->pData + buf->uiOffset);
3395 break;
3396
3397 case VASliceParameterBufferType:
3398 ctxPtr = DdiMedia_GetCtxFromVABufferID(mediaCtx, buf_id);
3399 DDI_CHK_NULL(ctxPtr, "nullptr ctxPtr", VA_STATUS_ERROR_INVALID_CONTEXT);
3400
3401 decCtx = DdiDecode_GetDecContextFromPVOID(ctxPtr);
3402 bufMgr = &(decCtx->BufMgr);
3403 switch (decCtx->wMode)
3404 {
3405 case CODECHAL_DECODE_MODE_AVCVLD:
3406 if(decCtx->bShortFormatInUse)
3407 *pbuf = (void *)((uint8_t*)(bufMgr->Codec_Param.Codec_Param_H264.pVASliceParaBufH264Base) + buf->uiOffset);
3408 else
3409 *pbuf = (void *)((uint8_t*)(bufMgr->Codec_Param.Codec_Param_H264.pVASliceParaBufH264) + buf->uiOffset);
3410 break;
3411 case CODECHAL_DECODE_MODE_MPEG2VLD:
3412 *pbuf = (void *)((uint8_t*)(bufMgr->Codec_Param.Codec_Param_MPEG2.pVASliceParaBufMPEG2) + buf->uiOffset);
3413 break;
3414 case CODECHAL_DECODE_MODE_VC1VLD:
3415 *pbuf = (void *)((uint8_t*)(bufMgr->Codec_Param.Codec_Param_VC1.pVASliceParaBufVC1) + buf->uiOffset);
3416 break;
3417 case CODECHAL_DECODE_MODE_JPEG:
3418 *pbuf = (void *)((uint8_t*)(bufMgr->Codec_Param.Codec_Param_JPEG.pVASliceParaBufJPEG) + buf->uiOffset);
3419 break;
3420 case CODECHAL_DECODE_MODE_VP8VLD:
3421 *pbuf = (void *)((uint8_t*)(bufMgr->Codec_Param.Codec_Param_VP8.pVASliceParaBufVP8) + buf->uiOffset);
3422 break;
3423 case CODECHAL_DECODE_MODE_HEVCVLD:
3424 if(decCtx->bShortFormatInUse)
3425 *pbuf = (void *)((uint8_t*)(bufMgr->Codec_Param.Codec_Param_HEVC.pVASliceParaBufBaseHEVC) + buf->uiOffset);
3426 else
3427 {
3428 if(!decCtx->m_ddiDecode->IsRextProfile())
3429 *pbuf = (void *)((uint8_t*)(bufMgr->Codec_Param.Codec_Param_HEVC.pVASliceParaBufHEVC) + buf->uiOffset);
3430 else
3431 *pbuf = (void *)((uint8_t*)(bufMgr->Codec_Param.Codec_Param_HEVC.pVASliceParaBufHEVCRext) + buf->uiOffset);
3432 }
3433 break;
3434 case CODECHAL_DECODE_MODE_VP9VLD:
3435 *pbuf = (void *)((uint8_t*)(bufMgr->Codec_Param.Codec_Param_VP9.pVASliceParaBufVP9) + buf->uiOffset);
3436 break;
3437 case CODECHAL_DECODE_MODE_AV1VLD:
3438 case CODECHAL_DECODE_MODE_VVCVLD:
3439 *pbuf = (void *)((uint8_t*)(bufMgr->pCodecSlcParamReserved) + buf->uiOffset);
3440 break;
3441 default:
3442 break;
3443 }
3444 break;
3445
3446 case VAEncCodedBufferType:
3447 DDI_CHK_NULL(encCtx, "nullptr encCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
3448
3449 if( DdiEncode_CodedBufferExistInStatusReport( encCtx, buf ) )
3450 {
3451 vaStatus = DdiEncode_StatusReport(encCtx, buf, pbuf);
3452 MOS_TraceEventExt(EVENT_VA_MAP, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
3453 return vaStatus;
3454 }
3455 // so far a coded buffer that has NOT been added into status report is skipped frame in non-CP case
3456 // but this can change in future if new usage models come up
3457 encCtx->BufMgr.pCodedBufferSegment->buf = DdiMediaUtil_LockBuffer(buf, flag);
3458 encCtx->BufMgr.pCodedBufferSegment->size = buf->iSize;
3459 *pbuf = encCtx->BufMgr.pCodedBufferSegment;
3460
3461 break;
3462
3463 case VAStatsStatisticsBufferType:
3464 case VAStatsStatisticsBottomFieldBufferType:
3465 case VAStatsMVBufferType:
3466 {
3467 DDI_CHK_NULL(encCtx, "nullptr encCtx", VA_STATUS_ERROR_INVALID_CONTEXT)
3468 DDI_ENCODE_PRE_ENC_BUFFER_TYPE idx = (buf->uiType == VAStatsMVBufferType) ? PRE_ENC_BUFFER_TYPE_MVDATA :
3469 ((buf->uiType == VAStatsStatisticsBufferType) ? PRE_ENC_BUFFER_TYPE_STATS
3470 : PRE_ENC_BUFFER_TYPE_STATS_BOT);
3471 if((encCtx->codecFunction == CODECHAL_FUNCTION_FEI_PRE_ENC) && DdiEncode_PreEncBufferExistInStatusReport( encCtx, buf, idx))
3472 {
3473 vaStatus = DdiEncode_PreEncStatusReport(encCtx, buf, pbuf);
3474 MOS_TraceEventExt(EVENT_VA_MAP, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
3475 return vaStatus;
3476 }
3477 if(buf->bo)
3478 {
3479 *pbuf = DdiMediaUtil_LockBuffer(buf, flag);
3480 }
3481 break;
3482 }
3483 case VAStatsMVPredictorBufferType:
3484 case VAEncFEIMBControlBufferType:
3485 case VAEncFEIMVPredictorBufferType:
3486 case VAEncQPBufferType:
3487 if(buf->bo)
3488 {
3489 *pbuf = DdiMediaUtil_LockBuffer(buf, flag);
3490 }
3491 break;
3492 case VADecodeStreamoutBufferType:
3493 if(buf->bo)
3494 {
3495 uint32_t timeout_NS = 100000000;
3496 while (0 != mos_bo_wait(buf->bo, timeout_NS))
3497 {
3498 // Just loop while gem_bo_wait times-out.
3499 }
3500 *pbuf = DdiMediaUtil_LockBuffer(buf, flag);
3501 }
3502 break;
3503 case VAEncFEIMVBufferType:
3504 case VAEncFEIMBCodeBufferType:
3505 case VAEncFEICURecordBufferType:
3506 case VAEncFEICTBCmdBufferType:
3507 case VAEncFEIDistortionBufferType:
3508 {
3509 DDI_CHK_NULL(encCtx, "nullptr encCtx", VA_STATUS_ERROR_INVALID_CONTEXT)
3510 if(encCtx->wModeType == CODECHAL_ENCODE_MODE_AVC)
3511 {
3512 CodecEncodeAvcFeiPicParams *feiPicParams = (CodecEncodeAvcFeiPicParams *)encCtx->pFeiPicParams;
3513
3514 DDI_ENCODE_FEI_ENC_BUFFER_TYPE idx = (buf->uiType == VAEncFEIMVBufferType) ? FEI_ENC_BUFFER_TYPE_MVDATA :
3515 ((buf->uiType == VAEncFEIMBCodeBufferType) ? FEI_ENC_BUFFER_TYPE_MBCODE :
3516 FEI_ENC_BUFFER_TYPE_DISTORTION);
3517 if((feiPicParams != nullptr) && (encCtx->codecFunction == CODECHAL_FUNCTION_FEI_ENC) && DdiEncode_EncBufferExistInStatusReport( encCtx, buf, idx))
3518 {
3519 vaStatus = DdiEncode_EncStatusReport(encCtx, buf, pbuf);
3520 MOS_TraceEventExt(EVENT_VA_MAP, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
3521 return vaStatus;
3522 }
3523 }
3524 else if(encCtx->wModeType == CODECHAL_ENCODE_MODE_HEVC)
3525
3526 {
3527 CodecEncodeHevcFeiPicParams *feiPicParams = (CodecEncodeHevcFeiPicParams *)encCtx->pFeiPicParams;
3528 DDI_ENCODE_FEI_ENC_BUFFER_TYPE idx = (buf->uiType == VAEncFEICTBCmdBufferType) ? FEI_ENC_BUFFER_TYPE_MVDATA :
3529 ((buf->uiType == VAEncFEICURecordBufferType) ? FEI_ENC_BUFFER_TYPE_MBCODE :
3530 FEI_ENC_BUFFER_TYPE_DISTORTION);
3531 if((feiPicParams != nullptr) && (encCtx->codecFunction == CODECHAL_FUNCTION_FEI_ENC) && DdiEncode_EncBufferExistInStatusReport( encCtx, buf, idx))
3532 {
3533 vaStatus = DdiEncode_EncStatusReport(encCtx, buf, pbuf);
3534 MOS_TraceEventExt(EVENT_VA_MAP, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
3535 return vaStatus;
3536 }
3537 }
3538 if(buf->bo)
3539 {
3540 *pbuf = DdiMediaUtil_LockBuffer(buf, flag);
3541 }
3542 }
3543 break;
3544 case VAStatsStatisticsParameterBufferType:
3545 *pbuf = (void *)(buf->pData + buf->uiOffset);
3546 break;
3547 case VAEncMacroblockMapBufferType:
3548 DdiMediaUtil_LockMutex(&mediaCtx->BufferMutex);
3549 *pbuf = DdiMediaUtil_LockBuffer(buf, flag);
3550 DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex);
3551 MOS_TraceEventExt(EVENT_VA_MAP, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
3552 if (nullptr == (*pbuf))
3553 {
3554 return VA_STATUS_ERROR_OPERATION_FAILED;
3555 }
3556 else
3557 {
3558 return VA_STATUS_SUCCESS;
3559 }
3560 break;
3561
3562 case VAProbabilityBufferType:
3563 *pbuf = (void *)(buf->pData + buf->uiOffset);
3564
3565 break;
3566
3567 case VAEncMacroblockDisableSkipMapBufferType:
3568 if(buf->bo)
3569 {
3570 *pbuf = DdiMediaUtil_LockBuffer(buf, flag);
3571 }
3572 break;
3573
3574 case VAImageBufferType:
3575 default:
3576 if((buf->format != Media_Format_CPU) && (DdiMedia_MediaFormatToOsFormat(buf->format) != VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT))
3577 {
3578 DdiMediaUtil_LockMutex(&mediaCtx->BufferMutex);
3579 // A critical section starts.
3580 // Make sure not to bailout with a return until the section ends.
3581
3582 if (nullptr != buf->pSurface && Media_Format_CPU != buf->format)
3583 {
3584 vaStatus = DdiMedia_MediaMemoryDecompress(mediaCtx, buf->pSurface);
3585 }
3586
3587 if (VA_STATUS_SUCCESS == vaStatus)
3588 {
3589 *pbuf = DdiMediaUtil_LockBuffer(buf, flag);
3590
3591 if (nullptr == *pbuf)
3592 {
3593 vaStatus = VA_STATUS_ERROR_OPERATION_FAILED;
3594 }
3595 }
3596
3597 // The critical section ends.
3598 DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex);
3599 }
3600 else
3601 {
3602 *pbuf = (void *)(buf->pData + buf->uiOffset);
3603 }
3604 break;
3605 }
3606
3607 MOS_TraceEventExt(EVENT_VA_MAP, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
3608 return vaStatus;
3609 }
3610
3611 VAStatus DdiMedia_MapBuffer (
3612 VADriverContextP ctx,
3613 VABufferID buf_id,
3614 void **pbuf
3615 )
3616 {
3617 return DdiMedia_MapBufferInternal(ctx, buf_id, pbuf, MOS_LOCKFLAG_READONLY | MOS_LOCKFLAG_WRITEONLY);
3618 }
3619
3620 VAStatus DdiMedia_UnmapBuffer (
3621 VADriverContextP ctx,
3622 VABufferID buf_id
3623 )
3624 {
3625 DDI_FUNCTION_ENTER();
3626 MOS_TraceEventExt(EVENT_VA_UNMAP, EVENT_TYPE_START, &buf_id, sizeof(buf_id), nullptr, 0);
3627
3628 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
3629
3630 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
3631 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
3632
3633 DDI_CHK_NULL( mediaCtx->pBufferHeap, "nullptr mediaCtx->pBufferHeap", VA_STATUS_ERROR_INVALID_CONTEXT);
3634 DDI_CHK_LESS((uint32_t)buf_id, mediaCtx->pBufferHeap->uiAllocatedHeapElements, "Invalid buf_id", VA_STATUS_ERROR_INVALID_BUFFER);
3635
3636 DDI_MEDIA_BUFFER *buf = DdiMedia_GetBufferFromVABufferID(mediaCtx, buf_id);
3637 DDI_CHK_NULL(buf, "nullptr buf", VA_STATUS_ERROR_INVALID_BUFFER);
3638
3639 // The context is nullptr when the buffer is created from DdiMedia_DeriveImage
3640 // So doesn't need to check the context for all cases
3641 // Only check the context in dec/enc mode
3642 void *ctxPtr = nullptr;
3643 uint32_t ctxType = DdiMedia_GetCtxTypeFromVABufferID(mediaCtx, buf_id);
3644 DDI_CODEC_COM_BUFFER_MGR *bufMgr = nullptr;
3645 PDDI_DECODE_CONTEXT decCtx = nullptr;
3646 PDDI_ENCODE_CONTEXT encCtx = nullptr;
3647 switch (ctxType)
3648 {
3649 case DDI_MEDIA_CONTEXT_TYPE_VP:
3650 case DDI_MEDIA_CONTEXT_TYPE_PROTECTED:
3651 break;
3652 case DDI_MEDIA_CONTEXT_TYPE_DECODER:
3653 ctxPtr = DdiMedia_GetCtxFromVABufferID(mediaCtx, buf_id);
3654 DDI_CHK_NULL(ctxPtr, "nullptr ctxPtr", VA_STATUS_ERROR_INVALID_CONTEXT);
3655
3656 decCtx = DdiDecode_GetDecContextFromPVOID(ctxPtr);
3657 bufMgr = &(decCtx->BufMgr);
3658 break;
3659 case DDI_MEDIA_CONTEXT_TYPE_ENCODER:
3660 ctxPtr = DdiMedia_GetCtxFromVABufferID(mediaCtx, buf_id);
3661 DDI_CHK_NULL(ctxPtr, "nullptr ctxPtr", VA_STATUS_ERROR_INVALID_CONTEXT);
3662
3663 encCtx = DdiEncode_GetEncContextFromPVOID(ctxPtr);
3664 bufMgr = &(encCtx->BufMgr);
3665 break;
3666 case DDI_MEDIA_CONTEXT_TYPE_MEDIA:
3667 break;
3668 default:
3669 return VA_STATUS_ERROR_INVALID_BUFFER;
3670 }
3671
3672 switch ((int32_t)buf->uiType)
3673 {
3674 case VASliceDataBufferType:
3675 case VAProtectedSliceDataBufferType:
3676 case VABitPlaneBufferType:
3677 break;
3678 case VAEncCodedBufferType:
3679 case VAStatsStatisticsBufferType:
3680 case VAStatsStatisticsBottomFieldBufferType:
3681 case VAStatsMVBufferType:
3682 case VAStatsMVPredictorBufferType:
3683 case VAEncFEIMBControlBufferType:
3684 case VAEncFEIMVPredictorBufferType:
3685 case VAEncFEIMVBufferType:
3686 case VAEncFEIMBCodeBufferType:
3687 case VAEncFEICURecordBufferType:
3688 case VAEncFEICTBCmdBufferType:
3689 case VAEncFEIDistortionBufferType:
3690 case VAEncQPBufferType:
3691 case VADecodeStreamoutBufferType:
3692 if(buf->bo)
3693 {
3694 DdiMediaUtil_UnlockBuffer(buf);
3695 }
3696 break;
3697 case VAEncMacroblockDisableSkipMapBufferType:
3698 if(buf->bo)
3699 {
3700 DdiMediaUtil_UnlockBuffer(buf);
3701 }
3702 break;
3703
3704 case VAImageBufferType:
3705 default:
3706 if((buf->format != Media_Format_CPU) &&(DdiMedia_MediaFormatToOsFormat(buf->format) != VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT))
3707 {
3708 DdiMediaUtil_LockMutex(&mediaCtx->BufferMutex);
3709 DdiMediaUtil_UnlockBuffer(buf);
3710 DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex);
3711 }
3712 break;
3713 }
3714
3715 MOS_TraceEventExt(EVENT_VA_UNMAP, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
3716 return VA_STATUS_SUCCESS;
3717 }
3718
3719 VAStatus DdiMedia_DestroyBuffer (
3720 VADriverContextP ctx,
3721 VABufferID buffer_id
3722 )
3723 {
3724 DDI_FUNCTION_ENTER();
3725 MOS_TraceEventExt(EVENT_VA_FREE_BUFFER, EVENT_TYPE_START, &buffer_id, sizeof(buffer_id), nullptr, 0);
3726
3727 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
3728
3729 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
3730 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
3731
3732 DDI_CHK_NULL(mediaCtx->pBufferHeap, "nullptr mediaCtx->pBufferHeap", VA_STATUS_ERROR_INVALID_CONTEXT);
3733 DDI_CHK_LESS((uint32_t)buffer_id, mediaCtx->pBufferHeap->uiAllocatedHeapElements, "Invalid bufferId", VA_STATUS_ERROR_INVALID_CONTEXT);
3734
3735 DDI_MEDIA_BUFFER *buf = DdiMedia_GetBufferFromVABufferID(mediaCtx, buffer_id);
3736 DDI_CHK_NULL(buf, "nullptr buf", VA_STATUS_ERROR_INVALID_BUFFER);
3737
3738 void *ctxPtr = DdiMedia_GetCtxFromVABufferID(mediaCtx, buffer_id);
3739 uint32_t ctxType = DdiMedia_GetCtxTypeFromVABufferID(mediaCtx, buffer_id);
3740
3741 DDI_CODEC_COM_BUFFER_MGR *bufMgr = nullptr;
3742 PDDI_ENCODE_CONTEXT encCtx = nullptr;
3743 PDDI_DECODE_CONTEXT decCtx = nullptr;
3744 switch (ctxType)
3745 {
3746 case DDI_MEDIA_CONTEXT_TYPE_DECODER:
3747 DDI_CHK_NULL(ctxPtr, "nullptr ctxPtr", VA_STATUS_ERROR_INVALID_CONTEXT);
3748 decCtx = DdiDecode_GetDecContextFromPVOID(ctxPtr);
3749 bufMgr = &(decCtx->BufMgr);
3750 break;
3751 case DDI_MEDIA_CONTEXT_TYPE_ENCODER:
3752 DDI_CHK_NULL(ctxPtr, "nullptr ctxPtr", VA_STATUS_ERROR_INVALID_CONTEXT);
3753 encCtx = DdiEncode_GetEncContextFromPVOID(ctxPtr);
3754 bufMgr = &(encCtx->BufMgr);
3755 break;
3756 case DDI_MEDIA_CONTEXT_TYPE_VP:
3757 break;
3758 case DDI_MEDIA_CONTEXT_TYPE_MEDIA:
3759 break;
3760 case DDI_MEDIA_CONTEXT_TYPE_PROTECTED:
3761 break;
3762 default:
3763 return VA_STATUS_ERROR_INVALID_BUFFER;
3764 }
3765 switch ((int32_t)buf->uiType)
3766 {
3767 case VASliceDataBufferType:
3768 case VAProtectedSliceDataBufferType:
3769 DdiMedia_ReleaseBsBuffer(bufMgr, buf);
3770 break;
3771 case VABitPlaneBufferType:
3772 DdiMedia_ReleaseBpBuffer(bufMgr, buf);
3773 break;
3774 case VAProbabilityBufferType:
3775 DdiMedia_ReleaseBpBuffer(bufMgr, buf);
3776 break;
3777
3778 case VASliceParameterBufferType:
3779 DdiMedia_ReleaseSliceControlBuffer(ctxType, ctxPtr, buf);
3780 break;
3781 case VAPictureParameterBufferType:
3782 break;
3783 case VAImageBufferType:
3784 if(buf->format == Media_Format_CPU)
3785 {
3786 MOS_FreeMemory(buf->pData);
3787 }
3788 else
3789 {
3790 DdiMediaUtil_UnRefBufObjInMediaBuffer(buf);
3791
3792 if (buf->uiExportcount) {
3793 buf->bPostponedBufFree = true;
3794 MOS_TraceEventExt(EVENT_VA_FREE_BUFFER, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
3795 return VA_STATUS_SUCCESS;
3796 }
3797 }
3798 break;
3799 case VAProcPipelineParameterBufferType:
3800 case VAProcFilterParameterBufferType:
3801 MOS_FreeMemory(buf->pData);
3802 break;
3803 case VASubsetsParameterBufferType:
3804 case VAIQMatrixBufferType:
3805 case VAHuffmanTableBufferType:
3806 case VAEncSliceParameterBufferType:
3807 case VAEncPictureParameterBufferType:
3808 case VAEncSequenceParameterBufferType:
3809 case VAEncPackedHeaderDataBufferType:
3810 case VAEncPackedHeaderParameterBufferType:
3811 MOS_FreeMemory(buf->pData);
3812 break;
3813 case VAEncMacroblockMapBufferType:
3814 DdiMediaUtil_FreeBuffer(buf);
3815 break;
3816 #ifdef ENABLE_ENC_UNLIMITED_OUTPUT
3817 case VAEncCodedBufferType:
3818 if(nullptr == encCtx)
3819 {
3820 encCtx = DdiEncode_GetEncContextFromPVOID(ctxPtr);
3821 if(nullptr == encCtx)
3822 return VA_STATUS_ERROR_INVALID_CONTEXT;
3823 }
3824 DdiMediaUtil_FreeBuffer(buf);
3825 break;
3826 #endif
3827 case VAStatsStatisticsParameterBufferType:
3828 MOS_FreeMemory(buf->pData);
3829 break;
3830 case VAStatsStatisticsBufferType:
3831 case VAStatsStatisticsBottomFieldBufferType:
3832 case VAStatsMVBufferType:
3833 {
3834 if(nullptr == encCtx)
3835 {
3836 encCtx = DdiEncode_GetEncContextFromPVOID(ctxPtr);
3837 if(nullptr == encCtx)
3838 return VA_STATUS_ERROR_INVALID_CONTEXT;
3839 }
3840 if(encCtx->codecFunction == CODECHAL_FUNCTION_FEI_PRE_ENC)
3841 {
3842 DDI_ENCODE_PRE_ENC_BUFFER_TYPE idx = (buf->uiType == VAStatsMVBufferType) ? PRE_ENC_BUFFER_TYPE_MVDATA :
3843 ((buf->uiType == VAStatsStatisticsBufferType) ? PRE_ENC_BUFFER_TYPE_STATS
3844 : PRE_ENC_BUFFER_TYPE_STATS_BOT);
3845 DdiEncode_RemoveFromPreEncStatusReportQueue(encCtx, buf, idx);
3846 }
3847 }
3848 DdiMediaUtil_FreeBuffer(buf);
3849 break;
3850 case VAStatsMVPredictorBufferType:
3851 case VAEncFEIMBControlBufferType:
3852 case VAEncFEIMVPredictorBufferType:
3853 case VAEncQPBufferType:
3854 case VADecodeStreamoutBufferType:
3855 DdiMediaUtil_FreeBuffer(buf);
3856 break;
3857 case VAEncFEIMVBufferType:
3858 case VAEncFEIMBCodeBufferType:
3859 case VAEncFEICURecordBufferType:
3860 case VAEncFEICTBCmdBufferType:
3861 case VAEncFEIDistortionBufferType:
3862 {
3863 if(nullptr == encCtx)
3864 {
3865 encCtx = DdiEncode_GetEncContextFromPVOID(ctxPtr);
3866 if(nullptr == encCtx)
3867 return VA_STATUS_ERROR_INVALID_CONTEXT;
3868 }
3869 if(encCtx->wModeType == CODECHAL_ENCODE_MODE_AVC)
3870 {
3871 CodecEncodeAvcFeiPicParams *feiPicParams;
3872 feiPicParams = (CodecEncodeAvcFeiPicParams *)(encCtx->pFeiPicParams);
3873 if((feiPicParams != nullptr) && (encCtx->codecFunction == CODECHAL_FUNCTION_FEI_ENC))
3874 {
3875 DDI_ENCODE_FEI_ENC_BUFFER_TYPE idx = (buf->uiType == VAEncFEIMVBufferType) ? FEI_ENC_BUFFER_TYPE_MVDATA :
3876 ((buf->uiType == VAEncFEIMBCodeBufferType) ? FEI_ENC_BUFFER_TYPE_MBCODE :
3877 FEI_ENC_BUFFER_TYPE_DISTORTION);
3878 DdiEncode_RemoveFromEncStatusReportQueue(encCtx, buf, idx);
3879 }
3880
3881 }
3882 else if(encCtx->wModeType == CODECHAL_ENCODE_MODE_HEVC)
3883 {
3884 CodecEncodeHevcFeiPicParams *feiPicParams;
3885 feiPicParams = (CodecEncodeHevcFeiPicParams *)(encCtx->pFeiPicParams);
3886 if((feiPicParams != nullptr) && (encCtx->codecFunction == CODECHAL_FUNCTION_FEI_ENC))
3887 {
3888 DDI_ENCODE_FEI_ENC_BUFFER_TYPE idx = (buf->uiType == VAEncFEICTBCmdBufferType) ? FEI_ENC_BUFFER_TYPE_MVDATA :
3889 ((buf->uiType == VAEncFEICURecordBufferType) ? FEI_ENC_BUFFER_TYPE_MBCODE :
3890 FEI_ENC_BUFFER_TYPE_DISTORTION);
3891 DdiEncode_RemoveFromEncStatusReportQueue(encCtx, buf, idx);
3892 }
3893 }
3894 }
3895 DdiMediaUtil_FreeBuffer(buf);
3896 break;
3897 default: // do not handle any un-listed buffer type
3898 MOS_FreeMemory(buf->pData);
3899 break;
3900 //return va_STATUS_SUCCESS;
3901 }
3902 MOS_FreeMemory(buf);
3903
3904 DdiMedia_DestroyBufFromVABufferID(mediaCtx, buffer_id);
3905 MOS_TraceEventExt(EVENT_VA_FREE_BUFFER, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
3906 return VA_STATUS_SUCCESS;
3907 }
3908
3909 VAStatus DdiMedia_BeginPicture (
3910 VADriverContextP ctx,
3911 VAContextID context,
3912 VASurfaceID render_target
3913 )
3914 {
3915 DDI_FUNCTION_ENTER();
3916
3917 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
3918
3919 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
3920
3921 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
3922 DDI_CHK_NULL(mediaCtx->pSurfaceHeap, "nullptr mediaCtx->pSurfaceHeap", VA_STATUS_ERROR_INVALID_CONTEXT);
3923 DDI_CHK_LESS((uint32_t)render_target, mediaCtx->pSurfaceHeap->uiAllocatedHeapElements, "render_target", VA_STATUS_ERROR_INVALID_SURFACE);
3924
3925 uint32_t ctxType = DDI_MEDIA_CONTEXT_TYPE_NONE;
3926 void *ctxPtr = DdiMedia_GetContextFromContextID(ctx, context, &ctxType);
3927 uint32_t event[] = {(uint32_t)context, ctxType, (uint32_t)render_target};
3928 MOS_TraceEventExt(EVENT_VA_PICTURE, EVENT_TYPE_START, event, sizeof(event), nullptr, 0);
3929
3930 PDDI_MEDIA_SURFACE surface = DdiMedia_GetSurfaceFromVASurfaceID(mediaCtx, render_target);
3931 DDI_CHK_NULL(surface, "nullptr surface", VA_STATUS_ERROR_INVALID_SURFACE);
3932
3933 DdiMediaUtil_LockMutex(&mediaCtx->SurfaceMutex);
3934 surface->curCtxType = ctxType;
3935 surface->curStatusReportQueryState = DDI_MEDIA_STATUS_REPORT_QUERY_STATE_PENDING;
3936 if(ctxType == DDI_MEDIA_CONTEXT_TYPE_VP)
3937 {
3938 surface->curStatusReport.vpp.status = VPREP_NOTAVAILABLE;
3939 }
3940 DdiMediaUtil_UnLockMutex(&mediaCtx->SurfaceMutex);
3941
3942 switch (ctxType)
3943 {
3944 case DDI_MEDIA_CONTEXT_TYPE_DECODER:
3945 return DdiDecode_BeginPicture(ctx, context, render_target);
3946 case DDI_MEDIA_CONTEXT_TYPE_ENCODER:
3947 return DdiEncode_BeginPicture(ctx, context, render_target);
3948 case DDI_MEDIA_CONTEXT_TYPE_VP:
3949 return DdiVp_BeginPicture(ctx, context, render_target);
3950 default:
3951 DDI_ASSERTMESSAGE("DDI: unsupported context in DdiCodec_BeginPicture.");
3952 return VA_STATUS_ERROR_INVALID_CONTEXT;
3953 }
3954 }
3955
3956 VAStatus DdiMedia_RenderPicture (
3957 VADriverContextP ctx,
3958 VAContextID context,
3959 VABufferID *buffers,
3960 int32_t num_buffers
3961 )
3962 {
3963
3964 DDI_FUNCTION_ENTER();
3965
3966 DDI_CHK_NULL( ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
3967 DDI_CHK_NULL( buffers, "nullptr buffers", VA_STATUS_ERROR_INVALID_PARAMETER);
3968 DDI_CHK_LARGER(num_buffers, 0, "Invalid number buffers", VA_STATUS_ERROR_INVALID_PARAMETER);
3969 uint32_t event[] = {(uint32_t)context, (uint32_t)num_buffers};
3970 MOS_TraceEventExt(EVENT_VA_PICTURE, EVENT_TYPE_INFO, event, sizeof(event), buffers, num_buffers*sizeof(VAGenericID));
3971
3972 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
3973 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
3974 DDI_CHK_NULL(mediaCtx->pBufferHeap, "nullptr mediaCtx->pBufferHeap", VA_STATUS_ERROR_INVALID_CONTEXT);
3975
3976 for(int32_t i = 0; i < num_buffers; i++)
3977 {
3978 DDI_CHK_LESS((uint32_t)buffers[i], mediaCtx->pBufferHeap->uiAllocatedHeapElements, "Invalid Buffer", VA_STATUS_ERROR_INVALID_BUFFER);
3979 }
3980
3981 uint32_t ctxType = DDI_MEDIA_CONTEXT_TYPE_NONE;
3982 void *ctxPtr = DdiMedia_GetContextFromContextID(ctx, context, &ctxType);
3983
3984 switch (ctxType)
3985 {
3986 case DDI_MEDIA_CONTEXT_TYPE_DECODER:
3987 return DdiDecode_RenderPicture(ctx, context, buffers, num_buffers);
3988 case DDI_MEDIA_CONTEXT_TYPE_ENCODER:
3989 return DdiEncode_RenderPicture(ctx, context, buffers, num_buffers);
3990 case DDI_MEDIA_CONTEXT_TYPE_VP:
3991 return DdiVp_RenderPicture(ctx, context, buffers, num_buffers);
3992 default:
3993 DDI_ASSERTMESSAGE("DDI: unsupported context in DdiCodec_RenderPicture.");
3994 return VA_STATUS_ERROR_INVALID_CONTEXT;
3995 }
3996
3997 }
3998
3999 VAStatus DdiMedia_EndPicture (
4000 VADriverContextP ctx,
4001 VAContextID context
4002 )
4003 {
4004 DDI_FUNCTION_ENTER();
4005
4006 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
4007
4008 uint32_t ctxType = DDI_MEDIA_CONTEXT_TYPE_NONE;
4009 void *ctxPtr = DdiMedia_GetContextFromContextID(ctx, context, &ctxType);
4010 VAStatus vaStatus = VA_STATUS_SUCCESS;
4011 switch (ctxType)
4012 {
4013 case DDI_MEDIA_CONTEXT_TYPE_DECODER:
4014 vaStatus = DdiDecode_EndPicture(ctx, context);
4015 break;
4016 case DDI_MEDIA_CONTEXT_TYPE_ENCODER:
4017 vaStatus = DdiEncode_EndPicture(ctx, context);
4018 break;
4019 case DDI_MEDIA_CONTEXT_TYPE_VP:
4020 vaStatus = DdiVp_EndPicture(ctx, context);
4021 break;
4022 default:
4023 DDI_ASSERTMESSAGE("DDI: unsupported context in DdiCodec_EndPicture.");
4024 vaStatus = VA_STATUS_ERROR_INVALID_CONTEXT;
4025 }
4026
4027 MOS_TraceEventExt(EVENT_VA_PICTURE, EVENT_TYPE_END, &context, sizeof(context), &vaStatus, sizeof(vaStatus));
4028 PERF_UTILITY_STOP_ONCE("First Frame Time", PERF_MOS, PERF_LEVEL_DDI);
4029
4030 return vaStatus;
4031 }
4032
4033 static VAStatus DdiMedia_StatusCheck (
4034 PDDI_MEDIA_CONTEXT mediaCtx,
4035 DDI_MEDIA_SURFACE *surface,
4036 VASurfaceID surface_id
4037 )
4038 {
4039 DDI_FUNCTION_ENTER();
4040
4041 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
4042 DDI_CHK_NULL(surface, "nullptr surface", VA_STATUS_ERROR_INVALID_CONTEXT);
4043
4044 uint32_t i = 0;
4045 PDDI_DECODE_CONTEXT decCtx = (PDDI_DECODE_CONTEXT)surface->pDecCtx;
4046 if (decCtx && surface->curCtxType == DDI_MEDIA_CONTEXT_TYPE_DECODER)
4047 {
4048 DdiMediaUtil_LockGuard guard(&mediaCtx->SurfaceMutex);
4049
4050 Codechal *codecHal = decCtx->pCodecHal;
4051 //return success just avoid vaDestroyContext is ahead of vaSyncSurface
4052 DDI_CHK_NULL(codecHal, "nullptr decCtx->pCodecHal", VA_STATUS_SUCCESS);
4053
4054 //return success just avoid vaDestroyContext is ahead of vaSyncSurface
4055 if (codecHal->IsApogeiosEnabled())
4056 {
4057 DecodePipelineAdapter *decoder = dynamic_cast<DecodePipelineAdapter *>(codecHal);
4058 DDI_CHK_NULL(decoder, "nullptr (DecodePipelineAdapter *decoder) ", VA_STATUS_SUCCESS);
4059 return DdiDecode_StatusReport(mediaCtx, decoder, surface);
4060 }
4061 else
4062 {
4063 CodechalDecode *decoder = dynamic_cast<CodechalDecode *>(codecHal);
4064 DDI_CHK_NULL(decoder, "nullptr (CodechalDecode *decoder)", VA_STATUS_SUCCESS);
4065 return DdiDecode_StatusReport(mediaCtx, decoder, surface);
4066 }
4067 }
4068
4069 if (surface->curCtxType == DDI_MEDIA_CONTEXT_TYPE_VP)
4070 {
4071 PDDI_VP_CONTEXT vpCtx = (PDDI_VP_CONTEXT)surface->pVpCtx;
4072 DDI_CHK_NULL(vpCtx , "nullptr vpCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
4073 DDI_CHK_NULL(vpCtx->pVpHal ,"nullptr vpCtx->pVpHal", VA_STATUS_ERROR_INVALID_CONTEXT);
4074
4075 QUERY_STATUS_REPORT_APP tempVpReport;
4076 MOS_ZeroMemory(&tempVpReport, sizeof(QUERY_STATUS_REPORT_APP));
4077
4078 // Get reported surfaces' count
4079 uint32_t tableLen = 0;
4080 vpCtx->pVpHal->GetStatusReportEntryLength(&tableLen);
4081
4082 if (tableLen > 0 && surface->curStatusReportQueryState == DDI_MEDIA_STATUS_REPORT_QUERY_STATE_PENDING)
4083 {
4084 // Query the status for all of surfaces which have finished
4085 for(i = 0; i < tableLen; i++)
4086 {
4087 MOS_ZeroMemory(&tempVpReport, sizeof(QUERY_STATUS_REPORT_APP));
4088 vpCtx->pVpHal->GetStatusReport(&tempVpReport, 1);
4089
4090 // StatusFeedBackID is last time submitted Target Surface ID which is set in BeginPicture,
4091 // So we can know the report is for which surface here.
4092 DDI_MEDIA_SURFACE *tempSurface = DdiMedia_GetSurfaceFromVASurfaceID(mediaCtx, tempVpReport.StatusFeedBackID);
4093 if(tempSurface == nullptr)
4094 {
4095 return VA_STATUS_ERROR_OPERATION_FAILED;
4096 }
4097
4098 // Update the status of the surface which is reported.
4099 tempSurface->curStatusReport.vpp.status = (uint32_t)tempVpReport.dwStatus;
4100 tempSurface->curStatusReportQueryState = DDI_MEDIA_STATUS_REPORT_QUERY_STATE_COMPLETED;
4101
4102 if(tempVpReport.StatusFeedBackID == surface_id)
4103 {
4104 break;
4105 }
4106 }
4107 }
4108
4109 if (surface->curStatusReportQueryState == DDI_MEDIA_STATUS_REPORT_QUERY_STATE_COMPLETED)
4110 {
4111 if(surface->curStatusReport.vpp.status == VPREP_OK)
4112 {
4113 return VA_STATUS_SUCCESS;
4114 }
4115 else if(surface->curStatusReport.vpp.status == VPREP_NOTREADY)
4116 {
4117 return mediaCtx->bMediaResetEnable ? VA_STATUS_SUCCESS : VA_STATUS_ERROR_HW_BUSY;
4118 }
4119 else
4120 {
4121 return VA_STATUS_ERROR_OPERATION_FAILED;
4122 }
4123 }
4124 else
4125 {
4126 return VA_STATUS_ERROR_OPERATION_FAILED;
4127 }
4128 }
4129
4130 return VA_STATUS_SUCCESS;
4131
4132 }
4133
4134 VAStatus DdiMedia_SyncSurface (
4135 VADriverContextP ctx,
4136 VASurfaceID render_target
4137 )
4138 {
4139 PERF_UTILITY_AUTO(__FUNCTION__, "ENCODE", "DDI");
4140
4141 DDI_FUNCTION_ENTER();
4142 MOS_TraceEventExt(EVENT_VA_SYNC, EVENT_TYPE_START, &render_target, sizeof(VAGenericID), nullptr, 0);
4143
4144 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
4145
4146 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
4147 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
4148 DDI_CHK_NULL(mediaCtx->pSurfaceHeap, "nullptr mediaCtx->pSurfaceHeap", VA_STATUS_ERROR_INVALID_CONTEXT);
4149
4150 DDI_CHK_LESS((uint32_t)render_target, mediaCtx->pSurfaceHeap->uiAllocatedHeapElements, "Invalid render_target", VA_STATUS_ERROR_INVALID_SURFACE);
4151
4152 DDI_MEDIA_SURFACE *surface = DdiMedia_GetSurfaceFromVASurfaceID(mediaCtx, render_target);
4153 DDI_CHK_NULL(surface, "nullptr surface", VA_STATUS_ERROR_INVALID_CONTEXT);
4154 if (surface->pCurrentFrameSemaphore)
4155 {
4156 DdiMediaUtil_WaitSemaphore(surface->pCurrentFrameSemaphore);
4157 DdiMediaUtil_PostSemaphore(surface->pCurrentFrameSemaphore);
4158 }
4159
4160 MOS_TraceEventExt(EVENT_VA_SYNC, EVENT_TYPE_INFO, surface->bo? &surface->bo->handle:nullptr, sizeof(uint32_t), nullptr, 0);
4161 // check the bo here?
4162 // zero is a expected return value
4163 uint32_t timeout_NS = 100000000;
4164 while (0 != mos_bo_wait(surface->bo, timeout_NS))
4165 {
4166 // Just loop while gem_bo_wait times-out.
4167 }
4168
4169 MOS_TraceEventExt(EVENT_VA_SYNC, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
4170 return DdiMedia_StatusCheck(mediaCtx, surface, render_target);
4171 }
4172
4173 #if VA_CHECK_VERSION(1, 9, 0)
4174 VAStatus DdiMedia_SyncSurface2 (
4175 VADriverContextP ctx,
4176 VASurfaceID surface_id,
4177 uint64_t timeout_ns
4178 )
4179 {
4180 PERF_UTILITY_AUTO(__FUNCTION__, "ENCODE", "DDI");
4181
4182 DDI_FUNCTION_ENTER();
4183 MOS_TraceEventExt(EVENT_VA_SYNC, EVENT_TYPE_START, &surface_id, sizeof(VAGenericID), nullptr, 0);
4184
4185 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
4186
4187 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
4188 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
4189 DDI_CHK_NULL(mediaCtx->pSurfaceHeap, "nullptr mediaCtx->pSurfaceHeap", VA_STATUS_ERROR_INVALID_CONTEXT);
4190
4191 DDI_CHK_LESS((uint32_t)surface_id, mediaCtx->pSurfaceHeap->uiAllocatedHeapElements, "Invalid render_target", VA_STATUS_ERROR_INVALID_SURFACE);
4192
4193 DDI_MEDIA_SURFACE *surface = DdiMedia_GetSurfaceFromVASurfaceID(mediaCtx, surface_id);
4194 DDI_CHK_NULL(surface, "nullptr surface", VA_STATUS_ERROR_INVALID_CONTEXT);
4195 if (surface->pCurrentFrameSemaphore)
4196 {
4197 DdiMediaUtil_WaitSemaphore(surface->pCurrentFrameSemaphore);
4198 DdiMediaUtil_PostSemaphore(surface->pCurrentFrameSemaphore);
4199 }
4200 MOS_TraceEventExt(EVENT_VA_SYNC, EVENT_TYPE_INFO, surface->bo? &surface->bo->handle:nullptr, sizeof(uint32_t), nullptr, 0);
4201
4202 if (timeout_ns == VA_TIMEOUT_INFINITE)
4203 {
4204 // zero is an expected return value when not hit timeout
4205 auto ret = mos_bo_wait(surface->bo, DDI_BO_INFINITE_TIMEOUT);
4206 if (0 != ret)
4207 {
4208 DDI_NORMALMESSAGE("vaSyncSurface2: surface is still used by HW\n\r");
4209 return VA_STATUS_ERROR_TIMEDOUT;
4210 }
4211 }
4212 else
4213 {
4214 int64_t timeoutBoWait1 = 0;
4215 int64_t timeoutBoWait2 = 0;
4216 if (timeout_ns >= DDI_BO_MAX_TIMEOUT)
4217 {
4218 timeoutBoWait1 = DDI_BO_MAX_TIMEOUT - 1;
4219 timeoutBoWait2 = timeout_ns - DDI_BO_MAX_TIMEOUT + 1;
4220 }
4221 else
4222 {
4223 timeoutBoWait1 = (int64_t)timeout_ns;
4224 }
4225
4226 // zero is an expected return value when not hit timeout
4227 auto ret = mos_bo_wait(surface->bo, timeoutBoWait1);
4228 if (0 != ret)
4229 {
4230 if (timeoutBoWait2)
4231 {
4232 ret = mos_bo_wait(surface->bo, timeoutBoWait2);
4233 }
4234 if (0 != ret)
4235 {
4236 DDI_NORMALMESSAGE("vaSyncSurface2: surface is still used by HW\n\r");
4237 return VA_STATUS_ERROR_TIMEDOUT;
4238 }
4239 }
4240 }
4241 MOS_TraceEventExt(EVENT_VA_SYNC, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
4242 return DdiMedia_StatusCheck(mediaCtx, surface, surface_id);
4243 }
4244
4245 VAStatus DdiMedia_SyncBuffer (
4246 VADriverContextP ctx,
4247 VABufferID buf_id,
4248 uint64_t timeout_ns
4249 )
4250 {
4251 PERF_UTILITY_AUTO(__FUNCTION__, "ENCODE", "DDI");
4252
4253 DDI_FUNCTION_ENTER();
4254 MOS_TraceEventExt(EVENT_VA_SYNC, EVENT_TYPE_START, &buf_id, sizeof(buf_id), nullptr, 0);
4255
4256 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
4257
4258 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
4259 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
4260 DDI_CHK_NULL(mediaCtx->pBufferHeap, "nullptr mediaCtx->pBufferHeap", VA_STATUS_ERROR_INVALID_CONTEXT);
4261
4262 DDI_CHK_LESS((uint32_t)buf_id, mediaCtx->pBufferHeap->uiAllocatedHeapElements, "Invalid buffer", VA_STATUS_ERROR_INVALID_BUFFER);
4263
4264 DDI_MEDIA_BUFFER *buffer = DdiMedia_GetBufferFromVABufferID(mediaCtx, buf_id);
4265 DDI_CHK_NULL(buffer, "nullptr buffer", VA_STATUS_ERROR_INVALID_CONTEXT);
4266
4267 MOS_TraceEventExt(EVENT_VA_SYNC, EVENT_TYPE_INFO, buffer->bo? &buffer->bo->handle:nullptr, sizeof(uint32_t), nullptr, 0);
4268 if (timeout_ns == VA_TIMEOUT_INFINITE)
4269 {
4270 // zero is a expected return value when not hit timeout
4271 auto ret = mos_bo_wait(buffer->bo, DDI_BO_INFINITE_TIMEOUT);
4272 if (0 != ret)
4273 {
4274 DDI_NORMALMESSAGE("vaSyncBuffer: buffer is still used by HW\n\r");
4275 return VA_STATUS_ERROR_TIMEDOUT;
4276 }
4277 }
4278 else
4279 {
4280 int64_t timeoutBoWait1 = 0;
4281 int64_t timeoutBoWait2 = 0;
4282 if (timeout_ns >= DDI_BO_MAX_TIMEOUT)
4283 {
4284 timeoutBoWait1 = DDI_BO_MAX_TIMEOUT - 1;
4285 timeoutBoWait2 = timeout_ns - DDI_BO_MAX_TIMEOUT + 1;
4286 }
4287 else
4288 {
4289 timeoutBoWait1 = (int64_t)timeout_ns;
4290 }
4291
4292 // zero is a expected return value when not hit timeout
4293 auto ret = mos_bo_wait(buffer->bo, timeoutBoWait1);
4294 if (0 != ret)
4295 {
4296 if (timeoutBoWait2)
4297 {
4298 ret = mos_bo_wait(buffer->bo, timeoutBoWait2);
4299 }
4300 if (0 != ret)
4301 {
4302 DDI_NORMALMESSAGE("vaSyncBuffer: buffer is still used by HW\n\r");
4303 return VA_STATUS_ERROR_TIMEDOUT;
4304 }
4305 }
4306 }
4307 MOS_TraceEventExt(EVENT_VA_SYNC, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
4308 return VA_STATUS_SUCCESS;
4309 }
4310 #endif
4311
4312 VAStatus DdiMedia_QuerySurfaceStatus (
4313 VADriverContextP ctx,
4314 VASurfaceID render_target,
4315 VASurfaceStatus *status
4316 )
4317 {
4318 DDI_FUNCTION_ENTER();
4319
4320 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
4321 DDI_CHK_NULL(status, "nullptr status", VA_STATUS_ERROR_INVALID_PARAMETER);
4322
4323 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
4324 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
4325 DDI_CHK_NULL(mediaCtx->pSurfaceHeap, "nullptr mediaCtx->pSurfaceHeap", VA_STATUS_ERROR_INVALID_CONTEXT);
4326
4327 DDI_CHK_LESS((uint32_t)render_target, mediaCtx->pSurfaceHeap->uiAllocatedHeapElements, "Invalid render_target", VA_STATUS_ERROR_INVALID_SURFACE);
4328 DDI_MEDIA_SURFACE *surface = DdiMedia_GetSurfaceFromVASurfaceID(mediaCtx, render_target);
4329 DDI_CHK_NULL(surface, "nullptr surface", VA_STATUS_ERROR_INVALID_SURFACE);
4330
4331 if (surface->pCurrentFrameSemaphore)
4332 {
4333 if(DdiMediaUtil_TryWaitSemaphore(surface->pCurrentFrameSemaphore) == 0)
4334 {
4335 DdiMediaUtil_PostSemaphore(surface->pCurrentFrameSemaphore);
4336 }
4337 else
4338 {
4339 // Return busy state if the surface is not submitted
4340 *status = VASurfaceRendering;
4341 return VA_STATUS_SUCCESS;
4342 }
4343 }
4344
4345 // Query the busy state of bo.
4346 // check the bo here?
4347 if(mos_bo_busy(surface->bo))
4348 {
4349 // busy
4350 *status = VASurfaceRendering;
4351 }
4352 else
4353 {
4354 // idle
4355 *status = VASurfaceReady;
4356 }
4357
4358 return VA_STATUS_SUCCESS;
4359 }
4360
4361 VAStatus DdiMedia_QuerySurfaceError(
4362 VADriverContextP ctx,
4363 VASurfaceID render_target,
4364 VAStatus error_status,
4365 void **error_info /*out*/
4366 )
4367 {
4368 DDI_UNUSED(error_status);
4369
4370 DDI_FUNCTION_ENTER();
4371
4372 DDI_CHK_NULL( ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT );
4373
4374 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
4375 DDI_CHK_NULL( mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
4376
4377 DDI_MEDIA_SURFACE *surface = DdiMedia_GetSurfaceFromVASurfaceID(mediaCtx, render_target);
4378 DDI_CHK_NULL(surface, "nullptr surface", VA_STATUS_ERROR_INVALID_SURFACE);
4379
4380 PDDI_DECODE_CONTEXT decCtx = (PDDI_DECODE_CONTEXT)surface->pDecCtx;
4381 DDI_CHK_NULL( decCtx, "nullptr surface->pDecCtx", VA_STATUS_ERROR_INVALID_CONTEXT );
4382
4383 VASurfaceDecodeMBErrors *surfaceErrors = decCtx->vaSurfDecErrOutput;
4384 DDI_CHK_NULL(surfaceErrors , "nullptr surfaceErrors", VA_STATUS_ERROR_INVALID_CONTEXT );
4385
4386 VAStatus vaStatus = VA_STATUS_SUCCESS;
4387
4388 DdiMediaUtil_LockMutex(&mediaCtx->SurfaceMutex);
4389 if (surface->curStatusReportQueryState == DDI_MEDIA_STATUS_REPORT_QUERY_STATE_COMPLETED)
4390 {
4391 if (error_status != -1 && surface->curCtxType == DDI_MEDIA_CONTEXT_TYPE_DECODER)
4392 {
4393 if (surface->curStatusReport.decode.status == CODECHAL_STATUS_ERROR ||
4394 surface->curStatusReport.decode.status == CODECHAL_STATUS_RESET)
4395 {
4396 surfaceErrors[1].status = -1;
4397 surfaceErrors[0].status = 1;
4398 surfaceErrors[0].start_mb = 0;
4399 surfaceErrors[0].end_mb = 0;
4400 surfaceErrors[0].num_mb = surface->curStatusReport.decode.errMbNum;
4401 #if VA_CHECK_VERSION(1, 20, 0)
4402 surfaceErrors[0].decode_error_type = (surface->curStatusReport.decode.status == CODECHAL_STATUS_RESET) ? VADecodeReset : VADecodeMBError;
4403 #else
4404 surfaceErrors[0].decode_error_type = VADecodeMBError;
4405 #endif
4406 *error_info = surfaceErrors;
4407 DdiMediaUtil_UnLockMutex(&mediaCtx->SurfaceMutex);
4408 return VA_STATUS_SUCCESS;
4409 }
4410 #if VA_CHECK_VERSION(1, 20, 0)
4411 else if (surface->curStatusReport.decode.status == CODECHAL_STATUS_INCOMPLETE ||
4412 surface->curStatusReport.decode.status == CODECHAL_STATUS_UNAVAILABLE)
4413 {
4414 MOS_ZeroMemory(&surfaceErrors[0], sizeof(VASurfaceDecodeMBErrors));
4415 surfaceErrors[1].status = -1;
4416 surfaceErrors[0].status = 1;
4417 surfaceErrors[0].decode_error_type = VADecodeReset;
4418 *error_info = surfaceErrors;
4419 DdiMediaUtil_UnLockMutex(&mediaCtx->SurfaceMutex);
4420 return VA_STATUS_SUCCESS;
4421 }
4422 #endif
4423 }
4424
4425 if (error_status == -1 && surface->curCtxType == DDI_MEDIA_CONTEXT_TYPE_DECODER)
4426 //&& surface->curStatusReport.decode.status == CODECHAL_STATUS_SUCCESSFUL) // get the crc value whatever the status is
4427 {
4428 CodechalDecode *decoder = dynamic_cast<CodechalDecode *>(decCtx->pCodecHal);
4429
4430 if (nullptr == decoder)
4431 {
4432 DDI_ASSERTMESSAGE("nullptr codechal decoder");
4433 vaStatus = VA_STATUS_ERROR_INVALID_CONTEXT;
4434 }
4435 else
4436 {
4437 if (decoder->GetStandard() != CODECHAL_AVC)
4438 {
4439 vaStatus = VA_STATUS_ERROR_UNIMPLEMENTED;
4440 }
4441 else
4442 {
4443 *error_info = (void *)&surface->curStatusReport.decode.crcValue;
4444 }
4445 }
4446
4447 DdiMediaUtil_UnLockMutex(&mediaCtx->SurfaceMutex);
4448 return vaStatus;
4449 }
4450
4451 if (surface->curCtxType == DDI_MEDIA_CONTEXT_TYPE_VP &&
4452 surface->curStatusReport.vpp.status == CODECHAL_STATUS_ERROR)
4453 {
4454 DdiMediaUtil_UnLockMutex(&mediaCtx->SurfaceMutex);
4455 return VA_STATUS_SUCCESS;
4456 }
4457 }
4458
4459 surfaceErrors[0].status = -1;
4460 DdiMediaUtil_UnLockMutex(&mediaCtx->SurfaceMutex);
4461 return VA_STATUS_SUCCESS;
4462 }
4463
4464 VAStatus DdiMedia_QuerySurfaceAttributes(
4465 VADriverContextP ctx,
4466 VAConfigID config_id,
4467 VASurfaceAttrib *attrib_list,
4468 uint32_t *num_attribs
4469 )
4470 {
4471 DDI_FUNCTION_ENTER();
4472
4473 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
4474 DDI_CHK_NULL(num_attribs, "nullptr num_attribs", VA_STATUS_ERROR_INVALID_PARAMETER);
4475
4476 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
4477 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
4478 DDI_CHK_NULL(mediaCtx->m_caps, "nullptr m_caps", VA_STATUS_ERROR_INVALID_CONTEXT);
4479
4480 return mediaCtx->m_caps->QuerySurfaceAttributes(config_id,
4481 attrib_list, num_attribs);
4482 }
4483
4484 VAStatus DdiMedia_PutSurface(
4485 VADriverContextP ctx,
4486 VASurfaceID surface,
4487 void* draw,
4488 int16_t srcx,
4489 int16_t srcy,
4490 uint16_t srcw,
4491 uint16_t srch,
4492 int16_t destx,
4493 int16_t desty,
4494 uint16_t destw,
4495 uint16_t desth,
4496 VARectangle *cliprects, /* client supplied clip list */
4497 uint32_t number_cliprects, /* number of clip rects in the clip list */
4498 uint32_t flags /* de-interlacing flags */
4499 )
4500 {
4501 DDI_FUNCTION_ENTER();
4502
4503 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_PARAMETER);
4504 if(number_cliprects > 0)
4505 {
4506 DDI_CHK_NULL(cliprects, "nullptr cliprects", VA_STATUS_ERROR_INVALID_PARAMETER);
4507 }
4508
4509 void *vpCtx = nullptr;
4510 PDDI_MEDIA_CONTEXT mediaDrvCtx = DdiMedia_GetMediaContext(ctx);
4511
4512 DDI_CHK_NULL(mediaDrvCtx, "nullptr mediaDrvCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
4513 DDI_CHK_NULL(mediaDrvCtx->pSurfaceHeap, "nullptr mediaDrvCtx->pSurfaceHeap", VA_STATUS_ERROR_INVALID_CONTEXT);
4514
4515 DDI_CHK_LESS((uint32_t)surface, mediaDrvCtx->pSurfaceHeap->uiAllocatedHeapElements, "Invalid surface", VA_STATUS_ERROR_INVALID_SURFACE);
4516
4517 if (nullptr != mediaDrvCtx->pVpCtxHeap->pHeapBase)
4518 {
4519 uint32_t ctxType = DDI_MEDIA_CONTEXT_TYPE_NONE;
4520 vpCtx = DdiMedia_GetContextFromContextID(ctx, (VAContextID)(0 + DDI_MEDIA_VACONTEXTID_OFFSET_VP), &ctxType);
4521 }
4522
4523 #if defined(ANDROID) || !defined(X11_FOUND)
4524 return VA_STATUS_ERROR_UNIMPLEMENTED;
4525 #else
4526 if(nullptr == vpCtx)
4527 {
4528 VAContextID context = VA_INVALID_ID;
4529 VAStatus vaStatus = DdiVp_CreateContext(ctx, 0, 0, 0, 0, 0, 0, &context);
4530 DDI_CHK_RET(vaStatus, "Create VP Context failed");
4531 }
4532 return DdiCodec_PutSurfaceLinuxHW(ctx, surface, draw, srcx, srcy, srcw, srch, destx, desty, destw, desth, cliprects, number_cliprects, flags);
4533 #endif
4534
4535 }
4536
4537 VAStatus DdiMedia_QueryImageFormats (
4538 VADriverContextP ctx,
4539 VAImageFormat *format_list,
4540 int32_t *num_formats
4541 )
4542 {
4543 DDI_FUNCTION_ENTER();
4544
4545 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
4546 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx.", VA_STATUS_ERROR_INVALID_PARAMETER);
4547 DDI_CHK_NULL(mediaCtx->m_caps, "nullptr pointer.", VA_STATUS_ERROR_INVALID_PARAMETER);
4548 return mediaCtx->m_caps->QueryImageFormats(format_list, num_formats);
4549 }
4550
4551 VAStatus DdiMedia_CreateImage(
4552 VADriverContextP ctx,
4553 VAImageFormat *format,
4554 int32_t width,
4555 int32_t height,
4556 VAImage *image /* out */
4557 )
4558 {
4559 DDI_FUNCTION_ENTER();
4560
4561 DDI_CHK_NULL(ctx, "Invalid context!", VA_STATUS_ERROR_INVALID_CONTEXT);
4562 DDI_CHK_NULL(format, "Invalid format!", VA_STATUS_ERROR_INVALID_PARAMETER);
4563 DDI_CHK_NULL(image, "Invalid image!", VA_STATUS_ERROR_INVALID_PARAMETER);
4564 DDI_CHK_LARGER(width, 0, "Invalid width!", VA_STATUS_ERROR_INVALID_PARAMETER);
4565 DDI_CHK_LARGER(height, 0, "Invalid height!", VA_STATUS_ERROR_INVALID_PARAMETER);
4566 int32_t event[] = {width, height, format->fourcc};
4567 MOS_TraceEventExt(EVENT_VA_IMAGE, EVENT_TYPE_START, event, sizeof(event), nullptr, 0);
4568
4569 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
4570 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx.", VA_STATUS_ERROR_INVALID_PARAMETER);
4571 DDI_CHK_NULL(mediaCtx->pGmmClientContext, "nullptr mediaCtx->pGmmClientContext.", VA_STATUS_ERROR_INVALID_PARAMETER);
4572
4573 VAImage *vaimg = (VAImage*)MOS_AllocAndZeroMemory(sizeof(VAImage));
4574 DDI_CHK_NULL(vaimg, "Insufficient to allocate an VAImage.", VA_STATUS_ERROR_ALLOCATION_FAILED);
4575
4576 GMM_RESCREATE_PARAMS gmmParams;
4577 GMM_RESOURCE_INFO *gmmResourceInfo;
4578 MOS_ZeroMemory(&gmmParams, sizeof(gmmParams));
4579
4580 gmmParams.BaseWidth = width;
4581 gmmParams.BaseHeight = height;
4582 gmmParams.ArraySize = 1;
4583 gmmParams.Type = RESOURCE_2D;
4584 gmmParams.Flags.Gpu.Video = true;
4585 gmmParams.Format = mediaCtx->m_caps->ConvertFourccToGmmFmt(format->fourcc);
4586
4587 if (gmmParams.Format == GMM_FORMAT_INVALID)
4588 {
4589 MOS_FreeMemory(vaimg);
4590 return VA_STATUS_ERROR_UNIMPLEMENTED;
4591 }
4592 gmmResourceInfo = mediaCtx->pGmmClientContext->CreateResInfoObject(&gmmParams);
4593 if(nullptr == gmmResourceInfo)
4594 {
4595 DDI_ASSERTMESSAGE("Gmm Create Resource Failed.");
4596 MOS_FreeMemory(vaimg);
4597 return VA_STATUS_ERROR_ALLOCATION_FAILED;
4598 }
4599
4600 // Get offset from GMM
4601 GMM_REQ_OFFSET_INFO reqInfo = {0};
4602 reqInfo.Plane = GMM_PLANE_U;
4603 reqInfo.ReqRender = 1;
4604 gmmResourceInfo->GetOffset(reqInfo);
4605 uint32_t offsetU = reqInfo.Render.Offset;
4606 MOS_ZeroMemory(&reqInfo, sizeof(GMM_REQ_OFFSET_INFO));
4607 reqInfo.Plane = GMM_PLANE_V;
4608 reqInfo.ReqRender = 1;
4609 gmmResourceInfo->GetOffset(reqInfo);
4610 uint32_t offsetV = reqInfo.Render.Offset;
4611 uint32_t size = (uint32_t)gmmResourceInfo->GetSizeSurface();
4612 uint32_t pitch = (uint32_t)gmmResourceInfo->GetRenderPitch();
4613 vaimg->format = *format;
4614 vaimg->format.byte_order = VA_LSB_FIRST;
4615 vaimg->width = width;
4616 vaimg->height = height;
4617 vaimg->data_size = size;
4618
4619 mediaCtx->pGmmClientContext->DestroyResInfoObject(gmmResourceInfo);
4620
4621 switch(format->fourcc)
4622 {
4623 case VA_FOURCC_RGBA:
4624 case VA_FOURCC_BGRA:
4625 case VA_FOURCC_ARGB:
4626 case VA_FOURCC_ABGR:
4627 case VA_FOURCC_BGRX:
4628 case VA_FOURCC_RGBX:
4629 case VA_FOURCC_XRGB:
4630 case VA_FOURCC_XBGR:
4631 case VA_FOURCC_A2R10G10B10:
4632 case VA_FOURCC_A2B10G10R10:
4633 case VA_FOURCC_X2R10G10B10:
4634 case VA_FOURCC_X2B10G10R10:
4635 case VA_FOURCC_R8G8B8:
4636 case VA_FOURCC_RGB565:
4637 case VA_FOURCC_UYVY:
4638 case VA_FOURCC_YUY2:
4639 case VA_FOURCC_VYUY:
4640 case VA_FOURCC_YVYU:
4641 case VA_FOURCC_AYUV:
4642 #if VA_CHECK_VERSION(1, 13, 0)
4643 case VA_FOURCC_XYUV:
4644 #endif
4645 case VA_FOURCC_Y210:
4646 #if VA_CHECK_VERSION(1, 9, 0)
4647 case VA_FOURCC_Y212:
4648 #endif
4649 case VA_FOURCC_Y216:
4650 case VA_FOURCC_Y410:
4651 #if VA_CHECK_VERSION(1, 9, 0)
4652 case VA_FOURCC_Y412:
4653 #endif
4654 case VA_FOURCC_Y416:
4655 case VA_FOURCC_Y800:
4656 vaimg->num_planes = 1;
4657 vaimg->pitches[0] = pitch;
4658 vaimg->offsets[0] = 0;
4659 break;
4660 case VA_FOURCC_NV12:
4661 case VA_FOURCC_NV21:
4662 case VA_FOURCC_P010:
4663 case VA_FOURCC_P012:
4664 case VA_FOURCC_P016:
4665 vaimg->num_planes = 2;
4666 vaimg->pitches[0] = pitch;
4667 vaimg->pitches[1] = pitch;
4668 vaimg->offsets[0] = 0;
4669 vaimg->offsets[1] = offsetU;
4670 if (height % 2 == 1)
4671 {
4672 vaimg->data_size += pitch;
4673 }
4674 break;
4675 case VA_FOURCC_YV12:
4676 vaimg->num_planes = 3;
4677 vaimg->pitches[0] = pitch;
4678 vaimg->pitches[1] = pitch / 2;
4679 vaimg->pitches[2] = pitch / 2;
4680 vaimg->offsets[0] = 0;
4681 vaimg->offsets[1] = offsetV;
4682 vaimg->offsets[2] = offsetU;
4683 break;
4684 case VA_FOURCC_I420:
4685 vaimg->num_planes = 3;
4686 vaimg->pitches[0] = pitch;
4687 vaimg->pitches[1] = pitch / 2;
4688 vaimg->pitches[2] = pitch / 2;
4689 vaimg->offsets[0] = 0;
4690 vaimg->offsets[1] = offsetU;
4691 vaimg->offsets[2] = offsetV;
4692 break;
4693 case VA_FOURCC_IMC3:
4694 case VA_FOURCC_411P:
4695 case VA_FOURCC_422V:
4696 case VA_FOURCC_422H:
4697 case VA_FOURCC_444P:
4698 case VA_FOURCC_RGBP:
4699 case VA_FOURCC_BGRP:
4700 vaimg->num_planes = 3;
4701 vaimg->pitches[0] = pitch;
4702 vaimg->pitches[1] = pitch;
4703 vaimg->pitches[2] = pitch;
4704 vaimg->offsets[0] = 0;
4705 vaimg->offsets[1] = offsetU;
4706 vaimg->offsets[2] = offsetV;
4707 break;
4708 default:
4709 MOS_FreeMemory(vaimg);
4710 return VA_STATUS_ERROR_UNIMPLEMENTED;
4711 }
4712
4713 DDI_MEDIA_BUFFER *buf = (DDI_MEDIA_BUFFER *)MOS_AllocAndZeroMemory(sizeof(DDI_MEDIA_BUFFER));
4714 if (nullptr == buf)
4715 {
4716 MOS_FreeMemory(vaimg);
4717 return VA_STATUS_ERROR_ALLOCATION_FAILED;
4718 }
4719 buf->uiNumElements = 1;
4720 buf->iSize = vaimg->data_size;
4721 buf->uiType = VAImageBufferType;
4722 buf->format = Media_Format_CPU;//DdiCodec_OsFormatToMediaFormat(vaimg->format.fourcc); //Media_Format_Buffer;
4723 buf->uiOffset = 0;
4724 buf->pMediaCtx = mediaCtx;
4725
4726 //Put Image in untiled buffer for better CPU access?
4727 VAStatus status= DdiMediaUtil_CreateBuffer(buf, mediaCtx->pDrmBufMgr);
4728 if((status != VA_STATUS_SUCCESS))
4729 {
4730 MOS_FreeMemory(vaimg);
4731 MOS_FreeMemory(buf);
4732 return status;
4733 }
4734 buf->TileType = TILING_NONE;
4735
4736 DdiMediaUtil_LockMutex(&mediaCtx->BufferMutex);
4737 PDDI_MEDIA_BUFFER_HEAP_ELEMENT bufferHeapElement = DdiMediaUtil_AllocPMediaBufferFromHeap(mediaCtx->pBufferHeap);
4738
4739 if (nullptr == bufferHeapElement)
4740 {
4741 DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex);
4742 MOS_FreeMemory(vaimg);
4743 DdiMediaUtil_FreeBuffer(buf);
4744 MOS_FreeMemory(buf);
4745 return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
4746 }
4747
4748 bufferHeapElement->pBuffer = buf;
4749 bufferHeapElement->pCtx = nullptr;
4750 bufferHeapElement->uiCtxType = DDI_MEDIA_CONTEXT_TYPE_MEDIA;
4751
4752 vaimg->buf = bufferHeapElement->uiVaBufferID;
4753 mediaCtx->uiNumBufs++;
4754 DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex);
4755
4756 DdiMediaUtil_LockMutex(&mediaCtx->ImageMutex);
4757 PDDI_MEDIA_IMAGE_HEAP_ELEMENT imageHeapElement = DdiMediaUtil_AllocPVAImageFromHeap(mediaCtx->pImageHeap);
4758 if (nullptr == imageHeapElement)
4759 {
4760 DdiMediaUtil_UnLockMutex(&mediaCtx->ImageMutex);
4761 MOS_FreeMemory(vaimg);
4762 return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
4763 }
4764 imageHeapElement->pImage = vaimg;
4765 mediaCtx->uiNumImages++;
4766 vaimg->image_id = imageHeapElement->uiVaImageID;
4767 DdiMediaUtil_UnLockMutex(&mediaCtx->ImageMutex);
4768
4769 *image = *vaimg;
4770 MOS_TraceEventExt(EVENT_VA_IMAGE, EVENT_TYPE_END, &vaimg->image_id, sizeof(VAGenericID), nullptr, 0);
4771 return VA_STATUS_SUCCESS;
4772 }
4773
4774 //!
4775 //! \brief Derive image
4776 //!
4777 //! \param [in] ctx
4778 //! Pointer to VA driver context
4779 //! \param [in] surface
4780 //! VA surface ID
4781 //! \param [in] image
4782 //! VA image
4783 //!
4784 //! \return VAStatus
4785 //! VA_STATUS_SUCCESS if success, else fail reason
4786 //!
4787 VAStatus DdiMedia_DeriveImage (
4788 VADriverContextP ctx,
4789 VASurfaceID surface,
4790 VAImage *image
4791 )
4792 {
4793 DDI_FUNCTION_ENTER();
4794 MOS_TraceEventExt(EVENT_VA_DERIVE, EVENT_TYPE_START, nullptr, 0, nullptr, 0);
4795
4796 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
4797 DDI_CHK_NULL(image, "nullptr image", VA_STATUS_ERROR_INVALID_PARAMETER);
4798
4799 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
4800 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
4801
4802 DDI_CHK_NULL(mediaCtx->pSurfaceHeap, "nullptr mediaCtx->pSurfaceHeap", VA_STATUS_ERROR_INVALID_CONTEXT);
4803 DDI_CHK_LESS((uint32_t)surface, mediaCtx->pSurfaceHeap->uiAllocatedHeapElements, "Invalid surface", VA_STATUS_ERROR_INVALID_SURFACE);
4804
4805 DDI_MEDIA_SURFACE *mediaSurface = DdiMedia_GetSurfaceFromVASurfaceID(mediaCtx, surface);
4806 DDI_CHK_NULL(mediaSurface, "nullptr mediaSurface", VA_STATUS_ERROR_INVALID_SURFACE);
4807
4808 VAImage *vaimg = (VAImage*)MOS_AllocAndZeroMemory(sizeof(VAImage));
4809 DDI_CHK_NULL(vaimg, "nullptr vaimg", VA_STATUS_ERROR_ALLOCATION_FAILED);
4810
4811 if (mediaSurface->pCurrentFrameSemaphore)
4812 {
4813 DdiMediaUtil_WaitSemaphore(mediaSurface->pCurrentFrameSemaphore);
4814 DdiMediaUtil_PostSemaphore(mediaSurface->pCurrentFrameSemaphore);
4815 }
4816 DdiMediaUtil_LockMutex(&mediaCtx->ImageMutex);
4817 PDDI_MEDIA_IMAGE_HEAP_ELEMENT imageHeapElement = DdiMediaUtil_AllocPVAImageFromHeap(mediaCtx->pImageHeap);
4818 if (nullptr == imageHeapElement)
4819 {
4820 DdiMediaUtil_UnLockMutex(&mediaCtx->ImageMutex);
4821 MOS_FreeMemory(vaimg);
4822 return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
4823 }
4824 imageHeapElement->pImage = vaimg;
4825 mediaCtx->uiNumImages++;
4826 vaimg->image_id = imageHeapElement->uiVaImageID;
4827 DdiMediaUtil_UnLockMutex(&mediaCtx->ImageMutex);
4828
4829 vaimg->format.fourcc = DdiMedia_MediaFormatToOsFormat(mediaSurface->format);
4830 vaimg->width = mediaSurface->iWidth;
4831 vaimg->height = mediaSurface->iRealHeight;
4832 vaimg->format.byte_order = VA_LSB_FIRST;
4833
4834 GMM_RESOURCE_INFO *gmmResourceInfo = mediaSurface->pGmmResourceInfo;
4835 GMM_REQ_OFFSET_INFO reqInfo = {0};
4836 reqInfo.Plane = GMM_PLANE_U;
4837 reqInfo.ReqRender = 1;
4838 gmmResourceInfo->GetOffset(reqInfo);
4839 uint32_t offsetU = reqInfo.Render.Offset;
4840 MOS_ZeroMemory(&reqInfo, sizeof(GMM_REQ_OFFSET_INFO));
4841 reqInfo.Plane = GMM_PLANE_V;
4842 reqInfo.ReqRender = 1;
4843 gmmResourceInfo->GetOffset(reqInfo);
4844 uint32_t offsetV = reqInfo.Render.Offset;
4845 vaimg->data_size = (uint32_t)gmmResourceInfo->GetSizeSurface();
4846
4847 switch( mediaSurface->format )
4848 {
4849 case Media_Format_YV12:
4850 case Media_Format_I420:
4851 case Media_Format_IYUV:
4852 vaimg->format.bits_per_pixel = 12;
4853 vaimg->num_planes = 3;
4854 vaimg->pitches[0] = mediaSurface->iPitch;
4855 vaimg->pitches[1] =
4856 vaimg->pitches[2] = mediaSurface->iPitch / 2;
4857 vaimg->offsets[0] = 0;
4858 vaimg->offsets[1] = mediaSurface->iHeight * mediaSurface->iPitch;
4859 vaimg->offsets[2] = vaimg->offsets[1] + vaimg->pitches[1] * MOS_ALIGN_CEIL(mediaSurface->iHeight, 2) / 2;
4860 break;
4861 case Media_Format_A8B8G8R8:
4862 case Media_Format_R8G8B8A8:
4863 case Media_Format_A8R8G8B8:
4864 vaimg->format.bits_per_pixel = 32;
4865 vaimg->format.alpha_mask = RGB_8BIT_ALPHAMASK;
4866 vaimg->num_planes = 1;
4867 vaimg->pitches[0] = mediaSurface->iPitch;
4868 vaimg->offsets[0] = 0;
4869 break;
4870 case Media_Format_X8R8G8B8:
4871 case Media_Format_X8B8G8R8:
4872 vaimg->format.bits_per_pixel = 32;
4873 vaimg->num_planes = 1;
4874 vaimg->pitches[0] = mediaSurface->iPitch;
4875 vaimg->offsets[0] = 0;
4876 break;
4877 case Media_Format_R10G10B10A2:
4878 case Media_Format_B10G10R10A2:
4879 vaimg->format.bits_per_pixel = 32;
4880 vaimg->format.alpha_mask = RGB_10BIT_ALPHAMASK;
4881 vaimg->num_planes = 1;
4882 vaimg->pitches[0] = mediaSurface->iPitch;
4883 vaimg->offsets[0] = 0;
4884 break;
4885 case Media_Format_R10G10B10X2:
4886 case Media_Format_B10G10R10X2:
4887 vaimg->format.bits_per_pixel = 32;
4888 vaimg->num_planes = 1;
4889 vaimg->pitches[0] = mediaSurface->iPitch;
4890 vaimg->offsets[0] = 0;
4891 break;
4892 case Media_Format_R5G6B5:
4893 vaimg->format.bits_per_pixel = 16;
4894 vaimg->data_size = mediaSurface->iPitch * mediaSurface->iHeight;
4895 vaimg->num_planes = 1;
4896 vaimg->pitches[0] = mediaSurface->iPitch;
4897 vaimg->offsets[0] = 0;
4898 break;
4899 case Media_Format_R8G8B8:
4900 vaimg->format.bits_per_pixel = 24;
4901 vaimg->num_planes = 1;
4902 vaimg->pitches[0] = mediaSurface->iPitch;
4903 vaimg->offsets[0] = 0;
4904 break;
4905 case Media_Format_YUY2:
4906 case Media_Format_UYVY:
4907 vaimg->format.bits_per_pixel = 16;
4908 vaimg->data_size = mediaSurface->iPitch * mediaSurface->iHeight;
4909 vaimg->num_planes = 1;
4910 vaimg->pitches[0] = mediaSurface->iPitch;
4911 vaimg->offsets[0] = 0;
4912 break;
4913 case Media_Format_400P:
4914 vaimg->format.bits_per_pixel = 8;
4915 vaimg->num_planes = 1;
4916 vaimg->pitches[0] = mediaSurface->iPitch;
4917 vaimg->offsets[0] = 0;
4918 break;
4919 case Media_Format_444P:
4920 case Media_Format_RGBP:
4921 case Media_Format_BGRP:
4922 vaimg->format.bits_per_pixel = 24;
4923 vaimg->num_planes = 3;
4924 vaimg->pitches[0] =
4925 vaimg->pitches[1] =
4926 vaimg->pitches[2] = mediaSurface->iPitch;
4927 vaimg->offsets[0] = 0;
4928 vaimg->offsets[1] = mediaSurface->iHeight * mediaSurface->iPitch;
4929 vaimg->offsets[2] = mediaSurface->iHeight * mediaSurface->iPitch * 2;
4930 break;
4931 case Media_Format_IMC3:
4932 vaimg->format.bits_per_pixel = 12;
4933 vaimg->num_planes = 3;
4934 vaimg->pitches[0] =
4935 vaimg->pitches[1] =
4936 vaimg->pitches[2] = mediaSurface->iPitch;
4937 vaimg->offsets[0] = 0;
4938 vaimg->offsets[1] = mediaSurface->iHeight * mediaSurface->iPitch;
4939 vaimg->offsets[2] = mediaSurface->iHeight * mediaSurface->iPitch * 3 / 2;
4940 break;
4941 case Media_Format_411P:
4942 vaimg->format.bits_per_pixel = 12;
4943 vaimg->num_planes = 3;
4944 vaimg->pitches[0] =
4945 vaimg->pitches[1] =
4946 vaimg->pitches[2] = mediaSurface->iPitch;
4947 vaimg->offsets[0] = 0;
4948 vaimg->offsets[1] = mediaSurface->iHeight * mediaSurface->iPitch;
4949 vaimg->offsets[2] = mediaSurface->iHeight * mediaSurface->iPitch * 2;
4950 break;
4951 case Media_Format_422V:
4952 vaimg->format.bits_per_pixel = 16;
4953 vaimg->num_planes = 3;
4954 vaimg->pitches[0] =
4955 vaimg->pitches[1] =
4956 vaimg->pitches[2] = mediaSurface->iPitch;
4957 vaimg->offsets[0] = 0;
4958 vaimg->offsets[1] = mediaSurface->iHeight * mediaSurface->iPitch;
4959 vaimg->offsets[2] = mediaSurface->iHeight * mediaSurface->iPitch * 3 / 2;
4960 break;
4961 case Media_Format_422H:
4962 vaimg->format.bits_per_pixel = 16;
4963 vaimg->num_planes = 3;
4964 vaimg->pitches[0] =
4965 vaimg->pitches[1] =
4966 vaimg->pitches[2] = mediaSurface->iPitch;
4967 vaimg->offsets[0] = 0;
4968 vaimg->offsets[1] = mediaSurface->iHeight * mediaSurface->iPitch;
4969 vaimg->offsets[2] = mediaSurface->iHeight * mediaSurface->iPitch * 2;
4970 break;
4971 case Media_Format_P010:
4972 case Media_Format_P012:
4973 case Media_Format_P016:
4974 vaimg->format.bits_per_pixel = 24;
4975 vaimg->num_planes = 2;
4976 vaimg->pitches[0] = mediaSurface->iPitch;
4977 vaimg->pitches[1] =
4978 vaimg->pitches[2] = mediaSurface->iPitch;
4979 vaimg->offsets[0] = 0;
4980 vaimg->offsets[1] = mediaSurface->iHeight * mediaSurface->iPitch;
4981 vaimg->offsets[2] = vaimg->offsets[1] + 2;
4982 break;
4983 case Media_Format_Y410:
4984 case Media_Format_AYUV:
4985 #if VA_CHECK_VERSION(1, 13, 0)
4986 case Media_Format_XYUV:
4987 #endif
4988 case Media_Format_Y210:
4989 #if VA_CHECK_VERSION(1, 9, 0)
4990 case Media_Format_Y212:
4991 #endif
4992 case Media_Format_Y216:
4993 vaimg->format.bits_per_pixel = 32;
4994 vaimg->data_size = mediaSurface->iPitch * mediaSurface->iHeight;
4995 vaimg->num_planes = 1;
4996 vaimg->pitches[0] = mediaSurface->iPitch;
4997 vaimg->offsets[0] = 0;
4998 break;
4999 #if VA_CHECK_VERSION(1, 9, 0)
5000 case Media_Format_Y412:
5001 #endif
5002 case Media_Format_Y416:
5003 vaimg->format.bits_per_pixel = 64; // packed format [alpha, Y, U, V], 16 bits per channel
5004 vaimg->num_planes = 1;
5005 vaimg->pitches[0] = mediaSurface->iPitch;
5006 vaimg->offsets[0] = 0;
5007 break;
5008 default:
5009 vaimg->format.bits_per_pixel = 12;
5010 vaimg->num_planes = 2;
5011 vaimg->pitches[0] = mediaSurface->iPitch;
5012 vaimg->pitches[1] =
5013 vaimg->pitches[2] = mediaSurface->iPitch;
5014 vaimg->offsets[0] = 0;
5015 if(MEDIA_IS_WA(&mediaCtx->WaTable, WaDisableGmmLibOffsetInDeriveImage))
5016 {
5017 vaimg->offsets[1] = mediaSurface->iHeight * mediaSurface->iPitch;
5018 vaimg->offsets[2] = vaimg->offsets[1] + 1;
5019 }
5020 else
5021 {
5022 vaimg->offsets[1] = offsetU;
5023 vaimg->offsets[2] = offsetV;
5024 }
5025 break;
5026 }
5027
5028 if ((mediaSurface->pSurfDesc != nullptr) && (mediaSurface->pSurfDesc->uiVaMemType == VA_SURFACE_ATTRIB_MEM_TYPE_USER_PTR))
5029 {
5030 vaimg->num_planes = mediaSurface->pSurfDesc->uiPlanes;
5031 for (uint32_t i = 0; i < vaimg->num_planes; i++)
5032 {
5033 vaimg->pitches[i] = mediaSurface->pSurfDesc->uiPitches[i];
5034 vaimg->offsets[i] = mediaSurface->pSurfDesc->uiOffsets[i];
5035 }
5036 }
5037
5038 mediaCtx->m_caps->PopulateColorMaskInfo(&vaimg->format);
5039
5040 DDI_MEDIA_BUFFER *buf = (DDI_MEDIA_BUFFER *)MOS_AllocAndZeroMemory(sizeof(DDI_MEDIA_BUFFER));
5041 if (buf == nullptr)
5042 {
5043 MOS_FreeMemory(vaimg);
5044 MOS_FreeMemory(buf);
5045 return VA_STATUS_ERROR_ALLOCATION_FAILED;
5046 }
5047 buf->uiNumElements = 1;
5048 buf->iSize = vaimg->data_size;
5049 buf->uiType = VAImageBufferType;
5050 buf->format = mediaSurface->format;
5051 buf->uiOffset = 0;
5052
5053 buf->bo = mediaSurface->bo;
5054 buf->format = mediaSurface->format;
5055 buf->TileType = mediaSurface->TileType;
5056 buf->pSurface = mediaSurface;
5057 mos_bo_reference(mediaSurface->bo);
5058
5059 DdiMediaUtil_LockMutex(&mediaCtx->BufferMutex);
5060 PDDI_MEDIA_BUFFER_HEAP_ELEMENT bufferHeapElement = DdiMediaUtil_AllocPMediaBufferFromHeap(mediaCtx->pBufferHeap);
5061
5062 if (nullptr == bufferHeapElement)
5063 {
5064 DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex);
5065 MOS_FreeMemory(vaimg);
5066 MOS_FreeMemory(buf);
5067 return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
5068 }
5069 bufferHeapElement->pBuffer = buf;
5070 bufferHeapElement->pCtx = nullptr;
5071 bufferHeapElement->uiCtxType = DDI_MEDIA_CONTEXT_TYPE_MEDIA;
5072
5073 vaimg->buf = bufferHeapElement->uiVaBufferID;
5074 mediaCtx->uiNumBufs++;
5075 DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex);
5076
5077 *image = *vaimg;
5078
5079 MOS_TraceEventExt(EVENT_VA_DERIVE, EVENT_TYPE_END, &surface, sizeof(surface), &vaimg->image_id, sizeof(VAGenericID));
5080 return VA_STATUS_SUCCESS;
5081 }
5082
5083 //!
5084 //! \brief Free allocated surfaceheap elements
5085 //!
5086 //! \param [in] ctx
5087 //! Pointer to VA driver context
5088 //! \param [in] image
5089 //! VA image ID
5090 //!
5091 //! \return VAStatus
5092 //! VA_STATUS_SUCCESS if success, else fail reason
5093 //!
5094 VAStatus DdiMedia_DestroyImage (
5095 VADriverContextP ctx,
5096 VAImageID image)
5097 {
5098 DDI_FUNCTION_ENTER();
5099 MOS_TraceEventExt(EVENT_VA_FREE_IMAGE, EVENT_TYPE_START, &image, sizeof(image), nullptr, 0);
5100
5101 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
5102 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
5103
5104 DDI_CHK_NULL(mediaCtx, "nullptr Media", VA_STATUS_ERROR_INVALID_CONTEXT);
5105 DDI_CHK_NULL(mediaCtx->pImageHeap, "nullptr mediaCtx->pImageHeap", VA_STATUS_ERROR_INVALID_CONTEXT);
5106 DDI_CHK_LESS((uint32_t)image, mediaCtx->pImageHeap->uiAllocatedHeapElements, "Invalid image", VA_STATUS_ERROR_INVALID_IMAGE);
5107
5108 VAImage *vaImage = DdiMedia_GetVAImageFromVAImageID(mediaCtx, image);
5109 if (vaImage == nullptr)
5110 {
5111 return VA_STATUS_ERROR_INVALID_PARAMETER;
5112 }
5113 DdiMedia_DestroyBuffer(ctx, vaImage->buf);
5114 MOS_FreeMemory(vaImage);
5115
5116 DdiMedia_DestroyImageFromVAImageID(mediaCtx, image);
5117
5118 MOS_TraceEventExt(EVENT_VA_FREE_IMAGE, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
5119 return VA_STATUS_SUCCESS;
5120 }
5121
5122 //!
5123 //! \brief Set image palette
5124 //!
5125 //! \param [in] ctx
5126 //! Pointer to VA driver context
5127 //! \param [in] image
5128 //! VA image ID
5129 //! \param [in] palette
5130 //! Palette
5131 //!
5132 //! \return VAStatus
5133 //! VA_STATUS_ERROR_UNIMPLEMENTED if call success, else fail reason
5134 //!
5135 VAStatus DdiMedia_SetImagePalette(
5136 VADriverContextP ctx,
5137 VAImageID image,
5138 unsigned char *palette
5139 )
5140 {
5141 DDI_UNUSED(ctx);
5142 DDI_UNUSED(image);
5143 DDI_UNUSED(palette);
5144 DDI_FUNCTION_ENTER();
5145
5146 return VA_STATUS_ERROR_UNIMPLEMENTED;
5147 }
5148
5149 VAStatus SwizzleSurface(PDDI_MEDIA_CONTEXT mediaCtx, PGMM_RESOURCE_INFO pGmmResInfo, void *pLockedAddr, uint32_t TileType, uint8_t* pResourceBase, bool bUpload)
5150 {
5151 uint32_t uiSize, uiPitch;
5152 GMM_RES_COPY_BLT gmmResCopyBlt;
5153 uint32_t uiPicHeight;
5154 uint32_t ulSwizzledSize;
5155 VAStatus vaStatus = VA_STATUS_SUCCESS;
5156
5157 DDI_CHK_NULL(pGmmResInfo, "pGmmResInfo is NULL", VA_STATUS_ERROR_OPERATION_FAILED);
5158 DDI_CHK_NULL(pLockedAddr, "pLockedAddr is NULL", VA_STATUS_ERROR_OPERATION_FAILED);
5159 DDI_CHK_NULL(pResourceBase, "pResourceBase is NULL", VA_STATUS_ERROR_ALLOCATION_FAILED);
5160
5161 memset(&gmmResCopyBlt, 0x0, sizeof(GMM_RES_COPY_BLT));
5162 uiPicHeight = pGmmResInfo->GetBaseHeight();
5163 uiSize = pGmmResInfo->GetSizeMainSurface();
5164 uiPitch = pGmmResInfo->GetRenderPitch();
5165 gmmResCopyBlt.Gpu.pData = pLockedAddr;
5166 gmmResCopyBlt.Sys.pData = pResourceBase;
5167 gmmResCopyBlt.Sys.RowPitch = uiPitch;
5168 gmmResCopyBlt.Sys.BufferSize = uiSize;
5169 gmmResCopyBlt.Sys.SlicePitch = uiSize;
5170 gmmResCopyBlt.Blt.Slices = 1;
5171 gmmResCopyBlt.Blt.Upload = bUpload;
5172
5173 if(mediaCtx->pGmmClientContext->IsPlanar(pGmmResInfo->GetResourceFormat()) == true)
5174 {
5175 gmmResCopyBlt.Blt.Width = pGmmResInfo->GetBaseWidth();
5176 gmmResCopyBlt.Blt.Height = uiSize/uiPitch;
5177 }
5178
5179 pGmmResInfo->CpuBlt(&gmmResCopyBlt);
5180
5181 return vaStatus;
5182 }
5183
5184 //!
5185 //! \brief Copy plane from src to dst row by row when src and dst strides are different
5186 //!
5187 //! \param [in] dst
5188 //! Destination plane
5189 //! \param [in] dstPitch
5190 //! Destination plane pitch
5191 //! \param [in] src
5192 //! Source plane
5193 //! \param [in] srcPitch
5194 //! Source plane pitch
5195 //! \param [in] height
5196 //! Plane hight
5197 //!
5198 static void DdiMedia_CopyPlane(
5199 uint8_t *dst,
5200 uint32_t dstPitch,
5201 uint8_t *src,
5202 uint32_t srcPitch,
5203 uint32_t height)
5204 {
5205 uint32_t rowSize = std::min(dstPitch, srcPitch);
5206 for (int y = 0; y < height; y += 1)
5207 {
5208 memcpy(dst, src, rowSize);
5209 dst += dstPitch;
5210 src += srcPitch;
5211 }
5212 }
5213
5214 //!
5215 //! \brief Copy data from surface to image
5216 //!
5217 //! \param [in] ctx
5218 //! Input driver context
5219 //! \param [in] surface
5220 //! Pointer to surface
5221 //! \param [in] image
5222 //! Pointer to image
5223 //!
5224 //! \return VAStatus
5225 //! VA_STATUS_SUCCESS if success, else fail reason
5226 //!
5227 static VAStatus DdiMedia_CopySurfaceToImage(
5228 VADriverContextP ctx,
5229 DDI_MEDIA_SURFACE *surface,
5230 VAImage *image)
5231 {
5232 DDI_FUNCTION_ENTER();
5233
5234 DDI_CHK_NULL(ctx, "nullptr ctx.", VA_STATUS_ERROR_INVALID_CONTEXT);
5235 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
5236 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
5237 DDI_CHK_NULL(surface, "nullptr meida surface.", VA_STATUS_ERROR_INVALID_BUFFER);
5238 uint32_t flag = MOS_LOCKFLAG_READONLY;
5239 VAStatus vaStatus = VA_STATUS_SUCCESS;
5240
5241 //Lock Surface
5242 if ((Media_Format_CPU != surface->format))
5243 {
5244 vaStatus = DdiMedia_MediaMemoryDecompress(mediaCtx, surface);
5245
5246 if (vaStatus != VA_STATUS_SUCCESS)
5247 {
5248 DDI_NORMALMESSAGE("surface Decompression fail, continue next steps.");
5249 }
5250 }
5251
5252 void* surfData = DdiMediaUtil_LockSurface(surface, flag);
5253
5254 if (surfData == nullptr)
5255 {
5256 DDI_ASSERTMESSAGE("nullptr surfData.");
5257 return vaStatus;
5258 }
5259
5260 void *imageData = nullptr;
5261 vaStatus = DdiMedia_MapBuffer(ctx, image->buf, &imageData);
5262 if (vaStatus != VA_STATUS_SUCCESS)
5263 {
5264 DDI_ASSERTMESSAGE("Failed to map buffer.");
5265 DdiMediaUtil_UnlockSurface(surface);
5266 return vaStatus;
5267 }
5268
5269 uint8_t *ySrc = (uint8_t*)surfData;
5270 uint8_t *yDst = (uint8_t*)imageData;
5271
5272 DdiMedia_CopyPlane(yDst, image->pitches[0], ySrc, surface->iPitch, image->height);
5273 if (image->num_planes > 1)
5274 {
5275 uint8_t *uSrc = ySrc + surface->iPitch * surface->iHeight;
5276 uint8_t *uDst = yDst + image->offsets[1];
5277 uint32_t chromaPitch = 0;
5278 uint32_t chromaHeight = 0;
5279 uint32_t imageChromaPitch = 0;
5280 uint32_t imageChromaHeight = 0;
5281 DdiMedia_GetChromaPitchHeight(DdiMedia_MediaFormatToOsFormat(surface->format), surface->iPitch, surface->iHeight, &chromaPitch, &chromaHeight);
5282 DdiMedia_GetChromaPitchHeight(image->format.fourcc, image->pitches[0], image->height, &imageChromaPitch, &imageChromaHeight);
5283 DdiMedia_CopyPlane(uDst, image->pitches[1], uSrc, chromaPitch, imageChromaHeight);
5284
5285 if(image->num_planes > 2)
5286 {
5287 uint8_t *vSrc = uSrc + chromaPitch * chromaHeight;
5288 uint8_t *vDst = yDst + image->offsets[2];
5289 DdiMedia_CopyPlane(vDst, image->pitches[2], vSrc, chromaPitch, imageChromaHeight);
5290 }
5291 }
5292
5293 vaStatus = DdiMedia_UnmapBuffer(ctx, image->buf);
5294 if (vaStatus != VA_STATUS_SUCCESS)
5295 {
5296 DDI_ASSERTMESSAGE("Failed to unmap buffer.");
5297 DdiMediaUtil_UnlockSurface(surface);
5298 return vaStatus;
5299 }
5300
5301 DdiMediaUtil_UnlockSurface(surface);
5302
5303 return vaStatus;
5304 }
5305
5306 //!
5307 //! \brief Retrive surface data into a VAImage
5308 //! \details Image must be in a format supported by the implementation
5309 //!
5310 //! \param [in] ctx
5311 //! Input driver context
5312 //! \param [in] surface
5313 //! Input surface ID of source
5314 //! \param [in] x
5315 //! X offset of the wanted region
5316 //! \param [in] y
5317 //! Y offset of the wanted region
5318 //! \param [in] width
5319 //! Width of the wanted region
5320 //! \param [in] height
5321 //! Height of the wanted region
5322 //! \param [in] image
5323 //! The image ID of the source image
5324 //!
5325 //! \return VAStatus
5326 //! VA_STATUS_SUCCESS if success, else fail reason
5327 //!
5328 VAStatus DdiMedia_GetImage(
5329 VADriverContextP ctx,
5330 VASurfaceID surface,
5331 int32_t x, /* coordinates of the upper left source pixel */
5332 int32_t y,
5333 uint32_t width, /* width and height of the region */
5334 uint32_t height,
5335 VAImageID image
5336 )
5337 {
5338 DDI_FUNCTION_ENTER();
5339 uint32_t event[] = {surface, x, y, width, height, image};
5340 MOS_TraceEventExt(EVENT_VA_GET, EVENT_TYPE_START, &event, sizeof(event), nullptr, 0);
5341
5342 DDI_CHK_NULL(ctx, "nullptr ctx.", VA_STATUS_ERROR_INVALID_CONTEXT);
5343
5344 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
5345 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
5346
5347 DDI_CHK_NULL(mediaCtx->pSurfaceHeap, "nullptr mediaCtx->pSurfaceHeap.", VA_STATUS_ERROR_INVALID_CONTEXT);
5348 DDI_CHK_NULL(mediaCtx->pImageHeap, "nullptr mediaCtx->pImageHeap.", VA_STATUS_ERROR_INVALID_CONTEXT);
5349 DDI_CHK_LESS((uint32_t)surface, mediaCtx->pSurfaceHeap->uiAllocatedHeapElements, "Invalid surface.", VA_STATUS_ERROR_INVALID_SURFACE);
5350 DDI_CHK_LESS((uint32_t)image, mediaCtx->pImageHeap->uiAllocatedHeapElements, "Invalid image.", VA_STATUS_ERROR_INVALID_IMAGE);
5351
5352 VAImage *vaimg = DdiMedia_GetVAImageFromVAImageID(mediaCtx, image);
5353 DDI_CHK_NULL(vaimg, "nullptr vaimg.", VA_STATUS_ERROR_INVALID_IMAGE);
5354
5355 DDI_MEDIA_BUFFER *buf = DdiMedia_GetBufferFromVABufferID(mediaCtx, vaimg->buf);
5356 DDI_CHK_NULL(buf, "nullptr buf.", VA_STATUS_ERROR_INVALID_BUFFER);
5357
5358 DDI_MEDIA_SURFACE *inputSurface = DdiMedia_GetSurfaceFromVASurfaceID(mediaCtx, surface);
5359 DDI_CHK_NULL(inputSurface, "nullptr inputSurface.", VA_STATUS_ERROR_INVALID_SURFACE);
5360 DDI_CHK_NULL(inputSurface->bo, "nullptr inputSurface->bo.", VA_STATUS_ERROR_INVALID_SURFACE);
5361
5362 VAStatus vaStatus = VA_STATUS_SUCCESS;
5363 #ifndef _FULL_OPEN_SOURCE
5364 VASurfaceID target_surface = VA_INVALID_SURFACE;
5365 VASurfaceID output_surface = surface;
5366
5367 if (inputSurface->format != DdiMedia_OsFormatToMediaFormat(vaimg->format.fourcc, vaimg->format.alpha_mask) ||
5368 (width != vaimg->width || height != vaimg->height) &&
5369 (vaimg->format.fourcc != VA_FOURCC_444P &&
5370 vaimg->format.fourcc != VA_FOURCC_422V &&
5371 vaimg->format.fourcc != VA_FOURCC_422H))
5372 {
5373 VAContextID context = VA_INVALID_ID;
5374 //Create VP Context.
5375 vaStatus = DdiVp_CreateContext(ctx, 0, 0, 0, 0, 0, 0, &context);
5376 DDI_CHK_RET(vaStatus, "Create VP Context failed.");
5377
5378 //Create target surface for VP pipeline.
5379 DDI_MEDIA_FORMAT mediaFmt = DdiMedia_OsFormatToMediaFormat(vaimg->format.fourcc, vaimg->format.fourcc);
5380 if (mediaFmt == Media_Format_Count)
5381 {
5382 DDI_ASSERTMESSAGE("Unsupported surface type.");
5383 DdiVp_DestroyContext(ctx, context);
5384 return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
5385 }
5386 PDDI_MEDIA_SURFACE_DESCRIPTOR surfDesc = (PDDI_MEDIA_SURFACE_DESCRIPTOR)MOS_AllocAndZeroMemory(sizeof(DDI_MEDIA_SURFACE_DESCRIPTOR));
5387 if (!surfDesc) {
5388 DDI_ASSERTMESSAGE("nullptr surfDesc.");
5389 DdiVp_DestroyContext(ctx, context);
5390 return VA_STATUS_ERROR_ALLOCATION_FAILED;
5391 }
5392 surfDesc->uiVaMemType = VA_SURFACE_ATTRIB_MEM_TYPE_VA;
5393 int memType = MOS_MEMPOOL_VIDEOMEMORY;
5394 if (MEDIA_IS_SKU(&mediaCtx->SkuTable, FtrLocalMemory))
5395 {
5396 memType = MOS_MEMPOOL_SYSTEMMEMORY;
5397 }
5398 target_surface = (VASurfaceID)DdiMedia_CreateRenderTarget(mediaCtx, mediaFmt, vaimg->width, vaimg->height, surfDesc, VA_SURFACE_ATTRIB_USAGE_HINT_GENERIC, memType);
5399 if (VA_INVALID_SURFACE == target_surface)
5400 {
5401 DDI_ASSERTMESSAGE("Create temp surface failed.");
5402 DdiVp_DestroyContext(ctx, context);
5403 return VA_STATUS_ERROR_ALLOCATION_FAILED;
5404 }
5405
5406 VARectangle srcRect, dstRect;
5407 srcRect.x = x;
5408 srcRect.y = y;
5409 srcRect.width = width;
5410 srcRect.height = height;
5411 dstRect.x = 0;
5412 dstRect.y = 0;
5413 dstRect.width = vaimg->width;
5414 dstRect.height = vaimg->height;
5415
5416 //Execute VP pipeline.
5417 vaStatus = DdiVp_VideoProcessPipeline(ctx, context, surface, &srcRect, target_surface, &dstRect);
5418 if (vaStatus != VA_STATUS_SUCCESS)
5419 {
5420 DDI_ASSERTMESSAGE("VP Pipeline failed.");
5421 DdiMedia_DestroySurfaces(ctx, &target_surface, 1);
5422 DdiVp_DestroyContext(ctx, context);
5423 return vaStatus;
5424 }
5425 vaStatus = DdiMedia_SyncSurface(ctx, target_surface);
5426 if (vaStatus != VA_STATUS_SUCCESS)
5427 {
5428 DDI_ASSERTMESSAGE("VP Sync surface failed.");
5429 }
5430 vaStatus = DdiVp_DestroyContext(ctx, context);
5431 if (vaStatus != VA_STATUS_SUCCESS)
5432 {
5433 DDI_ASSERTMESSAGE("VP Destroy Context failed.");
5434 }
5435 output_surface = target_surface;
5436 }
5437
5438 //Get Media Surface from output surface ID
5439 DDI_MEDIA_SURFACE *mediaSurface = DdiMedia_GetSurfaceFromVASurfaceID(mediaCtx, output_surface);
5440 DDI_CHK_NULL(mediaSurface, "nullptr mediaSurface.", VA_STATUS_ERROR_INVALID_SURFACE);
5441 DDI_CHK_NULL(mediaSurface->bo, "nullptr mediaSurface->bo.", VA_STATUS_ERROR_INVALID_SURFACE);
5442
5443 vaStatus = DdiMedia_CopySurfaceToImage(ctx, mediaSurface, vaimg);
5444
5445 if (vaStatus != MOS_STATUS_SUCCESS)
5446 {
5447 DDI_ASSERTMESSAGE("Failed to copy surface to image buffer data!");
5448 if(target_surface != VA_INVALID_SURFACE)
5449 {
5450 DdiMedia_DestroySurfaces(ctx, &target_surface, 1);
5451 }
5452 return vaStatus;
5453 }
5454
5455 //Destroy temp surface if created
5456 if(target_surface != VA_INVALID_SURFACE)
5457 {
5458 DdiMedia_DestroySurfaces(ctx, &target_surface, 1);
5459 }
5460 #else
5461 vaStatus = DdiMedia_CopySurfaceToImage(ctx, inputSurface, vaimg);
5462 DDI_CHK_RET(vaStatus, "Copy surface to image failed.");
5463 #endif
5464 MOS_TraceEventExt(EVENT_VA_GET, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
5465 return VA_STATUS_SUCCESS;
5466 }
5467
5468 //!
5469 //! \brief Copy data from a VAImage to a surface
5470 //! \details Image must be in a format supported by the implementation
5471 //!
5472 //! \param [in] ctx
5473 //! Input driver context
5474 //! \param [in] surface
5475 //! Surface ID of destination
5476 //! \param [in] image
5477 //! The image ID of the destination image
5478 //! \param [in] src_x
5479 //! Source x offset of the image region
5480 //! \param [in] src_y
5481 //! Source y offset of the image region
5482 //! \param [in] src_width
5483 //! Source width offset of the image region
5484 //! \param [in] src_height
5485 //! Source height offset of the image region
5486 //! \param [in] dest_x
5487 //! Destination x offset of the surface region
5488 //! \param [in] dest_y
5489 //! Destination y offset of the surface region
5490 //! \param [in] dest_width
5491 //! Destination width offset of the surface region
5492 //! \param [in] dest_height
5493 //! Destination height offset of the surface region
5494 //!
5495 //! \return VAStatus
5496 //! VA_STATUS_SUCCESS if success, else fail reason
5497 //!
5498 VAStatus DdiMedia_PutImage(
5499 VADriverContextP ctx,
5500 VASurfaceID surface,
5501 VAImageID image,
5502 int32_t src_x,
5503 int32_t src_y,
5504 uint32_t src_width,
5505 uint32_t src_height,
5506 int32_t dest_x,
5507 int32_t dest_y,
5508 uint32_t dest_width,
5509 uint32_t dest_height
5510 )
5511 {
5512 DDI_FUNCTION_ENTER();
5513 uint32_t event[] = {surface, image, src_x, src_y, src_width, src_height, dest_x, dest_y, dest_width, dest_height};
5514 MOS_TraceEventExt(EVENT_VA_PUT, EVENT_TYPE_START, &event, sizeof(event), nullptr, 0);
5515
5516 DDI_CHK_NULL(ctx, "nullptr ctx.", VA_STATUS_ERROR_INVALID_CONTEXT);
5517
5518 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
5519 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx.", VA_STATUS_ERROR_INVALID_CONTEXT);
5520
5521 DDI_CHK_NULL(mediaCtx->pSurfaceHeap, "nullptr mediaCtx->pSurfaceHeap.", VA_STATUS_ERROR_INVALID_CONTEXT);
5522 DDI_CHK_NULL(mediaCtx->pImageHeap, "nullptr mediaCtx->pImageHeap.", VA_STATUS_ERROR_INVALID_CONTEXT);
5523 DDI_CHK_LESS((uint32_t)surface, mediaCtx->pSurfaceHeap->uiAllocatedHeapElements, "Invalid surface.", VA_STATUS_ERROR_INVALID_SURFACE);
5524 DDI_CHK_LESS((uint32_t)image, mediaCtx->pImageHeap->uiAllocatedHeapElements, "Invalid image.", VA_STATUS_ERROR_INVALID_IMAGE);
5525
5526 DDI_MEDIA_SURFACE *mediaSurface = DdiMedia_GetSurfaceFromVASurfaceID(mediaCtx, surface);
5527 DDI_CHK_NULL(mediaSurface, "nullptr mediaSurface.", VA_STATUS_ERROR_INVALID_SURFACE);
5528 DDI_CHK_NULL(mediaSurface->bo, "Invalid buffer.", VA_STATUS_ERROR_INVALID_BUFFER);
5529
5530 if (mediaSurface->pCurrentFrameSemaphore)
5531 {
5532 DdiMediaUtil_WaitSemaphore(mediaSurface->pCurrentFrameSemaphore);
5533 DdiMediaUtil_PostSemaphore(mediaSurface->pCurrentFrameSemaphore);
5534 }
5535
5536 VAImage *vaimg = DdiMedia_GetVAImageFromVAImageID(mediaCtx, image);
5537 DDI_CHK_NULL(vaimg, "Invalid image.", VA_STATUS_ERROR_INVALID_IMAGE);
5538
5539 DDI_MEDIA_BUFFER *buf = DdiMedia_GetBufferFromVABufferID(mediaCtx, vaimg->buf);
5540 DDI_CHK_NULL(buf, "Invalid buffer.", VA_STATUS_ERROR_INVALID_BUFFER);
5541
5542 VAStatus vaStatus = VA_STATUS_SUCCESS;
5543 void *imageData = nullptr;
5544
5545 vaStatus = DdiMedia_MapBuffer(ctx, vaimg->buf, &imageData);
5546 DDI_CHK_RET(vaStatus, "MapBuffer failed.");
5547 DDI_CHK_NULL(imageData, "nullptr imageData.", VA_STATUS_ERROR_INVALID_IMAGE);
5548
5549 // VP Pipeline will be called for CSC/Scaling if the surface format or data size is not consistent with image.
5550 if (mediaSurface->format != DdiMedia_OsFormatToMediaFormat(vaimg->format.fourcc, vaimg->format.alpha_mask) ||
5551 dest_width != src_width || dest_height != src_height ||
5552 src_x != 0 || dest_x != 0 || src_y != 0 || dest_y != 0)
5553 {
5554 VAContextID context = VA_INVALID_ID;
5555
5556 //Create VP Context.
5557 vaStatus = DdiVp_CreateContext(ctx, 0, 0, 0, 0, 0, 0, &context);
5558 DDI_CHK_RET(vaStatus, "Create VP Context failed");
5559
5560 //Create temp surface for VP pipeline.
5561 DDI_MEDIA_FORMAT mediaFmt = DdiMedia_OsFormatToMediaFormat(vaimg->format.fourcc, vaimg->format.fourcc);
5562 if (mediaFmt == Media_Format_Count)
5563 {
5564 DDI_ASSERTMESSAGE("Unsupported surface type.");
5565 return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
5566 }
5567
5568 int memType = MOS_MEMPOOL_VIDEOMEMORY;
5569 if (MEDIA_IS_SKU(&mediaCtx->SkuTable, FtrLocalMemory))
5570 {
5571 memType = MOS_MEMPOOL_SYSTEMMEMORY;
5572 }
5573 VASurfaceID tempSurface = (VASurfaceID)DdiMedia_CreateRenderTarget(mediaCtx, mediaFmt, vaimg->width, vaimg->height, nullptr, VA_SURFACE_ATTRIB_USAGE_HINT_VPP_READ, memType);
5574 if (tempSurface == VA_INVALID_ID)
5575 {
5576 return VA_STATUS_ERROR_ALLOCATION_FAILED;
5577 }
5578
5579 DDI_MEDIA_SURFACE *tempMediaSurface = DdiMedia_GetSurfaceFromVASurfaceID(mediaCtx, tempSurface);
5580 DDI_CHK_NULL(tempMediaSurface, "nullptr tempMediaSurface.", VA_STATUS_ERROR_INVALID_SURFACE);
5581
5582 //Lock Surface
5583 void *tempSurfData = DdiMediaUtil_LockSurface(tempMediaSurface, (MOS_LOCKFLAG_READONLY | MOS_LOCKFLAG_WRITEONLY));
5584 if (nullptr == tempSurfData)
5585 {
5586 DdiMedia_DestroySurfaces(ctx, &tempSurface, 1);
5587 return VA_STATUS_ERROR_SURFACE_BUSY;
5588 }
5589
5590 //Copy data from image to temp surferce
5591 MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
5592 if (tempMediaSurface->data_size >= vaimg->data_size)
5593 {
5594 eStatus = MOS_SecureMemcpy(tempSurfData, tempMediaSurface->data_size, imageData, vaimg->data_size);
5595 }
5596 else
5597 {
5598 eStatus = MOS_SecureMemcpy(tempSurfData, tempMediaSurface->data_size, imageData, tempMediaSurface->data_size);
5599 }
5600 if (eStatus != MOS_STATUS_SUCCESS)
5601 {
5602 DDI_ASSERTMESSAGE("Failed to copy image to surface buffer.");
5603 DdiMediaUtil_UnlockSurface(tempMediaSurface);
5604 DdiMedia_DestroySurfaces(ctx, &tempSurface, 1);
5605 return VA_STATUS_ERROR_OPERATION_FAILED;
5606 }
5607
5608 vaStatus = DdiMedia_UnmapBuffer(ctx, vaimg->buf);
5609 if (vaStatus != VA_STATUS_SUCCESS)
5610 {
5611 DDI_ASSERTMESSAGE("Failed to unmap buffer.");
5612 DdiMediaUtil_UnlockSurface(tempMediaSurface);
5613 DdiMedia_DestroySurfaces(ctx, &tempSurface, 1);
5614 return vaStatus;
5615 }
5616
5617 DdiMediaUtil_UnlockSurface(tempMediaSurface);
5618
5619 VARectangle srcRect, dstRect;
5620 srcRect.x = src_x;
5621 srcRect.y = src_y;
5622 srcRect.width = src_width;
5623 srcRect.height = src_height;
5624 dstRect.x = dest_x;
5625 dstRect.y = dest_y;
5626 dstRect.width = dest_width;
5627 dstRect.height = dest_height;
5628
5629 //Execute VP pipeline.
5630 vaStatus = DdiVp_VideoProcessPipeline(ctx, context, tempSurface, &srcRect, surface, &dstRect);
5631 if (vaStatus != VA_STATUS_SUCCESS)
5632 {
5633 DDI_ASSERTMESSAGE("VP Pipeline failed.");
5634 DdiMedia_DestroySurfaces(ctx, &tempSurface, 1);
5635 return vaStatus;
5636 }
5637
5638 vaStatus = DdiMedia_SyncSurface(ctx, tempSurface);
5639 DdiMedia_DestroySurfaces(ctx, &tempSurface, 1);
5640 vaStatus = DdiVp_DestroyContext(ctx, context);
5641 }
5642 else
5643 {
5644 //Lock Surface
5645 if ((nullptr != buf->pSurface) && (Media_Format_CPU != mediaSurface->format))
5646 {
5647 vaStatus = DdiMedia_MediaMemoryDecompress(mediaCtx, mediaSurface);
5648
5649 if (vaStatus != VA_STATUS_SUCCESS)
5650 {
5651 DDI_NORMALMESSAGE("surface Decompression fail, continue next steps.");
5652 }
5653 }
5654
5655 void *surfData = DdiMediaUtil_LockSurface(mediaSurface, (MOS_LOCKFLAG_READONLY | MOS_LOCKFLAG_WRITEONLY));
5656 if (nullptr == surfData)
5657 {
5658 DDI_ASSERTMESSAGE("Failed to lock surface.");
5659 return VA_STATUS_ERROR_SURFACE_BUSY;
5660 }
5661
5662 if (src_width == dest_width && src_height == dest_height &&
5663 src_width == vaimg->width && src_height == vaimg->height &&
5664 src_width == mediaSurface->iWidth && src_height == mediaSurface->iHeight &&
5665 mediaSurface->data_size == vaimg->data_size)
5666 {
5667 //Copy data from image to surface
5668 MOS_STATUS eStatus = MOS_SecureMemcpy(surfData, vaimg->data_size, imageData, vaimg->data_size);
5669 DDI_CHK_CONDITION((eStatus != MOS_STATUS_SUCCESS), "Failed to copy image to surface buffer.", VA_STATUS_ERROR_OPERATION_FAILED);
5670 }
5671 else
5672 {
5673 uint8_t *ySrc = (uint8_t *)imageData + vaimg->offsets[0];
5674 uint8_t *yDst = (uint8_t *)surfData;
5675 DdiMedia_CopyPlane(yDst, mediaSurface->iPitch, ySrc, vaimg->pitches[0], src_height);
5676
5677 if (vaimg->num_planes > 1)
5678 {
5679 DDI_MEDIA_SURFACE uPlane = *mediaSurface;
5680
5681 uint32_t realChromaHeight = 0;
5682 uint32_t alignedChromaHeight = 0;
5683 uint32_t chromaPitch = 0;
5684 DdiMedia_GetChromaPitchHeight(DdiMedia_MediaFormatToOsFormat(uPlane.format), uPlane.iPitch, uPlane.iRealHeight, &chromaPitch, &realChromaHeight);
5685 DdiMedia_GetChromaPitchHeight(DdiMedia_MediaFormatToOsFormat(uPlane.format), uPlane.iPitch, uPlane.iHeight, &chromaPitch, &alignedChromaHeight);
5686
5687 uint8_t *uSrc = (uint8_t *)imageData + vaimg->offsets[1];
5688 uint8_t *uDst = yDst + mediaSurface->iPitch * mediaSurface->iHeight;
5689 DdiMedia_CopyPlane(uDst, chromaPitch, uSrc, vaimg->pitches[1], realChromaHeight);
5690 if (vaimg->num_planes > 2)
5691 {
5692 uint8_t *vSrc = (uint8_t *)imageData + vaimg->offsets[2];
5693 uint8_t *vDst = uDst + chromaPitch * alignedChromaHeight;
5694 DdiMedia_CopyPlane(vDst, chromaPitch, vSrc, vaimg->pitches[2], realChromaHeight);
5695 }
5696 }
5697 }
5698
5699 vaStatus = DdiMedia_UnmapBuffer(ctx, vaimg->buf);
5700 if (vaStatus != VA_STATUS_SUCCESS)
5701 {
5702 DDI_ASSERTMESSAGE("Failed to unmap buffer.");
5703 DdiMediaUtil_UnlockSurface(mediaSurface);
5704 return vaStatus;
5705 }
5706
5707 DdiMediaUtil_UnlockSurface(mediaSurface);
5708 }
5709 MOS_TraceEventExt(EVENT_VA_PUT, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
5710 return VA_STATUS_SUCCESS;
5711 }
5712
5713 //!
5714 //! \brief Query subpicture formats
5715 //!
5716 //! \param [in] ctx
5717 //! Pointer to VA driver context
5718 //! \param [in] format_list
5719 //! VA image format
5720 //! \param [in] flags
5721 //! Flags
5722 //! \param [in] num_formats
5723 //! Number of formats
5724 //!
5725 //! \return VAStatus
5726 //! VA_STATUS_SUCCESS if success, else fail reason
5727 //!
5728 VAStatus DdiMedia_QuerySubpictureFormats(
5729 VADriverContextP ctx,
5730 VAImageFormat *format_list,
5731 uint32_t *flags,
5732 uint32_t *num_formats)
5733 {
5734 DDI_UNUSED(ctx);
5735 DDI_UNUSED(format_list);
5736 DDI_UNUSED(flags);
5737
5738 DDI_FUNCTION_ENTER();
5739
5740 DDI_CHK_NULL(num_formats, "nullptr num_formats", VA_STATUS_ERROR_INVALID_PARAMETER);
5741 *num_formats = 0;
5742
5743 return VA_STATUS_SUCCESS;
5744 }
5745
5746 //!
5747 //! \brief Create subpicture
5748 //!
5749 //! \param [in] ctx
5750 //! Pointer to VA driver context
5751 //! \param [in] image
5752 //! VA image ID
5753 //! \param [out] subpicture
5754 //! VA subpicture ID
5755 //!
5756 //! \return VAStatus
5757 //! VA_STATUS_ERROR_UNIMPLEMENTED
5758 //!
5759 VAStatus DdiMedia_CreateSubpicture(
5760 VADriverContextP ctx,
5761 VAImageID image,
5762 VASubpictureID *subpicture /* out */
5763 )
5764 {
5765 DDI_UNUSED(ctx);
5766 DDI_UNUSED(image);
5767 DDI_UNUSED(subpicture);
5768
5769 DDI_FUNCTION_ENTER();
5770
5771 return VA_STATUS_ERROR_UNIMPLEMENTED;
5772 }
5773
5774 //!
5775 //! \brief Destroy subpicture
5776 //!
5777 //! \param [in] ctx
5778 //! Pointer to VA driver context
5779 //! \param [in] subpicture
5780 //! VA subpicture ID
5781 //!
5782 //! \return VAStatus
5783 //! VA_STATUS_ERROR_UNIMPLEMENTED
5784 //!
5785 VAStatus DdiMedia_DestroySubpicture(
5786 VADriverContextP ctx,
5787 VASubpictureID subpicture
5788 )
5789 {
5790 DDI_UNUSED(ctx);
5791 DDI_UNUSED(subpicture);
5792
5793 DDI_FUNCTION_ENTER();
5794
5795 return VA_STATUS_ERROR_UNIMPLEMENTED;
5796 }
5797
5798 //!
5799 //! \brief Set subpicture image
5800 //!
5801 //! \param [in] ctx
5802 //! Pointer to VA driver context
5803 //! \param [in] subpicture
5804 //! VA subpicture ID
5805 //! \param [in] image
5806 //! VA image ID
5807 //!
5808 //! \return VAStatus
5809 //! VA_STATUS_ERROR_UNIMPLEMENTED
5810 //!
5811 VAStatus DdiMedia_SetSubpictureImage(
5812 VADriverContextP ctx,
5813 VASubpictureID subpicture,
5814 VAImageID image
5815 )
5816 {
5817 DDI_UNUSED(ctx);
5818 DDI_UNUSED(subpicture);
5819 DDI_UNUSED(image);
5820
5821 DDI_FUNCTION_ENTER();
5822
5823 return VA_STATUS_ERROR_UNIMPLEMENTED;
5824 }
5825
5826 //!
5827 //! \brief Set subpicture chrome key
5828 //!
5829 //! \param [in] ctx
5830 //! Pointer to VA driver context
5831 //! \param [in] subpicture
5832 //! VA subpicture ID
5833 //! \param [in] chromakey_min
5834 //! Minimum chroma key
5835 //! \param [in] chromakey_max
5836 //! Maximum chroma key
5837 //! \param [in] chromakey_mask
5838 //! Chromakey mask
5839 //!
5840 //! \return VAStatus
5841 //! VA_STATUS_ERROR_UNIMPLEMENTED
5842 //!
5843 VAStatus DdiMedia_SetSubpictureChromakey(
5844 VADriverContextP ctx,
5845 VASubpictureID subpicture,
5846 uint32_t chromakey_min,
5847 uint32_t chromakey_max,
5848 uint32_t chromakey_mask
5849 )
5850 {
5851 DDI_UNUSED(ctx);
5852 DDI_UNUSED(subpicture);
5853 DDI_UNUSED(chromakey_min);
5854 DDI_UNUSED(chromakey_max);
5855 DDI_UNUSED(chromakey_mask);
5856
5857 DDI_FUNCTION_ENTER();
5858
5859 return VA_STATUS_ERROR_UNIMPLEMENTED;
5860 }
5861
5862 //!
5863 //! \brief set subpicture global alpha
5864 //!
5865 //! \param [in] ctx
5866 //! Pointer to VA driver context
5867 //! \param [in] subpicture
5868 //! VA subpicture ID
5869 //! \param [in] global_alpha
5870 //! Global alpha
5871 //!
5872 //! \return VAStatus
5873 //! VA_STATUS_ERROR_UNIMPLEMENTED
5874 VAStatus DdiMedia_SetSubpictureGlobalAlpha(
5875 VADriverContextP ctx,
5876 VASubpictureID subpicture,
5877 float global_alpha
5878 )
5879 {
5880 DDI_UNUSED(ctx);
5881 DDI_UNUSED(subpicture);
5882 DDI_UNUSED(global_alpha);
5883
5884 DDI_FUNCTION_ENTER();
5885
5886 return VA_STATUS_ERROR_UNIMPLEMENTED;
5887 }
5888
5889 //!
5890 //! \brief Associate subpicture
5891 //!
5892 //! \param [in] ctx
5893 //! Pointer to VA driver context
5894 //! \param [in] subpicture
5895 //! VA subpicture ID
5896 //! \param [in] target_surfaces
5897 //! VA surface ID
5898 //! \param [in] num_surfaces
5899 //! Number of surfaces
5900 //! \param [in] src_x
5901 //! Source x of the region
5902 //! \param [in] src_y
5903 //! Source y of the region
5904 //! \param [in] src_width
5905 //! Source width of the region
5906 //! \param [in] src_height
5907 //! Source height of the region
5908 //! \param [in] dest_x
5909 //! Destination x
5910 //! \param [in] dest_y
5911 //! Destination y
5912 //! \param [in] dest_width
5913 //! Destination width
5914 //! \param [in] dest_height
5915 //! Destination height
5916 //! \param [in] flags
5917 //! Flags
5918 //!
5919 //! \return VAStatus
5920 //! VA_STATUS_ERROR_UNIMPLEMENTED
5921 //!
5922 VAStatus DdiMedia_AssociateSubpicture(
5923 VADriverContextP ctx,
5924 VASubpictureID subpicture,
5925 VASurfaceID *target_surfaces,
5926 int32_t num_surfaces,
5927 int16_t src_x, /* upper left offset in subpicture */
5928 int16_t src_y,
5929 uint16_t src_width,
5930 uint16_t src_height,
5931 int16_t dest_x, /* upper left offset in surface */
5932 int16_t dest_y,
5933 uint16_t dest_width,
5934 uint16_t dest_height,
5935 /*
5936 * whether to enable chroma-keying or global-alpha
5937 * see VA_SUBPICTURE_XXX values
5938 */
5939 uint32_t flags
5940 )
5941 {
5942 DDI_UNUSED(ctx);
5943 DDI_UNUSED(subpicture);
5944 DDI_UNUSED(target_surfaces);
5945 DDI_UNUSED(num_surfaces);
5946 DDI_UNUSED(src_x);
5947 DDI_UNUSED(src_y);
5948 DDI_UNUSED(src_width);
5949 DDI_UNUSED(src_height);
5950 DDI_UNUSED(dest_x);
5951 DDI_UNUSED(dest_y);
5952 DDI_UNUSED(dest_width);
5953 DDI_UNUSED(dest_height);
5954 DDI_UNUSED(flags);
5955
5956 DDI_FUNCTION_ENTER();
5957
5958 return VA_STATUS_ERROR_UNIMPLEMENTED;
5959 }
5960
5961 //!
5962 //! \brief Deassociate subpicture
5963 //!
5964 //! \param [in] ctx
5965 //! Pointer to VA driver context
5966 //! \param [in] subpicture
5967 //! VA subpicture ID
5968 //! \param [in] target_surfaces
5969 //! VA surface ID
5970 //! \param [in] num_surfaces
5971 //! Number of surfaces
5972 //!
5973 //! \return VAStatus
5974 //! VA_STATUS_ERROR_UNIMPLEMENTED
5975 //!
5976 VAStatus DdiMedia_DeassociateSubpicture(
5977 VADriverContextP ctx,
5978 VASubpictureID subpicture,
5979 VASurfaceID *target_surfaces,
5980 int32_t num_surfaces
5981 )
5982 {
5983 DDI_UNUSED(ctx);
5984 DDI_UNUSED(subpicture);
5985 DDI_UNUSED(target_surfaces);
5986 DDI_UNUSED(num_surfaces);
5987
5988 DDI_FUNCTION_ENTER();
5989
5990 return VA_STATUS_ERROR_UNIMPLEMENTED;
5991 }
5992
5993 //!
5994 //! \brief Query display attributes
5995 //!
5996 //! \param [in] ctx
5997 //! Pointer to VA driver context
5998 //! \param [in] attr_list
5999 //! VA display attribute
6000 //! \param [in] num_attributes
6001 //! Number of attributes
6002 //!
6003 //! \return VAStatus
6004 //! VA_STATUS_SUCCESS if success, else fail reason
6005 //!
6006 VAStatus DdiMedia_QueryDisplayAttributes(
6007 VADriverContextP ctx,
6008 VADisplayAttribute *attr_list,
6009 int32_t *num_attributes)
6010 {
6011 DDI_FUNCTION_ENTER();
6012
6013 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
6014 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
6015 DDI_CHK_NULL(mediaCtx->m_caps, "nullptr m_caps", VA_STATUS_ERROR_INVALID_CONTEXT);
6016
6017 DDI_CHK_NULL(attr_list, "nullptr attr_list", VA_STATUS_ERROR_INVALID_PARAMETER);
6018 DDI_CHK_NULL(num_attributes, "nullptr num_attributes", VA_STATUS_ERROR_INVALID_PARAMETER);
6019
6020 return mediaCtx->m_caps->QueryDisplayAttributes(attr_list, num_attributes);
6021 }
6022
6023 //!
6024 //! \brief Get display attributes
6025 //! \details This function returns the current attribute values in "attr_list".
6026 //! Only attributes returned with VA_DISPLAY_ATTRIB_GETTABLE set in the "flags" field
6027 //! from vaQueryDisplayAttributes() can have their values retrieved.
6028 //!
6029 //! \param [in] ctx
6030 //! Pointer to VA driver context
6031 //! \param [in] attr_list
6032 //! VA display attribute
6033 //! \param [in] num_attributes
6034 //! Number of attributes
6035 //!
6036 //! \return VAStatus
6037 //! VA_STATUS_ERROR_UNIMPLEMENTED
6038 //!
6039 VAStatus DdiMedia_GetDisplayAttributes(
6040 VADriverContextP ctx,
6041 VADisplayAttribute *attr_list,
6042 int32_t num_attributes)
6043 {
6044 DDI_FUNCTION_ENTER();
6045
6046 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
6047 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
6048 DDI_CHK_NULL(mediaCtx->m_caps, "nullptr m_caps", VA_STATUS_ERROR_INVALID_CONTEXT);
6049
6050 DDI_CHK_NULL(attr_list, "nullptr attr_list", VA_STATUS_ERROR_INVALID_PARAMETER);
6051
6052 return mediaCtx->m_caps->GetDisplayAttributes(attr_list, num_attributes);
6053 }
6054
6055 //!
6056 //! \brief Set display attributes
6057 //! \details Only attributes returned with VA_DISPLAY_ATTRIB_SETTABLE set in the "flags" field
6058 //! from vaQueryDisplayAttributes() can be set. If the attribute is not settable or
6059 //! the value is out of range, the function returns VA_STATUS_ERROR_ATTR_NOT_SUPPORTED
6060 //!
6061 //! \param [in] ctx
6062 //! Pointer to VA driver context
6063 //! \param [in] attr_list
6064 //! VA display attribute
6065 //! \param [in] num_attributes
6066 //! Number of attributes
6067 //!
6068 //! \return VAStatus
6069 //! VA_STATUS_ERROR_UNIMPLEMENTED
6070 //!
6071 VAStatus DdiMedia_SetDisplayAttributes(
6072 VADriverContextP ctx,
6073 VADisplayAttribute *attr_list,
6074 int32_t num_attributes)
6075 {
6076 DDI_UNUSED(ctx);
6077 DDI_UNUSED(attr_list);
6078 DDI_UNUSED(num_attributes);
6079
6080 DDI_FUNCTION_ENTER();
6081
6082 return VA_STATUS_ERROR_UNIMPLEMENTED;
6083 }
6084
6085 //!
6086 //! \brief Query processing rate
6087 //!
6088 //! \param [in] ctx
6089 //! Pointer to VA driver context
6090 //! \param [in] config_id
6091 //! VA configuration ID
6092 //! \param [in] proc_buf
6093 //! VA processing rate parameter
6094 //! \param [out] processing_rate
6095 //! Processing rate
6096 //!
6097 //! \return VAStatus
6098 //! VA_STATUS_SUCCESS if success, else fail reason
6099 //!
6100 VAStatus
6101 DdiMedia_QueryProcessingRate(
6102 VADriverContextP ctx,
6103 VAConfigID config_id,
6104 VAProcessingRateParameter *proc_buf,
6105 uint32_t *processing_rate /* output parameter */)
6106 {
6107 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
6108 DDI_CHK_NULL(proc_buf, "nullptr proc_buf", VA_STATUS_ERROR_INVALID_PARAMETER);
6109 DDI_CHK_NULL(processing_rate, "nullptr processing_rate", VA_STATUS_ERROR_INVALID_PARAMETER);
6110
6111 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
6112 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
6113 DDI_CHK_NULL(mediaCtx->m_caps, "nullptr m_caps", VA_STATUS_ERROR_INVALID_CONTEXT);
6114
6115 return mediaCtx->m_caps->QueryProcessingRate(config_id,
6116 proc_buf, processing_rate);
6117 }
6118
6119 //!
6120 //! \brief media copy internal
6121 //!
6122 //! \param [in] mosCtx
6123 //! Pointer to mos context
6124 //! \param [in] src
6125 //! VA copy mos resource src.
6126 //! \param [in] dst
6127 //! VA copy mos resrouce dst.
6128 //! \param [in] option
6129 //! VA copy option, copy mode.
6130 //!
6131 //! \return VAStatus
6132 //! VA_STATUS_SUCCESS if success, else fail reason
6133 //!
6134 VAStatus
6135 DdiMedia_CopyInternal(
6136 PMOS_CONTEXT mosCtx,
6137 PMOS_RESOURCE src,
6138 PMOS_RESOURCE dst,
6139 uint32_t copy_mode
6140 )
6141 {
6142 VAStatus vaStatus = VA_STATUS_SUCCESS;
6143 MOS_STATUS mosStatus = MOS_STATUS_UNINITIALIZED;
6144 DDI_CHK_NULL(mosCtx, "nullptr mosCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
6145 DDI_CHK_NULL(src, "nullptr input osResource", VA_STATUS_ERROR_INVALID_SURFACE);
6146 DDI_CHK_NULL(dst, "nullptr output osResource", VA_STATUS_ERROR_INVALID_SURFACE);
6147
6148 MediaCopyBaseState *mediaCopyState = static_cast<MediaCopyBaseState*>(*mosCtx->ppMediaCopyState);
6149
6150 if (!mediaCopyState)
6151 {
6152 mediaCopyState = static_cast<MediaCopyBaseState*>(McpyDevice::CreateFactory(mosCtx));
6153 *mosCtx->ppMediaCopyState = mediaCopyState;
6154 }
6155
6156 DDI_CHK_NULL(mediaCopyState, "Invalid mediaCopy State", VA_STATUS_ERROR_INVALID_PARAMETER);
6157
6158 mosStatus = mediaCopyState->SurfaceCopy(src, dst, (MCPY_METHOD)copy_mode);
6159 if (mosStatus != MOS_STATUS_SUCCESS)
6160 {
6161 vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
6162 }
6163
6164 return vaStatus;
6165 }
6166
6167 #if VA_CHECK_VERSION(1,10,0)
6168 //!
6169 //! \brief media copy
6170 //!
6171 //! \param [in] ctx
6172 //! Pointer to VA driver context
6173 //! \param [in] dst_obj
6174 //! VA copy object dst.
6175 //! \param [in] src_obj
6176 //! VA copy object src.
6177 //! \param [in] option
6178 //! VA copy option, copy mode.
6179 //! \param [in] sync_handle
6180 //! VA copy sync handle
6181 //!
6182 //! \return VAStatus
6183 //! VA_STATUS_SUCCESS if success, else fail reason
6184 //!
6185 VAStatus
6186 DdiMedia_Copy(
6187 VADriverContextP ctx,
6188 VACopyObject *dst_obj,
6189 VACopyObject *src_obj,
6190 VACopyOption option
6191 )
6192 {
6193 VAStatus vaStatus = VA_STATUS_SUCCESS;
6194 MOS_CONTEXT mosCtx = {};
6195 MOS_RESOURCE src, dst;
6196 DdiCpInterface *pCpDdiInterface = nullptr;
6197 PDDI_MEDIA_SURFACE src_surface = nullptr;
6198 PDDI_MEDIA_SURFACE dst_surface = nullptr;
6199 PDDI_MEDIA_BUFFER src_buffer = nullptr;
6200 PDDI_MEDIA_BUFFER dst_buffer = nullptr;
6201
6202 DDI_FUNCTION_ENTER();
6203
6204 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
6205 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
6206
6207 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
6208 DDI_CHK_NULL(mediaCtx->pBufferHeap, "nullptr mediaCtx->pBufferHeap", VA_STATUS_ERROR_INVALID_CONTEXT);
6209 DDI_CHK_NULL(mediaCtx->pSurfaceHeap, "nullptr mediaCtx->pSurfaceHeap", VA_STATUS_ERROR_INVALID_CONTEXT);
6210 DDI_CHK_NULL(dst_obj, "nullptr copy dst", VA_STATUS_ERROR_INVALID_SURFACE);
6211 DDI_CHK_NULL(src_obj, "nullptr copy src", VA_STATUS_ERROR_INVALID_SURFACE);
6212
6213 if (dst_obj->obj_type == VACopyObjectSurface)
6214 {
6215 DDI_CHK_LESS((uint32_t)dst_obj->object.surface_id, mediaCtx->pSurfaceHeap->uiAllocatedHeapElements, "copy_dst", VA_STATUS_ERROR_INVALID_SURFACE);
6216 dst_surface = DdiMedia_GetSurfaceFromVASurfaceID(mediaCtx, dst_obj->object.surface_id);
6217 DDI_CHK_NULL(dst_surface, "nullptr surface", VA_STATUS_ERROR_INVALID_SURFACE);
6218 DDI_CHK_NULL(dst_surface->pGmmResourceInfo, "nullptr dst_surface->pGmmResourceInfo", VA_STATUS_ERROR_INVALID_PARAMETER);
6219
6220 MOS_ZeroMemory(&dst, sizeof(dst));
6221 DdiMedia_MediaSurfaceToMosResource(dst_surface, &dst);
6222 }
6223 else if (dst_obj->obj_type == VACopyObjectBuffer)
6224 {
6225 DDI_CHK_LESS((uint32_t)dst_obj->object.buffer_id, mediaCtx->pBufferHeap->uiAllocatedHeapElements, "Invalid copy dst buf_id", VA_STATUS_ERROR_INVALID_BUFFER);
6226 dst_buffer = DdiMedia_GetBufferFromVABufferID(mediaCtx, dst_obj->object.buffer_id);
6227 DDI_CHK_NULL(dst_buffer, "nullptr buffer", VA_STATUS_ERROR_INVALID_BUFFER);
6228 DDI_CHK_NULL(dst_buffer->pGmmResourceInfo, "nullptr dst_buffer->pGmmResourceInfo", VA_STATUS_ERROR_INVALID_PARAMETER);
6229
6230 MOS_ZeroMemory(&dst, sizeof(dst));
6231 DdiMedia_MediaBufferToMosResource(dst_buffer, &dst);
6232 }
6233 else
6234 {
6235 DDI_ASSERTMESSAGE("DDI: unsupported src copy object in DdiMedia_copy.");
6236 }
6237
6238 if (src_obj->obj_type == VACopyObjectSurface)
6239 {
6240 DDI_CHK_LESS((uint32_t)src_obj->object.surface_id, mediaCtx->pSurfaceHeap->uiAllocatedHeapElements, "copy_src", VA_STATUS_ERROR_INVALID_SURFACE);
6241 src_surface = DdiMedia_GetSurfaceFromVASurfaceID(mediaCtx, src_obj->object.surface_id);
6242 DDI_CHK_NULL(src_surface, "nullptr surface", VA_STATUS_ERROR_INVALID_SURFACE);
6243 DDI_CHK_NULL(src_surface->pGmmResourceInfo, "nullptr src_surface->pGmmResourceInfo", VA_STATUS_ERROR_INVALID_PARAMETER);
6244
6245 MOS_ZeroMemory(&src, sizeof(src));
6246 DdiMedia_MediaSurfaceToMosResource(src_surface, &src);
6247 }
6248 else if (src_obj->obj_type == VACopyObjectBuffer)
6249 {
6250 DDI_CHK_LESS((uint32_t)src_obj->object.buffer_id, mediaCtx->pBufferHeap->uiAllocatedHeapElements, "Invalid copy dst buf_id", VA_STATUS_ERROR_INVALID_BUFFER);
6251 src_buffer = DdiMedia_GetBufferFromVABufferID(mediaCtx, src_obj->object.buffer_id);
6252 DDI_CHK_NULL(src_buffer, "nullptr buffer", VA_STATUS_ERROR_INVALID_BUFFER);
6253 DDI_CHK_NULL(src_buffer->pGmmResourceInfo, "nullptr src_buffer->pGmmResourceInfo", VA_STATUS_ERROR_INVALID_PARAMETER);
6254
6255 MOS_ZeroMemory(&src, sizeof(src));
6256 DdiMedia_MediaBufferToMosResource(src_buffer, &src);
6257 }
6258 else
6259 {
6260 DDI_ASSERTMESSAGE("DDI: unsupported dst copy object in DdiMedia_copy.");
6261 }
6262
6263 mosCtx.bufmgr = mediaCtx->pDrmBufMgr;
6264 mosCtx.m_gpuContextMgr = mediaCtx->m_gpuContextMgr;
6265 mosCtx.m_cmdBufMgr = mediaCtx->m_cmdBufMgr;
6266 mosCtx.fd = mediaCtx->fd;
6267 mosCtx.iDeviceId = mediaCtx->iDeviceId;
6268 mosCtx.m_skuTable = mediaCtx->SkuTable;
6269 mosCtx.m_waTable = mediaCtx->WaTable;
6270 mosCtx.m_gtSystemInfo = *mediaCtx->pGtSystemInfo;
6271 mosCtx.m_platform = mediaCtx->platform;
6272
6273 mosCtx.ppMediaCopyState = &mediaCtx->pMediaCopyState;
6274 mosCtx.m_gtSystemInfo = *mediaCtx->pGtSystemInfo;
6275 mosCtx.m_auxTableMgr = mediaCtx->m_auxTableMgr;
6276 mosCtx.pGmmClientContext = mediaCtx->pGmmClientContext;
6277
6278 mosCtx.m_osDeviceContext = mediaCtx->m_osDeviceContext;
6279 mosCtx.m_apoMosEnabled = mediaCtx->m_apoMosEnabled;
6280 mosCtx.pPerfData = mediaCtx->perfData;
6281 mosCtx.m_userSettingPtr = mediaCtx->m_userSettingPtr;
6282
6283 pCpDdiInterface = Create_DdiCpInterface(mosCtx);
6284
6285 if (nullptr == pCpDdiInterface)
6286 {
6287 return VA_STATUS_ERROR_ALLOCATION_FAILED;
6288 }
6289
6290 vaStatus = DdiMedia_CopyInternal(&mosCtx, &src, &dst, option.bits.va_copy_mode);
6291
6292 if ((option.bits.va_copy_sync == VA_EXEC_SYNC) && dst_surface)
6293 {
6294 uint32_t timeout_NS = 100000000;
6295 while (0 != mos_bo_wait(dst_surface->bo, timeout_NS))
6296 {
6297 // Just loop while gem_bo_wait times-out.
6298 }
6299 }
6300
6301 if (pCpDdiInterface)
6302 {
6303 Delete_DdiCpInterface(pCpDdiInterface);
6304 pCpDdiInterface = NULL;
6305 }
6306
6307 return vaStatus;
6308 }
6309 #endif
6310
6311 //!
6312 //! \brief Check for buffer info
6313 //!
6314 //! \param [in] ctx
6315 //! Pointer to VA driver context
6316 //! \param [in] buf_id
6317 //! VA buffer ID
6318 //! \param [out] type
6319 //! VA buffer type
6320 //! \param [out] size
6321 //! Size
6322 //! \param [out] num_elements
6323 //! Number of elements
6324 //!
6325 //! \return VAStatus
6326 //! VA_STATUS_SUCCESS if success, else fail reason
6327 //!
6328 VAStatus DdiMedia_BufferInfo (
6329 VADriverContextP ctx,
6330 VABufferID buf_id,
6331 VABufferType *type,
6332 uint32_t *size,
6333 uint32_t *num_elements)
6334 {
6335 DDI_FUNCTION_ENTER();
6336
6337 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
6338 DDI_CHK_NULL(type, "nullptr type", VA_STATUS_ERROR_INVALID_PARAMETER);
6339 DDI_CHK_NULL(size, "nullptr size", VA_STATUS_ERROR_INVALID_PARAMETER);
6340 DDI_CHK_NULL(num_elements, "nullptr num_elements", VA_STATUS_ERROR_INVALID_PARAMETER);
6341
6342 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
6343 if (nullptr == mediaCtx)
6344 return VA_STATUS_ERROR_INVALID_CONTEXT;
6345
6346 DDI_CHK_NULL(mediaCtx->pBufferHeap, "nullptr mediaCtx->pBufferHeap", VA_STATUS_ERROR_INVALID_CONTEXT);
6347 DDI_CHK_LESS((uint32_t)buf_id, mediaCtx->pBufferHeap->uiAllocatedHeapElements, "Invalid buf_id", VA_STATUS_ERROR_INVALID_BUFFER);
6348
6349 DDI_MEDIA_BUFFER *buf = DdiMedia_GetBufferFromVABufferID(mediaCtx, buf_id);
6350 if (nullptr == buf)
6351 {
6352 return VA_STATUS_ERROR_INVALID_BUFFER;
6353 }
6354
6355 *type = (VABufferType)buf->uiType;
6356 *size = buf->iSize / buf->uiNumElements;
6357 *num_elements = buf->uiNumElements;
6358
6359 return VA_STATUS_SUCCESS;
6360 }
6361
6362 //!
6363 //! \brief Lock surface
6364 //!
6365 //! \param [in] ctx
6366 //! Pointer to VA driver context
6367 //! \param [in] surface
6368 //! VA surface ID
6369 //! \param [out] fourcc
6370 //! FourCC
6371 //! \param [out] luma_stride
6372 //! Luma stride
6373 //! \param [out] chroma_u_stride
6374 //! Chroma U stride
6375 //! \param [out] chroma_v_stride
6376 //! Chroma V stride
6377 //! \param [out] luma_offset
6378 //! Luma offset
6379 //! \param [out] chroma_u_offset
6380 //! Chroma U offset
6381 //! \param [out] chroma_v_offset
6382 //! Chroma V offset
6383 //! \param [out] buffer_name
6384 //! Buffer name
6385 //! \param [out] buffer
6386 //! Buffer
6387 //!
6388 //! \return VAStatus
6389 //! VA_STATUS_SUCCESS if success, else fail reason
6390 //!
6391 VAStatus DdiMedia_LockSurface (
6392 VADriverContextP ctx,
6393 VASurfaceID surface,
6394 uint32_t *fourcc,
6395 uint32_t *luma_stride,
6396 uint32_t *chroma_u_stride,
6397 uint32_t *chroma_v_stride,
6398 uint32_t *luma_offset,
6399 uint32_t *chroma_u_offset,
6400 uint32_t *chroma_v_offset,
6401 uint32_t *buffer_name,
6402 void **buffer )
6403 {
6404 DDI_FUNCTION_ENTER();
6405 MOS_TraceEventExt(EVENT_VA_LOCK, EVENT_TYPE_START, &surface, sizeof(surface), nullptr, 0);
6406
6407 DDI_CHK_NULL(ctx, "nullptr context", VA_STATUS_ERROR_INVALID_CONTEXT);
6408 DDI_CHK_NULL(fourcc, "nullptr fourcc", VA_STATUS_ERROR_INVALID_PARAMETER);
6409 DDI_CHK_NULL(luma_stride, "nullptr luma_stride", VA_STATUS_ERROR_INVALID_PARAMETER);
6410 DDI_CHK_NULL(chroma_u_stride, "nullptr chroma_u_stride", VA_STATUS_ERROR_INVALID_PARAMETER);
6411 DDI_CHK_NULL(chroma_v_stride, "nullptr chroma_v_stride", VA_STATUS_ERROR_INVALID_PARAMETER);
6412 DDI_CHK_NULL(luma_offset, "nullptr luma_offset", VA_STATUS_ERROR_INVALID_PARAMETER);
6413 DDI_CHK_NULL(chroma_u_offset, "nullptr chroma_u_offset", VA_STATUS_ERROR_INVALID_PARAMETER);
6414 DDI_CHK_NULL(chroma_v_offset, "nullptr chroma_v_offset", VA_STATUS_ERROR_INVALID_PARAMETER);
6415 DDI_CHK_NULL(buffer_name, "nullptr buffer_name", VA_STATUS_ERROR_INVALID_PARAMETER);
6416 DDI_CHK_NULL(buffer, "nullptr buffer", VA_STATUS_ERROR_INVALID_PARAMETER);
6417
6418 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
6419 DDI_CHK_NULL(mediaCtx, "nullptr Media", VA_STATUS_ERROR_INVALID_CONTEXT);
6420 DDI_CHK_NULL(mediaCtx->pSurfaceHeap, "nullptr mediaCtx->pSurfaceHeap", VA_STATUS_ERROR_INVALID_CONTEXT);
6421 DDI_CHK_LESS((uint32_t)surface, mediaCtx->pSurfaceHeap->uiAllocatedHeapElements, "Invalid surface", VA_STATUS_ERROR_INVALID_SURFACE);
6422
6423 DDI_MEDIA_SURFACE *mediaSurface = DdiMedia_GetSurfaceFromVASurfaceID(mediaCtx, surface);
6424
6425 // Decompress surface is needed
6426 DdiMedia_MediaMemoryDecompress(mediaCtx, mediaSurface);
6427
6428 if (nullptr == mediaSurface)
6429 {
6430 // Surface is absent.
6431 buffer = nullptr;
6432 return VA_STATUS_ERROR_INVALID_SURFACE;
6433 }
6434
6435 if (mediaSurface->uiLockedImageID != VA_INVALID_ID)
6436 {
6437 // Surface is locked already.
6438 buffer = nullptr;
6439 return VA_STATUS_ERROR_INVALID_PARAMETER;
6440 }
6441
6442 VAImage tmpImage;
6443 tmpImage.image_id = VA_INVALID_ID;
6444 VAStatus vaStatus = DdiMedia_DeriveImage(ctx,surface,&tmpImage);
6445 if (vaStatus != VA_STATUS_SUCCESS)
6446 {
6447 buffer = nullptr;
6448 return vaStatus;
6449 }
6450
6451 mediaSurface->uiLockedImageID = tmpImage.image_id;
6452
6453 vaStatus = DdiMedia_MapBuffer(ctx,tmpImage.buf,buffer);
6454 if (vaStatus != VA_STATUS_SUCCESS)
6455 {
6456 buffer = nullptr;
6457 return vaStatus;
6458 }
6459
6460 mediaSurface->uiLockedBufID = tmpImage.buf;
6461
6462 *fourcc = tmpImage.format.fourcc;
6463 *luma_offset = tmpImage.offsets[0];
6464 *luma_stride = tmpImage.pitches[0];
6465 *chroma_u_offset = tmpImage.offsets[1];
6466 *chroma_u_stride = tmpImage.pitches[1];
6467 *chroma_v_offset = tmpImage.offsets[2];
6468 *chroma_v_stride = tmpImage.pitches[2];
6469 *buffer_name = tmpImage.buf;
6470
6471 MOS_TraceEventExt(EVENT_VA_LOCK, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
6472 return VA_STATUS_SUCCESS;
6473 }
6474
6475 //!
6476 //! \brief Unlock surface
6477 //!
6478 //! \param [in] ctx
6479 //! Pointer to VA driver context
6480 //! \param [in] surface
6481 //! VA surface ID
6482 //!
6483 //! \return VAStatus
6484 //! VA_STATUS_SUCCESS if success, else fail reason
6485 //!
6486 VAStatus DdiMedia_UnlockSurface (
6487 VADriverContextP ctx,
6488 VASurfaceID surface)
6489 {
6490 DDI_FUNCTION_ENTER();
6491 MOS_TraceEventExt(EVENT_VA_UNLOCK, EVENT_TYPE_START, &surface, sizeof(surface), nullptr, 0);
6492
6493 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
6494
6495 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
6496 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
6497 DDI_CHK_NULL(mediaCtx->pSurfaceHeap, "nullptr mediaCtx->pSurfaceHeap", VA_STATUS_ERROR_INVALID_CONTEXT);
6498 DDI_CHK_LESS((uint32_t)surface, mediaCtx->pSurfaceHeap->uiAllocatedHeapElements, "Invalid surface", VA_STATUS_ERROR_INVALID_SURFACE);
6499
6500 DDI_MEDIA_SURFACE *mediaSurface = DdiMedia_GetSurfaceFromVASurfaceID(mediaCtx, surface);
6501 DDI_CHK_NULL(mediaSurface, "nullptr mediaSurface", VA_STATUS_ERROR_INVALID_SURFACE);
6502
6503 if (mediaSurface->uiLockedImageID == VA_INVALID_ID)
6504 {
6505 return VA_STATUS_ERROR_INVALID_PARAMETER;
6506 }
6507
6508 VABufferID bufID = (VABufferID)(mediaSurface->uiLockedBufID);
6509 VAStatus vaStatus = DdiMedia_UnmapBuffer(ctx, bufID);
6510 if (vaStatus != VA_STATUS_SUCCESS)
6511 {
6512 return vaStatus;
6513 }
6514 mediaSurface->uiLockedBufID = VA_INVALID_ID;
6515
6516 VAImageID imageID = (VAImageID)(mediaSurface->uiLockedImageID);
6517 vaStatus = DdiMedia_DestroyImage(ctx,imageID);
6518 if (vaStatus != VA_STATUS_SUCCESS)
6519 {
6520 return vaStatus;
6521 }
6522 mediaSurface->uiLockedImageID = VA_INVALID_ID;
6523
6524 MOS_TraceEventExt(EVENT_VA_UNLOCK, EVENT_TYPE_END, nullptr, 0, nullptr, 0);
6525 return VA_STATUS_SUCCESS;
6526 }
6527
6528 //!
6529 //! \brief Query video proc filters
6530 //!
6531 //! \param [in] ctx
6532 //! Pointer to VA driver context
6533 //! \param [in] context
6534 //! VA context ID
6535 //! \param [in] filters
6536 //! VA proc filter type
6537 //! \param [in] num_filters
6538 //! Number of filters
6539 //!
6540 //! \return VAStatus
6541 //! VA_STATUS_SUCCESS if success, else fail reason
6542 //!
6543 VAStatus
6544 DdiMedia_QueryVideoProcFilters(
6545 VADriverContextP ctx,
6546 VAContextID context,
6547 VAProcFilterType *filters,
6548 uint32_t *num_filters)
6549 {
6550 DDI_UNUSED(ctx);
6551 DDI_UNUSED(context);
6552
6553 DDI_FUNCTION_ENTER();
6554
6555 DDI_CHK_NULL(filters, "nullptr filters", VA_STATUS_ERROR_INVALID_PARAMETER);
6556 DDI_CHK_NULL(num_filters, "nullptr num_filters", VA_STATUS_ERROR_INVALID_PARAMETER);
6557
6558 uint32_t max_num_filters = DDI_VP_MAX_NUM_FILTERS;
6559 // check if array size is less than VP_MAX_NUM_FILTERS
6560 if(*num_filters < max_num_filters)
6561 {
6562 DDI_NORMALMESSAGE("num_filters %d < max_num_filters %d. Probably caused by Libva version upgrade!", *num_filters, max_num_filters);
6563 }
6564
6565 // Set the filters
6566 uint32_t i = 0;
6567 uint32_t num_supported_filters = 0;
6568 while(num_supported_filters < *num_filters && i < DDI_VP_MAX_NUM_FILTERS)
6569 {
6570 uint32_t num_filter_caps = 0;
6571 VAStatus vaStatus = DdiVp_QueryVideoProcFilterCaps(ctx, context, vp_supported_filters[i], nullptr, &num_filter_caps);
6572 if(vaStatus == VA_STATUS_SUCCESS && num_filter_caps != 0)
6573 {
6574 filters[num_supported_filters] = vp_supported_filters[i];
6575 num_supported_filters++;
6576 }
6577 i++;
6578 }
6579
6580 // Tell the app how many valid filters are filled in the array
6581 *num_filters = num_supported_filters;
6582
6583 return VA_STATUS_SUCCESS;
6584 }
6585
6586 //!
6587 //! \brief Query video processing filter capabilities.
6588 //! The real implementation is in media_libva_vp.c, since it needs to use some definitions in vphal.h.
6589 //!
6590 //! \param [in] ctx
6591 //! Pointer to VA driver context
6592 //! \param [in] context
6593 //! VA context ID
6594 //! \param [in] type
6595 //! VA proc filter type
6596 //! \param [inout] filter_caps
6597 //! FIlter caps
6598 //! \param [inout] num_filter_caps
6599 //! Number of filter caps
6600 //!
6601 //! \return VAStatus
6602 //! VA_STATUS_SUCCESS if success, else fail reason
6603 //!
6604 VAStatus
6605 DdiMedia_QueryVideoProcFilterCaps(
6606 VADriverContextP ctx,
6607 VAContextID context,
6608 VAProcFilterType type,
6609 void *filter_caps,
6610 uint32_t *num_filter_caps
6611 )
6612 {
6613 DDI_FUNCTION_ENTER();
6614
6615 return DdiVp_QueryVideoProcFilterCaps(ctx, context, type, filter_caps, num_filter_caps);
6616 }
6617
6618 //!
6619 //! \brief Query video proc pipeline caps
6620 //!
6621 //! \param [in] ctx
6622 //! Pointer to VA driver context
6623 //! \param [in] context
6624 //! VA context ID
6625 //! \param [in] filters
6626 //! VA buffer ID
6627 //! \param [in] num_filters
6628 //! Number of filters
6629 //! \param [in] pipeline_caps
6630 //! VA proc pipeline caps
6631 //!
6632 //! \return VAStatus
6633 //! VA_STATUS_SUCCESS if success, else fail reason
6634 //!
6635 VAStatus
6636 DdiMedia_QueryVideoProcPipelineCaps(
6637 VADriverContextP ctx,
6638 VAContextID context,
6639 VABufferID *filters,
6640 uint32_t num_filters,
6641 VAProcPipelineCaps *pipeline_caps
6642 )
6643 {
6644 DDI_FUNCTION_ENTER();
6645
6646 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
6647
6648 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
6649 DDI_CHK_NULL(pipeline_caps, "nullptr pipeline_caps", VA_STATUS_ERROR_INVALID_PARAMETER);
6650 if (num_filters > 0)
6651 DDI_CHK_NULL(filters, "nullptr filters", VA_STATUS_ERROR_INVALID_PARAMETER);
6652
6653 pipeline_caps->pipeline_flags = VA_PROC_PIPELINE_FAST;
6654 pipeline_caps->filter_flags = 0;
6655 pipeline_caps->rotation_flags = (1 << VA_ROTATION_NONE) | (1 << VA_ROTATION_90) | (1 << VA_ROTATION_180) | (1 << VA_ROTATION_270);
6656 pipeline_caps->mirror_flags = VA_MIRROR_HORIZONTAL | VA_MIRROR_VERTICAL;
6657 pipeline_caps->blend_flags = VA_BLEND_GLOBAL_ALPHA | VA_BLEND_PREMULTIPLIED_ALPHA | VA_BLEND_LUMA_KEY;
6658 pipeline_caps->num_forward_references = DDI_CODEC_NUM_FWD_REF;
6659 pipeline_caps->num_backward_references = DDI_CODEC_NUM_BK_REF;
6660 pipeline_caps->input_color_standards = vp_input_color_std;
6661 pipeline_caps->num_input_color_standards = DDI_VP_NUM_INPUT_COLOR_STD;
6662 pipeline_caps->output_color_standards = vp_output_color_std;
6663 pipeline_caps->num_output_color_standards = DDI_VP_NUM_OUT_COLOR_STD;
6664
6665 if ((context & DDI_MEDIA_MASK_VACONTEXT_TYPE) == DDI_MEDIA_VACONTEXTID_OFFSET_DECODER)
6666 {
6667 //Decode+SFC, go SFC path, the restriction here is the capability of SFC
6668 pipeline_caps->num_input_pixel_formats = 1;
6669 pipeline_caps->input_pixel_format[0] = VA_FOURCC_NV12;
6670 pipeline_caps->num_output_pixel_formats = 1;
6671 pipeline_caps->output_pixel_format[0] = VA_FOURCC_NV12;
6672 if((MEDIA_IS_SKU(&(mediaCtx->SkuTable), FtrHCP2SFCPipe)))
6673 {
6674 pipeline_caps->max_input_width = DDI_DECODE_HCP_SFC_MAX_WIDTH;
6675 pipeline_caps->max_input_height = DDI_DECODE_HCP_SFC_MAX_HEIGHT;
6676 }
6677 else
6678 {
6679 pipeline_caps->max_input_width = DDI_DECODE_SFC_MAX_WIDTH;
6680 pipeline_caps->max_input_height = DDI_DECODE_SFC_MAX_HEIGHT;
6681 }
6682 pipeline_caps->min_input_width = DDI_DECODE_SFC_MIN_WIDTH;
6683 pipeline_caps->min_input_height = DDI_DECODE_SFC_MIN_HEIGHT;
6684 pipeline_caps->max_output_width = DDI_DECODE_SFC_MAX_WIDTH;
6685 pipeline_caps->max_output_height = DDI_DECODE_SFC_MAX_HEIGHT;
6686 pipeline_caps->min_output_width = DDI_DECODE_SFC_MIN_WIDTH;
6687 pipeline_caps->min_output_height = DDI_DECODE_SFC_MIN_HEIGHT;
6688 }
6689 else if ((context & DDI_MEDIA_MASK_VACONTEXT_TYPE) == DDI_MEDIA_VACONTEXTID_OFFSET_VP)
6690 {
6691 if(mediaCtx->platform.eRenderCoreFamily <= IGFX_GEN8_CORE)
6692 {
6693 //Capability of Gen8- platform
6694 pipeline_caps->max_input_width = VP_MAX_PIC_WIDTH_Gen8;
6695 pipeline_caps->max_input_height = VP_MAX_PIC_HEIGHT_Gen8;
6696 pipeline_caps->max_output_width = VP_MAX_PIC_WIDTH_Gen8;
6697 pipeline_caps->max_output_height = VP_MAX_PIC_HEIGHT_Gen8;
6698 }else
6699 {
6700 //Capability of Gen9+ platform
6701 pipeline_caps->max_input_width = VP_MAX_PIC_WIDTH;
6702 pipeline_caps->max_input_height = VP_MAX_PIC_HEIGHT;
6703 pipeline_caps->max_output_width = VP_MAX_PIC_WIDTH;
6704 pipeline_caps->max_output_height = VP_MAX_PIC_HEIGHT;
6705 }
6706 pipeline_caps->min_input_width = VP_MIN_PIC_WIDTH;
6707 pipeline_caps->min_input_height = VP_MIN_PIC_HEIGHT;
6708 pipeline_caps->min_output_width = VP_MIN_PIC_WIDTH;
6709 pipeline_caps->min_output_height = VP_MIN_PIC_WIDTH;
6710 }
6711
6712
6713 for (int i = 0; i < num_filters; i++) {
6714 void *pData;
6715 DdiMedia_MapBuffer(ctx, filters[i], &pData);
6716 DDI_CHK_NULL(pData, "nullptr pData", VA_STATUS_ERROR_INVALID_PARAMETER);
6717 VAProcFilterParameterBufferBase* base_param = (VAProcFilterParameterBufferBase*) pData;
6718 if (base_param->type == VAProcFilterDeinterlacing)
6719 {
6720 VAProcFilterParameterBufferDeinterlacing *di_param = (VAProcFilterParameterBufferDeinterlacing *)base_param;
6721 if (di_param->algorithm == VAProcDeinterlacingMotionAdaptive ||
6722 di_param->algorithm == VAProcDeinterlacingMotionCompensated)
6723 {
6724 pipeline_caps->num_forward_references = 1;
6725 }
6726 }
6727 }
6728 return VA_STATUS_SUCCESS;
6729 }
6730
6731 /**
6732 * \brief Get surface attributes for the supplied config.
6733 *
6734 * This function retrieves the surface attributes matching the supplied
6735 * config. The caller shall provide an \c attrib_list with all attributes
6736 * to be retrieved. Upon successful return, the attributes in \c attrib_list
6737 * are updated with the requested value. Unknown attributes or attributes
6738 * that are not supported for the given config will have their \c flags
6739 * field set to \c VA_SURFACE_ATTRIB_NOT_SUPPORTED.
6740 *
6741 * param[in] ctx the VA display
6742 * param[in] config the config identifying a codec or a video
6743 * processing pipeline
6744 * param[out] attrib_list the list of attributes on output, with at
6745 * least \c type fields filled in, and possibly \c value fields whenever
6746 * necessary.The updated list of attributes and flags on output
6747 * param[in] num_attribs the number of attributes supplied in the
6748 * \c attrib_list array
6749 */
6750 VAStatus DdiMedia_GetSurfaceAttributes(
6751 VADriverContextP ctx,
6752 VAConfigID config,
6753 VASurfaceAttrib *attrib_list,
6754 uint32_t num_attribs
6755 )
6756 {
6757 DDI_UNUSED(ctx);
6758 DDI_UNUSED(config);
6759 DDI_UNUSED(attrib_list);
6760 DDI_UNUSED(num_attribs);
6761
6762 DDI_FUNCTION_ENTER();
6763
6764 VAStatus vaStatus = VA_STATUS_ERROR_UNIMPLEMENTED;
6765
6766 return vaStatus;
6767 }
6768
6769 //!
6770 //! \brief Aquire buffer handle
6771 //!
6772 //! \param [in] ctx
6773 //! Pointer to VA driver context
6774 //! \param [in] buf_id
6775 //! VA buffer ID
6776 //! \param [in] buf_info
6777 //! VA buffer Info
6778 //!
6779 //! \return VAStatus
6780 //! VA_STATUS_SUCCESS if success, else fail reason
6781 //!
6782 VAStatus DdiMedia_AcquireBufferHandle(
6783 VADriverContextP ctx,
6784 VABufferID buf_id,
6785 VABufferInfo *buf_info)
6786 {
6787 DDI_FUNCTION_ENTER();
6788
6789 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
6790 DDI_CHK_NULL(buf_info, "nullptr buf_info", VA_STATUS_ERROR_INVALID_PARAMETER);
6791
6792 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
6793 DDI_CHK_NULL(mediaCtx, "Invalid Media ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
6794
6795 DDI_MEDIA_BUFFER *buf = DdiMedia_GetBufferFromVABufferID(mediaCtx, buf_id);
6796 DDI_CHK_NULL(buf, "Invalid Media Buffer", VA_STATUS_ERROR_INVALID_BUFFER);
6797 DDI_CHK_NULL(buf->bo, "Invalid Media Buffer", VA_STATUS_ERROR_INVALID_BUFFER);
6798
6799 // If user did not specify memtype he want's we use something we prefer, we prefer PRIME
6800 if (!buf_info->mem_type)
6801 {
6802 buf_info->mem_type = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME;
6803 }
6804 // now chekcing memtype whether we support it
6805 if ((buf_info->mem_type != VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME) &&
6806 (buf_info->mem_type != VA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM))
6807 {
6808 return VA_STATUS_ERROR_UNSUPPORTED_MEMORY_TYPE;
6809 }
6810
6811 DdiMediaUtil_LockMutex(&mediaCtx->BufferMutex);
6812 // already acquired?
6813 if (buf->uiExportcount)
6814 { // yes, already acquired
6815 // can't provide access thru another memtype
6816 if (buf->uiMemtype != buf_info->mem_type)
6817 {
6818 DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex);
6819 return VA_STATUS_ERROR_INVALID_PARAMETER;
6820 }
6821 }
6822 else
6823 { // no, not acquired - doing this now
6824 switch (buf_info->mem_type) {
6825 case VA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM: {
6826 uint32_t flink = 0;
6827 if (mos_bo_flink(buf->bo, &flink) != 0)
6828 {
6829 DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex);
6830 return VA_STATUS_ERROR_INVALID_BUFFER;
6831 }
6832 buf->handle = (intptr_t)flink;
6833 break;
6834 }
6835 case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME: {
6836 int32_t prime_fd = 0;
6837 if (mos_bo_export_to_prime(buf->bo, &prime_fd) != 0)
6838 {
6839 DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex);
6840 return VA_STATUS_ERROR_INVALID_BUFFER;
6841 }
6842
6843 buf->handle = (intptr_t)prime_fd;
6844 break;
6845 }
6846 }
6847 // saving memtepy which was provided to the user
6848 buf->uiMemtype = buf_info->mem_type;
6849 }
6850
6851 ++buf->uiExportcount;
6852 mos_bo_reference(buf->bo);
6853
6854 buf_info->type = buf->uiType;
6855 buf_info->handle = buf->handle;
6856 buf_info->mem_size = buf->uiNumElements * buf->iSize;
6857
6858 DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex);
6859 return VA_STATUS_SUCCESS;
6860 }
6861
6862 //!
6863 //! \brief Release buffer handle
6864 //!
6865 //! \param [in] ctx
6866 //! Pointer to VA driver context
6867 //! \param [in] buf_id
6868 //! VA bufferID
6869 //!
6870 //! \return VAStatus
6871 //! VA_STATUS_SUCCESS if success, else fail reason
6872 //!
6873 VAStatus DdiMedia_ReleaseBufferHandle(
6874 VADriverContextP ctx,
6875 VABufferID buf_id)
6876 {
6877 DDI_FUNCTION_ENTER();
6878
6879 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
6880
6881 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
6882 DDI_CHK_NULL(mediaCtx, "Invalid Media ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
6883
6884 DDI_MEDIA_BUFFER *buf = DdiMedia_GetBufferFromVABufferID(mediaCtx, buf_id);
6885 DDI_CHK_NULL(buf, "Invalid Media Buffer", VA_STATUS_ERROR_INVALID_BUFFER);
6886 DDI_CHK_NULL(buf->bo, "Invalid Media Buffer", VA_STATUS_ERROR_INVALID_BUFFER);
6887
6888 DdiMediaUtil_LockMutex(&mediaCtx->BufferMutex);
6889 if (!buf->uiMemtype || !buf->uiExportcount)
6890 {
6891 DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex);
6892 return VA_STATUS_SUCCESS;
6893 }
6894 mos_bo_unreference(buf->bo);
6895 --buf->uiExportcount;
6896
6897 if (!buf->uiExportcount) {
6898 switch (buf->uiMemtype) {
6899 case VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME: {
6900 close((intptr_t)buf->handle);
6901 break;
6902 }
6903 }
6904 buf->uiMemtype = 0;
6905 }
6906 DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex);
6907
6908 if (!buf->uiExportcount && buf->bPostponedBufFree) {
6909 MOS_FreeMemory(buf);
6910 DdiMedia_DestroyBufFromVABufferID(mediaCtx, buf_id);
6911 }
6912
6913 return VA_STATUS_SUCCESS;
6914 }
6915
6916 static uint32_t DdiMedia_GetPlaneNum(PDDI_MEDIA_SURFACE mediaSurface, bool hasAuxPlane)
6917 {
6918 DDI_CHK_NULL(mediaSurface, "nullptr mediaSurface", VA_STATUS_ERROR_INVALID_PARAMETER);
6919
6920 uint32_t fourcc = DdiMedia_MediaFormatToOsFormat(mediaSurface->format);
6921 uint32_t plane_num = 0;
6922 switch(fourcc)
6923 {
6924 case VA_FOURCC_NV12:
6925 case VA_FOURCC_NV21:
6926 case VA_FOURCC_P010:
6927 case VA_FOURCC_P012:
6928 case VA_FOURCC_P016:
6929 plane_num = hasAuxPlane ? 4 : 2;
6930 break;
6931 case VA_FOURCC_I420:
6932 case VA_FOURCC_YV12:
6933 case VA_FOURCC_411P:
6934 case VA_FOURCC_422H:
6935 case VA_FOURCC_422V:
6936 case VA_FOURCC_444P:
6937 case VA_FOURCC_IMC3:
6938 case VA_FOURCC_RGBP:
6939 case VA_FOURCC_BGRP:
6940 plane_num = 3;
6941 break;
6942 case VA_FOURCC_YUY2:
6943 case VA_FOURCC_UYVY:
6944 case VA_FOURCC_YVYU:
6945 case VA_FOURCC_VYUY:
6946 case VA_FOURCC_Y800:
6947 case VA_FOURCC_Y210:
6948 #if VA_CHECK_VERSION(1, 9, 0)
6949 case VA_FOURCC_Y212:
6950 #endif
6951 case VA_FOURCC_Y216:
6952 case VA_FOURCC_Y410:
6953 #if VA_CHECK_VERSION(1, 9, 0)
6954 case VA_FOURCC_Y412:
6955 #endif
6956 case VA_FOURCC_Y416:
6957 case VA_FOURCC_AYUV:
6958 #if VA_CHECK_VERSION(1, 13, 0)
6959 case VA_FOURCC_XYUV:
6960 #endif
6961 case VA_FOURCC_RGBA:
6962 case VA_FOURCC_RGBX:
6963 case VA_FOURCC_BGRA:
6964 case VA_FOURCC_BGRX:
6965 case VA_FOURCC_ARGB:
6966 case VA_FOURCC_ABGR:
6967 case VA_FOURCC_XRGB:
6968 case VA_FOURCC_XBGR:
6969 case VA_FOURCC_RGB565:
6970 case VA_FOURCC_R8G8B8:
6971 case VA_FOURCC_A2R10G10B10:
6972 case VA_FOURCC_A2B10G10R10:
6973 case VA_FOURCC_X2R10G10B10:
6974 case VA_FOURCC_X2B10G10R10:
6975 plane_num = hasAuxPlane ? 2 : 1;
6976 break;
6977 default:
6978 DDI_ASSERTMESSAGE("Unsupported format.\n");
6979 }
6980 return plane_num;
6981 }
6982
6983 static uint32_t DdiMedia_GetDrmFormatOfSeparatePlane(uint32_t fourcc, int plane)
6984 {
6985 if (plane == 0)
6986 {
6987 switch (fourcc)
6988 {
6989 case VA_FOURCC_NV12:
6990 case VA_FOURCC_I420:
6991 case VA_FOURCC_IMC3:
6992 case VA_FOURCC_YV12:
6993 case VA_FOURCC_YV16:
6994 case VA_FOURCC_422H:
6995 case VA_FOURCC_422V:
6996 case VA_FOURCC_444P:
6997 case VA_FOURCC_Y800:
6998 case VA_FOURCC_RGBP:
6999 case VA_FOURCC_BGRP:
7000 return DRM_FORMAT_R8;
7001 case VA_FOURCC_P010:
7002 case VA_FOURCC_P012:
7003 case VA_FOURCC_P016:
7004 case VA_FOURCC_I010:
7005 return DRM_FORMAT_R16;
7006
7007 case VA_FOURCC_YUY2:
7008 return DRM_FORMAT_YUYV;
7009 case VA_FOURCC_YVYU:
7010 return DRM_FORMAT_YVYU;
7011 case VA_FOURCC_VYUY:
7012 return DRM_FORMAT_VYUY;
7013 case VA_FOURCC_UYVY:
7014 return DRM_FORMAT_UYVY;
7015 case VA_FOURCC_AYUV:
7016 return DRM_FORMAT_AYUV;
7017 #if VA_CHECK_VERSION(1, 13, 0)
7018 case VA_FOURCC_XYUV:
7019 return DRM_FORMAT_XYUV8888;
7020 #endif
7021 case VA_FOURCC_Y210:
7022 return DRM_FORMAT_Y210;
7023 case VA_FOURCC_Y216:
7024 return DRM_FORMAT_Y216;
7025 case VA_FOURCC_Y410:
7026 return DRM_FORMAT_Y410;
7027 case VA_FOURCC_Y416:
7028 return DRM_FORMAT_Y416;
7029 #if VA_CHECK_VERSION(1, 9, 0)
7030 case VA_FOURCC_Y212:
7031 return DRM_FORMAT_Y216;
7032 case VA_FOURCC_Y412:
7033 return DRM_FORMAT_Y416;
7034 #endif
7035
7036 case VA_FOURCC_ARGB:
7037 return DRM_FORMAT_ARGB8888;
7038 case VA_FOURCC_ABGR:
7039 return DRM_FORMAT_ABGR8888;
7040 case VA_FOURCC_RGBA:
7041 return DRM_FORMAT_RGBA8888;
7042 case VA_FOURCC_BGRA:
7043 return DRM_FORMAT_BGRA8888;
7044 case VA_FOURCC_XRGB:
7045 return DRM_FORMAT_XRGB8888;
7046 case VA_FOURCC_XBGR:
7047 return DRM_FORMAT_XBGR8888;
7048 case VA_FOURCC_RGBX:
7049 return DRM_FORMAT_RGBX8888;
7050 case VA_FOURCC_BGRX:
7051 return DRM_FORMAT_BGRX8888;
7052 case VA_FOURCC_A2R10G10B10:
7053 return DRM_FORMAT_ARGB2101010;
7054 case VA_FOURCC_A2B10G10R10:
7055 return DRM_FORMAT_ABGR2101010;
7056 case VA_FOURCC_X2R10G10B10:
7057 return DRM_FORMAT_XRGB2101010;
7058 case VA_FOURCC_X2B10G10R10:
7059 return DRM_FORMAT_XBGR2101010;
7060 }
7061 }
7062 else
7063 {
7064 switch (fourcc)
7065 {
7066 case VA_FOURCC_NV12:
7067 return DRM_FORMAT_GR88;
7068 case VA_FOURCC_I420:
7069 case VA_FOURCC_IMC3:
7070 case VA_FOURCC_YV12:
7071 case VA_FOURCC_YV16:
7072 case VA_FOURCC_422H:
7073 case VA_FOURCC_422V:
7074 case VA_FOURCC_444P:
7075 case VA_FOURCC_RGBP:
7076 case VA_FOURCC_BGRP:
7077 return DRM_FORMAT_R8;
7078 case VA_FOURCC_P010:
7079 case VA_FOURCC_P012:
7080 case VA_FOURCC_P016:
7081 return DRM_FORMAT_GR1616;
7082 case VA_FOURCC_I010:
7083 return DRM_FORMAT_R16;
7084 }
7085 }
7086 return 0;
7087 }
7088 static uint32_t DdiMedia_GetDrmFormatOfCompositeObject(uint32_t fourcc)
7089 {
7090 switch (fourcc)
7091 {
7092 case VA_FOURCC_NV12:
7093 return DRM_FORMAT_NV12;
7094 case VA_FOURCC_I420:
7095 return DRM_FORMAT_YUV420;
7096 case VA_FOURCC_IMC3:
7097 return DRM_FORMAT_YUV420;
7098 case VA_FOURCC_YV12:
7099 return DRM_FORMAT_YVU420;
7100 case VA_FOURCC_YV16:
7101 return DRM_FORMAT_YVU422;
7102 case VA_FOURCC_422H:
7103 return DRM_FORMAT_YUV422;
7104 case VA_FOURCC_422V:
7105 return DRM_FORMAT_YUV422;
7106 case VA_FOURCC_444P:
7107 return DRM_FORMAT_YUV444;
7108 case VA_FOURCC_YUY2:
7109 return DRM_FORMAT_YUYV;
7110 case VA_FOURCC_YVYU:
7111 return DRM_FORMAT_YVYU;
7112 case VA_FOURCC_VYUY:
7113 return DRM_FORMAT_VYUY;
7114 case VA_FOURCC_UYVY:
7115 return DRM_FORMAT_UYVY;
7116 case VA_FOURCC_AYUV:
7117 return DRM_FORMAT_AYUV;
7118 #if VA_CHECK_VERSION(1, 13, 0)
7119 case VA_FOURCC_XYUV:
7120 return DRM_FORMAT_XYUV8888;
7121 #endif
7122 case VA_FOURCC_Y210:
7123 return DRM_FORMAT_Y210;
7124 #if VA_CHECK_VERSION(1, 9, 0)
7125 case VA_FOURCC_Y212:
7126 return DRM_FORMAT_Y216;
7127 #endif
7128 case VA_FOURCC_Y216:
7129 return DRM_FORMAT_Y216;
7130 case VA_FOURCC_Y410:
7131 return DRM_FORMAT_Y410;
7132 #if VA_CHECK_VERSION(1, 9, 0)
7133 case VA_FOURCC_Y412:
7134 return DRM_FORMAT_Y416;
7135 #endif
7136 case VA_FOURCC_Y416:
7137 return DRM_FORMAT_Y416;
7138 case VA_FOURCC_Y800:
7139 return DRM_FORMAT_R8;
7140 case VA_FOURCC_P010:
7141 return DRM_FORMAT_P010;
7142 case VA_FOURCC_P012:
7143 return DRM_FORMAT_P016;
7144 case VA_FOURCC_P016:
7145 return DRM_FORMAT_P016;
7146 case VA_FOURCC_ARGB:
7147 return DRM_FORMAT_ARGB8888;
7148 case VA_FOURCC_ABGR:
7149 return DRM_FORMAT_ABGR8888;
7150 case VA_FOURCC_RGBA:
7151 return DRM_FORMAT_RGBA8888;
7152 case VA_FOURCC_BGRA:
7153 return DRM_FORMAT_BGRA8888;
7154 case VA_FOURCC_XRGB:
7155 return DRM_FORMAT_XRGB8888;
7156 case VA_FOURCC_XBGR:
7157 return DRM_FORMAT_XBGR8888;
7158 case VA_FOURCC_RGBX:
7159 return DRM_FORMAT_RGBX8888;
7160 case VA_FOURCC_BGRX:
7161 return DRM_FORMAT_BGRX8888;
7162 case VA_FOURCC_A2R10G10B10:
7163 return DRM_FORMAT_ARGB2101010;
7164 case VA_FOURCC_A2B10G10R10:
7165 return DRM_FORMAT_ABGR2101010;
7166 case VA_FOURCC_X2R10G10B10:
7167 return DRM_FORMAT_XRGB2101010;
7168 case VA_FOURCC_X2B10G10R10:
7169 return DRM_FORMAT_XBGR2101010;
7170 }
7171 return 0;
7172 }
7173
7174
7175 //!
7176 //! \brief API for export surface handle to other component
7177 //!
7178 //! \param [in] dpy
7179 //! VA display.
7180 //! \param [in] surface_id
7181 //! Surface to export.
7182 //! \param [in] mem_type
7183 //! Memory type to export to.
7184 //! \param [in] flags
7185 //! Combination of flags to apply
7186 //!\param [out] descriptor
7187 //!Pointer to the descriptor structure to fill
7188 //!with the handle details. The type of this structure depends on
7189 //!the value of mem_type.
7190 //! \return VAStatus
7191 //! VA_STATUS_SUCCESS if success, else fail reason
7192 //!
7193 VAStatus DdiMedia_ExportSurfaceHandle(
7194 VADriverContextP ctx,
7195 VASurfaceID surface_id,
7196 uint32_t mem_type,
7197 uint32_t flags,
7198 void * descriptor)
7199 {
7200 DDI_CHK_NULL(descriptor, "nullptr descriptor", VA_STATUS_ERROR_INVALID_PARAMETER);
7201 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
7202
7203 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
7204 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
7205 DDI_CHK_NULL(mediaCtx->pSurfaceHeap, "nullptr mediaCtx->pSurfaceHeap", VA_STATUS_ERROR_INVALID_CONTEXT);
7206 DDI_CHK_LESS((uint32_t)(surface_id), mediaCtx->pSurfaceHeap->uiAllocatedHeapElements, "Invalid surfaces", VA_STATUS_ERROR_INVALID_SURFACE);
7207
7208 DDI_MEDIA_SURFACE *mediaSurface = DdiMedia_GetSurfaceFromVASurfaceID(mediaCtx, surface_id);
7209 DDI_CHK_NULL(mediaSurface, "nullptr mediaSurface", VA_STATUS_ERROR_INVALID_SURFACE);
7210 DDI_CHK_NULL(mediaSurface->bo, "nullptr mediaSurface->bo", VA_STATUS_ERROR_INVALID_SURFACE);
7211 DDI_CHK_NULL(mediaSurface->pGmmResourceInfo, "nullptr mediaSurface->pGmmResourceInfo", VA_STATUS_ERROR_INVALID_SURFACE);
7212
7213 if (
7214 #if VA_CHECK_VERSION(1, 21, 0)
7215 mem_type != VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_3 &&
7216 #endif
7217 mem_type != VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2)
7218 {
7219 DDI_ASSERTMESSAGE("vaExportSurfaceHandle: memory type %08x is not supported.\n", mem_type);
7220 return VA_STATUS_ERROR_UNSUPPORTED_MEMORY_TYPE;
7221 }
7222
7223 if (mos_bo_export_to_prime(mediaSurface->bo, (int32_t*)&mediaSurface->name))
7224 {
7225 DDI_ASSERTMESSAGE("Failed drm_intel_gem_export_to_prime operation!!!\n");
7226 return VA_STATUS_ERROR_OPERATION_FAILED;
7227 }
7228
7229 VADRMPRIMESurfaceDescriptor *desc = (VADRMPRIMESurfaceDescriptor *)descriptor;
7230
7231 #if VA_CHECK_VERSION(1, 21, 0)
7232 if(mem_type == VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_3)
7233 {
7234 VADRMPRIME3SurfaceDescriptor *desc = (VADRMPRIME3SurfaceDescriptor *)descriptor;
7235 if(mediaSurface->pGmmResourceInfo->GetSetCpSurfTag(false, 0))
7236 {
7237 desc->flags |= VA_SURFACE_EXTBUF_DESC_PROTECTED;
7238 }
7239 }
7240 #endif
7241
7242 desc->fourcc = DdiMedia_MediaFormatToOsFormat(mediaSurface->format);
7243 if(desc->fourcc == VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT)
7244 {
7245 // close the handle to avoid leak
7246 close(mediaSurface->name);
7247 return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
7248 }
7249 desc->width = mediaSurface->iWidth;
7250 desc->height = mediaSurface->iRealHeight;
7251 desc->num_objects = 1;
7252 desc->objects[0].fd = mediaSurface->name;
7253 desc->objects[0].size = mediaSurface->pGmmResourceInfo->GetSizeSurface();
7254
7255 DDI_CHK_NULL(mediaCtx->m_caps, "nullptr m_caps", VA_STATUS_ERROR_INVALID_CONTEXT);
7256
7257 if(VA_STATUS_SUCCESS != mediaCtx->m_caps->GetSurfaceModifier(mediaSurface, desc->objects[0].drm_format_modifier))
7258 {
7259 close(mediaSurface->name);
7260 DDI_ASSERTMESSAGE("could not find related modifier values");
7261 return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
7262 }
7263
7264 int composite_object = flags & VA_EXPORT_SURFACE_COMPOSED_LAYERS;
7265
7266 uint32_t formats[4];
7267
7268 // Query Aux Plane info form GMM
7269 bool hasAuxPlane = false;
7270 GMM_RESOURCE_FLAG GmmFlags = mediaSurface->pGmmResourceInfo->GetResFlags();
7271
7272 if (((GmmFlags.Gpu.MMC ||
7273 GmmFlags.Gpu.CCS) &&
7274 (GmmFlags.Info.MediaCompressed ||
7275 GmmFlags.Info.RenderCompressed) &&
7276 mediaCtx->m_auxTableMgr) )
7277 {
7278 hasAuxPlane = true;
7279 }
7280 else
7281 {
7282 hasAuxPlane = false;
7283 }
7284
7285 uint32_t num_planes = DdiMedia_GetPlaneNum(mediaSurface, hasAuxPlane);
7286
7287 if(composite_object)
7288 {
7289 formats[0] = DdiMedia_GetDrmFormatOfCompositeObject(desc->fourcc);
7290 if(!formats[0])
7291 {
7292 close(mediaSurface->name);
7293 DDI_ASSERTMESSAGE("vaExportSurfaceHandle: fourcc %08x is not supported for export as a composite object.\n", desc->fourcc);
7294 return VA_STATUS_ERROR_INVALID_SURFACE;
7295 }
7296 }
7297 else
7298 {
7299 for (int i = 0; i < num_planes; i++)
7300 {
7301 formats[i] = DdiMedia_GetDrmFormatOfSeparatePlane(desc->fourcc,i);
7302 if (!formats[i])
7303 {
7304 close(mediaSurface->name);
7305 DDI_ASSERTMESSAGE("vaExportSurfaceHandle: fourcc %08x is not supported for export as separate planes.\n", desc->fourcc);
7306 return VA_STATUS_ERROR_INVALID_SURFACE;
7307 }
7308 }
7309 }
7310
7311 uint32_t pitch, height, chromaPitch, chromaHeight = 0;
7312 pitch = mediaSurface->iPitch;
7313 height = mediaSurface->iRealHeight;
7314 DdiMedia_GetChromaPitchHeight(desc->fourcc, pitch, height, &chromaPitch, &chromaHeight);
7315
7316 // Get Yoffset from GMM
7317 GMM_REQ_OFFSET_INFO reqInfo = {0};
7318 reqInfo.Plane = GMM_PLANE_Y;
7319 reqInfo.ReqRender = 1;
7320 mediaSurface->pGmmResourceInfo->GetOffset(reqInfo);
7321 uint32_t offsetY = reqInfo.Render.Offset;
7322
7323 // Get Uoffset from GMM
7324 MOS_ZeroMemory(&reqInfo, sizeof(GMM_REQ_OFFSET_INFO));
7325 reqInfo.Plane = GMM_PLANE_U;
7326 reqInfo.ReqRender = 1;
7327 mediaSurface->pGmmResourceInfo->GetOffset(reqInfo);
7328 uint32_t offsetU = reqInfo.Render.Offset;
7329
7330 // Get Voffset from GMM
7331 MOS_ZeroMemory(&reqInfo, sizeof(GMM_REQ_OFFSET_INFO));
7332 reqInfo.Plane = GMM_PLANE_V;
7333 reqInfo.ReqRender = 1;
7334 mediaSurface->pGmmResourceInfo->GetOffset(reqInfo);
7335 uint32_t offsetV = reqInfo.Render.Offset;
7336
7337 // Get Aux Plane offset
7338 uint32_t auxOffsetY = (uint32_t)mediaSurface->pGmmResourceInfo->GetPlanarAuxOffset(0, GMM_AUX_Y_CCS);
7339 uint32_t auxOffsetUV = (uint32_t)mediaSurface->pGmmResourceInfo->GetPlanarAuxOffset(0, GMM_AUX_UV_CCS);
7340
7341 if (composite_object) {
7342 desc->num_layers = 1;
7343 desc->layers[0].drm_format = formats[0];
7344 desc->layers[0].num_planes = num_planes;
7345 if (hasAuxPlane)
7346 {
7347 // For semi-planar formats like NV12, CCS planes follow the Y and UV planes,
7348 // i.e. planes 0 and 1 are used for Y and UV surfaces, planes 2 and 3 for the respective CCS.
7349 for (int i = 0; i < num_planes/2; i++)
7350 {
7351 desc->layers[0].object_index[2*i] = 0;
7352 desc->layers[0].object_index[2*i+1] = 0;
7353 if (i == 0)
7354 {
7355 // Y plane
7356 desc->layers[0].offset[i] = offsetY;
7357 desc->layers[0].pitch[i] = mediaSurface->iPitch;
7358 // Y aux plane
7359 desc->layers[0].offset[i + num_planes/2] = auxOffsetY;
7360 desc->layers[0].pitch[i + num_planes/2] = mediaSurface->iPitch/8;
7361 }
7362 else
7363 {
7364 // UV plane
7365 desc->layers[0].offset[i] = offsetU;
7366 desc->layers[0].pitch[i] = mediaSurface->iPitch;
7367 // UV aux plane
7368 desc->layers[0].offset[i + num_planes/2] = auxOffsetUV;
7369 desc->layers[0].pitch[i + num_planes/2] = mediaSurface->iPitch/8;
7370 }
7371 }
7372 }else
7373 {
7374 for (int i = 0; i < num_planes; i++)
7375 {
7376 desc->layers[0].object_index[i] = 0;
7377 switch(i)
7378 {
7379 case 0:
7380 desc->layers[0].offset[i] = offsetY;
7381 desc->layers[0].pitch[i] = pitch;
7382 break;
7383 case 1:
7384 if (desc->fourcc == VA_FOURCC_YV12)
7385 {
7386 desc->layers[0].offset[i] = offsetV;
7387 }
7388 else
7389 {
7390 desc->layers[0].offset[i] = offsetU;
7391 }
7392 desc->layers[0].pitch[i] = chromaPitch;
7393 break;
7394 case 2:
7395 if (desc->fourcc == VA_FOURCC_YV12)
7396 {
7397 desc->layers[0].offset[i] = offsetU;
7398 }
7399 else
7400 {
7401 desc->layers[0].offset[i] = offsetV;
7402 }
7403 desc->layers[0].pitch[i] = chromaPitch;
7404 break;
7405 default:
7406 DDI_ASSERTMESSAGE("vaExportSurfaceHandle: invalid plan numbers");
7407 }
7408 }
7409 }
7410 }
7411 else
7412 {
7413 if (hasAuxPlane)
7414 {
7415 desc->num_layers = num_planes / 2;
7416
7417 for (int i = 0; i < desc->num_layers; i++)
7418 {
7419 desc->layers[i].drm_format = formats[i];
7420 desc->layers[i].num_planes = 2;
7421
7422 desc->layers[i].object_index[0] = 0;
7423
7424 if (i == 0)
7425 {
7426 desc->layers[i].offset[0] = offsetY;
7427 desc->layers[i].offset[1] = auxOffsetY;
7428 desc->layers[i].pitch[0] = mediaSurface->iPitch;
7429 desc->layers[i].pitch[1] = mediaSurface->iPitch/8;
7430 }
7431 else
7432 {
7433 desc->layers[i].offset[0] = offsetU;
7434 desc->layers[i].offset[1] = auxOffsetUV;
7435 desc->layers[i].pitch[0] = mediaSurface->iPitch;
7436 desc->layers[i].pitch[1] = mediaSurface->iPitch/8;
7437 }
7438 }
7439 }else
7440 {
7441 desc->num_layers = num_planes;
7442
7443 for (int i = 0; i < num_planes; i++)
7444 {
7445 desc->layers[i].drm_format = formats[i];
7446 desc->layers[i].num_planes = 1;
7447
7448 desc->layers[i].object_index[0] = 0;
7449
7450 switch(i)
7451 {
7452 case 0:
7453 desc->layers[i].offset[0] = offsetY;
7454 desc->layers[i].pitch[0] = pitch;
7455 break;
7456 case 1:
7457 if (desc->fourcc == VA_FOURCC_YV12)
7458 {
7459 desc->layers[i].offset[0] = offsetV;
7460 }
7461 else
7462 {
7463 desc->layers[i].offset[0] = offsetU;
7464 }
7465 desc->layers[i].pitch[0] = chromaPitch;
7466 break;
7467 case 2:
7468 if (desc->fourcc == VA_FOURCC_YV12)
7469 {
7470 desc->layers[i].offset[0] = offsetU;
7471 }
7472 else
7473 {
7474 desc->layers[i].offset[0] = offsetV;
7475 }
7476 desc->layers[i].pitch[0] = chromaPitch;
7477 break;
7478 default:
7479 DDI_ASSERTMESSAGE("vaExportSurfaceHandle: invalid plan numbers");
7480 }
7481 }
7482 }
7483 }
7484
7485 return VA_STATUS_SUCCESS;
7486 }
7487
7488 //!
7489 //! \brief Init VA driver 0.31
7490 //!
7491 //! \param [in] ctx
7492 //! Pointer to VA driver context
7493 //!
7494 //! \return VAStatus
7495 //! VA_STATUS_SUCCESS if success, else fail reason
7496 //!
7497 VAStatus __vaDriverInit(VADriverContextP ctx )
7498 {
7499 return DdiMedia__Initialize(ctx, nullptr, nullptr);
7500 }
7501
7502 #ifdef __cplusplus
7503 extern "C" {
7504 #endif
7505
7506 //!
7507 //! \brief Get VA_MAJOR_VERSION and VA_MINOR_VERSION from libva
7508 //! To form the function name in the format of _vaDriverInit_[VA_MAJOR_VERSION]_[VA_MINOR_VERSION]
7509 //!
7510 #define VA_DRV_INIT_DEF(_major,_minor) __vaDriverInit_##_major##_##_minor
7511 #define VA_DRV_INIT_FUNC(va_major_version, va_minor_version) VA_DRV_INIT_DEF(va_major_version,va_minor_version)
7512 #define VA_DRV_INIT_FUC_NAME VA_DRV_INIT_FUNC(VA_MAJOR_VERSION,VA_MINOR_VERSION)
7513
7514 //!
7515 //! \brief VA driver init function name
7516 //!
7517 //! \param [in] ctx
7518 //! Pointer to VA driver context
7519 //!
7520 //! \return VAStatus
7521 //! VA_STATUS_SUCCESS if success, else fail reason
7522 //!
7523 MEDIAAPI_EXPORT VAStatus VA_DRV_INIT_FUC_NAME(VADriverContextP ctx )
7524 {
7525 return __vaDriverInit(ctx);
7526 }
7527
7528 //!
7529 //! \brief Private API for openCL
7530 //!
7531 //! \param [in] dpy
7532 //! VA display
7533 //! \param [in] surface
7534 //! VA surface ID
7535 //! \param [in] prime_fd
7536 //! Prime fd
7537 //!
7538 //! \return VAStatus
7539 //! VA_STATUS_SUCCESS if success, else fail reason
7540 //!
7541 MEDIAAPI_EXPORT VAStatus DdiMedia_ExtGetSurfaceHandle(
7542 VADisplay dpy,
7543 VASurfaceID *surface,
7544 int32_t *prime_fd)
7545 {
7546 DDI_CHK_NULL(dpy, "nullptr dpy", VA_STATUS_ERROR_INVALID_DISPLAY);
7547 DDI_CHK_NULL(surface, "nullptr surfaces", VA_STATUS_ERROR_INVALID_PARAMETER);
7548 DDI_CHK_NULL(prime_fd, "nullptr id", VA_STATUS_ERROR_INVALID_PARAMETER);
7549
7550 VADriverContextP ctx = ((VADisplayContextP)dpy)->pDriverContext;
7551 DDI_CHK_NULL(ctx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT);
7552
7553 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
7554 DDI_CHK_NULL(mediaCtx, "nullptr mediaCtx", VA_STATUS_ERROR_INVALID_CONTEXT);
7555 DDI_CHK_NULL(mediaCtx->pSurfaceHeap, "nullptr mediaCtx->pSurfaceHeap", VA_STATUS_ERROR_INVALID_CONTEXT);
7556 DDI_CHK_LESS((uint32_t)(*surface), mediaCtx->pSurfaceHeap->uiAllocatedHeapElements, "Invalid surfaces", VA_STATUS_ERROR_INVALID_SURFACE);
7557
7558 DDI_MEDIA_SURFACE *mediaSurface = DdiMedia_GetSurfaceFromVASurfaceID(mediaCtx, *surface);
7559 if (mediaSurface)
7560 {
7561 if (mediaSurface->bo)
7562 {
7563 int32_t ret = mos_bo_export_to_prime(mediaSurface->bo, (int32_t*)&mediaSurface->name);
7564 if (ret)
7565 {
7566 //LOGE("Failed drm_intel_gem_export_to_prime operation!!!\n");
7567 return VA_STATUS_ERROR_OPERATION_FAILED;
7568 }
7569 }
7570 }
7571 else
7572 {
7573 return VA_STATUS_ERROR_INVALID_SURFACE;
7574 }
7575
7576 *prime_fd = mediaSurface->name;
7577
7578 return VA_STATUS_SUCCESS;
7579 }
7580
7581 //!
7582 //! \brief Map buffer 2
7583 //!
7584 //! \param [in] dpy
7585 //! VA display
7586 //! \param [in] buf_id
7587 //! VA buffer ID
7588 //! \param [out] pbuf
7589 //! Pointer to buffer
7590 //! \param [in] flag
7591 //! Flag
7592 //!
7593 //! \return VAStatus
7594 //! VA_STATUS_SUCCESS if success, else fail reason
7595 //!
7596 MEDIAAPI_EXPORT VAStatus DdiMedia_MapBuffer2(
7597 VADisplay dpy,
7598 VABufferID buf_id,
7599 void **pbuf,
7600 int32_t flag
7601 )
7602 {
7603 DDI_CHK_NULL(dpy, "nullptr dpy", VA_STATUS_ERROR_INVALID_DISPLAY);
7604
7605 VADriverContextP ctx = ((VADisplayContextP)dpy)->pDriverContext;
7606
7607 if ((flag == 0) || (flag & ~(MOS_LOCKFLAG_READONLY | MOS_LOCKFLAG_WRITEONLY)))
7608 return VA_STATUS_ERROR_INVALID_PARAMETER;
7609
7610 return DdiMedia_MapBufferInternal(ctx, buf_id, pbuf, flag);
7611 }
7612
7613 #ifdef __cplusplus
7614 }
7615 #endif
7616