imgcache

package
v0.2.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2026 License: MIT Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package imgcache caches decoded raster images and the resampled rasters widget.Image paints from, under one shared LRU budget. It is the raster-image counterpart of internal/icons: same shape (a keyed map with sticky decode failures behind a mutex), but entries evict least-recently-used under an entry cap and a byte cap, because media widgets swap album art and avatars far more often than themes move.

Index

Constants

View Source
const DefaultByteLimit = 64 << 20

DefaultByteLimit is the byte half of the default budget: 64 MiB of decoded/resampled pixels.

View Source
const DefaultEntryLimit = 512

DefaultEntryLimit is the process-wide entry budget. A bar holds a handful of album covers and avatars at one or two sizes, so the default is generous without being unbounded.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache memoizes decoded sources and resampled rasters under one LRU budget. It is safe for concurrent use: the decode goroutine of an async image load consults it off the loop goroutine, while paints read it on the loop.

Failures stick: a source that failed to decode keeps its error until InvalidateSource, Reset, or eviction drops it, so a corrupt file costs one decode attempt, not one per frame.

func Default

func Default() *Cache

Default returns the process-wide cache widget.Image resolves through.

func New

func New() *Cache

New returns a cache with the default limits.

func (*Cache) Bytes

func (c *Cache) Bytes() int

Bytes returns the budgeted byte total of everything cached (tests).

func (*Cache) HasRaster

func (c *Cache) HasRaster(key RasterKey) bool

HasRaster reports whether the raster key is cached (tests).

func (*Cache) HasSource

func (c *Cache) HasSource(key string) bool

HasSource reports whether the source called key is cached (tests).

func (*Cache) InvalidateSource

func (c *Cache) InvalidateSource(key string)

InvalidateSource drops the decoded source called key and every raster derived from it. SetImage calls it so a swapped source cannot leave stale pixels behind.

func (*Cache) Len

func (c *Cache) Len() (sources, rasters int)

Len returns the number of cached sources and rasters (tests).

func (*Cache) Raster

func (c *Cache) Raster(key RasterKey, fill func(dst *image.RGBA) error) (render.StillImage, error)

Raster returns the resampled raster for key, key.W x key.H, as a still image with its own identity, so every widget drawing it shares one GPU texture. On a miss fill writes the pixels into the cache's own memory, off the Go heap. A raster is valid for the frame that draws it: eviction frees it once every recording that might draw it has ended (render.Retire), so a caller draws it and keeps nothing.

func (*Cache) Reset

func (c *Cache) Reset()

Reset drops every entry.

func (*Cache) SetForget

func (c *Cache) SetForget(forget func(render.ImageID))

SetForget sets what drops an evicted raster's GPU texture, nil for nothing: the GPU renderer's device, while one draws. It is called on whichever goroutine evicts.

func (*Cache) SetLimits

func (c *Cache) SetLimits(maxEntries, maxBytes int)

SetLimits changes the entry and byte budgets. It does not evict anything by itself; the next insert trims back under the new limits.

func (*Cache) Source

func (c *Cache) Source(key string, decode func() (image.Image, error)) (image.Image, error)

Source returns the decoded form of the source called key, calling decode once on a miss. Whatever decode returns is normalized into an *image.RGBA (premultiplied, the canvas's format) so the byte accounting is exact and every consumer blends uniformly. An empty key skips the cache and decodes every call.

func (*Cache) Stats

func (c *Cache) Stats() (hits, misses int)

Stats returns the cumulative hit and miss counts. A paint that hits the cache must not re-resample; tests read the miss counter to pin that.

type RasterKey

type RasterKey struct {
	Source string
	Frame  int
	Image  render.ImageID
	Src    image.Rectangle
	W, H   int
}

RasterKey identifies one resampled raster: the source it was derived from (and which frame of an animated source), the source rectangle that was sampled, and the device-pixel size it was resampled into. The device scale is implied by W and H: identical keys want identical pixels. InvalidateSource drops every frame of a source. A raster that never changes may name its source by Image, its identity, instead of Source and Frame.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL