pixels: Remove 'inconsistent' property

This commit is contained in:
Hajime Hoshi 2016-07-25 09:01:00 +09:00
parent 931e60902f
commit 95dae4ad45
4 changed files with 10 additions and 41 deletions

View File

@ -95,6 +95,9 @@ func (c *graphicsContext) initializeIfNeeded(context *opengl.Context) error {
if err := graphics.Reset(context); err != nil { if err := graphics.Reset(context); err != nil {
return err return err
} }
if err := theImagesForRestoring.flushPixels(context); err != nil {
return err
}
atomic.StoreInt32(&c.initialized, 1) atomic.StoreInt32(&c.initialized, 1)
} }
r, err := c.needsRestoring(context) r, err := c.needsRestoring(context)
@ -141,9 +144,6 @@ func (c *graphicsContext) UpdateAndDraw(context *opengl.Context, updateCount int
if err := c.initializeIfNeeded(context); err != nil { if err := c.initializeIfNeeded(context); err != nil {
return err return err
} }
if err := theImagesForRestoring.flushPixelsIfInconsistent(context); err != nil {
return err
}
for i := 0; i < updateCount; i++ { for i := 0; i < updateCount; i++ {
if err := theImagesForRestoring.clearVolatileImages(); err != nil { if err := theImagesForRestoring.clearVolatileImages(); err != nil {
return err return err

View File

@ -63,11 +63,11 @@ func (i *images) remove(img *Image) {
runtime.SetFinalizer(img, nil) runtime.SetFinalizer(img, nil)
} }
func (i *images) flushPixelsIfInconsistent(context *opengl.Context) error { func (i *images) flushPixels(context *opengl.Context) error {
i.m.Lock() i.m.Lock()
defer i.m.Unlock() defer i.m.Unlock()
for img := range i.images { for img := range i.images {
if err := img.flushPixelsIfInconsistent(context); err != nil { if err := img.flushPixels(context); err != nil {
return err return err
} }
} }

View File

@ -171,9 +171,6 @@ func (i *imageImpl) DrawImage(image *Image, options *DrawImageOptions) error {
} }
geom := options.GeoM geom := options.GeoM
colorm := options.ColorM colorm := options.ColorM
if image.impl.pixels.IsInconsistent() {
i.pixels.MakeInconsistent()
}
i.pixels.AppendDrawImageHistory(image.impl.image, vertices, &geom, &colorm, opengl.CompositeMode(options.CompositeMode)) i.pixels.AppendDrawImageHistory(image.impl.image, vertices, &geom, &colorm, opengl.CompositeMode(options.CompositeMode))
mode := opengl.CompositeMode(options.CompositeMode) mode := opengl.CompositeMode(options.CompositeMode)
if err := i.image.DrawImage(image.impl.image, vertices, &geom, &colorm, mode); err != nil { if err := i.image.DrawImage(image.impl.image, vertices, &geom, &colorm, mode); err != nil {
@ -199,13 +196,13 @@ func (i *imageImpl) At(x, y int, context *opengl.Context) color.Color {
return clr return clr
} }
func (i *imageImpl) flushPixelsIfInconsistent(context *opengl.Context) error { func (i *imageImpl) flushPixels(context *opengl.Context) error {
i.m.Lock() i.m.Lock()
defer i.m.Unlock() defer i.m.Unlock()
if i.disposed { if i.disposed {
return nil return nil
} }
if err := i.pixels.FlushIfInconsistent(context); err != nil { if err := i.pixels.Flush(context); err != nil {
return err return err
} }
return nil return nil
@ -225,7 +222,7 @@ func (i *imageImpl) flushPixelsIfNeeded(target *imageImpl, context *opengl.Conte
} }
if context == nil { if context == nil {
// context is null when this is not initialized yet. // context is null when this is not initialized yet.
i.pixels.MakeInconsistent() // In this case, pixels is inconsistent with the image.
return nil return nil
} }
if err := i.pixels.FlushIfNeeded(target.image, context); err != nil { if err := i.pixels.FlushIfNeeded(target.image, context); err != nil {

View File

@ -34,7 +34,6 @@ type drawImageHistoryItem struct {
type Pixels struct { type Pixels struct {
image *graphics.Image image *graphics.Image
inconsistent bool
basePixels []uint8 basePixels []uint8
baseColor color.Color baseColor color.Color
drawImageHistory []*drawImageHistoryItem drawImageHistory []*drawImageHistoryItem
@ -51,47 +50,27 @@ func (p *Pixels) ResetWithPixels(pixels []uint8) {
p.basePixels = make([]uint8, len(pixels)) p.basePixels = make([]uint8, len(pixels))
} }
copy(p.basePixels, pixels) copy(p.basePixels, pixels)
p.inconsistent = false
p.baseColor = nil p.baseColor = nil
p.drawImageHistory = nil p.drawImageHistory = nil
} }
func (p *Pixels) Clear() { func (p *Pixels) Clear() {
p.inconsistent = false
p.basePixels = nil p.basePixels = nil
p.baseColor = nil p.baseColor = nil
p.drawImageHistory = nil p.drawImageHistory = nil
} }
func (p *Pixels) IsCleared() bool { func (p *Pixels) IsCleared() bool {
if p.inconsistent {
return false
}
return p.basePixels == nil && p.baseColor == nil && p.drawImageHistory == nil return p.basePixels == nil && p.baseColor == nil && p.drawImageHistory == nil
} }
func (p *Pixels) Fill(clr color.Color) { func (p *Pixels) Fill(clr color.Color) {
p.inconsistent = false
p.basePixels = nil p.basePixels = nil
p.baseColor = clr p.baseColor = clr
p.drawImageHistory = nil p.drawImageHistory = nil
} }
func (p *Pixels) IsInconsistent() bool {
return p.inconsistent
}
func (p *Pixels) MakeInconsistent() {
p.inconsistent = true
p.basePixels = nil
p.baseColor = nil
p.drawImageHistory = nil
}
func (p *Pixels) AppendDrawImageHistory(image *graphics.Image, vertices []int16, geom graphics.Matrix, colorm graphics.Matrix, mode opengl.CompositeMode) { func (p *Pixels) AppendDrawImageHistory(image *graphics.Image, vertices []int16, geom graphics.Matrix, colorm graphics.Matrix, mode opengl.CompositeMode) {
if p.inconsistent {
return
}
item := &drawImageHistoryItem{ item := &drawImageHistoryItem{
image: image, image: image,
vertices: vertices, vertices: vertices,
@ -103,8 +82,7 @@ func (p *Pixels) AppendDrawImageHistory(image *graphics.Image, vertices []int16,
} }
func (p *Pixels) At(idx int, context *opengl.Context) (color.Color, error) { func (p *Pixels) At(idx int, context *opengl.Context) (color.Color, error) {
if p.inconsistent || p.basePixels == nil || p.drawImageHistory != nil { if p.basePixels == nil || p.drawImageHistory != nil {
p.inconsistent = false
var err error var err error
p.basePixels, err = p.image.Pixels(context) p.basePixels, err = p.image.Pixels(context)
if err != nil { if err != nil {
@ -126,11 +104,7 @@ func (p *Pixels) hasHistoryWith(target *graphics.Image) bool {
return false return false
} }
func (p *Pixels) FlushIfInconsistent(context *opengl.Context) error { func (p *Pixels) Flush(context *opengl.Context) error {
if !p.inconsistent {
return nil
}
p.inconsistent = false
var err error var err error
p.basePixels, err = p.image.Pixels(context) p.basePixels, err = p.image.Pixels(context)
if err != nil { if err != nil {
@ -148,7 +122,6 @@ func (p *Pixels) FlushIfNeeded(target *graphics.Image, context *opengl.Context)
if !p.hasHistoryWith(target) { if !p.hasHistoryWith(target) {
return nil return nil
} }
p.inconsistent = false
var err error var err error
p.basePixels, err = p.image.Pixels(context) p.basePixels, err = p.image.Pixels(context)
if err != nil { if err != nil {
@ -194,7 +167,6 @@ func (p *Pixels) Restore(context *opengl.Context, width, height int, filter open
return nil, err return nil, err
} }
} }
p.inconsistent = false
p.basePixels, err = gimg.Pixels(context) p.basePixels, err = gimg.Pixels(context)
if err != nil { if err != nil {
return nil, err return nil, err