/* * Copyright (C) 2019-2024 Intel Corporation * * SPDX-License-Identifier: MIT * */ #pragma once #include "shared/test/common/helpers/variable_backup.h" #include "shared/test/common/mocks/mock_device.h" #include "shared/test/common/mocks/mock_gmm.h" #include "shared/test/common/mocks/mock_memory_manager.h" #include "shared/test/common/test_macros/hw_test.h" #include "opencl/source/sharings/unified/unified_sharing_types.h" #include "opencl/test/unit_test/mocks/mock_cl_device.h" #include "opencl/test/unit_test/mocks/mock_context.h" namespace NEO { template struct UnifiedSharingContextFixture : ::testing::Test { void SetUp() override { device = std::make_unique(MockDevice::createWithNewExecutionEnvironment(defaultHwInfo.get())); cl_device_id deviceId = device.get(); deviceVector = std::make_unique(&deviceId, 1); if (validContext) { context = createValidContext(); } else { context = createInvalidContext(); } } std::unique_ptr createContext(const cl_context_properties *contextProperties) { cl_int retVal{}; auto context = std::unique_ptr(Context::create(contextProperties, *deviceVector, nullptr, nullptr, retVal)); EXPECT_EQ(CL_SUCCESS, retVal); return context; } std::unique_ptr createValidContext() { const cl_context_properties contextProperties[] = { static_cast(UnifiedSharingContextType::deviceHandle), 0, CL_CONTEXT_INTEROP_USER_SYNC, 1, 0}; return createContext(contextProperties); } std::unique_ptr createInvalidContext() { return createContext(nullptr); } std::unique_ptr device; std::unique_ptr deviceVector; std::unique_ptr context; }; template struct UnifiedSharingMockMemoryManager : MockMemoryManager { using MockMemoryManager::MockMemoryManager; GraphicsAllocation *createGraphicsAllocationFromSharedHandle(const OsHandleData &osHandleData, const AllocationProperties &properties, bool requireSpecificBitness, bool isHostIpcAllocation, bool reuseSharedAllocation, void *mapPointer) override { if (!validMemoryManager) { return nullptr; } auto graphicsAllocation = createMemoryAllocation(AllocationType::internalHostMemory, nullptr, reinterpret_cast(1), 1, 4096u, osHandleData.handle, MemoryPool::systemCpuInaccessible, properties.rootDeviceIndex, false, false, false); graphicsAllocation->setSharedHandle(osHandleData.handle); graphicsAllocation->set32BitAllocation(false); graphicsAllocation->setDefaultGmm(new MockGmm(executionEnvironment.rootDeviceEnvironments[properties.rootDeviceIndex]->getGmmHelper())); return graphicsAllocation; } }; template struct UnifiedSharingFixture : UnifiedSharingContextFixture { void SetUp() override { UnifiedSharingContextFixture::SetUp(); this->memoryManager = std::make_unique>(*this->device->getExecutionEnvironment()); this->memoryManagerBackup = std::make_unique>(&this->context->memoryManager, this->memoryManager.get()); } void TearDown() override { UnifiedSharingContextFixture::TearDown(); } std::unique_ptr> memoryManager; std::unique_ptr> memoryManagerBackup; }; } // namespace NEO