From 03567f74f9c5b4aa3e93549a7e07dd763e340912 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 10 Jun 2022 14:25:08 +0900 Subject: [PATCH] internal/atlas: bug fix: respect injected maxSize for testings --- internal/atlas/image.go | 14 ++++++++++++-- internal/atlas/image_test.go | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/internal/atlas/image.go b/internal/atlas/image.go index 4a5fcc723..f79ea44af 100644 --- a/internal/atlas/image.go +++ b/internal/atlas/image.go @@ -745,6 +745,10 @@ func (i *Image) allocate(putOnAtlas bool) { } if !putOnAtlas || !i.canBePutOnAtlas() { + if i.width+2*i.paddingSize() > maxSize || i.height+2*i.paddingSize() > maxSize { + panic(fmt.Sprintf("atlas: the image being put on an atlas is too big: width: %d, height: %d", i.width, i.height)) + } + typ := restorable.ImageTypeRegular if i.imageType == ImageTypeVolatile { typ = restorable.ImageTypeVolatile @@ -815,8 +819,14 @@ func BeginFrame(graphicsDriver graphicsdriver.Graphics) error { if len(theBackends) != 0 { panic("atlas: all the images must be not on an atlas before the game starts") } - minSize = 1024 - maxSize = restorable.MaxImageSize(graphicsDriver) + + // minSize and maxSize can already be set for testings. + if minSize == 0 { + minSize = 1024 + } + if maxSize == 0 { + maxSize = restorable.MaxImageSize(graphicsDriver) + } }) if err != nil { return err diff --git a/internal/atlas/image_test.go b/internal/atlas/image_test.go index cccf04874..65514a244 100644 --- a/internal/atlas/image_test.go +++ b/internal/atlas/image_test.go @@ -561,6 +561,21 @@ func Disable_TestMinImageSize(t *testing.T) { img.ReplacePixels(make([]byte, 4*s*s), nil) } +func TestMaxImageSizeExceeded(t *testing.T) { + // This tests that a too-big image is allocated correctly. + s := maxImageSizeForTesting + img := atlas.NewImage(s+1, s, atlas.ImageTypeRegular) + defer img.MarkDisposed() + + defer func() { + if err := recover(); err == nil { + t.Errorf("ReplacePixels must panic but not") + } + }() + + img.ReplacePixels(make([]byte, 4*(s+1)*s), nil) +} + // Issue #1421 func TestDisposedAndReputOnAtlas(t *testing.T) { const size = 16