diff --git a/docs/content/docs/images.mdx b/docs/content/docs/images.mdx index 07886cf..38da911 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 ddb147b..d5e309f 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 c8ee438..a2af52d 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 5143343..a4e84fb 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 8cb719b..0e00503 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