Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 2132a25

Browse files
committed
[web] move some vector functions into vector_math.dart
1 parent 5cf97fd commit 2132a25

File tree

9 files changed

+68
-60
lines changed

9 files changed

+68
-60
lines changed

lib/web_ui/lib/src/engine/canvaskit/canvaskit_canvas.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import 'dart:typed_data';
66

77
import 'package:ui/ui.dart' as ui;
88

9-
import '../../engine.dart' show toMatrix32;
109
import '../validators.dart';
10+
import '../vector_math.dart';
1111
import 'canvas.dart';
1212
import 'canvaskit_api.dart';
1313
import 'image.dart';

lib/web_ui/lib/src/engine/canvaskit/layer_scene_builder.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import 'dart:typed_data';
66

77
import 'package:ui/ui.dart' as ui;
88

9-
import '../../engine.dart' show toMatrix32;
109
import '../vector_math.dart';
1110
import 'layer.dart';
1211
import 'layer_tree.dart';

lib/web_ui/lib/src/engine/html/canvas.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import 'dart:typed_data';
77

88
import 'package:ui/ui.dart' as ui;
99

10-
import '../../engine.dart' show toMatrix32;
1110
import '../picture.dart';
1211
import '../util.dart';
1312
import '../validators.dart';
13+
import '../vector_math.dart';
1414
import 'painting.dart';
1515
import 'recording_canvas.dart';
1616
import 'render_vertices.dart';

lib/web_ui/lib/src/engine/html/path/path.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import 'dart:typed_data';
77

88
import 'package:ui/ui.dart' as ui;
99

10-
import '../../../engine.dart' show toMatrix32;
1110
import '../../util.dart';
1211
import '../../validators.dart';
12+
import '../../vector_math.dart';
1313
import 'conic.dart';
1414
import 'cubic.dart';
1515
import 'path_iterator.dart';

lib/web_ui/lib/src/engine/html/scene_builder.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import 'dart:typed_data';
66

77
import 'package:ui/ui.dart' as ui;
88

9-
import '../../engine.dart' show kProfileApplyFrame, kProfilePrerollFrame, toMatrix32;
9+
import '../../engine.dart' show kProfileApplyFrame, kProfilePrerollFrame;
1010
import '../picture.dart';
1111
import '../profiler.dart';
1212
import '../util.dart';

lib/web_ui/lib/src/engine/initialization.dart

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -318,39 +318,3 @@ void _addUrlStrategyListener() {
318318
/// rendering of PlatformViews into the web app.
319319
// TODO(dit): How to make this overridable from tests?
320320
final PlatformViewManager platformViewManager = PlatformViewManager();
321-
322-
// TODO(yjbanov): this does not belong here.
323-
// https://github.com/flutter/flutter/issues/100394
324-
325-
/// Converts a matrix represented using [Float64List] to one represented using
326-
/// [Float32List].
327-
///
328-
/// 32-bit precision is sufficient because Flutter Engine itself (as well as
329-
/// Skia) use 32-bit precision under the hood anyway.
330-
///
331-
/// 32-bit matrices require 2x less memory and in V8 they are allocated on the
332-
/// JavaScript heap, thus avoiding a malloc.
333-
///
334-
/// See also:
335-
/// * https://bugs.chromium.org/p/v8/issues/detail?id=9199
336-
/// * https://bugs.chromium.org/p/v8/issues/detail?id=2022
337-
Float32List toMatrix32(Float64List matrix64) {
338-
final Float32List matrix32 = Float32List(16);
339-
matrix32[15] = matrix64[15];
340-
matrix32[14] = matrix64[14];
341-
matrix32[13] = matrix64[13];
342-
matrix32[12] = matrix64[12];
343-
matrix32[11] = matrix64[11];
344-
matrix32[10] = matrix64[10];
345-
matrix32[9] = matrix64[9];
346-
matrix32[8] = matrix64[8];
347-
matrix32[7] = matrix64[7];
348-
matrix32[6] = matrix64[6];
349-
matrix32[5] = matrix64[5];
350-
matrix32[4] = matrix64[4];
351-
matrix32[3] = matrix64[3];
352-
matrix32[2] = matrix64[2];
353-
matrix32[1] = matrix64[1];
354-
matrix32[0] = matrix64[0];
355-
return matrix32;
356-
}

lib/web_ui/lib/src/engine/util.dart

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -499,24 +499,6 @@ void applyWebkitClipFix(html.Element? containerElement) {
499499
}
500500
}
501501

502-
// Stores matrix in a form that allows zero allocation transforms.
503-
class FastMatrix32 {
504-
final Float32List matrix;
505-
double transformedX = 0, transformedY = 0;
506-
FastMatrix32(this.matrix);
507-
508-
void transform(double x, double y) {
509-
transformedX = matrix[12] + (matrix[0] * x) + (matrix[4] * y);
510-
transformedY = matrix[13] + (matrix[1] * x) + (matrix[5] * y);
511-
}
512-
513-
String debugToString() =>
514-
'${matrix[0].toStringAsFixed(3)}, ${matrix[4].toStringAsFixed(3)}, ${matrix[8].toStringAsFixed(3)}, ${matrix[12].toStringAsFixed(3)}\n'
515-
'${matrix[1].toStringAsFixed(3)}, ${matrix[5].toStringAsFixed(3)}, ${matrix[9].toStringAsFixed(3)}, ${matrix[13].toStringAsFixed(3)}\n'
516-
'${matrix[2].toStringAsFixed(3)}, ${matrix[6].toStringAsFixed(3)}, ${matrix[10].toStringAsFixed(3)}, ${matrix[14].toStringAsFixed(3)}\n'
517-
'${matrix[3].toStringAsFixed(3)}, ${matrix[7].toStringAsFixed(3)}, ${matrix[11].toStringAsFixed(3)}, ${matrix[15].toStringAsFixed(3)}\n';
518-
}
519-
520502
/// Roughly the inverse of [ui.Shadow.convertRadiusToSigma].
521503
///
522504
/// This does not inverse [ui.Shadow.convertRadiusToSigma] exactly, because on

lib/web_ui/lib/src/engine/vector_math.dart

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

lib/web_ui/test/engine/semantics/semantics_tester.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import 'dart:html' as html;
77
import 'dart:typed_data';
88

99
import 'package:test/test.dart';
10-
import 'package:ui/src/engine.dart' show flutterViewEmbedder, toMatrix32;
1110
import 'package:ui/src/engine/browser_detection.dart';
11+
import 'package:ui/src/engine/embedder.dart';
1212
import 'package:ui/src/engine/host_node.dart';
1313
import 'package:ui/src/engine/semantics.dart';
1414
import 'package:ui/src/engine/util.dart';

0 commit comments

Comments
 (0)