shareable: Bug fix: wrong size calculation

The size calculation must consider the paddings.

Fixes #1217
This commit is contained in:
Hajime Hoshi 2020-06-27 17:09:23 +09:00
parent 00e8b701c1
commit 7e01ba17c1
3 changed files with 53 additions and 2 deletions

View File

@ -18,6 +18,29 @@ func MakeImagesSharedForTesting() error {
return makeImagesShared() return makeImagesShared()
} }
var (
oldMinSize int
oldMaxSize int
)
func SetImageSizeForTesting(min, max int) {
oldMinSize = min
oldMaxSize = max
minSize = min
maxSize = max
}
func ResetImageSizeForTesting() {
minSize = oldMinSize
maxSize = oldMaxSize
}
func ResetBackendsForTesting() {
backendsM.Lock()
defer backendsM.Unlock()
theBackends = nil
}
func (i *Image) IsSharedForTesting() bool { func (i *Image) IsSharedForTesting() bool {
backendsM.Lock() backendsM.Lock()
defer backendsM.Unlock() defer backendsM.Unlock()

View File

@ -573,7 +573,7 @@ func (i *Image) shareable() bool {
if i.screen { if i.screen {
return false return false
} }
return i.width <= maxSize && i.height <= maxSize return i.width+2*paddingSize <= maxSize && i.height+2*paddingSize <= maxSize
} }
func (i *Image) allocate(shareable bool) { func (i *Image) allocate(shareable bool) {
@ -606,7 +606,7 @@ func (i *Image) allocate(shareable bool) {
} }
} }
size := minSize size := minSize
for i.width > size || i.height > size { for i.width+2*paddingSize > size || i.height+2*paddingSize > size {
if size == maxSize { if size == maxSize {
panic(fmt.Sprintf("shareable: the image being shared is too big: width: %d, height: %d", i.width, i.height)) panic(fmt.Sprintf("shareable: the image being shared is too big: width: %d, height: %d", i.width, i.height))
} }

View File

@ -25,7 +25,14 @@ import (
t "github.com/hajimehoshi/ebiten/internal/testing" t "github.com/hajimehoshi/ebiten/internal/testing"
) )
const (
minImageSizeForTesting = 1024
maxImageSizeForTesting = 4096
)
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
SetImageSizeForTesting(minImageSizeForTesting, maxImageSizeForTesting)
defer ResetImageSizeForTesting()
t.MainWithRunLoop(m) t.MainWithRunLoop(m)
} }
@ -451,4 +458,25 @@ func TestExtendWithBigImage(t *testing.T) {
img1.ReplacePixels(make([]byte, 4*1025*1025)) img1.ReplacePixels(make([]byte, 4*1025*1025))
} }
// Issue #1217
func TestMaxImageSize(t *testing.T) {
// This tests that a too-big image is allocated correctly.
s := maxImageSizeForTesting
img := NewImage(s, s, false)
defer img.MarkDisposed()
img.ReplacePixels(make([]byte, 4*s*s))
}
// Issue #1217
func TestMinImageSize(t *testing.T) {
ResetBackendsForTesting()
// This tests that extending a backend works correctly.
// Though the image size is minimum size of the backend, extending the backend happens due to the paddings.
s := minImageSizeForTesting
img := NewImage(s, s, false)
defer img.MarkDisposed()
img.ReplacePixels(make([]byte, 4*s*s))
}
// TODO: Add tests to extend shareable image out of the main loop // TODO: Add tests to extend shareable image out of the main loop