xgb-draw: double buffer, change color, cleanup

This commit is contained in:
Přemysl Eric Janouch 2018-09-06 18:12:47 +02:00
parent e8381d86ce
commit 632b3ae494
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 60 additions and 45 deletions

View File

@ -1,9 +1,6 @@
// Network-friendly drawing application based on XRender. // Network-friendly drawing application based on XRender.
// //
// TODO // TODO: Maybe keep the pixmap as large as the window.
// - use double buffering to remove flicker
// (more pronounced over X11 forwarding)
// - maybe keep the pixmap as large as the window
package main package main
import ( import (
@ -26,6 +23,23 @@ func findPictureFormat(formats []render.Pictforminfo,
return 0 return 0
} }
func createNewPicture(X *xgb.Conn, depth byte, drawable xproto.Drawable,
width uint16, height uint16, format render.Pictformat) render.Picture {
pixmapid, err := xproto.NewPixmapId(X)
if err != nil {
log.Fatalln(err)
}
_ = xproto.CreatePixmap(X, depth, pixmapid, drawable, width, height)
pictid, err := render.NewPictureId(X)
if err != nil {
log.Fatalln(err)
}
_ = render.CreatePicture(X, pictid, xproto.Drawable(pixmapid), format,
0, []uint32{})
return pictid
}
func main() { func main() {
X, err := xgb.NewConn() X, err := xgb.NewConn()
if err != nil { if err != nil {
@ -125,53 +139,44 @@ func main() {
log.Fatalln(err) log.Fatalln(err)
} }
_ = render.CreateSolidFill(X, colorid, render.Color{ _ = render.CreateSolidFill(X, colorid, render.Color{
Red: 0xeeee, Red: 0x4444,
Green: 0x8888, Green: 0x8888,
Blue: 0x4444, Blue: 0xffff,
Alpha: 0xffff, Alpha: 0xffff,
}) })
// Pixmaps. // Various pixmaps.
const ( const (
pixWidth = 1000 pixWidth = 1000
pixHeight = 1000 pixHeight = 1000
) )
pixid, err := xproto.NewPixmapId(X) canvasid := createNewPicture(X, 24,
if err != nil { xproto.Drawable(screen.Root), pixWidth, pixHeight, pformatRGB)
log.Fatalln(err) bufferid := createNewPicture(X, 24,
} xproto.Drawable(screen.Root), pixWidth, pixHeight, pformatRGB)
_ = xproto.CreatePixmap(X, 24, pixid, xproto.Drawable(screen.Root), maskid := createNewPicture(X, 8,
pixWidth, pixHeight) xproto.Drawable(screen.Root), pixWidth, pixHeight, pformatAlpha)
pixpictid, err := render.NewPictureId(X) // Smoothing by way of blur, apparently a misguided idea.
if err != nil { /*
log.Fatalln(err) _ = render.SetPictureFilter(X, maskid,
} uint16(len("convolution")), "convolution",
render.CreatePicture(X, pixpictid, xproto.Drawable(pixid), []render.Fixed{F64ToFixed(3), F64ToFixed(3),
pformatRGB, 0, []uint32{}) F64ToFixed(0), F64ToFixed(0.15), F64ToFixed(0),
F64ToFixed(0.15), F64ToFixed(0.40), F64ToFixed(0.15),
pixmaskid, err := xproto.NewPixmapId(X) F64ToFixed(0), F64ToFixed(0.15), F64ToFixed(0)})
if err != nil { */
log.Fatalln(err)
}
_ = xproto.CreatePixmap(X, 8, pixmaskid, xproto.Drawable(screen.Root),
pixWidth, pixHeight)
pixmaskpictid, err := render.NewPictureId(X)
if err != nil {
log.Fatalln(err)
}
render.CreatePicture(X, pixmaskpictid, xproto.Drawable(pixmaskid),
pformatAlpha, 0, []uint32{})
// Pixmaps come uninitialized. // Pixmaps come uninitialized.
render.FillRectangles(X, _ = render.FillRectangles(X,
render.PictOpSrc, pixpictid, render.Color{ render.PictOpSrc, canvasid, render.Color{
Red: 0xffff, Green: 0xffff, Blue: 0xffff, Alpha: 0xffff, Red: 0xffff, Green: 0xffff, Blue: 0xffff, Alpha: 0xffff,
}, []xproto.Rectangle{{Width: pixWidth, Height: pixHeight}}) }, []xproto.Rectangle{{Width: pixWidth, Height: pixHeight}})
// This is the only method we can use to render proper brush strokes. // This is the only method we can use to render brush strokes without
// alpha accumulation due to stamping. Though this also seems to be
// misguided. Keeping it here for educational purposes.
// //
// ConjointOver is defined as: A = Aa * 1 + Ab * max(1-Aa/Ab,0) // ConjointOver is defined as: A = Aa * 1 + Ab * max(1-Aa/Ab,0)
// which basically resolves to: A = max(Aa, Ab) // which basically resolves to: A = max(Aa, Ab)
@ -184,20 +189,28 @@ func main() {
// - https://keithp.com/~keithp/talks/cairo2003.pdf // - https://keithp.com/~keithp/talks/cairo2003.pdf
drawPointAt := func(x, y int16) { drawPointAt := func(x, y int16) {
_ = render.Composite(X, render.PictOpConjointOver, _ = render.Composite(X, render.PictOpConjointOver,
brushid, render.PictureNone, pixmaskpictid, brushid, render.PictureNone, maskid,
0, 0, 0, 0, x-brushRadius, y-brushRadius, 0, 0, 0, 0, x-brushRadius, y-brushRadius,
brushRadius*2, brushRadius*2) brushRadius*2, brushRadius*2)
_ = render.SetPictureClipRectangles(X, pid, _ = render.SetPictureClipRectangles(X, bufferid,
x-brushRadius, y-brushRadius, []xproto.Rectangle{ x-brushRadius, y-brushRadius, []xproto.Rectangle{
{Width: brushRadius * 2, Height: brushRadius * 2}}) {Width: brushRadius * 2, Height: brushRadius * 2}})
_ = render.Composite(X, render.PictOpSrc, _ = render.Composite(X, render.PictOpSrc,
pixpictid, render.PictureNone, pid, canvasid, render.PictureNone, bufferid,
0, 0, 0, 0, 0 /* dst-x */, 0, /* dst-y */ 0, 0, 0, 0, 0 /* dst-x */, 0, /* dst-y */
pixWidth, pixHeight) pixWidth, pixHeight)
_ = render.Composite(X, render.PictOpOver, _ = render.Composite(X, render.PictOpOver,
colorid, pixmaskpictid, pid, colorid, maskid, bufferid,
0, 0, 0, 0, 0 /* dst-x */, 0, /* dst-y */
pixWidth, pixHeight)
// Composited, now blit to window without flicker.
_ = render.SetPictureClipRectangles(X, pid,
x-brushRadius, y-brushRadius, []xproto.Rectangle{
{Width: brushRadius * 2, Height: brushRadius * 2}})
_ = render.Composite(X, render.PictOpSrc,
bufferid, render.PictureNone, pid,
0, 0, 0, 0, 0 /* dst-x */, 0, /* dst-y */ 0, 0, 0, 0, 0 /* dst-x */, 0, /* dst-y */
pixWidth, pixHeight) pixWidth, pixHeight)
} }
@ -271,14 +284,16 @@ func main() {
_ = render.SetPictureClipRectangles(X, pid, int16(e.X), int16(e.Y), _ = render.SetPictureClipRectangles(X, pid, int16(e.X), int16(e.Y),
[]xproto.Rectangle{{Width: e.Width, Height: e.Height}}) []xproto.Rectangle{{Width: e.Width, Height: e.Height}})
// Not bothering to deflicker here using the buffer pixmap,
// with compositing this event is rare enough.
_ = render.Composite(X, render.PictOpSrc, _ = render.Composite(X, render.PictOpSrc,
pixpictid, render.PictureNone, pid, canvasid, render.PictureNone, pid,
0, 0, 0, 0, 0 /* dst-x */, 0, /* dst-y */ 0, 0, 0, 0, 0 /* dst-x */, 0, /* dst-y */
pixWidth, pixHeight) pixWidth, pixHeight)
if drawing { if drawing {
_ = render.Composite(X, render.PictOpOver, _ = render.Composite(X, render.PictOpOver,
colorid, pixmaskpictid, pid, colorid, maskid, pid,
0, 0, 0, 0, 0 /* dst-x */, 0, /* dst-y */ 0, 0, 0, 0, 0 /* dst-x */, 0, /* dst-y */
pixWidth, pixHeight) pixWidth, pixHeight)
} }
@ -286,7 +301,7 @@ func main() {
case xproto.ButtonPressEvent: case xproto.ButtonPressEvent:
if e.Detail == xproto.ButtonIndex1 { if e.Detail == xproto.ButtonIndex1 {
render.FillRectangles(X, render.FillRectangles(X,
render.PictOpSrc, pixmaskpictid, render.Color{}, render.PictOpSrc, maskid, render.Color{},
[]xproto.Rectangle{{Width: pixWidth, Height: pixHeight}}) []xproto.Rectangle{{Width: pixWidth, Height: pixHeight}})
drawing = true drawing = true
@ -303,7 +318,7 @@ func main() {
case xproto.ButtonReleaseEvent: case xproto.ButtonReleaseEvent:
if e.Detail == xproto.ButtonIndex1 { if e.Detail == xproto.ButtonIndex1 {
_ = render.Composite(X, render.PictOpOver, _ = render.Composite(X, render.PictOpOver,
colorid, pixmaskpictid, pixpictid, colorid, maskid, canvasid,
0, 0, 0, 0, 0 /* dst-x */, 0, /* dst-y */ 0, 0, 0, 0, 0 /* dst-x */, 0, /* dst-y */
pixWidth, pixHeight) pixWidth, pixHeight)