The whole frame, buffer and copy included, ran in queue.sync, and the
engine's raster thread takes the newest frame in copyPixelBuffer on the same
queue, so it waited for the copy: R T-4201 measured a p90 of 4.0 ms at 60 Hz
(6.5 ms at 120 Hz), and the raster thread draws the whole window.
The frame is now copied into its pool buffer outside the queue; the queue
only guards the pool, the newest frame and the handed buffers, so it is held
for a pointer swap.
Tests: raster_does_not_wait_for_copies passes (4K frames back to back: the
raster's median wait was 0.9 to 1.1 ms in three runs, up to 537 of 600 calls
over 1 ms; now 0.003 ms, at most 0.032 ms).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Every frame made a new IOSurface-backed CVPixelBuffer (33 MB at 4K), and
the engine let it go soon after. R T-4201 measured 2.2 to 7.8 ms per 4K
frame alone, up to 110 ms, and a 6.5 ms median at 60 fps; upstream #16922
saw 37% of the CPU and memory swings of 1.85 to 2.43 GB.
Frames now go into the buffers of a CVPixelBufferPool with IOSurface
properties, made again when the frame size changes. A pool hands a buffer
out again as soon as nobody holds it, even while its IOSurface backs a live
Metal texture (measured), and engine a18df97c lets go of the pixel buffer and
of its CVMetalTexture as soon as it has the Metal texture, which Skia then
draws until the next frame. So the texture keeps the buffers it last handed
to the engine: the one drawn, and the one drawn before it, which GPU work
queued meanwhile may still read (the plugin cannot see when the GPU is done,
flutter#157379; inferred). With the newest frame and the one being written, a
texture uses three buffers while the engine keeps up and four when it falls
behind.
Tests: pool_reuses_buffers passes (60 IOSurfaces for 60 frames of a size,
now at most 4). drawn_frame_is_not_overwritten fails when no handed buffer
is kept, frame_drawn_before_is_not_overwritten when one is; both pass with
two.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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 <noreply@anthropic.com>
Standalone Objective-C tests for the macOS texture, built against
FlutterMacOS.framework of the active Flutter SDK by macos/test/run_tests.sh
(optionally with ASan or TSan). The plugin sources are compiled into the
test; the plugin registers through a fake plugin registrar and is driven
through handleMethodCall:result:. The fake engine follows
FlutterTextureRegistrar.mm and FlutterExternalTexture.mm of engine a18df97c:
the texture id is the texture's address, the registrar keeps registered
textures, and copyPixelBuffer is wrapped into a Metal texture whose
CVMetalTexture and pixel buffer are released at once while the Metal texture
stays drawn. Frames come through FlutterRgbaRendererPluginOnRgba on a thread
without an autorelease pool. Memory is the task's phys_footprint, which counts
IOSurfaces, read after the engine's CVMetalTextureCache has let go of what it
wrapped (it keeps that until it makes a texture a second later, measured);
it is not checked under ASan, whose shadow of each new IOSurface mapping
counts in it. No app, window or screen capture.
On 50efc18e ten of the thirteen tests fail (Mac mini M4, macOS 26.5.2):
every frame gets a new IOSurface (60 for 60 frames), the raster thread waits
for the copy (median 0.9 to 1.1 ms in three runs, up to 537 of 600 calls over
1 ms, with 4K frames), frames are announced off the main thread, closed
textures keep their last frame (634 MB more after 20 4K textures) and still
take and announce late frames, a texture closed with recycle is never freed,
and rows of another stride than the pixel buffer's are copied in one piece
(shifted rows for 1366, 1001, 1279 and 3838 pixels wide with stride_align 1,
and 1001 with 256), short frames included. The two tests on frames the engine
was handed pass, as no buffer is ever written twice yet.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>