Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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/image/mojo/image_skia_struct_traits.h" | |
| 6 | |
| 7 #include <string.h> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "skia/public/interfaces/bitmap_skbitmap_struct_traits.h" | |
| 11 | |
| 12 namespace mojo { | |
| 13 | |
| 14 StructTraits<gfx::mojom::ImageSkiaRepDataView, | |
| 15 gfx::ImageSkiaRep>::Context::Context() = default; | |
| 16 StructTraits<gfx::mojom::ImageSkiaRepDataView, | |
| 17 gfx::ImageSkiaRep>::Context::~Context() = default; | |
| 18 | |
| 19 // static | |
| 20 void* StructTraits<gfx::mojom::ImageSkiaRepDataView, | |
| 21 gfx::ImageSkiaRep>::SetUpContext(const gfx::ImageSkiaRep& | |
| 22 input) { | |
| 23 // Shared buffer is not a good way for huge images. Consider alternatives. | |
| 24 DCHECK_LT(input.pixel_width() * input.pixel_height(), 4000 * 4000); | |
| 25 | |
| 26 const std::vector<uint8_t> serialized_sk_bitmap( | |
| 27 skia::mojom::Bitmap::Serialize(&(input.sk_bitmap()))); | |
| 28 | |
| 29 // Use a context to serialize bitmap to a shared buffer only once. | |
| 30 Context* context = new Context; | |
| 31 context->buffer_byte_size = serialized_sk_bitmap.size(); | |
| 32 context->shared_buffer_handle = | |
| 33 mojo::SharedBufferHandle::Create(context->buffer_byte_size); | |
| 34 mojo::ScopedSharedBufferMapping mapping = | |
| 35 context->shared_buffer_handle->Map(context->buffer_byte_size); | |
| 36 memcpy(mapping.get(), serialized_sk_bitmap.data(), context->buffer_byte_size); | |
| 37 return context; | |
| 38 } | |
| 39 | |
| 40 // static | |
| 41 void StructTraits<gfx::mojom::ImageSkiaRepDataView, gfx::ImageSkiaRep>:: | |
| 42 TearDownContext(const gfx::ImageSkiaRep& input, void* context) { | |
| 43 delete static_cast<Context*>(context); | |
| 44 } | |
| 45 | |
| 46 // static | |
| 47 mojo::ScopedSharedBufferHandle StructTraits< | |
| 48 gfx::mojom::ImageSkiaRepDataView, | |
| 49 gfx::ImageSkiaRep>::shared_buffer_handle(const gfx::ImageSkiaRep& input, | |
| 50 void* context) { | |
| 51 return (static_cast<Context*>(context)) | |
| 52 ->shared_buffer_handle->Clone( | |
| 53 mojo::SharedBufferHandle::AccessMode::READ_ONLY); | |
| 54 } | |
| 55 | |
| 56 // static | |
| 57 uint64_t StructTraits<gfx::mojom::ImageSkiaRepDataView, gfx::ImageSkiaRep>:: | |
| 58 buffer_byte_size(const gfx::ImageSkiaRep& input, void* context) { | |
| 59 return (static_cast<Context*>(context))->buffer_byte_size; | |
| 60 } | |
| 61 | |
| 62 // static | |
| 63 float StructTraits<gfx::mojom::ImageSkiaRepDataView, gfx::ImageSkiaRep>::scale( | |
| 64 const gfx::ImageSkiaRep& input) { | |
| 65 const float scale = input.unscaled() ? 0.0f : input.scale(); | |
| 66 DCHECK_GE(scale, 0.0f); | |
| 67 | |
| 68 return scale; | |
| 69 } | |
| 70 | |
| 71 // static | |
| 72 bool StructTraits<gfx::mojom::ImageSkiaRepDataView, gfx::ImageSkiaRep>::Read( | |
| 73 gfx::mojom::ImageSkiaRepDataView data, | |
| 74 gfx::ImageSkiaRep* out) { | |
| 75 // An acceptable scale must be greater or equal to 0. | |
|
msw
2017/03/23 20:47:53
nit: "greater than"
xiyuan
2017/03/24 05:45:31
Done.
| |
| 76 if (data.scale() < 0) | |
| 77 return false; | |
| 78 | |
| 79 mojo::ScopedSharedBufferHandle shared_buffer_handle = | |
| 80 data.TakeSharedBufferHandle(); | |
| 81 if (!shared_buffer_handle.is_valid()) | |
| 82 return false; | |
| 83 | |
| 84 mojo::ScopedSharedBufferMapping mapping = | |
| 85 shared_buffer_handle->Map(data.buffer_byte_size()); | |
| 86 if (!mapping) | |
| 87 return false; | |
| 88 | |
| 89 const std::vector<uint8_t> serialized_sk_bitmap( | |
| 90 static_cast<uint8_t*>(mapping.get()), | |
| 91 static_cast<uint8_t*>(mapping.get()) + data.buffer_byte_size()); | |
| 92 | |
| 93 SkBitmap bitmap; | |
| 94 if (!skia::mojom::Bitmap::Deserialize(serialized_sk_bitmap, &bitmap)) | |
| 95 return false; | |
| 96 | |
| 97 *out = gfx::ImageSkiaRep(bitmap, data.scale()); | |
| 98 return true; | |
| 99 } | |
| 100 | |
| 101 // static | |
| 102 void* StructTraits<gfx::mojom::ImageSkiaDataView, gfx::ImageSkia>::SetUpContext( | |
| 103 const gfx::ImageSkia& input) { | |
| 104 // Use a context to return a stable list of ImageSkiaRep objects. That is, | |
| 105 // multiple calls of image_reps() should return exactly the same list of | |
| 106 // ImageSkiaRep objects. So that ImageSkiaRep with the same backing pixel | |
| 107 // buffer is properly serialized and only once. | |
| 108 auto* image_reps = new std::vector<gfx::ImageSkiaRep>; | |
| 109 | |
| 110 // Strip out empty image reps. Needed because ImageSkia::image_reps does not | |
| 111 // filter out empty SkBitmap. | |
| 112 for (auto& input_rep : input.image_reps()) { | |
| 113 if (!StructTraits<gfx::mojom::ImageSkiaRepDataView, | |
| 114 gfx::ImageSkiaRep>::IsNull(input_rep)) { | |
| 115 image_reps->emplace_back(input_rep); | |
| 116 } | |
| 117 } | |
| 118 return image_reps; | |
| 119 } | |
| 120 | |
| 121 // static | |
| 122 void StructTraits<gfx::mojom::ImageSkiaDataView, | |
| 123 gfx::ImageSkia>::TearDownContext(const gfx::ImageSkia& input, | |
| 124 void* context) { | |
| 125 delete static_cast<std::vector<gfx::ImageSkiaRep>*>(context); | |
| 126 } | |
| 127 | |
| 128 // static | |
| 129 const std::vector<gfx::ImageSkiaRep>& | |
| 130 StructTraits<gfx::mojom::ImageSkiaDataView, gfx::ImageSkia>::image_reps( | |
| 131 const gfx::ImageSkia& input, | |
| 132 void* context) { | |
| 133 return *(static_cast<std::vector<gfx::ImageSkiaRep>*>(context)); | |
| 134 } | |
| 135 | |
| 136 // static | |
| 137 bool StructTraits<gfx::mojom::ImageSkiaDataView, gfx::ImageSkia>::Read( | |
| 138 gfx::mojom::ImageSkiaDataView data, | |
| 139 gfx::ImageSkia* out) { | |
| 140 DCHECK(out->isNull()); | |
| 141 | |
| 142 std::vector<gfx::ImageSkiaRep> image_reps; | |
| 143 if (!data.ReadImageReps(&image_reps)) | |
| 144 return false; | |
| 145 | |
| 146 for (const auto& image_rep : image_reps) | |
| 147 out->AddRepresentation(image_rep); | |
| 148 | |
| 149 if (out->isNull()) | |
| 150 return false; | |
| 151 | |
| 152 out->SetReadOnly(); | |
| 153 return true; | |
| 154 } | |
| 155 | |
| 156 } // namespace mojo | |
| OLD | NEW |