graphics: Clear volatile images at the start of a frame

This commit is contained in:
Hajime Hoshi 2016-07-06 01:30:49 +09:00
parent 1fdfa5707a
commit c7783b2ecf
3 changed files with 28 additions and 4 deletions

View File

@ -128,7 +128,8 @@ func (c *graphicsContext) UpdateAndDraw(context *opengl.Context, updateCount int
return err
}
for i := 0; i < updateCount; i++ {
if err := c.offscreen.Clear(); err != nil {
// TODO: This clears images not needed to be cleared (e.g. c.screen).
if err := theImagesForRestoring.clearVolatileImages(); err != nil {
return err
}
setRunningSlowly(i < updateCount-1)

View File

@ -81,6 +81,17 @@ func (i *images) restore(context *opengl.Context) error {
return nil
}
func (i *images) clearVolatileImages() error {
i.m.Lock()
defer i.m.Unlock()
for img := range i.images {
if err := img.clearIfVolatile(); err != nil {
return err
}
}
return nil
}
// Image represents an image.
// The pixel format is alpha-premultiplied.
// Image implements image.Image.
@ -209,14 +220,13 @@ func NewImage(width, height int, filter Filter) (*Image, error) {
}
// NewVolatileImage returns an empty 'volatile' image.
// A volatile image is an image that pixels might be lost at another frame.
// A volatile image is always cleared at the start of a frame.
//
// This is suitable for offscreen images that pixels are changed often.
//
// Pixels in regular non-volatile images are saved at each end of a frame if the image
// is changed, and restored automatically from the saved pixels on GL context lost.
// On the other hand, pixels in volatile images are not saved and can be lost
// on GL context lost.
// On the other hand, pixels in volatile images are not saved.
// Saving pixels is an expensive operation, and it is desirable to avoid it if possible.
//
// This function is concurrent-safe.

View File

@ -115,6 +115,19 @@ func (i *imageImpl) Fill(clr color.Color) error {
return i.image.Fill(clr)
}
func (i *imageImpl) clearIfVolatile() error {
i.m.Lock()
defer i.m.Unlock()
if i.disposed {
return nil
}
if !i.volatile {
return nil
}
i.pixels = nil
return i.image.Fill(color.Transparent)
}
func (i *imageImpl) DrawImage(image *Image, options *DrawImageOptions) error {
// Calculate vertices before locking because the user can do anything in
// options.ImageParts interface without deadlock (e.g. Call Image functions).