From 68318aa6c4d95d77e8160d045df9f949ba4f16fc Mon Sep 17 00:00:00 2001 From: riteshshukla04 Date: Tue, 10 Nov 2026 23:57:44 +0530 Subject: [PATCH] fix(render): keep a canvas's GPU cache at 256 MB or more A canvas's GPU cache was capped at the image budget, 64 MB by default. It also holds every frame's path geometry, and 10,000 paths need about 86 MB, so the geometry was evicted and rebuilt every frame. A larger image budget still raises the cache; resources unused for 120 frames are freed as before. --- docs/content/docs/images.mdx | 3 ++- packages/react-native-tgfx/cpp/Image/ImageRegistry.hpp | 2 +- packages/react-native-tgfx/cpp/Render/TgfxRenderer.cpp | 10 ++++++++-- packages/react-native-tgfx/src/hooks/useImage.ts | 5 ++--- .../react-native-tgfx/src/specs/TgfxImage.nitro.ts | 6 ++---- 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/docs/content/docs/images.mdx b/docs/content/docs/images.mdx index 07886cfc..38da9112 100644 --- a/docs/content/docs/images.mdx +++ b/docs/content/docs/images.mdx @@ -252,7 +252,8 @@ if (image != null) { setImageCacheLimit(32 * 1024 * 1024) ``` -The default is 64 MB, applied to both the decoded-pixel and GPU caches. +The default is 64 MB of decoded pixels. A canvas's GPU cache uses the same +limit when it is above 256 MB, and 256 MB otherwise. [`getCacheInfo`](/api/functions/getCacheInfo) reports usage. [`purgeCaches`](/api/functions/purgeCaches) frees it all, e.g. on memory pressure: diff --git a/packages/react-native-tgfx/cpp/Image/ImageRegistry.hpp b/packages/react-native-tgfx/cpp/Image/ImageRegistry.hpp index ddb147b4..d5e309fd 100644 --- a/packages/react-native-tgfx/cpp/Image/ImageRegistry.hpp +++ b/packages/react-native-tgfx/cpp/Image/ImageRegistry.hpp @@ -23,7 +23,7 @@ class ImageCodec; namespace tgfxrn { -/// Default budget for decoded pixels. Applied to the GPU cache too: an image is resident twice. +/// Default budget for decoded pixels. A canvas's GPU cache follows it above a floor (TgfxRenderer). inline constexpr size_t kDefaultImageCacheBytes = 64u << 20; struct ImageHandle { diff --git a/packages/react-native-tgfx/cpp/Render/TgfxRenderer.cpp b/packages/react-native-tgfx/cpp/Render/TgfxRenderer.cpp index c8ee4386..a2af52df 100644 --- a/packages/react-native-tgfx/cpp/Render/TgfxRenderer.cpp +++ b/packages/react-native-tgfx/cpp/Render/TgfxRenderer.cpp @@ -31,6 +31,11 @@ namespace { std::mutex renderersMutex; std::vector renderers; std::atomic purgeGeneration{1}; + +/// A canvas's GPU cache never goes below this, whatever the image budget. It also holds each +/// frame's path geometry and glyphs, so a budget smaller than one frame's set rebuilds them +/// every frame. Resources unused for 120 frames are freed regardless. +constexpr size_t kMinCanvasGpuCacheBytes = 256u << 20; } // namespace TgfxRenderer::TgfxRenderer() { @@ -365,8 +370,9 @@ bool TgfxRenderer::draw() { context->purgeResourcesNotUsedSince(std::chrono::steady_clock::now()); purgedGen_.store(purgeGen, std::memory_order_release); } - // A decoded image is resident twice, CPU and GPU, so both follow setImageCacheLimit(). - size_t cacheLimit = ImageRegistry::instance().cacheLimit(); + // A decoded image is resident twice, CPU and GPU, so a larger setImageCacheLimit() raises this + // too; a smaller one leaves the floor for the frame's geometry. + size_t cacheLimit = std::max(ImageRegistry::instance().cacheLimit(), kMinCanvasGpuCacheBytes); if (!cacheLimitApplied_ || appliedCacheLimit_ != cacheLimit) { context->setCacheLimit(cacheLimit); appliedCacheLimit_ = cacheLimit; diff --git a/packages/react-native-tgfx/src/hooks/useImage.ts b/packages/react-native-tgfx/src/hooks/useImage.ts index 51433434..a4e84fbc 100644 --- a/packages/react-native-tgfx/src/hooks/useImage.ts +++ b/packages/react-native-tgfx/src/hooks/useImage.ts @@ -18,9 +18,8 @@ function images(): TgfxImageFactory { } /** - * Byte budget for decoded images. A decoded image is resident twice - as CPU - * pixels and as the GPU texture uploaded from them - so this is applied to both - * the pixel cache and the tgfx context's resource cache. Defaults to 64 MB. + * Byte budget for decoded images. Defaults to 64 MB. A canvas's GPU cache uses + * the same budget when it is above 256 MB, and 256 MB otherwise. */ export function setImageCacheLimit(bytes: number): void { images().setCacheLimit(bytes) diff --git a/packages/react-native-tgfx/src/specs/TgfxImage.nitro.ts b/packages/react-native-tgfx/src/specs/TgfxImage.nitro.ts index 8cb719b6..0e005034 100644 --- a/packages/react-native-tgfx/src/specs/TgfxImage.nitro.ts +++ b/packages/react-native-tgfx/src/specs/TgfxImage.nitro.ts @@ -248,10 +248,8 @@ export interface TgfxImageFactory extends HybridObject<{ ): Promise /** - * Byte budget for decoded images. A decoded image is resident twice - as CPU - * pixels here and as the GPU texture uploaded from them - so this number is - * applied to both the pixel cache and the tgfx context's resource cache - * rather than to only one of them. Defaults to 64 MB. + * Byte budget for decoded images. Defaults to 64 MB. A canvas's GPU cache + * uses the same budget when it is above 256 MB, and 256 MB otherwise. */ setCacheLimit(bytes: number): void