From 92cf90b20a5bac3f80f03f62dcca418b692653f9 Mon Sep 17 00:00:00 2001 From: Luigi Rosso Date: Sat, 31 Mar 2018 20:01:40 -0700 Subject: [PATCH] Allow setting transform on radial gradients. (flutter/engine#4916) --- engine/src/flutter/lib/ui/painting.dart | 11 +++++++++-- engine/src/flutter/lib/ui/painting/gradient.cc | 11 +++++++++-- engine/src/flutter/lib/ui/painting/gradient.h | 5 ++++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/engine/src/flutter/lib/ui/painting.dart b/engine/src/flutter/lib/ui/painting.dart index 9ee3ef39a2..7d39b1b717 100644 --- a/engine/src/flutter/lib/ui/painting.dart +++ b/engine/src/flutter/lib/ui/painting.dart @@ -1921,23 +1921,30 @@ class Gradient extends Shader { /// If `center`, `radius`, `colors`, or `tileMode` are null, or if `colors` or /// `colorStops` contain null values, this constructor will throw a /// [NoSuchMethodError]. + /// + /// If `matrix4` is provided, the gradient fill will be transformed by the + /// specified 4x4 matrix relative to the local coordinate system. `matrix4` must + /// be a column-major matrix packed into a list of 16 values. Gradient.radial( Offset center, double radius, List colors, [ List colorStops, TileMode tileMode = TileMode.clamp, + Float64List matrix4 ]) : assert(_offsetIsValid(center)), assert(colors != null), assert(tileMode != null), super._() { + if (matrix4 != null && matrix4.length != 16) + throw new ArgumentError('"matrix4" must have 16 entries.'); _validateColorStops(colors, colorStops); final Int32List colorsBuffer = _encodeColorList(colors); final Float32List colorStopsBuffer = colorStops == null ? null : new Float32List.fromList(colorStops); _constructor(); - _initRadial(center.dx, center.dy, radius, colorsBuffer, colorStopsBuffer, tileMode.index); + _initRadial(center.dx, center.dy, radius, colorsBuffer, colorStopsBuffer, tileMode.index, matrix4); } - void _initRadial(double centerX, double centerY, double radius, Int32List colors, Float32List colorStops, int tileMode) native 'Gradient_initRadial'; + void _initRadial(double centerX, double centerY, double radius, Int32List colors, Float32List colorStops, int tileMode, Float64List matrix4) native 'Gradient_initRadial'; static void _validateColorStops(List colors, List colorStops) { if (colorStops == null) { diff --git a/engine/src/flutter/lib/ui/painting/gradient.cc b/engine/src/flutter/lib/ui/painting/gradient.cc index 7035df81b6..f6bb864627 100644 --- a/engine/src/flutter/lib/ui/painting/gradient.cc +++ b/engine/src/flutter/lib/ui/painting/gradient.cc @@ -59,17 +59,24 @@ void CanvasGradient::initRadial(double center_x, double radius, const tonic::Int32List& colors, const tonic::Float32List& color_stops, - SkShader::TileMode tile_mode) { + SkShader::TileMode tile_mode, + const tonic::Float64List& matrix4) { FXL_DCHECK(colors.num_elements() == color_stops.num_elements() || color_stops.data() == nullptr); static_assert(sizeof(SkColor) == sizeof(int32_t), "SkColor doesn't use int32_t."); + SkMatrix sk_matrix; + bool has_matrix = matrix4.data() != nullptr; + if (has_matrix) { + sk_matrix = ToSkMatrix(matrix4); + } + set_shader(SkGradientShader::MakeRadial( SkPoint::Make(center_x, center_y), radius, reinterpret_cast(colors.data()), color_stops.data(), - colors.num_elements(), tile_mode)); + colors.num_elements(), tile_mode, 0, has_matrix ? &sk_matrix : nullptr)); } CanvasGradient::CanvasGradient() : Shader(nullptr) {} diff --git a/engine/src/flutter/lib/ui/painting/gradient.h b/engine/src/flutter/lib/ui/painting/gradient.h index 94a55a57ac..5f0580b178 100644 --- a/engine/src/flutter/lib/ui/painting/gradient.h +++ b/engine/src/flutter/lib/ui/painting/gradient.h @@ -5,9 +5,11 @@ #ifndef FLUTTER_LIB_UI_PAINTING_GRADIENT_H_ #define FLUTTER_LIB_UI_PAINTING_GRADIENT_H_ +#include "flutter/lib/ui/painting/matrix.h" #include "flutter/lib/ui/painting/shader.h" #include "lib/tonic/dart_wrappable.h" #include "lib/tonic/typed_data/float32_list.h" +#include "lib/tonic/typed_data/float64_list.h" #include "lib/tonic/typed_data/int32_list.h" #include "third_party/skia/include/effects/SkGradientShader.h" @@ -38,7 +40,8 @@ class CanvasGradient : public Shader { double radius, const tonic::Int32List& colors, const tonic::Float32List& color_stops, - SkShader::TileMode tile_mode); + SkShader::TileMode tile_mode, + const tonic::Float64List& matrix4); static void RegisterNatives(tonic::DartLibraryNatives* natives);