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,18 +302,10 @@ func (i *imageImpl) restorePixels(context *opengl.Context) error {
if i.disposed {
return nil
}
if i.texture != nil {
// TODO: As the texture is already disposed, is it correct to delete it here?
if err := i.texture.Dispose(context); err != nil {
if err := graphics.Dispose(i.texture, i.framebuffer); err != nil {
return err
}
}
if i.framebuffer != nil {
// TODO: Ditto
if err := i.framebuffer.Dispose(context); err != nil {
return err
}
}
// TODO: Recalc i.pixels here
img := image.NewRGBA(image.Rect(0, 0, i.width, i.height))
for j := 0; j < i.height; j++ {
@ -338,18 +330,11 @@ func (i *imageImpl) Dispose() error {
if i.isDisposed() {
return errors.New("ebiten: image is already disposed")
}
if i.framebuffer != nil {
if err := i.framebuffer.Dispose(ui.GLContext()); err != nil {
if err := graphics.Dispose(i.texture, i.framebuffer); err != nil {
return err
}
i.framebuffer = nil
}
if i.texture != nil {
if err := i.texture.Dispose(ui.GLContext()); err != nil {
return err
}
i.texture = nil
}
i.disposed = true
i.pixels = 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)
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
}
func (f *Framebuffer) Dispose(c *opengl.Context) error {
// Don't delete the default framebuffer.
if f.native == opengl.ZeroFramebuffer {
return nil
func Dispose(texture *Texture, framebuffer *Framebuffer) error {
c := &disposeCommand{
framebuffer: framebuffer,
texture: texture,
}
c.DeleteFramebuffer(f.native)
theCommandQueue.Enqueue(c)
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
}
func (t *Texture) Dispose(c *opengl.Context) error {
c.DeleteTexture(t.native)
return nil
}