1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
/*
* Copyright (c) 2018, Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
//!
//! \file mos_gpucontextmgr.cpp
//! \brief Container class for the basic gpu context manager
//!
#include "mos_gpucontextmgr.h"
#include "mos_gpucontext_specific.h"
#include "mos_graphicsresource_specific.h"
GpuContextMgr::GpuContextMgr(GT_SYSTEM_INFO *gtSystemInfo, OsContext *osContext)
{
MOS_OS_FUNCTION_ENTER;
m_initialized = false;
m_gpuContextArrayMutex = MOS_CreateMutex();
MOS_OS_CHK_NULL_NO_STATUS_RETURN(m_gpuContextArrayMutex);
MOS_LockMutex(m_gpuContextArrayMutex);
m_gpuContextArray.clear();
MOS_UnlockMutex(m_gpuContextArrayMutex);
if (gtSystemInfo)
{
MOS_SecureMemcpy(&m_gtSystemInfo, sizeof(GT_SYSTEM_INFO), gtSystemInfo, sizeof(GT_SYSTEM_INFO));
}
else
{
MOS_OS_ASSERTMESSAGE("Input GTSystemInfo cannot be nullptr");
return;
}
if (osContext)
{
m_osContext = osContext;
}
else
{
MOS_OS_ASSERTMESSAGE("Input osContext cannot be nullptr");
return;
}
m_initialized = true;
}
GpuContextMgr::~GpuContextMgr()
{
MOS_OS_FUNCTION_ENTER;
if (m_gpuContextArrayMutex)
{
MOS_DestroyMutex(m_gpuContextArrayMutex);
m_gpuContextArrayMutex = nullptr;
}
}
GpuContextMgr *GpuContextMgr::GetObject(
GT_SYSTEM_INFO *gtSystemInfo,
OsContext * osContext)
{
MOS_OS_FUNCTION_ENTER;
if (gtSystemInfo == nullptr || osContext == nullptr)
{
MOS_OS_ASSERTMESSAGE("Invalid input parameters!");
return nullptr;
}
return MOS_New(GpuContextMgr, gtSystemInfo, osContext);
}
void GpuContextMgr::CleanUp()
{
MOS_OS_FUNCTION_ENTER;
if (m_initialized)
{
DestroyAllGpuContexts();
MOS_LockMutex(m_gpuContextArrayMutex);
m_gpuContextArray.clear();
MOS_UnlockMutex(m_gpuContextArrayMutex);
m_initialized = false;
}
return;
}
bool GpuContextMgr::ContextReuseNeeded()
{
MOS_OS_FUNCTION_ENTER;
// to be added after scalable design is nailed down
return false;
}
GpuContext *GpuContextMgr::SelectContextToReuse()
{
MOS_OS_FUNCTION_ENTER;
// to be added after scalable design is nailed down
return nullptr;
}
GpuContext *GpuContextMgr::CreateGpuContext(
const MOS_GPU_NODE gpuNode,
CmdBufMgr * cmdBufMgr,
MOS_GPU_CONTEXT mosGpuCtx)
{
MOS_OS_FUNCTION_ENTER;
if (cmdBufMgr == nullptr)
{
MOS_OS_ASSERTMESSAGE("nullptr of cmdbufmgr.");
return nullptr;
}
GpuContext *reusedContext = nullptr;
if (ContextReuseNeeded())
{
reusedContext = SelectContextToReuse();
}
GpuContext *gpuContext = GpuContext::Create(gpuNode, mosGpuCtx, cmdBufMgr, reusedContext);
if (gpuContext == nullptr)
{
MOS_OS_ASSERTMESSAGE("nullptr returned by GpuContext::Create.");
return nullptr;
}
MOS_LockMutex(m_gpuContextArrayMutex);
GPU_CONTEXT_HANDLE gpuContextHandle = 0;
if (m_noCycledGpuCxtMgmt)
{
// new created context at the end of m_gpuContextArray.
gpuContextHandle = m_gpuContextArray.size() ? m_gpuContextArray.size() : 0;
}
else
{
// Directly replace nullptr with new created context in m_gpuContextArray.
GpuContext *curGpuContext = nullptr;
int index = 0;
for (auto &curGpuContext : m_gpuContextArray)
{
if (curGpuContext == nullptr)
{
break;
}
index++;
}
gpuContextHandle = m_gpuContextArray.size() ? index : 0;
}
gpuContext->SetGpuContextHandle(gpuContextHandle);
if (gpuContextHandle == m_gpuContextArray.size())
{
m_gpuContextArray.push_back(gpuContext);
}
else
{
m_gpuContextArray[gpuContextHandle] = gpuContext;
}
m_gpuContextCount++;
MOS_UnlockMutex(m_gpuContextArrayMutex);
return gpuContext;
}
GpuContext *GpuContextMgr::GetGpuContext(GPU_CONTEXT_HANDLE gpuContextHandle)
{
MOS_OS_FUNCTION_ENTER;
if (gpuContextHandle == MOS_GPU_CONTEXT_INVALID_HANDLE)
{
MOS_OS_ASSERTMESSAGE("Input gpucontext handle cannot be MOS_GPU_CONTEXT_INVALID_HANDLE!");
return nullptr;
}
if (!m_gpuContextArray.empty() && gpuContextHandle <= m_gpuContextArray.size())
{
return m_gpuContextArray.at(gpuContextHandle);
}
else
{
MOS_OS_ASSERTMESSAGE("GPU context array is empty or got invalid index, something must be wrong!");
return nullptr;
}
}
void GpuContextMgr::DestroyGpuContext(GpuContext *gpuContext)
{
MOS_OS_FUNCTION_ENTER;
MOS_OS_CHK_NULL_NO_STATUS_RETURN(gpuContext);
GpuContext *curGpuContext = nullptr;
bool found = false;
MOS_LockMutex(m_gpuContextArrayMutex);
for (auto &curGpuContext : m_gpuContextArray)
{
if (curGpuContext == gpuContext)
{
found = true;
// to keep original order, here should not erase gpucontext, replace with nullptr in array.
MOS_Delete(curGpuContext); // delete gpu context.
m_gpuContextCount--;
break;
}
}
if (m_gpuContextCount == 0 && !m_noCycledGpuCxtMgmt)
{
m_gpuContextArray.clear(); // clear whole array
}
MOS_UnlockMutex(m_gpuContextArrayMutex);
if (!found)
{
MOS_OS_ASSERTMESSAGE("cannot find specified gpuContext in the gpucontext pool, something must be wrong");
}
}
void GpuContextMgr::DestroyAllGpuContexts()
{
MOS_OS_FUNCTION_ENTER;
GpuContext *curGpuContext = nullptr;
MOS_LockMutex(m_gpuContextArrayMutex);
// delete each instance in m_gpuContextArray
for (auto &curGpuContext : m_gpuContextArray)
{
MOS_Delete(curGpuContext);
}
m_gpuContextArray.clear(); // clear whole array
MOS_UnlockMutex(m_gpuContextArrayMutex);
}
|