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 {
disposed bool
restorable *restorable.Image
volatile bool
screen bool
m sync.Mutex
}
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 {
return nil, err
}
i := &imageImpl{
restorable: img,
volatile: volatile,
}
runtime.SetFinalizer(i, (*imageImpl).Dispose)
return i, nil
@ -84,7 +82,6 @@ func newScreenImageImpl(width, height int) (*imageImpl, error) {
}
i := &imageImpl{
restorable: img,
volatile: true,
screen: true,
}
runtime.SetFinalizer(i, (*imageImpl).Dispose)
@ -110,10 +107,7 @@ func (i *imageImpl) clearIfVolatile() error {
if i.disposed {
return nil
}
if !i.volatile {
return nil
}
if err := i.restorable.Clear(); err != nil {
if err := i.restorable.ClearIfVolatile(); err != nil {
return err
}
return nil
@ -185,9 +179,6 @@ func (i *imageImpl) resolveStalePixels(context *opengl.Context) error {
if i.disposed {
return nil
}
if i.volatile {
return nil
}
if err := i.restorable.ReadPixelsFromVRAMIfStale(context); err != nil {
return err
}
@ -230,12 +221,6 @@ func (i *imageImpl) restore(context *opengl.Context) error {
}
return nil
}
if i.volatile {
if err := i.restorable.Recreate(); err != nil {
return err
}
return nil
}
if err := i.restorable.RestoreImage(context); err != nil {
return err
}

View File

@ -43,18 +43,21 @@ type Image struct {
baseColor color.RGBA
drawImageHistory []*drawImageHistoryItem
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)
if err != nil {
return nil, err
}
return &Image{
image: img,
width: width,
height: height,
filter: filter,
image: img,
width: width,
height: height,
filter: filter,
volatile: volatile,
}, nil
}
@ -80,9 +83,10 @@ func NewScreenFramebufferImage(width, height int) (*Image, error) {
return nil, err
}
return &Image{
image: img,
width: width,
height: height,
image: img,
width: width,
height: height,
volatile: true,
}, nil
}
@ -97,7 +101,10 @@ func (p *Image) makeStale() {
p.stale = true
}
func (p *Image) Clear() error {
func (p *Image) ClearIfVolatile() error {
if !p.volatile {
return nil
}
p.basePixels = nil
p.baseColor = color.RGBA{}
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 {
if p.volatile {
return nil
}
if !p.stale {
return nil
}
@ -220,6 +230,15 @@ func (p *Image) HasDependency() bool {
// RestoreImage restores *graphics.Image from the pixels using its state.
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 {
return errors.New("restorable: pixels must not be stale when restoring")
}
@ -274,16 +293,6 @@ func (p *Image) RestoreAsScreen() error {
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 {
if err := p.image.Dispose(); err != nil {
return err