Chromium Code Reviews| Index: ui/gfx/ipc/gfx_skia_param_traits.cc |
| diff --git a/ui/gfx/ipc/gfx_skia_param_traits.cc b/ui/gfx/ipc/gfx_skia_param_traits.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0f4acd306762583cdf38796a9965fe90679d96df |
| --- /dev/null |
| +++ b/ui/gfx/ipc/gfx_skia_param_traits.cc |
| @@ -0,0 +1,95 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ui/gfx/ipc/gfx_skia_param_traits.h" |
| + |
| +#include <string> |
| + |
| +#include "base/pickle.h" |
| +#include "third_party/skia/include/core/SkBitmap.h" |
| +#include "third_party/skia/include/core/SkImageInfo.h" |
| + |
| +namespace { |
| + |
| +struct SkBitmap_Data { |
| + // The color type for the bitmap (bits per pixel, etc). |
| + 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.
|
| + |
| + // The alpha type for the bitmap (opaque, premul, unpremul). |
| + SkAlphaType fAlphaType; |
| + |
| + // The width of the bitmap in pixels. |
| + uint32_t fWidth; |
| + |
| + // The height of the bitmap in pixels. |
| + uint32_t fHeight; |
| + |
| + void InitSkBitmapDataForTransfer(const SkBitmap& bitmap) { |
| + const SkImageInfo& info = bitmap.info(); |
| + fColorType = info.colorType(); |
| + fAlphaType = info.alphaType(); |
| + fWidth = info.width(); |
| + fHeight = info.height(); |
| + } |
| + |
| + // Returns whether |bitmap| successfully initialized. |
| + bool InitSkBitmapFromData(SkBitmap* bitmap, |
| + const char* pixels, |
| + size_t pixels_size) const { |
| + if (!bitmap->tryAllocPixels( |
| + SkImageInfo::Make(fWidth, fHeight, fColorType, fAlphaType))) |
| + return false; |
| + if (pixels_size != bitmap->getSize()) |
| + return false; |
| + memcpy(bitmap->getPixels(), pixels, pixels_size); |
| + return true; |
| + } |
| +}; |
| + |
| +} // namespace |
| + |
| +namespace IPC { |
| + |
| +void ParamTraits<SkBitmap>::Write(base::Pickle* m, const SkBitmap& p) { |
| + size_t fixed_size = sizeof(SkBitmap_Data); |
| + SkBitmap_Data bmp_data; |
| + bmp_data.InitSkBitmapDataForTransfer(p); |
| + m->WriteData(reinterpret_cast<const char*>(&bmp_data), |
| + static_cast<int>(fixed_size)); |
| + size_t pixel_size = p.getSize(); |
| + SkAutoLockPixels p_lock(p); |
| + m->WriteData(reinterpret_cast<const char*>(p.getPixels()), |
| + static_cast<int>(pixel_size)); |
| +} |
| + |
| +bool ParamTraits<SkBitmap>::Read(const base::Pickle* m, |
| + base::PickleIterator* iter, |
| + SkBitmap* r) { |
| + const char* fixed_data; |
| + int fixed_data_size = 0; |
| + if (!iter->ReadData(&fixed_data, &fixed_data_size) || |
| + (fixed_data_size <= 0)) { |
| + NOTREACHED(); |
| + return false; |
| + } |
| + if (fixed_data_size != sizeof(SkBitmap_Data)) |
| + return false; // Message is malformed. |
| + |
| + const char* variable_data; |
| + int variable_data_size = 0; |
| + if (!iter->ReadData(&variable_data, &variable_data_size) || |
| + (variable_data_size < 0)) { |
| + NOTREACHED(); |
| + return false; |
| + } |
| + const SkBitmap_Data* bmp_data = |
| + reinterpret_cast<const SkBitmap_Data*>(fixed_data); |
| + return bmp_data->InitSkBitmapFromData(r, variable_data, variable_data_size); |
| +} |
| + |
| +void ParamTraits<SkBitmap>::Log(const SkBitmap& p, std::string* l) { |
| + l->append("<SkBitmap>"); |
| +} |
| + |
| +} // namespace IPC |