Skip to content

Commit e0d5153

Browse files
authored
Make CkVertices a ManagedSkiaObject (flutter#20421)
1 parent 94f5cf4 commit e0d5153

File tree

4 files changed

+116
-14
lines changed

4 files changed

+116
-14
lines changed

lib/web_ui/lib/src/engine/compositor/canvas.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
// @dart = 2.10
66
part of engine;
77

8-
/// A Dart wrapper around Skia's SKCanvas.
8+
/// A Dart wrapper around Skia's [SkCanvas].
9+
///
10+
/// This is intentionally not memory-managing the underlying [SkCanvas]. See
11+
/// the docs on [SkCanvas], which explain the reason.
912
class CkCanvas {
1013
final SkCanvas skCanvas;
1114

@@ -203,7 +206,7 @@ class CkCanvas {
203206
ui.Vertices vertices, ui.BlendMode blendMode, CkPaint paint) {
204207
CkVertices skVertices = vertices as CkVertices;
205208
skCanvas.drawVertices(
206-
skVertices.skVertices,
209+
skVertices.skiaObject,
207210
toSkBlendMode(blendMode),
208211
paint.skiaObject,
209212
);

lib/web_ui/lib/src/engine/compositor/canvaskit_api.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,11 @@ class SkPictureRecorder {
12461246
external void delete();
12471247
}
12481248

1249+
/// We do not use the `delete` method (which may be removed in the future anyway).
1250+
///
1251+
/// By Skia coding convention raw pointers should always be treated as
1252+
/// "borrowed", i.e. their memory is managed by other objects. In the case of
1253+
/// [SkCanvas] it is managed by [SkPictureRecorder].
12491254
@JS()
12501255
class SkCanvas {
12511256
external void clear(Float32List color);
@@ -1547,7 +1552,9 @@ class SkTextRange {
15471552
}
15481553

15491554
@JS()
1550-
class SkVertices {}
1555+
class SkVertices {
1556+
external void delete();
1557+
}
15511558

15521559
@JS()
15531560
@anonymous

lib/web_ui/lib/src/engine/compositor/vertices.dart

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@
55
// @dart = 2.10
66
part of engine;
77

8-
class CkVertices implements ui.Vertices {
9-
late SkVertices skVertices;
10-
11-
CkVertices(
8+
class CkVertices extends ManagedSkiaObject<SkVertices> implements ui.Vertices {
9+
factory CkVertices(
1210
ui.VertexMode mode,
1311
List<ui.Offset> positions, {
1412
List<ui.Offset>? textureCoordinates,
1513
List<ui.Color>? colors,
1614
List<int>? indices,
17-
}) : assert(mode != null), // ignore: unnecessary_null_comparison
18-
assert(positions != null) { // ignore: unnecessary_null_comparison
15+
}) {
16+
assert(mode != null); // ignore: unnecessary_null_comparison
17+
assert(positions != null); // ignore: unnecessary_null_comparison
1918
if (textureCoordinates != null &&
2019
textureCoordinates.length != positions.length)
2120
throw ArgumentError(
@@ -27,7 +26,7 @@ class CkVertices implements ui.Vertices {
2726
throw ArgumentError(
2827
'"indices" values must be valid indices in the positions list.');
2928

30-
skVertices = canvasKit.MakeSkVertices(
29+
return CkVertices._(
3130
toSkVertexMode(mode),
3231
toSkPoints2d(positions),
3332
textureCoordinates != null ? toSkPoints2d(textureCoordinates) : null,
@@ -36,14 +35,15 @@ class CkVertices implements ui.Vertices {
3635
);
3736
}
3837

39-
CkVertices.raw(
38+
factory CkVertices.raw(
4039
ui.VertexMode mode,
4140
Float32List positions, {
4241
Float32List? textureCoordinates,
4342
Int32List? colors,
4443
Uint16List? indices,
45-
}) : assert(mode != null), // ignore: unnecessary_null_comparison
46-
assert(positions != null) { // ignore: unnecessary_null_comparison
44+
}) {
45+
assert(mode != null); // ignore: unnecessary_null_comparison
46+
assert(positions != null); // ignore: unnecessary_null_comparison
4747
if (textureCoordinates != null &&
4848
textureCoordinates.length != positions.length)
4949
throw ArgumentError(
@@ -55,12 +55,47 @@ class CkVertices implements ui.Vertices {
5555
throw ArgumentError(
5656
'"indices" values must be valid indices in the positions list.');
5757

58-
skVertices = canvasKit.MakeSkVertices(
58+
return CkVertices._(
5959
toSkVertexMode(mode),
6060
rawPointsToSkPoints2d(positions),
6161
textureCoordinates != null ? rawPointsToSkPoints2d(textureCoordinates) : null,
6262
colors != null ? encodeRawColorList(colors) : null,
6363
indices,
6464
);
6565
}
66+
67+
CkVertices._(
68+
this._mode,
69+
this._positions,
70+
this._textureCoordinates,
71+
this._colors,
72+
this._indices,
73+
);
74+
75+
final SkVertexMode _mode;
76+
final List<Float32List> _positions;
77+
final List<Float32List>? _textureCoordinates;
78+
final List<Float32List>? _colors;
79+
final Uint16List? _indices;
80+
81+
@override
82+
SkVertices createDefault() {
83+
return canvasKit.MakeSkVertices(
84+
_mode,
85+
_positions,
86+
_textureCoordinates,
87+
_colors,
88+
_indices,
89+
);
90+
}
91+
92+
@override
93+
SkVertices resurrect() {
94+
return createDefault();
95+
}
96+
97+
@override
98+
void delete() {
99+
rawSkiaObject?.delete();
100+
}
66101
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
// @dart = 2.6
6+
import 'package:test/test.dart';
7+
import 'package:ui/src/engine.dart';
8+
import 'package:ui/ui.dart' as ui;
9+
10+
import 'common.dart';
11+
12+
void main() {
13+
group('Vertices', () {
14+
setUpAll(() async {
15+
await ui.webOnlyInitializePlatform();
16+
});
17+
18+
test('can be constructed, drawn, and deleted', () {
19+
final CkVertices vertices = _testVertices();
20+
expect(vertices, isA<CkVertices>());
21+
expect(vertices.createDefault(), isNotNull);
22+
expect(vertices.resurrect(), isNotNull);
23+
24+
final recorder = CkPictureRecorder();
25+
final canvas = recorder.beginRecording(const ui.Rect.fromLTRB(0, 0, 100, 100));
26+
canvas.drawVertices(
27+
vertices,
28+
ui.BlendMode.srcOver,
29+
ui.Paint(),
30+
);
31+
vertices.delete();
32+
});
33+
// TODO: https://github.com/flutter/flutter/issues/60040
34+
}, skip: isIosSafari);
35+
}
36+
37+
ui.Vertices _testVertices() {
38+
return ui.Vertices(
39+
ui.VertexMode.triangles,
40+
<ui.Offset>[
41+
ui.Offset(0, 0),
42+
ui.Offset(10, 10),
43+
ui.Offset(0, 20),
44+
],
45+
textureCoordinates: <ui.Offset>[
46+
ui.Offset(0, 0),
47+
ui.Offset(10, 10),
48+
ui.Offset(0, 20),
49+
],
50+
colors: <ui.Color>[
51+
ui.Color.fromRGBO(255, 0, 0, 1.0),
52+
ui.Color.fromRGBO(0, 255, 0, 1.0),
53+
ui.Color.fromRGBO(0, 0, 255, 1.0),
54+
],
55+
indices: <int>[0, 1, 2],
56+
);
57+
}

0 commit comments

Comments
 (0)