graphics: Rename shareableImages -> mipmap

This commit is contained in:
Hajime Hoshi 2018-10-25 02:05:25 +09:00
parent 022c40aa17
commit 15cad6ed9a
2 changed files with 43 additions and 47 deletions

View File

@ -152,7 +152,7 @@ func (c *graphicsContext) needsRestoring() (bool, error) {
if web.IsBrowser() { if web.IsBrowser() {
return c.invalidated, nil return c.invalidated, nil
} }
return c.offscreen.shareableImages.level(0).IsInvalidated() return c.offscreen.mipmap.level(0).IsInvalidated()
} }
func (c *graphicsContext) restoreIfNeeded() error { func (c *graphicsContext) restoreIfNeeded() error {

View File

@ -35,65 +35,61 @@ func init() {
emptyImage, _ = NewImage(16, 16, FilterDefault) emptyImage, _ = NewImage(16, 16, FilterDefault)
} }
type shareableImages struct { type mipmap struct {
imgs []*shareable.Image imgs []*shareable.Image
} }
func newShareableImages(s *shareable.Image) *shareableImages { func newMipmap(s *shareable.Image) *mipmap {
return &shareableImages{ return &mipmap{
imgs: []*shareable.Image{s}, imgs: []*shareable.Image{s},
} }
} }
func (s *shareableImages) level(i int) *shareable.Image { func (m *mipmap) level(i int) *shareable.Image {
w, h := s.imgs[len(s.imgs) - 1].Size() w, h := m.imgs[len(m.imgs) - 1].Size()
for len(s.imgs) < i+1 { for len(m.imgs) < i+1 {
lastl := len(s.imgs) - 1 lastl := len(m.imgs) - 1
src := s.imgs[lastl] src := m.imgs[lastl]
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 sh *shareable.Image var s *shareable.Image
if s.imgs[0].IsVolatile() { if m.imgs[0].IsVolatile() {
sh = shareable.NewVolatileImage(w2, h2) s = shareable.NewVolatileImage(w2, h2)
} else { } else {
sh = shareable.NewImage(w2, h2) s = shareable.NewImage(w2, h2)
} }
vs := src.QuadVertices(0, 0, w, h, 0.5, 0, 0, 0.5, 0, 0, 1, 1, 1, 1) vs := src.QuadVertices(0, 0, w, h, 0.5, 0, 0, 0.5, 0, 0, 1, 1, 1, 1)
is := graphicsutil.QuadIndices() is := graphicsutil.QuadIndices()
sh.DrawImage(src, vs, is, nil, opengl.CompositeModeCopy, graphics.FilterLinear) s.DrawImage(src, vs, is, nil, opengl.CompositeModeCopy, graphics.FilterLinear)
s.imgs = append(s.imgs, sh) m.imgs = append(m.imgs, s)
w = w2 w = w2
h = h2 h = h2
} }
if len(s.imgs) <= i { if len(m.imgs) <= i {
return nil return nil
} }
return s.imgs[i] return m.imgs[i]
} }
func (s *shareableImages) len() int { func (m *mipmap) isDisposed() bool {
return len(s.imgs) return len(m.imgs) == 0
} }
func (s *shareableImages) isDisposed() bool { func (m *mipmap) dispose() {
return len(s.imgs) == 0 for _, img := range m.imgs {
}
func (s *shareableImages) dispose() {
for _, img := range s.imgs {
img.Dispose() img.Dispose()
} }
s.imgs = nil m.imgs = nil
} }
func (s *shareableImages) disposeMipmaps() { func (m *mipmap) disposeMipmaps() {
for i := 1; i < len(s.imgs); i++ { for i := 1; i < len(m.imgs); i++ {
s.imgs[i].Dispose() m.imgs[i].Dispose()
} }
s.imgs = s.imgs[:1] m.imgs = m.imgs[:1]
} }
// Image represents a rectangle set of pixels. // Image represents a rectangle set of pixels.
@ -106,9 +102,9 @@ type Image struct {
// See strings.Builder for similar examples. // See strings.Builder for similar examples.
addr *Image addr *Image
// shareableImages is a set of shareable.Image sorted by the order of mipmap level. // mipmap is a set of shareable.Image sorted by the order of mipmap level.
// The level 0 image is a regular image and higher-level images are used for mipmap. // The level 0 image is a regular image and higher-level images are used for mipmap.
shareableImages *shareableImages mipmap *mipmap
filter Filter filter Filter
} }
@ -121,11 +117,11 @@ func (i *Image) copyCheck() {
// Size returns the size of the image. // Size returns the size of the image.
func (i *Image) Size() (width, height int) { func (i *Image) Size() (width, height int) {
return i.shareableImages.level(0).Size() return i.mipmap.level(0).Size()
} }
func (i *Image) isDisposed() bool { func (i *Image) isDisposed() bool {
return i.shareableImages.isDisposed() return i.mipmap.isDisposed()
} }
// Clear resets the pixels of the image into 0. // Clear resets the pixels of the image into 0.
@ -159,7 +155,7 @@ func (i *Image) Fill(clr color.Color) error {
func (i *Image) fill(r, g, b, a uint8) { func (i *Image) fill(r, g, b, a uint8) {
if r == 0 && g == 0 && b == 0 && a == 0 { if r == 0 && g == 0 && b == 0 && a == 0 {
i.shareableImages.level(0).ReplacePixels(nil) i.mipmap.level(0).ReplacePixels(nil)
i.disposeMipmaps() i.disposeMipmaps()
return return
} }
@ -200,7 +196,7 @@ func (i *Image) disposeMipmaps() {
if i.isDisposed() { if i.isDisposed() {
panic("not reached") panic("not reached")
} }
i.shareableImages.disposeMipmaps() i.mipmap.disposeMipmaps()
} }
// DrawImage draws the given image on the image i. // DrawImage draws the given image on the image i.
@ -351,7 +347,7 @@ func (i *Image) drawImage(img *Image, options *DrawImageOptions) {
sy1 = sy1 / s sy1 = sy1 / s
} }
if src := img.shareableImages.level(level); src != nil { if src := img.mipmap.level(level); src != nil {
colorm := options.ColorM.impl colorm := options.ColorM.impl
cr, cg, cb, ca := float32(1), float32(1), float32(1), float32(1) cr, cg, cb, ca := float32(1), float32(1), float32(1), float32(1)
if colorm.ScaleOnly() { if colorm.ScaleOnly() {
@ -367,7 +363,7 @@ func (i *Image) drawImage(img *Image, options *DrawImageOptions) {
if colorm.ScaleOnly() { if colorm.ScaleOnly() {
colorm = nil colorm = nil
} }
i.shareableImages.level(0).DrawImage(src, vs, is, colorm, mode, filter) i.mipmap.level(0).DrawImage(src, vs, is, colorm, mode, filter)
} }
i.disposeMipmaps() i.disposeMipmaps()
} }
@ -443,11 +439,11 @@ func (i *Image) DrawTriangles(vertices []Vertex, indices []uint16, img *Image, o
} }
vs := []float32{} vs := []float32{}
src := img.shareableImages.level(0) src := img.mipmap.level(0)
for _, v := range vertices { for _, v := range vertices {
vs = append(vs, src.Vertex(float32(v.DstX), float32(v.DstY), v.SrcX, v.SrcY, v.ColorR, v.ColorG, v.ColorB, v.ColorA)...) vs = append(vs, src.Vertex(float32(v.DstX), float32(v.DstY), v.SrcX, v.SrcY, v.ColorR, v.ColorG, v.ColorB, v.ColorA)...)
} }
i.shareableImages.level(0).DrawImage(img.shareableImages.level(0), vs, indices, options.ColorM.impl, mode, filter) i.mipmap.level(0).DrawImage(img.mipmap.level(0), vs, indices, options.ColorM.impl, mode, filter)
} }
// Bounds returns the bounds of the image. // Bounds returns the bounds of the image.
@ -475,7 +471,7 @@ func (i *Image) At(x, y int) color.Color {
if i.isDisposed() { if i.isDisposed() {
return color.RGBA{} return color.RGBA{}
} }
return i.shareableImages.level(0).At(x, y) return i.mipmap.level(0).At(x, y)
} }
// Dispose disposes the image data. After disposing, most of image functions do nothing and returns meaningless values. // Dispose disposes the image data. After disposing, most of image functions do nothing and returns meaningless values.
@ -490,7 +486,7 @@ func (i *Image) Dispose() error {
if i.isDisposed() { if i.isDisposed() {
return nil return nil
} }
i.shareableImages.dispose() i.mipmap.dispose()
runtime.SetFinalizer(i, nil) runtime.SetFinalizer(i, nil)
return nil return nil
} }
@ -511,7 +507,7 @@ func (i *Image) ReplacePixels(p []byte) error {
if i.isDisposed() { if i.isDisposed() {
return nil return nil
} }
i.shareableImages.level(0).ReplacePixels(p) i.mipmap.level(0).ReplacePixels(p)
i.disposeMipmaps() i.disposeMipmaps()
return nil return nil
} }
@ -576,7 +572,7 @@ type DrawImageOptions struct {
func NewImage(width, height int, filter Filter) (*Image, error) { func NewImage(width, height int, filter Filter) (*Image, error) {
s := shareable.NewImage(width, height) s := shareable.NewImage(width, height)
i := &Image{ i := &Image{
shareableImages: newShareableImages(s), mipmap: newMipmap(s),
filter: filter, filter: filter,
} }
i.addr = i i.addr = i
@ -599,7 +595,7 @@ func NewImage(width, height int, filter Filter) (*Image, error) {
// If width or height is less than 1 or more than device-dependent maximum size, newVolatileImage panics. // If width or height is less than 1 or more than device-dependent maximum size, newVolatileImage panics.
func newVolatileImage(width, height int) *Image { func newVolatileImage(width, height int) *Image {
i := &Image{ i := &Image{
shareableImages: newShareableImages(shareable.NewVolatileImage(width, height)), mipmap: newMipmap(shareable.NewVolatileImage(width, height)),
} }
i.addr = i i.addr = i
runtime.SetFinalizer(i, (*Image).Dispose) runtime.SetFinalizer(i, (*Image).Dispose)
@ -621,7 +617,7 @@ func NewImageFromImage(source image.Image, filter Filter) (*Image, error) {
s := shareable.NewImage(width, height) s := shareable.NewImage(width, height)
i := &Image{ i := &Image{
shareableImages: newShareableImages(s), mipmap: newMipmap(s),
filter: filter, filter: filter,
} }
i.addr = i i.addr = i
@ -633,7 +629,7 @@ func NewImageFromImage(source image.Image, filter Filter) (*Image, error) {
func newImageWithScreenFramebuffer(width, height int) *Image { func newImageWithScreenFramebuffer(width, height int) *Image {
i := &Image{ i := &Image{
shareableImages: newShareableImages(shareable.NewScreenFramebufferImage(width, height)), mipmap: newMipmap(shareable.NewScreenFramebufferImage(width, height)),
filter: FilterDefault, filter: FilterDefault,
} }
i.addr = i i.addr = i