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
+58 -9
View File
@@ -3,11 +3,13 @@
package draw
import (
"encoding/binary"
"image"
"math"
"gioui.org/ui"
"gioui.org/ui/f32"
"gioui.org/ui/internal/ops"
"gioui.org/ui/internal/path"
)
@@ -17,18 +19,65 @@ type OpImage struct {
SrcRect image.Rectangle
}
func (i OpImage) Add(o *ui.Ops) {
data := make([]byte, ops.TypeImageLen)
data[0] = byte(ops.TypeImage)
bo := binary.LittleEndian
ref := o.Ref(i.Src)
bo.PutUint32(data[1:], uint32(ref))
bo.PutUint32(data[5:], math.Float32bits(i.Rect.Min.X))
bo.PutUint32(data[9:], math.Float32bits(i.Rect.Min.Y))
bo.PutUint32(data[13:], math.Float32bits(i.Rect.Max.X))
bo.PutUint32(data[17:], math.Float32bits(i.Rect.Max.Y))
bo.PutUint32(data[21:], uint32(i.SrcRect.Min.X))
bo.PutUint32(data[25:], uint32(i.SrcRect.Min.Y))
bo.PutUint32(data[29:], uint32(i.SrcRect.Max.X))
bo.PutUint32(data[33:], uint32(i.SrcRect.Max.Y))
o.Write(data)
}
func (i *OpImage) Decode(d []byte, refs []interface{}) {
bo := binary.LittleEndian
if ops.OpType(d[0]) != ops.TypeImage {
panic("invalid op")
}
ref := int(bo.Uint32(d[1:]))
r := f32.Rectangle{
Min: f32.Point{
X: math.Float32frombits(bo.Uint32(d[5:])),
Y: math.Float32frombits(bo.Uint32(d[9:])),
},
Max: f32.Point{
X: math.Float32frombits(bo.Uint32(d[13:])),
Y: math.Float32frombits(bo.Uint32(d[17:])),
},
}
sr := image.Rectangle{
Min: image.Point{
X: int(bo.Uint32(d[21:])),
Y: int(bo.Uint32(d[25:])),
},
Max: image.Point{
X: int(bo.Uint32(d[29:])),
Y: int(bo.Uint32(d[33:])),
},
}
*i = OpImage{
Rect: r,
Src: refs[ref].(image.Image),
SrcRect: sr,
}
}
func (OpImage) ImplementsOp() {}
// ClipRect returns a special case of OpClip
// that clips to a pixel aligned rectangular area.
func ClipRect(r image.Rectangle, op ui.Op) OpClip {
return OpClip{
Path: &Path{
data: &path.Path{
Bounds: toRectF(r),
},
// RectPath constructs a path corresponding to
// a pixel aligned rectangular area.
func RectPath(r image.Rectangle) *Path {
return &Path{
data: &path.Path{
Bounds: toRectF(r),
},
Op: op,
}
}