Chromium Code Reviews| 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/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 | |
| 13 namespace { | |
| 14 | |
| 15 struct SkBitmap_Data { | |
| 16 // The color type for the bitmap (bits per pixel, etc). | |
| 17 SkColorType fColorType; | |
|
dcheng
2016/03/28 20:37:56
I know this is just copied code, but shouldn't we
Mark Dittmer
2016/03/29 17:41:25
Done.
| |
| 18 | |
| 19 // The alpha type for the bitmap (opaque, premul, unpremul). | |
| 20 SkAlphaType fAlphaType; | |
| 21 | |
| 22 // The width of the bitmap in pixels. | |
| 23 uint32_t fWidth; | |
| 24 | |
| 25 // The height of the bitmap in pixels. | |
| 26 uint32_t fHeight; | |
| 27 | |
| 28 void InitSkBitmapDataForTransfer(const SkBitmap& bitmap) { | |
| 29 const SkImageInfo& info = bitmap.info(); | |
| 30 fColorType = info.colorType(); | |
| 31 fAlphaType = info.alphaType(); | |
| 32 fWidth = info.width(); | |
| 33 fHeight = info.height(); | |
| 34 } | |
| 35 | |
| 36 // Returns whether |bitmap| successfully initialized. | |
| 37 bool InitSkBitmapFromData(SkBitmap* bitmap, | |
| 38 const char* pixels, | |
| 39 size_t pixels_size) const { | |
| 40 if (!bitmap->tryAllocPixels( | |
| 41 SkImageInfo::Make(fWidth, fHeight, fColorType, fAlphaType))) | |
| 42 return false; | |
| 43 if (pixels_size != bitmap->getSize()) | |
| 44 return false; | |
| 45 memcpy(bitmap->getPixels(), pixels, pixels_size); | |
| 46 return true; | |
| 47 } | |
| 48 }; | |
| 49 | |
| 50 } // namespace | |
| 51 | |
| 52 namespace IPC { | |
| 53 | |
| 54 void ParamTraits<SkBitmap>::Write(base::Pickle* m, const SkBitmap& p) { | |
| 55 size_t fixed_size = sizeof(SkBitmap_Data); | |
| 56 SkBitmap_Data bmp_data; | |
| 57 bmp_data.InitSkBitmapDataForTransfer(p); | |
| 58 m->WriteData(reinterpret_cast<const char*>(&bmp_data), | |
| 59 static_cast<int>(fixed_size)); | |
| 60 size_t pixel_size = p.getSize(); | |
| 61 SkAutoLockPixels p_lock(p); | |
| 62 m->WriteData(reinterpret_cast<const char*>(p.getPixels()), | |
| 63 static_cast<int>(pixel_size)); | |
| 64 } | |
| 65 | |
| 66 bool ParamTraits<SkBitmap>::Read(const base::Pickle* m, | |
| 67 base::PickleIterator* iter, | |
| 68 SkBitmap* r) { | |
| 69 const char* fixed_data; | |
| 70 int fixed_data_size = 0; | |
| 71 if (!iter->ReadData(&fixed_data, &fixed_data_size) || | |
| 72 (fixed_data_size <= 0)) { | |
| 73 NOTREACHED(); | |
| 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 NOTREACHED(); | |
| 84 return false; | |
| 85 } | |
| 86 const SkBitmap_Data* bmp_data = | |
| 87 reinterpret_cast<const SkBitmap_Data*>(fixed_data); | |
| 88 return bmp_data->InitSkBitmapFromData(r, variable_data, variable_data_size); | |
| 89 } | |
| 90 | |
| 91 void ParamTraits<SkBitmap>::Log(const SkBitmap& p, std::string* l) { | |
| 92 l->append("<SkBitmap>"); | |
| 93 } | |
| 94 | |
| 95 } // namespace IPC | |
| OLD | NEW |