Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
25 changes: 24 additions & 1 deletion lib/ui/painting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,7 @@ class Paint {
static const int _kMaskFilterBlurStyleIndex = 10;
static const int _kMaskFilterSigmaIndex = 11;
static const int _kInvertColorIndex = 12;
static const int _kDitherIndex = 13;

static const int _kIsAntiAliasOffset = _kIsAntiAliasIndex << 2;
static const int _kColorOffset = _kColorIndex << 2;
Expand All @@ -1072,8 +1073,9 @@ class Paint {
static const int _kMaskFilterBlurStyleOffset = _kMaskFilterBlurStyleIndex << 2;
static const int _kMaskFilterSigmaOffset = _kMaskFilterSigmaIndex << 2;
static const int _kInvertColorOffset = _kInvertColorIndex << 2;
static const int _kDitherOffset = _kDitherIndex << 2;
// If you add more fields, remember to update _kDataByteCount.
static const int _kDataByteCount = 52;
static const int _kDataByteCount = 56;

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

/// Whether to dither the output when drawing images.
///
/// If false, the default value, dithering will be enabled when the input
/// color depth is higher than the output color depth. For example,
/// drawing an RGB8 image onto an RGB565 canvas.
///
/// This value also controls dithering of [shader]s, which can make
/// gradients appear smoother.
///
/// Whether or not dithering affects the output is implementation defined.
/// Some implementations may choose to ignore this completely, if they're
/// unable to control dithering.
bool get dither {
return _data.getInt32(_kDitherOffset, _kFakeHostEndian) == 1;
}
set dither(bool value) {
_data.setInt32(_kDitherOffset, value ? 1 : 0, _kFakeHostEndian);
}

@override
String toString() {
final StringBuffer result = StringBuffer();
Expand Down Expand Up @@ -1459,6 +1480,8 @@ class Paint {
}
if (invertColors)
result.write('${semicolon}invert: $invertColors');
if (dither)
result.write('${semicolon}dither: $dither');
result.write(')');
return result.toString();
}
Expand Down
7 changes: 6 additions & 1 deletion lib/ui/painting/paint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ constexpr int kMaskFilterIndex = 9;
constexpr int kMaskFilterBlurStyleIndex = 10;
constexpr int kMaskFilterSigmaIndex = 11;
constexpr int kInvertColorIndex = 12;
constexpr size_t kDataByteCount = 52; // 4 * (last index + 1)
constexpr int kDitherIndex = 13;
constexpr size_t kDataByteCount = 56; // 4 * (last index + 1)

// Indices for objects.
constexpr int kShaderIndex = 0;
Expand Down Expand Up @@ -154,6 +155,10 @@ Paint::Paint(Dart_Handle paint_objects, Dart_Handle paint_data) {
paint_.setColorFilter(invert_filter);
}

if (uint_data[kDitherIndex]) {
paint_.setDither(true);
}

switch (uint_data[kMaskFilterIndex]) {
case Null:
break;
Expand Down
15 changes: 15 additions & 0 deletions lib/web_ui/lib/src/ui/painting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,21 @@ class Paint {
// TODO(flutter/flutter#35156): Implement ImageFilter.
}

/// Whether to dither the output when drawing images.
///
/// If false, the default value, dithering will be enabled when the input
/// color depth is higher than the output color depth. For example,
/// drawing an RGB8 image onto an RGB565 canvas.
///
/// This value also controls dithering of [shader]s, which can make
/// gradients appear smoother.
///
/// Whether or not dithering affects the output is implementation defined.
/// Some implementations may choose to ignore this completely, if they're
/// unable to control dithering.
bool get dither => false; // Not implemented on web
set dither(bool value) {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we do expose this, we should implement it on web.
if we do expose this and don't implement it on web, we should still make the property mutable, so that code that relies on it mutating doesn't randomly break on web.


// True if Paint instance has used in RecordingCanvas.
bool _frozen = false;

Expand Down