| 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 mos_cmdbufmgr.h |
| 24 | //! \brief Container class for the comamnd buffer manager |
| 25 | //! |
| 26 | |
| 27 | #ifndef __COMMAND_BUFFER_MANAGER_H__ |
| 28 | #define __COMMAND_BUFFER_MANAGER_H__ |
| 29 | |
| 30 | #include "mos_commandbuffer.h" |
| 31 | #include "mos_gpucontextmgr.h" |
| 32 | |
| 33 | //! |
| 34 | //! \class CmdBufMgr |
| 35 | //! |
| 36 | class CmdBufMgr |
| 37 | { |
| 38 | public: |
| 39 | //! |
| 40 | //! \brief Constructor |
| 41 | //! |
| 42 | CmdBufMgr(); |
| 43 | |
| 44 | //! |
| 45 | //! \brief Destructor |
| 46 | //! |
| 47 | ~CmdBufMgr(); |
| 48 | |
| 49 | //! |
| 50 | //! \brief Static entrypoint, get command buffer manager object |
| 51 | //! \return CmdBufMgr* |
| 52 | //! comamnd buffer manager object if success, else nullptr |
| 53 | //! |
| 54 | static CmdBufMgr* GetObject(); |
| 55 | |
| 56 | //! |
| 57 | //! \brief Initialize comamnd buffer manager object |
| 58 | //! \details This function mainly create initial cmd buffer as input size |
| 59 | //! \param [in] osContext |
| 60 | //! Pointer to the osContext handle |
| 61 | //! \param [in] cmdBufSize |
| 62 | //! initialized command buffer size |
| 63 | //! \return MOS_STATUS |
| 64 | //! MOS_STATUS_SUCCESS if success, else fail reason |
| 65 | //! |
| 66 | MOS_STATUS Initialize(OsContext *osContext, uint32_t cmdBufSize); |
| 67 | |
| 68 | //! |
| 69 | //! \brief Clean up the command buffer manager |
| 70 | //! \details This function mainly celar all allocated comamnd buffer in |
| 71 | //! available pool and in use pool |
| 72 | //! |
| 73 | void CleanUp(); |
| 74 | |
| 75 | //! |
| 76 | //! \brief Clean up the command buffer manager |
| 77 | //! \details This function will pick up one proper command buffer from |
| 78 | //! available pool, internal logic in below 3 conditions: |
| 79 | //! 1: if available pool has command buffer and its size bigger than |
| 80 | //! required, directly put it into in use pool and return; |
| 81 | //! 2: if available pool has command buffer but size smaller than |
| 82 | //! required, only create one command buffer as reqired and put |
| 83 | //! it to in use pool directly; |
| 84 | //! 3: if available pool is empty, will re-allocate bunch of command |
| 85 | //! buffers, buffer number base on m_initBufNum, buffer size |
| 86 | //! base on input required size. After re-allocate, put first buf |
| 87 | //! into inuse pool, remains push to available pool. |
| 88 | //! \param [in] size |
| 89 | //! Required command buffer size |
| 90 | //! \return CommandBuffer* |
| 91 | //! Proper comamnd bufffer pointer if success, other wise nullptr |
| 92 | //! |
| 93 | CommandBuffer *PickupOneCmdBuf(uint32_t size); |
| 94 | |
| 95 | //! |
| 96 | //! \brief insert the command buffer into available pool in proper location. |
| 97 | //! \details This function will find a the first cmd buffer which equal or |
| 98 | //! small cmd buf size in available pool, and then insert it. |
| 99 | //! \param [in] cmdBuf |
| 100 | //! command buffer to be released |
| 101 | //! |
| 102 | void UpperInsert(CommandBuffer *cmdBuf); |
| 103 | |
| 104 | //! |
| 105 | //! \brief Release command buffer from in-use status to standby status |
| 106 | //! \details This function designed for situations which need retire or |
| 107 | //! discard in use command buffer, it directly erase command buf |
| 108 | //! from in use pool and push it to available pool. If the command |
| 109 | //! buffer cannot be found inside in-use pool, some thing must be |
| 110 | //! wrong. |
| 111 | //! \param [in] cmdBuf |
| 112 | //! Command buffer need to be released |
| 113 | //! \return MOS_STATUS |
| 114 | //! MOS_STATUS_SUCCESS if success, other wise fail reason |
| 115 | //! |
| 116 | MOS_STATUS ReleaseCmdBuf(CommandBuffer *cmdBuf); |
| 117 | |
| 118 | //! |
| 119 | //! \brief Resize command buffer with required size |
| 120 | //! \param [in] cmdBufToResize |
| 121 | //! Command buffer need to be resized |
| 122 | //! \param [in] newSize |
| 123 | //! required new size |
| 124 | //! \return MOS_STATUS |
| 125 | //! MOS_STATUS_SUCCESS if success, other wise fail reason |
| 126 | //! |
| 127 | MOS_STATUS ResizeOneCmdBuf(CommandBuffer *cmdBufToResize, uint32_t newSize); |
| 128 | |
| 129 | //! |
| 130 | //! \brief Get the validity flag |
| 131 | //! \return bool |
| 132 | //! Get the validity flag of cmdBufMgr |
| 133 | //! |
| 134 | bool IsInitialized() |
| 135 | { |
| 136 | return m_initialized; |
| 137 | } |
| 138 | |
| 139 | private: |
| 140 | //! |
| 141 | //! \brief Self define compare method as std:sort input |
| 142 | //! \detail Command buffer size will be compared |
| 143 | //! \param [in] a |
| 144 | //! Input command buffer to be compared |
| 145 | //! \param [in] b |
| 146 | //! Input command buffer to be compared |
| 147 | //! \return bool |
| 148 | //! true if a size bigger than b, otherwise false |
| 149 | //! |
| 150 | static bool GreaterSizeSort(CommandBuffer *a, CommandBuffer *b) |
| 151 | { |
| 152 | return (a->GetCmdBufSize() > b->GetCmdBufSize()); |
| 153 | } |
| 154 | |
| 155 | //! \brief Max comamnd buffer number for per manager, including all |
| 156 | //! command buffer in availble pool and in-use pool |
| 157 | constexpr static uint32_t m_maxPoolSize = 1098304; |
| 158 | |
| 159 | //! \brief Current command buffer number in available and in-use pool |
| 160 | uint32_t m_cmdBufTotalNum = 0; |
| 161 | |
| 162 | //! \brief Command buffer number when bunch of re-allocate |
| 163 | constexpr static uint32_t m_bufIncStepSize = 8; |
| 164 | |
| 165 | //! \brief Initial command buffer number |
| 166 | constexpr static uint32_t m_initBufNum = 32; |
| 167 | |
| 168 | //! \brief Sorted List of available command buffer pool |
| 169 | std::vector<CommandBuffer *> m_availableCmdBufPool; |
| 170 | |
| 171 | //! \brief Mutex for available command buffer pool |
| 172 | PMOS_MUTEX m_availablePoolMutex = nullptr; |
| 173 | |
| 174 | //! \brief List of in used command buffer pool |
| 175 | std::vector<CommandBuffer *> m_inUseCmdBufPool; |
| 176 | |
| 177 | //! \brief Mutex for in-use command buffer pool |
| 178 | PMOS_MUTEX m_inUsePoolMutex = nullptr; |
| 179 | |
| 180 | //! \brief Flag to indicate cmd buf mgr initialized or not |
| 181 | bool m_initialized = false; |
| 182 | |
| 183 | //! \brief Corresponding os context |
| 184 | OsContext *m_osContext = nullptr; |
| 185 | }; |
| 186 | #endif // __COMMAND_BUFFER_MANAGER_H__ |