graphics: Refactoring

This commit is contained in:
Hajime Hoshi 2016-07-26 12:28:05 +09:00
parent 2e0b7da614
commit c5f3eaf925
3 changed files with 10 additions and 20 deletions

View File

@ -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)

View File

@ -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 {

View File

@ -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
}