forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitmap_skbitmap_struct_traits.cc
217 lines (190 loc) · 6.38 KB
/
bitmap_skbitmap_struct_traits.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "skia/public/interfaces/bitmap_skbitmap_struct_traits.h"
namespace mojo {
namespace {
SkColorType MojoColorTypeToSk(skia::mojom::ColorType type) {
switch (type) {
case skia::mojom::ColorType::UNKNOWN:
return kUnknown_SkColorType;
case skia::mojom::ColorType::ALPHA_8:
return kAlpha_8_SkColorType;
case skia::mojom::ColorType::RGB_565:
return kRGB_565_SkColorType;
case skia::mojom::ColorType::ARGB_4444:
return kARGB_4444_SkColorType;
case skia::mojom::ColorType::RGBA_8888:
return kRGBA_8888_SkColorType;
case skia::mojom::ColorType::BGRA_8888:
return kBGRA_8888_SkColorType;
case skia::mojom::ColorType::INDEX_8:
return kIndex_8_SkColorType;
case skia::mojom::ColorType::GRAY_8:
return kGray_8_SkColorType;
}
NOTREACHED();
return kUnknown_SkColorType;
}
SkAlphaType MojoAlphaTypeToSk(skia::mojom::AlphaType type) {
switch (type) {
case skia::mojom::AlphaType::UNKNOWN:
return kUnknown_SkAlphaType;
case skia::mojom::AlphaType::ALPHA_TYPE_OPAQUE:
return kOpaque_SkAlphaType;
case skia::mojom::AlphaType::PREMUL:
return kPremul_SkAlphaType;
case skia::mojom::AlphaType::UNPREMUL:
return kUnpremul_SkAlphaType;
}
NOTREACHED();
return kUnknown_SkAlphaType;
}
sk_sp<SkColorSpace> MojoProfileTypeToSk(skia::mojom::ColorProfileType type) {
switch (type) {
case skia::mojom::ColorProfileType::LINEAR:
return nullptr;
case skia::mojom::ColorProfileType::SRGB:
return SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
}
NOTREACHED();
return nullptr;
}
skia::mojom::ColorType SkColorTypeToMojo(SkColorType type) {
switch (type) {
case kUnknown_SkColorType:
return skia::mojom::ColorType::UNKNOWN;
case kAlpha_8_SkColorType:
return skia::mojom::ColorType::ALPHA_8;
case kRGB_565_SkColorType:
return skia::mojom::ColorType::RGB_565;
case kARGB_4444_SkColorType:
return skia::mojom::ColorType::ARGB_4444;
case kRGBA_8888_SkColorType:
return skia::mojom::ColorType::RGBA_8888;
case kBGRA_8888_SkColorType:
return skia::mojom::ColorType::BGRA_8888;
case kIndex_8_SkColorType:
return skia::mojom::ColorType::INDEX_8;
case kGray_8_SkColorType:
return skia::mojom::ColorType::GRAY_8;
case kRGBA_F16_SkColorType:
NOTREACHED();
return skia::mojom::ColorType::UNKNOWN;
}
NOTREACHED();
return skia::mojom::ColorType::UNKNOWN;
}
skia::mojom::AlphaType SkAlphaTypeToMojo(SkAlphaType type) {
switch (type) {
case kUnknown_SkAlphaType:
return skia::mojom::AlphaType::UNKNOWN;
case kOpaque_SkAlphaType:
return skia::mojom::AlphaType::ALPHA_TYPE_OPAQUE;
case kPremul_SkAlphaType:
return skia::mojom::AlphaType::PREMUL;
case kUnpremul_SkAlphaType:
return skia::mojom::AlphaType::UNPREMUL;
}
NOTREACHED();
return skia::mojom::AlphaType::UNKNOWN;
}
skia::mojom::ColorProfileType SkColorSpaceToMojo(SkColorSpace* cs) {
if (cs && cs->gammaCloseToSRGB())
return skia::mojom::ColorProfileType::SRGB;
return skia::mojom::ColorProfileType::LINEAR;
}
} // namespace
// static
bool StructTraits<skia::mojom::BitmapDataView, SkBitmap>::IsNull(
const SkBitmap& b) {
return b.isNull();
}
// static
void StructTraits<skia::mojom::BitmapDataView, SkBitmap>::SetToNull(
SkBitmap* b) {
b->reset();
}
// static
skia::mojom::ColorType StructTraits<skia::mojom::BitmapDataView,
SkBitmap>::color_type(const SkBitmap& b) {
return SkColorTypeToMojo(b.colorType());
}
// static
skia::mojom::AlphaType StructTraits<skia::mojom::BitmapDataView,
SkBitmap>::alpha_type(const SkBitmap& b) {
return SkAlphaTypeToMojo(b.alphaType());
}
// static
skia::mojom::ColorProfileType
StructTraits<skia::mojom::BitmapDataView, SkBitmap>::profile_type(
const SkBitmap& b) {
return SkColorSpaceToMojo(b.colorSpace());
}
// static
uint32_t StructTraits<skia::mojom::BitmapDataView, SkBitmap>::width(
const SkBitmap& b) {
return b.width() < 0 ? 0 : static_cast<uint32_t>(b.width());
}
// static
uint32_t StructTraits<skia::mojom::BitmapDataView, SkBitmap>::height(
const SkBitmap& b) {
return b.height() < 0 ? 0 : static_cast<uint32_t>(b.height());
}
// static
uint64_t StructTraits<skia::mojom::BitmapDataView, SkBitmap>::row_bytes(
const SkBitmap& b) {
return b.rowBytes();
}
// static
BitmapBuffer StructTraits<skia::mojom::BitmapDataView, SkBitmap>::pixel_data(
const SkBitmap& b) {
return {b.getSize(), b.getSize(), static_cast<uint8_t*>(b.getPixels())};
}
// static
bool StructTraits<skia::mojom::BitmapDataView, SkBitmap>::Read(
skia::mojom::BitmapDataView data,
SkBitmap* b) {
// TODO: Ensure width and height are reasonable, eg. <= kMaxBitmapSize?
*b = SkBitmap();
if (!b->tryAllocPixels(
SkImageInfo::Make(data.width(), data.height(),
MojoColorTypeToSk(data.color_type()),
MojoAlphaTypeToSk(data.alpha_type()),
MojoProfileTypeToSk(data.profile_type())),
data.row_bytes())) {
return false;
}
// If the image is empty, return success after setting the image info.
if (data.width() == 0 || data.height() == 0)
return true;
SkAutoPixmapUnlock pixmap;
mojo::ArrayDataView<uint8_t> data_view;
data.GetPixelDataDataView(&data_view);
if (static_cast<uint32_t>(b->width()) != data.width() ||
static_cast<uint32_t>(b->height()) != data.height() ||
static_cast<uint64_t>(b->rowBytes()) != data.row_bytes() ||
b->getSize() != data_view.size() || !b->requestLock(&pixmap) ||
!b->readyToDraw()) {
return false;
}
BitmapBuffer bitmap_buffer = {0, b->getSize(),
static_cast<uint8_t*>(b->getPixels())};
if (!data.ReadPixelData(&bitmap_buffer) || bitmap_buffer.size != b->getSize())
return false;
b->notifyPixelsChanged();
return true;
}
// static
void* StructTraits<skia::mojom::BitmapDataView, SkBitmap>::SetUpContext(
const SkBitmap& b) {
b.lockPixels();
return nullptr;
}
// static
void StructTraits<skia::mojom::BitmapDataView, SkBitmap>::TearDownContext(
const SkBitmap& b,
void* context) {
b.unlockPixels();
}
} // namespace mojo