Skip to content

Commit

Permalink
shareable: Bug fix: wrong size calculation
Browse files Browse the repository at this point in the history
The size calculation must consider the paddings.

Fixes hajimehoshi#1217
  • Loading branch information
hajimehoshi committed Jun 27, 2020
1 parent 00e8b70 commit 7e01ba1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
23 changes: 23 additions & 0 deletions internal/shareable/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,29 @@ func MakeImagesSharedForTesting() error {
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 {
backendsM.Lock()
defer backendsM.Unlock()
Expand Down
4 changes: 2 additions & 2 deletions internal/shareable/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ func (i *Image) shareable() bool {
if i.screen {
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) {
Expand Down Expand Up @@ -606,7 +606,7 @@ func (i *Image) allocate(shareable bool) {
}
}
size := minSize
for i.width > size || i.height > size {
for i.width+2*paddingSize > size || i.height+2*paddingSize > size {
if size == maxSize {
panic(fmt.Sprintf("shareable: the image being shared is too big: width: %d, height: %d", i.width, i.height))
}
Expand Down
28 changes: 28 additions & 0 deletions internal/shareable/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ import (
t "github.com/hajimehoshi/ebiten/internal/testing"
)

const (
minImageSizeForTesting = 1024
maxImageSizeForTesting = 4096
)

func TestMain(m *testing.M) {
SetImageSizeForTesting(minImageSizeForTesting, maxImageSizeForTesting)
defer ResetImageSizeForTesting()
t.MainWithRunLoop(m)
}

Expand Down Expand Up @@ -451,4 +458,25 @@ func TestExtendWithBigImage(t *testing.T) {
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

0 comments on commit 7e01ba1

Please sign in to comment.