xref: /aosp_15_r20/external/intel-media-driver/media_softlet/agnostic/common/codec/hal/codechal_common.h (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
1 /*
2 * Copyright (c) 2021, 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_common.h
24 //! \brief    Defines the public interface for CodecHal.
25 //!
26 #ifndef __CODECHAL_COMMON_H__
27 #define __CODECHAL_COMMON_H__
28 
29 #include "mos_os.h"
30 #include "mos_util_debug.h"
31 #include "codec_def_common.h"
32 
33 class CodechalDebugInterface;
34 class CodechalHwInterfaceNext;
35 class CodechalSetting;
36 
37 //------------------------------------------------------------------------------
38 // Simplified macros for debug message, Assert, Null check and MOS eStatus check
39 // within Codechal without the need to explicitly pass comp and sub-comp name
40 //------------------------------------------------------------------------------
41 //------------------------------------------------------------------------------
42 // Macros specific to MOS_CODEC_SUBCOMP_PUBLIC sub-comp
43 //------------------------------------------------------------------------------
44 #define CODECHAL_PUBLIC_ASSERT(_expr)                                                   \
45     MOS_ASSERT(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_PUBLIC, _expr)
46 
47 #define CODECHAL_PUBLIC_ASSERTMESSAGE(_message, ...)                                    \
48     MOS_ASSERTMESSAGE(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_PUBLIC, _message, ##__VA_ARGS__)
49 
50 #define CODECHAL_PUBLIC_NORMALMESSAGE(_message, ...)                                    \
51     MOS_NORMALMESSAGE(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_PUBLIC, _message, ##__VA_ARGS__)
52 
53 #define CODECHAL_PUBLIC_VERBOSEMESSAGE(_message, ...)                                   \
54     MOS_VERBOSEMESSAGE(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_PUBLIC, _message, ##__VA_ARGS__)
55 
56 #define CODECHAL_PUBLIC_FUNCTION_ENTER                                                  \
57     MOS_FUNCTION_ENTER(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_PUBLIC)
58 
59 #define CODECHAL_PUBLIC_CHK_STATUS_RETURN(_stmt)                                        \
60     MOS_CHK_STATUS_RETURN(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_PUBLIC, _stmt)
61 
62 #define CODECHAL_PUBLIC_CHK_STATUS_MESSAGE(_stmt, _message, ...)                        \
63     MOS_CHK_STATUS_MESSAGE(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_PUBLIC, _stmt, _message, ##__VA_ARGS__)
64 
65 #define CODECHAL_PUBLIC_CHK_NULL_RETURN(_ptr)                                           \
66     MOS_CHK_NULL_RETURN(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_PUBLIC, _ptr)
67 
68 #define CODECHAL_PUBLIC_CHK_NULL_NO_STATUS_RETURN(_ptr)                                 \
69     MOS_CHK_NULL_NO_STATUS_RETURN(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_PUBLIC, _ptr)
70 
71 #define CODECHAL_PUBLIC_CHK_STATUS_WITH_DESTROY_RETURN(_stmt, destroyFunction)                                                                                    \
72     {                                                                                                                                                             \
73         MOS_STATUS sts = (MOS_STATUS)(_stmt);                                                                                                                     \
74         MOS_CHK_COND_WITH_DESTROY_RETURN_VALUE(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_ENCODE, (MOS_STATUS_SUCCESS != sts), destroyFunction, sts, "error status!") \
75     }
76 
77 typedef struct CODECHAL_SSEU_SETTING
78 {
79     uint8_t ui8NumSlices;
80     uint8_t ui8NumSubSlices;
81     uint8_t ui8NumEUs;
82     uint8_t reserved;  // Place holder for frequency setting
83 } CODECHAL_SSEU_SETTING, *PCODECHAL_SSEU_SETTING;
84 
85 //!
86 //! \class CodechalResLock
87 //! \brief Help function to lock the resource, the resource will be unlock automatically when this class destroy.
88 //!
89 class CodechalResLock
90 {
91 public:
92     //! \brief lock flag
93     enum LockFlag
94     {
95         readOnly     = 1,       //!< lock with read only
96         writeOnly    = 1 << 1,  //!< lock with write only
97         tiledAsTiled = 1 << 2,  //!< lock tile surface as tile format
98         noOverWrite  = 1 << 3,  //!< lock with no overwrite
99         noDecompress = 1 << 4,  //!< lock with no decompress
100         uncached     = 1 << 5,  //!< lock with uncached
101         forceCached  = 1 << 6   //!< lock with cache by force
102     };
103 
104     //!
105     //! \brief    Constructor
106     //!
CodechalResLock(PMOS_INTERFACE osInterface,PMOS_RESOURCE resource)107     CodechalResLock(PMOS_INTERFACE osInterface, PMOS_RESOURCE resource) : m_os(osInterface),
108                                                                           m_res(resource),
109                                                                           m_lockedPtr(nullptr)
110     {
111     }
112 
113     //!
114     //! \brief    Destructor
115     //!
~CodechalResLock()116     ~CodechalResLock()
117     {
118         if (m_os != nullptr && m_res != nullptr && m_lockedPtr != nullptr)
119         {
120             m_os->pfnUnlockResource(m_os, m_res);
121         }
122     }
123 
124     //!
125     //! \brief    Lock the resource
126     //! \details  Lock the resource, will be unlock automatically when this class destroy
127     //! \param  [in] lockFlags
128     //!         Combined lock flags, reference to enum LockFlag.
129     //!
130     //! \return   void pointer
131     //!           locked pointer if success, else return null pointer
132     //!
Lock(uint32_t lockFlags)133     void *Lock(uint32_t lockFlags)
134     {
135         if (m_os == nullptr || m_res == nullptr)
136         {
137             return nullptr;
138         }
139 
140         if (m_lockedPtr == nullptr)
141         {
142             MOS_LOCK_PARAMS mosLockFlags;
143             MOS_ZeroMemory(&mosLockFlags, sizeof(MOS_LOCK_PARAMS));
144 
145             if ((lockFlags & readOnly) == readOnly)
146             {
147                 mosLockFlags.ReadOnly = 1;
148             }
149 
150             if ((lockFlags & writeOnly) == writeOnly)
151             {
152                 mosLockFlags.WriteOnly = 1;
153             }
154 
155             if ((lockFlags & tiledAsTiled) == tiledAsTiled)
156             {
157                 mosLockFlags.TiledAsTiled = 1;
158             }
159 
160             if ((lockFlags & noOverWrite) == noOverWrite)
161             {
162                 mosLockFlags.NoOverWrite = 1;
163             }
164 
165             if ((lockFlags & noDecompress) == noDecompress)
166             {
167                 mosLockFlags.NoDecompress = 1;
168             }
169 
170             if ((lockFlags & uncached) == uncached)
171             {
172                 mosLockFlags.Uncached = 1;
173             }
174 
175             if ((lockFlags & forceCached) == forceCached)
176             {
177                 mosLockFlags.ForceCached = 1;
178             }
179 
180             m_lockedPtr = m_os->pfnLockResource(
181                 m_os,
182                 m_res,
183                 &mosLockFlags);
184         }
185 
186         return m_lockedPtr;
187     }
188 
189 private:
190     PMOS_INTERFACE m_os;
191     PMOS_RESOURCE  m_res;
192     void          *m_lockedPtr;
193 
194 MEDIA_CLASS_DEFINE_END(CodechalResLock)
195 };
196 
197 /*! \brief Settings used to create the CodecHal device.
198 */
199 typedef struct _CODECHAL_STANDARD_INFO
200 {
201     CODECHAL_FUNCTION CodecFunction;  //!< High level codec functionality requested.
202     uint32_t          Mode;           //!< Mode requested (high level combination between Standard and CodecFunction).
203     /*! \brief Applies to decode only, hybrid decoding requested.
204     *
205     *   Hybrid decoding uses EU kernels when FF HW is not available.
206     */
207     bool bIsHybridCodec;
208 } CODECHAL_STANDARD_INFO, *PCODECHAL_STANDARD_INFO;
209 
210 //!
211 //! \class Codechal
212 //! \brief This class defines the common member fields, functions etc as Codechal base class.
213 //!
214 class Codechal
215 {
216 public:
217     //!
218     //! \brief    Constructor
219     //! \param    [in] hwInterface
220     //!           Hardware interface
221     //! \param    [in] debugInterface
222     //!           Debug interface
223     //!
224     Codechal(
225         CodechalHwInterfaceNext *hwInterface,
226         CodechalDebugInterface *debugInterface);
227 
228     //!
229     //! \brief    Copy constructor
230     //!
231     Codechal(const Codechal&) = delete;
232 
233     //!
234     //! \brief    Copy assignment operator
235     //!
236     Codechal& operator=(const Codechal&) = delete;
237 
238     //!
239     //! \brief    Destructor
240     //!
241     virtual ~Codechal();
242 
243     //!
244     //! \brief    Allocate and intialize the Codechal.
245     //! \param    [in] codecHalSettings
246     //!           Settings used to finalize the creation of the CodecHal device
247     //! \return   MOS_STATUS
248     //!           MOS_STATUS_SUCCESS if success else fail reason
249     //!
250     virtual MOS_STATUS Allocate(CodechalSetting *codecHalSettings);
251 
252     //!
253     //! \brief    Signals the beginning of a picture.
254     //! \details  Initializes necessary parameters to perform the requested operation.
255     //! \return   MOS_STATUS
256     //!           MOS_STATUS_SUCCESS if success else fail reason
257     //!
258     virtual MOS_STATUS BeginFrame();
259 
260     //!
261     //! \brief    Signals the end of a picture.
262     //! \details  This function closes out the picture which was started by BeginFrame().
263     //!           All Execute() calls for a particular picture must be complete before
264     //!           EndFrame() is called. Resets all current picture parameters in
265     //!           preparation for the next BeginFrame(). For decode, in the case
266     //!           of incomplete frames, if the picture is still incomplete at EndFrame(),
267     //!           CodecHal conceals the error internally and submits the codec workload.
268     //! \return   MOS_STATUS
269     //!           MOS_STATUS_SUCCESS if success else fail reason
270     //!
271     virtual MOS_STATUS EndFrame();
272 
273     virtual MOS_STATUS Reformat();
274 
275     //!
276     //! \brief    Performs the operation requested by the codec function.
277     //! \param    [in] params
278     //!           Parameters need to perform the requested function. The parameter structure
279     //!           changes based on codec function.
280     //! \return   MOS_STATUS
281     //!           MOS_STATUS_SUCCESS if success else fail reason
282     //!
283     virtual MOS_STATUS Execute(void *params);
284 
285     //!
286     //! \brief    Gets available statuses for executed pictures.
287     //! \details  All pictures for which EndFrame() has been called are eligable
288     //!           for status reporting. Once a successful or error status is reported out by
289     //!           CodecHal, it is discarded.
290     //! \param    [out] status
291     //!           Array to store statuses up to a maximum of wNumStatus, valid pointer
292     //!           must be passed in to GetStatusReport()
293     //! \param    [in] numStatus
294     //!           The size of the pCodecStatus array
295     //! \return   MOS_STATUS
296     //!           MOS_STATUS_SUCCESS if success else fail reason
297     //!
298     virtual MOS_STATUS GetStatusReport(
299         void                *status,
300         uint16_t            numStatus);
301 
302     //!
303     //! \brief  Destroy codechl state
304     //!
305     //! \return void
306     //!
307     virtual void Destroy();
308 
309     //!
310     //! \brief    Resolve MetaData.
311     //! \details  Resolve MetaData from Input to Output.
312     //! \param    [out] pOutput
313     //!           Resolved Metadata resource.
314     //! \param    [in] pInput
315     //!           Metadata resource to be resolve.
316     //! \return   MOS_STATUS
317     //!           MOS_STATUS_SUCCESS if success else fail reason
318     //!
319     virtual MOS_STATUS ResolveMetaData(PMOS_RESOURCE pInput, PMOS_RESOURCE pOutput);
320 
321     //!
322     //! \brief    Report Error Flag.
323     //! \details  Report error flag to metadata buffer
324     //! \param    [in] pMetadataBuffer
325     //!           Metadata buffer.
326     //! \param    [in] size
327     //!           Metadata size.
328     //! \param    [in] offset
329     //!           Error flag offset in the metdata.
330     //! \param    [in] flag
331     //!           Flag to report.
332     //! \return   MOS_STATUS
333     //!           MOS_STATUS_SUCCESS if success else fail reason
334     //!
335     virtual MOS_STATUS ReportErrorFlag(PMOS_RESOURCE pMetadataBuffer, uint32_t size,
336                                        uint32_t offset, uint32_t flag);
337 
338     //!
339     //! \brief    Gets hardware interface.
340     //! \return   CodechalHwInterface
341     //!           return hardware interface
342     //!
GetHwInterface()343     CodechalHwInterfaceNext *GetHwInterface() { return m_hwInterface; }
344 
345     //!
346     //! \brief    Gets OS interface.
347     //! \return   PMOS_INTERFACE
348     //!           return OS interface
349     //!
GetOsInterface()350     PMOS_INTERFACE GetOsInterface() { return m_osInterface; }
351 
352     //!
353     //! \brief    Gets debug interface.
354     //! \return   CodechalDebugInterface
355     //!           return debug interface
356     //!
GetDebugInterface()357     CodechalDebugInterface *GetDebugInterface() { return m_debugInterface; }
358 
359     //!
360     //! \brief    Check if Apogeios enabled.
361     //! \return   bool
362     //!           return m_apogeiosEnable
363     //!
IsApogeiosEnabled()364     bool IsApogeiosEnabled() { return m_apogeiosEnable; }
365 protected:
366     //! \brief    HW Inteface
367     //! \details  Responsible for constructing all defined states and commands.
368     //!           Each HAL has a separate OS interface.
369     CodechalHwInterfaceNext *m_hwInterface = nullptr;
370 
371     //! \brief    Os Inteface
372     //! \details  Used to abstract all OS and KMD interactions such that CodecHal may be
373     //!           OS agnostic. Each HAL has a separate OS interface.
374     PMOS_INTERFACE          m_osInterface       = nullptr;
375 
376     //! \brief    Interface used for debug dumps.
377     //! \details  This interface is only valid for release internal and debug builds.
378     CodechalDebugInterface  *m_debugInterface   = nullptr;
379 
380     //! \brief    Interface used for debug dumps in GetStatusReport.
381     //! \details  This interface is only valid for release internal and debug builds.
382     CodechalDebugInterface  *m_statusReportDebugInterface   = nullptr;
383 
384     //! \brief    Indicates whether or not using null hardware
385     bool                    m_useNullHw[MOS_GPU_CONTEXT_MAX] = { false };
386 
387     //! \brief    Apogeios Enable Flag
388     bool                    m_apogeiosEnable = false;
389 
390     //!< usersettingInstance
391     MediaUserSettingSharedPtr   m_userSettingPtr = nullptr;
392 MEDIA_CLASS_DEFINE_END(Codechal)
393 };
394 
395 #endif
396