Use uncompressed image size as the basis for the fuzzy pixel matching threshold in the Skia Gold scenario tests (flutter/engine#34431)

This commit is contained in:
Jason Simmons
2022-07-20 13:03:04 -07:00
committed by GitHub
parent 2318843e5d
commit 2232f767bd
4 changed files with 22 additions and 11 deletions

View File

@@ -36,11 +36,13 @@ public class ScreenshotUtil {
out = socket.getOutputStream();
}
synchronized void writeFile(String name, byte[] fileContent) throws IOException {
final ByteBuffer buffer = ByteBuffer.allocate(name.length() + fileContent.length + 8);
synchronized void writeFile(String name, byte[] fileContent, int pixelCount)
throws IOException {
final ByteBuffer buffer = ByteBuffer.allocate(name.length() + fileContent.length + 12);
// See ScreenshotBlobTransformer#bind in screenshot_transformer.dart for consumer side.
buffer.putInt(name.length());
buffer.putInt(fileContent.length);
buffer.putInt(pixelCount);
buffer.put(name.getBytes());
buffer.put(fileContent);
final byte[] bytes = buffer.array();
@@ -92,12 +94,13 @@ public class ScreenshotUtil {
* @param filename The file name.
* @param fileContent The file content.
*/
public static synchronized void writeFile(@NonNull String filename, @NonNull byte[] fileContent) {
public static synchronized void writeFile(
@NonNull String filename, @NonNull byte[] fileContent, int pixelCount) {
if (executor != null && conn != null) {
executor.execute(
() -> {
try {
conn.writeFile(filename, fileContent);
conn.writeFile(filename, fileContent, pixelCount);
} catch (IOException e) {
throw new RuntimeException(e);
}
@@ -123,8 +126,9 @@ public class ScreenshotUtil {
if (bitmap == null) {
throw new RuntimeException("failed to capture screenshot");
}
int pixelCount = bitmap.getWidth() * bitmap.getHeight();
final ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
ScreenshotUtil.writeFile(captureName, out.toByteArray());
ScreenshotUtil.writeFile(captureName, out.toByteArray(), pixelCount);
}
}

View File

@@ -76,7 +76,8 @@ void main(List<String> args) async {
log('wrote ${goldenFile.absolute.path}');
if (isSkiaGoldClientAvailable) {
final Future<void> comparison = skiaGoldClient!
.addImg(fileName, goldenFile, screenshotSize: fileContent.lengthInBytes)
.addImg(fileName, goldenFile,
screenshotSize: screenshot.pixelCount)
.catchError((dynamic err) {
panic(<String>['skia gold comparison failed: ${err.toString()}']);
});

View File

@@ -8,13 +8,16 @@ import 'dart:typed_data';
/// A screenshot from the Android emulator.
class Screenshot {
Screenshot(this.filename, this.fileContent);
Screenshot(this.filename, this.fileContent, this.pixelCount);
/// The name of the screenshot.
final String filename;
/// The binary content of the screenshot.
final Uint8List fileContent;
/// The number of pixels in the screenshot.
final int pixelCount;
}
/// Takes the input stream and transforms it into [Screenshot]s.
@@ -28,7 +31,7 @@ class ScreenshotBlobTransformer extends StreamTransformerBase<Uint8List, Screens
await for (final Uint8List blob in stream) {
pending.add(blob);
if (pending.length < 8) {
if (pending.length < 12) {
continue;
}
@@ -41,9 +44,12 @@ class ScreenshotBlobTransformer extends StreamTransformerBase<Uint8List, Screens
off += 4;
final int fcontentLen = byteData.getInt32(off);
off += 4;
final int pixelCount = byteData.getInt32(off);
off += 4;
assert(fnameLen > 0);
assert(fcontentLen > 0);
assert(pixelCount > 0);
if (pending.length < off + fnameLen) {
continue;
@@ -60,7 +66,7 @@ class ScreenshotBlobTransformer extends StreamTransformerBase<Uint8List, Screens
pending.clear();
pending.add(bytes.buffer.asUint8List(off));
yield Screenshot('$filename.png', fileContent);
yield Screenshot('$filename.png', fileContent, pixelCount);
}
}
}

View File

@@ -185,11 +185,11 @@ class SkiaGoldClient {
/// ```
///
/// [differentPixelsRate] is the fraction of accepted pixels to be wrong in the range [0.0, 1.0].
/// Defaults to 0.1. A value of 0.1 means that 10% of the pixels are allowed to change.
/// Defaults to 0.01. A value of 0.01 means that 1% of the pixels are allowed to change.
Future<void> addImg(
String testName,
File goldenFile, {
double differentPixelsRate = 0.1,
double differentPixelsRate = 0.01,
int pixelColorDelta = 0,
required int screenshotSize,
}) async {