packing: Enable to specify max size of a page

This commit is contained in:
Hajime Hoshi 2018-03-10 20:53:20 +09:00
parent df9c67d5db
commit 1583cd0f7b
3 changed files with 88 additions and 79 deletions

View File

@ -20,13 +20,19 @@ import (
) )
const ( const (
MaxSize = 2048
minSize = 1 minSize = 1
) )
type Page struct { type Page struct {
root *Node root *Node
m sync.Mutex maxSize int
m sync.Mutex
}
func NewPage(maxSize int) *Page {
return &Page{
maxSize: maxSize,
}
} }
func (p *Page) IsEmpty() bool { func (p *Page) IsEmpty() bool {
@ -145,8 +151,8 @@ func (p *Page) Alloc(width, height int) *Node {
} }
if p.root == nil { if p.root == nil {
p.root = &Node{ p.root = &Node{
width: MaxSize, width: p.maxSize,
height: MaxSize, height: p.maxSize,
} }
} }
if width < minSize { if width < minSize {

View File

@ -52,7 +52,7 @@ func TestPage(t *testing.T) {
{0, 0, 0}, {0, 0, 0},
{0, 0, 2}, {0, 0, 2},
{0, 0, 4}, {0, 0, 4},
{MaxSize, MaxSize, -1}, {1024, 1024, -1},
}, },
Out: []*Rect{ Out: []*Rect{
{0, 0, 100, 100}, {0, 0, 100, 100},
@ -67,115 +67,115 @@ func TestPage(t *testing.T) {
nil, nil,
nil, nil,
nil, nil,
{0, 0, MaxSize, MaxSize}, {0, 0, 1024, 1024},
}, },
}, },
{ {
In: []Op{ In: []Op{
{200, 400, -1}, {100, 200, -1},
{MaxSize, MaxSize, -1}, {1024, 1024, -1},
{200, 400, -1}, {100, 200, -1},
{100, 100, -1}, {50, 50, -1},
{400, 400, -1},
{MaxSize, MaxSize, -1},
{1000, 1000, -1},
{1200, 1200, -1},
{200, 200, -1}, {200, 200, -1},
{1024, 1024, -1},
{500, 500, -1},
{600, 600, -1},
{100, 100, -1},
{0, 0, 2}, {0, 0, 2},
{200, 400, -1}, {100, 200, -1},
}, },
Out: []*Rect{ Out: []*Rect{
{0, 0, 200, 400}, {0, 0, 100, 200},
nil, nil,
{0, 400, 200, 400}, {0, 200, 100, 200},
{0, 800, 100, 100}, {0, 400, 50, 50},
{200, 0, 400, 400}, {100, 0, 200, 200},
nil, nil,
{200, 400, 1000, 1000}, {100, 200, 500, 500},
nil, nil,
{0, 900, 200, 200}, {0, 450, 100, 100},
nil, nil,
{0, 400, 200, 400}, {0, 200, 100, 200},
}, },
}, },
{ {
In: []Op{ In: []Op{
{512, 512, -1}, {256, 256, -1},
{512, 512, -1}, {256, 256, -1},
{512, 512, -1}, {256, 256, -1},
{512, 512, -1}, {256, 256, -1},
{512, 512, -1}, {256, 256, -1},
{512, 512, -1}, {256, 256, -1},
{512, 512, -1}, {256, 256, -1},
{512, 512, -1}, {256, 256, -1},
{512, 512, -1}, {256, 256, -1},
{512, 512, -1}, {256, 256, -1},
{512, 512, -1}, {256, 256, -1},
{512, 512, -1}, {256, 256, -1},
{512, 512, -1}, {256, 256, -1},
{512, 512, -1}, {256, 256, -1},
{512, 512, -1}, {256, 256, -1},
{512, 512, -1}, {256, 256, -1},
{512, 512, -1}, {256, 256, -1},
}, },
Out: []*Rect{ Out: []*Rect{
{0, 0, 512, 512}, {0, 0, 256, 256},
{0, 512, 512, 512}, {0, 256, 256, 256},
{0, 1024, 512, 512}, {0, 512, 256, 256},
{0, 1536, 512, 512}, {0, 768, 256, 256},
{512, 0, 512, 512}, {256, 0, 256, 256},
{1024, 0, 512, 512}, {512, 0, 256, 256},
{1536, 0, 512, 512}, {768, 0, 256, 256},
{512, 512, 512, 512}, {256, 256, 256, 256},
{512, 1024, 512, 512}, {256, 512, 256, 256},
{512, 1536, 512, 512}, {256, 768, 256, 256},
{1024, 512, 512, 512}, {512, 256, 256, 256},
{1536, 512, 512, 512}, {768, 256, 256, 256},
{1024, 1024, 512, 512}, {512, 512, 256, 256},
{1024, 1536, 512, 512}, {512, 768, 256, 256},
{1536, 1024, 512, 512}, {768, 512, 256, 256},
{1536, 1536, 512, 512}, {768, 768, 256, 256},
nil, nil,
}, },
}, },
{ {
In: []Op{ In: []Op{
{600, 600, -1}, {300, 300, -1},
{600, 600, -1}, {300, 300, -1},
{600, 600, -1}, {300, 300, -1},
{600, 600, -1}, {300, 300, -1},
{600, 600, -1}, {300, 300, -1},
{600, 600, -1}, {300, 300, -1},
{600, 600, -1}, {300, 300, -1},
{600, 600, -1}, {300, 300, -1},
{600, 600, -1}, {300, 300, -1},
{600, 600, -1}, {300, 300, -1},
}, },
Out: []*Rect{ Out: []*Rect{
{0, 0, 600, 600}, {0, 0, 300, 300},
{0, 600, 600, 600}, {0, 300, 300, 300},
{0, 1200, 600, 600}, {0, 600, 300, 300},
{600, 0, 600, 600}, {300, 0, 300, 300},
{1200, 0, 600, 600}, {600, 0, 300, 300},
{600, 600, 600, 600}, {300, 300, 300, 300},
{600, 1200, 600, 600}, {300, 600, 300, 300},
{1200, 600, 600, 600}, {600, 300, 300, 300},
{1200, 1200, 600, 600}, {600, 600, 300, 300},
nil, nil,
}, },
}, },
} }
for caseIndex, c := range cases { for caseIndex, c := range cases {
p := &Page{} p := NewPage(1024)
nodes := []*Node{} nodes := []*Node{}
for _, in := range c.In { for _, in := range c.In {
if in.FreeNodeID == -1 { if in.FreeNodeID == -1 {

View File

@ -22,7 +22,7 @@ import (
type sharedImage struct { type sharedImage struct {
restorable *restorable.Image restorable *restorable.Image
page packing.Page page *packing.Page
} }
var ( var (
@ -64,10 +64,12 @@ func (s *sharedImagePart) Dispose() {
var sharedImageLock sync.Mutex var sharedImageLock sync.Mutex
func newSharedImagePart(width, height int) *sharedImagePart { func newSharedImagePart(width, height int) *sharedImagePart {
const maxSize = 2048
sharedImageLock.Lock() sharedImageLock.Lock()
sharedImageLock.Unlock() sharedImageLock.Unlock()
if width > packing.MaxSize || height > packing.MaxSize { if width > maxSize || height > maxSize {
return nil return nil
} }
for _, s := range theSharedImages { for _, s := range theSharedImages {
@ -79,7 +81,8 @@ func newSharedImagePart(width, height int) *sharedImagePart {
} }
} }
s := &sharedImage{ s := &sharedImage{
restorable: restorable.NewImage(packing.MaxSize, packing.MaxSize, false), restorable: restorable.NewImage(maxSize, maxSize, false),
page: packing.NewPage(maxSize),
} }
theSharedImages = append(theSharedImages, s) theSharedImages = append(theSharedImages, s)