diff --git a/io/key/key.go b/io/key/key.go index 1e32f281..7625b545 100644 --- a/io/key/key.go +++ b/io/key/key.go @@ -89,18 +89,16 @@ func (m Modifiers) Contain(m2 Modifiers) bool { } func (h InputOp) Add(o *op.Ops) { - data := make([]byte, opconst.TypeKeyInputLen) + data := o.Write(opconst.TypeKeyInputLen, h.Key) data[0] = byte(opconst.TypeKeyInput) if h.Focus { data[1] = 1 } - o.Write(data, h.Key) } func (h HideInputOp) Add(o *op.Ops) { - data := make([]byte, opconst.TypeHideInputLen) + data := o.Write(opconst.TypeHideInputLen) data[0] = byte(opconst.TypeHideInput) - o.Write(data) } func (EditEvent) ImplementsEvent() {} diff --git a/io/pointer/pointer.go b/io/pointer/pointer.go index 2ae760b4..095a1f58 100644 --- a/io/pointer/pointer.go +++ b/io/pointer/pointer.go @@ -136,7 +136,7 @@ func (op EllipseAreaOp) Add(ops *op.Ops) { } func (op areaOp) add(o *op.Ops) { - data := make([]byte, opconst.TypeAreaLen) + data := o.Write(opconst.TypeAreaLen) data[0] = byte(opconst.TypeArea) data[1] = byte(op.kind) bo := binary.LittleEndian @@ -144,25 +144,22 @@ func (op areaOp) add(o *op.Ops) { bo.PutUint32(data[6:], uint32(op.rect.Min.Y)) bo.PutUint32(data[10:], uint32(op.rect.Max.X)) bo.PutUint32(data[14:], uint32(op.rect.Max.Y)) - o.Write(data) } func (h InputOp) Add(o *op.Ops) { - data := make([]byte, opconst.TypePointerInputLen) + data := o.Write(opconst.TypePointerInputLen, h.Key) data[0] = byte(opconst.TypePointerInput) if h.Grab { data[1] = 1 } - o.Write(data, h.Key) } func (op PassOp) Add(o *op.Ops) { - data := make([]byte, opconst.TypePassLen) + data := o.Write(opconst.TypePassLen) data[0] = byte(opconst.TypePass) if op.Pass { data[1] = 1 } - o.Write(data) } func (t Type) String() string { diff --git a/io/profile/profile.go b/io/profile/profile.go index bdf774fe..c306613f 100644 --- a/io/profile/profile.go +++ b/io/profile/profile.go @@ -24,9 +24,8 @@ type Event struct { } func (p Op) Add(o *op.Ops) { - data := make([]byte, opconst.TypeProfileLen) + data := o.Write(opconst.TypeProfileLen, p.Key) data[0] = byte(opconst.TypeProfile) - o.Write(data, p.Key) } func (p Event) ImplementsEvent() {} diff --git a/op/op.go b/op/op.go index bba9d686..72ff7b18 100644 --- a/op/op.go +++ b/op/op.go @@ -125,7 +125,8 @@ func (s *StackOp) Push(o *Ops) { o.stackDepth++ s.stackDepth = o.stackDepth s.macroDepth = o.macroDepth - o.Write([]byte{byte(opconst.TypePush)}) + data := o.Write(opconst.TypePushLen) + data[0] = byte(opconst.TypePush) } // Pop (restore) a previously Pushed operations state. @@ -141,7 +142,8 @@ func (s *StackOp) Pop() { } s.active = false s.ops.stackDepth-- - s.ops.Write([]byte{byte(opconst.TypePop)}) + data := s.ops.Write(opconst.TypePopLen) + data[0] = byte(opconst.TypePop) } // Reset the Ops, preparing it for re-use. @@ -172,9 +174,10 @@ func (o *Ops) Version() int { } // Write is for internal use only. -func (o *Ops) Write(op []byte, refs ...interface{}) { - o.data = append(o.data, op...) +func (o *Ops) Write(n int, refs ...interface{}) []byte { + o.data = append(o.data, make([]byte, n)...) o.refs = append(o.refs, refs...) + return o.data[len(o.data)-n:] } func (o *Ops) pc() pc { @@ -191,7 +194,7 @@ func (m *MacroOp) Record(o *Ops) { m.ops.macroDepth++ m.pc = o.pc() // Reserve room for a macro definition. Updated in Stop. - m.ops.Write(make([]byte, opconst.TypeMacroDefLen)) + m.ops.Write(opconst.TypeMacroDefLen) m.fill() } @@ -227,17 +230,16 @@ func (m MacroOp) Add(o *Ops) { if m.ops == nil { return } - data := make([]byte, opconst.TypeMacroLen) + data := o.Write(opconst.TypeMacroLen, m.ops) 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)) - o.Write(data, m.ops) } func (r InvalidateOp) Add(o *Ops) { - data := make([]byte, opconst.TypeRedrawLen) + data := o.Write(opconst.TypeRedrawLen) data[0] = byte(opconst.TypeInvalidate) bo := binary.LittleEndian // UnixNano cannot represent the zero time. @@ -247,7 +249,6 @@ func (r InvalidateOp) Add(o *Ops) { bo.PutUint64(data[1:], uint64(nanos)) } } - o.Write(data) } // Offset the transformation. @@ -273,10 +274,9 @@ func (t TransformOp) Multiply(t2 TransformOp) TransformOp { } func (t TransformOp) Add(o *Ops) { - data := make([]byte, opconst.TypeTransformLen) + data := o.Write(opconst.TypeTransformLen) data[0] = byte(opconst.TypeTransform) bo := binary.LittleEndian bo.PutUint32(data[1:], math.Float32bits(t.offset.X)) bo.PutUint32(data[5:], math.Float32bits(t.offset.Y)) - o.Write(data) } diff --git a/op/paint/paint.go b/op/paint/paint.go index 07fe960f..4b71d8c5 100644 --- a/op/paint/paint.go +++ b/op/paint/paint.go @@ -66,33 +66,30 @@ func (i ImageOp) Add(o *op.Ops) { }.Add(o) return } - data := make([]byte, opconst.TypeImageLen) + data := o.Write(opconst.TypeImageLen, i.src) data[0] = byte(opconst.TypeImage) bo := binary.LittleEndian bo.PutUint32(data[1:], uint32(i.size.X)) bo.PutUint32(data[5:], uint32(i.size.Y)) - o.Write(data, i.src) } func (c ColorOp) Add(o *op.Ops) { - data := make([]byte, opconst.TypeColorLen) + data := o.Write(opconst.TypeColorLen) data[0] = byte(opconst.TypeColor) data[1] = c.Color.R data[2] = c.Color.G data[3] = c.Color.B data[4] = c.Color.A - o.Write(data) } func (d PaintOp) Add(o *op.Ops) { - data := make([]byte, opconst.TypePaintLen) + data := o.Write(opconst.TypePaintLen) data[0] = byte(opconst.TypePaint) bo := binary.LittleEndian bo.PutUint32(data[1:], math.Float32bits(d.Rect.Min.X)) bo.PutUint32(data[5:], math.Float32bits(d.Rect.Min.Y)) bo.PutUint32(data[9:], math.Float32bits(d.Rect.Max.X)) bo.PutUint32(data[13:], math.Float32bits(d.Rect.Max.Y)) - o.Write(data) } // RectClip returns a ClipOp corresponding to a pixel aligned diff --git a/op/paint/path.go b/op/paint/path.go index 7768cd41..73f3fab8 100644 --- a/op/paint/path.go +++ b/op/paint/path.go @@ -33,14 +33,13 @@ type ClipOp struct { func (p ClipOp) Add(o *op.Ops) { p.macro.Add(o) - data := make([]byte, opconst.TypeClipLen) + data := o.Write(opconst.TypeClipLen) data[0] = byte(opconst.TypeClip) bo := binary.LittleEndian bo.PutUint32(data[1:], math.Float32bits(p.bounds.Min.X)) bo.PutUint32(data[5:], math.Float32bits(p.bounds.Min.Y)) bo.PutUint32(data[9:], math.Float32bits(p.bounds.Max.X)) bo.PutUint32(data[13:], math.Float32bits(p.bounds.Max.Y)) - o.Write(data) } // Begin the path, storing the path data and final ClipOp into ops. @@ -50,7 +49,8 @@ func (p *Path) Begin(ops *op.Ops) { // Write the TypeAux opcode and a byte for marking whether the // path has had its MaxY filled out. If not, the gpu will fill it // before using it. - ops.Write([]byte{byte(opconst.TypeAux), 0}) + data := ops.Write(2) + data[0] = byte(opconst.TypeAux) } // MoveTo moves the pen to the given position. @@ -238,7 +238,7 @@ func (p *Path) vertex(cornerx, cornery int16, ctrl, to f32.Point) { ToX: to.X, ToY: to.Y, } - data := make([]byte, path.VertStride) + data := p.ops.Write(path.VertStride) bo := binary.LittleEndian data[0] = byte(uint16(v.CornerX)) data[1] = byte(uint16(v.CornerX) >> 8) @@ -252,7 +252,6 @@ func (p *Path) vertex(cornerx, cornery int16, ctrl, to f32.Point) { bo.PutUint32(data[20:], math.Float32bits(v.CtrlY)) bo.PutUint32(data[24:], math.Float32bits(v.ToX)) bo.PutUint32(data[28:], math.Float32bits(v.ToY)) - p.ops.Write(data) } func (p *Path) simpleQuadTo(ctrl, to f32.Point) {