| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/gfx/ipc/gfx_param_traits.h" | 5 #include "ui/gfx/ipc/gfx_param_traits.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "third_party/skia/include/core/SkBitmap.h" | |
| 13 #include "ui/gfx/geometry/point3_f.h" | 12 #include "ui/gfx/geometry/point3_f.h" |
| 14 #include "ui/gfx/geometry/rect.h" | 13 #include "ui/gfx/geometry/rect.h" |
| 15 #include "ui/gfx/geometry/rect_f.h" | 14 #include "ui/gfx/geometry/rect_f.h" |
| 16 #include "ui/gfx/geometry/scroll_offset.h" | 15 #include "ui/gfx/geometry/scroll_offset.h" |
| 17 #include "ui/gfx/range/range.h" | 16 #include "ui/gfx/range/range.h" |
| 18 #include "ui/gfx/transform.h" | |
| 19 | 17 |
| 20 #if defined(OS_MACOSX) | 18 #if defined(OS_MACOSX) |
| 21 #include "ipc/mach_port_mac.h" | 19 #include "ipc/mach_port_mac.h" |
| 22 #endif | 20 #endif |
| 23 | 21 |
| 24 namespace { | |
| 25 | |
| 26 struct SkBitmap_Data { | |
| 27 // The color type for the bitmap (bits per pixel, etc). | |
| 28 SkColorType fColorType; | |
| 29 | |
| 30 // The alpha type for the bitmap (opaque, premul, unpremul). | |
| 31 SkAlphaType fAlphaType; | |
| 32 | |
| 33 // The width of the bitmap in pixels. | |
| 34 uint32_t fWidth; | |
| 35 | |
| 36 // The height of the bitmap in pixels. | |
| 37 uint32_t fHeight; | |
| 38 | |
| 39 void InitSkBitmapDataForTransfer(const SkBitmap& bitmap) { | |
| 40 const SkImageInfo& info = bitmap.info(); | |
| 41 fColorType = info.colorType(); | |
| 42 fAlphaType = info.alphaType(); | |
| 43 fWidth = info.width(); | |
| 44 fHeight = info.height(); | |
| 45 } | |
| 46 | |
| 47 // Returns whether |bitmap| successfully initialized. | |
| 48 bool InitSkBitmapFromData(SkBitmap* bitmap, | |
| 49 const char* pixels, | |
| 50 size_t pixels_size) const { | |
| 51 if (!bitmap->tryAllocPixels( | |
| 52 SkImageInfo::Make(fWidth, fHeight, fColorType, fAlphaType))) | |
| 53 return false; | |
| 54 if (pixels_size != bitmap->getSize()) | |
| 55 return false; | |
| 56 memcpy(bitmap->getPixels(), pixels, pixels_size); | |
| 57 return true; | |
| 58 } | |
| 59 }; | |
| 60 | |
| 61 } // namespace | |
| 62 | |
| 63 namespace IPC { | 22 namespace IPC { |
| 64 | 23 |
| 65 void ParamTraits<gfx::Point>::Write(base::Pickle* m, const gfx::Point& p) { | 24 void ParamTraits<gfx::Point>::Write(base::Pickle* m, const gfx::Point& p) { |
| 66 WriteParam(m, p.x()); | 25 WriteParam(m, p.x()); |
| 67 WriteParam(m, p.y()); | 26 WriteParam(m, p.y()); |
| 68 } | 27 } |
| 69 | 28 |
| 70 bool ParamTraits<gfx::Point>::Read(const base::Pickle* m, | 29 bool ParamTraits<gfx::Point>::Read(const base::Pickle* m, |
| 71 base::PickleIterator* iter, | 30 base::PickleIterator* iter, |
| 72 gfx::Point* r) { | 31 gfx::Point* r) { |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 const float* values = reinterpret_cast<const float*>(char_values); | 227 const float* values = reinterpret_cast<const float*>(char_values); |
| 269 r->SetRect(values[0], values[1], values[2], values[3]); | 228 r->SetRect(values[0], values[1], values[2], values[3]); |
| 270 return true; | 229 return true; |
| 271 } | 230 } |
| 272 | 231 |
| 273 void ParamTraits<gfx::RectF>::Log(const gfx::RectF& p, std::string* l) { | 232 void ParamTraits<gfx::RectF>::Log(const gfx::RectF& p, std::string* l) { |
| 274 l->append(base::StringPrintf("(%f, %f, %f, %f)", p.x(), p.y(), | 233 l->append(base::StringPrintf("(%f, %f, %f, %f)", p.x(), p.y(), |
| 275 p.width(), p.height())); | 234 p.width(), p.height())); |
| 276 } | 235 } |
| 277 | 236 |
| 278 void ParamTraits<SkBitmap>::Write(base::Pickle* m, const SkBitmap& p) { | |
| 279 size_t fixed_size = sizeof(SkBitmap_Data); | |
| 280 SkBitmap_Data bmp_data; | |
| 281 bmp_data.InitSkBitmapDataForTransfer(p); | |
| 282 m->WriteData(reinterpret_cast<const char*>(&bmp_data), | |
| 283 static_cast<int>(fixed_size)); | |
| 284 size_t pixel_size = p.getSize(); | |
| 285 SkAutoLockPixels p_lock(p); | |
| 286 m->WriteData(reinterpret_cast<const char*>(p.getPixels()), | |
| 287 static_cast<int>(pixel_size)); | |
| 288 } | |
| 289 | |
| 290 bool ParamTraits<SkBitmap>::Read(const base::Pickle* m, | |
| 291 base::PickleIterator* iter, | |
| 292 SkBitmap* r) { | |
| 293 const char* fixed_data; | |
| 294 int fixed_data_size = 0; | |
| 295 if (!iter->ReadData(&fixed_data, &fixed_data_size) || | |
| 296 (fixed_data_size <= 0)) { | |
| 297 NOTREACHED(); | |
| 298 return false; | |
| 299 } | |
| 300 if (fixed_data_size != sizeof(SkBitmap_Data)) | |
| 301 return false; // Message is malformed. | |
| 302 | |
| 303 const char* variable_data; | |
| 304 int variable_data_size = 0; | |
| 305 if (!iter->ReadData(&variable_data, &variable_data_size) || | |
| 306 (variable_data_size < 0)) { | |
| 307 NOTREACHED(); | |
| 308 return false; | |
| 309 } | |
| 310 const SkBitmap_Data* bmp_data = | |
| 311 reinterpret_cast<const SkBitmap_Data*>(fixed_data); | |
| 312 return bmp_data->InitSkBitmapFromData(r, variable_data, variable_data_size); | |
| 313 } | |
| 314 | |
| 315 void ParamTraits<SkBitmap>::Log(const SkBitmap& p, std::string* l) { | |
| 316 l->append("<SkBitmap>"); | |
| 317 } | |
| 318 | |
| 319 void ParamTraits<gfx::Range>::Write(base::Pickle* m, const gfx::Range& r) { | 237 void ParamTraits<gfx::Range>::Write(base::Pickle* m, const gfx::Range& r) { |
| 320 m->WriteUInt32(r.start()); | 238 m->WriteUInt32(r.start()); |
| 321 m->WriteUInt32(r.end()); | 239 m->WriteUInt32(r.end()); |
| 322 } | 240 } |
| 323 | 241 |
| 324 bool ParamTraits<gfx::Range>::Read(const base::Pickle* m, | 242 bool ParamTraits<gfx::Range>::Read(const base::Pickle* m, |
| 325 base::PickleIterator* iter, | 243 base::PickleIterator* iter, |
| 326 gfx::Range* r) { | 244 gfx::Range* r) { |
| 327 uint32_t start, end; | 245 uint32_t start, end; |
| 328 if (!iter->ReadUInt32(&start) || !iter->ReadUInt32(&end)) | 246 if (!iter->ReadUInt32(&start) || !iter->ReadUInt32(&end)) |
| (...skipping 28 matching lines...) Expand all Loading... |
| 357 } | 275 } |
| 358 | 276 |
| 359 void ParamTraits<gfx::ScrollOffset>::Log(const param_type& p, std::string* l) { | 277 void ParamTraits<gfx::ScrollOffset>::Log(const param_type& p, std::string* l) { |
| 360 l->append("("); | 278 l->append("("); |
| 361 LogParam(p.x(), l); | 279 LogParam(p.x(), l); |
| 362 l->append(", "); | 280 l->append(", "); |
| 363 LogParam(p.y(), l); | 281 LogParam(p.y(), l); |
| 364 l->append(")"); | 282 l->append(")"); |
| 365 } | 283 } |
| 366 | 284 |
| 367 void ParamTraits<gfx::Transform>::Write(base::Pickle* m, const param_type& p) { | |
| 368 #ifdef SK_MSCALAR_IS_FLOAT | |
| 369 float column_major_data[16]; | |
| 370 p.matrix().asColMajorf(column_major_data); | |
| 371 #else | |
| 372 double column_major_data[16]; | |
| 373 p.matrix().asColMajord(column_major_data); | |
| 374 #endif | |
| 375 // We do this in a single write for performance reasons. | |
| 376 m->WriteBytes(&column_major_data, sizeof(SkMScalar) * 16); | |
| 377 } | |
| 378 | |
| 379 bool ParamTraits<gfx::Transform>::Read(const base::Pickle* m, | |
| 380 base::PickleIterator* iter, | |
| 381 param_type* r) { | |
| 382 const char* column_major_data; | |
| 383 if (!iter->ReadBytes(&column_major_data, sizeof(SkMScalar) * 16)) | |
| 384 return false; | |
| 385 r->matrix().setColMajor( | |
| 386 reinterpret_cast<const SkMScalar*>(column_major_data)); | |
| 387 return true; | |
| 388 } | |
| 389 | |
| 390 void ParamTraits<gfx::Transform>::Log( | |
| 391 const param_type& p, std::string* l) { | |
| 392 #ifdef SK_MSCALAR_IS_FLOAT | |
| 393 float row_major_data[16]; | |
| 394 p.matrix().asRowMajorf(row_major_data); | |
| 395 #else | |
| 396 double row_major_data[16]; | |
| 397 p.matrix().asRowMajord(row_major_data); | |
| 398 #endif | |
| 399 l->append("("); | |
| 400 for (int i = 0; i < 16; ++i) { | |
| 401 if (i > 0) | |
| 402 l->append(", "); | |
| 403 LogParam(row_major_data[i], l); | |
| 404 } | |
| 405 l->append(") "); | |
| 406 } | |
| 407 | |
| 408 #if defined(OS_MACOSX) && !defined(OS_IOS) | 285 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 409 void ParamTraits<gfx::ScopedRefCountedIOSurfaceMachPort>::Write( | 286 void ParamTraits<gfx::ScopedRefCountedIOSurfaceMachPort>::Write( |
| 410 base::Pickle* m, | 287 base::Pickle* m, |
| 411 const param_type p) { | 288 const param_type p) { |
| 412 MachPortMac mach_port_mac(p.get()); | 289 MachPortMac mach_port_mac(p.get()); |
| 413 ParamTraits<MachPortMac>::Write(m, mach_port_mac); | 290 ParamTraits<MachPortMac>::Write(m, mach_port_mac); |
| 414 } | 291 } |
| 415 | 292 |
| 416 bool ParamTraits<gfx::ScopedRefCountedIOSurfaceMachPort>::Read( | 293 bool ParamTraits<gfx::ScopedRefCountedIOSurfaceMachPort>::Read( |
| 417 const base::Pickle* m, | 294 const base::Pickle* m, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 447 #undef UI_GFX_IPC_GFX_PARAM_TRAITS_MACROS_H_ | 324 #undef UI_GFX_IPC_GFX_PARAM_TRAITS_MACROS_H_ |
| 448 #include "ui/gfx/ipc/gfx_param_traits_macros.h" | 325 #include "ui/gfx/ipc/gfx_param_traits_macros.h" |
| 449 } // namespace IPC | 326 } // namespace IPC |
| 450 | 327 |
| 451 // Generate param traits log methods. | 328 // Generate param traits log methods. |
| 452 #include "ipc/param_traits_log_macros.h" | 329 #include "ipc/param_traits_log_macros.h" |
| 453 namespace IPC { | 330 namespace IPC { |
| 454 #undef UI_GFX_IPC_GFX_PARAM_TRAITS_MACROS_H_ | 331 #undef UI_GFX_IPC_GFX_PARAM_TRAITS_MACROS_H_ |
| 455 #include "ui/gfx/ipc/gfx_param_traits_macros.h" | 332 #include "ui/gfx/ipc/gfx_param_traits_macros.h" |
| 456 } // namespace IPC | 333 } // namespace IPC |
| OLD | NEW |