From 2c334da6f91a2818888e7bb968f22f703b6c8c8e Mon Sep 17 00:00:00 2001 From: yunchan Date: Sun, 15 Nov 2026 09:44:38 +0900 Subject: [PATCH] macos: copy rows of another stride one by one (Kitedesk T-4204) FlutterRgbaRendererPluginOnRgba gets rows padded to stride_align, and the pixel buffer has its own bytes per row ((width * 4 + 63) & ~63 on Apple Silicon). The frame was copied in one memcpy of len bytes, so rows came out shifted whenever the two differ: Kitedesk sends stride_align 1 for a session drawn in software, whose "all displays" view still uses textures, and any width that is not a multiple of 16 then shears (1366, 1001, 1279, 3838). A stride above the pixel buffer's wrote past its data. Rows are now copied one by one, width * 4 bytes each, when the strides differ, and in one memcpy of the rows otherwise. A frame shorter than its rows promise is dropped instead of being read past its end. Tests: rows_follow_the_frame_stride and short_frames_are_dropped pass; the other eight that failed still do. Co-Authored-By: Claude Opus 5.5 --- macos/Classes/TextRgba.swift | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/macos/Classes/TextRgba.swift b/macos/Classes/TextRgba.swift index 1708c42..c2860bf 100644 --- a/macos/Classes/TextRgba.swift +++ b/macos/Classes/TextRgba.swift @@ -4,6 +4,9 @@ // // Created by kingtous on 2023/2/17. // +// Modified by the Kitedesk fork (2026, T-4204): rows are copied one by one +// when their stride differs from the pixel buffer's. +// import Foundation import FlutterMacOS @@ -34,6 +37,12 @@ import CoreVideo } private func _markFrameAvaliable(buffer: UnsafePointer, len: Int, width: Int, height: Int, stride_align: Int) -> Bool { + // The rows of buffer are padded to stride_align. + let row = width * 4 + let stride = stride_align > 1 ? (row + stride_align - 1) / stride_align * stride_align : row + guard width > 0 && height > 0 && len >= stride * height else { + return false + } var pixelBufferCopy: CVPixelBuffer? // macOS only support 32BGRA currently. let dict: [String: Any] = [ @@ -50,7 +59,14 @@ import CoreVideo CVPixelBufferLockBaseAddress(pixelBufferCopy!, []) let ptr = CVPixelBufferGetBaseAddress(pixelBufferCopy!)! - memcpy(ptr, buffer, len) + let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBufferCopy!) + if (bytesPerRow == stride) { + memcpy(ptr, buffer, stride * height) + } else { + for y in 0..