mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
internal/restorable: remove duplicated regions from i.staleRegions
Updates #2375 Updates #2581
This commit is contained in:
parent
22d74e78b2
commit
367606f274
@ -256,6 +256,9 @@ func (i *Image) makeStale(rect image.Rectangle) {
|
||||
i.basePixels.Clear(r.Min.X, r.Min.Y, r.Dx(), r.Dy())
|
||||
}
|
||||
|
||||
// Remove duplicated regions to avoid unnecessary reading pixels from GPU.
|
||||
i.staleRegions = removeDuplicatedRegions(i.staleRegions)
|
||||
|
||||
// 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
|
||||
@ -732,3 +735,29 @@ func regionToRectangle(region graphicsdriver.Region) image.Rectangle {
|
||||
int(math.Ceil(float64(region.X+region.Width))),
|
||||
int(math.Ceil(float64(region.Y+region.Height))))
|
||||
}
|
||||
|
||||
// removeDuplicatedRegions removes duplicated regions and returns a shrunk slice.
|
||||
// If a region covers preceding regions, the covered regions are removed.
|
||||
func removeDuplicatedRegions(regions []image.Rectangle) []image.Rectangle {
|
||||
for i, r := range regions {
|
||||
if r.Empty() {
|
||||
continue
|
||||
}
|
||||
for j, rr := range regions[:i] {
|
||||
if rr.In(r) {
|
||||
regions[j] = image.Rectangle{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
n := 0
|
||||
for _, r := range regions {
|
||||
if r.Empty() {
|
||||
continue
|
||||
}
|
||||
regions[n] = r
|
||||
n++
|
||||
}
|
||||
|
||||
return regions[:n]
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user