ui: build paths as ops

Instead of allocating and constructing a clip path, store path data
directly in op lists. Use separate op lists for cached text layout
paths.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-06-02 16:43:28 +02:00
parent 9f58ed0fea
commit 1e38eec0ab
15 changed files with 248 additions and 160 deletions
+55 -26
View File
@@ -3,6 +3,7 @@
package gpu package gpu
import ( import (
"encoding/binary"
"fmt" "fmt"
"image" "image"
"image/color" "image/color"
@@ -16,7 +17,6 @@ import (
gdraw "gioui.org/ui/draw" gdraw "gioui.org/ui/draw"
"gioui.org/ui/f32" "gioui.org/ui/f32"
"gioui.org/ui/internal/ops" "gioui.org/ui/internal/ops"
"gioui.org/ui/internal/path"
"golang.org/x/image/draw" "golang.org/x/image/draw"
) )
@@ -87,10 +87,11 @@ type pathOp struct {
off f32.Point off f32.Point
// clip is the union of all // clip is the union of all
// later clip rectangles. // later clip rectangles.
clip image.Rectangle clip image.Rectangle
path *path.Path pathKey ui.OpKey
parent *pathOp pathVerts []byte
place placement parent *pathOp
place placement
} }
type imageOp struct { type imageOp struct {
@@ -114,6 +115,31 @@ type material struct {
uvOffset f32.Point uvOffset f32.Point
} }
// opClip structure must match opClip in package ui/draw.
type opClip struct {
bounds f32.Rectangle
}
func (op *opClip) decode(data []byte) {
if ops.OpType(data[0]) != ops.TypeClip {
panic("invalid op")
}
bo := binary.LittleEndian
r := f32.Rectangle{
Min: f32.Point{
X: math.Float32frombits(bo.Uint32(data[1:])),
Y: math.Float32frombits(bo.Uint32(data[5:])),
},
Max: f32.Point{
X: math.Float32frombits(bo.Uint32(data[9:])),
Y: math.Float32frombits(bo.Uint32(data[13:])),
},
}
*op = opClip{
bounds: r,
}
}
type clipType uint8 type clipType uint8
type resourceCache struct { type resourceCache struct {
@@ -485,10 +511,10 @@ func (r *renderer) stencilClips(cache *resourceCache, ops []*pathOp) {
bindFramebuffer(r.ctx, f.fbo) bindFramebuffer(r.ctx, f.fbo)
r.ctx.Clear(gl.COLOR_BUFFER_BIT) r.ctx.Clear(gl.COLOR_BUFFER_BIT)
} }
data, exists := cache.get(p.path) data, exists := cache.get(p.pathKey)
if !exists { if !exists {
data = buildPath(r.ctx, p.path) data = buildPath(r.ctx, p.pathVerts)
cache.put(p.path, data) cache.put(p.pathKey, data)
} }
r.pather.stencilPath(p.clip, p.off, p.place.Pos, data.(*pathData)) r.pather.stencilPath(p.clip, p.off, p.place.Pos, data.(*pathData))
} }
@@ -528,7 +554,7 @@ func (r *renderer) intersectPath(p *pathOp, clip image.Rectangle) {
if p.parent != nil { if p.parent != nil {
r.intersectPath(p.parent, clip) r.intersectPath(p.parent, clip)
} }
if p.path == nil { if len(p.pathVerts) == 0 {
return return
} }
o := p.place.Pos.Add(clip.Min).Sub(p.clip.Min) o := p.place.Pos.Add(clip.Min).Sub(p.clip.Min)
@@ -550,7 +576,7 @@ func (r *renderer) packIntersections(ops []imageOp) {
var npaths int var npaths int
var onePath *pathOp var onePath *pathOp
for p := img.path; p != nil; p = p.parent { for p := img.path; p != nil; p = p.parent {
if p.path != nil { if len(p.pathVerts) > 0 {
onePath = p onePath = p
npaths++ npaths++
} }
@@ -665,27 +691,27 @@ func (d *drawOps) newPathOp() *pathOp {
} }
func (d *drawOps) collectOps(r *ui.OpsReader, state drawState) int { func (d *drawOps) collectOps(r *ui.OpsReader, state drawState) int {
var aux []byte
var auxKey ui.OpKey
loop: loop:
for { for {
data, refs, ok := r.Decode() encOp, ok := r.Decode()
if !ok { if !ok {
break break
} }
switch ops.OpType(data[0]) { switch ops.OpType(encOp.Data[0]) {
case ops.TypeTransform: case ops.TypeTransform:
var op ui.OpTransform var op ui.OpTransform
op.Decode(data) op.Decode(encOp.Data)
state.t = state.t.Mul(op.Transform) state.t = state.t.Mul(op.Transform)
case ops.TypeAux:
aux = encOp.Data[ops.TypeAuxLen:]
auxKey = encOp.Key
case ops.TypeClip: case ops.TypeClip:
var op gdraw.OpClip var op opClip
op.Decode(data, refs) op.decode(encOp.Data)
if op.Path == nil {
state.clip = f32.Rectangle{}
continue
}
data := op.Path.Data().(*path.Path)
off := state.t.Transform(f32.Point{}) off := state.t.Transform(f32.Point{})
state.clip = state.clip.Intersect(data.Bounds.Add(off)) state.clip = state.clip.Intersect(op.bounds.Add(off))
if state.clip.Empty() { if state.clip.Empty() {
continue continue
} }
@@ -695,24 +721,27 @@ loop:
off: off, off: off,
} }
state.cpath = npath state.cpath = npath
if len(data.Vertices) > 0 { if len(aux) > 0 {
state.rect = false state.rect = false
state.cpath.path = data state.cpath.pathKey = auxKey
state.cpath.pathVerts = aux
d.pathOps = append(d.pathOps, state.cpath) d.pathOps = append(d.pathOps, state.cpath)
} }
aux = nil
auxKey = ui.OpKey{}
case ops.TypeColor: case ops.TypeColor:
var op gdraw.OpColor var op gdraw.OpColor
op.Decode(data, refs) op.Decode(encOp.Data, encOp.Refs)
state.img = nil state.img = nil
state.color = op.Col state.color = op.Col
case ops.TypeImage: case ops.TypeImage:
var op gdraw.OpImage var op gdraw.OpImage
op.Decode(data, refs) op.Decode(encOp.Data, encOp.Refs)
state.img = op.Img state.img = op.Img
state.imgRect = op.Rect state.imgRect = op.Rect
case ops.TypeDraw: case ops.TypeDraw:
var op gdraw.OpDraw var op gdraw.OpDraw
op.Decode(data, refs) op.Decode(encOp.Data, encOp.Refs)
off := state.t.Transform(f32.Point{}) off := state.t.Transform(f32.Point{})
clip := state.clip.Intersect(op.Rect.Add(off)) clip := state.clip.Intersect(op.Rect.Add(off))
if clip.Empty() { if clip.Empty() {
+3 -3
View File
@@ -221,12 +221,12 @@ func (c *coverer) release() {
} }
} }
func buildPath(ctx *context, p *path.Path) *pathData { func buildPath(ctx *context, p []byte) *pathData {
buf := ctx.CreateBuffer() buf := ctx.CreateBuffer()
ctx.BindBuffer(gl.ARRAY_BUFFER, buf) ctx.BindBuffer(gl.ARRAY_BUFFER, buf)
ctx.BufferData(gl.ARRAY_BUFFER, gl.BytesView(p.Vertices), gl.STATIC_DRAW) ctx.BufferData(gl.ARRAY_BUFFER, p, gl.STATIC_DRAW)
return &pathData{ return &pathData{
ncurves: len(p.Vertices), ncurves: len(p) / path.VertStride,
data: buf, data: buf,
} }
} }
+3 -3
View File
@@ -156,14 +156,14 @@ func collectRedraws(r *ui.OpsReader) (time.Time, bool) {
var t time.Time var t time.Time
redraw := false redraw := false
for { for {
data, _, ok := r.Decode() encOp, ok := r.Decode()
if !ok { if !ok {
break break
} }
switch ops.OpType(data[0]) { switch ops.OpType(encOp.Data[0]) {
case ops.TypeRedraw: case ops.TypeRedraw:
var op ui.OpRedraw var op ui.OpRedraw
op.Decode(data) op.Decode(encOp.Data)
if !redraw || op.At.Before(t) { if !redraw || op.At.Before(t) {
redraw = true redraw = true
t = op.At t = op.At
+3 -8
View File
@@ -11,7 +11,6 @@ import (
"gioui.org/ui" "gioui.org/ui"
"gioui.org/ui/f32" "gioui.org/ui/f32"
"gioui.org/ui/internal/ops" "gioui.org/ui/internal/ops"
"gioui.org/ui/internal/path"
) )
type OpImage struct { type OpImage struct {
@@ -114,14 +113,10 @@ func (d *OpDraw) Decode(data []byte, refs []interface{}) {
} }
} }
// RectPath constructs a path corresponding to // RectClip append a clip op corresponding to
// a pixel aligned rectangular area. // a pixel aligned rectangular area.
func RectPath(r image.Rectangle) *Path { func RectClip(ops *ui.Ops, r image.Rectangle) {
return &Path{ opClip{bounds: toRectF(r)}.Add(ops)
data: &path.Path{
Bounds: toRectF(r),
},
}
} }
func itof(i int) float32 { func itof(i int) float32 {
+70 -68
View File
@@ -3,7 +3,9 @@
package draw package draw
import ( import (
"encoding/binary"
"math" "math"
"unsafe"
"gioui.org/ui" "gioui.org/ui"
"gioui.org/ui/f32" "gioui.org/ui/f32"
@@ -11,81 +13,71 @@ import (
"gioui.org/ui/internal/path" "gioui.org/ui/internal/path"
) )
type OpClip struct {
Path *Path
}
type Path struct {
data *path.Path
}
type PathBuilder struct { type PathBuilder struct {
verts []path.Vertex
firstVert int firstVert int
nverts int
maxy float32 maxy float32
pen f32.Point pen f32.Point
bounds f32.Rectangle bounds f32.Rectangle
hasBounds bool hasBounds bool
} }
// Data is for internal use only. // opClip structure must match opClip in package ui/internal/gpu.
func (p *Path) Data() interface{} { type opClip struct {
return p.data bounds f32.Rectangle
} }
func (c OpClip) Add(o *ui.Ops) { func (p opClip) Add(o *ui.Ops) {
data := make([]byte, ops.TypeClipLen) data := make([]byte, ops.TypeClipLen)
data[0] = byte(ops.TypeClip) data[0] = byte(ops.TypeClip)
o.Write(data, []interface{}{c.Path}) bo := binary.LittleEndian
} bo.PutUint32(data[1:], math.Float32bits(p.bounds.Min.X))
bo.PutUint32(data[5:], math.Float32bits(p.bounds.Min.Y))
func (c *OpClip) Decode(d []byte, refs []interface{}) { bo.PutUint32(data[9:], math.Float32bits(p.bounds.Max.X))
if ops.OpType(d[0]) != ops.TypeClip { bo.PutUint32(data[13:], math.Float32bits(p.bounds.Max.Y))
panic("invalid op") o.Write(data, nil)
}
*c = OpClip{
Path: refs[0].(*Path),
}
} }
// MoveTo moves the pen to the given position. // MoveTo moves the pen to the given position.
func (p *PathBuilder) Move(to f32.Point) { func (p *PathBuilder) Move(ops *ui.Ops, to f32.Point) {
p.end() p.end(ops)
to = to.Add(p.pen) to = to.Add(p.pen)
p.maxy = to.Y p.maxy = to.Y
p.pen = to p.pen = to
} }
// end completes the current contour. // end completes the current contour.
func (p *PathBuilder) end() { func (p *PathBuilder) end(ops *ui.Ops) {
// Fill in maximal Y coordinates of the NW and NE corners aux := ops.Aux()
// and offset their curve coordinates. bo := binary.LittleEndian
for i := p.firstVert; i < len(p.verts); i++ { // Fill in maximal Y coordinates of the NW and NE corners.
p.verts[i].MaxY = p.maxy for i := p.firstVert; i < p.nverts; i++ {
off := path.VertStride*i + int(unsafe.Offsetof(((*path.Vertex)(nil)).MaxY))
bo.PutUint32(aux[off:], math.Float32bits(p.maxy))
} }
p.firstVert = len(p.verts) p.firstVert = p.nverts
} }
// Line records a line from the pen to end. // Line records a line from the pen to end.
func (p *PathBuilder) Line(to f32.Point) { func (p *PathBuilder) Line(ops *ui.Ops, to f32.Point) {
to = to.Add(p.pen) to = to.Add(p.pen)
p.lineTo(to) p.lineTo(ops, to)
} }
func (p *PathBuilder) lineTo(to f32.Point) { func (p *PathBuilder) lineTo(ops *ui.Ops, to f32.Point) {
// Model lines as degenerate quadratic beziers. // Model lines as degenerate quadratic beziers.
p.quadTo(to.Add(p.pen).Mul(.5), to) p.quadTo(ops, to.Add(p.pen).Mul(.5), to)
} }
// Quad records a quadratic bezier from the pen to end // Quad records a quadratic bezier from the pen to end
// with the control point ctrl. // with the control point ctrl.
func (p *PathBuilder) Quad(ctrl, to f32.Point) { func (p *PathBuilder) Quad(ops *ui.Ops, ctrl, to f32.Point) {
ctrl = ctrl.Add(p.pen) ctrl = ctrl.Add(p.pen)
to = to.Add(p.pen) to = to.Add(p.pen)
p.quadTo(ctrl, to) p.quadTo(ops, ctrl, to)
} }
func (p *PathBuilder) quadTo(ctrl, to f32.Point) { func (p *PathBuilder) quadTo(ops *ui.Ops, ctrl, to f32.Point) {
// Zero width curves don't contribute to stenciling. // Zero width curves don't contribute to stenciling.
if p.pen.X == to.X && p.pen.X == ctrl.X { if p.pen.X == to.X && p.pen.X == ctrl.X {
p.pen = to p.pen = to
@@ -112,8 +104,8 @@ func (p *PathBuilder) quadTo(ctrl, to f32.Point) {
ctrl0 := p.pen.Mul(1 - t).Add(ctrl.Mul(t)) ctrl0 := p.pen.Mul(1 - t).Add(ctrl.Mul(t))
ctrl1 := ctrl.Mul(1 - t).Add(to.Mul(t)) ctrl1 := ctrl.Mul(1 - t).Add(to.Mul(t))
mid := ctrl0.Mul(1 - t).Add(ctrl1.Mul(t)) mid := ctrl0.Mul(1 - t).Add(ctrl1.Mul(t))
p.simpleQuadTo(ctrl0, mid) p.simpleQuadTo(ops, ctrl0, mid)
p.simpleQuadTo(ctrl1, to) p.simpleQuadTo(ops, ctrl1, to)
if mid.X > bounds.Max.X { if mid.X > bounds.Max.X {
bounds.Max.X = mid.X bounds.Max.X = mid.X
} }
@@ -121,7 +113,7 @@ func (p *PathBuilder) quadTo(ctrl, to f32.Point) {
bounds.Min.X = mid.X bounds.Min.X = mid.X
} }
} else { } else {
p.simpleQuadTo(ctrl, to) p.simpleQuadTo(ops, ctrl, to)
} }
// Find the y extremum, if any. // Find the y extremum, if any.
d = v0.Y - v1.Y d = v0.Y - v1.Y
@@ -140,7 +132,7 @@ func (p *PathBuilder) quadTo(ctrl, to f32.Point) {
// Cube records a cubic bezier from the pen through // Cube records a cubic bezier from the pen through
// two control points ending in to. // two control points ending in to.
func (p *PathBuilder) Cube(ctrl0, ctrl1, to f32.Point) { func (p *PathBuilder) Cube(ops *ui.Ops, ctrl0, ctrl1, to f32.Point) {
ctrl0 = ctrl0.Add(p.pen) ctrl0 = ctrl0.Add(p.pen)
ctrl1 = ctrl1.Add(p.pen) ctrl1 = ctrl1.Add(p.pen)
to = to.Add(p.pen) to = to.Add(p.pen)
@@ -154,12 +146,12 @@ func (p *PathBuilder) Cube(ctrl0, ctrl1, to f32.Point) {
if h := hull.Dy(); h > l { if h := hull.Dy(); h > l {
l = h l = h
} }
p.approxCubeTo(0, l*0.001, ctrl0, ctrl1, to) p.approxCubeTo(ops, 0, l*0.001, ctrl0, ctrl1, to)
} }
// approxCube approximates a cubic beziér by a series of quadratic // approxCube approximates a cubic beziér by a series of quadratic
// curves. // curves.
func (p *PathBuilder) approxCubeTo(splits int, maxDist float32, ctrl0, ctrl1, to f32.Point) int { func (p *PathBuilder) approxCubeTo(ops *ui.Ops, splits int, maxDist float32, ctrl0, ctrl1, to f32.Point) int {
// The idea is from // The idea is from
// https://caffeineowl.com/graphics/2d/vectorial/cubic2quad01.html // https://caffeineowl.com/graphics/2d/vectorial/cubic2quad01.html
// where a quadratic approximates a cubic by eliminating its t³ term // where a quadratic approximates a cubic by eliminating its t³ term
@@ -171,7 +163,7 @@ func (p *PathBuilder) approxCubeTo(splits int, maxDist float32, ctrl0, ctrl1, to
// //
// C1 = (3ctrl0 - pen)/2 // C1 = (3ctrl0 - pen)/2
// //
// The reverse cubic that is anchored at the end point has the polynomial // The reverse cubic anchored at the end point has the polynomial
// //
// P'(t) = to + 3t(ctrl1 - to) + 3t²(ctrl0 - 2ctrl1 + to) + t³(pen - 3ctrl0 + 3ctrl1 - to) // P'(t) = to + 3t(ctrl1 - to) + 3t²(ctrl0 - 2ctrl1 + to) + t³(pen - 3ctrl0 + 3ctrl1 - to)
// //
@@ -187,7 +179,7 @@ func (p *PathBuilder) approxCubeTo(splits int, maxDist float32, ctrl0, ctrl1, to
c := ctrl0.Mul(3).Sub(p.pen).Add(ctrl1.Mul(3)).Sub(to).Mul(1.0 / 4.0) c := ctrl0.Mul(3).Sub(p.pen).Add(ctrl1.Mul(3)).Sub(to).Mul(1.0 / 4.0)
const maxSplits = 32 const maxSplits = 32
if splits >= maxSplits { if splits >= maxSplits {
p.quadTo(c, to) p.quadTo(ops, c, to)
return splits return splits
} }
// The maximum distance between the cubic P and its approximation Q given t // The maximum distance between the cubic P and its approximation Q given t
@@ -199,7 +191,7 @@ func (p *PathBuilder) approxCubeTo(splits int, maxDist float32, ctrl0, ctrl1, to
v := to.Sub(ctrl1.Mul(3)).Add(ctrl0.Mul(3)).Sub(p.pen) v := to.Sub(ctrl1.Mul(3)).Add(ctrl0.Mul(3)).Sub(p.pen)
d2 := (v.X*v.X + v.Y*v.Y) * 3 / (36 * 36) d2 := (v.X*v.X + v.Y*v.Y) * 3 / (36 * 36)
if d2 <= maxDist*maxDist { if d2 <= maxDist*maxDist {
p.quadTo(c, to) p.quadTo(ops, c, to)
return splits return splits
} }
// De Casteljau split the curve and approximate the halves. // De Casteljau split the curve and approximate the halves.
@@ -211,8 +203,8 @@ func (p *PathBuilder) approxCubeTo(splits int, maxDist float32, ctrl0, ctrl1, to
c12 := c1.Add(c2.Sub(c1).Mul(t)) c12 := c1.Add(c2.Sub(c1).Mul(t))
c0112 := c01.Add(c12.Sub(c01).Mul(t)) c0112 := c01.Add(c12.Sub(c01).Mul(t))
splits++ splits++
splits = p.approxCubeTo(splits, maxDist, c0, c01, c0112) splits = p.approxCubeTo(ops, splits, maxDist, c0, c01, c0112)
splits = p.approxCubeTo(splits, maxDist, c12, c2, to) splits = p.approxCubeTo(ops, splits, maxDist, c12, c2, to)
return splits return splits
} }
@@ -228,8 +220,9 @@ func (p *PathBuilder) expand(b f32.Rectangle) {
p.bounds = p.bounds.Union(b) p.bounds = p.bounds.Union(b)
} }
func (p *PathBuilder) vertex(cornerx, cornery int16, ctrl, to f32.Point) { func (p *PathBuilder) vertex(o *ui.Ops, cornerx, cornery int16, ctrl, to f32.Point) {
p.verts = append(p.verts, path.Vertex{ p.nverts++
v := path.Vertex{
CornerX: cornerx, CornerX: cornerx,
CornerY: cornery, CornerY: cornery,
FromX: p.pen.X, FromX: p.pen.X,
@@ -238,10 +231,25 @@ func (p *PathBuilder) vertex(cornerx, cornery int16, ctrl, to f32.Point) {
CtrlY: ctrl.Y, CtrlY: ctrl.Y,
ToX: to.X, ToX: to.X,
ToY: to.Y, ToY: to.Y,
}) }
data := make([]byte, path.VertStride+1)
data[0] = byte(ops.TypeAux)
bo := binary.LittleEndian
data[1] = byte(uint16(v.CornerX))
data[2] = byte(uint16(v.CornerX) >> 8)
data[3] = byte(uint16(v.CornerY))
data[4] = byte(uint16(v.CornerY) >> 8)
bo.PutUint32(data[5:], math.Float32bits(v.MaxY))
bo.PutUint32(data[9:], math.Float32bits(v.FromX))
bo.PutUint32(data[13:], math.Float32bits(v.FromY))
bo.PutUint32(data[17:], math.Float32bits(v.CtrlX))
bo.PutUint32(data[21:], math.Float32bits(v.CtrlY))
bo.PutUint32(data[25:], math.Float32bits(v.ToX))
bo.PutUint32(data[29:], math.Float32bits(v.ToY))
o.Write(data, nil)
} }
func (p *PathBuilder) simpleQuadTo(ctrl, to f32.Point) { func (p *PathBuilder) simpleQuadTo(ops *ui.Ops, ctrl, to f32.Point) {
if p.pen.Y > p.maxy { if p.pen.Y > p.maxy {
p.maxy = p.pen.Y p.maxy = p.pen.Y
} }
@@ -252,25 +260,19 @@ func (p *PathBuilder) simpleQuadTo(ctrl, to f32.Point) {
p.maxy = to.Y p.maxy = to.Y
} }
// NW. // NW.
p.vertex(-1, 1, ctrl, to) p.vertex(ops, -1, 1, ctrl, to)
// NE. // NE.
p.vertex(1, 1, ctrl, to) p.vertex(ops, 1, 1, ctrl, to)
// SW. // SW.
p.vertex(-1, -1, ctrl, to) p.vertex(ops, -1, -1, ctrl, to)
// SE. // SE.
p.vertex(1, -1, ctrl, to) p.vertex(ops, 1, -1, ctrl, to)
p.pen = to p.pen = to
} }
func (p *PathBuilder) Path() *Path { func (p *PathBuilder) End(ops *ui.Ops) {
p.end() p.end(ops)
data := &Path{ opClip{
data: &path.Path{ bounds: p.bounds,
Bounds: p.bounds, }.Add(ops)
},
}
if !p.bounds.Empty() {
data.data.Vertices = p.verts
}
return data
} }
+6 -3
View File
@@ -11,7 +11,6 @@ const (
TypeTransform TypeTransform
TypeLayer TypeLayer
TypeRedraw TypeRedraw
TypeClip
TypeImage TypeImage
TypeDraw TypeDraw
TypeColor TypeColor
@@ -20,6 +19,8 @@ const (
TypeHideInput TypeHideInput
TypePush TypePush
TypePop TypePop
TypeAux
TypeClip
) )
const ( const (
@@ -28,7 +29,6 @@ const (
TypeTransformLen = 1 + 4*2 TypeTransformLen = 1 + 4*2
TypeLayerLen = 1 TypeLayerLen = 1
TypeRedrawLen = 1 + 8 TypeRedrawLen = 1 + 8
TypeClipLen = 1
TypeImageLen = 1 + 4*4 TypeImageLen = 1 + 4*4
TypeDrawLen = 1 + 4*4 TypeDrawLen = 1 + 4*4
TypeColorLen = 1 + 4 TypeColorLen = 1 + 4
@@ -37,13 +37,14 @@ const (
TypeHideInputLen = 1 TypeHideInputLen = 1
TypePushLen = 1 TypePushLen = 1
TypePopLen = 1 TypePopLen = 1
TypeAuxLen = 1 + 4
TypeClipLen = 1 + 4*4
TypeBlockDefRefs = 0 TypeBlockDefRefs = 0
TypeBlockRefs = 1 TypeBlockRefs = 1
TypeTransformRefs = 0 TypeTransformRefs = 0
TypeLayerRefs = 0 TypeLayerRefs = 0
TypeRedrawRefs = 0 TypeRedrawRefs = 0
TypeClipRefs = 1
TypeImageRefs = 1 TypeImageRefs = 1
TypeDrawRefs = 0 TypeDrawRefs = 0
TypeColorRefs = 0 TypeColorRefs = 0
@@ -52,4 +53,6 @@ const (
TypeHideInputRefs = 0 TypeHideInputRefs = 0
TypePushRefs = 0 TypePushRefs = 0
TypePopRefs = 0 TypePopRefs = 0
TypeAuxRefs = 0
TypeClipRefs = 0
) )
-7
View File
@@ -4,15 +4,8 @@ package path
import ( import (
"unsafe" "unsafe"
"gioui.org/ui/f32"
) )
type Path struct {
Vertices []Vertex
Bounds f32.Rectangle
}
// The vertex data suitable for passing to vertex programs. // The vertex data suitable for passing to vertex programs.
type Vertex struct { type Vertex struct {
CornerX, CornerY int16 CornerX, CornerY int16
+3 -3
View File
@@ -80,14 +80,14 @@ func resolveFocus(r *ui.OpsReader, focus Key) (Key, listenerPriority, bool) {
var hide bool var hide bool
loop: loop:
for { for {
data, refs, ok := r.Decode() encOp, ok := r.Decode()
if !ok { if !ok {
break break
} }
switch ops.OpType(data[0]) { switch ops.OpType(encOp.Data[0]) {
case ops.TypeKeyHandler: case ops.TypeKeyHandler:
var op OpHandler var op OpHandler
op.Decode(data, refs) op.Decode(encOp.Data, encOp.Refs)
var newPri listenerPriority var newPri listenerPriority
switch { switch {
case op.Focus: case op.Focus:
+1 -1
View File
@@ -179,7 +179,7 @@ func (l *List) Layout(ops *ui.Ops) Dimens {
Max: axisPoint(l.Axis, max, ui.Inf), Max: axisPoint(l.Axis, max, ui.Inf),
} }
ui.OpPush{}.Add(ops) ui.OpPush{}.Add(ops)
draw.OpClip{Path: draw.RectPath(r)}.Add(ops) draw.RectClip(ops, r)
ui.OpTransform{ ui.OpTransform{
Transform: ui.Offset(toPointF(axisPoint(l.Axis, pos, cross))), Transform: ui.Offset(toPointF(axisPoint(l.Axis, pos, cross))),
}.Add(ops) }.Add(ops)
+12 -9
View File
@@ -30,7 +30,7 @@ type cachedLayout struct {
type cachedPath struct { type cachedPath struct {
active bool active bool
path *draw.Path path ui.OpBlock
} }
type layoutKey struct { type layoutKey struct {
@@ -121,7 +121,7 @@ func (f *textFace) Layout(str string, singleLine bool, maxWidth int) *text.Layou
return l return l
} }
func (f *textFace) Path(str text.String) *draw.Path { func (f *textFace) Path(str text.String) ui.OpBlock {
ppem := fixed.Int26_6(f.faces.Cfg.Val(f.size)*64 + .5) ppem := fixed.Int26_6(f.faces.Cfg.Val(f.size)*64 + .5)
pk := pathKey{ pk := pathKey{
f: f.font.Font, f: f.font.Font,
@@ -229,11 +229,13 @@ func layoutText(ppem fixed.Int26_6, str string, f *opentype, singleLine bool, ma
return &text.Layout{Lines: lines} return &text.Layout{Lines: lines}
} }
func textPath(ppem fixed.Int26_6, f *opentype, str text.String) *draw.Path { func textPath(ppem fixed.Int26_6, f *opentype, str text.String) ui.OpBlock {
var lastPos f32.Point var lastPos f32.Point
var builder draw.PathBuilder var builder draw.PathBuilder
ops := new(ui.Ops)
var x fixed.Int26_6 var x fixed.Int26_6
var advIdx int var advIdx int
ops.Begin()
for _, r := range str.String { for _, r := range str.String {
if !unicode.IsSpace(r) { if !unicode.IsSpace(r) {
segs, ok := f.LoadGlyph(ppem, r) segs, ok := f.LoadGlyph(ppem, r)
@@ -244,7 +246,7 @@ func textPath(ppem fixed.Int26_6, f *opentype, str text.String) *draw.Path {
pos := f32.Point{ pos := f32.Point{
X: float32(x) / 64, X: float32(x) / 64,
} }
builder.Move(pos.Sub(lastPos)) builder.Move(ops, pos.Sub(lastPos))
lastPos = pos lastPos = pos
var lastArg f32.Point var lastArg f32.Point
// Convert sfnt.Segments to relative segments. // Convert sfnt.Segments to relative segments.
@@ -269,13 +271,13 @@ func textPath(ppem fixed.Int26_6, f *opentype, str text.String) *draw.Path {
} }
switch fseg.Op { switch fseg.Op {
case sfnt.SegmentOpMoveTo: case sfnt.SegmentOpMoveTo:
builder.Move(args[0]) builder.Move(ops, args[0])
case sfnt.SegmentOpLineTo: case sfnt.SegmentOpLineTo:
builder.Line(args[0]) builder.Line(ops, args[0])
case sfnt.SegmentOpQuadTo: case sfnt.SegmentOpQuadTo:
builder.Quad(args[0], args[1]) builder.Quad(ops, args[0], args[1])
case sfnt.SegmentOpCubeTo: case sfnt.SegmentOpCubeTo:
builder.Cube(args[0], args[1], args[2]) builder.Cube(ops, args[0], args[1], args[2])
default: default:
panic("unsupported segment op") panic("unsupported segment op")
} }
@@ -285,5 +287,6 @@ func textPath(ppem fixed.Int26_6, f *opentype, str text.String) *draw.Path {
x += str.Advances[advIdx] x += str.Advances[advIdx]
advIdx++ advIdx++
} }
return builder.Path() builder.End(ops)
return ops.End()
} }
+84 -13
View File
@@ -11,6 +11,10 @@ type Ops struct {
// Stack of block start indices. // Stack of block start indices.
stack []pc stack []pc
ops opsData ops opsData
inAux bool
auxOff int
auxLen int
} }
type opsData struct { type opsData struct {
@@ -21,14 +25,30 @@ type opsData struct {
refs []interface{} refs []interface{}
} }
// OpsReader parses an ops list. Internal use only.
type OpsReader struct { type OpsReader struct {
pc pc pc pc
stack []block stack []block
ops opsData ops *opsData
}
// EncodedOp represents an encoded op returned by
// OpsReader. Internal use only.
type EncodedOp struct {
Key OpKey
Data []byte
Refs []interface{}
}
// OpKey is a unique key for a given op. Internal use only.
type OpKey struct {
ops *opsData
pc int
version int
} }
type block struct { type block struct {
ops opsData ops *opsData
retPC pc retPC pc
endPC pc endPC pc
} }
@@ -44,7 +64,6 @@ var typeLengths = [...]int{
ops.TypeTransformLen, ops.TypeTransformLen,
ops.TypeLayerLen, ops.TypeLayerLen,
ops.TypeRedrawLen, ops.TypeRedrawLen,
ops.TypeClipLen,
ops.TypeImageLen, ops.TypeImageLen,
ops.TypeDrawLen, ops.TypeDrawLen,
ops.TypeColorLen, ops.TypeColorLen,
@@ -53,6 +72,8 @@ var typeLengths = [...]int{
ops.TypeHideInputLen, ops.TypeHideInputLen,
ops.TypePushLen, ops.TypePushLen,
ops.TypePopLen, ops.TypePopLen,
ops.TypeAuxLen,
ops.TypeClipLen,
} }
var refLengths = [...]int{ var refLengths = [...]int{
@@ -61,7 +82,6 @@ var refLengths = [...]int{
ops.TypeTransformRefs, ops.TypeTransformRefs,
ops.TypeLayerRefs, ops.TypeLayerRefs,
ops.TypeRedrawRefs, ops.TypeRedrawRefs,
ops.TypeClipRefs,
ops.TypeImageRefs, ops.TypeImageRefs,
ops.TypeDrawRefs, ops.TypeDrawRefs,
ops.TypeColorRefs, ops.TypeColorRefs,
@@ -70,6 +90,8 @@ var refLengths = [...]int{
ops.TypeHideInputRefs, ops.TypeHideInputRefs,
ops.TypePushRefs, ops.TypePushRefs,
ops.TypePopRefs, ops.TypePopRefs,
ops.TypeAuxRefs,
ops.TypeClipRefs,
} }
type OpPush struct{} type OpPush struct{}
@@ -77,7 +99,7 @@ type OpPush struct{}
type OpPop struct{} type OpPop struct{}
type OpBlock struct { type OpBlock struct {
ops *Ops ops *opsData
version int version int
pc pc pc pc
} }
@@ -86,6 +108,10 @@ type opBlockDef struct {
endpc pc endpc pc
} }
type opAux struct {
len int
}
func (p OpPush) Add(o *Ops) { func (p OpPush) Add(o *Ops) {
o.Write([]byte{byte(ops.TypePush)}, nil) o.Write([]byte{byte(ops.TypePush)}, nil)
} }
@@ -101,6 +127,16 @@ func (o *Ops) Begin() {
o.Write(make([]byte, ops.TypeBlockDefLen), nil) o.Write(make([]byte, ops.TypeBlockDefLen), nil)
} }
func (op *opAux) decode(data []byte) {
if ops.OpType(data[0]) != ops.TypeAux {
panic("invalid op")
}
bo := binary.LittleEndian
*op = opAux{
len: int(bo.Uint32(data[1:])),
}
}
func (op *opBlockDef) decode(data []byte) { func (op *opBlockDef) decode(data []byte) {
if ops.OpType(data[0]) != ops.TypeBlockDef { if ops.OpType(data[0]) != ops.TypeBlockDef {
panic("invalid op") panic("invalid op")
@@ -128,15 +164,24 @@ func (o *Ops) End() OpBlock {
bo := binary.LittleEndian bo := binary.LittleEndian
bo.PutUint32(data[1:], uint32(pc.data)) bo.PutUint32(data[1:], uint32(pc.data))
bo.PutUint32(data[5:], uint32(pc.refs)) bo.PutUint32(data[5:], uint32(pc.refs))
return OpBlock{ops: o, pc: start, version: o.ops.version} return OpBlock{ops: &o.ops, pc: start, version: o.ops.version}
} }
// Reset clears the Ops. // Reset clears the Ops.
func (o *Ops) Reset() { func (o *Ops) Reset() {
o.inAux = false
o.stack = o.stack[:0] o.stack = o.stack[:0]
o.ops.reset() o.ops.reset()
} }
// Internal use only.
func (o *Ops) Aux() []byte {
if !o.inAux {
return nil
}
return o.ops.data[o.auxOff+ops.TypeAuxLen : o.auxOff+ops.TypeAuxLen+o.auxLen]
}
func (d *opsData) reset() { func (d *opsData) reset() {
d.data = d.data[:0] d.data = d.data[:0]
d.refs = d.refs[:0] d.refs = d.refs[:0]
@@ -149,6 +194,26 @@ func (d *opsData) write(op []byte, refs []interface{}) {
} }
func (o *Ops) Write(op []byte, refs []interface{}) { func (o *Ops) Write(op []byte, refs []interface{}) {
switch ops.OpType(op[0]) {
case ops.TypeAux:
// Write only the data.
op = op[1:]
if !o.inAux {
o.inAux = true
o.auxOff = o.ops.pc().data
o.auxLen = 0
header := make([]byte, ops.TypeAuxLen)
header[0] = byte(ops.TypeAux)
o.ops.write(header, nil)
}
o.auxLen += len(op)
default:
if o.inAux {
o.inAux = false
bo := binary.LittleEndian
bo.PutUint32(o.ops.data[o.auxOff+1:], uint32(o.auxLen))
}
}
o.ops.write(op, refs) o.ops.write(op, refs)
} }
@@ -165,7 +230,7 @@ func (b *OpBlock) decode(data []byte, refs []interface{}) {
refsIdx := int(bo.Uint32(data[5:])) refsIdx := int(bo.Uint32(data[5:]))
version := int(bo.Uint32(data[9:])) version := int(bo.Uint32(data[9:]))
*b = OpBlock{ *b = OpBlock{
ops: refs[0].(*Ops), ops: refs[0].(*opsData),
pc: pc{ pc: pc{
data: dataIdx, data: dataIdx,
refs: refsIdx, refs: refsIdx,
@@ -186,12 +251,12 @@ func (b OpBlock) Add(o *Ops) {
// Reset start reading from the op list. // Reset start reading from the op list.
func (r *OpsReader) Reset(ops *Ops) { func (r *OpsReader) Reset(ops *Ops) {
r.ops = ops.ops r.ops = &ops.ops
r.stack = r.stack[:0] r.stack = r.stack[:0]
r.pc = pc{} r.pc = pc{}
} }
func (r *OpsReader) Decode() ([]byte, []interface{}, bool) { func (r *OpsReader) Decode() (EncodedOp, bool) {
for { for {
if len(r.stack) > 0 { if len(r.stack) > 0 {
b := r.stack[len(r.stack)-1] b := r.stack[len(r.stack)-1]
@@ -203,22 +268,28 @@ func (r *OpsReader) Decode() ([]byte, []interface{}, bool) {
} }
} }
if r.pc.data == len(r.ops.data) { if r.pc.data == len(r.ops.data) {
return nil, nil, false return EncodedOp{}, false
} }
key := OpKey{ops: r.ops, pc: r.pc.data, version: r.ops.version}
t := ops.OpType(r.ops.data[r.pc.data]) t := ops.OpType(r.ops.data[r.pc.data])
n := typeLengths[t-ops.FirstOpIndex] n := typeLengths[t-ops.FirstOpIndex]
nrefs := refLengths[t-ops.FirstOpIndex] nrefs := refLengths[t-ops.FirstOpIndex]
data := r.ops.data[r.pc.data : r.pc.data+n] data := r.ops.data[r.pc.data : r.pc.data+n]
refs := r.ops.refs[r.pc.refs : r.pc.refs+nrefs] refs := r.ops.refs[r.pc.refs : r.pc.refs+nrefs]
switch t { switch t {
case ops.TypeAux:
var op opAux
op.decode(data)
n += op.len
data = r.ops.data[r.pc.data : r.pc.data+n]
case ops.TypeBlock: case ops.TypeBlock:
var op OpBlock var op OpBlock
op.decode(data, refs) op.decode(data, refs)
blockOps := op.ops.ops blockOps := op.ops
if ops.OpType(blockOps.data[op.pc.data]) != ops.TypeBlockDef { if ops.OpType(blockOps.data[op.pc.data]) != ops.TypeBlockDef {
panic("invalid block reference") panic("invalid block reference")
} }
if op.version != r.ops.version { if op.version != op.ops.version {
panic("invalid OpBlock reference to reset Ops") panic("invalid OpBlock reference to reset Ops")
} }
var opDef opBlockDef var opDef opBlockDef
@@ -244,6 +315,6 @@ func (r *OpsReader) Decode() ([]byte, []interface{}, bool) {
} }
r.pc.data += n r.pc.data += n
r.pc.refs += nrefs r.pc.refs += nrefs
return data, refs, true return EncodedOp{Key: key, Data: data, Refs: refs}, true
} }
} }
+4 -4
View File
@@ -39,11 +39,11 @@ type handler struct {
func (q *Queue) collectHandlers(r *ui.OpsReader, t ui.Transform, layer int) { func (q *Queue) collectHandlers(r *ui.OpsReader, t ui.Transform, layer int) {
for { for {
data, refs, ok := r.Decode() encOp, ok := r.Decode()
if !ok { if !ok {
return return
} }
switch ops.OpType(data[0]) { switch ops.OpType(encOp.Data[0]) {
case ops.TypePush: case ops.TypePush:
q.collectHandlers(r, t, layer) q.collectHandlers(r, t, layer)
case ops.TypePop: case ops.TypePop:
@@ -53,11 +53,11 @@ func (q *Queue) collectHandlers(r *ui.OpsReader, t ui.Transform, layer int) {
q.hitTree = append(q.hitTree, hitNode{level: layer}) q.hitTree = append(q.hitTree, hitNode{level: layer})
case ops.TypeTransform: case ops.TypeTransform:
var op ui.OpTransform var op ui.OpTransform
op.Decode(data) op.Decode(encOp.Data)
t = t.Mul(op.Transform) t = t.Mul(op.Transform)
case ops.TypePointerHandler: case ops.TypePointerHandler:
var op OpHandler var op OpHandler
op.Decode(data, refs) op.Decode(encOp.Data, encOp.Refs)
q.hitTree = append(q.hitTree, hitNode{level: layer, key: op.Key}) q.hitTree = append(q.hitTree, hitNode{level: layer, key: op.Key})
h, ok := q.handlers[op.Key] h, ok := q.handlers[op.Key]
if !ok { if !ok {
+1 -8
View File
@@ -10,7 +10,6 @@ import (
"gioui.org/ui" "gioui.org/ui"
"gioui.org/ui/draw" "gioui.org/ui/draw"
"gioui.org/ui/f32"
"gioui.org/ui/gesture" "gioui.org/ui/gesture"
"gioui.org/ui/key" "gioui.org/ui/key"
"gioui.org/ui/layout" "gioui.org/ui/layout"
@@ -49,11 +48,6 @@ type Editor struct {
clicker gesture.Click clicker gesture.Click
} }
type linePath struct {
path *draw.Path
off f32.Point
}
const ( const (
blinksPerSecond = 1 blinksPerSecond = 1
maxBlinkDuration = 10 * time.Second maxBlinkDuration = 10 * time.Second
@@ -170,10 +164,9 @@ func (e *Editor) Layout(ops *ui.Ops, cs layout.Constraints) layout.Dimens {
if !ok { if !ok {
break break
} }
path := e.Face.Path(str)
ui.OpPush{}.Add(ops) ui.OpPush{}.Add(ops)
ui.OpTransform{Transform: ui.Offset(lineOff)}.Add(ops) ui.OpTransform{Transform: ui.Offset(lineOff)}.Add(ops)
draw.OpClip{Path: path}.Add(ops) e.Face.Path(str).Add(ops)
draw.OpDraw{Rect: toRectF(clip).Sub(lineOff)}.Add(ops) draw.OpDraw{Rect: toRectF(clip).Sub(lineOff)}.Add(ops)
ui.OpPop{}.Add(ops) ui.OpPop{}.Add(ops)
} }
+1 -2
View File
@@ -100,11 +100,10 @@ func (l Label) Layout(ops *ui.Ops, cs layout.Constraints) layout.Dimens {
if !ok { if !ok {
break break
} }
path := l.Face.Path(str)
lclip := toRectF(clip).Sub(off) lclip := toRectF(clip).Sub(off)
ui.OpPush{}.Add(ops) ui.OpPush{}.Add(ops)
ui.OpTransform{Transform: ui.Offset(off)}.Add(ops) ui.OpTransform{Transform: ui.Offset(off)}.Add(ops)
draw.OpClip{Path: path}.Add(ops) l.Face.Path(str).Add(ops)
draw.OpDraw{Rect: lclip}.Add(ops) draw.OpDraw{Rect: lclip}.Add(ops)
ui.OpPop{}.Add(ops) ui.OpPop{}.Add(ops)
} }
+2 -2
View File
@@ -6,7 +6,7 @@ import (
"fmt" "fmt"
"image" "image"
"gioui.org/ui/draw" "gioui.org/ui"
"gioui.org/ui/layout" "gioui.org/ui/layout"
"golang.org/x/image/math/fixed" "golang.org/x/image/math/fixed"
) )
@@ -35,7 +35,7 @@ type Layout struct {
type Face interface { type Face interface {
Layout(str string, singleLine bool, maxWidth int) *Layout Layout(str string, singleLine bool, maxWidth int) *Layout
Path(str String) *draw.Path Path(str String) ui.OpBlock
} }
type Alignment uint8 type Alignment uint8