internal/atlas: replace Pixels with At to reduce unnecessary slice allocations

This commit is contained in:
Hajime Hoshi 2022-03-20 18:38:29 +09:00
parent 11ff0ab48c
commit 14c327c89b
4 changed files with 14 additions and 20 deletions

View File

@ -718,11 +718,7 @@ func (i *Image) at(x, y int) (r, g, b, a uint8) {
if !image.Pt(x, y).In(i.Bounds()) { if !image.Pt(x, y).In(i.Bounds()) {
return 0, 0, 0, 0 return 0, 0, 0, 0
} }
pix := i.image.Pixels(x, y, 1, 1) return i.image.At(x, y)
if len(pix) == 0 {
return 0, 0, 0, 0
}
return pix[0], pix[1], pix[2], pix[3]
} }
// Set sets the color at (x, y). // Set sets the color at (x, y).

View File

@ -138,21 +138,19 @@ func (i *Image) MarkDisposed() {
i.img.MarkDisposed() i.img.MarkDisposed()
} }
func (img *Image) ReadPixels(graphicsDriver graphicsdriver.Graphics, pix []byte, x, y, width, height int) (err error) { func (img *Image) At(graphicsDriver graphicsdriver.Graphics, x, y int) (r, g, b, a byte, err error) {
checkDelayedCommandsFlushed("Pixels") checkDelayedCommandsFlushed("At")
if img.pixels == nil { if img.pixels == nil {
pix, err := img.img.Pixels(graphicsDriver) pix, err := img.img.Pixels(graphicsDriver)
if err != nil { if err != nil {
return err return 0, 0, 0, 0, err
} }
img.pixels = pix img.pixels = pix
} }
for j := 0; j < height; j++ { idx := 4 * (y*img.width + x)
copy(pix[4*j*width:4*(j+1)*width], img.pixels[4*((j+y)*img.width+x):]) return img.pixels[idx], img.pixels[idx+1], img.pixels[idx+2], img.pixels[idx+3], nil
}
return nil
} }
func (i *Image) DumpScreenshot(graphicsDriver graphicsdriver.Graphics, name string, blackbg bool) error { func (i *Image) DumpScreenshot(graphicsDriver graphicsdriver.Graphics, name string, blackbg bool) error {

View File

@ -84,8 +84,8 @@ func (m *Mipmap) ReplacePartialPixels(graphicsDriver graphicsdriver.Graphics, pi
return nil return nil
} }
func (m *Mipmap) ReadPixels(graphicsDriver graphicsdriver.Graphics, pix []byte, x, y, width, height int) error { func (m *Mipmap) At(graphicsDriver graphicsdriver.Graphics, x, y int) (r, g, b, a byte, err error) {
return m.orig.ReadPixels(graphicsDriver, pix, x, y, width, height) return m.orig.At(graphicsDriver, x, y)
} }
func (m *Mipmap) DrawTriangles(srcs [graphics.ShaderImageNum]*Mipmap, vertices []float32, indices []uint16, colorm affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageNum - 1][2]float32, shader *Shader, uniforms [][]float32, evenOdd bool, canSkipMipmap bool) { func (m *Mipmap) DrawTriangles(srcs [graphics.ShaderImageNum]*Mipmap, vertices []float32, indices []uint16, colorm affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageNum - 1][2]float32, shader *Shader, uniforms [][]float32, evenOdd bool, canSkipMipmap bool) {

View File

@ -81,21 +81,21 @@ func (i *Image) ReplacePartialPixels(pix []byte, x, y, width, height int) {
} }
} }
func (i *Image) Pixels(x, y, width, height int) []byte { func (i *Image) At(x, y int) (r, g, b, a byte) {
// Check the error existence and avoid unnecessary calls. // Check the error existence and avoid unnecessary calls.
if theGlobalState.error() != nil { if theGlobalState.error() != nil {
return nil return 0, 0, 0, 0
} }
pix := make([]byte, 4*width*height) r, g, b, a, err := i.mipmap.At(graphicsDriver(), x, y)
if err := i.mipmap.ReadPixels(graphicsDriver(), pix, x, y, width, height); err != nil { if err != nil {
if panicOnErrorOnReadingPixels { if panicOnErrorOnReadingPixels {
panic(err) panic(err)
} }
theGlobalState.setError(err) theGlobalState.setError(err)
return nil return 0, 0, 0, 0
} }
return pix return r, g, b, a
} }
func (i *Image) DumpScreenshot(name string, blackbg bool) error { func (i *Image) DumpScreenshot(name string, blackbg bool) error {