Skip to content

Commit

Permalink
optimize pixel copying path for destination type *fbimage.BGRA
Browse files Browse the repository at this point in the history
This specialization brings down copying time to 5ms (from 60-70ms) on an
amd64 qemu VM with virtio VGA.
  • Loading branch information
bradfitz committed Apr 3, 2023
1 parent 3f778c6 commit 192fa29
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions fbstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,14 @@ func (d *statusDrawer) draw1(ctx context.Context) error {
// using the pan ioctl when using the frame buffer), but in practice
// updates seem smooth enough, most likely because we are only
// updating timestamps.
if x, ok := d.img.(*fbimage.BGR565); ok {
switch x := d.img.(type) {
case *fbimage.BGR565:
copyRGBAtoBGR565(x, d.buffer)
} else {
case *fbimage.BGRA:
copyRGBAtoBGRA(x, d.buffer)
default:
if !d.slowPathNotified {
log.Printf("framebuffer not using pixel format BGR565, falling back to slow path")
log.Printf("framebuffer not using pixel format BGR565, falling back to slow path for img type %T", d.img)
d.slowPathNotified = true
}
draw.Draw(d.img, d.bounds, d.buffer, image.Point{}, draw.Src)
Expand Down Expand Up @@ -521,6 +524,19 @@ func copyRGBAtoBGR565(dst *fbimage.BGR565, src *image.RGBA) {
}
}

// copyRGBAtoBGRA is an inlined version of the hot pixel copying loop for the
// special case of copying from an *image.RGBA to an *fbimage.BGRA.
//
// This specialization brings down copying time to 5ms (from 60-70ms) on an
// amd64 qemu VM with virtio VGA.
func copyRGBAtoBGRA(dst *fbimage.BGRA, src *image.RGBA) {
for i := 0; i < len(src.Pix); i += 4 {
s := src.Pix[i : i+4 : i+4]
d := dst.Pix[i : i+4 : i+4]
d[0], d[1], d[2], d[3] = s[2], s[1], s[0], s[3]
}
}

//go:embed "gokrazy.png"
var gokrazyLogoPNG []byte

Expand Down

0 comments on commit 192fa29

Please sign in to comment.