Skip to content

Commit

Permalink
Merge pull request #1479 from andydotxyz/fix/1476
Browse files Browse the repository at this point in the history
Index svg cache by resource/file name not the resource struct
  • Loading branch information
andydotxyz committed Oct 29, 2020
2 parents 53d82cc + bf6acbe commit 5ffc72c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
4 changes: 2 additions & 2 deletions internal/painter/image.go
Expand Up @@ -41,7 +41,7 @@ func PaintImage(img *canvas.Image, c fyne.Canvas, width, height int) image.Image
}

if strings.ToLower(filepath.Ext(name)) == ".svg" {
tex := svgCacheGet(img.Resource, width, height)
tex := svgCacheGet(name, width, height)
if tex == nil {
// Not in cache, so load the item and add to cache

Expand Down Expand Up @@ -82,7 +82,7 @@ func PaintImage(img *canvas.Image, c fyne.Canvas, width, height int) image.Image
return nil
}

svgCachePut(img.Resource, tex, width, height)
svgCachePut(name, tex, width, height)
}

return tex
Expand Down
12 changes: 6 additions & 6 deletions internal/painter/svg_cache.go
Expand Up @@ -17,7 +17,7 @@ type rasterInfo struct {
}

var cacheDuration = time.Minute * 5
var rasters = make(map[fyne.Resource]*rasterInfo)
var rasters = make(map[string]*rasterInfo)
var aspects = make(map[interface{}]float32, 16)
var rasterMutex sync.RWMutex
var janitorOnce sync.Once
Expand Down Expand Up @@ -61,25 +61,25 @@ func svgCacheJanitor() {
})
}

func svgCacheGet(res fyne.Resource, w int, h int) *image.NRGBA {
func svgCacheGet(name string, w int, h int) *image.NRGBA {
rasterMutex.RLock()
defer rasterMutex.RUnlock()
v, ok := rasters[res]
v, ok := rasters[name]
if !ok || v == nil || v.w != w || v.h != h {
return nil
}
v.expires = time.Now().Add(cacheDuration)
return v.pix
}

func svgCachePut(res fyne.Resource, pix *image.NRGBA, w int, h int) {
func svgCachePut(name string, pix *image.NRGBA, w int, h int) {
rasterMutex.Lock()
defer rasterMutex.Unlock()
defer func() {
recover()
}()

rasters[res] = &rasterInfo{
rasters[name] = &rasterInfo{
pix: pix,
w: w,
h: h,
Expand All @@ -90,7 +90,7 @@ func svgCachePut(res fyne.Resource, pix *image.NRGBA, w int, h int) {
// SvgCacheReset clears the SVG cache.
func SvgCacheReset() {
rasterMutex.Lock()
rasters = make(map[fyne.Resource]*rasterInfo)
rasters = make(map[string]*rasterInfo)
rasterMutex.Unlock()
}

Expand Down
58 changes: 58 additions & 0 deletions internal/painter/svg_cache_test.go
@@ -0,0 +1,58 @@
package painter

import (
"image"
"testing"

"github.com/stretchr/testify/assert"

"fyne.io/fyne"
"fyne.io/fyne/canvas"
)

func TestSvgCacheGet(t *testing.T) {
SvgCacheReset()
img := addToCache("empty.svg", "<svg xmlns=\"http://www.w3.org/2000/svg\"/>", 25, 25)
assert.Equal(t, 1, len(rasters))

newImg := svgCacheGet("empty.svg", 25, 25)
assert.Equal(t, img, newImg)

miss := svgCacheGet("missing.svg", 25, 25)
assert.Nil(t, miss)
miss = svgCacheGet("empty.svg", 30, 30)
assert.Nil(t, miss)
}

func TestSvgCacheGet_File(t *testing.T) {
SvgCacheReset()
img := addFileToCache("testdata/stroke.svg", 25, 25)
assert.Equal(t, 1, len(rasters))

newImg := svgCacheGet("testdata/stroke.svg", 25, 25)
assert.Equal(t, img, newImg)

miss := svgCacheGet("missing.svg", 25, 25)
assert.Nil(t, miss)
miss = svgCacheGet("testdata/stroke.svg", 30, 30)
assert.Nil(t, miss)
}

func TestSvgCacheReset(t *testing.T) {
SvgCacheReset()
_ = addToCache("empty.svg", "<svg xmlns=\"http://www.w3.org/2000/svg\"/>", 25, 25)
assert.Equal(t, 1, len(rasters))

SvgCacheReset()
assert.Equal(t, 0, len(rasters))
}

func addFileToCache(path string, w, h int) image.Image {
img := canvas.NewImageFromFile(path)
return PaintImage(img, nil, w, h)
}

func addToCache(name, content string, w, h int) image.Image {
img := canvas.NewImageFromResource(fyne.NewStaticResource(name, []byte(content)))
return PaintImage(img, nil, w, h)
}

0 comments on commit 5ffc72c

Please sign in to comment.