cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / ui / gfx / ipc / gfx_param_traits.cc
blob78c024f234e31ba6fd5cfeaad0dc12f0c2857c45
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/rect.h"
11 #include "ui/gfx/geometry/rect_f.h"
12 #include "ui/gfx/range/range.h"
14 namespace {
16 struct SkBitmap_Data {
17 // The color type for the bitmap (bits per pixel, etc).
18 SkColorType fColorType;
20 // The alpha type for the bitmap (opaque, premul, unpremul).
21 SkAlphaType fAlphaType;
23 // The width of the bitmap in pixels.
24 uint32 fWidth;
26 // The height of the bitmap in pixels.
27 uint32 fHeight;
29 void InitSkBitmapDataForTransfer(const SkBitmap& bitmap) {
30 const SkImageInfo& info = bitmap.info();
31 fColorType = info.colorType();
32 fAlphaType = info.alphaType();
33 fWidth = info.width();
34 fHeight = info.height();
37 // Returns whether |bitmap| successfully initialized.
38 bool InitSkBitmapFromData(SkBitmap* bitmap,
39 const char* pixels,
40 size_t pixels_size) const {
41 if (!bitmap->tryAllocPixels(
42 SkImageInfo::Make(fWidth, fHeight, fColorType, fAlphaType)))
43 return false;
44 if (pixels_size != bitmap->getSize())
45 return false;
46 memcpy(bitmap->getPixels(), pixels, pixels_size);
47 return true;
51 } // namespace
53 namespace IPC {
55 void ParamTraits<gfx::Point>::Write(Message* m, const gfx::Point& p) {
56 m->WriteInt(p.x());
57 m->WriteInt(p.y());
60 bool ParamTraits<gfx::Point>::Read(const Message* m,
61 base::PickleIterator* iter,
62 gfx::Point* r) {
63 int x, y;
64 if (!iter->ReadInt(&x) ||
65 !iter->ReadInt(&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& v) {
77 ParamTraits<float>::Write(m, v.x());
78 ParamTraits<float>::Write(m, v.y());
81 bool ParamTraits<gfx::PointF>::Read(const Message* m,
82 base::PickleIterator* iter,
83 gfx::PointF* r) {
84 float x, y;
85 if (!ParamTraits<float>::Read(m, iter, &x) ||
86 !ParamTraits<float>::Read(m, iter, &y))
87 return false;
88 r->set_x(x);
89 r->set_y(y);
90 return true;
93 void ParamTraits<gfx::PointF>::Log(const gfx::PointF& v, std::string* l) {
94 l->append(base::StringPrintf("(%f, %f)", v.x(), v.y()));
97 void ParamTraits<gfx::Size>::Write(Message* m, const gfx::Size& p) {
98 DCHECK_GE(p.width(), 0);
99 DCHECK_GE(p.height(), 0);
100 int values[2] = { p.width(), p.height() };
101 m->WriteBytes(&values, sizeof(int) * 2);
104 bool ParamTraits<gfx::Size>::Read(const Message* m,
105 base::PickleIterator* iter,
106 gfx::Size* r) {
107 const char* char_values;
108 if (!iter->ReadBytes(&char_values, sizeof(int) * 2))
109 return false;
110 const int* values = reinterpret_cast<const int*>(char_values);
111 if (values[0] < 0 || values[1] < 0)
112 return false;
113 r->set_width(values[0]);
114 r->set_height(values[1]);
115 return true;
118 void ParamTraits<gfx::Size>::Log(const gfx::Size& p, std::string* l) {
119 l->append(base::StringPrintf("(%d, %d)", p.width(), p.height()));
122 void ParamTraits<gfx::SizeF>::Write(Message* m, const gfx::SizeF& p) {
123 float values[2] = { p.width(), p.height() };
124 m->WriteBytes(&values, sizeof(float) * 2);
127 bool ParamTraits<gfx::SizeF>::Read(const Message* m,
128 base::PickleIterator* iter,
129 gfx::SizeF* r) {
130 const char* char_values;
131 if (!iter->ReadBytes(&char_values, sizeof(float) * 2))
132 return false;
133 const float* values = reinterpret_cast<const float*>(char_values);
134 r->set_width(values[0]);
135 r->set_height(values[1]);
136 return true;
139 void ParamTraits<gfx::SizeF>::Log(const gfx::SizeF& p, std::string* l) {
140 l->append(base::StringPrintf("(%f, %f)", p.width(), p.height()));
143 void ParamTraits<gfx::Vector2d>::Write(Message* m, const gfx::Vector2d& p) {
144 int values[2] = { p.x(), p.y() };
145 m->WriteBytes(&values, sizeof(int) * 2);
148 bool ParamTraits<gfx::Vector2d>::Read(const Message* m,
149 base::PickleIterator* iter,
150 gfx::Vector2d* r) {
151 const char* char_values;
152 if (!iter->ReadBytes(&char_values, sizeof(int) * 2))
153 return false;
154 const int* values = reinterpret_cast<const int*>(char_values);
155 r->set_x(values[0]);
156 r->set_y(values[1]);
157 return true;
160 void ParamTraits<gfx::Vector2d>::Log(const gfx::Vector2d& v, std::string* l) {
161 l->append(base::StringPrintf("(%d, %d)", v.x(), v.y()));
164 void ParamTraits<gfx::Vector2dF>::Write(Message* m, const gfx::Vector2dF& p) {
165 float values[2] = { p.x(), p.y() };
166 m->WriteBytes(&values, sizeof(float) * 2);
169 bool ParamTraits<gfx::Vector2dF>::Read(const Message* m,
170 base::PickleIterator* iter,
171 gfx::Vector2dF* r) {
172 const char* char_values;
173 if (!iter->ReadBytes(&char_values, sizeof(float) * 2))
174 return false;
175 const float* values = reinterpret_cast<const float*>(char_values);
176 r->set_x(values[0]);
177 r->set_y(values[1]);
178 return true;
181 void ParamTraits<gfx::Vector2dF>::Log(const gfx::Vector2dF& v, std::string* l) {
182 l->append(base::StringPrintf("(%f, %f)", v.x(), v.y()));
185 void ParamTraits<gfx::Rect>::Write(Message* m, const gfx::Rect& p) {
186 int values[4] = { p.x(), p.y(), p.width(), p.height() };
187 m->WriteBytes(&values, sizeof(int) * 4);
190 bool ParamTraits<gfx::Rect>::Read(const Message* m,
191 base::PickleIterator* iter,
192 gfx::Rect* r) {
193 const char* char_values;
194 if (!iter->ReadBytes(&char_values, sizeof(int) * 4))
195 return false;
196 const int* values = reinterpret_cast<const int*>(char_values);
197 if (values[2] < 0 || values[3] < 0)
198 return false;
199 r->SetRect(values[0], values[1], values[2], values[3]);
200 return true;
203 void ParamTraits<gfx::Rect>::Log(const gfx::Rect& p, std::string* l) {
204 l->append(base::StringPrintf("(%d, %d, %d, %d)", p.x(), p.y(),
205 p.width(), p.height()));
208 void ParamTraits<gfx::RectF>::Write(Message* m, const gfx::RectF& p) {
209 float values[4] = { p.x(), p.y(), p.width(), p.height() };
210 m->WriteBytes(&values, sizeof(float) * 4);
213 bool ParamTraits<gfx::RectF>::Read(const Message* m,
214 base::PickleIterator* iter,
215 gfx::RectF* r) {
216 const char* char_values;
217 if (!iter->ReadBytes(&char_values, sizeof(float) * 4))
218 return false;
219 const float* values = reinterpret_cast<const float*>(char_values);
220 r->SetRect(values[0], values[1], values[2], values[3]);
221 return true;
224 void ParamTraits<gfx::RectF>::Log(const gfx::RectF& p, std::string* l) {
225 l->append(base::StringPrintf("(%f, %f, %f, %f)", p.x(), p.y(),
226 p.width(), p.height()));
229 void ParamTraits<SkBitmap>::Write(Message* m, const SkBitmap& p) {
230 size_t fixed_size = sizeof(SkBitmap_Data);
231 SkBitmap_Data bmp_data;
232 bmp_data.InitSkBitmapDataForTransfer(p);
233 m->WriteData(reinterpret_cast<const char*>(&bmp_data),
234 static_cast<int>(fixed_size));
235 size_t pixel_size = p.getSize();
236 SkAutoLockPixels p_lock(p);
237 m->WriteData(reinterpret_cast<const char*>(p.getPixels()),
238 static_cast<int>(pixel_size));
241 bool ParamTraits<SkBitmap>::Read(const Message* m,
242 base::PickleIterator* iter,
243 SkBitmap* r) {
244 const char* fixed_data;
245 int fixed_data_size = 0;
246 if (!iter->ReadData(&fixed_data, &fixed_data_size) ||
247 (fixed_data_size <= 0)) {
248 NOTREACHED();
249 return false;
251 if (fixed_data_size != sizeof(SkBitmap_Data))
252 return false; // Message is malformed.
254 const char* variable_data;
255 int variable_data_size = 0;
256 if (!iter->ReadData(&variable_data, &variable_data_size) ||
257 (variable_data_size < 0)) {
258 NOTREACHED();
259 return false;
261 const SkBitmap_Data* bmp_data =
262 reinterpret_cast<const SkBitmap_Data*>(fixed_data);
263 return bmp_data->InitSkBitmapFromData(r, variable_data, variable_data_size);
266 void ParamTraits<SkBitmap>::Log(const SkBitmap& p, std::string* l) {
267 l->append("<SkBitmap>");
270 void ParamTraits<gfx::Range>::Write(Message* m, const gfx::Range& r) {
271 m->WriteSizeT(r.start());
272 m->WriteSizeT(r.end());
275 bool ParamTraits<gfx::Range>::Read(const Message* m,
276 base::PickleIterator* iter,
277 gfx::Range* r) {
278 size_t start, end;
279 if (!iter->ReadSizeT(&start) || !iter->ReadSizeT(&end))
280 return false;
281 r->set_start(start);
282 r->set_end(end);
283 return true;
286 void ParamTraits<gfx::Range>::Log(const gfx::Range& r, std::string* l) {
287 l->append(base::StringPrintf("(%" PRIuS ", %" PRIuS ")", r.start(), r.end()));
290 } // namespace IPC
292 // Generate param traits write methods.
293 #include "ipc/param_traits_write_macros.h"
294 namespace IPC {
295 #undef UI_GFX_IPC_GFX_PARAM_TRAITS_MACROS_H_
296 #include "ui/gfx/ipc/gfx_param_traits_macros.h"
297 } // namespace IPC
299 // Generate param traits read methods.
300 #include "ipc/param_traits_read_macros.h"
301 namespace IPC {
302 #undef UI_GFX_IPC_GFX_PARAM_TRAITS_MACROS_H_
303 #include "ui/gfx/ipc/gfx_param_traits_macros.h"
304 } // namespace IPC
306 // Generate param traits log methods.
307 #include "ipc/param_traits_log_macros.h"
308 namespace IPC {
309 #undef UI_GFX_IPC_GFX_PARAM_TRAITS_MACROS_H_
310 #include "ui/gfx/ipc/gfx_param_traits_macros.h"
311 } // namespace IPC