2017-05-31 18:27:56 +02:00
|
|
|
// 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 (
|
2017-06-01 19:13:18 +02:00
|
|
|
"image"
|
2017-05-31 18:27:56 +02:00
|
|
|
"image/color"
|
|
|
|
"testing"
|
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
2022-02-06 12:41:32 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
2021-10-02 12:58:48 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/restorable"
|
2022-02-27 12:11:27 +01:00
|
|
|
etesting "github.com/hajimehoshi/ebiten/v2/internal/testing"
|
2022-03-19 15:55:14 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/ui"
|
2017-05-31 18:27:56 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2021-10-02 12:58:48 +02:00
|
|
|
restorable.EnableRestoringForTesting()
|
2022-02-27 12:11:27 +01:00
|
|
|
etesting.MainWithRunLoop(m)
|
2017-05-31 18:27:56 +02:00
|
|
|
}
|
|
|
|
|
2022-08-05 14:40:38 +02:00
|
|
|
func pixelsToColor(p *restorable.Pixels, i, j, imageWidth, imageHeight int) color.RGBA {
|
|
|
|
var pix [4]byte
|
2023-04-27 17:12:42 +02:00
|
|
|
p.ReadPixels(pix[:], image.Rect(i, j, i+1, j+1), imageWidth, imageHeight)
|
2023-01-28 11:06:38 +01:00
|
|
|
return color.RGBA{R: pix[0], G: pix[1], B: pix[2], A: pix[3]}
|
2017-05-31 18:27:56 +02:00
|
|
|
}
|
|
|
|
|
2018-02-03 08:43:18 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-05-31 18:27:56 +02:00
|
|
|
func TestRestore(t *testing.T) {
|
2022-06-06 02:29:30 +02:00
|
|
|
img0 := restorable.NewImage(1, 1, restorable.ImageTypeRegular)
|
2018-04-05 19:15:49 +02:00
|
|
|
defer img0.Dispose()
|
|
|
|
|
2023-01-28 11:06:38 +01:00
|
|
|
clr0 := color.RGBA{A: 0xff}
|
2023-04-27 17:12:42 +02:00
|
|
|
img0.WritePixels([]byte{clr0.R, clr0.G, clr0.B, clr0.A}, image.Rect(0, 0, 1, 1))
|
2023-05-07 18:06:14 +02:00
|
|
|
if err := restorable.ResolveStaleImages(ui.GraphicsDriverForTesting()); err != nil {
|
2020-01-18 17:18:56 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-03-19 15:55:14 +01:00
|
|
|
if err := restorable.RestoreIfNeeded(ui.GraphicsDriverForTesting()); err != nil {
|
2017-05-31 18:27:56 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
want := clr0
|
2022-08-05 14:40:38 +02:00
|
|
|
got := pixelsToColor(img0.BasePixelsForTesting(), 0, 0, 1, 1)
|
2018-02-03 08:43:18 +01:00
|
|
|
if !sameColors(got, want, 1) {
|
2017-05-31 18:27:56 +02:00
|
|
|
t.Errorf("got %v, want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 03:08:38 +01:00
|
|
|
func TestRestoreWithoutDraw(t *testing.T) {
|
2022-06-06 02:29:30 +02:00
|
|
|
img0 := restorable.NewImage(1024, 1024, restorable.ImageTypeRegular)
|
2018-10-30 03:08:38 +01:00
|
|
|
defer img0.Dispose()
|
|
|
|
|
|
|
|
// If there is no drawing command on img0, img0 is cleared when restored.
|
|
|
|
|
2023-05-07 18:06:14 +02:00
|
|
|
if err := restorable.ResolveStaleImages(ui.GraphicsDriverForTesting()); err != nil {
|
2020-01-18 17:18:56 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-03-19 15:55:14 +01:00
|
|
|
if err := restorable.RestoreIfNeeded(ui.GraphicsDriverForTesting()); err != nil {
|
2018-10-30 03:08:38 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-07-17 16:03:14 +02:00
|
|
|
for j := 0; j < 1024; j++ {
|
|
|
|
for i := 0; i < 1024; i++ {
|
2023-01-28 11:06:38 +01:00
|
|
|
want := color.RGBA{}
|
2022-08-05 14:40:38 +02:00
|
|
|
got := pixelsToColor(img0.BasePixelsForTesting(), i, j, 1024, 1024)
|
2019-07-17 16:03:14 +02:00
|
|
|
if !sameColors(got, want, 0) {
|
|
|
|
t.Errorf("got %v, want %v", got, want)
|
|
|
|
}
|
2018-10-30 03:08:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-16 11:56:14 +02:00
|
|
|
func quadVertices(sw, sh, x, y int) []float32 {
|
2019-09-19 18:55:18 +02:00
|
|
|
dx0 := float32(x)
|
|
|
|
dy0 := float32(y)
|
|
|
|
dx1 := float32(x + sw)
|
|
|
|
dy1 := float32(y + sh)
|
|
|
|
sx0 := float32(0)
|
|
|
|
sy0 := float32(0)
|
|
|
|
sx1 := float32(sw)
|
|
|
|
sy1 := float32(sh)
|
|
|
|
return []float32{
|
2023-04-16 11:56:14 +02:00
|
|
|
dx0, dy0, sx0, sy0, 1, 1, 1, 1,
|
|
|
|
dx1, dy0, sx1, sy0, 1, 1, 1, 1,
|
|
|
|
dx0, dy1, sx0, sy1, 1, 1, 1, 1,
|
|
|
|
dx1, dy1, sx1, sy1, 1, 1, 1, 1,
|
2019-09-19 18:55:18 +02:00
|
|
|
}
|
2019-02-14 13:04:34 +01:00
|
|
|
}
|
|
|
|
|
2017-06-01 03:44:28 +02:00
|
|
|
func TestRestoreChain(t *testing.T) {
|
|
|
|
const num = 10
|
2021-10-02 12:58:48 +02:00
|
|
|
imgs := []*restorable.Image{}
|
2017-06-01 03:44:28 +02:00
|
|
|
for i := 0; i < num; i++ {
|
2022-06-06 02:29:30 +02:00
|
|
|
img := restorable.NewImage(1, 1, restorable.ImageTypeRegular)
|
2017-06-01 19:13:18 +02:00
|
|
|
imgs = append(imgs, img)
|
2017-06-01 03:44:28 +02:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
for _, img := range imgs {
|
|
|
|
img.Dispose()
|
|
|
|
}
|
|
|
|
}()
|
2023-01-28 11:06:38 +01:00
|
|
|
clr := color.RGBA{A: 0xff}
|
2023-04-27 17:12:42 +02:00
|
|
|
imgs[0].WritePixels([]byte{clr.R, clr.G, clr.B, clr.A}, image.Rect(0, 0, 1, 1))
|
2017-06-01 03:44:28 +02:00
|
|
|
for i := 0; i < num-1; i++ {
|
2023-04-16 11:56:14 +02:00
|
|
|
vs := quadVertices(1, 1, 0, 0)
|
2018-10-28 15:03:06 +01:00
|
|
|
is := graphics.QuadIndices()
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, 1, 1)
|
|
|
|
imgs[i+1].DrawTriangles([graphics.ShaderImageCount]*restorable.Image{imgs[i]}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
2017-06-01 03:44:28 +02:00
|
|
|
}
|
2023-05-07 18:06:14 +02:00
|
|
|
if err := restorable.ResolveStaleImages(ui.GraphicsDriverForTesting()); err != nil {
|
2020-01-18 17:18:56 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-03-19 15:55:14 +01:00
|
|
|
if err := restorable.RestoreIfNeeded(ui.GraphicsDriverForTesting()); err != nil {
|
2017-06-01 03:44:28 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
want := clr
|
|
|
|
for i, img := range imgs {
|
2022-08-05 14:40:38 +02:00
|
|
|
got := pixelsToColor(img.BasePixelsForTesting(), 0, 0, 1, 1)
|
2018-02-03 08:43:18 +01:00
|
|
|
if !sameColors(got, want, 1) {
|
2017-06-01 03:44:28 +02:00
|
|
|
t.Errorf("%d: got %v, want %v", i, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-25 12:42:44 +02:00
|
|
|
func TestRestoreChain2(t *testing.T) {
|
2018-06-10 15:59:00 +02:00
|
|
|
const (
|
|
|
|
num = 10
|
|
|
|
w = 1
|
|
|
|
h = 1
|
|
|
|
)
|
2021-10-02 12:58:48 +02:00
|
|
|
imgs := []*restorable.Image{}
|
2018-03-25 12:42:44 +02:00
|
|
|
for i := 0; i < num; i++ {
|
2022-06-06 02:29:30 +02:00
|
|
|
img := restorable.NewImage(w, h, restorable.ImageTypeRegular)
|
2018-03-25 12:42:44 +02:00
|
|
|
imgs = append(imgs, img)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
for _, img := range imgs {
|
|
|
|
img.Dispose()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-01-28 11:06:38 +01:00
|
|
|
clr0 := color.RGBA{R: 0xff, A: 0xff}
|
2023-04-27 17:12:42 +02:00
|
|
|
imgs[0].WritePixels([]byte{clr0.R, clr0.G, clr0.B, clr0.A}, image.Rect(0, 0, w, h))
|
2023-01-28 11:06:38 +01:00
|
|
|
clr7 := color.RGBA{G: 0xff, A: 0xff}
|
2023-04-27 17:12:42 +02:00
|
|
|
imgs[7].WritePixels([]byte{clr7.R, clr7.G, clr7.B, clr7.A}, image.Rect(0, 0, w, h))
|
2023-01-28 11:06:38 +01:00
|
|
|
clr8 := color.RGBA{B: 0xff, A: 0xff}
|
2023-04-27 17:12:42 +02:00
|
|
|
imgs[8].WritePixels([]byte{clr8.R, clr8.G, clr8.B, clr8.A}, image.Rect(0, 0, w, h))
|
2018-03-25 12:42:44 +02:00
|
|
|
|
2018-10-28 15:03:06 +01:00
|
|
|
is := graphics.QuadIndices()
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, w, h)
|
|
|
|
imgs[8].DrawTriangles([graphics.ShaderImageCount]*restorable.Image{imgs[7]}, quadVertices(w, h, 0, 0), is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
|
|
|
imgs[9].DrawTriangles([graphics.ShaderImageCount]*restorable.Image{imgs[8]}, quadVertices(w, h, 0, 0), is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
2018-03-25 12:42:44 +02:00
|
|
|
for i := 0; i < 7; i++ {
|
2023-09-28 07:29:55 +02:00
|
|
|
imgs[i+1].DrawTriangles([graphics.ShaderImageCount]*restorable.Image{imgs[i]}, quadVertices(w, h, 0, 0), is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
2018-03-25 12:42:44 +02:00
|
|
|
}
|
|
|
|
|
2023-05-07 18:06:14 +02:00
|
|
|
if err := restorable.ResolveStaleImages(ui.GraphicsDriverForTesting()); err != nil {
|
2020-01-18 17:18:56 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-03-19 15:55:14 +01:00
|
|
|
if err := restorable.RestoreIfNeeded(ui.GraphicsDriverForTesting()); err != nil {
|
2018-03-25 12:42:44 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for i, img := range imgs {
|
|
|
|
want := clr0
|
|
|
|
if i == 8 || i == 9 {
|
|
|
|
want = clr7
|
|
|
|
}
|
2022-08-05 14:40:38 +02:00
|
|
|
got := pixelsToColor(img.BasePixelsForTesting(), 0, 0, w, h)
|
2018-03-25 12:42:44 +02:00
|
|
|
if !sameColors(got, want, 1) {
|
|
|
|
t.Errorf("%d: got %v, want %v", i, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 19:37:21 +02:00
|
|
|
func TestRestoreOverrideSource(t *testing.T) {
|
2018-06-10 15:59:00 +02:00
|
|
|
const (
|
|
|
|
w = 1
|
|
|
|
h = 1
|
|
|
|
)
|
2022-06-06 02:29:30 +02:00
|
|
|
img0 := restorable.NewImage(w, h, restorable.ImageTypeRegular)
|
|
|
|
img1 := restorable.NewImage(w, h, restorable.ImageTypeRegular)
|
|
|
|
img2 := restorable.NewImage(w, h, restorable.ImageTypeRegular)
|
|
|
|
img3 := restorable.NewImage(w, h, restorable.ImageTypeRegular)
|
2017-05-31 18:27:56 +02:00
|
|
|
defer func() {
|
|
|
|
img3.Dispose()
|
|
|
|
img2.Dispose()
|
|
|
|
img1.Dispose()
|
|
|
|
img0.Dispose()
|
|
|
|
}()
|
2023-01-28 11:06:38 +01:00
|
|
|
clr0 := color.RGBA{A: 0xff}
|
|
|
|
clr1 := color.RGBA{B: 0x01, A: 0xff}
|
2023-04-27 17:12:42 +02:00
|
|
|
img1.WritePixels([]byte{clr0.R, clr0.G, clr0.B, clr0.A}, image.Rect(0, 0, w, h))
|
2018-10-28 15:03:06 +01:00
|
|
|
is := graphics.QuadIndices()
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, w, h)
|
|
|
|
img2.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{img1}, quadVertices(w, h, 0, 0), is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
|
|
|
img3.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{img2}, quadVertices(w, h, 0, 0), is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
2023-04-27 17:12:42 +02:00
|
|
|
img0.WritePixels([]byte{clr1.R, clr1.G, clr1.B, clr1.A}, image.Rect(0, 0, w, h))
|
2023-09-28 07:29:55 +02:00
|
|
|
img1.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{img0}, quadVertices(w, h, 0, 0), is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
2023-05-07 18:06:14 +02:00
|
|
|
if err := restorable.ResolveStaleImages(ui.GraphicsDriverForTesting()); err != nil {
|
2020-01-18 17:18:56 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-03-19 15:55:14 +01:00
|
|
|
if err := restorable.RestoreIfNeeded(ui.GraphicsDriverForTesting()); err != nil {
|
2017-05-31 18:27:56 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
want color.RGBA
|
|
|
|
got color.RGBA
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"0",
|
|
|
|
clr1,
|
2022-08-05 14:40:38 +02:00
|
|
|
pixelsToColor(img0.BasePixelsForTesting(), 0, 0, w, h),
|
2017-05-31 18:27:56 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"1",
|
|
|
|
clr1,
|
2022-08-05 14:40:38 +02:00
|
|
|
pixelsToColor(img1.BasePixelsForTesting(), 0, 0, w, h),
|
2017-05-31 18:27:56 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"2",
|
|
|
|
clr0,
|
2022-08-05 14:40:38 +02:00
|
|
|
pixelsToColor(img2.BasePixelsForTesting(), 0, 0, w, h),
|
2017-05-31 18:27:56 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"3",
|
|
|
|
clr0,
|
2022-08-05 14:40:38 +02:00
|
|
|
pixelsToColor(img3.BasePixelsForTesting(), 0, 0, w, h),
|
2017-05-31 18:27:56 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range testCases {
|
2018-02-03 08:43:18 +01:00
|
|
|
if !sameColors(c.got, c.want, 1) {
|
2017-05-31 18:27:56 +02:00
|
|
|
t.Errorf("%s: got %v, want %v", c.name, c.got, c.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-31 19:37:21 +02:00
|
|
|
|
2017-06-02 14:58:26 +02:00
|
|
|
func TestRestoreComplexGraph(t *testing.T) {
|
2018-06-10 15:59:00 +02:00
|
|
|
const (
|
|
|
|
w = 4
|
|
|
|
h = 1
|
|
|
|
)
|
2017-06-02 14:58:26 +02:00
|
|
|
// 0 -> 3
|
|
|
|
// 1 -> 3
|
|
|
|
// 1 -> 4
|
|
|
|
// 2 -> 4
|
|
|
|
// 2 -> 7
|
|
|
|
// 3 -> 5
|
|
|
|
// 3 -> 6
|
|
|
|
// 3 -> 7
|
|
|
|
// 4 -> 6
|
2018-06-10 15:59:00 +02:00
|
|
|
base := image.NewRGBA(image.Rect(0, 0, w, h))
|
2017-06-01 19:13:18 +02:00
|
|
|
base.Pix[0] = 0xff
|
|
|
|
base.Pix[1] = 0xff
|
|
|
|
base.Pix[2] = 0xff
|
|
|
|
base.Pix[3] = 0xff
|
2018-02-25 15:13:06 +01:00
|
|
|
img0 := newImageFromImage(base)
|
|
|
|
img1 := newImageFromImage(base)
|
|
|
|
img2 := newImageFromImage(base)
|
2022-06-06 02:29:30 +02:00
|
|
|
img3 := restorable.NewImage(w, h, restorable.ImageTypeRegular)
|
|
|
|
img4 := restorable.NewImage(w, h, restorable.ImageTypeRegular)
|
|
|
|
img5 := restorable.NewImage(w, h, restorable.ImageTypeRegular)
|
|
|
|
img6 := restorable.NewImage(w, h, restorable.ImageTypeRegular)
|
|
|
|
img7 := restorable.NewImage(w, h, restorable.ImageTypeRegular)
|
2017-05-31 19:37:21 +02:00
|
|
|
defer func() {
|
2017-06-02 14:58:26 +02:00
|
|
|
img7.Dispose()
|
|
|
|
img6.Dispose()
|
|
|
|
img5.Dispose()
|
|
|
|
img4.Dispose()
|
|
|
|
img3.Dispose()
|
|
|
|
img2.Dispose()
|
2017-05-31 19:37:21 +02:00
|
|
|
img1.Dispose()
|
|
|
|
img0.Dispose()
|
|
|
|
}()
|
2018-10-28 15:03:06 +01:00
|
|
|
is := graphics.QuadIndices()
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, w, h)
|
2023-04-16 11:56:14 +02:00
|
|
|
vs := quadVertices(w, h, 0, 0)
|
2023-09-28 07:29:55 +02:00
|
|
|
img3.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{img0}, vs, is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
2023-04-16 11:56:14 +02:00
|
|
|
vs = quadVertices(w, h, 1, 0)
|
2023-09-28 07:29:55 +02:00
|
|
|
img3.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{img1}, vs, is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
2023-04-16 11:56:14 +02:00
|
|
|
vs = quadVertices(w, h, 1, 0)
|
2023-09-28 07:29:55 +02:00
|
|
|
img4.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{img1}, vs, is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
2023-04-16 11:56:14 +02:00
|
|
|
vs = quadVertices(w, h, 2, 0)
|
2023-09-28 07:29:55 +02:00
|
|
|
img4.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{img2}, vs, is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
2023-04-16 11:56:14 +02:00
|
|
|
vs = quadVertices(w, h, 0, 0)
|
2023-09-28 07:29:55 +02:00
|
|
|
img5.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{img3}, vs, is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
2023-04-16 11:56:14 +02:00
|
|
|
vs = quadVertices(w, h, 0, 0)
|
2023-09-28 07:29:55 +02:00
|
|
|
img6.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{img3}, vs, is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
2023-04-16 11:56:14 +02:00
|
|
|
vs = quadVertices(w, h, 1, 0)
|
2023-09-28 07:29:55 +02:00
|
|
|
img6.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{img4}, vs, is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
2023-04-16 11:56:14 +02:00
|
|
|
vs = quadVertices(w, h, 0, 0)
|
2023-09-28 07:29:55 +02:00
|
|
|
img7.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{img2}, vs, is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
2023-04-16 11:56:14 +02:00
|
|
|
vs = quadVertices(w, h, 2, 0)
|
2023-09-28 07:29:55 +02:00
|
|
|
img7.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{img3}, vs, is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
2023-05-07 18:06:14 +02:00
|
|
|
if err := restorable.ResolveStaleImages(ui.GraphicsDriverForTesting()); err != nil {
|
2020-01-18 17:18:56 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-03-19 15:55:14 +01:00
|
|
|
if err := restorable.RestoreIfNeeded(ui.GraphicsDriverForTesting()); err != nil {
|
2017-05-31 19:37:21 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
testCases := []struct {
|
2017-06-02 14:58:26 +02:00
|
|
|
name string
|
|
|
|
out string
|
2021-10-02 12:58:48 +02:00
|
|
|
image *restorable.Image
|
2017-05-31 19:37:21 +02:00
|
|
|
}{
|
|
|
|
{
|
2017-06-02 14:58:26 +02:00
|
|
|
"0",
|
|
|
|
"*---",
|
|
|
|
img0,
|
2017-05-31 19:37:21 +02:00
|
|
|
},
|
|
|
|
{
|
2017-06-02 14:58:26 +02:00
|
|
|
"1",
|
|
|
|
"*---",
|
|
|
|
img1,
|
2017-06-01 19:13:18 +02:00
|
|
|
},
|
|
|
|
{
|
2017-06-02 15:00:02 +02:00
|
|
|
"2",
|
2017-06-02 14:58:26 +02:00
|
|
|
"*---",
|
|
|
|
img2,
|
2017-06-01 19:13:18 +02:00
|
|
|
},
|
|
|
|
{
|
2017-06-02 14:58:26 +02:00
|
|
|
"3",
|
|
|
|
"**--",
|
|
|
|
img3,
|
2017-06-01 19:13:18 +02:00
|
|
|
},
|
|
|
|
{
|
2017-06-02 14:58:26 +02:00
|
|
|
"4",
|
|
|
|
"-**-",
|
|
|
|
img4,
|
2017-06-01 19:13:18 +02:00
|
|
|
},
|
|
|
|
{
|
2017-06-02 14:58:26 +02:00
|
|
|
"5",
|
|
|
|
"**--",
|
|
|
|
img5,
|
2017-06-01 19:13:18 +02:00
|
|
|
},
|
|
|
|
{
|
2017-06-02 14:58:26 +02:00
|
|
|
"6",
|
|
|
|
"****",
|
|
|
|
img6,
|
2017-06-01 19:13:18 +02:00
|
|
|
},
|
|
|
|
{
|
2017-06-02 14:58:26 +02:00
|
|
|
"7",
|
|
|
|
"*-**",
|
|
|
|
img7,
|
2017-05-31 19:37:21 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range testCases {
|
2017-06-02 14:58:26 +02:00
|
|
|
for i := 0; i < 4; i++ {
|
|
|
|
want := color.RGBA{}
|
|
|
|
if c.out[i] == '*' {
|
2023-01-28 11:06:38 +01:00
|
|
|
want = color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}
|
2017-06-02 14:58:26 +02:00
|
|
|
}
|
2022-08-05 14:40:38 +02:00
|
|
|
got := pixelsToColor(c.image.BasePixelsForTesting(), i, 0, w, h)
|
2018-02-03 08:43:18 +01:00
|
|
|
if !sameColors(got, want, 1) {
|
2017-06-02 14:58:26 +02:00
|
|
|
t.Errorf("%s[%d]: got %v, want %v", c.name, i, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
func newImageFromImage(rgba *image.RGBA) *restorable.Image {
|
2018-02-25 15:13:06 +01:00
|
|
|
s := rgba.Bounds().Size()
|
2022-06-06 02:29:30 +02:00
|
|
|
img := restorable.NewImage(s.X, s.Y, restorable.ImageTypeRegular)
|
2023-04-27 17:12:42 +02:00
|
|
|
img.WritePixels(rgba.Pix, image.Rect(0, 0, s.X, s.Y))
|
2018-02-25 15:13:06 +01:00
|
|
|
return img
|
|
|
|
}
|
|
|
|
|
2017-06-02 14:58:26 +02:00
|
|
|
func TestRestoreRecursive(t *testing.T) {
|
2018-06-10 15:59:00 +02:00
|
|
|
const (
|
|
|
|
w = 4
|
|
|
|
h = 1
|
|
|
|
)
|
|
|
|
base := image.NewRGBA(image.Rect(0, 0, w, h))
|
2017-06-02 14:58:26 +02:00
|
|
|
base.Pix[0] = 0xff
|
|
|
|
base.Pix[1] = 0xff
|
|
|
|
base.Pix[2] = 0xff
|
|
|
|
base.Pix[3] = 0xff
|
2018-03-19 16:37:02 +01:00
|
|
|
|
2018-02-25 15:13:06 +01:00
|
|
|
img0 := newImageFromImage(base)
|
2022-06-06 02:29:30 +02:00
|
|
|
img1 := restorable.NewImage(w, h, restorable.ImageTypeRegular)
|
2017-06-02 14:58:26 +02:00
|
|
|
defer func() {
|
|
|
|
img1.Dispose()
|
|
|
|
img0.Dispose()
|
|
|
|
}()
|
2018-10-28 15:03:06 +01:00
|
|
|
is := graphics.QuadIndices()
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, w, h)
|
|
|
|
img1.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{img0}, quadVertices(w, h, 1, 0), is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
|
|
|
img0.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{img1}, quadVertices(w, h, 1, 0), is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
2023-05-07 18:06:14 +02:00
|
|
|
if err := restorable.ResolveStaleImages(ui.GraphicsDriverForTesting()); err != nil {
|
2020-01-18 17:18:56 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-03-19 15:55:14 +01:00
|
|
|
if err := restorable.RestoreIfNeeded(ui.GraphicsDriverForTesting()); err != nil {
|
2017-06-02 14:58:26 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
out string
|
2021-10-02 12:58:48 +02:00
|
|
|
image *restorable.Image
|
2017-06-02 14:58:26 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"0",
|
|
|
|
"*-*-",
|
|
|
|
img0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"1",
|
|
|
|
"-*--",
|
|
|
|
img1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range testCases {
|
|
|
|
for i := 0; i < 4; i++ {
|
|
|
|
want := color.RGBA{}
|
|
|
|
if c.out[i] == '*' {
|
2023-01-28 11:06:38 +01:00
|
|
|
want = color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}
|
2017-06-02 14:58:26 +02:00
|
|
|
}
|
2022-08-05 14:40:38 +02:00
|
|
|
got := pixelsToColor(c.image.BasePixelsForTesting(), i, 0, w, h)
|
2018-02-03 08:43:18 +01:00
|
|
|
if !sameColors(got, want, 1) {
|
2017-06-02 14:58:26 +02:00
|
|
|
t.Errorf("%s[%d]: got %v, want %v", c.name, i, got, want)
|
|
|
|
}
|
2017-05-31 19:37:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 20:16:23 +02:00
|
|
|
func TestWritePixels(t *testing.T) {
|
2022-06-06 02:29:30 +02:00
|
|
|
img := restorable.NewImage(17, 31, restorable.ImageTypeRegular)
|
2018-03-19 16:37:02 +01:00
|
|
|
defer img.Dispose()
|
|
|
|
|
2018-02-28 16:27:55 +01:00
|
|
|
pix := make([]byte, 4*4*4)
|
|
|
|
for i := range pix {
|
|
|
|
pix[i] = 0xff
|
|
|
|
}
|
2023-04-27 17:12:42 +02:00
|
|
|
img.WritePixels(pix, image.Rect(5, 7, 9, 11))
|
2023-01-28 11:06:38 +01:00
|
|
|
// Check the region (5, 7)-(9, 11). Outside state is indeterminate.
|
2022-08-05 14:40:38 +02:00
|
|
|
for i := range pix {
|
|
|
|
pix[i] = 0
|
|
|
|
}
|
2023-04-27 17:12:42 +02:00
|
|
|
if err := img.ReadPixels(ui.GraphicsDriverForTesting(), pix, image.Rect(5, 7, 9, 11)); err != nil {
|
2022-08-05 14:40:38 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-03-19 16:37:02 +01:00
|
|
|
for j := 7; j < 11; j++ {
|
|
|
|
for i := 5; i < 9; i++ {
|
2022-08-05 14:40:38 +02:00
|
|
|
idx := 4 * ((j-7)*4 + i - 5)
|
2023-01-28 11:06:38 +01:00
|
|
|
got := color.RGBA{R: pix[idx], G: pix[idx+1], B: pix[idx+2], A: pix[idx+3]}
|
|
|
|
want := color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}
|
2018-02-28 16:27:55 +01:00
|
|
|
if got != want {
|
2022-08-05 14:40:38 +02:00
|
|
|
t.Errorf("(%d, %d): got: %v, want: %v", i, j, got, want)
|
2018-02-28 16:27:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-07 18:06:14 +02:00
|
|
|
if err := restorable.ResolveStaleImages(ui.GraphicsDriverForTesting()); err != nil {
|
2020-01-18 17:18:56 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-03-19 15:55:14 +01:00
|
|
|
if err := restorable.RestoreIfNeeded(ui.GraphicsDriverForTesting()); err != nil {
|
2018-02-28 18:43:59 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-04-27 17:12:42 +02:00
|
|
|
if err := img.ReadPixels(ui.GraphicsDriverForTesting(), pix, image.Rect(5, 7, 9, 11)); err != nil {
|
2022-08-05 14:40:38 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-03-19 16:37:02 +01:00
|
|
|
for j := 7; j < 11; j++ {
|
|
|
|
for i := 5; i < 9; i++ {
|
2022-08-05 14:40:38 +02:00
|
|
|
idx := 4 * ((j-7)*4 + i - 5)
|
2023-01-28 11:06:38 +01:00
|
|
|
got := color.RGBA{R: pix[idx], G: pix[idx+1], B: pix[idx+2], A: pix[idx+3]}
|
|
|
|
want := color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}
|
2018-02-28 18:43:59 +01:00
|
|
|
if got != want {
|
2022-08-05 14:40:38 +02:00
|
|
|
t.Errorf("(%d, %d): got: %v, want: %v", i, j, got, want)
|
2018-02-28 18:43:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-28 16:27:55 +01:00
|
|
|
}
|
|
|
|
|
2022-08-07 20:16:23 +02:00
|
|
|
func TestDrawTrianglesAndWritePixels(t *testing.T) {
|
2018-03-19 16:37:02 +01:00
|
|
|
base := image.NewRGBA(image.Rect(0, 0, 1, 1))
|
|
|
|
base.Pix[0] = 0xff
|
2019-01-05 19:15:32 +01:00
|
|
|
base.Pix[1] = 0
|
|
|
|
base.Pix[2] = 0
|
2018-03-19 16:37:02 +01:00
|
|
|
base.Pix[3] = 0xff
|
|
|
|
img0 := newImageFromImage(base)
|
|
|
|
defer img0.Dispose()
|
2022-06-06 02:29:30 +02:00
|
|
|
img1 := restorable.NewImage(2, 1, restorable.ImageTypeRegular)
|
2018-03-19 16:37:02 +01:00
|
|
|
defer img1.Dispose()
|
2018-06-10 15:59:00 +02:00
|
|
|
|
2023-04-16 11:56:14 +02:00
|
|
|
vs := quadVertices(1, 1, 0, 0)
|
2018-10-28 15:03:06 +01:00
|
|
|
is := graphics.QuadIndices()
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, 2, 1)
|
|
|
|
img1.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{img0}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
2023-04-27 17:12:42 +02:00
|
|
|
img1.WritePixels([]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, image.Rect(0, 0, 2, 1))
|
2018-03-19 16:37:02 +01:00
|
|
|
|
2023-05-07 18:06:14 +02:00
|
|
|
if err := restorable.ResolveStaleImages(ui.GraphicsDriverForTesting()); err != nil {
|
2020-01-18 17:18:56 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-03-19 15:55:14 +01:00
|
|
|
if err := restorable.RestoreIfNeeded(ui.GraphicsDriverForTesting()); err != nil {
|
2018-03-19 16:37:02 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-08-05 14:40:38 +02:00
|
|
|
var pix [4]byte
|
2023-04-27 17:12:42 +02:00
|
|
|
if err := img1.ReadPixels(ui.GraphicsDriverForTesting(), pix[:], image.Rect(0, 0, 1, 1)); err != nil {
|
2020-01-18 17:18:56 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-01-28 11:06:38 +01:00
|
|
|
got := color.RGBA{R: pix[0], G: pix[1], B: pix[2], A: pix[3]}
|
|
|
|
want := color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}
|
2018-03-19 16:37:02 +01:00
|
|
|
if !sameColors(got, want, 1) {
|
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-24 16:50:11 +01:00
|
|
|
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()
|
|
|
|
|
2018-10-28 15:03:06 +01:00
|
|
|
is := graphics.QuadIndices()
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, 1, 1)
|
|
|
|
img1.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{img2}, quadVertices(1, 1, 0, 0), is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
|
|
|
img0.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{img1}, quadVertices(1, 1, 0, 0), is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
2018-03-24 16:50:11 +01:00
|
|
|
img1.Dispose()
|
|
|
|
|
2023-05-07 18:06:14 +02:00
|
|
|
if err := restorable.ResolveStaleImages(ui.GraphicsDriverForTesting()); err != nil {
|
2020-01-18 17:18:56 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-03-19 15:55:14 +01:00
|
|
|
if err := restorable.RestoreIfNeeded(ui.GraphicsDriverForTesting()); err != nil {
|
2018-03-24 16:50:11 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-08-05 14:40:38 +02:00
|
|
|
var pix [4]byte
|
2023-04-27 17:12:42 +02:00
|
|
|
if err := img0.ReadPixels(ui.GraphicsDriverForTesting(), pix[:], image.Rect(0, 0, 1, 1)); err != nil {
|
2020-01-18 17:18:56 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-01-28 11:06:38 +01:00
|
|
|
got := color.RGBA{R: pix[0], G: pix[1], B: pix[2], A: pix[3]}
|
|
|
|
want := color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}
|
2018-03-24 16:50:11 +01:00
|
|
|
if !sameColors(got, want, 1) {
|
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 20:16:23 +02:00
|
|
|
func TestWritePixelsPart(t *testing.T) {
|
2019-07-17 16:03:14 +02:00
|
|
|
pix := make([]uint8, 4*2*2)
|
2018-04-28 15:50:00 +02:00
|
|
|
for i := range pix {
|
|
|
|
pix[i] = 0xff
|
|
|
|
}
|
|
|
|
|
2022-06-06 02:29:30 +02:00
|
|
|
img := restorable.NewImage(4, 4, restorable.ImageTypeRegular)
|
2018-04-28 15:50:00 +02:00
|
|
|
// This doesn't make the image stale. Its base pixels are available.
|
2023-04-27 17:12:42 +02:00
|
|
|
img.WritePixels(pix, image.Rect(1, 1, 3, 3))
|
2018-04-28 15:50:00 +02:00
|
|
|
|
|
|
|
cases := []struct {
|
2019-07-17 16:03:14 +02:00
|
|
|
i int
|
|
|
|
j int
|
|
|
|
want color.RGBA
|
2018-04-28 15:50:00 +02:00
|
|
|
}{
|
|
|
|
{
|
2019-07-17 16:03:14 +02:00
|
|
|
i: 0,
|
|
|
|
j: 0,
|
2023-01-28 11:06:38 +01:00
|
|
|
want: color.RGBA{},
|
2018-04-28 15:50:00 +02:00
|
|
|
},
|
|
|
|
{
|
2019-07-17 16:03:14 +02:00
|
|
|
i: 3,
|
|
|
|
j: 0,
|
2023-01-28 11:06:38 +01:00
|
|
|
want: color.RGBA{},
|
2018-04-28 15:50:00 +02:00
|
|
|
},
|
|
|
|
{
|
2019-07-17 16:03:14 +02:00
|
|
|
i: 0,
|
|
|
|
j: 1,
|
2023-01-28 11:06:38 +01:00
|
|
|
want: color.RGBA{},
|
2018-04-28 15:50:00 +02:00
|
|
|
},
|
|
|
|
{
|
2019-07-17 16:03:14 +02:00
|
|
|
i: 1,
|
|
|
|
j: 1,
|
2023-01-28 11:06:38 +01:00
|
|
|
want: color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff},
|
2018-04-28 15:50:00 +02:00
|
|
|
},
|
|
|
|
{
|
2019-07-17 16:03:14 +02:00
|
|
|
i: 3,
|
|
|
|
j: 1,
|
2023-01-28 11:06:38 +01:00
|
|
|
want: color.RGBA{},
|
2018-04-28 15:50:00 +02:00
|
|
|
},
|
|
|
|
{
|
2019-07-17 16:03:14 +02:00
|
|
|
i: 0,
|
|
|
|
j: 2,
|
2023-01-28 11:06:38 +01:00
|
|
|
want: color.RGBA{},
|
2018-04-28 15:50:00 +02:00
|
|
|
},
|
|
|
|
{
|
2019-07-17 16:03:14 +02:00
|
|
|
i: 2,
|
|
|
|
j: 2,
|
2023-01-28 11:06:38 +01:00
|
|
|
want: color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff},
|
2018-04-28 15:50:00 +02:00
|
|
|
},
|
|
|
|
{
|
2019-07-17 16:03:14 +02:00
|
|
|
i: 3,
|
|
|
|
j: 2,
|
2023-01-28 11:06:38 +01:00
|
|
|
want: color.RGBA{},
|
2018-04-28 15:50:00 +02:00
|
|
|
},
|
|
|
|
{
|
2019-07-17 16:03:14 +02:00
|
|
|
i: 0,
|
|
|
|
j: 3,
|
2023-01-28 11:06:38 +01:00
|
|
|
want: color.RGBA{},
|
2018-04-28 15:50:00 +02:00
|
|
|
},
|
|
|
|
{
|
2019-07-17 16:03:14 +02:00
|
|
|
i: 3,
|
|
|
|
j: 3,
|
2023-01-28 11:06:38 +01:00
|
|
|
want: color.RGBA{},
|
2018-04-28 15:50:00 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
2022-08-05 14:40:38 +02:00
|
|
|
got := pixelsToColor(img.BasePixelsForTesting(), c.i, c.j, 4, 4)
|
2019-07-17 16:03:14 +02:00
|
|
|
want := c.want
|
2018-04-28 15:50:00 +02:00
|
|
|
if got != want {
|
2019-07-17 16:03:14 +02:00
|
|
|
t.Errorf("base pixel (%d, %d): got %v, want %v", c.i, c.j, got, want)
|
2018-04-28 15:50:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 20:16:23 +02:00
|
|
|
func TestWritePixelsOnly(t *testing.T) {
|
2018-12-26 12:10:12 +01:00
|
|
|
const w, h = 128, 128
|
2022-06-06 02:29:30 +02:00
|
|
|
img0 := restorable.NewImage(w, h, restorable.ImageTypeRegular)
|
2018-12-26 13:14:29 +01:00
|
|
|
defer img0.Dispose()
|
2022-06-06 02:29:30 +02:00
|
|
|
img1 := restorable.NewImage(1, 1, restorable.ImageTypeRegular)
|
2018-12-26 13:14:29 +01:00
|
|
|
defer img1.Dispose()
|
2018-12-26 12:10:12 +01:00
|
|
|
|
|
|
|
for i := 0; i < w*h; i += 5 {
|
2023-04-27 17:12:42 +02:00
|
|
|
img0.WritePixels([]byte{1, 2, 3, 4}, image.Rect(i%w, i/w, i%w+1, i/w+1))
|
2018-12-26 12:10:12 +01:00
|
|
|
}
|
|
|
|
|
2023-04-16 11:56:14 +02:00
|
|
|
vs := quadVertices(1, 1, 0, 0)
|
2018-12-26 13:14:29 +01:00
|
|
|
is := graphics.QuadIndices()
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, 1, 1)
|
|
|
|
img1.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{img0}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
2023-04-27 17:12:42 +02:00
|
|
|
img0.WritePixels([]byte{5, 6, 7, 8}, image.Rect(0, 0, 1, 1))
|
2018-12-26 13:14:29 +01:00
|
|
|
|
2018-12-26 12:10:12 +01:00
|
|
|
// BasePixelsForTesting is available without GPU accessing.
|
2019-07-17 16:03:14 +02:00
|
|
|
for j := 0; j < h; j++ {
|
|
|
|
for i := 0; i < w; i++ {
|
|
|
|
idx := j*w + i
|
|
|
|
var want color.RGBA
|
|
|
|
switch {
|
|
|
|
case idx == 0:
|
2023-01-28 11:06:38 +01:00
|
|
|
want = color.RGBA{R: 5, G: 6, B: 7, A: 8}
|
2019-07-17 16:03:14 +02:00
|
|
|
case idx%5 == 0:
|
2023-01-28 11:06:38 +01:00
|
|
|
want = color.RGBA{R: 1, G: 2, B: 3, A: 4}
|
2019-07-17 16:03:14 +02:00
|
|
|
}
|
2022-08-05 14:40:38 +02:00
|
|
|
got := pixelsToColor(img0.BasePixelsForTesting(), i, j, w, h)
|
2019-07-17 16:03:14 +02:00
|
|
|
if !sameColors(got, want, 0) {
|
|
|
|
t.Errorf("got %v, want %v", got, want)
|
|
|
|
}
|
2018-12-26 12:10:12 +01:00
|
|
|
}
|
|
|
|
}
|
2018-12-26 13:14:29 +01:00
|
|
|
|
2023-05-07 18:06:14 +02:00
|
|
|
if err := restorable.ResolveStaleImages(ui.GraphicsDriverForTesting()); err != nil {
|
2020-01-18 17:18:56 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-03-19 15:55:14 +01:00
|
|
|
if err := restorable.RestoreIfNeeded(ui.GraphicsDriverForTesting()); err != nil {
|
2018-12-26 13:14:29 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-01-28 11:06:38 +01:00
|
|
|
want := color.RGBA{R: 1, G: 2, B: 3, A: 4}
|
2022-08-05 14:40:38 +02:00
|
|
|
got := pixelsToColor(img1.BasePixelsForTesting(), 0, 0, w, h)
|
2018-12-26 13:14:29 +01:00
|
|
|
if !sameColors(got, want, 0) {
|
|
|
|
t.Errorf("got %v, want %v", got, want)
|
|
|
|
}
|
2018-12-26 12:10:12 +01:00
|
|
|
}
|
|
|
|
|
2017-05-31 19:37:21 +02:00
|
|
|
// TODO: How about volatile/screen images?
|
2019-01-21 19:35:48 +01:00
|
|
|
|
|
|
|
// Issue #793
|
|
|
|
func TestReadPixelsFromVolatileImage(t *testing.T) {
|
|
|
|
const w, h = 16, 16
|
2022-06-06 02:29:30 +02:00
|
|
|
dst := restorable.NewImage(w, h, restorable.ImageTypeVolatile)
|
|
|
|
src := restorable.NewImage(w, h, restorable.ImageTypeRegular)
|
2019-01-21 19:35:48 +01:00
|
|
|
|
|
|
|
// First, make sure that dst has pixels
|
2023-04-27 17:12:42 +02:00
|
|
|
dst.WritePixels(make([]byte, 4*w*h), image.Rect(0, 0, w, h))
|
2019-01-21 19:35:48 +01:00
|
|
|
|
2019-01-22 15:33:56 +01:00
|
|
|
// Second, draw src to dst. If the implementation is correct, dst becomes stale.
|
2019-07-19 21:53:18 +02:00
|
|
|
pix := make([]byte, 4*w*h)
|
|
|
|
for i := range pix {
|
|
|
|
pix[i] = 0xff
|
|
|
|
}
|
2023-04-27 17:12:42 +02:00
|
|
|
src.WritePixels(pix, image.Rect(0, 0, w, h))
|
2023-04-16 11:56:14 +02:00
|
|
|
vs := quadVertices(1, 1, 0, 0)
|
2019-01-21 19:35:48 +01:00
|
|
|
is := graphics.QuadIndices()
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, w, h)
|
|
|
|
dst.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{src}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
2019-01-21 19:35:48 +01:00
|
|
|
|
2019-01-22 15:33:56 +01:00
|
|
|
// Read the pixels. If the implementation is correct, dst tries to read its pixels from GPU due to being
|
|
|
|
// stale.
|
2019-01-21 19:35:48 +01:00
|
|
|
want := byte(0xff)
|
2022-08-05 14:40:38 +02:00
|
|
|
|
|
|
|
var result [4]byte
|
2023-04-27 17:12:42 +02:00
|
|
|
if err := dst.ReadPixels(ui.GraphicsDriverForTesting(), result[:], image.Rect(0, 0, 1, 1)); err != nil {
|
2020-01-18 17:18:56 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-08-05 14:40:38 +02:00
|
|
|
got := result[0]
|
2019-01-21 19:35:48 +01:00
|
|
|
if got != want {
|
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
2019-07-16 20:15:01 +02:00
|
|
|
|
2022-08-07 20:16:23 +02:00
|
|
|
func TestAllowWritePixelsAfterDrawTriangles(t *testing.T) {
|
2019-07-16 20:15:01 +02:00
|
|
|
const w, h = 16, 16
|
2022-06-06 02:29:30 +02:00
|
|
|
src := restorable.NewImage(w, h, restorable.ImageTypeRegular)
|
|
|
|
dst := restorable.NewImage(w, h, restorable.ImageTypeRegular)
|
2019-07-16 20:15:01 +02:00
|
|
|
|
2023-04-16 11:56:14 +02:00
|
|
|
vs := quadVertices(w, h, 0, 0)
|
2019-07-16 20:15:01 +02:00
|
|
|
is := graphics.QuadIndices()
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, w, h)
|
|
|
|
dst.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{src}, vs, is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
2023-04-27 17:12:42 +02:00
|
|
|
dst.WritePixels(make([]byte, 4*w*h), image.Rect(0, 0, w, h))
|
2022-08-07 20:16:23 +02:00
|
|
|
// WritePixels for a whole image doesn't panic.
|
2019-07-16 20:15:01 +02:00
|
|
|
}
|
|
|
|
|
2022-09-13 15:09:29 +02:00
|
|
|
func TestAllowWritePixelsForPartAfterDrawTriangles(t *testing.T) {
|
2019-07-16 20:15:01 +02:00
|
|
|
const w, h = 16, 16
|
2022-06-06 02:29:30 +02:00
|
|
|
src := restorable.NewImage(w, h, restorable.ImageTypeRegular)
|
|
|
|
dst := restorable.NewImage(w, h, restorable.ImageTypeRegular)
|
2019-07-16 20:15:01 +02:00
|
|
|
|
2022-09-24 15:09:05 +02:00
|
|
|
pix := make([]byte, 4*w*h)
|
|
|
|
for i := range pix {
|
|
|
|
pix[i] = 0xff
|
|
|
|
}
|
2023-04-27 17:12:42 +02:00
|
|
|
src.WritePixels(pix, image.Rect(0, 0, w, h))
|
2022-09-24 15:09:05 +02:00
|
|
|
|
2023-04-16 11:56:14 +02:00
|
|
|
vs := quadVertices(w, h, 0, 0)
|
2019-07-16 20:15:01 +02:00
|
|
|
is := graphics.QuadIndices()
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, w, h)
|
|
|
|
dst.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{src}, vs, is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
2023-04-27 17:12:42 +02:00
|
|
|
dst.WritePixels(make([]byte, 4*2*2), image.Rect(0, 0, 2, 2))
|
2022-09-13 15:09:29 +02:00
|
|
|
// WritePixels for a part of image doesn't panic.
|
2022-09-24 15:09:05 +02:00
|
|
|
|
2023-05-07 18:06:14 +02:00
|
|
|
if err := restorable.ResolveStaleImages(ui.GraphicsDriverForTesting()); err != nil {
|
2022-09-24 15:09:05 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := restorable.RestoreIfNeeded(ui.GraphicsDriverForTesting()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make([]byte, 4*w*h)
|
2023-04-27 17:12:42 +02:00
|
|
|
if err := dst.ReadPixels(ui.GraphicsDriverForTesting(), result, image.Rect(0, 0, w, h)); err != nil {
|
2022-09-24 15:09:05 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for j := 0; j < h; j++ {
|
|
|
|
for i := 0; i < w; i++ {
|
2023-01-28 11:06:38 +01:00
|
|
|
got := color.RGBA{R: result[4*(j*w+i)], G: result[4*(j*w+i)+1], B: result[4*(j*w+i)+2], A: result[4*(j*w+i)+3]}
|
2022-09-24 15:09:05 +02:00
|
|
|
var want color.RGBA
|
|
|
|
if i >= 2 || j >= 2 {
|
2023-01-28 11:06:38 +01:00
|
|
|
want = color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}
|
2022-09-24 15:09:05 +02:00
|
|
|
}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("color at (%d, %d): got: %v, want: %v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-16 20:15:01 +02:00
|
|
|
}
|
2019-07-17 04:13:57 +02:00
|
|
|
|
|
|
|
func TestExtend(t *testing.T) {
|
|
|
|
pixAt := func(i, j int) byte {
|
|
|
|
return byte(17*i + 13*j + 0x40)
|
|
|
|
}
|
|
|
|
|
|
|
|
const w, h = 16, 16
|
2022-06-06 02:29:30 +02:00
|
|
|
orig := restorable.NewImage(w, h, restorable.ImageTypeRegular)
|
2019-07-17 04:13:57 +02:00
|
|
|
pix := make([]byte, 4*w*h)
|
|
|
|
for j := 0; j < h; j++ {
|
|
|
|
for i := 0; i < w; i++ {
|
|
|
|
idx := j*w + i
|
|
|
|
v := pixAt(i, j)
|
|
|
|
pix[4*idx] = v
|
|
|
|
pix[4*idx+1] = v
|
|
|
|
pix[4*idx+2] = v
|
|
|
|
pix[4*idx+3] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 17:12:42 +02:00
|
|
|
orig.WritePixels(pix, image.Rect(0, 0, w, h))
|
2019-07-17 04:13:57 +02:00
|
|
|
extended := orig.Extend(w*2, h*2) // After this, orig is already disposed.
|
|
|
|
|
2022-08-05 14:40:38 +02:00
|
|
|
result := make([]byte, 4*(w*2)*(h*2))
|
2023-04-27 17:12:42 +02:00
|
|
|
if err := extended.ReadPixels(ui.GraphicsDriverForTesting(), result, image.Rect(0, 0, w*2, h*2)); err != nil {
|
2023-02-28 18:06:41 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for j := 0; j < h*2; j++ {
|
|
|
|
for i := 0; i < w*2; i++ {
|
|
|
|
got := result[4*(j*(w*2)+i)]
|
|
|
|
want := byte(0)
|
|
|
|
if i < w && j < h {
|
|
|
|
want = pixAt(i, j)
|
|
|
|
}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("extended.At(%d, %d): got: %v, want: %v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDrawTrianglesAndExtend(t *testing.T) {
|
|
|
|
pixAt := func(i, j int) byte {
|
|
|
|
return byte(17*i + 13*j + 0x40)
|
|
|
|
}
|
|
|
|
|
|
|
|
const w, h = 16, 16
|
|
|
|
|
|
|
|
src := restorable.NewImage(w, h, restorable.ImageTypeRegular)
|
|
|
|
pix := make([]byte, 4*w*h)
|
|
|
|
for j := 0; j < h; j++ {
|
|
|
|
for i := 0; i < w; i++ {
|
|
|
|
idx := j*w + i
|
|
|
|
v := pixAt(i, j)
|
|
|
|
pix[4*idx] = v
|
|
|
|
pix[4*idx+1] = v
|
|
|
|
pix[4*idx+2] = v
|
|
|
|
pix[4*idx+3] = v
|
|
|
|
}
|
|
|
|
}
|
2023-04-27 17:12:42 +02:00
|
|
|
src.WritePixels(pix, image.Rect(0, 0, w, h))
|
2023-02-28 18:06:41 +01:00
|
|
|
|
|
|
|
orig := restorable.NewImage(w, h, restorable.ImageTypeRegular)
|
2023-04-16 11:56:14 +02:00
|
|
|
vs := quadVertices(w, h, 0, 0)
|
2023-02-28 18:06:41 +01:00
|
|
|
is := graphics.QuadIndices()
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, w, h)
|
|
|
|
orig.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{src}, vs, is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
2023-02-28 18:06:41 +01:00
|
|
|
extended := orig.Extend(w*2, h*2) // After this, orig is already disposed.
|
|
|
|
|
|
|
|
result := make([]byte, 4*(w*2)*(h*2))
|
2023-04-27 17:12:42 +02:00
|
|
|
if err := extended.ReadPixels(ui.GraphicsDriverForTesting(), result, image.Rect(0, 0, w*2, h*2)); err != nil {
|
2022-08-05 14:40:38 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-07-17 04:13:57 +02:00
|
|
|
for j := 0; j < h*2; j++ {
|
|
|
|
for i := 0; i < w*2; i++ {
|
2022-08-05 14:40:38 +02:00
|
|
|
got := result[4*(j*(w*2)+i)]
|
2019-07-17 04:13:57 +02:00
|
|
|
want := byte(0)
|
|
|
|
if i < w && j < h {
|
|
|
|
want = pixAt(i, j)
|
|
|
|
}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("extended.At(%d, %d): got: %v, want: %v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-17 16:03:14 +02:00
|
|
|
func TestClearPixels(t *testing.T) {
|
|
|
|
const w, h = 16, 16
|
2022-06-06 02:29:30 +02:00
|
|
|
img := restorable.NewImage(w, h, restorable.ImageTypeRegular)
|
2023-04-27 17:12:42 +02:00
|
|
|
img.WritePixels(make([]byte, 4*4*4), image.Rect(0, 0, 4, 4))
|
|
|
|
img.WritePixels(make([]byte, 4*4*4), image.Rect(4, 0, 8, 4))
|
|
|
|
img.ClearPixels(image.Rect(0, 0, 4, 4))
|
|
|
|
img.ClearPixels(image.Rect(4, 0, 8, 4))
|
2019-07-17 16:03:14 +02:00
|
|
|
|
|
|
|
// After clearing, the regions will be available again.
|
2023-04-27 17:12:42 +02:00
|
|
|
img.WritePixels(make([]byte, 4*8*4), image.Rect(0, 0, 8, 4))
|
2019-07-17 16:03:14 +02:00
|
|
|
}
|
2021-06-26 19:21:51 +02:00
|
|
|
|
|
|
|
func TestMutateSlices(t *testing.T) {
|
|
|
|
const w, h = 16, 16
|
2022-06-06 02:29:30 +02:00
|
|
|
dst := restorable.NewImage(w, h, restorable.ImageTypeRegular)
|
|
|
|
src := restorable.NewImage(w, h, restorable.ImageTypeRegular)
|
2021-06-26 19:21:51 +02:00
|
|
|
pix := make([]byte, 4*w*h)
|
|
|
|
for i := 0; i < w*h; i++ {
|
|
|
|
pix[4*i] = byte(i)
|
|
|
|
pix[4*i+1] = byte(i)
|
|
|
|
pix[4*i+2] = byte(i)
|
|
|
|
pix[4*i+3] = 0xff
|
|
|
|
}
|
2023-04-27 17:12:42 +02:00
|
|
|
src.WritePixels(pix, image.Rect(0, 0, w, h))
|
2021-06-26 19:21:51 +02:00
|
|
|
|
2023-04-16 11:56:14 +02:00
|
|
|
vs := quadVertices(w, h, 0, 0)
|
2021-06-26 19:21:51 +02:00
|
|
|
is := make([]uint16, len(graphics.QuadIndices()))
|
|
|
|
copy(is, graphics.QuadIndices())
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, w, h)
|
|
|
|
dst.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{src}, vs, is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
2021-06-26 19:21:51 +02:00
|
|
|
for i := range vs {
|
|
|
|
vs[i] = 0
|
|
|
|
}
|
|
|
|
for i := range is {
|
|
|
|
is[i] = 0
|
|
|
|
}
|
2023-05-07 18:06:14 +02:00
|
|
|
if err := restorable.ResolveStaleImages(ui.GraphicsDriverForTesting()); err != nil {
|
2021-06-26 19:21:51 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-03-19 15:55:14 +01:00
|
|
|
if err := restorable.RestoreIfNeeded(ui.GraphicsDriverForTesting()); err != nil {
|
2021-06-26 19:21:51 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-08-05 14:40:38 +02:00
|
|
|
srcPix := make([]byte, 4*w*h)
|
2023-04-27 17:12:42 +02:00
|
|
|
if err := src.ReadPixels(ui.GraphicsDriverForTesting(), srcPix, image.Rect(0, 0, w, h)); err != nil {
|
2022-08-05 14:40:38 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
dstPix := make([]byte, 4*w*h)
|
2023-04-27 17:12:42 +02:00
|
|
|
if err := dst.ReadPixels(ui.GraphicsDriverForTesting(), dstPix, image.Rect(0, 0, w, h)); err != nil {
|
2022-08-05 14:40:38 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-06-26 19:21:51 +02:00
|
|
|
for j := 0; j < h; j++ {
|
|
|
|
for i := 0; i < w; i++ {
|
2022-08-05 14:40:38 +02:00
|
|
|
idx := 4 * (j*w + i)
|
2023-01-28 11:06:38 +01:00
|
|
|
want := color.RGBA{R: srcPix[idx], G: srcPix[idx+1], B: srcPix[idx+2], A: srcPix[idx+3]}
|
|
|
|
got := color.RGBA{R: dstPix[idx], G: dstPix[idx+1], B: dstPix[idx+2], A: dstPix[idx+3]}
|
2021-06-26 19:21:51 +02:00
|
|
|
if !sameColors(got, want, 1) {
|
|
|
|
t.Errorf("(%d, %d): got %v, want %v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-20 15:00:00 +01:00
|
|
|
|
|
|
|
func TestOverlappedPixels(t *testing.T) {
|
2022-06-06 02:29:30 +02:00
|
|
|
dst := restorable.NewImage(3, 3, restorable.ImageTypeRegular)
|
2022-03-20 15:00:00 +01:00
|
|
|
|
|
|
|
pix0 := make([]byte, 4*2*2)
|
|
|
|
for j := 0; j < 2; j++ {
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
idx := 4 * (j*2 + i)
|
|
|
|
pix0[idx] = 0xff
|
|
|
|
pix0[idx+1] = 0
|
|
|
|
pix0[idx+2] = 0
|
|
|
|
pix0[idx+3] = 0xff
|
|
|
|
}
|
|
|
|
}
|
2023-04-27 17:12:42 +02:00
|
|
|
dst.WritePixels(pix0, image.Rect(0, 0, 2, 2))
|
2022-03-20 15:00:00 +01:00
|
|
|
|
|
|
|
pix1 := make([]byte, 4*2*2)
|
|
|
|
for j := 0; j < 2; j++ {
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
idx := 4 * (j*2 + i)
|
|
|
|
pix1[idx] = 0
|
|
|
|
pix1[idx+1] = 0xff
|
|
|
|
pix1[idx+2] = 0
|
|
|
|
pix1[idx+3] = 0xff
|
|
|
|
}
|
|
|
|
}
|
2023-04-27 17:12:42 +02:00
|
|
|
dst.WritePixels(pix1, image.Rect(1, 1, 3, 3))
|
2022-03-20 15:00:00 +01:00
|
|
|
|
|
|
|
wantColors := []color.RGBA{
|
|
|
|
{0xff, 0, 0, 0xff},
|
|
|
|
{0xff, 0, 0, 0xff},
|
|
|
|
{0, 0, 0, 0},
|
|
|
|
|
|
|
|
{0xff, 0, 0, 0xff},
|
|
|
|
{0, 0xff, 0, 0xff},
|
|
|
|
{0, 0xff, 0, 0xff},
|
|
|
|
|
|
|
|
{0, 0, 0, 0},
|
|
|
|
{0, 0xff, 0, 0xff},
|
|
|
|
{0, 0xff, 0, 0xff},
|
|
|
|
}
|
2022-08-05 14:40:38 +02:00
|
|
|
|
|
|
|
result := make([]byte, 4*3*3)
|
2023-04-27 17:12:42 +02:00
|
|
|
if err := dst.ReadPixels(ui.GraphicsDriverForTesting(), result, image.Rect(0, 0, 3, 3)); err != nil {
|
2022-08-05 14:40:38 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-03-20 15:00:00 +01:00
|
|
|
for j := 0; j < 3; j++ {
|
|
|
|
for i := 0; i < 3; i++ {
|
2022-08-05 14:40:38 +02:00
|
|
|
idx := 4 * (j*3 + i)
|
2023-01-28 11:06:38 +01:00
|
|
|
got := color.RGBA{R: result[idx], G: result[idx+1], B: result[idx+2], A: result[idx+3]}
|
2022-03-20 15:00:00 +01:00
|
|
|
want := wantColors[3*j+i]
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("color at (%d, %d): got %v, want: %v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 17:12:42 +02:00
|
|
|
dst.WritePixels(nil, image.Rect(1, 0, 3, 2))
|
2022-03-20 15:00:00 +01:00
|
|
|
|
|
|
|
wantColors = []color.RGBA{
|
|
|
|
{0xff, 0, 0, 0xff},
|
|
|
|
{0, 0, 0, 0},
|
|
|
|
{0, 0, 0, 0},
|
|
|
|
|
|
|
|
{0xff, 0, 0, 0xff},
|
|
|
|
{0, 0, 0, 0},
|
|
|
|
{0, 0, 0, 0},
|
|
|
|
|
|
|
|
{0, 0, 0, 0},
|
|
|
|
{0, 0xff, 0, 0xff},
|
|
|
|
{0, 0xff, 0, 0xff},
|
|
|
|
}
|
2023-04-27 17:12:42 +02:00
|
|
|
if err := dst.ReadPixels(ui.GraphicsDriverForTesting(), result, image.Rect(0, 0, 3, 3)); err != nil {
|
2022-08-05 14:40:38 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-03-20 15:00:00 +01:00
|
|
|
for j := 0; j < 3; j++ {
|
|
|
|
for i := 0; i < 3; i++ {
|
2022-08-05 14:40:38 +02:00
|
|
|
idx := 4 * (j*3 + i)
|
2023-01-28 11:06:38 +01:00
|
|
|
got := color.RGBA{R: result[idx], G: result[idx+1], B: result[idx+2], A: result[idx+3]}
|
2022-03-20 15:00:00 +01:00
|
|
|
want := wantColors[3*j+i]
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("color at (%d, %d): got %v, want: %v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-20 18:44:48 +01:00
|
|
|
|
2022-03-20 19:04:52 +01:00
|
|
|
pix2 := make([]byte, 4*2*2)
|
|
|
|
for j := 0; j < 2; j++ {
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
idx := 4 * (j*2 + i)
|
|
|
|
pix2[idx] = 0
|
|
|
|
pix2[idx+1] = 0
|
|
|
|
pix2[idx+2] = 0xff
|
|
|
|
pix2[idx+3] = 0xff
|
|
|
|
}
|
2022-03-20 18:44:48 +01:00
|
|
|
}
|
2023-04-27 17:12:42 +02:00
|
|
|
dst.WritePixels(pix2, image.Rect(1, 1, 3, 3))
|
2022-03-20 18:44:48 +01:00
|
|
|
|
|
|
|
wantColors = []color.RGBA{
|
|
|
|
{0xff, 0, 0, 0xff},
|
|
|
|
{0, 0, 0, 0},
|
|
|
|
{0, 0, 0, 0},
|
|
|
|
|
|
|
|
{0xff, 0, 0, 0xff},
|
2022-03-20 19:04:52 +01:00
|
|
|
{0, 0, 0xff, 0xff},
|
|
|
|
{0, 0, 0xff, 0xff},
|
2022-03-20 18:44:48 +01:00
|
|
|
|
|
|
|
{0, 0, 0, 0},
|
2022-03-20 19:04:52 +01:00
|
|
|
{0, 0, 0xff, 0xff},
|
|
|
|
{0, 0, 0xff, 0xff},
|
2022-03-20 18:44:48 +01:00
|
|
|
}
|
2023-04-27 17:12:42 +02:00
|
|
|
if err := dst.ReadPixels(ui.GraphicsDriverForTesting(), result, image.Rect(0, 0, 3, 3)); err != nil {
|
2022-08-05 14:40:38 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-03-20 18:44:48 +01:00
|
|
|
for j := 0; j < 3; j++ {
|
|
|
|
for i := 0; i < 3; i++ {
|
2022-08-05 14:40:38 +02:00
|
|
|
idx := 4 * (j*3 + i)
|
2023-01-28 11:06:38 +01:00
|
|
|
got := color.RGBA{R: result[idx], G: result[idx+1], B: result[idx+2], A: result[idx+3]}
|
2022-03-20 18:44:48 +01:00
|
|
|
want := wantColors[3*j+i]
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("color at (%d, %d): got %v, want: %v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-20 19:04:52 +01:00
|
|
|
|
2023-05-07 18:06:14 +02:00
|
|
|
if err := restorable.ResolveStaleImages(ui.GraphicsDriverForTesting()); err != nil {
|
2022-03-20 19:04:52 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := restorable.RestoreIfNeeded(ui.GraphicsDriverForTesting()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-04-27 17:12:42 +02:00
|
|
|
if err := dst.ReadPixels(ui.GraphicsDriverForTesting(), result, image.Rect(0, 0, 3, 3)); err != nil {
|
2022-08-05 14:40:38 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-03-20 19:04:52 +01:00
|
|
|
for j := 0; j < 3; j++ {
|
|
|
|
for i := 0; i < 3; i++ {
|
2022-08-05 14:40:38 +02:00
|
|
|
idx := 4 * (j*3 + i)
|
2023-01-28 11:06:38 +01:00
|
|
|
got := color.RGBA{R: result[idx], G: result[idx+1], B: result[idx+2], A: result[idx+3]}
|
2022-03-20 19:04:52 +01:00
|
|
|
want := wantColors[3*j+i]
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("color at (%d, %d): got %v, want: %v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-20 15:00:00 +01:00
|
|
|
}
|
2022-09-13 19:17:12 +02:00
|
|
|
|
|
|
|
// Issue #2324
|
|
|
|
func TestDrawTrianglesAndReadPixels(t *testing.T) {
|
|
|
|
const w, h = 1, 1
|
|
|
|
src := restorable.NewImage(w, h, restorable.ImageTypeRegular)
|
|
|
|
dst := restorable.NewImage(w, h, restorable.ImageTypeRegular)
|
|
|
|
|
2023-04-27 17:12:42 +02:00
|
|
|
src.WritePixels([]byte{0x80, 0x80, 0x80, 0x80}, image.Rect(0, 0, 1, 1))
|
2022-09-13 19:17:12 +02:00
|
|
|
|
2023-04-16 11:56:14 +02:00
|
|
|
vs := quadVertices(w, h, 0, 0)
|
2022-09-13 19:17:12 +02:00
|
|
|
is := graphics.QuadIndices()
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(0, 0, w, h)
|
|
|
|
dst.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{src}, vs, is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
2022-09-13 19:17:12 +02:00
|
|
|
|
|
|
|
pix := make([]byte, 4*w*h)
|
2023-04-27 17:12:42 +02:00
|
|
|
if err := dst.ReadPixels(ui.GraphicsDriverForTesting(), pix, image.Rect(0, 0, w, h)); err != nil {
|
2022-09-13 19:17:12 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-01-28 11:06:38 +01:00
|
|
|
if got, want := (color.RGBA{R: pix[0], G: pix[1], B: pix[2], A: pix[3]}), (color.RGBA{R: 0x80, G: 0x80, B: 0x80, A: 0x80}); !sameColors(got, want, 1) {
|
2022-09-13 19:17:12 +02:00
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
2023-02-25 05:50:02 +01:00
|
|
|
|
|
|
|
func TestWritePixelsAndDrawTriangles(t *testing.T) {
|
|
|
|
src := restorable.NewImage(1, 1, restorable.ImageTypeRegular)
|
|
|
|
dst := restorable.NewImage(2, 1, restorable.ImageTypeRegular)
|
|
|
|
|
2023-04-27 17:12:42 +02:00
|
|
|
src.WritePixels([]byte{0x80, 0x80, 0x80, 0x80}, image.Rect(0, 0, 1, 1))
|
2023-02-25 05:50:02 +01:00
|
|
|
|
|
|
|
// Call WritePixels first.
|
2023-04-27 17:12:42 +02:00
|
|
|
dst.WritePixels([]byte{0x40, 0x40, 0x40, 0x40}, image.Rect(0, 0, 1, 1))
|
2023-02-25 05:50:02 +01:00
|
|
|
|
|
|
|
// Call DrawTriangles at a different region second.
|
2023-04-16 11:56:14 +02:00
|
|
|
vs := quadVertices(1, 1, 1, 0)
|
2023-02-25 05:50:02 +01:00
|
|
|
is := graphics.QuadIndices()
|
2023-09-28 07:29:55 +02:00
|
|
|
dr := image.Rect(1, 0, 2, 1)
|
|
|
|
dst.DrawTriangles([graphics.ShaderImageCount]*restorable.Image{src}, vs, is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, restorable.NearestFilterShader, nil, false)
|
2023-02-25 05:50:02 +01:00
|
|
|
|
|
|
|
// Get the pixels.
|
|
|
|
pix := make([]byte, 4*2*1)
|
2023-04-27 17:12:42 +02:00
|
|
|
if err := dst.ReadPixels(ui.GraphicsDriverForTesting(), pix, image.Rect(0, 0, 2, 1)); err != nil {
|
2023-02-25 05:50:02 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if got, want := (color.RGBA{R: pix[0], G: pix[1], B: pix[2], A: pix[3]}), (color.RGBA{R: 0x40, G: 0x40, B: 0x40, A: 0x40}); !sameColors(got, want, 1) {
|
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
if got, want := (color.RGBA{R: pix[4], G: pix[5], B: pix[6], A: pix[7]}), (color.RGBA{R: 0x80, G: 0x80, B: 0x80, A: 0x80}); !sameColors(got, want, 1) {
|
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|