OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/image/mojo/image_skia_struct_traits.h" | 5 #include "ui/gfx/image/mojo/image_skia_struct_traits.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "skia/public/interfaces/bitmap_skbitmap_struct_traits.h" | 10 #include "skia/public/interfaces/bitmap_skbitmap_struct_traits.h" |
11 | 11 |
12 namespace mojo { | 12 namespace mojo { |
13 | 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 | 14 // static |
85 float StructTraits<gfx::mojom::ImageSkiaRepDataView, gfx::ImageSkiaRep>::scale( | 15 float StructTraits<gfx::mojom::ImageSkiaRepDataView, gfx::ImageSkiaRep>::scale( |
86 const gfx::ImageSkiaRep& input) { | 16 const gfx::ImageSkiaRep& input) { |
87 const float scale = input.unscaled() ? 0.0f : input.scale(); | 17 const float scale = input.unscaled() ? 0.0f : input.scale(); |
88 DCHECK_GE(scale, 0.0f); | 18 DCHECK_GE(scale, 0.0f); |
89 | 19 |
90 return scale; | 20 return scale; |
91 } | 21 } |
92 | 22 |
93 // static | 23 // static |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 out->AddRepresentation(image_rep); | 79 out->AddRepresentation(image_rep); |
150 | 80 |
151 if (out->isNull()) | 81 if (out->isNull()) |
152 return false; | 82 return false; |
153 | 83 |
154 out->SetReadOnly(); | 84 out->SetReadOnly(); |
155 return true; | 85 return true; |
156 } | 86 } |
157 | 87 |
158 } // namespace mojo | 88 } // namespace mojo |
OLD | NEW |