restoring: Refactoring: Add Image.volatile

This commit is contained in:
Hajime Hoshi 2016-09-04 02:06:54 +09:00
parent 43e8349567
commit d386f57ac6
2 changed files with 30 additions and 36 deletions

View File

@ -30,19 +30,17 @@ import (
type imageImpl struct { type imageImpl struct {
disposed bool disposed bool
restorable *restorable.Image restorable *restorable.Image
volatile bool
screen bool screen bool
m sync.Mutex m sync.Mutex
} }
func newImageImpl(width, height int, filter Filter, volatile bool) (*imageImpl, error) { func newImageImpl(width, height int, filter Filter, volatile bool) (*imageImpl, error) {
img, err := restorable.NewImage(width, height, glFilter(filter)) img, err := restorable.NewImage(width, height, glFilter(filter), volatile)
if err != nil { if err != nil {
return nil, err return nil, err
} }
i := &imageImpl{ i := &imageImpl{
restorable: img, restorable: img,
volatile: volatile,
} }
runtime.SetFinalizer(i, (*imageImpl).Dispose) runtime.SetFinalizer(i, (*imageImpl).Dispose)
return i, nil return i, nil
@ -84,7 +82,6 @@ func newScreenImageImpl(width, height int) (*imageImpl, error) {
} }
i := &imageImpl{ i := &imageImpl{
restorable: img, restorable: img,
volatile: true,
screen: true, screen: true,
} }
runtime.SetFinalizer(i, (*imageImpl).Dispose) runtime.SetFinalizer(i, (*imageImpl).Dispose)
@ -110,10 +107,7 @@ func (i *imageImpl) clearIfVolatile() error {
if i.disposed { if i.disposed {
return nil return nil
} }
if !i.volatile { if err := i.restorable.ClearIfVolatile(); err != nil {
return nil
}
if err := i.restorable.Clear(); err != nil {
return err return err
} }
return nil return nil
@ -185,9 +179,6 @@ func (i *imageImpl) resolveStalePixels(context *opengl.Context) error {
if i.disposed { if i.disposed {
return nil return nil
} }
if i.volatile {
return nil
}
if err := i.restorable.ReadPixelsFromVRAMIfStale(context); err != nil { if err := i.restorable.ReadPixelsFromVRAMIfStale(context); err != nil {
return err return err
} }
@ -230,12 +221,6 @@ func (i *imageImpl) restore(context *opengl.Context) error {
} }
return nil return nil
} }
if i.volatile {
if err := i.restorable.Recreate(); err != nil {
return err
}
return nil
}
if err := i.restorable.RestoreImage(context); err != nil { if err := i.restorable.RestoreImage(context); err != nil {
return err return err
} }

View File

@ -43,18 +43,21 @@ type Image struct {
baseColor color.RGBA baseColor color.RGBA
drawImageHistory []*drawImageHistoryItem drawImageHistory []*drawImageHistoryItem
stale bool stale bool
volatile bool
} }
func NewImage(width, height int, filter opengl.Filter) (*Image, error) { func NewImage(width, height int, filter opengl.Filter, volatile bool) (*Image, error) {
img, err := graphics.NewImage(width, height, filter) img, err := graphics.NewImage(width, height, filter)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &Image{ return &Image{
image: img, image: img,
width: width, width: width,
height: height, height: height,
filter: filter, filter: filter,
volatile: volatile,
}, nil }, nil
} }
@ -80,9 +83,10 @@ func NewScreenFramebufferImage(width, height int) (*Image, error) {
return nil, err return nil, err
} }
return &Image{ return &Image{
image: img, image: img,
width: width, width: width,
height: height, height: height,
volatile: true,
}, nil }, nil
} }
@ -97,7 +101,10 @@ func (p *Image) makeStale() {
p.stale = true p.stale = true
} }
func (p *Image) Clear() error { func (p *Image) ClearIfVolatile() error {
if !p.volatile {
return nil
}
p.basePixels = nil p.basePixels = nil
p.baseColor = color.RGBA{} p.baseColor = color.RGBA{}
p.drawImageHistory = nil p.drawImageHistory = nil
@ -205,6 +212,9 @@ func (p *Image) readPixelsFromVRAM(image *graphics.Image, context *opengl.Contex
} }
func (p *Image) ReadPixelsFromVRAMIfStale(context *opengl.Context) error { func (p *Image) ReadPixelsFromVRAMIfStale(context *opengl.Context) error {
if p.volatile {
return nil
}
if !p.stale { if !p.stale {
return nil return nil
} }
@ -220,6 +230,15 @@ func (p *Image) HasDependency() bool {
// RestoreImage restores *graphics.Image from the pixels using its state. // RestoreImage restores *graphics.Image from the pixels using its state.
func (p *Image) RestoreImage(context *opengl.Context) error { func (p *Image) RestoreImage(context *opengl.Context) error {
if p.volatile {
var err error
p.image, err = graphics.NewImage(p.width, p.height, p.filter)
if err != nil {
return err
}
// TODO: Reset other values?
return nil
}
if p.stale { if p.stale {
return errors.New("restorable: pixels must not be stale when restoring") return errors.New("restorable: pixels must not be stale when restoring")
} }
@ -274,16 +293,6 @@ func (p *Image) RestoreAsScreen() error {
return nil return nil
} }
func (p *Image) Recreate() error {
var err error
p.image, err = graphics.NewImage(p.width, p.height, p.filter)
if err != nil {
return err
}
// TODO: Reset other values?
return nil
}
func (p *Image) Dispose() error { func (p *Image) Dispose() error {
if err := p.image.Dispose(); err != nil { if err := p.image.Dispose(); err != nil {
return err return err