diff --git a/runtime/sharings/unified/unified_buffer.cpp b/runtime/sharings/unified/unified_buffer.cpp index 7e1a7401c9..2399974eb0 100644 --- a/runtime/sharings/unified/unified_buffer.cpp +++ b/runtime/sharings/unified/unified_buffer.cpp @@ -20,7 +20,7 @@ using namespace NEO; Buffer *UnifiedBuffer::createSharedUnifiedBuffer(Context *context, cl_mem_flags flags, UnifiedSharingMemoryDescription extMem, cl_int *errcodeRet) { ErrorCodeHelper errorCode(errcodeRet, CL_SUCCESS); - auto graphicsAllocation = UnifiedBuffer::createGraphicsAllocation(context, extMem); + auto graphicsAllocation = UnifiedBuffer::createGraphicsAllocation(context, extMem, GraphicsAllocation::AllocationType::SHARED_BUFFER); if (!graphicsAllocation) { errorCode.set(CL_INVALID_MEM_OBJECT); return nullptr; diff --git a/runtime/sharings/unified/unified_image.cpp b/runtime/sharings/unified/unified_image.cpp index 450d815c2d..0acc443aab 100644 --- a/runtime/sharings/unified/unified_image.cpp +++ b/runtime/sharings/unified/unified_image.cpp @@ -29,7 +29,7 @@ Image *UnifiedImage::createSharedUnifiedImage(Context *context, cl_mem_flags fla imgInfo.imgDesc = Image::convertDescriptor(*imageDesc); imgInfo.surfaceFormat = &clSurfaceFormat->surfaceFormat; - GraphicsAllocation *graphicsAllocation = createGraphicsAllocation(context, description); + GraphicsAllocation *graphicsAllocation = createGraphicsAllocation(context, description, GraphicsAllocation::AllocationType::SHARED_IMAGE); if (!graphicsAllocation) { errorCode.set(CL_INVALID_MEM_OBJECT); return nullptr; diff --git a/runtime/sharings/unified/unified_sharing.cpp b/runtime/sharings/unified/unified_sharing.cpp index 12acd12d08..0ae01875f0 100644 --- a/runtime/sharings/unified/unified_sharing.cpp +++ b/runtime/sharings/unified/unified_sharing.cpp @@ -30,11 +30,16 @@ void UnifiedSharing::synchronizeObject(UpdateData &updateData) { void UnifiedSharing::releaseResource(MemObj *memObject) { } -GraphicsAllocation *UnifiedSharing::createGraphicsAllocation(Context *context, UnifiedSharingMemoryDescription description) { +GraphicsAllocation *UnifiedSharing::createGraphicsAllocation(Context *context, UnifiedSharingMemoryDescription description, GraphicsAllocation::AllocationType allocationType) { + auto memoryManager = context->getMemoryManager(); switch (description.type) { case UnifiedSharingHandleType::Win32Nt: { - auto graphicsAllocation = context->getMemoryManager()->createGraphicsAllocationFromNTHandle(description.handle, 0u); - return graphicsAllocation; + return memoryManager->createGraphicsAllocationFromNTHandle(description.handle, 0u); + } + case UnifiedSharingHandleType::LinuxFd: + case UnifiedSharingHandleType::Win32Shared: { + const AllocationProperties properties{0u, false, 0u, allocationType, false}; + return memoryManager->createGraphicsAllocationFromSharedHandle(((osHandle)(uint64_t)description.handle), properties, false); } default: return nullptr; diff --git a/runtime/sharings/unified/unified_sharing.h b/runtime/sharings/unified/unified_sharing.h index c43291d4c2..4d5a44fd94 100644 --- a/runtime/sharings/unified/unified_sharing.h +++ b/runtime/sharings/unified/unified_sharing.h @@ -39,7 +39,7 @@ class UnifiedSharing : public SharingHandler { void synchronizeObject(UpdateData &updateData) override; void releaseResource(MemObj *memObject) override; - static GraphicsAllocation *createGraphicsAllocation(Context *context, UnifiedSharingMemoryDescription description); + static GraphicsAllocation *createGraphicsAllocation(Context *context, UnifiedSharingMemoryDescription description, GraphicsAllocation::AllocationType allocationType); private: UnifiedSharingFunctions *sharingFunctions; diff --git a/unit_tests/sharings/unified/unified_sharing_buffer_tests.cpp b/unit_tests/sharings/unified/unified_sharing_buffer_tests.cpp index 85c3a5f26f..04fc8d86e3 100644 --- a/unit_tests/sharings/unified/unified_sharing_buffer_tests.cpp +++ b/unit_tests/sharings/unified/unified_sharing_buffer_tests.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019 Intel Corporation + * Copyright (C) 2019-2020 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -57,15 +57,7 @@ TEST_F(UnifiedSharingBufferTestsWithMemoryManager, givenUnsupportedHandleTypeWhe desc.handle = reinterpret_cast(0x1234); auto buffer = std::unique_ptr(UnifiedBuffer::createSharedUnifiedBuffer(context.get(), flags, desc, &retVal)); - ASSERT_EQ(CL_INVALID_MEM_OBJECT, retVal); - - desc.type = UnifiedSharingHandleType::Win32Shared; - buffer = std::unique_ptr(UnifiedBuffer::createSharedUnifiedBuffer(context.get(), flags, desc, &retVal)); - ASSERT_EQ(CL_INVALID_MEM_OBJECT, retVal); - - desc.type = UnifiedSharingHandleType::LinuxFd; - buffer = std::unique_ptr(UnifiedBuffer::createSharedUnifiedBuffer(context.get(), flags, desc, &retVal)); - ASSERT_EQ(CL_INVALID_MEM_OBJECT, retVal); + EXPECT_EQ(CL_INVALID_MEM_OBJECT, retVal); } TEST_F(UnifiedSharingBufferTestsWithMemoryManager, givenValidContextAndMemoryManagerWhenCreatingBufferFromSharedHandleThenReturnSuccess) { diff --git a/unit_tests/sharings/unified/unified_sharing_image_tests.cpp b/unit_tests/sharings/unified/unified_sharing_image_tests.cpp index 764e4249e8..de16454147 100644 --- a/unit_tests/sharings/unified/unified_sharing_image_tests.cpp +++ b/unit_tests/sharings/unified/unified_sharing_image_tests.cpp @@ -66,16 +66,6 @@ TEST_F(UnifiedSharingImageTestsWithMemoryManager, givenUnsupportedHandleTypeWhen auto image = std::unique_ptr(UnifiedImage::createSharedUnifiedImage(context.get(), flags, desc, &format, &imageDesc, &retVal)); EXPECT_EQ(CL_INVALID_MEM_OBJECT, retVal); - - desc.type = UnifiedSharingHandleType::Win32Shared; - image = std::unique_ptr(UnifiedImage::createSharedUnifiedImage(context.get(), flags, desc, - &format, &imageDesc, &retVal)); - EXPECT_EQ(CL_INVALID_MEM_OBJECT, retVal); - - desc.type = UnifiedSharingHandleType::LinuxFd; - image = std::unique_ptr(UnifiedImage::createSharedUnifiedImage(context.get(), flags, desc, - &format, &imageDesc, &retVal)); - EXPECT_EQ(CL_INVALID_MEM_OBJECT, retVal); } TEST_F(UnifiedSharingImageTestsWithMemoryManager, givenValidContextAndMemoryManagerWhenCreatingImageFromSharedHandleThenReturnSuccess) { diff --git a/unit_tests/sharings/unified/unified_sharing_tests.cpp b/unit_tests/sharings/unified/unified_sharing_tests.cpp index 25392c7148..cf4cc19e47 100644 --- a/unit_tests/sharings/unified/unified_sharing_tests.cpp +++ b/unit_tests/sharings/unified/unified_sharing_tests.cpp @@ -162,3 +162,79 @@ TEST_F(UnifiedSharingTestsWithMemoryManager, givenUnifiedSharingHandlerWhenAcqui sharingHandler->release(buffer.get()); EXPECT_EQ(1u, sharingHandler->releaseResourceCalled); } + +struct UnifiedSharingCreateAllocationTests : UnifiedSharingTestsWithMemoryManager { + struct MemoryManagerCheckingAllocationMethod : MockMemoryManager { + using MockMemoryManager::MockMemoryManager; + + GraphicsAllocation *createGraphicsAllocationFromNTHandle(void *handle, uint32_t rootDeviceIndex) override { + this->createFromNTHandleCalled = true; + this->handle = (osHandle)(uint64_t)handle; + return nullptr; + } + GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, const AllocationProperties &properties, bool requireSpecificBitness) override { + this->createFromSharedHandleCalled = true; + this->handle = handle; + this->properties = std::make_unique(properties); + return nullptr; + } + + bool createFromNTHandleCalled = false; + bool createFromSharedHandleCalled = false; + osHandle handle; + std::unique_ptr properties; + }; + + struct MockSharingHandler : UnifiedSharing { + using UnifiedSharing::createGraphicsAllocation; + }; + + void SetUp() override { + UnifiedSharingTestsWithMemoryManager::SetUp(); + this->memoryManager = std::make_unique(); + this->memoryManagerBackup = std::make_unique>(&this->context->memoryManager, this->memoryManager.get()); + } + + std::unique_ptr memoryManager; + std::unique_ptr> memoryManagerBackup; +}; + +TEST_F(UnifiedSharingCreateAllocationTests, givenWindowsNtHandleWhenCreateGraphicsAllocationIsCalledThenUseNtHandleMethod) { + UnifiedSharingMemoryDescription desc{}; + desc.handle = reinterpret_cast(0x1234); + desc.type = UnifiedSharingHandleType::Win32Nt; + GraphicsAllocation::AllocationType allocationType = GraphicsAllocation::AllocationType::SHARED_IMAGE; + MockSharingHandler::createGraphicsAllocation(this->context.get(), desc, allocationType); + + EXPECT_TRUE(memoryManager->createFromNTHandleCalled); + EXPECT_FALSE(memoryManager->createFromSharedHandleCalled); + EXPECT_EQ((osHandle)(uint64_t)desc.handle, memoryManager->handle); +} + +TEST_F(UnifiedSharingCreateAllocationTests, givenWindowsSharedHandleWhenCreateGraphicsAllocationIsCalledThenUseSharedHandleMethod) { + UnifiedSharingMemoryDescription desc{}; + desc.handle = reinterpret_cast(0x1234); + desc.type = UnifiedSharingHandleType::Win32Shared; + GraphicsAllocation::AllocationType allocationType = GraphicsAllocation::AllocationType::SHARED_IMAGE; + MockSharingHandler::createGraphicsAllocation(this->context.get(), desc, allocationType); + + EXPECT_FALSE(memoryManager->createFromNTHandleCalled); + EXPECT_TRUE(memoryManager->createFromSharedHandleCalled); + EXPECT_EQ((osHandle)(uint64_t)desc.handle, memoryManager->handle); + const AllocationProperties expectedProperties{0u, false, 0u, allocationType, false}; + EXPECT_EQ(expectedProperties.allFlags, memoryManager->properties->allFlags); +} + +TEST_F(UnifiedSharingCreateAllocationTests, givenLinuxSharedHandleWhenCreateGraphicsAllocationIsCalledThenUseSharedHandleMethod) { + UnifiedSharingMemoryDescription desc{}; + desc.handle = reinterpret_cast(0x1234); + desc.type = UnifiedSharingHandleType::LinuxFd; + GraphicsAllocation::AllocationType allocationType = GraphicsAllocation::AllocationType::SHARED_IMAGE; + MockSharingHandler::createGraphicsAllocation(this->context.get(), desc, allocationType); + + EXPECT_FALSE(memoryManager->createFromNTHandleCalled); + EXPECT_TRUE(memoryManager->createFromSharedHandleCalled); + EXPECT_EQ((osHandle)(uint64_t)desc.handle, memoryManager->handle); + const AllocationProperties expectedProperties{0u, false, 0u, allocationType, false}; + EXPECT_EQ(expectedProperties.allFlags, memoryManager->properties->allFlags); +}