From 24e20306f2e522a5204cc9849453c27eefd47f56 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 5 Apr 2018 11:15:24 +0900 Subject: [PATCH] restorable: Move 'putting random-colored dot' to restorable --- internal/restorable/image.go | 25 +++++++++++++++++++++++++ internal/shareable/shareable.go | 24 ------------------------ 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/internal/restorable/image.go b/internal/restorable/image.go index d99a0149f..ee1698e1b 100644 --- a/internal/restorable/image.go +++ b/internal/restorable/image.go @@ -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 } diff --git a/internal/shareable/shareable.go b/internal/shareable/shareable.go index a4b79789e..953e31ce8 100644 --- a/internal/shareable/shareable.go +++ b/internal/shareable/shareable.go @@ -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()