draw optimizations

This commit is contained in:
IliaDenisov
2026-03-08 23:30:11 +02:00
parent fdcbb5d6f4
commit ac35360d60
18 changed files with 875 additions and 566 deletions
+43 -29
View File
@@ -290,19 +290,26 @@ func (d *GGDrawer) ClearAllTo(bg color.Color) {
panic("GGDrawer.ClearAllTo: backing image is not *image.RGBA")
}
r, g, b, a := bg.RGBA()
// Convert from 16-bit range to 8-bit.
R := byte(r >> 8)
G := byte(g >> 8)
B := byte(b >> 8)
A := byte(a >> 8)
R, G, B, A := rgba8(bg)
p := img.Pix
for i := 0; i+3 < len(p); i += 4 {
p[i+0] = R
p[i+1] = G
p[i+2] = B
p[i+3] = A
// Prepare one full scanline once.
w := img.Bounds().Dx()
if w <= 0 {
return
}
line := make([]byte, w*4)
for i := 0; i < len(line); i += 4 {
line[i+0] = R
line[i+1] = G
line[i+2] = B
line[i+3] = A
}
// Copy scanline into each row (fast memmove).
h := img.Bounds().Dy()
for y := 0; y < h; y++ {
off := y * img.Stride
copy(img.Pix[off:off+w*4], line)
}
}
@@ -316,33 +323,40 @@ func (d *GGDrawer) ClearRectTo(x, y, w, h int, bg color.Color) {
panic("GGDrawer.ClearRectTo: backing image is not *image.RGBA")
}
bounds := img.Bounds()
x0 := max(x, bounds.Min.X)
y0 := max(y, bounds.Min.Y)
x1 := min(x+w, bounds.Max.X)
y1 := min(y+h, bounds.Max.Y)
b := img.Bounds()
x0 := max(x, b.Min.X)
y0 := max(y, b.Min.Y)
x1 := min(x+w, b.Max.X)
y1 := min(y+h, b.Max.Y)
if x0 >= x1 || y0 >= y1 {
return
}
r, g, b, a := bg.RGBA()
R := byte(r >> 8)
G := byte(g >> 8)
B := byte(b >> 8)
A := byte(a >> 8)
R, G, B, A := rgba8(bg)
rowPx := x1 - x0
rowBytes := rowPx * 4
// Build one row once for this rect width.
line := make([]byte, rowBytes)
for i := 0; i < rowBytes; i += 4 {
line[i+0] = R
line[i+1] = G
line[i+2] = B
line[i+3] = A
}
rowBytes := (x1 - x0) * 4
for yy := y0; yy < y1; yy++ {
off := yy*img.Stride + x0*4
for i := 0; i < rowBytes; i += 4 {
img.Pix[off+i+0] = R
img.Pix[off+i+1] = G
img.Pix[off+i+2] = B
img.Pix[off+i+3] = A
}
copy(img.Pix[off:off+rowBytes], line)
}
}
func rgba8(c color.Color) (R, G, B, A byte) {
r, g, b, a := c.RGBA()
return byte(r >> 8), byte(g >> 8), byte(b >> 8), byte(a >> 8)
}
func (g *GGDrawer) DrawImage(img image.Image, x, y int) {
g.DC.DrawImage(img, x, y)
}