File: resource_sizes_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 (140 lines) | stat: -rw-r--r-- 4,829 bytes parent folder | download | duplicates (5)
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
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stddef.h>

#include "components/viz/common/resources/resource_sizes.h"
#include "components/viz/common/resources/shared_image_format.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace viz {
namespace {

struct TestFormat {
  SharedImageFormat format;
  size_t expected_bytes;
  size_t expected_bytes_aligned;
};

class ResourceUtilTest : public testing::Test {
 public:
  void TestCheckedWidthInBytes(int width, base::span<TestFormat> test_formats) {
    for (auto& test_format : test_formats) {
      size_t bytes =
          ResourceSizes::CheckedWidthInBytes<size_t>(width, test_format.format);
      EXPECT_EQ(bytes, test_format.expected_bytes);
    }
  }

  void TestUncheckedWidthInBytes(int width,
                                 base::span<TestFormat> test_formats) {
    for (auto& test_format : test_formats) {
      size_t bytes = ResourceSizes::UncheckedWidthInBytes<size_t>(
          width, test_format.format);
      EXPECT_EQ(bytes, test_format.expected_bytes);
    }
  }

  void TestCheckedSizeInBytes(const gfx::Size& size,
                              base::span<TestFormat> test_formats) {
    for (auto& test_format : test_formats) {
      size_t bytes =
          ResourceSizes::CheckedSizeInBytes<size_t>(size, test_format.format);
      EXPECT_EQ(bytes, test_format.expected_bytes);
    }
  }
};

TEST_F(ResourceUtilTest, WidthInBytes) {
  // Check bytes for even width.
  int width = 10;
  TestFormat test_formats[] = {
      {SinglePlaneFormat::kRGBA_8888, 40, 40},  // for 32 bits
      {SinglePlaneFormat::kRGBA_4444, 20, 20},  // for 16 bits
      {SinglePlaneFormat::kALPHA_8, 10, 12},    // for 8 bits
      {SinglePlaneFormat::kETC1, 5, 8}          // for 4 bits
  };

  TestCheckedWidthInBytes(width, test_formats);
  TestUncheckedWidthInBytes(width, test_formats);

  // Check bytes for odd width.
  int width_odd = 11;
  TestFormat test_formats_odd[] = {
      {SinglePlaneFormat::kRGBA_8888, 44, 44},  // for 32 bits
      {SinglePlaneFormat::kRGBA_4444, 22, 24},  // for 16 bits
      {SinglePlaneFormat::kALPHA_8, 11, 12},    // for 8 bits
      {SinglePlaneFormat::kETC1, 6, 8}          // for 4 bits
  };

  TestCheckedWidthInBytes(width_odd, test_formats_odd);
  TestUncheckedWidthInBytes(width_odd, test_formats_odd);
}

TEST_F(ResourceUtilTest, SizeInBytes) {
  // Check bytes for even size.
  gfx::Size size(10, 10);
  TestFormat test_formats[] = {
      {SinglePlaneFormat::kRGBA_8888, 400, 400},  // for 32 bits
      {SinglePlaneFormat::kRGBA_4444, 200, 200},  // for 16 bits
      {SinglePlaneFormat::kALPHA_8, 100, 120},    // for 8 bits
      {SinglePlaneFormat::kETC1, 50, 80}          // for 4 bits
  };

  TestCheckedSizeInBytes(size, test_formats);

  // Check bytes for odd size.
  gfx::Size size_odd(11, 11);
  TestFormat test_formats_odd[] = {
      {SinglePlaneFormat::kRGBA_8888, 484, 484},  // for 32 bits
      {SinglePlaneFormat::kRGBA_4444, 242, 264},  // for 16 bits
      {SinglePlaneFormat::kALPHA_8, 121, 132},    // for 8 bits
      {SinglePlaneFormat::kETC1, 66, 88}          // for 4 bits
  };

  TestCheckedSizeInBytes(size_odd, test_formats_odd);
}

TEST_F(ResourceUtilTest, SizeInBytesOverflow) {
  gfx::Size size(10, 10);
  // 10 * 16 * 10 = 1600 bits, overflows in char, but fits in int.
  signed char ignored_char;
  EXPECT_FALSE(ResourceSizes::MaybeSizeInBytes<signed char>(
      size, SinglePlaneFormat::kRGBA_4444, &ignored_char));
  int ignored_int;
  EXPECT_TRUE(ResourceSizes::MaybeSizeInBytes<int>(
      size, SinglePlaneFormat::kRGBA_4444, &ignored_int));
}

TEST_F(ResourceUtilTest, WidthOverflowDoesNotCrash) {
  gfx::Size size(0x20000000, 1);
  // 0x20000000 * 4 = 0x80000000 which overflows int. Should return false, not
  // crash.
  int bytes;
  EXPECT_FALSE(ResourceSizes::MaybeSizeInBytes<int>(
      size, SinglePlaneFormat::kBGRA_8888, &bytes));
}

// Checks that we do not incorrectly indicate that a size has overflowed when
// only the size in bits overflows, but not the size in bytes.
TEST_F(ResourceUtilTest, SizeInBitsOverflowBytesOk) {
  gfx::Size size(10000, 10000);
  // 8192 * 8192 * 32 = 0x80000000, overflows int.
  // Bytes are /8 and do not overflow.
  int ignored;
  EXPECT_TRUE(ResourceSizes::MaybeSizeInBytes<int>(
      size, SinglePlaneFormat::kBGRA_8888, &ignored));
}

// Checks that we correctly identify overflow in cases caused by rounding.
TEST_F(ResourceUtilTest, RoundingOverflows) {
  gfx::Size size(0x1FFFFFFF, 1);
  // 0x1FFFFFFF * 4 = 0x7FFFFFFC. Will overflow when rounded up.
  int ignored;
  EXPECT_FALSE(ResourceSizes::MaybeSizeInBytes<int>(
      size, SinglePlaneFormat::kETC1, &ignored));
}

}  // namespace
}  // namespace viz