| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package x11driver |
| 6 |
|
| 7 |
import ( |
| 8 |
"image" |
| 9 |
"image/color" |
| 10 |
"image/draw" |
| 11 |
"log" |
| 12 |
"sync" |
| 13 |
"unsafe" |
| 14 |
|
| 15 |
"github.com/jezek/xgb" |
| 16 |
"github.com/jezek/xgb/render" |
| 17 |
"github.com/jezek/xgb/shm" |
| 18 |
"github.com/jezek/xgb/xproto" |
| 19 |
|
| 20 |
"golang.org/x/exp/shiny/driver/internal/swizzle" |
| 21 |
) |
| 22 |
|
| 23 |
type bufferUploader interface { |
| 24 |
upload(xd xproto.Drawable, xg xproto.Gcontext, depth uint8, dp image.Point, sr image.Rectangle) |
| 25 |
} |
| 26 |
|
| 27 |
type bufferImpl struct { |
| 28 |
s *screenImpl |
| 29 |
|
| 30 |
addr unsafe.Pointer |
| 31 |
buf []byte |
| 32 |
rgba image.RGBA |
| 33 |
size image.Point |
| 34 |
xs shm.Seg |
| 35 |
|
| 36 |
mu sync.Mutex |
| 37 |
nUpload uint32 |
| 38 |
released bool |
| 39 |
cleanedUp bool |
| 40 |
} |
| 41 |
|
| 42 |
func (b *bufferImpl) degenerate() bool { return b.size.X == 0 || b.size.Y == 0 } |
| 43 |
func (b *bufferImpl) Size() image.Point { return b.size } |
| 44 |
func (b *bufferImpl) Bounds() image.Rectangle { return image.Rectangle{Max: b.size} } |
| 45 |
func (b *bufferImpl) RGBA() *image.RGBA { return &b.rgba } |
| 46 |
|
| 47 |
func (b *bufferImpl) preUpload() { |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
if len(b.buf) != 0 && len(b.rgba.Pix) != 0 && &b.buf[0] != &b.rgba.Pix[0] { |
| 54 |
panic("x11driver: invalid Buffer.RGBA modification") |
| 55 |
} |
| 56 |
|
| 57 |
b.mu.Lock() |
| 58 |
defer b.mu.Unlock() |
| 59 |
|
| 60 |
if b.released { |
| 61 |
panic("x11driver: Buffer.Upload called after Buffer.Release") |
| 62 |
} |
| 63 |
if b.nUpload == 0 { |
| 64 |
swizzle.BGRA(b.buf) |
| 65 |
} |
| 66 |
b.nUpload++ |
| 67 |
} |
| 68 |
|
| 69 |
func (b *bufferImpl) postUpload() { |
| 70 |
b.mu.Lock() |
| 71 |
defer b.mu.Unlock() |
| 72 |
|
| 73 |
b.nUpload-- |
| 74 |
if b.nUpload != 0 { |
| 75 |
return |
| 76 |
} |
| 77 |
|
| 78 |
if b.released { |
| 79 |
go b.cleanUp() |
| 80 |
} else { |
| 81 |
swizzle.BGRA(b.buf) |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
func (b *bufferImpl) Release() { |
| 86 |
b.mu.Lock() |
| 87 |
defer b.mu.Unlock() |
| 88 |
|
| 89 |
if !b.released && b.nUpload == 0 { |
| 90 |
go b.cleanUp() |
| 91 |
} |
| 92 |
b.released = true |
| 93 |
} |
| 94 |
|
| 95 |
func (b *bufferImpl) cleanUp() { |
| 96 |
b.mu.Lock() |
| 97 |
if b.cleanedUp { |
| 98 |
b.mu.Unlock() |
| 99 |
panic("x11driver: Buffer clean-up occurred twice") |
| 100 |
} |
| 101 |
b.cleanedUp = true |
| 102 |
b.mu.Unlock() |
| 103 |
|
| 104 |
b.s.mu.Lock() |
| 105 |
delete(b.s.buffers, b.xs) |
| 106 |
b.s.mu.Unlock() |
| 107 |
|
| 108 |
if b.degenerate() { |
| 109 |
return |
| 110 |
} |
| 111 |
shm.Detach(b.s.xc, b.xs) |
| 112 |
if err := shmClose(b.addr); err != nil { |
| 113 |
log.Printf("x11driver: shmClose: %v", err) |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
func (b *bufferImpl) upload(xd xproto.Drawable, xg xproto.Gcontext, depth uint8, dp image.Point, sr image.Rectangle) { |
| 118 |
originalSRMin := sr.Min |
| 119 |
sr = sr.Intersect(b.Bounds()) |
| 120 |
if sr.Empty() { |
| 121 |
return |
| 122 |
} |
| 123 |
dp = dp.Add(sr.Min.Sub(originalSRMin)) |
| 124 |
b.preUpload() |
| 125 |
|
| 126 |
b.s.mu.Lock() |
| 127 |
b.s.nPendingUploads++ |
| 128 |
b.s.mu.Unlock() |
| 129 |
|
| 130 |
cookie := shm.PutImage( |
| 131 |
b.s.xc, xd, xg, |
| 132 |
uint16(b.size.X), uint16(b.size.Y), |
| 133 |
uint16(sr.Min.X), uint16(sr.Min.Y), |
| 134 |
uint16(sr.Dx()), uint16(sr.Dy()), |
| 135 |
int16(dp.X), int16(dp.Y), |
| 136 |
depth, xproto.ImageFormatZPixmap, |
| 137 |
1, b.xs, 0, |
| 138 |
) |
| 139 |
|
| 140 |
completion := make(chan struct{}) |
| 141 |
|
| 142 |
b.s.mu.Lock() |
| 143 |
b.s.uploads[cookie.Sequence] = completion |
| 144 |
b.s.nPendingUploads-- |
| 145 |
b.s.handleCompletions() |
| 146 |
b.s.mu.Unlock() |
| 147 |
|
| 148 |
<-completion |
| 149 |
|
| 150 |
b.postUpload() |
| 151 |
} |
| 152 |
|
| 153 |
func fill(xc *xgb.Conn, xp render.Picture, dr image.Rectangle, src color.Color, op draw.Op) { |
| 154 |
r, g, b, a := src.RGBA() |
| 155 |
c := render.Color{ |
| 156 |
Red: uint16(r), |
| 157 |
Green: uint16(g), |
| 158 |
Blue: uint16(b), |
| 159 |
Alpha: uint16(a), |
| 160 |
} |
| 161 |
x, y := dr.Min.X, dr.Min.Y |
| 162 |
if x < -0x8000 || 0x7fff < x || y < -0x8000 || 0x7fff < y { |
| 163 |
return |
| 164 |
} |
| 165 |
dx, dy := dr.Dx(), dr.Dy() |
| 166 |
if dx < 0 || 0xffff < dx || dy < 0 || 0xffff < dy { |
| 167 |
return |
| 168 |
} |
| 169 |
render.FillRectangles(xc, renderOp(op), xp, c, []xproto.Rectangle{{ |
| 170 |
X: int16(x), |
| 171 |
Y: int16(y), |
| 172 |
Width: uint16(dx), |
| 173 |
Height: uint16(dy), |
| 174 |
}}) |
| 175 |
} |