1 /*
2 * Copyright (c) 2017, 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     codechal_kernel_base.h
24 //! \brief    Defines base class for all kernels
25 //! \details  Kernel base class abstracts all common functions and definitions
26 //!           for all kernels, each kernel class should inherit from kernel base
27 //!
28 
29 #ifndef __CODECHAL_KERNEL_BASE_H__
30 #define __CODECHAL_KERNEL_BASE_H__
31 
32 #include "codechal.h"
33 #include "codechal_hw.h"
34 #include "codechal_encoder_base.h"
35 #include <map>
36 
37 //!
38 //! \class    CodechalKernelBase
39 //! \brief    Codechal kernel base
40 //!
41 class CodechalKernelBase
42 {
43 public:
44     using KernelBinaryCallback = MOS_STATUS (*)(void *,
45                                                 EncOperation,
46                                                 uint32_t,
47                                                 void *,
48                                                 uint32_t *);
49 
50     //!
51     //! \brief Constructor
52     //!
53     CodechalKernelBase(CodechalEncoderState *encoder);
54 
55     //!
56     //! \brief Destructor
57     //!
58     virtual ~CodechalKernelBase();
59 
60     //!
61     //! \brief  Intialize kernel state object
62     //!
63     //! \param  [in] callback
64     //!         callback function which parse kernel header from kernel structure
65     //! \param  [in] binaryBase
66     //!         pointer to the base address of kernel binary array
67     //! \param  [in] kernelUID
68     //!         kernel UID
69     //!
70     virtual MOS_STATUS Initialize(
71         KernelBinaryCallback callback,
72         uint8_t             *binaryBase,
73         uint32_t             kernelUID);
74 
75     //!
76     //! \brief  Allocate kernel required resources
77     //!
78     //! \return MOS_STATUS
79     //!         MOS_STATUS_SUCCESS if success
80     //!
81     virtual MOS_STATUS AllocateResources() = 0;
82 
83     //!
84     //! \brief  Get binding table count
85     //!
86     //! \return uint32_t
87     //!         Binding table count
88     //!
89     virtual uint32_t GetBTCount() = 0;
90 
91     //!
92     //! \brief  Get input/output surfaces allocated by kernel
93     //!
94     //! \param  [in] surfaceId
95     //!         uint32_t, surface index id
96     //! \return PMOS_SURFACE
97     //!         Pointer to MOS_SURFACE
98     //!
99     PMOS_SURFACE GetSurface(uint32_t surfaceId);
100 
101     //!
102     //! \brief  Get input/output surfaces allocated for HME kernel when using MDF RT
103     //!
104     //! \param  [in] surfaceId
105     //!         uint32_t, surface index id
106     //!         Pointer to CM 2D Surface
107     //!
GetCmSurface(uint32_t surfaceId)108     virtual CmSurface2D* GetCmSurface(uint32_t surfaceId) { return nullptr; };
109 
110 #if USE_CODECHAL_DEBUG_TOOL
DumpKernelOutput()111     virtual MOS_STATUS DumpKernelOutput() { return MOS_STATUS_SUCCESS; }
112 #endif
113 
114 protected:
115     //!
116     //! \brief  get kernel bianry address and size
117     //!
118     //! \param  [in] kernelBase
119     //!         base address of kernel binary
120     //! \param  [in] kernelUID
121     //!         kernel UID
122     //! \param  [out] kernelBinary
123     //!         kernel binary address for kernel UID
124     //! \param  [out] size
125     //!         kernel binary size
126     //!
127     //! \return MOS_STATUS
128     //!         MOS_STATUS_SUCCESS if success
129     //!
130     static MOS_STATUS GetKernelBinaryAndSize(
131         uint8_t * kernelBase,
132         uint32_t  kernelUID,
133         uint8_t **kernelBinary,
134         uint32_t *size);
135     //!
136     //! \brief  create kernel states
137     //!
138     //! \param  [out] kernelState
139     //!         pointer to MHW_KERNEL_STATE*
140     //! \param  [in] kernelIndex
141     //!         the sub kernel index in kernel state map
142     //! \param  [in] operation
143     //!         EncOperation,index used to get kernel header
144     //! \param  [in] kernelOffset
145     //!         kernel offset index
146     //!
147     //! \return MOS_STATUS
148     //!         MOS_STATUS_SUCCESS if success
149     //!
150     MOS_STATUS CreateKernelState(
151         MHW_KERNEL_STATE **           kernelState,
152         uint32_t                      kernelIndex,
153         EncOperation                  operation,
154         uint32_t                      kernelOffset);
155 
156     //!
157     //! \brief  Release kernel required resources
158     //!
159     //! \return MOS_STATUS
160     //!         MOS_STATUS_SUCCESS if success
161     //!
162     virtual MOS_STATUS ReleaseResources() = 0;
163 
164     //!
165     //! \brief  Define common pipeline to run the kernel
166     //!
167     //! \return MOS_STATUS
168     //!         MOS_STATUS_SUCCESS if success
169     //!
170     virtual MOS_STATUS Run();
171 
172     //!
173     //! \brief  Get curbe buffer size
174     //!
175     //! \return uint32_t
176     //!         Curbe buffer size
177     //!
178     virtual uint32_t GetCurbeSize() = 0;
179 
180     //!
181     //! \brief  Get DSH inline data length
182     //!
183     //! \return uint32_t
184     //!         Inline data length
185     //!
GetInlineDataLength()186     virtual uint32_t GetInlineDataLength() { return 0; }
187 
188     //!
189     //! \brief  Add perf tag for each kernel
190     //!
191     //! \return MOS_STATUS
192     //!         MOS_STATUS_SUCCESS if success
193     //!
194     virtual MOS_STATUS AddPerfTag() = 0;
195 
196     //!
197     //! \brief  Get the pointer to current active kernel state
198     //!
199     //! \return MHW_KERNEL_STATE*
200     //!         Pointer to MHW_KERNEL_STATE
201     //!
202     virtual MHW_KERNEL_STATE * GetActiveKernelState() = 0;
203 
204     //!
205     //! \brief  Get media state type of each kernel
206     //!
207     //! \return CODECHAL_MEDIA_STATE_TYPE
208     //!         media state type
209     //!
210     virtual CODECHAL_MEDIA_STATE_TYPE GetMediaStateType() = 0;
211 
212     //!
213     //! \brief  Set curbe data for current kernel state
214     //!
215     //! \param  [in] kernelState
216     //!         pointer to current active kernel state
217     //!
218     //! \return MOS_STATUS
219     //!         MOS_STATUS_SUCCESS if success
220     //!
SetCurbe(MHW_KERNEL_STATE * kernelState)221     virtual MOS_STATUS SetCurbe(MHW_KERNEL_STATE *kernelState) { return MOS_STATUS_UNIMPLEMENTED; };
222 
223     //!
224     //! \brief  Send input and output surfaces for current kernel
225     //!
226     //! \param  [in] cmd
227     //!         pointer to MOS_COMMAND_BUFFER
228     //! \param  [in] kernelState
229     //!         pointer to the MHW_KERNEL_STATE
230     //!
231     //! \return MOS_STATUS
232     //!         MOS_STATUS_SUCCESS if success
233     //!
234     virtual MOS_STATUS SendSurfaces(PMOS_COMMAND_BUFFER cmd, MHW_KERNEL_STATE *kernelState) = 0;
235 
236     //!
237     //! \brief  Initialize media walker parameters
238     //!
239     //! \param  [in] walkerParam
240     //!         Reference to CODECHAL_WALKEr_CODEC_PARAMS
241     //!
242     //! \return MOS_STATUS
243     //!         MOS_STATUS_SUCCESS if success
244     //!
245     virtual MOS_STATUS InitWalkerCodecParams(CODECHAL_WALKER_CODEC_PARAMS &walkerParam) = 0;
246 
247     //!
248     //! \brief  Utility function to clean up resource
249     //!
250     //! \param  [in] resource
251     //!         Pointer to MOS_RESOURCE
252     //! \param  [in] allocParam
253     //!         Pointer to MOS_ALLOC_GFXRES_PARAMS
254     //!
255     //! \return MOS_STATUS
256     //!         MOS_STATUS_SUCCESS if success
257     //!
258     MOS_STATUS CleanUpResource(PMOS_RESOURCE resource, PMOS_ALLOC_GFXRES_PARAMS allocParam);
259 
260     //!
261     //! \brief  Allocate 2D surface and store surface pointer in the pool
262     //!
263     //! \param  [in] param
264     //!         Pointer to MOS_ALLOC_GFXRES_PARAMS
265     //! \param  [in] surface
266     //!         Pointer to MOS_SURFACE
267     //! \param  [in] surfaceId
268     //!         uint32_t, index to surface
269     //!
270     //! \return MOS_STATUS
271     //!         MOS_STATUS_SUCCESS if success
272     //!
273     MOS_STATUS AllocateSurface(PMOS_ALLOC_GFXRES_PARAMS param, PMOS_SURFACE surface, uint32_t surfaceId);
274 
275 protected:
276     CodechalEncoderState      *m_encoder            = nullptr;  //!< Pointer to ENCODER base class
277     MOS_INTERFACE             *m_osInterface        = nullptr;  //!< OS interface
278     CodechalHwInterface       *m_hwInterface        = nullptr;  //!< HW interface
279     CodechalDebugInterface    *m_debugInterface     = nullptr;  //!< Debug interface
280     MhwMiInterface            *m_miInterface        = nullptr;  //!< Common MI interface
281     MhwRenderInterface        *m_renderInterface    = nullptr;  //!< Render engine interface
282     XMHW_STATE_HEAP_INTERFACE *m_stateHeapInterface = nullptr;  //!< State heap class interface
283 
284     KernelBinaryCallback       m_callback           = nullptr;  //!< kernel binary structure callback
285     uint8_t                   *m_kernelBinary       = nullptr;  //!< kernel bianry base address
286 
287     std::map<uint32_t, MHW_KERNEL_STATE*> m_kernelStatePool;     //!< pool to store kernel state with index
288     std::map<uint32_t, PMOS_SURFACE>      m_surfacePool;         //!< pool to store surface with index
289 
290     bool& m_firstTaskInPhase;
291     bool& m_lastTaskInPhase;
292     bool& m_singleTaskPhaseSupported;
293     bool& m_renderContextUsesNullHw;
294     bool& m_groupIdSelectSupported;
295     bool& m_fieldScalingOutputInterleaved;
296     bool& m_vdencEnabled;
297     uint8_t&  m_groupId;
298     uint32_t& m_maxBtCount;
299     uint32_t& m_vmeStatesSize;
300     uint32_t& m_storeData;
301     uint32_t& m_verticalLineStride;
302     uint32_t& m_downscaledWidthInMb4x;
303     uint32_t& m_downscaledHeightInMb4x;
304     uint32_t& m_downscaledWidthInMb16x;
305     uint32_t& m_downscaledHeightInMb16x;
306     uint32_t& m_downscaledWidthInMb32x;
307     uint32_t& m_downscaledHeightInMb32x;
308     uint32_t& m_mode;
309     uint16_t& m_pictureCodingType;
310     uint32_t& m_frameWidth;
311     uint32_t& m_frameHeight;
312     uint32_t& m_frameFieldHeight;
313     uint32_t& m_standard;
314     MHW_WALKER_MODE& m_walkerMode;
315 
316 };
317 
318 #endif /* __CODECHAL_KERNEL_BASE_H__ */
319