From 1583cd0f7bfb6c10893f75684dabe40e51a89974 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 10 Mar 2018 20:53:20 +0900 Subject: [PATCH] packing: Enable to specify max size of a page --- internal/packing/packing.go | 16 ++-- internal/packing/packing_test.go | 142 +++++++++++++++---------------- shared.go | 9 +- 3 files changed, 88 insertions(+), 79 deletions(-) diff --git a/internal/packing/packing.go b/internal/packing/packing.go index 77c1c1dce..bc8cb6b53 100644 --- a/internal/packing/packing.go +++ b/internal/packing/packing.go @@ -20,13 +20,19 @@ import ( ) const ( - MaxSize = 2048 minSize = 1 ) type Page struct { - root *Node - m sync.Mutex + root *Node + maxSize int + m sync.Mutex +} + +func NewPage(maxSize int) *Page { + return &Page{ + maxSize: maxSize, + } } func (p *Page) IsEmpty() bool { @@ -145,8 +151,8 @@ func (p *Page) Alloc(width, height int) *Node { } if p.root == nil { p.root = &Node{ - width: MaxSize, - height: MaxSize, + width: p.maxSize, + height: p.maxSize, } } if width < minSize { diff --git a/internal/packing/packing_test.go b/internal/packing/packing_test.go index 91cf0a709..86a65cc73 100644 --- a/internal/packing/packing_test.go +++ b/internal/packing/packing_test.go @@ -52,7 +52,7 @@ func TestPage(t *testing.T) { {0, 0, 0}, {0, 0, 2}, {0, 0, 4}, - {MaxSize, MaxSize, -1}, + {1024, 1024, -1}, }, Out: []*Rect{ {0, 0, 100, 100}, @@ -67,115 +67,115 @@ func TestPage(t *testing.T) { nil, nil, nil, - {0, 0, MaxSize, MaxSize}, + {0, 0, 1024, 1024}, }, }, { In: []Op{ - {200, 400, -1}, - {MaxSize, MaxSize, -1}, - {200, 400, -1}, - {100, 100, -1}, - {400, 400, -1}, - {MaxSize, MaxSize, -1}, - {1000, 1000, -1}, - {1200, 1200, -1}, + {100, 200, -1}, + {1024, 1024, -1}, + {100, 200, -1}, + {50, 50, -1}, {200, 200, -1}, + {1024, 1024, -1}, + {500, 500, -1}, + {600, 600, -1}, + {100, 100, -1}, {0, 0, 2}, - {200, 400, -1}, + {100, 200, -1}, }, Out: []*Rect{ - {0, 0, 200, 400}, + {0, 0, 100, 200}, nil, - {0, 400, 200, 400}, - {0, 800, 100, 100}, - {200, 0, 400, 400}, + {0, 200, 100, 200}, + {0, 400, 50, 50}, + {100, 0, 200, 200}, nil, - {200, 400, 1000, 1000}, + {100, 200, 500, 500}, nil, - {0, 900, 200, 200}, + {0, 450, 100, 100}, nil, - {0, 400, 200, 400}, + {0, 200, 100, 200}, }, }, { In: []Op{ - {512, 512, -1}, - {512, 512, -1}, - {512, 512, -1}, - {512, 512, -1}, + {256, 256, -1}, + {256, 256, -1}, + {256, 256, -1}, + {256, 256, -1}, - {512, 512, -1}, - {512, 512, -1}, - {512, 512, -1}, - {512, 512, -1}, + {256, 256, -1}, + {256, 256, -1}, + {256, 256, -1}, + {256, 256, -1}, - {512, 512, -1}, - {512, 512, -1}, - {512, 512, -1}, - {512, 512, -1}, + {256, 256, -1}, + {256, 256, -1}, + {256, 256, -1}, + {256, 256, -1}, - {512, 512, -1}, - {512, 512, -1}, - {512, 512, -1}, - {512, 512, -1}, + {256, 256, -1}, + {256, 256, -1}, + {256, 256, -1}, + {256, 256, -1}, - {512, 512, -1}, + {256, 256, -1}, }, Out: []*Rect{ - {0, 0, 512, 512}, - {0, 512, 512, 512}, - {0, 1024, 512, 512}, - {0, 1536, 512, 512}, + {0, 0, 256, 256}, + {0, 256, 256, 256}, + {0, 512, 256, 256}, + {0, 768, 256, 256}, - {512, 0, 512, 512}, - {1024, 0, 512, 512}, - {1536, 0, 512, 512}, - {512, 512, 512, 512}, + {256, 0, 256, 256}, + {512, 0, 256, 256}, + {768, 0, 256, 256}, + {256, 256, 256, 256}, - {512, 1024, 512, 512}, - {512, 1536, 512, 512}, - {1024, 512, 512, 512}, - {1536, 512, 512, 512}, + {256, 512, 256, 256}, + {256, 768, 256, 256}, + {512, 256, 256, 256}, + {768, 256, 256, 256}, - {1024, 1024, 512, 512}, - {1024, 1536, 512, 512}, - {1536, 1024, 512, 512}, - {1536, 1536, 512, 512}, + {512, 512, 256, 256}, + {512, 768, 256, 256}, + {768, 512, 256, 256}, + {768, 768, 256, 256}, nil, }, }, { In: []Op{ - {600, 600, -1}, - {600, 600, -1}, - {600, 600, -1}, - {600, 600, -1}, - {600, 600, -1}, - {600, 600, -1}, - {600, 600, -1}, - {600, 600, -1}, - {600, 600, -1}, - {600, 600, -1}, + {300, 300, -1}, + {300, 300, -1}, + {300, 300, -1}, + {300, 300, -1}, + {300, 300, -1}, + {300, 300, -1}, + {300, 300, -1}, + {300, 300, -1}, + {300, 300, -1}, + {300, 300, -1}, }, Out: []*Rect{ - {0, 0, 600, 600}, - {0, 600, 600, 600}, - {0, 1200, 600, 600}, - {600, 0, 600, 600}, - {1200, 0, 600, 600}, - {600, 600, 600, 600}, - {600, 1200, 600, 600}, - {1200, 600, 600, 600}, - {1200, 1200, 600, 600}, + {0, 0, 300, 300}, + {0, 300, 300, 300}, + {0, 600, 300, 300}, + {300, 0, 300, 300}, + {600, 0, 300, 300}, + {300, 300, 300, 300}, + {300, 600, 300, 300}, + {600, 300, 300, 300}, + {600, 600, 300, 300}, nil, }, }, } for caseIndex, c := range cases { - p := &Page{} + p := NewPage(1024) nodes := []*Node{} for _, in := range c.In { if in.FreeNodeID == -1 { diff --git a/shared.go b/shared.go index 5b0e30222..c6549354e 100644 --- a/shared.go +++ b/shared.go @@ -22,7 +22,7 @@ import ( type sharedImage struct { restorable *restorable.Image - page packing.Page + page *packing.Page } var ( @@ -64,10 +64,12 @@ func (s *sharedImagePart) Dispose() { var sharedImageLock sync.Mutex func newSharedImagePart(width, height int) *sharedImagePart { + const maxSize = 2048 + sharedImageLock.Lock() sharedImageLock.Unlock() - if width > packing.MaxSize || height > packing.MaxSize { + if width > maxSize || height > maxSize { return nil } for _, s := range theSharedImages { @@ -79,7 +81,8 @@ func newSharedImagePart(width, height int) *sharedImagePart { } } s := &sharedImage{ - restorable: restorable.NewImage(packing.MaxSize, packing.MaxSize, false), + restorable: restorable.NewImage(maxSize, maxSize, false), + page: packing.NewPage(maxSize), } theSharedImages = append(theSharedImages, s)