Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/content/docs/images.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-tgfx/cpp/Image/ImageRegistry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 8 additions & 2 deletions packages/react-native-tgfx/cpp/Render/TgfxRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ namespace {
std::mutex renderersMutex;
std::vector<TgfxRenderer*> renderers;
std::atomic<uint64_t> 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() {
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions packages/react-native-tgfx/src/hooks/useImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions packages/react-native-tgfx/src/specs/TgfxImage.nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,8 @@ export interface TgfxImageFactory extends HybridObject<{
): Promise<TgfxImage>

/**
* 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

Expand Down
Loading