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
- type Cache
- func (c *Cache) Bytes() int
- func (c *Cache) HasRaster(key RasterKey) bool
- func (c *Cache) HasSource(key string) bool
- func (c *Cache) InvalidateSource(key string)
- func (c *Cache) Len() (sources, rasters int)
- func (c *Cache) Raster(key RasterKey, fill func(dst *image.RGBA) error) (render.StillImage, error)
- func (c *Cache) Reset()
- func (c *Cache) SetForget(forget func(render.ImageID)) (remove func())
- func (c *Cache) SetLimits(maxEntries, maxBytes int)
- func (c *Cache) Source(key string, decode func() (image.Image, error)) (image.Image, error)
- func (c *Cache) Stats() (hits, misses int)
- type RasterKey
Constants ¶
const DefaultByteLimit = 64 << 20
DefaultByteLimit is the byte half of the default budget: 64 MiB of decoded/resampled pixels.
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 (*Cache) InvalidateSource ¶
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) Raster ¶
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) SetForget ¶
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. The returned function removes this one and leaves a later install in place.
func (*Cache) SetLimits ¶
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 ¶
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.
type RasterKey ¶
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.