Debian Codegraph @

opencl/test/unit_test/sharings/unified/unified_sharing_fixtures.h

raw
1 /*
2 * Copyright (C) 2019-2024 Intel Corporation
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 */
7
8 #pragma once
9
10 #include "shared/test/common/helpers/variable_backup.h"
11 #include "shared/test/common/mocks/mock_device.h"
12 #include "shared/test/common/mocks/mock_gmm.h"
13 #include "shared/test/common/mocks/mock_memory_manager.h"
14 #include "shared/test/common/test_macros/hw_test.h"
15
16 #include "opencl/source/sharings/unified/unified_sharing_types.h"
17 #include "opencl/test/unit_test/mocks/mock_cl_device.h"
18 #include "opencl/test/unit_test/mocks/mock_context.h"
19
20 namespace NEO {
21
22 template <bool validContext>
23 struct UnifiedSharingContextFixture : ::testing::Test {
24 void SetUp() override {
25 device = std::make_unique<MockClDevice>(MockDevice::createWithNewExecutionEnvironment<MockDevice>(defaultHwInfo.get()));
26 cl_device_id deviceId = device.get();
27 deviceVector = std::make_unique<ClDeviceVector>(&deviceId, 1);
28 if (validContext) {
29 context = createValidContext();
30 } else {
31 context = createInvalidContext();
32 }
33 }
34
35 std::unique_ptr<MockContext> createContext(const cl_context_properties *contextProperties) {
36 cl_int retVal{};
37 auto context = std::unique_ptr<MockContext>(Context::create<MockContext>(contextProperties, *deviceVector,
38 nullptr, nullptr, retVal));
39 EXPECT_EQ(CL_SUCCESS, retVal);
40 return context;
41 }
42
43 std::unique_ptr<MockContext> createValidContext() {
44 const cl_context_properties contextProperties[] = {
45 static_cast<cl_context_properties>(UnifiedSharingContextType::deviceHandle), 0,
46 CL_CONTEXT_INTEROP_USER_SYNC, 1,
47 0};
48 return createContext(contextProperties);
49 }
50
51 std::unique_ptr<MockContext> createInvalidContext() {
52 return createContext(nullptr);
53 }
54
55 std::unique_ptr<MockClDevice> device;
56 std::unique_ptr<ClDeviceVector> deviceVector;
57 std::unique_ptr<MockContext> context;
58 };
59
60 template <bool validMemoryManager>
61 struct UnifiedSharingMockMemoryManager : MockMemoryManager {
62 using MockMemoryManager::MockMemoryManager;
63 GraphicsAllocation *createGraphicsAllocationFromSharedHandle(const OsHandleData &osHandleData, const AllocationProperties &properties, bool requireSpecificBitness, bool isHostIpcAllocation, bool reuseSharedAllocation, void *mapPointer) override {
64 if (!validMemoryManager) {
65 return nullptr;
66 }
67
68 auto graphicsAllocation = createMemoryAllocation(AllocationType::internalHostMemory, nullptr, reinterpret_cast<void *>(1), 1,
69 4096u, osHandleData.handle, MemoryPool::systemCpuInaccessible,
70 properties.rootDeviceIndex, false, false, false);
71 graphicsAllocation->setSharedHandle(osHandleData.handle);
72 graphicsAllocation->set32BitAllocation(false);
73 graphicsAllocation->setDefaultGmm(new MockGmm(executionEnvironment.rootDeviceEnvironments[properties.rootDeviceIndex]->getGmmHelper()));
74 return graphicsAllocation;
75 }
76 };
77
78 template <bool validContext, bool validMemoryManager>
79 struct UnifiedSharingFixture : UnifiedSharingContextFixture<validContext> {
80 void SetUp() override {
81 UnifiedSharingContextFixture<validContext>::SetUp();
82 this->memoryManager = std::make_unique<UnifiedSharingMockMemoryManager<validMemoryManager>>(*this->device->getExecutionEnvironment());
83 this->memoryManagerBackup = std::make_unique<VariableBackup<MemoryManager *>>(&this->context->memoryManager, this->memoryManager.get());
84 }
85
86 void TearDown() override {
87 UnifiedSharingContextFixture<validContext>::TearDown();
88 }
89
90 std::unique_ptr<UnifiedSharingMockMemoryManager<validMemoryManager>> memoryManager;
91 std::unique_ptr<VariableBackup<MemoryManager *>> memoryManagerBackup;
92 };
93
94 } // namespace NEO