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 const std::vector<uint8_t> serialized_sk_bitmap( | |
24 skia::mojom::Bitmap::Serialize(&(input.sk_bitmap()))); | |
msw
2017/03/23 20:47:51
q: Do we need to make read only or make thread saf
xiyuan
2017/03/24 05:45:20
We dont' need to. A SkBitmap in ImageSkiaRep is im
| |
25 | |
26 // Use a context to serialize bitmap to a shared buffer only once. | |
msw
2017/03/23 20:47:51
nit: "the bitmap"
xiyuan
2017/03/24 05:45:20
Done.
| |
27 Context* context = new Context; | |
28 context->buffer_byte_size = serialized_sk_bitmap.size(); | |
29 context->shared_buffer_handle = | |
30 mojo::SharedBufferHandle::Create(context->buffer_byte_size); | |
31 mojo::ScopedSharedBufferMapping mapping = | |
32 context->shared_buffer_handle->Map(context->buffer_byte_size); | |
33 memcpy(mapping.get(), serialized_sk_bitmap.data(), context->buffer_byte_size); | |
msw
2017/03/23 20:47:51
hmm, I would have thought we could avoid a memcpy
xiyuan
2017/03/24 05:45:20
I would love to do that as well but not sure how f
| |
34 return context; | |
35 } | |
36 | |
37 // static | |
38 void StructTraits<gfx::mojom::ImageSkiaRepDataView, gfx::ImageSkiaRep>:: | |
39 TearDownContext(const gfx::ImageSkiaRep& input, void* context) { | |
40 delete static_cast<Context*>(context); | |
41 } | |
42 | |
43 // static | |
44 mojo::ScopedSharedBufferHandle StructTraits< | |
45 gfx::mojom::ImageSkiaRepDataView, | |
46 gfx::ImageSkiaRep>::shared_buffer_handle(const gfx::ImageSkiaRep& input, | |
47 void* context) { | |
48 return (static_cast<Context*>(context)) | |
49 ->shared_buffer_handle->Clone( | |
50 mojo::SharedBufferHandle::AccessMode::READ_ONLY); | |
51 } | |
52 | |
53 // static | |
54 uint32_t StructTraits<gfx::mojom::ImageSkiaRepDataView, gfx::ImageSkiaRep>:: | |
55 buffer_byte_size(const gfx::ImageSkiaRep& input, void* context) { | |
56 return (static_cast<Context*>(context))->buffer_byte_size; | |
57 } | |
58 | |
59 // static | |
60 bool StructTraits<gfx::mojom::ImageSkiaRepDataView, gfx::ImageSkiaRep>::Read( | |
61 gfx::mojom::ImageSkiaRepDataView data, | |
62 gfx::ImageSkiaRep* out) { | |
63 mojo::ScopedSharedBufferHandle shared_buffer_handle = | |
64 data.TakeSharedBufferHandle(); | |
65 if (!shared_buffer_handle.is_valid()) | |
66 return false; | |
67 | |
68 mojo::ScopedSharedBufferMapping mapping = | |
69 shared_buffer_handle->Map(data.buffer_byte_size()); | |
70 if (!mapping) | |
71 return false; | |
72 | |
73 const std::vector<uint8_t> serialized_sk_bitmap( | |
74 static_cast<uint8_t*>(mapping.get()), | |
75 static_cast<uint8_t*>(mapping.get()) + data.buffer_byte_size()); | |
76 | |
77 SkBitmap bitmap; | |
78 if (!skia::mojom::Bitmap::Deserialize(serialized_sk_bitmap, &bitmap)) | |
msw
2017/03/23 20:47:52
Again, I'm wondering if there's a more performance
xiyuan
2017/03/24 05:45:20
Yes, I am not happy about this too. :(
Right now,
| |
79 return false; | |
80 | |
81 *out = gfx::ImageSkiaRep(bitmap, data.scale()); | |
82 return true; | |
83 } | |
84 | |
85 // static | |
86 void* StructTraits<gfx::mojom::ImageSkiaDataView, gfx::ImageSkia>::SetUpContext( | |
87 const gfx::ImageSkia& input) { | |
88 // Use a context to return a stable list of ImageSkiaRep objects. That is, | |
89 // multiple calls of image_reps() should return exactly the same list of | |
90 // ImageSkiaRep objects. So that ImageSkiaRep with the same backing pixel | |
91 // buffer is properly serialized and only once. | |
92 auto* image_reps = new std::vector<gfx::ImageSkiaRep>; | |
93 | |
94 // Strip out empty image reps. Needed because ImageSkia::image_reps does not | |
95 // filter out empty SkBitmap. | |
96 for (auto& input_rep : input.image_reps()) { | |
97 if (!StructTraits<gfx::mojom::ImageSkiaRepDataView, | |
98 gfx::ImageSkiaRep>::IsNull(input_rep)) { | |
99 image_reps->emplace_back(input_rep); | |
100 } | |
101 } | |
102 return image_reps; | |
103 } | |
104 | |
105 // static | |
106 void StructTraits<gfx::mojom::ImageSkiaDataView, | |
107 gfx::ImageSkia>::TearDownContext(const gfx::ImageSkia& input, | |
108 void* context) { | |
109 delete static_cast<std::vector<gfx::ImageSkiaRep>*>(context); | |
110 } | |
111 | |
112 // static | |
113 const std::vector<gfx::ImageSkiaRep>& | |
114 StructTraits<gfx::mojom::ImageSkiaDataView, gfx::ImageSkia>::image_reps( | |
115 const gfx::ImageSkia& input, | |
116 void* context) { | |
117 return *(static_cast<std::vector<gfx::ImageSkiaRep>*>(context)); | |
msw
2017/03/23 20:47:52
optional nit: // See the comment in SetUpContext r
xiyuan
2017/03/24 05:45:20
Done.
| |
118 } | |
119 | |
120 // static | |
121 bool StructTraits<gfx::mojom::ImageSkiaDataView, gfx::ImageSkia>::Read( | |
122 gfx::mojom::ImageSkiaDataView data, | |
123 gfx::ImageSkia* out) { | |
124 DCHECK(out->isNull()); | |
125 | |
126 std::vector<gfx::ImageSkiaRep> image_reps; | |
127 if (!data.ReadImageReps(&image_reps)) | |
128 return false; | |
129 | |
130 for (const auto& image_rep : image_reps) | |
131 out->AddRepresentation(image_rep); | |
132 | |
133 if (out->isNull()) | |
134 return false; | |
135 | |
136 out->SetReadOnly(); | |
137 return true; | |
138 } | |
139 | |
140 } // namespace mojo | |
OLD | NEW |