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