all: serialize ops

Pros:
- Much less per-frame garbage
- Allow future preprocessing of ops while building it
- Much fewer interface calls and pointer chasing
- Allow future serialization of ops for remote rendering

Cons:
- Slightly clumsier API

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-04-24 13:33:01 +02:00
parent 7b6e1ce35a
commit 252e058766
21 changed files with 809 additions and 385 deletions
+29 -22
View File
@@ -11,18 +11,18 @@ import (
type Stack struct {
Alignment Direction
ops *ui.Ops
cs Constraints
children []stackChild
maxSZ image.Point
baseline int
ccache [10]stackChild
opCache [10]ui.Op
ccache [10]stackChild
}
type stackChild struct {
op ui.Op
dims Dimens
block ui.OpBlock
dims Dimens
}
type Direction uint8
@@ -38,26 +38,31 @@ const (
W
)
func (s *Stack) Init(cs Constraints) *Stack {
func (s *Stack) Init(ops *ui.Ops, cs Constraints) *Stack {
if s.children == nil {
s.children = s.ccache[:0]
}
s.children = s.children[:0]
s.maxSZ = image.Point{}
s.baseline = 0
s.ops = ops
s.cs = cs
return s
}
func (s *Stack) Rigid(w Widget) *Stack {
op, dims := w.Layout(s.cs)
s.ops.Begin()
ui.OpLayer{}.Add(s.ops)
dims := w.Layout(s.ops, s.cs)
b := s.ops.End()
if w := dims.Size.X; w > s.maxSZ.X {
s.maxSZ.X = w
}
if h := dims.Size.Y; h > s.maxSZ.Y {
s.maxSZ.Y = h
}
s.add(op, dims)
s.addjustBaseline(dims)
s.children = append(s.children, stackChild{b, dims})
return s
}
@@ -66,16 +71,21 @@ func (s *Stack) Expand(idx int, w Widget) *Stack {
Width: Constraint{Min: s.maxSZ.X, Max: s.maxSZ.X},
Height: Constraint{Min: s.maxSZ.Y, Max: s.maxSZ.Y},
}
s.add(w.Layout(cs))
s.ops.Begin()
ui.OpLayer{}.Add(s.ops)
dims := w.Layout(s.ops, cs)
b := s.ops.End()
s.addjustBaseline(dims)
if idx < 0 {
idx += len(s.children)
idx += len(s.children) + 1
}
s.children[idx], s.children[len(s.children)-1] = s.children[len(s.children)-1], s.children[idx]
s.children = append(s.children, stackChild{})
copy(s.children[idx+1:], s.children[idx:])
s.children[idx] = stackChild{b, dims}
return s
}
func (s *Stack) add(op ui.Op, dims Dimens) {
s.children = append(s.children, stackChild{op, dims})
func (s *Stack) addjustBaseline(dims Dimens) {
if s.baseline == 0 {
if b := dims.Baseline; b != dims.Size.Y {
s.baseline = b
@@ -83,14 +93,8 @@ func (s *Stack) add(op ui.Op, dims Dimens) {
}
}
func (s *Stack) Layout() (ui.Op, Dimens) {
var ops ui.Ops
if len(s.children) > len(s.opCache) {
ops = make([]ui.Op, len(s.children))
} else {
ops = s.opCache[:len(s.children)]
}
for i, ch := range s.children {
func (s *Stack) Layout() Dimens {
for _, ch := range s.children {
sz := ch.dims.Size
var p image.Point
switch s.Alignment {
@@ -105,13 +109,16 @@ func (s *Stack) Layout() (ui.Op, Dimens) {
case SW, S, SE:
p.Y = s.maxSZ.Y - sz.Y
}
ops[i] = ui.OpLayer{Op: ui.OpTransform{Transform: ui.Offset(toPointF(p)), Op: ch.op}}
s.ops.Begin()
ui.OpTransform{Transform: ui.Offset(toPointF(p))}.Add(s.ops)
ch.block.Add(s.ops)
s.ops.End().Add(s.ops)
}
b := s.baseline
if b == 0 {
b = s.maxSZ.Y
}
return ops, Dimens{
return Dimens{
Size: s.maxSZ,
Baseline: b,
}