Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / gfx / ipc / gfx_param_traits.cc
bloba6444d063d5ec1c3a77944b95f5faeb5e48e6089
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.fColorType;
32 fAlphaType = info.fAlphaType;
33 fWidth = info.fWidth;
34 fHeight = info.fHeight;
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, PickleIterator* iter,
61 gfx::Point* r) {
62 int x, y;
63 if (!iter->ReadInt(&x) ||
64 !iter->ReadInt(&y))
65 return false;
66 r->set_x(x);
67 r->set_y(y);
68 return true;
71 void ParamTraits<gfx::Point>::Log(const gfx::Point& p, std::string* l) {
72 l->append(base::StringPrintf("(%d, %d)", p.x(), p.y()));
75 void ParamTraits<gfx::PointF>::Write(Message* m, const gfx::PointF& v) {
76 ParamTraits<float>::Write(m, v.x());
77 ParamTraits<float>::Write(m, v.y());
80 bool ParamTraits<gfx::PointF>::Read(const Message* m,
81 PickleIterator* iter,
82 gfx::PointF* r) {
83 float x, y;
84 if (!ParamTraits<float>::Read(m, iter, &x) ||
85 !ParamTraits<float>::Read(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& v, std::string* l) {
93 l->append(base::StringPrintf("(%f, %f)", v.x(), v.y()));
96 void ParamTraits<gfx::Size>::Write(Message* m, const gfx::Size& p) {
97 DCHECK_GE(p.width(), 0);
98 DCHECK_GE(p.height(), 0);
99 int values[2] = { p.width(), p.height() };
100 m->WriteBytes(&values, sizeof(int) * 2);
103 bool ParamTraits<gfx::Size>::Read(const Message* m,
104 PickleIterator* iter,
105 gfx::Size* r) {
106 const char* char_values;
107 if (!iter->ReadBytes(&char_values, sizeof(int) * 2))
108 return false;
109 const int* values = reinterpret_cast<const int*>(char_values);
110 if (values[0] < 0 || values[1] < 0)
111 return false;
112 r->set_width(values[0]);
113 r->set_height(values[1]);
114 return true;
117 void ParamTraits<gfx::Size>::Log(const gfx::Size& p, std::string* l) {
118 l->append(base::StringPrintf("(%d, %d)", p.width(), p.height()));
121 void ParamTraits<gfx::SizeF>::Write(Message* m, const gfx::SizeF& p) {
122 float values[2] = { p.width(), p.height() };
123 m->WriteBytes(&values, sizeof(float) * 2);
126 bool ParamTraits<gfx::SizeF>::Read(const Message* m,
127 PickleIterator* iter,
128 gfx::SizeF* r) {
129 const char* char_values;
130 if (!iter->ReadBytes(&char_values, sizeof(float) * 2))
131 return false;
132 const float* values = reinterpret_cast<const float*>(char_values);
133 r->set_width(values[0]);
134 r->set_height(values[1]);
135 return true;
138 void ParamTraits<gfx::SizeF>::Log(const gfx::SizeF& p, std::string* l) {
139 l->append(base::StringPrintf("(%f, %f)", p.width(), p.height()));
142 void ParamTraits<gfx::Vector2d>::Write(Message* m, const gfx::Vector2d& p) {
143 int values[2] = { p.x(), p.y() };
144 m->WriteBytes(&values, sizeof(int) * 2);
147 bool ParamTraits<gfx::Vector2d>::Read(const Message* m,
148 PickleIterator* iter,
149 gfx::Vector2d* r) {
150 const char* char_values;
151 if (!iter->ReadBytes(&char_values, sizeof(int) * 2))
152 return false;
153 const int* values = reinterpret_cast<const int*>(char_values);
154 r->set_x(values[0]);
155 r->set_y(values[1]);
156 return true;
159 void ParamTraits<gfx::Vector2d>::Log(const gfx::Vector2d& v, std::string* l) {
160 l->append(base::StringPrintf("(%d, %d)", v.x(), v.y()));
163 void ParamTraits<gfx::Vector2dF>::Write(Message* m, const gfx::Vector2dF& p) {
164 float values[2] = { p.x(), p.y() };
165 m->WriteBytes(&values, sizeof(float) * 2);
168 bool ParamTraits<gfx::Vector2dF>::Read(const Message* m,
169 PickleIterator* iter,
170 gfx::Vector2dF* r) {
171 const char* char_values;
172 if (!iter->ReadBytes(&char_values, sizeof(float) * 2))
173 return false;
174 const float* values = reinterpret_cast<const float*>(char_values);
175 r->set_x(values[0]);
176 r->set_y(values[1]);
177 return true;
180 void ParamTraits<gfx::Vector2dF>::Log(const gfx::Vector2dF& v, std::string* l) {
181 l->append(base::StringPrintf("(%f, %f)", v.x(), v.y()));
184 void ParamTraits<gfx::Rect>::Write(Message* m, const gfx::Rect& p) {
185 int values[4] = { p.x(), p.y(), p.width(), p.height() };
186 m->WriteBytes(&values, sizeof(int) * 4);
189 bool ParamTraits<gfx::Rect>::Read(const Message* m,
190 PickleIterator* iter,
191 gfx::Rect* r) {
192 const char* char_values;
193 if (!iter->ReadBytes(&char_values, sizeof(int) * 4))
194 return false;
195 const int* values = reinterpret_cast<const int*>(char_values);
196 if (values[2] < 0 || values[3] < 0)
197 return false;
198 r->SetRect(values[0], values[1], values[2], values[3]);
199 return true;
202 void ParamTraits<gfx::Rect>::Log(const gfx::Rect& p, std::string* l) {
203 l->append(base::StringPrintf("(%d, %d, %d, %d)", p.x(), p.y(),
204 p.width(), p.height()));
207 void ParamTraits<gfx::RectF>::Write(Message* m, const gfx::RectF& p) {
208 float values[4] = { p.x(), p.y(), p.width(), p.height() };
209 m->WriteBytes(&values, sizeof(float) * 4);
212 bool ParamTraits<gfx::RectF>::Read(const Message* m,
213 PickleIterator* iter,
214 gfx::RectF* r) {
215 const char* char_values;
216 if (!iter->ReadBytes(&char_values, sizeof(float) * 4))
217 return false;
218 const float* values = reinterpret_cast<const float*>(char_values);
219 r->SetRect(values[0], values[1], values[2], values[3]);
220 return true;
223 void ParamTraits<gfx::RectF>::Log(const gfx::RectF& p, std::string* l) {
224 l->append(base::StringPrintf("(%f, %f, %f, %f)", p.x(), p.y(),
225 p.width(), p.height()));
228 void ParamTraits<SkBitmap>::Write(Message* m, const SkBitmap& p) {
229 size_t fixed_size = sizeof(SkBitmap_Data);
230 SkBitmap_Data bmp_data;
231 bmp_data.InitSkBitmapDataForTransfer(p);
232 m->WriteData(reinterpret_cast<const char*>(&bmp_data),
233 static_cast<int>(fixed_size));
234 size_t pixel_size = p.getSize();
235 SkAutoLockPixels p_lock(p);
236 m->WriteData(reinterpret_cast<const char*>(p.getPixels()),
237 static_cast<int>(pixel_size));
240 bool ParamTraits<SkBitmap>::Read(const Message* m,
241 PickleIterator* iter,
242 SkBitmap* r) {
243 const char* fixed_data;
244 int fixed_data_size = 0;
245 if (!iter->ReadData(&fixed_data, &fixed_data_size) ||
246 (fixed_data_size <= 0)) {
247 NOTREACHED();
248 return false;
250 if (fixed_data_size != sizeof(SkBitmap_Data))
251 return false; // Message is malformed.
253 const char* variable_data;
254 int variable_data_size = 0;
255 if (!iter->ReadData(&variable_data, &variable_data_size) ||
256 (variable_data_size < 0)) {
257 NOTREACHED();
258 return false;
260 const SkBitmap_Data* bmp_data =
261 reinterpret_cast<const SkBitmap_Data*>(fixed_data);
262 return bmp_data->InitSkBitmapFromData(r, variable_data, variable_data_size);
265 void ParamTraits<SkBitmap>::Log(const SkBitmap& p, std::string* l) {
266 l->append("<SkBitmap>");
269 void ParamTraits<gfx::Range>::Write(Message* m, const gfx::Range& r) {
270 m->WriteSizeT(r.start());
271 m->WriteSizeT(r.end());
274 bool ParamTraits<gfx::Range>::Read(const Message* m,
275 PickleIterator* iter,
276 gfx::Range* r) {
277 size_t start, end;
278 if (!iter->ReadSizeT(&start) || !iter->ReadSizeT(&end))
279 return false;
280 r->set_start(start);
281 r->set_end(end);
282 return true;
285 void ParamTraits<gfx::Range>::Log(const gfx::Range& r, std::string* l) {
286 l->append(base::StringPrintf("(%" PRIuS ", %" PRIuS ")", r.start(), r.end()));
290 } // namespace IPC