1 /*
2 * Copyright (c) 2018-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     decode_allocator.cpp
24 //! \brief    Defines the interface for decode resource allocate
25 //! \details  decode allocator will allocate and destory buffers, the caller
26 //!           can use directly
27 //!
28 
29 #include "decode_allocator.h"
30 #include "decode_utils.h"
31 #include "External/Common/GmmResourceInfoExt.h"
32 #include "External/Common/GmmCachePolicyExt.h"
33 #include "decode_utils.h"
34 #include "media_allocator.h"
35 #include "mos_os_cp_interface_specific.h"
36 #include "mos_utilities.h"
37 #include "decode_resource_array.h"
38 #include "mos_interface.h"
39 
40 namespace decode {
41 
DecodeAllocator(PMOS_INTERFACE osInterface,bool limitedLMemBar)42 DecodeAllocator::DecodeAllocator(PMOS_INTERFACE osInterface, bool limitedLMemBar) :
43     m_osInterface(osInterface), m_limitedLMemBar(limitedLMemBar)
44 {
45     m_allocator = MOS_New(Allocator, m_osInterface);
46 #if (_DEBUG || _RELEASE_INTERNAL)
47     m_forceLockable = ReadUserFeature(m_osInterface->pfnGetUserSettingInstance(m_osInterface), "ForceDecodeResourceLockable", MediaUserSetting::Group::Sequence).Get<uint32_t>();
48 #endif
49 }
50 
~DecodeAllocator()51 DecodeAllocator::~DecodeAllocator()
52 {
53     MOS_Delete(m_allocator);
54 }
55 
AllocateBuffer(const uint32_t sizeOfBuffer,const char * nameOfBuffer,ResourceUsage resUsageType,ResourceAccessReq accessReq,bool initOnAllocate,uint8_t initValue,bool bPersistent)56 MOS_BUFFER* DecodeAllocator::AllocateBuffer(
57     const uint32_t sizeOfBuffer, const char* nameOfBuffer,
58     ResourceUsage resUsageType, ResourceAccessReq accessReq,
59     bool initOnAllocate, uint8_t initValue, bool bPersistent)
60 {
61     if (!m_allocator)
62         return nullptr;
63 
64     MOS_ALLOC_GFXRES_PARAMS allocParams;
65     MOS_ZeroMemory(&allocParams, sizeof(MOS_ALLOC_GFXRES_PARAMS));
66     allocParams.Type            = MOS_GFXRES_BUFFER;
67     allocParams.TileType        = MOS_TILE_LINEAR;
68     allocParams.Format          = Format_Buffer;
69     allocParams.dwBytes         = sizeOfBuffer;
70     allocParams.pBufName        = nameOfBuffer;
71     allocParams.bIsPersistent   = bPersistent;
72     allocParams.ResUsageType    = static_cast<MOS_HW_RESOURCE_DEF>(resUsageType);
73     SetAccessRequirement(accessReq, allocParams);
74 
75     MOS_BUFFER* buffer = m_allocator->AllocateBuffer(allocParams, false, COMPONENT_Decode);
76     if (buffer == nullptr)
77     {
78         return nullptr;
79     }
80 
81     if (initOnAllocate)
82     {
83         DECODE_ASSERT(accessReq != notLockableVideoMem);
84         MOS_STATUS status = m_allocator->OsFillResource(&buffer->OsResource, sizeOfBuffer, initValue);
85         if (status != MOS_STATUS_SUCCESS)
86         {
87             DECODE_ASSERTMESSAGE("Failed to initialize buffer %s", nameOfBuffer);
88         }
89     }
90 
91     buffer->size = sizeOfBuffer;
92     buffer->name = nameOfBuffer;
93     buffer->initOnAllocate = initOnAllocate;
94     buffer->initValue = initValue;
95     buffer->bPersistent = bPersistent;
96 
97     return buffer;
98 }
99 
AllocateBufferArray(const uint32_t sizeOfBuffer,const char * nameOfBuffer,const uint32_t numberOfBuffer,ResourceUsage resUsageType,ResourceAccessReq accessReq,bool initOnAllocate,uint8_t initValue,bool bPersistent)100 BufferArray * DecodeAllocator::AllocateBufferArray(
101     const uint32_t sizeOfBuffer, const char* nameOfBuffer, const uint32_t numberOfBuffer,
102     ResourceUsage resUsageType, ResourceAccessReq accessReq,
103     bool initOnAllocate, uint8_t initValue, bool bPersistent)
104 {
105     if (!m_allocator)
106     {
107         return nullptr;
108     }
109 
110     BufferArray * bufferArray = MOS_New(BufferArray, this);
111     if (bufferArray == nullptr)
112     {
113         return nullptr;
114     }
115 
116     for (uint32_t i = 0; i < numberOfBuffer; i++)
117     {
118         MOS_BUFFER *buf = AllocateBuffer(sizeOfBuffer, nameOfBuffer, resUsageType, accessReq,
119             initOnAllocate, initValue, bPersistent);
120         bufferArray->Push(buf);
121     }
122 
123     return bufferArray;
124 }
125 
AllocateSurface(const uint32_t width,const uint32_t height,const char * nameOfSurface,MOS_FORMAT format,bool isCompressible,ResourceUsage resUsageType,ResourceAccessReq accessReq,MOS_TILE_MODE_GMM gmmTileMode)126 MOS_SURFACE* DecodeAllocator::AllocateSurface(
127     const uint32_t width, const uint32_t height, const char* nameOfSurface,
128     MOS_FORMAT format, bool isCompressible,
129     ResourceUsage resUsageType, ResourceAccessReq accessReq,
130     MOS_TILE_MODE_GMM gmmTileMode)
131 {
132     if (!m_allocator)
133     {
134         return nullptr;
135     }
136 
137     MOS_ALLOC_GFXRES_PARAMS allocParams;
138     MOS_ZeroMemory(&allocParams, sizeof(MOS_ALLOC_GFXRES_PARAMS));
139     allocParams.Type        = MOS_GFXRES_2D;
140     allocParams.TileType    = MOS_TILE_Y;
141     allocParams.Format      = format;
142     allocParams.dwWidth     = width;
143     allocParams.dwHeight    = height;
144     allocParams.dwArraySize = 1;
145     allocParams.pBufName    = nameOfSurface;
146     allocParams.bIsCompressible = isCompressible;
147     allocParams.ResUsageType = static_cast<MOS_HW_RESOURCE_DEF>(resUsageType);
148     allocParams.m_tileModeByForce = gmmTileMode;
149     SetAccessRequirement(accessReq, allocParams);
150 
151     MOS_SURFACE* surface = m_allocator->AllocateSurface(allocParams, false, COMPONENT_Decode);
152     if (surface == nullptr)
153     {
154         return nullptr;
155     }
156     if (GetSurfaceInfo(surface) != MOS_STATUS_SUCCESS)
157     {
158         DECODE_ASSERTMESSAGE("Failed to get surface informaton for %s", nameOfSurface);
159     }
160 
161     return surface;
162 }
163 
AllocateLinearSurface(const uint32_t width,const uint32_t height,const char * nameOfSurface,MOS_FORMAT format,bool isCompressible,ResourceUsage resUsageType,ResourceAccessReq accessReq,MOS_TILE_MODE_GMM gmmTileMode)164 MOS_SURFACE *DecodeAllocator::AllocateLinearSurface(
165     const uint32_t width, const uint32_t height, const char *nameOfSurface,
166     MOS_FORMAT format, bool isCompressible,
167     ResourceUsage resUsageType, ResourceAccessReq accessReq,
168     MOS_TILE_MODE_GMM gmmTileMode)
169 {
170     if (!m_allocator)
171     {
172         return nullptr;
173     }
174 
175     MOS_ALLOC_GFXRES_PARAMS allocParams;
176     MOS_ZeroMemory(&allocParams, sizeof(MOS_ALLOC_GFXRES_PARAMS));
177     allocParams.Type              = MOS_GFXRES_2D;
178     allocParams.TileType          = MOS_TILE_LINEAR;
179     allocParams.Format            = format;
180     allocParams.dwWidth           = width;
181     allocParams.dwHeight          = height;
182     allocParams.dwArraySize       = 1;
183     allocParams.pBufName          = nameOfSurface;
184     allocParams.bIsCompressible   = isCompressible;
185     allocParams.ResUsageType      = static_cast<MOS_HW_RESOURCE_DEF>(resUsageType);
186     allocParams.m_tileModeByForce = gmmTileMode;
187     SetAccessRequirement(accessReq, allocParams);
188 
189     MOS_SURFACE *surface = m_allocator->AllocateSurface(allocParams, false, COMPONENT_Decode);
190     if (surface == nullptr)
191     {
192         return nullptr;
193     }
194     if (GetSurfaceInfo(surface) != MOS_STATUS_SUCCESS)
195     {
196         DECODE_ASSERTMESSAGE("Failed to get surface informaton for %s", nameOfSurface);
197     }
198 
199     return surface;
200 }
201 
AllocateSurfaceArray(const uint32_t width,const uint32_t height,const char * nameOfSurface,const uint32_t numberOfSurface,MOS_FORMAT format,bool isCompressed,ResourceUsage resUsageType,ResourceAccessReq accessReq)202 SurfaceArray * DecodeAllocator::AllocateSurfaceArray(
203     const uint32_t width, const uint32_t height, const char* nameOfSurface,
204     const uint32_t numberOfSurface, MOS_FORMAT format, bool isCompressed,
205     ResourceUsage resUsageType, ResourceAccessReq accessReq)
206 {
207     if (!m_allocator)
208         return nullptr;
209 
210     SurfaceArray * surfaceArray = MOS_New(SurfaceArray, this);
211     if (surfaceArray == nullptr)
212     {
213         return nullptr;
214     }
215 
216     for (uint32_t i = 0; i < numberOfSurface; i++)
217     {
218         MOS_SURFACE *surface = AllocateSurface(width, height, nameOfSurface, format, isCompressed,
219             resUsageType, accessReq);
220         surfaceArray->Push(surface);
221     }
222 
223     return surfaceArray;
224 }
225 
AllocateBatchBuffer(const uint32_t sizeOfBuffer,const uint32_t numOfBuffer,ResourceAccessReq accessReq)226 PMHW_BATCH_BUFFER DecodeAllocator::AllocateBatchBuffer(
227     const uint32_t sizeOfBuffer, const uint32_t numOfBuffer, ResourceAccessReq accessReq)
228 {
229     // The default setting is lockableVideoMem
230     bool notLockable = false;
231     bool inSystemMem = false;
232 
233     // Config setting if running with limited LMem bar config or HM enabled.
234     if (m_limitedLMemBar ||
235         (m_osInterface->osCpInterface != nullptr && m_osInterface->osCpInterface->IsHMEnabled())
236     )
237     {
238         if (accessReq == notLockableVideoMem)
239         {
240             if (m_osInterface->osCpInterface != nullptr && m_osInterface->osCpInterface->IsHMEnabled())
241             {
242                 notLockable = true;
243                 inSystemMem = false;
244             }
245         }
246         else
247         {
248             // allocate batch buffer in systemMem with limited LMem bar config due to local memory limitation
249             notLockable = false;
250             inSystemMem = true;
251         }
252     }
253 
254 #if (_DEBUG || _RELEASE_INTERNAL)
255     if (m_forceLockable)
256     {
257         notLockable = 0;
258     }
259 #endif
260 
261     PMHW_BATCH_BUFFER batchBuffer = MOS_New(MHW_BATCH_BUFFER);
262     if (Mhw_AllocateBb(m_osInterface, batchBuffer, nullptr, sizeOfBuffer, numOfBuffer,
263                        notLockable, inSystemMem) != MOS_STATUS_SUCCESS)
264     {
265         MOS_Delete(batchBuffer);
266         return nullptr;
267     }
268 
269     return batchBuffer;
270 }
271 
AllocateBatchBufferArray(const uint32_t sizeOfSubBuffer,const uint32_t numOfSubBuffer,const uint32_t numberOfBatchBuffer,bool secondLevel,ResourceAccessReq accessReq)272 BatchBufferArray * DecodeAllocator::AllocateBatchBufferArray(
273     const uint32_t sizeOfSubBuffer, const uint32_t numOfSubBuffer,
274     const uint32_t numberOfBatchBuffer, bool secondLevel,
275     ResourceAccessReq accessReq)
276 {
277     if (!m_allocator)
278         return nullptr;
279 
280     BatchBufferArray * batchBufferArray = MOS_New(BatchBufferArray, this);
281     if (batchBufferArray == nullptr)
282     {
283         return nullptr;
284     }
285 
286     for (uint32_t i = 0; i < numberOfBatchBuffer; i++)
287     {
288         PMHW_BATCH_BUFFER batchBuffer = AllocateBatchBuffer(sizeOfSubBuffer, numOfSubBuffer, accessReq);
289         if (batchBuffer == nullptr)
290         {
291             continue;
292         }
293         batchBuffer->bSecondLevel = secondLevel;
294         batchBufferArray->Push(batchBuffer);
295     }
296 
297     return batchBufferArray;
298 }
299 
Lock(MOS_RESOURCE * resource,MOS_LOCK_PARAMS * lockFlag)300 void* DecodeAllocator::Lock(MOS_RESOURCE* resource, MOS_LOCK_PARAMS* lockFlag)
301 {
302     if (!m_allocator)
303         return nullptr;
304 
305     return m_allocator->Lock(resource, lockFlag);
306 }
307 
LockResourceForWrite(MOS_RESOURCE * resource)308 void* DecodeAllocator::LockResourceForWrite(MOS_RESOURCE* resource)
309 {
310     MOS_LOCK_PARAMS lockFlags;
311     MOS_ZeroMemory(&lockFlags, sizeof(MOS_LOCK_PARAMS));
312     lockFlags.WriteOnly = 1;
313 
314     if (!m_allocator)
315         return nullptr;
316 
317     return m_allocator->Lock(resource, &lockFlags);
318 }
319 
LockResourceWithNoOverwrite(MOS_RESOURCE * resource)320 void* DecodeAllocator::LockResourceWithNoOverwrite(MOS_RESOURCE* resource)
321 {
322     MOS_LOCK_PARAMS lockFlags;
323     MOS_ZeroMemory(&lockFlags, sizeof(MOS_LOCK_PARAMS));
324     lockFlags.WriteOnly   = 1;
325     lockFlags.NoOverWrite = 1;
326 
327     if (!m_allocator)
328         return nullptr;
329 
330     return m_allocator->Lock(resource, &lockFlags);
331 }
332 
LockResourceForRead(MOS_RESOURCE * resource)333 void* DecodeAllocator::LockResourceForRead(MOS_RESOURCE* resource)
334 {
335     MOS_LOCK_PARAMS lockFlags;
336     MOS_ZeroMemory(&lockFlags, sizeof(MOS_LOCK_PARAMS));
337     lockFlags.ReadOnly = 1;
338 
339     if (!m_allocator)
340         return nullptr;
341 
342     return m_allocator->Lock(resource, &lockFlags);
343 }
344 
LockResourceForRead(MOS_BUFFER * buffer)345 void* DecodeAllocator::LockResourceForRead(MOS_BUFFER* buffer)
346 {
347     MOS_LOCK_PARAMS lockFlags;
348     MOS_ZeroMemory(&lockFlags, sizeof(MOS_LOCK_PARAMS));
349     lockFlags.ReadOnly = 1;
350 
351     if (m_allocator == nullptr || buffer == nullptr)
352         return nullptr;
353 
354     return m_allocator->Lock(&buffer->OsResource, &lockFlags);
355 }
356 
Lock(PMHW_BATCH_BUFFER batchBuffer)357 MOS_STATUS DecodeAllocator::Lock(PMHW_BATCH_BUFFER batchBuffer)
358 {
359     DECODE_CHK_NULL(batchBuffer);
360     return Mhw_LockBb(m_osInterface, batchBuffer);
361 }
362 
UnLock(MOS_RESOURCE * resource)363 MOS_STATUS DecodeAllocator::UnLock(MOS_RESOURCE* resource)
364 {
365     DECODE_CHK_NULL(m_allocator);
366     return m_allocator->UnLock(resource);
367 }
368 
UnLock(MOS_BUFFER * buffer)369 MOS_STATUS DecodeAllocator::UnLock(MOS_BUFFER* buffer)
370 {
371     DECODE_CHK_NULL(m_allocator);
372     DECODE_CHK_NULL(buffer);
373     return m_allocator->UnLock(&buffer->OsResource);
374 }
375 
UnLock(PMHW_BATCH_BUFFER batchBuffer,bool resetBuffer)376 MOS_STATUS DecodeAllocator::UnLock(PMHW_BATCH_BUFFER batchBuffer, bool resetBuffer)
377 {
378     DECODE_CHK_NULL(batchBuffer);
379     return Mhw_UnlockBb(m_osInterface, batchBuffer, resetBuffer);
380 }
381 
Resize(MOS_BUFFER * & buffer,const uint32_t sizeNew,ResourceAccessReq accessReq,bool force,bool clearData)382 MOS_STATUS DecodeAllocator::Resize(MOS_BUFFER* &buffer, const uint32_t sizeNew,
383                                    ResourceAccessReq accessReq, bool force, bool clearData)
384 {
385     DECODE_CHK_NULL(buffer);
386 
387     if (sizeNew == buffer->size)
388     {
389         if (clearData)
390         {
391             if(m_allocator->OsFillResource(&buffer->OsResource, buffer->size, 0) != MOS_STATUS_SUCCESS)
392             {
393                 DECODE_ASSERTMESSAGE("Failed to clear buffer data");
394             }
395         }
396         return MOS_STATUS_SUCCESS;
397     }
398 
399     if (force || (sizeNew > buffer->size))
400     {
401         if(clearData)
402         {
403             buffer->initOnAllocate = true;
404             buffer->initValue      = 0;
405         }
406         ResourceUsage resUsageType = ConvertGmmResourceUsage(buffer->OsResource.pGmmResInfo->GetCachePolicyUsage());
407         MOS_BUFFER* bufferNew = AllocateBuffer(
408             sizeNew, buffer->name, resUsageType, accessReq,
409             buffer->initOnAllocate, buffer->initValue, buffer->bPersistent);
410         DECODE_CHK_NULL(bufferNew);
411 
412         Destroy(buffer);
413         buffer = bufferNew;
414     }
415 
416     return MOS_STATUS_SUCCESS;
417 }
418 
Resize(MOS_SURFACE * & surface,const uint32_t widthNew,const uint32_t heightNew,ResourceAccessReq accessReq,bool force,const char * nameOfSurface)419 MOS_STATUS DecodeAllocator::Resize(MOS_SURFACE* &surface, const uint32_t widthNew, const uint32_t heightNew,
420                                    ResourceAccessReq accessReq, bool force, const char* nameOfSurface)
421 {
422     DECODE_CHK_NULL(surface);
423 
424     if ((widthNew == surface->dwWidth) && (heightNew == surface->dwHeight))
425     {
426         return MOS_STATUS_SUCCESS;
427     }
428 
429     if (force || (widthNew > surface->dwWidth) || (heightNew > surface->dwHeight))
430     {
431         ResourceUsage resUsageType = ConvertGmmResourceUsage(surface->OsResource.pGmmResInfo->GetCachePolicyUsage());
432         MOS_SURFACE* surfaceNew = AllocateSurface(widthNew, heightNew, nameOfSurface,
433             surface->Format, surface->bCompressible, resUsageType, accessReq, surface->TileModeGMM);
434         DECODE_CHK_NULL(surfaceNew);
435 
436         Destroy(surface);
437         surface = surfaceNew;
438     }
439 
440     return MOS_STATUS_SUCCESS;
441 }
442 
Resize(PMHW_BATCH_BUFFER & batchBuffer,const uint32_t sizeOfBufferNew,const uint32_t numOfBufferNew,ResourceAccessReq accessReq)443 MOS_STATUS DecodeAllocator::Resize(
444     PMHW_BATCH_BUFFER &batchBuffer, const uint32_t sizeOfBufferNew, const uint32_t numOfBufferNew,
445     ResourceAccessReq accessReq)
446 {
447     DECODE_CHK_NULL(batchBuffer);
448 
449     if (int32_t(sizeOfBufferNew) <= batchBuffer->iSize && numOfBufferNew <= batchBuffer->count)
450     {
451         return MOS_STATUS_SUCCESS;
452     }
453 
454     PMHW_BATCH_BUFFER batchBufferNew = AllocateBatchBuffer(sizeOfBufferNew, numOfBufferNew, accessReq);
455     DECODE_CHK_NULL(batchBufferNew);
456 
457     DECODE_CHK_STATUS(Destroy(batchBuffer));
458     batchBuffer = batchBufferNew;
459 
460     return MOS_STATUS_SUCCESS;
461 }
462 
Destroy(MOS_BUFFER * & buffer)463 MOS_STATUS DecodeAllocator::Destroy(MOS_BUFFER* & buffer)
464 {
465     DECODE_CHK_NULL(m_allocator);
466     if (buffer == nullptr)
467     {
468         return MOS_STATUS_SUCCESS;
469     }
470 
471     DECODE_CHK_STATUS(m_allocator->DestroyBuffer(buffer));
472     buffer = nullptr;
473     return MOS_STATUS_SUCCESS;
474 }
475 
Destroy(MOS_SURFACE * & surface)476 MOS_STATUS DecodeAllocator::Destroy(MOS_SURFACE* & surface)
477 {
478     DECODE_CHK_NULL(m_allocator);
479     if (surface == nullptr)
480     {
481         return MOS_STATUS_SUCCESS;
482     }
483 
484     //if free the compressed surface, need set the sync dealloc flag as 1 for sync dealloc for aux table update
485     MOS_GFXRES_FREE_FLAGS resFreeFlags = {0};
486     resFreeFlags.SynchronousDestroy = m_allocator->isSyncFreeNeededForMMCSurface(surface) ? 1 : 0;
487     DECODE_NORMALMESSAGE("SynchronousDestroy flag (%d) for surface\n", resFreeFlags.SynchronousDestroy);
488 
489     DECODE_CHK_STATUS(m_allocator->DestroySurface(surface, resFreeFlags));
490     surface = nullptr;
491     return MOS_STATUS_SUCCESS;
492 }
493 
Destroy(MOS_SURFACE & surface)494 MOS_STATUS DecodeAllocator::Destroy(MOS_SURFACE& surface)
495 {
496     DECODE_CHK_NULL(m_allocator);
497 
498     MOS_SURFACE* dup = MOS_New(MOS_SURFACE);
499     DECODE_CHK_NULL(dup);
500     MOS_STATUS status = MOS_SecureMemcpy(dup, sizeof(MOS_SURFACE), &surface, sizeof(MOS_SURFACE));
501     if (status != MOS_STATUS_SUCCESS)
502     {
503         MOS_Delete(dup);
504         return status;
505     }
506 
507     //if free the compressed surface, need set the sync dealloc flag as 1 for sync dealloc for aux table update
508     MOS_GFXRES_FREE_FLAGS resFreeFlags = {0};
509     resFreeFlags.SynchronousDestroy = m_allocator->isSyncFreeNeededForMMCSurface(dup) ? 1 : 0;
510     DECODE_NORMALMESSAGE("SynchronousDestroy flag (%d) for surface\n", resFreeFlags.SynchronousDestroy);
511 
512     DECODE_CHK_STATUS(m_allocator->DestroySurface(dup, resFreeFlags));
513 
514     return MOS_STATUS_SUCCESS;
515 }
516 
Destroy(BufferArray * & bufferArray)517 MOS_STATUS DecodeAllocator::Destroy(BufferArray* & bufferArray)
518 {
519     DECODE_CHK_NULL(m_allocator);
520     if (bufferArray == nullptr)
521     {
522         return MOS_STATUS_SUCCESS;
523     }
524 
525     MOS_Delete(bufferArray);
526     bufferArray = nullptr;
527     return MOS_STATUS_SUCCESS;
528 }
529 
Destroy(SurfaceArray * & surfaceArray)530 MOS_STATUS DecodeAllocator::Destroy(SurfaceArray* & surfaceArray)
531 {
532     DECODE_CHK_NULL(m_allocator);
533     if (surfaceArray == nullptr)
534     {
535         return MOS_STATUS_SUCCESS;
536     }
537 
538     MOS_Delete(surfaceArray);
539     surfaceArray = nullptr;
540     return MOS_STATUS_SUCCESS;
541 }
542 
Destroy(PMHW_BATCH_BUFFER & batchBuffer)543 MOS_STATUS DecodeAllocator::Destroy(PMHW_BATCH_BUFFER &batchBuffer)
544 {
545     if (batchBuffer == nullptr)
546     {
547         return MOS_STATUS_SUCCESS;
548     }
549 
550     DECODE_CHK_STATUS(Mhw_FreeBb(m_osInterface, batchBuffer, nullptr));
551     MOS_Delete(batchBuffer);
552     batchBuffer = nullptr;
553     return MOS_STATUS_SUCCESS;
554 }
555 
Destroy(BatchBufferArray * & batchBufferArray)556 MOS_STATUS DecodeAllocator::Destroy(BatchBufferArray* & batchBufferArray)
557 {
558     DECODE_CHK_NULL(m_allocator);
559     if (batchBufferArray == nullptr)
560     {
561         return MOS_STATUS_SUCCESS;
562     }
563 
564     MOS_Delete(batchBufferArray);
565     batchBufferArray = nullptr;
566     return MOS_STATUS_SUCCESS;
567 }
568 
DestroyAllResources()569 MOS_STATUS DecodeAllocator::DestroyAllResources()
570 {
571     DECODE_CHK_NULL(m_allocator);
572 
573     return m_allocator->DestroyAllResources();
574 }
575 
SyncOnResource(MOS_RESOURCE * resource,bool IsWriteOperation)576 MOS_STATUS DecodeAllocator::SyncOnResource(MOS_RESOURCE* resource, bool IsWriteOperation)
577 {
578     DECODE_CHK_NULL(resource);
579 
580     MOS_GPU_CONTEXT gpuContext = m_osInterface->pfnGetGpuContext(m_osInterface);
581     m_osInterface->pfnSyncOnResource(m_osInterface, resource, gpuContext, IsWriteOperation ? 1 : 0);
582 
583     return MOS_STATUS_SUCCESS;
584 }
585 
SyncOnResource(MOS_BUFFER * buffer,bool IsWriteOperation)586 MOS_STATUS DecodeAllocator::SyncOnResource(MOS_BUFFER* buffer, bool IsWriteOperation)
587 {
588     DECODE_CHK_NULL(buffer);
589 
590     MOS_GPU_CONTEXT gpuContext = m_osInterface->pfnGetGpuContext(m_osInterface);
591     m_osInterface->pfnSyncOnResource(m_osInterface, &buffer->OsResource, gpuContext, IsWriteOperation ? 1 : 0);
592 
593     return MOS_STATUS_SUCCESS;
594 }
595 
SkipResourceSync(MOS_RESOURCE * resource)596 MOS_STATUS DecodeAllocator::SkipResourceSync(MOS_RESOURCE *resource)
597 {
598     DECODE_CHK_NULL(m_allocator);
599 
600     return m_allocator->SkipResourceSync(resource);
601 }
602 
SkipResourceSync(MOS_BUFFER * buffer)603 MOS_STATUS DecodeAllocator::SkipResourceSync(MOS_BUFFER *buffer)
604 {
605     DECODE_CHK_NULL(m_allocator);
606     DECODE_CHK_NULL(buffer);
607 
608     return m_allocator->SkipResourceSync(&buffer->OsResource);
609 }
610 
ResourceIsNull(MOS_RESOURCE * resource)611 bool DecodeAllocator::ResourceIsNull(MOS_RESOURCE* resource)
612 {
613     return Mos_ResourceIsNull(resource);
614 }
615 
GetSurfaceInfo(PMOS_SURFACE surface)616 MOS_STATUS DecodeAllocator::GetSurfaceInfo(PMOS_SURFACE surface)
617 {
618     DECODE_CHK_NULL(m_allocator);
619     DECODE_CHK_NULL(surface);
620 
621     surface->Format       = Format_Invalid;
622     surface->dwArraySlice = 0;
623     surface->dwMipSlice   = 0;
624     surface->S3dChannel   = MOS_S3D_NONE;
625     DECODE_CHK_STATUS(m_allocator->GetSurfaceInfo(&surface->OsResource, surface));
626 
627     return MOS_STATUS_SUCCESS;
628 
629 }
630 
UpdateResoreceUsageType(PMOS_RESOURCE osResource,ResourceUsage resUsageType)631 MOS_STATUS DecodeAllocator::UpdateResoreceUsageType(
632     PMOS_RESOURCE osResource,
633     ResourceUsage resUsageType)
634 {
635     DECODE_CHK_NULL(m_allocator);
636 
637     return (m_allocator->UpdateResourceUsageType(osResource, static_cast<MOS_HW_RESOURCE_DEF>(resUsageType)));
638 }
639 
RegisterResource(PMOS_RESOURCE osResource)640 MOS_STATUS DecodeAllocator::RegisterResource(PMOS_RESOURCE osResource)
641 {
642     DECODE_CHK_NULL(osResource);
643 
644     return(m_osInterface->pfnRegisterResource(
645             m_osInterface,
646             osResource,
647             false,
648             false));
649 }
650 
ConvertGmmResourceUsage(const GMM_RESOURCE_USAGE_TYPE gmmResUsage)651 ResourceUsage DecodeAllocator::ConvertGmmResourceUsage(const GMM_RESOURCE_USAGE_TYPE gmmResUsage)
652 {
653     if (nullptr == m_osInterface)
654     {
655         DECODE_ASSERTMESSAGE("mos interface is nullptr")
656         return resourceDefault;
657     }
658     MOS_HW_RESOURCE_DEF gmmUsage = m_osInterface->pfnGmmToMosResourceUsageType(gmmResUsage);
659     return (ResourceUsage)gmmUsage;
660 }
661 
SetAccessRequirement(ResourceAccessReq accessReq,MOS_ALLOC_GFXRES_PARAMS & allocParams)662 void DecodeAllocator::SetAccessRequirement(
663     ResourceAccessReq accessReq, MOS_ALLOC_GFXRES_PARAMS &allocParams)
664 {
665     // The default setting is lockableVideoMem, just use default setting
666     // if not running with limited LMem bar config and not enabled HM. otherwise goto required setting.
667     if (!m_limitedLMemBar &&
668         !(m_osInterface->osCpInterface != nullptr && m_osInterface->osCpInterface->IsHMEnabled())
669     )
670     {
671         allocParams.Flags.bNotLockable = 0;
672         allocParams.dwMemType = MOS_MEMPOOL_VIDEOMEMORY;
673         return;
674     }
675 
676     allocParams.Flags.bNotLockable = (accessReq == notLockableVideoMem) ? 1 : 0;
677 
678     if (accessReq == lockableSystemMem)
679     {
680         allocParams.dwMemType = MOS_MEMPOOL_SYSTEMMEMORY;
681     }
682     else if (accessReq == notLockableVideoMem)
683     {
684         allocParams.dwMemType = MOS_MEMPOOL_DEVICEMEMORY;
685     }
686     else
687     {
688         allocParams.dwMemType = MOS_MEMPOOL_VIDEOMEMORY;
689     }
690 
691 #if (_DEBUG || _RELEASE_INTERNAL)
692     if (m_forceLockable)
693     {
694         allocParams.Flags.bNotLockable = 0;
695 
696         if (allocParams.dwMemType == MOS_MEMPOOL_DEVICEMEMORY)
697         {
698             allocParams.dwMemType = MOS_MEMPOOL_VIDEOMEMORY;
699         }
700     }
701 #endif
702 }
703 
704 }
705