diff --git a/.changeset/swift-eels-wait.md b/.changeset/swift-eels-wait.md new file mode 100644 index 0000000000..70152a9c00 --- /dev/null +++ b/.changeset/swift-eels-wait.md @@ -0,0 +1,5 @@ +--- +"posthog-ios": patch +--- + +Upload session replay screenshots at one pixel per point whether or not the frame has masks, instead of at the screen's native scale for unmasked frames. diff --git a/PostHog.xcodeproj/project.pbxproj b/PostHog.xcodeproj/project.pbxproj index 2f48aa120d..65a6122aaa 100644 --- a/PostHog.xcodeproj/project.pbxproj +++ b/PostHog.xcodeproj/project.pbxproj @@ -337,6 +337,7 @@ DA578E982D6768BC00B3A56C /* PostHogScreenViewIntegration.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA578E972D6768B400B3A56C /* PostHogScreenViewIntegration.swift */; }; DA578E9C2D68578500B3A56C /* MockApplicationLifecyclePublisher.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA578E9B2D68578500B3A56C /* MockApplicationLifecyclePublisher.swift */; }; C08700012E00000100000870 /* PostHogReplayCameraTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = C08700022E00000100000870 /* PostHogReplayCameraTest.swift */; }; + C08700032E00000100000870 /* PostHogReplayScreenshotSizeTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = C08700042E00000100000870 /* PostHogReplayScreenshotSizeTest.swift */; }; C02440012E00000100000244 /* PostHogViewControllerTraversalTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = C02440022E00000100000244 /* PostHogViewControllerTraversalTest.swift */; }; DA578E9E2D6858BA00B3A56C /* PostHogScreenViewIntegrationTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA578E9D2D6858B200B3A56C /* PostHogScreenViewIntegrationTest.swift */; }; DA578EA02D6858CE00B3A56C /* MockScreenViewPublisher.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA578E9F2D6858C900B3A56C /* MockScreenViewPublisher.swift */; }; @@ -1195,6 +1196,7 @@ DA578E992D676AE700B3A56C /* ApplicationScreenViewPublisher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApplicationScreenViewPublisher.swift; sourceTree = ""; }; DA578E9B2D68578500B3A56C /* MockApplicationLifecyclePublisher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockApplicationLifecyclePublisher.swift; sourceTree = ""; }; C08700022E00000100000870 /* PostHogReplayCameraTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogReplayCameraTest.swift; sourceTree = ""; }; + C08700042E00000100000870 /* PostHogReplayScreenshotSizeTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogReplayScreenshotSizeTest.swift; sourceTree = ""; }; C02440022E00000100000244 /* PostHogViewControllerTraversalTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogViewControllerTraversalTest.swift; sourceTree = ""; }; DA578E9D2D6858B200B3A56C /* PostHogScreenViewIntegrationTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogScreenViewIntegrationTest.swift; sourceTree = ""; }; DA578E9F2D6858C900B3A56C /* MockScreenViewPublisher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockScreenViewPublisher.swift; sourceTree = ""; }; @@ -1911,6 +1913,7 @@ DA6F24812D4A6CA100CA2777 /* PostHogApiTest.swift */, DA578E9D2D6858B200B3A56C /* PostHogScreenViewIntegrationTest.swift */, C08700022E00000100000870 /* PostHogReplayCameraTest.swift */, + C08700042E00000100000870 /* PostHogReplayScreenshotSizeTest.swift */, C02440022E00000100000244 /* PostHogViewControllerTraversalTest.swift */, DACF6D5C2CD2F5BC00F14133 /* PostHogAutocaptureIntegrationSpec.swift */, C2A0C3FD5963418C916D3CF6 /* SwiftUITapAutocaptureTests.swift */, @@ -3792,6 +3795,7 @@ 69ED1AB62C90711D00FE7A91 /* PostHogSDKPersonProfilesTest.swift in Sources */, DA578E9E2D6858BA00B3A56C /* PostHogScreenViewIntegrationTest.swift in Sources */, C08700012E00000100000870 /* PostHogReplayCameraTest.swift in Sources */, + C08700032E00000100000870 /* PostHogReplayScreenshotSizeTest.swift in Sources */, C02440012E00000100000244 /* PostHogViewControllerTraversalTest.swift in Sources */, 3A62646A29C9E385007E8C07 /* MockPostHogServer.swift in Sources */, 690FF0BB2AEF8B8200A0B06B /* PostHogContextTest.swift in Sources */, diff --git a/PostHog/Replay/RRWireframe.swift b/PostHog/Replay/RRWireframe.swift index dd69136fce..03da817142 100644 --- a/PostHog/Replay/RRWireframe.swift +++ b/PostHog/Replay/RRWireframe.swift @@ -58,9 +58,13 @@ class RRWireframe { // Shared so tests can redact through the exact production path instead of reimplementing it. static func maskImage(_ image: UIImage, maskableWidgets: [CGRect]) -> UIImage? { guard !maskableWidgets.isEmpty else { return nil } + return redrawAtOnePixelPerPoint(image, maskableWidgets: maskableWidgets) + } - return autoreleasepool { - // Use scale=1 to preserve the existing masked screenshot payload size. + /// Screenshots render at native scale on main; this downscale runs at encode time, off main, + /// so every screenshot uploads at the same size whether or not it is masked. + private static func redrawAtOnePixelPerPoint(_ image: UIImage, maskableWidgets: [CGRect] = []) -> UIImage? { + autoreleasepool { let renderer = PostHogGraphicsImageRenderer(size: image.size, scale: 1) return renderer.image { context in context.interpolationQuality = .none @@ -110,6 +114,9 @@ class RRWireframe { // unset and let the caller drop the frame — never the raw image. maskRenderFailed = true } + } else if type == "screenshot" { + // Nothing to hide, so unlike masking a failed redraw can fall back to the native image. + base64 = (RRWireframe.redrawAtOnePixelPerPoint(image) ?? image).toBase64() } else { base64 = image.toBase64() } diff --git a/PostHogTests/PostHogReplayScreenshotSizeTest.swift b/PostHogTests/PostHogReplayScreenshotSizeTest.swift new file mode 100644 index 0000000000..e2afc71f5b --- /dev/null +++ b/PostHogTests/PostHogReplayScreenshotSizeTest.swift @@ -0,0 +1,122 @@ +#if os(iOS) + @testable import PostHog + import Testing + import UIKit + + @Suite("Replay screenshot size", .serialized) + @MainActor + struct PostHogReplayScreenshotSizeTest { + private static let secretRect = CGRect(x: 40, y: 60, width: 100, height: 80) + + private func window() throws -> UIWindow { + let root = UIViewController() + root.view.backgroundColor = .white + let secret = UIView(frame: Self.secretRect) + secret.backgroundColor = .red + root.view.addSubview(secret) + let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 200, height: 300)) + window.rootViewController = root + window.makeKeyAndVisible() + root.view.frame = window.bounds + // On a 1x screen the native render is already one pixel per point, so nothing would be tested. + try #require(window.screen.scale > 1) + return window + } + + private func uploaded(_ image: UIImage, masks: [CGRect]) throws -> CGImage { + let wireframe = RRWireframe() + wireframe.type = "screenshot" + wireframe.image = image + wireframe.maskableWidgets = masks + return try decode(wireframe) + } + + private func decode(_ wireframe: RRWireframe) throws -> CGImage { + let base64 = try #require(wireframe.toDict()["base64"] as? String) + let encoded = try #require(base64.split(separator: ",").last.map(String.init)) + let data = try #require(Data(base64Encoded: encoded)) + return try #require(UIImage(data: data)?.cgImage) + } + + private func pixelSize(_ image: CGImage) -> CGSize { + CGSize(width: image.width, height: image.height) + } + + /// Reads the pixel at `point` taken as pixel coordinates, not scaled to the image size. + private func rgb(_ image: CGImage, at point: CGPoint) throws -> [UInt8] { + var rgba = [UInt8](repeating: 0, count: 4) + let context = try #require(CGContext( + data: &rgba, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 4, + space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue + )) + let x = Int(point.x), y = Int(point.y) + context.draw(image, in: CGRect(x: -x, y: y - image.height + 1, width: image.width, height: image.height)) + return Array(rgba.prefix(3)) + } + + private func isRed(_ rgb: [UInt8]) -> Bool { + rgb[0] > 180 && rgb[1] < 90 && rgb[2] < 90 + } + + private func isBlack(_ rgb: [UInt8]) -> Bool { + rgb.allSatisfy { $0 < 50 } + } + + private func isWhite(_ rgb: [UInt8]) -> Bool { + rgb.allSatisfy { $0 > 200 } + } + + @Test("an unmasked frame uploads at one pixel per point from every renderer") + func unmaskedUploadIsOnePixelPerPoint() throws { + let window = try window() + defer { window.isHidden = true } + let renders = [ + window.toImage(preferFidelityRenderer: false), + window.toImage(preferFidelityRenderer: true), + window.toImage(afterScreenUpdates: true), + ] + for render in renders { + let image = try #require(render) + #expect(try pixelSize(uploaded(image, masks: [])) == window.bounds.size) + } + } + + @Test("a wireframe-mode image uploads at its own size") + func wireframeImageKeepsItsSize() throws { + let window = try window() + defer { window.isHidden = true } + let image = try #require(window.toImage(preferFidelityRenderer: false)) + let wireframe = RRWireframe() + wireframe.type = "image" + wireframe.image = image + let uploaded = try decode(wireframe) + #expect(uploaded.width == image.cgImage?.width) + #expect(uploaded.height == image.cgImage?.height) + } + + @Test("a masked frame uploads at the unmasked size, with the mask on the secret's pixels") + func maskLandsOnUnmaskedPixels() throws { + let window = try window() + defer { window.isHidden = true } + let image = try #require(window.toImage(preferFidelityRenderer: false)) + let unmasked = try uploaded(image, masks: []) + let masked = try uploaded(image, masks: [Self.secretRect]) + #expect(pixelSize(masked) == pixelSize(unmasked)) + + let rect = Self.secretRect + let inside = [CGPoint(x: rect.midX, y: rect.midY), CGPoint(x: rect.minX + 12, y: rect.minY + 12), + CGPoint(x: rect.maxX - 12, y: rect.maxY - 12)] + for point in inside { + #expect(try isRed(rgb(unmasked, at: point)), "unmasked frame at \(point)") + #expect(try isBlack(rgb(masked, at: point)), "masked frame at \(point)") + } + + let outside = [CGPoint(x: rect.minX - 8, y: rect.midY), CGPoint(x: rect.maxX + 8, y: rect.midY), + CGPoint(x: rect.midX, y: rect.maxY + 8)] + for point in outside { + #expect(try isWhite(rgb(unmasked, at: point)), "unmasked frame at \(point)") + #expect(try isWhite(rgb(masked, at: point)), "masked frame at \(point)") + } + } + } +#endif