B252: keep closed textures valid until the engine is done with them

Windows: closeTexture hands the texture to UnregisterTexture's callback and
frees its frame buffers there; frames pushed after close are dropped. Without
"recycle" the object is kept for the process lifetime (the session may still
hold its pointer); an engine that shuts down keeps its open textures the same
way. A duplicate createTexture key gets -1, getTexturePtr of an unknown key 0.

macOS: closeTexture drops the frame and closes the texture to later frames;
only "recycle" removes it from the plugin.

Linux: the frame handed to the engine last is freed at that engine's next
copy_pixels or once the engine has shut down, not at close: from Flutter 3.32
the raster thread can still be uploading it.

Tests: windows/test/run_tests.sh (plain, --asan, --tsan) and
linux/test/run_tests.sh (plain, --asan).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
yunchan 2026-10-27 03:19:05 +09:00
parent 8cd34a80ff
commit f38b9420ac
15 changed files with 1388 additions and 31 deletions

View file

@ -7,7 +7,9 @@
// registrar with the reference rules of the Flutter 3.24 engine, and a plugin
// registrar. Frame buffers that the plugin copies with new[] are counted.
// The engine's GL work (fl_pixel_buffer_texture_populate) is replaced by calls
// to the copy_pixels hook it would make; no GL context is needed.
// to the copy_pixels hook it would make; no GL context is needed. Reading the
// frame copy_pixels returned after the call stands for the glTexImage2D that
// populate makes then.
//
// Build and run: linux/test/run_tests.sh [--asan]
@ -47,6 +49,19 @@ int live_frames() {
return n;
}
bool frame_is_live(const void* p) {
lock_frames();
bool live = false;
for (int i = 0; i < kSlots && !live; i++) {
live = g_frames[i] == p;
}
unlock_frames();
return live;
}
// Keeps the reads of an "upload" from being optimized away.
volatile uint8_t g_uploaded;
} // namespace
void* operator new[](size_t size) {
@ -565,8 +580,9 @@ int g_failed_checks = 0;
} \
} while (0)
// D1: closing a texture frees the frame it shows and the frame still waiting
// for the next raster frame.
// D1: closing a texture frees the frame still waiting for the next raster
// frame at once, and the frame it shows at the window's next raster frame,
// when the engine cannot be uploading that frame any more.
static void test_close_frees_frame_buffers() {
const int base = live_frames();
Engine* e = engine_new();
@ -580,10 +596,80 @@ static void test_close_frees_frame_buffers() {
send_frame(ptr, 2);
CHECK(live_frames() == base + 2);
CHECK(close_texture(e, 101, 0) == 1);
CHECK(live_frames() == base + 1);
int64_t other = create_texture(e, 102);
CHECK(pull_frame(other).ok);
CHECK(live_frames() == base);
engine_shutdown(e);
}
// B252: from Flutter 3.32 the Linux engine rasterizes on its own thread (up to
// 3.29 on the platform thread, where a close cannot come in between).
// fl_pixel_buffer_texture_populate uploads the frame copy_pixels returned
// after the call, and unregistering the texture does not wait for that, so a
// close on the platform thread can come while the frame is uploading.
static void test_close_keeps_frame_being_uploaded() {
const int base = live_frames();
for (int recycle = 0; recycle <= 1; recycle++) {
Engine* e = engine_new();
int64_t key = 150 + recycle;
int64_t ptr = create_texture(e, key);
send_frame(ptr, 0x5A);
Pulled uploading = pull_frame(ptr);
CHECK(uploading.ok && uploading.width == kWidth);
send_frame(ptr, 0x5B);
CHECK(close_texture(e, key, recycle) == 1);
CHECK(frame_is_live(uploading.buffer));
CHECK(uploading.buffer[0] == 0x5A &&
uploading.buffer[kFrameBytes - 1] == 0x5A);
// A raster frame that had looked the texture up before the close gets no
// frame, and the frame uploaded before is freed.
CHECK(!pull_frame(ptr).ok);
CHECK(live_frames() == base);
engine_shutdown(e);
}
}
// B252: a closed texture's frame is freed by its own window's raster thread
// only; another window's raster thread runs while this one uploads.
static void test_shown_frame_waits_for_its_own_window() {
const int base = live_frames();
Engine* w1 = engine_new();
Engine* w2 = engine_new();
int64_t a = create_texture(w1, 170);
int64_t b = create_texture(w2, 171);
send_frame(a, 40);
Pulled uploading = pull_frame(a);
CHECK(uploading.ok);
CHECK(close_texture(w1, 170, 1) == 1);
CHECK(pull_frame(b).ok);
CHECK(frame_is_live(uploading.buffer));
int64_t c = create_texture(w1, 172);
CHECK(c == a);
CHECK(pull_frame(c).ok);
CHECK(live_frames() == base);
engine_shutdown(w1);
engine_shutdown(w2);
}
// B252: when a window goes away (its engine stops rasterizing first), the
// frames its closed textures showed last are freed.
static void test_engine_shutdown_frees_shown_frames() {
const int base = live_frames();
Engine* e = engine_new();
int64_t a = create_texture(e, 160);
int64_t b = create_texture(e, 161);
send_frame(a, 30);
CHECK(pull_frame(a).ok);
send_frame(b, 31);
CHECK(pull_frame(b).ok);
CHECK(close_texture(e, 160, 0) == 1);
CHECK(close_texture(e, 161, 1) == 1);
CHECK(live_frames() == base + 2);
engine_shutdown(e);
CHECK(live_frames() == base);
}
// D1: after close, late frames are dropped and the texture never hands out a
// frame again (it would be a freed buffer).
static void test_closed_texture_ignores_late_frames() {
@ -643,11 +729,13 @@ static void test_recycled_texture_is_reused_in_same_window() {
send_frame(ptr, 8);
CHECK(close_texture(e, key, 1) == 1);
CHECK(!registered(e->textures, ptr));
CHECK(live_frames() == base);
// The frame shown last waits for the window's next raster frame.
CHECK(live_frames() == base + 1);
}
CHECK(seen.size() == 1);
CHECK(!finalized);
engine_shutdown(e);
CHECK(live_frames() == base);
}
// D2, multi-window: windows never get each other's textures back.
@ -790,21 +878,68 @@ static void test_multi_display_tab() {
// Close the tab, open another one with two displays.
CHECK(close_texture(e, 800, 1) == 1);
CHECK(close_texture(e, 802, 1) == 1);
CHECK(live_frames() == base);
CHECK(live_frames() == base + 2);
int64_t n0 = create_texture(e, 803);
int64_t n1 = create_texture(e, 804);
CHECK(std::set<int64_t>({n0, n1}) == std::set<int64_t>({d0, d1}));
CHECK(pull_frame(n0).ok);
CHECK(live_frames() == base);
CHECK(close_texture(e, 803, 1) == 1);
CHECK(close_texture(e, 804, 1) == 1);
engine_shutdown(e);
}
// B252 with threads (Flutter 3.32 and later): a raster thread uploads each
// frame it gets while the video thread pushes frames and the platform thread
// closes the texture.
static void test_raster_thread_uploads_while_closing() {
const int base = live_frames();
for (int round = 0; round < 100; round++) {
Engine* e = engine_new();
int64_t key = 900 + round;
int64_t ptr = create_texture(e, key);
std::atomic<bool> stop{false};
std::thread video([ptr, &stop]() {
for (int i = 0; !stop.load(); i++) {
send_frame(ptr, static_cast<uint8_t>(10 + i % 2));
}
});
std::thread raster([ptr, &stop]() {
while (!stop.load()) {
Pulled p = pull_frame(ptr);
if (p.ok && p.width == kWidth) {
uint8_t x = 0;
for (size_t i = 0; i < kFrameBytes; i++) {
x ^= p.buffer[i];
}
g_uploaded = x;
}
}
});
g_usleep(1000);
int64_t closed = close_texture(e, key, round % 2);
g_usleep(1000);
stop = true;
video.join();
raster.join();
engine_shutdown(e);
CHECK(closed == 1);
}
CHECK(live_frames() == base);
}
int main() {
struct {
const char* name;
void (*run)();
} tests[] = {
{"close_frees_frame_buffers", test_close_frees_frame_buffers},
{"close_keeps_frame_being_uploaded",
test_close_keeps_frame_being_uploaded},
{"shown_frame_waits_for_its_own_window",
test_shown_frame_waits_for_its_own_window},
{"engine_shutdown_frees_shown_frames",
test_engine_shutdown_frees_shown_frames},
{"closed_texture_ignores_late_frames",
test_closed_texture_ignores_late_frames},
{"finalize_frees_frame_buffers", test_finalize_frees_frame_buffers},
@ -819,6 +954,8 @@ int main() {
test_engine_shutdown_keeps_recycled_textures},
{"frames_racing_close", test_frames_racing_close},
{"multi_display_tab", test_multi_display_tab},
{"raster_thread_uploads_while_closing",
test_raster_thread_uploads_while_closing},
};
int failed = 0;
int count = 0;