1 /*
2 * Copyright (c) 2018, 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     media_allocator.h
24 //! \brief    Defines the common interface for media resource manage
25 //! \details  Media allocator will allocate and destory buffers, each component
26 //!           is recommended to inherits and implement it's owner allocator
27 //!
28 
29 #ifndef __MEDIA_ALLOCATOR_H__
30 #define __MEDIA_ALLOCATOR_H__
31 
32 #include <stdint.h>
33 #include <vector>
34 #include "mos_defs.h"
35 #include "mos_os_hw.h"
36 #include "mos_os_specific.h"
37 #include "mos_os.h"
38 #include "media_class_trace.h"
39 
40 class Allocator
41 {
42 public:
43     //!
44     //! \brief  Static method to get the allocator instance
45     //! \param  [in] osInterface
46     //!         pointer to MOS_INTERFACE
47     //! \return Allocator&
48     //!         reference to static allocator
49     //!
GetAllocator(PMOS_INTERFACE osInterface)50     static Allocator &GetAllocator(PMOS_INTERFACE osInterface)
51     {
52         static Allocator allocator(osInterface);
53         return allocator;
54     }
55 
56     //!
57     //! \brief  Constructor
58     //! \param  [in] osInterface
59     //!         Pointer to MOS_INTERFACE
60     //!
61     Allocator(PMOS_INTERFACE osInterface);
62 
63     //!
64     //! \brief  Destructor
65     //!
66     ~Allocator();
67 
68     //!
69     //! \brief  Destroy all resources
70     //! \return MOS_STATUS
71     //!         MOS_STATUS_SUCCESS if success, otherwise failed reason
72     //!
73     MOS_STATUS DestroyAllResources();
74 
75     //!
76     //! \brief  Allocate Resource
77     //! \param  [in] param
78     //!         reference to MOS_ALLOC_GFXRES_PARAMS
79     //! \param  [in] zeroOnAllocate
80     //!         zero the memory if true
81     //! \param  [in] component
82     //!         component type to track the buffer
83     //! \return MOS_RESOURCE*
84     //!         return the pointer to MOS_RESOURCE
85     //!
86     MOS_RESOURCE *AllocateResource(MOS_ALLOC_GFXRES_PARAMS &param, bool zeroOnAllocate, MOS_COMPONENT component);
87 
88     //!
89     //! \brief  Allocate Buffer
90     //! \param  [in] param
91     //!         reference to MOS_ALLOC_GFXRES_PARAMS
92     //! \param  [in] zeroOnAllocate
93     //!         zero the memory if true
94     //! \param  [in] component
95     //!         component type to track the buffer
96     //! \return MOS_BUFFER*
97     //!         return the pointer to MOS_BUFFER
98     //!
99     PMOS_BUFFER AllocateBuffer(MOS_ALLOC_GFXRES_PARAMS &param, bool zeroOnAllocate, MOS_COMPONENT component);
100 
101     //!
102     //! \brief  Allocate Surface
103     //! \param  [in] param
104     //!         reference to MOS_ALLOC_GFXRES_PARAMS
105     //! \param  [in] zeroOnAllocate
106     //!         zero the memory if true
107     //! \param  [in] component
108     //!         component type to track the buffer
109     //! \return MOS_SURFACE*
110     //!         return the pointer to MOS_SURFACE
111     //!
112     MOS_SURFACE *AllocateSurface(MOS_ALLOC_GFXRES_PARAMS &param, bool zeroOnAllocate, MOS_COMPONENT component);
113 
114     //!
115     //! \brief  Allocate a resource for a surface, so far vp does not use the mos_surface.
116     //! \res    [in] param
117     //!         pointer to MOS_RESOURCE
118     //! \param  [in] param
119     //!         reference to MOS_ALLOC_GFXRES_PARAMS
120     //! \return MOS_STATUS
121     //!         MOS_STATUS_SUCCESS if success, otherwise failed reason
122     //!
123     MOS_STATUS AllocateResource(MOS_RESOURCE *res, MOS_ALLOC_GFXRES_PARAMS &param);
124 
125     //!
126     //! \brief  Destroy Resource
127     //! \param  [in] resource
128     //!         Pointer to MOS_RESOURCE
129     //! \return MOS_STATUS
130     //!         MOS_STATUS_SUCCESS if success, else fail reason
131     //!
132     MOS_STATUS DestroyResource(MOS_RESOURCE *resource);
133 
134     //!
135     //! \brief  Destroy Buffer
136     //! \param  [in] buffer
137     //!         Pointer to MOS_BUFFER
138     //! \return MOS_STATUS
139     //!         MOS_STATUS_SUCCESS if success, else fail reason
140     //!
141     MOS_STATUS DestroyBuffer(MOS_BUFFER *buffer);
142 
143     //!
144     //! \brief  Destroy Surface
145     //! \param  [in] surface
146     //!         Pointer to MOS_SURFACE
147     //! \param  [in] flags
148     //!         flags for surface destroy
149     //! \return MOS_STATUS
150     //!         MOS_STATUS_SUCCESS if success, else fail reason
151     //!
152     MOS_STATUS DestroySurface(MOS_SURFACE *surface, MOS_GFXRES_FREE_FLAGS flags = {0});
153 
154     //!
155     //! \brief  Free a resource
156     //! \param  [in] res
157     //!         Pointer to MOS_RESOURCE
158     //! \return MOS_STATUS
159     //!         MOS_STATUS_SUCCESS if success, else fail reason
160     //!
161     MOS_STATUS FreeResource(MOS_RESOURCE *res);
162 
163     //!
164     //! \brief  Lock Surface
165     //! \param  [in] resource
166     //!         Pointer to MOS_RESOURCE
167     //! \param  [in] lockFlag
168     //!         Pointer to MOS_LOCK_PARAMS
169     //! \return void*
170     //!         a poniter to data
171     //!
172     void *Lock(MOS_RESOURCE *resource, MOS_LOCK_PARAMS *lockFlag);
173 
174     //!
175     //! \brief  UnLock Surface
176     //! \param  [in] resource
177     //!         Pointer to MOS_RESOURCE
178     //! \return MOS_STATUS
179     //!         MOS_STATUS_SUCCESS if success, else fail reason
180     //!
181     MOS_STATUS UnLock(MOS_RESOURCE *resource);
182 
183     //!
184     //! \brief  Skip sync resource
185     //! \param  [in] resource
186     //!         Pointer to MOS_RESOURCE
187     //! \return MOS_STATUS
188     //!         MOS_STATUS_SUCCESS if success, else fail reason
189     //!
190     MOS_STATUS SkipResourceSync(MOS_RESOURCE *resource);
191 
192     //!
193     //! \brief  get surface info from resource
194     //! \param  [in] osResource
195     //!         Pointer to PMOS_RESOURCE
196     //! \param  [out] resDetails
197     //!         Pointer to MOS_RESOURCE
198     //! \return MOS_STATUS
199     //!         MOS_STATUS_SUCCESS if success, else fail reason
200     //!
201     MOS_STATUS GetSurfaceInfo(PMOS_RESOURCE osResource, PMOS_SURFACE resDetails);
202 
203     //!
204     //! \brief    OS fill Resource
205     //! \details  Locks the surface and fills the resource with data
206     //! \param    PMOS_RESOURCE osResource
207     //!           [in] Pointer to OS Resource
208     //! \param    uint32_t size
209     //!           [in] Size of the Buffer
210     //! \param    uint8_t value
211     //!           [in] Value to be filled
212     //! \return   MOS_STATUS
213     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
214     //!
215     MOS_STATUS OsFillResource(PMOS_RESOURCE osResource, uint32_t  size, uint8_t value);
216 
217     //!
218     //! \brief    Tag based synchronization at the resource level
219     //! \details  Tag based synchronization at the resource level
220     //! \param    PMOS_RESOURCE OsResource
221     //!           [in] OS resource sturcture
222     //! \param    BOOL bWriteOperation
223     //!           [in] Indicate if it is a write operation
224     //! \return   VOID
225     //!
226     MOS_STATUS SyncOnResource(
227         PMOS_RESOURCE         osResource,
228         bool                  bWriteOperation);
229 
230     //!
231     //! \brief    Update the usage type of resource for cache policy
232     //! \details  Update the usage type of resource for cache policy
233     //! \param    PMOS_RESOURCE OsResource
234     //!           [in] OS resource sturcture
235     //! \param    MOS_HW_RESOURCE_DEF resUsageType
236     //!           [in] MOS_HW_RESOURCE_DEF to be set
237     //! \return   MOS_STATUS
238     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
239     //!
240     MOS_STATUS UpdateResourceUsageType(
241         PMOS_RESOURCE           osResource,
242         MOS_HW_RESOURCE_DEF     resUsageType);
243 
244     //!
245     //! \brief    Check if sync free needed for compressed surface
246     //! \param    PMOS_SURFACE pOsSurface
247     //!           [in] os surface pointer
248     //! \return   bool
249     //!           true if success, otherwise failed reason
250     //!
251     bool isSyncFreeNeededForMMCSurface(PMOS_SURFACE pOsSurface);
252 
253 protected:
254 
255     //!
256     //! \brief  Clear Resource
257     //! \param  [in] resource
258     //!         pointer to MOS_RESOURCE
259     //! \param  [in] param
260     //!         reference to MOS_ALLOC_GFXRES_PARAMS
261     //! \return MOS_STATUS
262     //!         MOS_STATUS_SUCCESS if success, else fail reason
263     //!
264     MOS_STATUS ClearResource(MOS_RESOURCE *resource, MOS_ALLOC_GFXRES_PARAMS &param);
265 
266 #if (_DEBUG || _RELEASE_INTERNAL)
267     struct TraceInfo
268     {
269         MOS_COMPONENT component;
270         std::string   name;
271     };
272 
273     std::map<MOS_RESOURCE *, TraceInfo *> m_resourcePool;
274     std::map<MOS_SURFACE *, TraceInfo *>  m_surfacePool;
275 #else
276     std::vector<MOS_RESOURCE *> m_resourcePool;
277     std::vector<MOS_SURFACE *>  m_surfacePool;
278 #endif
279 
280     PMOS_INTERFACE m_osInterface = nullptr;  //!< PMOS_INTERFACE
281 MEDIA_CLASS_DEFINE_END(Allocator)
282 };
283 #endif  // !__MEDIA_ALLOCATOR_H__
284