Skip to content

Commit

Permalink
fix: Avoid leaks when using PictureRecorders (#1643)
Browse files Browse the repository at this point in the history
  • Loading branch information
st-pasha authored May 20, 2022
1 parent 39201c4 commit d67065e
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/flame/lib/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export 'src/extensions/image.dart';
export 'src/extensions/matrix4.dart';
export 'src/extensions/offset.dart';
export 'src/extensions/paint.dart';
export 'src/extensions/picture_extension.dart';
export 'src/extensions/rect.dart';
export 'src/extensions/rectangle.dart';
export 'src/extensions/size.dart';
Expand Down
3 changes: 2 additions & 1 deletion packages/flame/lib/src/components/text_box_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:math' as math;
import 'dart:ui';

import 'package:flame/components.dart';
import 'package:flame/src/extensions/picture_extension.dart';
import 'package:flame/src/palette.dart';
import 'package:flame/src/text/text_renderer.dart';
import 'package:flutter/widgets.dart' hide Image;
Expand Down Expand Up @@ -214,7 +215,7 @@ class TextBoxComponent<T extends TextRenderer> extends TextComponent {
final c = Canvas(recorder, size.toRect());
c.scale(pixelRatio);
_fullRender(c);
return recorder.endRecording().toImage(
return recorder.endRecording().toImageSafe(
(width * pixelRatio).ceil(),
(height * pixelRatio).ceil(),
);
Expand Down
11 changes: 11 additions & 0 deletions packages/flame/lib/src/extensions/picture_extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'dart:ui';

extension PictureExtension on Picture {
/// Converts [Picture] into an [Image] and disposes of the picture.
Future<Image> toImageSafe(int width, int height) {
return toImage(width, height).then((image) {
dispose();
return image;
});
}
}
2 changes: 1 addition & 1 deletion packages/flame/lib/src/image_composition.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class ImageComposition {

return recorder
.endRecording()
.toImage(output.width.toInt(), output.height.toInt());
.toImageSafe(output.width.toInt(), output.height.toInt());
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/flame/lib/src/sprite_batch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:ui';
import 'package:flame/game.dart';
import 'package:flame/src/cache/images.dart';
import 'package:flame/src/extensions/image.dart';
import 'package:flame/src/extensions/picture_extension.dart';
import 'package:flame/src/flame.dart';

extension SpriteBatchExtension on Game {
Expand Down Expand Up @@ -191,7 +192,7 @@ class SpriteBatch {
canvas.drawImage(image, Offset(-image.width * 2, 0), _emptyPaint);

final picture = recorder.endRecording();
final atlas = picture.toImage(image.width * 2, image.height);
final atlas = picture.toImageSafe(image.width * 2, image.height);
return atlas;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/flame_tiled/test/tiled_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void main() {
overlapMap.render(canvas);
final picture = canvasRecorder.endRecording();

final image = await picture.toImage(32, 16);
final image = await picture.toImageSafe(32, 16);
final bytes = await image.toByteData();
canvasPixelData = bytes!.buffer.asUint8List();
});
Expand Down Expand Up @@ -144,7 +144,7 @@ void main() {
overlapMap.render(canvas);
final picture = canvasRecorder.endRecording();

final image = await picture.toImage(64, 48);
final image = await picture.toImageSafe(64, 48);
final bytes = await image.toByteData();
canvasPixelData = bytes!.buffer.asUint8List();
});
Expand Down

0 comments on commit d67065e

Please sign in to comment.