restorable: Move 'putting random-colored dot' to restorable

This commit is contained in:
Hajime Hoshi 2018-04-05 11:15:24 +09:00
parent 810dc33b4a
commit 24e20306f2
2 changed files with 25 additions and 24 deletions

View File

@ -72,6 +72,20 @@ type Image struct {
screen bool
}
type rand struct {
x, y, z, w uint32
}
func (r *rand) next() uint32 {
// Xorshift: http://en.wikipedia.org/wiki/Xorshift
t := r.x ^ (r.x << 11)
r.x, r.y, r.z = r.y, r.z, r.w
r.w = (r.w ^ (r.w >> 19)) ^ (t ^ (t >> 8))
return r.w
}
var theRand = &rand{12345678, 4185243, 776511, 45411}
// NewImage creates an empty image with the given size.
func NewImage(width, height int, volatile bool) *Image {
i := &Image{
@ -80,6 +94,17 @@ func NewImage(width, height int, volatile bool) *Image {
}
theImages.add(i)
runtime.SetFinalizer(i, (*Image).Dispose)
// Put a random color pixel on newImg to make tests reliable
// (e.g. shareable_test.TestEnsureNotShared. This test might pass
// without this ReplacePixels even when the next DrawImage has a bug).
// This avoids to use remaining GPU memory state unexpectedly.
//
// TODO: Is it better clearing the image? How about the cost?
v := theRand.next()
r, g, b := uint8(v>>24), uint8(v>>16), uint8(v>>8)
i.ReplacePixels([]byte{r, g, b, 0xff}, 0, 0, 1, 1)
return i
}

View File

@ -87,20 +87,6 @@ type Image struct {
node *packing.Node
}
type rand struct {
x, y, z, w uint32
}
func (r *rand) next() uint32 {
// Xorshift: http://en.wikipedia.org/wiki/Xorshift
t := r.x ^ (r.x << 11)
r.x, r.y, r.z = r.y, r.z, r.w
r.w = (r.w ^ (r.w >> 19)) ^ (t ^ (t >> 8))
return r.w
}
var theRand = &rand{12345678, 4185243, 776511, 45411}
func (s *Image) ensureNotShared() {
if s.node == nil {
return
@ -108,16 +94,6 @@ func (s *Image) ensureNotShared() {
x, y, w, h := s.region()
newImg := restorable.NewImage(w, h, false)
// Put a random color pixel on newImg to make tests reliable.
//
// Of course this function works without this ReplacePixels,
// but the tests might pass without this even if there is a bug
// in DrawImage because of previous GPU memory state.
v := theRand.next()
r, g, b := uint8(v>>24), uint8(v>>16), uint8(v>>8)
newImg.ReplacePixels([]byte{r, g, b, 0xff}, 0, 0, 1, 1)
newImg.DrawImage(s.backend.restorable, x, y, x+w, y+h, nil, nil, opengl.CompositeModeCopy, graphics.FilterNearest)
s.dispose()