Avoid the deprecated SkFilterQuality in the Engine APIs (flutter/engine#24797)

This commit is contained in:
Jim Graham
2021-04-01 14:34:02 -07:00
committed by GitHub
parent 772a6a7c32
commit 1a617496d9
25 changed files with 371 additions and 154 deletions

View File

@@ -533,6 +533,7 @@ class SceneBuilder extends NativeFieldWrapperClass2 {
Rect maskRect,
BlendMode blendMode, {
ShaderMaskEngineLayer? oldLayer,
FilterQuality filterQuality = FilterQuality.low,
}) {
assert(_debugCheckCanBeUsedAsOldLayer(oldLayer, 'pushShaderMask'));
final EngineLayer engineLayer = EngineLayer._();
@@ -544,6 +545,7 @@ class SceneBuilder extends NativeFieldWrapperClass2 {
maskRect.top,
maskRect.bottom,
blendMode.index,
filterQuality.index,
oldLayer?._nativeLayer,
);
final ShaderMaskEngineLayer layer = ShaderMaskEngineLayer._(engineLayer);
@@ -559,6 +561,7 @@ class SceneBuilder extends NativeFieldWrapperClass2 {
double maskRectTop,
double maskRectBottom,
int blendMode,
int filterQualityIndex,
EngineLayer? oldLayer) native 'SceneBuilder_pushShaderMask';
/// Pushes a physical layer operation for an arbitrary shape onto the

View File

@@ -228,13 +228,13 @@ void SceneBuilder::pushShaderMask(Dart_Handle layer_handle,
double maskRectTop,
double maskRectBottom,
int blendMode,
int filterQualityIndex,
fml::RefPtr<EngineLayer> oldLayer) {
SkRect rect = SkRect::MakeLTRB(maskRectLeft, maskRectTop, maskRectRight,
maskRectBottom);
// TODO: Quality come from the caller
SkFilterQuality quality = kLow_SkFilterQuality;
auto sampling = ImageFilter::SamplingFromIndex(filterQualityIndex);
auto layer = std::make_shared<flutter::ShaderMaskLayer>(
shader->shader(quality), rect, static_cast<SkBlendMode>(blendMode));
shader->shader(sampling), rect, static_cast<SkBlendMode>(blendMode));
PushLayer(layer);
EngineLayer::MakeRetained(layer_handle, layer);
@@ -286,10 +286,8 @@ void SceneBuilder::addTexture(double dx,
double height,
int64_t textureId,
bool freeze,
int filterQuality) {
// TODO: take sampling directly from caller: filter-quality is deprecated
auto sampling = SkSamplingOptions(static_cast<SkFilterQuality>(filterQuality),
SkSamplingOptions::kMedium_asMipmapLinear);
int filterQualityIndex) {
auto sampling = ImageFilter::SamplingFromIndex(filterQualityIndex);
auto layer = std::make_unique<flutter::TextureLayer>(
SkPoint::Make(dx, dy), SkSize::Make(width, height), textureId, freeze,
sampling);

View File

@@ -80,6 +80,7 @@ class SceneBuilder : public RefCountedDartWrappable<SceneBuilder> {
double maskRectTop,
double maskRectBottom,
int blendMode,
int filterQualityIndex,
fml::RefPtr<EngineLayer> oldLayer);
void pushPhysicalShape(Dart_Handle layer_handle,
const CanvasPath* path,

View File

@@ -823,17 +823,19 @@ enum BlendMode {
luminosity,
}
/// Quality levels for image filters.
/// Quality levels for image sampling in [ImageFilter] and [Shader] objects that sample
/// images and for [Canvas] operations that render images.
///
/// When scaling up typically the quality is lowest at [none], higher at [low],
/// even higher at [medium], and the highest at [high].
/// When scaling up typically the quality is lowest at [none], higher at [low] and [medium],
/// and for very large scale factors (over 10x) the highest at [high].
///
/// When scaling down, the higher the scale factor the less effective the [high]
/// level is. Beyond certain scale level [medium] may provide better visual
/// result than [high].
/// When scaling down, [medium] provides the best quality especially when scaling an
/// image to less than half its size or for animating the scale factor between such
/// reductions. Otherwise, [low] and [high] provide similar effects for reductions of
/// between 50% and 100% but the image may lose detail and have dropouts below 50%.
///
/// To get high quality when scaling images up and down, or when the scale is
/// unknown, [medium] is typically a good enough choice.
/// unknown, [medium] is typically a good balanced choice.
///
/// ![](https://flutter.github.io/assets-for-api-docs/assets/dart-ui/filter_quality.png)
///
@@ -844,43 +846,56 @@ enum BlendMode {
/// See also:
///
/// * [Paint.filterQuality], which is used to pass [FilterQuality] to the
/// engine while painting.
/// engine while using drawImage calls on a [Canvas].
/// * [ImageShader].
/// * [ImageFilter.matrix].
/// * [Canvas.drawImage].
/// * [Canvas.drawImageRect].
/// * [Canvas.drawImageNine].
/// * [Canvas.drawAtlas].
enum FilterQuality {
// This list comes from Skia's SkFilterQuality.h and the values (order) should
// be kept in sync.
// This list and the values (order) should be kept in sync with the equivalent list
// in lib/ui/painting/image_filter.cc
/// Fastest possible filtering, albeit also the lowest quality.
/// The fastest filtering method, albeit also the lowest quality.
///
/// Typically this implies nearest-neighbor filtering.
/// This value results in a "Nearest Neighbor" algorithm which just
/// repeats or eliminates pixels as an image is scaled up or down.
none,
/// Better quality than [none], faster than [medium].
///
/// Typically this implies bilinear interpolation.
/// This value results in a "Bilinear" algorithm which smoothly
/// interpolates between pixels in an image.
low,
/// Better quality than [low], faster than [high].
/// The best all around filtering method that is only worse than [high]
/// at extremely large scale factors.
///
/// When scaling down images, this is frequently better than [high].
///
/// Typically this implies a combination of bilinear interpolation and
/// pyramidal parametric pre-filtering (mipmaps).
/// This value improves upon the "Bilinear" algorithm specified by [low]
/// by utilizing a Mipmap that pre-computes high quality lower resolutions
/// of the image at half (and quarter and eighth, etc.) sizes and then
/// blends between those to prevent loss of detail at small scale sizes.
///
/// {@template dart.ui.filterQuality.seeAlso}
/// See also:
///
/// * [FilterQuality] class-level documentation that goes into detail about
/// relative qualities of the enum values.
/// relative qualities of the constant values.
/// {@endtemplate}
medium,
/// Best possible quality when scaling up images.
/// Best possible quality when scaling up images by scale factors larger than
/// 5-10x.
///
/// When images are scaled down, this is frequently worse than [medium].
/// When images are scaled down, this can be worse than [medium] for scales
/// smaller than 0.5x, or when animating the scale factor.
///
/// This option is the slowest.
/// This option is also the slowest.
///
/// Typically this implies bicubic interpolation or better.
/// This value results in a standard "Bicubic" algorithm which uses a 3rd order
/// equation to smooth the abrupt transitions between pixels while preserving
/// some of the sense of an edge and avoiding sharp peaks in the result.
///
/// {@macro dart.ui.filterQuality.seeAlso}
high,
@@ -1364,9 +1379,9 @@ class Paint {
}
}
/// Controls the performance vs quality trade-off to use when applying
/// filters, such as [maskFilter], or when drawing images, as with
/// [Canvas.drawImageRect] or [Canvas.drawImageNine].
/// Controls the performance vs quality trade-off to use when sampling bitmaps,
/// as with an [ImageShader], or when drawing images, as with [Canvas.drawImage],
/// [Canvas.drawImageRect], [Canvas.drawImageNine] or [Canvas.drawAtlas].
///
/// Defaults to [FilterQuality.none].
// TODO(ianh): verify that the image drawing methods actually respect this
@@ -3676,9 +3691,13 @@ class ImageShader extends Shader {
/// tile. The second and third arguments specify the [TileMode] for the x
/// direction and y direction respectively. The fourth argument gives the
/// matrix to apply to the effect. All the arguments are required and must not
/// be null.
/// be null, except for [filterQuality]. If [filterQuality] is not specified
/// at construction time it will be deduced from the environment where it is used,
/// such as from [Paint.filterQuality].
@pragma('vm:entry-point')
ImageShader(Image image, TileMode tmx, TileMode tmy, Float64List matrix4) :
ImageShader(Image image, TileMode tmx, TileMode tmy, Float64List matrix4, {
FilterQuality? filterQuality,
}) :
assert(image != null), // image is checked on the engine side
assert(tmx != null),
assert(tmy != null),
@@ -3687,10 +3706,10 @@ class ImageShader extends Shader {
if (matrix4.length != 16)
throw ArgumentError('"matrix4" must have 16 entries.');
_constructor();
_initWithImage(image._image, tmx.index, tmy.index, matrix4);
_initWithImage(image._image, tmx.index, tmy.index, filterQuality?.index ?? -1, matrix4);
}
void _constructor() native 'ImageShader_constructor';
void _initWithImage(_Image image, int tmx, int tmy, Float64List matrix4) native 'ImageShader_initWithImage';
void _initWithImage(_Image image, int tmx, int tmy, int filterQualityIndex, Float64List matrix4) native 'ImageShader_initWithImage';
}
/// Defines how a list of points is interpreted when drawing a set of triangles.
@@ -4287,13 +4306,14 @@ class Canvas extends NativeFieldWrapperClass2 {
assert(image != null); // image is checked on the engine side
assert(_offsetIsValid(offset));
assert(paint != null);
_drawImage(image._image, offset.dx, offset.dy, paint._objects, paint._data);
_drawImage(image._image, offset.dx, offset.dy, paint._objects, paint._data, paint.filterQuality.index);
}
void _drawImage(_Image image,
double x,
double y,
List<dynamic>? paintObjects,
ByteData paintData) native 'Canvas_drawImage';
ByteData paintData,
int filterQualityIndex) native 'Canvas_drawImage';
/// Draws the subset of the given image described by the `src` argument into
/// the canvas in the axis-aligned rectangle given by the `dst` argument.
@@ -4319,7 +4339,8 @@ class Canvas extends NativeFieldWrapperClass2 {
dst.right,
dst.bottom,
paint._objects,
paint._data);
paint._data,
paint.filterQuality.index);
}
void _drawImageRect(_Image image,
double srcLeft,
@@ -4331,7 +4352,8 @@ class Canvas extends NativeFieldWrapperClass2 {
double dstRight,
double dstBottom,
List<dynamic>? paintObjects,
ByteData paintData) native 'Canvas_drawImageRect';
ByteData paintData,
int filterQualityIndex) native 'Canvas_drawImageRect';
/// Draws the given [Image] into the canvas using the given [Paint].
///
@@ -4361,7 +4383,8 @@ class Canvas extends NativeFieldWrapperClass2 {
dst.right,
dst.bottom,
paint._objects,
paint._data);
paint._data,
paint.filterQuality.index);
}
void _drawImageNine(_Image image,
double centerLeft,
@@ -4373,7 +4396,8 @@ class Canvas extends NativeFieldWrapperClass2 {
double dstRight,
double dstBottom,
List<dynamic>? paintObjects,
ByteData paintData) native 'Canvas_drawImageNine';
ByteData paintData,
int filterQualityIndex) native 'Canvas_drawImageNine';
/// Draw the given picture onto the canvas. To create a picture, see
/// [PictureRecorder].
@@ -4636,9 +4660,10 @@ class Canvas extends NativeFieldWrapperClass2 {
final Int32List? colorBuffer = (colors == null || colors.isEmpty) ? null : _encodeColorList(colors);
final Float32List? cullRectBuffer = cullRect?._value32;
final int qualityIndex = paint.filterQuality.index;
_drawAtlas(
paint._objects, paint._data, atlas._image, rstTransformBuffer, rectBuffer,
paint._objects, paint._data, qualityIndex, atlas._image, rstTransformBuffer, rectBuffer,
colorBuffer, (blendMode ?? BlendMode.src).index, cullRectBuffer
);
}
@@ -4804,15 +4829,17 @@ class Canvas extends NativeFieldWrapperClass2 {
throw ArgumentError('"rstTransforms" and "rects" lengths must be a multiple of four.');
if (colors != null && colors.length * 4 != rectCount)
throw ArgumentError('If non-null, "colors" length must be one fourth the length of "rstTransforms" and "rects".');
final int qualityIndex = paint.filterQuality.index;
_drawAtlas(
paint._objects, paint._data, atlas._image, rstTransforms, rects,
paint._objects, paint._data, qualityIndex, atlas._image, rstTransforms, rects,
colors, (blendMode ?? BlendMode.src).index, cullRect?._value32
);
}
void _drawAtlas(List<dynamic>? paintObjects,
ByteData paintData,
int filterQualityIndex,
_Image atlas,
Float32List rstTransforms,
Float32List rects,

View File

@@ -3,6 +3,7 @@
// found in the LICENSE file.
#include "flutter/lib/ui/painting/canvas.h"
#include "flutter/lib/ui/painting/image_filter.h"
#include <cmath>
@@ -311,17 +312,12 @@ void Canvas::drawPath(const CanvasPath* path,
canvas_->drawPath(path->path(), *paint.paint());
}
static SkSamplingOptions paint_to_sampling(const SkPaint* paint) {
return SkSamplingOptions(
paint ? paint->getFilterQuality() : kNone_SkFilterQuality,
SkSamplingOptions::kMedium_asMipmapLinear);
}
void Canvas::drawImage(const CanvasImage* image,
double x,
double y,
const Paint& paint,
const PaintData& paint_data) {
const PaintData& paint_data,
int filterQualityIndex) {
if (!canvas_) {
return;
}
@@ -330,8 +326,7 @@ void Canvas::drawImage(const CanvasImage* image,
ToDart("Canvas.drawImage called with non-genuine Image."));
return;
}
// TODO: add filtering to public API, since paint's quality is deprecated
SkSamplingOptions sampling = paint_to_sampling(paint.paint());
auto sampling = ImageFilter::SamplingFromIndex(filterQualityIndex);
canvas_->drawImage(image->image(), x, y, sampling, paint.paint());
}
@@ -345,7 +340,8 @@ void Canvas::drawImageRect(const CanvasImage* image,
double dst_right,
double dst_bottom,
const Paint& paint,
const PaintData& paint_data) {
const PaintData& paint_data,
int filterQualityIndex) {
if (!canvas_) {
return;
}
@@ -356,18 +352,11 @@ void Canvas::drawImageRect(const CanvasImage* image,
}
SkRect src = SkRect::MakeLTRB(src_left, src_top, src_right, src_bottom);
SkRect dst = SkRect::MakeLTRB(dst_left, dst_top, dst_right, dst_bottom);
// TODO: add filtering to public API, since paint's quality is deprecated
SkSamplingOptions sampling = paint_to_sampling(paint.paint());
auto sampling = ImageFilter::SamplingFromIndex(filterQualityIndex);
canvas_->drawImageRect(image->image(), src, dst, sampling, paint.paint(),
SkCanvas::kFast_SrcRectConstraint);
}
static SkFilterMode paint_to_filter(const SkPaint* paint) {
return paint && (paint->getFilterQuality() != kNone_SkFilterQuality)
? SkFilterMode::kLinear
: SkFilterMode::kNearest;
}
void Canvas::drawImageNine(const CanvasImage* image,
double center_left,
double center_top,
@@ -378,7 +367,8 @@ void Canvas::drawImageNine(const CanvasImage* image,
double dst_right,
double dst_bottom,
const Paint& paint,
const PaintData& paint_data) {
const PaintData& paint_data,
int bitmapSamplingIndex) {
if (!canvas_) {
return;
}
@@ -392,8 +382,7 @@ void Canvas::drawImageNine(const CanvasImage* image,
SkIRect icenter;
center.round(&icenter);
SkRect dst = SkRect::MakeLTRB(dst_left, dst_top, dst_right, dst_bottom);
// TODO: add filtering to public API, since paint's quality is deprecated
SkFilterMode filter = paint_to_filter(paint.paint());
auto filter = ImageFilter::FilterModeFromIndex(bitmapSamplingIndex);
canvas_->drawImageNine(image->image().get(), icenter, dst, filter,
paint.paint());
}
@@ -444,6 +433,7 @@ void Canvas::drawVertices(const Vertices* vertices,
void Canvas::drawAtlas(const Paint& paint,
const PaintData& paint_data,
int filterQualityIndex,
CanvasImage* atlas,
const tonic::Float32List& transforms,
const tonic::Float32List& rects,
@@ -467,8 +457,7 @@ void Canvas::drawAtlas(const Paint& paint,
static_assert(sizeof(SkRect) == sizeof(float) * 4,
"SkRect doesn't use floats.");
// TODO: add filtering to public API, since paint's quality is deprecated
SkSamplingOptions sampling = paint_to_sampling(paint.paint());
auto sampling = ImageFilter::SamplingFromIndex(filterQualityIndex);
canvas_->drawAtlas(
skImage.get(), reinterpret_cast<const SkRSXform*>(transforms.data()),

View File

@@ -110,7 +110,8 @@ class Canvas : public RefCountedDartWrappable<Canvas> {
double x,
double y,
const Paint& paint,
const PaintData& paint_data);
const PaintData& paint_data,
int filterQualityIndex);
void drawImageRect(const CanvasImage* image,
double src_left,
double src_top,
@@ -121,7 +122,8 @@ class Canvas : public RefCountedDartWrappable<Canvas> {
double dst_right,
double dst_bottom,
const Paint& paint,
const PaintData& paint_data);
const PaintData& paint_data,
int filterQualityIndex);
void drawImageNine(const CanvasImage* image,
double center_left,
double center_top,
@@ -132,7 +134,8 @@ class Canvas : public RefCountedDartWrappable<Canvas> {
double dst_right,
double dst_bottom,
const Paint& paint,
const PaintData& paint_data);
const PaintData& paint_data,
int bitmapSamplingIndex);
void drawPicture(Picture* picture);
// The paint argument is first for the following functions because Paint
@@ -152,6 +155,7 @@ class Canvas : public RefCountedDartWrappable<Canvas> {
void drawAtlas(const Paint& paint,
const PaintData& paint_data,
int filterQualityIndex,
CanvasImage* atlas,
const tonic::Float32List& transforms,
const tonic::Float32List& rects,

View File

@@ -62,7 +62,9 @@ class CanvasGradient : public Shader {
SkTileMode tile_mode,
const tonic::Float64List& matrix4);
sk_sp<SkShader> shader(SkFilterQuality) override { return sk_shader_.get(); }
sk_sp<SkShader> shader(SkSamplingOptions) override {
return sk_shader_.get();
}
static void RegisterNatives(tonic::DartLibraryNatives* natives);

View File

@@ -40,6 +40,30 @@ fml::RefPtr<ImageFilter> ImageFilter::Create() {
return fml::MakeRefCounted<ImageFilter>();
}
static const std::array<SkSamplingOptions, 4> filter_qualities = {
SkSamplingOptions(SkFilterMode::kNearest, SkMipmapMode::kNone),
SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kNone),
SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear),
SkSamplingOptions(SkCubicResampler{1 / 3.0f, 1 / 3.0f}),
};
SkSamplingOptions ImageFilter::SamplingFromIndex(int filterQualityIndex) {
if (filterQualityIndex < 0) {
return filter_qualities.front();
} else if (((size_t)filterQualityIndex) >= filter_qualities.size()) {
return filter_qualities.back();
} else {
return filter_qualities[filterQualityIndex];
}
}
SkFilterMode ImageFilter::FilterModeFromIndex(int filterQualityIndex) {
if (filterQualityIndex <= 0) {
return SkFilterMode::kNearest;
}
return SkFilterMode::kLinear;
}
ImageFilter::ImageFilter() {}
ImageFilter::~ImageFilter() {}
@@ -59,9 +83,8 @@ void ImageFilter::initBlur(double sigma_x,
}
void ImageFilter::initMatrix(const tonic::Float64List& matrix4,
int filterQuality) {
auto sampling = SkSamplingOptions(static_cast<SkFilterQuality>(filterQuality),
SkSamplingOptions::kMedium_asMipmapLinear);
int filterQualityIndex) {
auto sampling = ImageFilter::SamplingFromIndex(filterQualityIndex);
filter_ =
SkImageFilters::MatrixTransform(ToSkMatrix(matrix4), sampling, nullptr);
}

View File

@@ -22,10 +22,13 @@ class ImageFilter : public RefCountedDartWrappable<ImageFilter> {
~ImageFilter() override;
static fml::RefPtr<ImageFilter> Create();
static SkSamplingOptions SamplingFromIndex(int filterQualityIndex);
static SkFilterMode FilterModeFromIndex(int index);
void initImage(CanvasImage* image);
void initPicture(Picture*);
void initBlur(double sigma_x, double sigma_y, SkTileMode tile_mode);
void initMatrix(const tonic::Float64List& matrix4, int filter_quality);
void initMatrix(const tonic::Float64List& matrix4, int filter_quality_index);
void initColorFilter(ColorFilter* colorFilter);
void initComposeFilter(ImageFilter* outer, ImageFilter* inner);

View File

@@ -3,6 +3,7 @@
// found in the LICENSE file.
#include "flutter/lib/ui/painting/image_shader.h"
#include "flutter/lib/ui/painting/image_filter.h"
#include "flutter/lib/ui/ui_dart_state.h"
#include "third_party/tonic/converter/dart_converter.h"
@@ -37,6 +38,7 @@ fml::RefPtr<ImageShader> ImageShader::Create() {
void ImageShader::initWithImage(CanvasImage* image,
SkTileMode tmx,
SkTileMode tmy,
int filter_quality_index,
const tonic::Float64List& matrix4) {
if (!image) {
Dart_ThrowException(
@@ -47,19 +49,26 @@ void ImageShader::initWithImage(CanvasImage* image,
tmx_ = tmx;
tmy_ = tmy;
local_matrix_ = ToSkMatrix(matrix4);
if (filter_quality_index >= 0) {
cached_sampling_ = ImageFilter::SamplingFromIndex(filter_quality_index);
sampling_is_locked_ = true;
} else {
sampling_is_locked_ = false;
}
}
sk_sp<SkShader> ImageShader::shader(SkFilterQuality quality) {
if (!cached_shader_.get() || cached_quality_ != quality) {
SkSamplingOptions sampling(quality,
SkSamplingOptions::kMedium_asMipmapLinear);
cached_quality_ = quality;
sk_sp<SkShader> ImageShader::shader(SkSamplingOptions sampling) {
if (sampling_is_locked_) {
sampling = cached_sampling_;
}
if (!cached_shader_.get() || cached_sampling_ != sampling) {
cached_sampling_ = sampling;
cached_shader_ = UIDartState::CreateGPUObject(
sk_image_->makeShader(tmx_, tmy_, sampling, &local_matrix_));
}
return cached_shader_.get();
}
ImageShader::ImageShader() = default;
ImageShader::~ImageShader() = default;

View File

@@ -31,9 +31,10 @@ class ImageShader : public Shader {
void initWithImage(CanvasImage* image,
SkTileMode tmx,
SkTileMode tmy,
int filter_quality_index,
const tonic::Float64List& matrix4);
sk_sp<SkShader> shader(SkFilterQuality) override;
sk_sp<SkShader> shader(SkSamplingOptions) override;
static void RegisterNatives(tonic::DartLibraryNatives* natives);
@@ -44,8 +45,9 @@ class ImageShader : public Shader {
SkTileMode tmx_;
SkTileMode tmy_;
SkMatrix local_matrix_;
bool sampling_is_locked_;
SkFilterQuality cached_quality_ = kNone_SkFilterQuality;
SkSamplingOptions cached_sampling_;
flutter::SkiaGPUObject<SkShader> cached_shader_;
};

View File

@@ -77,9 +77,6 @@ Paint::Paint(Dart_Handle paint_objects, Dart_Handle paint_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());
auto filter_quality =
static_cast<SkFilterQuality>(uint_data[kFilterQualityIndex]);
Dart_Handle values[kObjectCount];
if (!Dart_IsNull(paint_objects)) {
FML_DCHECK(Dart_IsList(paint_objects));
@@ -95,7 +92,9 @@ Paint::Paint(Dart_Handle paint_objects, Dart_Handle paint_data) {
Dart_Handle shader = values[kShaderIndex];
if (!Dart_IsNull(shader)) {
Shader* decoded = tonic::DartConverter<Shader*>::FromDart(shader);
paint_.setShader(decoded->shader(filter_quality));
auto sampling =
ImageFilter::SamplingFromIndex(uint_data[kFilterQualityIndex]);
paint_.setShader(decoded->shader(sampling));
}
Dart_Handle color_filter = values[kColorFilterIndex];
@@ -114,7 +113,6 @@ Paint::Paint(Dart_Handle paint_objects, Dart_Handle paint_data) {
}
paint_.setAntiAlias(uint_data[kIsAntiAliasIndex] == 0);
paint_.setFilterQuality(filter_quality);
uint32_t encoded_color = uint_data[kColorIndex];
if (encoded_color) {

View File

@@ -8,7 +8,6 @@
#include "flutter/flow/skia_gpu_object.h"
#include "flutter/lib/ui/dart_wrapper.h"
#include "flutter/lib/ui/ui_dart_state.h"
#include "third_party/skia/include/core/SkFilterQuality.h"
#include "third_party/skia/include/core/SkShader.h"
namespace flutter {
@@ -20,7 +19,7 @@ class Shader : public RefCountedDartWrappable<Shader> {
public:
~Shader() override;
virtual sk_sp<SkShader> shader(SkFilterQuality) = 0;
virtual sk_sp<SkShader> shader(SkSamplingOptions) = 0;
protected:
Shader() {}

View File

@@ -14,6 +14,11 @@ final SkClipOp _clipOpIntersect = canvasKit.ClipOp.Intersect;
/// This is intentionally not memory-managing the underlying [SkCanvas]. See
/// the docs on [SkCanvas], which explain the reason.
class CkCanvas {
// Cubic equation coefficients recommended by Mitchell & Netravali
// in their paper on cubic interpolation.
static const double _kMitchellNetravali_B = 1.0 / 3.0;
static const double _kMitchellNetravali_C = 1.0 / 3.0;
final SkCanvas skCanvas;
CkCanvas(this.skCanvas);
@@ -65,6 +70,7 @@ class CkCanvas {
);
}
// TODO(flar): CanvasKit does not expose sampling options available on SkCanvas.drawAtlas
void drawAtlasRaw(
CkPaint paint,
CkImage atlas,
@@ -108,30 +114,57 @@ class CkCanvas {
}
void drawImage(CkImage image, ui.Offset offset, CkPaint paint) {
skCanvas.drawImage(
image.skImage,
offset.dx,
offset.dy,
paint.skiaObject,
);
ui.FilterQuality filterQuality = paint.filterQuality;
if (filterQuality == ui.FilterQuality.high) {
skCanvas.drawImageCubic(
image.skImage,
offset.dx,
offset.dy,
_kMitchellNetravali_B,
_kMitchellNetravali_C,
paint.skiaObject,
);
} else {
skCanvas.drawImageOptions(
image.skImage,
offset.dx,
offset.dy,
toSkFilterMode(filterQuality),
toSkMipmapMode(filterQuality),
paint.skiaObject,
);
}
}
void drawImageRect(CkImage image, ui.Rect src, ui.Rect dst, CkPaint paint) {
skCanvas.drawImageRect(
image.skImage,
toSkRect(src),
toSkRect(dst),
paint.skiaObject,
false,
);
ui.FilterQuality filterQuality = paint.filterQuality;
if (filterQuality == ui.FilterQuality.high) {
skCanvas.drawImageRectCubic(
image.skImage,
toSkRect(src),
toSkRect(dst),
_kMitchellNetravali_B,
_kMitchellNetravali_C,
paint.skiaObject,
);
} else {
skCanvas.drawImageRectOptions(
image.skImage,
toSkRect(src),
toSkRect(dst),
toSkFilterMode(filterQuality),
toSkMipmapMode(filterQuality),
paint.skiaObject,
);
}
}
void drawImageNine(
CkImage image, ui.Rect center, ui.Rect dst, CkPaint paint) {
void drawImageNine(CkImage image, ui.Rect center, ui.Rect dst, CkPaint paint) {
skCanvas.drawImageNine(
image.skImage,
toSkRect(center),
toSkRect(dst),
toSkFilterMode(paint.filterQuality),
paint.skiaObject,
);
}
@@ -934,12 +967,26 @@ class CkDrawImageCommand extends CkPaintCommand {
@override
void apply(SkCanvas canvas) {
canvas.drawImage(
image.skImage,
offset.dx,
offset.dy,
paint.skiaObject,
);
ui.FilterQuality filterQuality = paint.filterQuality;
if (filterQuality == ui.FilterQuality.high) {
canvas.drawImageCubic(
image.skImage,
offset.dx,
offset.dy,
CkCanvas._kMitchellNetravali_B,
CkCanvas._kMitchellNetravali_C,
paint.skiaObject,
);
} else {
canvas.drawImageOptions(
image.skImage,
offset.dx,
offset.dy,
toSkFilterMode(filterQuality),
toSkMipmapMode(filterQuality),
paint.skiaObject,
);
}
}
@override
@@ -959,13 +1006,26 @@ class CkDrawImageRectCommand extends CkPaintCommand {
@override
void apply(SkCanvas canvas) {
canvas.drawImageRect(
image.skImage,
toSkRect(src),
toSkRect(dst),
paint.skiaObject,
false,
);
ui.FilterQuality filterQuality = paint.filterQuality;
if (filterQuality == ui.FilterQuality.high) {
canvas.drawImageRectCubic(
image.skImage,
toSkRect(src),
toSkRect(dst),
CkCanvas._kMitchellNetravali_B,
CkCanvas._kMitchellNetravali_C,
paint.skiaObject,
);
} else {
canvas.drawImageRectOptions(
image.skImage,
toSkRect(src),
toSkRect(dst),
toSkFilterMode(filterQuality),
toSkMipmapMode(filterQuality),
paint.skiaObject,
);
}
}
@override
@@ -989,6 +1049,7 @@ class CkDrawImageNineCommand extends CkPaintCommand {
image.skImage,
toSkRect(center),
toSkRect(dst),
toSkFilterMode(paint.filterQuality),
paint.skiaObject,
);
}

View File

@@ -665,6 +665,12 @@ class SkFilterMode {
external int get value;
}
SkFilterMode toSkFilterMode(ui.FilterQuality filterQuality) {
return filterQuality == ui.FilterQuality.none
? canvasKit.FilterMode.Nearest
: canvasKit.FilterMode.Linear;
}
@JS()
class SkMipmapModeEnum {
external SkMipmapMode get None;
@@ -677,6 +683,12 @@ class SkMipmapMode {
external int get value;
}
SkMipmapMode toSkMipmapMode(ui.FilterQuality filterQuality) {
return filterQuality == ui.FilterQuality.medium
? canvasKit.MipmapMode.Linear
: canvasKit.MipmapMode.None;
}
@JS()
class SkAlphaTypeEnum {
external SkAlphaType get Opaque;
@@ -734,6 +746,13 @@ class SkImage {
external void delete();
external int width();
external int height();
external SkShader makeShaderCubic(
SkTileMode tileModeX,
SkTileMode tileModeY,
double B,
double C,
Float32List? matrix, // 3x3 matrix
);
external SkShader makeShaderOptions(
SkTileMode tileModeX,
SkTileMode tileModeY,
@@ -1351,23 +1370,43 @@ class SkCanvas {
Float32List inner,
SkPaint paint,
);
external void drawImage(
external void drawImageCubic(
SkImage image,
double x,
double y,
double B,
double C,
SkPaint paint,
);
external void drawImageRect(
external void drawImageOptions(
SkImage image,
double x,
double y,
SkFilterMode filterMode,
SkMipmapMode mipmapMode,
SkPaint paint,
);
external void drawImageRectCubic(
SkImage image,
Float32List src,
Float32List dst,
double B,
double C,
SkPaint paint,
);
external void drawImageRectOptions(
SkImage image,
Float32List src,
Float32List dst,
SkFilterMode filterMode,
SkMipmapMode mipmapMode,
SkPaint paint,
bool fastSample,
);
external void drawImageNine(
SkImage image,
Float32List center,
Float32List dst,
SkFilterMode filterMode,
SkPaint paint,
);
external void drawLine(

View File

@@ -260,11 +260,6 @@ class CanvasKitCanvas implements ui.Canvas {
assert(rectIsValid(center));
assert(rectIsValid(dst));
assert(paint != null); // ignore: unnecessary_null_comparison
_drawImageNine(image, center, dst, paint);
}
void _drawImageNine(
ui.Image image, ui.Rect center, ui.Rect dst, ui.Paint paint) {
_canvas.drawImageNine(image as CkImage, center, dst, paint as CkPaint);
}

View File

@@ -379,11 +379,12 @@ class ImageFilterEngineLayer extends ContainerLayer implements ui.ImageFilterEng
}
class ShaderMaskEngineLayer extends ContainerLayer implements ui.ShaderMaskEngineLayer {
ShaderMaskEngineLayer(this.shader, this.maskRect, this.blendMode);
ShaderMaskEngineLayer(this.shader, this.maskRect, this.blendMode, this.filterQuality);
final ui.Shader shader;
final ui.Rect maskRect;
final ui.BlendMode blendMode;
final ui.FilterQuality filterQuality;
@override
void paint(PaintContext paintContext) {
@@ -395,6 +396,7 @@ class ShaderMaskEngineLayer extends ContainerLayer implements ui.ShaderMaskEngin
CkPaint paint = CkPaint();
paint.shader = shader;
paint.blendMode = blendMode;
paint.filterQuality = filterQuality;
paintContext.leafNodesCanvas!.save();
paintContext.leafNodesCanvas!.translate(maskRect.left, maskRect.top);

View File

@@ -192,9 +192,10 @@ class LayerSceneBuilder implements ui.SceneBuilder {
ui.Rect maskRect,
ui.BlendMode blendMode, {
ui.EngineLayer? oldLayer,
ui.FilterQuality filterQuality = ui.FilterQuality.low,
}) {
return pushLayer<ShaderMaskEngineLayer>(ShaderMaskEngineLayer(
shader, maskRect, blendMode));
shader, maskRect, blendMode, filterQuality));
}
@override

View File

@@ -124,7 +124,7 @@ class CkPaint extends ManagedSkiaObject<SkPaint> implements ui.Paint {
return;
}
_shader = value as CkShader?;
skiaObject.setShader(_shader?.skiaObject);
skiaObject.setShader(_shader?.withQuality(_filterQuality));
}
CkShader? _shader;
@@ -159,6 +159,7 @@ class CkPaint extends ManagedSkiaObject<SkPaint> implements ui.Paint {
return;
}
_filterQuality = value;
skiaObject.setShader(_shader?.withQuality(value));
skiaObject.setFilterQuality(toSkFilterQuality(value));
}
@@ -227,7 +228,7 @@ class CkPaint extends ManagedSkiaObject<SkPaint> implements ui.Paint {
paint.setStrokeWidth(_strokeWidth);
paint.setAntiAlias(_isAntiAlias);
paint.setColorInt(_color.value);
paint.setShader(_shader?.skiaObject);
paint.setShader(_shader?.withQuality(_filterQuality));
paint.setMaskFilter(_ckMaskFilter?.skiaObject);
paint.setColorFilter(_managedColorFilter?.skiaObject);
paint.setImageFilter(_managedImageFilter?.skiaObject);

View File

@@ -7,6 +7,8 @@ part of engine;
abstract class CkShader extends ManagedSkiaObject<SkShader>
implements ui.Shader {
SkShader withQuality(ui.FilterQuality contextualQuality) => skiaObject;
@override
void delete() {
rawSkiaObject?.delete();
@@ -162,25 +164,49 @@ class CkGradientConical extends CkShader implements ui.Gradient {
}
class CkImageShader extends CkShader implements ui.ImageShader {
CkImageShader(ui.Image image, this.tileModeX, this.tileModeY, this.matrix4)
CkImageShader(ui.Image image, this.tileModeX, this.tileModeY, this.matrix4, this.filterQuality)
: _image = image as CkImage;
final ui.TileMode tileModeX;
final ui.TileMode tileModeY;
final Float64List matrix4;
final ui.FilterQuality? filterQuality;
final CkImage _image;
ui.FilterQuality? _cachedQuality;
@override
SkShader createDefault() => _image.skImage.makeShaderOptions(
toSkTileMode(tileModeX),
toSkTileMode(tileModeY),
canvasKit.FilterMode.Nearest,
canvasKit.MipmapMode.None,
toSkMatrixFromFloat64(matrix4),
);
SkShader withQuality(ui.FilterQuality contextualQuality) {
ui.FilterQuality quality = filterQuality ?? contextualQuality;
SkShader? shader = rawSkiaObject;
if (_cachedQuality != quality || shader == null) {
if (quality == ui.FilterQuality.high) {
shader = _image.skImage.makeShaderCubic(
toSkTileMode(tileModeX),
toSkTileMode(tileModeY),
1.0 / 3.0,
1.0 / 3.0,
toSkMatrixFromFloat64(matrix4),
);
} else {
shader = _image.skImage.makeShaderOptions(
toSkTileMode(tileModeX),
toSkTileMode(tileModeY),
toSkFilterMode(quality),
toSkMipmapMode(quality),
toSkMatrixFromFloat64(matrix4),
);
}
_cachedQuality = quality;
rawSkiaObject = shader;
}
return shader;
}
@override
SkShader resurrect() => createDefault();
SkShader createDefault() => withQuality(ui.FilterQuality.none);
@override
SkShader resurrect() => withQuality(_cachedQuality ?? ui.FilterQuality.none);
@override
void delete() {

View File

@@ -234,10 +234,12 @@ class SurfaceSceneBuilder implements ui.SceneBuilder {
ui.Rect maskRect,
ui.BlendMode blendMode, {
ui.ShaderMaskEngineLayer? oldLayer,
ui.FilterQuality filterQuality = ui.FilterQuality.low,
}) {
assert(blendMode != null); // ignore: unnecessary_null_comparison
return _pushSurface<PersistedShaderMask>(PersistedShaderMask(
oldLayer as PersistedShaderMask?, shader, maskRect, blendMode));
oldLayer as PersistedShaderMask?,
shader, maskRect, blendMode, filterQuality));
}
/// Pushes a physical layer operation for an arbitrary shape onto the
@@ -384,12 +386,11 @@ class SurfaceSceneBuilder implements ui.SceneBuilder {
bool freeze = false,
ui.FilterQuality filterQuality = ui.FilterQuality.low,
}) {
_addTexture(
offset.dx, offset.dy, width, height, textureId, filterQuality.index);
_addTexture(offset.dx, offset.dy, width, height, textureId, filterQuality);
}
void _addTexture(double dx, double dy, double width, double height,
int textureId, int filterQuality) {
int textureId, ui.FilterQuality filterQuality) {
// In test mode, allow this to be a no-op.
if (!ui.debugEmulateFlutterTesterEnvironment) {
throw UnimplementedError('Textures are not supported in Flutter Web');

View File

@@ -23,12 +23,14 @@ class PersistedShaderMask extends PersistedContainerSurface
this.shader,
this.maskRect,
this.blendMode,
this.filterQuality,
) : super(oldLayer);
html.Element? _childContainer;
final ui.Shader shader;
final ui.Rect maskRect;
final ui.BlendMode blendMode;
final ui.FilterQuality filterQuality;
html.Element? _shaderElement;
static int activeShaderMaskCount = 0;
final bool isWebKit = browserEngine == BrowserEngine.webkit;

View File

@@ -86,6 +86,7 @@ abstract class SceneBuilder {
Rect maskRect,
BlendMode blendMode, {
ShaderMaskEngineLayer? oldLayer,
FilterQuality filterQuality = FilterQuality.low,
});
PhysicalShapeEngineLayer? pushPhysicalShape({
required Path path,

View File

@@ -385,8 +385,6 @@ class MaskFilter {
}
enum FilterQuality {
// This list comes from Skia's SkFilterQuality.h and the values (order) should
// be kept in sync.
none,
low,
medium,
@@ -690,9 +688,11 @@ class Shadow {
}
class ImageShader extends Shader {
factory ImageShader(Image image, TileMode tmx, TileMode tmy, Float64List matrix4) {
factory ImageShader(Image image, TileMode tmx, TileMode tmy, Float64List matrix4, {
FilterQuality? filterQuality,
}) {
if (engine.useCanvasKit) {
return engine.CkImageShader(image, tmx, tmy, matrix4);
return engine.CkImageShader(image, tmx, tmy, matrix4, filterQuality);
}
throw UnsupportedError('ImageShader not implemented for web platform.');
}

View File

@@ -980,26 +980,55 @@ void _canvasTests() {
);
});
test('drawImage', () {
test('drawImageOptions', () {
final SkAnimatedImage image =
canvasKit.MakeAnimatedImageFromEncoded(kTransparentImage);
canvas.drawImage(
canvas.drawImageOptions(
image.getCurrentFrame(),
10,
20,
canvasKit.FilterMode.Linear,
canvasKit.MipmapMode.None,
SkPaint(),
);
});
test('drawImageRect', () {
test('drawImageCubic', () {
final SkAnimatedImage image =
canvasKit.MakeAnimatedImageFromEncoded(kTransparentImage);
canvas.drawImageRect(
canvasKit.MakeAnimatedImageFromEncoded(kTransparentImage);
canvas.drawImageCubic(
image.getCurrentFrame(),
10,
20,
0.3,
0.3,
SkPaint(),
);
});
test('drawImageRectOptions', () {
final SkAnimatedImage image =
canvasKit.MakeAnimatedImageFromEncoded(kTransparentImage);
canvas.drawImageRectOptions(
image.getCurrentFrame(),
Float32List.fromList([0, 0, 1, 1]),
Float32List.fromList([0, 0, 1, 1]),
canvasKit.FilterMode.Linear,
canvasKit.MipmapMode.None,
SkPaint(),
);
});
test('drawImageRectCubic', () {
final SkAnimatedImage image =
canvasKit.MakeAnimatedImageFromEncoded(kTransparentImage);
canvas.drawImageRectCubic(
image.getCurrentFrame(),
Float32List.fromList([0, 0, 1, 1]),
Float32List.fromList([0, 0, 1, 1]),
0.3,
0.3,
SkPaint(),
false,
);
});
@@ -1010,6 +1039,7 @@ void _canvasTests() {
image.getCurrentFrame(),
Float32List.fromList([0, 0, 1, 1]),
Float32List.fromList([0, 0, 1, 1]),
canvasKit.FilterMode.Linear,
SkPaint(),
);
});