xgb-image: add support for the MIT-SHM extension
This commit is contained in:
		@@ -4,10 +4,13 @@ import (
 | 
			
		||||
	"encoding/binary"
 | 
			
		||||
	"github.com/BurntSushi/xgb"
 | 
			
		||||
	"github.com/BurntSushi/xgb/render"
 | 
			
		||||
	"github.com/BurntSushi/xgb/shm"
 | 
			
		||||
	"github.com/BurntSushi/xgb/xproto"
 | 
			
		||||
	"log"
 | 
			
		||||
	"os"
 | 
			
		||||
	"reflect"
 | 
			
		||||
	"time"
 | 
			
		||||
	"unsafe"
 | 
			
		||||
 | 
			
		||||
	"image"
 | 
			
		||||
	_ "image/gif"
 | 
			
		||||
@@ -15,6 +18,10 @@ import (
 | 
			
		||||
	_ "image/png"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// #include <sys/ipc.h>
 | 
			
		||||
// #include <sys/shm.h>
 | 
			
		||||
import "C"
 | 
			
		||||
 | 
			
		||||
func F64ToFixed(f float64) render.Fixed { return render.Fixed(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.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()
 | 
			
		||||
 | 
			
		||||
	Lstart := time.Now()
 | 
			
		||||
 | 
			
		||||
	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))
 | 
			
		||||
	if err := shm.Init(X); err != nil {
 | 
			
		||||
		log.Println("MIT-SHM unavailable")
 | 
			
		||||
 | 
			
		||||
		// 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.
 | 
			
		||||
		// 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,
 | 
			
		||||
			xproto.Drawable(pixid), cid, uint16(bounds.Dx()), 1,
 | 
			
		||||
			0, int16(y),
 | 
			
		||||
			0, 32, row)
 | 
			
		||||
	} else {
 | 
			
		||||
		rep, err := shm.QueryVersion(X).Reply()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			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))
 | 
			
		||||
 | 
			
		||||
	// TODO: SHM, for an example see https://github.com/BurntSushi/xgb/issues/28
 | 
			
		||||
 | 
			
		||||
	var scale float64 = 1
 | 
			
		||||
	for {
 | 
			
		||||
		ev, xerr := X.WaitForEvent()
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user