| 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 | |
| 25 | class Surface2DUPTest: public CmTest |
| 26 | { |
| 27 | public: |
| 28 | static const uint32_t WIDTH = 64; |
| 29 | static const uint32_t HEIGHT = 64; |
| 30 | |
| 31 | Surface2DUPTest(): m_surface(nullptr), m_sys_mem(nullptr) {} |
| 32 | |
| 33 | ~Surface2DUPTest() { Release(); } |
| 34 | |
| 35 | int32_t CreateDestroy(CM_SURFACE_FORMAT format, |
| 36 | uint32_t width, |
| 37 | uint32_t height) |
| 38 | { |
| 39 | uint32_t pitch = 0, alloc_size = 0; |
| 40 | if (CM_SUCCESS == m_mockDevice->GetSurface2DInfo(width, height, format, |
| 41 | pitch, alloc_size)) |
| 42 | { |
| 43 | m_sys_mem = AllocateAlignedMemory(alloc_size, 0x1000); |
| 44 | } |
| 45 | else // In case width or height is invalid. This pointer will not be referenced anyway. |
| 46 | { |
| 47 | m_sys_mem = AllocateAlignedMemory(4*WIDTH*HEIGHT, 0x1000); |
| 48 | } |
| 49 | int32_t result = m_mockDevice->CreateSurface2DUP(width, height, format, |
| 50 | m_sys_mem, m_surface); |
| 51 | if (CM_SUCCESS != result) |
| 52 | { |
| 53 | Release(); |
| 54 | return result; |
| 55 | } |
| 56 | SurfaceIndex *surface_index = nullptr; |
| 57 | result = m_surface->GetIndex(surface_index); |
| 58 | EXPECT_EQ(CM_SUCCESS, result); |
| 59 | EXPECT_GT(surface_index->get_data(), static_cast<uint32_t>(0)); |
| 60 | result = m_mockDevice->DestroySurface2DUP(m_surface); |
| 61 | Release(); |
| 62 | return result; |
| 63 | }//=============== |
| 64 | |
| 65 | int32_t CreateDestroy(void *sys_mem) |
| 66 | { |
| 67 | int32_t result = m_mockDevice->CreateSurface2DUP( |
| 68 | WIDTH, HEIGHT, CM_SURFACE_FORMAT_A8R8G8B8, sys_mem, m_surface); |
| 69 | if (CM_SUCCESS != result) |
| 70 | { |
| 71 | return result; |
| 72 | } |
| 73 | return m_mockDevice->DestroySurface2DUP(m_surface); |
| 74 | }//==================================================== |
| 75 | |
| 76 | bool Release() |
| 77 | { |
| 78 | if (nullptr == m_sys_mem) |
| 79 | { |
| 80 | return true; |
| 81 | } |
| 82 | FreeAlignedMemory(m_sys_mem); |
| 83 | m_sys_mem = nullptr; |
| 84 | return true; |
| 85 | }//============= |
| 86 | |
| 87 | private: |
| 88 | CMRT_UMD::CmSurface2DUP *m_surface; |
| 89 | void *m_sys_mem; |
| 90 | };//================ |
| 91 | |
| 92 | TEST_F(Surface2DUPTest, MultipleSizes) |
| 93 | { |
| 94 | RunEach<int32_t>(CM_SUCCESS, |
| 95 | [this]() { return CreateDestroy(CM_SURFACE_FORMAT_A8R8G8B8, |
| 96 | WIDTH, HEIGHT); }); |
| 97 | |
| 98 | RunEach<int32_t>(CM_SUCCESS, |
| 99 | [this]() { return CreateDestroy(CM_SURFACE_FORMAT_A8R8G8B8, |
| 100 | WIDTH + 1, HEIGHT + 1); }); |
| 101 | |
| 102 | RunEach<int32_t>(CM_SUCCESS, |
| 103 | [this]() { return CreateDestroy(CM_SURFACE_FORMAT_A8R8G8B8, |
| 104 | CM_MIN_SURF_WIDTH, HEIGHT); }); |
| 105 | |
| 106 | RunEach<int32_t>(CM_INVALID_WIDTH, |
| 107 | [this]() { return CreateDestroy(CM_SURFACE_FORMAT_A8R8G8B8, |
| 108 | 0, HEIGHT); }); |
| 109 | |
| 110 | RunEach<int32_t>( |
| 111 | CM_SUCCESS, |
| 112 | [this]() { return CreateDestroy(CM_SURFACE_FORMAT_A8R8G8B8, |
| 113 | WIDTH, CM_MIN_SURF_HEIGHT); }); |
| 114 | |
| 115 | RunEach<int32_t>(CM_INVALID_HEIGHT, |
| 116 | [this]() { return CreateDestroy(CM_SURFACE_FORMAT_A8R8G8B8, |
| 117 | WIDTH, 0); }); |
| 118 | |
| 119 | RunEach<int32_t>( |
| 120 | CM_SUCCESS, |
| 121 | [this]() { return CreateDestroy(CM_SURFACE_FORMAT_A8R8G8B8, |
| 122 | CM_MAX_2D_SURF_WIDTH, HEIGHT); }); |
| 123 | |
| 124 | RunEach<int32_t>(CM_INVALID_WIDTH, |
| 125 | [this]() { return CreateDestroy(CM_SURFACE_FORMAT_A8R8G8B8, |
| 126 | CM_MAX_2D_SURF_WIDTH + 1, |
| 127 | HEIGHT); }); |
| 128 | |
| 129 | RunEach<int32_t>( |
| 130 | CM_SUCCESS, |
| 131 | [this]() { return CreateDestroy(CM_SURFACE_FORMAT_A8R8G8B8, |
| 132 | WIDTH, CM_MAX_2D_SURF_HEIGHT); }); |
| 133 | |
| 134 | RunEach<int32_t>( |
| 135 | CM_INVALID_HEIGHT, |
| 136 | [this]() { return CreateDestroy(CM_SURFACE_FORMAT_A8R8G8B8, |
| 137 | WIDTH, |
| 138 | CM_MAX_2D_SURF_HEIGHT + 1); }); |
| 139 | return; |
| 140 | }//======== |
| 141 | |
| 142 | TEST_F(Surface2DUPTest, InvalidSystemMemory) |
| 143 | { |
| 144 | uint8_t *sys_mem = nullptr; |
| 145 | RunEach<int32_t>(CM_INVALID_ARG_VALUE, |
| 146 | [this, sys_mem]() { return CreateDestroy(sys_mem); }); |
| 147 | |
| 148 | sys_mem = new uint8_t[4*WIDTH*HEIGHT]; |
| 149 | uint8_t *sys_mem_for_surface = sys_mem; |
| 150 | if ((reinterpret_cast<uintptr_t>(sys_mem) & (0x1000 - 1)) == 0) // 4k-aligned. |
| 151 | { |
| 152 | ++sys_mem_for_surface; |
| 153 | } |
| 154 | auto CreateWithNonalignedPointer |
| 155 | = [this, sys_mem_for_surface]() |
| 156 | { return CreateDestroy(sys_mem_for_surface); }; |
| 157 | RunEach<int32_t>(CM_INVALID_ARG_VALUE, CreateWithNonalignedPointer); |
| 158 | delete[] sys_mem; |
| 159 | return; |
| 160 | }//======== |
| 161 | |
| 162 | TEST_F(Surface2DUPTest, PlanarFormat) |
| 163 | { |
| 164 | RunEach<int32_t>(CM_SUCCESS, |
| 165 | [this]() { return CreateDestroy(CM_SURFACE_FORMAT_NV12, |
| 166 | WIDTH, HEIGHT); }); |
| 167 | RunEach<int32_t>(CM_INVALID_WIDTH, |
| 168 | [this]() { return CreateDestroy(CM_SURFACE_FORMAT_NV12, |
| 169 | WIDTH + 1, HEIGHT); }); |
| 170 | |
| 171 | RunEach<int32_t>(CM_SUCCESS, |
| 172 | [this]() { return CreateDestroy(CM_SURFACE_FORMAT_YUY2, |
| 173 | WIDTH, HEIGHT); }); |
| 174 | RunEach<int32_t>(CM_INVALID_WIDTH, |
| 175 | [this]() { return CreateDestroy(CM_SURFACE_FORMAT_YUY2, |
| 176 | WIDTH + 1, HEIGHT); }); |
| 177 | |
| 178 | return; |
| 179 | }//======== |