~eliasnaur/gio

ref: 9f58ed0fea4e7b1f8f69846d8f72bec19390d7ed gio/ui/ops.go -rw-r--r-- 5.0 KiB
9f58ed0fElias Naur ui: add version to OpBlock to track invalidated blocks 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package ui

import (
	"encoding/binary"

	"gioui.org/ui/internal/ops"
)

// Ops holds a list of serialized Ops.
type Ops struct {
	// Stack of block start indices.
	stack []pc
	ops   opsData
}

type opsData struct {
	version int
	// Serialized ops.
	data []byte
	// Op references.
	refs []interface{}
}

type OpsReader struct {
	pc    pc
	stack []block
	ops   opsData
}

type block struct {
	ops   opsData
	retPC pc
	endPC pc
}

type pc struct {
	data int
	refs int
}

var typeLengths = [...]int{
	ops.TypeBlockDefLen,
	ops.TypeBlockLen,
	ops.TypeTransformLen,
	ops.TypeLayerLen,
	ops.TypeRedrawLen,
	ops.TypeClipLen,
	ops.TypeImageLen,
	ops.TypeDrawLen,
	ops.TypeColorLen,
	ops.TypePointerHandlerLen,
	ops.TypeKeyHandlerLen,
	ops.TypeHideInputLen,
	ops.TypePushLen,
	ops.TypePopLen,
}

var refLengths = [...]int{
	ops.TypeBlockDefRefs,
	ops.TypeBlockRefs,
	ops.TypeTransformRefs,
	ops.TypeLayerRefs,
	ops.TypeRedrawRefs,
	ops.TypeClipRefs,
	ops.TypeImageRefs,
	ops.TypeDrawRefs,
	ops.TypeColorRefs,
	ops.TypePointerHandlerRefs,
	ops.TypeKeyHandlerRefs,
	ops.TypeHideInputRefs,
	ops.TypePushRefs,
	ops.TypePopRefs,
}

type OpPush struct{}

type OpPop struct{}

type OpBlock struct {
	ops     *Ops
	version int
	pc      pc
}

type opBlockDef struct {
	endpc pc
}

func (p OpPush) Add(o *Ops) {
	o.Write([]byte{byte(ops.TypePush)}, nil)
}

func (p OpPop) Add(o *Ops) {
	o.Write([]byte{byte(ops.TypePop)}, nil)
}

// Begin a block of ops.
func (o *Ops) Begin() {
	o.stack = append(o.stack, o.ops.pc())
	// Make room for a block definition. Filled out in End.
	o.Write(make([]byte, ops.TypeBlockDefLen), nil)
}

func (op *opBlockDef) decode(data []byte) {
	if ops.OpType(data[0]) != ops.TypeBlockDef {
		panic("invalid op")
	}
	bo := binary.LittleEndian
	dataIdx := int(bo.Uint32(data[1:]))
	refsIdx := int(bo.Uint32(data[5:]))
	*op = opBlockDef{
		endpc: pc{
			data: dataIdx,
			refs: refsIdx,
		},
	}
}

// End the most recent block and return
// an op for invoking the completed block.
func (o *Ops) End() OpBlock {
	start := o.stack[len(o.stack)-1]
	o.stack = o.stack[:len(o.stack)-1]
	pc := o.ops.pc()
	// Write the block header reserved in Begin.
	data := o.ops.data[start.data : start.data+ops.TypeBlockDefLen]
	data[0] = byte(ops.TypeBlockDef)
	bo := binary.LittleEndian
	bo.PutUint32(data[1:], uint32(pc.data))
	bo.PutUint32(data[5:], uint32(pc.refs))
	return OpBlock{ops: o, pc: start, version: o.ops.version}
}

// Reset clears the Ops.
func (o *Ops) Reset() {
	o.stack = o.stack[:0]
	o.ops.reset()
}

func (d *opsData) reset() {
	d.data = d.data[:0]
	d.refs = d.refs[:0]
	d.version++
}

func (d *opsData) write(op []byte, refs []interface{}) {
	d.data = append(d.data, op...)
	d.refs = append(d.refs, refs...)
}

func (o *Ops) Write(op []byte, refs []interface{}) {
	o.ops.write(op, refs)
}

func (d *opsData) pc() pc {
	return pc{data: len(d.data), refs: len(d.refs)}
}

func (b *OpBlock) decode(data []byte, refs []interface{}) {
	if ops.OpType(data[0]) != ops.TypeBlock {
		panic("invalid op")
	}
	bo := binary.LittleEndian
	dataIdx := int(bo.Uint32(data[1:]))
	refsIdx := int(bo.Uint32(data[5:]))
	version := int(bo.Uint32(data[9:]))
	*b = OpBlock{
		ops: refs[0].(*Ops),
		pc: pc{
			data: dataIdx,
			refs: refsIdx,
		},
		version: version,
	}
}

func (b OpBlock) Add(o *Ops) {
	data := make([]byte, ops.TypeBlockLen)
	data[0] = byte(ops.TypeBlock)
	bo := binary.LittleEndian
	bo.PutUint32(data[1:], uint32(b.pc.data))
	bo.PutUint32(data[5:], uint32(b.pc.refs))
	bo.PutUint32(data[9:], uint32(b.version))
	o.Write(data, []interface{}{b.ops})
}

// Reset start reading from the op list.
func (r *OpsReader) Reset(ops *Ops) {
	r.ops = ops.ops
	r.stack = r.stack[:0]
	r.pc = pc{}
}

func (r *OpsReader) Decode() ([]byte, []interface{}, bool) {
	for {
		if len(r.stack) > 0 {
			b := r.stack[len(r.stack)-1]
			if r.pc == b.endPC {
				r.ops = b.ops
				r.pc = b.retPC
				r.stack = r.stack[:len(r.stack)-1]
				continue
			}
		}
		if r.pc.data == len(r.ops.data) {
			return nil, nil, false
		}
		t := ops.OpType(r.ops.data[r.pc.data])
		n := typeLengths[t-ops.FirstOpIndex]
		nrefs := refLengths[t-ops.FirstOpIndex]
		data := r.ops.data[r.pc.data : r.pc.data+n]
		refs := r.ops.refs[r.pc.refs : r.pc.refs+nrefs]
		switch t {
		case ops.TypeBlock:
			var op OpBlock
			op.decode(data, refs)
			blockOps := op.ops.ops
			if ops.OpType(blockOps.data[op.pc.data]) != ops.TypeBlockDef {
				panic("invalid block reference")
			}
			if op.version != r.ops.version {
				panic("invalid OpBlock reference to reset Ops")
			}
			var opDef opBlockDef
			opDef.decode(blockOps.data[op.pc.data : op.pc.data+ops.TypeBlockDefLen])
			retPC := r.pc
			retPC.data += n
			retPC.refs += nrefs
			r.stack = append(r.stack, block{
				ops:   r.ops,
				retPC: retPC,
				endPC: opDef.endpc,
			})
			r.ops = blockOps
			r.pc = op.pc
			r.pc.data += ops.TypeBlockDefLen
			r.pc.refs += ops.TypeBlockDefRefs
			continue
		case ops.TypeBlockDef:
			var op opBlockDef
			op.decode(data)
			r.pc = op.endpc
			continue
		}
		r.pc.data += n
		r.pc.refs += nrefs
		return data, refs, true
	}
}