From c5f3eaf92579a0f38517ff90f82bdb4f1885e4e9 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 26 Jul 2016 12:28:05 +0900 Subject: [PATCH] graphics: Refactoring --- image.go | 14 +++++++------- imageimpl.go | 4 ++-- internal/pixels/pixels.go | 12 +----------- 3 files changed, 10 insertions(+), 20 deletions(-) diff --git a/image.go b/image.go index c8e9c30c7..c5c0c28a1 100644 --- a/image.go +++ b/image.go @@ -74,11 +74,11 @@ func (i *images) ensurePixels(context *opengl.Context) error { return nil } -func (i *images) resetPixelsIfNeeded(target *Image, context *opengl.Context) error { +func (i *images) resetPixelsIfDependingOn(target *Image, context *opengl.Context) error { i.m.Lock() defer i.m.Unlock() for img := range i.images { - if err := img.resetPixelsIfNeeded(target.impl, context); err != nil { + if err := img.resetPixelsIfDependingOn(target.impl, context); err != nil { return err } } @@ -152,7 +152,7 @@ func (i *Image) Size() (width, height int) { // // This function is concurrent-safe. func (i *Image) Clear() error { - if err := theImagesForRestoring.resetPixelsIfNeeded(i, glContext()); err != nil { + if err := theImagesForRestoring.resetPixelsIfDependingOn(i, glContext()); err != nil { return err } return i.impl.Fill(color.Transparent) @@ -162,7 +162,7 @@ func (i *Image) Clear() error { // // This function is concurrent-safe. func (i *Image) Fill(clr color.Color) error { - if err := theImagesForRestoring.resetPixelsIfNeeded(i, glContext()); err != nil { + if err := theImagesForRestoring.resetPixelsIfDependingOn(i, glContext()); err != nil { return err } return i.impl.Fill(clr) @@ -185,7 +185,7 @@ func (i *Image) Fill(clr color.Color) error { // // This function is concurrent-safe. func (i *Image) DrawImage(image *Image, options *DrawImageOptions) error { - if err := theImagesForRestoring.resetPixelsIfNeeded(i, glContext()); err != nil { + if err := theImagesForRestoring.resetPixelsIfDependingOn(i, glContext()); err != nil { return err } return i.impl.DrawImage(image, options) @@ -223,7 +223,7 @@ func (i *Image) At(x, y int) color.Color { // // This function is concurrent-safe. func (i *Image) Dispose() error { - if err := theImagesForRestoring.resetPixelsIfNeeded(i, glContext()); err != nil { + if err := theImagesForRestoring.resetPixelsIfDependingOn(i, glContext()); err != nil { return err } if i.impl.isDisposed() { @@ -240,7 +240,7 @@ func (i *Image) Dispose() error { // // This function is concurrent-safe. func (i *Image) ReplacePixels(p []uint8) error { - if err := theImagesForRestoring.resetPixelsIfNeeded(i, glContext()); err != nil { + if err := theImagesForRestoring.resetPixelsIfDependingOn(i, glContext()); err != nil { return err } return i.impl.ReplacePixels(p) diff --git a/imageimpl.go b/imageimpl.go index 5f4a2ae87..669139b5e 100644 --- a/imageimpl.go +++ b/imageimpl.go @@ -214,7 +214,7 @@ func (i *imageImpl) ensurePixels(context *opengl.Context) error { return nil } -func (i *imageImpl) resetPixelsIfNeeded(target *imageImpl, context *opengl.Context) error { +func (i *imageImpl) resetPixelsIfDependingOn(target *imageImpl, context *opengl.Context) error { i.m.Lock() defer i.m.Unlock() if i == target { @@ -231,7 +231,7 @@ func (i *imageImpl) resetPixelsIfNeeded(target *imageImpl, context *opengl.Conte } // target is an image begin tried to mutate. // If pixels object is related to that image, the pixels must be reset. - if !i.pixels.NeedsReset(target.image) { + if !i.pixels.DependsOn(target.image) { return nil } if context == nil { diff --git a/internal/pixels/pixels.go b/internal/pixels/pixels.go index fb278b27f..1e1bec0ad 100644 --- a/internal/pixels/pixels.go +++ b/internal/pixels/pixels.go @@ -96,7 +96,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 *graphics.Image) bool { +func (p *Pixels) DependsOn(target *graphics.Image) bool { for _, c := range p.drawImageHistory { if c.image == target { return true @@ -116,16 +116,6 @@ func (p *Pixels) Reset(context *opengl.Context) error { return nil } -func (p *Pixels) NeedsReset(target *graphics.Image) bool { - if p.drawImageHistory == nil { - return false - } - if !p.hasHistoryWith(target) { - return false - } - return true -} - func (p *Pixels) HasHistory() bool { return p.drawImageHistory != nil }