Debian Codegraph @

opencl/test/unit_test/sharings/gl/windows/gl_reused_buffers_tests.cpp

raw
1 /*
2 * Copyright (C) 2018-2024 Intel Corporation
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 */
7
8 #include "shared/source/gmm_helper/gmm.h"
9 #include "shared/source/gmm_helper/resource_info.h"
10 #include "shared/test/common/mocks/mock_memory_manager.h"
11 #include "shared/test/common/test_macros/hw_test.h"
12
13 #include "opencl/source/cl_device/cl_device.h"
14 #include "opencl/source/mem_obj/buffer.h"
15 #include "opencl/source/sharings/gl/gl_buffer.h"
16 #include "opencl/test/unit_test/mocks/gl/windows/mock_gl_sharing_windows.h"
17 #include "opencl/test/unit_test/mocks/mock_context.h"
18
19 using namespace NEO;
20
21 struct GlReusedBufferTests : public ::testing::Test {
22 void SetUp() override {
23 glSharingFunctions = new GlSharingFunctionsMock();
24
25 context.setSharingFunctions(glSharingFunctions);
26 rootDeviceIndex = context.getDevice(0)->getRootDeviceIndex();
27 graphicsAllocationsForGlBufferReuse = &glSharingFunctions->graphicsAllocationsForGlBufferReuse;
28 }
29
30 GlSharingFunctionsMock *glSharingFunctions = nullptr;
31
32 uint32_t rootDeviceIndex = 0;
33 MockContext context;
34
35 std::vector<std::pair<unsigned int, GraphicsAllocation *>> *graphicsAllocationsForGlBufferReuse = nullptr;
36 unsigned int bufferId1 = 5;
37 unsigned int bufferId2 = 7;
38 cl_int retVal = CL_SUCCESS;
39 };
40
41 class FailingMemoryManager : public MockMemoryManager {
42 public:
43 GraphicsAllocation *createGraphicsAllocationFromSharedHandle(const OsHandleData &osHandleData, const AllocationProperties &properties, bool requireSpecificBitness, bool isHostIpcAllocation, bool reuseSharedAllocation, void *mapPointer) override {
44 return nullptr;
45 }
46 };
47
48 TEST_F(GlReusedBufferTests, givenMultipleBuffersWithTheSameIdWhenCreatedThenReuseGraphicsAllocation) {
49 std::unique_ptr<Buffer> glBuffers[10]; // first 5 with bufferId1, next 5 with bufferId2
50
51 for (size_t i = 0; i < 10; i++) {
52 glBuffers[i].reset(GlBuffer::createSharedGlBuffer(&context, CL_MEM_READ_WRITE, (i < 5 ? bufferId1 : bufferId2), &retVal));
53 EXPECT_NE(nullptr, glBuffers[i].get());
54 EXPECT_NE(nullptr, glBuffers[i]->getGraphicsAllocation(rootDeviceIndex));
55 }
56
57 EXPECT_EQ(2u, graphicsAllocationsForGlBufferReuse->size());
58 EXPECT_EQ(bufferId1, graphicsAllocationsForGlBufferReuse->at(0).first);
59 EXPECT_EQ(bufferId2, graphicsAllocationsForGlBufferReuse->at(1).first);
60
61 auto storedGraphicsAllocation1 = graphicsAllocationsForGlBufferReuse->at(0).second;
62 auto storedGraphicsAllocation2 = graphicsAllocationsForGlBufferReuse->at(1).second;
63 EXPECT_EQ(5u, storedGraphicsAllocation1->peekReuseCount());
64 EXPECT_EQ(5u, storedGraphicsAllocation2->peekReuseCount());
65
66 for (size_t i = 0; i < 10; i++) {
67 EXPECT_EQ(i < 5 ? storedGraphicsAllocation1 : storedGraphicsAllocation2,
68 glBuffers[i]->getGraphicsAllocation(rootDeviceIndex));
69 }
70 }
71
72 TEST_F(GlReusedBufferTests, givenMultipleBuffersWithReusedAllocationWhenReleasingThenClearVectorByLastObject) {
73 std::unique_ptr<Buffer> glBuffer1(GlBuffer::createSharedGlBuffer(&context, CL_MEM_READ_WRITE, bufferId1, &retVal));
74 std::unique_ptr<Buffer> glBuffer2(GlBuffer::createSharedGlBuffer(&context, CL_MEM_READ_WRITE, bufferId1, &retVal));
75
76 EXPECT_EQ(1u, graphicsAllocationsForGlBufferReuse->size());
77 EXPECT_EQ(2u, graphicsAllocationsForGlBufferReuse->at(0).second->peekReuseCount());
78
79 glBuffer1.reset(nullptr);
80
81 EXPECT_EQ(1u, graphicsAllocationsForGlBufferReuse->size());
82 EXPECT_EQ(1u, graphicsAllocationsForGlBufferReuse->at(0).second->peekReuseCount());
83
84 glBuffer2.reset(nullptr);
85 EXPECT_EQ(0u, graphicsAllocationsForGlBufferReuse->size());
86 }
87
88 TEST_F(GlReusedBufferTests, givenMultipleBuffersWithReusedAllocationWhenCreatingThenReuseGmmResourceToo) {
89 std::unique_ptr<Buffer> glBuffer1(GlBuffer::createSharedGlBuffer(&context, CL_MEM_READ_WRITE, bufferId1, &retVal));
90 GmmRequirements gmmRequirements{};
91 gmmRequirements.allowLargePages = true;
92 gmmRequirements.preferCompressed = false;
93 glBuffer1->getGraphicsAllocation(rootDeviceIndex)->setDefaultGmm(new Gmm(context.getDevice(0)->getGmmHelper(), (void *)0x100, 1, 0, GMM_RESOURCE_USAGE_OCL_BUFFER, {}, gmmRequirements));
94
95 std::unique_ptr<Buffer> glBuffer2(GlBuffer::createSharedGlBuffer(&context, CL_MEM_READ_WRITE, bufferId1, &retVal));
96
97 EXPECT_EQ(glBuffer1->getGraphicsAllocation(rootDeviceIndex)->getDefaultGmm()->gmmResourceInfo->peekHandle(),
98 glBuffer2->getGraphicsAllocation(rootDeviceIndex)->getDefaultGmm()->gmmResourceInfo->peekHandle());
99 }
100
101 TEST_F(GlReusedBufferTests, givenGlobalShareHandleChangedWhenAcquiringSharedBufferThenChangeGraphicsAllocation) {
102 GlDllHelper dllParam;
103 CL_GL_BUFFER_INFO bufferInfoOutput = dllParam.getBufferInfo();
104 bufferInfoOutput.globalShareHandle = 40;
105 dllParam.loadBuffer(bufferInfoOutput);
106 auto clBuffer = std::unique_ptr<Buffer>(GlBuffer::createSharedGlBuffer(&context, CL_MEM_READ_WRITE, bufferId1, &retVal));
107 auto glBuffer = clBuffer->peekSharingHandler();
108 auto oldGraphicsAllocation = clBuffer->getGraphicsAllocation(rootDeviceIndex);
109
110 ASSERT_EQ(40u, oldGraphicsAllocation->peekSharedHandle());
111
112 bufferInfoOutput.globalShareHandle = 41;
113 dllParam.loadBuffer(bufferInfoOutput);
114 glBuffer->acquire(clBuffer.get(), rootDeviceIndex);
115 auto newGraphicsAllocation = clBuffer->getGraphicsAllocation(rootDeviceIndex);
116
117 EXPECT_NE(oldGraphicsAllocation, newGraphicsAllocation);
118 EXPECT_EQ(41u, newGraphicsAllocation->peekSharedHandle());
119
120 glBuffer->release(clBuffer.get(), rootDeviceIndex);
121 }
122
123 TEST_F(GlReusedBufferTests, givenGlobalShareHandleDidNotChangeWhenAcquiringSharedBufferThenDontDynamicallyAllocateBufferInfo) {
124 class MyGlBuffer : public GlBuffer {
125 public:
126 MyGlBuffer(GLSharingFunctions *sharingFunctions, unsigned int glObjectId) : GlBuffer(sharingFunctions, glObjectId) {}
127
128 protected:
129 void resolveGraphicsAllocationChange(osHandle currentSharedHandle, UpdateData *updateData) override {
130 EXPECT_EQ(nullptr, updateData->updateData);
131 GlBuffer::resolveGraphicsAllocationChange(currentSharedHandle, updateData);
132 }
133 };
134 GlDllHelper dllParam;
135 CL_GL_BUFFER_INFO bufferInfoOutput = dllParam.getBufferInfo();
136 bufferInfoOutput.globalShareHandle = 40;
137
138 dllParam.loadBuffer(bufferInfoOutput);
139 auto clBuffer = std::unique_ptr<Buffer>(GlBuffer::createSharedGlBuffer(&context, CL_MEM_READ_WRITE, bufferId1, &retVal));
140 auto glBuffer = new MyGlBuffer(context.getSharing<GLSharingFunctions>(), bufferId1);
141 clBuffer->setSharingHandler(glBuffer);
142
143 glBuffer->acquire(clBuffer.get(), rootDeviceIndex);
144
145 glBuffer->release(clBuffer.get(), rootDeviceIndex);
146 }
147
148 TEST_F(GlReusedBufferTests, givenGlobalShareHandleChangedWhenAcquiringSharedBufferThenDynamicallyAllocateBufferInfo) {
149 class MyGlBuffer : public GlBuffer {
150 public:
151 MyGlBuffer(GLSharingFunctions *sharingFunctions, unsigned int glObjectId) : GlBuffer(sharingFunctions, glObjectId) {}
152
153 protected:
154 void resolveGraphicsAllocationChange(osHandle currentSharedHandle, UpdateData *updateData) override {
155 EXPECT_NE(nullptr, updateData->updateData);
156 GlBuffer::resolveGraphicsAllocationChange(currentSharedHandle, updateData);
157 }
158 };
159 GlDllHelper dllParam;
160 CL_GL_BUFFER_INFO bufferInfoOutput = dllParam.getBufferInfo();
161 bufferInfoOutput.globalShareHandle = 40;
162 dllParam.loadBuffer(bufferInfoOutput);
163 auto clBuffer = std::unique_ptr<Buffer>(GlBuffer::createSharedGlBuffer(&context, CL_MEM_READ_WRITE, bufferId1, &retVal));
164 auto glBuffer = new MyGlBuffer(context.getSharing<GLSharingFunctions>(), bufferId1);
165 clBuffer->setSharingHandler(glBuffer);
166
167 bufferInfoOutput.globalShareHandle = 41;
168 dllParam.loadBuffer(bufferInfoOutput);
169 glBuffer->acquire(clBuffer.get(), rootDeviceIndex);
170
171 glBuffer->release(clBuffer.get(), rootDeviceIndex);
172 }
173
174 TEST_F(GlReusedBufferTests, givenMultipleBuffersAndGlobalShareHandleChangedWhenAcquiringSharedBufferThenDeleteOldGfxAllocationFromReuseVector) {
175 GlDllHelper dllParam;
176 CL_GL_BUFFER_INFO bufferInfoOutput = dllParam.getBufferInfo();
177 bufferInfoOutput.globalShareHandle = 40;
178 dllParam.loadBuffer(bufferInfoOutput);
179 auto clBuffer1 = std::unique_ptr<Buffer>(GlBuffer::createSharedGlBuffer(&context, CL_MEM_READ_WRITE, bufferId1, &retVal));
180 auto clBuffer2 = std::unique_ptr<Buffer>(GlBuffer::createSharedGlBuffer(&context, CL_MEM_READ_WRITE, bufferId1, &retVal));
181 auto graphicsAllocation1 = clBuffer1->getGraphicsAllocation(rootDeviceIndex);
182 auto graphicsAllocation2 = clBuffer2->getGraphicsAllocation(rootDeviceIndex);
183 ASSERT_EQ(graphicsAllocation1, graphicsAllocation2);
184 ASSERT_EQ(2u, graphicsAllocation1->peekReuseCount());
185 ASSERT_EQ(1u, graphicsAllocationsForGlBufferReuse->size());
186
187 bufferInfoOutput.globalShareHandle = 41;
188 dllParam.loadBuffer(bufferInfoOutput);
189 clBuffer1->peekSharingHandler()->acquire(clBuffer1.get(), rootDeviceIndex);
190 auto newGraphicsAllocation = clBuffer1->getGraphicsAllocation(rootDeviceIndex);
191 EXPECT_EQ(1u, graphicsAllocationsForGlBufferReuse->size());
192 EXPECT_EQ(newGraphicsAllocation, graphicsAllocationsForGlBufferReuse->at(0).second);
193
194 clBuffer2->peekSharingHandler()->acquire(clBuffer2.get(), rootDeviceIndex);
195 EXPECT_EQ(clBuffer2->getGraphicsAllocation(rootDeviceIndex), newGraphicsAllocation);
196 EXPECT_EQ(1u, graphicsAllocationsForGlBufferReuse->size());
197 EXPECT_EQ(newGraphicsAllocation, graphicsAllocationsForGlBufferReuse->at(0).second);
198
199 clBuffer1->peekSharingHandler()->release(clBuffer1.get(), rootDeviceIndex);
200 clBuffer2->peekSharingHandler()->release(clBuffer2.get(), rootDeviceIndex);
201 }
202
203 TEST_F(GlReusedBufferTests, givenGraphicsAllocationCreationReturnsNullptrWhenAcquiringGlBufferThenReturnOutOfResourcesAndNullifyAllocation) {
204 auto suceedingMemoryManager = context.getMemoryManager();
205 auto failingMemoryManager = std::unique_ptr<FailingMemoryManager>(new FailingMemoryManager());
206
207 GlDllHelper dllParam;
208 CL_GL_BUFFER_INFO bufferInfoOutput = dllParam.getBufferInfo();
209 bufferInfoOutput.globalShareHandle = 40;
210
211 dllParam.loadBuffer(bufferInfoOutput);
212 auto clBuffer = std::unique_ptr<Buffer>(GlBuffer::createSharedGlBuffer(&context, CL_MEM_READ_WRITE, bufferId1, &retVal));
213 auto glBuffer = clBuffer->peekSharingHandler();
214
215 bufferInfoOutput.globalShareHandle = 41;
216 dllParam.loadBuffer(bufferInfoOutput);
217 context.memoryManager = failingMemoryManager.get();
218 auto result = glBuffer->acquire(clBuffer.get(), rootDeviceIndex);
219
220 EXPECT_EQ(CL_OUT_OF_RESOURCES, result);
221 EXPECT_EQ(nullptr, clBuffer->getGraphicsAllocation(rootDeviceIndex));
222
223 context.memoryManager = suceedingMemoryManager;
224 }