graphics: Add imageImpl and change Image to be a wrapper (emulating weak refs)

This commit is contained in:
Hajime Hoshi 2016-05-17 01:03:28 +09:00
parent 23863d5a2f
commit 7ab2efaa3f

253
image.go
View File

@ -64,34 +64,33 @@ func (t *delayedImageTasks) exec() error {
} }
type images struct { type images struct {
// TODO: This must be weak refs. Create a wrapper for *Image. images map[*imageImpl]struct{}
images map[*Image]struct{}
evacuated bool evacuated bool
m sync.Mutex m sync.Mutex
} }
var theImages = images{ var theImages = images{
images: map[*Image]struct{}{}, images: map[*imageImpl]struct{}{},
} }
func (i *images) add(img *Image) error { func (i *images) add(img *imageImpl) (*Image, error) {
i.m.Lock() i.m.Lock()
defer i.m.Unlock() defer i.m.Unlock()
if i.evacuated { if i.evacuated {
return errors.New("ebiten: images must not be evacuated") return nil, errors.New("ebiten: images must not be evacuated")
} }
i.images[img] = struct{}{} i.images[img] = struct{}{}
return nil eimg := &Image{img}
runtime.SetFinalizer(eimg, func(i *Image) {
theImages.remove(i)
})
return eimg, nil
} }
func (i *images) remove(img *Image) error { func (i *images) remove(img *Image) {
i.m.Lock() i.m.Lock()
defer i.m.Unlock() defer i.m.Unlock()
if i.evacuated { delete(i.images, img.impl)
return errors.New("ebiten: images must not be evacuated")
}
delete(i.images, img)
return nil
} }
func (i *images) isEvacuated() bool { func (i *images) isEvacuated() bool {
@ -134,57 +133,28 @@ func (i *images) restorePixels() error {
// The pixel format is alpha-premultiplied. // The pixel format is alpha-premultiplied.
// Image implements image.Image. // Image implements image.Image.
type Image struct { type Image struct {
framebuffer *graphics.Framebuffer impl *imageImpl
texture *graphics.Texture
defaultFramebuffer bool
disposed bool
evacuated bool
pixels []uint8
width int
height int
filter Filter
} }
// Size returns the size of the image. // Size returns the size of the image.
// //
// This function is concurrent-safe. // This function is concurrent-safe.
func (i *Image) Size() (width, height int) { func (i *Image) Size() (width, height int) {
return i.width, i.height return i.impl.Size()
} }
// Clear resets the pixels of the image into 0. // Clear resets the pixels of the image into 0.
// //
// This function is concurrent-safe. // This function is concurrent-safe.
func (i *Image) Clear() (err error) { func (i *Image) Clear() error {
return i.clear() return i.impl.Clear()
}
func (i *Image) clear() (err error) {
return i.fill(color.Transparent)
} }
// Fill fills the image with a solid color. // Fill fills the image with a solid color.
// //
// This function is concurrent-safe. // This function is concurrent-safe.
func (i *Image) Fill(clr color.Color) (err error) { func (i *Image) Fill(clr color.Color) error {
return i.fill(clr) return i.impl.Fill(clr)
}
func (i *Image) fill(clr color.Color) (err error) {
f := func() error {
imageM.Lock()
defer imageM.Unlock()
if i.isDisposed() {
return errors.New("ebiten: image is already disposed")
}
i.pixels = nil
return i.framebuffer.Fill(glContext, clr)
}
if theDelayedImageTasks.add(f) {
return nil
}
return f()
} }
// DrawImage draws the given image on the receiver image. // DrawImage draws the given image on the receiver image.
@ -204,7 +174,102 @@ func (i *Image) fill(clr color.Color) (err error) {
// It would be better if you could call this method fewer times. // It would be better if you could call this method fewer times.
// //
// This function is concurrent-safe. // This function is concurrent-safe.
func (i *Image) DrawImage(image *Image, options *DrawImageOptions) (err error) { func (i *Image) DrawImage(image *Image, options *DrawImageOptions) error {
return i.impl.DrawImage(image, options)
}
// Bounds returns the bounds of the image.
//
// This function is concurrent-safe.
func (i *Image) Bounds() image.Rectangle {
return i.impl.Bounds()
}
// ColorModel returns the color model of the image.
//
// This function is concurrent-safe.
func (i *Image) ColorModel() color.Model {
return i.impl.ColorModel()
}
// At returns the color of the image at (x, y).
//
// This method loads pixels from VRAM to system memory if necessary.
//
// This method can't be called before the main loop (ebiten.Run) starts (as of version 1.4.0-alpha).
//
// This function is concurrent-safe.
func (i *Image) At(x, y int) color.Color {
return i.impl.At(x, y)
}
// Dispose disposes the image data. After disposing, the image becomes invalid.
// This is useful to save memory.
//
// The behavior of any functions for a disposed image is undefined.
//
// This function is concurrent-safe.
func (i *Image) Dispose() error {
return i.impl.Dispose()
}
// ReplacePixels replaces the pixels of the image with p.
//
// The given p must represent RGBA pre-multiplied alpha values. len(p) must equal to 4 * (image width) * (image height).
//
// This function may be slow (as for implementation, this calls glTexSubImage2D).
//
// This function is concurrent-safe.
func (i *Image) ReplacePixels(p []uint8) error {
return i.impl.ReplacePixels(p)
}
type imageImpl struct {
framebuffer *graphics.Framebuffer
texture *graphics.Texture
defaultFramebuffer bool
disposed bool
evacuated bool
pixels []uint8
width int
height int
filter Filter
}
func (i *imageImpl) Size() (width, height int) {
return i.width, i.height
}
func (i *imageImpl) Clear() error {
return i.clear()
}
func (i *imageImpl) clear() error {
return i.fill(color.Transparent)
}
func (i *imageImpl) Fill(clr color.Color) (err error) {
return i.fill(clr)
}
func (i *imageImpl) fill(clr color.Color) (err error) {
f := func() error {
imageM.Lock()
defer imageM.Unlock()
if i.isDisposed() {
return errors.New("ebiten: image is already disposed")
}
i.pixels = nil
return i.framebuffer.Fill(glContext, clr)
}
if theDelayedImageTasks.add(f) {
return nil
}
return f()
}
func (i *imageImpl) DrawImage(image *Image, options *DrawImageOptions) error {
// Calculate vertices before locking because the user can do anything in // Calculate vertices before locking because the user can do anything in
// options.ImageParts interface without deadlock (e.g. Call Image functions). // options.ImageParts interface without deadlock (e.g. Call Image functions).
if options == nil { if options == nil {
@ -217,10 +282,10 @@ func (i *Image) DrawImage(image *Image, options *DrawImageOptions) (err error) {
if dparts != nil { if dparts != nil {
parts = imageParts(dparts) parts = imageParts(dparts)
} else { } else {
parts = &wholeImage{image.width, image.height} parts = &wholeImage{image.impl.width, image.impl.height}
} }
} }
quads := &textureQuads{parts: parts, width: image.width, height: image.height} quads := &textureQuads{parts: parts, width: image.impl.width, height: image.impl.height}
// TODO: Reuse one vertices instead of making here, but this would need locking. // TODO: Reuse one vertices instead of making here, but this would need locking.
vertices := make([]int16, parts.Len()*16) vertices := make([]int16, parts.Len()*16)
n := quads.vertices(vertices) n := quads.vertices(vertices)
@ -228,7 +293,7 @@ func (i *Image) DrawImage(image *Image, options *DrawImageOptions) (err error) {
return nil return nil
} }
if i == image { if i == image.impl {
return errors.New("ebiten: Image.DrawImage: image should be different from the receiver") return errors.New("ebiten: Image.DrawImage: image should be different from the receiver")
} }
f := func() error { f := func() error {
@ -239,7 +304,7 @@ func (i *Image) DrawImage(image *Image, options *DrawImageOptions) (err error) {
} }
i.pixels = nil i.pixels = nil
m := opengl.CompositeMode(options.CompositeMode) m := opengl.CompositeMode(options.CompositeMode)
return i.framebuffer.DrawTexture(glContext, image.texture, vertices[:16*n], &options.GeoM, &options.ColorM, m) return i.framebuffer.DrawTexture(glContext, image.impl.texture, vertices[:16*n], &options.GeoM, &options.ColorM, m)
} }
if theDelayedImageTasks.add(f) { if theDelayedImageTasks.add(f) {
return nil return nil
@ -247,28 +312,15 @@ func (i *Image) DrawImage(image *Image, options *DrawImageOptions) (err error) {
return f() return f()
} }
// Bounds returns the bounds of the image. func (i *imageImpl) Bounds() image.Rectangle {
//
// This function is concurrent-safe.
func (i *Image) Bounds() image.Rectangle {
return image.Rect(0, 0, i.width, i.height) return image.Rect(0, 0, i.width, i.height)
} }
// ColorModel returns the color model of the image. func (i *imageImpl) ColorModel() color.Model {
//
// This function is concurrent-safe.
func (i *Image) ColorModel() color.Model {
return color.RGBAModel return color.RGBAModel
} }
// At returns the color of the image at (x, y). func (i *imageImpl) At(x, y int) color.Color {
//
// This method loads pixels from VRAM to system memory if necessary.
//
// This method can't be called before the main loop (ebiten.Run) starts (as of version 1.4.0-alpha).
//
// This function is concurrent-safe.
func (i *Image) At(x, y int) color.Color {
if !currentRunContext.isRunning() { if !currentRunContext.isRunning() {
panic("ebiten: At can't be called when the GL context is not initialized (this panic happens as of version 1.4.0-alpha)") panic("ebiten: At can't be called when the GL context is not initialized (this panic happens as of version 1.4.0-alpha)")
} }
@ -289,7 +341,7 @@ func (i *Image) At(x, y int) color.Color {
return color.RGBA{r, g, b, a} return color.RGBA{r, g, b, a}
} }
func (i *Image) evacuatePixels() error { func (i *imageImpl) evacuatePixels() error {
imageM.Lock() imageM.Lock()
defer imageM.Unlock() defer imageM.Unlock()
defer func() { defer func() {
@ -323,7 +375,7 @@ func (i *Image) evacuatePixels() error {
return nil return nil
} }
func (i *Image) restorePixels() error { func (i *imageImpl) restorePixels() error {
imageM.Lock() imageM.Lock()
defer imageM.Unlock() defer imageM.Unlock()
defer func() { defer func() {
@ -360,13 +412,7 @@ func (i *Image) restorePixels() error {
return nil return nil
} }
// Dispose disposes the image data. After disposing, the image becomes invalid. func (i *imageImpl) Dispose() error {
// This is useful to save memory.
//
// The behavior of any functions for a disposed image is undefined.
//
// This function is concurrent-safe.
func (i *Image) Dispose() error {
f := func() error { f := func() error {
imageM.Lock() imageM.Lock()
defer imageM.Unlock() defer imageM.Unlock()
@ -387,9 +433,6 @@ func (i *Image) Dispose() error {
} }
i.disposed = true i.disposed = true
i.pixels = nil i.pixels = nil
if err := theImages.remove(i); err != nil {
return err
}
runtime.SetFinalizer(i, nil) runtime.SetFinalizer(i, nil)
return nil return nil
} }
@ -400,18 +443,11 @@ func (i *Image) Dispose() error {
return f() return f()
} }
func (i *Image) isDisposed() bool { func (i *imageImpl) isDisposed() bool {
return i.disposed return i.disposed
} }
// ReplacePixels replaces the pixels of the image with p. func (i *imageImpl) ReplacePixels(p []uint8) error {
//
// The given p must represent RGBA pre-multiplied alpha values. len(p) must equal to 4 * (image width) * (image height).
//
// This function may be slow (as for implementation, this calls glTexSubImage2D).
//
// This function is concurrent-safe.
func (i *Image) ReplacePixels(p []uint8) error {
if l := 4 * i.width * i.height; len(p) != l { if l := 4 * i.width * i.height; len(p) != l {
return fmt.Errorf("ebiten: p's length must be %d", l) return fmt.Errorf("ebiten: p's length must be %d", l)
} }
@ -448,12 +484,13 @@ type DrawImageOptions struct {
// //
// This function is concurrent-safe. // This function is concurrent-safe.
func NewImage(width, height int, filter Filter) (*Image, error) { func NewImage(width, height int, filter Filter) (*Image, error) {
image := &Image{ image := &imageImpl{
width: width, width: width,
height: height, height: height,
filter: filter, filter: filter,
} }
if err := theImages.add(image); err != nil { eimg, err := theImages.add(image)
if err != nil {
return nil, err return nil, err
} }
f := func() error { f := func() error {
@ -470,19 +507,19 @@ func NewImage(width, height int, filter Filter) (*Image, error) {
} }
image.framebuffer = framebuffer image.framebuffer = framebuffer
image.texture = texture image.texture = texture
runtime.SetFinalizer(image, (*Image).Dispose) runtime.SetFinalizer(image, (*imageImpl).Dispose)
if err := image.framebuffer.Fill(glContext, color.Transparent); err != nil { if err := image.framebuffer.Fill(glContext, color.Transparent); err != nil {
return err return err
} }
return nil return nil
} }
if theDelayedImageTasks.add(f) { if theDelayedImageTasks.add(f) {
return image, nil return eimg, nil
} }
if err := f(); err != nil { if err := f(); err != nil {
return nil, err return nil, err
} }
return image, nil return eimg, nil
} }
// NewImageFromImage creates a new image with the given image (img). // NewImageFromImage creates a new image with the given image (img).
@ -490,23 +527,24 @@ func NewImage(width, height int, filter Filter) (*Image, error) {
// NewImageFromImage generates a new texture and a new framebuffer. // NewImageFromImage generates a new texture and a new framebuffer.
// //
// This function is concurrent-safe. // This function is concurrent-safe.
func NewImageFromImage(img image.Image, filter Filter) (*Image, error) { func NewImageFromImage(source image.Image, filter Filter) (*Image, error) {
size := img.Bounds().Size() size := source.Bounds().Size()
w, h := size.X, size.Y w, h := size.X, size.Y
// TODO: Return error when the image is too big! // TODO: Return error when the image is too big!
eimg := &Image{ img := &imageImpl{
width: w, width: w,
height: h, height: h,
filter: filter, filter: filter,
} }
if err := theImages.add(eimg); err != nil { eimg, err := theImages.add(img)
if err != nil {
return nil, err return nil, err
} }
f := func() error { f := func() error {
// Don't lock while manipulating an image.Image interface. // Don't lock while manipulating an image.Image interface.
rgbaImg, ok := img.(*image.RGBA) rgbaImg, ok := source.(*image.RGBA)
if !ok { if !ok {
origImg := img origImg := source
newImg := image.NewRGBA(origImg.Bounds()) newImg := image.NewRGBA(origImg.Bounds())
draw.Draw(newImg, newImg.Bounds(), origImg, origImg.Bounds().Min, draw.Src) draw.Draw(newImg, newImg.Bounds(), origImg, origImg.Bounds().Min, draw.Src)
rgbaImg = newImg rgbaImg = newImg
@ -522,9 +560,9 @@ func NewImageFromImage(img image.Image, filter Filter) (*Image, error) {
// TODO: texture should be removed here? // TODO: texture should be removed here?
return err return err
} }
eimg.framebuffer = framebuffer img.framebuffer = framebuffer
eimg.texture = texture img.texture = texture
runtime.SetFinalizer(eimg, (*Image).Dispose) runtime.SetFinalizer(img, (*imageImpl).Dispose)
return nil return nil
} }
if theDelayedImageTasks.add(f) { if theDelayedImageTasks.add(f) {
@ -543,16 +581,17 @@ func newImageWithZeroFramebuffer(width, height int) (*Image, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
img := &Image{ img := &imageImpl{
framebuffer: f, framebuffer: f,
texture: nil, texture: nil,
width: width, width: width,
height: height, height: height,
defaultFramebuffer: true, defaultFramebuffer: true,
} }
if err := theImages.add(img); err != nil { eimg, err := theImages.add(img)
if err != nil {
return nil, err return nil, err
} }
runtime.SetFinalizer(img, (*Image).Dispose) runtime.SetFinalizer(img, (*imageImpl).Dispose)
return img, nil return eimg, nil
} }