Made ownership of textures more explicit

This commit is contained in:
Sasha Koshka 2023-09-04 12:21:17 -04:00
parent 7b28419432
commit 6ac653adb6
3 changed files with 7 additions and 17 deletions

View File

@ -19,7 +19,7 @@ type Backend interface {
// NewTexture creates a new texture from an image. The backend must
// reject any texture that was not made by it.
NewTexture (image.Image) canvas.Texture
NewTexture (image.Image) canvas.TextureCloser
// Run runs the event loop until Stop() is called, or the backend
// experiences a fatal error.

View File

@ -5,26 +5,16 @@ import "image"
// Texture is a handle that points to a 2D raster image managed by the backend.
type Texture interface {
io.Closer
image.Image
// Clip returns a smaller section of this texture, pointing to the same
// internal data. Becaue of this, closing a clipped section will close
// the original texture as well.
// internal data.
Clip (image.Rectangle) Texture
}
type protectedTexture struct {
// TextureCloser is a texture that can be closed. Anything that receives a
// TextureCloser must close it after use.
type TextureCloser interface {
Texture
}
func (protectedTexture) Close () error {
return nil
}
// Protect makes the Close() method of a texture do nothing. This is useful if
// several of the same texture are given out to different objects, but only one
// has the responsibility of closing it.
func Protect (texture Texture) Texture {
return protectedTexture { Texture: texture }
io.Closer
}

View File

@ -83,7 +83,7 @@ func NewContainerBox () ContainerBox {
// NewTexture creates a new texture from an image. When no longer in use, it
// must be freed using Close().
func NewTexture (source image.Image) canvas.Texture {
func NewTexture (source image.Image) canvas.TextureCloser {
assertBackend()
return backend.NewTexture(source)
}