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/rect.h"
11 #include "ui/gfx/geometry/rect_f.h"
12 #include "ui/gfx/range/range.h"
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.
26 // The height of the bitmap in pixels.
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
,
40 size_t pixels_size
) const {
41 if (!bitmap
->tryAllocPixels(
42 SkImageInfo::Make(fWidth
, fHeight
, fColorType
, fAlphaType
)))
44 if (pixels_size
!= bitmap
->getSize())
46 memcpy(bitmap
->getPixels(), pixels
, pixels_size
);
55 void ParamTraits
<gfx::Point
>::Write(Message
* m
, const gfx::Point
& p
) {
60 bool ParamTraits
<gfx::Point
>::Read(const Message
* m
,
61 base::PickleIterator
* iter
,
64 if (!iter
->ReadInt(&x
) ||
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
,
85 if (!ParamTraits
<float>::Read(m
, iter
, &x
) ||
86 !ParamTraits
<float>::Read(m
, iter
, &y
))
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
,
107 const char* char_values
;
108 if (!iter
->ReadBytes(&char_values
, sizeof(int) * 2))
110 const int* values
= reinterpret_cast<const int*>(char_values
);
111 if (values
[0] < 0 || values
[1] < 0)
113 r
->set_width(values
[0]);
114 r
->set_height(values
[1]);
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
,
130 const char* char_values
;
131 if (!iter
->ReadBytes(&char_values
, sizeof(float) * 2))
133 const float* values
= reinterpret_cast<const float*>(char_values
);
134 r
->set_width(values
[0]);
135 r
->set_height(values
[1]);
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
,
151 const char* char_values
;
152 if (!iter
->ReadBytes(&char_values
, sizeof(int) * 2))
154 const int* values
= reinterpret_cast<const int*>(char_values
);
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
,
172 const char* char_values
;
173 if (!iter
->ReadBytes(&char_values
, sizeof(float) * 2))
175 const float* values
= reinterpret_cast<const float*>(char_values
);
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
,
193 const char* char_values
;
194 if (!iter
->ReadBytes(&char_values
, sizeof(int) * 4))
196 const int* values
= reinterpret_cast<const int*>(char_values
);
197 if (values
[2] < 0 || values
[3] < 0)
199 r
->SetRect(values
[0], values
[1], values
[2], values
[3]);
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
,
216 const char* char_values
;
217 if (!iter
->ReadBytes(&char_values
, sizeof(float) * 4))
219 const float* values
= reinterpret_cast<const float*>(char_values
);
220 r
->SetRect(values
[0], values
[1], values
[2], values
[3]);
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
,
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)) {
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)) {
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
,
279 if (!iter
->ReadSizeT(&start
) || !iter
->ReadSizeT(&end
))
286 void ParamTraits
<gfx::Range
>::Log(const gfx::Range
& r
, std::string
* l
) {
287 l
->append(base::StringPrintf("(%" PRIuS
", %" PRIuS
")", r
.start(), r
.end()));
292 // Generate param traits write methods.
293 #include "ipc/param_traits_write_macros.h"
295 #undef UI_GFX_IPC_GFX_PARAM_TRAITS_MACROS_H_
296 #include "ui/gfx/ipc/gfx_param_traits_macros.h"
299 // Generate param traits read methods.
300 #include "ipc/param_traits_read_macros.h"
302 #undef UI_GFX_IPC_GFX_PARAM_TRAITS_MACROS_H_
303 #include "ui/gfx/ipc/gfx_param_traits_macros.h"
306 // Generate param traits log methods.
307 #include "ipc/param_traits_log_macros.h"
309 #undef UI_GFX_IPC_GFX_PARAM_TRAITS_MACROS_H_
310 #include "ui/gfx/ipc/gfx_param_traits_macros.h"