internal/atlas: backends for unmanaged images were never released

Closes #3030
This commit is contained in:
Hajime Hoshi 2024-06-30 22:51:05 +09:00
parent 3f91d8cdf1
commit 2a22260d14
3 changed files with 36 additions and 12 deletions

View File

@ -69,3 +69,9 @@ func DeferredFuncCountForTesting() int {
defer deferredM.Unlock() defer deferredM.Unlock()
return len(deferred) return len(deferred)
} }
func BackendCountForTesting() int {
backendsM.Lock()
defer backendsM.Unlock()
return len(theBackends)
}

View File

@ -689,18 +689,14 @@ func (i *Image) deallocate() {
return return
} }
if !i.isOnAtlas() { if i.isOnAtlas() {
i.backend.image.Dispose() i.backend.page.Free(i.node)
i.backend.image = nil if !i.backend.page.IsEmpty() {
return // As this part can be reused, this should be cleared explicitly.
} r := i.regionWithPadding()
i.backend.clearPixels(r)
i.backend.page.Free(i.node) return
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() i.backend.image.Dispose()

View File

@ -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 // TODO: Add tests to extend image on an atlas out of the main loop