Files
gio/gpu/internal/rendertest/clip_test.go
T
Elias Naur 936c266b03 all: [API] split operation stack into per-state stacks
The op.Save and Load methods exist to support the need for
transformation, clip, pointer area state to behave as stacks. For
example, layout needs to apply an offset to its children but not
subsequent operations.

Before this change, op.Save and Load were used to save and restore the
state:

    ops := new(op.Ops)
    // Save state.
    state := op.Save(ops)
    // Apply offset.
    op.Offset(...).Add(ops)
    // Draw with offset applied.
    draw(ops)
    // Restore state.
    state.Load()

A drawback with the op.Save mechanism is that there is no direct
connection between the state change and the saving and loading of state.
This causes confusion as to when a Save/Load is needed and who is
responsible for performing them, which leads to subtle bugs and over-use
of Save/Loads.

This change gets rid of the general state stack and replaces it with
per-state stacks. There is now a stack for transformation, clip, pointer
areas, and they can only be restored by the code pushing state to them.
The example above now becomes:

    ops := new(op.Ops)
    // Push offset to the transformation stack.
    stack := op.Offset(...).Push(ops)
    // Draw with offset applied.
    draw(ops)
    // Restore state.
    stack.Pop()

For convenience, transformation also be Add'ed if the stack operation is
not required.

Simple state such as the current material no longer has a way to be
restored; it is assumed the client of a PaintOp adds their desired
material operation before it.

API change: replace op.Save/Load with explicit Push/Pop scopes for
op.TransformOps, pointer.AreaOps, clip.Ops.

To ease porting, this change retains a version of op.Save/Load that
saves and restores the transformation and clip stacks. It also retains
an Add method for clip.Op.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-10-08 17:21:56 +02:00

567 lines
12 KiB
Go

