mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
Revert "Revert 2 commits using ColorM cache"
This reverts commit 0cd0921e6f
.
Reason: DrawTriangles worked fine for the reporter's purpose.
Updates #1655
This commit is contained in:
parent
4457dc1c95
commit
d06e9c140c
@ -20,7 +20,6 @@ import (
|
||||
"math"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/colormcache"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -32,11 +31,17 @@ func init() {
|
||||
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 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) {
|
||||
length := math.Hypot(x2-x1, y2-y1)
|
||||
|
||||
@ -44,7 +49,7 @@ func DrawLine(dst *ebiten.Image, x1, y1, x2, y2 float64, clr color.Color) {
|
||||
op.GeoM.Scale(length, 1)
|
||||
op.GeoM.Rotate(math.Atan2(y2-y1, x2-x1))
|
||||
op.GeoM.Translate(x1, y1)
|
||||
op.ColorM = colormcache.ColorToColorM(clr)
|
||||
op.ColorM.Scale(colorToScale(clr))
|
||||
// Filter must be 'nearest' filter (default).
|
||||
// Linear filtering would make edges blurred.
|
||||
dst.DrawImage(emptySubImage, op)
|
||||
@ -53,13 +58,11 @@ func DrawLine(dst *ebiten.Image, x1, y1, x2, y2 float64, clr color.Color) {
|
||||
// DrawRect draws a rectangle on the given destination dst.
|
||||
//
|
||||
// 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) {
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
op.GeoM.Scale(width, height)
|
||||
op.GeoM.Translate(x, y)
|
||||
op.ColorM = colormcache.ColorToColorM(clr)
|
||||
op.ColorM.Scale(colorToScale(clr))
|
||||
// Filter must be 'nearest' filter (default).
|
||||
// Linear filtering would make edges blurred.
|
||||
dst.DrawImage(emptyImage.SubImage(image.Rect(1, 1, 2, 2)).(*ebiten.Image), op)
|
||||
|
@ -17,6 +17,7 @@ package affine
|
||||
import (
|
||||
"image/color"
|
||||
"math"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// ColorMDim is a dimension of a ColorM.
|
||||
@ -441,15 +442,16 @@ func (c *ColorM) Concat(other *ColorM) *ColorM {
|
||||
// Scale scales the matrix by (r, g, b, a).
|
||||
func (c *ColorM) Scale(r, g, b, a float32) *ColorM {
|
||||
if !c.isInited() {
|
||||
return &ColorM{
|
||||
body: []float32{
|
||||
r, 0, 0, 0,
|
||||
0, g, 0, 0,
|
||||
0, 0, b, 0,
|
||||
0, 0, 0, a,
|
||||
},
|
||||
}
|
||||
return getCachedScalingColorM(r, g, b, a)
|
||||
}
|
||||
|
||||
if c.ScaleOnly() {
|
||||
if c.body == nil {
|
||||
return getCachedScalingColorM(r, g, b, a)
|
||||
}
|
||||
return getCachedScalingColorM(r*c.body[0], g*c.body[5], b*c.body[10], a*c.body[15])
|
||||
}
|
||||
|
||||
eb := make([]float32, len(colorMIdentityBody))
|
||||
if c.body != nil {
|
||||
copy(eb, c.body)
|
||||
@ -549,3 +551,62 @@ func (c *ColorM) ChangeHSV(hueTheta float64, saturationScale float32, valueScale
|
||||
c = c.Concat(yCbCrToRgb)
|
||||
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,6 +22,33 @@ import (
|
||||
. "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) {
|
||||
cases := []struct {
|
||||
In *ColorM
|
||||
|
@ -1,96 +0,0 @@
|
||||
// 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,7 +27,6 @@ import (
|
||||
"golang.org/x/image/math/fixed"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/colormcache"
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/hooks"
|
||||
)
|
||||
|
||||
@ -177,13 +176,19 @@ func Draw(dst *ebiten.Image, text string, face font.Face, x, y int, clr color.Co
|
||||
textM.Lock()
|
||||
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)
|
||||
prevR := rune(-1)
|
||||
|
||||
faceHeight := face.Metrics().Height
|
||||
|
||||
colorm := colormcache.ColorToColorM(clr)
|
||||
|
||||
for _, r := range text {
|
||||
if prevR >= 0 {
|
||||
fx += face.Kern(prevR, r)
|
||||
|
Loading…
Reference in New Issue
Block a user