diff --git a/internal/packing/packing.go b/internal/packing/packing.go index 78c4940fd..c376fcb9e 100644 --- a/internal/packing/packing.go +++ b/internal/packing/packing.go @@ -207,11 +207,14 @@ func walk(n *Node, f func(n *Node) error) error { return nil } -func (p *Page) Extend() bool { +func (p *Page) Extend(count int) bool { if p.size >= p.maxSize { return false } - newSize := p.size * 2 + newSize := p.size + for i := 0; i < count; i++ { + newSize *= 2 + } edgeNodes := []*Node{} abort := errors.New("abort") aborted := false diff --git a/internal/packing/packing_test.go b/internal/packing/packing_test.go index 8737b9c2e..38a26f471 100644 --- a/internal/packing/packing_test.go +++ b/internal/packing/packing_test.go @@ -257,7 +257,7 @@ func TestExtend(t *testing.T) { p := NewPage(1024, 4096) s := p.Size() p.Alloc(s/2, s/2) - p.Extend() + p.Extend(1) if p.Size() != s*2 { t.Errorf("p.Size(): got: %d, want: %d", p.Size(), s*2) } @@ -281,7 +281,7 @@ func TestExtend2(t *testing.T) { p.Alloc(s/2, s/2) p.Free(n1) p.Free(n2) - p.Extend() + p.Extend(1) if p.Size() != s*2 { t.Errorf("p.Size(): got: %d, want: %d", p.Size(), s*2) } diff --git a/internal/shareable/image.go b/internal/shareable/image.go index 84848a036..eaa99af1b 100644 --- a/internal/shareable/image.go +++ b/internal/shareable/image.go @@ -91,16 +91,17 @@ type backend struct { } func (b *backend) TryAlloc(width, height int) (*packing.Node, bool) { - // If the region is allocated without any extension, it's fine. + // If the region is allocated without any extension, that's fine. if n := b.page.Alloc(width, height); n != nil { return n, true } // Simulate the extending the page and calculate the appropriate page size. + // By simulating, we can avoid unnecessary extention of underlying textures. page := b.page.Clone() nExtended := 0 for { - if !page.Extend() { + if !page.Extend(1) { // The page can't be extended any more. Return as failure. return nil, false } @@ -111,9 +112,7 @@ func (b *backend) TryAlloc(width, height int) (*packing.Node, bool) { } } - for i := 0; i < nExtended; i++ { - b.page.Extend() - } + b.page.Extend(nExtended) s := b.page.Size() b.restorable = b.restorable.Extend(s, s)