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