xref: /aosp_15_r20/external/intel-media-driver/media_driver/agnostic/common/os/mos_graphicsresource.h (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
1 /*
2 * Copyright (c) 2017-2020, 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    mos_graphicsresource.h
24 //! \brief   Container class for the basic gpu resource
25 //!
26 
27 #ifndef __MOS_GRAPHICS_RESOURCE_H__
28 #define __MOS_GRAPHICS_RESOURCE_H__
29 
30 #include "mos_os.h"
31 #include "mos_resource_defs.h"
32 #include <string>
33 #include "mos_context.h"
34 #include "mos_interface.h"
35 
36 class GraphicsResource
37 {
38 public:
39     enum ResourceType
40     {
41         undefinedResource    = 0,    //!< Undefined Resource Type
42         osSpecificResource   = 1,    //!< Os Specific Resource Type
43         soloResource         = 2,    //!< MediaSolo based Resource Type
44         invalidResource      = 0xff  //!< Ceil Guard for Resource Type
45     };
46 
47     constexpr static uint32_t  m_maxBufNameLength = 256;
48 
49     //!
50     //! \brief  Structure to Resource allocation parameters
51     //!
52     struct CreateParams
53     {
54     public:
55         //!
56         //! \brief  0,1: 1 element. >1: N elements
57         //!
58         uint32_t m_arraySize = 1;
59 
60         //!
61         //! \brief   Compression mode.
62         //!
63         MOS_RESOURCE_MMC_MODE m_compressionMode = MOS_MMC_DISABLED;
64 
65         //!
66         //! \brief   depth information.
67         //!
68         uint32_t m_depth     = 0;
69 
70         //!
71         //! \brief   Pixel format.
72         //!
73         MOS_FORMAT m_format = Format_Invalid;
74 
75         //!
76         //! \brief   Type == 2D || VOLUME, height in rows. Type == BUFFER, n/a
77         //!
78         uint32_t m_height   = 1;
79 
80         //!
81         //! \brief   Resource is compressible or not.
82         //!
83         bool m_isCompressible = false;
84 
85         //!
86         //! \brief   Optional parameter. Used to indicate that resource
87         //!              can not be evicted
88         //!
89         bool m_isPersistent = false;
90 
91         //!
92         //! \brief   Optional parameter only used in Linux. A string indicates the buffer
93         //!              name and is used for debugging. NULL is OK.
94         //!
95         std::string m_name  = "";
96 
97         //!
98         //! \brief   Optional parameter. If non null, TileType must be set to linear.
99         //!
100         void* m_pSystemMemory = nullptr;
101 
102         //!
103         //! \brief  Whether SVM is in use.
104         //!
105         bool m_svm = false;
106 
107         //!
108         //! \brief   Defines the layout of a physical page. Optimal choice depends on usage
109         //!              model.
110         //!
111         MOS_TILE_TYPE m_tileType = MOS_TILE_INVALID;
112 
113         //!
114         //! \brief   Basic resource geometry
115         //!
116         MOS_GFXRES_TYPE m_type = MOS_GFXRES_INVALID;
117 
118         //!
119         //! \brief   Flags to describe attributes
120         //!
121         MOS_GFXRES_FLAGS m_flags = {};
122 
123         //!
124         //! \brief   width in pixels
125         //!
126         uint32_t m_width = 0;
127 
128         //!
129         //! \brief   allocation memory type
130         //!
131         uint32_t m_memType = MOS_MEMPOOL_VIDEOMEMORY;
132 
133         //!
134         //! \brief   A tile Encoding (aligned w/ GMM defination) needs set by force, take effects when implicitly tile handling
135         //!
136         MOS_TILE_MODE_GMM m_tileModeByForce = MOS_TILE_UNSET_GMM;
137 
138         //!
139         //! \brief   gmm resource usage type
140         //!
141         GMM_RESOURCE_USAGE_TYPE m_gmmResUsageType = GMM_RESOURCE_USAGE_UNKNOWN;
142 
143         //!
144         //! \brief   mos resource usage type, set mocs index
145         //!
146         MOS_HW_RESOURCE_DEF m_mocsMosResUsageType = MOS_MP_RESOURCE_USAGE_DEFAULT;
147 
148         //!
149         //! \brief   Create the graphics buffer from a PMOS_ALLOC_GFXRES_PARAMS, for wrapper usage, to be deleted
150         //!
CreateParamsCreateParams151         CreateParams(PMOS_ALLOC_GFXRES_PARAMS pParams)
152         {
153             m_arraySize       = pParams->dwArraySize;
154             m_compressionMode = pParams->CompressionMode;
155             m_depth           = pParams->dwDepth;
156             m_format          = pParams->Format;
157             m_height          = pParams->dwHeight;
158             m_isCompressible  = (pParams->bIsCompressible == 1) ? true : false;
159             m_isPersistent    = (pParams->bIsPersistent == 1) ? true : false;
160             if (pParams->pBufName != nullptr)
161             {
162                  m_name = pParams->pBufName;
163             }
164             m_pSystemMemory   = pParams->pSystemMemory;
165             m_tileType        = pParams->TileType;
166             m_type            = pParams->Type;
167             m_flags           = pParams->Flags;
168             m_width           = pParams->dwWidth;
169             m_memType         = pParams->dwMemType;
170             m_tileModeByForce = pParams->m_tileModeByForce;
171 
172             if (pParams->ResUsageType == MOS_CODEC_RESOURCE_USAGE_BEGIN_CODEC ||
173                 pParams->ResUsageType == MOS_HW_RESOURCE_DEF_MAX)  // the usage type is invalid, set to default usage.
174             {
175                 m_mocsMosResUsageType = MOS_MP_RESOURCE_USAGE_DEFAULT;
176             }
177             else
178             {
179                 m_mocsMosResUsageType = pParams->ResUsageType;
180             }
181             m_gmmResUsageType = MosInterface::GetGmmResourceUsageType(m_mocsMosResUsageType);
182         };
183 
CreateParamsCreateParams184         CreateParams()
185         {
186         };
187     };
188 
189     //!
190     //! \brief  Structure to Structure to Lock params
191     //!
192     struct LockParams
193     {
194     public:
195         bool m_forceCached  = false;
196         bool m_noDecompress = false;
197         bool m_readRequest  = false;
198         bool m_tileAsTiled  = false;
199         bool m_uncached     = false;
200         bool m_writeRequest = false;
201         bool m_noOverWrite  = false;
202 
203         //!
204         //! \brief   For wrapper usage, to be removed
205         //!
LockParamsLockParams206         LockParams(PMOS_LOCK_PARAMS pLockFlags)
207         {
208             m_forceCached  = pLockFlags->ForceCached;
209             m_noDecompress = pLockFlags->NoDecompress;
210             m_readRequest  = pLockFlags->ReadOnly;
211             m_tileAsTiled  = pLockFlags->TiledAsTiled;
212             m_uncached     = pLockFlags->Uncached;
213             m_writeRequest = pLockFlags->WriteOnly;
214             m_noOverWrite  = pLockFlags->NoOverWrite;
215         };
216 
LockParamsLockParams217         LockParams()
218         {
219         };
220     };
221 
222     //!
223     //! \brief Structure to OS sync parameters
224     //!
225     struct SyncParams
226     {
227         MOS_GPU_CONTEXT         m_gpuContext      = {};             //!< GPU context you would like to signal on or wait in
228         GraphicsResource*       m_resSyncResource = nullptr;        //!< Has 2 meanings:
229                                                                     //!< 1) a resource that requires sync, like a destination surface
230                                                                     //!< 2) a resource used by HW/OS to sync between engines, like for MI_SEMAPHORE_MBOX
231         uint32_t                m_semaphoreCount  = 0;              //!< Semaphore count
232         uint32_t                m_semaphoreValue  = 0;              //!< Tag value in the case of resource tagging
233         uint32_t                m_semaphoreOffset = 0;              //!< Offset into the sync resource for value read/write
234         bool                    m_readOnly        = false;          //!< Marks the resource as read or write for future waits
235         bool                    m_disableDecodeSyncLock   = false;  //!< Disable the lock function for decode.
236         bool                    m_disableLockForTranscode = false;  //!< Disable the lock function for transcode perf.
237 
238         //!
239         //! \brief   For wrapper usage, to be removed
240         //!
SyncParamsSyncParams241         SyncParams(PMOS_SYNC_PARAMS pParams)
242         {
243             m_gpuContext              = pParams->GpuContext;
244             m_resSyncResource         = pParams->presSyncResource->pGfxResource;
245             m_semaphoreCount          = pParams->uiSemaphoreCount;
246             m_semaphoreValue          = pParams->uiSemaphoreValue;
247             m_semaphoreOffset         = pParams->uiSemaphoreOffset;
248             m_readOnly                = (pParams->bReadOnly == 1) ? true : false;
249             m_disableDecodeSyncLock   = (pParams->bDisableDecodeSyncLock == 1) ? true : false;
250             m_disableLockForTranscode = (pParams->bDisableLockForTranscode == 1) ? true : false;
251         };
252 
SyncParamsSyncParams253         SyncParams()
254         {
255         };
256     } ;
257 
258 protected:
259     //!
260     //! \brief  Constructor
261     //! \return N/A
262     //!
263     GraphicsResource();
264 
265 public:
266     //!
267     //! \brief  Create Graphic resource based on specific resource type
268     //! \param  [in] resourceType
269     //!         Resource type, either os specific resoure or solo resource
270     //! \return One specific Graphic Resource
271     //!
272     static class GraphicsResource* CreateGraphicResource(ResourceType resourceType);
273 
274     //!
275     //! \brief  Destructor
276     //! \return N/A
277     //!
278     virtual ~GraphicsResource();
279 
280     //!
281     //! \brief  Get the size of the graphic resource
282     //! \return buf size of the graphic resource
283     //!
GetSize()284     uint32_t GetSize() { return m_size; };
285 
286     //!
287     //! \brief  Get the locked address of resource
288     //! \return address locked
289     //!
GetLockedAddr()290     uint8_t* GetLockedAddr() {return m_pData; };
291 
292     //!
293     //! \brief  Get allocation index of resource
294     //! \param  [in] gpuContextHandle
295     //!         Gpu context handle used to get the alloc index
296     //! \return index got from current resource
297     //!
298     int32_t GetAllocationIndex(GPU_CONTEXT_HANDLE gpuContextHandle);
299 
300     //!
301     //! \brief  Dump the content of the graphic resource into a specific file
302     //! \param  [in] osContextPtr
303     //!         Pointer to the osContext handle
304     //! \param  [in] overrideOffset
305     //!         the offset inside the dump file
306     //! \param  [in] overrideSize
307     //!         the dump length
308     //! \param  [in] outputFileName
309     //!         the dump file name
310     //! \param  [in] outputPath
311     //!         the dump file path
312     //! \return MOS_STATUS_SUCCESS on success case, MOS error status on fail cases
313     //!
314     MOS_STATUS Dump(OsContext* osContextPtr, uint32_t overrideOffset, uint32_t overrideSize, std::string outputFileName, std::string outputPath);
315 
316     //!
317     //! \brief  Check whether the resource is nullptr
318     //! \param  [in] pResource
319     //!         ptr to the graphics resource to be checked
320     //! \return ture if the resource is nullptr, false on other cases
321     //!
322     virtual bool ResourceIsNull() = 0;
323 
324     //!
325     //! \brief  Allocate the graphic memory to back up the graphic resource
326     //! \param  [in] osContextPtr
327     //!         Pointer to the osContext handle
328     //! \param  [in] params
329     //!         Resource creation Params
330     //! \return MOS_STATUS_SUCCESS on success case, MOS error status on fail cases
331     //!
332     virtual MOS_STATUS Allocate(OsContext* osContextPtr, CreateParams& params) = 0;
333 
334     //!
335     //! \brief  Frees the specified resource with specific flag, if locked, unlocks it.
336     //! \param  [in] osContextPtr
337     //!         Pointer to the osContext handle
338     //! \param  [in] freeFlag
339     //!         specific flag to free the resource
340     //!
341     virtual void Free(OsContext* osContextPtr, uint32_t  freeFlag = 0) = 0;
342 
343     //!
344     //! \brief  check whether the current graphic resource is equal to another one
345     //! \param  [in] toCompare
346     //!         ptr to the graphics resource to be compared with
347     //! \return ture if equal, false if not
348     //!
349     virtual bool IsEqual(GraphicsResource* toCompare) = 0;
350 
351     //!
352     //! \brief  Check whether the current graphic resource is valid
353     //! \return Returns true if the two resources are equal and false otherwise.
354     //!
355     virtual bool IsValid() = 0;
356 
357     //!
358     //! \brief  Locks a resource and returns a mapped system memory pointer.
359     //! \param  [in] osContextPtr
360     //!         Pointer to the osContext handle
361     //! \param  [in] params
362     //!         Resource lock Params
363     //! \return CPU side lock address in success case, nullptr in fail cases
364     //!
365     virtual void* Lock(OsContext* osContextPtr, LockParams& params) = 0;
366 
367     //!
368     //! \brief  Unlocks a resource that has already been locked, if no lock has
369     //!         occurred, this function does nothing
370     //! \param  [in] osContextPtr
371     //!         Pointer to the osContext handle
372     //! \return MOS_SUCCESS in success case, MOS error status in fail cases
373     //!
374     virtual MOS_STATUS Unlock(OsContext* osContextPtr) = 0;
375 
376     //!
377     //! \brief  Converts an OS specific resource to a MOS resource.
378     //! \param  [out] pMosResource
379     //!         ptr to the MOS_RESOURCE as the convertion result
380     //! \return MOS_SUCCESS in success case, MOS error status in fail cases
381     //!
382     virtual MOS_STATUS ConvertToMosResource(MOS_RESOURCE* pMosResource) = 0;
383 
384     //!
385     //! \brief  For MemNinja support.
386     //! \return Global memory alloction counter
387     //!
GetMemAllocCounterGfx()388     static uint32_t GetMemAllocCounterGfx() { return m_memAllocCounterGfx; };
389 
390     //!
391     //! \brief  For MemNinja support.
392     //! \return Global memory alloction counter
393     //!
SetMemAllocCounterGfx(uint32_t memAllocCounterGfx)394     static void  SetMemAllocCounterGfx(uint32_t memAllocCounterGfx) { m_memAllocCounterGfx = memAllocCounterGfx; };
395 
396     //!
397     //! \brief  Reset allocation index of all Gpu context in the resource
398     //! \return void
399     //!
400     void ResetResourceAllocationIndex();
401 
402 protected:
403     //!
404     //! \brief  Global graphic resource counter
405     //!
406     static uint32_t m_memAllocCounterGfx;
407 
408     //!
409     //! \brief  buf name of the graphic resource
410     //!
411     std::string m_name = "";
412 
413     //!
414     //! \brief  CPU side lock address for the graphic resource
415     //!
416     uint8_t* m_pData = nullptr;
417 
418     //!
419     //! \brief  0,1: 1 element. >1: N elements
420     //!
421     uint32_t m_arraySize = 0;
422 
423     //!
424     //! \brief  Graphic resource compressiable or not
425     //!
426     bool m_compressible = false;
427 
428     //!
429     //! \brief  compression mode for the Graphic resource
430     //!
431     MOS_RESOURCE_MMC_MODE m_compressionMode = MOS_MMC_DISABLED;
432 
433     //!MOS_MMC_DISABLED
434     //! \brief  0: Implies 2D resource. >=1: volume resource
435     //!
436     uint32_t m_depth = 0;
437 
438     //!
439     //! \brief  wheather the flip chain is in use
440     //!
441     bool m_flipChain = false;
442 
443     //!
444     //! \brief  Pixel format
445     //!
446     MOS_FORMAT m_format = Format_Invalid;
447 
448     //!
449     //! \brief  Type == 2D || VOLUME, height in rows. Type == BUFFER, n/a
450     //!
451     uint32_t m_height = 0;
452 
453     //!
454     //! \brief  the Graphic resource is compressed or not
455     //!
456     bool m_isCompressed = false;
457 
458     //!
459     //! \brief  the flag to indicate if overlay is in use
460     //!
461     bool m_overlay = false;
462 
463     //!
464     //! \brief   < RenderPitch > pitch in bytes used for programming HW
465     //!
466     uint32_t m_pitch = 0;
467 
468     //!
469     //! \brief   distance in rows between R-Planes used for programming HW
470     //!
471     uint32_t m_qPitch = 0;
472 
473     //!
474     //! \brief   the active m_count for the graphic resource
475     //!
476     uint32_t m_count = 0;
477 
478     //!
479     //! \brief   the active S3D_CHANNEL for the graphic resource
480     //!
481     MOS_S3D_CHANNEL m_s3dChannel = MOS_S3D_NONE;
482 
483     //!
484     //! \brief   the size of the graphic resource
485     //!
486     uint32_t m_size = 0;
487 
488     //!
489     //! \brief   Type == VOLUME, byte offset to next slice. Type != VOLUME, n/a
490     //!
491     uint32_t m_slicePitch = 0;
492 
493     //!
494     //! \brief   Defines the layout of a physical page. Optimal choice depends on usage
495     //!
496     MOS_TILE_TYPE       m_tileType = MOS_TILE_INVALID;
497 
498     //!
499     //! \brief   Transparent GMM Tiletype specifying in hwcmd finally
500     //!
501     MOS_TILE_MODE_GMM   m_tileModeGMM = MOS_TILE_LINEAR_GMM;
502 
503     //!
504     //! \brief   GMM defined Tile mode flag
505     //!
506     bool                m_isGMMTileEnabled = false;
507 
508     //!
509     //! \brief   Basic resource geometry
510     //!
511     MOS_GFXRES_TYPE m_type = MOS_GFXRES_INVALID;
512 
513     //!
514     //! \brief   U surface plane offset
515     //!
516     OsContext::MOS_PLANE_OFFSET m_uPlaneOffset = {};
517 
518     //!
519     //! \brief   V surface plane offset
520     //!
521     OsContext::MOS_PLANE_OFFSET m_vPlaneOffset = {};
522 
523     //!
524     //! \brief   Type == 2D || VOLUME, width in pixels.
525     //!
526     uint32_t m_width = 0;
527 
528     //!
529     //! \brief   Y surface plane offset or RGB
530     //!
531     OsContext::MOS_PLANE_OFFSET m_yPlaneOffset = {};
532 
533     //!
534     //! \brief  This is used by MDF when a wrapper MOS Resource is used to
535     //!         set surface state for a given VA, not necessary from start,
536     //!         in another actual MOS resource
537     //!
538     uint64_t                m_userProvidedVA = 0;
539 
540     //!
541     //! \brief  Array of Gpu context and alloctaion index tuple.
542     //!
543     std::vector <std::tuple<GPU_CONTEXT_HANDLE, int32_t>>  m_allocationIndexArray;
544 
545     //! \brief   Mutex for allocation index array
546     PMOS_MUTEX m_allocationIndexMutex = nullptr;
547 
548     //! \brief   Mutex for allocation index array
549     MEMORY_OBJECT_CONTROL_STATE m_memObjCtrlState = {};
550 
551     //!
552     //! \brief   mos resource usage type, set mocs index
553     //!
554     MOS_HW_RESOURCE_DEF m_mocsMosResUsageType = MOS_MP_RESOURCE_USAGE_DEFAULT;
555 };
556 #endif // #ifndef __MOS_GRAPHICS_RESOURCE_H__
557 
558