diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d600515..5d7bab2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## Next +- perf: Optimize Screenshot diff check ([#271](https://github.com/PostHog/posthog-flutter/pull/271)) - chore: improve survey color handling ([#233](https://github.com/PostHog/posthog-flutter/pull/233)) - feat: add `beforeSend` callback to `PostHogConfig` for dropping or modifying events before they are sent to PostHog ([#255](https://github.com/PostHog/posthog-flutter/pull/255)) diff --git a/lib/src/replay/screenshot/screenshot_capturer.dart b/lib/src/replay/screenshot/screenshot_capturer.dart index 79f50c03..e092d49a 100644 --- a/lib/src/replay/screenshot/screenshot_capturer.dart +++ b/lib/src/replay/screenshot/screenshot_capturer.dart @@ -54,10 +54,10 @@ class ScreenshotCapturer { return min(width / srcWidth, height / srcHeight); } - Future _getImageBytes(ui.Image img) async { + Future _getImageBytes(ui.Image img, + {ui.ImageByteFormat format = ui.ImageByteFormat.png}) async { try { - final ByteData? byteData = - await img.toByteData(format: ui.ImageByteFormat.png); + final ByteData? byteData = await img.toByteData(format: format); if (byteData == null || byteData.lengthInBytes == 0) { printIfDebug('Error: Failed to convert image to byte data.'); return null; @@ -133,11 +133,10 @@ class ScreenshotCapturer { final recorder = ui.PictureRecorder(); final canvas = Canvas(recorder); - // using png because its compressed, the native SDKs will decompress it - // and transform to webp or jpeg if needed - // https://github.com/brendan-duncan/image does not have webp encoding - Uint8List? pngBytes = await _getImageBytes(image); - if (pngBytes == null || pngBytes.isEmpty) { + // using rawRgba for the diff check because it is faster than png encoding + Uint8List? imageBytes = + await _getImageBytes(image, format: ui.ImageByteFormat.rawRgba); + if (imageBytes == null || imageBytes.isEmpty) { printIfDebug( 'Error: Failed to convert image byte data to Uint8List.'); recorder.endRecording().dispose(); @@ -146,7 +145,7 @@ class ScreenshotCapturer { return; } - if (const PHListEquality().equals(pngBytes, statusView.imageBytes)) { + if (const PHListEquality().equals(imageBytes, statusView.imageBytes)) { printIfDebug( 'Debug: Snapshot is the same as the last one, nothing changed, do nothing.'); recorder.endRecording().dispose(); @@ -155,7 +154,7 @@ class ScreenshotCapturer { return; } - statusView.imageBytes = pngBytes; + statusView.imageBytes = imageBytes; try { canvas.drawImage(image, Offset.zero, Paint());