OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/buffer_format_util.h" | 5 #include "ui/gfx/buffer_format_util.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/numerics/safe_math.h" | 9 #include "base/numerics/safe_math.h" |
10 | 10 |
11 namespace gfx { | 11 namespace gfx { |
12 namespace { | 12 namespace { |
13 | 13 |
14 const BufferFormat kBufferFormats[] = {BufferFormat::ATC, | 14 const BufferFormat kBufferFormats[] = { |
15 BufferFormat::ATCIA, | 15 BufferFormat::ATC, BufferFormat::ATCIA, |
16 BufferFormat::DXT1, | 16 BufferFormat::DXT1, BufferFormat::DXT5, |
17 BufferFormat::DXT5, | 17 BufferFormat::ETC1, BufferFormat::R_8, |
18 BufferFormat::ETC1, | 18 BufferFormat::RG_88, BufferFormat::BGR_565, |
19 BufferFormat::R_8, | 19 BufferFormat::RGBA_4444, BufferFormat::RGBX_8888, |
20 BufferFormat::RG_88, | 20 BufferFormat::RGBA_8888, BufferFormat::BGRX_8888, |
21 BufferFormat::BGR_565, | 21 BufferFormat::BGRA_8888, BufferFormat::RGBA_F16, |
22 BufferFormat::RGBA_4444, | 22 BufferFormat::UYVY_422, BufferFormat::YUV_420_BIPLANAR, |
23 BufferFormat::RGBX_8888, | 23 BufferFormat::YVU_420}; |
24 BufferFormat::RGBA_8888, | |
25 BufferFormat::BGRX_8888, | |
26 BufferFormat::BGRA_8888, | |
27 BufferFormat::UYVY_422, | |
28 BufferFormat::YUV_420_BIPLANAR, | |
29 BufferFormat::YVU_420}; | |
30 | 24 |
31 static_assert(arraysize(kBufferFormats) == | 25 static_assert(arraysize(kBufferFormats) == |
32 (static_cast<int>(BufferFormat::LAST) + 1), | 26 (static_cast<int>(BufferFormat::LAST) + 1), |
33 "BufferFormat::LAST must be last value of kBufferFormats"); | 27 "BufferFormat::LAST must be last value of kBufferFormats"); |
34 | 28 |
35 | 29 |
36 bool RowSizeForBufferFormatChecked( | 30 bool RowSizeForBufferFormatChecked( |
37 size_t width, BufferFormat format, size_t plane, size_t* size_in_bytes) { | 31 size_t width, BufferFormat format, size_t plane, size_t* size_in_bytes) { |
38 base::CheckedNumeric<size_t> checked_size = width; | 32 base::CheckedNumeric<size_t> checked_size = width; |
39 switch (format) { | 33 switch (format) { |
(...skipping 27 matching lines...) Expand all Loading... |
67 return true; | 61 return true; |
68 case BufferFormat::BGRX_8888: | 62 case BufferFormat::BGRX_8888: |
69 case BufferFormat::RGBX_8888: | 63 case BufferFormat::RGBX_8888: |
70 case BufferFormat::RGBA_8888: | 64 case BufferFormat::RGBA_8888: |
71 case BufferFormat::BGRA_8888: | 65 case BufferFormat::BGRA_8888: |
72 checked_size *= 4; | 66 checked_size *= 4; |
73 if (!checked_size.IsValid()) | 67 if (!checked_size.IsValid()) |
74 return false; | 68 return false; |
75 *size_in_bytes = checked_size.ValueOrDie(); | 69 *size_in_bytes = checked_size.ValueOrDie(); |
76 return true; | 70 return true; |
| 71 case BufferFormat::RGBA_F16: |
| 72 checked_size *= 8; |
| 73 if (!checked_size.IsValid()) |
| 74 return false; |
| 75 *size_in_bytes = checked_size.ValueOrDie(); |
| 76 return true; |
77 case BufferFormat::YVU_420: | 77 case BufferFormat::YVU_420: |
78 DCHECK_EQ(0u, width % 2); | 78 DCHECK_EQ(0u, width % 2); |
79 *size_in_bytes = width / SubsamplingFactorForBufferFormat(format, plane); | 79 *size_in_bytes = width / SubsamplingFactorForBufferFormat(format, plane); |
80 return true; | 80 return true; |
81 case BufferFormat::YUV_420_BIPLANAR: | 81 case BufferFormat::YUV_420_BIPLANAR: |
82 DCHECK_EQ(width % 2, 0u); | 82 DCHECK_EQ(width % 2, 0u); |
83 *size_in_bytes = width; | 83 *size_in_bytes = width; |
84 return true; | 84 return true; |
85 } | 85 } |
86 NOTREACHED(); | 86 NOTREACHED(); |
(...skipping 15 matching lines...) Expand all Loading... |
102 case BufferFormat::DXT5: | 102 case BufferFormat::DXT5: |
103 case BufferFormat::ETC1: | 103 case BufferFormat::ETC1: |
104 case BufferFormat::R_8: | 104 case BufferFormat::R_8: |
105 case BufferFormat::RG_88: | 105 case BufferFormat::RG_88: |
106 case BufferFormat::BGR_565: | 106 case BufferFormat::BGR_565: |
107 case BufferFormat::RGBA_4444: | 107 case BufferFormat::RGBA_4444: |
108 case BufferFormat::RGBX_8888: | 108 case BufferFormat::RGBX_8888: |
109 case BufferFormat::RGBA_8888: | 109 case BufferFormat::RGBA_8888: |
110 case BufferFormat::BGRX_8888: | 110 case BufferFormat::BGRX_8888: |
111 case BufferFormat::BGRA_8888: | 111 case BufferFormat::BGRA_8888: |
| 112 case BufferFormat::RGBA_F16: |
112 case BufferFormat::UYVY_422: | 113 case BufferFormat::UYVY_422: |
113 return 1; | 114 return 1; |
114 case BufferFormat::YUV_420_BIPLANAR: | 115 case BufferFormat::YUV_420_BIPLANAR: |
115 return 2; | 116 return 2; |
116 case BufferFormat::YVU_420: | 117 case BufferFormat::YVU_420: |
117 return 3; | 118 return 3; |
118 } | 119 } |
119 NOTREACHED(); | 120 NOTREACHED(); |
120 return 0; | 121 return 0; |
121 } | 122 } |
122 | 123 |
123 size_t SubsamplingFactorForBufferFormat(BufferFormat format, size_t plane) { | 124 size_t SubsamplingFactorForBufferFormat(BufferFormat format, size_t plane) { |
124 switch (format) { | 125 switch (format) { |
125 case BufferFormat::ATC: | 126 case BufferFormat::ATC: |
126 case BufferFormat::ATCIA: | 127 case BufferFormat::ATCIA: |
127 case BufferFormat::DXT1: | 128 case BufferFormat::DXT1: |
128 case BufferFormat::DXT5: | 129 case BufferFormat::DXT5: |
129 case BufferFormat::ETC1: | 130 case BufferFormat::ETC1: |
130 case BufferFormat::R_8: | 131 case BufferFormat::R_8: |
131 case BufferFormat::RG_88: | 132 case BufferFormat::RG_88: |
132 case BufferFormat::BGR_565: | 133 case BufferFormat::BGR_565: |
133 case BufferFormat::RGBA_4444: | 134 case BufferFormat::RGBA_4444: |
134 case BufferFormat::RGBX_8888: | 135 case BufferFormat::RGBX_8888: |
135 case BufferFormat::RGBA_8888: | 136 case BufferFormat::RGBA_8888: |
136 case BufferFormat::BGRX_8888: | 137 case BufferFormat::BGRX_8888: |
137 case BufferFormat::BGRA_8888: | 138 case BufferFormat::BGRA_8888: |
| 139 case BufferFormat::RGBA_F16: |
138 case BufferFormat::UYVY_422: | 140 case BufferFormat::UYVY_422: |
139 return 1; | 141 return 1; |
140 case BufferFormat::YVU_420: { | 142 case BufferFormat::YVU_420: { |
141 static size_t factor[] = {1, 2, 2}; | 143 static size_t factor[] = {1, 2, 2}; |
142 DCHECK_LT(static_cast<size_t>(plane), arraysize(factor)); | 144 DCHECK_LT(static_cast<size_t>(plane), arraysize(factor)); |
143 return factor[plane]; | 145 return factor[plane]; |
144 } | 146 } |
145 case BufferFormat::YUV_420_BIPLANAR: { | 147 case BufferFormat::YUV_420_BIPLANAR: { |
146 static size_t factor[] = {1, 2}; | 148 static size_t factor[] = {1, 2}; |
147 DCHECK_LT(static_cast<size_t>(plane), arraysize(factor)); | 149 DCHECK_LT(static_cast<size_t>(plane), arraysize(factor)); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 case BufferFormat::DXT5: | 201 case BufferFormat::DXT5: |
200 case BufferFormat::ETC1: | 202 case BufferFormat::ETC1: |
201 case BufferFormat::R_8: | 203 case BufferFormat::R_8: |
202 case BufferFormat::RG_88: | 204 case BufferFormat::RG_88: |
203 case BufferFormat::BGR_565: | 205 case BufferFormat::BGR_565: |
204 case BufferFormat::RGBA_4444: | 206 case BufferFormat::RGBA_4444: |
205 case BufferFormat::RGBX_8888: | 207 case BufferFormat::RGBX_8888: |
206 case BufferFormat::RGBA_8888: | 208 case BufferFormat::RGBA_8888: |
207 case BufferFormat::BGRX_8888: | 209 case BufferFormat::BGRX_8888: |
208 case BufferFormat::BGRA_8888: | 210 case BufferFormat::BGRA_8888: |
| 211 case BufferFormat::RGBA_F16: |
209 case BufferFormat::UYVY_422: | 212 case BufferFormat::UYVY_422: |
210 return 0; | 213 return 0; |
211 case BufferFormat::YVU_420: { | 214 case BufferFormat::YVU_420: { |
212 static size_t offset_in_2x2_sub_sampling_sizes[] = {0, 4, 5}; | 215 static size_t offset_in_2x2_sub_sampling_sizes[] = {0, 4, 5}; |
213 DCHECK_LT(plane, arraysize(offset_in_2x2_sub_sampling_sizes)); | 216 DCHECK_LT(plane, arraysize(offset_in_2x2_sub_sampling_sizes)); |
214 return offset_in_2x2_sub_sampling_sizes[plane] * | 217 return offset_in_2x2_sub_sampling_sizes[plane] * |
215 (size.width() / 2 + size.height() / 2); | 218 (size.width() / 2 + size.height() / 2); |
216 } | 219 } |
217 case gfx::BufferFormat::YUV_420_BIPLANAR: { | 220 case gfx::BufferFormat::YUV_420_BIPLANAR: { |
218 static size_t offset_in_2x2_sub_sampling_sizes[] = {0, 4}; | 221 static size_t offset_in_2x2_sub_sampling_sizes[] = {0, 4}; |
219 DCHECK_LT(plane, arraysize(offset_in_2x2_sub_sampling_sizes)); | 222 DCHECK_LT(plane, arraysize(offset_in_2x2_sub_sampling_sizes)); |
220 return offset_in_2x2_sub_sampling_sizes[plane] * | 223 return offset_in_2x2_sub_sampling_sizes[plane] * |
221 (size.width() / 2 + size.height() / 2); | 224 (size.width() / 2 + size.height() / 2); |
222 } | 225 } |
223 } | 226 } |
224 NOTREACHED(); | 227 NOTREACHED(); |
225 return 0; | 228 return 0; |
226 } | 229 } |
227 | 230 |
228 } // namespace gfx | 231 } // namespace gfx |
OLD | NEW |