internal/restorable: refactoring

This commit is contained in:
Hajime Hoshi 2023-03-10 22:29:59 +09:00
parent 46173ffec6
commit 84146510ce
3 changed files with 15 additions and 13 deletions

View File

@ -18,6 +18,6 @@ import (
"image" "image"
) )
func RemoveDuplicatedRegions(regions []image.Rectangle) []image.Rectangle { func RemoveDuplicatedRegions(regions []image.Rectangle) int {
return removeDuplicatedRegions(regions) return removeDuplicatedRegions(regions)
} }

View File

@ -257,7 +257,8 @@ func (i *Image) makeStale(rect image.Rectangle) {
} }
// Remove duplicated regions to avoid unnecessary reading pixels from GPU. // Remove duplicated regions to avoid unnecessary reading pixels from GPU.
i.staleRegions = removeDuplicatedRegions(i.staleRegions) n := removeDuplicatedRegions(i.staleRegions)
i.staleRegions = i.staleRegions[:n]
// Don't have to call makeStale recursively here. // Don't have to call makeStale recursively here.
// Restoring is done after topological sorting is done. // Restoring is done after topological sorting is done.
@ -486,8 +487,8 @@ func (i *Image) readPixelsFromGPU(graphicsDriver graphicsdriver.Graphics) error
defer func() { defer func() {
i.regionsCache = i.regionsCache[:0] i.regionsCache = i.regionsCache[:0]
}() }()
i.regionsCache = removeDuplicatedRegions(i.regionsCache) n := removeDuplicatedRegions(i.regionsCache)
rs = i.regionsCache rs = i.regionsCache[:n]
} }
for _, r := range rs { for _, r := range rs {
@ -628,9 +629,10 @@ func (i *Image) restore(graphicsDriver graphicsdriver.Graphics) error {
defer func() { defer func() {
i.regionsCache = i.regionsCache[:0] i.regionsCache = i.regionsCache[:0]
}() }()
i.regionsCache = removeDuplicatedRegions(i.regionsCache) n := removeDuplicatedRegions(i.regionsCache)
rs := i.regionsCache[:n]
for _, r := range i.regionsCache { for _, r := range rs {
if r.Empty() { if r.Empty() {
continue continue
} }
@ -706,9 +708,8 @@ func (i *Image) appendRegionsForDrawTriangles(regions []image.Rectangle) []image
regions = append(regions, r) regions = append(regions, r)
} }
origRegions := regions[:n] nn := removeDuplicatedRegions(regions[n:])
newRegions := removeDuplicatedRegions(regions[n:]) return regions[:n+nn]
return append(origRegions, newRegions...)
} }
func regionToRectangle(region graphicsdriver.Region) image.Rectangle { func regionToRectangle(region graphicsdriver.Region) image.Rectangle {
@ -719,9 +720,9 @@ func regionToRectangle(region graphicsdriver.Region) image.Rectangle {
int(math.Ceil(float64(region.Y+region.Height)))) int(math.Ceil(float64(region.Y+region.Height))))
} }
// removeDuplicatedRegions removes duplicated regions and returns a shrunk slice. // removeDuplicatedRegions removes duplicated regions and returns the new size of the slice.
// If a region covers other regions, the covered regions are removed. // If a region covers other regions, the covered regions are removed.
func removeDuplicatedRegions(regions []image.Rectangle) []image.Rectangle { func removeDuplicatedRegions(regions []image.Rectangle) int {
for i, r := range regions { for i, r := range regions {
if r.Empty() { if r.Empty() {
continue continue
@ -748,5 +749,5 @@ func removeDuplicatedRegions(regions []image.Rectangle) []image.Rectangle {
n++ n++
} }
return regions[:n] return n
} }

View File

@ -120,7 +120,8 @@ func TestRemoveDuplicatedRegions(t *testing.T) {
} }
for _, c := range cases { for _, c := range cases {
got := restorable.RemoveDuplicatedRegions(c.In) n := restorable.RemoveDuplicatedRegions(c.In)
got := c.In[:n]
want := c.Out want := c.Out
if !areEqualRectangles(got, want) { if !areEqualRectangles(got, want) {
t.Errorf("restorable.RemoveDuplicatedRegions(%#v): got: %#v, want: %#v", c.In, got, want) t.Errorf("restorable.RemoveDuplicatedRegions(%#v): got: %#v, want: %#v", c.In, got, want)