diff --git a/image.go b/image.go index fe73dc5c6..e23ae1f29 100644 --- a/image.go +++ b/image.go @@ -302,17 +302,9 @@ 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 { - return err - } - } - if i.framebuffer != nil { - // TODO: Ditto - if err := i.framebuffer.Dispose(context); err != nil { - return err - } + // TODO: As the texture is already disposed, is it correct to delete it here? + if err := graphics.Dispose(i.texture, i.framebuffer); err != nil { + return err } // TODO: Recalc i.pixels here img := image.NewRGBA(image.Rect(0, 0, i.width, i.height)) @@ -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 { - return err - } - i.framebuffer = nil - } - if i.texture != nil { - if err := i.texture.Dispose(ui.GLContext()); err != nil { - return err - } - i.texture = nil + if err := graphics.Dispose(i.texture, i.framebuffer); err != nil { + return err } + i.framebuffer = nil + i.texture = nil i.disposed = true i.pixels = nil runtime.SetFinalizer(i, nil) diff --git a/internal/graphics/command.go b/internal/graphics/command.go index 2d7e38ffd..d336d8fa4 100644 --- a/internal/graphics/command.go +++ b/internal/graphics/command.go @@ -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 +} diff --git a/internal/graphics/framebuffer.go b/internal/graphics/framebuffer.go index 08b3eb6b3..bdb048187 100644 --- a/internal/graphics/framebuffer.go +++ b/internal/graphics/framebuffer.go @@ -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 } diff --git a/internal/graphics/texture.go b/internal/graphics/texture.go index 8fb4ae42e..d7554ffd4 100644 --- a/internal/graphics/texture.go +++ b/internal/graphics/texture.go @@ -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 -}