Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(185)

Side by Side Diff: ui/gfx/image/mojo/image_skia_struct_traits.cc

Issue 2770693002: ash: HiDPI user avatar for SessionController (Closed)
Patch Set: nits, and gn check Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 return new std::vector<gfx::ImageSkiaRep>(input.image_reps());
120 }
121
122 // static
123 void StructTraits<gfx::mojom::ImageSkiaDataView,
124 gfx::ImageSkia>::TearDownContext(const gfx::ImageSkia& input,
125 void* context) {
126 delete static_cast<std::vector<gfx::ImageSkiaRep>*>(context);
127 }
128
129 // static
130 const std::vector<gfx::ImageSkiaRep>&
131 StructTraits<gfx::mojom::ImageSkiaDataView, gfx::ImageSkia>::image_reps(
132 const gfx::ImageSkia& input,
133 void* context) {
134 // See the comment in SetUpContext regarding context usage.
135 return *(static_cast<std::vector<gfx::ImageSkiaRep>*>(context));
136 }
137
138 // static
139 bool StructTraits<gfx::mojom::ImageSkiaDataView, gfx::ImageSkia>::Read(
140 gfx::mojom::ImageSkiaDataView data,
141 gfx::ImageSkia* out) {
142 DCHECK(out->isNull());
143
144 std::vector<gfx::ImageSkiaRep> image_reps;
145 if (!data.ReadImageReps(&image_reps))
146 return false;
147
148 for (const auto& image_rep : image_reps)
149 out->AddRepresentation(image_rep);
150
151 if (out->isNull())
152 return false;
153
154 out->SetReadOnly();
155 return true;
156 }
157
158 } // namespace mojo
OLDNEW
« no previous file with comments | « ui/gfx/image/mojo/image_skia_struct_traits.h ('k') | ui/gfx/image/mojo/image_traits_test_service.mojom » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698