File: resource_sizes.h

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 (224 lines) | stat: -rw-r--r-- 7,632 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
// 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.

#ifndef COMPONENTS_VIZ_COMMON_RESOURCES_RESOURCE_SIZES_H_
#define COMPONENTS_VIZ_COMMON_RESOURCES_RESOURCE_SIZES_H_

#include <stddef.h>

#include <limits>

#include "base/check_op.h"
#include "base/component_export.h"
#include "base/numerics/safe_math.h"
#include "cc/base/math_util.h"
#include "components/viz/common/resources/shared_image_format.h"
#include "ui/gfx/geometry/size.h"

namespace viz {

class COMPONENT_EXPORT(VIZ_SHARED_IMAGE_FORMAT) ResourceSizes {
 public:
  // Returns true if the size is valid and fits in bytes, false otherwise.
  // Sets the bytes result in the out parameter |bytes|.
  template <typename T>
  static bool MaybeSizeInBytes(const gfx::Size& size,
                               SharedImageFormat format,
                               T* bytes);

  // Dies with a CRASH() if the width can not be represented as a positive
  // number of bytes.
  // WARNING: The `format` must be single planar.
  template <typename T>
  static T CheckedWidthInBytes(int width, SharedImageFormat format);
  // Dies with a CRASH() if the size can not be represented as a positive
  // number of bytes.
  // WARNING: The `format` must be single planar.
  template <typename T>
  static T CheckedSizeInBytes(const gfx::Size& size, SharedImageFormat format);
  // Returns the width in bytes but may overflow or return 0. Only do this for
  // computing widths for sizes that have already been checked.
  // WARNING: The `format` must be single planar.
  template <typename T>
  static T UncheckedWidthInBytes(int width, SharedImageFormat format);

 private:
  template <typename T>
  static inline void VerifyType();

  template <typename T>
  static bool VerifyWidthInBytesInternal(int width,
                                         SharedImageFormat format,
                                         bool aligned);

  template <typename T>
  static bool MaybeWidthInBytesInternal(int width,
                                        SharedImageFormat format,
                                        bool aligned,
                                        T* bytes);

  template <typename T>
  static bool MaybeSizeInBytesInternal(const gfx::Size& size,
                                       SharedImageFormat format,
                                       bool aligned,
                                       T* bytes);

  template <typename T>
  static T WidthInBytesInternal(int width,
                                SharedImageFormat format,
                                bool aligned);

  template <typename T>
  static T SizeInBytesInternal(const gfx::Size& size,
                               SharedImageFormat format,
                               bool aligned);

  template <typename T>
  static bool MaybeRound(base::CheckedNumeric<T>* value, T mul);

  // Not instantiable.
  ResourceSizes() = delete;
};

template <typename T>
bool ResourceSizes::MaybeSizeInBytes(const gfx::Size& size,
                                     SharedImageFormat format,
                                     T* bytes) {
  VerifyType<T>();
  if (size.IsEmpty())
    return false;
  return MaybeSizeInBytesInternal<T>(size, format, false, bytes);
}

template <typename T>
T ResourceSizes::CheckedWidthInBytes(int width, SharedImageFormat format) {
  VerifyType<T>();
  CHECK_GT(width, 0);
  T bytes;
  CHECK(MaybeWidthInBytesInternal<T>(width, format, false, &bytes));
  return bytes;
}

template <typename T>
T ResourceSizes::CheckedSizeInBytes(const gfx::Size& size,
                                    SharedImageFormat format) {
  VerifyType<T>();
  CHECK(!size.IsEmpty());
  T bytes;
  CHECK(MaybeSizeInBytesInternal<T>(size, format, false, &bytes));
  return bytes;
}

template <typename T>
T ResourceSizes::UncheckedWidthInBytes(int width, SharedImageFormat format) {
  VerifyType<T>();
  DCHECK_GT(width, 0);
  DCHECK(VerifyWidthInBytesInternal<T>(width, format, false));
  return WidthInBytesInternal<T>(width, format, false);
}

template <typename T>
void ResourceSizes::VerifyType() {
  static_assert(
      std::numeric_limits<T>::is_integer && !std::is_same<T, bool>::value,
      "T must be non-bool integer type. Preferred type is size_t.");
}

template <typename T>
bool ResourceSizes::VerifyWidthInBytesInternal(int width,
                                               SharedImageFormat format,
                                               bool aligned) {
  T ignored;
  return MaybeWidthInBytesInternal(width, format, aligned, &ignored);
}

template <typename T>
bool ResourceSizes::MaybeWidthInBytesInternal(int width,
                                              SharedImageFormat format,
                                              bool aligned,
                                              T* bytes) {
  base::CheckedNumeric<T> bits_per_row = format.BitsPerPixel();
  bits_per_row *= width;
  if (!bits_per_row.IsValid())
    return false;

  // Roundup bits to byte (8 bits) boundary. If width is 3 and BitsPerPixel is
  // 4, then it should return 16, so that row pixels do not get truncated.
  if (!MaybeRound<T>(&bits_per_row, 8))
    return false;

  // Convert to bytes by dividing by 8. This can't fail as we've rounded to a
  // multiple of 8 above.
  base::CheckedNumeric<T> bytes_per_row = bits_per_row / 8;
  DCHECK(bytes_per_row.IsValid());

  // If aligned is true, bytes are aligned on 4-bytes boundaries for upload
  // performance, assuming that GL_PACK_ALIGNMENT or GL_UNPACK_ALIGNMENT have
  // not changed from default.
  if (aligned) {
    // This can't fail as we've just divided by 8, so we can always round up to
    // the nearest multiple of 4.
    bool succeeded = MaybeRound<T>(&bytes_per_row, 4);
    DCHECK(succeeded);
  }

  *bytes = bytes_per_row.ValueOrDie();
  return true;
}

template <typename T>
bool ResourceSizes::MaybeSizeInBytesInternal(const gfx::Size& size,
                                             SharedImageFormat format,
                                             bool aligned,
                                             T* bytes) {
  T width_in_bytes;
  if (!MaybeWidthInBytesInternal<T>(size.width(), format, aligned,
                                    &width_in_bytes)) {
    return false;
  }

  base::CheckedNumeric<T> total_bytes = width_in_bytes;
  total_bytes *= size.height();
  if (!total_bytes.IsValid())
    return false;

  *bytes = total_bytes.ValueOrDie();
  return true;
}

template <typename T>
T ResourceSizes::WidthInBytesInternal(int width,
                                      SharedImageFormat format,
                                      bool aligned) {
  T bytes = format.BitsPerPixel();
  bytes *= width;
  bytes = cc::MathUtil::UncheckedRoundUp<T>(bytes, 8);
  bytes /= 8;
  if (aligned)
    bytes = cc::MathUtil::UncheckedRoundUp<T>(bytes, 4);
  return bytes;
}

template <typename T>
T ResourceSizes::SizeInBytesInternal(const gfx::Size& size,
                                     SharedImageFormat format,
                                     bool aligned) {
  T bytes = WidthInBytesInternal<T>(size.width(), format, aligned);
  bytes *= size.height();
  return bytes;
}

template <typename T>
bool ResourceSizes::MaybeRound(base::CheckedNumeric<T>* value, T mul) {
  DCHECK(value->IsValid());
  T to_round = value->ValueOrDie();
  if (!cc::MathUtil::VerifyRoundup<T>(to_round, mul))
    return false;
  *value = cc::MathUtil::UncheckedRoundUp<T>(to_round, mul);
  return true;
}

}  // namespace viz

#endif  // COMPONENTS_VIZ_COMMON_RESOURCES_RESOURCE_SIZES_H_