Commit afe3158d authored by Adam Barth's avatar Adam Barth

Remove clients of getTotalMatrix (#4487)

Instead of using getTotalMatrix and setMatrix, we can just use
save/restore, which is more idiomatic.

The getTotalMatrix/setMatrix pattern was introduced to improve
performance, but the original code was calling getTotalMatrix/setMatrix
at every node in the sprite tree, which is much slower than the normal
save/transform/restore pattern.

Related to #4254
parent c02c5538
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:ui' as ui show PictureRecorder;
import 'dart:ui' show Rect, Color, Paint, Canvas;
import 'package:test/test.dart';
import 'package:vector_math/vector_math_64.dart';
void main() {
ui.PictureRecorder recorder = new ui.PictureRecorder();
Canvas canvas = new Canvas(recorder, new Rect.fromLTRB(0.0, 0.0, 100.0, 100.0));
test("matrix access should work", () {
// Matrix equality doesn't work!
// https://github.com/google/vector_math.dart/issues/147
expect(canvas.getTotalMatrix(), equals(new Matrix4.identity().storage));
Matrix4 matrix = new Matrix4.identity();
// Round-tripping through getTotalMatrix will lose the z value
// So only scale to 1x in the z direction.
matrix.scale(2.0, 2.0, 1.0);
canvas.setMatrix(matrix.storage);
canvas.drawPaint(new Paint()..color = const Color(0xFF00FF00));
expect(canvas.getTotalMatrix(), equals(matrix.storage));
});
}
...@@ -17,6 +17,7 @@ import 'package:flutter/rendering.dart'; ...@@ -17,6 +17,7 @@ import 'package:flutter/rendering.dart';
import 'package:flutter/scheduler.dart'; import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:meta/meta.dart';
import 'package:mojo/core.dart'; import 'package:mojo/core.dart';
import 'package:sky_services/media/media.mojom.dart'; import 'package:sky_services/media/media.mojom.dart';
import 'package:vector_math/vector_math_64.dart'; import 'package:vector_math/vector_math_64.dart';
......
...@@ -30,16 +30,16 @@ class Layer extends Node with SpritePaint { ...@@ -30,16 +30,16 @@ class Layer extends Node with SpritePaint {
..isAntiAlias = false; ..isAntiAlias = false;
@override @override
void _prePaint(Canvas canvas, Matrix4 matrix) { void _prePaint(Canvas canvas) {
super._prePaint(canvas, matrix); super._prePaint(canvas);
_updatePaint(_cachedPaint); _updatePaint(_cachedPaint);
canvas.saveLayer(layerRect, _cachedPaint); canvas.saveLayer(layerRect, _cachedPaint);
} }
@override @override
void _postPaint(Canvas canvas, Matrix4 totalMatrix) { void _postPaint(Canvas canvas) {
canvas.restore(); canvas.restore();
super._postPaint(canvas, totalMatrix); super._postPaint(canvas);
} }
} }
...@@ -55,7 +55,6 @@ class Node { ...@@ -55,7 +55,6 @@ class Node {
int _addedOrder; int _addedOrder;
int _childrenLastAddedOrder = 0; int _childrenLastAddedOrder = 0;
bool _childrenNeedSorting = false; bool _childrenNeedSorting = false;
Matrix4 _savedTotalMatrix;
/// Decides if the node and its children is currently paused. /// Decides if the node and its children is currently paused.
/// ///
...@@ -508,20 +507,20 @@ class Node { ...@@ -508,20 +507,20 @@ class Node {
// Rendering // Rendering
void _visit(Canvas canvas, Matrix4 totalMatrix) { void _visit(Canvas canvas) {
assert(canvas != null); assert(canvas != null);
if (!visible) return; if (!visible) return;
_prePaint(canvas, totalMatrix); _prePaint(canvas);
_visitChildren(canvas, totalMatrix); _visitChildren(canvas);
_postPaint(canvas, totalMatrix); _postPaint(canvas);
} }
void _prePaint(Canvas canvas, Matrix4 matrix) { @mustCallSuper
_savedTotalMatrix = new Matrix4.copy(matrix); void _prePaint(Canvas canvas) {
canvas
// Get the transformation matrix and apply transform ..save()
matrix.multiply(transformMatrix); ..transform(transformMatrix.storage);
} }
/// Paints this node to the canvas. /// Paints this node to the canvas.
...@@ -543,7 +542,7 @@ class Node { ...@@ -543,7 +542,7 @@ class Node {
void paint(Canvas canvas) { void paint(Canvas canvas) {
} }
void _visitChildren(Canvas canvas, Matrix4 totalMatrix) { void _visitChildren(Canvas canvas) {
// Sort children if needed // Sort children if needed
_sortChildren(); _sortChildren();
...@@ -553,24 +552,24 @@ class Node { ...@@ -553,24 +552,24 @@ class Node {
while (i < _children.length) { while (i < _children.length) {
Node child = _children[i]; Node child = _children[i];
if (child.zPosition >= 0.0) break; if (child.zPosition >= 0.0) break;
child._visit(canvas, totalMatrix); child._visit(canvas);
i++; i++;
} }
// Paint this node // Paint this node
canvas.setMatrix(totalMatrix.storage);
paint(canvas); paint(canvas);
// Visit children in front of this node // Visit children in front of this node
while (i < _children.length) { while (i < _children.length) {
Node child = _children[i]; Node child = _children[i];
child._visit(canvas, totalMatrix); child._visit(canvas);
i++; i++;
} }
} }
void _postPaint(Canvas canvas, Matrix4 totalMatrix) { @mustCallSuper
totalMatrix.setFrom(_savedTotalMatrix); void _postPaint(Canvas canvas) {
canvas.restore();
} }
// Receiving update calls // Receiving update calls
......
...@@ -353,15 +353,14 @@ class SpriteBox extends RenderBox { ...@@ -353,15 +353,14 @@ class SpriteBox extends RenderBox {
@override @override
void paint(PaintingContext context, Offset offset) { void paint(PaintingContext context, Offset offset) {
final Canvas canvas = context.canvas; final Canvas canvas = context.canvas;
canvas.save();
// Move to correct coordinate space before drawing // Move to correct coordinate space before drawing
canvas.translate(offset.dx, offset.dy); canvas
canvas.transform(transformMatrix.storage); ..save()
..translate(offset.dx, offset.dy)
..transform(transformMatrix.storage);
// Draw the sprite tree // Draw the sprite tree
Matrix4 totalMatrix = new Matrix4.fromFloat64List(canvas.getTotalMatrix()); _rootNode._visit(canvas);
_rootNode._visit(canvas, totalMatrix);
canvas.restore(); canvas.restore();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment