Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 8c6984e

Browse files
author
Dwayne Slater
committed
Add ability to control dithering with Paint
1 parent 97df087 commit 8c6984e

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

lib/ui/painting.dart

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,7 @@ class Paint {
10581058
static const int _kMaskFilterBlurStyleIndex = 10;
10591059
static const int _kMaskFilterSigmaIndex = 11;
10601060
static const int _kInvertColorIndex = 12;
1061+
static const int _kDitherIndex = 13;
10611062

10621063
static const int _kIsAntiAliasOffset = _kIsAntiAliasIndex << 2;
10631064
static const int _kColorOffset = _kColorIndex << 2;
@@ -1072,8 +1073,9 @@ class Paint {
10721073
static const int _kMaskFilterBlurStyleOffset = _kMaskFilterBlurStyleIndex << 2;
10731074
static const int _kMaskFilterSigmaOffset = _kMaskFilterSigmaIndex << 2;
10741075
static const int _kInvertColorOffset = _kInvertColorIndex << 2;
1076+
static const int _kDitherOffset = _kDitherIndex << 2;
10751077
// If you add more fields, remember to update _kDataByteCount.
1076-
static const int _kDataByteCount = 52;
1078+
static const int _kDataByteCount = 56;
10771079

10781080
// Binary format must match the deserialization code in paint.cc.
10791081
List<dynamic> _objects;
@@ -1401,6 +1403,25 @@ class Paint {
14011403
_data.setInt32(_kInvertColorOffset, value ? 1 : 0, _kFakeHostEndian);
14021404
}
14031405

1406+
/// Whether to dither the output when drawing images.
1407+
///
1408+
/// If false, the default value, dithering will be enabled when the input
1409+
/// color depth is higher than the output color depth. For example,
1410+
/// drawing an RGB8 image onto an RGB565 canvas.
1411+
///
1412+
/// This value also controls dithering of [shader]s, which can make
1413+
/// gradients appear smoother.
1414+
///
1415+
/// Whether or not dithering affects the output is implementation defined.
1416+
/// Some implementations may choose to ignore this completely, if they're
1417+
/// unable to control dithering.
1418+
bool get dither {
1419+
return _data.getInt32(_kDitherOffset, _kFakeHostEndian) == 1;
1420+
}
1421+
set dither(bool value) {
1422+
_data.setInt32(_kDitherOffset, value ? 1 : 0, _kFakeHostEndian);
1423+
}
1424+
14041425
@override
14051426
String toString() {
14061427
final StringBuffer result = StringBuffer();
@@ -1459,6 +1480,8 @@ class Paint {
14591480
}
14601481
if (invertColors)
14611482
result.write('${semicolon}invert: $invertColors');
1483+
if (dither)
1484+
result.write('${semicolon}dither: $dither');
14621485
result.write(')');
14631486
return result.toString();
14641487
}

lib/ui/painting/paint.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ constexpr int kMaskFilterIndex = 9;
3232
constexpr int kMaskFilterBlurStyleIndex = 10;
3333
constexpr int kMaskFilterSigmaIndex = 11;
3434
constexpr int kInvertColorIndex = 12;
35-
constexpr size_t kDataByteCount = 52; // 4 * (last index + 1)
35+
constexpr int kDitherIndex = 13;
36+
constexpr size_t kDataByteCount = 56; // 4 * (last index + 1)
3637

3738
// Indices for objects.
3839
constexpr int kShaderIndex = 0;
@@ -154,6 +155,10 @@ Paint::Paint(Dart_Handle paint_objects, Dart_Handle paint_data) {
154155
paint_.setColorFilter(invert_filter);
155156
}
156157

158+
if (uint_data[kDitherIndex]) {
159+
paint_.setDither(true);
160+
}
161+
157162
switch (uint_data[kMaskFilterIndex]) {
158163
case Null:
159164
break;

lib/web_ui/lib/src/ui/painting.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,21 @@ class Paint {
11741174
// TODO(flutter/flutter#35156): Implement ImageFilter.
11751175
}
11761176

1177+
/// Whether to dither the output when drawing images.
1178+
///
1179+
/// If false, the default value, dithering will be enabled when the input
1180+
/// color depth is higher than the output color depth. For example,
1181+
/// drawing an RGB8 image onto an RGB565 canvas.
1182+
///
1183+
/// This value also controls dithering of [shader]s, which can make
1184+
/// gradients appear smoother.
1185+
///
1186+
/// Whether or not dithering affects the output is implementation defined.
1187+
/// Some implementations may choose to ignore this completely, if they're
1188+
/// unable to control dithering.
1189+
bool get dither => false; // Not implemented on web
1190+
set dither(bool value) {}
1191+
11771192
// True if Paint instance has used in RecordingCanvas.
11781193
bool _frozen = false;
11791194

0 commit comments

Comments
 (0)