Flutter Engine
gradient.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "flutter/lib/ui/painting/gradient.h"
6 
11 
12 namespace flutter {
13 
14 typedef CanvasGradient
15  Gradient; // Because the C++ name doesn't match the Dart name.
16 
17 static void Gradient_constructor(Dart_NativeArguments args) {
20 }
21 
23 
24 #define FOR_EACH_BINDING(V) \
25  V(Gradient, initLinear) \
26  V(Gradient, initRadial) \
27  V(Gradient, initSweep) \
28  V(Gradient, initTwoPointConical)
29 
31 
32 void CanvasGradient::RegisterNatives(tonic::DartLibraryNatives* natives) {
33  natives->Register({{"Gradient_constructor", Gradient_constructor, 1, true},
35 }
36 
38  return fml::MakeRefCounted<CanvasGradient>();
39 }
40 
41 void CanvasGradient::initLinear(const tonic::Float32List& end_points,
42  const tonic::Int32List& colors,
43  const tonic::Float32List& color_stops,
44  SkTileMode tile_mode,
45  const tonic::Float64List& matrix4) {
46  FML_DCHECK(end_points.num_elements() == 4);
47  FML_DCHECK(colors.num_elements() == color_stops.num_elements() ||
48  color_stops.data() == nullptr);
49 
50  static_assert(sizeof(SkPoint) == sizeof(float) * 2,
51  "SkPoint doesn't use floats.");
52  static_assert(sizeof(SkColor) == sizeof(int32_t),
53  "SkColor doesn't use int32_t.");
54 
55  SkMatrix sk_matrix;
56  bool has_matrix = matrix4.data() != nullptr;
57  if (has_matrix) {
58  sk_matrix = ToSkMatrix(matrix4);
59  }
60 
61  set_shader(UIDartState::CreateGPUObject(SkGradientShader::MakeLinear(
62  reinterpret_cast<const SkPoint*>(end_points.data()),
63  reinterpret_cast<const SkColor*>(colors.data()), color_stops.data(),
64  colors.num_elements(), tile_mode, 0, has_matrix ? &sk_matrix : nullptr)));
65 }
66 
67 void CanvasGradient::initRadial(double center_x,
68  double center_y,
69  double radius,
70  const tonic::Int32List& colors,
71  const tonic::Float32List& color_stops,
72  SkTileMode tile_mode,
73  const tonic::Float64List& matrix4) {
74  FML_DCHECK(colors.num_elements() == color_stops.num_elements() ||
75  color_stops.data() == nullptr);
76 
77  static_assert(sizeof(SkColor) == sizeof(int32_t),
78  "SkColor doesn't use int32_t.");
79 
80  SkMatrix sk_matrix;
81  bool has_matrix = matrix4.data() != nullptr;
82  if (has_matrix) {
83  sk_matrix = ToSkMatrix(matrix4);
84  }
85 
86  set_shader(UIDartState::CreateGPUObject(SkGradientShader::MakeRadial(
87  SkPoint::Make(center_x, center_y), radius,
88  reinterpret_cast<const SkColor*>(colors.data()), color_stops.data(),
89  colors.num_elements(), tile_mode, 0, has_matrix ? &sk_matrix : nullptr)));
90 }
91 
92 void CanvasGradient::initSweep(double center_x,
93  double center_y,
94  const tonic::Int32List& colors,
95  const tonic::Float32List& color_stops,
96  SkTileMode tile_mode,
97  double start_angle,
98  double end_angle,
99  const tonic::Float64List& matrix4) {
100  FML_DCHECK(colors.num_elements() == color_stops.num_elements() ||
101  color_stops.data() == nullptr);
102 
103  static_assert(sizeof(SkColor) == sizeof(int32_t),
104  "SkColor doesn't use int32_t.");
105 
106  SkMatrix sk_matrix;
107  bool has_matrix = matrix4.data() != nullptr;
108  if (has_matrix) {
109  sk_matrix = ToSkMatrix(matrix4);
110  }
111 
112  set_shader(UIDartState::CreateGPUObject(SkGradientShader::MakeSweep(
113  center_x, center_y, reinterpret_cast<const SkColor*>(colors.data()),
114  color_stops.data(), colors.num_elements(), tile_mode,
115  start_angle * 180.0 / M_PI, end_angle * 180.0 / M_PI, 0,
116  has_matrix ? &sk_matrix : nullptr)));
117 }
118 
120  double start_y,
121  double start_radius,
122  double end_x,
123  double end_y,
124  double end_radius,
125  const tonic::Int32List& colors,
126  const tonic::Float32List& color_stops,
127  SkTileMode tile_mode,
128  const tonic::Float64List& matrix4) {
129  FML_DCHECK(colors.num_elements() == color_stops.num_elements() ||
130  color_stops.data() == nullptr);
131 
132  static_assert(sizeof(SkColor) == sizeof(int32_t),
133  "SkColor doesn't use int32_t.");
134 
135  SkMatrix sk_matrix;
136  bool has_matrix = matrix4.data() != nullptr;
137  if (has_matrix) {
138  sk_matrix = ToSkMatrix(matrix4);
139  }
140 
141  set_shader(UIDartState::CreateGPUObject(SkGradientShader::MakeTwoPointConical(
142  SkPoint::Make(start_x, start_y), start_radius,
143  SkPoint::Make(end_x, end_y), end_radius,
144  reinterpret_cast<const SkColor*>(colors.data()), color_stops.data(),
145  colors.num_elements(), tile_mode, 0, has_matrix ? &sk_matrix : nullptr)));
146 }
147 
148 CanvasGradient::CanvasGradient() = default;
149 
151 
152 } // namespace flutter
G_BEGIN_DECLS FlValue * args
void initRadial(double center_x, double center_y, double radius, const tonic::Int32List &colors, const tonic::Float32List &color_stops, SkTileMode tile_mode, const tonic::Float64List &matrix4)
Definition: gradient.cc:67
void initLinear(const tonic::Float32List &end_points, const tonic::Int32List &colors, const tonic::Float32List &color_stops, SkTileMode tile_mode, const tonic::Float64List &matrix4)
Definition: gradient.cc:41
#define FML_DCHECK(condition)
Definition: logging.h:86
void DartCallConstructor(Sig func, Dart_NativeArguments args)
Definition: dart_args.h:218
static fml::RefPtr< CanvasGradient > Create()
Definition: gradient.cc:37
static void Gradient_constructor(Dart_NativeArguments args)
Definition: gradient.cc:17
#define DART_NATIVE_CALLBACK(CLASS, METHOD)
void initTwoPointConical(double start_x, double start_y, double start_radius, double end_x, double end_y, double end_radius, const tonic::Int32List &colors, const tonic::Float32List &color_stops, SkTileMode tile_mode, const tonic::Float64List &matrix4)
Definition: gradient.cc:119
IMPLEMENT_WRAPPERTYPEINFO(ui, Scene)
void set_shader(flutter::SkiaGPUObject< SkShader > shader)
Definition: shader.h:24
static void ThrowIfUIOperationsProhibited()
void initSweep(double center_x, double center_y, const tonic::Int32List &colors, const tonic::Float32List &color_stops, SkTileMode tile_mode, double start_angle, double end_angle, const tonic::Float64List &matrix4)
Definition: gradient.cc:92
#define FOR_EACH_BINDING(V)
Definition: gradient.cc:24
#define DART_REGISTER_NATIVE(CLASS, METHOD)
SkMatrix ToSkMatrix(const tonic::Float64List &matrix4)
Definition: matrix.cc:20
static flutter::SkiaGPUObject< T > CreateGPUObject(sk_sp< T > object)
Definition: ui_dart_state.h:78
CanvasGradient Gradient
Definition: gradient.cc:15