shareable: Fix Image.DrawImage to accept vertices and indices

This commit is contained in:
Hajime Hoshi 2018-07-21 03:30:27 +09:00
parent 2971fbb9ad
commit 2e7745044d
3 changed files with 26 additions and 7 deletions

View File

@ -126,6 +126,10 @@ func (i *Image) fill(r, g, b, a uint8) {
_ = i.DrawImage(emptyImage, op)
}
var (
quadIndices = []uint16{0, 1, 2, 1, 2, 3}
)
// DrawImage draws the given image on the image i.
//
// DrawImage accepts the options. For details, see the document of DrawImageOptions.
@ -238,7 +242,8 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) error {
}
a, b, c, d, tx, ty := geom.elements()
i.shareableImage.DrawImage(img.shareableImage, sx0, sy0, sx1, sy1, a, b, c, d, tx, ty, options.ColorM.impl, mode, filter)
vs := img.shareableImage.QuadVertices(sx0, sy0, sx1, sy1, a, b, c, d, tx, ty)
i.shareableImage.DrawImage(img.shareableImage, vs, quadIndices, options.ColorM.impl, mode, filter)
return nil
}

View File

@ -138,7 +138,16 @@ func (i *Image) Size() (width, height int) {
return i.width, i.height
}
func (i *Image) DrawImage(img *Image, sx0, sy0, sx1, sy1 int, a, b, c, d, tx, ty float32, colorm *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) {
func (i *Image) QuadVertices(sx0, sy0, sx1, sy1 int, a, b, c, d, tx, ty float32) []float32 {
if i.backend == nil {
i.allocate(true)
}
dx, dy, _, _ := i.region()
w, h := i.backend.restorable.SizePowerOf2()
return graphicsutil.QuadVertices(w, h, sx0+dx, sy0+dy, sx1+dx, sy1+dy, a, b, c, d, tx, ty)
}
func (i *Image) DrawImage(img *Image, vertices []float32, indices []uint16, colorm *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) {
backendsM.Lock()
defer backendsM.Unlock()
@ -160,10 +169,10 @@ func (i *Image) DrawImage(img *Image, sx0, sy0, sx1, sy1 int, a, b, c, d, tx, ty
panic("shareable: Image.DrawImage: img must be different from the receiver")
}
dx, dy, _, _ := img.region()
/*dx, dy, _, _ := img.region()
w, h := img.backend.restorable.SizePowerOf2()
vs := graphicsutil.QuadVertices(w, h, sx0+dx, sy0+dy, sx1+dx, sy1+dy, a, b, c, d, tx, ty)
i.backend.restorable.DrawImage(img.backend.restorable, vs, quadIndices, colorm, mode, filter)
vs := graphicsutil.QuadVertices(w, h, sx0+dx, sy0+dy, sx1+dx, sy1+dy, a, b, c, d, tx, ty)*/
i.backend.restorable.DrawImage(img.backend.restorable, vertices, indices, colorm, mode, filter)
}
func (i *Image) ReplacePixels(p []byte) {

View File

@ -45,6 +45,10 @@ func TestMain(m *testing.M) {
const bigSize = 2049
var (
quadIndices = []uint16{0, 1, 2, 1, 2, 3}
)
func TestEnsureNotShared(t *testing.T) {
// Create img1 and img2 with this size so that the next images are allocated
// with non-upper-left location.
@ -84,7 +88,8 @@ func TestEnsureNotShared(t *testing.T) {
dy1 = size * 3 / 4
)
// img4.ensureNotShared() should be called.
img4.DrawImage(img3, 0, 0, size/2, size/2, 1, 0, 0, 1, size/4, size/4, nil, opengl.CompositeModeCopy, graphics.FilterNearest)
vs := img3.QuadVertices(0, 0, size/2, size/2, 1, 0, 0, 1, size/4, size/4)
img4.DrawImage(img3, vs, quadIndices, nil, opengl.CompositeModeCopy, graphics.FilterNearest)
for j := 0; j < size; j++ {
for i := 0; i < size; i++ {
@ -102,5 +107,5 @@ func TestEnsureNotShared(t *testing.T) {
// Check further drawing doesn't cause panic.
// This bug was fixed by 03dcd948.
img4.DrawImage(img3, 0, 0, size/2, size/2, 1, 0, 0, 1, size/4, size/4, nil, opengl.CompositeModeCopy, graphics.FilterNearest)
img4.DrawImage(img3, vs, quadIndices, nil, opengl.CompositeModeCopy, graphics.FilterNearest)
}