mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-12 22:17:26 +01:00
Revert 2 commits using ColorM cache
This reverts commit87c8625692
. This reverts commita6dd6196b4
. Reason: Performance regression (#1655) Closes #1655
This commit is contained in:
parent
d05bd7a58c
commit
0cd0921e6f
@ -20,6 +20,7 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2/internal/colormcache"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -31,17 +32,11 @@ func init() {
|
|||||||
emptyImage.Fill(color.White)
|
emptyImage.Fill(color.White)
|
||||||
}
|
}
|
||||||
|
|
||||||
func colorToScale(clr color.Color) (float64, float64, float64, float64) {
|
|
||||||
cr, cg, cb, ca := clr.RGBA()
|
|
||||||
if ca == 0 {
|
|
||||||
return 0, 0, 0, 0
|
|
||||||
}
|
|
||||||
return float64(cr) / float64(ca), float64(cg) / float64(ca), float64(cb) / float64(ca), float64(ca) / 0xffff
|
|
||||||
}
|
|
||||||
|
|
||||||
// DrawLine draws a line segment on the given destination dst.
|
// DrawLine draws a line segment on the given destination dst.
|
||||||
//
|
//
|
||||||
// DrawLine is intended to be used mainly for debugging or prototyping purpose.
|
// DrawLine is intended to be used mainly for debugging or prototyping purpose.
|
||||||
|
//
|
||||||
|
// DrawLine is not concurrent-safe.
|
||||||
func DrawLine(dst *ebiten.Image, x1, y1, x2, y2 float64, clr color.Color) {
|
func DrawLine(dst *ebiten.Image, x1, y1, x2, y2 float64, clr color.Color) {
|
||||||
length := math.Hypot(x2-x1, y2-y1)
|
length := math.Hypot(x2-x1, y2-y1)
|
||||||
|
|
||||||
@ -49,7 +44,7 @@ func DrawLine(dst *ebiten.Image, x1, y1, x2, y2 float64, clr color.Color) {
|
|||||||
op.GeoM.Scale(length, 1)
|
op.GeoM.Scale(length, 1)
|
||||||
op.GeoM.Rotate(math.Atan2(y2-y1, x2-x1))
|
op.GeoM.Rotate(math.Atan2(y2-y1, x2-x1))
|
||||||
op.GeoM.Translate(x1, y1)
|
op.GeoM.Translate(x1, y1)
|
||||||
op.ColorM.Scale(colorToScale(clr))
|
op.ColorM = colormcache.ColorToColorM(clr)
|
||||||
// Filter must be 'nearest' filter (default).
|
// Filter must be 'nearest' filter (default).
|
||||||
// Linear filtering would make edges blurred.
|
// Linear filtering would make edges blurred.
|
||||||
dst.DrawImage(emptySubImage, op)
|
dst.DrawImage(emptySubImage, op)
|
||||||
@ -58,11 +53,13 @@ func DrawLine(dst *ebiten.Image, x1, y1, x2, y2 float64, clr color.Color) {
|
|||||||
// DrawRect draws a rectangle on the given destination dst.
|
// DrawRect draws a rectangle on the given destination dst.
|
||||||
//
|
//
|
||||||
// DrawRect is intended to be used mainly for debugging or prototyping purpose.
|
// DrawRect is intended to be used mainly for debugging or prototyping purpose.
|
||||||
|
//
|
||||||
|
// DrawRect is not concurrent-safe.
|
||||||
func DrawRect(dst *ebiten.Image, x, y, width, height float64, clr color.Color) {
|
func DrawRect(dst *ebiten.Image, x, y, width, height float64, clr color.Color) {
|
||||||
op := &ebiten.DrawImageOptions{}
|
op := &ebiten.DrawImageOptions{}
|
||||||
op.GeoM.Scale(width, height)
|
op.GeoM.Scale(width, height)
|
||||||
op.GeoM.Translate(x, y)
|
op.GeoM.Translate(x, y)
|
||||||
op.ColorM.Scale(colorToScale(clr))
|
op.ColorM = colormcache.ColorToColorM(clr)
|
||||||
// Filter must be 'nearest' filter (default).
|
// Filter must be 'nearest' filter (default).
|
||||||
// Linear filtering would make edges blurred.
|
// Linear filtering would make edges blurred.
|
||||||
dst.DrawImage(emptyImage.SubImage(image.Rect(1, 1, 2, 2)).(*ebiten.Image), op)
|
dst.DrawImage(emptyImage.SubImage(image.Rect(1, 1, 2, 2)).(*ebiten.Image), op)
|
||||||
|
@ -17,7 +17,6 @@ package affine
|
|||||||
import (
|
import (
|
||||||
"image/color"
|
"image/color"
|
||||||
"math"
|
"math"
|
||||||
"sync"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ColorMDim is a dimension of a ColorM.
|
// ColorMDim is a dimension of a ColorM.
|
||||||
@ -442,16 +441,15 @@ func (c *ColorM) Concat(other *ColorM) *ColorM {
|
|||||||
// Scale scales the matrix by (r, g, b, a).
|
// Scale scales the matrix by (r, g, b, a).
|
||||||
func (c *ColorM) Scale(r, g, b, a float32) *ColorM {
|
func (c *ColorM) Scale(r, g, b, a float32) *ColorM {
|
||||||
if !c.isInited() {
|
if !c.isInited() {
|
||||||
return getCachedScalingColorM(r, g, b, a)
|
return &ColorM{
|
||||||
}
|
body: []float32{
|
||||||
|
r, 0, 0, 0,
|
||||||
if c.ScaleOnly() {
|
0, g, 0, 0,
|
||||||
if c.body == nil {
|
0, 0, b, 0,
|
||||||
return getCachedScalingColorM(r, g, b, a)
|
0, 0, 0, a,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
return getCachedScalingColorM(r*c.body[0], g*c.body[5], b*c.body[10], a*c.body[15])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
eb := make([]float32, len(colorMIdentityBody))
|
eb := make([]float32, len(colorMIdentityBody))
|
||||||
if c.body != nil {
|
if c.body != nil {
|
||||||
copy(eb, c.body)
|
copy(eb, c.body)
|
||||||
@ -551,62 +549,3 @@ func (c *ColorM) ChangeHSV(hueTheta float64, saturationScale float32, valueScale
|
|||||||
c = c.Concat(yCbCrToRgb)
|
c = c.Concat(yCbCrToRgb)
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
type cachedScalingColorMKey struct {
|
|
||||||
r, g, b, a float32
|
|
||||||
}
|
|
||||||
|
|
||||||
type cachedScalingColorMValue struct {
|
|
||||||
c *ColorM
|
|
||||||
atime uint64
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
cachedScalingColorM = map[cachedScalingColorMKey]*cachedScalingColorMValue{}
|
|
||||||
cachedScalingColorMM sync.Mutex
|
|
||||||
cacheMonotonicClock uint64
|
|
||||||
)
|
|
||||||
|
|
||||||
func getCachedScalingColorM(r, g, b, a float32) *ColorM {
|
|
||||||
key := cachedScalingColorMKey{r, g, b, a}
|
|
||||||
|
|
||||||
cachedScalingColorMM.Lock()
|
|
||||||
defer cachedScalingColorMM.Unlock()
|
|
||||||
|
|
||||||
cacheMonotonicClock++
|
|
||||||
now := cacheMonotonicClock
|
|
||||||
|
|
||||||
if v, ok := cachedScalingColorM[key]; ok {
|
|
||||||
v.atime = now
|
|
||||||
return v.c
|
|
||||||
}
|
|
||||||
|
|
||||||
const maxCacheSize = 512 // An arbitrary number
|
|
||||||
|
|
||||||
for len(cachedScalingColorM) >= maxCacheSize {
|
|
||||||
var oldest uint64 = math.MaxUint64
|
|
||||||
var oldestKey cachedScalingColorMKey
|
|
||||||
for k, v := range cachedScalingColorM {
|
|
||||||
if v.atime < oldest {
|
|
||||||
oldestKey = k
|
|
||||||
oldest = v.atime
|
|
||||||
}
|
|
||||||
}
|
|
||||||
delete(cachedScalingColorM, oldestKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
v := &cachedScalingColorMValue{
|
|
||||||
c: &ColorM{
|
|
||||||
body: []float32{
|
|
||||||
r, 0, 0, 0,
|
|
||||||
0, g, 0, 0,
|
|
||||||
0, 0, b, 0,
|
|
||||||
0, 0, 0, a,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
atime: now,
|
|
||||||
}
|
|
||||||
cachedScalingColorM[key] = v
|
|
||||||
|
|
||||||
return v.c
|
|
||||||
}
|
|
||||||
|
@ -22,33 +22,6 @@ import (
|
|||||||
. "github.com/hajimehoshi/ebiten/v2/internal/affine"
|
. "github.com/hajimehoshi/ebiten/v2/internal/affine"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestColorMScale(t *testing.T) {
|
|
||||||
cases := []struct {
|
|
||||||
In *ColorM
|
|
||||||
Out *ColorM
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
nil,
|
|
||||||
(*ColorM)(nil).Scale(0.25, 0.5, 0.75, 1),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
(*ColorM)(nil).Scale(0.5, 0.5, 0.5, 0.8),
|
|
||||||
(*ColorM)(nil).Scale(0.125, 0.25, 0.375, 0.8),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
(*ColorM)(nil).Translate(0, 0, 0, 0),
|
|
||||||
(*ColorM)(nil).Scale(0.25, 0.5, 0.75, 1),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, c := range cases {
|
|
||||||
got := c.In.Scale(0.25, 0.5, 0.75, 1)
|
|
||||||
want := c.Out
|
|
||||||
if got != want {
|
|
||||||
t.Errorf("%v.Scale(): got: %v, want: %v", c.In, got, want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestColorMScaleOnly(t *testing.T) {
|
func TestColorMScaleOnly(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
In *ColorM
|
In *ColorM
|
||||||
|
96
internal/colormcache/cache.go
Normal file
96
internal/colormcache/cache.go
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
// Copyright 2020 The Ebiten Authors
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package colormcache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image/color"
|
||||||
|
"math"
|
||||||
|
|
||||||
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
monotonicClock int64
|
||||||
|
)
|
||||||
|
|
||||||
|
func now() int64 {
|
||||||
|
monotonicClock++
|
||||||
|
return monotonicClock
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
cacheLimit = 512 // This is an arbitrary number.
|
||||||
|
)
|
||||||
|
|
||||||
|
type colorMCacheKey uint32
|
||||||
|
|
||||||
|
type colorMCacheEntry struct {
|
||||||
|
m ebiten.ColorM
|
||||||
|
atime int64
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
colorMCache = map[colorMCacheKey]*colorMCacheEntry{}
|
||||||
|
emptyColorM ebiten.ColorM
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
emptyColorM.Scale(0, 0, 0, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ColorToColorM(clr color.Color) ebiten.ColorM {
|
||||||
|
// RGBA() is in [0 - 0xffff]. Adjust them in [0 - 0xff].
|
||||||
|
cr, cg, cb, ca := clr.RGBA()
|
||||||
|
cr /= 0x101
|
||||||
|
cg /= 0x101
|
||||||
|
cb /= 0x101
|
||||||
|
ca /= 0x101
|
||||||
|
if ca == 0 {
|
||||||
|
return emptyColorM
|
||||||
|
}
|
||||||
|
|
||||||
|
key := colorMCacheKey(uint32(cr) | (uint32(cg) << 8) | (uint32(cb) << 16) | (uint32(ca) << 24))
|
||||||
|
e, ok := colorMCache[key]
|
||||||
|
if ok {
|
||||||
|
e.atime = now()
|
||||||
|
return e.m
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(colorMCache) > cacheLimit {
|
||||||
|
oldest := int64(math.MaxInt64)
|
||||||
|
oldestKey := colorMCacheKey(0)
|
||||||
|
for key, c := range colorMCache {
|
||||||
|
if c.atime < oldest {
|
||||||
|
oldestKey = key
|
||||||
|
oldest = c.atime
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delete(colorMCache, oldestKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
cm := ebiten.ColorM{}
|
||||||
|
rf := float64(cr) / float64(ca)
|
||||||
|
gf := float64(cg) / float64(ca)
|
||||||
|
bf := float64(cb) / float64(ca)
|
||||||
|
af := float64(ca) / 0xff
|
||||||
|
cm.Scale(rf, gf, bf, af)
|
||||||
|
e = &colorMCacheEntry{
|
||||||
|
m: cm,
|
||||||
|
atime: now(),
|
||||||
|
}
|
||||||
|
colorMCache[key] = e
|
||||||
|
|
||||||
|
return e.m
|
||||||
|
}
|
11
text/text.go
11
text/text.go
@ -27,6 +27,7 @@ import (
|
|||||||
"golang.org/x/image/math/fixed"
|
"golang.org/x/image/math/fixed"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2/internal/colormcache"
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/hooks"
|
"github.com/hajimehoshi/ebiten/v2/internal/hooks"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -176,19 +177,13 @@ func Draw(dst *ebiten.Image, text string, face font.Face, x, y int, clr color.Co
|
|||||||
textM.Lock()
|
textM.Lock()
|
||||||
defer textM.Unlock()
|
defer textM.Unlock()
|
||||||
|
|
||||||
cr, cg, cb, ca := clr.RGBA()
|
|
||||||
if ca == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var colorm ebiten.ColorM
|
|
||||||
colorm.Scale(float64(cr)/float64(ca), float64(cg)/float64(ca), float64(cb)/float64(ca), float64(ca)/0xffff)
|
|
||||||
|
|
||||||
fx, fy := fixed.I(x), fixed.I(y)
|
fx, fy := fixed.I(x), fixed.I(y)
|
||||||
prevR := rune(-1)
|
prevR := rune(-1)
|
||||||
|
|
||||||
faceHeight := face.Metrics().Height
|
faceHeight := face.Metrics().Height
|
||||||
|
|
||||||
|
colorm := colormcache.ColorToColorM(clr)
|
||||||
|
|
||||||
for _, r := range text {
|
for _, r := range text {
|
||||||
if prevR >= 0 {
|
if prevR >= 0 {
|
||||||
fx += face.Kern(prevR, r)
|
fx += face.Kern(prevR, r)
|
||||||
|
Loading…
Reference in New Issue
Block a user