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