| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package x11driver |
| 6 |
|
| 7 |
import ( |
| 8 |
"image" |
| 9 |
"image/color" |
| 10 |
"image/draw" |
| 11 |
"math" |
| 12 |
"sync" |
| 13 |
|
| 14 |
"github.com/jezek/xgb/render" |
| 15 |
"github.com/jezek/xgb/xproto" |
| 16 |
|
| 17 |
"golang.org/x/exp/shiny/screen" |
| 18 |
"golang.org/x/image/math/f64" |
| 19 |
) |
| 20 |
|
| 21 |
const textureDepth = 32 |
| 22 |
|
| 23 |
type textureImpl struct { |
| 24 |
s *screenImpl |
| 25 |
|
| 26 |
size image.Point |
| 27 |
xm xproto.Pixmap |
| 28 |
xp render.Picture |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
renderMu sync.Mutex |
| 36 |
|
| 37 |
releasedMu sync.Mutex |
| 38 |
released bool |
| 39 |
} |
| 40 |
|
| 41 |
func (t *textureImpl) degenerate() bool { return t.size.X == 0 || t.size.Y == 0 } |
| 42 |
func (t *textureImpl) Size() image.Point { return t.size } |
| 43 |
func (t *textureImpl) Bounds() image.Rectangle { return image.Rectangle{Max: t.size} } |
| 44 |
|
| 45 |
func (t *textureImpl) Release() { |
| 46 |
t.releasedMu.Lock() |
| 47 |
released := t.released |
| 48 |
t.released = true |
| 49 |
t.releasedMu.Unlock() |
| 50 |
|
| 51 |
if released || t.degenerate() { |
| 52 |
return |
| 53 |
} |
| 54 |
render.FreePicture(t.s.xc, t.xp) |
| 55 |
xproto.FreePixmap(t.s.xc, t.xm) |
| 56 |
} |
| 57 |
|
| 58 |
func (t *textureImpl) Upload(dp image.Point, src screen.Buffer, sr image.Rectangle) { |
| 59 |
if t.degenerate() { |
| 60 |
return |
| 61 |
} |
| 62 |
src.(bufferUploader).upload(xproto.Drawable(t.xm), t.s.gcontext32, textureDepth, dp, sr) |
| 63 |
} |
| 64 |
|
| 65 |
func (t *textureImpl) Fill(dr image.Rectangle, src color.Color, op draw.Op) { |
| 66 |
if t.degenerate() { |
| 67 |
return |
| 68 |
} |
| 69 |
fill(t.s.xc, t.xp, dr, src, op) |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
func f64ToFixed(x float64) render.Fixed { |
| 74 |
return render.Fixed(x * 65536) |
| 75 |
} |
| 76 |
|
| 77 |
func inv(x *f64.Aff3) f64.Aff3 { |
| 78 |
invDet := 1 / (x[0]*x[4] - x[1]*x[3]) |
| 79 |
return f64.Aff3{ |
| 80 |
+x[4] * invDet, |
| 81 |
-x[1] * invDet, |
| 82 |
(x[1]*x[5] - x[2]*x[4]) * invDet, |
| 83 |
-x[3] * invDet, |
| 84 |
+x[0] * invDet, |
| 85 |
(x[2]*x[3] - x[0]*x[5]) * invDet, |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
func (t *textureImpl) draw(xp render.Picture, src2dst *f64.Aff3, sr image.Rectangle, op draw.Op, opts *screen.DrawOptions) { |
| 90 |
sr = sr.Intersect(t.Bounds()) |
| 91 |
if sr.Empty() { |
| 92 |
return |
| 93 |
} |
| 94 |
|
| 95 |
t.renderMu.Lock() |
| 96 |
defer t.renderMu.Unlock() |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
if src2dst[1] == 0 && src2dst[3] == 0 { |
| 103 |
dstXMin := float64(sr.Min.X)*src2dst[0] + src2dst[2] |
| 104 |
dstXMax := float64(sr.Max.X)*src2dst[0] + src2dst[2] |
| 105 |
if dstXMin > dstXMax { |
| 106 |
|
| 107 |
dstXMin, dstXMax = dstXMax, dstXMin |
| 108 |
} |
| 109 |
dXMin := int(math.Floor(dstXMin)) |
| 110 |
dXMax := int(math.Ceil(dstXMax)) |
| 111 |
|
| 112 |
dstYMin := float64(sr.Min.Y)*src2dst[4] + src2dst[5] |
| 113 |
dstYMax := float64(sr.Max.Y)*src2dst[4] + src2dst[5] |
| 114 |
if dstYMin > dstYMax { |
| 115 |
|
| 116 |
dstYMin, dstYMax = dstYMax, dstYMin |
| 117 |
} |
| 118 |
dYMin := int(math.Floor(dstYMin)) |
| 119 |
dYMax := int(math.Ceil(dstYMax)) |
| 120 |
|
| 121 |
render.SetPictureTransform(t.s.xc, t.xp, render.Transform{ |
| 122 |
f64ToFixed(1 / src2dst[0]), 0, 0, |
| 123 |
0, f64ToFixed(1 / src2dst[4]), 0, |
| 124 |
0, 0, 1 << 16, |
| 125 |
}) |
| 126 |
render.Composite(t.s.xc, renderOp(op), t.xp, 0, xp, |
| 127 |
int16(sr.Min.X), int16(sr.Min.Y), |
| 128 |
0, 0, |
| 129 |
int16(dXMin), int16(dYMin), |
| 130 |
uint16(dXMax-dXMin), uint16(dYMax-dYMin), |
| 131 |
) |
| 132 |
return |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
dst2src := inv(src2dst) |
| 138 |
render.SetPictureTransform(t.s.xc, t.xp, render.Transform{ |
| 139 |
f64ToFixed(dst2src[0]), f64ToFixed(dst2src[1]), render.Fixed(sr.Min.X << 16), |
| 140 |
f64ToFixed(dst2src[3]), f64ToFixed(dst2src[4]), render.Fixed(sr.Min.Y << 16), |
| 141 |
0, 0, 1 << 16, |
| 142 |
}) |
| 143 |
|
| 144 |
points := trifanPoints(src2dst, sr) |
| 145 |
if op == draw.Src { |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
render.TriFan(t.s.xc, render.PictOpOutReverse, t.s.opaqueP, xp, 0, 0, 0, points[:]) |
| 171 |
} |
| 172 |
render.TriFan(t.s.xc, render.PictOpOver, t.xp, xp, 0, 0, 0, points[:]) |
| 173 |
} |
| 174 |
|
| 175 |
func trifanPoints(src2dst *f64.Aff3, sr image.Rectangle) [4]render.Pointfix { |
| 176 |
minX := float64(sr.Min.X) |
| 177 |
maxX := float64(sr.Max.X) |
| 178 |
minY := float64(sr.Min.Y) |
| 179 |
maxY := float64(sr.Max.Y) |
| 180 |
return [4]render.Pointfix{{ |
| 181 |
f64ToFixed(src2dst[0]*minX + src2dst[1]*minY + src2dst[2]), |
| 182 |
f64ToFixed(src2dst[3]*minX + src2dst[4]*minY + src2dst[5]), |
| 183 |
}, { |
| 184 |
f64ToFixed(src2dst[0]*maxX + src2dst[1]*minY + src2dst[2]), |
| 185 |
f64ToFixed(src2dst[3]*maxX + src2dst[4]*minY + src2dst[5]), |
| 186 |
}, { |
| 187 |
f64ToFixed(src2dst[0]*maxX + src2dst[1]*maxY + src2dst[2]), |
| 188 |
f64ToFixed(src2dst[3]*maxX + src2dst[4]*maxY + src2dst[5]), |
| 189 |
}, { |
| 190 |
f64ToFixed(src2dst[0]*minX + src2dst[1]*maxY + src2dst[2]), |
| 191 |
f64ToFixed(src2dst[3]*minX + src2dst[4]*maxY + src2dst[5]), |
| 192 |
}} |
| 193 |
} |
| 194 |
|
| 195 |
func renderOp(op draw.Op) byte { |
| 196 |
if op == draw.Src { |
| 197 |
return render.PictOpSrc |
| 198 |
} |
| 199 |
return render.PictOpOver |
| 200 |
} |