xref: /aosp_15_r20/external/intel-media-driver/media_softlet/agnostic/common/os/mos_gpucontextmgr_next.h (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
1 /*
2 * Copyright (c) 2019, 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_gpucontextmgr_next.h
24 //! \brief    Container class for the gpu context manager
25 //!
26 
27 #ifndef __MOS_GPU_CONTEXT_MGR_NEXT_H__
28 #define __MOS_GPU_CONTEXT_MGR_NEXT_H__
29 
30 #include "mos_defs.h"
31 #include "mos_gpucontext_next.h"
32 
33 class OsContextNext;
34 //!
35 //! \class  GpuContextMgr
36 //!
37 class GpuContextMgrNext
38 {
39 public:
40     //!
41     //! \brief  Constructor
42     //!
43     GpuContextMgrNext(OsContextNext *osContext);
44 
45     //!
46     //! \brief    Copy constructor
47     //!
48     GpuContextMgrNext(const GpuContextMgrNext&) = delete;
49 
50     //!
51     //! \brief    Copy assignment operator
52     //!
53     GpuContextMgrNext& operator=(const GpuContextMgrNext&) = delete;
54 
55     //!
56     //! \brief  Destructor
57     //!
58     virtual ~GpuContextMgrNext();
59 
60     //!
61     //! \brief    Static entrypoint, get gpu context manager object
62     //! \return   GpuContextMgrNext*
63     //!           gpu context manager specific object if success, else nullptr
64     //!
65     static GpuContextMgrNext* GetObject(
66         OsContextNext      *osContext);
67 
68     //!
69     //! \brief    Initialized the gpu context manager
70     //!
71     //!
72     virtual MOS_STATUS Initialize();
73 
74     //!
75     //! \brief    Clean up the gpu context manager
76     //! \details  This function mainly celar all allocated gpu context in
77     //!           apu context array
78     //!
79     void CleanUp();
80 
81     //!
82     //! \brief    Create GPU context
83     //! \param    [in] gpuNode
84     //!           Reqired gpu node
85     //! \param    [in] cmdBufMgr
86     //!           Command buffer manager
87     //! \return   GpuContext*
88     //!           Gpu context pointer if success, otherwise nullptr
89     //!
90     virtual GpuContextNext* CreateGpuContext(
91         const MOS_GPU_NODE gpuNode,
92         CmdBufMgrNext     *cmdBufMgr);
93 
94     //!
95     //! \brief    Get GPU context base on gpu context handle
96     //! \detail   Gpu context manager maintain an array, gpu context handle
97     //!           as its index.
98     //! \param    [in] gpuContextHandle
99     //!           Reqired gpu context's handle
100     //! \return   GpuContext*
101     //!           Gpu context pointer if success, otherwise nullptr
102     //!
103     virtual GpuContextNext* GetGpuContext(GPU_CONTEXT_HANDLE gpuContextHandle);
104 
105     //!
106     //! \brief    Destroy specified gpu context
107     //! \param    [in] gpuContext
108     //!           Gpu context need to be destroyed
109     //!
110     virtual void DestroyGpuContext(GpuContextNext *gpuContext);
111 
112     //!
113     //! \brief    Destroy all gpu context instance
114     //! \detail   Destroy all gpu context maintained in m_gpuContextArray
115     //!
116     void DestroyAllGpuContexts();
117 
118     //!
119     //! \brief    Determine whether gpu context reuse is needed
120     //! \detail   Implementation to be added after reuse scheme is nailed down
121     //! \return   bool
122     //!           True if needed, otherwise false
123     //!
124     bool ContextReuseNeeded();
125 
126     //!
127     //! \brief    Select one gpu context to be reused
128     //! \detail   Implementation to be added after reuse scheme is nailed down
129     //! \return   GpuContext*
130     //!           Gpu context pointer if success, otherwise nullptr
131     //!
132     GpuContextNext* SelectContextToReuse();
133 
134     //!
135     //! \brief    Get os context used in manager
136     //! \return   OsContext*
137     //!           Os context pointer if success, otherwise nullptr
138     //!
GetOsContext()139     OsContextNext* GetOsContext(){ return m_osContext; }
140 
GetGpuContextMap()141     std::map<GPU_CONTEXT_HANDLE, GpuContextNext *> &GetGpuContextMap()
142     {
143         return m_gpuContextMap;
144     }
145         //!
146     //! \brief    Get the validity flag
147     //! \return   bool
148     //!           Get the validity flag of GpuContextMgrNext
149     //!
IsInitialized()150     bool IsInitialized()
151     {
152         return m_initialized;
153     }
154 
GetGpuContextArrayMutex()155     PMOS_MUTEX GetGpuContextArrayMutex()
156     {
157         return m_gpuContextArrayMutex;
158     }
159 
160     //! \brief   Indicate whether new gpu context is inserted into the first slot w/ null ctx handle
161     //!          or always at the end of the gpucontext array
162     bool m_noCycledGpuCxtMgmt = false;
163 
164 protected:
165     GpuContextMgrNext() = default;
166 
167     //! \brief    Os Context
168     OsContextNext *m_osContext = nullptr;
169 
170     //! \brief    Gpu context array mutex
171     PMOS_MUTEX m_gpuContextArrayMutex = nullptr;
172 
173     //! \brief    Gpu context count
174     uint32_t m_gpuContextCount = 0;
175     uint32_t m_gpuContextHanleForNonCycledCase = 0;
176     //! \brief    Maintained gpu context array
177     std::map<GPU_CONTEXT_HANDLE, GpuContextNext *> m_gpuContextMap;
178 
179     //! \brief   Flag to indicate gpu context mgr initialized or not
180     bool m_initialized = false;
181 
182 MEDIA_CLASS_DEFINE_END(GpuContextMgrNext)
183 };
184 
185 #endif  // #ifndef __MOS_GPU_CONTEXT_MGR_NEXT_H__
186