From edc81ea0bb18d88194bde84b27cc6f929cc81466 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Wed, 11 Dec 2019 23:16:59 +0100 Subject: [PATCH] op: remove operation list argument from MacroOp.Add The ability to invoke other operation lists belongs in the new CallOp. While we're here, make MacroOp.Add use a pointer receiver to match the other methods. Signed-off-by: Elias Naur --- internal/opconst/ops.go | 4 ++-- internal/ops/reader.go | 19 ++++--------------- layout/flex.go | 2 +- layout/layout.go | 2 +- layout/list.go | 4 ++-- layout/stack.go | 2 +- op/clip/clip.go | 2 +- op/op.go | 13 ++++--------- widget/material/editor.go | 2 +- 9 files changed, 17 insertions(+), 33 deletions(-) diff --git a/internal/opconst/ops.go b/internal/opconst/ops.go index 14844751..aac1bcfc 100644 --- a/internal/opconst/ops.go +++ b/internal/opconst/ops.go @@ -31,7 +31,7 @@ const ( const ( TypeMacroDefLen = 1 + 4 + 4 - TypeMacroLen = 1 + 4 + 4 + 4 + TypeMacroLen = 1 + 4 + 4 TypeTransformLen = 1 + 4*2 TypeLayerLen = 1 TypeRedrawLen = 1 + 8 @@ -77,7 +77,7 @@ func (t OpType) Size() int { func (t OpType) NumRefs() int { switch t { - case TypeMacro, TypeKeyInput, TypePointerInput, TypeProfile, TypeCall: + case TypeKeyInput, TypePointerInput, TypeProfile, TypeCall: return 1 case TypeImage: return 2 diff --git a/internal/ops/reader.go b/internal/ops/reader.go index dd2b3406..52d3a401 100644 --- a/internal/ops/reader.go +++ b/internal/ops/reader.go @@ -33,9 +33,7 @@ type Key struct { // Shadow of op.MacroOp. type macroOp struct { - ops *op.Ops - version int - pc pc + pc pc } // Shadow of op.CallOp. @@ -119,16 +117,11 @@ func (r *Reader) Decode() (EncodedOp, bool) { continue case opconst.TypeMacro: var op macroOp - op.decode(data, refs) - macroOps := op.ops - macroData := macroOps.Data() - macroData = macroData[op.pc.data:] + op.decode(data) + macroData := r.ops.Data()[op.pc.data:] if opconst.OpType(macroData[0]) != opconst.TypeMacroDef { panic("invalid macro reference") } - if op.version != op.ops.Version() { - panic("invalid MacroOp reference to reset Ops") - } var opDef opMacroDef opDef.decode(macroData[:opconst.TypeMacroDef.Size()]) retPC := r.pc @@ -139,7 +132,6 @@ func (r *Reader) Decode() (EncodedOp, bool) { retPC: retPC, endPC: opDef.endpc, }) - r.ops = macroOps r.pc = op.pc r.pc.data += opconst.TypeMacroDef.Size() r.pc.refs += opconst.TypeMacroDef.NumRefs() @@ -180,20 +172,17 @@ func (m *callOp) decode(data []byte, refs []interface{}) { } } -func (m *macroOp) decode(data []byte, refs []interface{}) { +func (m *macroOp) decode(data []byte) { if opconst.OpType(data[0]) != opconst.TypeMacro { panic("invalid op") } bo := binary.LittleEndian dataIdx := int(int32(bo.Uint32(data[1:]))) refsIdx := int(int32(bo.Uint32(data[5:]))) - version := int(int32(bo.Uint32(data[9:]))) *m = macroOp{ - ops: refs[0].(*op.Ops), pc: pc{ data: dataIdx, refs: refsIdx, }, - version: version, } } diff --git a/layout/flex.go b/layout/flex.go index 0b17111f..af65a3eb 100644 --- a/layout/flex.go +++ b/layout/flex.go @@ -173,7 +173,7 @@ func (f *Flex) Layout(gtx *Context, children ...FlexChild) { var stack op.StackOp stack.Push(gtx.Ops) op.TransformOp{}.Offset(toPointF(axisPoint(f.Axis, mainSize, cross))).Add(gtx.Ops) - child.macro.Add(gtx.Ops) + child.macro.Add() stack.Pop() mainSize += axisMain(f.Axis, dims.Size) if i < len(children)-1 { diff --git a/layout/layout.go b/layout/layout.go index 9eee28d8..4a98b02d 100644 --- a/layout/layout.go +++ b/layout/layout.go @@ -173,7 +173,7 @@ func (a Align) Layout(gtx *Context, w Widget) { var stack op.StackOp stack.Push(gtx.Ops) op.TransformOp{}.Offset(toPointF(p)).Add(gtx.Ops) - macro.Add(gtx.Ops) + macro.Add() stack.Pop() gtx.Dimensions = Dimensions{ Size: sz, diff --git a/layout/list.go b/layout/list.go index c585ce6a..a9e2a9e1 100644 --- a/layout/list.go +++ b/layout/list.go @@ -262,7 +262,7 @@ func (l *List) layout() Dimensions { stack.Push(ops) clip.Rect{Rect: toRectF(r)}.Op(ops).Add(ops) op.TransformOp{}.Offset(toPointF(axisPoint(l.Axis, pos, cross))).Add(ops) - child.macro.Add(ops) + child.macro.Add() stack.Pop() pos += childSize } @@ -276,7 +276,7 @@ func (l *List) layout() Dimensions { l.macro.Stop() pointer.Rect(image.Rectangle{Max: dims}).Add(ops) l.scroll.Add(ops) - l.macro.Add(ops) + l.macro.Add() return Dimensions{Size: dims} } diff --git a/layout/stack.go b/layout/stack.go index 9e80ea9a..5dd87032 100644 --- a/layout/stack.go +++ b/layout/stack.go @@ -97,7 +97,7 @@ func (s *Stack) Layout(gtx *Context, children ...StackChild) { var stack op.StackOp stack.Push(gtx.Ops) op.TransformOp{}.Offset(toPointF(p)).Add(gtx.Ops) - ch.macro.Add(gtx.Ops) + ch.macro.Add() stack.Pop() if baseline == 0 { if b := ch.dims.Baseline; b != 0 { diff --git a/op/clip/clip.go b/op/clip/clip.go index f0d40655..79c393e3 100644 --- a/op/clip/clip.go +++ b/op/clip/clip.go @@ -40,7 +40,7 @@ type Op struct { } func (p Op) Add(o *op.Ops) { - p.macro.Add(o) + p.macro.Add() data := o.Write(opconst.TypeClipLen) data[0] = byte(opconst.TypeClip) bo := binary.LittleEndian diff --git a/op/op.go b/op/op.go index 354f9a62..c4def563 100644 --- a/op/op.go +++ b/op/op.go @@ -63,7 +63,7 @@ The MacroOp records a list of operations to be executed later: macro.Stop() // replay the recorded operations by calling Add: - macro.Add(ops) + macro.Add() */ package op @@ -106,7 +106,6 @@ type StackOp struct { type MacroOp struct { recording bool ops *Ops - version int id stackID pc pc } @@ -255,25 +254,21 @@ func (m *MacroOp) fill() { bo := binary.LittleEndian bo.PutUint32(data[1:], uint32(pc.data)) bo.PutUint32(data[5:], uint32(pc.refs)) - m.version = m.ops.version } -// Add the recorded list of operations. The Ops -// argument may be different than the Ops argument -// passed to Record. -func (m MacroOp) Add(o *Ops) { +// Add the recorded list of operations. +func (m *MacroOp) Add() { if m.recording { panic("a recording is in progress") } if m.ops == nil { return } - data := o.Write(opconst.TypeMacroLen, m.ops) + data := m.ops.Write(opconst.TypeMacroLen) data[0] = byte(opconst.TypeMacro) bo := binary.LittleEndian bo.PutUint32(data[1:], uint32(m.pc.data)) bo.PutUint32(data[5:], uint32(m.pc.refs)) - bo.PutUint32(data[9:], uint32(m.version)) } func (r InvalidateOp) Add(o *Ops) { diff --git a/widget/material/editor.go b/widget/material/editor.go index 61a5d048..34ec8be7 100644 --- a/widget/material/editor.go +++ b/widget/material/editor.go @@ -56,7 +56,7 @@ func (e Editor) Layout(gtx *layout.Context, editor *widget.Editor) { paint.ColorOp{Color: e.Color}.Add(gtx.Ops) editor.PaintText(gtx) } else { - macro.Add(gtx.Ops) + macro.Add() } paint.ColorOp{Color: e.Color}.Add(gtx.Ops) editor.PaintCaret(gtx)