Debian Codegraph @

opencl/source/sharings/gl/linux/gl_buffer_linux.cpp

raw
1 /*
2 * Copyright (C) 2023-2024 Intel Corporation
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 */
7
8 #include "shared/source/execution_environment/root_device_environment.h"
9 #include "shared/source/gmm_helper/gmm.h"
10 #include "shared/source/helpers/get_info.h"
11 #include "shared/source/memory_manager/allocation_properties.h"
12 #include "shared/source/memory_manager/memory_manager.h"
13
14 #include "opencl/extensions/public/cl_gl_private_intel.h"
15 #include "opencl/source/cl_device/cl_device.h"
16 #include "opencl/source/context/context.h"
17 #include "opencl/source/mem_obj/buffer.h"
18 #include "opencl/source/sharings/gl/gl_buffer.h"
19 #include "opencl/source/sharings/gl/linux/gl_sharing_linux.h"
20
21 #include "config.h"
22
23 using namespace NEO;
24
25 Buffer *GlBuffer::createSharedGlBuffer(Context *context, cl_mem_flags flags, unsigned int bufferId, cl_int *errcodeRet) {
26 ErrorCodeHelper errorCode(errcodeRet, CL_SUCCESS);
27
28 /* Prepare export request */
29 struct mesa_glinterop_export_in objIn = {};
30 struct mesa_glinterop_export_out objOut = {};
31
32 objIn.version = 2;
33 objIn.target = GL_ARRAY_BUFFER;
34 objIn.obj = bufferId;
35
36 switch (flags) {
37 case CL_MEM_READ_ONLY:
38 objIn.access = MESA_GLINTEROP_ACCESS_READ_ONLY;
39 break;
40 case CL_MEM_WRITE_ONLY:
41 objIn.access = MESA_GLINTEROP_ACCESS_WRITE_ONLY;
42 break;
43 case CL_MEM_READ_WRITE:
44 objIn.access = MESA_GLINTEROP_ACCESS_READ_WRITE;
45 break;
46 default:
47 errorCode.set(CL_INVALID_VALUE);
48 return nullptr;
49 }
50
51 objOut.version = 2;
52
53 /* Call MESA interop */
54 GLSharingFunctionsLinux *sharingFunctions = context->getSharing<GLSharingFunctionsLinux>();
55
56 int retValue = sharingFunctions->exportObject(&objIn, &objOut);
57 if ((retValue != MESA_GLINTEROP_SUCCESS) || (objOut.version != 2)) {
58 switch (retValue) {
59 case MESA_GLINTEROP_INVALID_DISPLAY:
60 case MESA_GLINTEROP_INVALID_CONTEXT:
61 errorCode.set(CL_INVALID_CONTEXT);
62 break;
63 case MESA_GLINTEROP_INVALID_OBJECT:
64 errorCode.set(CL_INVALID_GL_OBJECT);
65 break;
66 case MESA_GLINTEROP_OUT_OF_HOST_MEMORY:
67 errorCode.set(CL_OUT_OF_HOST_MEMORY);
68 break;
69 case MESA_GLINTEROP_OUT_OF_RESOURCES:
70 default:
71 errorCode.set(CL_OUT_OF_RESOURCES);
72 break;
73 }
74
75 return nullptr;
76 }
77
78 /* Map result for rest of the function */
79 CL_GL_BUFFER_INFO bufferInfo = {};
80 bufferInfo.bufferName = bufferId;
81 bufferInfo.globalShareHandle = static_cast<unsigned int>(objOut.dmabuf_fd);
82 bufferInfo.bufferSize = static_cast<GLint>(objOut.buf_size);
83 bufferInfo.bufferOffset = static_cast<GLint>(objOut.buf_offset);
84
85 auto graphicsAllocation = GlBuffer::createGraphicsAllocation(context, bufferId, bufferInfo);
86 if (!graphicsAllocation) {
87 errorCode.set(CL_INVALID_GL_OBJECT);
88 return nullptr;
89 }
90
91 auto glHandler = new GlBuffer(sharingFunctions, bufferId);
92 auto rootDeviceIndex = graphicsAllocation->getRootDeviceIndex();
93 auto multiGraphicsAllocation = MultiGraphicsAllocation(rootDeviceIndex);
94 multiGraphicsAllocation.addAllocation(graphicsAllocation);
95
96 return Buffer::createSharedBuffer(context, flags, glHandler, std::move(multiGraphicsAllocation));
97 }
98
99 void GlBuffer::synchronizeObject(UpdateData &updateData) {
100 auto sharingFunctions = static_cast<GLSharingFunctionsLinux *>(this->sharingFunctions);
101
102 /* Prepare flush request */
103 struct mesa_glinterop_export_in objIn = {};
104 struct mesa_glinterop_flush_out syncOut = {};
105 int fenceFd = -1;
106
107 objIn.version = 2;
108 objIn.target = GL_ARRAY_BUFFER;
109 objIn.obj = this->clGlObjectId;
110
111 syncOut.version = 1;
112 syncOut.fence_fd = &fenceFd;
113
114 bool success = sharingFunctions->flushObjectsAndWait(1, &objIn, &syncOut);
115 updateData.synchronizationStatus = success ? SynchronizeStatus::ACQUIRE_SUCCESFUL : SynchronizeStatus::SYNCHRONIZE_ERROR;
116 }
117
118 void GlBuffer::resolveGraphicsAllocationChange(osHandle currentSharedHandle, UpdateData *updateData) {
119 const auto memObject = updateData->memObject;
120 if (currentSharedHandle != updateData->sharedHandle) {
121 const auto bufferInfo = std::unique_ptr<CL_GL_BUFFER_INFO>(static_cast<CL_GL_BUFFER_INFO *>(updateData->updateData));
122
123 auto oldGraphicsAllocation = memObject->getGraphicsAllocation(updateData->rootDeviceIndex);
124 popGraphicsAllocationFromReuse(oldGraphicsAllocation);
125
126 Context *context = memObject->getContext();
127 auto newGraphicsAllocation = createGraphicsAllocation(context, clGlObjectId, *bufferInfo);
128 if (newGraphicsAllocation == nullptr) {
129 updateData->synchronizationStatus = SynchronizeStatus::SYNCHRONIZE_ERROR;
130 memObject->removeGraphicsAllocation(updateData->rootDeviceIndex);
131 } else {
132 updateData->synchronizationStatus = SynchronizeStatus::ACQUIRE_SUCCESFUL;
133 memObject->resetGraphicsAllocation(newGraphicsAllocation);
134 }
135
136 if (updateData->synchronizationStatus == SynchronizeStatus::ACQUIRE_SUCCESFUL) {
137 memObject->getGraphicsAllocation(updateData->rootDeviceIndex)->setAllocationOffset(bufferInfo->bufferOffset);
138 }
139 }
140 }
141
142 void GlBuffer::popGraphicsAllocationFromReuse(GraphicsAllocation *graphicsAllocation) {
143 auto sharingFunctions = static_cast<GLSharingFunctionsLinux *>(this->sharingFunctions);
144
145 std::unique_lock<std::mutex> lock(sharingFunctions->mutex);
146
147 auto &graphicsAllocations = sharingFunctions->graphicsAllocationsForGlBufferReuse;
148 auto foundIter = std::find_if(graphicsAllocations.begin(), graphicsAllocations.end(),
149 [&graphicsAllocation](const std::pair<unsigned int, GraphicsAllocation *> &entry) {
150 return entry.second == graphicsAllocation;
151 });
152 if (foundIter != graphicsAllocations.end()) {
153 std::iter_swap(foundIter, graphicsAllocations.end() - 1);
154 graphicsAllocations.pop_back();
155 }
156 graphicsAllocation->decReuseCount();
157 }
158
159 void GlBuffer::releaseReusedGraphicsAllocation() {
160 auto sharingFunctions = static_cast<GLSharingFunctionsLinux *>(this->sharingFunctions);
161
162 std::unique_lock<std::mutex> lock(sharingFunctions->mutex);
163
164 auto &allocationsVector = sharingFunctions->graphicsAllocationsForGlBufferReuse;
165 auto itEnd = allocationsVector.end();
166 for (auto it = allocationsVector.begin(); it != itEnd; it++) {
167 if (it->first == clGlObjectId) {
168 it->second->decReuseCount();
169 if (it->second->peekReuseCount() == 0) {
170 std::iter_swap(it, itEnd - 1);
171 allocationsVector.pop_back();
172 }
173 break;
174 }
175 }
176 }
177
178 GraphicsAllocation *GlBuffer::createGraphicsAllocation(Context *context, unsigned int bufferId, _tagCLGLBufferInfo &bufferInfo) {
179 GLSharingFunctionsLinux *sharingFunctions = context->getSharing<GLSharingFunctionsLinux>();
180 auto &allocationsVector = sharingFunctions->graphicsAllocationsForGlBufferReuse;
181 GraphicsAllocation *graphicsAllocation = nullptr;
182 bool reusedAllocation = false;
183
184 std::unique_lock<std::mutex> lock(sharingFunctions->mutex);
185 auto endIter = allocationsVector.end();
186
187 auto foundIter =
188 std::find_if(allocationsVector.begin(), endIter,
189 [&bufferId](const std::pair<unsigned int, GraphicsAllocation *> &entry) { return entry.first == bufferId; });
190
191 if (foundIter != endIter) {
192 graphicsAllocation = foundIter->second;
193 reusedAllocation = true;
194 }
195
196 if (!graphicsAllocation) {
197 AllocationProperties properties = {context->getDevice(0)->getRootDeviceIndex(),
198 false, // allocateMemory
199 0u, // size
200 AllocationType::sharedBuffer,
201 false, // isMultiStorageAllocation
202 context->getDeviceBitfieldForAllocation(context->getDevice(0)->getRootDeviceIndex())};
203 // couldn't find allocation for reuse - create new
204 MemoryManager::OsHandleData osHandleData{bufferInfo.globalShareHandle};
205 graphicsAllocation =
206 context->getMemoryManager()->createGraphicsAllocationFromSharedHandle(osHandleData, properties, false, false, false, nullptr);
207 }
208
209 if (!graphicsAllocation) {
210 return nullptr;
211 }
212 graphicsAllocation->incReuseCount(); // decremented in releaseReusedGraphicsAllocation() called from MemObj destructor
213
214 if (!reusedAllocation) {
215 sharingFunctions->graphicsAllocationsForGlBufferReuse.push_back(std::make_pair(bufferId, graphicsAllocation));
216 if (bufferInfo.pGmmResInfo) {
217 DEBUG_BREAK_IF(graphicsAllocation->getDefaultGmm() != nullptr);
218 auto helper = context->getDevice(0)->getRootDeviceEnvironment().getGmmHelper();
219 graphicsAllocation->setDefaultGmm(new Gmm(helper, bufferInfo.pGmmResInfo));
220 } else {
221 auto helper = context->getDevice(0)->getRootDeviceEnvironment().getGmmHelper();
222 StorageInfo storageInfo = {};
223 GmmRequirements gmmRequirements{};
224
225 graphicsAllocation->setDefaultGmm(new Gmm(helper,
226 nullptr, bufferInfo.bufferSize, 1, GMM_RESOURCE_USAGE_UNKNOWN, storageInfo, gmmRequirements));
227 }
228 }
229
230 return graphicsAllocation;
231 }
232
233 void GlBuffer::releaseResource(MemObj *memObject, uint32_t rootDeviceIndex) {
234 auto memoryManager = memObject->getMemoryManager();
235 memoryManager->closeSharedHandle(memObject->getGraphicsAllocation(rootDeviceIndex));
236 }
237
238 void GlBuffer::callReleaseResource(bool createOrDestroy) {}