Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Side by Side Diff: cc/resources/resource_util.cc

Issue 1202843008: cc: Fix BytesPerPixel issue and refactor. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Corrected comments. Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "cc/resources/resource_util.h"
6
7 #include "base/numerics/safe_math.h"
8 #include "cc/base/math_util.h"
9
10 namespace cc {
11
12 // static
13 size_t ResourceUtil::CheckedSizeInBytes(const gfx::Size& size,
14 ResourceFormat format) {
15 DCHECK(VerifySizeInBytes(size, format));
16 base::CheckedNumeric<size_t> checked_value = BitsPerPixel(format);
17 checked_value *= size.width();
18 checked_value *= size.height();
19 checked_value /= 8;
20 return checked_value.ValueOrDie();
21 }
22
23 // static
24 size_t ResourceUtil::UncheckedWidthInBytes(size_t width,
25 ResourceFormat format) {
26 size_t bytes_per_row = width * static_cast<size_t>(BitsPerPixel(format)) / 8;
27 return bytes_per_row;
28 }
29
30 // static
31 size_t ResourceUtil::UncheckedWidthInBytesAligned(size_t width,
32 ResourceFormat format) {
33 // Use 4-byte row alignment (OpenGL default) for upload performance.
34 // Assuming that GL_UNPACK_ALIGNMENT has not changed from default.
35 return MathUtil::RoundUp<size_t>(UncheckedWidthInBytes(width, format), 4u);
36 }
37
38 // static
39 size_t ResourceUtil::UncheckedSizeInBytes(const gfx::Size& size,
40 ResourceFormat format) {
41 DCHECK(VerifySizeInBytes(size, format));
42 return size.height() * UncheckedWidthInBytes(size.width(), format);
43 }
44
45 // static
46 size_t ResourceUtil::UncheckedSizeInBytesAligned(const gfx::Size& size,
47 ResourceFormat format) {
48 DCHECK(VerifySizeInBytes(size, format));
49 return size.height() * UncheckedWidthInBytesAligned(size.width(), format);
50 }
51
52 // static
53 bool ResourceUtil::VerifySizeInBytes(const gfx::Size& size,
54 ResourceFormat format) {
55 base::CheckedNumeric<size_t> checked_value = BitsPerPixel(format);
56 checked_value *= size.width();
57 checked_value *= size.height();
58 if (!checked_value.IsValid())
59 return false;
60 size_t value = checked_value.ValueOrDie();
61 if ((value % 8) != 0)
62 return false;
63 return true;
64 }
65
66 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698