mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-07 02:15:34 +00:00
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:
+58
-9
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+21
-4
@@ -3,16 +3,17 @@
|
||||
package draw
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"math"
|
||||
|
||||
"gioui.org/ui"
|
||||
"gioui.org/ui/f32"
|
||||
"gioui.org/ui/internal/ops"
|
||||
"gioui.org/ui/internal/path"
|
||||
)
|
||||
|
||||
type OpClip struct {
|
||||
Path *Path
|
||||
Op ui.Op
|
||||
}
|
||||
|
||||
type Path struct {
|
||||
@@ -33,11 +34,25 @@ func (p *Path) Data() interface{} {
|
||||
return p.data
|
||||
}
|
||||
|
||||
func (p OpClip) ChildOp() ui.Op {
|
||||
return p.Op
|
||||
func (c OpClip) Add(o *ui.Ops) {
|
||||
data := make([]byte, ops.TypeClipLen)
|
||||
data[0] = byte(ops.TypeClip)
|
||||
bo := binary.LittleEndian
|
||||
ref := o.Ref(c.Path)
|
||||
bo.PutUint32(data[1:], uint32(ref))
|
||||
o.Write(data)
|
||||
}
|
||||
|
||||
func (p OpClip) ImplementsOp() {}
|
||||
func (c *OpClip) Decode(d []byte, refs []interface{}) {
|
||||
bo := binary.LittleEndian
|
||||
if ops.OpType(d[0]) != ops.TypeClip {
|
||||
panic("invalid op")
|
||||
}
|
||||
ref := int(bo.Uint32(d[1:]))
|
||||
*c = OpClip{
|
||||
Path: refs[ref].(*Path),
|
||||
}
|
||||
}
|
||||
|
||||
// MoveTo moves the pen to the given position.
|
||||
func (p *PathBuilder) Move(to f32.Point) {
|
||||
@@ -265,3 +280,5 @@ func (p *PathBuilder) Path() *Path {
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func (p OpClip) ImplementsOp() {}
|
||||
|
||||
Reference in New Issue
Block a user