From 9b6b5372ef1fd36f4badc0d54dd01f87206b99ce Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 27 Jul 2016 12:24:30 +0900 Subject: [PATCH] pixels: Remove image member --- imageimpl.go | 13 +++++-------- internal/pixels/pixels.go | 21 ++++++--------------- 2 files changed, 11 insertions(+), 23 deletions(-) diff --git a/imageimpl.go b/imageimpl.go index 4a3baf220..257e8b30a 100644 --- a/imageimpl.go +++ b/imageimpl.go @@ -34,7 +34,7 @@ type imageImpl struct { width int height int filter Filter - pixels *pixels.Pixels + pixels pixels.Pixels volatile bool screen bool m sync.Mutex @@ -51,7 +51,6 @@ func newImageImpl(width, height int, filter Filter, volatile bool) (*imageImpl, height: height, filter: filter, volatile: volatile, - pixels: pixels.NewPixels(img), } runtime.SetFinalizer(i, (*imageImpl).Dispose) return i, nil @@ -83,7 +82,6 @@ func newImageImplFromImage(source image.Image, filter Filter) (*imageImpl, error width: w, height: h, filter: filter, - pixels: pixels.NewPixels(img), } i.pixels.ReplacePixels(p) runtime.SetFinalizer(i, (*imageImpl).Dispose) @@ -101,7 +99,6 @@ func newScreenImageImpl(width, height int) (*imageImpl, error) { height: height, volatile: true, screen: true, - pixels: pixels.NewPixels(img), } runtime.SetFinalizer(i, (*imageImpl).Dispose) return i, nil @@ -181,7 +178,7 @@ func (i *imageImpl) At(x, y int, context *opengl.Context) color.Color { return color.Transparent } idx := 4*x + 4*y*i.width - clr, err := i.pixels.At(idx, context) + clr, err := i.pixels.At(idx, i.image, context) if err != nil { panic(err) } @@ -197,7 +194,7 @@ func (i *imageImpl) ensurePixels(context *opengl.Context) error { if i.volatile { return nil } - if err := i.pixels.ReadPixelsFromVRAMIfStale(context); err != nil { + if err := i.pixels.ReadPixelsFromVRAMIfStale(i.image, context); err != nil { return err } return nil @@ -225,7 +222,7 @@ func (i *imageImpl) resetPixelsIfDependingOn(target *imageImpl, context *opengl. i.pixels.MakeStale() return nil } - if err := i.pixels.ReadPixelsFromVRAM(context); err != nil { + if err := i.pixels.ReadPixelsFromVRAM(i.image, context); err != nil { return err } return nil @@ -282,7 +279,7 @@ func (i *imageImpl) Dispose() error { } i.image = nil i.disposed = true - i.pixels = nil + i.pixels.Clear() runtime.SetFinalizer(i, nil) return nil } diff --git a/internal/pixels/pixels.go b/internal/pixels/pixels.go index 0c71729cc..c1189786d 100644 --- a/internal/pixels/pixels.go +++ b/internal/pixels/pixels.go @@ -33,8 +33,6 @@ type drawImageHistoryItem struct { // Pixels represents pixels of an image for restoring when GL context is lost. type Pixels struct { - image *graphics.Image - // basePixels and baseColor are exclusive. basePixels []uint8 baseColor color.Color @@ -42,12 +40,6 @@ type Pixels struct { stale bool } -func NewPixels(image *graphics.Image) *Pixels { - return &Pixels{ - image: image, - } -} - func (p *Pixels) MakeStale() { p.basePixels = nil p.baseColor = nil @@ -97,9 +89,9 @@ func (p *Pixels) AppendDrawImageHistory(image *graphics.Image, vertices []int16, // // Note that this must not be called until context is available. // This means Pixels members must match with acutal state in VRAM. -func (p *Pixels) At(idx int, context *opengl.Context) (color.Color, error) { +func (p *Pixels) At(idx int, image *graphics.Image, context *opengl.Context) (color.Color, error) { if p.basePixels == nil || p.drawImageHistory != nil || p.stale { - if err := p.ReadPixelsFromVRAM(context); err != nil { + if err := p.ReadPixelsFromVRAM(image, context); err != nil { return nil, err } } @@ -119,9 +111,9 @@ func (p *Pixels) DependsOn(target *graphics.Image) bool { return false } -func (p *Pixels) ReadPixelsFromVRAM(context *opengl.Context) error { +func (p *Pixels) ReadPixelsFromVRAM(image *graphics.Image, context *opengl.Context) error { var err error - p.basePixels, err = p.image.Pixels(context) + p.basePixels, err = image.Pixels(context) if err != nil { return err } @@ -131,11 +123,11 @@ func (p *Pixels) ReadPixelsFromVRAM(context *opengl.Context) error { return nil } -func (p *Pixels) ReadPixelsFromVRAMIfStale(context *opengl.Context) error { +func (p *Pixels) ReadPixelsFromVRAMIfStale(image *graphics.Image, context *opengl.Context) error { if !p.stale { return nil } - return p.ReadPixelsFromVRAM(context) + return p.ReadPixelsFromVRAM(image, context) } func (p *Pixels) HasDependency() bool { @@ -177,7 +169,6 @@ func (p *Pixels) CreateImage(context *opengl.Context, width, height int, filter return nil, err } } - p.image = gimg p.basePixels, err = gimg.Pixels(context) if err != nil { return nil, err