From b348223297cbfde867a01c4ba2e5062f9149a8a6 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 21 Jul 2019 16:38:16 +0900 Subject: [PATCH] restorable: Refactoring Reduces accesses to Image member from outside. --- internal/graphicscommand/image.go | 5 +++++ internal/restorable/image.go | 6 +++++- internal/restorable/images.go | 11 +---------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/internal/graphicscommand/image.go b/internal/graphicscommand/image.go index 663a4c4b7..7c9debb6f 100644 --- a/internal/graphicscommand/image.go +++ b/internal/graphicscommand/image.go @@ -182,6 +182,11 @@ func (i *Image) IsInvalidated() bool { // // This is for testing usage. func (i *Image) Dump(path string) error { + // Screen image cannot be dumped. + if i.screen { + return nil + } + path = strings.ReplaceAll(path, "*", fmt.Sprintf("%d", i.id)) f, err := os.Create(path) if err != nil { diff --git a/internal/restorable/image.go b/internal/restorable/image.go index 777d6f3ba..85f1239c6 100644 --- a/internal/restorable/image.go +++ b/internal/restorable/image.go @@ -476,7 +476,11 @@ func (i *Image) hasDependency() bool { // Restore restores *graphicscommand.Image from the pixels using its state. func (i *Image) restore() error { - w, h := i.image.Size() + w, h := i.Size() + + // Dispose the internal image after getting its size for safety. + i.image.Dispose() + if i.screen { // The screen image should also be recreated because framebuffer might // be changed. diff --git a/internal/restorable/images.go b/internal/restorable/images.go index 145af18c1..9ef68d5bd 100644 --- a/internal/restorable/images.go +++ b/internal/restorable/images.go @@ -95,10 +95,7 @@ func RestoreIfNeeded() error { // This is for testing usage. func DumpImages(dir string) error { for img := range theImages.images { - if img.screen { - continue - } - if err := img.image.Dump(filepath.Join(dir, "*.png")); err != nil { + if err := img.Dump(filepath.Join(dir, "*.png")); err != nil { return err } } @@ -154,12 +151,6 @@ func (i *images) restore() error { panic("restorable: restore cannot be called when restoring is disabled") } - // Dispose image explicitly - for img := range i.images { - img.image.Dispose() - // img.image can't be set nil here, or Size() panics when restoring. - } - // Let's do topological sort based on dependencies of drawing history. // It is assured that there are not loops since cyclic drawing makes images stale. type edge struct {