This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Expand file tree
/
Copy pathpaint.cc
More file actions
330 lines (276 loc) · 11.1 KB
/
Copy pathpaint.cc
File metadata and controls
330 lines (276 loc) · 11.1 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Copyright 2013 The Flutter 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 "flutter/lib/ui/painting/paint.h"
#include "flutter/display_list/dl_builder.h"
#include "flutter/fml/logging.h"
#include "flutter/lib/ui/floating_point.h"
#include "flutter/lib/ui/painting/color_filter.h"
#include "flutter/lib/ui/painting/image_filter.h"
#include "flutter/lib/ui/painting/shader.h"
#include "third_party/skia/include/core/SkColorFilter.h"
#include "third_party/skia/include/core/SkImageFilter.h"
#include "third_party/skia/include/core/SkMaskFilter.h"
#include "third_party/skia/include/core/SkShader.h"
#include "third_party/skia/include/core/SkString.h"
#include "third_party/tonic/typed_data/dart_byte_data.h"
#include "third_party/tonic/typed_data/typed_list.h"
namespace flutter {
// Indices for 32bit values.
// Must match //lib/ui/painting.dart.
constexpr int kIsAntiAliasIndex = 0;
constexpr int kColorRedIndex = 1;
constexpr int kColorGreenIndex = 2;
constexpr int kColorBlueIndex = 3;
constexpr int kColorAlphaIndex = 4;
constexpr int kColorSpaceIndex = 5;
constexpr int kBlendModeIndex = 6;
constexpr int kStyleIndex = 7;
constexpr int kStrokeWidthIndex = 8;
constexpr int kStrokeCapIndex = 9;
constexpr int kStrokeJoinIndex = 10;
constexpr int kStrokeMiterLimitIndex = 11;
constexpr int kFilterQualityIndex = 12;
constexpr int kMaskFilterIndex = 13;
constexpr int kMaskFilterBlurStyleIndex = 14;
constexpr int kMaskFilterSigmaIndex = 15;
constexpr int kInvertColorIndex = 16;
constexpr size_t kDataByteCount = 68; // 4 * (last index + 1)
static_assert(kDataByteCount == sizeof(uint32_t) * (kInvertColorIndex + 1),
"kDataByteCount must match the size of the data array.");
// Indices for objects.
constexpr int kShaderIndex = 0;
constexpr int kColorFilterIndex = 1;
constexpr int kImageFilterIndex = 2;
constexpr int kObjectCount = 3; // One larger than largest object index.
// Must be kept in sync with the default in painting.dart.
constexpr uint32_t kBlendModeDefault =
static_cast<uint32_t>(SkBlendMode::kSrcOver);
// Must be kept in sync with the default in painting.dart, and also with the
// default SkPaintDefaults_MiterLimit in Skia (which is not in a public header).
constexpr float kStrokeMiterLimitDefault = 4.0f;
// Must be kept in sync with the MaskFilter private constants in painting.dart.
enum MaskFilterType { kNull, kBlur };
namespace {
DlColor ReadColor(const tonic::DartByteData& byte_data) {
const uint32_t* uint_data = static_cast<const uint32_t*>(byte_data.data());
const float* float_data = static_cast<const float*>(byte_data.data());
float red = float_data[kColorRedIndex];
float green = float_data[kColorGreenIndex];
float blue = float_data[kColorBlueIndex];
// Invert alpha so 0 initialized buffer has default value;
float alpha = 1.f - float_data[kColorAlphaIndex];
uint32_t colorspace = uint_data[kColorSpaceIndex];
(void)colorspace;
uint32_t encoded_color =
static_cast<uint8_t>(std::round(alpha * 255.f)) << 24 | //
static_cast<uint8_t>(std::round(red * 255.f)) << 16 | //
static_cast<uint8_t>(std::round(green * 255.f)) << 8 | //
static_cast<uint8_t>(std::round(blue * 255.f)) << 0;
// TODO(gaaclarke): Pass down color info to DlColor.
return DlColor(encoded_color);
}
} // namespace
Paint::Paint(Dart_Handle paint_objects, Dart_Handle paint_data)
: paint_objects_(paint_objects), paint_data_(paint_data) {}
const DlPaint* Paint::paint(DlPaint& paint,
const DisplayListAttributeFlags& flags) const {
if (isNull()) {
return nullptr;
}
tonic::DartByteData byte_data(paint_data_);
FML_CHECK(byte_data.length_in_bytes() == kDataByteCount);
const uint32_t* uint_data = static_cast<const uint32_t*>(byte_data.data());
const float* float_data = static_cast<const float*>(byte_data.data());
Dart_Handle values[kObjectCount];
if (Dart_IsNull(paint_objects_)) {
if (flags.applies_shader()) {
paint.setColorSource(nullptr);
}
if (flags.applies_color_filter()) {
paint.setColorFilter(nullptr);
}
if (flags.applies_image_filter()) {
paint.setImageFilter(nullptr);
}
} else {
FML_DCHECK(Dart_IsList(paint_objects_));
intptr_t length = 0;
Dart_ListLength(paint_objects_, &length);
FML_CHECK(length == kObjectCount);
if (Dart_IsError(
Dart_ListGetRange(paint_objects_, 0, kObjectCount, values))) {
return nullptr;
}
if (flags.applies_shader()) {
Dart_Handle shader = values[kShaderIndex];
if (Dart_IsNull(shader)) {
paint.setColorSource(nullptr);
} else {
if (Shader* decoded = tonic::DartConverter<Shader*>::FromDart(shader)) {
auto sampling =
ImageFilter::SamplingFromIndex(uint_data[kFilterQualityIndex]);
paint.setColorSource(decoded->shader(sampling));
} else {
paint.setColorSource(nullptr);
}
}
}
if (flags.applies_color_filter()) {
Dart_Handle color_filter = values[kColorFilterIndex];
if (Dart_IsNull(color_filter)) {
paint.setColorFilter(nullptr);
} else {
ColorFilter* decoded =
tonic::DartConverter<ColorFilter*>::FromDart(color_filter);
paint.setColorFilter(decoded->filter());
}
}
if (flags.applies_image_filter()) {
Dart_Handle image_filter = values[kImageFilterIndex];
if (Dart_IsNull(image_filter)) {
paint.setImageFilter(nullptr);
} else {
ImageFilter* decoded =
tonic::DartConverter<ImageFilter*>::FromDart(image_filter);
paint.setImageFilter(decoded->filter());
}
}
}
if (flags.applies_anti_alias()) {
paint.setAntiAlias(uint_data[kIsAntiAliasIndex] == 0);
}
if (flags.applies_alpha_or_color()) {
paint.setColor(ReadColor(byte_data));
}
if (flags.applies_blend()) {
uint32_t encoded_blend_mode = uint_data[kBlendModeIndex];
uint32_t blend_mode = encoded_blend_mode ^ kBlendModeDefault;
paint.setBlendMode(static_cast<DlBlendMode>(blend_mode));
}
if (flags.applies_style()) {
uint32_t style = uint_data[kStyleIndex];
paint.setDrawStyle(static_cast<DlDrawStyle>(style));
}
if (flags.is_stroked(paint.getDrawStyle())) {
float stroke_width = float_data[kStrokeWidthIndex];
paint.setStrokeWidth(stroke_width);
float stroke_miter_limit = float_data[kStrokeMiterLimitIndex];
paint.setStrokeMiter(stroke_miter_limit + kStrokeMiterLimitDefault);
uint32_t stroke_cap = uint_data[kStrokeCapIndex];
paint.setStrokeCap(static_cast<DlStrokeCap>(stroke_cap));
uint32_t stroke_join = uint_data[kStrokeJoinIndex];
paint.setStrokeJoin(static_cast<DlStrokeJoin>(stroke_join));
}
if (flags.applies_color_filter()) {
paint.setInvertColors(uint_data[kInvertColorIndex] != 0);
}
if (flags.applies_mask_filter()) {
switch (uint_data[kMaskFilterIndex]) {
case kNull:
paint.setMaskFilter(nullptr);
break;
case kBlur:
DlBlurStyle blur_style =
static_cast<DlBlurStyle>(uint_data[kMaskFilterBlurStyleIndex]);
double sigma = float_data[kMaskFilterSigmaIndex];
paint.setMaskFilter(
DlBlurMaskFilter::Make(blur_style, SafeNarrow(sigma)));
break;
}
}
return &paint;
}
void Paint::toDlPaint(DlPaint& paint) const {
if (isNull()) {
return;
}
FML_DCHECK(paint == DlPaint());
tonic::DartByteData byte_data(paint_data_);
FML_CHECK(byte_data.length_in_bytes() == kDataByteCount);
const uint32_t* uint_data = static_cast<const uint32_t*>(byte_data.data());
const float* float_data = static_cast<const float*>(byte_data.data());
Dart_Handle values[kObjectCount];
if (!Dart_IsNull(paint_objects_)) {
FML_DCHECK(Dart_IsList(paint_objects_));
intptr_t length = 0;
Dart_ListLength(paint_objects_, &length);
FML_CHECK(length == kObjectCount);
if (Dart_IsError(
Dart_ListGetRange(paint_objects_, 0, kObjectCount, values))) {
return;
}
Dart_Handle shader = values[kShaderIndex];
if (!Dart_IsNull(shader)) {
if (Shader* decoded = tonic::DartConverter<Shader*>::FromDart(shader)) {
auto sampling =
ImageFilter::SamplingFromIndex(uint_data[kFilterQualityIndex]);
paint.setColorSource(decoded->shader(sampling));
}
}
Dart_Handle color_filter = values[kColorFilterIndex];
if (!Dart_IsNull(color_filter)) {
ColorFilter* decoded =
tonic::DartConverter<ColorFilter*>::FromDart(color_filter);
paint.setColorFilter(decoded->filter());
}
Dart_Handle image_filter = values[kImageFilterIndex];
if (!Dart_IsNull(image_filter)) {
ImageFilter* decoded =
tonic::DartConverter<ImageFilter*>::FromDart(image_filter);
paint.setImageFilter(decoded->filter());
}
}
paint.setAntiAlias(uint_data[kIsAntiAliasIndex] == 0);
paint.setColor(ReadColor(byte_data));
uint32_t encoded_blend_mode = uint_data[kBlendModeIndex];
uint32_t blend_mode = encoded_blend_mode ^ kBlendModeDefault;
paint.setBlendMode(static_cast<DlBlendMode>(blend_mode));
uint32_t style = uint_data[kStyleIndex];
paint.setDrawStyle(static_cast<DlDrawStyle>(style));
float stroke_width = float_data[kStrokeWidthIndex];
paint.setStrokeWidth(stroke_width);
float stroke_miter_limit = float_data[kStrokeMiterLimitIndex];
paint.setStrokeMiter(stroke_miter_limit + kStrokeMiterLimitDefault);
uint32_t stroke_cap = uint_data[kStrokeCapIndex];
paint.setStrokeCap(static_cast<DlStrokeCap>(stroke_cap));
uint32_t stroke_join = uint_data[kStrokeJoinIndex];
paint.setStrokeJoin(static_cast<DlStrokeJoin>(stroke_join));
paint.setInvertColors(uint_data[kInvertColorIndex] != 0);
switch (uint_data[kMaskFilterIndex]) {
case kNull:
break;
case kBlur:
DlBlurStyle blur_style =
static_cast<DlBlurStyle>(uint_data[kMaskFilterBlurStyleIndex]);
float sigma = SafeNarrow(float_data[kMaskFilterSigmaIndex]);
// Make could return a nullptr here if the values are NOP or
// do not make sense. We could interpret that as if there was
// no value passed from Dart at all (i.e. don't change the
// setting in the paint object as in the kNull branch right
// above here), but the maskfilter flag was actually set
// indicating that the developer "tried" to set a mask, so we
// should set the null value rather than do nothing.
paint.setMaskFilter(DlBlurMaskFilter::Make(blur_style, sigma));
break;
}
}
} // namespace flutter
namespace tonic {
flutter::Paint DartConverter<flutter::Paint>::FromArguments(
Dart_NativeArguments args,
int index,
Dart_Handle& exception) {
Dart_Handle paint_objects = Dart_GetNativeArgument(args, index);
FML_DCHECK(!CheckAndHandleError(paint_objects));
Dart_Handle paint_data = Dart_GetNativeArgument(args, index + 1);
FML_DCHECK(!CheckAndHandleError(paint_data));
return flutter::Paint(paint_objects, paint_data);
}
flutter::PaintData DartConverter<flutter::PaintData>::FromArguments(
Dart_NativeArguments args,
int index,
Dart_Handle& exception) {
return flutter::PaintData();
}
} // namespace tonic