restorable: Remove Fill

This commit is contained in:
Hajime Hoshi 2018-02-24 05:28:15 +09:00
parent a87f197c1b
commit 2ba89591db
2 changed files with 28 additions and 42 deletions

View File

@ -65,9 +65,7 @@ func (d *drawImageHistoryItem) canMerge(image *Image, colorm *affine.ColorM, mod
type Image struct { type Image struct {
image *graphics.Image image *graphics.Image
// baseImage and baseColor are exclusive.
basePixels []byte basePixels []byte
baseColor color.RGBA
// drawImageHistory is a set of draw-image commands. // drawImageHistory is a set of draw-image commands.
// TODO: This should be merged with the similar command queue in package graphics (#433). // TODO: This should be merged with the similar command queue in package graphics (#433).
@ -143,7 +141,6 @@ func (i *Image) Size() (int, int) {
// makeStale makes the image stale. // makeStale makes the image stale.
func (i *Image) makeStale() { func (i *Image) makeStale() {
i.basePixels = nil i.basePixels = nil
i.baseColor = color.RGBA{}
i.drawImageHistory = nil i.drawImageHistory = nil
i.stale = true i.stale = true
} }
@ -154,7 +151,6 @@ func (i *Image) clearIfVolatile() {
return return
} }
i.basePixels = nil i.basePixels = nil
i.baseColor = color.RGBA{}
i.drawImageHistory = nil i.drawImageHistory = nil
i.stale = false i.stale = false
if i.image == nil { if i.image == nil {
@ -163,22 +159,11 @@ func (i *Image) clearIfVolatile() {
i.image.Fill(0, 0, 0, 0) i.image.Fill(0, 0, 0, 0)
} }
// Fill fills the image with the given color.
func (i *Image) Fill(r, g, b, a uint8) {
theImages.makeStaleIfDependingOn(i)
i.basePixels = nil
i.baseColor = color.RGBA{r, g, b, a}
i.drawImageHistory = nil
i.stale = false
i.image.Fill(r, g, b, a)
}
// ReplacePixels replaces the image pixels with the given pixels slice. // ReplacePixels replaces the image pixels with the given pixels slice.
func (i *Image) ReplacePixels(pixels []byte) { func (i *Image) ReplacePixels(pixels []byte) {
theImages.makeStaleIfDependingOn(i) theImages.makeStaleIfDependingOn(i)
i.image.ReplacePixels(pixels) i.image.ReplacePixels(pixels)
i.basePixels = pixels i.basePixels = pixels
i.baseColor = color.RGBA{}
i.drawImageHistory = nil i.drawImageHistory = nil
i.stale = false i.stale = false
} }
@ -259,7 +244,6 @@ func (i *Image) readPixelsFromGPU(image *graphics.Image) error {
if err != nil { if err != nil {
return err return err
} }
i.baseColor = color.RGBA{}
i.drawImageHistory = nil i.drawImageHistory = nil
i.stale = false i.stale = false
return nil return nil
@ -314,7 +298,6 @@ func (i *Image) restore() error {
// be changed. // be changed.
i.image = graphics.NewScreenFramebufferImage(w, h, i.offsetX, i.offsetY) i.image = graphics.NewScreenFramebufferImage(w, h, i.offsetX, i.offsetY)
i.basePixels = nil i.basePixels = nil
i.baseColor = color.RGBA{}
i.drawImageHistory = nil i.drawImageHistory = nil
i.stale = false i.stale = false
return nil return nil
@ -322,7 +305,6 @@ func (i *Image) restore() error {
if i.volatile { if i.volatile {
i.image = graphics.NewImage(w, h) i.image = graphics.NewImage(w, h)
i.basePixels = nil i.basePixels = nil
i.baseColor = color.RGBA{}
i.drawImageHistory = nil i.drawImageHistory = nil
i.stale = false i.stale = false
return nil return nil
@ -339,12 +321,6 @@ func (i *Image) restore() error {
} }
} }
gimg := graphics.NewImageFromImage(img, w, h) gimg := graphics.NewImageFromImage(img, w, h)
if i.baseColor != (color.RGBA{}) {
if i.basePixels != nil {
panic("not reached")
}
gimg.Fill(i.baseColor.R, i.baseColor.G, i.baseColor.B, i.baseColor.A)
}
for _, c := range i.drawImageHistory { for _, c := range i.drawImageHistory {
// All dependencies must be already resolved. // All dependencies must be already resolved.
if c.image.hasDependency() { if c.image.hasDependency() {
@ -359,7 +335,6 @@ func (i *Image) restore() error {
if err != nil { if err != nil {
return err return err
} }
i.baseColor = color.RGBA{}
i.drawImageHistory = nil i.drawImageHistory = nil
i.stale = false i.stale = false
return nil return nil
@ -373,7 +348,6 @@ func (i *Image) Dispose() {
i.image.Dispose() i.image.Dispose()
i.image = nil i.image = nil
i.basePixels = nil i.basePixels = nil
i.baseColor = color.RGBA{}
i.drawImageHistory = nil i.drawImageHistory = nil
i.stale = false i.stale = false
theImages.remove(i) theImages.remove(i)

View File

@ -66,16 +66,28 @@ func sameColors(c1, c2 color.RGBA, delta int) bool {
abs(int(c1.A)-int(c2.A)) <= delta abs(int(c1.A)-int(c2.A)) <= delta
} }
func fill(img *Image, r, g, b, a uint8) {
w, h := img.Size()
pix := make([]uint8, w*h*4)
for i := 0; i < w*h; i++ {
pix[4*i] = r
pix[4*i+1] = g
pix[4*i+2] = b
pix[4*i+3] = a
}
img.ReplacePixels(pix)
}
func TestRestore(t *testing.T) { func TestRestore(t *testing.T) {
img0 := NewImage(1, 1, false) img0 := NewImage(1, 1, false)
// Clear images explicitly. // Clear images explicitly.
// In this 'restorable' layer, reused texture might not be cleared. // In this 'restorable' layer, reused texture might not be cleared.
img0.Fill(0, 0, 0, 0) fill(img0, 0, 0, 0, 0)
defer func() { defer func() {
img0.Dispose() img0.Dispose()
}() }()
clr0 := color.RGBA{0x00, 0x00, 0x00, 0xff} clr0 := color.RGBA{0x00, 0x00, 0x00, 0xff}
img0.Fill(clr0.R, clr0.G, clr0.B, clr0.A) fill(img0, clr0.R, clr0.G, clr0.B, clr0.A)
if err := ResolveStaleImages(); err != nil { if err := ResolveStaleImages(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -110,7 +122,7 @@ func TestRestoreChain(t *testing.T) {
imgs := []*Image{} imgs := []*Image{}
for i := 0; i < num; i++ { for i := 0; i < num; i++ {
img := NewImage(1, 1, false) img := NewImage(1, 1, false)
img.Fill(0, 0, 0, 0) fill(img, 0, 0, 0, 0)
imgs = append(imgs, img) imgs = append(imgs, img)
} }
defer func() { defer func() {
@ -119,7 +131,7 @@ func TestRestoreChain(t *testing.T) {
} }
}() }()
clr := color.RGBA{0x00, 0x00, 0x00, 0xff} clr := color.RGBA{0x00, 0x00, 0x00, 0xff}
imgs[0].Fill(clr.R, clr.G, clr.B, clr.A) fill(imgs[0], clr.R, clr.G, clr.B, clr.A)
for i := 0; i < num-1; i++ { for i := 0; i < num-1; i++ {
imgs[i+1].DrawImage(imgs[i], vertices(1, 1, 0, 0), &affine.ColorM{}, opengl.CompositeModeSourceOver, graphics.FilterNearest) imgs[i+1].DrawImage(imgs[i], vertices(1, 1, 0, 0), &affine.ColorM{}, opengl.CompositeModeSourceOver, graphics.FilterNearest)
} }
@ -140,13 +152,13 @@ func TestRestoreChain(t *testing.T) {
func TestRestoreOverrideSource(t *testing.T) { func TestRestoreOverrideSource(t *testing.T) {
img0 := NewImage(1, 1, false) img0 := NewImage(1, 1, false)
img0.Fill(0, 0, 0, 0) fill(img0, 0, 0, 0, 0)
img1 := NewImage(1, 1, false) img1 := NewImage(1, 1, false)
img1.Fill(0, 0, 0, 0) fill(img1, 0, 0, 0, 0)
img2 := NewImage(1, 1, false) img2 := NewImage(1, 1, false)
img2.Fill(0, 0, 0, 0) fill(img2, 0, 0, 0, 0)
img3 := NewImage(1, 1, false) img3 := NewImage(1, 1, false)
img3.Fill(0, 0, 0, 0) fill(img3, 0, 0, 0, 0)
defer func() { defer func() {
img3.Dispose() img3.Dispose()
img2.Dispose() img2.Dispose()
@ -155,10 +167,10 @@ func TestRestoreOverrideSource(t *testing.T) {
}() }()
clr0 := color.RGBA{0x00, 0x00, 0x00, 0xff} clr0 := color.RGBA{0x00, 0x00, 0x00, 0xff}
clr1 := color.RGBA{0x00, 0x00, 0x01, 0xff} clr1 := color.RGBA{0x00, 0x00, 0x01, 0xff}
img1.Fill(clr0.R, clr0.G, clr0.B, clr0.A) fill(img1, clr0.R, clr0.G, clr0.B, clr0.A)
img2.DrawImage(img1, vertices(1, 1, 0, 0), &affine.ColorM{}, opengl.CompositeModeSourceOver, graphics.FilterNearest) img2.DrawImage(img1, vertices(1, 1, 0, 0), &affine.ColorM{}, opengl.CompositeModeSourceOver, graphics.FilterNearest)
img3.DrawImage(img2, vertices(1, 1, 0, 0), &affine.ColorM{}, opengl.CompositeModeSourceOver, graphics.FilterNearest) img3.DrawImage(img2, vertices(1, 1, 0, 0), &affine.ColorM{}, opengl.CompositeModeSourceOver, graphics.FilterNearest)
img0.Fill(clr1.R, clr1.G, clr1.B, clr1.A) fill(img0, clr1.R, clr1.G, clr1.B, clr1.A)
img1.DrawImage(img0, vertices(1, 1, 0, 0), &affine.ColorM{}, opengl.CompositeModeSourceOver, graphics.FilterNearest) img1.DrawImage(img0, vertices(1, 1, 0, 0), &affine.ColorM{}, opengl.CompositeModeSourceOver, graphics.FilterNearest)
if err := ResolveStaleImages(); err != nil { if err := ResolveStaleImages(); err != nil {
t.Fatal(err) t.Fatal(err)
@ -218,15 +230,15 @@ func TestRestoreComplexGraph(t *testing.T) {
img1 := NewImageFromImage(base) img1 := NewImageFromImage(base)
img2 := NewImageFromImage(base) img2 := NewImageFromImage(base)
img3 := NewImage(4, 1, false) img3 := NewImage(4, 1, false)
img3.Fill(0, 0, 0, 0) fill(img3, 0, 0, 0, 0)
img4 := NewImage(4, 1, false) img4 := NewImage(4, 1, false)
img4.Fill(0, 0, 0, 0) fill(img4, 0, 0, 0, 0)
img5 := NewImage(4, 1, false) img5 := NewImage(4, 1, false)
img5.Fill(0, 0, 0, 0) fill(img5, 0, 0, 0, 0)
img6 := NewImage(4, 1, false) img6 := NewImage(4, 1, false)
img6.Fill(0, 0, 0, 0) fill(img6, 0, 0, 0, 0)
img7 := NewImage(4, 1, false) img7 := NewImage(4, 1, false)
img7.Fill(0, 0, 0, 0) fill(img7, 0, 0, 0, 0)
defer func() { defer func() {
img7.Dispose() img7.Dispose()
img6.Dispose() img6.Dispose()
@ -320,7 +332,7 @@ func TestRestoreRecursive(t *testing.T) {
base.Pix[3] = 0xff base.Pix[3] = 0xff
img0 := NewImageFromImage(base) img0 := NewImageFromImage(base)
img1 := NewImage(4, 1, false) img1 := NewImage(4, 1, false)
img1.Fill(0, 0, 0, 0) fill(img1, 0, 0, 0, 0)
defer func() { defer func() {
img1.Dispose() img1.Dispose()
img0.Dispose() img0.Dispose()