[dart:ui] remove expensive index assertion in Vertices. (flutter/engine#53558)

Iterating through every index value is really, really expensive. Especially if users are suppllying hundreds of thousands of vertices.
This commit is contained in:
Jonah Williams
2024-07-11 15:13:04 -07:00
committed by GitHub
parent 412e42ae83
commit ac871bacc2
4 changed files with 66 additions and 19 deletions

View File

@@ -354,6 +354,30 @@ TEST_P(AiksTest, DrawVerticesPremultipliesColors) {
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
}
TEST_P(AiksTest, DrawVerticesWithInvalidIndices) {
std::vector<SkPoint> positions = {
SkPoint::Make(100, 300), SkPoint::Make(200, 100), SkPoint::Make(300, 300),
SkPoint::Make(200, 500)};
std::vector<uint16_t> indices = {0, 1, 2, 0, 2, 3, 99, 100, 101};
auto vertices = flutter::DlVertices::Make(
flutter::DlVertexMode::kTriangles, positions.size(), positions.data(),
/*texture_coordinates=*/nullptr, /*colors=*/nullptr, indices.size(),
indices.data());
EXPECT_EQ(vertices->bounds(), SkRect::MakeLTRB(100, 100, 300, 500));
flutter::DisplayListBuilder builder;
flutter::DlPaint paint;
paint.setBlendMode(flutter::DlBlendMode::kSrcOver);
paint.setColor(flutter::DlColor::kRed());
builder.DrawRect(SkRect::MakeLTRB(0, 0, 400, 400), paint);
builder.DrawVertices(vertices, flutter::DlBlendMode::kSrc, paint);
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
}
// All four vertices should form a solid red rectangle with no gaps.
// The blur rectangle drawn under them should not be visible.
TEST_P(AiksTest, DrawVerticesTextureCoordinatesWithFragmentShader) {

View File

@@ -4762,17 +4762,20 @@ base class Vertices extends NativeFieldWrapperClass1 {
if (textureCoordinates != null && textureCoordinates.length != positions.length) {
throw ArgumentError('"positions" and "textureCoordinates" lengths must match.');
}
if (indices != null) {
for (int index = 0; index < indices.length; index += 1) {
if (indices[index] >= positions.length) {
throw ArgumentError(
'"indices" values must be valid indices in the positions list '
'(i.e. numbers in the range 0..${positions.length - 1}), '
'but indices[$index] is ${indices[index]}, which is too big.',
);
assert(() {
if (indices != null) {
for (int index = 0; index < indices.length; index += 1) {
if (indices[index] >= positions.length) {
throw ArgumentError(
'"indices" values must be valid indices in the positions list '
'(i.e. numbers in the range 0..${positions.length - 1}), '
'but indices[$index] is ${indices[index]}, which is too big.',
);
}
}
}
}
return true;
}());
final Float32List encodedPositions = _encodePointList(positions);
final Float32List? encodedTextureCoordinates = (textureCoordinates != null)
? _encodePointList(textureCoordinates)
@@ -4849,17 +4852,20 @@ base class Vertices extends NativeFieldWrapperClass1 {
if (textureCoordinates != null && textureCoordinates.length != positions.length) {
throw ArgumentError('"positions" and "textureCoordinates" lengths must match.');
}
if (indices != null) {
for (int index = 0; index < indices.length; index += 1) {
if (indices[index] * 2 >= positions.length) {
throw ArgumentError(
'"indices" values must be valid indices in the positions list '
'(i.e. numbers in the range 0..${positions.length ~/ 2 - 1}), '
'but indices[$index] is ${indices[index]}, which is too big.',
);
assert(() {
if (indices != null) {
for (int index = 0; index < indices.length; index += 1) {
if (indices[index] * 2 >= positions.length) {
throw ArgumentError(
'"indices" values must be valid indices in the positions list '
'(i.e. numbers in the range 0..${positions.length ~/ 2 - 1}), '
'but indices[$index] is ${indices[index]}, which is too big.',
);
}
}
}
}
return true;
}());
if (!_init(this, mode.index, positions, textureCoordinates, colors, indices)) {
throw ArgumentError('Invalid configuration for vertices.');
}

View File

@@ -34,6 +34,12 @@ void main() {
});
test('Vertices.raw checks', () {
bool assertsEnabled = false;
assert(() {
assertsEnabled = true;
return true;
}());
try {
Vertices.raw(
VertexMode.triangles,
@@ -43,6 +49,8 @@ void main() {
} on ArgumentError catch (e) {
expect('$e', 'Invalid argument(s): "positions" must have an even number of entries (each coordinate is an x,y pair).');
}
Object? indicesError;
try {
Vertices.raw(
VertexMode.triangles,
@@ -51,8 +59,14 @@ void main() {
);
throw 'Vertices.raw did not throw the expected error.';
} on ArgumentError catch (e) {
expect('$e', 'Invalid argument(s): "indices" values must be valid indices in the positions list (i.e. numbers in the range 0..2), but indices[2] is 5, which is too big.');
indicesError = e;
}
if (assertsEnabled) {
expect('$indicesError', 'Invalid argument(s): "indices" values must be valid indices in the positions list (i.e. numbers in the range 0..2), but indices[2] is 5, which is too big.');
} else {
expect(indicesError, null);
}
Vertices.raw( // This one does not throw.
VertexMode.triangles,
Float32List.fromList(const <double>[0.0, 0.0]),

View File

@@ -593,6 +593,9 @@ impeller_Play_AiksTest_DrawVerticesSolidColorTrianglesWithoutIndices_Vulkan.png
impeller_Play_AiksTest_DrawVerticesTextureCoordinatesWithFragmentShader_Metal.png
impeller_Play_AiksTest_DrawVerticesTextureCoordinatesWithFragmentShader_OpenGLES.png
impeller_Play_AiksTest_DrawVerticesTextureCoordinatesWithFragmentShader_Vulkan.png
impeller_Play_AiksTest_DrawVerticesWithInvalidIndices_Metal.png
impeller_Play_AiksTest_DrawVerticesWithInvalidIndices_OpenGLES.png
impeller_Play_AiksTest_DrawVerticesWithInvalidIndices_Vulkan.png
impeller_Play_AiksTest_EmptySaveLayerIgnoresPaint_Metal.png
impeller_Play_AiksTest_EmptySaveLayerIgnoresPaint_OpenGLES.png
impeller_Play_AiksTest_EmptySaveLayerIgnoresPaint_Vulkan.png