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
This commit is contained in:
parent
c02c553867
commit
afe3158d5b
@ -1,29 +0,0 @@
|
||||
// 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';
|
||||
import 'package:flutter/scheduler.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:mojo/core.dart';
|
||||
import 'package:sky_services/media/media.mojom.dart';
|
||||
import 'package:vector_math/vector_math_64.dart';
|
||||
|
@ -30,16 +30,16 @@ class Layer extends Node with SpritePaint {
|
||||
..isAntiAlias = false;
|
||||
|
||||
@override
|
||||
void _prePaint(Canvas canvas, Matrix4 matrix) {
|
||||
super._prePaint(canvas, matrix);
|
||||
void _prePaint(Canvas canvas) {
|
||||
super._prePaint(canvas);
|
||||
|
||||
_updatePaint(_cachedPaint);
|
||||
canvas.saveLayer(layerRect, _cachedPaint);
|
||||
}
|
||||
|
||||
@override
|
||||
void _postPaint(Canvas canvas, Matrix4 totalMatrix) {
|
||||
void _postPaint(Canvas canvas) {
|
||||
canvas.restore();
|
||||
super._postPaint(canvas, totalMatrix);
|
||||
super._postPaint(canvas);
|
||||
}
|
||||
}
|
||||
|
@ -55,7 +55,6 @@ class Node {
|
||||
int _addedOrder;
|
||||
int _childrenLastAddedOrder = 0;
|
||||
bool _childrenNeedSorting = false;
|
||||
Matrix4 _savedTotalMatrix;
|
||||
|
||||
/// Decides if the node and its children is currently paused.
|
||||
///
|
||||
@ -508,20 +507,20 @@ class Node {
|
||||
|
||||
// Rendering
|
||||
|
||||
void _visit(Canvas canvas, Matrix4 totalMatrix) {
|
||||
void _visit(Canvas canvas) {
|
||||
assert(canvas != null);
|
||||
if (!visible) return;
|
||||
|
||||
_prePaint(canvas, totalMatrix);
|
||||
_visitChildren(canvas, totalMatrix);
|
||||
_postPaint(canvas, totalMatrix);
|
||||
_prePaint(canvas);
|
||||
_visitChildren(canvas);
|
||||
_postPaint(canvas);
|
||||
}
|
||||
|
||||
void _prePaint(Canvas canvas, Matrix4 matrix) {
|
||||
_savedTotalMatrix = new Matrix4.copy(matrix);
|
||||
|
||||
// Get the transformation matrix and apply transform
|
||||
matrix.multiply(transformMatrix);
|
||||
@mustCallSuper
|
||||
void _prePaint(Canvas canvas) {
|
||||
canvas
|
||||
..save()
|
||||
..transform(transformMatrix.storage);
|
||||
}
|
||||
|
||||
/// Paints this node to the canvas.
|
||||
@ -543,7 +542,7 @@ class Node {
|
||||
void paint(Canvas canvas) {
|
||||
}
|
||||
|
||||
void _visitChildren(Canvas canvas, Matrix4 totalMatrix) {
|
||||
void _visitChildren(Canvas canvas) {
|
||||
// Sort children if needed
|
||||
_sortChildren();
|
||||
|
||||
@ -553,24 +552,24 @@ class Node {
|
||||
while (i < _children.length) {
|
||||
Node child = _children[i];
|
||||
if (child.zPosition >= 0.0) break;
|
||||
child._visit(canvas, totalMatrix);
|
||||
child._visit(canvas);
|
||||
i++;
|
||||
}
|
||||
|
||||
// Paint this node
|
||||
canvas.setMatrix(totalMatrix.storage);
|
||||
paint(canvas);
|
||||
|
||||
// Visit children in front of this node
|
||||
while (i < _children.length) {
|
||||
Node child = _children[i];
|
||||
child._visit(canvas, totalMatrix);
|
||||
child._visit(canvas);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void _postPaint(Canvas canvas, Matrix4 totalMatrix) {
|
||||
totalMatrix.setFrom(_savedTotalMatrix);
|
||||
@mustCallSuper
|
||||
void _postPaint(Canvas canvas) {
|
||||
canvas.restore();
|
||||
}
|
||||
|
||||
// Receiving update calls
|
||||
|
@ -353,15 +353,14 @@ class SpriteBox extends RenderBox {
|
||||
@override
|
||||
void paint(PaintingContext context, Offset offset) {
|
||||
final Canvas canvas = context.canvas;
|
||||
canvas.save();
|
||||
|
||||
// Move to correct coordinate space before drawing
|
||||
canvas.translate(offset.dx, offset.dy);
|
||||
canvas.transform(transformMatrix.storage);
|
||||
canvas
|
||||
..save()
|
||||
..translate(offset.dx, offset.dy)
|
||||
..transform(transformMatrix.storage);
|
||||
|
||||
// Draw the sprite tree
|
||||
Matrix4 totalMatrix = new Matrix4.fromFloat64List(canvas.getTotalMatrix());
|
||||
_rootNode._visit(canvas, totalMatrix);
|
||||
_rootNode._visit(canvas);
|
||||
|
||||
canvas.restore();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user