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 mojo::Array<uint8_t> pixel_data(const SkBitmap& r) { | |
| 16 if (r.drawsNothing()) | |
| 17 return mojo::Array<uint8_t>(); | |
|
dcheng
2016/09/07 22:47:17
These methods are pretty complex. We should out-of
yoshiki
2016/09/09 16:45:30
Made this simpler. Is it ok?
| |
| 18 | |
| 19 const SkImageInfo& info = r.info(); | |
| 20 if (info.colorType() != kRGBA_8888_SkColorType) | |
| 21 return mojo::Array<uint8_t>(); | |
| 22 | |
| 23 mojo::Array<uint8_t> pixel_data(r.getSize()); | |
| 24 if (!r.readPixels(info, &pixel_data[0], r.rowBytes(), 0, 0)) | |
| 25 return mojo::Array<uint8_t>(); | |
| 26 | |
| 27 return pixel_data; | |
| 28 } | |
| 29 | |
| 30 static uint32_t width(const SkBitmap& r) { | |
| 31 return r.width(); | |
| 32 } | |
| 33 | |
| 34 static uint32_t height(const SkBitmap& r) { | |
| 35 return r.height(); | |
| 36 } | |
| 37 | |
| 38 static bool Read(arc::mojom::ArcBitmapDataView data, SkBitmap* out) { | |
|
dcheng
2016/09/07 22:47:17
SkBitmap is an out param. In effect, Read() is cal
yoshiki
2016/09/09 16:45:30
I was misunderstood. Fixed.
| |
| 39 mojo::Array<uint8_t> pixel_data; | |
| 40 if (!data.ReadPixelData(&pixel_data)) { | |
| 41 // Can't read the pixel data. | |
| 42 out = new SkBitmap(); | |
| 43 return false; | |
| 44 } | |
| 45 | |
| 46 if (data.width() == 0 || data.height() == 0) { | |
| 47 // Valid empty data. | |
| 48 out = new SkBitmap(); | |
| 49 return true; | |
| 50 } | |
| 51 | |
| 52 SkImageInfo info = SkImageInfo::Make( | |
| 53 data.width(), data.height(), | |
| 54 kRGBA_8888_SkColorType, kPremul_SkAlphaType); | |
| 55 if (info.getSafeSize(info.minRowBytes()) > pixel_data.size()) { | |
| 56 // Insufficient buffer size. | |
| 57 out = new SkBitmap(); | |
|
xiyuan
2016/09/07 22:09:00
This looks wrong. It would leak and created SkBitm
yoshiki
2016/09/09 16:45:30
Good catch. Let me use out->reset() here.
| |
| 58 return false; | |
| 59 } | |
| 60 | |
| 61 // Create the SkBitmap object which wraps the arc bitmap pixels. | |
| 62 SkBitmap bitmap; | |
| 63 if (!bitmap.installPixels(info, | |
| 64 const_cast<uint8_t*>(pixel_data.storage().data()), | |
| 65 info.minRowBytes())) { | |
| 66 // Error in installing pixels. | |
| 67 out = new SkBitmap(); | |
|
xiyuan
2016/09/07 22:09:00
ditto
yoshiki
2016/09/09 16:45:30
Done.
| |
| 68 return false; | |
| 69 } | |
| 70 | |
| 71 // Copy the pixels with converting color type. | |
| 72 if (!bitmap.copyTo(out, kN32_SkColorType)) { | |
| 73 // Error in copying. | |
| 74 out = new SkBitmap(); | |
|
xiyuan
2016/09/07 22:09:00
ditto
yoshiki
2016/09/09 16:45:30
Done.
| |
| 75 return false; | |
| 76 } | |
| 77 | |
| 78 return true; | |
| 79 } | |
| 80 }; | |
| 81 | |
| 82 } | |
| 83 | |
| 84 #endif // COMPONENT_ARC_BITMAP_BITMAP_STRUCT_TRAITS_H_ | |
| OLD | NEW |