graphics: Improve clearing the offscreen performance

This commit is contained in:
Hajime Hoshi 2018-03-02 01:55:32 +09:00
parent d1ac0e1857
commit d0592d30e0
3 changed files with 2 additions and 36 deletions

View File

@ -88,7 +88,8 @@ func (c *graphicsContext) Update(afterFrameUpdate func()) error {
return err
}
for i := 0; i < updateCount; i++ {
restorable.ClearVolatileImages()
c.offscreen.fill(0, 0, 0, 0)
setRunningSlowly(i < updateCount-1)
if err := hooks.Run(); err != nil {
return err

View File

@ -115,25 +115,6 @@ func (i *Image) makeStale() {
i.stale = true
}
// clearIfVolatile clears the image if the image is volatile.
func (i *Image) clearIfVolatile() {
if !i.volatile {
return
}
i.basePixels = nil
i.drawImageHistory = nil
i.stale = false
if i.image == nil {
panic("not reached")
}
// TODO: ReplacePixels is bad in terms of performance. Use DrawImage if possible.
// Note that using DrawImage with *graphics.Image directly is dangerous since the image
// is never restored from context lost.
w, h := i.image.Size()
i.image.ReplacePixels(make([]byte, 4*w*h), 0, 0, w, h)
}
// ReplacePixels replaces the image pixels with the given pixels slice.
func (i *Image) ReplacePixels(pixels []byte, x, y, width, height int) {
w, h := i.image.Size()

View File

@ -73,13 +73,6 @@ func Restore() error {
return theImages.restore()
}
// ClearVolatileImages clears volatile images.
//
// ClearVolatileImages is intended to be called at the start of a frame.
func ClearVolatileImages() {
theImages.clearVolatileImages()
}
// add adds img to the images.
func (i *images) add(img *Image) {
i.m.Lock()
@ -195,15 +188,6 @@ func (i *images) restore() error {
return nil
}
// clearVolatileImages clears the volatile images.
func (i *images) clearVolatileImages() {
i.m.Lock()
defer i.m.Unlock()
for img := range i.images {
img.clearIfVolatile()
}
}
// InitializeGLState initializes the GL state.
func InitializeGLState() error {
return graphics.ResetGLState()