Commit graph

17 commits

Author SHA1 Message Date
b87d45fd09 macos: drain an autorelease pool around each frame (Kitedesk T-4204)
FlutterRgbaRendererPluginOnRgba runs on the caller's thread, a Kitedesk
video thread, which has no autorelease pool and lives as long as the
session: an object autoreleased there would stay until the session ends
(pointed out in upstream PR #2). Each frame now runs in an @autoreleasepool.

On macOS 26.5.2 the frame path autoreleases nothing on that thread, before
and after this series (frames_leave_no_autoreleased_objects passes on
50efc18e too), so this is a guard: a probe saw CoreVideo autorelease objects
when a process first made an IOSurface on such a thread, and the engine's
registrar logs with NSLog for a frame announced to it after its engine is
gone, which the old code did on the video thread.

Tests: 13 of 13 pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 09:56:22 +09:00
6c3425539f macos: let closed textures' buffers go, keep closed identities (Kitedesk T-4204)
closeTexture unregistered the texture but kept it in the plugin's map with
its last frame (33 MB at 4K) until the engine went away, and the texture
went on taking and announcing frames pushed to its pointer. Kitedesk makes a
texture per display, so each display switch and closed tab leaked a frame.

closeTexture now takes the texture out of the map and closes it: frames
pushed afterwards are dropped and not announced, the engine gets no frame any
more, and the frames, the handed buffers and the pool go. As on Windows
(B252), a texture closed without "recycle" may still get frames through the
pointer from getTexturePtr (a tab moving to another window keeps its
session's pointer), so it is kept for the process lifetime and its address
is never another texture's; with "recycle" it is freed once the engine has
unregistered it. The textures still open when the engine goes away are closed
and kept the same way: the registry's reference to them used to keep them
alive, announcing every frame to a registry without an engine. A closed
texture no longer holds the registry.

Tests: closing_frees_frame_buffers (634 MB more after 20 4K textures, now
0.2 MB), closed_texture_drops_late_frames,
closing_keeps_only_unrecycled_textures, open_textures_outlive_their_engine
and frames_racing_close pass: 13 of 13.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 09:56:22 +09:00
2a36975b62 macos: announce frames on the main queue (Kitedesk T-4204)
textureFrameAvailable was called on the thread that pushed the frame, a
Kitedesk video thread. The engine wants it on the platform thread
(Shell::OnPlatformViewMarkTextureFrameAvailable checks it, engine
a18df97c), as it wants registerTexture and unregisterTexture, which already
run there; Kitedesk's own GPU texture (KdMatoyaTexture.swift) posts its
notices the same way.

The notice is now posted to the main queue; the registry is only used on the
main thread.

Tests: frames_are_announced_on_the_main_thread passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 09:56:22 +09:00
11478d230c macos: copy frames outside the queue the raster thread waits on (Kitedesk T-4204)
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>
2026-11-15 09:56:22 +09:00
7af0d01118 macos: take frame buffers from a pool, keep the two last handed (Kitedesk T-4204)
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>
2026-11-15 09:56:22 +09:00
2c334da6f9 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 <noreply@anthropic.com>
2026-11-15 09:56:22 +09:00
fufesou
b91e1b2092
fix memory leak on macOS (#9)
Signed-off-by: fufesou <shuanglongchen@yeah.net>
2023-03-14 12:44:36 +08:00
fufesou
8d66a7c55e
Refact/stride align (#8)
* rename row_align_bytes  to stride_align

Signed-off-by: fufesou <shuanglongchen@yeah.net>

* trivail changes

Signed-off-by: fufesou <shuanglongchen@yeah.net>

---------

Signed-off-by: fufesou <shuanglongchen@yeah.net>
2023-03-03 09:46:33 +08:00
fufesou
873919902e
Feat/row align bytes (#7)
* better bytes alignment

Signed-off-by: fufesou <shuanglongchen@yeah.net>

* debug macos

Signed-off-by: fufesou <shuanglongchen@yeah.net>

* debug linux

Signed-off-by: fufesou <shuanglongchen@yeah.net>

* windows switch_rgba

Signed-off-by: fufesou <shuanglongchen@yeah.net>

* better switch_rgba

Signed-off-by: fufesou <shuanglongchen@yeah.net>

---------

Signed-off-by: fufesou <shuanglongchen@yeah.net>
2023-03-03 09:30:05 +08:00
fufesou
044bb260d9
add stride_align for onRgba (#6)
Signed-off-by: fufesou <shuanglongchen@yeah.net>
2023-03-02 21:38:55 +08:00
Kingtous
4f059a9138 fix: macos ptr access 2023-02-19 23:22:40 -08:00
Kingtous
6fd900c479 feat: texture rgba interface 2023-02-19 19:14:48 -08:00
Kingtous
71e77ef3cf fix: metal issue on macos 2023-02-17 22:23:39 +08:00
Kingtous
0e16af448c opt: add doc for macOS 2023-02-17 01:52:16 -08:00
Kingtous
4221adb1cb feat: initial macos 2023-02-17 01:35:25 -08:00
Kingtous
1f012dffac add: macos impl 2023-02-17 00:55:21 -08:00
Kingtous
b2bd5910e7 feat: init repo 2023-02-16 15:03:12 +08:00