OLD | NEW |
(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 |
OLD | NEW |