xgb-image: add support for the MIT-SHM extension
This commit is contained in:
parent
cea1792913
commit
c8fd1068d1
|
@ -4,10 +4,13 @@ import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"github.com/BurntSushi/xgb"
|
"github.com/BurntSushi/xgb"
|
||||||
"github.com/BurntSushi/xgb/render"
|
"github.com/BurntSushi/xgb/render"
|
||||||
|
"github.com/BurntSushi/xgb/shm"
|
||||||
"github.com/BurntSushi/xgb/xproto"
|
"github.com/BurntSushi/xgb/xproto"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
"image"
|
"image"
|
||||||
_ "image/gif"
|
_ "image/gif"
|
||||||
|
@ -15,6 +18,10 @@ import (
|
||||||
_ "image/png"
|
_ "image/png"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// #include <sys/ipc.h>
|
||||||
|
// #include <sys/shm.h>
|
||||||
|
import "C"
|
||||||
|
|
||||||
func F64ToFixed(f float64) render.Fixed { return render.Fixed(f * 65536) }
|
func F64ToFixed(f float64) render.Fixed { return render.Fixed(f * 65536) }
|
||||||
func FixedToF64(f render.Fixed) float64 { return float64(f) / 65536 }
|
func FixedToF64(f render.Fixed) float64 { return float64(f) / 65536 }
|
||||||
|
|
||||||
|
@ -182,30 +189,84 @@ func main() {
|
||||||
_ = xproto.CreateGC(X, cid, xproto.Drawable(pixid),
|
_ = xproto.CreateGC(X, cid, xproto.Drawable(pixid),
|
||||||
xproto.GcGraphicsExposures, []uint32{0})
|
xproto.GcGraphicsExposures, []uint32{0})
|
||||||
|
|
||||||
// Load data into the pixmap. Also see xgbutil/xgraphics/xsurface.go.
|
|
||||||
// We're being lazy and resolve the 1<<16 limit of requests by sending
|
|
||||||
// a row at a time. The encoding is also done inefficiently.
|
|
||||||
bounds := img.Bounds()
|
bounds := img.Bounds()
|
||||||
|
|
||||||
Lstart := time.Now()
|
Lstart := time.Now()
|
||||||
|
|
||||||
row := make([]byte, bounds.Dx()*4)
|
if err := shm.Init(X); err != nil {
|
||||||
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
|
log.Println("MIT-SHM unavailable")
|
||||||
for x := bounds.Min.X; x < bounds.Max.X; x++ {
|
|
||||||
r, g, b, a := img.At(x, y).RGBA()
|
// We're being lazy and resolve the 1<<16 limit of requests by sending
|
||||||
encoding.PutUint32(row[x*4:],
|
// a row at a time. The encoding is also done inefficiently.
|
||||||
(a>>8)<<24|(r>>8)<<16|(g>>8)<<8|(b>>8))
|
// Also see xgbutil/xgraphics/xsurface.go.
|
||||||
|
row := make([]byte, bounds.Dx()*4)
|
||||||
|
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
|
||||||
|
for x := bounds.Min.X; x < bounds.Max.X; x++ {
|
||||||
|
r, g, b, a := img.At(x, y).RGBA()
|
||||||
|
encoding.PutUint32(row[x*4:],
|
||||||
|
(a>>8)<<24|(r>>8)<<16|(g>>8)<<8|(b>>8))
|
||||||
|
}
|
||||||
|
_ = xproto.PutImage(X, xproto.ImageFormatZPixmap,
|
||||||
|
xproto.Drawable(pixid), cid, uint16(bounds.Dx()), 1,
|
||||||
|
0, int16(y),
|
||||||
|
0, 32, row)
|
||||||
}
|
}
|
||||||
_ = xproto.PutImage(X, xproto.ImageFormatZPixmap,
|
} else {
|
||||||
xproto.Drawable(pixid), cid, uint16(bounds.Dx()), 1,
|
rep, err := shm.QueryVersion(X).Reply()
|
||||||
0, int16(y),
|
if err != nil {
|
||||||
0, 32, row)
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
if rep.PixmapFormat != xproto.ImageFormatZPixmap ||
|
||||||
|
!rep.SharedPixmaps {
|
||||||
|
log.Fatalln("MIT-SHM configuration unfit")
|
||||||
|
}
|
||||||
|
|
||||||
|
shmSize := bounds.Dx() * bounds.Dy() * 4
|
||||||
|
|
||||||
|
// As a side note, to clean up unreferenced segments (orphans):
|
||||||
|
// ipcs -m | awk '$6 == "0" { print $2 }' | xargs ipcrm shm
|
||||||
|
shmID := int(C.shmget(C.IPC_PRIVATE,
|
||||||
|
C.size_t(shmSize), C.IPC_CREAT|0777))
|
||||||
|
if shmID == -1 {
|
||||||
|
// TODO: We should handle this case by falling back to PutImage,
|
||||||
|
// if only because the allocation may hit a system limit.
|
||||||
|
log.Fatalln("memory allocation failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
dataRaw := C.shmat(C.int(shmID), nil, 0)
|
||||||
|
defer C.shmdt(dataRaw)
|
||||||
|
defer C.shmctl(C.int(shmID), C.IPC_RMID, nil)
|
||||||
|
|
||||||
|
data := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||||
|
Data: uintptr(dataRaw), Len: shmSize, Cap: shmSize}))
|
||||||
|
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
|
||||||
|
row := data[y*bounds.Dx()*4:]
|
||||||
|
for x := bounds.Min.X; x < bounds.Max.X; x++ {
|
||||||
|
r, g, b, a := img.At(x, y).RGBA()
|
||||||
|
encoding.PutUint32(row[x*4:],
|
||||||
|
(a>>8)<<24|(r>>8)<<16|(g>>8)<<8|(b>>8))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
segid, err := shm.NewSegId(X)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Need to have it attached on the server before we unload the segment.
|
||||||
|
c := shm.AttachChecked(X, segid, uint32(shmID), true /* RO */)
|
||||||
|
if err := c.Check(); err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = shm.PutImage(X, xproto.Drawable(pixid), cid,
|
||||||
|
uint16(bounds.Dx()), uint16(bounds.Dy()), 0, 0,
|
||||||
|
uint16(bounds.Dx()), uint16(bounds.Dy()), 0, 0,
|
||||||
|
32, xproto.ImageFormatZPixmap,
|
||||||
|
0 /* SendEvent */, segid, 0 /* Offset */)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("uploading took", time.Now().Sub(Lstart))
|
log.Println("uploading took", time.Now().Sub(Lstart))
|
||||||
|
|
||||||
// TODO: SHM, for an example see https://github.com/BurntSushi/xgb/issues/28
|
|
||||||
|
|
||||||
var scale float64 = 1
|
var scale float64 = 1
|
||||||
for {
|
for {
|
||||||
ev, xerr := X.WaitForEvent()
|
ev, xerr := X.WaitForEvent()
|
||||||
|
|
Loading…
Reference in New Issue