1 // copyright (c) 2022 the android open source project
2 //
3 // licensed under the apache license, version 2.0 (the "license");
4 // you may not use this file except in compliance with the license.
5 // you may obtain a copy of the license at
6 //
7 // http://www.apache.org/licenses/license-2.0
8 //
9 // unless required by applicable law or agreed to in writing, software
10 // distributed under the license is distributed on an "as is" basis,
11 // without warranties or conditions of any kind, either express or implied.
12 // see the license for the specific language governing permissions and
13 // limitations under the license.
14
15 #include <gmock/gmock.h>
16 #include <gtest/gtest.h>
17
18 #include "VkFormatUtils.h"
19
20 namespace gfxstream {
21 namespace vk {
22 namespace {
23
24 using ::testing::AllOf;
25 using ::testing::ElementsAre;
26 using ::testing::Eq;
27 using ::testing::ExplainMatchResult;
28 using ::testing::Field;
29 using ::testing::IsFalse;
30 using ::testing::IsTrue;
31
32 MATCHER_P(EqsVkExtent3D, expected, "") {
33 return ExplainMatchResult(AllOf(Field("width", &VkExtent3D::width, Eq(expected.width)),
34 Field("height", &VkExtent3D::height, Eq(expected.height)),
35 Field("depth", &VkExtent3D::depth, Eq(expected.depth))),
36 arg, result_listener);
37 }
38
39 MATCHER_P(EqsVkImageSubresourceLayers, expected, "") {
40 return ExplainMatchResult(
41 AllOf(Field("aspectMask", &VkImageSubresourceLayers::aspectMask, Eq(expected.aspectMask)),
42 Field("mipLevel", &VkImageSubresourceLayers::mipLevel, Eq(expected.mipLevel)),
43 Field("baseArrayLayer", &VkImageSubresourceLayers::baseArrayLayer,
44 Eq(expected.baseArrayLayer)),
45 Field("layerCount", &VkImageSubresourceLayers::layerCount, Eq(expected.layerCount))),
46 arg, result_listener);
47 }
48
49 MATCHER_P(EqsVkOffset3D, expected, "") {
50 return ExplainMatchResult(AllOf(Field("x", &VkOffset3D::x, Eq(expected.x)),
51 Field("y", &VkOffset3D::y, Eq(expected.y)),
52 Field("z", &VkOffset3D::z, Eq(expected.z))),
53 arg, result_listener);
54 }
55
56 MATCHER_P(EqsVkBufferImageCopy, expected, "") {
57 return ExplainMatchResult(
58 AllOf(Field("bufferOffset", &VkBufferImageCopy::bufferOffset, Eq(expected.bufferOffset)),
59 Field("bufferRowLength", &VkBufferImageCopy::bufferRowLength,
60 Eq(expected.bufferRowLength)),
61 Field("bufferImageHeight", &VkBufferImageCopy::bufferImageHeight,
62 Eq(expected.bufferImageHeight)),
63 Field("imageSubresource", &VkBufferImageCopy::imageSubresource,
64 EqsVkImageSubresourceLayers(expected.imageSubresource)),
65 Field("imageOffset", &VkBufferImageCopy::imageOffset,
66 EqsVkOffset3D(expected.imageOffset)),
67 Field("imageExtent", &VkBufferImageCopy::imageExtent,
68 EqsVkExtent3D(expected.imageExtent))),
69 arg, result_listener);
70 }
71
TEST(VkFormatUtilsTest,GetTransferInfoInvalidFormat)72 TEST(VkFormatUtilsTest, GetTransferInfoInvalidFormat) {
73 const VkFormat format = VK_FORMAT_UNDEFINED;
74 const uint32_t width = 16;
75 const uint32_t height = 16;
76 ASSERT_THAT(getFormatTransferInfo(format, width, height, nullptr, nullptr), IsFalse());
77 }
78
TEST(VkFormatUtilsTest,GetTransferInfoRGBA)79 TEST(VkFormatUtilsTest, GetTransferInfoRGBA) {
80 const VkFormat format = VK_FORMAT_R8G8B8A8_UNORM;
81 const uint32_t width = 16;
82 const uint32_t height = 16;
83
84 VkDeviceSize bufferCopySize;
85 std::vector<VkBufferImageCopy> bufferImageCopies;
86 ASSERT_THAT(getFormatTransferInfo(format, width, height, &bufferCopySize, &bufferImageCopies),
87 IsTrue());
88 EXPECT_THAT(bufferCopySize, Eq(1024));
89 ASSERT_THAT(bufferImageCopies, ElementsAre(EqsVkBufferImageCopy(VkBufferImageCopy{
90 .bufferOffset = 0,
91 .bufferRowLength = 16,
92 .bufferImageHeight = 0,
93 .imageSubresource =
94 {
95 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
96 .mipLevel = 0,
97 .baseArrayLayer = 0,
98 .layerCount = 1,
99 },
100 .imageOffset =
101 {
102 .x = 0,
103 .y = 0,
104 .z = 0,
105 },
106 .imageExtent =
107 {
108 .width = 16,
109 .height = 16,
110 .depth = 1,
111 },
112 })));
113 }
114
TEST(VkFormatUtilsTest,GetTransferInfoNV12OrNV21)115 TEST(VkFormatUtilsTest, GetTransferInfoNV12OrNV21) {
116 const VkFormat format = VK_FORMAT_G8_B8R8_2PLANE_420_UNORM;
117 const uint32_t width = 16;
118 const uint32_t height = 16;
119
120 VkDeviceSize bufferCopySize;
121 std::vector<VkBufferImageCopy> bufferImageCopies;
122 ASSERT_THAT(getFormatTransferInfo(format, width, height, &bufferCopySize, &bufferImageCopies),
123 IsTrue());
124 EXPECT_THAT(bufferCopySize, Eq(384));
125 ASSERT_THAT(bufferImageCopies,
126 ElementsAre(EqsVkBufferImageCopy(VkBufferImageCopy{
127 .bufferOffset = 0,
128 .bufferRowLength = 16,
129 .bufferImageHeight = 0,
130 .imageSubresource =
131 {
132 .aspectMask = VK_IMAGE_ASPECT_PLANE_0_BIT,
133 .mipLevel = 0,
134 .baseArrayLayer = 0,
135 .layerCount = 1,
136 },
137 .imageOffset =
138 {
139 .x = 0,
140 .y = 0,
141 .z = 0,
142 },
143 .imageExtent =
144 {
145 .width = 16,
146 .height = 16,
147 .depth = 1,
148 },
149 }),
150 EqsVkBufferImageCopy(VkBufferImageCopy{
151 .bufferOffset = 256,
152 .bufferRowLength = 8,
153 .bufferImageHeight = 0,
154 .imageSubresource =
155 {
156 .aspectMask = VK_IMAGE_ASPECT_PLANE_1_BIT,
157 .mipLevel = 0,
158 .baseArrayLayer = 0,
159 .layerCount = 1,
160 },
161 .imageOffset =
162 {
163 .x = 0,
164 .y = 0,
165 .z = 0,
166 },
167 .imageExtent =
168 {
169 .width = 8,
170 .height = 8,
171 .depth = 1,
172 },
173 })));
174 }
175
TEST(VkFormatUtilsTest,GetTransferInfoYV12OrYV21)176 TEST(VkFormatUtilsTest, GetTransferInfoYV12OrYV21) {
177 const VkFormat format = VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM;
178 const uint32_t width = 32;
179 const uint32_t height = 32;
180
181 VkDeviceSize bufferCopySize;
182 std::vector<VkBufferImageCopy> bufferImageCopies;
183 ASSERT_THAT(getFormatTransferInfo(format, width, height, &bufferCopySize, &bufferImageCopies),
184 IsTrue());
185 EXPECT_THAT(bufferCopySize, Eq(1536));
186 ASSERT_THAT(bufferImageCopies,
187 ElementsAre(EqsVkBufferImageCopy(VkBufferImageCopy{
188 .bufferOffset = 0,
189 .bufferRowLength = 32,
190 .bufferImageHeight = 0,
191 .imageSubresource =
192 {
193 .aspectMask = VK_IMAGE_ASPECT_PLANE_0_BIT,
194 .mipLevel = 0,
195 .baseArrayLayer = 0,
196 .layerCount = 1,
197 },
198 .imageOffset =
199 {
200 .x = 0,
201 .y = 0,
202 .z = 0,
203 },
204 .imageExtent =
205 {
206 .width = 32,
207 .height = 32,
208 .depth = 1,
209 },
210 }),
211 EqsVkBufferImageCopy(VkBufferImageCopy{
212 .bufferOffset = 1024,
213 .bufferRowLength = 16,
214 .bufferImageHeight = 0,
215 .imageSubresource =
216 {
217 .aspectMask = VK_IMAGE_ASPECT_PLANE_1_BIT,
218 .mipLevel = 0,
219 .baseArrayLayer = 0,
220 .layerCount = 1,
221 },
222 .imageOffset =
223 {
224 .x = 0,
225 .y = 0,
226 .z = 0,
227 },
228 .imageExtent =
229 {
230 .width = 16,
231 .height = 16,
232 .depth = 1,
233 },
234 }),
235 EqsVkBufferImageCopy(VkBufferImageCopy{
236 .bufferOffset = 1280,
237 .bufferRowLength = 16,
238 .bufferImageHeight = 0,
239 .imageSubresource =
240 {
241 .aspectMask = VK_IMAGE_ASPECT_PLANE_2_BIT,
242 .mipLevel = 0,
243 .baseArrayLayer = 0,
244 .layerCount = 1,
245 },
246 .imageOffset =
247 {
248 .x = 0,
249 .y = 0,
250 .z = 0,
251 },
252 .imageExtent =
253 {
254 .width = 16,
255 .height = 16,
256 .depth = 1,
257 },
258 })));
259 }
260
261 } // namespace
262 } // namespace vk
263 } // namespace gfxstream
264
265