1 /*
2 * Copyright (c) 2017, Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23 #include "cm_test.h"
24 #include <malloc.h>
25
26 class BufferUPTest: public CmTest
27 {
28 public:
29 static const uint32_t SIZE = 4096;
30
BufferUPTest()31 BufferUPTest(): m_buffer(nullptr), m_sys_mem(nullptr) {}
32
~BufferUPTest()33 ~BufferUPTest() { Release(); }
34
CreateDestroy(uint32_t size)35 int32_t CreateDestroy(uint32_t size)
36 {
37 uint32_t real_size = size? size: SIZE;
38 m_sys_mem = AllocateAlignedMemory(real_size, 0x1000);
39
40 int32_t result = m_mockDevice->CreateBufferUP(size, m_sys_mem, m_buffer);
41 if (CM_SUCCESS != result)
42 {
43 Release();
44 return result;
45 }
46
47 SurfaceIndex *surface_index = nullptr;
48 result = m_buffer->GetIndex(surface_index);
49 EXPECT_EQ(CM_SUCCESS, result);
50 EXPECT_GT(surface_index->get_data(), static_cast<uint32_t>(0));
51 result = m_mockDevice->DestroyBufferUP(m_buffer);
52 Release();
53 return result;
54 }//===============
55
CreateDestroy(void * sys_mem)56 int32_t CreateDestroy(void *sys_mem)
57 {
58 int32_t result = m_mockDevice->CreateBufferUP(SIZE, sys_mem, m_buffer);
59 if (CM_SUCCESS != result)
60 {
61 return result;
62 }
63 return m_mockDevice->DestroyBufferUP(m_buffer);
64 }//===============================================
65
Release()66 bool Release()
67 {
68 if (nullptr == m_sys_mem)
69 {
70 return true;
71 }
72 FreeAlignedMemory(m_sys_mem);
73 m_sys_mem = nullptr;
74 return true;
75 }//=============
76
77 private:
78 CMRT_UMD::CmBufferUP *m_buffer;
79 void *m_sys_mem;
80 };//================
81
TEST_F(BufferUPTest,MultipleSizes)82 TEST_F(BufferUPTest, MultipleSizes)
83 {
84 RunEach<int32_t>(CM_SUCCESS,
85 [this]() { return CreateDestroy(SIZE); });
86
87 RunEach<int32_t>(CM_INVALID_WIDTH,
88 [this]() { return CreateDestroy(SIZE + 1); });
89
90 RunEach<int32_t>(CM_SUCCESS,
91 [this]() { return CreateDestroy(4); }); // 4-byte is the minimum size.
92
93 RunEach<int32_t>(CM_SUCCESS,
94 [this]() { return CreateDestroy(16); });
95
96 RunEach<int32_t>(CM_SUCCESS,
97 [this]() { return CreateDestroy(512*1024*1024); }); // The maximum size.
98
99 uint32_t zero_size = 0;
100 RunEach<int32_t>(CM_INVALID_WIDTH,
101 [this, zero_size]() { return CreateDestroy(zero_size); });
102
103 return;
104 }//========
105
TEST_F(BufferUPTest,InvalidSystemMemory)106 TEST_F(BufferUPTest, InvalidSystemMemory)
107 {
108 uint8_t *sys_mem = nullptr;
109 RunEach<int32_t>(CM_INVALID_ARG_VALUE,
110 [this, sys_mem]() { return CreateDestroy(sys_mem); });
111
112 sys_mem = new uint8_t[SIZE];
113 uint8_t *sys_mem_for_surface = sys_mem;
114 if ((reinterpret_cast<uintptr_t>(sys_mem) & (0x1000 - 1)) == 0) // 4k-aligned.
115 {
116 ++sys_mem_for_surface;
117 }
118 auto CreateWithNonalignedPointer
119 = [this, sys_mem_for_surface]()
120 { return CreateDestroy(sys_mem_for_surface); };
121 RunEach<int32_t>(CM_INVALID_ARG_VALUE, CreateWithNonalignedPointer);
122 delete[] sys_mem;
123 return;
124 }//========
125