mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-06 09:55:40 +00:00
op: introduce Defer for deferring CallOps
Updates gio#164 Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
@@ -10,6 +10,7 @@ const firstOpIndex = 200
|
|||||||
const (
|
const (
|
||||||
TypeMacro OpType = iota + firstOpIndex
|
TypeMacro OpType = iota + firstOpIndex
|
||||||
TypeCall
|
TypeCall
|
||||||
|
TypeDefer
|
||||||
TypeTransform
|
TypeTransform
|
||||||
TypeLayer
|
TypeLayer
|
||||||
TypeInvalidate
|
TypeInvalidate
|
||||||
@@ -39,6 +40,7 @@ const (
|
|||||||
const (
|
const (
|
||||||
TypeMacroLen = 1 + 4 + 4
|
TypeMacroLen = 1 + 4 + 4
|
||||||
TypeCallLen = 1 + 4 + 4
|
TypeCallLen = 1 + 4 + 4
|
||||||
|
TypeDeferLen = 1
|
||||||
TypeTransformLen = 1 + 4*6
|
TypeTransformLen = 1 + 4*6
|
||||||
TypeLayerLen = 1
|
TypeLayerLen = 1
|
||||||
TypeRedrawLen = 1 + 8
|
TypeRedrawLen = 1 + 8
|
||||||
@@ -69,6 +71,7 @@ func (t OpType) Size() int {
|
|||||||
return [...]int{
|
return [...]int{
|
||||||
TypeMacroLen,
|
TypeMacroLen,
|
||||||
TypeCallLen,
|
TypeCallLen,
|
||||||
|
TypeDeferLen,
|
||||||
TypeTransformLen,
|
TypeTransformLen,
|
||||||
TypeLayerLen,
|
TypeLayerLen,
|
||||||
TypeRedrawLen,
|
TypeRedrawLen,
|
||||||
|
|||||||
+32
-5
@@ -12,9 +12,11 @@ import (
|
|||||||
|
|
||||||
// Reader parses an ops list.
|
// Reader parses an ops list.
|
||||||
type Reader struct {
|
type Reader struct {
|
||||||
pc pc
|
pc pc
|
||||||
stack []macro
|
stack []macro
|
||||||
ops *op.Ops
|
ops *op.Ops
|
||||||
|
deferOps op.Ops
|
||||||
|
deferDone bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncodedOp represents an encoded op returned by
|
// EncodedOp represents an encoded op returned by
|
||||||
@@ -57,6 +59,8 @@ type opMacroDef struct {
|
|||||||
// Reset start reading from the op list.
|
// Reset start reading from the op list.
|
||||||
func (r *Reader) Reset(ops *op.Ops) {
|
func (r *Reader) Reset(ops *op.Ops) {
|
||||||
r.stack = r.stack[:0]
|
r.stack = r.stack[:0]
|
||||||
|
r.deferOps.Reset()
|
||||||
|
r.deferDone = false
|
||||||
r.pc = pc{}
|
r.pc = pc{}
|
||||||
r.ops = ops
|
r.ops = ops
|
||||||
}
|
}
|
||||||
@@ -74,6 +78,7 @@ func (r *Reader) Decode() (EncodedOp, bool) {
|
|||||||
if r.ops == nil {
|
if r.ops == nil {
|
||||||
return EncodedOp{}, false
|
return EncodedOp{}, false
|
||||||
}
|
}
|
||||||
|
deferring := false
|
||||||
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]
|
||||||
@@ -86,18 +91,30 @@ func (r *Reader) Decode() (EncodedOp, bool) {
|
|||||||
}
|
}
|
||||||
data := r.ops.Data()
|
data := r.ops.Data()
|
||||||
data = data[r.pc.data:]
|
data = data[r.pc.data:]
|
||||||
|
refs := r.ops.Refs()
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
return EncodedOp{}, false
|
if r.deferDone {
|
||||||
|
return EncodedOp{}, false
|
||||||
|
}
|
||||||
|
r.deferDone = true
|
||||||
|
// Execute deferred macros.
|
||||||
|
r.ops = &r.deferOps
|
||||||
|
r.pc = pc{}
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
key := Key{ops: r.ops, pc: r.pc.data, version: r.ops.Version()}
|
key := Key{ops: r.ops, pc: r.pc.data, version: r.ops.Version()}
|
||||||
t := opconst.OpType(data[0])
|
t := opconst.OpType(data[0])
|
||||||
n := t.Size()
|
n := t.Size()
|
||||||
nrefs := t.NumRefs()
|
nrefs := t.NumRefs()
|
||||||
data = data[:n]
|
data = data[:n]
|
||||||
refs := r.ops.Refs()
|
|
||||||
refs = refs[r.pc.refs:]
|
refs = refs[r.pc.refs:]
|
||||||
refs = refs[:nrefs]
|
refs = refs[:nrefs]
|
||||||
switch t {
|
switch t {
|
||||||
|
case opconst.TypeDefer:
|
||||||
|
deferring = true
|
||||||
|
r.pc.data += n
|
||||||
|
r.pc.refs += nrefs
|
||||||
|
continue
|
||||||
case opconst.TypeAux:
|
case opconst.TypeAux:
|
||||||
// An Aux operations is always wrapped in a macro, and
|
// An Aux operations is always wrapped in a macro, and
|
||||||
// its length is the remaining space.
|
// its length is the remaining space.
|
||||||
@@ -105,6 +122,16 @@ func (r *Reader) Decode() (EncodedOp, bool) {
|
|||||||
n += block.endPC.data - r.pc.data - opconst.TypeAuxLen
|
n += block.endPC.data - r.pc.data - opconst.TypeAuxLen
|
||||||
data = data[:n]
|
data = data[:n]
|
||||||
case opconst.TypeCall:
|
case opconst.TypeCall:
|
||||||
|
if deferring {
|
||||||
|
deferring = false
|
||||||
|
// Copy macro for deferred execution.
|
||||||
|
if t.NumRefs() != 1 {
|
||||||
|
panic("internal error: unexpected number of macro refs")
|
||||||
|
}
|
||||||
|
deferData := r.deferOps.Write1(t.Size(), refs[0])
|
||||||
|
copy(deferData, data)
|
||||||
|
continue
|
||||||
|
}
|
||||||
var op macroOp
|
var op macroOp
|
||||||
op.decode(data, refs)
|
op.decode(data, refs)
|
||||||
macroData := op.ops.Data()[op.pc.data:]
|
macroData := op.ops.Data()[op.pc.data:]
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 396 B |
@@ -103,6 +103,26 @@ func TestNoClipFromPaint(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDeferredPaint(t *testing.T) {
|
||||||
|
run(t, func(o *op.Ops) {
|
||||||
|
state := op.Save(o)
|
||||||
|
clip.Rect(image.Rect(0, 0, 80, 80)).Op().Add(o)
|
||||||
|
paint.ColorOp{Color: color.NRGBA{A: 0xff, R: 0xff}}.Add(o)
|
||||||
|
m := op.Record(o)
|
||||||
|
paint.PaintOp{}.Add(o)
|
||||||
|
paintMacro := m.Stop()
|
||||||
|
op.Defer(o, paintMacro)
|
||||||
|
paint.ColorOp{Color: color.NRGBA{A: 0xff, G: 0xff}}.Add(o)
|
||||||
|
paint.PaintOp{}.Add(o)
|
||||||
|
state.Load()
|
||||||
|
op.Affine(f32.Affine2D{}.Offset(f32.Pt(10, 10))).Add(o)
|
||||||
|
clip.Rect(image.Rect(0, 0, 80, 80)).Op().Add(o)
|
||||||
|
paint.ColorOp{Color: color.NRGBA{A: 0xff, B: 0xff}}.Add(o)
|
||||||
|
paint.PaintOp{}.Add(o)
|
||||||
|
}, func(r result) {
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func constSqPath() op.CallOp {
|
func constSqPath() op.CallOp {
|
||||||
innerOps := new(op.Ops)
|
innerOps := new(op.Ops)
|
||||||
m := op.Record(innerOps)
|
m := op.Record(innerOps)
|
||||||
|
|||||||
@@ -142,6 +142,31 @@ type pc struct {
|
|||||||
refs int
|
refs int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Defer executes c after all other operations have completed,
|
||||||
|
// including previously deferred operations.
|
||||||
|
// Defer saves the operation state, which is then loaded prior
|
||||||
|
// to execution.
|
||||||
|
//
|
||||||
|
// Note that deferred operations are executed in first-in-first-out
|
||||||
|
// order, unlike the Go facility of the same name.
|
||||||
|
func Defer(o *Ops, c CallOp) {
|
||||||
|
if c.ops == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
state := Save(o)
|
||||||
|
// Wrap c in a macro that loads the saved state before execution.
|
||||||
|
m := Record(o)
|
||||||
|
//
|
||||||
|
state.load()
|
||||||
|
c.Add(o)
|
||||||
|
c = m.Stop()
|
||||||
|
// A Defer is recorded as a TypeDefer followed by the
|
||||||
|
// wrapped macro.
|
||||||
|
data := o.Write(opconst.TypeDeferLen)
|
||||||
|
data[0] = byte(opconst.TypeDefer)
|
||||||
|
c.Add(o)
|
||||||
|
}
|
||||||
|
|
||||||
// Save the current operations state.
|
// Save the current operations state.
|
||||||
func Save(o *Ops) StateOp {
|
func Save(o *Ops) StateOp {
|
||||||
o.nextStateID++
|
o.nextStateID++
|
||||||
@@ -159,12 +184,17 @@ func Save(o *Ops) StateOp {
|
|||||||
|
|
||||||
// Load a previously saved operations state.
|
// Load a previously saved operations state.
|
||||||
func (s StateOp) Load() {
|
func (s StateOp) Load() {
|
||||||
|
if s.ops.macroStack.currentID != s.macroID {
|
||||||
|
panic("load in a different macro than save")
|
||||||
|
}
|
||||||
|
s.load()
|
||||||
|
}
|
||||||
|
|
||||||
|
// load is like Load without the same-macro check.
|
||||||
|
func (s StateOp) load() {
|
||||||
if s.id == 0 {
|
if s.id == 0 {
|
||||||
panic("zero-value op")
|
panic("zero-value op")
|
||||||
}
|
}
|
||||||
if s.ops.macroStack.currentID != s.macroID {
|
|
||||||
panic("pop in a different macro than push")
|
|
||||||
}
|
|
||||||
bo := binary.LittleEndian
|
bo := binary.LittleEndian
|
||||||
data := s.ops.Write(opconst.TypeLoadLen)
|
data := s.ops.Write(opconst.TypeLoadLen)
|
||||||
data[0] = byte(opconst.TypeLoad)
|
data[0] = byte(opconst.TypeLoad)
|
||||||
|
|||||||
Reference in New Issue
Block a user