mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-27 03:02:49 +01:00
shareable: Bug fix: Defer Dispose so that ClearPixels doesn't affect other images later
Updates #913
This commit is contained in:
parent
613b9bc02a
commit
eb5ab57cdc
@ -50,6 +50,7 @@ func init() {
|
|||||||
hooks.AppendHookOnBeforeUpdate(func() error {
|
hooks.AppendHookOnBeforeUpdate(func() error {
|
||||||
backendsM.Lock()
|
backendsM.Lock()
|
||||||
defer backendsM.Unlock()
|
defer backendsM.Unlock()
|
||||||
|
|
||||||
once.Do(func() {
|
once.Do(func() {
|
||||||
if len(theBackends) != 0 {
|
if len(theBackends) != 0 {
|
||||||
panic("shareable: all the images must be not-shared before the game starts")
|
panic("shareable: all the images must be not-shared before the game starts")
|
||||||
@ -64,11 +65,23 @@ func init() {
|
|||||||
maxSize = 512
|
maxSize = 512
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
resolveDeferred()
|
||||||
makeImagesShared()
|
makeImagesShared()
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func resolveDeferred() {
|
||||||
|
deferredM.Lock()
|
||||||
|
defer deferredM.Unlock()
|
||||||
|
|
||||||
|
for _, f := range deferred {
|
||||||
|
f()
|
||||||
|
}
|
||||||
|
deferred = nil
|
||||||
|
}
|
||||||
|
|
||||||
// MaxCountForShare represents the time duration when the image can become shared.
|
// MaxCountForShare represents the time duration when the image can become shared.
|
||||||
//
|
//
|
||||||
// This value is expoted for testing.
|
// This value is expoted for testing.
|
||||||
@ -137,6 +150,9 @@ var (
|
|||||||
theBackends = []*backend{}
|
theBackends = []*backend{}
|
||||||
|
|
||||||
imagesToMakeShared = map[*Image]struct{}{}
|
imagesToMakeShared = map[*Image]struct{}{}
|
||||||
|
|
||||||
|
deferred []func()
|
||||||
|
deferredM sync.Mutex
|
||||||
)
|
)
|
||||||
|
|
||||||
// isShareable reports whether the new allocation can use the shareable backends.
|
// isShareable reports whether the new allocation can use the shareable backends.
|
||||||
@ -367,9 +383,17 @@ func (i *Image) at(x, y int) (byte, byte, byte, byte) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *Image) Dispose() {
|
func (i *Image) Dispose() {
|
||||||
backendsM.Lock()
|
deferredM.Lock()
|
||||||
defer backendsM.Unlock()
|
defer deferredM.Unlock()
|
||||||
i.dispose(true)
|
|
||||||
|
// Defer actual disposing until disposing can finish completely.
|
||||||
|
// Otherwise, ClearPixels can be deferred in between frames, and delayed ClearPixels can affect other images.
|
||||||
|
// (#913)
|
||||||
|
//
|
||||||
|
// TODO: Other operations should be deferred too?
|
||||||
|
deferred = append(deferred, func() {
|
||||||
|
i.dispose(true)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Image) dispose(markDisposed bool) {
|
func (i *Image) dispose(markDisposed bool) {
|
||||||
|
Loading…
Reference in New Issue
Block a user