graphics: Add disposeCommand

This commit is contained in:
Hajime Hoshi 2016-06-11 22:52:07 +09:00
parent b59206b777
commit 2a58c095b8
4 changed files with 27 additions and 32 deletions

View File

@ -302,17 +302,9 @@ func (i *imageImpl) restorePixels(context *opengl.Context) error {
if i.disposed { if i.disposed {
return nil return nil
} }
if i.texture != nil { // TODO: As the texture is already disposed, is it correct to delete it here?
// TODO: As the texture is already disposed, is it correct to delete it here? if err := graphics.Dispose(i.texture, i.framebuffer); err != nil {
if err := i.texture.Dispose(context); err != nil { return err
return err
}
}
if i.framebuffer != nil {
// TODO: Ditto
if err := i.framebuffer.Dispose(context); err != nil {
return err
}
} }
// TODO: Recalc i.pixels here // TODO: Recalc i.pixels here
img := image.NewRGBA(image.Rect(0, 0, i.width, i.height)) img := image.NewRGBA(image.Rect(0, 0, i.width, i.height))
@ -338,18 +330,11 @@ func (i *imageImpl) Dispose() error {
if i.isDisposed() { if i.isDisposed() {
return errors.New("ebiten: image is already disposed") return errors.New("ebiten: image is already disposed")
} }
if i.framebuffer != nil { if err := graphics.Dispose(i.texture, i.framebuffer); err != nil {
if err := i.framebuffer.Dispose(ui.GLContext()); err != nil { return err
return err
}
i.framebuffer = nil
}
if i.texture != nil {
if err := i.texture.Dispose(ui.GLContext()); err != nil {
return err
}
i.texture = nil
} }
i.framebuffer = nil
i.texture = nil
i.disposed = true i.disposed = true
i.pixels = nil i.pixels = nil
runtime.SetFinalizer(i, nil) runtime.SetFinalizer(i, nil)

View File

@ -144,3 +144,18 @@ func (c *replacePixelsCommand) Exec(context *opengl.Context) error {
context.TexSubImage2D(c.pixels, c.texture.width, c.texture.height) context.TexSubImage2D(c.pixels, c.texture.width, c.texture.height)
return nil return nil
} }
type disposeCommand struct {
framebuffer *Framebuffer
texture *Texture
}
func (c *disposeCommand) Exec(context *opengl.Context) error {
if c.framebuffer != nil && c.framebuffer.native != opengl.ZeroFramebuffer {
context.DeleteFramebuffer(c.framebuffer.native)
}
if c.texture != nil {
context.DeleteTexture(c.texture.native)
}
return nil
}

View File

@ -64,12 +64,12 @@ func NewFramebufferFromTexture(c *opengl.Context, texture *Texture) (*Framebuffe
return f, nil return f, nil
} }
func (f *Framebuffer) Dispose(c *opengl.Context) error { func Dispose(texture *Texture, framebuffer *Framebuffer) error {
// Don't delete the default framebuffer. c := &disposeCommand{
if f.native == opengl.ZeroFramebuffer { framebuffer: framebuffer,
return nil texture: texture,
} }
c.DeleteFramebuffer(f.native) theCommandQueue.Enqueue(c)
return nil return nil
} }

View File

@ -82,8 +82,3 @@ func NewTextureFromImage(c *opengl.Context, img *image.RGBA, filter opengl.Filte
} }
return &Texture{native, origSize.X, origSize.Y}, nil return &Texture{native, origSize.X, origSize.Y}, nil
} }
func (t *Texture) Dispose(c *opengl.Context) error {
c.DeleteTexture(t.native)
return nil
}