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