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
.fColorType
;
32 fAlphaType
= info
.fAlphaType
;
34 fHeight
= info
.fHeight
;
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
, PickleIterator
* iter
,
63 if (!iter
->ReadInt(&x
) ||
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
,
84 if (!ParamTraits
<float>::Read(m
, iter
, &x
) ||
85 !ParamTraits
<float>::Read(m
, iter
, &y
))
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
,
106 const char* char_values
;
107 if (!iter
->ReadBytes(&char_values
, sizeof(int) * 2))
109 const int* values
= reinterpret_cast<const int*>(char_values
);
110 if (values
[0] < 0 || values
[1] < 0)
112 r
->set_width(values
[0]);
113 r
->set_height(values
[1]);
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
,
129 const char* char_values
;
130 if (!iter
->ReadBytes(&char_values
, sizeof(float) * 2))
132 const float* values
= reinterpret_cast<const float*>(char_values
);
133 r
->set_width(values
[0]);
134 r
->set_height(values
[1]);
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
,
150 const char* char_values
;
151 if (!iter
->ReadBytes(&char_values
, sizeof(int) * 2))
153 const int* values
= reinterpret_cast<const int*>(char_values
);
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
,
171 const char* char_values
;
172 if (!iter
->ReadBytes(&char_values
, sizeof(float) * 2))
174 const float* values
= reinterpret_cast<const float*>(char_values
);
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
,
192 const char* char_values
;
193 if (!iter
->ReadBytes(&char_values
, sizeof(int) * 4))
195 const int* values
= reinterpret_cast<const int*>(char_values
);
196 if (values
[2] < 0 || values
[3] < 0)
198 r
->SetRect(values
[0], values
[1], values
[2], values
[3]);
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
,
215 const char* char_values
;
216 if (!iter
->ReadBytes(&char_values
, sizeof(float) * 4))
218 const float* values
= reinterpret_cast<const float*>(char_values
);
219 r
->SetRect(values
[0], values
[1], values
[2], values
[3]);
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
,
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)) {
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)) {
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
,
278 if (!iter
->ReadSizeT(&start
) || !iter
->ReadSizeT(&end
))
285 void ParamTraits
<gfx::Range
>::Log(const gfx::Range
& r
, std::string
* l
) {
286 l
->append(base::StringPrintf("(%" PRIuS
", %" PRIuS
")", r
.start(), r
.end()));