Add a virtual beamforming audio device on ChromeOS.
[chromium-blink-merge.git] / ui / gfx / ipc / gfx_param_traits.cc
blob3013b0c8cc9b543fbfc0d6291e4bbf4b49cef6ef
1 // Copyright 2014 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/ipc/gfx_param_traits.h"
7 #include <string>
9 #include "third_party/skia/include/core/SkBitmap.h"
10 #include "ui/gfx/geometry/point3_f.h"
11 #include "ui/gfx/geometry/rect.h"
12 #include "ui/gfx/geometry/rect_f.h"
13 #include "ui/gfx/range/range.h"
15 namespace {
17 struct SkBitmap_Data {
18 // The color type for the bitmap (bits per pixel, etc).
19 SkColorType fColorType;
21 // The alpha type for the bitmap (opaque, premul, unpremul).
22 SkAlphaType fAlphaType;
24 // The width of the bitmap in pixels.
25 uint32 fWidth;
27 // The height of the bitmap in pixels.
28 uint32 fHeight;
30 void InitSkBitmapDataForTransfer(const SkBitmap& bitmap) {
31 const SkImageInfo& info = bitmap.info();
32 fColorType = info.colorType();
33 fAlphaType = info.alphaType();
34 fWidth = info.width();
35 fHeight = info.height();
38 // Returns whether |bitmap| successfully initialized.
39 bool InitSkBitmapFromData(SkBitmap* bitmap,
40 const char* pixels,
41 size_t pixels_size) const {
42 if (!bitmap->tryAllocPixels(
43 SkImageInfo::Make(fWidth, fHeight, fColorType, fAlphaType)))
44 return false;
45 if (pixels_size != bitmap->getSize())
46 return false;
47 memcpy(bitmap->getPixels(), pixels, pixels_size);
48 return true;
52 } // namespace
54 namespace IPC {
56 void ParamTraits<gfx::Point>::Write(Message* m, const gfx::Point& p) {
57 WriteParam(m, p.x());
58 WriteParam(m, p.y());
61 bool ParamTraits<gfx::Point>::Read(const Message* m,
62 base::PickleIterator* iter,
63 gfx::Point* r) {
64 int x, y;
65 if (!ReadParam(m, iter, &x) || !ReadParam(m, iter, &y))
66 return false;
67 r->set_x(x);
68 r->set_y(y);
69 return true;
72 void ParamTraits<gfx::Point>::Log(const gfx::Point& p, std::string* l) {
73 l->append(base::StringPrintf("(%d, %d)", p.x(), p.y()));
76 void ParamTraits<gfx::PointF>::Write(Message* m, const gfx::PointF& p) {
77 WriteParam(m, p.x());
78 WriteParam(m, p.y());
81 bool ParamTraits<gfx::PointF>::Read(const Message* m,
82 base::PickleIterator* iter,
83 gfx::PointF* r) {
84 float x, y;
85 if (!ReadParam(m, iter, &x) || !ReadParam(m, iter, &y))
86 return false;
87 r->set_x(x);
88 r->set_y(y);
89 return true;
92 void ParamTraits<gfx::PointF>::Log(const gfx::PointF& p, std::string* l) {
93 l->append(base::StringPrintf("(%f, %f)", p.x(), p.y()));
96 void ParamTraits<gfx::Point3F>::Write(Message* m, const gfx::Point3F& p) {
97 WriteParam(m, p.x());
98 WriteParam(m, p.y());
99 WriteParam(m, p.z());
102 bool ParamTraits<gfx::Point3F>::Read(const Message* m,
103 base::PickleIterator* iter,
104 gfx::Point3F* r) {
105 float x, y, z;
106 if (!ReadParam(m, iter, &x) || !ReadParam(m, iter, &y) ||
107 !ReadParam(m, iter, &z))
108 return false;
109 r->set_x(x);
110 r->set_y(y);
111 r->set_z(z);
112 return true;
115 void ParamTraits<gfx::Point3F>::Log(const gfx::Point3F& p, std::string* l) {
116 l->append(base::StringPrintf("(%f, %f, %f)", p.x(), p.y(), p.z()));
119 void ParamTraits<gfx::Size>::Write(Message* m, const gfx::Size& p) {
120 DCHECK_GE(p.width(), 0);
121 DCHECK_GE(p.height(), 0);
122 int values[2] = { p.width(), p.height() };
123 m->WriteBytes(&values, sizeof(int) * 2);
126 bool ParamTraits<gfx::Size>::Read(const Message* m,
127 base::PickleIterator* iter,
128 gfx::Size* r) {
129 const char* char_values;
130 if (!iter->ReadBytes(&char_values, sizeof(int) * 2))
131 return false;
132 const int* values = reinterpret_cast<const int*>(char_values);
133 if (values[0] < 0 || values[1] < 0)
134 return false;
135 r->set_width(values[0]);
136 r->set_height(values[1]);
137 return true;
140 void ParamTraits<gfx::Size>::Log(const gfx::Size& p, std::string* l) {
141 l->append(base::StringPrintf("(%d, %d)", p.width(), p.height()));
144 void ParamTraits<gfx::SizeF>::Write(Message* m, const gfx::SizeF& p) {
145 float values[2] = { p.width(), p.height() };
146 m->WriteBytes(&values, sizeof(float) * 2);
149 bool ParamTraits<gfx::SizeF>::Read(const Message* m,
150 base::PickleIterator* iter,
151 gfx::SizeF* r) {
152 const char* char_values;
153 if (!iter->ReadBytes(&char_values, sizeof(float) * 2))
154 return false;
155 const float* values = reinterpret_cast<const float*>(char_values);
156 r->set_width(values[0]);
157 r->set_height(values[1]);
158 return true;
161 void ParamTraits<gfx::SizeF>::Log(const gfx::SizeF& p, std::string* l) {
162 l->append(base::StringPrintf("(%f, %f)", p.width(), p.height()));
165 void ParamTraits<gfx::Vector2d>::Write(Message* m, const gfx::Vector2d& p) {
166 int values[2] = { p.x(), p.y() };
167 m->WriteBytes(&values, sizeof(int) * 2);
170 bool ParamTraits<gfx::Vector2d>::Read(const Message* m,
171 base::PickleIterator* iter,
172 gfx::Vector2d* r) {
173 const char* char_values;
174 if (!iter->ReadBytes(&char_values, sizeof(int) * 2))
175 return false;
176 const int* values = reinterpret_cast<const int*>(char_values);
177 r->set_x(values[0]);
178 r->set_y(values[1]);
179 return true;
182 void ParamTraits<gfx::Vector2d>::Log(const gfx::Vector2d& v, std::string* l) {
183 l->append(base::StringPrintf("(%d, %d)", v.x(), v.y()));
186 void ParamTraits<gfx::Vector2dF>::Write(Message* m, const gfx::Vector2dF& p) {
187 float values[2] = { p.x(), p.y() };
188 m->WriteBytes(&values, sizeof(float) * 2);
191 bool ParamTraits<gfx::Vector2dF>::Read(const Message* m,
192 base::PickleIterator* iter,
193 gfx::Vector2dF* r) {
194 const char* char_values;
195 if (!iter->ReadBytes(&char_values, sizeof(float) * 2))
196 return false;
197 const float* values = reinterpret_cast<const float*>(char_values);
198 r->set_x(values[0]);
199 r->set_y(values[1]);
200 return true;
203 void ParamTraits<gfx::Vector2dF>::Log(const gfx::Vector2dF& v, std::string* l) {
204 l->append(base::StringPrintf("(%f, %f)", v.x(), v.y()));
207 void ParamTraits<gfx::Rect>::Write(Message* m, const gfx::Rect& p) {
208 int values[4] = { p.x(), p.y(), p.width(), p.height() };
209 m->WriteBytes(&values, sizeof(int) * 4);
212 bool ParamTraits<gfx::Rect>::Read(const Message* m,
213 base::PickleIterator* iter,
214 gfx::Rect* r) {
215 const char* char_values;
216 if (!iter->ReadBytes(&char_values, sizeof(int) * 4))
217 return false;
218 const int* values = reinterpret_cast<const int*>(char_values);
219 if (values[2] < 0 || values[3] < 0)
220 return false;
221 r->SetRect(values[0], values[1], values[2], values[3]);
222 return true;
225 void ParamTraits<gfx::Rect>::Log(const gfx::Rect& p, std::string* l) {
226 l->append(base::StringPrintf("(%d, %d, %d, %d)", p.x(), p.y(),
227 p.width(), p.height()));
230 void ParamTraits<gfx::RectF>::Write(Message* m, const gfx::RectF& p) {
231 float values[4] = { p.x(), p.y(), p.width(), p.height() };
232 m->WriteBytes(&values, sizeof(float) * 4);
235 bool ParamTraits<gfx::RectF>::Read(const Message* m,
236 base::PickleIterator* iter,
237 gfx::RectF* r) {
238 const char* char_values;
239 if (!iter->ReadBytes(&char_values, sizeof(float) * 4))
240 return false;
241 const float* values = reinterpret_cast<const float*>(char_values);
242 r->SetRect(values[0], values[1], values[2], values[3]);
243 return true;
246 void ParamTraits<gfx::RectF>::Log(const gfx::RectF& p, std::string* l) {
247 l->append(base::StringPrintf("(%f, %f, %f, %f)", p.x(), p.y(),
248 p.width(), p.height()));
251 void ParamTraits<SkBitmap>::Write(Message* m, const SkBitmap& p) {
252 size_t fixed_size = sizeof(SkBitmap_Data);
253 SkBitmap_Data bmp_data;
254 bmp_data.InitSkBitmapDataForTransfer(p);
255 m->WriteData(reinterpret_cast<const char*>(&bmp_data),
256 static_cast<int>(fixed_size));
257 size_t pixel_size = p.getSize();
258 SkAutoLockPixels p_lock(p);
259 m->WriteData(reinterpret_cast<const char*>(p.getPixels()),
260 static_cast<int>(pixel_size));
263 bool ParamTraits<SkBitmap>::Read(const Message* m,
264 base::PickleIterator* iter,
265 SkBitmap* r) {
266 const char* fixed_data;
267 int fixed_data_size = 0;
268 if (!iter->ReadData(&fixed_data, &fixed_data_size) ||
269 (fixed_data_size <= 0)) {
270 NOTREACHED();
271 return false;
273 if (fixed_data_size != sizeof(SkBitmap_Data))
274 return false; // Message is malformed.
276 const char* variable_data;
277 int variable_data_size = 0;
278 if (!iter->ReadData(&variable_data, &variable_data_size) ||
279 (variable_data_size < 0)) {
280 NOTREACHED();
281 return false;
283 const SkBitmap_Data* bmp_data =
284 reinterpret_cast<const SkBitmap_Data*>(fixed_data);
285 return bmp_data->InitSkBitmapFromData(r, variable_data, variable_data_size);
288 void ParamTraits<SkBitmap>::Log(const SkBitmap& p, std::string* l) {
289 l->append("<SkBitmap>");
292 void ParamTraits<gfx::Range>::Write(Message* m, const gfx::Range& r) {
293 m->WriteSizeT(r.start());
294 m->WriteSizeT(r.end());
297 bool ParamTraits<gfx::Range>::Read(const Message* m,
298 base::PickleIterator* iter,
299 gfx::Range* r) {
300 size_t start, end;
301 if (!iter->ReadSizeT(&start) || !iter->ReadSizeT(&end))
302 return false;
303 r->set_start(start);
304 r->set_end(end);
305 return true;
308 void ParamTraits<gfx::Range>::Log(const gfx::Range& r, std::string* l) {
309 l->append(base::StringPrintf("(%" PRIuS ", %" PRIuS ")", r.start(), r.end()));
312 } // namespace IPC
314 // Generate param traits write methods.
315 #include "ipc/param_traits_write_macros.h"
316 namespace IPC {
317 #undef UI_GFX_IPC_GFX_PARAM_TRAITS_MACROS_H_
318 #include "ui/gfx/ipc/gfx_param_traits_macros.h"
319 } // namespace IPC
321 // Generate param traits read methods.
322 #include "ipc/param_traits_read_macros.h"
323 namespace IPC {
324 #undef UI_GFX_IPC_GFX_PARAM_TRAITS_MACROS_H_
325 #include "ui/gfx/ipc/gfx_param_traits_macros.h"
326 } // namespace IPC
328 // Generate param traits log methods.
329 #include "ipc/param_traits_log_macros.h"
330 namespace IPC {
331 #undef UI_GFX_IPC_GFX_PARAM_TRAITS_MACROS_H_
332 #include "ui/gfx/ipc/gfx_param_traits_macros.h"
333 } // namespace IPC