| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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. |
| 4 |
| 5 #include "ui/gfx/ipc/skia/gfx_skia_param_traits.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/pickle.h" |
| 10 #include "third_party/skia/include/core/SkBitmap.h" |
| 11 #include "third_party/skia/include/core/SkImageInfo.h" |
| 12 #include "ui/gfx/transform.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 struct SkBitmap_Data { |
| 17 // The color type for the bitmap (bits per pixel, etc). |
| 18 SkColorType color_type; |
| 19 |
| 20 // The alpha type for the bitmap (opaque, premul, unpremul). |
| 21 SkAlphaType alpha_type; |
| 22 |
| 23 // The width of the bitmap in pixels. |
| 24 uint32_t width; |
| 25 |
| 26 // The height of the bitmap in pixels. |
| 27 uint32_t height; |
| 28 |
| 29 void InitSkBitmapDataForTransfer(const SkBitmap& bitmap) { |
| 30 const SkImageInfo& info = bitmap.info(); |
| 31 color_type = info.colorType(); |
| 32 alpha_type = info.alphaType(); |
| 33 width = info.width(); |
| 34 height = info.height(); |
| 35 } |
| 36 |
| 37 // Returns whether |bitmap| successfully initialized. |
| 38 bool InitSkBitmapFromData(SkBitmap* bitmap, |
| 39 const char* pixels, |
| 40 size_t pixels_size) const { |
| 41 if (!bitmap->tryAllocPixels( |
| 42 SkImageInfo::Make(width, height, color_type, alpha_type))) |
| 43 return false; |
| 44 if (pixels_size != bitmap->getSize()) |
| 45 return false; |
| 46 memcpy(bitmap->getPixels(), pixels, pixels_size); |
| 47 return true; |
| 48 } |
| 49 }; |
| 50 |
| 51 } // namespace |
| 52 |
| 53 namespace IPC { |
| 54 |
| 55 void ParamTraits<SkBitmap>::Write(base::Pickle* m, const SkBitmap& p) { |
| 56 size_t fixed_size = sizeof(SkBitmap_Data); |
| 57 SkBitmap_Data bmp_data; |
| 58 bmp_data.InitSkBitmapDataForTransfer(p); |
| 59 m->WriteData(reinterpret_cast<const char*>(&bmp_data), |
| 60 static_cast<int>(fixed_size)); |
| 61 size_t pixel_size = p.getSize(); |
| 62 SkAutoLockPixels p_lock(p); |
| 63 m->WriteData(reinterpret_cast<const char*>(p.getPixels()), |
| 64 static_cast<int>(pixel_size)); |
| 65 } |
| 66 |
| 67 bool ParamTraits<SkBitmap>::Read(const base::Pickle* m, |
| 68 base::PickleIterator* iter, |
| 69 SkBitmap* r) { |
| 70 const char* fixed_data; |
| 71 int fixed_data_size = 0; |
| 72 if (!iter->ReadData(&fixed_data, &fixed_data_size) || |
| 73 (fixed_data_size <= 0)) { |
| 74 return false; |
| 75 } |
| 76 if (fixed_data_size != sizeof(SkBitmap_Data)) |
| 77 return false; // Message is malformed. |
| 78 |
| 79 const char* variable_data; |
| 80 int variable_data_size = 0; |
| 81 if (!iter->ReadData(&variable_data, &variable_data_size) || |
| 82 (variable_data_size < 0)) { |
| 83 return false; |
| 84 } |
| 85 const SkBitmap_Data* bmp_data = |
| 86 reinterpret_cast<const SkBitmap_Data*>(fixed_data); |
| 87 return bmp_data->InitSkBitmapFromData(r, variable_data, variable_data_size); |
| 88 } |
| 89 |
| 90 void ParamTraits<SkBitmap>::Log(const SkBitmap& p, std::string* l) { |
| 91 l->append("<SkBitmap>"); |
| 92 } |
| 93 |
| 94 void ParamTraits<gfx::Transform>::Write(base::Pickle* m, const param_type& p) { |
| 95 #ifdef SK_MSCALAR_IS_FLOAT |
| 96 float column_major_data[16]; |
| 97 p.matrix().asColMajorf(column_major_data); |
| 98 #else |
| 99 double column_major_data[16]; |
| 100 p.matrix().asColMajord(column_major_data); |
| 101 #endif |
| 102 // We do this in a single write for performance reasons. |
| 103 m->WriteBytes(&column_major_data, sizeof(SkMScalar) * 16); |
| 104 } |
| 105 |
| 106 bool ParamTraits<gfx::Transform>::Read(const base::Pickle* m, |
| 107 base::PickleIterator* iter, |
| 108 param_type* r) { |
| 109 const char* column_major_data; |
| 110 if (!iter->ReadBytes(&column_major_data, sizeof(SkMScalar) * 16)) |
| 111 return false; |
| 112 r->matrix().setColMajor( |
| 113 reinterpret_cast<const SkMScalar*>(column_major_data)); |
| 114 return true; |
| 115 } |
| 116 |
| 117 void ParamTraits<gfx::Transform>::Log( |
| 118 const param_type& p, std::string* l) { |
| 119 #ifdef SK_MSCALAR_IS_FLOAT |
| 120 float row_major_data[16]; |
| 121 p.matrix().asRowMajorf(row_major_data); |
| 122 #else |
| 123 double row_major_data[16]; |
| 124 p.matrix().asRowMajord(row_major_data); |
| 125 #endif |
| 126 l->append("("); |
| 127 for (int i = 0; i < 16; ++i) { |
| 128 if (i > 0) |
| 129 l->append(", "); |
| 130 LogParam(row_major_data[i], l); |
| 131 } |
| 132 l->append(") "); |
| 133 } |
| 134 |
| 135 } // namespace IPC |
| OLD | NEW |