From 1eab4b84a6d368c44738769a7fb23e4fe057d0f2 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Mon, 25 Oct 2021 11:20:14 +0200 Subject: [PATCH] gpu,internal/ops: move gpu.clipOp to ops.ClipOp We're about to use clip.Ops for pointer areas; this change makes the decoding accessible from the router package. Signed-off-by: Elias Naur --- gpu/compute.go | 7 ++++--- gpu/gpu.go | 48 ++++++++++----------------------------------- internal/ops/ops.go | 28 ++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 41 deletions(-) diff --git a/gpu/compute.go b/gpu/compute.go index 05aee8df..3baebe60 100644 --- a/gpu/compute.go +++ b/gpu/compute.go @@ -1772,9 +1772,10 @@ func (c *collector) collect(root *op.Ops, viewport image.Point, texOps *[]textur pathData.key = encOp.Key pathData.hash = hash case ops.TypeClip: - var op clipOp - op.decode(encOp.Data) - c.addClip(&state, fview, op.bounds, pathData.data, pathData.key, pathData.hash, strWidth, true) + var op ops.ClipOp + op.Decode(encOp.Data) + bounds := layout.FRect(op.Bounds) + c.addClip(&state, fview, bounds, pathData.data, pathData.key, pathData.hash, strWidth, true) pathData.data = nil strWidth = 0 case ops.TypePopClip: diff --git a/gpu/gpu.go b/gpu/gpu.go index 0ee15c90..b3f52f70 100644 --- a/gpu/gpu.go +++ b/gpu/gpu.go @@ -165,13 +165,6 @@ type material struct { uvTrans f32.Affine2D } -// clipOp is the shadow of clip.Op. -type clipOp struct { - // TODO: Use image.Rectangle? - bounds f32.Rectangle - outline bool -} - // imageOpData is the shadow of paint.ImageOp. type imageOpData struct { src *image.RGBA @@ -185,27 +178,6 @@ type linearGradientOpData struct { color2 color.NRGBA } -func (op *clipOp) decode(data []byte) { - if ops.OpType(data[0]) != ops.TypeClip { - panic("invalid op") - } - bo := binary.LittleEndian - r := image.Rectangle{ - Min: image.Point{ - X: int(int32(bo.Uint32(data[1:]))), - Y: int(int32(bo.Uint32(data[5:]))), - }, - Max: image.Point{ - X: int(int32(bo.Uint32(data[9:]))), - Y: int(int32(bo.Uint32(data[13:]))), - }, - } - *op = clipOp{ - bounds: layout.FRect(r), - outline: data[17] == 1, - } -} - func decodeImageOp(data []byte, refs []interface{}) imageOpData { if ops.OpType(data[0]) != ops.TypeImage { panic("invalid op") @@ -937,10 +909,10 @@ loop: quads.key.Key = encOp.Key case ops.TypeClip: - var op clipOp - op.decode(encOp.Data) - quads.key.outline = op.outline - bounds := op.bounds + var op ops.ClipOp + op.Decode(encOp.Data) + quads.key.outline = op.Outline + bounds := layout.FRect(op.Bounds) trans, off := splitTransform(state.t) if len(quads.aux) > 0 { // There is a clipping path, build the gpu data and update the @@ -950,22 +922,22 @@ loop: if v, ok := d.pathCache.get(quads.key); ok { // Since the GPU data exists in the cache aux will not be used. // Why is this not used for the offset shapes? - op.bounds = v.bounds + bounds = v.bounds } else { - pathData, bounds := d.buildVerts( + var pathData []byte + pathData, bounds = d.buildVerts( quads.aux, trans, quads.key.outline, quads.key.strokeWidth, ) - op.bounds = bounds quads.aux = pathData // add it to the cache, without GPU data, so the transform can be // reused. - d.pathCache.put(quads.key, opCacheValue{bounds: op.bounds}) + d.pathCache.put(quads.key, opCacheValue{bounds: bounds}) } } else { - quads.aux, op.bounds, _ = d.boundsForTransformedRect(bounds, trans) + quads.aux, bounds, _ = d.boundsForTransformedRect(bounds, trans) quads.key = opKey{Key: encOp.Key} } - d.addClipPath(&state, quads.aux, quads.key, op.bounds, off, true) + d.addClipPath(&state, quads.aux, quads.key, bounds, off, true) quads = quadsOp{} case ops.TypePopClip: state.cpath = state.cpath.parent diff --git a/internal/ops/ops.go b/internal/ops/ops.go index e13bbe85..103eabbb 100644 --- a/internal/ops/ops.go +++ b/internal/ops/ops.go @@ -4,6 +4,7 @@ package ops import ( "encoding/binary" + "image" "math" "gioui.org/f32" @@ -86,6 +87,12 @@ type stack struct { type StackKind uint8 +// ClipOp is the shadow of clip.Op. +type ClipOp struct { + Bounds image.Rectangle + Outline bool +} + const ( ClipStack StackKind = iota AreaStack @@ -126,6 +133,27 @@ const ( TypeStrokeLen = 1 + 4 ) +func (op *ClipOp) Decode(data []byte) { + if OpType(data[0]) != TypeClip { + panic("invalid op") + } + bo := binary.LittleEndian + r := image.Rectangle{ + Min: image.Point{ + X: int(int32(bo.Uint32(data[1:]))), + Y: int(int32(bo.Uint32(data[5:]))), + }, + Max: image.Point{ + X: int(int32(bo.Uint32(data[9:]))), + Y: int(int32(bo.Uint32(data[13:]))), + }, + } + *op = ClipOp{ + Bounds: r, + Outline: data[17] == 1, + } +} + func Reset(o *Ops) { o.macroStack = stack{} for i := range o.stacks {