text: Limit color matrices cache size

This commit is contained in:
Hajime Hoshi 2018-02-12 04:07:59 +09:00
parent 12cef4d27e
commit c4f02ac07c

View File

@ -124,8 +124,13 @@ func fixed26_6ToFloat64(x fixed.Int26_6) float64 {
return float64(x) / (1 << 6)
}
type colorMCacheEntry struct {
m ebiten.ColorM
atime int64
}
var (
colorMCache = map[color.Color]ebiten.ColorM{}
colorMCache = map[color.Color]*colorMCacheEntry{}
)
func (g *glyph) draw(dst *ebiten.Image, x, y fixed.Int26_6, clr color.Color) {
@ -138,17 +143,34 @@ func (g *glyph) draw(dst *ebiten.Image, x, y fixed.Int26_6, clr color.Color) {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(fixed26_6ToFloat64(x+b.Min.X), fixed26_6ToFloat64(y+b.Min.Y))
cm, ok := colorMCache[clr]
if !ok {
e, ok := colorMCache[clr]
if ok {
e.atime = now()
} else {
if len(colorMCache) >= 256 {
var oldest color.Color
t := int64(math.MaxInt64)
for key, e := range colorMCache {
if e.atime < t {
t = g.atime
oldest = key
}
}
delete(colorMCache, oldest)
}
cm := ebiten.ColorM{}
rf := float64(cr) / float64(ca)
gf := float64(cg) / float64(ca)
bf := float64(cb) / float64(ca)
af := float64(ca) / 0xffff
cm.Reset()
cm.Scale(rf, gf, bf, af)
colorMCache[clr] = cm
e = &colorMCacheEntry{
m: cm,
atime: now(),
}
colorMCache[clr] = e
}
op.ColorM = cm
op.ColorM = e.m
a := atlases[g.char.face][g.char.atlasGroup()]
sx, sy := a.at(g)