/* * Copyright (C) 2017-2018 Intel Corporation * * SPDX-License-Identifier: MIT * */ #include "gtest/gtest.h" #include "test.h" #include "unit_tests/mocks/mock_buffer.h" #include "unit_tests/mocks/mock_csr.h" #include "runtime/memory_manager/surface.h" #include "runtime/memory_manager/graphics_allocation.h" #include "hw_cmds.h" #include using namespace OCLRT; typedef ::testing::Types SurfaceTypes; namespace createSurface { template Surface *Create(char *data, MockBuffer *buffer, GraphicsAllocation *gfxAllocation); template <> Surface *Create(char *data, MockBuffer *buffer, GraphicsAllocation *gfxAllocation) { return new NullSurface; } template <> Surface *Create(char *data, MockBuffer *buffer, GraphicsAllocation *gfxAllocation) { return new HostPtrSurface(data, 10, gfxAllocation); } template <> Surface *Create(char *data, MockBuffer *buffer, GraphicsAllocation *gfxAllocation) { return new MemObjSurface(buffer); } template <> Surface *Create(char *data, MockBuffer *buffer, GraphicsAllocation *gfxAllocation) { return new GeneralSurface(gfxAllocation); } } // namespace createSurface template class SurfaceTest : public ::testing::Test { public: char data[10]; MockBuffer buffer; MockGraphicsAllocation gfxAllocation{nullptr, 0}; }; TYPED_TEST_CASE(SurfaceTest, SurfaceTypes); HWTEST_TYPED_TEST(SurfaceTest, GivenSurfaceWhenInterfaceIsUsedThenSurfaceBehavesCorrectly) { int32_t execStamp; ExecutionEnvironment executionEnvironment; MockCsr *csr = new MockCsr(execStamp, executionEnvironment); executionEnvironment.commandStreamReceivers.push_back(std::unique_ptr(csr)); executionEnvironment.memoryManager.reset(csr->createMemoryManager(false, false)); Surface *surface = createSurface::Create(this->data, &this->buffer, &this->gfxAllocation); ASSERT_NE(nullptr, surface); Surface *duplicatedSurface = surface->duplicate(); ASSERT_NE(nullptr, duplicatedSurface); surface->makeResident(*csr); if (std::is_same::value || std::is_same::value || std::is_same::value) { EXPECT_EQ(1u, csr->madeResidentGfxAllocations.size()); } delete duplicatedSurface; delete surface; } class CoherentMemObjSurface : public SurfaceTest { public: CoherentMemObjSurface() { this->buffer.getGraphicsAllocation()->setCoherent(true); } }; TEST_F(CoherentMemObjSurface, BufferFromCoherentSvm) { Surface *surface = createSurface::Create(this->data, &this->buffer, &this->gfxAllocation); EXPECT_TRUE(surface->IsCoherent); delete surface; } TEST(HostPtrSurfaceTest, givenHostPtrSurfaceWhenCreatedWithoutSpecifyingPtrCopyAllowanceThenPtrCopyIsNotAllowed) { char memory[2]; HostPtrSurface surface(memory, sizeof(memory)); EXPECT_FALSE(surface.peekIsPtrCopyAllowed()); } TEST(HostPtrSurfaceTest, givenHostPtrSurfaceWhenCreatedWithPtrCopyAllowedThenQueryReturnsTrue) { char memory[2]; HostPtrSurface surface(memory, sizeof(memory), true); EXPECT_TRUE(surface.peekIsPtrCopyAllowed()); } TEST(HostPtrSurfaceTest, givenHostPtrSurfaceWhenCreatedWithPtrCopyNotAllowedThenQueryReturnsFalse) { char memory[2]; HostPtrSurface surface(memory, sizeof(memory), false); EXPECT_FALSE(surface.peekIsPtrCopyAllowed()); }