diff --git a/internal/atlas/export_test.go b/internal/atlas/export_test.go index eb8af636c..648122fc8 100644 --- a/internal/atlas/export_test.go +++ b/internal/atlas/export_test.go @@ -69,3 +69,9 @@ func DeferredFuncCountForTesting() int { defer deferredM.Unlock() return len(deferred) } + +func BackendCountForTesting() int { + backendsM.Lock() + defer backendsM.Unlock() + return len(theBackends) +} diff --git a/internal/atlas/image.go b/internal/atlas/image.go index af6d983d9..e0040515f 100644 --- a/internal/atlas/image.go +++ b/internal/atlas/image.go @@ -689,18 +689,14 @@ func (i *Image) deallocate() { return } - if !i.isOnAtlas() { - i.backend.image.Dispose() - i.backend.image = nil - return - } - - i.backend.page.Free(i.node) - if !i.backend.page.IsEmpty() { - // As this part can be reused, this should be cleared explicitly. - r := i.regionWithPadding() - i.backend.clearPixels(r) - return + if i.isOnAtlas() { + i.backend.page.Free(i.node) + if !i.backend.page.IsEmpty() { + // As this part can be reused, this should be cleared explicitly. + r := i.regionWithPadding() + i.backend.clearPixels(r) + return + } } i.backend.image.Dispose() diff --git a/internal/atlas/image_test.go b/internal/atlas/image_test.go index ea3d44558..51d829a38 100644 --- a/internal/atlas/image_test.go +++ b/internal/atlas/image_test.go @@ -881,4 +881,26 @@ func TestGC(t *testing.T) { } } +func TestDallocateUnmanagedImageBackends(t *testing.T) { + const w, h = 16, 16 + img0 := atlas.NewImage(w, h, atlas.ImageTypeUnmanaged) + img1 := atlas.NewImage(w, h, atlas.ImageTypeUnmanaged) + + // Call DrawTriangles to ensure the images are on backends. + vs := quadVertices(w, h, 0, 0, 1) + is := graphics.QuadIndices() + dr := image.Rect(0, 0, w, h) + img0.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{img1}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillRuleFillAll) + + // Get the difference of the number of backends before and after the images are deallocated. + c := atlas.BackendCountForTesting() + img0.Deallocate() + img1.Deallocate() + + diff := c - atlas.BackendCountForTesting() + if got, want := diff, 2; got != want { + t.Errorf("got: %d, want: %d", got, want) + } +} + // TODO: Add tests to extend image on an atlas out of the main loop