mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-27 12:18:55 +01:00
669 lines
16 KiB
Go
669 lines
16 KiB
Go
// Copyright 2017 The Ebiten Authors
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package restorable_test
|
|
|
|
import (
|
|
"errors"
|
|
"image"
|
|
"image/color"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/hajimehoshi/ebiten"
|
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
|
. "github.com/hajimehoshi/ebiten/internal/restorable"
|
|
"github.com/hajimehoshi/ebiten/internal/testflock"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
testflock.Lock()
|
|
defer testflock.Unlock()
|
|
|
|
EnableRestoringForTesting()
|
|
code := 0
|
|
regularTermination := errors.New("regular termination")
|
|
f := func(screen *ebiten.Image) error {
|
|
code = m.Run()
|
|
return regularTermination
|
|
}
|
|
if err := ebiten.Run(f, 320, 240, 1, "Test"); err != nil && err != regularTermination {
|
|
panic(err)
|
|
}
|
|
os.Exit(code)
|
|
}
|
|
|
|
func pixelsToColor(p *Pixels, index int) color.RGBA {
|
|
i := index * 4
|
|
return color.RGBA{p.At(i), p.At(i + 1), p.At(i + 2), p.At(i + 3)}
|
|
}
|
|
|
|
func abs(x int) int {
|
|
if x < 0 {
|
|
return -x
|
|
}
|
|
return x
|
|
}
|
|
|
|
// sameColors compares c1 and c2 and returns a boolean value indicating
|
|
// if the two colors are (almost) same.
|
|
//
|
|
// Pixels read from GPU might include errors (#492), and
|
|
// sameColors considers such errors as delta.
|
|
func sameColors(c1, c2 color.RGBA, delta int) bool {
|
|
return abs(int(c1.R)-int(c2.R)) <= delta &&
|
|
abs(int(c1.G)-int(c2.G)) <= delta &&
|
|
abs(int(c1.B)-int(c2.B)) <= delta &&
|
|
abs(int(c1.A)-int(c2.A)) <= delta
|
|
}
|
|
|
|
func TestRestore(t *testing.T) {
|
|
img0 := NewImage(1, 1)
|
|
defer img0.Dispose()
|
|
|
|
clr0 := color.RGBA{0x00, 0x00, 0x00, 0xff}
|
|
img0.Fill(clr0.R, clr0.G, clr0.B, clr0.A)
|
|
ResolveStaleImages()
|
|
if err := Restore(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := clr0
|
|
got := pixelsToColor(img0.BasePixelsForTesting(), 0)
|
|
if !sameColors(got, want, 1) {
|
|
t.Errorf("got %v, want %v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestRestoreWithoutDraw(t *testing.T) {
|
|
img0 := NewImage(1024, 1024)
|
|
defer img0.Dispose()
|
|
|
|
// If there is no drawing command on img0, img0 is cleared when restored.
|
|
|
|
ResolveStaleImages()
|
|
if err := Restore(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
for i := 0; i < 1024*1024; i++ {
|
|
want := color.RGBA{0x00, 0x00, 0x00, 0x00}
|
|
got := pixelsToColor(img0.BasePixelsForTesting(), i)
|
|
if !sameColors(got, want, 0) {
|
|
t.Errorf("got %v, want %v", got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func quadVertices(src *Image, sw, sh, x, y int) []float32 {
|
|
return src.QuadVertices(0, 0, sw, sh,
|
|
1, 0, 0, 1, float32(x), float32(y),
|
|
1, 1, 1, 1)
|
|
}
|
|
|
|
func TestRestoreChain(t *testing.T) {
|
|
const num = 10
|
|
imgs := []*Image{}
|
|
for i := 0; i < num; i++ {
|
|
img := NewImage(1, 1)
|
|
imgs = append(imgs, img)
|
|
}
|
|
defer func() {
|
|
for _, img := range imgs {
|
|
img.Dispose()
|
|
}
|
|
}()
|
|
clr := color.RGBA{0x00, 0x00, 0x00, 0xff}
|
|
imgs[0].Fill(clr.R, clr.G, clr.B, clr.A)
|
|
for i := 0; i < num-1; i++ {
|
|
vs := quadVertices(imgs[i], 1, 1, 0, 0)
|
|
is := graphics.QuadIndices()
|
|
imgs[i+1].DrawImage(imgs[i], vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero)
|
|
}
|
|
ResolveStaleImages()
|
|
if err := Restore(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := clr
|
|
for i, img := range imgs {
|
|
got := pixelsToColor(img.BasePixelsForTesting(), 0)
|
|
if !sameColors(got, want, 1) {
|
|
t.Errorf("%d: got %v, want %v", i, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRestoreChain2(t *testing.T) {
|
|
const (
|
|
num = 10
|
|
w = 1
|
|
h = 1
|
|
)
|
|
imgs := []*Image{}
|
|
for i := 0; i < num; i++ {
|
|
img := NewImage(w, h)
|
|
imgs = append(imgs, img)
|
|
}
|
|
defer func() {
|
|
for _, img := range imgs {
|
|
img.Dispose()
|
|
}
|
|
}()
|
|
|
|
clr0 := color.RGBA{0xff, 0x00, 0x00, 0xff}
|
|
imgs[0].Fill(clr0.R, clr0.G, clr0.B, clr0.A)
|
|
clr7 := color.RGBA{0x00, 0xff, 0x00, 0xff}
|
|
imgs[7].Fill(clr7.R, clr7.G, clr7.B, clr7.A)
|
|
clr8 := color.RGBA{0x00, 0x00, 0xff, 0xff}
|
|
imgs[8].Fill(clr8.R, clr8.G, clr8.B, clr8.A)
|
|
|
|
is := graphics.QuadIndices()
|
|
imgs[8].DrawImage(imgs[7], quadVertices(imgs[7], w, h, 0, 0), is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero)
|
|
imgs[9].DrawImage(imgs[8], quadVertices(imgs[8], w, h, 0, 0), is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero)
|
|
for i := 0; i < 7; i++ {
|
|
imgs[i+1].DrawImage(imgs[i], quadVertices(imgs[i], w, h, 0, 0), is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero)
|
|
}
|
|
|
|
ResolveStaleImages()
|
|
if err := Restore(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i, img := range imgs {
|
|
want := clr0
|
|
if i == 8 || i == 9 {
|
|
want = clr7
|
|
}
|
|
got := pixelsToColor(img.BasePixelsForTesting(), 0)
|
|
if !sameColors(got, want, 1) {
|
|
t.Errorf("%d: got %v, want %v", i, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRestoreOverrideSource(t *testing.T) {
|
|
const (
|
|
w = 1
|
|
h = 1
|
|
)
|
|
img0 := NewImage(w, h)
|
|
img1 := NewImage(w, h)
|
|
img2 := NewImage(w, h)
|
|
img3 := NewImage(w, h)
|
|
defer func() {
|
|
img3.Dispose()
|
|
img2.Dispose()
|
|
img1.Dispose()
|
|
img0.Dispose()
|
|
}()
|
|
clr0 := color.RGBA{0x00, 0x00, 0x00, 0xff}
|
|
clr1 := color.RGBA{0x00, 0x00, 0x01, 0xff}
|
|
img1.Fill(clr0.R, clr0.G, clr0.B, clr0.A)
|
|
is := graphics.QuadIndices()
|
|
img2.DrawImage(img1, quadVertices(img1, w, h, 0, 0), is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest, graphics.AddressClampToZero)
|
|
img3.DrawImage(img2, quadVertices(img2, w, h, 0, 0), is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest, graphics.AddressClampToZero)
|
|
img0.Fill(clr1.R, clr1.G, clr1.B, clr1.A)
|
|
img1.DrawImage(img0, quadVertices(img0, w, h, 0, 0), is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest, graphics.AddressClampToZero)
|
|
ResolveStaleImages()
|
|
if err := Restore(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
testCases := []struct {
|
|
name string
|
|
want color.RGBA
|
|
got color.RGBA
|
|
}{
|
|
{
|
|
"0",
|
|
clr1,
|
|
pixelsToColor(img0.BasePixelsForTesting(), 0),
|
|
},
|
|
{
|
|
"1",
|
|
clr1,
|
|
pixelsToColor(img1.BasePixelsForTesting(), 0),
|
|
},
|
|
{
|
|
"2",
|
|
clr0,
|
|
pixelsToColor(img2.BasePixelsForTesting(), 0),
|
|
},
|
|
{
|
|
"3",
|
|
clr0,
|
|
pixelsToColor(img3.BasePixelsForTesting(), 0),
|
|
},
|
|
}
|
|
for _, c := range testCases {
|
|
if !sameColors(c.got, c.want, 1) {
|
|
t.Errorf("%s: got %v, want %v", c.name, c.got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRestoreComplexGraph(t *testing.T) {
|
|
const (
|
|
w = 4
|
|
h = 1
|
|
)
|
|
// 0 -> 3
|
|
// 1 -> 3
|
|
// 1 -> 4
|
|
// 2 -> 4
|
|
// 2 -> 7
|
|
// 3 -> 5
|
|
// 3 -> 6
|
|
// 3 -> 7
|
|
// 4 -> 6
|
|
base := image.NewRGBA(image.Rect(0, 0, w, h))
|
|
base.Pix[0] = 0xff
|
|
base.Pix[1] = 0xff
|
|
base.Pix[2] = 0xff
|
|
base.Pix[3] = 0xff
|
|
img0 := newImageFromImage(base)
|
|
img1 := newImageFromImage(base)
|
|
img2 := newImageFromImage(base)
|
|
img3 := NewImage(w, h)
|
|
img4 := NewImage(w, h)
|
|
img5 := NewImage(w, h)
|
|
img6 := NewImage(w, h)
|
|
img7 := NewImage(w, h)
|
|
defer func() {
|
|
img7.Dispose()
|
|
img6.Dispose()
|
|
img5.Dispose()
|
|
img4.Dispose()
|
|
img3.Dispose()
|
|
img2.Dispose()
|
|
img1.Dispose()
|
|
img0.Dispose()
|
|
}()
|
|
vs := quadVertices(img0, w, h, 0, 0)
|
|
is := graphics.QuadIndices()
|
|
img3.DrawImage(img0, vs, is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest, graphics.AddressClampToZero)
|
|
vs = quadVertices(img1, w, h, 1, 0)
|
|
img3.DrawImage(img1, vs, is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest, graphics.AddressClampToZero)
|
|
vs = quadVertices(img1, w, h, 1, 0)
|
|
img4.DrawImage(img1, vs, is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest, graphics.AddressClampToZero)
|
|
vs = quadVertices(img2, w, h, 2, 0)
|
|
img4.DrawImage(img2, vs, is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest, graphics.AddressClampToZero)
|
|
vs = quadVertices(img3, w, h, 0, 0)
|
|
img5.DrawImage(img3, vs, is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest, graphics.AddressClampToZero)
|
|
vs = quadVertices(img3, w, h, 0, 0)
|
|
img6.DrawImage(img3, vs, is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest, graphics.AddressClampToZero)
|
|
vs = quadVertices(img4, w, h, 1, 0)
|
|
img6.DrawImage(img4, vs, is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest, graphics.AddressClampToZero)
|
|
vs = quadVertices(img2, w, h, 0, 0)
|
|
img7.DrawImage(img2, vs, is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest, graphics.AddressClampToZero)
|
|
vs = quadVertices(img3, w, h, 2, 0)
|
|
img7.DrawImage(img3, vs, is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest, graphics.AddressClampToZero)
|
|
ResolveStaleImages()
|
|
if err := Restore(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
testCases := []struct {
|
|
name string
|
|
out string
|
|
image *Image
|
|
}{
|
|
{
|
|
"0",
|
|
"*---",
|
|
img0,
|
|
},
|
|
{
|
|
"1",
|
|
"*---",
|
|
img1,
|
|
},
|
|
{
|
|
"2",
|
|
"*---",
|
|
img2,
|
|
},
|
|
{
|
|
"3",
|
|
"**--",
|
|
img3,
|
|
},
|
|
{
|
|
"4",
|
|
"-**-",
|
|
img4,
|
|
},
|
|
{
|
|
"5",
|
|
"**--",
|
|
img5,
|
|
},
|
|
{
|
|
"6",
|
|
"****",
|
|
img6,
|
|
},
|
|
{
|
|
"7",
|
|
"*-**",
|
|
img7,
|
|
},
|
|
}
|
|
for _, c := range testCases {
|
|
for i := 0; i < 4; i++ {
|
|
want := color.RGBA{}
|
|
if c.out[i] == '*' {
|
|
want = color.RGBA{0xff, 0xff, 0xff, 0xff}
|
|
}
|
|
got := pixelsToColor(c.image.BasePixelsForTesting(), i)
|
|
if !sameColors(got, want, 1) {
|
|
t.Errorf("%s[%d]: got %v, want %v", c.name, i, got, want)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func newImageFromImage(rgba *image.RGBA) *Image {
|
|
s := rgba.Bounds().Size()
|
|
img := NewImage(s.X, s.Y)
|
|
img.ReplacePixels(rgba.Pix, 0, 0, s.X, s.Y)
|
|
return img
|
|
}
|
|
|
|
func TestRestoreRecursive(t *testing.T) {
|
|
const (
|
|
w = 4
|
|
h = 1
|
|
)
|
|
base := image.NewRGBA(image.Rect(0, 0, w, h))
|
|
base.Pix[0] = 0xff
|
|
base.Pix[1] = 0xff
|
|
base.Pix[2] = 0xff
|
|
base.Pix[3] = 0xff
|
|
|
|
img0 := newImageFromImage(base)
|
|
img1 := NewImage(w, h)
|
|
defer func() {
|
|
img1.Dispose()
|
|
img0.Dispose()
|
|
}()
|
|
is := graphics.QuadIndices()
|
|
img1.DrawImage(img0, quadVertices(img0, w, h, 1, 0), is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest, graphics.AddressClampToZero)
|
|
img0.DrawImage(img1, quadVertices(img1, w, h, 1, 0), is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest, graphics.AddressClampToZero)
|
|
ResolveStaleImages()
|
|
if err := Restore(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
testCases := []struct {
|
|
name string
|
|
out string
|
|
image *Image
|
|
}{
|
|
{
|
|
"0",
|
|
"*-*-",
|
|
img0,
|
|
},
|
|
{
|
|
"1",
|
|
"-*--",
|
|
img1,
|
|
},
|
|
}
|
|
for _, c := range testCases {
|
|
for i := 0; i < 4; i++ {
|
|
want := color.RGBA{}
|
|
if c.out[i] == '*' {
|
|
want = color.RGBA{0xff, 0xff, 0xff, 0xff}
|
|
}
|
|
got := pixelsToColor(c.image.BasePixelsForTesting(), i)
|
|
if !sameColors(got, want, 1) {
|
|
t.Errorf("%s[%d]: got %v, want %v", c.name, i, got, want)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestReplacePixels(t *testing.T) {
|
|
const (
|
|
w = 17
|
|
h = 31
|
|
)
|
|
|
|
img := NewImage(17, 31)
|
|
defer img.Dispose()
|
|
|
|
pix := make([]byte, 4*4*4)
|
|
for i := range pix {
|
|
pix[i] = 0xff
|
|
}
|
|
img.ReplacePixels(pix, 5, 7, 4, 4)
|
|
// Check the region (5, 7)-(9, 11). Outside state is indeterministic.
|
|
for j := 7; j < 11; j++ {
|
|
for i := 5; i < 9; i++ {
|
|
r, g, b, a := img.At(i, j)
|
|
got := color.RGBA{r, g, b, a}
|
|
want := color.RGBA{0xff, 0xff, 0xff, 0xff}
|
|
if got != want {
|
|
t.Errorf("img.At(%d, %d): got: %v, want: %v", i, j, got, want)
|
|
}
|
|
}
|
|
}
|
|
ResolveStaleImages()
|
|
if err := Restore(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for j := 7; j < 11; j++ {
|
|
for i := 5; i < 9; i++ {
|
|
r, g, b, a := img.At(i, j)
|
|
got := color.RGBA{r, g, b, a}
|
|
want := color.RGBA{0xff, 0xff, 0xff, 0xff}
|
|
if got != want {
|
|
t.Errorf("img.At(%d, %d): got: %v, want: %v", i, j, got, want)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDrawImageAndReplacePixels(t *testing.T) {
|
|
base := image.NewRGBA(image.Rect(0, 0, 1, 1))
|
|
base.Pix[0] = 0xff
|
|
base.Pix[1] = 0
|
|
base.Pix[2] = 0
|
|
base.Pix[3] = 0xff
|
|
img0 := newImageFromImage(base)
|
|
defer img0.Dispose()
|
|
img1 := NewImage(2, 1)
|
|
defer img1.Dispose()
|
|
|
|
vs := quadVertices(img0, 1, 1, 0, 0)
|
|
is := graphics.QuadIndices()
|
|
img1.DrawImage(img0, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero)
|
|
img1.ReplacePixels([]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 0, 0, 2, 1)
|
|
|
|
ResolveStaleImages()
|
|
if err := Restore(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r, g, b, a := img1.At(0, 0)
|
|
got := color.RGBA{r, g, b, a}
|
|
want := color.RGBA{0xff, 0xff, 0xff, 0xff}
|
|
if !sameColors(got, want, 1) {
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestDispose(t *testing.T) {
|
|
base0 := image.NewRGBA(image.Rect(0, 0, 1, 1))
|
|
img0 := newImageFromImage(base0)
|
|
defer img0.Dispose()
|
|
|
|
base1 := image.NewRGBA(image.Rect(0, 0, 1, 1))
|
|
img1 := newImageFromImage(base1)
|
|
|
|
base2 := image.NewRGBA(image.Rect(0, 0, 1, 1))
|
|
base2.Pix[0] = 0xff
|
|
base2.Pix[1] = 0xff
|
|
base2.Pix[2] = 0xff
|
|
base2.Pix[3] = 0xff
|
|
img2 := newImageFromImage(base2)
|
|
defer img2.Dispose()
|
|
|
|
is := graphics.QuadIndices()
|
|
img1.DrawImage(img2, quadVertices(img2, 1, 1, 0, 0), is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero)
|
|
img0.DrawImage(img1, quadVertices(img1, 1, 1, 0, 0), is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero)
|
|
img1.Dispose()
|
|
|
|
ResolveStaleImages()
|
|
if err := Restore(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r, g, b, a := img0.At(0, 0)
|
|
got := color.RGBA{r, g, b, a}
|
|
want := color.RGBA{0xff, 0xff, 0xff, 0xff}
|
|
if !sameColors(got, want, 1) {
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestClear(t *testing.T) {
|
|
pix := make([]uint8, 4*4*4)
|
|
for i := range pix {
|
|
pix[i] = 0xff
|
|
}
|
|
|
|
img := NewImage(4, 4)
|
|
img.ReplacePixels(pix, 0, 0, 4, 4)
|
|
// This doesn't make the image stale. Its base pixels are available.
|
|
img.ReplacePixels(nil, 1, 1, 2, 2)
|
|
|
|
cases := []struct {
|
|
Index int
|
|
Want color.RGBA
|
|
}{
|
|
{
|
|
Index: 0,
|
|
Want: color.RGBA{0xff, 0xff, 0xff, 0xff},
|
|
},
|
|
{
|
|
Index: 3,
|
|
Want: color.RGBA{0xff, 0xff, 0xff, 0xff},
|
|
},
|
|
{
|
|
Index: 4,
|
|
Want: color.RGBA{0xff, 0xff, 0xff, 0xff},
|
|
},
|
|
{
|
|
Index: 5,
|
|
Want: color.RGBA{0, 0, 0, 0},
|
|
},
|
|
{
|
|
Index: 7,
|
|
Want: color.RGBA{0xff, 0xff, 0xff, 0xff},
|
|
},
|
|
{
|
|
Index: 8,
|
|
Want: color.RGBA{0xff, 0xff, 0xff, 0xff},
|
|
},
|
|
{
|
|
Index: 10,
|
|
Want: color.RGBA{0, 0, 0, 0},
|
|
},
|
|
{
|
|
Index: 11,
|
|
Want: color.RGBA{0xff, 0xff, 0xff, 0xff},
|
|
},
|
|
{
|
|
Index: 12,
|
|
Want: color.RGBA{0xff, 0xff, 0xff, 0xff},
|
|
},
|
|
{
|
|
Index: 15,
|
|
Want: color.RGBA{0xff, 0xff, 0xff, 0xff},
|
|
},
|
|
}
|
|
for _, c := range cases {
|
|
got := pixelsToColor(img.BasePixelsForTesting(), c.Index)
|
|
want := c.Want
|
|
if got != want {
|
|
t.Errorf("base pixel [%d]: got %v, want %v", c.Index, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestReplacePixelsOnly(t *testing.T) {
|
|
const w, h = 128, 128
|
|
img0 := NewImage(w, h)
|
|
defer img0.Dispose()
|
|
img1 := NewImage(1, 1)
|
|
defer img1.Dispose()
|
|
|
|
for i := 0; i < w*h; i += 5 {
|
|
img0.ReplacePixels([]byte{1, 2, 3, 4}, i%w, i/w, 1, 1)
|
|
}
|
|
|
|
vs := quadVertices(img0, 1, 1, 0, 0)
|
|
is := graphics.QuadIndices()
|
|
img1.DrawImage(img0, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero)
|
|
img0.ReplacePixels([]byte{5, 6, 7, 8}, 0, 0, 1, 1)
|
|
|
|
// BasePixelsForTesting is available without GPU accessing.
|
|
for i := 0; i < w*h; i++ {
|
|
var want color.RGBA
|
|
switch {
|
|
case i == 0:
|
|
want = color.RGBA{5, 6, 7, 8}
|
|
case i%5 == 0:
|
|
want = color.RGBA{1, 2, 3, 4}
|
|
}
|
|
got := pixelsToColor(img0.BasePixelsForTesting(), i)
|
|
if !sameColors(got, want, 0) {
|
|
t.Errorf("got %v, want %v", got, want)
|
|
}
|
|
}
|
|
|
|
ResolveStaleImages()
|
|
if err := Restore(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := color.RGBA{1, 2, 3, 4}
|
|
got := pixelsToColor(img1.BasePixelsForTesting(), 0)
|
|
if !sameColors(got, want, 0) {
|
|
t.Errorf("got %v, want %v", got, want)
|
|
}
|
|
}
|
|
|
|
// TODO: How about volatile/screen images?
|
|
|
|
// Issue #793
|
|
func TestReadPixelsFromVolatileImage(t *testing.T) {
|
|
const w, h = 16, 16
|
|
dst := NewImage(w, h)
|
|
dst.MakeVolatile()
|
|
src := NewImage(w, h)
|
|
|
|
// First, make sure that dst has pixels
|
|
dst.ReplacePixels(make([]byte, 4*w*h), 0, 0, w, h)
|
|
|
|
// Second, draw src to dst. If the implementation is correct, dst becomes stale.
|
|
src.Fill(0xff, 0xff, 0xff, 0xff)
|
|
vs := quadVertices(src, 1, 1, 0, 0)
|
|
is := graphics.QuadIndices()
|
|
dst.DrawImage(src, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero)
|
|
|
|
// Read the pixels. If the implementation is correct, dst tries to read its pixels from GPU due to being
|
|
// stale.
|
|
want := byte(0xff)
|
|
got, _, _, _ := dst.At(0, 0)
|
|
if got != want {
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
}
|
|
}
|