Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / gfx / buffer_format_util.cc
blob36222c9032119bed0a26cb053c807096771d4808
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.
5 #include "ui/gfx/buffer_format_util.h"
7 #include "base/logging.h"
8 #include "base/numerics/safe_math.h"
10 namespace gfx {
12 size_t NumberOfPlanesForBufferFormat(BufferFormat format) {
13 switch (format) {
14 case BufferFormat::ATC:
15 case BufferFormat::ATCIA:
16 case BufferFormat::DXT1:
17 case BufferFormat::DXT5:
18 case BufferFormat::ETC1:
19 case BufferFormat::R_8:
20 case BufferFormat::RGBA_4444:
21 case BufferFormat::RGBA_8888:
22 case BufferFormat::BGRX_8888:
23 case BufferFormat::BGRA_8888:
24 case BufferFormat::UYVY_422:
25 return 1;
26 case BufferFormat::YUV_420_BIPLANAR:
27 return 2;
28 case BufferFormat::YUV_420:
29 return 3;
31 NOTREACHED();
32 return 0;
35 size_t SubsamplingFactorForBufferFormat(BufferFormat format, int plane) {
36 switch (format) {
37 case BufferFormat::ATC:
38 case BufferFormat::ATCIA:
39 case BufferFormat::DXT1:
40 case BufferFormat::DXT5:
41 case BufferFormat::ETC1:
42 case BufferFormat::R_8:
43 case BufferFormat::RGBA_4444:
44 case BufferFormat::RGBA_8888:
45 case BufferFormat::BGRX_8888:
46 case BufferFormat::BGRA_8888:
47 case BufferFormat::UYVY_422:
48 return 1;
49 case BufferFormat::YUV_420: {
50 static size_t factor[] = {1, 2, 2};
51 DCHECK_LT(static_cast<size_t>(plane), arraysize(factor));
52 return factor[plane];
54 case gfx::BufferFormat::YUV_420_BIPLANAR: {
55 static size_t factor[] = {1, 2};
56 DCHECK_LT(static_cast<size_t>(plane), arraysize(factor));
57 return factor[plane];
60 NOTREACHED();
61 return 0;
64 size_t RowSizeForBufferFormat(size_t width, BufferFormat format, int plane) {
65 size_t row_size = 0;
66 bool valid = RowSizeForBufferFormatChecked(width, format, plane, &row_size);
67 DCHECK(valid);
68 return row_size;
71 bool RowSizeForBufferFormatChecked(
72 size_t width, BufferFormat format, int plane, size_t* size_in_bytes) {
73 base::CheckedNumeric<size_t> checked_size = width;
74 switch (format) {
75 case BufferFormat::ATCIA:
76 case BufferFormat::DXT5:
77 DCHECK_EQ(0, plane);
78 *size_in_bytes = width;
79 return true;
80 case BufferFormat::ATC:
81 case BufferFormat::DXT1:
82 case BufferFormat::ETC1:
83 DCHECK_EQ(0, plane);
84 DCHECK_EQ(0u, width % 2);
85 *size_in_bytes = width / 2;
86 return true;
87 case BufferFormat::R_8:
88 checked_size += 3;
89 if (!checked_size.IsValid())
90 return false;
91 *size_in_bytes = checked_size.ValueOrDie() & ~0x3;
92 return true;
93 case BufferFormat::RGBA_4444:
94 case BufferFormat::UYVY_422:
95 checked_size *= 2;
96 if (!checked_size.IsValid())
97 return false;
98 *size_in_bytes = checked_size.ValueOrDie();
99 return true;
100 case BufferFormat::BGRX_8888:
101 case BufferFormat::RGBA_8888:
102 case BufferFormat::BGRA_8888:
103 checked_size *= 4;
104 if (!checked_size.IsValid())
105 return false;
106 *size_in_bytes = checked_size.ValueOrDie();
107 return true;
108 case BufferFormat::YUV_420:
109 DCHECK_EQ(0u, width % 2);
110 *size_in_bytes = width / SubsamplingFactorForBufferFormat(format, plane);
111 return true;
112 case gfx::BufferFormat::YUV_420_BIPLANAR:
113 DCHECK_EQ(width % 2, 0u);
114 *size_in_bytes = width;
115 return true;
117 NOTREACHED();
118 return false;
121 size_t BufferSizeForBufferFormat(
122 const Size& size, BufferFormat format) {
123 size_t buffer_size = 0;
124 bool valid = BufferSizeForBufferFormatChecked(size, format, &buffer_size);
125 DCHECK(valid);
126 return buffer_size;
129 bool BufferSizeForBufferFormatChecked(
130 const Size& size, BufferFormat format, size_t* size_in_bytes) {
131 base::CheckedNumeric<size_t> checked_size = 0;
132 size_t num_planes = NumberOfPlanesForBufferFormat(format);
133 for (size_t i = 0; i < num_planes; ++i) {
134 size_t row_size = 0;
135 if (!RowSizeForBufferFormatChecked(size.width(), format, i, &row_size))
136 return false;
137 base::CheckedNumeric<size_t> checked_plane_size = row_size;
138 checked_plane_size *= size.height() /
139 SubsamplingFactorForBufferFormat(format, i);
140 if (!checked_plane_size.IsValid())
141 return false;
142 checked_size += checked_plane_size.ValueOrDie();
143 if (!checked_size.IsValid())
144 return false;
146 *size_in_bytes = checked_size.ValueOrDie();
147 return true;
150 } // namespace gfx