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>
This commit is contained in:
Elias Naur
2021-10-01 18:52:43 +02:00
parent 6f80b94b4a
commit 936c266b03
46 changed files with 727 additions and 579 deletions
+49 -31
View File
@@ -179,15 +179,21 @@ type collector struct {
hasher maphash.Hash
profile bool
reader ops.Reader
states []encoderState
states []f32.Affine2D
clear bool
clearColor f32color.RGBA
clipStates []clipState
order []hashIndex
transStack []transEntry
prevFrame opsCollector
frame opsCollector
}
type transEntry struct {
t f32.Affine2D
relTrans f32.Affine2D
}
type hashIndex struct {
index int
hash uint64
@@ -260,6 +266,7 @@ type clipState struct {
path []byte
pathKey ops.Key
intersect f32.Rectangle
push bool
clipKey
}
@@ -1666,6 +1673,7 @@ func (c *collector) reset() {
c.prevFrame, c.frame = c.frame, c.prevFrame
c.profile = false
c.clipStates = c.clipStates[:0]
c.transStack = c.transStack[:0]
c.frame.reset()
}
@@ -1676,9 +1684,9 @@ func (c *opsCollector) reset() {
c.layers = c.layers[:0]
}
func (c *collector) addClip(state *encoderState, viewport, bounds f32.Rectangle, path []byte, key ops.Key, hash uint64, stroke clip.StrokeStyle) {
func (c *collector) addClip(state *encoderState, viewport, bounds f32.Rectangle, path []byte, key ops.Key, hash uint64, stroke clip.StrokeStyle, push bool) {
// Rectangle clip regions.
if len(path) == 0 {
if len(path) == 0 && !push {
// If the rectangular clip region contains a previous path it can be discarded.
p := state.clip
t := state.relTrans.Invert()
@@ -1704,6 +1712,7 @@ func (c *collector) addClip(state *encoderState, viewport, bounds f32.Rectangle,
path: path,
pathKey: key,
intersect: intersect,
push: push,
clipKey: clipKey{
bounds: bounds,
relTrans: state.relTrans,
@@ -1718,11 +1727,15 @@ func (c *collector) addClip(state *encoderState, viewport, bounds f32.Rectangle,
func (c *collector) collect(root *op.Ops, viewport image.Point, texOps *[]textureOp) {
fview := f32.Rectangle{Max: layout.FPt(viewport)}
c.reader.Reset(root)
state := encoderState{
paintKey: paintKey{
color: color.NRGBA{A: 0xff},
},
var state encoderState
reset := func() {
state = encoderState{
paintKey: paintKey{
color: color.NRGBA{A: 0xff},
},
}
}
reset()
r := &c.reader
var (
pathData struct {
@@ -1732,16 +1745,24 @@ func (c *collector) collect(root *op.Ops, viewport image.Point, texOps *[]textur
}
str clip.StrokeStyle
)
c.save(opconst.InitialStateID, state)
c.addClip(&state, fview, fview, nil, ops.Key{}, 0, clip.StrokeStyle{})
c.addClip(&state, fview, fview, nil, ops.Key{}, 0, clip.StrokeStyle{}, false)
for encOp, ok := r.Decode(); ok; encOp, ok = r.Decode() {
switch opconst.OpType(encOp.Data[0]) {
case opconst.TypeProfile:
c.profile = true
case opconst.TypeTransform:
dop := ops.DecodeTransform(encOp.Data)
dop, push := ops.DecodeTransform(encOp.Data)
if push {
c.transStack = append(c.transStack, transEntry{t: state.t, relTrans: state.relTrans})
}
state.t = state.t.Mul(dop)
state.relTrans = state.relTrans.Mul(dop)
case opconst.TypePopTransform:
n := len(c.transStack)
st := c.transStack[n-1]
c.transStack = c.transStack[:n-1]
state.t = st.t
state.relTrans = st.relTrans
case opconst.TypeStroke:
str = decodeStrokeOp(encOp.Data)
case opconst.TypePath:
@@ -1756,9 +1777,18 @@ func (c *collector) collect(root *op.Ops, viewport image.Point, texOps *[]textur
case opconst.TypeClip:
var op clipOp
op.decode(encOp.Data)
c.addClip(&state, fview, op.bounds, pathData.data, pathData.key, pathData.hash, str)
c.addClip(&state, fview, op.bounds, pathData.data, pathData.key, pathData.hash, str, op.push)
pathData.data = nil
str = clip.StrokeStyle{}
case opconst.TypePopClip:
for {
push := state.clip.push
state.relTrans = state.clip.relTrans.Mul(state.relTrans)
state.clip = state.clip.parent
if push {
break
}
}
case opconst.TypeColor:
state.matType = materialColor
state.color = decodeColorOp(encOp.Data)
@@ -1777,7 +1807,7 @@ func (c *collector) collect(root *op.Ops, viewport image.Point, texOps *[]textur
if paintState.matType == materialTexture {
// Clip to the bounds of the image, to hide other images in the atlas.
bounds := paintState.image.src.Bounds()
c.addClip(&paintState, fview, layout.FRect(bounds), nil, ops.Key{}, 0, clip.StrokeStyle{})
c.addClip(&paintState, fview, layout.FRect(bounds), nil, ops.Key{}, 0, clip.StrokeStyle{}, false)
}
intersect := paintState.clip.intersect
if intersect.Empty() {
@@ -1818,24 +1848,12 @@ func (c *collector) collect(root *op.Ops, viewport image.Point, texOps *[]textur
})
case opconst.TypeSave:
id := ops.DecodeSave(encOp.Data)
c.save(id, state)
c.save(id, state.t)
case opconst.TypeLoad:
id, mask := ops.DecodeLoad(encOp.Data)
s := c.states[id]
if mask&^opconst.TransformState != 0 {
state = s
} else if mask&opconst.TransformState != 0 {
state.t = s.t
state.relTrans = s.t
if cl := state.clip; cl != nil {
var relTrans f32.Affine2D
for cl != nil {
relTrans = cl.relTrans.Mul(relTrans)
cl = cl.parent
}
state.relTrans = relTrans.Invert().Mul(state.relTrans)
}
}
reset()
id := ops.DecodeLoad(encOp.Data)
state.t = c.states[id]
state.relTrans = state.t
}
}
for i := range c.frame.ops {
@@ -2132,9 +2150,9 @@ func encodeOp(viewport image.Point, absOff image.Point, enc *encoder, texOps []t
}
}
func (c *collector) save(id int, state encoderState) {
func (c *collector) save(id int, state f32.Affine2D) {
if extra := id - len(c.states) + 1; extra > 0 {
c.states = append(c.states, make([]encoderState, extra)...)
c.states = append(c.states, make([]f32.Affine2D, extra)...)
}
c.states[id] = state
}
+46 -26
View File
@@ -75,7 +75,8 @@ type renderer struct {
type drawOps struct {
profile bool
reader ops.Reader
states []drawState
states []f32.Affine2D
transStack []f32.Affine2D
cache *resourceCache
vertCache []byte
viewport image.Point
@@ -110,6 +111,9 @@ type pathOp struct {
// rect tracks whether the clip stack can be represented by a
// pixel-aligned rectangle.
rect bool
// push is set to true for clip operations that corresponds to
// a push operation.
push bool
// clip is the union of all
// later clip rectangles.
clip image.Rectangle
@@ -171,6 +175,7 @@ type clipOp struct {
// TODO: Use image.Rectangle?
bounds f32.Rectangle
outline bool
push bool
}
// imageOpData is the shadow of paint.ImageOp.
@@ -204,6 +209,7 @@ func (op *clipOp) decode(data []byte) {
*op = clipOp{
bounds: layout.FRect(r),
outline: data[17] == 1,
push: data[18] == 1,
}
}
@@ -822,6 +828,7 @@ func (d *drawOps) reset(cache *resourceCache, viewport image.Point) {
d.pathOps = d.pathOps[:0]
d.pathOpCache = d.pathOpCache[:0]
d.vertCache = d.vertCache[:0]
d.transStack = d.transStack[:0]
}
func (d *drawOps) collect(root *op.Ops, viewport image.Point) {
@@ -829,10 +836,7 @@ func (d *drawOps) collect(root *op.Ops, viewport image.Point) {
Max: f32.Point{X: float32(viewport.X), Y: float32(viewport.Y)},
}
d.reader.Reset(root)
state := drawState{
color: color.NRGBA{A: 0xff},
}
d.collectOps(&d.reader, viewf, state)
d.collectOps(&d.reader, viewf)
}
func (d *drawOps) buildPaths(ctx driver.Device) {
@@ -853,7 +857,7 @@ func (d *drawOps) newPathOp() *pathOp {
return &d.pathOpCache[len(d.pathOpCache)-1]
}
func (d *drawOps) addClipPath(state *drawState, aux []byte, auxKey opKey, bounds f32.Rectangle, off f32.Point) {
func (d *drawOps) addClipPath(state *drawState, aux []byte, auxKey opKey, bounds f32.Rectangle, off f32.Point, push bool) {
npath := d.newPathOp()
*npath = pathOp{
parent: state.cpath,
@@ -861,6 +865,7 @@ func (d *drawOps) addClipPath(state *drawState, aux []byte, auxKey opKey, bounds
off: off,
intersect: bounds.Add(off),
rect: true,
push: push,
}
if npath.parent != nil {
npath.rect = npath.parent.rect
@@ -885,9 +890,9 @@ func splitTransform(t f32.Affine2D) (srs f32.Affine2D, offset f32.Point) {
return
}
func (d *drawOps) save(id int, state drawState) {
func (d *drawOps) save(id int, state f32.Affine2D) {
if extra := id - len(d.states) + 1; extra > 0 {
d.states = append(d.states, make([]drawState, extra)...)
d.states = append(d.states, make([]f32.Affine2D, extra)...)
}
d.states[id] = state
}
@@ -901,20 +906,33 @@ func (k opKey) SetTransform(t f32.Affine2D) opKey {
return k
}
func (d *drawOps) collectOps(r *ops.Reader, viewport f32.Rectangle, state drawState) {
func (d *drawOps) collectOps(r *ops.Reader, viewport f32.Rectangle) {
var (
quads quadsOp
str clip.StrokeStyle
state drawState
)
d.save(opconst.InitialStateID, state)
reset := func() {
state = drawState{
color: color.NRGBA{A: 0xff},
}
}
reset()
loop:
for encOp, ok := r.Decode(); ok; encOp, ok = r.Decode() {
switch opconst.OpType(encOp.Data[0]) {
case opconst.TypeProfile:
d.profile = true
case opconst.TypeTransform:
dop := ops.DecodeTransform(encOp.Data)
dop, push := ops.DecodeTransform(encOp.Data)
if push {
d.transStack = append(d.transStack, state.t)
}
state.t = state.t.Mul(dop)
case opconst.TypePopTransform:
n := len(d.transStack)
state.t = d.transStack[n-1]
d.transStack = d.transStack[:n-1]
case opconst.TypeStroke:
str = decodeStrokeOp(encOp.Data)
@@ -954,11 +972,18 @@ loop:
} else {
quads.aux, op.bounds, _ = d.boundsForTransformedRect(bounds, trans)
quads.key = opKey{Key: encOp.Key}
quads.key.SetTransform(trans) // TODO: This call has no effect.
}
d.addClipPath(&state, quads.aux, quads.key, op.bounds, off)
d.addClipPath(&state, quads.aux, quads.key, op.bounds, off, op.push)
quads = quadsOp{}
str = clip.StrokeStyle{}
case opconst.TypePopClip:
for {
push := state.cpath.push
state.cpath = state.cpath.parent
if push {
break
}
}
case opconst.TypeColor:
state.matType = materialColor
@@ -977,7 +1002,7 @@ loop:
// Transform (if needed) the painting rectangle and if so generate a clip path,
// for those cases also compute a partialTrans that maps texture coordinates between
// the new bounding rectangle and the transformed original paint rectangle.
trans, off := splitTransform(state.t)
t, off := splitTransform(state.t)
// Fill the clip area, unless the material is a (bounded) image.
// TODO: Find a tighter bound.
inf := float32(1e6)
@@ -985,7 +1010,7 @@ loop:
if state.matType == materialTexture {
dst = layout.FRect(state.image.src.Rect)
}
clipData, bnd, partialTrans := d.boundsForTransformedRect(dst, trans)
clipData, bnd, partialTrans := d.boundsForTransformedRect(dst, t)
cl := viewport.Intersect(bnd.Add(off))
if state.cpath != nil {
cl = state.cpath.intersect.Intersect(cl)
@@ -998,8 +1023,8 @@ loop:
// The paint operation is sheared or rotated, add a clip path representing
// this transformed rectangle.
k := opKey{Key: encOp.Key}
k.SetTransform(trans) // TODO: This call has no effect.
d.addClipPath(&state, clipData, k, bnd, off)
k.SetTransform(t) // TODO: This call has no effect.
d.addClipPath(&state, clipData, k, bnd, off, false)
}
bounds := boundRectF(cl)
@@ -1027,16 +1052,11 @@ loop:
}
case opconst.TypeSave:
id := ops.DecodeSave(encOp.Data)
d.save(id, state)
d.save(id, state.t)
case opconst.TypeLoad:
id, mask := ops.DecodeLoad(encOp.Data)
s := d.states[id]
if mask&opconst.TransformState != 0 {
state.t = s.t
}
if mask&^opconst.TransformState != 0 {
state = s
}
reset()
id := ops.DecodeLoad(encOp.Data)
state.t = d.states[id]
}
}
}
+2 -2
View File
@@ -54,7 +54,7 @@ func TestClipping(t *testing.T) {
Max: f32.Point{X: 250, Y: 250},
},
SE: 75,
}.Add(&ops)
}.Push(&ops)
paint.PaintOp{}.Add(&ops)
paint.ColorOp{Color: col2}.Add(&ops)
clip.RRect{
@@ -63,7 +63,7 @@ func TestClipping(t *testing.T) {
Max: f32.Point{X: 350, Y: 350},
},
NW: 75,
}.Add(&ops)
}.Push(&ops)
paint.PaintOp{}.Add(&ops)
if err := w.Frame(&ops); err != nil {
t.Fatal(err)
+12 -22
View File
@@ -84,13 +84,12 @@ func BenchmarkDrawUI(b *testing.B) {
for i := 0; i < b.N; i++ {
resetOps(gtx)
p := op.Save(gtx.Ops)
off := float32(math.Mod(float64(i)/10, 10))
op.Offset(f32.Pt(off, off)).Add(gtx.Ops)
t := op.Offset(f32.Pt(off, off)).Push(gtx.Ops)
drawCore(gtx, th)
p.Load()
t.Pop()
w.Frame(gtx.Ops)
}
finishBenchmark(b, w)
@@ -107,14 +106,13 @@ func BenchmarkDrawUITransformed(b *testing.B) {
for i := 0; i < b.N; i++ {
resetOps(gtx)
p := op.Save(gtx.Ops)
angle := float32(math.Mod(float64(i)/1000, 0.05))
a := f32.Affine2D{}.Shear(f32.Point{}, angle, angle).Rotate(f32.Point{}, angle)
op.Affine(a).Add(gtx.Ops)
t := op.Affine(a).Push(gtx.Ops)
drawCore(gtx, th)
p.Load()
t.Pop()
w.Frame(gtx.Ops)
}
finishBenchmark(b, w)
@@ -158,7 +156,6 @@ func Benchmark1000CirclesInstanced(b *testing.B) {
func draw1000Circles(gtx layout.Context) {
ops := gtx.Ops
for x := 0; x < 100; x++ {
p := op.Save(ops)
op.Offset(f32.Pt(float32(x*10), 0)).Add(ops)
for y := 0; y < 10; y++ {
paint.FillShape(ops,
@@ -167,7 +164,6 @@ func draw1000Circles(gtx layout.Context) {
)
op.Offset(f32.Pt(0, float32(100))).Add(ops)
}
p.Load()
}
}
@@ -175,21 +171,18 @@ func draw1000CirclesInstanced(gtx layout.Context) {
ops := gtx.Ops
r := op.Record(ops)
clip.RRect{Rect: f32.Rect(0, 0, 10, 10), NE: 5, SE: 5, SW: 5, NW: 5}.Add(ops)
cl := clip.RRect{Rect: f32.Rect(0, 0, 10, 10), NE: 5, SE: 5, SW: 5, NW: 5}.Push(ops)
paint.PaintOp{}.Add(ops)
cl.Pop()
c := r.Stop()
for x := 0; x < 100; x++ {
p := op.Save(ops)
op.Offset(f32.Pt(float32(x*10), 0)).Add(ops)
for y := 0; y < 10; y++ {
pi := op.Save(ops)
paint.ColorOp{Color: color.NRGBA{R: 100 + uint8(x), G: 100 + uint8(y), B: 100, A: 120}}.Add(ops)
c.Add(ops)
pi.Load()
op.Offset(f32.Pt(0, float32(100))).Add(ops)
}
p.Load()
}
}
@@ -209,7 +202,6 @@ func drawIndividualShapes(gtx layout.Context, th *material.Theme) chan op.CallOp
ops := &op1
c := op.Record(ops)
for x := 0; x < 9; x++ {
p := op.Save(ops)
op.Offset(f32.Pt(float32(x*50), 0)).Add(ops)
for y := 0; y < 9; y++ {
paint.FillShape(ops,
@@ -218,7 +210,6 @@ func drawIndividualShapes(gtx layout.Context, th *material.Theme) chan op.CallOp
)
op.Offset(f32.Pt(0, float32(50))).Add(ops)
}
p.Load()
}
c1 <- c.Stop()
}()
@@ -232,18 +223,18 @@ func drawShapeInstances(gtx layout.Context, th *material.Theme) chan op.CallOp {
co := op.Record(ops)
r := op.Record(ops)
clip.RRect{Rect: f32.Rect(0, 0, 25, 25), NE: 10, SE: 10, SW: 10, NW: 10}.Add(ops)
cl := clip.RRect{Rect: f32.Rect(0, 0, 25, 25), NE: 10, SE: 10, SW: 10, NW: 10}.Push(ops)
paint.PaintOp{}.Add(ops)
cl.Pop()
c := r.Stop()
squares.Add(ops)
rad := float32(0)
for x := 0; x < 20; x++ {
for y := 0; y < 20; y++ {
p := op.Save(ops)
op.Offset(f32.Pt(float32(x*50+25), float32(y*50+25))).Add(ops)
t := op.Offset(f32.Pt(float32(x*50+25), float32(y*50+25))).Push(ops)
c.Add(ops)
p.Load()
t.Pop()
rad += math.Pi * 2 / 400
}
}
@@ -261,11 +252,10 @@ func drawText(gtx layout.Context, th *material.Theme) chan op.CallOp {
txt := material.H6(th, "")
for x := 0; x < 40; x++ {
txt.Text = textRows[x]
p := op.Save(ops)
op.Offset(f32.Pt(float32(0), float32(24*x))).Add(ops)
t := op.Offset(f32.Pt(float32(0), float32(24*x))).Push(ops)
gtx.Ops = ops
txt.Layout(gtx)
p.Load()
t.Pop()
}
c3 <- c.Stop()
}()
+69 -84
View File
@@ -28,7 +28,7 @@ func TestPaintRect(t *testing.T) {
func TestPaintClippedRect(t *testing.T) {
run(t, func(o *op.Ops) {
clip.RRect{Rect: f32.Rect(25, 25, 60, 60)}.Add(o)
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)
@@ -42,8 +42,8 @@ func TestPaintClippedRect(t *testing.T) {
func TestPaintClippedCircle(t *testing.T) {
run(t, func(o *op.Ops) {
r := float32(10)
clip.RRect{Rect: f32.Rect(20, 20, 40, 40), SE: r, SW: r, NW: r, NE: r}.Add(o)
clip.Rect(image.Rect(0, 0, 30, 50)).Add(o)
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)
@@ -72,9 +72,9 @@ func TestPaintArc(t *testing.T) {
p.Line(f32.Pt(0, -10))
p.Line(f32.Pt(-50, 0))
p.Close()
clip.Outline{
defer clip.Outline{
Path: p.End(),
}.Op().Add(o)
}.Op().Push(o).Pop()
paint.FillShape(o, red, clip.Rect(image.Rect(0, 0, 128, 128)).Op())
}, func(r result) {
@@ -94,9 +94,9 @@ func TestPaintAbsolute(t *testing.T) {
p.LineTo(f32.Pt(80, 20))
p.QuadTo(f32.Pt(80, 80), f32.Pt(20, 80))
p.Close()
clip.Outline{
defer clip.Outline{
Path: p.End(),
}.Op().Add(o)
}.Op().Push(o).Pop()
paint.FillShape(o, red, clip.Rect(image.Rect(0, 0, 128, 128)).Op())
}, func(r result) {
@@ -110,7 +110,7 @@ func TestPaintAbsolute(t *testing.T) {
func TestPaintTexture(t *testing.T) {
run(t, func(o *op.Ops) {
squares.Add(o)
scale(80.0/512, 80.0/512).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)
@@ -123,15 +123,15 @@ func TestPaintTexture(t *testing.T) {
func TestTexturedStrokeClipped(t *testing.T) {
run(t, func(o *op.Ops) {
smallSquares.Add(o)
op.Offset(f32.Pt(50, 50)).Add(o)
clip.Stroke{
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().Add(o)
clip.RRect{Rect: f32.Rect(-30, -30, 60, 60)}.Add(o)
op.Offset(f32.Pt(-10, -10)).Add(o)
}.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) {
})
@@ -140,14 +140,14 @@ func TestTexturedStrokeClipped(t *testing.T) {
func TestTexturedStroke(t *testing.T) {
run(t, func(o *op.Ops) {
smallSquares.Add(o)
op.Offset(f32.Pt(50, 50)).Add(o)
clip.Stroke{
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().Add(o)
op.Offset(f32.Pt(-10, -10)).Add(o)
}.Op().Push(o).Pop()
defer op.Offset(f32.Pt(-10, -10)).Push(o).Pop()
paint.PaintOp{}.Add(o)
}, func(r result) {
})
@@ -156,8 +156,8 @@ func TestTexturedStroke(t *testing.T) {
func TestPaintClippedTexture(t *testing.T) {
run(t, func(o *op.Ops) {
squares.Add(o)
clip.RRect{Rect: f32.Rect(0, 0, 40, 40)}.Add(o)
scale(80.0/512, 80.0/512).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)
@@ -168,14 +168,14 @@ func TestPaintClippedTexture(t *testing.T) {
func TestStrokedPathBevelFlat(t *testing.T) {
run(t, func(o *op.Ops) {
p := newStrokedPath(o)
clip.Stroke{
defer clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 2.5,
Cap: clip.FlatCap,
Join: clip.BevelJoin,
},
}.Op().Add(o)
}.Op().Push(o).Pop()
paint.Fill(o, red)
}, func(r result) {
@@ -187,14 +187,14 @@ func TestStrokedPathBevelFlat(t *testing.T) {
func TestStrokedPathBevelRound(t *testing.T) {
run(t, func(o *op.Ops) {
p := newStrokedPath(o)
clip.Stroke{
defer clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 2.5,
Cap: clip.RoundCap,
Join: clip.BevelJoin,
},
}.Op().Add(o)
}.Op().Push(o).Pop()
paint.Fill(o, red)
}, func(r result) {
@@ -206,14 +206,14 @@ func TestStrokedPathBevelRound(t *testing.T) {
func TestStrokedPathBevelSquare(t *testing.T) {
run(t, func(o *op.Ops) {
p := newStrokedPath(o)
clip.Stroke{
defer clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 2.5,
Cap: clip.SquareCap,
Join: clip.BevelJoin,
},
}.Op().Add(o)
}.Op().Push(o).Pop()
paint.Fill(o, red)
}, func(r result) {
@@ -225,14 +225,14 @@ func TestStrokedPathBevelSquare(t *testing.T) {
func TestStrokedPathRoundRound(t *testing.T) {
run(t, func(o *op.Ops) {
p := newStrokedPath(o)
clip.Stroke{
defer clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 2.5,
Cap: clip.RoundCap,
Join: clip.RoundJoin,
},
}.Op().Add(o)
}.Op().Push(o).Pop()
paint.Fill(o, red)
}, func(r result) {
@@ -244,9 +244,8 @@ func TestStrokedPathRoundRound(t *testing.T) {
func TestStrokedPathFlatMiter(t *testing.T) {
run(t, func(o *op.Ops) {
{
stk := op.Save(o)
p := newZigZagPath(o)
clip.Stroke{
cl := clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 10,
@@ -254,23 +253,22 @@ func TestStrokedPathFlatMiter(t *testing.T) {
Join: clip.BevelJoin,
Miter: 5,
},
}.Op().Add(o)
}.Op().Push(o)
paint.Fill(o, red)
stk.Load()
cl.Pop()
}
{
stk := op.Save(o)
p := newZigZagPath(o)
clip.Stroke{
cl := clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 2,
Cap: clip.FlatCap,
Join: clip.BevelJoin,
},
}.Op().Add(o)
}.Op().Push(o)
paint.Fill(o, black)
stk.Load()
cl.Pop()
}
}, func(r result) {
@@ -283,9 +281,8 @@ func TestStrokedPathFlatMiter(t *testing.T) {
func TestStrokedPathFlatMiterInf(t *testing.T) {
run(t, func(o *op.Ops) {
{
stk := op.Save(o)
p := newZigZagPath(o)
clip.Stroke{
cl := clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 10,
@@ -293,25 +290,23 @@ func TestStrokedPathFlatMiterInf(t *testing.T) {
Join: clip.BevelJoin,
Miter: float32(math.Inf(+1)),
},
}.Op().Add(o)
}.Op().Push(o)
paint.Fill(o, red)
stk.Load()
cl.Pop()
}
{
stk := op.Save(o)
p := newZigZagPath(o)
clip.Stroke{
cl := clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 2,
Cap: clip.FlatCap,
Join: clip.BevelJoin,
},
}.Op().Add(o)
}.Op().Push(o)
paint.Fill(o, black)
stk.Load()
cl.Pop()
}
}, func(r result) {
r.expect(0, 0, transparent)
r.expect(40, 10, colornames.Black)
@@ -322,36 +317,34 @@ func TestStrokedPathFlatMiterInf(t *testing.T) {
func TestStrokedPathZeroWidth(t *testing.T) {
run(t, func(o *op.Ops) {
{
stk := op.Save(o)
p := new(clip.Path)
p.Begin(o)
p.Move(f32.Pt(10, 50))
p.Line(f32.Pt(50, 0))
clip.Stroke{
cl := clip.Stroke{
Path: p.End(),
Style: clip.StrokeStyle{
Width: 2,
Cap: clip.FlatCap,
Join: clip.BevelJoin,
},
}.Op().Add(o)
}.Op().Push(o)
paint.Fill(o, black)
stk.Load()
cl.Pop()
}
{
stk := op.Save(o)
p := new(clip.Path)
p.Begin(o)
p.Move(f32.Pt(10, 50))
p.Line(f32.Pt(30, 0))
clip.Stroke{
cl := clip.Stroke{
Path: p.End(),
}.Op().Add(o) // width=0, disable stroke
}.Op().Push(o) // width=0, disable stroke
paint.Fill(o, red)
stk.Load()
cl.Pop()
}
}, func(r result) {
@@ -365,7 +358,6 @@ func TestStrokedPathZeroWidth(t *testing.T) {
func TestDashedPathFlatCapEllipse(t *testing.T) {
run(t, func(o *op.Ops) {
{
stk := op.Save(o)
p := newEllipsePath(o)
var dash clip.Dash
@@ -373,7 +365,7 @@ func TestDashedPathFlatCapEllipse(t *testing.T) {
dash.Dash(5)
dash.Dash(3)
clip.Stroke{
cl := clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 10,
@@ -382,29 +374,28 @@ func TestDashedPathFlatCapEllipse(t *testing.T) {
Miter: float32(math.Inf(+1)),
},
Dashes: dash.End(),
}.Op().Add(o)
}.Op().Push(o)
paint.Fill(
o,
red,
)
stk.Load()
cl.Pop()
}
{
stk := op.Save(o)
p := newEllipsePath(o)
clip.Stroke{
cl := clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 2,
},
}.Op().Add(o)
}.Op().Push(o)
paint.Fill(
o,
black,
)
stk.Load()
cl.Pop()
}
}, func(r result) {
@@ -417,14 +408,13 @@ func TestDashedPathFlatCapEllipse(t *testing.T) {
func TestDashedPathFlatCapZ(t *testing.T) {
run(t, func(o *op.Ops) {
{
stk := op.Save(o)
p := newZigZagPath(o)
var dash clip.Dash
dash.Begin(o)
dash.Dash(5)
dash.Dash(3)
clip.Stroke{
cl := clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 10,
@@ -433,24 +423,23 @@ func TestDashedPathFlatCapZ(t *testing.T) {
Miter: float32(math.Inf(+1)),
},
Dashes: dash.End(),
}.Op().Add(o)
}.Op().Push(o)
paint.Fill(o, red)
stk.Load()
cl.Pop()
}
{
stk := op.Save(o)
p := newZigZagPath(o)
clip.Stroke{
cl := clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 2,
Cap: clip.FlatCap,
Join: clip.BevelJoin,
},
}.Op().Add(o)
}.Op().Push(o)
paint.Fill(o, black)
stk.Load()
cl.Pop()
}
}, func(r result) {
r.expect(0, 0, transparent)
@@ -463,13 +452,12 @@ func TestDashedPathFlatCapZ(t *testing.T) {
func TestDashedPathFlatCapZNoDash(t *testing.T) {
run(t, func(o *op.Ops) {
{
stk := op.Save(o)
p := newZigZagPath(o)
var dash clip.Dash
dash.Begin(o)
dash.Phase(1)
clip.Stroke{
cl := clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 10,
@@ -478,22 +466,21 @@ func TestDashedPathFlatCapZNoDash(t *testing.T) {
Miter: float32(math.Inf(+1)),
},
Dashes: dash.End(),
}.Op().Add(o)
}.Op().Push(o)
paint.Fill(o, red)
stk.Load()
cl.Pop()
}
{
stk := op.Save(o)
clip.Stroke{
cl := clip.Stroke{
Path: newZigZagPath(o),
Style: clip.StrokeStyle{
Width: 2,
Cap: clip.FlatCap,
Join: clip.BevelJoin,
},
}.Op().Add(o)
}.Op().Push(o)
paint.Fill(o, black)
stk.Load()
cl.Pop()
}
}, func(r result) {
r.expect(0, 0, transparent)
@@ -506,11 +493,10 @@ func TestDashedPathFlatCapZNoDash(t *testing.T) {
func TestDashedPathFlatCapZNoPath(t *testing.T) {
run(t, func(o *op.Ops) {
{
stk := op.Save(o)
var dash clip.Dash
dash.Begin(o)
dash.Dash(0)
clip.Stroke{
cl := clip.Stroke{
Path: newZigZagPath(o),
Style: clip.StrokeStyle{
Width: 10,
@@ -519,23 +505,22 @@ func TestDashedPathFlatCapZNoPath(t *testing.T) {
Miter: float32(math.Inf(+1)),
},
Dashes: dash.End(),
}.Op().Add(o)
}.Op().Push(o)
paint.Fill(o, red)
stk.Load()
cl.Pop()
}
{
stk := op.Save(o)
p := newZigZagPath(o)
clip.Stroke{
cl := clip.Stroke{
Path: p,
Style: clip.StrokeStyle{
Width: 2,
Cap: clip.FlatCap,
Join: clip.BevelJoin,
},
}.Op().Add(o)
}.Op().Push(o)
paint.Fill(o, black)
stk.Load()
cl.Pop()
}
}, func(r result) {
r.expect(0, 0, transparent)
+47 -65
View File
@@ -35,26 +35,24 @@ func TestTransformMacro(t *testing.T) {
m2 := op.Record(o)
paint.ColorOp{Color: red}.Add(o)
// Simulate a draw text call
stack := op.Save(o)
op.Offset(f32.Pt(0, 10)).Add(o)
t := op.Offset(f32.Pt(0, 10)).Push(o)
// Apply the clip-path.
c.Add(o)
cl := c.Push(o)
paint.PaintOp{}.Add(o)
stack.Load()
cl.Pop()
t.Pop()
c2 := m2.Stop()
// Call each of them in a transform
s1 := op.Save(o)
op.Offset(f32.Pt(0, 0)).Add(o)
t = op.Offset(f32.Pt(0, 0)).Push(o)
c1.Add(o)
s1.Load()
s2 := op.Save(o)
op.Offset(f32.Pt(0, 0)).Add(o)
t.Pop()
t = op.Offset(f32.Pt(0, 0)).Push(o)
c2.Add(o)
s2.Load()
t.Pop()
}, func(r result) {
r.expect(5, 15, colornames.Red)
r.expect(15, 15, colornames.Black)
@@ -75,9 +73,9 @@ func TestRepeatedPaintsZ(t *testing.T) {
builder.Line(f32.Pt(-10, 0))
builder.Line(f32.Pt(0, -10))
p := builder.End()
clip.Outline{
defer clip.Outline{
Path: p,
}.Op().Add(o)
}.Op().Push(o).Pop()
paint.Fill(o, red)
}, func(r result) {
r.expect(5, 5, colornames.Red)
@@ -91,10 +89,10 @@ func TestNoClipFromPaint(t *testing.T) {
// by leaving any clip paths in place.
run(t, func(o *op.Ops) {
a := f32.Affine2D{}.Rotate(f32.Pt(20, 20), math.Pi/4)
op.Affine(a).Add(o)
defer op.Affine(a).Push(o).Pop()
paint.FillShape(o, red, clip.Rect(image.Rect(10, 10, 30, 30)).Op())
a = f32.Affine2D{}.Rotate(f32.Pt(20, 20), -math.Pi/4)
op.Affine(a).Add(o)
defer op.Affine(a).Push(o).Pop()
paint.FillShape(o, black, clip.Rect(image.Rect(0, 0, 50, 50)).Op())
}, func(r result) {
@@ -107,31 +105,31 @@ 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)
cl := clip.Rect(image.Rect(0, 0, 80, 80)).Op().Push(o)
paint.ColorOp{Color: color.NRGBA{A: 0x60, G: 0xff}}.Add(o)
paint.PaintOp{}.Add(o)
cl.Pop()
op.Affine(f32.Affine2D{}.Offset(f32.Pt(20, 20))).Add(o)
t := op.Affine(f32.Affine2D{}.Offset(f32.Pt(20, 20))).Push(o)
m := op.Record(o)
clip.Rect(image.Rect(0, 0, 80, 80)).Op().Add(o)
cl2 := clip.Rect(image.Rect(0, 0, 80, 80)).Op().Push(o)
paint.ColorOp{Color: color.NRGBA{A: 0x60, R: 0xff, G: 0xff}}.Add(o)
paint.PaintOp{}.Add(o)
cl2.Pop()
paintMacro := m.Stop()
op.Defer(o, paintMacro)
t.Pop()
state.Load()
op.Affine(f32.Affine2D{}.Offset(f32.Pt(10, 10))).Add(o)
clip.Rect(image.Rect(0, 0, 80, 80)).Op().Add(o)
defer op.Affine(f32.Affine2D{}.Offset(f32.Pt(10, 10))).Push(o).Pop()
defer clip.Rect(image.Rect(0, 0, 80, 80)).Op().Push(o).Pop()
paint.ColorOp{Color: color.NRGBA{A: 0x60, B: 0xff}}.Add(o)
paint.PaintOp{}.Add(o)
}, func(r result) {
})
}
func constSqPath() op.CallOp {
func constSqPath() clip.Op {
innerOps := new(op.Ops)
m := op.Record(innerOps)
builder := clip.Path{}
builder.Begin(innerOps)
builder.Move(f32.Pt(0, 0))
@@ -140,22 +138,20 @@ func constSqPath() op.CallOp {
builder.Line(f32.Pt(-10, 0))
builder.Line(f32.Pt(0, -10))
p := builder.End()
clip.Outline{Path: p}.Op().Add(innerOps)
return m.Stop()
return clip.Outline{Path: p}.Op()
}
func constSqCirc() op.CallOp {
func constSqCirc() clip.Op {
innerOps := new(op.Ops)
m := op.Record(innerOps)
clip.RRect{Rect: f32.Rect(0, 0, 40, 40),
NW: 20, NE: 20, SW: 20, SE: 20}.Add(innerOps)
return m.Stop()
return clip.RRect{Rect: f32.Rect(0, 0, 40, 40),
NW: 20, NE: 20, SW: 20, SE: 20}.Op(innerOps)
}
func drawChild(ops *op.Ops, text op.CallOp) op.CallOp {
func drawChild(ops *op.Ops, text clip.Op) op.CallOp {
r1 := op.Record(ops)
text.Add(ops)
cl := text.Push(ops)
paint.PaintOp{}.Add(ops)
cl.Pop()
return r1.Stop()
}
@@ -166,14 +162,10 @@ func TestReuseStencil(t *testing.T) {
c2 := drawChild(ops, txt)
// lay out the children
stack1 := op.Save(ops)
c1.Add(ops)
stack1.Load()
stack2 := op.Save(ops)
op.Offset(f32.Pt(0, 50)).Add(ops)
defer op.Offset(f32.Pt(0, 50)).Push(ops).Pop()
c2.Add(ops)
stack2.Load()
}, func(r result) {
r.expect(5, 5, colornames.Black)
r.expect(5, 55, colornames.Black)
@@ -187,11 +179,9 @@ func TestBuildOffscreen(t *testing.T) {
txt := constSqCirc()
draw := func(off float32, o *op.Ops) {
s := op.Save(o)
op.Offset(f32.Pt(0, off)).Add(o)
txt.Add(o)
defer op.Offset(f32.Pt(0, off)).Push(o).Pop()
defer txt.Push(o).Pop()
paint.PaintOp{}.Add(o)
s.Load()
}
multiRun(t,
@@ -214,8 +204,8 @@ func TestBuildOffscreen(t *testing.T) {
func TestNegativeOverlaps(t *testing.T) {
run(t, func(ops *op.Ops) {
clip.RRect{Rect: f32.Rect(50, 50, 100, 100)}.Add(ops)
clip.Rect(image.Rect(0, 120, 100, 122)).Add(ops)
defer clip.RRect{Rect: f32.Rect(50, 50, 100, 100)}.Push(ops).Pop()
clip.Rect(image.Rect(0, 120, 100, 122)).Push(ops).Pop()
paint.PaintOp{}.Add(ops)
}, func(r result) {
r.expect(60, 60, transparent)
@@ -227,13 +217,8 @@ func TestNegativeOverlaps(t *testing.T) {
func TestDepthOverlap(t *testing.T) {
run(t, func(ops *op.Ops) {
stack := op.Save(ops)
paint.FillShape(ops, red, clip.Rect{Max: image.Pt(128, 64)}.Op())
stack.Load()
stack = op.Save(ops)
paint.FillShape(ops, green, clip.Rect{Max: image.Pt(64, 128)}.Op())
stack.Load()
}, func(r result) {
r.expect(96, 32, colornames.Red)
r.expect(32, 96, colornames.Green)
@@ -272,12 +257,13 @@ func TestLinearGradient(t *testing.T) {
Stop2: f32.Pt(gr.Max.X, gr.Min.Y),
Color2: g.To,
}.Add(ops)
st := op.Save(ops)
clip.RRect{Rect: gr}.Add(ops)
op.Affine(f32.Affine2D{}.Offset(pixelAligned.Min)).Add(ops)
scale(pixelAligned.Dx()/128, 1).Add(ops)
cl := clip.RRect{Rect: gr}.Push(ops)
t1 := op.Affine(f32.Affine2D{}.Offset(pixelAligned.Min)).Push(ops)
t2 := scale(pixelAligned.Dx()/128, 1).Push(ops)
paint.PaintOp{}.Add(ops)
st.Load()
t2.Pop()
t1.Pop()
cl.Pop()
gr = gr.Add(f32.Pt(0, gradienth))
}
}, func(r result) {
@@ -302,10 +288,9 @@ func TestLinearGradientAngled(t *testing.T) {
Stop2: f32.Pt(0, 0),
Color2: red,
}.Add(ops)
st := op.Save(ops)
clip.Rect(image.Rect(0, 0, 64, 64)).Add(ops)
cl := clip.Rect(image.Rect(0, 0, 64, 64)).Push(ops)
paint.PaintOp{}.Add(ops)
st.Load()
cl.Pop()
paint.LinearGradientOp{
Stop1: f32.Pt(64, 64),
@@ -313,10 +298,9 @@ func TestLinearGradientAngled(t *testing.T) {
Stop2: f32.Pt(128, 0),
Color2: green,
}.Add(ops)
st = op.Save(ops)
clip.Rect(image.Rect(64, 0, 128, 64)).Add(ops)
cl = clip.Rect(image.Rect(64, 0, 128, 64)).Push(ops)
paint.PaintOp{}.Add(ops)
st.Load()
cl.Pop()
paint.LinearGradientOp{
Stop1: f32.Pt(64, 64),
@@ -324,10 +308,9 @@ func TestLinearGradientAngled(t *testing.T) {
Stop2: f32.Pt(128, 128),
Color2: blue,
}.Add(ops)
st = op.Save(ops)
clip.Rect(image.Rect(64, 64, 128, 128)).Add(ops)
cl = clip.Rect(image.Rect(64, 64, 128, 128)).Push(ops)
paint.PaintOp{}.Add(ops)
st.Load()
cl.Pop()
paint.LinearGradientOp{
Stop1: f32.Pt(64, 64),
@@ -335,10 +318,9 @@ func TestLinearGradientAngled(t *testing.T) {
Stop2: f32.Pt(0, 128),
Color2: magenta,
}.Add(ops)
st = op.Save(ops)
clip.Rect(image.Rect(0, 64, 64, 128)).Add(ops)
cl = clip.Rect(image.Rect(0, 64, 64, 128)).Push(ops)
paint.PaintOp{}.Add(ops)
st.Load()
cl.Pop()
}, func(r result) {})
}
+29 -30
View File
@@ -17,7 +17,7 @@ import (
func TestPaintOffset(t *testing.T) {
run(t, func(o *op.Ops) {
op.Offset(f32.Pt(10, 20)).Add(o)
defer op.Offset(f32.Pt(10, 20)).Push(o).Pop()
paint.FillShape(o, red, clip.Rect(image.Rect(0, 0, 50, 50)).Op())
}, func(r result) {
r.expect(0, 0, transparent)
@@ -30,7 +30,7 @@ func TestPaintOffset(t *testing.T) {
func TestPaintRotate(t *testing.T) {
run(t, func(o *op.Ops) {
a := f32.Affine2D{}.Rotate(f32.Pt(40, 40), -math.Pi/8)
op.Affine(a).Add(o)
defer op.Affine(a).Push(o).Pop()
paint.FillShape(o, red, clip.Rect(image.Rect(20, 20, 60, 60)).Op())
}, func(r result) {
r.expect(40, 40, colornames.Red)
@@ -43,7 +43,7 @@ func TestPaintRotate(t *testing.T) {
func TestPaintShear(t *testing.T) {
run(t, func(o *op.Ops) {
a := f32.Affine2D{}.Shear(f32.Point{}, math.Pi/4, 0)
op.Affine(a).Add(o)
defer op.Affine(a).Push(o).Pop()
paint.FillShape(o, red, clip.Rect(image.Rect(0, 0, 40, 40)).Op())
}, func(r result) {
r.expect(10, 30, transparent)
@@ -52,8 +52,8 @@ func TestPaintShear(t *testing.T) {
func TestClipPaintOffset(t *testing.T) {
run(t, func(o *op.Ops) {
clip.RRect{Rect: f32.Rect(10, 10, 30, 30)}.Add(o)
op.Offset(f32.Pt(20, 20)).Add(o)
defer clip.RRect{Rect: f32.Rect(10, 10, 30, 30)}.Push(o).Pop()
defer op.Offset(f32.Pt(20, 20)).Push(o).Pop()
paint.FillShape(o, red, clip.Rect(image.Rect(0, 0, 100, 100)).Op())
}, func(r result) {
r.expect(0, 0, transparent)
@@ -65,8 +65,8 @@ func TestClipPaintOffset(t *testing.T) {
func TestClipOffset(t *testing.T) {
run(t, func(o *op.Ops) {
op.Offset(f32.Pt(20, 20)).Add(o)
clip.RRect{Rect: f32.Rect(10, 10, 30, 30)}.Add(o)
defer op.Offset(f32.Pt(20, 20)).Push(o).Pop()
defer clip.RRect{Rect: f32.Rect(10, 10, 30, 30)}.Push(o).Pop()
paint.FillShape(o, red, clip.Rect(image.Rect(0, 0, 100, 100)).Op())
}, func(r result) {
r.expect(0, 0, transparent)
@@ -80,8 +80,8 @@ func TestClipOffset(t *testing.T) {
func TestClipScale(t *testing.T) {
run(t, func(o *op.Ops) {
a := f32.Affine2D{}.Scale(f32.Point{}, f32.Pt(2, 2)).Offset(f32.Pt(10, 10))
op.Affine(a).Add(o)
clip.RRect{Rect: f32.Rect(10, 10, 20, 20)}.Add(o)
defer op.Affine(a).Push(o).Pop()
defer clip.RRect{Rect: f32.Rect(10, 10, 20, 20)}.Push(o).Pop()
paint.FillShape(o, red, clip.Rect(image.Rect(0, 0, 1000, 1000)).Op())
}, func(r result) {
r.expect(19+10, 19+10, transparent)
@@ -93,8 +93,8 @@ func TestClipScale(t *testing.T) {
func TestClipRotate(t *testing.T) {
run(t, func(o *op.Ops) {
op.Affine(f32.Affine2D{}.Rotate(f32.Pt(40, 40), -math.Pi/4)).Add(o)
clip.RRect{Rect: f32.Rect(30, 30, 50, 50)}.Add(o)
defer op.Affine(f32.Affine2D{}.Rotate(f32.Pt(40, 40), -math.Pi/4)).Push(o).Pop()
defer clip.RRect{Rect: f32.Rect(30, 30, 50, 50)}.Push(o).Pop()
paint.FillShape(o, red, clip.Rect(image.Rect(0, 40, 100, 100)).Op())
}, func(r result) {
r.expect(39, 39, transparent)
@@ -105,9 +105,9 @@ func TestClipRotate(t *testing.T) {
func TestOffsetTexture(t *testing.T) {
run(t, func(o *op.Ops) {
op.Offset(f32.Pt(15, 15)).Add(o)
defer op.Offset(f32.Pt(15, 15)).Push(o).Pop()
squares.Add(o)
scale(50.0/512, 50.0/512).Add(o)
defer scale(50.0/512, 50.0/512).Push(o).Pop()
paint.PaintOp{}.Add(o)
}, func(r result) {
r.expect(14, 20, transparent)
@@ -119,10 +119,10 @@ func TestOffsetTexture(t *testing.T) {
func TestOffsetScaleTexture(t *testing.T) {
run(t, func(o *op.Ops) {
op.Offset(f32.Pt(15, 15)).Add(o)
defer op.Offset(f32.Pt(15, 15)).Push(o).Pop()
squares.Add(o)
op.Affine(f32.Affine2D{}.Scale(f32.Point{}, f32.Pt(2, 1))).Add(o)
scale(50.0/512, 50.0/512).Add(o)
defer op.Affine(f32.Affine2D{}.Scale(f32.Point{}, f32.Pt(2, 1))).Push(o).Pop()
defer scale(50.0/512, 50.0/512).Push(o).Pop()
paint.PaintOp{}.Add(o)
}, func(r result) {
r.expect(114, 64, colornames.Blue)
@@ -132,11 +132,10 @@ func TestOffsetScaleTexture(t *testing.T) {
func TestRotateTexture(t *testing.T) {
run(t, func(o *op.Ops) {
defer op.Save(o).Load()
squares.Add(o)
a := f32.Affine2D{}.Offset(f32.Pt(30, 30)).Rotate(f32.Pt(40, 40), math.Pi/4)
op.Affine(a).Add(o)
scale(20.0/512, 20.0/512).Add(o)
defer op.Affine(a).Push(o).Pop()
defer scale(20.0/512, 20.0/512).Push(o).Pop()
paint.PaintOp{}.Add(o)
}, func(r result) {
r.expect(40, 40-12, colornames.Blue)
@@ -148,10 +147,10 @@ func TestRotateClipTexture(t *testing.T) {
run(t, func(o *op.Ops) {
squares.Add(o)
a := f32.Affine2D{}.Rotate(f32.Pt(40, 40), math.Pi/8)
op.Affine(a).Add(o)
clip.RRect{Rect: f32.Rect(30, 30, 50, 50)}.Add(o)
op.Affine(f32.Affine2D{}.Offset(f32.Pt(10, 10))).Add(o)
scale(60.0/512, 60.0/512).Add(o)
defer op.Affine(a).Push(o).Pop()
defer clip.RRect{Rect: f32.Rect(30, 30, 50, 50)}.Push(o).Pop()
defer op.Affine(f32.Affine2D{}.Offset(f32.Pt(10, 10))).Push(o).Pop()
defer scale(60.0/512, 60.0/512).Push(o).Pop()
paint.PaintOp{}.Add(o)
}, func(r result) {
r.expect(0, 0, transparent)
@@ -167,13 +166,13 @@ func TestComplicatedTransform(t *testing.T) {
run(t, func(o *op.Ops) {
squares.Add(o)
clip.RRect{Rect: f32.Rect(0, 0, 100, 100), SE: 50, SW: 50, NW: 50, NE: 50}.Add(o)
defer clip.RRect{Rect: f32.Rect(0, 0, 100, 100), SE: 50, SW: 50, NW: 50, NE: 50}.Push(o).Pop()
a := f32.Affine2D{}.Shear(f32.Point{}, math.Pi/4, 0)
op.Affine(a).Add(o)
clip.RRect{Rect: f32.Rect(0, 0, 50, 40)}.Add(o)
defer op.Affine(a).Push(o).Pop()
defer clip.RRect{Rect: f32.Rect(0, 0, 50, 40)}.Push(o).Pop()
scale(50.0/512, 50.0/512).Add(o)
defer scale(50.0/512, 50.0/512).Push(o).Pop()
paint.PaintOp{}.Add(o)
}, func(r result) {
r.expect(20, 5, transparent)
@@ -184,13 +183,13 @@ func TestTransformOrder(t *testing.T) {
// check the ordering of operations bot in affine and in gpu stack.
run(t, func(o *op.Ops) {
a := f32.Affine2D{}.Offset(f32.Pt(64, 64))
op.Affine(a).Add(o)
defer op.Affine(a).Push(o).Pop()
b := f32.Affine2D{}.Scale(f32.Point{}, f32.Pt(8, 8))
op.Affine(b).Add(o)
defer op.Affine(b).Push(o).Pop()
c := f32.Affine2D{}.Offset(f32.Pt(-10, -10)).Scale(f32.Point{}, f32.Pt(0.5, 0.5))
op.Affine(c).Add(o)
defer op.Affine(c).Push(o).Pop()
paint.FillShape(o, red, clip.Rect(image.Rect(0, 0, 20, 20)).Op())
}, func(r result) {
// centered and with radius 40