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"
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"
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.
27 // The height of the bitmap in pixels.
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
,
41 size_t pixels_size
) const {
42 if (!bitmap
->tryAllocPixels(
43 SkImageInfo::Make(fWidth
, fHeight
, fColorType
, fAlphaType
)))
45 if (pixels_size
!= bitmap
->getSize())
47 memcpy(bitmap
->getPixels(), pixels
, pixels_size
);
56 void ParamTraits
<gfx::Point
>::Write(Message
* m
, const gfx::Point
& p
) {
61 bool ParamTraits
<gfx::Point
>::Read(const Message
* m
,
62 base::PickleIterator
* iter
,
65 if (!ReadParam(m
, iter
, &x
) || !ReadParam(m
, iter
, &y
))
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
) {
81 bool ParamTraits
<gfx::PointF
>::Read(const Message
* m
,
82 base::PickleIterator
* iter
,
85 if (!ReadParam(m
, iter
, &x
) || !ReadParam(m
, iter
, &y
))
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
) {
102 bool ParamTraits
<gfx::Point3F
>::Read(const Message
* m
,
103 base::PickleIterator
* iter
,
106 if (!ReadParam(m
, iter
, &x
) || !ReadParam(m
, iter
, &y
) ||
107 !ReadParam(m
, iter
, &z
))
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
,
129 const char* char_values
;
130 if (!iter
->ReadBytes(&char_values
, sizeof(int) * 2))
132 const int* values
= reinterpret_cast<const int*>(char_values
);
133 if (values
[0] < 0 || values
[1] < 0)
135 r
->set_width(values
[0]);
136 r
->set_height(values
[1]);
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
,
152 const char* char_values
;
153 if (!iter
->ReadBytes(&char_values
, sizeof(float) * 2))
155 const float* values
= reinterpret_cast<const float*>(char_values
);
156 r
->set_width(values
[0]);
157 r
->set_height(values
[1]);
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
,
173 const char* char_values
;
174 if (!iter
->ReadBytes(&char_values
, sizeof(int) * 2))
176 const int* values
= reinterpret_cast<const int*>(char_values
);
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
,
194 const char* char_values
;
195 if (!iter
->ReadBytes(&char_values
, sizeof(float) * 2))
197 const float* values
= reinterpret_cast<const float*>(char_values
);
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
,
215 const char* char_values
;
216 if (!iter
->ReadBytes(&char_values
, sizeof(int) * 4))
218 const int* values
= reinterpret_cast<const int*>(char_values
);
219 if (values
[2] < 0 || values
[3] < 0)
221 r
->SetRect(values
[0], values
[1], values
[2], values
[3]);
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
,
238 const char* char_values
;
239 if (!iter
->ReadBytes(&char_values
, sizeof(float) * 4))
241 const float* values
= reinterpret_cast<const float*>(char_values
);
242 r
->SetRect(values
[0], values
[1], values
[2], values
[3]);
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
,
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)) {
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)) {
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
,
301 if (!iter
->ReadSizeT(&start
) || !iter
->ReadSizeT(&end
))
308 void ParamTraits
<gfx::Range
>::Log(const gfx::Range
& r
, std::string
* l
) {
309 l
->append(base::StringPrintf("(%" PRIuS
", %" PRIuS
")", r
.start(), r
.end()));
314 // Generate param traits write methods.
315 #include "ipc/param_traits_write_macros.h"
317 #undef UI_GFX_IPC_GFX_PARAM_TRAITS_MACROS_H_
318 #include "ui/gfx/ipc/gfx_param_traits_macros.h"
321 // Generate param traits read methods.
322 #include "ipc/param_traits_read_macros.h"
324 #undef UI_GFX_IPC_GFX_PARAM_TRAITS_MACROS_H_
325 #include "ui/gfx/ipc/gfx_param_traits_macros.h"
328 // Generate param traits log methods.
329 #include "ipc/param_traits_log_macros.h"
331 #undef UI_GFX_IPC_GFX_PARAM_TRAITS_MACROS_H_
332 #include "ui/gfx/ipc/gfx_param_traits_macros.h"