1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
/*
* Copyright (C) 2026 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/source/os_interface/linux/drm_allocation.h"
#include "shared/source/os_interface/linux/drm_buffer_object.h"
#include "shared/source/os_interface/linux/drm_memory_manager.h"
#include "shared/test/common/libult/linux/drm_mock.h"
#include "shared/test/common/mocks/linux/mock_drm_memory_manager.h"
#include "shared/test/common/mocks/mock_execution_environment.h"
#include "shared/test/common/os_interface/linux/device_command_stream_fixture.h"
#include "gtest/gtest.h"
using namespace NEO;
struct DrmAllocationViewTest : public ::testing::Test {
void SetUp() override {
executionEnvironment = std::make_unique<MockExecutionEnvironment>();
executionEnvironment->rootDeviceEnvironments[0]->osInterface = std::make_unique<OSInterface>();
drm = new DrmMock(*executionEnvironment->rootDeviceEnvironments[0]);
executionEnvironment->rootDeviceEnvironments[0]->osInterface->setDriverModel(std::unique_ptr<DriverModel>(drm));
memoryManager = std::make_unique<TestedDrmMemoryManager>(false, false, false, *executionEnvironment);
parentAllocation = std::make_unique<DrmAllocation>(0u, 1u, AllocationType::buffer, nullptr, cpuPtr, gpuAddress, allocationSize, MemoryPool::localMemory);
}
const size_t allocationSize = 4096u;
const size_t viewOffset = 1024u;
const size_t viewSize = 512u;
const uint64_t gpuAddress = 0x1000000u;
void *cpuPtr = reinterpret_cast<void *>(0x2000000u);
std::unique_ptr<MockExecutionEnvironment> executionEnvironment;
std::unique_ptr<TestedDrmMemoryManager> memoryManager;
DrmMock *drm = nullptr;
std::unique_ptr<DrmAllocation> parentAllocation;
};
TEST_F(DrmAllocationViewTest, whenCreatingViewThenReturnsDrmAllocation) {
std::unique_ptr<GraphicsAllocation> view(parentAllocation->createView(viewOffset, viewSize));
ASSERT_NE(nullptr, view);
EXPECT_TRUE(view->isView());
EXPECT_EQ(parentAllocation.get(), view->getParentAllocation());
}
TEST_F(DrmAllocationViewTest, whenCreatingViewThenViewHasCorrectAddresses) {
std::unique_ptr<GraphicsAllocation> view(parentAllocation->createView(viewOffset, viewSize));
EXPECT_EQ(gpuAddress + viewOffset, view->getGpuAddress());
EXPECT_EQ(ptrOffset(cpuPtr, viewOffset), view->getUnderlyingBuffer());
EXPECT_EQ(viewSize, view->getUnderlyingBufferSize());
}
TEST_F(DrmAllocationViewTest, whenPeekInternalHandleOnViewThenDelegatesToParent) {
auto bo = std::make_unique<BufferObject>(0u, drm, 3u, 0, 0, 1);
parentAllocation->getBufferObjectToModify(0) = bo.get();
auto view = std::unique_ptr<DrmAllocation>(static_cast<DrmAllocation *>(parentAllocation->createView(viewOffset, viewSize)));
uint64_t handle = 0;
int result = view->peekInternalHandle(memoryManager.get(), 0u, handle);
EXPECT_EQ(0, result);
EXPECT_EQ(static_cast<uint64_t>(bo->peekHandle()), handle);
parentAllocation->getBufferObjectToModify(0) = nullptr;
}
TEST_F(DrmAllocationViewTest, whenClearInternalHandleOnViewThenDelegatesToParent) {
auto bo = std::make_unique<BufferObject>(0u, drm, 3u, 0, 0, 1);
parentAllocation->getBufferObjectToModify(0) = bo.get();
auto view = std::unique_ptr<DrmAllocation>(static_cast<DrmAllocation *>(parentAllocation->createView(viewOffset, viewSize)));
view->clearInternalHandle(0u);
parentAllocation->getBufferObjectToModify(0) = nullptr;
}
TEST_F(DrmAllocationViewTest, whenFreeingViewAllocationThenMemoryManagerReturnsEarly) {
auto view = parentAllocation->createView(viewOffset, viewSize);
memoryManager->freeGraphicsMemory(view);
// View was not deleted by freeGraphicsMemory
delete view;
// Parent should still be valid
EXPECT_FALSE(parentAllocation->isView());
EXPECT_EQ(allocationSize, parentAllocation->getUnderlyingBufferSize());
}
TEST_F(DrmAllocationViewTest, whenCreatingViewThenViewInheritsBufferObjects) {
auto bo = std::make_unique<BufferObject>(0u, drm, 3u, 0, 0, 1);
parentAllocation->getBufferObjectToModify(0) = bo.get();
auto view = std::unique_ptr<DrmAllocation>(static_cast<DrmAllocation *>(parentAllocation->createView(viewOffset, viewSize)));
EXPECT_EQ(parentAllocation->getBO(), view->getBO());
parentAllocation->getBufferObjectToModify(0) = nullptr;
}
|