From fcab0cd2daf621ce8d02daca343ee7b62751c5c2 Mon Sep 17 00:00:00 2001 From: "Xu, Zhengguo" Date: Tue, 23 Apr 2024 15:21:06 +0800 Subject: [PATCH] [Decode] move lock from element to pSurface/ pBuffer Signed-off-by: Xu, Zhengguo --- .../codec/ddi/media_ddi_decode_base.cpp | 2 + .../codec/ddi/media_ddi_encode_base.cpp | 1 + .../common/codec/ddi/media_libva_decoder.cpp | 8 +- media_driver/linux/common/ddi/media_libva.cpp | 97 +++++++++++-------- .../linux/common/ddi/media_libva_common.cpp | 24 ++--- .../linux/common/ddi/media_libva_util.cpp | 38 ++++++-- .../linux/common/vp/ddi/media_libva_vp.c | 1 + .../common/ddi/media_libva_common_next.h | 7 +- 8 files changed, 110 insertions(+), 68 deletions(-) diff --git a/media_driver/linux/common/codec/ddi/media_ddi_decode_base.cpp b/media_driver/linux/common/codec/ddi/media_ddi_decode_base.cpp index ed98570fee..fa1ce7d8e9 100644 --- a/media_driver/linux/common/codec/ddi/media_ddi_decode_base.cpp +++ b/media_driver/linux/common/codec/ddi/media_ddi_decode_base.cpp @@ -994,6 +994,7 @@ VAStatus DdiMediaDecode::CreateBuffer( buf->uiOffset = 0; buf->bCFlushReq = false; buf->pMediaCtx = m_ddiDecodeCtx->pMediaCtx; + buf->lock = MOS_New(std::shared_timed_mutex); switch ((int32_t)type) { @@ -1122,6 +1123,7 @@ VAStatus DdiMediaDecode::CreateBuffer( if(buf) { MOS_FreeMemory(buf->pData); + MOS_Delete(buf->lock); MOS_FreeMemory(buf); } return va; diff --git a/media_driver/linux/common/codec/ddi/media_ddi_encode_base.cpp b/media_driver/linux/common/codec/ddi/media_ddi_encode_base.cpp index a600f0e8b6..6f57ad9dc4 100644 --- a/media_driver/linux/common/codec/ddi/media_ddi_encode_base.cpp +++ b/media_driver/linux/common/codec/ddi/media_ddi_encode_base.cpp @@ -917,6 +917,7 @@ VAStatus DdiEncodeBase::CreateBuffer( buf->uiNumElements = elementsNum; buf->uiType = type; buf->uiOffset = 0; + buf->lock = MOS_New(std::shared_timed_mutex); uint32_t bufSize = 0; uint32_t expectedSize = 0xffffffff; diff --git a/media_driver/linux/common/codec/ddi/media_libva_decoder.cpp b/media_driver/linux/common/codec/ddi/media_libva_decoder.cpp index 230840cc43..f656c6c4ef 100755 --- a/media_driver/linux/common/codec/ddi/media_libva_decoder.cpp +++ b/media_driver/linux/common/codec/ddi/media_libva_decoder.cpp @@ -408,7 +408,7 @@ VAStatus DdiDecode_StatusReport(PDDI_MEDIA_CONTEXT mediaCtx, CodechalDecode *dec (tempNewReport.m_codecStatus == CODECHAL_STATUS_INCOMPLETE) || (tempNewReport.m_codecStatus == CODECHAL_STATUS_RESET)) { - mediaCtx->pSurfaceHeap->lock.lock_shared(); + mediaCtx->pSurfaceHeap->lock->lock_shared(); PDDI_MEDIA_SURFACE_HEAP_ELEMENT mediaSurfaceHeapElmt = (PDDI_MEDIA_SURFACE_HEAP_ELEMENT)mediaCtx->pSurfaceHeap->pHeapBase; uint32_t j = 0; @@ -423,7 +423,7 @@ VAStatus DdiDecode_StatusReport(PDDI_MEDIA_CONTEXT mediaCtx, CodechalDecode *dec break; } } - mediaCtx->pSurfaceHeap->lock.unlock_shared(); + mediaCtx->pSurfaceHeap->lock->unlock_shared(); } else { @@ -490,7 +490,7 @@ VAStatus DdiDecode_StatusReport(PDDI_MEDIA_CONTEXT mediaCtx, DecodePipelineAdapt (tempNewReport.codecStatus == CODECHAL_STATUS_INCOMPLETE) || (tempNewReport.codecStatus == CODECHAL_STATUS_RESET)) { - mediaCtx->pSurfaceHeap->lock.lock_shared(); + mediaCtx->pSurfaceHeap->lock->lock_shared(); PDDI_MEDIA_SURFACE_HEAP_ELEMENT mediaSurfaceHeapElmt = (PDDI_MEDIA_SURFACE_HEAP_ELEMENT)mediaCtx->pSurfaceHeap->pHeapBase; uint32_t j = 0; @@ -505,7 +505,7 @@ VAStatus DdiDecode_StatusReport(PDDI_MEDIA_CONTEXT mediaCtx, DecodePipelineAdapt break; } } - mediaCtx->pSurfaceHeap->lock.unlock_shared(); + mediaCtx->pSurfaceHeap->lock->unlock_shared(); } else { diff --git a/media_driver/linux/common/ddi/media_libva.cpp b/media_driver/linux/common/ddi/media_libva.cpp index 393e26936d..03908e99a1 100755 --- a/media_driver/linux/common/ddi/media_libva.cpp +++ b/media_driver/linux/common/ddi/media_libva.cpp @@ -309,13 +309,11 @@ static uint32_t DdiMedia_CreateRenderTarget( return VA_INVALID_ID; } - surfaceElement->lock.lock(); surfaceElement->pSurface = (DDI_MEDIA_SURFACE *)MOS_AllocAndZeroMemory(sizeof(DDI_MEDIA_SURFACE)); if (nullptr == surfaceElement->pSurface) { DdiMediaUtil_ReleasePMediaSurfaceFromHeap(mediaDrvCtx->pSurfaceHeap, surfaceElement->uiVaSurfaceID); //DdiMediaUtil_UnLockMutex(&mediaDrvCtx->SurfaceMutex); - surfaceElement->lock.unlock(); return VA_INVALID_ID; } @@ -328,19 +326,19 @@ static uint32_t DdiMedia_CreateRenderTarget( surfaceElement->pSurface->uiLockedImageID = VA_INVALID_ID; surfaceElement->pSurface->surfaceUsageHint= surfaceUsageHint; surfaceElement->pSurface->memType = memType; + surfaceElement->pSurface->lock = MOS_New(std::shared_timed_mutex); if(DdiMediaUtil_CreateSurface(surfaceElement->pSurface, mediaDrvCtx)!= VA_STATUS_SUCCESS) { + MOS_Delete(surfaceElement->pSurface->lock); MOS_FreeMemory(surfaceElement->pSurface); DdiMediaUtil_ReleasePMediaSurfaceFromHeap(mediaDrvCtx->pSurfaceHeap, surfaceElement->uiVaSurfaceID); //DdiMediaUtil_UnLockMutex(&mediaDrvCtx->SurfaceMutex); - surfaceElement->lock.unlock(); return VA_INVALID_ID; } mediaDrvCtx->uiNumSurfaces++; uint32_t surfaceID = surfaceElement->uiVaSurfaceID; - surfaceElement->lock.unlock(); //DdiMediaUtil_UnLockMutex(&mediaDrvCtx->SurfaceMutex); return surfaceID; } @@ -1004,11 +1002,11 @@ void* DdiMedia_GetCtxFromVABufferID (PDDI_MEDIA_CONTEXT mediaCtx, VABufferID buf uint32_t i = (uint32_t)bufferID; DDI_CHK_LESS(i, mediaCtx->pBufferHeap->uiAllocatedHeapElements, "invalid buffer id", nullptr); //DdiMediaUtil_LockMutex(&mediaCtx->BufferMutex); - mediaCtx->pBufferHeap->lock.lock_shared(); + mediaCtx->pBufferHeap->lock->lock_shared(); PDDI_MEDIA_BUFFER_HEAP_ELEMENT bufHeapElement = (PDDI_MEDIA_BUFFER_HEAP_ELEMENT)mediaCtx->pBufferHeap->pHeapBase; bufHeapElement += i; void *temp = bufHeapElement->pCtx; - mediaCtx->pBufferHeap->lock.unlock_shared(); + mediaCtx->pBufferHeap->lock->unlock_shared(); //DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex); return temp; @@ -1032,11 +1030,11 @@ uint32_t DdiMedia_GetCtxTypeFromVABufferID (PDDI_MEDIA_CONTEXT mediaCtx, VABuffe uint32_t i = (uint32_t)bufferID; DDI_CHK_LESS(i, mediaCtx->pBufferHeap->uiAllocatedHeapElements, "invalid buffer id", DDI_MEDIA_CONTEXT_TYPE_NONE); //DdiMediaUtil_LockMutex(&mediaCtx->BufferMutex); - mediaCtx->pBufferHeap->lock.lock_shared(); + mediaCtx->pBufferHeap->lock->lock_shared(); PDDI_MEDIA_BUFFER_HEAP_ELEMENT bufHeapElement = (PDDI_MEDIA_BUFFER_HEAP_ELEMENT)mediaCtx->pBufferHeap->pHeapBase; bufHeapElement += i; uint32_t ctxType = bufHeapElement->uiCtxType; - mediaCtx->pBufferHeap->lock.unlock_shared(); + mediaCtx->pBufferHeap->lock->unlock_shared(); //DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex); return ctxType; @@ -1344,12 +1342,14 @@ VAStatus DdiMedia_MediaMemoryDecompress(PDDI_MEDIA_CONTEXT mediaCtx, DDI_MEDIA_S else { //DdiMediaUtil_LockMutex(&mediaCtx->SurfaceMutex); - //DdiMediaUtil_LockMutex(&mediaCtx->MemDecompMutex); + mediaSurface->lock->lock(); + DdiMediaUtil_LockMutex(&mediaCtx->MemDecompMutex); - //Note: find surfaceElement from pSurface and surfaceElement->lock.lock_shared(); + //Note: find surfaceElement from pSurface and surfaceElement->lock->lock_shared(); DdiMedia_MediaSurfaceToMosResource(mediaSurface, &surface); DdiMedia_MediaMemoryDecompressInternal(&mosCtx, &surface); - //DdiMediaUtil_UnLockMutex(&mediaCtx->MemDecompMutex); + DdiMediaUtil_UnLockMutex(&mediaCtx->MemDecompMutex); + mediaSurface->lock->unlock(); //DdiMediaUtil_UnLockMutex(&mediaCtx->SurfaceMutex); if (pCpDdiInterface) @@ -1392,10 +1392,12 @@ static VAStatus DdiMedia_HeapInitialize( mediaCtx->pSurfaceHeap = (DDI_MEDIA_HEAP *)MOS_AllocAndZeroMemory(sizeof(DDI_MEDIA_HEAP)); DDI_CHK_NULL(mediaCtx->pSurfaceHeap, "nullptr pSurfaceHeap", VA_STATUS_ERROR_ALLOCATION_FAILED); mediaCtx->pSurfaceHeap->uiHeapElementSize = sizeof(DDI_MEDIA_SURFACE_HEAP_ELEMENT); + mediaCtx->pSurfaceHeap->lock = MOS_New(std::shared_timed_mutex); mediaCtx->pBufferHeap = (DDI_MEDIA_HEAP *)MOS_AllocAndZeroMemory(sizeof(DDI_MEDIA_HEAP)); DDI_CHK_NULL(mediaCtx->pBufferHeap, "nullptr BufferHeap", VA_STATUS_ERROR_ALLOCATION_FAILED); mediaCtx->pBufferHeap->uiHeapElementSize = sizeof(DDI_MEDIA_BUFFER_HEAP_ELEMENT); + mediaCtx->pBufferHeap->lock = MOS_New(std::shared_timed_mutex); mediaCtx->pImageHeap = (DDI_MEDIA_HEAP *)MOS_AllocAndZeroMemory(sizeof(DDI_MEDIA_HEAP)); DDI_CHK_NULL(mediaCtx->pImageHeap, "nullptr ImageHeap", VA_STATUS_ERROR_ALLOCATION_FAILED); @@ -1453,9 +1455,17 @@ static VAStatus DdiMedia_HeapDestroy( { DDI_CHK_NULL(mediaCtx, "nullptr ctx", VA_STATUS_ERROR_INVALID_CONTEXT); // destroy heaps + if (mediaCtx->pSurfaceHeap->lock) + { + MOS_Delete(mediaCtx->pSurfaceHeap->lock); + } MOS_FreeMemory(mediaCtx->pSurfaceHeap->pHeapBase); MOS_FreeMemory(mediaCtx->pSurfaceHeap); + if (mediaCtx->pBufferHeap->lock) + { + MOS_Delete(mediaCtx->pBufferHeap->lock); + } MOS_FreeMemory(mediaCtx->pBufferHeap->pHeapBase); MOS_FreeMemory(mediaCtx->pBufferHeap); @@ -2563,12 +2573,13 @@ VAStatus DdiMedia_DestroySurfaces ( } //DdiMediaUtil_LockMutex(&mediaCtx->SurfaceMutex); - surfaceElement->lock.lock(); + //surface->lock->lock(); DdiMediaUtil_FreeSurface(surface); + //surface->lock->unlock(); + //MOS_Delete(surface->lock); MOS_FreeMemory(surface); DdiMediaUtil_ReleasePMediaSurfaceFromHeap(mediaCtx->pSurfaceHeap, (uint32_t)surfaces[i]); mediaCtx->uiNumSurfaces--; - surfaceElement->lock.unlock(); //DdiMediaUtil_UnLockMutex(&mediaCtx->SurfaceMutex); } @@ -3587,9 +3598,9 @@ VAStatus DdiMedia_MapBufferInternal ( break; case VAEncMacroblockMapBufferType: //DdiMediaUtil_LockMutex(&mediaCtx->BufferMutex); - bufHeapElement->lock.lock(); + buf->lock->lock(); *pbuf = DdiMediaUtil_LockBuffer(buf, flag); - bufHeapElement->lock.unlock(); + buf->lock->unlock(); //DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex); MOS_TraceEventExt(EVENT_VA_MAP, EVENT_TYPE_END, nullptr, 0, nullptr, 0); if (nullptr == (*pbuf)) @@ -3619,13 +3630,15 @@ VAStatus DdiMedia_MapBufferInternal ( if((buf->format != Media_Format_CPU) && (DdiMedia_MediaFormatToOsFormat(buf->format) != VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT)) { //DdiMediaUtil_LockMutex(&mediaCtx->BufferMutex); - bufHeapElement->lock.lock(); + buf->lock->lock(); // A critical section starts. // Make sure not to bailout with a return until the section ends. if (nullptr != buf->pSurface && Media_Format_CPU != buf->format) { + buf->pSurface->lock->lock(); vaStatus = DdiMedia_MediaMemoryDecompress(mediaCtx, buf->pSurface); + buf->pSurface->lock->unlock(); } if (VA_STATUS_SUCCESS == vaStatus) @@ -3639,7 +3652,7 @@ VAStatus DdiMedia_MapBufferInternal ( } // The critical section ends. - bufHeapElement->lock.unlock(); + buf->lock->unlock(); //DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex); } else @@ -3659,6 +3672,7 @@ VAStatus DdiMedia_MapBuffer ( void **pbuf ) { + //printf(">>>>>>>vaMap buf: %d\n", buf_id); return DdiMedia_MapBufferInternal(ctx, buf_id, pbuf, MOS_LOCKFLAG_READONLY | MOS_LOCKFLAG_WRITEONLY); } @@ -3753,9 +3767,9 @@ VAStatus DdiMedia_UnmapBuffer ( if((buf->format != Media_Format_CPU) &&(DdiMedia_MediaFormatToOsFormat(buf->format) != VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT)) { //DdiMediaUtil_LockMutex(&mediaCtx->BufferMutex); - bufHeapElement->lock.lock(); + buf->lock->lock(); DdiMediaUtil_UnlockBuffer(buf); - bufHeapElement->lock.unlock(); + buf->lock->unlock(); //DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex); } break; @@ -3770,6 +3784,7 @@ VAStatus DdiMedia_DestroyBuffer ( VABufferID buffer_id ) { + //printf(">>>>>>>vaDestroy buf: %d\n", buffer_id); DDI_FUNCTION_ENTER(); MOS_TraceEventExt(EVENT_VA_FREE_BUFFER, EVENT_TYPE_START, &buffer_id, sizeof(buffer_id), nullptr, 0); @@ -3984,14 +3999,14 @@ VAStatus DdiMedia_BeginPicture ( DDI_CHK_NULL(surface, "nullptr surface", VA_STATUS_ERROR_INVALID_SURFACE); //DdiMediaUtil_LockMutex(&mediaCtx->SurfaceMutex); - surfaceElement->lock.lock(); + surface->lock->lock(); surface->curCtxType = ctxType; surface->curStatusReportQueryState = DDI_MEDIA_STATUS_REPORT_QUERY_STATE_PENDING; if(ctxType == DDI_MEDIA_CONTEXT_TYPE_VP) { surface->curStatusReport.vpp.status = VPREP_NOTAVAILABLE; } - surfaceElement->lock.unlock(); + surface->lock->unlock(); //DdiMediaUtil_UnLockMutex(&mediaCtx->SurfaceMutex); switch (ctxType) @@ -4103,7 +4118,7 @@ static VAStatus DdiMedia_StatusCheck ( PDDI_MEDIA_SURFACE_HEAP_ELEMENT surfaceElement = DdiMedia_GetSurfaceElementFromVASurfaceID (mediaCtx, surface_id); DDI_CHK_NULL(surfaceElement , "nullptr surfaceElement", VA_STATUS_ERROR_INVALID_SURFACE); //DdiMediaUtil_LockGuard guard(&mediaCtx->SurfaceMutex); - DdiMediaUtil_LockGuard2 guard(&surfaceElement->lock); + DdiMediaUtil_LockGuard2 guard(surface->lock); Codechal *codecHal = decCtx->pCodecHal; //return success just avoid vaDestroyContext is ahead of vaSyncSurface @@ -4446,7 +4461,7 @@ VAStatus DdiMedia_QuerySurfaceError( VAStatus vaStatus = VA_STATUS_SUCCESS; //DdiMediaUtil_LockMutex(&mediaCtx->SurfaceMutex); - surfaceElement->lock.lock_shared(); + surface->lock->lock_shared(); if (surface->curStatusReportQueryState == DDI_MEDIA_STATUS_REPORT_QUERY_STATE_COMPLETED) { if (error_status != -1 && surface->curCtxType == DDI_MEDIA_CONTEXT_TYPE_DECODER) @@ -4465,7 +4480,7 @@ VAStatus DdiMedia_QuerySurfaceError( surfaceErrors[0].decode_error_type = VADecodeMBError; #endif *error_info = surfaceErrors; - surfaceElement->lock.unlock_shared(); + surface->lock->unlock_shared(); //DdiMediaUtil_UnLockMutex(&mediaCtx->SurfaceMutex); return VA_STATUS_SUCCESS; } @@ -4478,7 +4493,7 @@ VAStatus DdiMedia_QuerySurfaceError( surfaceErrors[0].status = 1; surfaceErrors[0].decode_error_type = VADecodeReset; *error_info = surfaceErrors; - surfaceElement->lock.unlock_shared(); + surface->lock->unlock_shared(); //DdiMediaUtil_UnLockMutex(&mediaCtx->SurfaceMutex); return VA_STATUS_SUCCESS; } @@ -4507,7 +4522,7 @@ VAStatus DdiMedia_QuerySurfaceError( } } - surfaceElement->lock.unlock_shared(); + surface->lock->unlock_shared(); //DdiMediaUtil_UnLockMutex(&mediaCtx->SurfaceMutex); return vaStatus; } @@ -4515,14 +4530,14 @@ VAStatus DdiMedia_QuerySurfaceError( if (surface->curCtxType == DDI_MEDIA_CONTEXT_TYPE_VP && surface->curStatusReport.vpp.status == CODECHAL_STATUS_ERROR) { - surfaceElement->lock.unlock_shared(); + surface->lock->unlock_shared(); //DdiMediaUtil_UnLockMutex(&mediaCtx->SurfaceMutex); return VA_STATUS_SUCCESS; } } surfaceErrors[0].status = -1; - surfaceElement->lock.unlock_shared(); + surface->lock->unlock_shared(); //DdiMediaUtil_UnLockMutex(&mediaCtx->SurfaceMutex); return VA_STATUS_SUCCESS; } @@ -4784,12 +4799,14 @@ VAStatus DdiMedia_CreateImage( buf->format = Media_Format_CPU;//DdiCodec_OsFormatToMediaFormat(vaimg->format.fourcc); //Media_Format_Buffer; buf->uiOffset = 0; buf->pMediaCtx = mediaCtx; + buf->lock = MOS_New(std::shared_timed_mutex); //Put Image in untiled buffer for better CPU access? VAStatus status= DdiMediaUtil_CreateBuffer(buf, mediaCtx->pDrmBufMgr); if((status != VA_STATUS_SUCCESS)) { MOS_FreeMemory(vaimg); + MOS_Delete(buf->lock); MOS_FreeMemory(buf); return status; } @@ -4803,18 +4820,17 @@ VAStatus DdiMedia_CreateImage( //DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex); MOS_FreeMemory(vaimg); DdiMediaUtil_FreeBuffer(buf); + //MOS_Delete(buf->lock); MOS_FreeMemory(buf); return VA_STATUS_ERROR_MAX_NUM_EXCEEDED; } - bufferHeapElement->lock.lock(); bufferHeapElement->pBuffer = buf; bufferHeapElement->pCtx = nullptr; bufferHeapElement->uiCtxType = DDI_MEDIA_CONTEXT_TYPE_MEDIA; vaimg->buf = bufferHeapElement->uiVaBufferID; mediaCtx->uiNumBufs++; - bufferHeapElement->lock.unlock(); //DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex); DdiMediaUtil_LockMutex(&mediaCtx->ImageMutex); @@ -5118,6 +5134,8 @@ VAStatus DdiMedia_DeriveImage ( buf->format = mediaSurface->format; buf->TileType = mediaSurface->TileType; buf->pSurface = mediaSurface; + buf->lock = MOS_New(std::shared_timed_mutex); + //buf->lock = mediaSurface->lock; mos_bo_reference(mediaSurface->bo); //DdiMediaUtil_LockMutex(&mediaCtx->BufferMutex); @@ -5127,17 +5145,16 @@ VAStatus DdiMedia_DeriveImage ( { //DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex); MOS_FreeMemory(vaimg); + MOS_Delete(buf->lock); MOS_FreeMemory(buf); return VA_STATUS_ERROR_MAX_NUM_EXCEEDED; } - bufferHeapElement->lock.lock(); bufferHeapElement->pBuffer = buf; bufferHeapElement->pCtx = nullptr; bufferHeapElement->uiCtxType = DDI_MEDIA_CONTEXT_TYPE_MEDIA; vaimg->buf = bufferHeapElement->uiVaBufferID; mediaCtx->uiNumBufs++; - bufferHeapElement->lock.unlock(); //DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex); *image = *vaimg; @@ -6852,14 +6869,14 @@ VAStatus DdiMedia_AcquireBufferHandle( } //DdiMediaUtil_LockMutex(&mediaCtx->BufferMutex); - bufHeapElement->lock.lock(); + buf->lock->lock(); // already acquired? if (buf->uiExportcount) { // yes, already acquired // can't provide access thru another memtype if (buf->uiMemtype != buf_info->mem_type) { - bufHeapElement->lock.unlock(); + buf->lock->unlock(); //DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex); return VA_STATUS_ERROR_INVALID_PARAMETER; } @@ -6871,7 +6888,7 @@ VAStatus DdiMedia_AcquireBufferHandle( uint32_t flink = 0; if (mos_bo_flink(buf->bo, &flink) != 0) { - bufHeapElement->lock.unlock(); + buf->lock->unlock(); //DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex); return VA_STATUS_ERROR_INVALID_BUFFER; } @@ -6882,7 +6899,7 @@ VAStatus DdiMedia_AcquireBufferHandle( int32_t prime_fd = 0; if (mos_bo_export_to_prime(buf->bo, &prime_fd) != 0) { - bufHeapElement->lock.unlock(); + buf->lock->unlock(); //DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex); return VA_STATUS_ERROR_INVALID_BUFFER; } @@ -6902,7 +6919,7 @@ VAStatus DdiMedia_AcquireBufferHandle( buf_info->handle = buf->handle; buf_info->mem_size = buf->uiNumElements * buf->iSize; - bufHeapElement->lock.unlock(); + buf->lock->unlock(); //DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex); return VA_STATUS_SUCCESS; } @@ -6936,10 +6953,10 @@ VAStatus DdiMedia_ReleaseBufferHandle( DDI_CHK_NULL(buf->bo, "Invalid Media Buffer", VA_STATUS_ERROR_INVALID_BUFFER); //DdiMediaUtil_LockMutex(&mediaCtx->BufferMutex); - bufHeapElement->lock.lock(); + buf->lock->lock(); if (!buf->uiMemtype || !buf->uiExportcount) { - bufHeapElement->lock.unlock(); + buf->lock->unlock(); //DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex); return VA_STATUS_SUCCESS; } @@ -6955,7 +6972,7 @@ VAStatus DdiMedia_ReleaseBufferHandle( } buf->uiMemtype = 0; } - bufHeapElement->lock.unlock(); + buf->lock->unlock(); //DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex); if (!buf->uiExportcount && buf->bPostponedBufFree) { diff --git a/media_driver/linux/common/ddi/media_libva_common.cpp b/media_driver/linux/common/ddi/media_libva_common.cpp index cc5b2b6a95..6378dd4e9f 100644 --- a/media_driver/linux/common/ddi/media_libva_common.cpp +++ b/media_driver/linux/common/ddi/media_libva_common.cpp @@ -143,11 +143,11 @@ PDDI_MEDIA_SURFACE_HEAP_ELEMENT DdiMedia_GetSurfaceElementFromVASurfaceID (PDDI_ if(validSurface) { DDI_CHK_LESS(i, mediaCtx->pSurfaceHeap->uiAllocatedHeapElements, "invalid surface id", nullptr); - mediaCtx->pSurfaceHeap->lock.lock_shared(); + mediaCtx->pSurfaceHeap->lock->lock(); //DdiMediaUtil_LockMutex(&mediaCtx->SurfaceMutex); surfaceElement = (PDDI_MEDIA_SURFACE_HEAP_ELEMENT)mediaCtx->pSurfaceHeap->pHeapBase; surfaceElement += i; - mediaCtx->pSurfaceHeap->lock.unlock_shared(); + mediaCtx->pSurfaceHeap->lock->unlock(); //DdiMediaUtil_UnLockMutex(&mediaCtx->SurfaceMutex); } @@ -169,18 +169,18 @@ VASurfaceID DdiMedia_GetVASurfaceIDFromSurface(PDDI_MEDIA_SURFACE surface) { DDI_CHK_NULL(surface, "nullptr surface", VA_INVALID_SURFACE); - surface->pMediaCtx->pSurfaceHeap->lock.lock_shared(); + surface->pMediaCtx->pSurfaceHeap->lock->lock(); PDDI_MEDIA_SURFACE_HEAP_ELEMENT surfaceElement = (PDDI_MEDIA_SURFACE_HEAP_ELEMENT)surface->pMediaCtx->pSurfaceHeap->pHeapBase; for(uint32_t i = 0; i < surface->pMediaCtx->pSurfaceHeap->uiAllocatedHeapElements; i ++) { if(surface == surfaceElement->pSurface) { - surface->pMediaCtx->pSurfaceHeap->lock.unlock_shared(); + surface->pMediaCtx->pSurfaceHeap->lock->unlock(); return surfaceElement->uiVaSurfaceID; } surfaceElement ++; } - surface->pMediaCtx->pSurfaceHeap->lock.unlock_shared(); + surface->pMediaCtx->pSurfaceHeap->lock->unlock(); return VA_INVALID_SURFACE; } @@ -235,7 +235,7 @@ PDDI_MEDIA_SURFACE DdiMedia_ReplaceSurfaceWithNewFormat(PDDI_MEDIA_SURFACE surfa //lock surface heap //DdiMediaUtil_LockMutex(&mediaCtx->SurfaceMutex); - mediaCtx->pSurfaceHeap->lock.lock_shared(); + mediaCtx->pSurfaceHeap->lock->lock(); uint32_t i; //get current element heap and index for(i = 0; i < mediaCtx->pSurfaceHeap->uiAllocatedHeapElements; i ++) @@ -249,7 +249,7 @@ PDDI_MEDIA_SURFACE DdiMedia_ReplaceSurfaceWithNewFormat(PDDI_MEDIA_SURFACE surfa //if cant find if(i == surface->pMediaCtx->pSurfaceHeap->uiAllocatedHeapElements) { - mediaCtx->pSurfaceHeap->lock.unlock_shared(); + mediaCtx->pSurfaceHeap->lock->unlock(); //DdiMediaUtil_UnLockMutex(&mediaCtx->SurfaceMutex); if(dstSurface->pShadowBuffer) { @@ -266,7 +266,7 @@ PDDI_MEDIA_SURFACE DdiMedia_ReplaceSurfaceWithNewFormat(PDDI_MEDIA_SURFACE surfa DdiMediaUtil_FreeSurface(surface); MOS_FreeMemory(surface); - mediaCtx->pSurfaceHeap->lock.unlock_shared(); + mediaCtx->pSurfaceHeap->lock->unlock(); //DdiMediaUtil_UnLockMutex(&mediaCtx->SurfaceMutex); return dstSurface; @@ -293,9 +293,9 @@ PDDI_MEDIA_SURFACE DdiMedia_ReplaceSurfaceWithVariant(PDDI_MEDIA_SURFACE surface } //DdiMediaUtil_LockMutex(&mediaCtx->BufferMutex); - surface->pMediaCtx->pSurfaceHeap->lock.lock_shared(); + surface->pMediaCtx->pSurfaceHeap->lock->lock(); PDDI_MEDIA_SURFACE_HEAP_ELEMENT surfaceElement = (PDDI_MEDIA_SURFACE_HEAP_ELEMENT)surface->pMediaCtx->pSurfaceHeap->pHeapBase; - surface->pMediaCtx->pSurfaceHeap->lock.unlock_shared(); + surface->pMediaCtx->pSurfaceHeap->lock->unlock(); //DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex); if (nullptr == surfaceElement) { @@ -388,10 +388,10 @@ PDDI_MEDIA_BUFFER_HEAP_ELEMENT DdiMedia_GetBufferElementFromVABufferID (PDDI_MED i = (uint32_t)bufferID; DDI_CHK_LESS(i, mediaCtx->pBufferHeap->uiAllocatedHeapElements, "invalid buffer id", nullptr); //DdiMediaUtil_LockMutex(&mediaCtx->BufferMutex); - mediaCtx->pBufferHeap->lock.lock_shared(); + mediaCtx->pBufferHeap->lock->lock(); bufHeapElement = (PDDI_MEDIA_BUFFER_HEAP_ELEMENT)mediaCtx->pBufferHeap->pHeapBase; bufHeapElement += i; - mediaCtx->pBufferHeap->lock.unlock_shared(); + mediaCtx->pBufferHeap->lock->unlock(); //DdiMediaUtil_UnLockMutex(&mediaCtx->BufferMutex); return bufHeapElement; diff --git a/media_driver/linux/common/ddi/media_libva_util.cpp b/media_driver/linux/common/ddi/media_libva_util.cpp index b63ee4d30a..c0a6fdbb70 100755 --- a/media_driver/linux/common/ddi/media_libva_util.cpp +++ b/media_driver/linux/common/ddi/media_libva_util.cpp @@ -1182,6 +1182,7 @@ void* DdiMediaUtil_LockSurface(DDI_MEDIA_SURFACE* surface, uint32_t flag) { DDI_CHK_NULL(surface, "nullptr surface", nullptr); DDI_CHK_NULL(surface->pMediaCtx, "nullptr surface->pMediaCtx", nullptr); + surface->lock->lock(); if (MEDIA_IS_SKU(&surface->pMediaCtx->SkuTable, FtrLocalMemory)) { if ((MosUtilities::MosAtomicIncrement(&surface->iRefCount) == 1) && (false == surface->bMapped)) @@ -1205,6 +1206,7 @@ void* DdiMediaUtil_LockSurface(DDI_MEDIA_SURFACE* surface, uint32_t flag) } surface->iRefCount++; } + surface->lock->unlock(); return surface->pData; @@ -1496,6 +1498,8 @@ void DdiMediaUtil_FreeSurface(DDI_MEDIA_SURFACE *surface) DDI_CHK_NULL(surface->pMediaCtx, "nullptr surface->pMediaCtx", ); DDI_CHK_NULL(surface->pMediaCtx->pGmmClientContext, "nullptr surface->pMediaCtx->pGmmClientContext", ); + if (surface->lock) + surface->lock->lock(); // Unmap Aux mapping if the surface was mapped if (surface->pMediaCtx->m_auxTableMgr) { @@ -1528,6 +1532,12 @@ void DdiMediaUtil_FreeSurface(DDI_MEDIA_SURFACE *surface) surface->pMediaCtx->pGmmClientContext->DestroyResInfoObject(surface->pGmmResourceInfo); surface->pGmmResourceInfo = nullptr; } + + if (surface->lock) + { + surface->lock->unlock(); + MOS_Delete(surface->lock); + } } @@ -1536,6 +1546,10 @@ void DdiMediaUtil_FreeBuffer(DDI_MEDIA_BUFFER *buf) { DDI_CHK_NULL(buf, "nullptr", ); // calling sequence checking + if (buf->lock) + { + buf->lock->lock(); + } if (buf->bMapped) { DdiMediaUtil_UnlockBuffer(buf); @@ -1557,6 +1571,12 @@ void DdiMediaUtil_FreeBuffer(DDI_MEDIA_BUFFER *buf) buf->pMediaCtx->pGmmClientContext->DestroyResInfoObject(buf->pGmmResourceInfo); buf->pGmmResourceInfo = nullptr; } + + if (buf->lock) + { + buf->lock->unlock(); + MOS_Delete(buf->lock); + } } /////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -1650,14 +1670,14 @@ PDDI_MEDIA_SURFACE_HEAP_ELEMENT DdiMediaUtil_AllocPMediaSurfaceFromHeap(PDDI_MED PDDI_MEDIA_SURFACE_HEAP_ELEMENT mediaSurfaceHeapElmt = nullptr; - surfaceHeap->lock.lock(); + surfaceHeap->lock->lock(); if (nullptr == surfaceHeap->pFirstFreeHeapElement) { void *newHeapBase = MOS_ReallocMemory(surfaceHeap->pHeapBase, (surfaceHeap->uiAllocatedHeapElements + DDI_MEDIA_HEAP_INCREMENTAL_SIZE) * sizeof(DDI_MEDIA_SURFACE_HEAP_ELEMENT)); if (nullptr == newHeapBase) { - surfaceHeap->lock.unlock(); + surfaceHeap->lock->unlock(); DDI_ASSERTMESSAGE("DDI: realloc failed."); return nullptr; } @@ -1676,7 +1696,7 @@ PDDI_MEDIA_SURFACE_HEAP_ELEMENT DdiMediaUtil_AllocPMediaSurfaceFromHeap(PDDI_MED mediaSurfaceHeapElmt = (PDDI_MEDIA_SURFACE_HEAP_ELEMENT)surfaceHeap->pFirstFreeHeapElement; surfaceHeap->pFirstFreeHeapElement = mediaSurfaceHeapElmt->pNextFree; - surfaceHeap->lock.unlock(); + surfaceHeap->lock->unlock(); return mediaSurfaceHeapElmt; } @@ -1690,14 +1710,14 @@ void DdiMediaUtil_ReleasePMediaSurfaceFromHeap(PDDI_MEDIA_HEAP surfaceHeap, uint PDDI_MEDIA_SURFACE_HEAP_ELEMENT mediaSurfaceHeapBase = (PDDI_MEDIA_SURFACE_HEAP_ELEMENT)surfaceHeap->pHeapBase; DDI_CHK_NULL(mediaSurfaceHeapBase, "nullptr mediaSurfaceHeapBase", ); - surfaceHeap->lock.lock(); + surfaceHeap->lock->lock(); PDDI_MEDIA_SURFACE_HEAP_ELEMENT mediaSurfaceHeapElmt = &mediaSurfaceHeapBase[vaSurfaceID]; DDI_CHK_NULL(mediaSurfaceHeapElmt->pSurface, "surface is already released", ); void *firstFree = surfaceHeap->pFirstFreeHeapElement; surfaceHeap->pFirstFreeHeapElement = (void*)mediaSurfaceHeapElmt; mediaSurfaceHeapElmt->pNextFree = (PDDI_MEDIA_SURFACE_HEAP_ELEMENT)firstFree; mediaSurfaceHeapElmt->pSurface = nullptr; - surfaceHeap->lock.unlock(); + surfaceHeap->lock->unlock(); } @@ -1706,7 +1726,7 @@ PDDI_MEDIA_BUFFER_HEAP_ELEMENT DdiMediaUtil_AllocPMediaBufferFromHeap(PDDI_MEDIA DDI_CHK_NULL(bufferHeap, "nullptr bufferHeap", nullptr); PDDI_MEDIA_BUFFER_HEAP_ELEMENT mediaBufferHeapElmt = nullptr; - bufferHeap->lock.lock(); + bufferHeap->lock->lock(); if (nullptr == bufferHeap->pFirstFreeHeapElement) { void *newHeapBase = MOS_ReallocMemory(bufferHeap->pHeapBase, (bufferHeap->uiAllocatedHeapElements + DDI_MEDIA_HEAP_INCREMENTAL_SIZE) * sizeof(DDI_MEDIA_BUFFER_HEAP_ELEMENT)); @@ -1729,7 +1749,7 @@ PDDI_MEDIA_BUFFER_HEAP_ELEMENT DdiMediaUtil_AllocPMediaBufferFromHeap(PDDI_MEDIA mediaBufferHeapElmt = (PDDI_MEDIA_BUFFER_HEAP_ELEMENT)bufferHeap->pFirstFreeHeapElement; bufferHeap->pFirstFreeHeapElement = mediaBufferHeapElmt->pNextFree; - bufferHeap->lock.unlock(); + bufferHeap->lock->unlock(); return mediaBufferHeapElmt; } @@ -1739,7 +1759,7 @@ void DdiMediaUtil_ReleasePMediaBufferFromHeap(PDDI_MEDIA_HEAP bufferHeap, uint32 DDI_CHK_NULL(bufferHeap, "nullptr bufferHeap", ); DDI_CHK_LESS(vaBufferID, bufferHeap->uiAllocatedHeapElements, "invalid buffer id", ); - bufferHeap->lock.lock(); + bufferHeap->lock->lock(); PDDI_MEDIA_BUFFER_HEAP_ELEMENT mediaBufferHeapBase = (PDDI_MEDIA_BUFFER_HEAP_ELEMENT)bufferHeap->pHeapBase; PDDI_MEDIA_BUFFER_HEAP_ELEMENT mediaBufferHeapElmt = &mediaBufferHeapBase[vaBufferID]; DDI_CHK_NULL(mediaBufferHeapElmt->pBuffer, "buffer is already released", ); @@ -1747,7 +1767,7 @@ void DdiMediaUtil_ReleasePMediaBufferFromHeap(PDDI_MEDIA_HEAP bufferHeap, uint32 bufferHeap->pFirstFreeHeapElement = (void*)mediaBufferHeapElmt; mediaBufferHeapElmt->pNextFree = (PDDI_MEDIA_BUFFER_HEAP_ELEMENT)firstFree; mediaBufferHeapElmt->pBuffer = nullptr; - bufferHeap->lock.unlock(); + bufferHeap->lock->unlock(); } PDDI_MEDIA_IMAGE_HEAP_ELEMENT DdiMediaUtil_AllocPVAImageFromHeap(PDDI_MEDIA_HEAP imageHeap) diff --git a/media_driver/linux/common/vp/ddi/media_libva_vp.c b/media_driver/linux/common/vp/ddi/media_libva_vp.c index dcff2555a7..cb7fff40b2 100644 --- a/media_driver/linux/common/vp/ddi/media_libva_vp.c +++ b/media_driver/linux/common/vp/ddi/media_libva_vp.c @@ -3069,6 +3069,7 @@ VAStatus DdiVp_CreateBuffer( pBuf->format = Media_Format_Buffer; pBuf->uiOffset = 0; pBuf->pData = (uint8_t*)MOS_AllocAndZeroMemory(uiSize * uiNumElements); + pBuf->lock = MOS_New(std::shared_timed_mutex); if (nullptr == pBuf->pData) { MOS_FreeMemAndSetNull(pBuf); diff --git a/media_softlet/linux/common/ddi/media_libva_common_next.h b/media_softlet/linux/common/ddi/media_libva_common_next.h index 575b6d9819..221e1541d8 100644 --- a/media_softlet/linux/common/ddi/media_libva_common_next.h +++ b/media_softlet/linux/common/ddi/media_libva_common_next.h @@ -260,6 +260,7 @@ typedef struct _DDI_MEDIA_SURFACE uint32_t uiVariantFlag; int memType; + std::shared_timed_mutex *lock = nullptr;; } DDI_MEDIA_SURFACE, *PDDI_MEDIA_SURFACE; typedef struct _DDI_MEDIA_BUFFER @@ -291,6 +292,7 @@ typedef struct _DDI_MEDIA_BUFFER PDDI_MEDIA_SURFACE pSurface = nullptr; GMM_RESOURCE_INFO *pGmmResourceInfo = nullptr; // GMM resource descriptor PDDI_MEDIA_CONTEXT pMediaCtx = nullptr; // Media driver Context + std::shared_timed_mutex *lock = nullptr;; } DDI_MEDIA_BUFFER, *PDDI_MEDIA_BUFFER; typedef struct _DDI_MEDIA_SURFACE_HEAP_ELEMENT @@ -298,7 +300,6 @@ typedef struct _DDI_MEDIA_SURFACE_HEAP_ELEMENT PDDI_MEDIA_SURFACE pSurface; uint32_t uiVaSurfaceID; struct _DDI_MEDIA_SURFACE_HEAP_ELEMENT *pNextFree; - std::shared_timed_mutex lock; }DDI_MEDIA_SURFACE_HEAP_ELEMENT, *PDDI_MEDIA_SURFACE_HEAP_ELEMENT; typedef struct _DDI_MEDIA_BUFFER_HEAP_ELEMENT @@ -308,7 +309,6 @@ typedef struct _DDI_MEDIA_BUFFER_HEAP_ELEMENT uint32_t uiCtxType; uint32_t uiVaBufferID; struct _DDI_MEDIA_BUFFER_HEAP_ELEMENT *pNextFree; - std::shared_timed_mutex lock; }DDI_MEDIA_BUFFER_HEAP_ELEMENT, *PDDI_MEDIA_BUFFER_HEAP_ELEMENT; typedef struct _DDI_MEDIA_IMAGE_HEAP_ELEMENT @@ -331,7 +331,8 @@ typedef struct _DDI_MEDIA_HEAP uint32_t uiHeapElementSize; uint32_t uiAllocatedHeapElements; void *pFirstFreeHeapElement; - std::shared_timed_mutex lock; + //std::shared_timed_mutex lock; + std::shared_timed_mutex *lock = nullptr;; }DDI_MEDIA_HEAP, *PDDI_MEDIA_HEAP; #ifndef ANDROID