Skip to content

Commit c3f4c1a

Browse files
authored
Migrate Path to AssociateWithDartWrapper (flutter#16753)
1 parent afe7968 commit c3f4c1a

File tree

5 files changed

+68
-55
lines changed

5 files changed

+68
-55
lines changed

lib/ui/painting.dart

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,20 +1893,20 @@ class Path extends NativeFieldWrapperClass2 {
18931893
Path() { _constructor(); }
18941894
void _constructor() native 'Path_constructor';
18951895

1896-
// Workaround for tonic, which expects classes with native fields to have a
1897-
// private constructor.
1898-
// TODO(dnfield): rework this to use ClaimNativeField - https://github.com/flutter/flutter/issues/50997
1899-
@pragma('vm:entry-point')
1900-
Path._() { _constructor(); }
1896+
/// Avoids creating a new native backing for the path for methods that will
1897+
/// create it later, such as [Path.from], [shift] and [transform].
1898+
Path._();
19011899

19021900
/// Creates a copy of another [Path].
19031901
///
19041902
/// This copy is fast and does not require additional memory unless either
19051903
/// the `source` path or the path returned by this constructor are modified.
19061904
factory Path.from(Path source) {
1907-
return source._clone();
1905+
final Path clonedPath = Path._();
1906+
source._clone(clonedPath);
1907+
return clonedPath;
19081908
}
1909-
Path _clone() native 'Path_clone';
1909+
void _clone(Path outPath) native 'Path_clone';
19101910

19111911
/// Determines how the interior of this path is calculated.
19121912
///
@@ -2169,17 +2169,21 @@ class Path extends NativeFieldWrapperClass2 {
21692169
/// sub-path translated by the given offset.
21702170
Path shift(Offset offset) {
21712171
assert(_offsetIsValid(offset));
2172-
return _shift(offset.dx, offset.dy);
2172+
final Path path = Path._();
2173+
_shift(path, offset.dx, offset.dy);
2174+
return path;
21732175
}
2174-
Path _shift(double dx, double dy) native 'Path_shift';
2176+
void _shift(Path outPath, double dx, double dy) native 'Path_shift';
21752177

21762178
/// Returns a copy of the path with all the segments of every
21772179
/// sub-path transformed by the given matrix.
21782180
Path transform(Float64List matrix4) {
21792181
assert(_matrix4IsValid(matrix4));
2180-
return _transform(matrix4);
2182+
final Path path = Path._();
2183+
_transform(path, matrix4);
2184+
return path;
21812185
}
2182-
Path _transform(Float64List matrix4) native 'Path_transform';
2186+
void _transform(Path outPath, Float64List matrix4) native 'Path_transform';
21832187

21842188
/// Computes the bounding rectangle for this path.
21852189
///
@@ -2455,9 +2459,11 @@ class _PathMeasure extends NativeFieldWrapperClass2 {
24552459

24562460
Path extractPath(int contourIndex, double start, double end, {bool startWithMoveTo = true}) {
24572461
assert(contourIndex <= currentContourIndex, 'Iterator must be advanced before index $contourIndex can be used.');
2458-
return _extractPath(contourIndex, start, end, startWithMoveTo: startWithMoveTo);
2462+
final Path path = Path._();
2463+
_extractPath(path, contourIndex, start, end, startWithMoveTo: startWithMoveTo);
2464+
return path;
24592465
}
2460-
Path _extractPath(int contourIndex, double start, double end, {bool startWithMoveTo = true}) native 'PathMeasure_getSegment';
2466+
void _extractPath(Path outPath, int contourIndex, double start, double end, {bool startWithMoveTo = true}) native 'PathMeasure_getSegment';
24612467

24622468
bool isClosed(int contourIndex) {
24632469
assert(contourIndex <= currentContourIndex, 'Iterator must be advanced before index $contourIndex can be used.');

lib/ui/painting/path.cc

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace flutter {
2020
typedef CanvasPath Path;
2121

2222
static void Path_constructor(Dart_NativeArguments args) {
23-
DartCallConstructor(&CanvasPath::Create, args);
23+
DartCallConstructor(&CanvasPath::CreateNew, args);
2424
}
2525

2626
IMPLEMENT_WRAPPERTYPEINFO(ui, Path);
@@ -262,17 +262,16 @@ bool CanvasPath::contains(double x, double y) {
262262
return path_.contains(x, y);
263263
}
264264

265-
fml::RefPtr<CanvasPath> CanvasPath::shift(double dx, double dy) {
266-
fml::RefPtr<CanvasPath> path = CanvasPath::Create();
265+
void CanvasPath::shift(Dart_Handle path_handle, double dx, double dy) {
266+
fml::RefPtr<CanvasPath> path = CanvasPath::Create(path_handle);
267267
path_.offset(dx, dy, &path->path_);
268-
return path;
269268
}
270269

271-
fml::RefPtr<CanvasPath> CanvasPath::transform(tonic::Float64List& matrix4) {
272-
fml::RefPtr<CanvasPath> path = CanvasPath::Create();
270+
void CanvasPath::transform(Dart_Handle path_handle,
271+
tonic::Float64List& matrix4) {
272+
fml::RefPtr<CanvasPath> path = CanvasPath::Create(path_handle);
273273
path_.transform(ToSkMatrix(matrix4), &path->path_);
274274
matrix4.Release();
275-
return path;
276275
}
277276

278277
tonic::Float32List CanvasPath::getBounds() {
@@ -289,12 +288,11 @@ bool CanvasPath::op(CanvasPath* path1, CanvasPath* path2, int operation) {
289288
return Op(path1->path(), path2->path(), (SkPathOp)operation, &path_);
290289
}
291290

292-
fml::RefPtr<CanvasPath> CanvasPath::clone() {
293-
fml::RefPtr<CanvasPath> path = CanvasPath::Create();
291+
void CanvasPath::clone(Dart_Handle path_handle) {
292+
fml::RefPtr<CanvasPath> path = CanvasPath::Create(path_handle);
294293
// per Skia docs, this will create a fast copy
295294
// data is shared until the source path or dest path are mutated
296295
path->path_ = path_;
297-
return path;
298296
}
299297

300298
} // namespace flutter

lib/ui/painting/path.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,19 @@ class CanvasPath : public RefCountedDartWrappable<CanvasPath> {
2323

2424
public:
2525
~CanvasPath() override;
26-
static fml::RefPtr<CanvasPath> Create() {
26+
static fml::RefPtr<CanvasPath> CreateNew(Dart_Handle path_handle) {
2727
return fml::MakeRefCounted<CanvasPath>();
2828
}
2929

30-
static fml::RefPtr<CanvasPath> CreateFrom(const SkPath& src) {
31-
fml::RefPtr<CanvasPath> path = CanvasPath::Create();
30+
static fml::RefPtr<CanvasPath> Create(Dart_Handle path_handle) {
31+
auto path = fml::MakeRefCounted<CanvasPath>();
32+
path->AssociateWithDartWrapper(path_handle);
33+
return path;
34+
}
35+
36+
static fml::RefPtr<CanvasPath> CreateFrom(Dart_Handle path_handle,
37+
const SkPath& src) {
38+
fml::RefPtr<CanvasPath> path = CanvasPath::Create(path_handle);
3239
path->path_ = src;
3340
return path;
3441
}
@@ -95,11 +102,11 @@ class CanvasPath : public RefCountedDartWrappable<CanvasPath> {
95102
void close();
96103
void reset();
97104
bool contains(double x, double y);
98-
fml::RefPtr<CanvasPath> shift(double dx, double dy);
99-
fml::RefPtr<CanvasPath> transform(tonic::Float64List& matrix4);
105+
void shift(Dart_Handle path_handle, double dx, double dy);
106+
void transform(Dart_Handle path_handle, tonic::Float64List& matrix4);
100107
tonic::Float32List getBounds();
101108
bool op(CanvasPath* path1, CanvasPath* path2, int operation);
102-
fml::RefPtr<CanvasPath> clone();
109+
void clone(Dart_Handle path_handle);
103110

104111
const SkPath& path() const { return path_; }
105112

lib/ui/painting/path_measure.cc

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,26 @@ void CanvasPathMeasure::setPath(const CanvasPath* path, bool isClosed) {
6565
path_measure_->reset(skPath, isClosed);
6666
}
6767

68-
float CanvasPathMeasure::getLength(int contourIndex) {
68+
float CanvasPathMeasure::getLength(int contour_index) {
6969
if (static_cast<std::vector<sk_sp<SkContourMeasure>>::size_type>(
70-
contourIndex) < measures_.size()) {
71-
return measures_[contourIndex]->length();
70+
contour_index) < measures_.size()) {
71+
return measures_[contour_index]->length();
7272
}
7373
return -1;
7474
}
7575

76-
tonic::Float32List CanvasPathMeasure::getPosTan(int contourIndex,
76+
tonic::Float32List CanvasPathMeasure::getPosTan(int contour_index,
7777
float distance) {
7878
tonic::Float32List posTan(Dart_NewTypedData(Dart_TypedData_kFloat32, 5));
7979
posTan[0] = 0; // dart code will check for this for failure
8080
if (static_cast<std::vector<sk_sp<SkContourMeasure>>::size_type>(
81-
contourIndex) >= measures_.size()) {
81+
contour_index) >= measures_.size()) {
8282
return posTan;
8383
}
8484

8585
SkPoint pos;
8686
SkVector tan;
87-
bool success = measures_[contourIndex]->getPosTan(distance, &pos, &tan);
87+
bool success = measures_[contour_index]->getPosTan(distance, &pos, &tan);
8888

8989
if (success) {
9090
posTan[0] = 1; // dart code will check for this for success
@@ -97,28 +97,29 @@ tonic::Float32List CanvasPathMeasure::getPosTan(int contourIndex,
9797
return posTan;
9898
}
9999

100-
fml::RefPtr<CanvasPath> CanvasPathMeasure::getSegment(int contourIndex,
101-
float startD,
102-
float stopD,
103-
bool startWithMoveTo) {
100+
void CanvasPathMeasure::getSegment(Dart_Handle path_handle,
101+
int contour_index,
102+
float start_d,
103+
float stop_d,
104+
bool start_with_move_to) {
104105
if (static_cast<std::vector<sk_sp<SkContourMeasure>>::size_type>(
105-
contourIndex) >= measures_.size()) {
106-
return CanvasPath::Create();
106+
contour_index) >= measures_.size()) {
107+
CanvasPath::Create(path_handle);
107108
}
108109
SkPath dst;
109-
bool success =
110-
measures_[contourIndex]->getSegment(startD, stopD, &dst, startWithMoveTo);
110+
bool success = measures_[contour_index]->getSegment(start_d, stop_d, &dst,
111+
start_with_move_to);
111112
if (!success) {
112-
return CanvasPath::Create();
113+
CanvasPath::Create(path_handle);
113114
} else {
114-
return CanvasPath::CreateFrom(dst);
115+
CanvasPath::CreateFrom(path_handle, dst);
115116
}
116117
}
117118

118-
bool CanvasPathMeasure::isClosed(int contourIndex) {
119+
bool CanvasPathMeasure::isClosed(int contour_index) {
119120
if (static_cast<std::vector<sk_sp<SkContourMeasure>>::size_type>(
120-
contourIndex) < measures_.size()) {
121-
return measures_[contourIndex]->isClosed();
121+
contour_index) < measures_.size()) {
122+
return measures_[contour_index]->isClosed();
122123
}
123124
return false;
124125
}

lib/ui/painting/path_measure.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@ class CanvasPathMeasure : public RefCountedDartWrappable<CanvasPathMeasure> {
3232
bool forceClosed);
3333

3434
void setPath(const CanvasPath* path, bool isClosed);
35-
float getLength(int contourIndex);
36-
tonic::Float32List getPosTan(int contourIndex, float distance);
37-
fml::RefPtr<CanvasPath> getSegment(int contourIndex,
38-
float startD,
39-
float stopD,
40-
bool startWithMoveTo);
41-
bool isClosed(int contourIndex);
35+
float getLength(int contour_index);
36+
tonic::Float32List getPosTan(int contour_index, float distance);
37+
void getSegment(Dart_Handle path_handle,
38+
int contour_index,
39+
float start_d,
40+
float stop_d,
41+
bool start_with_move_to);
42+
bool isClosed(int contour_index);
4243
bool nextContour();
4344

4445
static void RegisterNatives(tonic::DartLibraryNatives* natives);

0 commit comments

Comments
 (0)