mirror of
https://github.com/Dadido3/noita-mapcap.git
synced 2024-11-18 17:17:31 +00:00
Let StitchedImage invalidate cache based on time
This commit is contained in:
parent
3016919348
commit
a0d5c13557
@ -1,4 +1,4 @@
|
|||||||
// Copyright (c) 2022 David Vogel
|
// Copyright (c) 2022-2023 David Vogel
|
||||||
//
|
//
|
||||||
// This software is released under the MIT License.
|
// This software is released under the MIT License.
|
||||||
// https://opensource.org/licenses/MIT
|
// https://opensource.org/licenses/MIT
|
||||||
@ -21,6 +21,8 @@ type StitchedImageCache struct {
|
|||||||
|
|
||||||
rect image.Rectangle // Position and size of the cached area.
|
rect image.Rectangle // Position and size of the cached area.
|
||||||
image *image.RGBA // Cached RGBA image. The bounds of this image are determined by the filename.
|
image *image.RGBA // Cached RGBA image. The bounds of this image are determined by the filename.
|
||||||
|
|
||||||
|
idleCounter byte // Is incremented when this cache object idles, and reset every time the cache is used.
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStitchedImageCache(stitchedImage *StitchedImage, rect image.Rectangle) StitchedImageCache {
|
func NewStitchedImageCache(stitchedImage *StitchedImage, rect image.Rectangle) StitchedImageCache {
|
||||||
@ -30,6 +32,21 @@ func NewStitchedImageCache(stitchedImage *StitchedImage, rect image.Rectangle) S
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// InvalidateAuto invalidates this cache object when it had idled for too long.
|
||||||
|
// The cache will be invalidated after `threshold + 1` calls to InvalidateAuto.
|
||||||
|
func (sic *StitchedImageCache) InvalidateAuto(threshold byte) {
|
||||||
|
sic.Lock()
|
||||||
|
defer sic.Unlock()
|
||||||
|
|
||||||
|
if sic.image != nil {
|
||||||
|
if sic.idleCounter >= threshold {
|
||||||
|
sic.image = nil
|
||||||
|
return
|
||||||
|
}
|
||||||
|
sic.idleCounter++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Invalidate clears the cached image.
|
// Invalidate clears the cached image.
|
||||||
func (sic *StitchedImageCache) Invalidate() {
|
func (sic *StitchedImageCache) Invalidate() {
|
||||||
sic.Lock()
|
sic.Lock()
|
||||||
@ -43,6 +60,8 @@ func (sic *StitchedImageCache) Regenerate() *image.RGBA {
|
|||||||
sic.Lock()
|
sic.Lock()
|
||||||
defer sic.Unlock()
|
defer sic.Unlock()
|
||||||
|
|
||||||
|
sic.idleCounter = 0
|
||||||
|
|
||||||
// Check if there is already a cache image.
|
// Check if there is already a cache image.
|
||||||
if sic.image != nil {
|
if sic.image != nil {
|
||||||
return sic.image
|
return sic.image
|
||||||
@ -114,6 +133,7 @@ func (sic *StitchedImageCache) RGBAAt(x, y int) color.RGBA {
|
|||||||
sic.Lock()
|
sic.Lock()
|
||||||
if sic.image != nil {
|
if sic.image != nil {
|
||||||
defer sic.Unlock()
|
defer sic.Unlock()
|
||||||
|
sic.idleCounter = 0
|
||||||
return sic.image.RGBAAt(x, y)
|
return sic.image.RGBAAt(x, y)
|
||||||
}
|
}
|
||||||
sic.Unlock()
|
sic.Unlock()
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
// Copyright (c) 2022 David Vogel
|
// Copyright (c) 2022-2023 David Vogel
|
||||||
//
|
//
|
||||||
// This software is released under the MIT License.
|
// This software is released under the MIT License.
|
||||||
// https://opensource.org/licenses/MIT
|
// https://opensource.org/licenses/MIT
|
||||||
@ -10,6 +10,7 @@ import (
|
|||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// StitchedImageCacheGridSize defines the worker chunk size when the cache image is regenerated.
|
// StitchedImageCacheGridSize defines the worker chunk size when the cache image is regenerated.
|
||||||
@ -71,6 +72,17 @@ func NewStitchedImage(tiles ImageTiles, bounds image.Rectangle, blendMethod Stit
|
|||||||
stitchedImage.cacheRowYOffset = -bounds.Min.Y
|
stitchedImage.cacheRowYOffset = -bounds.Min.Y
|
||||||
stitchedImage.cacheRows = cacheRows
|
stitchedImage.cacheRows = cacheRows
|
||||||
|
|
||||||
|
// Start ticker to automatically invalidate caches.
|
||||||
|
// Due to this, the stitchedImage object is not composable, as this goroutine will always have a reference.
|
||||||
|
go func() {
|
||||||
|
ticker := time.NewTicker(1 * time.Second)
|
||||||
|
for range ticker.C {
|
||||||
|
for rowIndex := range stitchedImage.cacheRows {
|
||||||
|
stitchedImage.cacheRows[rowIndex].InvalidateAuto(3) // Invalidate cache row after 3 seconds of being idle.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
return stitchedImage, nil
|
return stitchedImage, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user