Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: flame svg perfomance #1373

Merged
merged 4 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions examples/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ environment:

dependencies:
flame: ^1.0.0
flame_svg:
path: ../packages/flame_svg
flame_svg: ^1.0.0
dashbook: 0.1.5
flutter:
sdk: flutter
Expand Down
2 changes: 2 additions & 0 deletions packages/flame/lib/src/cache/memory_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ class MemoryCache<K, V> {
bool containsKey(K key) => _cache.containsKey(key);

int get size => _cache.length;

Iterable<K> get keys => _cache.keys;
}
57 changes: 52 additions & 5 deletions packages/flame_svg/lib/svg.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import 'dart:ui';

import 'package:flame/assets.dart';
import 'package:flame/cache.dart';
import 'package:flame/extensions.dart';
import 'package:flame/flame.dart';
import 'package:flame/game.dart';
Expand All @@ -7,11 +10,17 @@ import 'package:flutter_svg/flutter_svg.dart';
/// A [Svg] to be rendered on a Flame [Game].
class Svg {
/// The [DrawableRoot] that this [Svg] represents.
DrawableRoot svgRoot;
final DrawableRoot svgRoot;

/// Creates an [Svg] with the received [svgRoot].
erickzanardo marked this conversation as resolved.
Show resolved Hide resolved
Svg(this.svgRoot);

final MemoryCache<Vector2, Image> _imageCache = MemoryCache();
erickzanardo marked this conversation as resolved.
Show resolved Hide resolved

final _paint = Paint()..filterQuality = FilterQuality.high;

final List<Vector2> _lock = [];

/// Loads an [Svg] with the received [cache]. When no [cache] is provided,
/// the global [Flame.assets] is used.
static Future<Svg> load(String fileName, {AssetsCache? cache}) async {
Expand All @@ -22,10 +31,13 @@ class Svg {

/// Renders the svg on the [canvas] using the dimensions provided by [size].
void render(Canvas canvas, Vector2 size) {
canvas.save();
svgRoot.scaleCanvasToViewBox(canvas, size.toSize());
svgRoot.draw(canvas, svgRoot.viewport.viewBoxRect);
canvas.restore();
final image = _getImage(size);

if (image != null) {
canvas.drawImage(image, Offset.zero, _paint);
} else {
_render(canvas, size);
}
erickzanardo marked this conversation as resolved.
Show resolved Hide resolved
}

/// Renders the svg on the [canvas] on the given [position] using the
Expand All @@ -37,6 +49,41 @@ class Svg {
) {
canvas.renderAt(position, (c) => render(c, size));
}

Image? _getImage(Vector2 size) {
final image = _imageCache.getValue(size);
erickzanardo marked this conversation as resolved.
Show resolved Hide resolved

if (image == null && !_lock.contains(size)) {
_lock.add(size);
final recorder = PictureRecorder();

final canvas = Canvas(recorder);
_render(canvas, size);
final _picture = recorder.endRecording();

_picture.toImage(size.x.toInt(), size.y.toInt()).then((image) {
_imageCache.setValue(size, image);
_lock.remove(size);
_picture.dispose();
});
}

return image;
}
erickzanardo marked this conversation as resolved.
Show resolved Hide resolved

void _render(Canvas canvas, Vector2 size) {
svgRoot.scaleCanvasToViewBox(canvas, size.toSize());
svgRoot.draw(canvas, svgRoot.viewport.viewBoxRect);
}

/// Clear all the stored cache from this SVG, be sure to call
/// this method once the instance is no longer needed to avoid
/// memory leaks
void dispose() {
_imageCache.keys.forEach((key) {
_imageCache.getValue(key)?.dispose();
});
}
}

/// Provides a loading method for [Svg] on the [Game] class.
Expand Down
28 changes: 24 additions & 4 deletions packages/flame_svg/lib/svg_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ import './svg.dart';
/// Wraps [Svg] in a Flame component.
class SvgComponent extends PositionComponent {
/// The wrapped instance of [Svg].
Svg? svg;
Svg? _svg;

/// Creates an [SvgComponent]
SvgComponent({
this.svg,
required Svg svg,
Vector2? position,
Vector2? size,
Vector2? scale,
double? angle,
Anchor? anchor,
int? priority,
}) : super(
}) : _svg = svg,
super(
position: position,
size: size,
scale: scale,
Expand Down Expand Up @@ -50,8 +51,27 @@ class SvgComponent extends PositionComponent {
priority: priority,
);

/// Sets a new [svg] instance
set svg(Svg? svg) {
if (_svg != null) {
_svg!.dispose();
}
erickzanardo marked this conversation as resolved.
Show resolved Hide resolved

_svg = svg;
}

/// Returns the current [svg] instance
Svg? get svg => _svg;

@override
void render(Canvas canvas) {
svg?.render(canvas, size);
_svg?.render(canvas, size);
}

@override
void onRemove() {
super.onRemove();

_svg?.dispose();
}
}
2 changes: 2 additions & 0 deletions packages/flame_svg/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ dependencies:
dev_dependencies:
dartdoc: ^4.1.0
flame_lint: ^0.0.1
test: ^1.17.12
spydon marked this conversation as resolved.
Show resolved Hide resolved
mocktail: ^0.2.0
32 changes: 32 additions & 0 deletions packages/flame_svg/test/svg_component_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'package:flame_svg/flame_svg.dart';
import 'package:mocktail/mocktail.dart';
import 'package:test/test.dart';

class MockSvg extends Mock implements Svg {}

void main() {
group('SvgComponent', () {
late Svg svg;

setUp(() {
svg = MockSvg();
when(svg.dispose).thenAnswer((_) {});
});

test('disposes the svg instance when it is removed', () {
final component = SvgComponent(svg: svg);
component.onRemove();

verify(svg.dispose).called(1);
});

test('disposes the old svg instance when a new one is received', () {
final component = SvgComponent(svg: svg);

final newSvg = MockSvg();
component.svg = newSvg;

verify(svg.dispose).called(1);
});
});
}