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