restorable: Bug fix: Make limit to the number of draw-image command history (#349)

This commit is contained in:
Hajime Hoshi 2017-05-03 22:15:59 +09:00
parent e8b155fefe
commit d94e9c9d86
2 changed files with 9 additions and 4 deletions

View File

@ -72,7 +72,7 @@ func (i *images) resolveStalePixels(context *opengl.Context) error {
defer i.m.Unlock() defer i.m.Unlock()
i.lastChecked = nil i.lastChecked = nil
for img := range i.images { for img := range i.images {
if err := img.ReadPixelsFromVRAMIfStale(context); err != nil { if err := img.ResolveStalePixels(context); err != nil {
return err return err
} }
} }

View File

@ -24,6 +24,8 @@ import (
"github.com/hajimehoshi/ebiten/internal/opengl" "github.com/hajimehoshi/ebiten/internal/opengl"
) )
const drawImageHistoryMax = 100
type drawImageHistoryItem struct { type drawImageHistoryItem struct {
image *Image image *Image
vertices []float32 vertices []float32
@ -129,6 +131,10 @@ func (p *Image) appendDrawImageHistory(image *Image, vertices []float32, colorm
if p.stale { if p.stale {
return return
} }
if len(p.drawImageHistory)+1 > drawImageHistoryMax {
p.makeStale()
return
}
// All images must be resolved and not stale each after frame. // All images must be resolved and not stale each after frame.
// So we don't have to care if image is stale or not here. // So we don't have to care if image is stale or not here.
item := &drawImageHistoryItem{ item := &drawImageHistoryItem{
@ -164,7 +170,6 @@ func (p *Image) MakeStaleIfDependingOn(target *Image) {
if p.stale { if p.stale {
return return
} }
// 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 {
p.makeStale() p.makeStale()
@ -185,7 +190,7 @@ func (p *Image) readPixelsFromVRAM(image *graphics.Image, context *opengl.Contex
return nil return nil
} }
func (p *Image) ReadPixelsFromVRAMIfStale(context *opengl.Context) error { func (p *Image) ResolveStalePixels(context *opengl.Context) error {
if p.volatile { if p.volatile {
return nil return nil
} }
@ -199,7 +204,7 @@ func (p *Image) HasDependency() bool {
if p.stale { if p.stale {
return false return false
} }
return p.drawImageHistory != nil return len(p.drawImageHistory) > 0
} }
// Restore restores *graphics.Image from the pixels using its state. // Restore restores *graphics.Image from the pixels using its state.