restorable: Refactoring

This commit is contained in:
Hajime Hoshi 2017-08-06 23:00:24 +09:00
parent 1fb7e123c2
commit 9ab517cf82
6 changed files with 30 additions and 44 deletions

View File

@ -75,7 +75,7 @@ func (c *graphicsContext) SetSize(screenWidth, screenHeight int, screenScale flo
func (c *graphicsContext) initializeIfNeeded() error {
if !c.initialized {
if err := restorable.Reset(); err != nil {
if err := restorable.ResetGLState(); err != nil {
return err
}
c.initialized = true
@ -96,15 +96,6 @@ func drawWithFittingScale(dst *Image, src *Image) {
_ = dst.DrawImage(src, op)
}
func (c *graphicsContext) drawToDefaultRenderTarget() error {
_ = c.screen.Clear()
drawWithFittingScale(c.screen, c.offscreen2)
if err := restorable.FlushCommands(); err != nil {
return err
}
return nil
}
func (c *graphicsContext) Update(updateCount int) error {
if err := c.initializeIfNeeded(); err != nil {
return err
@ -119,11 +110,10 @@ func (c *graphicsContext) Update(updateCount int) error {
if 0 < updateCount {
drawWithFittingScale(c.offscreen2, c.offscreen)
}
if err := c.drawToDefaultRenderTarget(); err != nil {
return err
}
// TODO: Add tests to check if this behavior is correct (#357)
if err := restorable.ResolveStalePixels(); err != nil {
_ = c.screen.Clear()
drawWithFittingScale(c.screen, c.offscreen2)
if err := restorable.FlushAndResolveStalePixels(); err != nil {
return err
}
return nil
@ -140,9 +130,6 @@ func (c *graphicsContext) restoreIfNeeded() error {
if !r {
return nil
}
if err := restorable.Reset(); err != nil {
return err
}
if err := restorable.Restore(); err != nil {
return err
}

View File

@ -16,14 +16,6 @@
package ebiten
import (
"github.com/hajimehoshi/ebiten/internal/restorable"
)
func (c *graphicsContext) needsRestoring() (bool, error) {
// FlushCommands is required because c.offscreen.impl might not have an actual texture.
if err := restorable.FlushCommands(); err != nil {
return false, err
}
return c.offscreen.restorable.IsInvalidated(), nil
return c.offscreen.restorable.IsInvalidated()
}

View File

@ -125,7 +125,7 @@ const (
maxQuads = indicesNum / 6
)
func Reset() error {
func ResetGLState() error {
return theOpenGLState.reset()
}

View File

@ -347,9 +347,14 @@ func (p *Image) Dispose() {
runtime.SetFinalizer(p, nil)
}
func (p *Image) IsInvalidated() bool {
if !IsRestoringEnabled() {
return false
func (p *Image) IsInvalidated() (bool, error) {
// FlushCommands is required because c.offscreen.impl might not have an actual texture.
if err := graphics.FlushCommands(); err != nil {
return false, err
}
return p.image.IsInvalidated()
if !IsRestoringEnabled() {
return false, nil
}
return p.image.IsInvalidated(), nil
}

View File

@ -42,11 +42,17 @@ var theImages = &images{
images: map[*Image]struct{}{},
}
func ResolveStalePixels() error {
func FlushAndResolveStalePixels() error {
if err := graphics.FlushCommands(); err != nil {
return err
}
return theImages.resolveStalePixels()
}
func Restore() error {
if err := graphics.ResetGLState(); err != nil {
return err
}
return theImages.restore()
}
@ -166,10 +172,6 @@ func (i *images) clearVolatileImages() {
}
}
func Reset() error {
return graphics.Reset()
}
func FlushCommands() error {
return graphics.FlushCommands()
func ResetGLState() error {
return graphics.ResetGLState()
}

View File

@ -56,7 +56,7 @@ func TestRestore(t *testing.T) {
}()
clr0 := color.RGBA{0x00, 0x00, 0x00, 0xff}
img0.Fill(clr0.R, clr0.G, clr0.B, clr0.A)
if err := ResolveStalePixels(); err != nil {
if err := FlushAndResolveStalePixels(); err != nil {
t.Fatal(err)
}
if err := Restore(); err != nil {
@ -101,7 +101,7 @@ func TestRestoreChain(t *testing.T) {
for i := 0; i < num-1; i++ {
imgs[i+1].DrawImage(imgs[i], vertices(1, 1, 0, 0), &affine.ColorM{}, opengl.CompositeModeSourceOver)
}
if err := ResolveStalePixels(); err != nil {
if err := FlushAndResolveStalePixels(); err != nil {
t.Fatal(err)
}
if err := Restore(); err != nil {
@ -138,7 +138,7 @@ func TestRestoreOverrideSource(t *testing.T) {
img3.DrawImage(img2, vertices(1, 1, 0, 0), &affine.ColorM{}, opengl.CompositeModeSourceOver)
img0.Fill(clr1.R, clr1.G, clr1.B, clr1.A)
img1.DrawImage(img0, vertices(1, 1, 0, 0), &affine.ColorM{}, opengl.CompositeModeSourceOver)
if err := ResolveStalePixels(); err != nil {
if err := FlushAndResolveStalePixels(); err != nil {
t.Fatal(err)
}
if err := Restore(); err != nil {
@ -224,7 +224,7 @@ func TestRestoreComplexGraph(t *testing.T) {
img6.DrawImage(img4, vertices(4, 1, 1, 0), &affine.ColorM{}, opengl.CompositeModeSourceOver)
img7.DrawImage(img2, vertices(4, 1, 0, 0), &affine.ColorM{}, opengl.CompositeModeSourceOver)
img7.DrawImage(img3, vertices(4, 1, 2, 0), &affine.ColorM{}, opengl.CompositeModeSourceOver)
if err := ResolveStalePixels(); err != nil {
if err := FlushAndResolveStalePixels(); err != nil {
t.Fatal(err)
}
if err := Restore(); err != nil {
@ -305,7 +305,7 @@ func TestRestoreRecursive(t *testing.T) {
}()
img1.DrawImage(img0, vertices(4, 1, 1, 0), &affine.ColorM{}, opengl.CompositeModeSourceOver)
img0.DrawImage(img1, vertices(4, 1, 1, 0), &affine.ColorM{}, opengl.CompositeModeSourceOver)
if err := ResolveStalePixels(); err != nil {
if err := FlushAndResolveStalePixels(); err != nil {
t.Fatal(err)
}
if err := Restore(); err != nil {