restorable: Refactoring: Replace CopyPixels with NewImageFromImage

This commit is contained in:
Hajime Hoshi 2019-07-15 02:00:26 +09:00
parent 9e8b23f7dc
commit aee26eec1e
2 changed files with 20 additions and 17 deletions

View File

@ -143,6 +143,24 @@ func NewImage(width, height int) *Image {
return i
}
// NewImage creates a new image and copies the pixels of the given source image.
//
// The given size (width and height) doesn't have to match with the source image's size.
// The image is copied at the left-upper corner of the new image.
func NewImageFromImage(width, height int, src *Image) *Image {
i := NewImage(width, height)
// Do not use DrawTriangles here. ReplacePixels will be called on a part of newImg later, and it looked like
// ReplacePixels on a part of image deletes other region that are rendered by DrawTriangles (#593, #758).
i.image.CopyPixels(src.image)
// As pixels should not be obtained here, making the image stale is inevitable.
// TODO: Copy pixel data from the source instead of making this stale (#897).
i.makeStale()
return i
}
func (i *Image) MakeVolatile() {
i.volatile = true
}
@ -270,17 +288,6 @@ func (i *Image) makeStale() {
// the former image can be restored from the latest state of the latter image.
}
func (i *Image) CopyPixels(src *Image) {
// TODO: Avoid making other images stale if possible. (#514)
// For this purpuse, images should remember which part of that is used for DrawTriangles.
theImages.makeStaleIfDependingOn(i)
i.image.CopyPixels(src.image)
// As pixels should not be obtained here, making the image stale is inevitable.
i.makeStale()
}
// ClearPixels clears the specified region by ReplacePixels.
func (i *Image) ClearPixels(x, y, width, height int) {
// TODO: Allocating bytes for all pixels are wasteful. Allocate memory only for required regions (#897).

View File

@ -121,12 +121,8 @@ func (b *backend) TryAlloc(width, height int) (*packing.Node, bool) {
b.page.Extend()
}
s := b.page.Size()
newImg := restorable.NewImage(s, s)
oldImg := b.restorable
// Do not use DrawTriangles here. ReplacePixels will be called on a part of newImg later, and it looked like
// ReplacePixels on a part of image deletes other region that are rendered by DrawTriangles (#593, #758).
newImg.CopyPixels(oldImg)
oldImg.Dispose()
newImg := restorable.NewImageFromImage(s, s, b.restorable)
b.restorable.Dispose()
b.restorable = newImg
n := b.page.Alloc(width, height)