Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 0fad460

Browse files
authored
[skwasm] use temporary RawPaint objects (#54917)
Same as #54818, but for Skwasm. Addresses the `Paint` issue in flutter/flutter#153678 in Skwasm
1 parent 51d93f8 commit 0fad460

File tree

5 files changed

+227
-292
lines changed

5 files changed

+227
-292
lines changed

lib/web_ui/lib/src/engine/skwasm/skwasm_impl/canvas.dart

Lines changed: 96 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,29 @@ class SkwasmCanvas implements SceneCanvas {
2929

3030
@override
3131
void saveLayer(ui.Rect? bounds, ui.Paint paint) {
32-
paint as SkwasmPaint;
32+
final paintHandle = (paint as SkwasmPaint).toRawPaint();
3333
if (bounds != null) {
3434
withStackScope((StackScope s) {
35-
canvasSaveLayer(_handle, s.convertRectToNative(bounds), paint.handle, nullptr);
35+
canvasSaveLayer(_handle, s.convertRectToNative(bounds), paintHandle, nullptr);
3636
});
3737
} else {
38-
canvasSaveLayer(_handle, nullptr, paint.handle, nullptr);
38+
canvasSaveLayer(_handle, nullptr, paintHandle, nullptr);
3939
}
40+
paintDispose(paintHandle);
4041
}
4142

4243
@override
4344
void saveLayerWithFilter(ui.Rect? bounds, ui.Paint paint, ui.ImageFilter imageFilter) {
4445
final SkwasmImageFilter nativeFilter = SkwasmImageFilter.fromUiFilter(imageFilter);
45-
paint as SkwasmPaint;
46+
final paintHandle = (paint as SkwasmPaint).toRawPaint();
4647
if (bounds != null) {
4748
withStackScope((StackScope s) {
48-
canvasSaveLayer(_handle, s.convertRectToNative(bounds), paint.handle, nativeFilter.handle);
49+
canvasSaveLayer(_handle, s.convertRectToNative(bounds), paintHandle, nativeFilter.handle);
4950
});
5051
} else {
51-
canvasSaveLayer(_handle, nullptr, paint.handle, nativeFilter.handle);
52+
canvasSaveLayer(_handle, nullptr, paintHandle, nativeFilter.handle);
5253
}
54+
paintDispose(paintHandle);
5355
}
5456

5557
@override
@@ -111,136 +113,158 @@ class SkwasmCanvas implements SceneCanvas {
111113

112114
@override
113115
void drawLine(ui.Offset p1, ui.Offset p2, ui.Paint paint) {
114-
paint as SkwasmPaint;
115-
canvasDrawLine(_handle, p1.dx, p1.dy, p2.dx, p2.dy, paint.handle);
116+
final paintHandle = (paint as SkwasmPaint).toRawPaint();
117+
canvasDrawLine(_handle, p1.dx, p1.dy, p2.dx, p2.dy, paintHandle);
118+
paintDispose(paintHandle);
116119
}
117120

118121
@override
119122
void drawPaint(ui.Paint paint) {
120-
paint as SkwasmPaint;
121-
canvasDrawPaint(_handle, paint.handle);
123+
final paintHandle = (paint as SkwasmPaint).toRawPaint();
124+
canvasDrawPaint(_handle, paintHandle);
125+
paintDispose(paintHandle);
122126
}
123127

124128
@override
125129
void drawRect(ui.Rect rect, ui.Paint paint) {
126-
paint as SkwasmPaint;
130+
final paintHandle = (paint as SkwasmPaint).toRawPaint();
127131
withStackScope((StackScope s) {
128132
canvasDrawRect(
129133
_handle,
130134
s.convertRectToNative(rect),
131-
paint.handle
135+
paintHandle
132136
);
133137
});
138+
paintDispose(paintHandle);
134139
}
135140

136141
@override
137142
void drawRRect(ui.RRect rrect, ui.Paint paint) {
138-
paint as SkwasmPaint;
143+
final paintHandle = (paint as SkwasmPaint).toRawPaint();
139144
withStackScope((StackScope s) {
140145
canvasDrawRRect(
141146
_handle,
142147
s.convertRRectToNative(rrect),
143-
paint.handle
148+
paintHandle
144149
);
145150
});
151+
paintDispose(paintHandle);
146152
}
147153

148154
@override
149155
void drawDRRect(ui.RRect outer, ui.RRect inner, ui.Paint paint) {
150-
paint as SkwasmPaint;
156+
final paintHandle = (paint as SkwasmPaint).toRawPaint();
151157
withStackScope((StackScope s) {
152158
canvasDrawDRRect(
153159
_handle,
154160
s.convertRRectToNative(outer),
155161
s.convertRRectToNative(inner),
156-
paint.handle
162+
paintHandle
157163
);
158164
});
165+
paintDispose(paintHandle);
159166
}
160167

161168
@override
162169
void drawOval(ui.Rect rect, ui.Paint paint) {
163-
paint as SkwasmPaint;
170+
final paintHandle = (paint as SkwasmPaint).toRawPaint();
164171
withStackScope((StackScope s) {
165-
canvasDrawOval(_handle, s.convertRectToNative(rect), paint.handle);
172+
canvasDrawOval(_handle, s.convertRectToNative(rect), paintHandle);
166173
});
174+
paintDispose(paintHandle);
167175
}
168176

169177
@override
170178
void drawCircle(ui.Offset center, double radius, ui.Paint paint) {
171-
paint as SkwasmPaint;
172-
canvasDrawCircle(_handle, center.dx, center.dy, radius, paint.handle);
179+
final paintHandle = (paint as SkwasmPaint).toRawPaint();
180+
canvasDrawCircle(_handle, center.dx, center.dy, radius, paintHandle);
181+
paintDispose(paintHandle);
173182
}
174183

175184
@override
176185
void drawArc(ui.Rect rect, double startAngle, double sweepAngle,
177186
bool useCenter, ui.Paint paint) {
178-
paint as SkwasmPaint;
187+
final paintHandle = (paint as SkwasmPaint).toRawPaint();
179188
withStackScope((StackScope s) {
180189
canvasDrawArc(
181190
_handle,
182191
s.convertRectToNative(rect),
183192
ui.toDegrees(startAngle),
184193
ui.toDegrees(sweepAngle),
185194
useCenter,
186-
paint.handle
195+
paintHandle,
187196
);
188197
});
198+
paintDispose(paintHandle);
189199
}
190200

191201
@override
192202
void drawPath(ui.Path path, ui.Paint paint) {
193-
paint as SkwasmPaint;
194203
path as SkwasmPath;
195-
canvasDrawPath(_handle, path.handle, paint.handle);
204+
final paintHandle = (paint as SkwasmPaint).toRawPaint();
205+
canvasDrawPath(_handle, path.handle, paintHandle);
206+
paintDispose(paintHandle);
196207
}
197208

198209
@override
199-
void drawImage(ui.Image image, ui.Offset offset, ui.Paint paint) =>
210+
void drawImage(ui.Image image, ui.Offset offset, ui.Paint paint) {
211+
final paintHandle = (paint as SkwasmPaint).toRawPaint();
200212
canvasDrawImage(
201213
_handle,
202214
(image as SkwasmImage).handle,
203215
offset.dx,
204216
offset.dy,
205-
(paint as SkwasmPaint).handle,
217+
paintHandle,
206218
paint.filterQuality.index,
207219
);
220+
paintDispose(paintHandle);
221+
}
208222

209223
@override
210224
void drawImageRect(
211225
ui.Image image,
212226
ui.Rect src,
213227
ui.Rect dst,
214-
ui.Paint paint) => withStackScope((StackScope scope) {
215-
final Pointer<Float> sourceRect = scope.convertRectToNative(src);
216-
final Pointer<Float> destRect = scope.convertRectToNative(dst);
217-
canvasDrawImageRect(
218-
_handle,
219-
(image as SkwasmImage).handle,
220-
sourceRect,
221-
destRect,
222-
(paint as SkwasmPaint).handle,
223-
paint.filterQuality.index,
224-
);
225-
});
228+
ui.Paint paint,
229+
) {
230+
withStackScope((StackScope scope) {
231+
final Pointer<Float> sourceRect = scope.convertRectToNative(src);
232+
final Pointer<Float> destRect = scope.convertRectToNative(dst);
233+
final paintHandle = (paint as SkwasmPaint).toRawPaint();
234+
canvasDrawImageRect(
235+
_handle,
236+
(image as SkwasmImage).handle,
237+
sourceRect,
238+
destRect,
239+
paintHandle,
240+
paint.filterQuality.index,
241+
);
242+
paintDispose(paintHandle);
243+
});
244+
}
226245

227246
@override
228247
void drawImageNine(
229248
ui.Image image,
230249
ui.Rect center,
231250
ui.Rect dst,
232-
ui.Paint paint) => withStackScope((StackScope scope) {
233-
final Pointer<Int32> centerRect = scope.convertIRectToNative(center);
234-
final Pointer<Float> destRect = scope.convertRectToNative(dst);
235-
canvasDrawImageNine(
236-
_handle,
237-
(image as SkwasmImage).handle,
238-
centerRect,
239-
destRect,
240-
(paint as SkwasmPaint).handle,
241-
paint.filterQuality.index,
242-
);
243-
});
251+
ui.Paint paint,
252+
) {
253+
withStackScope((StackScope scope) {
254+
final Pointer<Int32> centerRect = scope.convertIRectToNative(center);
255+
final Pointer<Float> destRect = scope.convertRectToNative(dst);
256+
final paintHandle = (paint as SkwasmPaint).toRawPaint();
257+
canvasDrawImageNine(
258+
_handle,
259+
(image as SkwasmImage).handle,
260+
centerRect,
261+
destRect,
262+
paintHandle,
263+
paint.filterQuality.index,
264+
);
265+
paintDispose(paintHandle);
266+
});
267+
}
244268

245269
@override
246270
void drawPicture(ui.Picture picture) {
@@ -264,13 +288,15 @@ class SkwasmCanvas implements SceneCanvas {
264288
ui.Paint paint
265289
) => withStackScope((StackScope scope) {
266290
final RawPointArray rawPoints = scope.convertPointArrayToNative(points);
291+
final paintHandle = (paint as SkwasmPaint).toRawPaint();
267292
canvasDrawPoints(
268293
_handle,
269294
pointMode.index,
270295
rawPoints,
271296
points.length,
272-
(paint as SkwasmPaint).handle,
297+
paintHandle,
273298
);
299+
paintDispose(paintHandle);
274300
});
275301

276302
@override
@@ -280,26 +306,32 @@ class SkwasmCanvas implements SceneCanvas {
280306
ui.Paint paint
281307
) => withStackScope((StackScope scope) {
282308
final RawPointArray rawPoints = scope.convertDoublesToNative(points);
309+
final paintHandle = (paint as SkwasmPaint).toRawPaint();
283310
canvasDrawPoints(
284311
_handle,
285312
pointMode.index,
286313
rawPoints,
287314
points.length ~/ 2,
288-
(paint as SkwasmPaint).handle,
315+
paintHandle,
289316
);
317+
paintDispose(paintHandle);
290318
});
291319

292320
@override
293321
void drawVertices(
294322
ui.Vertices vertices,
295323
ui.BlendMode blendMode,
296324
ui.Paint paint,
297-
) => canvasDrawVertices(
298-
_handle,
299-
(vertices as SkwasmVertices).handle,
300-
blendMode.index,
301-
(paint as SkwasmPaint).handle,
302-
);
325+
) {
326+
final paintHandle = (paint as SkwasmPaint).toRawPaint();
327+
canvasDrawVertices(
328+
_handle,
329+
(vertices as SkwasmVertices).handle,
330+
blendMode.index,
331+
paintHandle,
332+
);
333+
paintDispose(paintHandle);
334+
}
303335

304336
@override
305337
void drawAtlas(
@@ -319,6 +351,7 @@ class SkwasmCanvas implements SceneCanvas {
319351
final RawRect rawCullRect = cullRect != null
320352
? scope.convertRectToNative(cullRect)
321353
: nullptr;
354+
final paintHandle = (paint as SkwasmPaint).toRawPaint();
322355
canvasDrawAtlas(
323356
_handle,
324357
(atlas as SkwasmImage).handle,
@@ -328,8 +361,9 @@ class SkwasmCanvas implements SceneCanvas {
328361
transforms.length,
329362
(blendMode ?? ui.BlendMode.src).index,
330363
rawCullRect,
331-
(paint as SkwasmPaint).handle,
364+
paintHandle,
332365
);
366+
paintDispose(paintHandle);
333367
});
334368

335369
@override
@@ -350,6 +384,7 @@ class SkwasmCanvas implements SceneCanvas {
350384
final RawRect rawCullRect = cullRect != null
351385
? scope.convertRectToNative(cullRect)
352386
: nullptr;
387+
final paintHandle = (paint as SkwasmPaint).toRawPaint();
353388
canvasDrawAtlas(
354389
_handle,
355390
(atlas as SkwasmImage).handle,
@@ -359,8 +394,9 @@ class SkwasmCanvas implements SceneCanvas {
359394
rstTransforms.length ~/ 4,
360395
(blendMode ?? ui.BlendMode.src).index,
361396
rawCullRect,
362-
(paint as SkwasmPaint).handle,
397+
paintHandle,
363398
);
399+
paintDispose(paintHandle);
364400
});
365401

366402
@override

0 commit comments

Comments
 (0)