| 1 | /* |
| 2 | * Copyright (C) 2019-2025 Intel Corporation |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include "shared/source/gmm_helper/gmm.h" |
| 9 | #include "shared/source/os_interface/product_helper_hw.h" |
| 10 | #include "shared/test/common/helpers/raii_product_helper.h" |
| 11 | #include "shared/test/common/mocks/mock_gmm_resource_info.h" |
| 12 | |
| 13 | #include "opencl/source/mem_obj/image.h" |
| 14 | #include "opencl/source/sharings/unified/unified_image.h" |
| 15 | #include "opencl/test/unit_test/sharings/unified/unified_sharing_fixtures.h" |
| 16 | |
| 17 | namespace NEO { |
| 18 | class MemoryManager; |
| 19 | } // namespace NEO |
| 20 | |
| 21 | using namespace NEO; |
| 22 | |
| 23 | using UnifiedSharingImageTestsWithMemoryManager = UnifiedSharingFixture<true, true>; |
| 24 | using UnifiedSharingImageTestsWithInvalidMemoryManager = UnifiedSharingFixture<true, false>; |
| 25 | |
| 26 | static UnifiedSharingMemoryDescription getValidUnifiedSharingDesc() { |
| 27 | UnifiedSharingMemoryDescription desc{}; |
| 28 | desc.handle = reinterpret_cast<void *>(0x1234); |
| 29 | desc.type = UnifiedSharingHandleType::win32Nt; |
| 30 | return desc; |
| 31 | } |
| 32 | |
| 33 | static cl_image_format getValidImageFormat() { |
| 34 | cl_image_format format{}; |
| 35 | format.image_channel_data_type = CL_UNORM_INT8; |
| 36 | format.image_channel_order = CL_RGBA; |
| 37 | return format; |
| 38 | } |
| 39 | |
| 40 | static cl_image_desc getValidImageDesc() { |
| 41 | cl_image_desc imageDesc{}; |
| 42 | imageDesc.image_type = CL_MEM_OBJECT_IMAGE2D; |
| 43 | imageDesc.image_width = 128; |
| 44 | imageDesc.image_height = 128; |
| 45 | imageDesc.image_depth = 1; |
| 46 | imageDesc.image_array_size = 1; |
| 47 | imageDesc.image_row_pitch = 256; |
| 48 | imageDesc.image_slice_pitch = 0u; |
| 49 | imageDesc.num_mip_levels = 1; |
| 50 | imageDesc.num_samples = 0; |
| 51 | imageDesc.buffer = nullptr; |
| 52 | return imageDesc; |
| 53 | } |
| 54 | |
| 55 | TEST_F(UnifiedSharingImageTestsWithInvalidMemoryManager, givenValidContextAndAllocationFailsWhenCreatingImageFromSharedHandleThenReturnInvalidMemObject) { |
| 56 | cl_mem_flags flags{}; |
| 57 | cl_int retVal{}; |
| 58 | const auto format = getValidImageFormat(); |
| 59 | const auto imageDesc = getValidImageDesc(); |
| 60 | auto image = std::unique_ptr<Image>(UnifiedImage::createSharedUnifiedImage(context.get(), flags, getValidUnifiedSharingDesc(), |
| 61 | &format, &imageDesc, &retVal)); |
| 62 | ASSERT_EQ(CL_INVALID_MEM_OBJECT, retVal); |
| 63 | } |
| 64 | |
| 65 | TEST_F(UnifiedSharingImageTestsWithMemoryManager, givenUnsupportedHandleTypeWhenCreatingImageFromSharedHandleThenReturnInvalidMemObject) { |
| 66 | cl_mem_flags flags{}; |
| 67 | cl_int retVal{}; |
| 68 | UnifiedSharingMemoryDescription desc{}; |
| 69 | desc.handle = reinterpret_cast<void *>(0x1234); |
| 70 | const auto format = getValidImageFormat(); |
| 71 | const auto imageDesc = getValidImageDesc(); |
| 72 | |
| 73 | auto image = std::unique_ptr<Image>(UnifiedImage::createSharedUnifiedImage(context.get(), flags, desc, |
| 74 | &format, &imageDesc, &retVal)); |
| 75 | EXPECT_EQ(CL_INVALID_MEM_OBJECT, retVal); |
| 76 | } |
| 77 | |
| 78 | TEST_F(UnifiedSharingImageTestsWithMemoryManager, givenValidContextAndMemoryManagerWhenCreatingImageFromSharedHandleThenReturnSuccess) { |
| 79 | cl_mem_flags flags{}; |
| 80 | cl_int retVal{}; |
| 81 | const auto format = getValidImageFormat(); |
| 82 | const auto imageDesc = getValidImageDesc(); |
| 83 | auto image = std::unique_ptr<Image>(UnifiedImage::createSharedUnifiedImage(context.get(), flags, getValidUnifiedSharingDesc(), |
| 84 | &format, &imageDesc, &retVal)); |
| 85 | ASSERT_EQ(CL_SUCCESS, retVal); |
| 86 | |
| 87 | auto renderSize = image->getGraphicsAllocation(device->getRootDeviceIndex())->getDefaultGmm()->gmmResourceInfo->getSizeAllocation(); |
| 88 | size_t expectedSize = imageDesc.image_row_pitch * imageDesc.image_height; |
| 89 | EXPECT_GE(renderSize, expectedSize); |
| 90 | } |
| 91 | |
| 92 | TEST_F(UnifiedSharingImageTestsWithMemoryManager, givenPassedFormatWhenCreatingUnifiedImageThenFormatIsCorrectlySetInImageObject) { |
| 93 | cl_image_format format{}; |
| 94 | format.image_channel_data_type = CL_HALF_FLOAT; |
| 95 | format.image_channel_order = CL_RG; |
| 96 | |
| 97 | cl_mem_flags flags{}; |
| 98 | cl_int retVal{}; |
| 99 | const auto imageDesc = getValidImageDesc(); |
| 100 | auto image = std::unique_ptr<Image>(UnifiedImage::createSharedUnifiedImage(context.get(), flags, getValidUnifiedSharingDesc(), |
| 101 | &format, &imageDesc, &retVal)); |
| 102 | ASSERT_EQ(CL_SUCCESS, retVal); |
| 103 | EXPECT_EQ(GMM_FORMAT_R16G16_FLOAT_TYPE, image->getSurfaceFormatInfo().surfaceFormat.gmmSurfaceFormat); |
| 104 | EXPECT_EQ(GFX3DSTATE_SURFACEFORMAT_R16G16_FLOAT, image->getSurfaceFormatInfo().surfaceFormat.genxSurfaceFormat); |
| 105 | } |
| 106 | |
| 107 | template <bool pageTableManagerSupported> |
| 108 | class MockProductHelper : public ProductHelperHw<IGFX_UNKNOWN> { |
| 109 | public: |
| 110 | bool isPageTableManagerSupported(const HardwareInfo &hwInfo) const override { |
| 111 | return pageTableManagerSupported; |
| 112 | } |
| 113 | }; |
| 114 | |
| 115 | struct MemoryManagerReturningCompressedAllocations : UnifiedSharingMockMemoryManager<true> { |
| 116 | GraphicsAllocation *createGraphicsAllocationFromSharedHandle(const OsHandleData &osHandleData, const AllocationProperties &properties, bool requireSpecificBitness, bool isHostIpcAllocation, bool reuseSharedAllocation, void *mapPointer) override { |
| 117 | auto allocation = UnifiedSharingMockMemoryManager<true>::createGraphicsAllocationFromSharedHandle(osHandleData, properties, false, false, false, nullptr); |
| 118 | |
| 119 | auto gmm = allocation->getDefaultGmm(); |
| 120 | auto mockGmmResourceInfo = std::make_unique<MockGmmResourceInfo>(gmm->gmmResourceInfo->peekGmmResourceInfo()); |
| 121 | mockGmmResourceInfo->mockResourceCreateParams.Type = RESOURCE_1D; |
| 122 | mockGmmResourceInfo->setUnifiedAuxTranslationCapable(); |
| 123 | gmm->gmmResourceInfo = std::move(mockGmmResourceInfo); |
| 124 | |
| 125 | return allocation; |
| 126 | } |
| 127 | |
| 128 | bool mapAuxGpuVA(GraphicsAllocation *graphicsAllocation) override { |
| 129 | calledMapAuxGpuVA++; |
| 130 | return resultOfMapAuxGpuVA; |
| 131 | } |
| 132 | |
| 133 | unsigned int calledMapAuxGpuVA{}; |
| 134 | bool resultOfMapAuxGpuVA{}; |
| 135 | }; |
| 136 | |
| 137 | HWTEST_F(UnifiedSharingImageTestsWithMemoryManager, givenCompressedImageAndNoPageTableManagerWhenCreatingUnifiedImageThenSetCorrespondingFieldInGmmAndDoNotUsePageTableManager) { |
| 138 | MemoryManagerReturningCompressedAllocations memoryManager{}; |
| 139 | VariableBackup<MemoryManager *> memoryManagerBackup{&this->context->memoryManager, &memoryManager}; |
| 140 | using ProductHelperNotSupportingPageTableManager = MockProductHelper<false>; |
| 141 | RAIIProductHelperFactory<ProductHelperNotSupportingPageTableManager> productHelperBackup{*this->context->getDevice(0)->getExecutionEnvironment()->rootDeviceEnvironments[0]}; |
| 142 | |
| 143 | cl_mem_flags flags{}; |
| 144 | cl_int retVal{}; |
| 145 | const auto format = getValidImageFormat(); |
| 146 | const auto imageDesc = getValidImageDesc(); |
| 147 | auto image = std::unique_ptr<Image>(UnifiedImage::createSharedUnifiedImage(context.get(), flags, getValidUnifiedSharingDesc(), |
| 148 | &format, &imageDesc, &retVal)); |
| 149 | ASSERT_EQ(CL_SUCCESS, retVal); |
| 150 | EXPECT_TRUE(image->getGraphicsAllocation(device->getRootDeviceIndex())->getDefaultGmm()->isCompressionEnabled()); |
| 151 | EXPECT_EQ(0u, memoryManager.calledMapAuxGpuVA); |
| 152 | } |
| 153 | |
| 154 | HWTEST_F(UnifiedSharingImageTestsWithMemoryManager, givenCompressedImageAndPageTableManagerWhenCreatingUnifiedImageThenSetCorrespondingFieldInGmmBasedOnAuxGpuVaMappingResult) { |
| 155 | MemoryManagerReturningCompressedAllocations memoryManager{}; |
| 156 | VariableBackup<MemoryManager *> memoryManagerBackup{&this->context->memoryManager, &memoryManager}; |
| 157 | using ProductHelperNotSupportingPageTableManager = MockProductHelper<true>; |
| 158 | RAIIProductHelperFactory<ProductHelperNotSupportingPageTableManager> productHelperBackup{*this->context->getDevice(0)->getExecutionEnvironment()->rootDeviceEnvironments[0]}; |
| 159 | |
| 160 | cl_mem_flags flags{}; |
| 161 | cl_int retVal{}; |
| 162 | const auto format = getValidImageFormat(); |
| 163 | const auto imageDesc = getValidImageDesc(); |
| 164 | |
| 165 | memoryManager.resultOfMapAuxGpuVA = true; |
| 166 | auto image = std::unique_ptr<Image>(UnifiedImage::createSharedUnifiedImage(context.get(), flags, getValidUnifiedSharingDesc(), |
| 167 | &format, &imageDesc, &retVal)); |
| 168 | ASSERT_EQ(CL_SUCCESS, retVal); |
| 169 | EXPECT_EQ(memoryManager.resultOfMapAuxGpuVA, image->getGraphicsAllocation(device->getRootDeviceIndex())->getDefaultGmm()->isCompressionEnabled()); |
| 170 | EXPECT_EQ(1u, memoryManager.calledMapAuxGpuVA); |
| 171 | |
| 172 | memoryManager.resultOfMapAuxGpuVA = false; |
| 173 | image = std::unique_ptr<Image>(UnifiedImage::createSharedUnifiedImage(context.get(), flags, getValidUnifiedSharingDesc(), |
| 174 | &format, &imageDesc, &retVal)); |
| 175 | ASSERT_EQ(CL_SUCCESS, retVal); |
| 176 | EXPECT_EQ(memoryManager.resultOfMapAuxGpuVA, image->getGraphicsAllocation(device->getRootDeviceIndex())->getDefaultGmm()->isCompressionEnabled()); |
| 177 | EXPECT_EQ(2u, memoryManager.calledMapAuxGpuVA); |
| 178 | } |