From e7925a12bdab758761307efc9ea9c44b7419beba Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 16 Mar 2018 01:21:33 +0900 Subject: [PATCH] shareable: Refactoring --- internal/shareable/shareable.go | 44 +++++++++++++++++---------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/internal/shareable/shareable.go b/internal/shareable/shareable.go index 33251275b..18483857c 100644 --- a/internal/shareable/shareable.go +++ b/internal/shareable/shareable.go @@ -29,21 +29,28 @@ import ( type backend struct { restorable *restorable.Image - page *packing.Page + + // If page is nil, the backend is not shared. + page *packing.Page } -func (b *backend) Extend() bool { - if !b.page.Extend() { - return false +func (b *backend) TryAlloc(width, height int) (*packing.Node, bool) { + for { + if n := b.page.Alloc(width, height); n != nil { + return n, true + } + if !b.page.Extend() { + break + } + + s := b.page.Size() + newImg := restorable.NewImage(s, s, false) + newImg.DrawImage(b.restorable, 0, 0, s, s, nil, nil, opengl.CompositeModeCopy, graphics.FilterNearest) + + b.restorable.Dispose() + b.restorable = newImg } - - s := b.page.Size() - newImg := restorable.NewImage(s, s, false) - newImg.DrawImage(b.restorable, 0, 0, s, s, nil, nil, opengl.CompositeModeCopy, graphics.FilterNearest) - - b.restorable.Dispose() - b.restorable = newImg - return true + return nil, false } var ( @@ -209,15 +216,10 @@ func NewImage(width, height int) *Image { } for _, b := range theBackends { - for { - if n := b.page.Alloc(width, height); n != nil { - return &Image{ - backend: b, - node: n, - } - } - if !b.Extend() { - break + if n, ok := b.TryAlloc(width, height); ok { + return &Image{ + backend: b, + node: n, } } }