restorable: Reduce Image() usage and remove MakeStale()

This commit is contained in:
Hajime Hoshi 2016-09-03 23:54:52 +09:00
parent e7558036ae
commit 82febc9c6e
2 changed files with 15 additions and 17 deletions

View File

@ -180,7 +180,7 @@ func (i *imageImpl) At(x, y int, context *opengl.Context) color.Color {
return color.Transparent return color.Transparent
} }
idx := 4*x + 4*y*i.width idx := 4*x + 4*y*i.width
clr, err := i.restorable.At(idx, i.restorable.Image(), context) clr, err := i.restorable.At(idx, context)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -196,7 +196,7 @@ func (i *imageImpl) resolveStalePixels(context *opengl.Context) error {
if i.volatile { if i.volatile {
return nil return nil
} }
if err := i.restorable.ReadPixelsFromVRAMIfStale(i.restorable.Image(), context); err != nil { if err := i.restorable.ReadPixelsFromVRAMIfStale(context); err != nil {
return err return err
} }
return nil return nil
@ -216,10 +216,7 @@ func (i *imageImpl) resetPixelsIfDependingOn(target *imageImpl, context *opengl.
} }
// target is an image that is about to be tried mutating. // target is an image that is about to be tried mutating.
// If pixels object is related to that image, the pixels must be reset. // If pixels object is related to that image, the pixels must be reset.
if !i.restorable.DependsOn(target.restorable.Image()) { i.restorable.MakeStaleIfDependingOn(target.restorable)
return nil
}
i.restorable.MakeStale()
return nil return nil
} }

View File

@ -73,7 +73,7 @@ func NewScreenFramebufferImage(width, height int) (*Image, error) {
}, nil }, nil
} }
func (p *Image) MakeStale() { func (p *Image) makeStale() {
p.basePixels = nil p.basePixels = nil
p.baseColor = color.RGBA{} p.baseColor = color.RGBA{}
p.drawImageHistory = nil p.drawImageHistory = nil
@ -117,7 +117,7 @@ func (p *Image) ReplacePixels(pixels []uint8) {
func (p *Image) DrawImage(img *Image, vertices []int16, geom graphics.Matrix, colorm graphics.Matrix, mode opengl.CompositeMode) error { func (p *Image) DrawImage(img *Image, vertices []int16, geom graphics.Matrix, colorm graphics.Matrix, mode opengl.CompositeMode) error {
if img.stale { if img.stale {
p.MakeStale() p.makeStale()
} else { } else {
p.appendDrawImageHistory(img.image, vertices, geom, colorm, mode) p.appendDrawImageHistory(img.image, vertices, geom, colorm, mode)
} }
@ -152,9 +152,9 @@ func (p *Image) appendDrawImageHistory(image *graphics.Image, vertices []int16,
// //
// Note that this must not be called until context is available. // Note that this must not be called until context is available.
// This means Pixels members must match with acutal state in VRAM. // This means Pixels members must match with acutal state in VRAM.
func (p *Image) At(idx int, image *graphics.Image, context *opengl.Context) (color.RGBA, error) { func (p *Image) At(idx int, context *opengl.Context) (color.RGBA, error) {
if p.basePixels == nil || p.drawImageHistory != nil || p.stale { if p.basePixels == nil || p.drawImageHistory != nil || p.stale {
if err := p.readPixelsFromVRAM(image, context); err != nil { if err := p.readPixelsFromVRAM(p.image, context); err != nil {
return color.RGBA{}, err return color.RGBA{}, err
} }
} }
@ -162,17 +162,18 @@ func (p *Image) At(idx int, image *graphics.Image, context *opengl.Context) (col
return color.RGBA{r, g, b, a}, nil return color.RGBA{r, g, b, a}, nil
} }
func (p *Image) DependsOn(target *graphics.Image) bool { func (p *Image) MakeStaleIfDependingOn(target *Image) {
if p.stale { if p.stale {
return false return
} }
// TODO: Performance is bad when drawImageHistory is too many. // TODO: Performance is bad when drawImageHistory is too many.
for _, c := range p.drawImageHistory { for _, c := range p.drawImageHistory {
if c.image == target { if c.image == target.image {
return true p.makeStale()
return
} }
} }
return false return
} }
func (p *Image) readPixelsFromVRAM(image *graphics.Image, context *opengl.Context) error { func (p *Image) readPixelsFromVRAM(image *graphics.Image, context *opengl.Context) error {
@ -187,11 +188,11 @@ func (p *Image) readPixelsFromVRAM(image *graphics.Image, context *opengl.Contex
return nil return nil
} }
func (p *Image) ReadPixelsFromVRAMIfStale(image *graphics.Image, context *opengl.Context) error { func (p *Image) ReadPixelsFromVRAMIfStale(context *opengl.Context) error {
if !p.stale { if !p.stale {
return nil return nil
} }
return p.readPixelsFromVRAM(image, context) return p.readPixelsFromVRAM(p.image, context)
} }
func (p *Image) HasDependency() bool { func (p *Image) HasDependency() bool {