graphics: Remove pixels when inconsistent

This commit is contained in:
Hajime Hoshi 2016-07-26 10:35:24 +09:00
parent 1ec503e1b5
commit 558f559323
3 changed files with 23 additions and 10 deletions

View File

@ -95,9 +95,6 @@ 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.resetPixels(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)
@ -110,7 +107,6 @@ func (c *graphicsContext) initializeIfNeeded(context *opengl.Context) error {
if err := c.restore(context); err != nil { if err := c.restore(context); err != nil {
return err return err
} }
return nil return nil
} }
@ -144,6 +140,9 @@ 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.resetPixels(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

@ -115,6 +115,9 @@ func (i *imageImpl) Fill(clr color.Color) error {
if i.disposed { if i.disposed {
return errors.New("ebiten: image is already disposed") return errors.New("ebiten: image is already disposed")
} }
if i.pixels == nil {
i.pixels = pixels.NewPixels(i.image)
}
i.pixels.Fill(clr) i.pixels.Fill(clr)
return i.image.Fill(clr) return i.image.Fill(clr)
} }
@ -128,6 +131,9 @@ func (i *imageImpl) clearIfVolatile() error {
if !i.volatile { if !i.volatile {
return nil return nil
} }
if i.pixels == nil {
i.pixels = pixels.NewPixels(i.image)
}
i.pixels.Clear() i.pixels.Clear()
return i.image.Fill(color.Transparent) return i.image.Fill(color.Transparent)
} }
@ -165,8 +171,10 @@ func (i *imageImpl) DrawImage(image *Image, options *DrawImageOptions) error {
} }
geom := options.GeoM geom := options.GeoM
colorm := options.ColorM colorm := options.ColorM
i.pixels.AppendDrawImageHistory(image.impl.image, vertices, &geom, &colorm, opengl.CompositeMode(options.CompositeMode))
mode := opengl.CompositeMode(options.CompositeMode) mode := opengl.CompositeMode(options.CompositeMode)
if i.pixels != nil {
i.pixels.AppendDrawImageHistory(image.impl.image, vertices, &geom, &colorm, mode)
}
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 {
return err return err
} }
@ -196,6 +204,10 @@ func (i *imageImpl) resetPixels(context *opengl.Context) error {
if i.disposed { if i.disposed {
return nil return nil
} }
if i.pixels != nil {
return nil
}
i.pixels = pixels.NewPixels(i.image)
if err := i.pixels.Reset(context); err != nil { if err := i.pixels.Reset(context); err != nil {
return err return err
} }
@ -216,7 +228,7 @@ func (i *imageImpl) resetPixelsIfNeeded(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.
// In this case, pixels is inconsistent with the image. i.pixels = nil
return nil return nil
} }
if err := i.pixels.ResetIfNeeded(target.image, context); err != nil { if err := i.pixels.ResetIfNeeded(target.image, context); err != nil {
@ -228,6 +240,9 @@ func (i *imageImpl) resetPixelsIfNeeded(target *imageImpl, context *opengl.Conte
func (i *imageImpl) hasHistory() bool { func (i *imageImpl) hasHistory() bool {
i.m.Lock() i.m.Lock()
defer i.m.Unlock() defer i.m.Unlock()
if i.pixels == nil {
return false
}
return i.pixels.HasHistory() return i.pixels.HasHistory()
} }
@ -287,6 +302,9 @@ func (i *imageImpl) ReplacePixels(p []uint8) error {
} }
i.m.Lock() i.m.Lock()
defer i.m.Unlock() defer i.m.Unlock()
if i.pixels == nil {
i.pixels = pixels.NewPixels(i.image)
}
i.pixels.ResetWithPixels(p) i.pixels.ResetWithPixels(p)
if i.disposed { if i.disposed {
return errors.New("ebiten: image is already disposed") return errors.New("ebiten: image is already disposed")

View File

@ -31,10 +31,6 @@ type drawImageHistoryItem struct {
} }
// Pixels represents pixels of an image for restoring when GL context is lost. // Pixels represents pixels of an image for restoring when GL context is lost.
//
// Until GL context is available,
// Pixels member states might not match with actual pixels in GPU so
// there is few function to retrieve the current state.
type Pixels struct { type Pixels struct {
image *graphics.Image image *graphics.Image