Index: ui/gfx/buffer_format_util.cc |
diff --git a/ui/gfx/buffer_format_util.cc b/ui/gfx/buffer_format_util.cc |
index 33bf627aad2817e56a82ac1d6e9f05e396f0343d..704b68038d6aa069bf8c7a973e2953a20b090c88 100644 |
--- a/ui/gfx/buffer_format_util.cc |
+++ b/ui/gfx/buffer_format_util.cc |
@@ -23,9 +23,60 @@ static_assert(arraysize(kBufferFormats) == |
(static_cast<int>(BufferFormat::LAST) + 1), |
"BufferFormat::LAST must be last value of kBufferFormats"); |
+ |
+bool RowSizeForBufferFormatChecked( |
+ size_t width, BufferFormat format, int plane, size_t* size_in_bytes) { |
+ base::CheckedNumeric<size_t> checked_size = width; |
+ switch (format) { |
+ case BufferFormat::ATCIA: |
+ case BufferFormat::DXT5: |
+ DCHECK_EQ(0, plane); |
+ *size_in_bytes = width; |
+ return true; |
+ case BufferFormat::ATC: |
+ case BufferFormat::DXT1: |
+ case BufferFormat::ETC1: |
+ DCHECK_EQ(0, plane); |
+ DCHECK_EQ(0u, width % 2); |
+ *size_in_bytes = width / 2; |
+ return true; |
+ case BufferFormat::R_8: |
+ checked_size += 3; |
+ if (!checked_size.IsValid()) |
+ return false; |
+ *size_in_bytes = checked_size.ValueOrDie() & ~0x3; |
+ return true; |
+ case BufferFormat::RGBA_4444: |
+ case BufferFormat::UYVY_422: |
+ checked_size *= 2; |
+ if (!checked_size.IsValid()) |
+ return false; |
+ *size_in_bytes = checked_size.ValueOrDie(); |
+ return true; |
+ case BufferFormat::BGRX_8888: |
+ case BufferFormat::RGBA_8888: |
+ case BufferFormat::BGRA_8888: |
+ checked_size *= 4; |
+ if (!checked_size.IsValid()) |
+ return false; |
+ *size_in_bytes = checked_size.ValueOrDie(); |
+ return true; |
+ case BufferFormat::YUV_420: |
+ DCHECK_EQ(0u, width % 2); |
+ *size_in_bytes = width / SubsamplingFactorForBufferFormat(format, plane); |
+ return true; |
+ case BufferFormat::YUV_420_BIPLANAR: |
+ DCHECK_EQ(width % 2, 0u); |
+ *size_in_bytes = width; |
+ return true; |
+ } |
+ NOTREACHED(); |
+ return false; |
+} |
+ |
} // namespace |
-std::vector<BufferFormat> GetBufferFormats() { |
+std::vector<BufferFormat> GetBufferFormatsForTesting() { |
return std::vector<BufferFormat>(kBufferFormats, |
kBufferFormats + arraysize(kBufferFormats)); |
} |
@@ -72,7 +123,7 @@ size_t SubsamplingFactorForBufferFormat(BufferFormat format, int plane) { |
DCHECK_LT(static_cast<size_t>(plane), arraysize(factor)); |
return factor[plane]; |
} |
- case gfx::BufferFormat::YUV_420_BIPLANAR: { |
+ case BufferFormat::YUV_420_BIPLANAR: { |
static size_t factor[] = {1, 2}; |
DCHECK_LT(static_cast<size_t>(plane), arraysize(factor)); |
return factor[plane]; |
@@ -89,66 +140,16 @@ size_t RowSizeForBufferFormat(size_t width, BufferFormat format, int plane) { |
return row_size; |
} |
-bool RowSizeForBufferFormatChecked( |
- size_t width, BufferFormat format, int plane, size_t* size_in_bytes) { |
- base::CheckedNumeric<size_t> checked_size = width; |
- switch (format) { |
- case BufferFormat::ATCIA: |
- case BufferFormat::DXT5: |
- DCHECK_EQ(0, plane); |
- *size_in_bytes = width; |
- return true; |
- case BufferFormat::ATC: |
- case BufferFormat::DXT1: |
- case BufferFormat::ETC1: |
- DCHECK_EQ(0, plane); |
- DCHECK_EQ(0u, width % 2); |
- *size_in_bytes = width / 2; |
- return true; |
- case BufferFormat::R_8: |
- checked_size += 3; |
- if (!checked_size.IsValid()) |
- return false; |
- *size_in_bytes = checked_size.ValueOrDie() & ~0x3; |
- return true; |
- case BufferFormat::RGBA_4444: |
- case BufferFormat::UYVY_422: |
- checked_size *= 2; |
- if (!checked_size.IsValid()) |
- return false; |
- *size_in_bytes = checked_size.ValueOrDie(); |
- return true; |
- case BufferFormat::BGRX_8888: |
- case BufferFormat::RGBA_8888: |
- case BufferFormat::BGRA_8888: |
- checked_size *= 4; |
- if (!checked_size.IsValid()) |
- return false; |
- *size_in_bytes = checked_size.ValueOrDie(); |
- return true; |
- case BufferFormat::YUV_420: |
- DCHECK_EQ(0u, width % 2); |
- *size_in_bytes = width / SubsamplingFactorForBufferFormat(format, plane); |
- return true; |
- case gfx::BufferFormat::YUV_420_BIPLANAR: |
- DCHECK_EQ(width % 2, 0u); |
- *size_in_bytes = width; |
- return true; |
- } |
- NOTREACHED(); |
- return false; |
-} |
- |
-size_t BufferSizeForBufferFormat( |
- const Size& size, BufferFormat format) { |
+size_t BufferSizeForBufferFormat(const Size& size, BufferFormat format) { |
size_t buffer_size = 0; |
bool valid = BufferSizeForBufferFormatChecked(size, format, &buffer_size); |
DCHECK(valid); |
return buffer_size; |
} |
-bool BufferSizeForBufferFormatChecked( |
- const Size& size, BufferFormat format, size_t* size_in_bytes) { |
+bool BufferSizeForBufferFormatChecked(const Size& size, |
+ BufferFormat format, |
+ size_t* size_in_bytes) { |
base::CheckedNumeric<size_t> checked_size = 0; |
size_t num_planes = NumberOfPlanesForBufferFormat(format); |
for (size_t i = 0; i < num_planes; ++i) { |
@@ -168,4 +169,38 @@ bool BufferSizeForBufferFormatChecked( |
return true; |
} |
+int BufferOffsetForBufferFormat(const Size& size, |
+ BufferFormat format, |
+ size_t plane) { |
+ DCHECK_LT(plane, gfx::NumberOfPlanesForBufferFormat(format)); |
+ switch (format) { |
+ case BufferFormat::ATC: |
+ case BufferFormat::ATCIA: |
+ case BufferFormat::DXT1: |
+ case BufferFormat::DXT5: |
+ case BufferFormat::ETC1: |
+ case BufferFormat::R_8: |
+ case BufferFormat::RGBA_4444: |
+ case BufferFormat::RGBA_8888: |
+ case BufferFormat::BGRX_8888: |
+ case BufferFormat::BGRA_8888: |
+ case BufferFormat::UYVY_422: |
+ return 0; |
+ case BufferFormat::YUV_420: { |
+ static size_t offset_in_2x2_sub_sampling_sizes[] = {0, 4, 5}; |
+ DCHECK_LT(plane, arraysize(offset_in_2x2_sub_sampling_sizes)); |
+ return offset_in_2x2_sub_sampling_sizes[plane] * |
+ (size.width() / 2 + size.height() / 2); |
+ } |
+ case gfx::BufferFormat::YUV_420_BIPLANAR: { |
+ static size_t offset_in_2x2_sub_sampling_sizes[] = {0, 4}; |
+ DCHECK_LT(plane, arraysize(offset_in_2x2_sub_sampling_sizes)); |
+ return offset_in_2x2_sub_sampling_sizes[plane] * |
+ (size.width() / 2 + size.height() / 2); |
+ } |
+ } |
+ NOTREACHED(); |
+ return 0; |
+} |
+ |
} // namespace gfx |