diff --git a/apps/example/src/offscreen.harness.tsx b/apps/example/src/offscreen.harness.tsx index 6cab45c..86546e8 100644 --- a/apps/example/src/offscreen.harness.tsx +++ b/apps/example/src/offscreen.harness.tsx @@ -10,11 +10,14 @@ import { Rect, RoundedRect, Scene, + blur, circle, createHandle, createOffscreenSurface, fill, group, + image, + imageFromPixels, rect, renderToImage, rrect, @@ -168,4 +171,60 @@ describe('tgfx offscreen surface', () => { } expect(failed).toBe(true); }); + + // Pixel assertions verify the workload; memory is measured outside the app. + test('offscreen leak soak: repeatedly renders and releases snapshots on one retained surface', async () => { + const size = 512; + const surface = createOffscreenSurface({ width: size, height: size }); + const pixels = new Uint32Array(size * size); + const scene = Scene.create(); + try { + scene.attach(surface); + for (let frame = 0; frame < 450; frame += 1) { + const x = frame % 2 === 0 ? 0 : size - 32; + const blue = frame % 256; + const top = 0xff000000 + blue * 0x10000; + pixels.fill(top); + pixels.fill(top + 0xffff, pixels.length / 2); // sharp horizontal boundary + const input = imageFromPixels(pixels.buffer, size, size); + try { + // Literal scenes isolate the worker from UI-runtime slot scheduling. + scene.replaceChildren([ + fill({ color: '#000' }), + image({ x: 0, y: 0, width: size, height: size, image: input }, [ + blur({ blur: 4 }), + ]), + rect({ x, y: 0, width: 32, height: 32, color: '#ff0000' }), + ]); + scene.flush(); + // Wait for a real worker render; flush coalescing cannot skip frames. + const snapshot = await surface.toImage(); + try { + if (frame < 2 || frame === 449) { + const bytes = new Uint8Array(await snapshot.encode('raw')); + expect(bytes.length).toBe(size * size * 4); + expect(bytes[(x + 16 * size) * 4]).toBe(255); + expect(bytes[(x + 16 * size) * 4 + 1]).toBe(0); + const background = (size / 2 + (size / 4) * size) * 4; + expect(bytes[background]).toBe(0); + expect(bytes[background + 2]).toBe(blue); + const boundary = (size / 2 + (size / 2) * size) * 4; + // Without blur this first pixel in the bottom half is 255. + expect(bytes[boundary]).toBeGreaterThan(32); + expect(bytes[boundary]).toBeLessThan(223); + } + } finally { + snapshot.dispose(); + } + } finally { + input.dispose(); + } + await sleep(150); + } + } finally { + scene.dispose(); + surface.dispose(); + } + await sleep(500); + }); }); diff --git a/packages/react-native-tgfx/cpp/Offscreen/OffscreenRender.hpp b/packages/react-native-tgfx/cpp/Offscreen/OffscreenRender.hpp index 9bf88d1..901096f 100644 --- a/packages/react-native-tgfx/cpp/Offscreen/OffscreenRender.hpp +++ b/packages/react-native-tgfx/cpp/Offscreen/OffscreenRender.hpp @@ -19,6 +19,28 @@ class Image; namespace tgfxrn { +/** + * Drains what Metal autoreleases inside its scope: offscreen GPU work runs on + * long-lived C++ threads (the surface worker, Nitro's promise pool) that never + * drain a pool of their own. A no-op off Apple. + */ +class AutoreleasePool { + public: +#if defined(__APPLE__) + AutoreleasePool(); + ~AutoreleasePool(); +#else + AutoreleasePool() = default; +#endif + AutoreleasePool(const AutoreleasePool&) = delete; + AutoreleasePool& operator=(const AutoreleasePool&) = delete; + + private: +#if defined(__APPLE__) + void* pool_; +#endif +}; + /** * Holds the shared windowless GPU context for as long as it lives, replacing a * device that lost its context for good. Throws if there is no usable GPU. @@ -35,6 +57,7 @@ class OffscreenContext { } private: + AutoreleasePool pool_; std::shared_ptr device_; tgfx::Context* context_ = nullptr; }; diff --git a/packages/react-native-tgfx/cpp/tests/CMakeLists.txt b/packages/react-native-tgfx/cpp/tests/CMakeLists.txt index 2d9090a..252ef15 100644 --- a/packages/react-native-tgfx/cpp/tests/CMakeLists.txt +++ b/packages/react-native-tgfx/cpp/tests/CMakeLists.txt @@ -14,6 +14,12 @@ add_subdirectory(${TGFX_DIR} ${CMAKE_BINARY_DIR}/tgfx) add_executable(SceneTest SceneTest.cpp ../Scene/TgfxScene.cpp ../Path/SvgPath.cpp ../Image/SvgImage.cpp ../Image/ImageRegistry.cpp ../Offscreen/OffscreenRender.cpp ../Offscreen/OffscreenSurface.cpp ../Export/PdfExport.cpp ../Export/SvgExport.cpp ../TextLayout/FontRegistry.cpp ../TextLayout/TextLayout.cpp ../TextLayout/TextShaping.cpp ../Path/PathOps.cpp ../Buffers/BufferRegistry.cpp) +if(APPLE) + enable_language(OBJCXX) + set_property(TARGET SceneTest PROPERTY OBJCXX_STANDARD 20) + target_sources(SceneTest PRIVATE ../../ios/TgfxAutoreleasePool.mm) + target_link_libraries(SceneTest "-framework Foundation") +endif() # tgfx's src too: the EXIF test names JpegCodec rather than going through # ImageCodec::MakeFrom, which would fall through to the platform decoder and # prove nothing about the embedded one. diff --git a/packages/react-native-tgfx/ios/TgfxAutoreleasePool.mm b/packages/react-native-tgfx/ios/TgfxAutoreleasePool.mm new file mode 100644 index 0000000..c6616b0 --- /dev/null +++ b/packages/react-native-tgfx/ios/TgfxAutoreleasePool.mm @@ -0,0 +1,16 @@ +#include "OffscreenRender.hpp" + +// What @autoreleasepool compiles to (Clang's ARC spec, "Runtime support"), +// called directly so the pool can span a C++ scope. +extern "C" void* objc_autoreleasePoolPush(void); +extern "C" void objc_autoreleasePoolPop(void* pool); + +namespace tgfxrn { + +AutoreleasePool::AutoreleasePool() : pool_(objc_autoreleasePoolPush()) {} + +AutoreleasePool::~AutoreleasePool() { + objc_autoreleasePoolPop(pool_); +} + +} // namespace tgfxrn