2018-04-04 19:35:30 +02:00
|
|
|
// Copyright 2018 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 shareable_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"image/color"
|
|
|
|
"os"
|
2018-07-21 22:52:11 +02:00
|
|
|
"runtime"
|
2018-04-04 19:35:30 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten"
|
2018-10-28 12:25:52 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
2018-04-04 19:35:30 +02:00
|
|
|
. "github.com/hajimehoshi/ebiten/internal/shareable"
|
2018-04-05 20:54:10 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/testflock"
|
2018-04-04 19:35:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2018-04-05 20:54:10 +02:00
|
|
|
testflock.Lock()
|
|
|
|
defer testflock.Unlock()
|
|
|
|
|
2018-04-04 19:35:30 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-04-05 18:12:08 +02:00
|
|
|
const bigSize = 2049
|
|
|
|
|
2018-04-05 20:54:10 +02:00
|
|
|
func TestEnsureNotShared(t *testing.T) {
|
2018-04-04 19:35:30 +02:00
|
|
|
// Create img1 and img2 with this size so that the next images are allocated
|
|
|
|
// with non-upper-left location.
|
2018-04-05 18:12:08 +02:00
|
|
|
img1 := NewImage(bigSize, 100)
|
2018-04-04 19:35:30 +02:00
|
|
|
defer img1.Dispose()
|
2018-04-29 12:15:04 +02:00
|
|
|
// Ensure img1's region is allocated.
|
|
|
|
img1.ReplacePixels(make([]byte, 4*bigSize*100))
|
2018-04-04 19:35:30 +02:00
|
|
|
|
2018-04-05 18:12:08 +02:00
|
|
|
img2 := NewImage(100, bigSize)
|
2018-04-04 19:35:30 +02:00
|
|
|
defer img2.Dispose()
|
2018-04-29 12:15:04 +02:00
|
|
|
img2.ReplacePixels(make([]byte, 4*100*bigSize))
|
2018-04-04 19:35:30 +02:00
|
|
|
|
|
|
|
const size = 32
|
|
|
|
|
|
|
|
img3 := NewImage(size/2, size/2)
|
|
|
|
defer img3.Dispose()
|
2018-04-29 12:15:04 +02:00
|
|
|
img3.ReplacePixels(make([]byte, (size/2)*(size/2)*4))
|
2018-04-04 19:35:30 +02:00
|
|
|
|
|
|
|
img4 := NewImage(size, size)
|
|
|
|
defer img4.Dispose()
|
|
|
|
|
|
|
|
pix := make([]byte, size*size*4)
|
|
|
|
for j := 0; j < size; j++ {
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
pix[4*(i+j*size)] = byte(i + j)
|
|
|
|
pix[4*(i+j*size)+1] = byte(i + j)
|
|
|
|
pix[4*(i+j*size)+2] = byte(i + j)
|
|
|
|
pix[4*(i+j*size)+3] = byte(i + j)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
img4.ReplacePixels(pix)
|
|
|
|
|
|
|
|
const (
|
|
|
|
dx0 = size / 4
|
|
|
|
dy0 = size / 4
|
|
|
|
dx1 = size * 3 / 4
|
|
|
|
dy1 = size * 3 / 4
|
|
|
|
)
|
|
|
|
// img4.ensureNotShared() should be called.
|
2018-08-09 20:33:28 +02:00
|
|
|
vs := img3.QuadVertices(0, 0, size/2, size/2, 1, 0, 0, 1, size/4, size/4, 1, 1, 1, 1)
|
2018-10-28 15:03:06 +01:00
|
|
|
is := graphics.QuadIndices()
|
2018-12-23 19:00:00 +01:00
|
|
|
img4.DrawImage(img3, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero)
|
2018-07-11 18:40:24 +02:00
|
|
|
want := false
|
|
|
|
if got := img4.IsSharedForTesting(); got != want {
|
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
2018-04-04 19:35:30 +02:00
|
|
|
|
|
|
|
for j := 0; j < size; j++ {
|
|
|
|
for i := 0; i < size; i++ {
|
2019-01-13 20:25:39 +01:00
|
|
|
r, g, b, a := img4.At(i, j)
|
|
|
|
got := color.RGBA{r, g, b, a}
|
2018-04-04 19:35:30 +02:00
|
|
|
var want color.RGBA
|
|
|
|
if i < dx0 || dx1 <= i || j < dy0 || dy1 <= j {
|
|
|
|
c := byte(i + j)
|
|
|
|
want = color.RGBA{c, c, c, c}
|
|
|
|
}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("img4.At(%d, %d): got: %v, want: %v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-03 07:10:43 +02:00
|
|
|
|
|
|
|
// Check further drawing doesn't cause panic.
|
|
|
|
// This bug was fixed by 03dcd948.
|
2018-12-23 19:00:00 +01:00
|
|
|
img4.DrawImage(img3, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero)
|
2018-04-04 19:35:30 +02:00
|
|
|
}
|
2018-07-11 18:40:24 +02:00
|
|
|
|
2018-08-08 17:19:55 +02:00
|
|
|
func Disabled_TestReshared(t *testing.T) {
|
2018-07-11 18:40:24 +02:00
|
|
|
const size = 16
|
|
|
|
|
|
|
|
img0 := NewImage(size, size)
|
|
|
|
defer img0.Dispose()
|
|
|
|
img0.ReplacePixels(make([]byte, 4*size*size))
|
|
|
|
|
|
|
|
img1 := NewImage(size, size)
|
|
|
|
defer img1.Dispose()
|
|
|
|
img1.ReplacePixels(make([]byte, 4*size*size))
|
|
|
|
want := true
|
|
|
|
if got := img1.IsSharedForTesting(); got != want {
|
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
img2 := NewImage(size, size)
|
|
|
|
defer img2.Dispose()
|
|
|
|
pix := make([]byte, 4*size*size)
|
|
|
|
for j := 0; j < size; j++ {
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
pix[4*(i+j*size)] = byte(i + j)
|
|
|
|
pix[4*(i+j*size)+1] = byte(i + j)
|
|
|
|
pix[4*(i+j*size)+2] = byte(i + j)
|
|
|
|
pix[4*(i+j*size)+3] = byte(i + j)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
img2.ReplacePixels(pix)
|
|
|
|
|
2018-07-24 18:08:04 +02:00
|
|
|
img3 := NewVolatileImage(size, size)
|
|
|
|
defer img3.Dispose()
|
|
|
|
img1.ReplacePixels(make([]byte, 4*size*size))
|
|
|
|
want = false
|
|
|
|
if got := img3.IsSharedForTesting(); got != want {
|
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
|
2018-07-11 18:40:24 +02:00
|
|
|
// Use img1 as a render target.
|
2018-08-09 20:33:28 +02:00
|
|
|
vs := img2.QuadVertices(0, 0, size, size, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1)
|
2018-10-28 15:03:06 +01:00
|
|
|
is := graphics.QuadIndices()
|
2018-12-23 19:00:00 +01:00
|
|
|
img1.DrawImage(img2, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero)
|
2018-07-11 18:40:24 +02:00
|
|
|
want = false
|
|
|
|
if got := img1.IsSharedForTesting(); got != want {
|
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use img1 as a render source.
|
2018-07-21 21:32:36 +02:00
|
|
|
for i := 0; i < MaxCountForShare-1; i++ {
|
2018-12-23 19:00:00 +01:00
|
|
|
img0.DrawImage(img1, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero)
|
2018-07-11 18:40:24 +02:00
|
|
|
want := false
|
|
|
|
if got := img1.IsSharedForTesting(); got != want {
|
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for j := 0; j < size; j++ {
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
want := color.RGBA{byte(i + j), byte(i + j), byte(i + j), byte(i + j)}
|
2019-01-13 20:25:39 +01:00
|
|
|
r, g, b, a := img1.At(i, j)
|
|
|
|
got := color.RGBA{r, g, b, a}
|
2018-07-11 18:40:24 +02:00
|
|
|
if got != want {
|
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-23 19:00:00 +01:00
|
|
|
img0.DrawImage(img1, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero)
|
2018-07-11 18:40:24 +02:00
|
|
|
want = true
|
|
|
|
if got := img1.IsSharedForTesting(); got != want {
|
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
for j := 0; j < size; j++ {
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
want := color.RGBA{byte(i + j), byte(i + j), byte(i + j), byte(i + j)}
|
2019-01-13 20:25:39 +01:00
|
|
|
r, g, b, a := img1.At(i, j)
|
|
|
|
got := color.RGBA{r, g, b, a}
|
2018-07-11 18:40:24 +02:00
|
|
|
if got != want {
|
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-21 22:52:11 +02:00
|
|
|
|
2018-07-24 18:08:04 +02:00
|
|
|
// Use img3 as a render source. img3 never uses a shared texture.
|
|
|
|
for i := 0; i < MaxCountForShare*2; i++ {
|
2018-12-23 19:00:00 +01:00
|
|
|
img0.DrawImage(img3, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero)
|
2018-07-24 18:08:04 +02:00
|
|
|
want := false
|
|
|
|
if got := img3.IsSharedForTesting(); got != want {
|
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-21 22:52:11 +02:00
|
|
|
runtime.GC()
|
2018-07-11 18:40:24 +02:00
|
|
|
}
|
2018-12-15 11:51:17 +01:00
|
|
|
|
|
|
|
func TestExtend(t *testing.T) {
|
2018-12-15 21:09:43 +01:00
|
|
|
const w0, h0 = 100, 100
|
|
|
|
img0 := NewImage(w0, h0)
|
2019-01-10 15:22:59 +01:00
|
|
|
defer img0.Dispose()
|
2018-12-15 21:09:43 +01:00
|
|
|
p0 := make([]byte, 4*w0*h0)
|
|
|
|
for i := 0; i < w0*h0; i++ {
|
2018-12-15 11:51:17 +01:00
|
|
|
p0[4*i] = byte(i)
|
|
|
|
p0[4*i+1] = byte(i)
|
|
|
|
p0[4*i+2] = byte(i)
|
|
|
|
p0[4*i+3] = byte(i)
|
|
|
|
}
|
|
|
|
img0.ReplacePixels(p0)
|
|
|
|
|
2018-12-15 21:09:43 +01:00
|
|
|
const w1, h1 = 1025, 100
|
|
|
|
img1 := NewImage(w1, h1)
|
2019-01-10 15:22:59 +01:00
|
|
|
defer img1.Dispose()
|
2018-12-15 21:09:43 +01:00
|
|
|
p1 := make([]byte, 4*w1*h1)
|
|
|
|
for i := 0; i < w1*h1; i++ {
|
|
|
|
p1[4*i] = byte(i)
|
|
|
|
p1[4*i+1] = byte(i)
|
|
|
|
p1[4*i+2] = byte(i)
|
|
|
|
p1[4*i+3] = byte(i)
|
|
|
|
}
|
2018-12-15 11:51:17 +01:00
|
|
|
// Ensure to allocate
|
|
|
|
img1.ReplacePixels(p1)
|
|
|
|
|
2018-12-15 21:09:43 +01:00
|
|
|
for j := 0; j < h0; j++ {
|
|
|
|
for i := 0; i < w0; i++ {
|
2019-01-13 20:25:39 +01:00
|
|
|
r, g, b, a := img0.At(i, j)
|
|
|
|
got := color.RGBA{r, g, b, a}
|
2018-12-15 21:09:43 +01:00
|
|
|
c := byte(i + w0*j)
|
2018-12-15 11:51:17 +01:00
|
|
|
want := color.RGBA{c, c, c, c}
|
|
|
|
if got != want {
|
2018-12-15 21:09:43 +01:00
|
|
|
t.Errorf("img0.At(%d, %d): got: %v, want: %v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for j := 0; j < h1; j++ {
|
|
|
|
for i := 0; i < w1; i++ {
|
2019-01-13 20:25:39 +01:00
|
|
|
r, g, b, a := img1.At(i, j)
|
|
|
|
got := color.RGBA{r, g, b, a}
|
2018-12-15 21:09:43 +01:00
|
|
|
c := byte(i + w1*j)
|
|
|
|
want := color.RGBA{c, c, c, c}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("img1.At(%d, %d): got: %v, want: %v", i, j, got, want)
|
2018-12-15 11:51:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
img0.Dispose()
|
|
|
|
img1.Dispose()
|
|
|
|
}
|
2018-12-16 07:13:35 +01:00
|
|
|
|
|
|
|
func TestReplacePixelsAfterDrawImage(t *testing.T) {
|
|
|
|
const w, h = 256, 256
|
|
|
|
src := NewImage(w, h)
|
2019-01-10 15:22:59 +01:00
|
|
|
defer src.Dispose()
|
2018-12-16 07:13:35 +01:00
|
|
|
dst := NewImage(w, h)
|
2019-01-10 15:22:59 +01:00
|
|
|
defer dst.Dispose()
|
2018-12-16 07:13:35 +01: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] = byte(i)
|
|
|
|
}
|
|
|
|
src.ReplacePixels(pix)
|
|
|
|
|
|
|
|
vs := src.QuadVertices(0, 0, w, h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1)
|
|
|
|
is := graphics.QuadIndices()
|
2018-12-23 19:00:00 +01:00
|
|
|
dst.DrawImage(src, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest, graphics.AddressClampToZero)
|
2018-12-16 07:13:35 +01:00
|
|
|
dst.ReplacePixels(pix)
|
|
|
|
|
|
|
|
for j := 0; j < h; j++ {
|
|
|
|
for i := 0; i < w; i++ {
|
2019-01-13 20:25:39 +01:00
|
|
|
r, g, b, a := dst.At(i, j)
|
|
|
|
got := color.RGBA{r, g, b, a}
|
2018-12-16 07:13:35 +01:00
|
|
|
c := byte(i + w*j)
|
|
|
|
want := color.RGBA{c, c, c, c}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("dst.At(%d, %d): got %v, want: %v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-02 18:39:36 +01:00
|
|
|
|
|
|
|
// TODO: Add tests to extend shareable image out of the main loop
|