Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
FlutterMutatorsStack.java
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
5package io.flutter.embedding.engine.mutatorsstack;
6
7import android.graphics.Matrix;
8import android.graphics.Path;
9import android.graphics.Rect;
10import android.graphics.RectF;
11import androidx.annotation.Keep;
12import androidx.annotation.NonNull;
13import androidx.annotation.Nullable;
14import java.util.ArrayList;
15import java.util.List;
16
17/**
18 * The mutator stack containing a list of mutators
19 *
20 * <p>The mutators can be applied to a {@link io.flutter.plugin.platform.PlatformView} to perform a
21 * series mutations. See {@link FlutterMutatorsStack.FlutterMutator} for informations on Mutators.
22 */
23@Keep
25 /**
26 * The type of a Mutator See {@link FlutterMutatorsStack.FlutterMutator} for informations on
27 * Mutators.
28 */
36
37 /**
38 * A class represents a mutator
39 *
40 * <p>A mutator contains information of a single mutation operation that can be applied to a
41 * {@link io.flutter.plugin.platform.PlatformView}. See {@link
42 * FlutterMutatorsStack.FlutterMutator} for informations on Mutators.
43 */
44 public class FlutterMutator {
45
46 @Nullable private Matrix matrix;
47 @Nullable private Rect rect;
48 @Nullable private Path path;
49 @Nullable private float[] radiis;
50
51 private FlutterMutatorType type;
52
53 /**
54 * Initialize a clip rect mutator.
55 *
56 * @param rect the rect to be clipped.
57 */
58 public FlutterMutator(Rect rect) {
59 this.type = FlutterMutatorType.CLIP_RECT;
60 this.rect = rect;
61 }
62
63 /**
64 * Initialize a clip rrect mutator.
65 *
66 * @param rect the rect of the rrect
67 * @param radiis the radiis of the rrect. Array of 8 values, 4 pairs of [X,Y]. This value cannot
68 * be null.
69 */
70 public FlutterMutator(Rect rect, float[] radiis) {
72 this.rect = rect;
73 this.radiis = radiis;
74 }
75
76 /**
77 * Initialize a clip path mutator.
78 *
79 * @param path the path to be clipped.
80 */
81 public FlutterMutator(Path path) {
82 this.type = FlutterMutatorType.CLIP_PATH;
83 this.path = path;
84 }
85
86 /**
87 * Initialize a transform mutator.
88 *
89 * @param matrix the transform matrix to apply.
90 */
91 public FlutterMutator(Matrix matrix) {
92 this.type = FlutterMutatorType.TRANSFORM;
93 this.matrix = matrix;
94 }
95
96 /**
97 * Get the mutator type.
98 *
99 * @return The type of the mutator.
100 */
102 return type;
103 }
104
105 /**
106 * Get the rect of the mutator if the {@link #getType()} returns FlutterMutatorType.CLIP_RECT.
107 *
108 * @return the clipping rect if the type is FlutterMutatorType.CLIP_RECT; otherwise null.
109 */
110 public Rect getRect() {
111 return rect;
112 }
113
114 /**
115 * Get the path of the mutator if the {@link #getType()} returns FlutterMutatorType.CLIP_PATH.
116 *
117 * @return the clipping path if the type is FlutterMutatorType.CLIP_PATH; otherwise null.
118 */
119 public Path getPath() {
120 return path;
121 }
122
123 /**
124 * Get the matrix of the mutator if the {@link #getType()} returns FlutterMutatorType.TRANSFORM.
125 *
126 * @return the matrix if the type is FlutterMutatorType.TRANSFORM; otherwise null.
127 */
128 public Matrix getMatrix() {
129 return matrix;
130 }
131 }
132
133 private @NonNull List<FlutterMutator> mutators;
134
135 private List<Path> finalClippingPaths;
136 private Matrix finalMatrix;
137
138 /** Initialize the mutator stack. */
140 this.mutators = new ArrayList<FlutterMutator>();
141 finalMatrix = new Matrix();
142 finalClippingPaths = new ArrayList<Path>();
143 }
144
145 /**
146 * Push a transform {@link FlutterMutatorsStack.FlutterMutator} to the stack.
147 *
148 * @param values the transform matrix to be pushed to the stack. The array matches how a {@link
149 * android.graphics.Matrix} is constructed.
150 */
151 public void pushTransform(float[] values) {
152 Matrix matrix = new Matrix();
153 matrix.setValues(values);
154 FlutterMutator mutator = new FlutterMutator(matrix);
155 mutators.add(mutator);
156 finalMatrix.preConcat(mutator.getMatrix());
157 }
158
159 /** Push a clipRect {@link FlutterMutatorsStack.FlutterMutator} to the stack. */
160 public void pushClipRect(int left, int top, int right, int bottom) {
161 Rect rect = new Rect(left, top, right, bottom);
162 FlutterMutator mutator = new FlutterMutator(rect);
163 mutators.add(mutator);
164 Path path = new Path();
165 path.addRect(new RectF(rect), Path.Direction.CCW);
166 path.transform(finalMatrix);
167 finalClippingPaths.add(path);
168 }
169
170 /**
171 * Push a clipRRect {@link FlutterMutatorsStack.FlutterMutator} to the stack.
172 *
173 * @param left left offset of the rrect.
174 * @param top top offset of the rrect.
175 * @param right right position of the rrect.
176 * @param bottom bottom position of the rrect.
177 * @param radiis the radiis of the rrect. It must be size of 8, including an x and y for each
178 * corner.
179 */
180 public void pushClipRRect(int left, int top, int right, int bottom, float[] radiis) {
181 Rect rect = new Rect(left, top, right, bottom);
182 FlutterMutator mutator = new FlutterMutator(rect, radiis);
183 mutators.add(mutator);
184 Path path = new Path();
185 path.addRoundRect(new RectF(rect), radiis, Path.Direction.CCW);
186 path.transform(finalMatrix);
187 finalClippingPaths.add(path);
188 }
189
190 /**
191 * Get a list of all the raw mutators. The 0 index of the returned list is the top of the stack.
192 */
194 return mutators;
195 }
196
197 /**
198 * Get a list of all the clipping operations. All the clipping operations -- whether it is clip
199 * rect, clip rrect, or clip path -- are converted into Paths. The paths are also transformed with
200 * the matrix that up to their stack positions. For example: If the stack looks like (from top to
201 * bottom): TransA -&gt; ClipA -&gt; TransB -&gt; ClipB, the final paths will look like
202 * [TransA*ClipA, TransA*TransB*ClipB].
203 *
204 * <p>Clipping this list to the parent canvas of a view results the final clipping path.
205 */
207 return finalClippingPaths;
208 }
209
210 /**
211 * Returns the final matrix. Apply this matrix to the canvas of a view results the final
212 * transformation of the view.
213 */
214 public Matrix getFinalMatrix() {
215 return finalMatrix;
216 }
217}
static bool left(const SkPoint &p0, const SkPoint &p1)
static bool right(const SkPoint &p0, const SkPoint &p1)
void add(sk_sp< SkIDChangeListener > listener) SK_EXCLUDES(fMutex)
void pushClipRRect(int left, int top, int right, int bottom, float[] radiis)
void pushClipRect(int left, int top, int right, int bottom)