internal/buffered: simplify the API

This commit is contained in:
Hajime Hoshi 2022-03-21 15:19:06 +09:00
parent 5c3f8915d1
commit c316aaae72
4 changed files with 15 additions and 44 deletions

View File

@ -741,7 +741,7 @@ func (i *Image) Set(x, y int, clr color.Color) {
} }
r, g, b, a := clr.RGBA() r, g, b, a := clr.RGBA()
i.image.ReplacePartialPixels([]byte{byte(r >> 8), byte(g >> 8), byte(b >> 8), byte(a >> 8)}, x, y, 1, 1) i.image.ReplacePixels([]byte{byte(r >> 8), byte(g >> 8), byte(b >> 8), byte(a >> 8)}, x, y, 1, 1)
} }
// Dispose disposes the image data. // Dispose disposes the image data.
@ -783,16 +783,11 @@ func (i *Image) ReplacePixels(pixels []byte) {
return return
} }
if !i.isSubImage() {
i.image.ReplacePixels(pixels)
return
}
r := i.Bounds() r := i.Bounds()
// Do not need to copy pixels here. // Do not need to copy pixels here.
// * In internal/mipmap, pixels are copied when necessary. // * In internal/mipmap, pixels are copied when necessary.
// * In internal/atlas, pixels are copied to make its paddings. // * In internal/atlas, pixels are copied to make its paddings.
i.image.ReplacePartialPixels(pixels, r.Min.X, r.Min.Y, r.Dx(), r.Dy()) i.image.ReplacePixels(pixels, r.Min.X, r.Min.Y, r.Dx(), r.Dy())
} }
// NewImage returns an empty image. // NewImage returns an empty image.

View File

@ -174,9 +174,8 @@ func (i *Image) DumpScreenshot(graphicsDriver graphicsdriver.Graphics, name stri
} }
// ReplacePixels replaces the pixels at the specified region. // ReplacePixels replaces the pixels at the specified region.
// This call is not accumulated and send one draw call to replace pixels. func (i *Image) ReplacePixels(pix []byte, x, y, width, height int) {
func (i *Image) ReplacePixels(pix []byte) { if l := 4 * width * height; len(pix) != l {
if l := 4 * i.width * i.height; len(pix) != l {
panic(fmt.Sprintf("buffered: len(pix) was %d but must be %d", len(pix), l)) panic(fmt.Sprintf("buffered: len(pix) was %d but must be %d", len(pix), l))
} }
@ -184,35 +183,21 @@ func (i *Image) ReplacePixels(pix []byte) {
copied := make([]byte, len(pix)) copied := make([]byte, len(pix))
copy(copied, pix) copy(copied, pix)
if tryAddDelayedCommand(func() error { if tryAddDelayedCommand(func() error {
i.ReplacePixels(copied) i.ReplacePixels(copied, x, y, width, height)
return nil return nil
}) { }) {
return return
} }
} }
i.invalidatePendingPixels() if x == 0 && y == 0 && width == i.width && height == i.height {
i.invalidatePendingPixels()
i.img.ReplacePixels(pix, nil) i.img.ReplacePixels(pix, nil)
} return
// ReplacePartial replaces the pixel at the specified partial region.
// This call might be accumulated and send one draw call to replace pixels for the accumulated calls.
func (i *Image) ReplacePartialPixels(pix []byte, x, y, width, height int) {
if l := 4 * width * height; len(pix) != l {
panic(fmt.Sprintf("buffered: len(pix) was %d but must be %d", len(pix), l))
} }
if maybeCanAddDelayedCommand() { // TODO: If width/height is big enough, ReplacePixels can be called instead of replacePendingPixels.
if tryAddDelayedCommand(func() error { // Check if this is efficient.
copied := make([]byte, len(pix))
copy(copied, pix)
i.ReplacePartialPixels(copied, x, y, width, height)
return nil
}) {
return
}
}
i.replacePendingPixels(pix, x, y, width, height) i.replacePendingPixels(pix, x, y, width, height)
} }

View File

@ -71,13 +71,8 @@ func (m *Mipmap) DumpScreenshot(graphicsDriver graphicsdriver.Graphics, name str
return m.orig.DumpScreenshot(graphicsDriver, name, blackbg) return m.orig.DumpScreenshot(graphicsDriver, name, blackbg)
} }
func (m *Mipmap) ReplacePixels(pix []byte) { func (m *Mipmap) ReplacePixels(pix []byte, x, y, width, height int) {
m.orig.ReplacePixels(pix) m.orig.ReplacePixels(pix, x, y, width, height)
m.disposeMipmaps()
}
func (m *Mipmap) ReplacePartialPixels(pix []byte, x, y, width, height int) {
m.orig.ReplacePartialPixels(pix, x, y, width, height)
m.disposeMipmaps() m.disposeMipmaps()
} }

View File

@ -68,12 +68,8 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderImageNum]*Image, vertices []f
i.mipmap.DrawTriangles(srcMipmaps, vertices, indices, colorm, mode, filter, address, dstRegion, srcRegion, subimageOffsets, s, uniforms, evenOdd, canSkipMipmap) i.mipmap.DrawTriangles(srcMipmaps, vertices, indices, colorm, mode, filter, address, dstRegion, srcRegion, subimageOffsets, s, uniforms, evenOdd, canSkipMipmap)
} }
func (i *Image) ReplacePixels(pix []byte) { func (i *Image) ReplacePixels(pix []byte, x, y, width, height int) {
i.mipmap.ReplacePixels(pix) i.mipmap.ReplacePixels(pix, x, y, width, height)
}
func (i *Image) ReplacePartialPixels(pix []byte, x, y, width, height int) {
i.mipmap.ReplacePartialPixels(pix, x, y, width, height)
} }
func (i *Image) At(x, y int) (r, g, b, a byte) { func (i *Image) At(x, y int) (r, g, b, a byte) {