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

Side by Side Diff: ui/gfx/buffer_format_util.cc

Issue 1412223009: Reland: GpuMemoryBuffer interface change: Map(**) to Map() and memory(plane); stride(plane) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Win X64 compile fix Created 5 years, 1 month 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
« no previous file with comments | « ui/gfx/buffer_format_util.h ('k') | ui/gfx/gpu_memory_buffer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/numerics/safe_math.h" 8 #include "base/numerics/safe_math.h"
9 9
10 namespace gfx { 10 namespace gfx {
11 namespace { 11 namespace {
12 12
13 const BufferFormat kBufferFormats[] = { 13 const BufferFormat kBufferFormats[] = {
14 BufferFormat::ATC, BufferFormat::ATCIA, 14 BufferFormat::ATC, BufferFormat::ATCIA,
15 BufferFormat::DXT1, BufferFormat::DXT5, 15 BufferFormat::DXT1, BufferFormat::DXT5,
16 BufferFormat::ETC1, BufferFormat::R_8, 16 BufferFormat::ETC1, BufferFormat::R_8,
17 BufferFormat::RGBA_4444, BufferFormat::RGBA_8888, 17 BufferFormat::RGBA_4444, BufferFormat::RGBA_8888,
18 BufferFormat::BGRX_8888, BufferFormat::BGRA_8888, 18 BufferFormat::BGRX_8888, BufferFormat::BGRA_8888,
19 BufferFormat::UYVY_422, BufferFormat::YUV_420_BIPLANAR, 19 BufferFormat::UYVY_422, BufferFormat::YUV_420_BIPLANAR,
20 BufferFormat::YUV_420}; 20 BufferFormat::YUV_420};
21 21
22 static_assert(arraysize(kBufferFormats) == 22 static_assert(arraysize(kBufferFormats) ==
23 (static_cast<int>(BufferFormat::LAST) + 1), 23 (static_cast<int>(BufferFormat::LAST) + 1),
24 "BufferFormat::LAST must be last value of kBufferFormats"); 24 "BufferFormat::LAST must be last value of kBufferFormats");
25 25
26
27 bool RowSizeForBufferFormatChecked(
28 size_t width, BufferFormat format, int plane, size_t* size_in_bytes) {
29 base::CheckedNumeric<size_t> checked_size = width;
30 switch (format) {
31 case BufferFormat::ATCIA:
32 case BufferFormat::DXT5:
33 DCHECK_EQ(0, plane);
34 *size_in_bytes = width;
35 return true;
36 case BufferFormat::ATC:
37 case BufferFormat::DXT1:
38 case BufferFormat::ETC1:
39 DCHECK_EQ(0, plane);
40 DCHECK_EQ(0u, width % 2);
41 *size_in_bytes = width / 2;
42 return true;
43 case BufferFormat::R_8:
44 checked_size += 3;
45 if (!checked_size.IsValid())
46 return false;
47 *size_in_bytes = checked_size.ValueOrDie() & ~0x3;
48 return true;
49 case BufferFormat::RGBA_4444:
50 case BufferFormat::UYVY_422:
51 checked_size *= 2;
52 if (!checked_size.IsValid())
53 return false;
54 *size_in_bytes = checked_size.ValueOrDie();
55 return true;
56 case BufferFormat::BGRX_8888:
57 case BufferFormat::RGBA_8888:
58 case BufferFormat::BGRA_8888:
59 checked_size *= 4;
60 if (!checked_size.IsValid())
61 return false;
62 *size_in_bytes = checked_size.ValueOrDie();
63 return true;
64 case BufferFormat::YUV_420:
65 DCHECK_EQ(0u, width % 2);
66 *size_in_bytes = width / SubsamplingFactorForBufferFormat(format, plane);
67 return true;
68 case BufferFormat::YUV_420_BIPLANAR:
69 DCHECK_EQ(width % 2, 0u);
70 *size_in_bytes = width;
71 return true;
72 }
73 NOTREACHED();
74 return false;
75 }
76
26 } // namespace 77 } // namespace
27 78
28 std::vector<BufferFormat> GetBufferFormats() { 79 std::vector<BufferFormat> GetBufferFormatsForTesting() {
29 return std::vector<BufferFormat>(kBufferFormats, 80 return std::vector<BufferFormat>(kBufferFormats,
30 kBufferFormats + arraysize(kBufferFormats)); 81 kBufferFormats + arraysize(kBufferFormats));
31 } 82 }
32 83
33 size_t NumberOfPlanesForBufferFormat(BufferFormat format) { 84 size_t NumberOfPlanesForBufferFormat(BufferFormat format) {
34 switch (format) { 85 switch (format) {
35 case BufferFormat::ATC: 86 case BufferFormat::ATC:
36 case BufferFormat::ATCIA: 87 case BufferFormat::ATCIA:
37 case BufferFormat::DXT1: 88 case BufferFormat::DXT1:
38 case BufferFormat::DXT5: 89 case BufferFormat::DXT5:
(...skipping 26 matching lines...) Expand all
65 case BufferFormat::RGBA_8888: 116 case BufferFormat::RGBA_8888:
66 case BufferFormat::BGRX_8888: 117 case BufferFormat::BGRX_8888:
67 case BufferFormat::BGRA_8888: 118 case BufferFormat::BGRA_8888:
68 case BufferFormat::UYVY_422: 119 case BufferFormat::UYVY_422:
69 return 1; 120 return 1;
70 case BufferFormat::YUV_420: { 121 case BufferFormat::YUV_420: {
71 static size_t factor[] = {1, 2, 2}; 122 static size_t factor[] = {1, 2, 2};
72 DCHECK_LT(static_cast<size_t>(plane), arraysize(factor)); 123 DCHECK_LT(static_cast<size_t>(plane), arraysize(factor));
73 return factor[plane]; 124 return factor[plane];
74 } 125 }
75 case gfx::BufferFormat::YUV_420_BIPLANAR: { 126 case BufferFormat::YUV_420_BIPLANAR: {
76 static size_t factor[] = {1, 2}; 127 static size_t factor[] = {1, 2};
77 DCHECK_LT(static_cast<size_t>(plane), arraysize(factor)); 128 DCHECK_LT(static_cast<size_t>(plane), arraysize(factor));
78 return factor[plane]; 129 return factor[plane];
79 } 130 }
80 } 131 }
81 NOTREACHED(); 132 NOTREACHED();
82 return 0; 133 return 0;
83 } 134 }
84 135
85 size_t RowSizeForBufferFormat(size_t width, BufferFormat format, int plane) { 136 size_t RowSizeForBufferFormat(size_t width, BufferFormat format, int plane) {
86 size_t row_size = 0; 137 size_t row_size = 0;
87 bool valid = RowSizeForBufferFormatChecked(width, format, plane, &row_size); 138 bool valid = RowSizeForBufferFormatChecked(width, format, plane, &row_size);
88 DCHECK(valid); 139 DCHECK(valid);
89 return row_size; 140 return row_size;
90 } 141 }
91 142
92 bool RowSizeForBufferFormatChecked( 143 size_t BufferSizeForBufferFormat(const Size& size, BufferFormat format) {
93 size_t width, BufferFormat format, int plane, size_t* size_in_bytes) {
94 base::CheckedNumeric<size_t> checked_size = width;
95 switch (format) {
96 case BufferFormat::ATCIA:
97 case BufferFormat::DXT5:
98 DCHECK_EQ(0, plane);
99 *size_in_bytes = width;
100 return true;
101 case BufferFormat::ATC:
102 case BufferFormat::DXT1:
103 case BufferFormat::ETC1:
104 DCHECK_EQ(0, plane);
105 DCHECK_EQ(0u, width % 2);
106 *size_in_bytes = width / 2;
107 return true;
108 case BufferFormat::R_8:
109 checked_size += 3;
110 if (!checked_size.IsValid())
111 return false;
112 *size_in_bytes = checked_size.ValueOrDie() & ~0x3;
113 return true;
114 case BufferFormat::RGBA_4444:
115 case BufferFormat::UYVY_422:
116 checked_size *= 2;
117 if (!checked_size.IsValid())
118 return false;
119 *size_in_bytes = checked_size.ValueOrDie();
120 return true;
121 case BufferFormat::BGRX_8888:
122 case BufferFormat::RGBA_8888:
123 case BufferFormat::BGRA_8888:
124 checked_size *= 4;
125 if (!checked_size.IsValid())
126 return false;
127 *size_in_bytes = checked_size.ValueOrDie();
128 return true;
129 case BufferFormat::YUV_420:
130 DCHECK_EQ(0u, width % 2);
131 *size_in_bytes = width / SubsamplingFactorForBufferFormat(format, plane);
132 return true;
133 case gfx::BufferFormat::YUV_420_BIPLANAR:
134 DCHECK_EQ(width % 2, 0u);
135 *size_in_bytes = width;
136 return true;
137 }
138 NOTREACHED();
139 return false;
140 }
141
142 size_t BufferSizeForBufferFormat(
143 const Size& size, BufferFormat format) {
144 size_t buffer_size = 0; 144 size_t buffer_size = 0;
145 bool valid = BufferSizeForBufferFormatChecked(size, format, &buffer_size); 145 bool valid = BufferSizeForBufferFormatChecked(size, format, &buffer_size);
146 DCHECK(valid); 146 DCHECK(valid);
147 return buffer_size; 147 return buffer_size;
148 } 148 }
149 149
150 bool BufferSizeForBufferFormatChecked( 150 bool BufferSizeForBufferFormatChecked(const Size& size,
151 const Size& size, BufferFormat format, size_t* size_in_bytes) { 151 BufferFormat format,
152 size_t* size_in_bytes) {
152 base::CheckedNumeric<size_t> checked_size = 0; 153 base::CheckedNumeric<size_t> checked_size = 0;
153 size_t num_planes = NumberOfPlanesForBufferFormat(format); 154 size_t num_planes = NumberOfPlanesForBufferFormat(format);
154 for (size_t i = 0; i < num_planes; ++i) { 155 for (size_t i = 0; i < num_planes; ++i) {
155 size_t row_size = 0; 156 size_t row_size = 0;
156 if (!RowSizeForBufferFormatChecked(size.width(), format, i, &row_size)) 157 if (!RowSizeForBufferFormatChecked(size.width(), format, i, &row_size))
157 return false; 158 return false;
158 base::CheckedNumeric<size_t> checked_plane_size = row_size; 159 base::CheckedNumeric<size_t> checked_plane_size = row_size;
159 checked_plane_size *= size.height() / 160 checked_plane_size *= size.height() /
160 SubsamplingFactorForBufferFormat(format, i); 161 SubsamplingFactorForBufferFormat(format, i);
161 if (!checked_plane_size.IsValid()) 162 if (!checked_plane_size.IsValid())
162 return false; 163 return false;
163 checked_size += checked_plane_size.ValueOrDie(); 164 checked_size += checked_plane_size.ValueOrDie();
164 if (!checked_size.IsValid()) 165 if (!checked_size.IsValid())
165 return false; 166 return false;
166 } 167 }
167 *size_in_bytes = checked_size.ValueOrDie(); 168 *size_in_bytes = checked_size.ValueOrDie();
168 return true; 169 return true;
169 } 170 }
170 171
172 int BufferOffsetForBufferFormat(const Size& size,
173 BufferFormat format,
174 size_t plane) {
175 DCHECK_LT(plane, gfx::NumberOfPlanesForBufferFormat(format));
176 switch (format) {
177 case BufferFormat::ATC:
178 case BufferFormat::ATCIA:
179 case BufferFormat::DXT1:
180 case BufferFormat::DXT5:
181 case BufferFormat::ETC1:
182 case BufferFormat::R_8:
183 case BufferFormat::RGBA_4444:
184 case BufferFormat::RGBA_8888:
185 case BufferFormat::BGRX_8888:
186 case BufferFormat::BGRA_8888:
187 case BufferFormat::UYVY_422:
188 return 0;
189 case BufferFormat::YUV_420: {
190 static size_t offset_in_2x2_sub_sampling_sizes[] = {0, 4, 5};
191 DCHECK_LT(plane, arraysize(offset_in_2x2_sub_sampling_sizes));
192 return offset_in_2x2_sub_sampling_sizes[plane] *
193 (size.width() / 2 + size.height() / 2);
194 }
195 case gfx::BufferFormat::YUV_420_BIPLANAR: {
196 static size_t offset_in_2x2_sub_sampling_sizes[] = {0, 4};
197 DCHECK_LT(plane, arraysize(offset_in_2x2_sub_sampling_sizes));
198 return offset_in_2x2_sub_sampling_sizes[plane] *
199 (size.width() / 2 + size.height() / 2);
200 }
201 }
202 NOTREACHED();
203 return 0;
204 }
205
171 } // namespace gfx 206 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/buffer_format_util.h ('k') | ui/gfx/gpu_memory_buffer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698