From 3ea6033c3dbe4a8ec650412c3174b2fd8ce688d2 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 24 Jul 2016 06:22:33 +0900 Subject: [PATCH] graphics: Don't ref *Image in pixels. Ref *graphics.Image instead --- image.go | 2 +- imageimpl.go | 15 ++++++++++++--- pixels.go | 15 ++++++--------- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/image.go b/image.go index 8613d1689..d50ae947f 100644 --- a/image.go +++ b/image.go @@ -78,7 +78,7 @@ func (i *images) flushPixelsIfNeeded(target *Image, context *opengl.Context) err i.m.Lock() defer i.m.Unlock() for img := range i.images { - if err := img.flushPixelsIfNeeded(target, context); err != nil { + if err := img.flushPixelsIfNeeded(target.impl, context); err != nil { return err } } diff --git a/imageimpl.go b/imageimpl.go index b87f09c93..ac4dca5f3 100644 --- a/imageimpl.go +++ b/imageimpl.go @@ -175,12 +175,15 @@ func (i *imageImpl) DrawImage(image *Image, options *DrawImageOptions) error { return errors.New("ebiten: image is already disposed") } c := &drawImageHistoryItem{ - image: image, + image: image.impl.image, vertices: vertices, geom: options.GeoM, colorm: options.ColorM, mode: opengl.CompositeMode(options.CompositeMode), } + if image.impl.pixels.inconsistent { + i.pixels.makeInconsistent() + } i.pixels.appendDrawImageHistory(c) geom := &options.GeoM colorm := &options.ColorM @@ -220,13 +223,19 @@ func (i *imageImpl) flushPixelsIfInconsistent(context *opengl.Context) error { return nil } -func (i *imageImpl) flushPixelsIfNeeded(target *Image, context *opengl.Context) error { +func (i *imageImpl) flushPixelsIfNeeded(target *imageImpl, context *opengl.Context) error { i.m.Lock() defer i.m.Unlock() + if i == target { + return nil + } if i.disposed { return nil } - if err := i.pixels.flushIfNeeded(target, context); err != nil { + if target.isDisposed() { + return errors.New("ebiten: target is already disposed") + } + if err := i.pixels.flushIfNeeded(target.image, context); err != nil { return err } return nil diff --git a/pixels.go b/pixels.go index 1c8aa7234..3582cb213 100644 --- a/pixels.go +++ b/pixels.go @@ -23,7 +23,7 @@ import ( ) type drawImageHistoryItem struct { - image *Image + image *graphics.Image vertices []int16 geom GeoM colorm ColorM @@ -83,9 +83,6 @@ func (p *pixels) makeInconsistent() { } func (p *pixels) appendDrawImageHistory(item *drawImageHistoryItem) { - if item.image.impl.pixels.inconsistent { - p.makeInconsistent() - } if p.inconsistent { return } @@ -107,7 +104,7 @@ func (p *pixels) at(idx int, context *opengl.Context) (color.Color, error) { return color.RGBA{r, g, b, a}, nil } -func (p *pixels) hasHistoryWith(target *Image) bool { +func (p *pixels) hasHistoryWith(target *graphics.Image) bool { for _, c := range p.drawImageHistory { if c.image == target { return true @@ -131,7 +128,7 @@ func (p *pixels) flushIfInconsistent(context *opengl.Context) error { return nil } -func (p *pixels) flushIfNeeded(target *Image, context *opengl.Context) error { +func (p *pixels) flushIfNeeded(target *graphics.Image, context *opengl.Context) error { if p.drawImageHistory == nil { return nil } @@ -181,10 +178,10 @@ func (p *pixels) restore(context *opengl.Context, width, height int, filter Filt } for _, c := range p.drawImageHistory { // c.image.impl must be already restored. - if c.image.impl.hasHistory() { + /*if c.image.impl.hasHistory() { panic("not reach") - } - if err := gimg.DrawImage(c.image.impl.image, c.vertices, &c.geom, &c.colorm, c.mode); err != nil { + }*/ + if err := gimg.DrawImage(c.image, c.vertices, &c.geom, &c.colorm, c.mode); err != nil { return nil, err } }