Skip to content

Commit 7db2cc8

Browse files
Swiftaxemdebbar
andauthored
refactor(web): SkwasmPath and SkwasmPathBuilder separation (flutter#189086)
SkwasmPath was implementing both the immutable and mutable interfaces. To separate concerns, SkwasmPath now implements the immutable interface, and a SkwasmPathBuilder class is added for the mutable interface. Also, test coverage improved to verify that nothing breaks. Closes flutter#184840 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. --------- Co-authored-by: Mouad Debbar <[email protected]>
1 parent 5725b62 commit 7db2cc8

3 files changed

Lines changed: 262 additions & 47 deletions

File tree

‎engine/src/flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/path.dart‎

Lines changed: 71 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ enum PathDirection { clockwise, counterClockwise }
1414

1515
enum PathArcSize { small, large }
1616

17-
class SkwasmPath implements BackendPath, BackendPathBuilder {
18-
factory SkwasmPath() {
19-
return SkwasmPath.fromHandle(pathCreate());
20-
}
21-
22-
factory SkwasmPath.from(SkwasmPath source) {
23-
return SkwasmPath.fromHandle(pathCopy(source.handle));
24-
}
17+
ui.Rect _getPathBounds(Pointer<RawPath> handle) {
18+
return withStackScope((StackScope s) {
19+
final Pointer<Float> rectBuffer = s.allocFloatArray(4);
20+
pathGetBounds(handle, rectBuffer);
21+
return s.convertRectFromNative(rectBuffer);
22+
});
23+
}
2524

25+
class SkwasmPath implements BackendPath {
2626
SkwasmPath.fromHandle(this.handle);
2727

2828
final Pointer<RawPath> handle;
@@ -31,12 +31,62 @@ class SkwasmPath implements BackendPath, BackendPathBuilder {
3131
@override
3232
void dispose() {
3333
assert(!_isDisposed, 'SkwasmPath has already been disposed.');
34-
pathDispose(handle);
35-
_isDisposed = true;
34+
if (!_isDisposed) {
35+
pathDispose(handle);
36+
_isDisposed = true;
37+
}
38+
}
39+
40+
bool contains(ui.Offset point) => pathContains(handle, point.dx, point.dy);
41+
ui.PathFillType get fillType => ui.PathFillType.values[pathGetFillType(handle)];
42+
43+
@override
44+
ui.Rect getBounds() => _getPathBounds(handle);
45+
46+
@override
47+
SkwasmPathMetricIterator computeMetrics({bool forceClosed = false}) {
48+
return SkwasmPathMetricIterator(this, forceClosed);
49+
}
50+
51+
@override
52+
String toSvgString() {
53+
final SkStringHandle skString = pathGetSvgString(handle);
54+
final Pointer<Int8> buffer = skStringGetData(skString);
55+
final int length = skStringGetLength(skString);
56+
final String svgString = utf8.decode(buffer.toUint8List(length));
57+
skStringFree(skString);
58+
return svgString;
59+
}
60+
}
61+
62+
class SkwasmPathBuilder implements BackendPathBuilder {
63+
factory SkwasmPathBuilder() {
64+
return SkwasmPathBuilder.fromHandle(pathCreate());
65+
}
66+
67+
factory SkwasmPathBuilder.from(SkwasmPath source) {
68+
return SkwasmPathBuilder.fromHandle(pathCopy(source.handle));
3669
}
3770

71+
SkwasmPathBuilder.fromHandle(this.handle);
72+
73+
final Pointer<RawPath> handle;
74+
bool _isDisposed = false;
75+
3876
@override
39-
SkwasmPath build() => this;
77+
SkwasmPath build() {
78+
assert(!_isDisposed, 'SkwasmPathBuilder has been disposed.');
79+
return SkwasmPath.fromHandle(pathCopy(handle));
80+
}
81+
82+
@override
83+
void dispose() {
84+
assert(!_isDisposed, 'SkwasmPathBuilder has already been disposed.');
85+
if (!_isDisposed) {
86+
pathDispose(handle);
87+
_isDisposed = true;
88+
}
89+
}
4090

4191
@override
4292
ui.PathFillType get fillType => ui.PathFillType.values[pathGetFillType(handle)];
@@ -231,43 +281,25 @@ class SkwasmPath implements BackendPath, BackendPathBuilder {
231281
}
232282

233283
@override
234-
ui.Rect getBounds() {
235-
return withStackScope((StackScope s) {
236-
final Pointer<Float> rectBuffer = s.allocFloatArray(4);
237-
pathGetBounds(handle, rectBuffer);
238-
return s.convertRectFromNative(rectBuffer);
239-
});
240-
}
241-
242-
static SkwasmPath combine(ui.PathOperation operation, SkwasmPath path1, SkwasmPath path2) =>
243-
SkwasmPath.fromHandle(pathCombine(operation.index, path1.handle, path2.handle));
284+
ui.Rect getBounds() => _getPathBounds(handle);
244285

245-
@override
246-
SkwasmPathMetricIterator computeMetrics({bool forceClosed = false}) {
247-
return SkwasmPathMetricIterator(this, forceClosed);
248-
}
249-
250-
@override
251-
String toSvgString() {
252-
final SkStringHandle skString = pathGetSvgString(handle);
253-
final Pointer<Int8> buffer = skStringGetData(skString);
254-
final int length = skStringGetLength(skString);
255-
final String svgString = utf8.decode(buffer.toUint8List(length));
256-
skStringFree(skString);
257-
return svgString;
258-
}
286+
static SkwasmPathBuilder combine(
287+
ui.PathOperation operation,
288+
SkwasmPath path1,
289+
SkwasmPath path2,
290+
) => SkwasmPathBuilder.fromHandle(pathCombine(operation.index, path1.handle, path2.handle));
259291
}
260292

261293
class SkwasmPathConstructors implements BackendPathConstructors {
262294
@override
263-
SkwasmPath createNew() => SkwasmPath();
295+
SkwasmPathBuilder createNew() => SkwasmPathBuilder();
264296

265297
@override
266-
BackendPathBuilder fromPath(BackendPath path) => SkwasmPath.from(path as SkwasmPath);
298+
BackendPathBuilder fromPath(BackendPath path) => SkwasmPathBuilder.from(path as SkwasmPath);
267299

268300
@override
269-
SkwasmPath combinePaths(ui.PathOperation operation, BackendPath path1, BackendPath path2) {
270-
return SkwasmPath.combine(operation, path1 as SkwasmPath, path2 as SkwasmPath);
301+
SkwasmPathBuilder combinePaths(ui.PathOperation operation, BackendPath path1, BackendPath path2) {
302+
return SkwasmPathBuilder.combine(operation, path1 as SkwasmPath, path2 as SkwasmPath);
271303
}
272304
}
273305

‎engine/src/flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/path_metrics.dart‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ class SkwasmPathMetric implements BackendPathMetric {
6464

6565
@override
6666
BackendPathBuilder extractPath(double start, double end, {bool startWithMoveTo = true}) {
67-
return SkwasmPath.fromHandle(contourMeasureGetSegment(handle, start, end, startWithMoveTo));
67+
return SkwasmPathBuilder.fromHandle(
68+
contourMeasureGetSegment(handle, start, end, startWithMoveTo),
69+
);
6870
}
6971

7072
@override

‎engine/src/flutter/lib/web_ui/test/ui/path_test.dart‎

Lines changed: 188 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'dart:math' show pi;
56
import 'dart:typed_data' show Float64List;
67

78
import 'package:test/bootstrap/browser.dart';
@@ -248,13 +249,193 @@ Future<void> testMain() async {
248249
expect(newFirstMetric.extractPath(4.0, 10.0).computeMetrics().first.length, 6.0);
249250
});
250251

251-
test('path relativeLineTo', () {
252-
final p = Path();
253-
p.moveTo(100, 100);
254-
p.relativeLineTo(-50, -50);
255-
p.relativeLineTo(100, 0);
256-
p.relativeLineTo(-50, 50);
257-
expect(p.getBounds(), equals(const Rect.fromLTRB(50.0, 50.0, 150.0, 100.0)));
252+
group('path building', () {
253+
test('fillType', () {
254+
final p = Path();
255+
expect(p.fillType, equals(PathFillType.nonZero));
256+
p.fillType = PathFillType.evenOdd;
257+
expect(p.fillType, equals(PathFillType.evenOdd));
258+
});
259+
260+
test('moveTo', () {
261+
final p = Path();
262+
p.moveTo(10, 10);
263+
p.lineTo(20, 10);
264+
expect(p.getBounds(), equals(const Rect.fromLTRB(10.0, 10.0, 20.0, 10.0)));
265+
});
266+
267+
test('relativeMoveTo', () {
268+
final p = Path();
269+
p.relativeMoveTo(10, 10);
270+
p.lineTo(20, 10);
271+
expect(p.getBounds(), equals(const Rect.fromLTRB(10.0, 10.0, 20.0, 10.0)));
272+
});
273+
274+
test('lineTo', () {
275+
final p = Path();
276+
p.lineTo(0, 10);
277+
expect(p.getBounds(), equals(const Rect.fromLTRB(0.0, 0.0, 0.0, 10.0)));
278+
});
279+
280+
test('path relativeLineTo', () {
281+
final p = Path();
282+
p.moveTo(100, 100);
283+
p.relativeLineTo(-50, -50);
284+
p.relativeLineTo(100, 0);
285+
p.relativeLineTo(-50, 50);
286+
expect(p.getBounds(), equals(const Rect.fromLTRB(50.0, 50.0, 150.0, 100.0)));
287+
});
288+
289+
test('quadraticBezierTo', () {
290+
final p = Path();
291+
p.quadraticBezierTo(0, 10, 10, 0);
292+
expect(p.getBounds(), equals(const Rect.fromLTRB(0.0, 0.0, 10.0, 10.0)));
293+
});
294+
295+
test('relativeQuadraticBezierTo', () {
296+
final p = Path();
297+
p.moveTo(10, 10);
298+
// Relative control (0,10), end (10,0) → absolute control (10,20), end (20,10).
299+
// getBounds() returns the control-polygon bbox: (10,10), (10,20), (20,10).
300+
p.relativeQuadraticBezierTo(0, 10, 10, 0);
301+
expect(p.getBounds(), equals(const Rect.fromLTRB(10.0, 10.0, 20.0, 20.0)));
302+
});
303+
304+
test('cubicTo', () {
305+
final p = Path();
306+
// getBounds() returns the control-polygon bbox: (0,0), (0,10), (10,10), (10,0).
307+
p.cubicTo(0, 10, 10, 10, 10, 0);
308+
expect(p.getBounds(), equals(const Rect.fromLTRB(0.0, 0.0, 10.0, 10.0)));
309+
});
310+
311+
test('relativeCubicTo', () {
312+
final p = Path();
313+
p.moveTo(5, 5);
314+
// Absolute control points: (5,15), (15,15), end (15,5).
315+
// getBounds() returns the control-polygon bbox: (5,5), (5,15), (15,15), (15,5).
316+
p.relativeCubicTo(0, 10, 10, 10, 10, 0);
317+
expect(p.getBounds(), equals(const Rect.fromLTRB(5.0, 5.0, 15.0, 15.0)));
318+
});
319+
320+
test('conicTo', () {
321+
final p = Path();
322+
// Conic with w=1 is equivalent to a quadratic bezier.
323+
// getBounds() returns the control-polygon bbox: (0,0), (0,10), (10,0).
324+
p.conicTo(0, 10, 10, 0, 1);
325+
expect(p.getBounds(), equals(const Rect.fromLTRB(0.0, 0.0, 10.0, 10.0)));
326+
});
327+
328+
test('relativeConicTo', () {
329+
final p = Path();
330+
p.moveTo(10, 10);
331+
// Relative conic with w=1: equivalent to relativeQuadraticBezierTo.
332+
// getBounds() returns the control-polygon bbox: (10,10), (10,20), (20,10).
333+
p.relativeConicTo(0, 10, 10, 0, 1);
334+
expect(p.getBounds(), equals(const Rect.fromLTRB(10.0, 10.0, 20.0, 20.0)));
335+
});
336+
337+
test('arcTo', () {
338+
final p = Path();
339+
// Quarter-circle arc from (20,10) to (10,20), clockwise.
340+
// Control-polygon bbox spans the bottom-right quadrant of the oval.
341+
p.arcTo(const Rect.fromLTRB(0.0, 0.0, 20.0, 20.0), 0, pi / 2, true);
342+
expect(p.getBounds(), equals(const Rect.fromLTRB(10.0, 10.0, 20.0, 20.0)));
343+
});
344+
345+
test('arcToPoint', () {
346+
final p = Path();
347+
p.moveTo(10, 0);
348+
// Small clockwise quarter-circle from (10,0) to (0,10) with radius 10.
349+
// Center at the origin; the arc spans the first quadrant.
350+
p.arcToPoint(const Offset(0, 10), radius: const Radius.circular(10));
351+
expect(p.getBounds(), equals(const Rect.fromLTRB(0.0, 0.0, 10.0, 10.0)));
352+
});
353+
354+
test('relativeArcToPoint', () {
355+
final p = Path();
356+
p.moveTo(10, 0);
357+
// Relative (-10, 10) from (10, 0) = absolute endpoint (0, 10): same arc as arcToPoint.
358+
p.relativeArcToPoint(const Offset(-10, 10), radius: const Radius.circular(10));
359+
expect(p.getBounds(), equals(const Rect.fromLTRB(0.0, 0.0, 10.0, 10.0)));
360+
});
361+
362+
test('addRect', () {
363+
final p = Path();
364+
p.addRect(const Rect.fromLTRB(10.0, 20.0, 30.0, 40.0));
365+
expect(p.getBounds(), equals(const Rect.fromLTRB(10.0, 20.0, 30.0, 40.0)));
366+
});
367+
368+
test('addOval', () {
369+
final p = Path();
370+
p.addOval(const Rect.fromLTRB(10.0, 20.0, 30.0, 40.0));
371+
expect(p.getBounds(), equals(const Rect.fromLTRB(10.0, 20.0, 30.0, 40.0)));
372+
});
373+
374+
test('addArc', () {
375+
final p = Path();
376+
p.addArc(const Rect.fromLTRB(10.0, 20.0, 30.0, 40.0), 0, 2 * pi);
377+
expect(p.getBounds(), equals(const Rect.fromLTRB(10.0, 20.0, 30.0, 40.0)));
378+
});
379+
380+
test('addPolygon', () {
381+
final p = Path();
382+
p.addPolygon(const <Offset>[
383+
Offset(10.0, 20.0),
384+
Offset(30.0, 40.0),
385+
Offset(50.0, 60.0),
386+
], true);
387+
expect(p.getBounds(), equals(const Rect.fromLTRB(10.0, 20.0, 50.0, 60.0)));
388+
});
389+
390+
test('addRRect', () {
391+
final p = Path();
392+
p.addRRect(RRect.fromLTRBR(10.0, 20.0, 30.0, 40.0, const Radius.circular(5.0)));
393+
expect(p.getBounds(), equals(const Rect.fromLTRB(10.0, 20.0, 30.0, 40.0)));
394+
});
395+
396+
test('addRSuperellipse', () {
397+
final p = Path();
398+
p.addRSuperellipse(
399+
RSuperellipse.fromLTRBR(10.0, 20.0, 30.0, 40.0, const Radius.circular(5.0)),
400+
);
401+
expect(p.getBounds(), equals(const Rect.fromLTRB(10.0, 20.0, 30.0, 40.0)));
402+
});
403+
404+
test('addPath', () {
405+
final p1 = Path()..addRect(const Rect.fromLTRB(10.0, 20.0, 30.0, 40.0));
406+
final p2 = Path()..addRect(const Rect.fromLTRB(50.0, 60.0, 70.0, 80.0));
407+
p1.addPath(p2, const Offset(10.0, 10.0));
408+
expect(p1.getBounds(), equals(const Rect.fromLTRB(10.0, 20.0, 80.0, 90.0)));
409+
});
410+
411+
test('extendWithPath', () {
412+
final p1 = Path()..addRect(const Rect.fromLTRB(10.0, 20.0, 30.0, 40.0));
413+
final p2 = Path()..addRect(const Rect.fromLTRB(50.0, 60.0, 70.0, 80.0));
414+
p1.extendWithPath(p2, const Offset(10.0, 10.0));
415+
expect(p1.getBounds(), equals(const Rect.fromLTRB(10.0, 20.0, 80.0, 90.0)));
416+
});
417+
418+
test('close', () {
419+
final p = Path();
420+
p.lineTo(10.0, 10.0);
421+
expect(p.getBounds(), equals(const Rect.fromLTRB(0.0, 0.0, 10.0, 10.0)));
422+
p.close();
423+
expect(p.getBounds(), equals(const Rect.fromLTRB(0.0, 0.0, 10.0, 10.0)));
424+
});
425+
426+
test('reset', () {
427+
final p = Path();
428+
p.lineTo(10.0, 10.0);
429+
expect(p.getBounds(), equals(const Rect.fromLTRB(0.0, 0.0, 10.0, 10.0)));
430+
p.reset();
431+
expect(p.getBounds(), equals(Rect.zero));
432+
});
433+
434+
test('contains', () {
435+
final p = Path()..addRect(const Rect.fromLTRB(0, 0, 10, 10));
436+
expect(p.contains(const Offset(5, 5)), isTrue);
437+
expect(p.contains(const Offset(15, 5)), isFalse);
438+
});
258439
});
259440

260441
test('path containing polygon with thousands of points', () {

0 commit comments

Comments
 (0)