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 (
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 {

View File

@ -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 {

View File

@ -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)