forked from flutter/engine
/
vertices.dart
143 lines (126 loc) · 4.57 KB
/
vertices.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @dart = 2.6
part of engine;
Int32List _encodeColorList(List<ui.Color> colors) {
final int colorCount = colors.length;
final Int32List result = Int32List(colorCount);
for (int i = 0; i < colorCount; ++i) result[i] = colors[i].value;
return result;
}
class SkVertices implements ui.Vertices {
js.JsObject skVertices;
final Int32List _colors;
final Float32List _positions;
final ui.VertexMode _mode;
final Float32List _textureCoordinates;
final Uint16List _indices;
SkVertices(
ui.VertexMode mode,
List<ui.Offset> positions, {
List<ui.Offset> textureCoordinates,
List<ui.Color> colors,
List<int> indices,
}) : assert(mode != null),
assert(positions != null),
_colors =
Int32List.fromList(colors.map((ui.Color c) => c.value).toList()),
_positions = encodePointList(positions),
_mode = mode,
_textureCoordinates = (textureCoordinates != null)
? encodePointList(textureCoordinates) : null,
_indices = indices != null ? Uint16List.fromList(indices) : null {
if (textureCoordinates != null &&
textureCoordinates.length != positions.length)
throw ArgumentError(
'"positions" and "textureCoordinates" lengths must match.');
if (colors != null && colors.length != positions.length)
throw ArgumentError('"positions" and "colors" lengths must match.');
if (indices != null &&
indices.any((int i) => i < 0 || i >= positions.length))
throw ArgumentError(
'"indices" values must be valid indices in the positions list.');
final Float32List encodedPositions = encodePointList(positions);
final Int32List encodedColors =
colors != null ? _encodeColorList(colors) : null;
if (!_init(mode, encodedPositions, _textureCoordinates, encodedColors,
_indices))
throw ArgumentError('Invalid configuration for vertices.');
}
SkVertices.raw(
ui.VertexMode mode,
Float32List positions, {
Float32List textureCoordinates,
Int32List colors,
Uint16List indices,
}) : assert(mode != null),
assert(positions != null),
_colors = colors,
_positions = positions,
_mode = mode,
_textureCoordinates = textureCoordinates,
_indices = indices {
if (textureCoordinates != null &&
textureCoordinates.length != positions.length)
throw ArgumentError(
'"positions" and "textureCoordinates" lengths must match.');
if (colors != null && colors.length * 2 != positions.length)
throw ArgumentError('"positions" and "colors" lengths must match.');
if (indices != null &&
indices.any((int i) => i < 0 || i >= positions.length))
throw ArgumentError(
'"indices" values must be valid indices in the positions list.');
if (!_init(mode, positions, textureCoordinates, colors, indices))
throw ArgumentError('Invalid configuration for vertices.');
}
bool _init(ui.VertexMode mode, Float32List positions,
Float32List textureCoordinates, Int32List colors, Uint16List indices) {
js.JsObject skVertexMode;
switch (mode) {
case ui.VertexMode.triangles:
skVertexMode = canvasKit['VertexMode']['Triangles'];
break;
case ui.VertexMode.triangleStrip:
skVertexMode = canvasKit['VertexMode']['TrianglesStrip'];
break;
case ui.VertexMode.triangleFan:
skVertexMode = canvasKit['VertexMode']['TriangleFan'];
break;
}
final js.JsObject vertices =
canvasKit.callMethod('MakeSkVertices', <dynamic>[
skVertexMode,
_encodePoints(positions),
_encodePoints(textureCoordinates),
colors,
null,
null,
indices,
]);
if (vertices != null) {
skVertices = vertices;
return true;
} else {
return false;
}
}
static _encodePoints(List<double> points) {
if (points == null) return null;
js.JsArray<js.JsArray<double>> encodedPoints =
js.JsArray<js.JsArray<double>>();
encodedPoints.length = points.length ~/ 2;
for (int i = 0; i < points.length; i += 2) {
encodedPoints[i ~/ 2] = makeSkPoint(ui.Offset(points[i], points[i + 1]));
}
return encodedPoints;
}
@override
Int32List get colors => _colors;
@override
Float32List get positions => _positions;
@override
ui.VertexMode get mode => _mode;
Float32List get textureCoordinates => _textureCoordinates;
Uint16List get indices => _indices;
}