@@ -1395,3 +1395,66 @@ class Vector3 {
13951395 double get y => _v3storage[1 ];
13961396 double get z => _v3storage[2 ];
13971397}
1398+
1399+ /// Converts a matrix represented using [Float64List] to one represented using
1400+ /// [Float32List] .
1401+ ///
1402+ /// 32-bit precision is sufficient because Flutter Engine itself (as well as
1403+ /// Skia) use 32-bit precision under the hood anyway.
1404+ ///
1405+ /// 32-bit matrices require 2x less memory and in V8 they are allocated on the
1406+ /// JavaScript heap, thus avoiding a malloc.
1407+ ///
1408+ /// See also:
1409+ /// * https://bugs.chromium.org/p/v8/issues/detail?id=9199
1410+ /// * https://bugs.chromium.org/p/v8/issues/detail?id=2022
1411+ Float32List toMatrix32 (Float64List matrix64) {
1412+ final Float32List matrix32 = Float32List (16 );
1413+ matrix32[15 ] = matrix64[15 ];
1414+ matrix32[14 ] = matrix64[14 ];
1415+ matrix32[13 ] = matrix64[13 ];
1416+ matrix32[12 ] = matrix64[12 ];
1417+ matrix32[11 ] = matrix64[11 ];
1418+ matrix32[10 ] = matrix64[10 ];
1419+ matrix32[9 ] = matrix64[9 ];
1420+ matrix32[8 ] = matrix64[8 ];
1421+ matrix32[7 ] = matrix64[7 ];
1422+ matrix32[6 ] = matrix64[6 ];
1423+ matrix32[5 ] = matrix64[5 ];
1424+ matrix32[4 ] = matrix64[4 ];
1425+ matrix32[3 ] = matrix64[3 ];
1426+ matrix32[2 ] = matrix64[2 ];
1427+ matrix32[1 ] = matrix64[1 ];
1428+ matrix32[0 ] = matrix64[0 ];
1429+ return matrix32;
1430+ }
1431+
1432+ // Stores matrix in a form that allows zero allocation transforms.
1433+ // TODO(yjbanov): re-evaluate the need for this class. It may be an
1434+ // over-optimization. It is only used by `GradientLinear` in the
1435+ // HTML renderer. However that class creates a whole new WebGL
1436+ // context to render the gradient, then copies the resulting
1437+ // bitmap back into the destination canvas. This is multiple
1438+ // orders of magnitude more computation and data copying. Saving
1439+ // an allocation of one point is unlikely to save anything, but
1440+ // is guaranteed to add complexity (e.g. it's stateful).
1441+ class FastMatrix32 {
1442+ FastMatrix32 (this .matrix);
1443+
1444+ final Float32List matrix;
1445+ double transformedX = 0 ;
1446+ double transformedY = 0 ;
1447+
1448+ /// Transforms the point defined by [x] and [y] using the [matrix] and stores
1449+ /// the results in [transformedX] and [transformedY] .
1450+ void transform (double x, double y) {
1451+ transformedX = matrix[12 ] + (matrix[0 ] * x) + (matrix[4 ] * y);
1452+ transformedY = matrix[13 ] + (matrix[1 ] * x) + (matrix[5 ] * y);
1453+ }
1454+
1455+ String debugToString () =>
1456+ '${matrix [0 ].toStringAsFixed (3 )}, ${matrix [4 ].toStringAsFixed (3 )}, ${matrix [8 ].toStringAsFixed (3 )}, ${matrix [12 ].toStringAsFixed (3 )}\n '
1457+ '${matrix [1 ].toStringAsFixed (3 )}, ${matrix [5 ].toStringAsFixed (3 )}, ${matrix [9 ].toStringAsFixed (3 )}, ${matrix [13 ].toStringAsFixed (3 )}\n '
1458+ '${matrix [2 ].toStringAsFixed (3 )}, ${matrix [6 ].toStringAsFixed (3 )}, ${matrix [10 ].toStringAsFixed (3 )}, ${matrix [14 ].toStringAsFixed (3 )}\n '
1459+ '${matrix [3 ].toStringAsFixed (3 )}, ${matrix [7 ].toStringAsFixed (3 )}, ${matrix [11 ].toStringAsFixed (3 )}, ${matrix [15 ].toStringAsFixed (3 )}\n ' ;
1460+ }
0 commit comments