internal/atlas: refactoring

This commit is contained in:
Hajime Hoshi 2022-03-20 18:07:29 +09:00
parent 54b4e87506
commit 367a9ec5bf
4 changed files with 30 additions and 39 deletions

View File

@ -337,7 +337,7 @@ func (i *Image) putOnAtlas(graphicsDriver graphicsdriver.Graphics) error {
pixels[4*(i.width*y+x)+3] = a
}
}
newI.replacePixels(pixels, 0, 0, i.width, i.height)
newI.replacePixels(pixels)
} else {
// If the underlying graphics driver doesn't require restoring from the context lost, just a regular
// rendering works.
@ -538,13 +538,16 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderImageNum]*Image, vertices []f
}
}
func (i *Image) ReplacePixels(pix []byte, x, y, width, height int) {
// ReplacePixels replaces the pixels on the image.
// ReplacePixels cannot take a region due to the current implementation.
// internal/restorable.Image has to record the areas of replaced pixels, and the areas must not be overlapped so far.
func (i *Image) ReplacePixels(pix []byte) {
backendsM.Lock()
defer backendsM.Unlock()
i.replacePixels(pix, x, y, width, height)
i.replacePixels(pix)
}
func (i *Image) replacePixels(pix []byte, x, y, width, height int) {
func (i *Image) replacePixels(pix []byte) {
if i.disposed {
panic("atlas: the image must not be disposed at replacePixels")
}
@ -558,18 +561,6 @@ func (i *Image) replacePixels(pix []byte, x, y, width, height int) {
i.allocate(true)
}
// If the replacing area is small, replace the pixels without the padding.
if x != 0 || y != 0 || width != i.width || height != i.height {
ox, oy, _, _ := i.regionWithPadding()
x += ox + paddingSize
y += oy + paddingSize
copied := make([]byte, len(pix))
copy(copied, pix)
i.backend.restorable.ReplacePixels(copied, x, y, width, height)
return
}
// If the whole area is being replaced, add the padding.
px, py, pw, ph := i.regionWithPadding()
if pix == nil {
i.backend.restorable.ReplacePixels(nil, px, py, pw, ph)

View File

@ -63,17 +63,17 @@ func TestEnsureIsolated(t *testing.T) {
img1 := atlas.NewImage(bigSize, 100)
defer img1.MarkDisposed()
// Ensure img1's region is allocated.
img1.ReplacePixels(make([]byte, 4*bigSize*100), 0, 0, bigSize, 100)
img1.ReplacePixels(make([]byte, 4*bigSize*100))
img2 := atlas.NewImage(100, bigSize)
defer img2.MarkDisposed()
img2.ReplacePixels(make([]byte, 4*100*bigSize), 0, 0, 100, bigSize)
img2.ReplacePixels(make([]byte, 4*100*bigSize))
const size = 32
img3 := atlas.NewImage(size/2, size/2)
defer img3.MarkDisposed()
img3.ReplacePixels(make([]byte, (size/2)*(size/2)*4), 0, 0, size/2, size/2)
img3.ReplacePixels(make([]byte, (size/2)*(size/2)*4))
img4 := atlas.NewImage(size, size)
defer img4.MarkDisposed()
@ -87,7 +87,7 @@ func TestEnsureIsolated(t *testing.T) {
pix[4*(i+j*size)+3] = byte(i + j)
}
}
img4.ReplacePixels(pix, 0, 0, size, size)
img4.ReplacePixels(pix)
const (
dx0 = size / 4
@ -142,11 +142,11 @@ func TestReputOnAtlas(t *testing.T) {
img0 := atlas.NewImage(size, size)
defer img0.MarkDisposed()
img0.ReplacePixels(make([]byte, 4*size*size), 0, 0, size, size)
img0.ReplacePixels(make([]byte, 4*size*size))
img1 := atlas.NewImage(size, size)
defer img1.MarkDisposed()
img1.ReplacePixels(make([]byte, 4*size*size), 0, 0, size, size)
img1.ReplacePixels(make([]byte, 4*size*size))
if got, want := img1.IsOnAtlasForTesting(), true; got != want {
t.Errorf("got: %v, want: %v", got, want)
}
@ -162,12 +162,12 @@ func TestReputOnAtlas(t *testing.T) {
pix[4*(i+j*size)+3] = byte(i + j)
}
}
img2.ReplacePixels(pix, 0, 0, size, size)
img2.ReplacePixels(pix)
img3 := atlas.NewImage(size, size)
img3.SetVolatile(true)
defer img3.MarkDisposed()
img1.ReplacePixels(make([]byte, 4*size*size), 0, 0, size, size)
img1.ReplacePixels(make([]byte, 4*size*size))
if got, want := img3.IsOnAtlasForTesting(), false; got != want {
t.Errorf("got: %v, want: %v", got, want)
}
@ -256,7 +256,7 @@ func TestReputOnAtlas(t *testing.T) {
if err := atlas.PutImagesOnAtlasForTesting(ui.GraphicsDriverForTesting()); err != nil {
t.Fatal(err)
}
img1.ReplacePixels(make([]byte, 4*size*size), 0, 0, size, size)
img1.ReplacePixels(make([]byte, 4*size*size))
img0.DrawTriangles([graphics.ShaderImageNum]*atlas.Image{img1}, vs, is, affine.ColorMIdentity{}, graphicsdriver.CompositeModeCopy, graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, dr, graphicsdriver.Region{}, [graphics.ShaderImageNum - 1][2]float32{}, nil, nil, false)
if got, want := img1.IsOnAtlasForTesting(), false; got != want {
t.Errorf("got: %v, want: %v", got, want)
@ -298,7 +298,7 @@ func TestExtend(t *testing.T) {
p0[4*i+2] = byte(i)
p0[4*i+3] = byte(i)
}
img0.ReplacePixels(p0, 0, 0, w0, h0)
img0.ReplacePixels(p0)
const w1, h1 = minImageSizeForTesting + 1, 100
img1 := atlas.NewImage(w1, h1)
@ -312,7 +312,7 @@ func TestExtend(t *testing.T) {
p1[4*i+3] = byte(i)
}
// Ensure to allocate
img1.ReplacePixels(p1, 0, 0, w1, h1)
img1.ReplacePixels(p1)
pix0, err := img0.Pixels(ui.GraphicsDriverForTesting(), 0, 0, w0, h0)
if err != nil {
@ -367,7 +367,7 @@ func TestReplacePixelsAfterDrawTriangles(t *testing.T) {
pix[4*i+2] = byte(i)
pix[4*i+3] = byte(i)
}
src.ReplacePixels(pix, 0, 0, w, h)
src.ReplacePixels(pix)
vs := quadVertices(w, h, 0, 0, 1)
is := graphics.QuadIndices()
@ -378,7 +378,7 @@ func TestReplacePixelsAfterDrawTriangles(t *testing.T) {
Height: h,
}
dst.DrawTriangles([graphics.ShaderImageNum]*atlas.Image{src}, vs, is, affine.ColorMIdentity{}, graphicsdriver.CompositeModeCopy, graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, dr, graphicsdriver.Region{}, [graphics.ShaderImageNum - 1][2]float32{}, nil, nil, false)
dst.ReplacePixels(pix, 0, 0, w, h)
dst.ReplacePixels(pix)
pix, err := dst.Pixels(ui.GraphicsDriverForTesting(), 0, 0, w, h)
if err != nil {
@ -415,7 +415,7 @@ func TestSmallImages(t *testing.T) {
pix[4*i+2] = 0xff
pix[4*i+3] = 0xff
}
src.ReplacePixels(pix, 0, 0, w, h)
src.ReplacePixels(pix)
vs := quadVertices(w, h, 0, 0, 1)
is := graphics.QuadIndices()
@ -462,7 +462,7 @@ func TestLongImages(t *testing.T) {
pix[4*i+2] = 0xff
pix[4*i+3] = 0xff
}
src.ReplacePixels(pix, 0, 0, w, h)
src.ReplacePixels(pix)
const scale = 120
vs := quadVertices(w, h, 0, 0, scale)
@ -512,12 +512,12 @@ func TestExtendWithBigImage(t *testing.T) {
img0 := atlas.NewImage(1, 1)
defer img0.MarkDisposed()
img0.ReplacePixels(make([]byte, 4*1*1), 0, 0, 1, 1)
img0.ReplacePixels(make([]byte, 4*1*1))
img1 := atlas.NewImage(minImageSizeForTesting+1, minImageSizeForTesting+1)
defer img1.MarkDisposed()
img1.ReplacePixels(make([]byte, 4*(minImageSizeForTesting+1)*(minImageSizeForTesting+1)), 0, 0, minImageSizeForTesting+1, minImageSizeForTesting+1)
img1.ReplacePixels(make([]byte, 4*(minImageSizeForTesting+1)*(minImageSizeForTesting+1)))
}
// Issue #1217
@ -526,7 +526,7 @@ func TestMaxImageSize(t *testing.T) {
s := maxImageSizeForTesting - 2*atlas.PaddingSize
img := atlas.NewImage(s, s)
defer img.MarkDisposed()
img.ReplacePixels(make([]byte, 4*s*s), 0, 0, s, s)
img.ReplacePixels(make([]byte, 4*s*s))
}
// Issue #1217 (disabled)
@ -539,7 +539,7 @@ func Disable_TestMinImageSize(t *testing.T) {
s := minImageSizeForTesting
img := atlas.NewImage(s, s)
defer img.MarkDisposed()
img.ReplacePixels(make([]byte, 4*s*s), 0, 0, s, s)
img.ReplacePixels(make([]byte, 4*s*s))
}
// Issue #1421

View File

@ -65,9 +65,9 @@ func TestImageDrawTwice(t *testing.T) {
dst := atlas.NewImage(w, h)
src0 := atlas.NewImage(w, h)
src0.ReplacePixels([]byte{0xff, 0xff, 0xff, 0xff}, 0, 0, w, h)
src0.ReplacePixels([]byte{0xff, 0xff, 0xff, 0xff})
src1 := atlas.NewImage(w, h)
src1.ReplacePixels([]byte{0x80, 0x80, 0x80, 0xff}, 0, 0, w, h)
src1.ReplacePixels([]byte{0x80, 0x80, 0x80, 0xff})
vs := quadVertices(w, h, 0, 0, 1)
is := graphics.QuadIndices()

View File

@ -118,7 +118,7 @@ func (i *Image) invalidatePendingPixels() {
func (i *Image) resolvePendingPixels(keepPendingPixels bool) {
if i.needsToResolvePixels {
i.img.ReplacePixels(i.pixels, 0, 0, i.width, i.height)
i.img.ReplacePixels(i.pixels)
if !keepPendingPixels {
i.pixels = nil
}
@ -187,7 +187,7 @@ func (i *Image) ReplacePixels(pix []byte) {
i.invalidatePendingPixels()
i.img.ReplacePixels(pix, 0, 0, i.width, i.height)
i.img.ReplacePixels(pix)
}
// ReplacePartial replaces the pixel at the specified partial region.