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/rect.h"
11 #include "ui/gfx/rect_f.h"
15 struct SkBitmap_Data
{
16 // The color type for the bitmap (bits per pixel, etc).
17 SkColorType fColorType
;
19 // The alpha type for the bitmap (opaque, premul, unpremul).
20 SkAlphaType fAlphaType
;
22 // The width of the bitmap in pixels.
25 // The height of the bitmap in pixels.
28 void InitSkBitmapDataForTransfer(const SkBitmap
& bitmap
) {
29 const SkImageInfo
& info
= bitmap
.info();
30 fColorType
= info
.fColorType
;
31 fAlphaType
= info
.fAlphaType
;
33 fHeight
= info
.fHeight
;
36 // Returns whether |bitmap| successfully initialized.
37 bool InitSkBitmapFromData(SkBitmap
* bitmap
,
39 size_t pixels_size
) const {
40 if (!bitmap
->tryAllocPixels(
41 SkImageInfo::Make(fWidth
, fHeight
, fColorType
, fAlphaType
)))
43 if (pixels_size
!= bitmap
->getSize())
45 memcpy(bitmap
->getPixels(), pixels
, pixels_size
);
54 void ParamTraits
<gfx::Point
>::Write(Message
* m
, const gfx::Point
& p
) {
59 bool ParamTraits
<gfx::Point
>::Read(const Message
* m
, PickleIterator
* iter
,
62 if (!m
->ReadInt(iter
, &x
) ||
63 !m
->ReadInt(iter
, &y
))
70 void ParamTraits
<gfx::Point
>::Log(const gfx::Point
& p
, std::string
* l
) {
71 l
->append(base::StringPrintf("(%d, %d)", p
.x(), p
.y()));
74 void ParamTraits
<gfx::PointF
>::Write(Message
* m
, const gfx::PointF
& v
) {
75 ParamTraits
<float>::Write(m
, v
.x());
76 ParamTraits
<float>::Write(m
, v
.y());
79 bool ParamTraits
<gfx::PointF
>::Read(const Message
* m
,
83 if (!ParamTraits
<float>::Read(m
, iter
, &x
) ||
84 !ParamTraits
<float>::Read(m
, iter
, &y
))
91 void ParamTraits
<gfx::PointF
>::Log(const gfx::PointF
& v
, std::string
* l
) {
92 l
->append(base::StringPrintf("(%f, %f)", v
.x(), v
.y()));
95 void ParamTraits
<gfx::Size
>::Write(Message
* m
, const gfx::Size
& p
) {
96 DCHECK_GE(p
.width(), 0);
97 DCHECK_GE(p
.height(), 0);
98 int values
[2] = { p
.width(), p
.height() };
99 m
->WriteBytes(&values
, sizeof(int) * 2);
102 bool ParamTraits
<gfx::Size
>::Read(const Message
* m
,
103 PickleIterator
* iter
,
105 const char* char_values
;
106 if (!m
->ReadBytes(iter
, &char_values
, sizeof(int) * 2))
108 const int* values
= reinterpret_cast<const int*>(char_values
);
109 if (values
[0] < 0 || values
[1] < 0)
111 r
->set_width(values
[0]);
112 r
->set_height(values
[1]);
116 void ParamTraits
<gfx::Size
>::Log(const gfx::Size
& p
, std::string
* l
) {
117 l
->append(base::StringPrintf("(%d, %d)", p
.width(), p
.height()));
120 void ParamTraits
<gfx::SizeF
>::Write(Message
* m
, const gfx::SizeF
& p
) {
121 float values
[2] = { p
.width(), p
.height() };
122 m
->WriteBytes(&values
, sizeof(float) * 2);
125 bool ParamTraits
<gfx::SizeF
>::Read(const Message
* m
,
126 PickleIterator
* iter
,
128 const char* char_values
;
129 if (!m
->ReadBytes(iter
, &char_values
, sizeof(float) * 2))
131 const float* values
= reinterpret_cast<const float*>(char_values
);
132 r
->set_width(values
[0]);
133 r
->set_height(values
[1]);
137 void ParamTraits
<gfx::SizeF
>::Log(const gfx::SizeF
& p
, std::string
* l
) {
138 l
->append(base::StringPrintf("(%f, %f)", p
.width(), p
.height()));
141 void ParamTraits
<gfx::Vector2d
>::Write(Message
* m
, const gfx::Vector2d
& p
) {
142 int values
[2] = { p
.x(), p
.y() };
143 m
->WriteBytes(&values
, sizeof(int) * 2);
146 bool ParamTraits
<gfx::Vector2d
>::Read(const Message
* m
,
147 PickleIterator
* iter
,
149 const char* char_values
;
150 if (!m
->ReadBytes(iter
, &char_values
, sizeof(int) * 2))
152 const int* values
= reinterpret_cast<const int*>(char_values
);
158 void ParamTraits
<gfx::Vector2d
>::Log(const gfx::Vector2d
& v
, std::string
* l
) {
159 l
->append(base::StringPrintf("(%d, %d)", v
.x(), v
.y()));
162 void ParamTraits
<gfx::Vector2dF
>::Write(Message
* m
, const gfx::Vector2dF
& p
) {
163 float values
[2] = { p
.x(), p
.y() };
164 m
->WriteBytes(&values
, sizeof(float) * 2);
167 bool ParamTraits
<gfx::Vector2dF
>::Read(const Message
* m
,
168 PickleIterator
* iter
,
170 const char* char_values
;
171 if (!m
->ReadBytes(iter
, &char_values
, sizeof(float) * 2))
173 const float* values
= reinterpret_cast<const float*>(char_values
);
179 void ParamTraits
<gfx::Vector2dF
>::Log(const gfx::Vector2dF
& v
, std::string
* l
) {
180 l
->append(base::StringPrintf("(%f, %f)", v
.x(), v
.y()));
183 void ParamTraits
<gfx::Rect
>::Write(Message
* m
, const gfx::Rect
& p
) {
184 int values
[4] = { p
.x(), p
.y(), p
.width(), p
.height() };
185 m
->WriteBytes(&values
, sizeof(int) * 4);
188 bool ParamTraits
<gfx::Rect
>::Read(const Message
* m
,
189 PickleIterator
* iter
,
191 const char* char_values
;
192 if (!m
->ReadBytes(iter
, &char_values
, sizeof(int) * 4))
194 const int* values
= reinterpret_cast<const int*>(char_values
);
195 if (values
[2] < 0 || values
[3] < 0)
197 r
->SetRect(values
[0], values
[1], values
[2], values
[3]);
201 void ParamTraits
<gfx::Rect
>::Log(const gfx::Rect
& p
, std::string
* l
) {
202 l
->append(base::StringPrintf("(%d, %d, %d, %d)", p
.x(), p
.y(),
203 p
.width(), p
.height()));
206 void ParamTraits
<gfx::RectF
>::Write(Message
* m
, const gfx::RectF
& p
) {
207 float values
[4] = { p
.x(), p
.y(), p
.width(), p
.height() };
208 m
->WriteBytes(&values
, sizeof(float) * 4);
211 bool ParamTraits
<gfx::RectF
>::Read(const Message
* m
,
212 PickleIterator
* iter
,
214 const char* char_values
;
215 if (!m
->ReadBytes(iter
, &char_values
, sizeof(float) * 4))
217 const float* values
= reinterpret_cast<const float*>(char_values
);
218 r
->SetRect(values
[0], values
[1], values
[2], values
[3]);
222 void ParamTraits
<gfx::RectF
>::Log(const gfx::RectF
& p
, std::string
* l
) {
223 l
->append(base::StringPrintf("(%f, %f, %f, %f)", p
.x(), p
.y(),
224 p
.width(), p
.height()));
227 void ParamTraits
<SkBitmap
>::Write(Message
* m
, const SkBitmap
& p
) {
228 size_t fixed_size
= sizeof(SkBitmap_Data
);
229 SkBitmap_Data bmp_data
;
230 bmp_data
.InitSkBitmapDataForTransfer(p
);
231 m
->WriteData(reinterpret_cast<const char*>(&bmp_data
),
232 static_cast<int>(fixed_size
));
233 size_t pixel_size
= p
.getSize();
234 SkAutoLockPixels
p_lock(p
);
235 m
->WriteData(reinterpret_cast<const char*>(p
.getPixels()),
236 static_cast<int>(pixel_size
));
239 bool ParamTraits
<SkBitmap
>::Read(const Message
* m
,
240 PickleIterator
* iter
,
242 const char* fixed_data
;
243 int fixed_data_size
= 0;
244 if (!m
->ReadData(iter
, &fixed_data
, &fixed_data_size
) ||
245 (fixed_data_size
<= 0)) {
249 if (fixed_data_size
!= sizeof(SkBitmap_Data
))
250 return false; // Message is malformed.
252 const char* variable_data
;
253 int variable_data_size
= 0;
254 if (!m
->ReadData(iter
, &variable_data
, &variable_data_size
) ||
255 (variable_data_size
< 0)) {
259 const SkBitmap_Data
* bmp_data
=
260 reinterpret_cast<const SkBitmap_Data
*>(fixed_data
);
261 return bmp_data
->InitSkBitmapFromData(r
, variable_data
, variable_data_size
);
264 void ParamTraits
<SkBitmap
>::Log(const SkBitmap
& p
, std::string
* l
) {
265 l
->append("<SkBitmap>");