Skip to content

Commit

Permalink
internal/atlas: backends for unmanaged images were never released
Browse files Browse the repository at this point in the history
Closes #3030
  • Loading branch information
hajimehoshi committed Jun 30, 2024
1 parent f6827de commit 001ac9a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
6 changes: 6 additions & 0 deletions internal/atlas/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,9 @@ func DeferredFuncCountForTesting() int {
defer deferredM.Unlock()
return len(deferred)
}

func BackendCountForTesting() int {
backendsM.Lock()
defer backendsM.Unlock()
return len(theBackends)
}
20 changes: 8 additions & 12 deletions internal/atlas/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,18 +686,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()
Expand Down
22 changes: 22 additions & 0 deletions internal/atlas/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 001ac9a

Please sign in to comment.