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 #ifndef COMPONENT_ARC_BITMAP_BITMAP_STRUCT_TRAITS_H_ | |
| 6 #define COMPONENT_ARC_BITMAP_BITMAP_STRUCT_TRAITS_H_ | |
| 7 | |
| 8 #include "components/arc/common/bitmap.mojom.h" | |
| 9 #include "third_party/skia/include/core/SkBitmap.h" | |
| 10 | |
| 11 namespace mojo { | |
| 12 | |
| 13 template <> | |
| 14 struct StructTraits<arc::mojom::ArcBitmapDataView, SkBitmap> { | |
| 15 static const mojo::CArray<uint8_t> pixel_data(const SkBitmap& r) { | |
| 16 DCHECK(!r.drawsNothing()); | |
| 17 | |
| 18 const SkImageInfo& info = r.info(); | |
| 19 DCHECK_EQ(info.colorType(), kRGBA_8888_SkColorType); | |
| 20 | |
| 21 return mojo::CArray<uint8_t>( | |
| 22 r.getSize(), r.getSize(), static_cast<uint8_t*>(r.getPixels())); | |
| 23 } | |
| 24 | |
| 25 static uint32_t width(const SkBitmap& r) { | |
| 26 return r.width(); | |
| 27 } | |
| 28 | |
| 29 static uint32_t height(const SkBitmap& r) { | |
| 30 return r.height(); | |
| 31 } | |
| 32 | |
| 33 static bool Read(arc::mojom::ArcBitmapDataView data, SkBitmap* out) { | |
|
dcheng
2016/09/13 01:13:09
Please out-of-line and place the definition of thi
yoshiki
2016/09/13 18:01:43
Done.
| |
| 34 mojo::ArrayDataView<uint8_t> pixel_data; | |
| 35 data.GetPixelDataDataView(&pixel_data); | |
| 36 | |
| 37 SkImageInfo info = SkImageInfo::Make( | |
| 38 data.width(), data.height(), | |
| 39 kRGBA_8888_SkColorType, kPremul_SkAlphaType); | |
| 40 if (info.getSafeSize(info.minRowBytes()) > pixel_data.size()) { | |
| 41 // Insufficient buffer size. | |
| 42 return false; | |
| 43 } | |
| 44 | |
| 45 // Create the SkBitmap object which wraps the arc bitmap pixels. This | |
| 46 // doesn't copy and |data| and |bitmap| share the buffer. | |
| 47 SkBitmap bitmap; | |
| 48 if (!bitmap.installPixels(info, | |
| 49 const_cast<uint8_t*>(pixel_data.data()), | |
| 50 info.minRowBytes())) { | |
| 51 // Error in installing pixels. | |
| 52 return false; | |
| 53 } | |
| 54 | |
| 55 // Copy the pixels with converting color type. | |
| 56 if (!bitmap.copyTo(out, kN32_SkColorType)) { | |
| 57 // Error in copying or converting the color type. | |
| 58 return false; | |
| 59 } | |
| 60 | |
| 61 return true; | |
| 62 } | |
| 63 }; | |
| 64 | |
| 65 } | |
| 66 | |
| 67 #endif // COMPONENT_ARC_BITMAP_BITMAP_STRUCT_TRAITS_H_ | |
| OLD | NEW |