xref: /aosp_15_r20/external/intel-media-driver/media_driver/agnostic/common/os/mos_graphicsresource.cpp (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
1 /*
2 * Copyright (c) 2017-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    mos_graphicsresource.cpp
24 //! \brief   Container class for the basic gpu resource
25 //!
26 
27 #include "mos_graphicsresource.h"
28 #include "mos_graphicsresource_specific.h"
29 #include "mos_util_debug.h"
30 #include "mos_utilities.h"
31 #include <string>
32 #include <fcntl.h>
33 #include <tuple>
34 
35 uint32_t GraphicsResource::m_memAllocCounterGfx;
36 
GraphicsResource()37 GraphicsResource::GraphicsResource()
38 {
39     MOS_OS_FUNCTION_ENTER;
40     m_allocationIndexMutex = MosUtilities::MosCreateMutex();
41     MOS_OS_CHK_NULL_NO_STATUS_RETURN(m_allocationIndexMutex);
42 }
43 
~GraphicsResource()44 GraphicsResource::~GraphicsResource()
45 {
46     MOS_OS_FUNCTION_ENTER;
47     MosUtilities::MosDestroyMutex(m_allocationIndexMutex);
48     m_allocationIndexMutex = nullptr;
49 }
50 
Dump(OsContext * osContextPtr,uint32_t overrideOffset,uint32_t overrideSize,std::string outputFileName,std::string outputPath)51 MOS_STATUS GraphicsResource::Dump(OsContext* osContextPtr, uint32_t overrideOffset, uint32_t overrideSize, std::string outputFileName, std::string outputPath)
52 {
53     MOS_OS_FUNCTION_ENTER;
54 
55     MOS_STATUS  eStatus = MOS_STATUS_SUCCESS;
56     if (overrideSize == 0)
57     {
58         eStatus = MOS_STATUS_UNKNOWN;
59         return eStatus;
60     }
61 
62     char        sPath[MOS_MAX_PATH_LENGTH + 1];
63     uint32_t    dwWritten = 0;
64     MOS_SecureStringPrint(
65         sPath,
66         sizeof(sPath),
67         sizeof(sPath) - 1,
68         "%s%s",
69         outputPath.c_str(),
70         outputFileName.c_str());
71 
72     void*       hFile = nullptr;
73     // Open file for writing
74     eStatus = MosUtilities::MosCreateFile(
75         &hFile,
76         sPath,
77         O_WRONLY|O_CREAT);
78 
79     if (eStatus != MOS_STATUS_SUCCESS)
80     {
81         MOS_OS_ASSERTMESSAGE("Failed to open file '%s'.", sPath);
82         eStatus = MOS_STATUS_FILE_OPEN_FAILED;
83         if (hFile != nullptr)
84         {
85             MosUtilities::MosCloseHandle(hFile);
86         }
87         return eStatus;
88     }
89 
90     char*       pbData = nullptr;
91     LockParams  params = {0};
92     params.m_readRequest = true;
93     pbData = (char *)this->Lock(osContextPtr, params);
94     if (pbData == nullptr)
95     {
96         MOS_OS_ASSERTMESSAGE("Failed to lock the gpu resource");
97         if (hFile != nullptr)
98         {
99             MosUtilities::MosCloseHandle(hFile);
100         }
101         return MOS_STATUS_UNKNOWN;
102     }
103 
104     pbData += overrideOffset;
105 
106     // Write the file
107     if ((eStatus = MosUtilities::MosWriteFile(
108         hFile,
109         pbData,
110         overrideSize,
111         &dwWritten,
112         nullptr)) != MOS_STATUS_SUCCESS)
113     {
114         MOS_OS_ASSERTMESSAGE("Failed to write to file '%s'.", sPath);
115         if (hFile != nullptr)
116         {
117             MosUtilities::MosCloseHandle(hFile);
118             hFile = nullptr;
119         }
120     }
121 
122     eStatus = this->Unlock(osContextPtr);
123     if (eStatus != MOS_STATUS_SUCCESS)
124     {
125         MOS_OS_ASSERTMESSAGE("Failed to unlock the gpu resource");
126     }
127 
128     if (hFile != nullptr)
129     {
130         MosUtilities::MosCloseHandle(hFile);
131     }
132 
133     return eStatus;
134 }
135 
CreateGraphicResource(GraphicsResource::ResourceType resourceType)136 class GraphicsResource* GraphicsResource::CreateGraphicResource(GraphicsResource::ResourceType resourceType)
137 {
138     MOS_OS_FUNCTION_ENTER;
139 
140     class GraphicsResource* pResource = nullptr;
141 
142     switch (resourceType)
143     {
144     case osSpecificResource:
145         pResource = MOS_New(GraphicsResourceSpecific);
146         break;
147     default:
148         MOS_OS_ASSERTMESSAGE("Unknown Graphic Reosurce type %u passed in", resourceType);
149         pResource = nullptr;
150     }
151 
152     return pResource;
153 }
154 
GetAllocationIndex(GPU_CONTEXT_HANDLE gpuContextHandle)155 int32_t GraphicsResource::GetAllocationIndex(GPU_CONTEXT_HANDLE gpuContextHandle)
156 {
157     MOS_OS_FUNCTION_ENTER;
158 
159     GPU_CONTEXT_HANDLE curGpuContext = 0;
160     int32_t curAllocIndex            = MOS_INVALID_ALLOC_INDEX;
161     int32_t ret                      = MOS_INVALID_ALLOC_INDEX;
162 
163     MosUtilities::MosLockMutex(m_allocationIndexMutex);
164     for (auto& curAllocationIndexTp : m_allocationIndexArray)
165     {
166         std::tie(curGpuContext, curAllocIndex) = curAllocationIndexTp ;
167         if (curGpuContext == gpuContextHandle)
168         {
169              ret = curAllocIndex;
170              break;
171         }
172     }
173 
174     MosUtilities::MosUnlockMutex(m_allocationIndexMutex);
175     return ret;
176 }
177 
ResetResourceAllocationIndex()178 void GraphicsResource::ResetResourceAllocationIndex()
179 {
180     MOS_OS_FUNCTION_ENTER;
181     MosUtilities::MosLockMutex(m_allocationIndexMutex);
182     m_allocationIndexArray.clear();
183     MosUtilities::MosUnlockMutex(m_allocationIndexMutex);
184 }
185 
186