Debian Codegraph @

media_driver/linux/common/ddi/media_libva_util.cpp

raw
1 /*
2 * Copyright (c) 2009-2022, Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22 //!
23 //! \file media_libva_util.cpp
24 //! \brief libva(and its extension) utility
25 //!
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <pthread.h>
30 #include <sys/time.h>
31 #include <unistd.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <dlfcn.h>
36 #include <errno.h>
37 #include "inttypes.h"
38
39 #include "media_libva_util.h"
40 #include "mos_utilities.h"
41 #include "mos_os.h"
42 #include "mos_defs.h"
43 #include "hwinfo_linux.h"
44 #include "media_ddi_decode_base.h"
45 #include "media_ddi_encode_base.h"
46 #include "media_libva_decoder.h"
47 #include "media_libva_encoder.h"
48 #include "media_libva_caps.h"
49 #include "memory_policy_manager.h"
50 #include "drm_fourcc.h"
51
52 // default protected surface tag
53 #define PROTECTED_SURFACE_TAG 0x3000f
54
55 #ifdef DEBUG
56 static int32_t frameCountFps = -1;
57 static struct timeval tv1;
58 static pthread_mutex_t fpsMutex = PTHREAD_MUTEX_INITIALIZER;
59 static int32_t vaFpsSampleSize = 100;
60
61 #define LENGTH_OF_FPS_FILE_NAME 128
62
63 #ifdef ANDROID
64 #define FPS_FILE_NAME "/mnt/sdcard/fps.txt"
65 #else
66 #define FPS_FILE_NAME "./fps.txt"
67 #endif
68 #endif
69 #ifdef DEBUG
70 void DdiMediaUtil_MediaPrintFps()
71 {
72 struct timeval tv2;
73
74 if (0 == vaFpsSampleSize)
75 {
76 return;
77 }
78 gettimeofday(&tv2, 0);
79
80 pthread_mutex_lock(&fpsMutex);
81 if (-1 == frameCountFps)
82 {
83 gettimeofday(&tv1, 0);
84 }
85
86 if (++frameCountFps >= vaFpsSampleSize)
87 {
88 char fpsFileName[LENGTH_OF_FPS_FILE_NAME];
89 FILE *fp = nullptr;
90 char temp[LENGTH_OF_FPS_FILE_NAME];
91
92 int64_t diff = (tv2.tv_sec - tv1.tv_sec)*1000000 + tv2.tv_usec - tv1.tv_usec;
93 float fps = frameCountFps / (diff / 1000000.0);
94 DDI_NORMALMESSAGE("FPS:%6.4f, Interval:%11lu.", fps,((uint64_t)tv2.tv_sec)*1000 + (tv2.tv_usec/1000));
95 sprintf(temp,"FPS:%6.4f, Interval:%" PRIu64"\n", fps,((uint64_t)tv2.tv_sec)*1000 + (tv2.tv_usec/1000));
96
97 MOS_ZeroMemory(fpsFileName,LENGTH_OF_FPS_FILE_NAME);
98 sprintf(fpsFileName, FPS_FILE_NAME);
99 if ((fp = fopen(fpsFileName, "wb")) == nullptr)
100 {
101 pthread_mutex_unlock(&fpsMutex);
102 DDI_ASSERTMESSAGE("Unable to open fps file.");
103 }
104
105 fwrite(temp, 1, strlen(temp), fp);
106 fclose(fp);
107 frameCountFps = -1;
108 }
109 pthread_mutex_unlock(&fpsMutex);
110 }
111 #else
112 void DdiMediaUtil_MediaPrintFps()
113 {
114 return;
115 }
116 #endif
117
118 /*
119 * DdiMediaUtil_IsExternalSurface
120 * Descripion: if the bo of media surface was allocated from App,
121 * should return true, otherwise, false. In current implemeation
122 * external buffer passed with pSurfDesc.
123 */
124 bool DdiMediaUtil_IsExternalSurface(PDDI_MEDIA_SURFACE surface)
125 {
126 if ( nullptr == surface )
127 {
128 return false;
129 }
130 else if ( surface->pSurfDesc == nullptr )
131 {
132 return false;
133 }
134 else
135 {
136 if (surface->pSurfDesc->uiVaMemType == VA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM ||
137 surface->pSurfDesc->uiVaMemType == VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME ||
138 surface->pSurfDesc->uiVaMemType == VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2 ||
139 #if VA_CHECK_VERSION(1, 21, 0)
140 surface->pSurfDesc->uiVaMemType == VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_3 ||
141 #endif
142 surface->pSurfDesc->uiVaMemType == VA_SURFACE_ATTRIB_MEM_TYPE_USER_PTR)
143 {
144 return true;
145 }else
146 {
147 return false;
148 }
149 }
150 }
151
152 //!
153 //! \brief Allocate surface
154 //!
155 //! \param [in] format
156 //! Ddi media format
157 //! \param [in] width
158 //! Width of the region
159 //! \param [in] height
160 //! Height of the region
161 //! \param [out] mediaSurface
162 //! Pointer to ddi media surface
163 //! \param [in] mediaDrvCtx
164 //! Pointer to ddi media context
165 //!
166 //! \return VAStatus
167 //! VA_STATUS_SUCCESS if success, else fail reason
168 //!
169 VAStatus DdiMediaUtil_AllocateSurface(
170 DDI_MEDIA_FORMAT format,
171 int32_t width,
172 int32_t height,
173 PDDI_MEDIA_SURFACE mediaSurface,
174 PDDI_MEDIA_CONTEXT mediaDrvCtx)
175 {
176 uint32_t pitch = 0;
177 MOS_LINUX_BO *bo = nullptr;
178 GMM_RESCREATE_PARAMS gmmParams;
179 GMM_RESCREATE_CUSTOM_PARAMS_2 gmmCustomParams;
180 GMM_RESOURCE_INFO *gmmResourceInfo = nullptr;
181
182 DDI_CHK_NULL(mediaSurface, "mediaSurface is nullptr", VA_STATUS_ERROR_INVALID_BUFFER);
183 DDI_CHK_NULL(mediaDrvCtx, "mediaDrvCtx is nullptr", VA_STATUS_ERROR_INVALID_BUFFER);
184 DDI_CHK_NULL(mediaDrvCtx->pGmmClientContext, "mediaDrvCtx->pGmmClientContext is nullptr", VA_STATUS_ERROR_INVALID_BUFFER);
185
186 int32_t size = 0;
187 uint32_t tileformat = TILING_NONE;
188 VAStatus hRes = VA_STATUS_SUCCESS;
189 int32_t alignedWidth = width;
190 int32_t alignedHeight = height;
191 uint32_t cpTag = 0;
192 int mem_type = mediaSurface->memType;
193
194 bool bMemCompEnable = true;
195
196 bool bMemCompRC = false;
197
198 switch (format)
199 {
200 case Media_Format_X8R8G8B8:
201 case Media_Format_X8B8G8R8:
202 case Media_Format_A8B8G8R8:
203 case Media_Format_R8G8B8A8:
204 case Media_Format_R5G6B5:
205 case Media_Format_R8G8B8:
206 case Media_Format_R10G10B10A2:
207 case Media_Format_B10G10R10A2:
208 case Media_Format_R10G10B10X2:
209 case Media_Format_B10G10R10X2:
210 case Media_Format_A16R16G16B16:
211 case Media_Format_A16B16G16R16:
212 if (VA_SURFACE_ATTRIB_USAGE_HINT_ENCODER != mediaSurface->surfaceUsageHint &&
213 !(mediaSurface->surfaceUsageHint & VA_SURFACE_ATTRIB_USAGE_HINT_VPP_WRITE))
214 {
215 tileformat = TILING_NONE;
216 break;
217 }
218 case Media_Format_YV12:
219 case Media_Format_I420:
220 case Media_Format_IYUV:
221 if (VA_SURFACE_ATTRIB_USAGE_HINT_ENCODER != mediaSurface->surfaceUsageHint &&
222 !(mediaSurface->surfaceUsageHint & VA_SURFACE_ATTRIB_USAGE_HINT_VPP_WRITE))
223 {
224 tileformat = TILING_NONE;
225 alignedWidth = MOS_ALIGN_CEIL(width, 2);
226 alignedHeight = MOS_ALIGN_CEIL(height, 2);
227 break;
228 }
229
230 case Media_Format_RGBP:
231 case Media_Format_BGRP:
232 if (VA_SURFACE_ATTRIB_USAGE_HINT_ENCODER != mediaSurface->surfaceUsageHint &&
233 !(mediaSurface->surfaceUsageHint & VA_SURFACE_ATTRIB_USAGE_HINT_DECODER) &&
234 !(mediaSurface->surfaceUsageHint & VA_SURFACE_ATTRIB_USAGE_HINT_VPP_WRITE))
235 {
236 tileformat = TILING_NONE;
237 break;
238 }
239 case Media_Format_A8R8G8B8:
240 if (VA_SURFACE_ATTRIB_USAGE_HINT_ENCODER != mediaSurface->surfaceUsageHint &&
241 !(mediaSurface->surfaceUsageHint & VA_SURFACE_ATTRIB_USAGE_HINT_DECODER) &&
242 !(mediaSurface->surfaceUsageHint & VA_SURFACE_ATTRIB_USAGE_HINT_VPP_WRITE) &&
243 !(MEDIA_IS_SKU(&mediaDrvCtx->SkuTable, FtrRenderCompressionOnly) &&
244 MEDIA_IS_SKU(&mediaDrvCtx->SkuTable, FtrE2ECompression)))
245 {
246 tileformat = TILING_NONE;
247 break;
248 }
249 case Media_Format_NV12:
250 case Media_Format_NV21:
251 case Media_Format_444P:
252 case Media_Format_422H:
253 case Media_Format_411P:
254 case Media_Format_422V:
255 case Media_Format_IMC3:
256 case Media_Format_400P:
257 case Media_Format_P010:
258 case Media_Format_P012:
259 case Media_Format_P016:
260 case Media_Format_YUY2:
261 case Media_Format_Y210:
262 #if VA_CHECK_VERSION(1, 9, 0)
263 case Media_Format_Y212:
264 #endif
265 case Media_Format_Y216:
266 case Media_Format_AYUV:
267 #if VA_CHECK_VERSION(1, 13, 0)
268 case Media_Format_XYUV:
269 #endif
270 case Media_Format_Y410:
271 #if VA_CHECK_VERSION(1, 9, 0)
272 case Media_Format_Y412:
273 #endif
274 case Media_Format_Y416:
275 case Media_Format_Y8:
276 case Media_Format_Y16S:
277 case Media_Format_Y16U:
278 case Media_Format_VYUY:
279 case Media_Format_YVYU:
280 case Media_Format_UYVY:
281 if (VA_SURFACE_ATTRIB_USAGE_HINT_ENCODER != mediaSurface->surfaceUsageHint &&
282 !(mediaSurface->surfaceUsageHint & VA_SURFACE_ATTRIB_USAGE_HINT_VPP_WRITE))
283 {
284 #if UFO_GRALLOC_NEW_FORMAT
285 //Planar type surface align 64 to improve performance.
286 alignedHeight = MOS_ALIGN_CEIL(height, 64);
287 #else
288 //Planar type surface align 32 to improve performance.
289 alignedHeight = MOS_ALIGN_CEIL(height, 32);
290 #endif
291 }
292 alignedWidth = MOS_ALIGN_CEIL(width, 8);
293 if (mediaSurface->surfaceUsageHint & VA_SURFACE_ATTRIB_USAGE_HINT_VPP_WRITE)
294 {
295 if ((format == Media_Format_YV12) ||
296 (format == Media_Format_I420))
297 {
298 alignedWidth = MOS_ALIGN_CEIL(width, 128);
299 }
300
301 if ((format == Media_Format_NV12) ||
302 (format == Media_Format_P010) ||
303 (format == Media_Format_RGBP) ||
304 (format == Media_Format_BGRP))
305 {
306 #if UFO_GRALLOC_NEW_FORMAT
307 //Planar type surface align 64 to improve performance.
308 alignedHeight = MOS_ALIGN_CEIL(height, 64);
309 #else
310 //Planar type surface align 32 to improve performance.
311 alignedHeight = MOS_ALIGN_CEIL(height, 32);
312 #endif
313 }
314 }
315 tileformat = TILING_Y;
316 break;
317 case Media_Format_Buffer:
318 tileformat = TILING_NONE;
319 break;
320 default:
321 DDI_ASSERTMESSAGE("Unsupported format");
322 hRes = VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
323 goto finish;
324 }
325 //different alignment requirement for different codec and different platform
326 //for MPEG2 and AVC , it should be 16
327 //for HEVC on the platform pre-gen12 (include), it is 64
328 //may need api change for to convey different codec usage.
329 if(VA_SURFACE_ATTRIB_USAGE_HINT_ENCODER & mediaSurface->surfaceUsageHint)
330 {
331 alignedWidth = MOS_ALIGN_CEIL(alignedWidth, 16);
332 alignedHeight = MOS_ALIGN_CEIL(alignedHeight, 16);
333 }
334
335 if (DdiMediaUtil_IsExternalSurface(mediaSurface))
336 {
337 // Default set as compression not supported, surface compression import only support from Memory Type VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2 or VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_3
338 bMemCompEnable = false;
339 bMemCompRC = false;
340 pitch = mediaSurface->pSurfDesc->uiPitches[0];
341 DDI_CHK_CONDITION(pitch == 0, "Invalid pich.", VA_STATUS_ERROR_INVALID_PARAMETER);
342 // DRM buffer allocated by Application, No need to re-allocate new DRM buffer
343 if ((mediaSurface->pSurfDesc->uiVaMemType == VA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM) ||
344 (mediaSurface->pSurfDesc->uiVaMemType == VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME))
345 {
346 if (mediaSurface->pSurfDesc->uiVaMemType == VA_SURFACE_ATTRIB_MEM_TYPE_KERNEL_DRM)
347 {
348 bo = mos_bo_create_from_name(mediaDrvCtx->pDrmBufMgr, "MEDIA", mediaSurface->pSurfDesc->ulBuffer);
349 }
350 else
351 {
352 struct mos_drm_bo_alloc_prime alloc_prime;
353 alloc_prime.name = "prime";
354 alloc_prime.prime_fd = mediaSurface->pSurfDesc->ulBuffer;
355 alloc_prime.size = mediaSurface->pSurfDesc->uiSize;
356 alloc_prime.pat_index = PAT_INDEX_INVALID; //Setting with default pat index 0 in legacy ddi
357 bo = mos_bo_create_from_prime(mediaDrvCtx->pDrmBufMgr, &alloc_prime);
358 }
359
360 if (bo != nullptr)
361 {
362 uint32_t swizzle_mode;
363 //Overwrite the tile format that matches the exteral buffer
364 mos_bo_get_tiling(bo, &tileformat, &swizzle_mode);
365 if(tileformat == 0)
366 {
367 tileformat = mediaSurface->pSurfDesc->uiFlags & VA_SURFACE_EXTBUF_DESC_ENABLE_TILING? TILING_Y:TILING_NONE;
368 }
369 }
370 else
371 {
372 DDI_ASSERTMESSAGE("Failed to create drm buffer object according to input buffer descriptor.");
373 return VA_STATUS_ERROR_ALLOCATION_FAILED;
374 }
375 }
376 else if (
377 #if VA_CHECK_VERSION(1, 21, 0)
378 mediaSurface->pSurfDesc->uiVaMemType == VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_3 ||
379 #endif
380 mediaSurface->pSurfDesc->uiVaMemType == VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2)
381 {
382 struct mos_drm_bo_alloc_prime alloc_prime;
383 alloc_prime.name = "prime";
384 alloc_prime.prime_fd = mediaSurface->pSurfDesc->ulBuffer;
385 alloc_prime.size = mediaSurface->pSurfDesc->uiSize;
386 alloc_prime.pat_index = PAT_INDEX_INVALID; //Setting with default pat index 0 in legacy ddi
387 bo = mos_bo_create_from_prime(mediaDrvCtx->pDrmBufMgr, &alloc_prime);
388 if( bo != nullptr )
389 {
390 pitch = mediaSurface->pSurfDesc->uiPitches[0];
391
392 DDI_CHK_NULL(mediaDrvCtx->m_caps, "nullptr m_caps", VA_STATUS_ERROR_INVALID_CONTEXT);
393 if (VA_STATUS_SUCCESS != mediaDrvCtx->m_caps->SetExternalSurfaceTileFormat(mediaSurface, tileformat, bMemCompEnable, bMemCompRC)) {
394 DDI_ASSERTMESSAGE("Unsupported modifier.");
395 hRes = VA_STATUS_ERROR_INVALID_PARAMETER;
396 goto finish;
397 }
398 }
399 else
400 {
401 DDI_ASSERTMESSAGE("Failed to create drm buffer object according to input buffer descriptor.");
402 return VA_STATUS_ERROR_ALLOCATION_FAILED;
403 }
404 }
405 else if( mediaSurface->pSurfDesc->uiVaMemType == VA_SURFACE_ATTRIB_MEM_TYPE_USER_PTR )
406 {
407 struct mos_drm_bo_alloc_userptr alloc_uptr;
408 alloc_uptr.name = "SysSurface";
409 alloc_uptr.addr = (void *)mediaSurface->pSurfDesc->ulBuffer;
410 alloc_uptr.tiling_mode = mediaSurface->pSurfDesc->uiTile;
411 alloc_uptr.stride = pitch;
412 alloc_uptr.size = mediaSurface->pSurfDesc->uiBuffserSize;
413
414 bo = mos_bo_alloc_userptr( mediaDrvCtx->pDrmBufMgr, &alloc_uptr);
415
416 if (bo != nullptr)
417 {
418 uint32_t swizzle_mode;
419 //Overwrite the tile format that matches the exteral buffer
420 mos_bo_get_tiling(bo, &tileformat, &swizzle_mode);
421 }
422 else
423 {
424 DDI_ASSERTMESSAGE("Failed to create drm buffer vmap.");
425 return VA_STATUS_ERROR_ALLOCATION_FAILED;
426 }
427 }
428 else
429 {
430 DDI_ASSERTMESSAGE("Unsupported external surface memory type.");
431 return VA_STATUS_ERROR_ALLOCATION_FAILED;
432 }
433
434 // Set cp flag to indicate the secure surface
435 if (mediaSurface->pSurfDesc->uiFlags & VA_SURFACE_EXTBUF_DESC_PROTECTED)
436 {
437 cpTag = PROTECTED_SURFACE_TAG;
438 }
439
440 int32_t baseHeight = 0;
441 DDI_CHK_CONDITION(mediaSurface->pSurfDesc->uiPlanes == 0,
442 "Invalid plane number.",
443 VA_STATUS_ERROR_INVALID_PARAMETER);
444 if (mediaSurface->pSurfDesc->uiPlanes == 1)
445 {
446 DDI_CHK_CONDITION(mediaSurface->pSurfDesc->uiSize == 0,
447 "Invalid Size.",
448 VA_STATUS_ERROR_INVALID_PARAMETER);
449 baseHeight = mediaSurface->pSurfDesc->uiSize / pitch;
450 }
451 else
452 {
453 DDI_CHK_CONDITION(mediaSurface->pSurfDesc->uiOffsets[1] == 0,
454 "Invalid offset.",
455 VA_STATUS_ERROR_INVALID_PARAMETER);
456 baseHeight = mediaSurface->pSurfDesc->uiOffsets[1] / pitch;
457 }
458 // Create GmmResourceInfo
459 MOS_ZeroMemory(&gmmCustomParams, sizeof(gmmCustomParams));
460 gmmCustomParams.Type = RESOURCE_2D;
461 gmmCustomParams.Format = mediaDrvCtx->m_caps->ConvertMediaFmtToGmmFmt(format);
462 if ((format == Media_Format_YV12) || \
463 (format == Media_Format_I420) || \
464 (format == Media_Format_IYUV) || \
465 (format == Media_Format_NV12) || \
466 (format == Media_Format_NV21)) {
467 // Align width to 2 for specific planar formats to handle
468 // odd dimensions for external non-compressible surfaces
469 gmmCustomParams.BaseWidth64 = MOS_ALIGN_CEIL(width, 2);
470 } else {
471 gmmCustomParams.BaseWidth64 = width;
472 }
473 gmmCustomParams.BaseHeight = baseHeight;
474 gmmCustomParams.Pitch = pitch;
475 gmmCustomParams.Size = mediaSurface->pSurfDesc->uiSize;
476 gmmCustomParams.BaseAlignment = 4096;
477 gmmCustomParams.NoOfPlanes = mediaSurface->pSurfDesc->uiPlanes;
478 gmmCustomParams.CpTag = cpTag;
479 switch (tileformat)
480 {
481 case TILING_Y:
482 gmmCustomParams.Flags.Info.TiledY = true;
483 gmmCustomParams.Flags.Gpu.MMC = false;
484 if (MEDIA_IS_SKU(&mediaDrvCtx->SkuTable, FtrE2ECompression) &&
485 (!MEDIA_IS_WA(&mediaDrvCtx->WaTable, WaDisableVPMmc) &&
486 !MEDIA_IS_WA(&mediaDrvCtx->WaTable, WaDisableCodecMmc)) &&
487 bMemCompEnable)
488 {
489 gmmCustomParams.Flags.Gpu.MMC = true;
490 gmmCustomParams.Flags.Info.MediaCompressed = 1;
491 gmmCustomParams.Flags.Info.RenderCompressed = 0;
492 gmmCustomParams.Flags.Gpu.CCS = 1;
493 gmmCustomParams.Flags.Gpu.RenderTarget = 1;
494 gmmCustomParams.Flags.Gpu.UnifiedAuxSurface = 1;
495
496 if (bMemCompRC)
497 {
498 gmmCustomParams.Flags.Info.MediaCompressed = 0;
499 gmmCustomParams.Flags.Info.RenderCompressed = 1;
500 }
501
502 if(MEDIA_IS_SKU(&mediaDrvCtx->SkuTable, FtrRenderCompressionOnly))
503 {
504 gmmCustomParams.Flags.Info.MediaCompressed = 0;
505
506 if (format == Media_Format_X8R8G8B8 ||
507 format == Media_Format_X8B8G8R8 ||
508 format == Media_Format_A8B8G8R8 ||
509 format == Media_Format_A8R8G8B8 ||
510 format == Media_Format_R8G8B8A8)
511 {
512 gmmCustomParams.Flags.Info.MediaCompressed = 0;
513 gmmCustomParams.Flags.Info.RenderCompressed = 1;
514 }
515 }
516
517 if(MEDIA_IS_SKU(&mediaDrvCtx->SkuTable, FtrFlatPhysCCS))
518 {
519 gmmCustomParams.Flags.Gpu.UnifiedAuxSurface = 0;
520 }
521 }
522 break;
523 case TILING_X:
524 gmmCustomParams.Flags.Info.TiledX = true;
525 break;
526 case TILING_NONE:
527 default:
528 gmmCustomParams.Flags.Info.Linear = true;
529 }
530
531 if(bMemCompEnable)
532 {
533 gmmCustomParams.AuxSurf.BaseAlignment = {0};
534 gmmCustomParams.NoOfPlanes = mediaSurface->pSurfDesc->uiPlanes/2;
535 gmmCustomParams.Size = (gmmCustomParams.NoOfPlanes == 1) ? mediaSurface->pSurfDesc->uiOffsets[1]:mediaSurface->pSurfDesc->uiOffsets[2];
536 }
537 switch(gmmCustomParams.NoOfPlanes)
538 {
539 case 1:
540 gmmCustomParams.PlaneOffset.X[GMM_PLANE_Y] = 0;
541 gmmCustomParams.PlaneOffset.Y[GMM_PLANE_Y] = mediaSurface->pSurfDesc->uiOffsets[0] / pitch;
542 if (bMemCompEnable)
543 {
544 gmmCustomParams.AuxSurf.Size = mediaSurface->pSurfDesc->uiSize - gmmCustomParams.Size;
545 gmmCustomParams.AuxSurf.Pitch = mediaSurface->pSurfDesc->uiPitches[1];
546 gmmCustomParams.AuxSurf.PlaneOffset.X[GMM_PLANE_Y] = 0;
547 gmmCustomParams.AuxSurf.PlaneOffset.Y[GMM_PLANE_Y] = 0;
548 }
549 break;
550 case 2:
551 gmmCustomParams.PlaneOffset.X[GMM_PLANE_Y] = 0;
552 gmmCustomParams.PlaneOffset.Y[GMM_PLANE_Y] = mediaSurface->pSurfDesc->uiOffsets[0] / pitch;
553 gmmCustomParams.PlaneOffset.X[GMM_PLANE_U] = 0;
554 gmmCustomParams.PlaneOffset.Y[GMM_PLANE_U] = mediaSurface->pSurfDesc->uiOffsets[1] / pitch;
555 gmmCustomParams.PlaneOffset.X[GMM_PLANE_V] = 0;
556 gmmCustomParams.PlaneOffset.Y[GMM_PLANE_V] = mediaSurface->pSurfDesc->uiOffsets[1] / pitch;
557 if (bMemCompEnable)
558 {
559 gmmCustomParams.AuxSurf.Size = (mediaSurface->pSurfDesc->uiOffsets[3] - mediaSurface->pSurfDesc->uiOffsets[2]) * 2;
560 gmmCustomParams.AuxSurf.Pitch = mediaSurface->pSurfDesc->uiPitches[2];
561 gmmCustomParams.AuxSurf.PlaneOffset.X[GMM_PLANE_Y] = 0;
562 gmmCustomParams.AuxSurf.PlaneOffset.Y[GMM_PLANE_Y] = 0;
563 gmmCustomParams.AuxSurf.PlaneOffset.X[GMM_PLANE_U] = (mediaSurface->pSurfDesc->uiOffsets[3]
564 - mediaSurface->pSurfDesc->uiOffsets[2]);
565 gmmCustomParams.AuxSurf.PlaneOffset.Y[GMM_PLANE_U] = 0;
566 gmmCustomParams.AuxSurf.PlaneOffset.X[GMM_PLANE_V] = (mediaSurface->pSurfDesc->uiOffsets[3]
567 - mediaSurface->pSurfDesc->uiOffsets[2]);
568 gmmCustomParams.AuxSurf.PlaneOffset.Y[GMM_PLANE_V] = 0;
569 }
570 break;
571 case 3:
572 if (mediaSurface->format == Media_Format_YV12)
573 {
574 gmmCustomParams.PlaneOffset.X[GMM_PLANE_Y] = 0;
575 gmmCustomParams.PlaneOffset.Y[GMM_PLANE_Y] = mediaSurface->pSurfDesc->uiOffsets[0] / pitch;
576 gmmCustomParams.PlaneOffset.X[GMM_PLANE_U] = 0;
577 gmmCustomParams.PlaneOffset.Y[GMM_PLANE_U] = mediaSurface->pSurfDesc->uiOffsets[2] / pitch;
578 gmmCustomParams.PlaneOffset.X[GMM_PLANE_V] = 0;
579 gmmCustomParams.PlaneOffset.Y[GMM_PLANE_V] = mediaSurface->pSurfDesc->uiOffsets[1] / pitch;
580 }
581 else
582 {
583 gmmCustomParams.PlaneOffset.X[GMM_PLANE_Y] = 0;
584 gmmCustomParams.PlaneOffset.Y[GMM_PLANE_Y] = mediaSurface->pSurfDesc->uiOffsets[0] / pitch;
585 gmmCustomParams.PlaneOffset.X[GMM_PLANE_U] = 0;
586 gmmCustomParams.PlaneOffset.Y[GMM_PLANE_U] = mediaSurface->pSurfDesc->uiOffsets[1] / pitch;
587 gmmCustomParams.PlaneOffset.X[GMM_PLANE_V] = 0;
588 gmmCustomParams.PlaneOffset.Y[GMM_PLANE_V] = mediaSurface->pSurfDesc->uiOffsets[2] / pitch;
589 }
590 break;
591 default:
592 DDI_ASSERTMESSAGE("Invalid plane number.");
593 return VA_STATUS_ERROR_ALLOCATION_FAILED;
594 }
595 gmmResourceInfo = mediaDrvCtx->pGmmClientContext->CreateCustomResInfoObject_2(&gmmCustomParams);
596
597 if(nullptr == gmmResourceInfo)
598 {
599 DDI_ASSERTMESSAGE("Gmm Create Resource Failed.");
600 hRes = VA_STATUS_ERROR_ALLOCATION_FAILED;
601 goto finish;
602 }
603 int gmmTiledType = gmmResourceInfo->GetTileType();
604 if (bo)
605 {
606 mediaSurface->pGmmResourceInfo = gmmResourceInfo;
607 mediaSurface->bMapped = false;
608 mediaSurface->format = format;
609 mediaSurface->iWidth = width;
610 mediaSurface->iHeight = gmmResourceInfo->GetBaseHeight();
611 mediaSurface->iRealHeight = height;
612 mediaSurface->iPitch = pitch;
613 mediaSurface->iRefCount = 0;
614 mediaSurface->bo = bo;
615 mediaSurface->TileType = tileformat;
616 mediaSurface->isTiled = (tileformat != TILING_NONE) ? 1 : 0;
617 mediaSurface->pData = (uint8_t*) bo->virt;
618 DDI_VERBOSEMESSAGE("Allocate external surface %7d bytes (%d x %d resource). gmmTiledType %d", mediaSurface->pSurfDesc->uiSize, width, height, gmmTiledType);
619 uint32_t event[] = {bo->handle, format, width, height, pitch, bo->size, tileformat, cpTag};
620 MOS_TraceEventExt(EVENT_VA_SURFACE, EVENT_TYPE_INFO, event, sizeof(event), &gmmResourceInfo->GetResFlags(), sizeof(GMM_RESOURCE_FLAG));
621 }
622 else
623 {
624 DDI_ASSERTMESSAGE("Fail to allocate external surface");
625 return VA_STATUS_ERROR_ALLOCATION_FAILED;
626 }
627 }
628 else
629 {
630 if (mediaSurface->pSurfDesc)
631 {
632 if( mediaSurface->pSurfDesc->uiFlags & VA_SURFACE_EXTBUF_DESC_ENABLE_TILING )
633 {
634 tileformat = TILING_Y;
635 }
636 else if (mediaSurface->pSurfDesc->uiVaMemType == VA_SURFACE_ATTRIB_MEM_TYPE_VA)
637 {
638 tileformat = TILING_NONE;
639 alignedHeight = height;
640 if (format == Media_Format_YV12 ||
641 format == Media_Format_I420)
642 {
643 alignedHeight = MOS_ALIGN_CEIL(height, 2);
644 }
645 }
646 }
647
648 // Create GmmResourceInfo
649 MOS_ZeroMemory(&gmmParams, sizeof(gmmParams));
650 gmmParams.BaseWidth = alignedWidth;
651 gmmParams.BaseHeight = alignedHeight;
652 gmmParams.ArraySize = 1;
653 gmmParams.Type = RESOURCE_2D;
654 gmmParams.Format = mediaDrvCtx->m_caps->ConvertMediaFmtToGmmFmt(format);
655 DDI_CHK_CONDITION(gmmParams.Format == GMM_FORMAT_INVALID, "Unsupported format", VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT);
656 switch (tileformat)
657 {
658 case TILING_Y:
659 // Disable MMC for application required surfaces, because some cases' output streams have corruption.
660 gmmParams.Flags.Gpu.MMC = false;
661 if (MEDIA_IS_SKU(&mediaDrvCtx->SkuTable, FtrE2ECompression) &&
662 (!MEDIA_IS_WA(&mediaDrvCtx->WaTable, WaDisableVPMmc) &&
663 !MEDIA_IS_WA(&mediaDrvCtx->WaTable, WaDisableCodecMmc)) &&
664 MEDIA_IS_SKU(&mediaDrvCtx->SkuTable, FtrCompressibleSurfaceDefault) &&
665 bMemCompEnable)
666 {
667 gmmParams.Flags.Gpu.MMC = true;
668 gmmParams.Flags.Info.MediaCompressed = 1;
669 gmmParams.Flags.Info.RenderCompressed = 0;
670 gmmParams.Flags.Gpu.CCS = 1;
671 gmmParams.Flags.Gpu.RenderTarget = 1;
672 gmmParams.Flags.Gpu.UnifiedAuxSurface = 1;
673
674 if (bMemCompRC)
675 {
676 gmmParams.Flags.Info.MediaCompressed = 0;
677 gmmParams.Flags.Info.RenderCompressed = 1;
678 }
679
680 if(MEDIA_IS_SKU(&mediaDrvCtx->SkuTable, FtrRenderCompressionOnly))
681 {
682 gmmParams.Flags.Info.MediaCompressed = 0;
683
684 if (format == Media_Format_X8R8G8B8 ||
685 format == Media_Format_X8B8G8R8 ||
686 format == Media_Format_A8B8G8R8 ||
687 format == Media_Format_A8R8G8B8 ||
688 format == Media_Format_R8G8B8A8)
689 {
690 gmmParams.Flags.Info.MediaCompressed = 0;
691 gmmParams.Flags.Info.RenderCompressed = 1;
692 }
693 else
694 {
695 gmmParams.Flags.Gpu.MMC = false;
696 gmmParams.Flags.Info.MediaCompressed = 0;
697 gmmParams.Flags.Info.RenderCompressed = 0;
698 gmmParams.Flags.Gpu.CCS = 0;
699 gmmParams.Flags.Gpu.UnifiedAuxSurface = 0;
700 }
701 }
702 }
703 break;
704 case TILING_X:
705 gmmParams.Flags.Info.TiledX = true;
706 break;
707 default:
708 gmmParams.Flags.Info.Linear = true;
709 }
710 gmmParams.Flags.Gpu.Video = true;
711 gmmParams.Flags.Info.LocalOnly = MEDIA_IS_SKU(&mediaDrvCtx->SkuTable, FtrLocalMemory);
712
713 mediaSurface->pGmmResourceInfo = gmmResourceInfo = mediaDrvCtx->pGmmClientContext->CreateResInfoObject(&gmmParams);
714
715 if(nullptr == gmmResourceInfo)
716 {
717 DDI_ASSERTMESSAGE("Gmm Create Resource Failed.");
718 hRes = VA_STATUS_ERROR_ALLOCATION_FAILED;
719 goto finish;
720 }
721
722 uint32_t gmmPitch = (uint32_t)gmmResourceInfo->GetRenderPitch();
723 uint32_t gmmSize = (uint32_t)gmmResourceInfo->GetSizeSurface();
724 uint32_t gmmHeight = gmmResourceInfo->GetBaseHeight();
725
726 if ( 0 == gmmPitch || 0 == gmmSize || 0 == gmmHeight)
727 {
728 DDI_ASSERTMESSAGE("Gmm Create Resource Failed.");
729 hRes = VA_STATUS_ERROR_ALLOCATION_FAILED;
730 goto finish;
731 }
732
733 switch (gmmResourceInfo->GetTileType())
734 {
735 case GMM_TILED_Y:
736 tileformat = TILING_Y;
737 break;
738 case GMM_TILED_X:
739 tileformat = TILING_X;
740 break;
741 case GMM_NOT_TILED:
742 tileformat = TILING_NONE;
743 break;
744 default:
745 tileformat = TILING_Y;
746 break;
747 }
748
749 MemoryPolicyParameter memPolicyPar;
750 MOS_ZeroMemory(&memPolicyPar, sizeof(MemoryPolicyParameter));
751
752 memPolicyPar.skuTable = &mediaDrvCtx->SkuTable;
753 memPolicyPar.waTable = &mediaDrvCtx->WaTable;
754 memPolicyPar.resInfo = mediaSurface->pGmmResourceInfo;
755 memPolicyPar.resName = "Media Surface";
756 memPolicyPar.preferredMemType = (MEDIA_IS_WA(&mediaDrvCtx->WaTable, WaForceAllocateLML4)) ? MOS_MEMPOOL_DEVICEMEMORY : mem_type;
757
758 mem_type = MemoryPolicyManager::UpdateMemoryPolicy(&memPolicyPar);
759
760 if ( tileformat == TILING_NONE )
761 {
762 struct mos_drm_bo_alloc alloc;
763 alloc.name = "MEDIA";
764 alloc.size = gmmSize;
765 alloc.alignment = 4096;
766 alloc.ext.mem_type = mem_type;
767
768 bo = mos_bo_alloc(mediaDrvCtx->pDrmBufMgr, &alloc);
769 pitch = gmmPitch;
770 }
771 else
772 {
773 struct mos_drm_bo_alloc_tiled alloc_tiled;
774 alloc_tiled.name = "MEDIA";
775 alloc_tiled.x = gmmPitch;
776 alloc_tiled.y = (gmmSize + gmmPitch -1)/gmmPitch;
777 alloc_tiled.cpp = 1;
778 alloc_tiled.ext.tiling_mode = tileformat;
779 alloc_tiled.ext.mem_type = mem_type;
780
781 bo = mos_bo_alloc_tiled(mediaDrvCtx->pDrmBufMgr, &alloc_tiled);
782 pitch = alloc_tiled.pitch;
783 }
784
785 mediaSurface->bMapped = false;
786 if (bo)
787 {
788 mediaSurface->format = format;
789 mediaSurface->iWidth = width;
790 mediaSurface->iHeight = gmmHeight;
791 mediaSurface->iRealHeight = height;
792 mediaSurface->iPitch = pitch;
793 mediaSurface->iRefCount = 0;
794 mediaSurface->bo = bo;
795 mediaSurface->TileType = tileformat;
796 mediaSurface->isTiled = (tileformat != TILING_NONE) ? 1 : 0;
797 mediaSurface->pData = (uint8_t*) bo->virt;
798 DDI_VERBOSEMESSAGE("Alloc %7d bytes (%d x %d resource, gmmTiledType %d).",gmmSize, width, height, gmmResourceInfo->GetTileType());
799 uint32_t event[] = {bo->handle, format, width, height, pitch, bo->size, tileformat, cpTag};
800 MOS_TraceEventExt(EVENT_VA_SURFACE, EVENT_TYPE_INFO, event, sizeof(event), &gmmResourceInfo->GetResFlags(), sizeof(GMM_RESOURCE_FLAG));
801 }
802 else
803 {
804 DDI_ASSERTMESSAGE("Fail to Alloc %7d bytes (%d x %d resource).",gmmSize, width, height);
805 hRes = VA_STATUS_ERROR_ALLOCATION_FAILED;
806 goto finish;
807 }
808 }
809
810
811 finish:
812 return hRes;
813 }
814
815 //!
816 //! \brief Allocate buffer
817 //!
818 //! \param [in] format
819 //! Ddi media format
820 //! \param [in] size
821 //! Size of the region
822 //! \param [out] mediaBuffer
823 //! Pointer to ddi media buffer
824 //! \param [in] bufmgr
825 //! Mos buffer manager
826 //!
827 //! \return VAStatus
828 //! VA_STATUS_SUCCESS if success, else fail reason
829 //!
830 VAStatus DdiMediaUtil_AllocateBuffer(
831 DDI_MEDIA_FORMAT format,
832 int32_t size,
833 PDDI_MEDIA_BUFFER mediaBuffer,
834 MOS_BUFMGR *bufmgr)
835 {
836
837 DDI_CHK_NULL(mediaBuffer, "mediaBuffer is nullptr", VA_STATUS_ERROR_INVALID_BUFFER);
838 DDI_CHK_NULL(mediaBuffer->pMediaCtx, "mediaBuffer->pMediaCtx is nullptr", VA_STATUS_ERROR_INVALID_BUFFER);
839 DDI_CHK_NULL(mediaBuffer->pMediaCtx->pGmmClientContext, "mediaBuffer->pMediaCtx->pGmmClientContext is nullptr", VA_STATUS_ERROR_INVALID_BUFFER);
840 if(format >= Media_Format_Count)
841 return VA_STATUS_ERROR_INVALID_PARAMETER;
842
843 VAStatus hRes = VA_STATUS_SUCCESS;
844 int32_t mem_type = MOS_MEMPOOL_VIDEOMEMORY;
845
846 // create fake GmmResourceInfo
847 GMM_RESCREATE_PARAMS gmmParams;
848 MOS_ZeroMemory(&gmmParams, sizeof(gmmParams));
849 gmmParams.BaseWidth = 1;
850 gmmParams.BaseHeight = 1;
851 gmmParams.ArraySize = 0;
852 gmmParams.Type = RESOURCE_1D;
853 gmmParams.Format = GMM_FORMAT_GENERIC_8BIT;
854 gmmParams.Flags.Gpu.Video = true;
855 gmmParams.Flags.Info.Linear = true;
856 DDI_CHK_NULL(mediaBuffer->pMediaCtx, "MediaCtx is null", VA_STATUS_ERROR_INVALID_BUFFER);
857 gmmParams.Flags.Info.LocalOnly = MEDIA_IS_SKU(&mediaBuffer->pMediaCtx->SkuTable, FtrLocalMemory);
858
859 mediaBuffer->pGmmResourceInfo = mediaBuffer->pMediaCtx->pGmmClientContext->CreateResInfoObject(&gmmParams);
860
861 DDI_CHK_NULL(mediaBuffer->pGmmResourceInfo, "pGmmResourceInfo is nullptr", VA_STATUS_ERROR_INVALID_BUFFER);
862 mediaBuffer->pGmmResourceInfo->OverrideSize(mediaBuffer->iSize);
863 mediaBuffer->pGmmResourceInfo->OverrideBaseWidth(mediaBuffer->iSize);
864 mediaBuffer->pGmmResourceInfo->OverridePitch(mediaBuffer->iSize);
865
866 MemoryPolicyParameter memPolicyPar;
867 MOS_ZeroMemory(&memPolicyPar, sizeof(MemoryPolicyParameter));
868
869 memPolicyPar.skuTable = &mediaBuffer->pMediaCtx->SkuTable;
870 memPolicyPar.waTable = &mediaBuffer->pMediaCtx->WaTable;
871 memPolicyPar.resInfo = mediaBuffer->pGmmResourceInfo;
872 memPolicyPar.resName = "Media Buffer";
873 memPolicyPar.uiType = mediaBuffer->uiType;
874 memPolicyPar.preferredMemType = mediaBuffer->bUseSysGfxMem ? MOS_MEMPOOL_SYSTEMMEMORY : 0;
875
876 mem_type = MemoryPolicyManager::UpdateMemoryPolicy(&memPolicyPar);
877
878 struct mos_drm_bo_alloc alloc;
879 alloc.name = "Media Buffer";
880 alloc.size = size;
881 alloc.alignment = 4096;
882 alloc.ext.mem_type = mem_type;
883 MOS_LINUX_BO *bo = mos_bo_alloc(bufmgr, &alloc);
884
885 mediaBuffer->bMapped = false;
886 if (bo)
887 {
888 mediaBuffer->format = format;
889 mediaBuffer->iSize = size;
890 mediaBuffer->iRefCount = 0;
891 mediaBuffer->bo = bo;
892 mediaBuffer->pData = (uint8_t*) bo->virt;
893
894 DDI_VERBOSEMESSAGE("Alloc %8d bytes resource.",size);
895 uint32_t event[] = {bo->handle, format, size, 1, size, bo->size, 0, 0};
896 MOS_TraceEventExt(EVENT_VA_BUFFER, EVENT_TYPE_INFO, event, sizeof(event), &mediaBuffer->pGmmResourceInfo->GetResFlags(), sizeof(GMM_RESOURCE_FLAG));
897 }
898 else
899 {
900 DDI_ASSERTMESSAGE("Fail to Alloc %8d bytes resource.",size);
901 hRes = VA_STATUS_ERROR_ALLOCATION_FAILED;
902 goto finish;
903 }
904
905 finish:
906 return hRes;
907 }
908
909 //!
910 //! \brief Allocate 2D buffer
911 //!
912 //! \param [in] height
913 //! Height of the region
914 //! \param [in] width
915 //! Width of the region
916 //! \param [out] mediaBuffer
917 //! Pointer to ddi media buffer
918 //! \param [in] bufmgr
919 //! Mos buffer manager
920 //!
921 //! \return VAStatus
922 //! VA_STATUS_SUCCESS if success, else fail reason
923 //!
924 VAStatus DdiMediaUtil_Allocate2DBuffer(
925 uint32_t height,
926 uint32_t width,
927 PDDI_MEDIA_BUFFER mediaBuffer,
928 MOS_BUFMGR *bufmgr)
929 {
930 DDI_CHK_NULL(mediaBuffer, "mediaBuffer is nullptr", VA_STATUS_ERROR_INVALID_BUFFER);
931 DDI_CHK_NULL(mediaBuffer->pMediaCtx, "mediaBuffer->pMediaCtx is nullptr", VA_STATUS_ERROR_INVALID_BUFFER);
932 DDI_CHK_NULL(mediaBuffer->pMediaCtx->pGmmClientContext, "mediaBuffer->pMediaCtx->pGmmClientContext is nullptr", VA_STATUS_ERROR_INVALID_BUFFER);
933
934 int32_t size = 0;
935 uint32_t tileformat = TILING_NONE;
936 VAStatus hRes = VA_STATUS_SUCCESS;
937 int32_t mem_type = MOS_MEMPOOL_VIDEOMEMORY;
938
939 // Create GmmResourceInfo
940 GMM_RESCREATE_PARAMS gmmParams;
941 MOS_ZeroMemory(&gmmParams, sizeof(gmmParams));
942 gmmParams.BaseWidth = width;
943 gmmParams.BaseHeight = height;
944 gmmParams.ArraySize = 1;
945 gmmParams.Type = RESOURCE_2D;
946 gmmParams.Format = GMM_FORMAT_GENERIC_8BIT;
947
948 DDI_CHK_CONDITION(gmmParams.Format == GMM_FORMAT_INVALID,
949 "Unsupported format",
950 VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT);
951
952 gmmParams.Flags.Info.Linear = true;
953 gmmParams.Flags.Gpu.Video = true;
954 DDI_CHK_NULL(mediaBuffer->pMediaCtx, "MediaCtx is null", VA_STATUS_ERROR_INVALID_BUFFER);
955 gmmParams.Flags.Info.LocalOnly = MEDIA_IS_SKU(&mediaBuffer->pMediaCtx->SkuTable, FtrLocalMemory);
956 GMM_RESOURCE_INFO *gmmResourceInfo;
957 mediaBuffer->pGmmResourceInfo = gmmResourceInfo = mediaBuffer->pMediaCtx->pGmmClientContext->CreateResInfoObject(&gmmParams);
958
959 if(nullptr == gmmResourceInfo)
960 {
961 DDI_VERBOSEMESSAGE("Gmm Create Resource Failed.");
962 hRes = VA_STATUS_ERROR_ALLOCATION_FAILED;
963 return hRes;
964 }
965 uint32_t gmmPitch;
966 uint32_t gmmSize;
967 uint32_t gmmHeight;
968 gmmPitch = (uint32_t)gmmResourceInfo->GetRenderPitch();
969 gmmSize = (uint32_t)gmmResourceInfo->GetSizeSurface();
970 gmmHeight = gmmResourceInfo->GetBaseHeight();
971
972 MemoryPolicyParameter memPolicyPar;
973 MOS_ZeroMemory(&memPolicyPar, sizeof(MemoryPolicyParameter));
974
975 memPolicyPar.skuTable = &mediaBuffer->pMediaCtx->SkuTable;
976 memPolicyPar.waTable = &mediaBuffer->pMediaCtx->WaTable;
977 memPolicyPar.resInfo = mediaBuffer->pGmmResourceInfo;
978 memPolicyPar.resName = "Media 2D Buffer";
979 memPolicyPar.uiType = mediaBuffer->uiType;
980 memPolicyPar.preferredMemType = mediaBuffer->bUseSysGfxMem ? MOS_MEMPOOL_SYSTEMMEMORY : 0;
981
982 mem_type = MemoryPolicyManager::UpdateMemoryPolicy(&memPolicyPar);
983
984 MOS_LINUX_BO *bo;
985 struct mos_drm_bo_alloc alloc;
986 alloc.name = "Media 2D Buffer";
987 alloc.size = gmmSize;
988 alloc.alignment = 4096;
989 alloc.ext.mem_type = mem_type;
990 bo = mos_bo_alloc(bufmgr, &alloc);
991
992 mediaBuffer->bMapped = false;
993 if (bo)
994 {
995 mediaBuffer->format = Media_Format_2DBuffer;
996 mediaBuffer->uiWidth = width;
997 mediaBuffer->uiHeight = gmmHeight;
998 mediaBuffer->uiPitch = gmmPitch;
999 mediaBuffer->iSize = gmmSize;
1000 mediaBuffer->iRefCount = 0;
1001 mediaBuffer->bo = bo;
1002 mediaBuffer->TileType = tileformat;
1003 mediaBuffer->pData = (uint8_t*) bo->virt;
1004 DDI_VERBOSEMESSAGE("Alloc %7d bytes (%d x %d resource)\n",size, width, height);
1005 uint32_t event[] = {bo->handle, mediaBuffer->format, width, height, gmmPitch, bo->size, tileformat, 0};
1006 MOS_TraceEventExt(EVENT_VA_BUFFER, EVENT_TYPE_INFO, event, sizeof(event), &gmmResourceInfo->GetResFlags(), sizeof(GMM_RESOURCE_FLAG));
1007 }
1008 else
1009 {
1010 DDI_VERBOSEMESSAGE("Fail to Alloc %7d bytes (%d x %d resource)\n", size, width, height);
1011 hRes = VA_STATUS_ERROR_ALLOCATION_FAILED;
1012 }
1013
1014 finish:
1015 return hRes;
1016 }
1017
1018 VAStatus DdiMediaUtil_CreateSurface(DDI_MEDIA_SURFACE *surface, PDDI_MEDIA_CONTEXT mediaDrvCtx)
1019 {
1020 VAStatus hr = VA_STATUS_SUCCESS;
1021
1022 DDI_CHK_NULL(surface, "nullptr surface", VA_STATUS_ERROR_INVALID_BUFFER);
1023
1024 // better to differentiate 1D and 2D type
1025 hr = DdiMediaUtil_AllocateSurface(surface->format,
1026 surface->iWidth,
1027 surface->iHeight,
1028 surface,
1029 mediaDrvCtx);
1030 if (VA_STATUS_SUCCESS == hr && nullptr != surface->bo)
1031 surface->base = surface->name;
1032
1033 return hr;
1034 }
1035
1036 VAStatus DdiMediaUtil_CreateBuffer(DDI_MEDIA_BUFFER *buffer, MOS_BUFMGR *bufmgr)
1037 {
1038 VAStatus hr = VA_STATUS_SUCCESS;
1039
1040 DDI_CHK_NULL(buffer, "nullptr buffer", VA_STATUS_ERROR_INVALID_BUFFER);
1041
1042 DDI_CHK_LESS(buffer->format, Media_Format_Count, "Invalid buffer->format", VA_STATUS_ERROR_INVALID_PARAMETER);
1043
1044 if (buffer->format == Media_Format_CPU)
1045 {
1046 buffer->pData= (uint8_t*)MOS_AllocAndZeroMemory(buffer->iSize);
1047 if (nullptr == buffer->pData)
1048 hr = VA_STATUS_ERROR_ALLOCATION_FAILED;
1049 }
1050
1051 else
1052 {
1053 if (Media_Format_2DBuffer == buffer->format)
1054 {
1055 hr = DdiMediaUtil_Allocate2DBuffer(buffer->uiHeight,
1056 buffer->uiWidth,
1057 buffer,
1058 bufmgr);
1059 }
1060 else
1061 {
1062 hr = DdiMediaUtil_AllocateBuffer(buffer->format,
1063 buffer->iSize,
1064 buffer,
1065 bufmgr);
1066 }
1067 }
1068
1069 buffer->uiLockedBufID = VA_INVALID_ID;
1070 buffer->uiLockedImageID = VA_INVALID_ID;
1071 buffer->iRefCount = 0;
1072
1073 return hr;
1074 }
1075
1076 VAStatus SwizzleSurface(PDDI_MEDIA_CONTEXT mediaCtx, PGMM_RESOURCE_INFO pGmmResInfo, void *pLockedAddr, uint32_t TileType, uint8_t* pResourceBase, bool bUpload);
1077
1078 static VAStatus CreateShadowResource(DDI_MEDIA_SURFACE *surface)
1079 {
1080 VAStatus vaStatus = VA_STATUS_SUCCESS;
1081 DDI_CHK_NULL(surface, "nullptr surface", VA_STATUS_ERROR_INVALID_SURFACE);
1082
1083 if (surface->pGmmResourceInfo->GetSetCpSurfTag(0, 0) != 0)
1084 {
1085 return VA_STATUS_ERROR_INVALID_SURFACE;
1086 }
1087
1088 // 422V fallback to SW swizzle
1089 if (surface->iWidth < 64 || surface->iRealHeight < 64 || (surface->iPitch % 64 != 0) || surface->format == Media_Format_P016 || surface->format == Media_Format_422V)
1090 {
1091 return VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT;
1092 }
1093
1094 surface->pShadowBuffer = (DDI_MEDIA_BUFFER *)MOS_AllocAndZeroMemory(sizeof(DDI_MEDIA_BUFFER));
1095 DDI_CHK_NULL(surface->pShadowBuffer, "Failed to allocate shadow buffer", VA_STATUS_ERROR_INVALID_BUFFER);
1096 surface->pShadowBuffer->pMediaCtx = surface->pMediaCtx;
1097 surface->pShadowBuffer->bUseSysGfxMem = true;
1098 surface->pShadowBuffer->iSize = surface->pGmmResourceInfo->GetSizeSurface();
1099
1100 vaStatus = DdiMediaUtil_AllocateBuffer(Media_Format_Buffer,
1101 surface->pShadowBuffer->iSize,
1102 surface->pShadowBuffer,
1103 surface->pMediaCtx->pDrmBufMgr);
1104
1105 if (vaStatus != VA_STATUS_SUCCESS)
1106 {
1107 MOS_FreeMemory(surface->pShadowBuffer);
1108 surface->pShadowBuffer = nullptr;
1109 }
1110
1111 return vaStatus;
1112 }
1113
1114 //!
1115 //! \brief Swizzle surface by Hardware, current only support VEBOX
1116 //!
1117 //! \param [in] surface
1118 //! Pointer of surface
1119 //! \param [in] isDeSwizzle
1120 //! Whether it's de-swizzling or not
1121 //! Swizzling - copying from video memory to temporary buffer
1122 //! De-swizzling - copying from temporary buffer to video memory
1123 //!
1124 //! \return VAStatus
1125 //! VA_STATUS_SUCCESS if success, else fail reason
1126 //!
1127 static VAStatus SwizzleSurfaceByHW(DDI_MEDIA_SURFACE *surface, bool isDeSwizzle = false)
1128 {
1129 DDI_CHK_NULL(surface, "nullptr surface", VA_STATUS_ERROR_INVALID_SURFACE);
1130 DDI_CHK_NULL(surface->pMediaCtx, "nullptr media context", VA_STATUS_ERROR_INVALID_CONTEXT);
1131
1132 MOS_CONTEXT mosCtx = { };
1133 PERF_DATA perfData = { };
1134 PDDI_MEDIA_CONTEXT mediaDrvCtx = surface->pMediaCtx;
1135 VAStatus vaStatus = VA_STATUS_SUCCESS;
1136
1137 // Get the buf manager for codechal create
1138 mosCtx.bufmgr = mediaDrvCtx->pDrmBufMgr;
1139 mosCtx.m_gpuContextMgr = mediaDrvCtx->m_gpuContextMgr;
1140 mosCtx.m_cmdBufMgr = mediaDrvCtx->m_cmdBufMgr;
1141 mosCtx.fd = mediaDrvCtx->fd;
1142 mosCtx.iDeviceId = mediaDrvCtx->iDeviceId;
1143 mosCtx.m_skuTable = mediaDrvCtx->SkuTable;
1144 mosCtx.m_waTable = mediaDrvCtx->WaTable;
1145 mosCtx.m_gtSystemInfo = *mediaDrvCtx->pGtSystemInfo;
1146 mosCtx.m_platform = mediaDrvCtx->platform;
1147
1148 mosCtx.ppMediaMemDecompState = &mediaDrvCtx->pMediaMemDecompState;
1149 mosCtx.pfnMemoryDecompress = mediaDrvCtx->pfnMemoryDecompress;
1150 mosCtx.pfnMediaMemoryCopy = mediaDrvCtx->pfnMediaMemoryCopy;
1151 mosCtx.pfnMediaMemoryCopy2D = mediaDrvCtx->pfnMediaMemoryCopy2D;
1152 mosCtx.pPerfData = &perfData;
1153 mosCtx.m_gtSystemInfo = *mediaDrvCtx->pGtSystemInfo;
1154 mosCtx.m_auxTableMgr = mediaDrvCtx->m_auxTableMgr;
1155 mosCtx.pGmmClientContext = mediaDrvCtx->pGmmClientContext;
1156 mosCtx.m_userSettingPtr = mediaDrvCtx->m_userSettingPtr;
1157
1158 mosCtx.m_osDeviceContext = mediaDrvCtx->m_osDeviceContext;
1159
1160 MOS_RESOURCE source = {};
1161 MOS_RESOURCE target = {};
1162
1163 if (isDeSwizzle)
1164 {
1165 DdiMedia_MediaBufferToMosResource(surface->pShadowBuffer, &source);
1166 DdiMedia_MediaSurfaceToMosResource(surface, &target);
1167 }
1168 else
1169 {
1170 DdiMedia_MediaSurfaceToMosResource(surface, &source);
1171 DdiMedia_MediaBufferToMosResource(surface->pShadowBuffer, &target);
1172 }
1173
1174 DdiMediaUtil_LockMutex(&mediaDrvCtx->MemDecompMutex);
1175 vaStatus = mediaDrvCtx->pfnMediaMemoryTileConvert(
1176 &mosCtx,
1177 &source,
1178 &target,
1179 surface->pGmmResourceInfo->GetBaseWidth(),
1180 surface->pGmmResourceInfo->GetBaseHeight(),
1181 0,
1182 0,
1183 !isDeSwizzle,
1184 false);
1185 DdiMediaUtil_UnLockMutex(&mediaDrvCtx->MemDecompMutex);
1186 return vaStatus;
1187 }
1188
1189 void* DdiMediaUtil_LockSurface(DDI_MEDIA_SURFACE* surface, uint32_t flag)
1190 {
1191 DDI_CHK_NULL(surface, "nullptr surface", nullptr);
1192 DDI_CHK_NULL(surface->pMediaCtx, "nullptr surface->pMediaCtx", nullptr);
1193 if (MEDIA_IS_SKU(&surface->pMediaCtx->SkuTable, FtrLocalMemory))
1194 {
1195 if ((MosUtilities::MosAtomicIncrement(&surface->iRefCount) == 1) && (false == surface->bMapped))
1196 {
1197 return DdiMediaUtil_LockSurfaceInternal(surface, flag);
1198 }
1199 else
1200 {
1201 DDI_VERBOSEMESSAGE("line %d, invalide operation for lockSurface. the surface reference count = %d", __LINE__, surface->iRefCount);
1202 }
1203 }
1204 else
1205 {
1206 if ((surface->iRefCount == 0) && (false == surface->bMapped))
1207 {
1208 DdiMediaUtil_LockSurfaceInternal(surface, flag);
1209 }
1210 else
1211 {
1212 // do nothing here
1213 }
1214 surface->iRefCount++;
1215 }
1216
1217 return surface->pData;
1218
1219 }
1220
1221 // add thread protection for multiple thread?
1222 void* DdiMediaUtil_LockSurfaceInternal(DDI_MEDIA_SURFACE *surface, uint32_t flag)
1223 {
1224 DDI_CHK_NULL(surface, "nullptr surface", nullptr);
1225 DDI_CHK_NULL(surface->bo, "nullptr surface->bo", nullptr);
1226
1227 if (surface->pMediaCtx->bIsAtomSOC)
1228 {
1229 mos_bo_map_gtt(surface->bo);
1230 }
1231 else
1232 {
1233 if (surface->TileType == TILING_NONE)
1234 {
1235 mos_bo_map(surface->bo, flag & MOS_LOCKFLAG_WRITEONLY);
1236 }
1237 else if ((surface->pMediaCtx->m_useSwSwizzling) && !(flag & MOS_LOCKFLAG_NO_SWIZZLE))
1238 {
1239 uint64_t surfSize = surface->pGmmResourceInfo->GetSizeMainSurface();
1240 DDI_CHK_CONDITION((surface->TileType != TILING_Y), "Unsupported tile type", nullptr);
1241 DDI_CHK_CONDITION((surfSize <= 0 || surface->iPitch <= 0), "Invalid surface size or pitch", nullptr);
1242
1243 VAStatus vaStatus = VA_STATUS_SUCCESS;
1244 if (MEDIA_IS_SKU(&surface->pMediaCtx->SkuTable, FtrLocalMemory))
1245 {
1246 if (surface->pShadowBuffer == nullptr)
1247 {
1248 CreateShadowResource(surface);
1249 }
1250
1251 if (surface->pShadowBuffer != nullptr)
1252 {
1253 vaStatus = SwizzleSurfaceByHW(surface);
1254 int err = 0;
1255 if (vaStatus == VA_STATUS_SUCCESS)
1256 {
1257 err = mos_bo_map(surface->pShadowBuffer->bo, flag & MOS_LOCKFLAG_WRITEONLY);
1258 }
1259
1260 if (vaStatus != VA_STATUS_SUCCESS || err != 0)
1261 {
1262 DdiMediaUtil_FreeBuffer(surface->pShadowBuffer);
1263 MOS_FreeMemory(surface->pShadowBuffer);
1264 surface->pShadowBuffer = nullptr;
1265 }
1266 }
1267 }
1268
1269 mos_bo_map(surface->bo, flag & MOS_LOCKFLAG_WRITEONLY);
1270
1271 if (surface->pShadowBuffer == nullptr)
1272 {
1273 if (surface->pSystemShadow == nullptr)
1274 {
1275 surface->pSystemShadow = (uint8_t*)MOS_AllocMemory(surface->bo->size);
1276 DDI_CHK_CONDITION((surface->pSystemShadow == nullptr), "Failed to allocate shadow surface", nullptr);
1277 }
1278
1279 vaStatus = SwizzleSurface(surface->pMediaCtx,
1280 surface->pGmmResourceInfo,
1281 surface->bo->virt,
1282 (MOS_TILE_TYPE)surface->TileType,
1283 (uint8_t *)surface->pSystemShadow,
1284 false);
1285 DDI_CHK_CONDITION((vaStatus != VA_STATUS_SUCCESS), "SwizzleSurface failed", nullptr);
1286 }
1287
1288 }
1289 else if (flag & MOS_LOCKFLAG_NO_SWIZZLE)
1290 {
1291 mos_bo_map(surface->bo, flag & MOS_LOCKFLAG_READONLY);
1292 }
1293 else if (flag & MOS_LOCKFLAG_WRITEONLY)
1294 {
1295 mos_bo_map_gtt(surface->bo);
1296 }
1297 else
1298 {
1299 mos_bo_map_unsynchronized(surface->bo); // only call mmap_gtt ioctl
1300 mos_bo_start_gtt_access(surface->bo, 0); // set to GTT domain,0 means readonly
1301 }
1302 }
1303 surface->uiMapFlag = flag;
1304 if (surface->pShadowBuffer)
1305 {
1306 surface->pData = (uint8_t *)surface->pShadowBuffer->bo->virt;
1307 }
1308 else if (surface->pSystemShadow)
1309 {
1310 surface->pData = surface->pSystemShadow;
1311 }
1312 else
1313 {
1314 surface->pData = (uint8_t*) surface->bo->virt;
1315 }
1316 surface->data_size = surface->bo->size;
1317 surface->bMapped = true;
1318
1319 return surface->pData;
1320 }
1321
1322 void DdiMediaUtil_UnlockSurface(DDI_MEDIA_SURFACE *surface)
1323 {
1324 DDI_CHK_NULL(surface, "nullptr surface", );
1325 DDI_CHK_NULL(surface->pMediaCtx, "nullptr surface->pMediaCtx", );
1326 if (0 == surface->iRefCount)
1327 return;
1328
1329 if (MEDIA_IS_SKU(&surface->pMediaCtx->SkuTable, FtrLocalMemory))
1330 {
1331 if (MosUtilities::MosAtomicDecrement(&surface->iRefCount) == 0 && (true == surface->bMapped))
1332 {
1333 DdiMediaUtil_UnlockSurfaceInternal(surface);
1334 }
1335 else
1336 {
1337 DDI_VERBOSEMESSAGE("invalide operation for unlockSurface. the surface reference count = %d", surface->iRefCount);
1338 }
1339 }
1340 else
1341 {
1342 if ((1 == surface->iRefCount) && (true == surface->bMapped))
1343 {
1344 DdiMediaUtil_UnlockSurfaceInternal(surface);
1345 }
1346 else
1347 {
1348 DDI_VERBOSEMESSAGE("invalide operation for unlockSurface. the surface reference count = %d", surface->iRefCount);
1349 }
1350 surface->iRefCount --;
1351 }
1352 }
1353
1354 void DdiMediaUtil_UnlockSurfaceInternal(DDI_MEDIA_SURFACE *surface)
1355 {
1356 DDI_CHK_NULL(surface, "nullptr surface",);
1357 DDI_CHK_NULL(surface->bo, "nullptr surface->bo", );
1358
1359 if (surface->pMediaCtx->bIsAtomSOC)
1360 {
1361 mos_bo_unmap_gtt(surface->bo);
1362 }
1363 else
1364 {
1365 if (surface->TileType == TILING_NONE)
1366 {
1367 mos_bo_unmap(surface->bo);
1368 }
1369 else if (surface->pShadowBuffer != nullptr)
1370 {
1371 SwizzleSurfaceByHW(surface, true);
1372
1373 mos_bo_unmap(surface->pShadowBuffer->bo);
1374 mos_bo_unmap(surface->bo);
1375 }
1376 else if (surface->pSystemShadow)
1377 {
1378 SwizzleSurface(surface->pMediaCtx,
1379 surface->pGmmResourceInfo,
1380 surface->bo->virt,
1381 (MOS_TILE_TYPE)surface->TileType,
1382 (uint8_t *)surface->pSystemShadow,
1383 true);
1384
1385 MOS_FreeMemory(surface->pSystemShadow);
1386 surface->pSystemShadow = nullptr;
1387
1388 mos_bo_unmap(surface->bo);
1389 }
1390 else if(surface->uiMapFlag & MOS_LOCKFLAG_NO_SWIZZLE)
1391 {
1392 mos_bo_unmap(surface->bo);
1393 }
1394 else
1395 {
1396 mos_bo_unmap_gtt(surface->bo);
1397 }
1398 }
1399 surface->pData = nullptr;
1400 surface->bo->virt = nullptr;
1401 surface->bMapped = false;
1402 }
1403
1404 // add thread protection for multiple thread?
1405 // MapBuffer?
1406 void* DdiMediaUtil_LockBuffer(DDI_MEDIA_BUFFER *buf, uint32_t flag)
1407 {
1408 DDI_CHK_NULL(buf, "nullptr buf", nullptr);
1409 if((Media_Format_CPU != buf->format) && (false == buf->bMapped))
1410 {
1411 if (nullptr != buf->pSurface)
1412 {
1413 DdiMediaUtil_LockSurface(buf->pSurface, flag);
1414 buf->pData = buf->pSurface->pData;
1415 }
1416 else
1417 {
1418 if (buf->pMediaCtx->bIsAtomSOC)
1419 {
1420 mos_bo_map_gtt(buf->bo);
1421 }
1422 else
1423 {
1424 if (buf->TileType == TILING_NONE)
1425 {
1426 mos_bo_map(buf->bo, ((MOS_LOCKFLAG_READONLY | MOS_LOCKFLAG_WRITEONLY) & flag));
1427 }
1428 else
1429 {
1430 mos_bo_map_gtt(buf->bo);
1431 }
1432 }
1433
1434 buf->pData = (uint8_t*)(buf->bo->virt);
1435 }
1436
1437 buf->bMapped = true;
1438 buf->iRefCount++;
1439 }
1440 else if ((Media_Format_CPU == buf->format) && (false == buf->bMapped))
1441 {
1442 buf->bMapped = true;
1443 buf->iRefCount++;
1444 }
1445 else
1446 {
1447 buf->iRefCount++;
1448 }
1449
1450 return buf->pData;
1451 }
1452
1453 void DdiMediaUtil_UnlockBuffer(DDI_MEDIA_BUFFER *buf)
1454 {
1455 DDI_CHK_NULL(buf, "nullptr buf", );
1456 if (0 == buf->iRefCount)
1457 return;
1458 if((true == buf->bMapped) && (Media_Format_CPU != buf->format) && (1 == buf->iRefCount))
1459 {
1460 if (nullptr != buf->pSurface)
1461 {
1462 DdiMediaUtil_UnlockSurface(buf->pSurface);
1463 }
1464 else
1465 {
1466 if (buf->pMediaCtx->bIsAtomSOC)
1467 {
1468 mos_bo_unmap_gtt(buf->bo);
1469 }
1470 else
1471 {
1472 if (buf->TileType == TILING_NONE)
1473 {
1474 mos_bo_unmap(buf->bo);
1475 }
1476 else
1477 {
1478 mos_bo_unmap_gtt(buf->bo);
1479 }
1480 }
1481 buf->bo->virt = nullptr;
1482 }
1483
1484 buf->pData = nullptr;
1485
1486 buf->bMapped = false;
1487 }
1488 else if ((true == buf->bMapped) && (Media_Format_CPU == buf->format) && (1 == buf->iRefCount))
1489 {
1490 buf->bMapped = false;
1491 }
1492 else
1493 {
1494 // do nothing here
1495 }
1496 buf->iRefCount--;
1497 }
1498
1499 // should ref_count added for bo?
1500 void DdiMediaUtil_FreeSurface(DDI_MEDIA_SURFACE *surface)
1501 {
1502 DDI_CHK_NULL(surface, "nullptr surface", );
1503 DDI_CHK_NULL(surface->bo, "nullptr surface->bo", );
1504 DDI_CHK_NULL(surface->pMediaCtx, "nullptr surface->pMediaCtx", );
1505 DDI_CHK_NULL(surface->pMediaCtx->pGmmClientContext, "nullptr surface->pMediaCtx->pGmmClientContext", );
1506
1507 // Unmap Aux mapping if the surface was mapped
1508 if (surface->pMediaCtx->m_auxTableMgr)
1509 {
1510 surface->pMediaCtx->m_auxTableMgr->UnmapResource(surface->pGmmResourceInfo, surface->bo);
1511 }
1512
1513 // free shadow buffer it created
1514 if (surface->pShadowBuffer != nullptr)
1515 {
1516 DdiMediaUtil_FreeBuffer(surface->pShadowBuffer);
1517 MOS_FreeMemory(surface->pShadowBuffer);
1518 surface->pShadowBuffer = nullptr;
1519 }
1520
1521 if(surface->bMapped)
1522 {
1523 DdiMediaUtil_UnlockSurface(surface);
1524 DDI_VERBOSEMESSAGE("DDI: try to free a locked surface.");
1525 }
1526 mos_bo_unreference(surface->bo);
1527 // For External Buffer, only needs to destory SurfaceDescriptor
1528 if (surface->pSurfDesc)
1529 {
1530 MOS_FreeMemory(surface->pSurfDesc);
1531 surface->pSurfDesc = nullptr;
1532 }
1533
1534 if (nullptr != surface->pGmmResourceInfo)
1535 {
1536 surface->pMediaCtx->pGmmClientContext->DestroyResInfoObject(surface->pGmmResourceInfo);
1537 surface->pGmmResourceInfo = nullptr;
1538 }
1539 }
1540
1541
1542 // should ref_count added for bo?
1543 void DdiMediaUtil_FreeBuffer(DDI_MEDIA_BUFFER *buf)
1544 {
1545 DDI_CHK_NULL(buf, "nullptr", );
1546 // calling sequence checking
1547 if (buf->bMapped)
1548 {
1549 DdiMediaUtil_UnlockBuffer(buf);
1550 DDI_VERBOSEMESSAGE("DDI: try to free a locked buffer.");
1551 }
1552 if (buf->format == Media_Format_CPU)
1553 {
1554 MOS_FreeMemory(buf->pData);
1555 buf->pData = nullptr;
1556 }
1557 else
1558 {
1559 mos_bo_unreference(buf->bo);
1560 buf->bo = nullptr;
1561 }
1562
1563 if (nullptr != buf->pMediaCtx && nullptr != buf->pMediaCtx->pGmmClientContext && nullptr != buf->pGmmResourceInfo)
1564 {
1565 buf->pMediaCtx->pGmmClientContext->DestroyResInfoObject(buf->pGmmResourceInfo);
1566 buf->pGmmResourceInfo = nullptr;
1567 }
1568 }
1569
1570 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1571 // purpose: fill a rect structure with the regsion specified by parameters
1572 // rect[in]: input pointer to the rect
1573 // offset_x: x offset of the region
1574 // offset_y: y offset of the region
1575 // width: width of the region
1576 // hiehgt: height of the regsion
1577 ////////////////////////////////////////////////////////////////////////////////////////////////////
1578 VAStatus DdiMediaUtil_FillPositionToRect(RECT *rect, int16_t offset_x, int16_t offset_y, int16_t width, int16_t height)
1579 {
1580 DDI_CHK_NULL(rect, "Invalid Rect.", VA_STATUS_ERROR_INVALID_PARAMETER);
1581
1582 rect->left = offset_x;
1583 rect->top = offset_y;
1584 rect->right = offset_x + width;
1585 rect->bottom = offset_y + height;
1586
1587 return VA_STATUS_SUCCESS;
1588 }
1589
1590 void DdiMediaUtil_InitMutex(PMEDIA_MUTEX_T mutex)
1591 {
1592 pthread_mutex_init(mutex, nullptr);
1593 }
1594
1595 void DdiMediaUtil_DestroyMutex(PMEDIA_MUTEX_T mutex)
1596 {
1597 int32_t ret = pthread_mutex_destroy(mutex);
1598 if(ret != 0)
1599 {
1600 DDI_NORMALMESSAGE("can't destroy the mutex!\n");
1601 }
1602 }
1603
1604 void DdiMediaUtil_LockMutex(PMEDIA_MUTEX_T mutex)
1605 {
1606 int32_t ret = pthread_mutex_lock(mutex);
1607 if(ret != 0)
1608 {
1609 DDI_NORMALMESSAGE("can't lock the mutex!\n");
1610 }
1611 }
1612
1613 void DdiMediaUtil_UnLockMutex(PMEDIA_MUTEX_T mutex)
1614 {
1615 int32_t ret = pthread_mutex_unlock(mutex);
1616 if(ret != 0)
1617 {
1618 DDI_NORMALMESSAGE("can't unlock the mutex!\n");
1619 }
1620 }
1621
1622 void DdiMediaUtil_DestroySemaphore(PMEDIA_SEM_T sem)
1623 {
1624 int32_t ret = sem_destroy(sem);
1625 if(ret != 0)
1626 {
1627 DDI_NORMALMESSAGE("can't destroy the semaphore!\n");
1628 }
1629 }
1630
1631 void DdiMediaUtil_WaitSemaphore(PMEDIA_SEM_T sem)
1632 {
1633 int32_t ret = sem_wait(sem);
1634 if(ret != 0)
1635 {
1636 DDI_NORMALMESSAGE("wait semaphore error!\n");
1637 }
1638 }
1639
1640 int32_t DdiMediaUtil_TryWaitSemaphore(PMEDIA_SEM_T sem)
1641 {
1642 return sem_trywait(sem);
1643 }
1644
1645 void DdiMediaUtil_PostSemaphore(PMEDIA_SEM_T sem)
1646 {
1647 int32_t ret = sem_post(sem);
1648 if(ret != 0)
1649 {
1650 DDI_NORMALMESSAGE("post semaphore error!\n");
1651 }
1652 }
1653
1654 // heap related
1655 PDDI_MEDIA_SURFACE_HEAP_ELEMENT DdiMediaUtil_AllocPMediaSurfaceFromHeap(PDDI_MEDIA_HEAP surfaceHeap)
1656 {
1657 DDI_CHK_NULL(surfaceHeap, "nullptr surfaceHeap", nullptr);
1658
1659 PDDI_MEDIA_SURFACE_HEAP_ELEMENT mediaSurfaceHeapElmt = nullptr;
1660
1661 if (nullptr == surfaceHeap->pFirstFreeHeapElement)
1662 {
1663 void *newHeapBase = MOS_ReallocMemory(surfaceHeap->pHeapBase, (surfaceHeap->uiAllocatedHeapElements + DDI_MEDIA_HEAP_INCREMENTAL_SIZE) * sizeof(DDI_MEDIA_SURFACE_HEAP_ELEMENT));
1664
1665 if (nullptr == newHeapBase)
1666 {
1667 DDI_ASSERTMESSAGE("DDI: realloc failed.");
1668 return nullptr;
1669 }
1670 surfaceHeap->pHeapBase = newHeapBase;
1671 PDDI_MEDIA_SURFACE_HEAP_ELEMENT surfaceHeapBase = (PDDI_MEDIA_SURFACE_HEAP_ELEMENT)surfaceHeap->pHeapBase;
1672 surfaceHeap->pFirstFreeHeapElement = (void*)(&surfaceHeapBase[surfaceHeap->uiAllocatedHeapElements]);
1673 for (int32_t i = 0; i < (DDI_MEDIA_HEAP_INCREMENTAL_SIZE); i++)
1674 {
1675 mediaSurfaceHeapElmt = &surfaceHeapBase[surfaceHeap->uiAllocatedHeapElements + i];
1676 mediaSurfaceHeapElmt->pNextFree = (i == (DDI_MEDIA_HEAP_INCREMENTAL_SIZE - 1))? nullptr : &surfaceHeapBase[surfaceHeap->uiAllocatedHeapElements + i + 1];
1677 mediaSurfaceHeapElmt->uiVaSurfaceID = surfaceHeap->uiAllocatedHeapElements + i;
1678 mediaSurfaceHeapElmt->pSurface = nullptr;
1679 }
1680 surfaceHeap->uiAllocatedHeapElements += DDI_MEDIA_HEAP_INCREMENTAL_SIZE;
1681 }
1682
1683 mediaSurfaceHeapElmt = (PDDI_MEDIA_SURFACE_HEAP_ELEMENT)surfaceHeap->pFirstFreeHeapElement;
1684 surfaceHeap->pFirstFreeHeapElement = mediaSurfaceHeapElmt->pNextFree;
1685
1686 return mediaSurfaceHeapElmt;
1687 }
1688
1689
1690 void DdiMediaUtil_ReleasePMediaSurfaceFromHeap(PDDI_MEDIA_HEAP surfaceHeap, uint32_t vaSurfaceID)
1691 {
1692 DDI_CHK_NULL(surfaceHeap, "nullptr surfaceHeap", );
1693
1694 DDI_CHK_LESS(vaSurfaceID, surfaceHeap->uiAllocatedHeapElements, "invalid surface id", );
1695 PDDI_MEDIA_SURFACE_HEAP_ELEMENT mediaSurfaceHeapBase = (PDDI_MEDIA_SURFACE_HEAP_ELEMENT)surfaceHeap->pHeapBase;
1696 DDI_CHK_NULL(mediaSurfaceHeapBase, "nullptr mediaSurfaceHeapBase", );
1697
1698 PDDI_MEDIA_SURFACE_HEAP_ELEMENT mediaSurfaceHeapElmt = &mediaSurfaceHeapBase[vaSurfaceID];
1699 DDI_CHK_NULL(mediaSurfaceHeapElmt->pSurface, "surface is already released", );
1700 void *firstFree = surfaceHeap->pFirstFreeHeapElement;
1701 surfaceHeap->pFirstFreeHeapElement = (void*)mediaSurfaceHeapElmt;
1702 mediaSurfaceHeapElmt->pNextFree = (PDDI_MEDIA_SURFACE_HEAP_ELEMENT)firstFree;
1703 mediaSurfaceHeapElmt->pSurface = nullptr;
1704 }
1705
1706
1707 PDDI_MEDIA_BUFFER_HEAP_ELEMENT DdiMediaUtil_AllocPMediaBufferFromHeap(PDDI_MEDIA_HEAP bufferHeap)
1708 {
1709 DDI_CHK_NULL(bufferHeap, "nullptr bufferHeap", nullptr);
1710
1711 PDDI_MEDIA_BUFFER_HEAP_ELEMENT mediaBufferHeapElmt = nullptr;
1712 if (nullptr == bufferHeap->pFirstFreeHeapElement)
1713 {
1714 void *newHeapBase = MOS_ReallocMemory(bufferHeap->pHeapBase, (bufferHeap->uiAllocatedHeapElements + DDI_MEDIA_HEAP_INCREMENTAL_SIZE) * sizeof(DDI_MEDIA_BUFFER_HEAP_ELEMENT));
1715 if (nullptr == newHeapBase)
1716 {
1717 DDI_ASSERTMESSAGE("DDI: realloc failed.");
1718 return nullptr;
1719 }
1720 bufferHeap->pHeapBase = newHeapBase;
1721 PDDI_MEDIA_BUFFER_HEAP_ELEMENT mediaBufferHeapBase = (PDDI_MEDIA_BUFFER_HEAP_ELEMENT)bufferHeap->pHeapBase;
1722 bufferHeap->pFirstFreeHeapElement = (void*)(&mediaBufferHeapBase[bufferHeap->uiAllocatedHeapElements]);
1723 for (int32_t i = 0; i < (DDI_MEDIA_HEAP_INCREMENTAL_SIZE); i++)
1724 {
1725 mediaBufferHeapElmt = &mediaBufferHeapBase[bufferHeap->uiAllocatedHeapElements + i];
1726 mediaBufferHeapElmt->pNextFree = (i == (DDI_MEDIA_HEAP_INCREMENTAL_SIZE - 1))? nullptr : &mediaBufferHeapBase[bufferHeap->uiAllocatedHeapElements + i + 1];
1727 mediaBufferHeapElmt->uiVaBufferID = bufferHeap->uiAllocatedHeapElements + i;
1728 }
1729 bufferHeap->uiAllocatedHeapElements += DDI_MEDIA_HEAP_INCREMENTAL_SIZE;
1730 }
1731
1732 mediaBufferHeapElmt = (PDDI_MEDIA_BUFFER_HEAP_ELEMENT)bufferHeap->pFirstFreeHeapElement;
1733 bufferHeap->pFirstFreeHeapElement = mediaBufferHeapElmt->pNextFree;
1734 return mediaBufferHeapElmt;
1735 }
1736
1737
1738 void DdiMediaUtil_ReleasePMediaBufferFromHeap(PDDI_MEDIA_HEAP bufferHeap, uint32_t vaBufferID)
1739 {
1740 DDI_CHK_NULL(bufferHeap, "nullptr bufferHeap", );
1741
1742 DDI_CHK_LESS(vaBufferID, bufferHeap->uiAllocatedHeapElements, "invalid buffer id", );
1743 PDDI_MEDIA_BUFFER_HEAP_ELEMENT mediaBufferHeapBase = (PDDI_MEDIA_BUFFER_HEAP_ELEMENT)bufferHeap->pHeapBase;
1744 PDDI_MEDIA_BUFFER_HEAP_ELEMENT mediaBufferHeapElmt = &mediaBufferHeapBase[vaBufferID];
1745 DDI_CHK_NULL(mediaBufferHeapElmt->pBuffer, "buffer is already released", );
1746 void *firstFree = bufferHeap->pFirstFreeHeapElement;
1747 bufferHeap->pFirstFreeHeapElement = (void*)mediaBufferHeapElmt;
1748 mediaBufferHeapElmt->pNextFree = (PDDI_MEDIA_BUFFER_HEAP_ELEMENT)firstFree;
1749 mediaBufferHeapElmt->pBuffer = nullptr;
1750 }
1751
1752 PDDI_MEDIA_IMAGE_HEAP_ELEMENT DdiMediaUtil_AllocPVAImageFromHeap(PDDI_MEDIA_HEAP imageHeap)
1753 {
1754 PDDI_MEDIA_IMAGE_HEAP_ELEMENT vaimageHeapElmt = nullptr;
1755
1756 DDI_CHK_NULL(imageHeap, "nullptr imageHeap", nullptr);
1757
1758 if (nullptr == imageHeap->pFirstFreeHeapElement)
1759 {
1760 void *newHeapBase = MOS_ReallocMemory(imageHeap->pHeapBase, (imageHeap->uiAllocatedHeapElements + DDI_MEDIA_HEAP_INCREMENTAL_SIZE) * sizeof(DDI_MEDIA_IMAGE_HEAP_ELEMENT));
1761
1762 if (nullptr == newHeapBase)
1763 {
1764 DDI_ASSERTMESSAGE("DDI: realloc failed.");
1765 return nullptr;
1766 }
1767 imageHeap->pHeapBase = newHeapBase;
1768 PDDI_MEDIA_IMAGE_HEAP_ELEMENT vaimageHeapBase = (PDDI_MEDIA_IMAGE_HEAP_ELEMENT)imageHeap->pHeapBase;
1769 imageHeap->pFirstFreeHeapElement = (void*)(&vaimageHeapBase[imageHeap->uiAllocatedHeapElements]);
1770 for (int32_t i = 0; i < (DDI_MEDIA_HEAP_INCREMENTAL_SIZE); i++)
1771 {
1772 vaimageHeapElmt = &vaimageHeapBase[imageHeap->uiAllocatedHeapElements + i];
1773 vaimageHeapElmt->pNextFree = (i == (DDI_MEDIA_HEAP_INCREMENTAL_SIZE - 1))? nullptr : &vaimageHeapBase[imageHeap->uiAllocatedHeapElements + i + 1];
1774 vaimageHeapElmt->uiVaImageID = imageHeap->uiAllocatedHeapElements + i;
1775 }
1776 imageHeap->uiAllocatedHeapElements += DDI_MEDIA_HEAP_INCREMENTAL_SIZE;
1777
1778 }
1779
1780 vaimageHeapElmt = (PDDI_MEDIA_IMAGE_HEAP_ELEMENT)imageHeap->pFirstFreeHeapElement;
1781 imageHeap->pFirstFreeHeapElement = vaimageHeapElmt->pNextFree;
1782 return vaimageHeapElmt;
1783 }
1784
1785
1786 void DdiMediaUtil_ReleasePVAImageFromHeap(PDDI_MEDIA_HEAP imageHeap, uint32_t vaImageID)
1787 {
1788 PDDI_MEDIA_IMAGE_HEAP_ELEMENT vaImageHeapBase = nullptr;
1789 PDDI_MEDIA_IMAGE_HEAP_ELEMENT vaImageHeapElmt = nullptr;
1790 void *firstFree = nullptr;
1791
1792 DDI_CHK_NULL(imageHeap, "nullptr imageHeap", );
1793
1794 DDI_CHK_LESS(vaImageID, imageHeap->uiAllocatedHeapElements, "invalid image id", );
1795 vaImageHeapBase = (PDDI_MEDIA_IMAGE_HEAP_ELEMENT)imageHeap->pHeapBase;
1796 vaImageHeapElmt = &vaImageHeapBase[vaImageID];
1797 DDI_CHK_NULL(vaImageHeapElmt->pImage, "image is already released", );
1798 firstFree = imageHeap->pFirstFreeHeapElement;
1799 imageHeap->pFirstFreeHeapElement = (void*)vaImageHeapElmt;
1800 vaImageHeapElmt->pNextFree = (PDDI_MEDIA_IMAGE_HEAP_ELEMENT)firstFree;
1801 vaImageHeapElmt->pImage = nullptr;
1802 }
1803
1804 PDDI_MEDIA_VACONTEXT_HEAP_ELEMENT DdiMediaUtil_AllocPVAContextFromHeap(PDDI_MEDIA_HEAP vaContextHeap)
1805 {
1806 PDDI_MEDIA_VACONTEXT_HEAP_ELEMENT vacontextHeapElmt = nullptr;
1807 DDI_CHK_NULL(vaContextHeap, "nullptr vaContextHeap", nullptr);
1808
1809 if (nullptr == vaContextHeap->pFirstFreeHeapElement)
1810 {
1811 void *newHeapBase = MOS_ReallocMemory(vaContextHeap->pHeapBase, (vaContextHeap->uiAllocatedHeapElements + DDI_MEDIA_HEAP_INCREMENTAL_SIZE) * sizeof(DDI_MEDIA_VACONTEXT_HEAP_ELEMENT));
1812
1813 if (nullptr == newHeapBase)
1814 {
1815 DDI_ASSERTMESSAGE("DDI: realloc failed.");
1816 return nullptr;
1817 }
1818 vaContextHeap->pHeapBase = newHeapBase;
1819 PDDI_MEDIA_VACONTEXT_HEAP_ELEMENT vacontextHeapBase = (PDDI_MEDIA_VACONTEXT_HEAP_ELEMENT)vaContextHeap->pHeapBase;
1820 vaContextHeap->pFirstFreeHeapElement = (void*)(&(vacontextHeapBase[vaContextHeap->uiAllocatedHeapElements]));
1821 for (int32_t i = 0; i < (DDI_MEDIA_HEAP_INCREMENTAL_SIZE); i++)
1822 {
1823 vacontextHeapElmt = &vacontextHeapBase[vaContextHeap->uiAllocatedHeapElements + i];
1824 vacontextHeapElmt->pNextFree = (i == (DDI_MEDIA_HEAP_INCREMENTAL_SIZE - 1))? nullptr : &vacontextHeapBase[vaContextHeap->uiAllocatedHeapElements + i + 1];
1825 vacontextHeapElmt->uiVaContextID = vaContextHeap->uiAllocatedHeapElements + i;
1826 vacontextHeapElmt->pVaContext = nullptr;
1827 }
1828 vaContextHeap->uiAllocatedHeapElements += DDI_MEDIA_HEAP_INCREMENTAL_SIZE;
1829 }
1830
1831 vacontextHeapElmt = (PDDI_MEDIA_VACONTEXT_HEAP_ELEMENT)vaContextHeap->pFirstFreeHeapElement;
1832 vaContextHeap->pFirstFreeHeapElement = vacontextHeapElmt->pNextFree;
1833 return vacontextHeapElmt;
1834 }
1835
1836
1837 void DdiMediaUtil_ReleasePVAContextFromHeap(PDDI_MEDIA_HEAP vaContextHeap, uint32_t vaContextID)
1838 {
1839 DDI_CHK_NULL(vaContextHeap, "nullptr vaContextHeap", );
1840 DDI_CHK_LESS(vaContextID, vaContextHeap->uiAllocatedHeapElements, "invalid context id", );
1841 PDDI_MEDIA_VACONTEXT_HEAP_ELEMENT vaContextHeapBase = (PDDI_MEDIA_VACONTEXT_HEAP_ELEMENT)vaContextHeap->pHeapBase;
1842 PDDI_MEDIA_VACONTEXT_HEAP_ELEMENT vaContextHeapElmt = &vaContextHeapBase[vaContextID];
1843 DDI_CHK_NULL(vaContextHeapElmt->pVaContext, "context is already released", );
1844 void *firstFree = vaContextHeap->pFirstFreeHeapElement;
1845 vaContextHeap->pFirstFreeHeapElement = (void*)vaContextHeapElmt;
1846 vaContextHeapElmt->pNextFree = (PDDI_MEDIA_VACONTEXT_HEAP_ELEMENT)firstFree;
1847 vaContextHeapElmt->pVaContext = nullptr;
1848 }
1849
1850 void DdiMediaUtil_UnRefBufObjInMediaBuffer(PDDI_MEDIA_BUFFER buf)
1851 {
1852 mos_bo_unreference(buf->bo);
1853 }
1854
1855 // Open Intel's Graphics Device to get the file descriptor
1856 int32_t DdiMediaUtil_OpenGraphicsAdaptor(char *devName)
1857 {
1858 struct stat st;
1859 int32_t hDevice = -1;
1860 if(nullptr == devName)
1861 {
1862 DDI_ASSERTMESSAGE("Invalid Graphics Node");
1863 return -1;
1864 }
1865
1866 hDevice = open (devName, O_RDWR);
1867 if (-1 == hDevice)
1868 {
1869 DDI_ASSERTMESSAGE("Cannot open '%s': %d, %s.", devName, errno, strerror (errno));
1870 return -1;
1871 }
1872
1873 if (-1 == fstat (hDevice, &st))
1874 {
1875 DDI_ASSERTMESSAGE("Cannot identify '%s': %d, %s.", devName, errno, strerror (errno));
1876 close(hDevice);
1877 return -1;
1878 }
1879
1880 if (!S_ISCHR (st.st_mode))
1881 {
1882 DDI_ASSERTMESSAGE("%s is no device.", devName);
1883 close(hDevice);
1884 return -1;
1885 }
1886 return hDevice;
1887 }
1888
1889 VAStatus DdiMediaUtil_UnRegisterRTSurfaces(
1890 VADriverContextP ctx,
1891 PDDI_MEDIA_SURFACE surface)
1892 {
1893 DDI_CHK_NULL(ctx,"nullptr context!", VA_STATUS_ERROR_INVALID_CONTEXT);
1894 PDDI_MEDIA_CONTEXT mediaCtx = DdiMedia_GetMediaContext(ctx);
1895 DDI_CHK_NULL(mediaCtx,"nullptr mediaCtx!", VA_STATUS_ERROR_INVALID_CONTEXT);
1896 DDI_CHK_NULL(surface, "nullptr surface!", VA_STATUS_ERROR_INVALID_PARAMETER);
1897
1898 //Look through all decode contexts to unregister the surface in each decode context's RTtable.
1899 if (mediaCtx->pDecoderCtxHeap != nullptr)
1900 {
1901 PDDI_MEDIA_VACONTEXT_HEAP_ELEMENT decVACtxHeapBase;
1902
1903 DdiMediaUtil_LockMutex(&mediaCtx->DecoderMutex);
1904 decVACtxHeapBase = (PDDI_MEDIA_VACONTEXT_HEAP_ELEMENT)mediaCtx->pDecoderCtxHeap->pHeapBase;
1905 for (uint32_t j = 0; j < mediaCtx->pDecoderCtxHeap->uiAllocatedHeapElements; j++)
1906 {
1907 if (decVACtxHeapBase[j].pVaContext != nullptr)
1908 {
1909 PDDI_DECODE_CONTEXT decCtx = (PDDI_DECODE_CONTEXT)decVACtxHeapBase[j].pVaContext;
1910 if (decCtx && decCtx->m_ddiDecode)
1911 {
1912 //not check the return value since the surface may not be registered in the context. pay attention to LOGW.
1913 decCtx->m_ddiDecode->UnRegisterRTSurfaces(&decCtx->RTtbl, surface);
1914 }
1915 }
1916 }
1917 DdiMediaUtil_UnLockMutex(&mediaCtx->DecoderMutex);
1918 }
1919 if (mediaCtx->pEncoderCtxHeap != nullptr)
1920 {
1921 PDDI_MEDIA_VACONTEXT_HEAP_ELEMENT pEncVACtxHeapBase;
1922
1923 DdiMediaUtil_LockMutex(&mediaCtx->EncoderMutex);
1924 pEncVACtxHeapBase = (PDDI_MEDIA_VACONTEXT_HEAP_ELEMENT)mediaCtx->pEncoderCtxHeap->pHeapBase;
1925 for (uint32_t j = 0; j < mediaCtx->pEncoderCtxHeap->uiAllocatedHeapElements; j++)
1926 {
1927 if (pEncVACtxHeapBase[j].pVaContext != nullptr)
1928 {
1929 PDDI_ENCODE_CONTEXT pEncCtx = (PDDI_ENCODE_CONTEXT)pEncVACtxHeapBase[j].pVaContext;
1930 if (pEncCtx && pEncCtx->m_encode)
1931 {
1932 //not check the return value since the surface may not be registered in the context. pay attention to LOGW.
1933 pEncCtx->m_encode->UnRegisterRTSurfaces(&pEncCtx->RTtbl, surface);
1934 }
1935 }
1936 }
1937 DdiMediaUtil_UnLockMutex(&mediaCtx->EncoderMutex);
1938 }
1939
1940 return VA_STATUS_SUCCESS;
1941 }
1942
1943 VAStatus DdiMediaUtil_SetMediaResetEnableFlag(PDDI_MEDIA_CONTEXT mediaCtx)
1944 {
1945 bool enableReset = false;
1946 mediaCtx->bMediaResetEnable = false;
1947
1948 DDI_CHK_NULL(mediaCtx,"nullptr mediaCtx!", VA_STATUS_ERROR_INVALID_CONTEXT);
1949
1950 if(!MEDIA_IS_SKU(&mediaCtx->SkuTable, FtrSWMediaReset))
1951 {
1952 mediaCtx->bMediaResetEnable = false;
1953 return VA_STATUS_SUCCESS;
1954 }
1955
1956 mediaCtx->bMediaResetEnable = true;
1957 #if (_DEBUG || _RELEASE_INTERNAL)
1958 ReadUserSettingForDebug(
1959 mediaCtx->m_userSettingPtr,
1960 enableReset,
1961 __MEDIA_USER_FEATURE_VALUE_MEDIA_RESET_ENABLE,
1962 MediaUserSetting::Group::Device);
1963 mediaCtx->bMediaResetEnable = enableReset;
1964 #endif
1965
1966 if(!mediaCtx->bMediaResetEnable)
1967 {
1968 return VA_STATUS_SUCCESS;
1969 }
1970
1971 char* mediaResetEnv = getenv("INTEL_MEDIA_RESET_WATCHDOG");
1972 if(mediaResetEnv)
1973 {
1974 mediaCtx->bMediaResetEnable = strcmp(mediaResetEnv, "1") ? false : true;
1975 return VA_STATUS_SUCCESS;
1976 }
1977
1978 return VA_STATUS_SUCCESS;
1979 }