// SPDX-License-Identifier: Unlicense OR MIT
package rendertest
import (
"image"
"math"
"testing"
"golang.org/x/image/colornames"
"gioui.org/f32"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint"
)
func TestPaintRect(t *testing.T) {
run(t, func(o *op.Ops) {
paint.FillShape(o, red, clip.Rect(image.Rect(0, 0, 50, 50)).Op())
}, func(r result) {
r.expect(0, 0, colornames.Red)
r.expect(49, 0, colornames.Red)
r.expect(50, 0, transparent)
r.expect(10, 50, transparent)
})
}
func TestPaintClippedRect(t *testing.T) {
run(t, func(o *op.Ops) {
defer clip.RRect{Rect: f32.Rect(25, 25, 60, 60)}.Push(o).Pop()
paint.FillShape(o, red, clip.Rect(image.Rect(0, 0, 50, 50)).Op())
}, func(r result) {
r.expect(0, 0, transparent)
r.expect(24, 35, transparent)
r.expect(25, 35, colornames.Red)
r.expect(50, 0, transparent)
r.expect(10, 50, transparent)
})
}
func TestPaintClippedCircle(t *testing.T) {
run(t, func(o *op.Ops) {
r := float32(10)
defer clip.RRect{Rect: f32.Rect(20, 20, 40, 40), SE: r, SW: r, NW: r, NE: r}.Push(o).Pop()
defer clip.Rect(image.Rect(0, 0, 30, 50)).Push(o).Pop()
paint.Fill(o, red)
}, func(r result) {
r.expect(21, 21, transparent)
r.expect(25, 30, colornames.Red)
r.expect(31, 30, transparent)
})
}
func TestPaintArc(t *testing.T) {
run(t, func(o *op.Ops) {
p := new(clip.Path)
p.Begin(o)
p.Move(f32.Pt(0, 20))
p.Line(f32.Pt(10, 0))
p.Arc(f32.Pt(10, 0), f32.Pt(40, 0), math.Pi)
p.Line(f32.Pt(30, 0))
p.Line(f32.Pt(0, 25))
p.Arc(f32.Pt(-10, 5), f32.Pt(10, 15), -math.Pi)
p.Line(f32.Pt(0, 25))
p.Arc(f32.Pt(10, 10), f32.Pt(10, 10), 2*math.Pi)
p.Line(f32.Pt(-10, 0))
p.Arc(f32.Pt(-10, 0), f32.Pt(-40, 0), -math.Pi)
p.Line(f32.Pt(-10, 0))
p.Line(f32.Pt(0, -10))
p.Arc(f32.Pt(-10, -20), f32.Pt(10, -5), math.Pi)
p.Line(f32.Pt(0, -10))
p.Line(f32.Pt(-50, 0))
p.Close()
defer clip.Outline{
Path: p.End(),
}.Op().Push(o).Pop()
paint.FillShape(o, red, clip.Rect(image.Rect(0, 0, 128, 128)).Op())
}, func(r result) {
r.expect(0, 0, transparent)
r.expect(0, 25, colornames.Red)
r.expect(0, 15, transparent)
})
}
func TestPaintAbsolute(t *testing.T) {
run(t, func(o *op.Ops) {
p := new(clip.Path)
p.Begin(o)
p.Move(f32.Pt(100, 100)) // offset the initial pen position to test "MoveTo"
p.MoveTo(f32.Pt(20, 20))
p.LineTo(f32.Pt(80, 20))
p.QuadTo(f32.Pt(80, 80), f32.Pt(20, 80))
p.Close()
defer clip.Outline{
Path: p.End(),
}.Op().Push(o).Pop()
paint.FillShape(o, red, clip.Rect(image.Rect(0, 0, 128, 128)).Op())
}, func(r result) {
r.expect(0, 0, transparent)
r.expect(30, 30, colornames.Red)
r.expect(79, 79, transparent)
r.expect(90, 90, transparent)
})
}
func TestPaintTexture(t *testing.T) {
run(t, func(o *op.Ops) {
squares.Add(o)
defer scale(80.0/512, 80.0/512).Push(o).Pop()
paint.PaintOp{}.Add(o)
}, func(r result) {
r.expect(0, 0, colornames.Blue)
r.expect(79, 10, colornames.Green)
r.expect(80, 0, transparent)
r.expect(10, 80, transparent)
})
}
func TestTexturedStrokeClipped(t *testing.T) {
run(t, func(o *op.Ops) {
smallSquares.Add(o)
defer op.Offset(f32.Pt(50, 50)).Push(o).Pop()
defer clip.Stroke{
Path: clip.RRect{Rect: f32.Rect(0, 0, 30, 30)}.Path(o),
Style: clip.StrokeStyle{
Width: 10,
},
}.Op().Push(o).Pop()
defer clip.RRect{Rect: f32.Rect(-30, -30, 60, 60)}.Push(o).Pop()
defer op.Offset(f32.Pt(-10, -10)).Push(o).Pop()
paint.PaintOp{}.Add(o)
}, func(r result) {
})
}
func TestTexturedStroke(t *testing.T) {
run(t, func(o *op.Ops) {
smallSquares.Add(o)
defer op.Offset(f32.Pt(50, 50)).Push(o).Pop()
defer clip.Stroke{
Path: clip.RRect{Rect: f32.Rect(0, 0, 30, 30)}.Path(o),
Style: clip.StrokeStyle{
Width: 10,
},
}.Op().Push(o).Pop()
defer op.Offset(f32.Pt(-10, -10)).Push(o).Pop()
paint.PaintOp{}.Add(o)
}, func(r result) {
})
}
func TestPaintClippedTexture(t *testing.T) {
run(t, func(o *op.Ops) {
squares.Add(o)
defer clip.RRect{Rect: f32.Rect(0, 0, 40, 40)}.Push(o).Pop()
defer scale(80.0/512, 80.0/512).Push(o).Pop()
paint.PaintOp{}.Add(o)
}, func(r result) {
r.expect(40, 40, transparent)
r.expect(25, 35, colornames.Blue)
})
}
func TestStrokedPathBevelFlat(t *testing.T) {
run(t, func(o *op.Ops) {
p := newStrokedPath(o)
defer clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 2.5,
Cap: clip.FlatCap,
Join: clip.BevelJoin,
},
}.Op().Push(o).Pop()
paint.Fill(o, red)
}, func(r result) {
r.expect(0, 0, transparent)
r.expect(10, 50, colornames.Red)
})
}
func TestStrokedPathBevelRound(t *testing.T) {
run(t, func(o *op.Ops) {
p := newStrokedPath(o)
defer clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 2.5,
Cap: clip.RoundCap,
Join: clip.BevelJoin,
},
}.Op().Push(o).Pop()
paint.Fill(o, red)
}, func(r result) {
r.expect(0, 0, transparent)
r.expect(10, 50, colornames.Red)
})
}
func TestStrokedPathBevelSquare(t *testing.T) {
run(t, func(o *op.Ops) {
p := newStrokedPath(o)
defer clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 2.5,
Cap: clip.SquareCap,
Join: clip.BevelJoin,
},
}.Op().Push(o).Pop()
paint.Fill(o, red)
}, func(r result) {
r.expect(0, 0, transparent)
r.expect(10, 50, colornames.Red)
})
}
func TestStrokedPathRoundRound(t *testing.T) {
run(t, func(o *op.Ops) {
p := newStrokedPath(o)
defer clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 2.5,
Cap: clip.RoundCap,
Join: clip.RoundJoin,
},
}.Op().Push(o).Pop()
paint.Fill(o, red)
}, func(r result) {
r.expect(0, 0, transparent)
r.expect(10, 50, colornames.Red)
})
}
func TestStrokedPathFlatMiter(t *testing.T) {
run(t, func(o *op.Ops) {
{
p := newZigZagPath(o)
cl := clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 10,
Cap: clip.FlatCap,
Join: clip.BevelJoin,
Miter: 5,
},
}.Op().Push(o)
paint.Fill(o, red)
cl.Pop()
}
{
p := newZigZagPath(o)
cl := clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 2,
Cap: clip.FlatCap,
Join: clip.BevelJoin,
},
}.Op().Push(o)
paint.Fill(o, black)
cl.Pop()
}
}, func(r result) {
r.expect(0, 0, transparent)
r.expect(40, 10, colornames.Black)
r.expect(40, 12, colornames.Red)
})
}
func TestStrokedPathFlatMiterInf(t *testing.T) {
run(t, func(o *op.Ops) {
{
p := newZigZagPath(o)
cl := clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 10,
Cap: clip.FlatCap,
Join: clip.BevelJoin,
Miter: float32(math.Inf(+1)),
},
}.Op().Push(o)
paint.Fill(o, red)
cl.Pop()
}
{
p := newZigZagPath(o)
cl := clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 2,
Cap: clip.FlatCap,
Join: clip.BevelJoin,
},
}.Op().Push(o)
paint.Fill(o, black)
cl.Pop()
}
}, func(r result) {
r.expect(0, 0, transparent)
r.expect(40, 10, colornames.Black)
r.expect(40, 12, colornames.Red)
})
}
func TestStrokedPathZeroWidth(t *testing.T) {
run(t, func(o *op.Ops) {
{
p := new(clip.Path)
p.Begin(o)
p.Move(f32.Pt(10, 50))
p.Line(f32.Pt(50, 0))
cl := clip.Stroke{
Path: p.End(),
Style: clip.StrokeStyle{
Width: 2,
Cap: clip.FlatCap,
Join: clip.BevelJoin,
},
}.Op().Push(o)
paint.Fill(o, black)
cl.Pop()
}
{
p := new(clip.Path)
p.Begin(o)
p.Move(f32.Pt(10, 50))
p.Line(f32.Pt(30, 0))
cl := clip.Stroke{
Path: p.End(),
}.Op().Push(o) // width=0, disable stroke
paint.Fill(o, red)
cl.Pop()
}
}, func(r result) {
r.expect(0, 0, transparent)
r.expect(10, 50, colornames.Black)
r.expect(30, 50, colornames.Black)
r.expect(65, 50, transparent)
})
}
func TestDashedPathFlatCapEllipse(t *testing.T) {
run(t, func(o *op.Ops) {
{
p := newEllipsePath(o)
var dash clip.Dash
dash.Begin(o)
dash.Dash(5)
dash.Dash(3)
cl := clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 10,
Cap: clip.FlatCap,
Join: clip.BevelJoin,
Miter: float32(math.Inf(+1)),
},
Dashes: dash.End(),
}.Op().Push(o)
paint.Fill(
o,
red,
)
cl.Pop()
}
{
p := newEllipsePath(o)
cl := clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 2,
},
}.Op().Push(o)
paint.Fill(
o,
black,
)
cl.Pop()
}
}, func(r result) {
r.expect(0, 0, transparent)
r.expect(0, 62, colornames.Red)
r.expect(0, 65, colornames.Black)
})
}
func TestDashedPathFlatCapZ(t *testing.T) {
run(t, func(o *op.Ops) {
{
p := newZigZagPath(o)
var dash clip.Dash
dash.Begin(o)
dash.Dash(5)
dash.Dash(3)
cl := clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 10,
Cap: clip.FlatCap,
Join: clip.BevelJoin,
Miter: float32(math.Inf(+1)),
},
Dashes: dash.End(),
}.Op().Push(o)
paint.Fill(o, red)
cl.Pop()
}
{
p := newZigZagPath(o)
cl := clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 2,
Cap: clip.FlatCap,
Join: clip.BevelJoin,
},
}.Op().Push(o)
paint.Fill(o, black)
cl.Pop()
}
}, func(r result) {
r.expect(0, 0, transparent)
r.expect(40, 10, colornames.Black)
r.expect(40, 12, colornames.Red)
r.expect(46, 12, transparent)
})
}
func TestDashedPathFlatCapZNoDash(t *testing.T) {
run(t, func(o *op.Ops) {
{
p := newZigZagPath(o)
var dash clip.Dash
dash.Begin(o)
dash.Phase(1)
cl := clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 10,
Cap: clip.FlatCap,
Join: clip.BevelJoin,
Miter: float32(math.Inf(+1)),
},
Dashes: dash.End(),
}.Op().Push(o)
paint.Fill(o, red)
cl.Pop()
}
{
cl := clip.Stroke{
Path: newZigZagPath(o),
Style: clip.StrokeStyle{
Width: 2,
Cap: clip.FlatCap,
Join: clip.BevelJoin,
},
}.Op().Push(o)
paint.Fill(o, black)
cl.Pop()
}
}, func(r result) {
r.expect(0, 0, transparent)
r.expect(40, 10, colornames.Black)
r.expect(40, 12, colornames.Red)
r.expect(46, 12, colornames.Red)
})
}
func TestDashedPathFlatCapZNoPath(t *testing.T) {
run(t, func(o *op.Ops) {
{
var dash clip.Dash
dash.Begin(o)
dash.Dash(0)
cl := clip.Stroke{
Path: newZigZagPath(o),
Style: clip.StrokeStyle{
Width: 10,
Cap: clip.FlatCap,
Join: clip.BevelJoin,
Miter: float32(math.Inf(+1)),
},
Dashes: dash.End(),
}.Op().Push(o)
paint.Fill(o, red)
cl.Pop()
}
{
p := newZigZagPath(o)
cl := clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 2,
Cap: clip.FlatCap,
Join: clip.BevelJoin,
},
}.Op().Push(o)
paint.Fill(o, black)
cl.Pop()
}
}, func(r result) {
r.expect(0, 0, transparent)
r.expect(40, 10, colornames.Black)
r.expect(40, 12, transparent)
r.expect(46, 12, transparent)
})
}
func newStrokedPath(o *op.Ops) clip.PathSpec {
p := new(clip.Path)
p.Begin(o)
p.Move(f32.Pt(10, 50))
p.Line(f32.Pt(10, 0))
p.Arc(f32.Pt(10, 0), f32.Pt(20, 0), math.Pi)
p.Line(f32.Pt(10, 0))
p.Line(f32.Pt(10, 10))
p.Arc(f32.Pt(0, 30), f32.Pt(0, 30), 2*math.Pi)
p.Line(f32.Pt(-20, 0))
p.Quad(f32.Pt(-10, -10), f32.Pt(-30, 30))
return p.End()
}
func newZigZagPath(o *op.Ops) clip.PathSpec {
p := new(clip.Path)
p.Begin(o)
p.Move(f32.Pt(40, 10))
p.Line(f32.Pt(50, 0))
p.Line(f32.Pt(-50, 50))
p.Line(f32.Pt(50, 0))
p.Quad(f32.Pt(-50, 20), f32.Pt(-50, 50))
p.Line(f32.Pt(50, 0))
return p.End()
}
func newEllipsePath(o *op.Ops) clip.PathSpec {
p := new(clip.Path)
p.Begin(o)
p.Move(f32.Pt(0, 65))
p.Line(f32.Pt(20, 0))
p.Arc(f32.Pt(20, 0), f32.Pt(70, 0), 2*math.Pi)
return p.End()
}