@@ -1056,13 +1056,10 @@ class Paint {
1056
1056
static const int _kStrokeJoinIndex = 6 ;
1057
1057
static const int _kStrokeMiterLimitIndex = 7 ;
1058
1058
static const int _kFilterQualityIndex = 8 ;
1059
- static const int _kColorFilterIndex = 9 ;
1060
- static const int _kColorFilterColorIndex = 10 ;
1061
- static const int _kColorFilterBlendModeIndex = 11 ;
1062
- static const int _kMaskFilterIndex = 12 ;
1063
- static const int _kMaskFilterBlurStyleIndex = 13 ;
1064
- static const int _kMaskFilterSigmaIndex = 14 ;
1065
- static const int _kInvertColorIndex = 15 ;
1059
+ static const int _kMaskFilterIndex = 9 ;
1060
+ static const int _kMaskFilterBlurStyleIndex = 10 ;
1061
+ static const int _kMaskFilterSigmaIndex = 11 ;
1062
+ static const int _kInvertColorIndex = 12 ;
1066
1063
1067
1064
static const int _kIsAntiAliasOffset = _kIsAntiAliasIndex << 2 ;
1068
1065
static const int _kColorOffset = _kColorIndex << 2 ;
@@ -1073,20 +1070,17 @@ class Paint {
1073
1070
static const int _kStrokeJoinOffset = _kStrokeJoinIndex << 2 ;
1074
1071
static const int _kStrokeMiterLimitOffset = _kStrokeMiterLimitIndex << 2 ;
1075
1072
static const int _kFilterQualityOffset = _kFilterQualityIndex << 2 ;
1076
- static const int _kColorFilterOffset = _kColorFilterIndex << 2 ;
1077
- static const int _kColorFilterColorOffset = _kColorFilterColorIndex << 2 ;
1078
- static const int _kColorFilterBlendModeOffset = _kColorFilterBlendModeIndex << 2 ;
1079
1073
static const int _kMaskFilterOffset = _kMaskFilterIndex << 2 ;
1080
1074
static const int _kMaskFilterBlurStyleOffset = _kMaskFilterBlurStyleIndex << 2 ;
1081
1075
static const int _kMaskFilterSigmaOffset = _kMaskFilterSigmaIndex << 2 ;
1082
1076
static const int _kInvertColorOffset = _kInvertColorIndex << 2 ;
1083
1077
// If you add more fields, remember to update _kDataByteCount.
1084
- static const int _kDataByteCount = 75 ;
1078
+ static const int _kDataByteCount = 52 ;
1085
1079
1086
1080
// Binary format must match the deserialization code in paint.cc.
1087
1081
List <dynamic > _objects;
1088
1082
static const int _kShaderIndex = 0 ;
1089
- static const int _kColorFilterMatrixIndex = 1 ;
1083
+ static const int _kColorFilterIndex = 1 ;
1090
1084
static const int _kImageFilterIndex = 2 ;
1091
1085
static const int _kObjectCount = 3 ; // Must be one larger than the largest index.
1092
1086
@@ -1342,48 +1336,23 @@ class Paint {
1342
1336
///
1343
1337
/// When a shape is being drawn, [colorFilter] overrides [color] and [shader] .
1344
1338
ColorFilter get colorFilter {
1345
- switch (_data.getInt32 (_kColorFilterOffset, _kFakeHostEndian)) {
1346
- case ColorFilter ._TypeNone :
1347
- return null ;
1348
- case ColorFilter ._TypeMode :
1349
- return ColorFilter .mode (
1350
- Color (_data.getInt32 (_kColorFilterColorOffset, _kFakeHostEndian)),
1351
- BlendMode .values[_data.getInt32 (_kColorFilterBlendModeOffset, _kFakeHostEndian)],
1352
- );
1353
- case ColorFilter ._TypeMatrix :
1354
- return ColorFilter .matrix (_objects[_kColorFilterMatrixIndex]);
1355
- case ColorFilter ._TypeLinearToSrgbGamma :
1356
- return const ColorFilter .linearToSrgbGamma ();
1357
- case ColorFilter ._TypeSrgbToLinearGamma :
1358
- return const ColorFilter .srgbToLinearGamma ();
1339
+ if (_objects == null || _objects[_kColorFilterIndex] == null ) {
1340
+ return null ;
1359
1341
}
1360
-
1361
- return null ;
1342
+ return _objects[_kColorFilterIndex].creator;
1362
1343
}
1363
1344
1364
1345
set colorFilter (ColorFilter value) {
1365
1346
if (value == null ) {
1366
- _data.setInt32 (_kColorFilterOffset, ColorFilter ._TypeNone , _kFakeHostEndian);
1367
- _data.setInt32 (_kColorFilterColorOffset, 0 , _kFakeHostEndian);
1368
- _data.setInt32 (_kColorFilterBlendModeOffset, 0 , _kFakeHostEndian);
1369
-
1370
1347
if (_objects != null ) {
1371
- _objects[_kColorFilterMatrixIndex ] = null ;
1348
+ _objects[_kColorFilterIndex ] = null ;
1372
1349
}
1373
1350
} else {
1374
- _data.setInt32 (_kColorFilterOffset, value._type, _kFakeHostEndian);
1375
-
1376
- if (value._type == ColorFilter ._TypeMode ) {
1377
- assert (value._color != null );
1378
- assert (value._blendMode != null );
1379
-
1380
- _data.setInt32 (_kColorFilterColorOffset, value._color.value, _kFakeHostEndian);
1381
- _data.setInt32 (_kColorFilterBlendModeOffset, value._blendMode.index, _kFakeHostEndian);
1382
- } else if (value._type == ColorFilter ._TypeMatrix ) {
1383
- assert (value._matrix != null );
1384
-
1385
- _objects ?? = List <dynamic >(_kObjectCount);
1386
- _objects[_kColorFilterMatrixIndex] = Float32List .fromList (value._matrix);
1351
+ if (_objects == null ) {
1352
+ _objects = List <dynamic >(_kObjectCount);
1353
+ _objects[_kColorFilterIndex] = value._toNativeColorFilter ();
1354
+ } else if (_objects[_kColorFilterIndex]? .creator != value) {
1355
+ _objects[_kColorFilterIndex] = value._toNativeColorFilter ();
1387
1356
}
1388
1357
}
1389
1358
}
@@ -2520,7 +2489,9 @@ class ColorFilter {
2520
2489
/// to the [Paint.blendMode] , using the output of this filter as the source
2521
2490
/// and the background as the destination.
2522
2491
const ColorFilter .mode (Color color, BlendMode blendMode)
2523
- : _color = color,
2492
+ : assert (color != null ),
2493
+ assert (blendMode != null ),
2494
+ _color = color,
2524
2495
_blendMode = blendMode,
2525
2496
_matrix = null ,
2526
2497
_type = _TypeMode ;
@@ -2529,7 +2500,9 @@ class ColorFilter {
2529
2500
/// matrix is in row-major order and the translation column is specified in
2530
2501
/// unnormalized, 0...255, space.
2531
2502
const ColorFilter .matrix (List <double > matrix)
2532
- : _color = null ,
2503
+ : assert (matrix != null ),
2504
+ assert (matrix.length == 20 ),
2505
+ _color = null ,
2533
2506
_blendMode = null ,
2534
2507
_matrix = matrix,
2535
2508
_type = _TypeMatrix ;
@@ -2580,6 +2553,21 @@ class ColorFilter {
2580
2553
return _color == typedOther._color && _blendMode == typedOther._blendMode;
2581
2554
}
2582
2555
2556
+ _ColorFilter _toNativeColorFilter () {
2557
+ switch (_type) {
2558
+ case _TypeMode :
2559
+ return _ColorFilter .mode (this );
2560
+ case _TypeMatrix :
2561
+ return _ColorFilter .matrix (this );
2562
+ case _TypeLinearToSrgbGamma :
2563
+ return _ColorFilter .linearToSrgbGamma (this );
2564
+ case _TypeSrgbToLinearGamma :
2565
+ return _ColorFilter .srgbToLinearGamma (this );
2566
+ default :
2567
+ throw StateError ('Unknown mode $_type for ColorFilter.' );
2568
+ }
2569
+ }
2570
+
2583
2571
@override
2584
2572
int get hashCode => hashValues (_color, _blendMode, hashList (_matrix), _type);
2585
2573
@@ -2600,6 +2588,51 @@ class ColorFilter {
2600
2588
}
2601
2589
}
2602
2590
2591
+ /// A [ColorFilter] that is backed by a native SkColorFilter.
2592
+ ///
2593
+ /// This is a private class, rather than being the implementation of the public
2594
+ /// ColorFilter, because we want ColorFilter to be const constructible and
2595
+ /// efficiently comparable, so that widgets can check for ColorFilter equality to
2596
+ // avoid repainting.
2597
+ class _ColorFilter extends NativeFieldWrapperClass2 {
2598
+ _ColorFilter .mode (this .creator)
2599
+ : assert (creator != null ),
2600
+ assert (creator._type == ColorFilter ._TypeMode ) {
2601
+ _constructor ();
2602
+ _initMode (creator._color.value, creator._blendMode.index);
2603
+ }
2604
+
2605
+ _ColorFilter .matrix (this .creator)
2606
+ : assert (creator != null ),
2607
+ assert (creator._type == ColorFilter ._TypeMatrix ) {
2608
+ _constructor ();
2609
+ _initMatrix (Float32List .fromList (creator._matrix));
2610
+ }
2611
+ _ColorFilter .linearToSrgbGamma (this .creator)
2612
+ : assert (creator != null ),
2613
+ assert (creator._type == ColorFilter ._TypeLinearToSrgbGamma ) {
2614
+ _constructor ();
2615
+ _initLinearToSrgbGamma ();
2616
+ }
2617
+
2618
+ _ColorFilter .srgbToLinearGamma (this .creator)
2619
+ : assert (creator != null ),
2620
+ assert (creator._type == ColorFilter ._TypeSrgbToLinearGamma ) {
2621
+ _constructor ();
2622
+ _initSrgbToLinearGamma ();
2623
+ }
2624
+
2625
+ /// The original Dart object that created the native wrapper, which retains
2626
+ /// the values used for the filter.
2627
+ final ColorFilter creator;
2628
+
2629
+ void _constructor () native 'ColorFilter_constructor' ;
2630
+ void _initMode (int color, int blendMode) native 'ColorFilter_initMode' ;
2631
+ void _initMatrix (Float32List matrix) native 'ColorFilter_initMatrix' ;
2632
+ void _initLinearToSrgbGamma () native 'ColorFilter_initLinearToSrgbGamma' ;
2633
+ void _initSrgbToLinearGamma () native 'ColorFilter_initSrgbToLinearGamma' ;
2634
+ }
2635
+
2603
2636
/// A filter operation to apply to a raster image.
2604
2637
///
2605
2638
/// See also:
0 commit comments