restorable: Refactoring

This commit is contained in:
Hajime Hoshi 2017-05-03 23:24:00 +09:00
parent bc105e7350
commit 8b48d010ed
3 changed files with 9 additions and 7 deletions

View File

@ -167,7 +167,7 @@ func (i *Image) Dispose() error {
if i.restorable == nil { if i.restorable == nil {
return nil return nil
} }
restorable.Images().Remove(i.restorable) i.restorable.Dispose()
i.restorable = nil i.restorable = nil
runtime.SetFinalizer(i, nil) runtime.SetFinalizer(i, nil)
return nil return nil

View File

@ -18,6 +18,7 @@ import (
"errors" "errors"
"image" "image"
"image/color" "image/color"
"runtime"
"github.com/hajimehoshi/ebiten/internal/affine" "github.com/hajimehoshi/ebiten/internal/affine"
"github.com/hajimehoshi/ebiten/internal/graphics" "github.com/hajimehoshi/ebiten/internal/graphics"
@ -55,6 +56,7 @@ func NewImage(width, height int, filter opengl.Filter, volatile bool) *Image {
volatile: volatile, volatile: volatile,
} }
theImages.add(i) theImages.add(i)
runtime.SetFinalizer(i, (*Image).Dispose)
return i return i
} }
@ -70,6 +72,7 @@ func NewImageFromImage(source *image.RGBA, width, height int, filter opengl.Filt
filter: filter, filter: filter,
} }
theImages.add(i) theImages.add(i)
runtime.SetFinalizer(i, (*Image).Dispose)
return i return i
} }
@ -80,6 +83,7 @@ func NewScreenFramebufferImage(width, height int) *Image {
screen: true, screen: true,
} }
theImages.add(i) theImages.add(i)
runtime.SetFinalizer(i, (*Image).Dispose)
return i return i
} }
@ -275,7 +279,7 @@ func (p *Image) restore(context *opengl.Context) error {
return nil return nil
} }
func (p *Image) dispose() { func (p *Image) Dispose() {
theImages.resetPixelsIfDependingOn(p) theImages.resetPixelsIfDependingOn(p)
p.image.Dispose() p.image.Dispose()
p.image = nil p.image = nil
@ -283,6 +287,8 @@ func (p *Image) dispose() {
p.baseColor = color.RGBA{} p.baseColor = color.RGBA{}
p.drawImageHistory = nil p.drawImageHistory = nil
p.stale = false p.stale = false
theImages.remove(p)
runtime.SetFinalizer(p, nil)
} }
func (p *Image) IsInvalidated(context *opengl.Context) bool { func (p *Image) IsInvalidated(context *opengl.Context) bool {

View File

@ -15,7 +15,6 @@
package restorable package restorable
import ( import (
"runtime"
"sync" "sync"
"github.com/hajimehoshi/ebiten/internal/opengl" "github.com/hajimehoshi/ebiten/internal/opengl"
@ -39,15 +38,12 @@ func (i *images) add(img *Image) {
i.m.Lock() i.m.Lock()
defer i.m.Unlock() defer i.m.Unlock()
i.images[img] = struct{}{} i.images[img] = struct{}{}
runtime.SetFinalizer(img, theImages.Remove)
} }
func (i *images) Remove(img *Image) { func (i *images) remove(img *Image) {
img.dispose()
i.m.Lock() i.m.Lock()
defer i.m.Unlock() defer i.m.Unlock()
delete(i.images, img) delete(i.images, img)
runtime.SetFinalizer(img, nil)
} }
func (i *images) ResolveStalePixels(context *opengl.Context) error { func (i *images) ResolveStalePixels(context *opengl.Context) error {