graphics: Refactoring (preparing SubImage)

This commit is contained in:
Hajime Hoshi 2018-10-25 12:48:18 +09:00
parent 765af5a7af
commit 58f4feda8d

View File

@ -36,31 +36,39 @@ func init() {
} }
type mipmap struct { type mipmap struct {
orig *shareable.Image
imgs []*shareable.Image imgs []*shareable.Image
} }
func newMipmap(s *shareable.Image) *mipmap { func newMipmap(s *shareable.Image) *mipmap {
return &mipmap{ return &mipmap{
imgs: []*shareable.Image{s}, orig: s,
} }
} }
func (m *mipmap) original() *shareable.Image { func (m *mipmap) original() *shareable.Image {
return m.imgs[0] return m.orig
} }
func (m *mipmap) level(i int) *shareable.Image { func (m *mipmap) level(level int) *shareable.Image {
w, h := m.imgs[len(m.imgs)-1].Size() if level == 0 {
for len(m.imgs) < i+1 { return m.orig
lastl := len(m.imgs) - 1 }
src := m.imgs[lastl]
idx := level - 1
w, h := m.orig.Size()
if len(m.imgs) > 0 {
w, h = m.imgs[len(m.imgs)-1].Size()
}
for len(m.imgs) < idx+1 {
src := m.level(len(m.imgs))
w2 := w / 2 w2 := w / 2
h2 := h / 2 h2 := h / 2
if w2 == 0 || h2 == 0 { if w2 == 0 || h2 == 0 {
return nil return nil
} }
var s *shareable.Image var s *shareable.Image
if m.imgs[0].IsVolatile() { if m.orig.IsVolatile() {
s = shareable.NewVolatileImage(w2, h2) s = shareable.NewVolatileImage(w2, h2)
} else { } else {
s = shareable.NewImage(w2, h2) s = shareable.NewImage(w2, h2)
@ -72,30 +80,29 @@ func (m *mipmap) level(i int) *shareable.Image {
w = w2 w = w2
h = h2 h = h2
} }
if len(m.imgs) <= i { if len(m.imgs) <= idx {
return nil return nil
} }
return m.imgs[i] return m.imgs[idx]
} }
func (m *mipmap) isDisposed() bool { func (m *mipmap) isDisposed() bool {
return len(m.imgs) == 0 return m.orig == nil
} }
func (m *mipmap) dispose() { func (m *mipmap) dispose() {
m.disposeMipmaps()
m.orig.Dispose()
m.orig = nil
}
func (m *mipmap) disposeMipmaps() {
for _, img := range m.imgs { for _, img := range m.imgs {
img.Dispose() img.Dispose()
} }
m.imgs = nil m.imgs = nil
} }
func (m *mipmap) disposeMipmaps() {
for i := 1; i < len(m.imgs); i++ {
m.imgs[i].Dispose()
}
m.imgs = m.imgs[:1]
}
// Image represents a rectangle set of pixels. // Image represents a rectangle set of pixels.
// The pixel format is alpha-premultiplied RGBA. // The pixel format is alpha-premultiplied RGBA.
// Image implements image.Image. // Image implements image.Image.