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

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: RowSize=>Width. 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 bool ResourceUtil::VerifySizeInBytes(const gfx::Size& size,
14 ResourceFormat format) {
15 base::CheckedNumeric<size_t> checked_value = BitsPerPixel(format);
16 checked_value *= size.width();
17 checked_value *= size.height();
18 if (!checked_value.IsValid())
19 return false;
20 size_t value = checked_value.ValueOrDie();
21 if ((value % 8) != 0)
22 return false;
23 return true;
24 }
25
26 // static
27 size_t ResourceUtil::CheckedSizeInBytes(const gfx::Size& size,
vmpstr 2015/07/14 17:44:15 I'd weakly prefer SizeInBytes to be the checked ve
prashant.n 2015/07/15 07:03:31 You mean to say - instead of SizeInBytes, use Unch
vmpstr 2015/07/15 17:43:22 Yeah, just a function rename. If it's just Foo it
28 ResourceFormat format) {
29 DCHECK(VerifySizeInBytes(size, format));
30 base::CheckedNumeric<size_t> checked_value = BitsPerPixel(format);
31 checked_value *= size.width();
32 checked_value *= size.height();
33 checked_value /= 8;
34 return checked_value.ValueOrDie();
35 }
36
37 // static
38 size_t ResourceUtil::WidthInBytes(size_t width, ResourceFormat format) {
39 size_t bytes_per_row = width * static_cast<size_t>(BitsPerPixel(format)) / 8;
40 return bytes_per_row;
41 }
42
43 // static
44 size_t ResourceUtil::SizeInBytes(const gfx::Size& size, ResourceFormat format) {
45 DCHECK(VerifySizeInBytes(size, format));
46 return size.height() * WidthInBytes(size.width(), format);
47 }
48
49 // static
50 size_t ResourceUtil::WidthInBytesAligned(size_t width, ResourceFormat format) {
51 // Use 4-byte row alignment (OpenGL default) for upload performance.
52 // Assuming that GL_UNPACK_ALIGNMENT has not changed from default.
53 return MathUtil::RoundUp<size_t>(WidthInBytes(width, format), 4u);
54 }
55
56 // static
57 size_t ResourceUtil::SizeInBytesAligned(const gfx::Size& size,
58 ResourceFormat format) {
59 DCHECK(VerifySizeInBytes(size, format));
60 return size.height() * WidthInBytesAligned(size.width(), format);
61 }
62
63 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698