xref: /aosp_15_r20/external/intel-media-driver/media_driver/agnostic/common/cm/cm_surface_manager_base.h (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
1 /*===================== begin_copyright_notice ==================================
2 /*
3 * Copyright (c) 2019, Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 */
23 //!
24 //! \file      cm_surface_manager_base.h
25 //! \brief     Contains Class CmSurfaceManagerBase definitions
26 //!
27 
28 #ifndef MEDIADRIVER_COMMON_CM_CMSURFACEMANAGER_BASE_H_
29 #define MEDIADRIVER_COMMON_CM_CMSURFACEMANAGER_BASE_H_
30 
31 #include "cm_def.h"
32 #include "cm_hal.h"
33 #include <set>
34 
35 typedef enum _MOS_FORMAT MOS_FORMAT;
36 
37 namespace CMRT_UMD
38 {
39 
40 class CmDeviceRT;
41 class CmSurface;
42 class CmBuffer_RT;
43 class CmSurfaceVme;
44 class CmSurfaceSampler8x8;
45 class CmSurfaceSampler;
46 class CmSurface2DUPRT;
47 class CmSurface2DRT;
48 class CmSurface3DRT;
49 class CmStateBuffer;
50 class CmKernelRT;
51 
52 class CmSurfaceManagerBase
53 {
54 public:
55     virtual int32_t CreateSurface2D(uint32_t width, uint32_t height, uint32_t pitch,
56                             bool bIsCmCreated, CM_SURFACE_FORMAT format,
57                             CmSurface2DRT* & pSurface);
58     int32_t CreateBuffer(size_t size, CM_BUFFER_TYPE type,
59                          bool isCMRTAllocatedSVM, CmBuffer_RT* & pSurface1D,
60                          PMOS_RESOURCE pMosResource, void* &pSysMem,
61                          bool isConditionalBuffer, uint32_t comparisonValue );
62     int32_t DestroySurface( CmBuffer_RT* & pSurface, SURFACE_DESTROY_KIND destroyKind);
63     virtual int32_t UpdateBuffer(MOS_RESOURCE * mosResource, int index, uint32_t handle);
64 
65     int32_t CreateSurface2DUP(uint32_t width, uint32_t height,
66                               CM_SURFACE_FORMAT format, void* pSysMem,
67                               CmSurface2DUPRT* & pSurface2D);
68     int32_t DestroySurface( CmSurface2DUPRT* & pSurface,  SURFACE_DESTROY_KIND destroyKind);
69 
70     virtual int32_t CreateSurface2DFromMosResource(PMOS_RESOURCE pMosResource,
71                                            bool bIsCmCreated, CmSurface2DRT* & pSurface2D) = 0;
72     virtual int32_t UpdateSurface2D(MOS_RESOURCE * mosResource, int index, uint32_t handle) = 0;
73 
74     int32_t DestroySurface( CmSurface2DRT* & pSurface, SURFACE_DESTROY_KIND destroyKind);
75     int32_t CreateSurface3D(uint32_t width, uint32_t height, uint32_t depth,
76                             CM_SURFACE_FORMAT format, CmSurface3DRT* & pSurface );
77     int32_t DestroySurface( CmSurface3DRT* & pSurface, SURFACE_DESTROY_KIND destroyKind);
78 
79     int32_t CreateVmeSurface( CmSurface2DRT* pCurrentSurface,CmSurface2DRT** pForwardSurface,
80                           CmSurface2DRT** pBackwardSurface,const uint32_t surfaceCountForward,
81                           const uint32_t surfaceCountBackward, SurfaceIndex* & pVmeIndex );
82     int32_t DestroyVmeSurface( SurfaceIndex* & pVmeIndex );
83     int32_t DestroySurface( CmSurfaceVme* & pSurfaceVme );
84 
85     int32_t CreateSampler8x8Surface(CmSurface2DRT* pCurrentSurface,
86                                     SurfaceIndex* & pSampler8x8Index,
87                                     CM_SAMPLER8x8_SURFACE sampler8x8_type,
88                                     CM_SURFACE_ADDRESS_CONTROL_MODE mode, CM_FLAG* pFlag);
89     int32_t DestroySampler8x8Surface( SurfaceIndex* & pSampler8x8Index );
90     int32_t CreateSampler8x8SurfaceFromAlias(
91                                     CmSurface2DRT *originalSurface,
92                                     SurfaceIndex *aliasIndex,
93                                     CM_SURFACE_ADDRESS_CONTROL_MODE addressControl,
94                                     SurfaceIndex* &sampler8x8SurfaceIndex);
95 
96     int32_t DestroySurface( CmSurfaceSampler8x8* & pSurfaceSampler8x8 );
97 
98     virtual int32_t GetSurface( const uint32_t index, CmSurface* & pSurface );
99     virtual int32_t GetCmDevice( CmDeviceRT* & pCmDevice );
100 
101     virtual int32_t GetPixelBytesAndHeight(uint32_t width, uint32_t height,
102                                    CM_SURFACE_FORMAT format, uint32_t& sizePerPixel,
103                                    uint32_t& updatedHeight);
104     virtual int32_t Surface2DSanityCheck(uint32_t width, uint32_t height, CM_SURFACE_FORMAT format) = 0;
105 
106     int32_t CreateSamplerSurface(CmSurface2DRT* pCurrentSurface,
107                                  SurfaceIndex* & pSamplerSurfaceIndex, CM_FLAG* pFlag);
108     int32_t CreateSamplerSurface(CmSurface2DUPRT* pCurrentSurface,
109                                  SurfaceIndex* & pSamplerSurfaceIndex);
110     int32_t CreateSamplerSurface(CmSurface3DRT* pCurrentSurface,
111                                  SurfaceIndex* & pSamplerSurfaceIndex);
112     int32_t DestroySamplerSurface(SurfaceIndex* & pSamplerSurfaceIndex );
113     int32_t DestroySurface( CmSurfaceSampler* & pSurfaceSampler );
114     virtual uint32_t GetSurfacePoolSize();
115     int32_t IncreaseSurfaceUsage(uint32_t index);
116     int32_t DecreaseSurfaceUsage(uint32_t index);
117     virtual int32_t RefreshDelayDestroySurfaces(uint32_t &freeSurfaceCount);
118     int32_t TouchSurfaceInPoolForDestroy();
119     int32_t GetFreeSurfaceIndexFromPool(uint32_t &freeIndex);
120     int32_t GetFreeSurfaceIndex(uint32_t &index);
121 
122     int32_t AllocateSurfaceIndex(size_t width, uint32_t height,
123                                  uint32_t depth, CM_SURFACE_FORMAT format,
124                                  uint32_t &index, void *pSysMem);
125 
126     int32_t DestroySurfaceArrayElement( uint32_t index );
127     inline int32_t GetMemorySizeOfSurfaces();
128 
129     virtual int32_t GetSurfaceArraySize(uint32_t& surfaceArraySize);
130 
131     int32_t UpdateStateForDelayedDestroy(SURFACE_DESTROY_KIND destroyKind, uint32_t i);
132 
133     int32_t UpdateStateForRealDestroy(uint32_t i, CM_ENUM_CLASS_TYPE kind);
134     int32_t UpdateProfileFor2DSurface(uint32_t index, uint32_t width,
135                                       uint32_t height, CM_SURFACE_FORMAT format);
136     int32_t UpdateProfileFor1DSurface(uint32_t index, uint32_t size);
137     int32_t UpdateProfileFor3DSurface(uint32_t index, uint32_t width,
138                                       uint32_t height, uint32_t depth, CM_SURFACE_FORMAT format);
139 
140     virtual int32_t UpdateSurface2DTableMosResource( uint32_t index, PMOS_RESOURCE pMosResource );
141     virtual int32_t UpdateSurface2DTableRotation(uint32_t index, CM_ROTATION rotationFlag);
142 
143     virtual int32_t UpdateSurface2DTableFrameType(uint32_t index, CM_FRAME_TYPE frameType);
144     virtual int32_t UpdateSurface2DTableChromaSiting(uint32_t index, int32_t chromaSiting);
145 
146     int32_t DestroyStateBuffer( CmStateBuffer *&buffer_ptr,
147                                 SURFACE_DESTROY_KIND destroyKind );
148     int32_t DestroyMediaState( void  *media_state_ptr );
149 
150     virtual bool IsCmReservedSurfaceIndex(uint32_t surfBTI);
151     virtual bool IsValidSurfaceIndex(uint32_t surfBTI);
152     uint32_t MaxIndirectSurfaceCount();
153     virtual uint32_t ValidSurfaceIndexStart();
154     bool IsSupportedForSamplerSurface2D(CM_SURFACE_FORMAT format);
SetLatestVeboxTrackerAddr(uint32_t * tracker)155     inline void SetLatestVeboxTrackerAddr(uint32_t *tracker) {m_latestVeboxTracker = tracker; }
LatestVeboxTracker()156     inline uint32_t LatestVeboxTracker() {return *m_latestVeboxTracker; }
157 
158     void AddToDelayDestroyList(CmSurface *surface);
159     void RemoveFromDelayDestroyList(CmSurface *surface);
GetStatelessSurfaceArray()160     std::set<CmSurface *> & GetStatelessSurfaceArray() { return m_statelessSurfaceArray; }
161 
162 #if MDF_SURFACE_CONTENT_DUMP
163     virtual CM_HAL_STATE* GetHalState();
164 #endif  // #if MDF_SURFACE_CONTENT_DUMP
165 
166 protected:
167     CmSurfaceManagerBase( CmDeviceRT* pCmDevice );
168     CmSurfaceManagerBase();
169 
170     virtual ~CmSurfaceManagerBase( void );
171     int32_t Initialize( CM_HAL_MAX_VALUES HalMaxValues, CM_HAL_MAX_VALUES_EX HalMaxValuesEx );
172 
173     int32_t AllocateBuffer(size_t size, CM_BUFFER_TYPE type, uint32_t & handle,
174                            PMOS_RESOURCE pMosResource, void* &pSysMem,
175                            uint64_t &gfxMem); // Create the internal buffer of surface1D in driver
176     int32_t FreeBuffer( uint32_t handle ); // Destroy the internal buffer of surface1D in driver
177 
178     int32_t AllocateSurface2DUP(uint32_t width, uint32_t height, CM_SURFACE_FORMAT format,
179                                 void* pSysMem, uint32_t & handle );
180     int32_t FreeSurface2DUP( uint32_t handle );
181 
182     int32_t AllocateSurface2D(uint32_t width, uint32_t height, CM_SURFACE_FORMAT format,
183                               uint32_t & handle, uint32_t & pitch );
184     int32_t AllocateSurface2D(uint32_t width, uint32_t height, CM_SURFACE_FORMAT format,
185                               PMOS_RESOURCE pMosResource, uint32_t & handle);
186 
187     int32_t FreeSurface2D( uint32_t handle );
188 
189     int32_t Allocate3DSurface(uint32_t width, uint32_t height, uint32_t depth,
190                               CM_SURFACE_FORMAT format, uint32_t & handle );
191     int32_t Free3DSurface( uint32_t handle );
192 
193     int32_t GetFormatSize(CM_SURFACE_FORMAT format,uint32_t &sizeperpixel);
194 
195     virtual int32_t GetSurfaceInfo( PMOS_RESOURCE pResource, uint32_t &width,
196                                     uint32_t &height, uint32_t &pitch, CM_SURFACE_FORMAT &format) = 0;
197 
198     int32_t GetSurfaceBTIInfo();
199 
200 public:
201     // mamimum number of cm device allowed for creating a cm surf2d wrapper for a mos resource
202     static const uint32_t MAX_DEVICE_FOR_SAME_SURF = 64;
203 protected:
204 
205     CmDeviceRT* m_device;
206     // if VPHAL is not defined, it should be CM_MAX_ALL_SURFACE_COUNT
207     uint32_t m_surfaceArraySize;
208 
209     CmSurface** m_surfaceArray;
210     // the max index allocated in the m_SurfaceArray
211     uint32_t m_maxSurfaceIndexAllocated;
212     // Size of each surface in surface array
213     int32_t *m_surfaceSizes;
214 
215     uint32_t m_maxBufferCount;
216     uint32_t m_bufferCount;
217 
218     uint32_t m_max2DSurfaceCount;
219     uint32_t m_2DSurfaceCount;
220 
221     uint32_t m_max3DSurfaceCount;
222     uint32_t m_3DSurfaceCount;
223 
224     uint32_t m_max2DUPSurfaceCount;
225     uint32_t m_2DUPSurfaceCount;
226 
227     uint32_t m_bufferAllCount;
228     uint32_t m_2DSurfaceAllCount;
229     uint32_t m_3DSurfaceAllCount;
230 
231     uint32_t m_bufferAllSize;
232     uint32_t m_2DSurfaceAllSize;
233     uint32_t m_3DSurfaceAllSize;
234 
235     uint32_t m_garbageCollectionTriggerTimes;
236     uint32_t m_garbageCollection1DSize;
237     uint32_t m_garbageCollection2DSize;
238     uint32_t m_garbageCollection3DSize;
239 
240     CM_SURFACE_BTI_INFO m_surfaceBTIInfo;
241 
242     uint32_t *m_latestVeboxTracker;
243 
244     CmSurface *m_delayDestroyHead;
245     CmSurface *m_delayDestroyTail;
246     CSync m_delayDestoryListSync;
247 
248     std::set<CmSurface *> m_statelessSurfaceArray;
249 
250 private:
251     CmSurfaceManagerBase(const CmSurfaceManagerBase& other);
252     CmSurfaceManagerBase& operator= (const CmSurfaceManagerBase& other);
253 };
254 }; //namespace
255 
256 #endif  // #ifndef MEDIADRIVER_COMMON_CM_CMSURFACEMANAGER_BASE_H_
257