File: shared_image_format_unittest.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (400 lines) | stat: -rw-r--r-- 16,263 bytes parent folder | download | duplicates (7)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/viz/common/resources/shared_image_format.h"

#include <limits>
#include <vector>

#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace viz {
namespace {

constexpr gfx::Size kDefaultSize(100, 100);
constexpr gfx::Size kOddSize(9, 9);

class SharedImageFormatTest : public testing::Test {
 public:
  void TestNumChannelsInPlane(std::vector<int> expected_channels,
                              SharedImageFormat format) {
    ASSERT_EQ(format.NumberOfPlanes(),
              static_cast<int>(expected_channels.size()));
    for (int plane_index = 0; plane_index < format.NumberOfPlanes();
         plane_index++) {
      EXPECT_TRUE(format.IsValidPlaneIndex(plane_index));
      EXPECT_EQ(expected_channels[plane_index],
                format.NumChannelsInPlane(plane_index));
    }
  }
};

TEST_F(SharedImageFormatTest, MultiPlaneNV12) {
  // 8-bit 4:2:0 Y_UV biplanar format (YUV_420_BIPLANAR)
  SharedImageFormat format = MultiPlaneFormat::kNV12;
  // Test for NumChannelsInPlane
  std::vector<int> expected_channels = {1, 2};
  TestNumChannelsInPlane(expected_channels, format);

  // Y: 100 bytes per row (1 channel * 1 byte * 100 width) * 100 rows = 10000
  // bytes.
  // UV: 100 bytes per row (2 channel * 1 byte * 50 width) * 50 rows = 5000
  // bytes.
  EXPECT_EQ(format.EstimatedSizeInBytes(kDefaultSize), 15000u);

  // Y: 9 bytes per row (1 channel * 1 byte * 9 width) * 9 rows = 81 bytes.
  // UV: 10 bytes per row (2 channels * 1 byte * 5 width) * 5 rows = 50 bytes.
  EXPECT_EQ(format.EstimatedSizeInBytes(kOddSize), 131u);
}

TEST_F(SharedImageFormatTest, MultiPlaneNV16) {
  // 8-bit 4:2:2 Y_UV biplanar format (YUV_422_BIPLANAR)
  SharedImageFormat format = MultiPlaneFormat::kNV16;
  // Test for NumChannelsInPlane
  std::vector<int> expected_channels = {1, 2};
  TestNumChannelsInPlane(expected_channels, format);

  // Y: 100 bytes per row (1 channel * 1 byte * 100 width) * 100 rows = 10000
  // bytes.
  // UV: 100 bytes per row (2 channel * 1 byte * 50 width) * 100 rows = 10000
  // bytes.
  EXPECT_EQ(format.EstimatedSizeInBytes(kDefaultSize), 20000u);

  // Y: 9 bytes per row (1 channel * 1 byte * 9 width) * 9 rows = 81 bytes.
  // UV: 10 bytes per row (2 channels * 1 byte * 5 width) * 9 rows = 90 bytes.
  EXPECT_EQ(format.EstimatedSizeInBytes(kOddSize), 171u);
}

TEST_F(SharedImageFormatTest, MultiPlaneNV24) {
  // 8-bit 4:4:4 Y_UV biplanar format (YUV_444_BIPLANAR)
  SharedImageFormat format = MultiPlaneFormat::kNV24;
  // Test for NumChannelsInPlane
  std::vector<int> expected_channels = {1, 2};
  TestNumChannelsInPlane(expected_channels, format);

  // Y: 100 bytes per row (1 channel * 1 byte * 100 width) * 100 rows = 10000
  // bytes.
  // UV: 200 bytes per row (2 channel * 1 byte * 100 width) * 100 rows = 20000
  // bytes.
  EXPECT_EQ(format.EstimatedSizeInBytes(kDefaultSize), 30000u);

  // Y: 9 bytes per row (1 channel * 1 byte * 9 width) * 9 rows = 81 bytes.
  // UV: 20 bytes per row (2 channels * 1 byte * 9 width) * 9 rows = 162
  // bytes.
  EXPECT_EQ(format.EstimatedSizeInBytes(kOddSize), 243u);
}

TEST_F(SharedImageFormatTest, MultiPlaneYVU) {
  // 8-bit 4:2:0 Y_V_U format (YVU_420)
  SharedImageFormat format = MultiPlaneFormat::kYV12;
  // Test for NumChannelsInPlane
  std::vector<int> expected_channels = {1, 1, 1};
  TestNumChannelsInPlane(expected_channels, format);

  EXPECT_EQ(format.EstimatedSizeInBytes(kDefaultSize), 15000u);

  // Y: 9 bytes per row (1 channel * 1 byte * 9 width) * 9 rows = 81 bytes.
  // V: 5 bytes per row (1 channels * 1 byte * 5 width) * 5 rows = 25 bytes.
  // U: 5 bytes per row (1 channels * 1 byte * 5 width) * 5 rows = 25 bytes.
  EXPECT_EQ(format.EstimatedSizeInBytes(kOddSize), 131u);
}

TEST_F(SharedImageFormatTest, MultiPlaneI420) {
  // 8-bit 4:2:0 Y_U_V format (I420)
  SharedImageFormat format = MultiPlaneFormat::kI420;
  // Test for NumChannelsInPlane
  std::vector<int> expected_channels = {1, 1, 1};
  TestNumChannelsInPlane(expected_channels, format);

  EXPECT_EQ(format.EstimatedSizeInBytes(kDefaultSize), 15000u);

  // Y: 9 bytes per row (1 channel * 1 byte * 9 width) * 9 rows = 81 bytes.
  // U: 5 bytes per row (1 channels * 1 byte * 5 width) * 5 rows = 25 bytes.
  // V: 5 bytes per row (1 channels * 1 byte * 5 width) * 5 rows = 25 bytes.
  EXPECT_EQ(format.EstimatedSizeInBytes(kOddSize), 131u);
}

TEST_F(SharedImageFormatTest, MultiPlaneI422) {
  // 8-bit 4:2:2 Y_U_V format (I422)
  SharedImageFormat format =
      SharedImageFormat::MultiPlane(SharedImageFormat::PlaneConfig::kY_U_V,
                                    SharedImageFormat::Subsampling::k422,
                                    SharedImageFormat::ChannelFormat::k8);
  // Test for NumChannelsInPlane
  std::vector<int> expected_channels = {1, 1, 1};
  TestNumChannelsInPlane(expected_channels, format);

  // Y: 100 bytes per row (1 channel * 1 byte * 100 width) * 100 rows = 10000
  // bytes.
  // U: 50 bytes per row (1 channel * 1 byte * 50 width) * 100 rows = 5000
  // bytes.
  // V: 50 bytes per row (1 channel * 1 byte * 50 width) * 100 rows = 5000
  // bytes.
  EXPECT_EQ(format.EstimatedSizeInBytes(kDefaultSize), 20000u);

  // Y: 9 bytes per row (1 channel * 1 byte * 9 width) * 9 rows = 81 bytes.
  // U: 5 bytes per row (1 channel * 1 byte * 5 width) * 9 rows = 45 bytes.
  // V: 5 bytes per row (1 channel * 1 byte * 5 width) * 9 rows = 45 bytes.
  EXPECT_EQ(format.EstimatedSizeInBytes(kOddSize), 171u);
}

TEST_F(SharedImageFormatTest, MultiPlaneI444) {
  // 8-bit 4:2:2 Y_U_V format (I444)
  SharedImageFormat format =
      SharedImageFormat::MultiPlane(SharedImageFormat::PlaneConfig::kY_U_V,
                                    SharedImageFormat::Subsampling::k444,
                                    SharedImageFormat::ChannelFormat::k8);
  // Test for NumChannelsInPlane
  std::vector<int> expected_channels = {1, 1, 1};
  TestNumChannelsInPlane(expected_channels, format);

  // Y: 100 bytes per row (1 channel * 1 byte * 100 width) * 100 rows = 10000
  // bytes.
  // U: 100 bytes per row (1 channel * 1 byte * 100 width) * 100 rows = 10000
  // bytes.
  // V: 100 bytes per row (1 channel * 1 byte * 100 width) * 100 rows = 10000
  // bytes.
  EXPECT_EQ(format.EstimatedSizeInBytes(kDefaultSize), 30000u);

  // Y: 9 bytes per row (1 channel * 1 byte * 9 width) * 9 rows = 81 bytes.
  // U: 9 bytes per row (1 channel * 1 byte * 9 width) * 9 rows = 81 bytes.
  // V: 9 bytes per row (1 channel * 1 byte * 9 width) * 9 rows = 81 bytes.
  EXPECT_EQ(format.EstimatedSizeInBytes(kOddSize), 243u);
}

TEST_F(SharedImageFormatTest, MultiPlaneP010) {
  // 10-bit 4:2:0 Y_UV biplanar format (P010)
  SharedImageFormat format = MultiPlaneFormat::kP010;
  // Test for NumChannelsInPlane
  std::vector<int> expected_channels = {1, 2};
  TestNumChannelsInPlane(expected_channels, format);

  // Y: 200 bytes per row (1 channel * 2 byte * 100 width) * 100 rows = 20000
  // bytes.
  // UV: 200 bytes per row (2 channel * 2 byte * 50 width) * 50 rows = 10000
  // bytes.
  EXPECT_EQ(format.EstimatedSizeInBytes(kDefaultSize), 30000u);

  // Y: 18 bytes per row (1 channel * 2 bytes * 9 width) * 9 rows = 162 bytes.
  // UV: 20 bytes per row (2 channels * 2 bytes * 5 width) * 5 rows = 100 bytes.
  EXPECT_EQ(format.EstimatedSizeInBytes(kOddSize), 262u);
}

TEST_F(SharedImageFormatTest, MultiPlaneP210) {
  // 10-bit 4:2:2 Y_UV biplanar format (P210)
  SharedImageFormat format = MultiPlaneFormat::kP210;
  // Test for NumChannelsInPlane
  std::vector<int> expected_channels = {1, 2};
  TestNumChannelsInPlane(expected_channels, format);

  // Y: 200 bytes per row (1 channel * 2 byte * 100 width) * 100 rows = 20000
  // bytes.
  // UV: 200 bytes per row (2 channel * 2 byte * 50 width) * 100 rows = 20000
  // bytes.
  EXPECT_EQ(format.EstimatedSizeInBytes(kDefaultSize), 40000u);

  // Y: 18 bytes per row (1 channel * 2 bytes * 9 width) * 9 rows = 162 bytes.
  // UV: 20 bytes per row (2 channels * 2 bytes * 5 width) * 9 rows = 180
  // bytes.
  EXPECT_EQ(format.EstimatedSizeInBytes(kOddSize), 342u);
}

TEST_F(SharedImageFormatTest, MultiPlaneP410) {
  // 10-bit 4:4:4 Y_UV biplanar format (P410)
  SharedImageFormat format = MultiPlaneFormat::kP410;
  // Test for NumChannelsInPlane
  std::vector<int> expected_channels = {1, 2};
  TestNumChannelsInPlane(expected_channels, format);

  // Y: 200 bytes per row (1 channel * 2 byte * 100 width) * 100 rows = 20000
  // bytes.
  // UV: 400 bytes per row (2 channel * 2 byte * 100 width) * 100 rows = 40000
  // bytes.
  EXPECT_EQ(format.EstimatedSizeInBytes(kDefaultSize), 60000u);

  // Y: 18 bytes per row (1 channel * 2 bytes * 9 width) * 9 rows = 162 bytes.
  // UV: 36 bytes per row (2 channels * 2 bytes * 9 width) * 9 rows = 324
  // bytes.
  EXPECT_EQ(format.EstimatedSizeInBytes(kOddSize), 486u);
}

TEST_F(SharedImageFormatTest, MultiPlaneYUVBiplanar16bit) {
  // 16-bit 4:2:0 Y_UV biplanar format
  SharedImageFormat format =
      SharedImageFormat::MultiPlane(SharedImageFormat::PlaneConfig::kY_UV,
                                    SharedImageFormat::Subsampling::k420,
                                    SharedImageFormat::ChannelFormat::k16);
  // Test for NumChannelsInPlane
  std::vector<int> expected_channels = {1, 2};
  TestNumChannelsInPlane(expected_channels, format);

  EXPECT_EQ(format.EstimatedSizeInBytes(kDefaultSize), 30000u);

  // Y: 18 bytes per row (1 channel * 2 bytes * 9 width) * 9 rows = 162 bytes.
  // UV: 20 bytes per row (2 channels * 2 bytes * 5 width) * 5 rows = 100 bytes.
  EXPECT_EQ(format.EstimatedSizeInBytes(kOddSize), 262u);
}

TEST_F(SharedImageFormatTest, MultiPlaneYUVATriplanar) {
  // 16-bit float 4:2:0 Y_UV_A triplanar format
  SharedImageFormat format =
      SharedImageFormat::MultiPlane(SharedImageFormat::PlaneConfig::kY_UV_A,
                                    SharedImageFormat::Subsampling::k420,
                                    SharedImageFormat::ChannelFormat::k16F);
  // Test for NumChannelsInPlane
  std::vector<int> expected_channels = {1, 2, 1};
  TestNumChannelsInPlane(expected_channels, format);

  EXPECT_EQ(format.EstimatedSizeInBytes(kDefaultSize), 50000u);

  // Y: 18 bytes per row (1 channel * 2 bytes * 9 width) * 9 rows = 162 bytes.
  // UV: 20 bytes per row (2 channels * 2 bytes * 5 width) * 5 rows = 100 bytes.
  // A: 18 bytes per row (1 channel * 2 bytes * 9 width) * 9 rows = 162 bytes.
  EXPECT_EQ(format.EstimatedSizeInBytes(kOddSize), 424u);
}

TEST_F(SharedImageFormatTest, MultiPlaneYUV420P10) {
  // 10-bit float 4:2:0 Y_U_V format (YUV420P10)
  SharedImageFormat format =
      SharedImageFormat::MultiPlane(SharedImageFormat::PlaneConfig::kY_U_V,
                                    SharedImageFormat::Subsampling::k420,
                                    SharedImageFormat::ChannelFormat::k10);
  // Test for NumChannelsInPlane
  std::vector<int> expected_channels = {1, 1, 1};
  TestNumChannelsInPlane(expected_channels, format);

  // Y: 200 bytes per row (1 channel * 2 bytes * 100 width) * 100 rows = 20000
  // bytes.
  // U: 100 bytes per row (1 channel * 2 bytes * 50 width) * 50 rows = 5000
  // bytes.
  // V: 100 bytes per row (1 channel * 2 bytes * 50 width) * 50 rows = 5000
  // bytes.
  EXPECT_EQ(format.EstimatedSizeInBytes(kDefaultSize), 30000u);

  // Y: 18 bytes per row (1 channel * 2 bytes * 9 width) * 9 rows = 162 bytes.
  // U: 10 bytes per row (1 channel * 2 bytes * 5 width) * 5 rows = 50 bytes.
  // V: 10 bytes per row (1 channel * 2 bytes * 5 width) * 5 rows = 50 bytes.
  EXPECT_EQ(format.EstimatedSizeInBytes(kOddSize), 262u);
}

TEST_F(SharedImageFormatTest, MultiPlaneYUV422P10) {
  // 10-bit float 4:2:2 Y_U_V format (YUV422P10)
  SharedImageFormat format =
      SharedImageFormat::MultiPlane(SharedImageFormat::PlaneConfig::kY_U_V,
                                    SharedImageFormat::Subsampling::k422,
                                    SharedImageFormat::ChannelFormat::k10);
  // Test for NumChannelsInPlane
  std::vector<int> expected_channels = {1, 1, 1};
  TestNumChannelsInPlane(expected_channels, format);

  // Y: 200 bytes per row (1 channel * 2 bytes * 100 width) * 100 rows = 20000
  // bytes.
  // U: 100 bytes per row (1 channel * 2 bytes * 50 width) * 100 rows = 10000
  // bytes.
  // V: 100 bytes per row (1 channel * 2 bytes * 50 width) * 100 rows = 10000
  // bytes.
  EXPECT_EQ(format.EstimatedSizeInBytes(kDefaultSize), 40000u);

  // Y: 18 bytes per row (1 channel * 2 bytes * 9 width) * 9 rows = 162 bytes.
  // U: 10 bytes per row (1 channel * 2 bytes * 5 width) * 9 rows = 90 bytes.
  // V: 10 bytes per row (1 channel * 2 bytes * 5 width) * 9 rows = 90 bytes.
  EXPECT_EQ(format.EstimatedSizeInBytes(kOddSize), 342u);
}

TEST_F(SharedImageFormatTest, MultiPlaneYUV444P10) {
  // 10-bit float 4:4:4 Y_U_V format (YUV444P10)
  SharedImageFormat format =
      SharedImageFormat::MultiPlane(SharedImageFormat::PlaneConfig::kY_U_V,
                                    SharedImageFormat::Subsampling::k444,
                                    SharedImageFormat::ChannelFormat::k10);
  // Test for NumChannelsInPlane
  std::vector<int> expected_channels = {1, 1, 1};
  TestNumChannelsInPlane(expected_channels, format);

  // Y: 200 bytes per row (1 channel * 2 bytes * 100 width) * 100 rows = 20000
  // bytes.
  // U: 200 bytes per row (1 channel * 2 bytes * 100 width) * 100 rows = 20000
  // bytes.
  // V: 200 bytes per row (1 channel * 2 bytes * 100 width) * 100 rows = 20000
  // bytes.
  EXPECT_EQ(format.EstimatedSizeInBytes(kDefaultSize), 60000u);

  // Y: 18 bytes per row (1 channel * 2 bytes * 9 width) * 9 rows = 162 bytes.
  // U: 18 bytes per row (1 channel * 2 bytes * 9 width) * 9 rows = 162 bytes.
  // V: 18 bytes per row (1 channel * 2 bytes * 9 width) * 9 rows = 162 bytes.
  EXPECT_EQ(format.EstimatedSizeInBytes(kOddSize), 486u);
}

TEST_F(SharedImageFormatTest, SinglePlaneRGBA_8888) {
  auto format = SinglePlaneFormat::kRGBA_8888;
  EXPECT_EQ(1, format.NumberOfPlanes());

  // 4 bytes per pixel.
  EXPECT_EQ(format.EstimatedSizeInBytes(kDefaultSize), 40000u);
}

TEST_F(SharedImageFormatTest, SinglePlaneRED_8) {
  auto format = SinglePlaneFormat::kR_8;
  EXPECT_EQ(1, format.NumberOfPlanes());

  // 1 byte per pixel.
  EXPECT_EQ(format.EstimatedSizeInBytes(kDefaultSize), 10000u);

  // 9 bytes per row * 9 rows.
  EXPECT_EQ(format.EstimatedSizeInBytes(kOddSize), 81u);
}

TEST_F(SharedImageFormatTest, SinglePlaneRG_88) {
  auto format = SinglePlaneFormat::kRG_88;
  EXPECT_EQ(1, format.NumberOfPlanes());

  // 2 bytes per pixel.
  EXPECT_EQ(format.EstimatedSizeInBytes(kDefaultSize), 20000u);

  // 18 bytes per row * 9 rows.
  EXPECT_EQ(format.EstimatedSizeInBytes(kOddSize), 162u);
}

TEST_F(SharedImageFormatTest, SinglePlaneETC1) {
  auto format = SinglePlaneFormat::kETC1;
  EXPECT_EQ(1, format.NumberOfPlanes());

  // 4 bits (not bytes) per pixel.
  EXPECT_EQ(format.EstimatedSizeInBytes(kDefaultSize), 5000u);

  // 5 bytes per row (rounded up) * 9 rows.
  EXPECT_EQ(format.EstimatedSizeInBytes(kOddSize), 45u);
}

TEST_F(SharedImageFormatTest, EstimatedSizeInBytesOverflow) {
  auto format = SinglePlaneFormat::kRGBA_F16;

  constexpr gfx::Size max_size(std::numeric_limits<int>::max(),
                               std::numeric_limits<int>::max());

  // MaybeEstimatedSizeInBytes() will return nullopt on overflow.
  EXPECT_FALSE(format.MaybeEstimatedSizeInBytes(max_size).has_value());

  // EstimatedSizeInBytes() will return 0 on overflow.
  EXPECT_EQ(format.EstimatedSizeInBytes(max_size), 0u);

  // VerifySizeInBytes() should return false on overflow.
  EXPECT_FALSE(format.VerifySizeInBytes(max_size));
}

TEST_F(SharedImageFormatTest, PrefersExternalSampler) {
  auto singleplanar_format = SinglePlaneFormat::kRGBA_F16;
  auto multiplanar_format = MultiPlaneFormat::kNV12;
  EXPECT_FALSE(singleplanar_format.PrefersExternalSampler());
  EXPECT_FALSE(multiplanar_format.PrefersExternalSampler());

#if BUILDFLAG(IS_OZONE)
  multiplanar_format.SetPrefersExternalSampler();
  EXPECT_TRUE(multiplanar_format.PrefersExternalSampler());
#endif
}

}  // namespace
}  // namespace viz