From fc47d2843ab438b262ac9dae04c2ff649ecf5a68 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 25 Mar 2018 19:42:44 +0900 Subject: [PATCH] restorable: Add TestRestoreChain2 --- internal/restorable/image.go | 5 ++++ internal/restorable/images.go | 2 -- internal/restorable/images_test.go | 47 +++++++++++++++++++++++++++++- 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/internal/restorable/image.go b/internal/restorable/image.go index 235b0385c..9a4908ba5 100644 --- a/internal/restorable/image.go +++ b/internal/restorable/image.go @@ -110,6 +110,11 @@ func (i *Image) makeStale() { i.basePixels = nil i.drawImageHistory = nil i.stale = true + + // Don't have to call makeStale recursively here. + // Restoring is done after topological sorting is done. + // If an image depends on another stale image, this means that + // the former image can be restored from the latest state of the latter image. } // ReplacePixels replaces the image pixels with the given pixels slice. diff --git a/internal/restorable/images.go b/internal/restorable/images.go index 2017f02b0..896ceb050 100644 --- a/internal/restorable/images.go +++ b/internal/restorable/images.go @@ -121,8 +121,6 @@ func (i *images) makeStaleIfDependingOnImpl(target *Image) { } i.lastTarget = target for img := range i.images { - // TODO: This seems not enough: What if img becomes stale but what about - // other images depend on img? (#357) img.makeStaleIfDependingOn(target) } } diff --git a/internal/restorable/images_test.go b/internal/restorable/images_test.go index 909065cdb..b72854b08 100644 --- a/internal/restorable/images_test.go +++ b/internal/restorable/images_test.go @@ -117,7 +117,7 @@ func TestRestoreChain(t *testing.T) { clr := color.RGBA{0x00, 0x00, 0x00, 0xff} fill(imgs[0], clr.R, clr.G, clr.B, clr.A) for i := 0; i < num-1; i++ { - imgs[i+1].DrawImage(imgs[i], 0, 0, 1, 1, nil, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest) + imgs[i+1].DrawImage(imgs[i], 0, 0, 1, 1, nil, nil, opengl.CompositeModeCopy, graphics.FilterNearest) } if err := ResolveStaleImages(); err != nil { t.Fatal(err) @@ -134,6 +134,51 @@ func TestRestoreChain(t *testing.T) { } } +func TestRestoreChain2(t *testing.T) { + const num = 10 + imgs := []*Image{} + for i := 0; i < num; i++ { + img := NewImage(1, 1, false) + fill(img, 0, 0, 0, 0) + imgs = append(imgs, img) + } + defer func() { + for _, img := range imgs { + img.Dispose() + } + }() + + clr0 := color.RGBA{0xff, 0x00, 0x00, 0xff} + fill(imgs[0], clr0.R, clr0.G, clr0.B, clr0.A) + clr7 := color.RGBA{0x00, 0xff, 0x00, 0xff} + fill(imgs[7], clr7.R, clr7.G, clr7.B, clr7.A) + clr8 := color.RGBA{0x00, 0x00, 0xff, 0xff} + fill(imgs[8], clr8.R, clr8.G, clr8.B, clr8.A) + + imgs[8].DrawImage(imgs[7], 0, 0, 1, 1, nil, nil, opengl.CompositeModeCopy, graphics.FilterNearest) + imgs[9].DrawImage(imgs[8], 0, 0, 1, 1, nil, nil, opengl.CompositeModeCopy, graphics.FilterNearest) + for i := 0; i < 7; i++ { + imgs[i+1].DrawImage(imgs[i], 0, 0, 1, 1, nil, nil, opengl.CompositeModeCopy, graphics.FilterNearest) + } + + if err := ResolveStaleImages(); err != nil { + t.Fatal(err) + } + if err := Restore(); err != nil { + t.Fatal(err) + } + for i, img := range imgs { + want := clr0 + if i == 8 || i == 9 { + want = clr7 + } + got := byteSliceToColor(img.BasePixelsForTesting(), 0) + if !sameColors(got, want, 1) { + t.Errorf("%d: got %v, want %v", i, got, want) + } + } +} + func TestRestoreOverrideSource(t *testing.T) { img0 := NewImage(1, 1, false) fill(img0, 0, 0, 0, 0)