diff --git a/internal/buffered/image.go b/internal/buffered/image.go index 3c9f2197d..0b215cf3d 100644 --- a/internal/buffered/image.go +++ b/internal/buffered/image.go @@ -222,6 +222,9 @@ func (i *Image) ReplacePixels(pix []byte) { i.img.ReplacePixels(pix) } +// DrawTriangles draws the src image with the given vertices. +// +// Copying vertices and indices is the caller's responsibility. func (i *Image) DrawTriangles(src *Image, vertices []float32, indices []uint16, colorm *affine.ColorM, mode driver.CompositeMode, filter driver.Filter, address driver.Address) { if i == src { panic("buffered: Image.DrawTriangles: src must be different from the receiver") @@ -232,6 +235,7 @@ func (i *Image) DrawTriangles(src *Image, vertices []float32, indices []uint16, delayedCommands = append(delayedCommands, func() { src.resolvePendingPixels(true) i.resolvePendingPixels(false) + // Arguments are not copied. Copying is the caller's responsibility. i.img.DrawTriangles(src.img, vertices, indices, colorm, mode, filter, address) }) delayedCommandsM.Unlock() diff --git a/internal/buffered/image_test.go b/internal/buffered/image_test.go index d693839a5..1ab2789e9 100644 --- a/internal/buffered/image_test.go +++ b/internal/buffered/image_test.go @@ -206,3 +206,43 @@ func TestSetAndReplacePixelsBeforeMain(t *testing.T) { t.Errorf("got: %v, want: %v", got, want) } } + +var testReplacePixelsAndModifyBeforeMainResult = func() testResult { + img, _ := ebiten.NewImage(16, 16, ebiten.FilterDefault) + pix := make([]byte, 4*16*16) + for i := 0; i < len(pix)/4; i++ { + pix[4*i] = 1 + pix[4*i+1] = 2 + pix[4*i+2] = 3 + pix[4*i+3] = 4 + } + img.ReplacePixels(pix) + // After calling ReplacePixels, modifying pix must not affect the result. + for i := 0; i < len(pix)/4; i++ { + pix[4*i] = 5 + pix[4*i+1] = 6 + pix[4*i+2] = 7 + pix[4*i+3] = 8 + } + + ch := make(chan color.RGBA, 1) + go func() { + runOnMainThread(func() { + ch <- img.At(0, 0).(color.RGBA) + }) + }() + + return testResult{ + want: color.RGBA{1, 2, 3, 4}, + got: ch, + } +}() + +func TestReplacePixelsAndModifyBeforeMain(t *testing.T) { + got := <-testReplacePixelsAndModifyBeforeMainResult.got + want := testReplacePixelsAndModifyBeforeMainResult.want + + if got != want { + t.Errorf("got: %v, want: %v", got, want) + } +}