app/headless,internal/rendertest: replace PaintOps with Fill/FillShape

We're about to remove PaintOp.Rect. Replacing PaintOps with Fill or
FillShape where possible will ease the transition.

Using Fill in tests exposed a problem with the infinity in paint.Fill.
Adjust it for now; it will be removed later.

Updates gio#167

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-11-05 16:06:27 +01:00
parent 852958f4b5
commit afb52194d1
6 changed files with 36 additions and 61 deletions
+3 -12
View File
@@ -22,10 +22,7 @@ func TestHeadless(t *testing.T) {
var ops op.Ops var ops op.Ops
paint.ColorOp{Color: col}.Add(&ops) paint.ColorOp{Color: col}.Add(&ops)
// Paint only part of the screen to avoid the glClear optimization. // Paint only part of the screen to avoid the glClear optimization.
paint.PaintOp{Rect: f32.Rectangle{Max: f32.Point{ paint.FillShape(&ops, col, clip.Rect(image.Rect(0, 0, sz.X-100, sz.Y-100)).Op())
X: float32(sz.X) - 100,
Y: float32(sz.Y) - 100,
}}}.Add(&ops)
if err := w.Frame(&ops); err != nil { if err := w.Frame(&ops); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -108,15 +105,9 @@ func TestDepth(t *testing.T) {
var ops op.Ops var ops op.Ops
blue := color.RGBA{B: 0xFF, A: 0xFF} blue := color.RGBA{B: 0xFF, A: 0xFF}
paint.ColorOp{Color: blue}.Add(&ops) paint.FillShape(&ops, blue, clip.Rect(image.Rect(0, 0, 50, 100)).Op())
paint.PaintOp{Rect: f32.Rectangle{
Max: f32.Point{X: 50, Y: 100},
}}.Add(&ops)
red := color.RGBA{R: 0xFF, A: 0xFF} red := color.RGBA{R: 0xFF, A: 0xFF}
paint.ColorOp{Color: red}.Add(&ops) paint.FillShape(&ops, red, clip.Rect(image.Rect(0, 0, 100, 50)).Op())
paint.PaintOp{Rect: f32.Rectangle{
Max: f32.Point{X: 100, Y: 50},
}}.Add(&ops)
if err := w.Frame(&ops); err != nil { if err := w.Frame(&ops); err != nil {
t.Fatal(err) t.Fatal(err)
} }
+8 -10
View File
@@ -156,11 +156,10 @@ func draw1000Circles(gtx layout.Context) {
p := op.Push(ops) p := op.Push(ops)
op.Offset(f32.Pt(float32(x*10), 0)).Add(ops) op.Offset(f32.Pt(float32(x*10), 0)).Add(ops)
for y := 0; y < 10; y++ { for y := 0; y < 10; y++ {
pi := op.Push(ops) paint.FillShape(ops,
paint.ColorOp{Color: color.RGBA{R: 100 + uint8(x), G: 100 + uint8(y), B: 100, A: 120}}.Add(ops) color.RGBA{R: 100 + uint8(x), G: 100 + uint8(y), B: 100, A: 120},
clip.RRect{Rect: f32.Rect(0, 0, 10, 10), NE: 5, SE: 5, SW: 5, NW: 5}.Add(ops) clip.RRect{Rect: f32.Rect(0, 0, 10, 10), NE: 5, SE: 5, SW: 5, NW: 5}.Op(ops),
paint.PaintOp{Rect: f32.Rect(0, 0, 10, 10)}.Add(ops) )
pi.Pop()
op.Offset(f32.Pt(0, float32(100))).Add(ops) op.Offset(f32.Pt(0, float32(100))).Add(ops)
} }
p.Pop() p.Pop()
@@ -208,11 +207,10 @@ func drawIndividualShapes(gtx layout.Context, th *material.Theme) chan op.CallOp
p := op.Push(ops) p := op.Push(ops)
op.Offset(f32.Pt(float32(x*50), 0)).Add(ops) op.Offset(f32.Pt(float32(x*50), 0)).Add(ops)
for y := 0; y < 9; y++ { for y := 0; y < 9; y++ {
pi := op.Push(ops) paint.FillShape(ops,
paint.ColorOp{Color: color.RGBA{R: 100 + uint8(x), G: 100 + uint8(y), B: 100, A: 120}}.Add(ops) color.RGBA{R: 100 + uint8(x), G: 100 + uint8(y), B: 100, A: 120},
clip.RRect{Rect: f32.Rect(0, 0, 25, 25), NE: 10, SE: 10, SW: 10, NW: 10}.Add(ops) clip.RRect{Rect: f32.Rect(0, 0, 25, 25), NE: 10, SE: 10, SW: 10, NW: 10}.Op(ops),
paint.PaintOp{Rect: f32.Rect(0, 0, 25, 25)}.Add(ops) )
pi.Pop()
op.Offset(f32.Pt(0, float32(50))).Add(ops) op.Offset(f32.Pt(0, float32(50))).Add(ops)
} }
p.Pop() p.Pop()
+6 -8
View File
@@ -1,6 +1,7 @@
package rendertest package rendertest
import ( import (
"image"
"math" "math"
"testing" "testing"
@@ -13,8 +14,7 @@ import (
func TestPaintRect(t *testing.T) { func TestPaintRect(t *testing.T) {
run(t, func(o *op.Ops) { run(t, func(o *op.Ops) {
paint.ColorOp{Color: colornames.Red}.Add(o) paint.FillShape(o, colornames.Red, clip.Rect(image.Rect(0, 0, 50, 50)).Op())
paint.PaintOp{Rect: f32.Rect(0, 0, 50, 50)}.Add(o)
}, func(r result) { }, func(r result) {
r.expect(0, 0, colornames.Red) r.expect(0, 0, colornames.Red)
r.expect(49, 0, colornames.Red) r.expect(49, 0, colornames.Red)
@@ -25,9 +25,8 @@ func TestPaintRect(t *testing.T) {
func TestPaintClippedRect(t *testing.T) { func TestPaintClippedRect(t *testing.T) {
run(t, func(o *op.Ops) { run(t, func(o *op.Ops) {
paint.ColorOp{Color: colornames.Red}.Add(o)
clip.RRect{Rect: f32.Rect(25, 25, 60, 60)}.Add(o) clip.RRect{Rect: f32.Rect(25, 25, 60, 60)}.Add(o)
paint.PaintOp{Rect: f32.Rect(0, 0, 50, 50)}.Add(o) paint.FillShape(o, colornames.Red, clip.Rect(image.Rect(0, 0, 50, 50)).Op())
}, func(r result) { }, func(r result) {
r.expect(0, 0, colornames.White) r.expect(0, 0, colornames.White)
r.expect(24, 35, colornames.White) r.expect(24, 35, colornames.White)
@@ -39,10 +38,10 @@ func TestPaintClippedRect(t *testing.T) {
func TestPaintClippedCirle(t *testing.T) { func TestPaintClippedCirle(t *testing.T) {
run(t, func(o *op.Ops) { run(t, func(o *op.Ops) {
paint.ColorOp{Color: colornames.Red}.Add(o)
r := float32(10) r := float32(10)
clip.RRect{Rect: f32.Rect(20, 20, 40, 40), SE: r, SW: r, NW: r, NE: r}.Add(o) clip.RRect{Rect: f32.Rect(20, 20, 40, 40), SE: r, SW: r, NW: r, NE: r}.Add(o)
paint.PaintOp{Rect: f32.Rect(0, 0, 30, 50)}.Add(o) clip.Rect(image.Rect(0, 0, 30, 50)).Add(o)
paint.Fill(o, colornames.Red)
}, func(r result) { }, func(r result) {
r.expect(21, 21, colornames.White) r.expect(21, 21, colornames.White)
r.expect(25, 30, colornames.Red) r.expect(25, 30, colornames.Red)
@@ -71,8 +70,7 @@ func TestPaintArc(t *testing.T) {
p.Line(f32.Pt(-50, 0)) p.Line(f32.Pt(-50, 0))
p.End().Add(o) p.End().Add(o)
paint.ColorOp{Color: colornames.Red}.Add(o) paint.FillShape(o, colornames.Red, clip.Rect(image.Rect(0, 0, 128, 128)).Op())
paint.PaintOp{Rect: f32.Rect(0, 0, 128, 128)}.Add(o)
}, func(r result) { }, func(r result) {
r.expect(0, 0, colornames.White) r.expect(0, 0, colornames.White)
r.expect(0, 25, colornames.Red) r.expect(0, 25, colornames.Red)
+9 -13
View File
@@ -1,6 +1,7 @@
package rendertest package rendertest
import ( import (
"image"
"image/color" "image/color"
"math" "math"
"testing" "testing"
@@ -16,16 +17,15 @@ import (
func TestTransformMacro(t *testing.T) { func TestTransformMacro(t *testing.T) {
// testcase resulting from original bug when rendering layout.Stacked // testcase resulting from original bug when rendering layout.Stacked
// pre-build the text // Build clip-path.
c := constSqPath() c := constSqPath()
run(t, func(o *op.Ops) { run(t, func(o *op.Ops) {
// render the first Stacked item // render the first Stacked item
m1 := op.Record(o) m1 := op.Record(o)
dr := f32.Rect(0, 0, 128, 50) dr := image.Rect(0, 0, 128, 50)
paint.ColorOp{Color: colornames.Black}.Add(o) paint.FillShape(o, colornames.Black, clip.Rect(dr).Op())
paint.PaintOp{Rect: dr}.Add(o)
c1 := m1.Stop() c1 := m1.Stop()
// Render the second stacked item // Render the second stacked item
@@ -62,8 +62,7 @@ func TestTransformMacro(t *testing.T) {
func TestRepeatedPaintsZ(t *testing.T) { func TestRepeatedPaintsZ(t *testing.T) {
run(t, func(o *op.Ops) { run(t, func(o *op.Ops) {
// Draw a rectangle // Draw a rectangle
paint.ColorOp{Color: colornames.Black}.Add(o) paint.FillShape(o, colornames.Black, clip.Rect(image.Rect(0, 0, 128, 50)).Op())
paint.PaintOp{Rect: f32.Rect(0, 0, 128, 50)}.Add(o)
builder := clip.Path{} builder := clip.Path{}
builder.Begin(o) builder.Begin(o)
@@ -73,8 +72,7 @@ func TestRepeatedPaintsZ(t *testing.T) {
builder.Line(f32.Pt(-10, 0)) builder.Line(f32.Pt(-10, 0))
builder.Line(f32.Pt(0, -10)) builder.Line(f32.Pt(0, -10))
builder.End().Add(o) builder.End().Add(o)
paint.ColorOp{Color: colornames.Red}.Add(o) paint.Fill(o, colornames.Red)
paint.PaintOp{Rect: f32.Rect(0, 0, 10, 10)}.Add(o)
}, func(r result) { }, func(r result) {
r.expect(5, 5, colornames.Red) r.expect(5, 5, colornames.Red)
r.expect(11, 15, colornames.Black) r.expect(11, 15, colornames.Black)
@@ -84,17 +82,15 @@ func TestRepeatedPaintsZ(t *testing.T) {
func TestNoClipFromPaint(t *testing.T) { func TestNoClipFromPaint(t *testing.T) {
// ensure that a paint operation does not polute the state // ensure that a paint operation does not polute the state
// by leaving any clip paths i place. // by leaving any clip paths in place.
run(t, func(o *op.Ops) { run(t, func(o *op.Ops) {
a := f32.Affine2D{}.Rotate(f32.Pt(20, 20), math.Pi/4) a := f32.Affine2D{}.Rotate(f32.Pt(20, 20), math.Pi/4)
op.Affine(a).Add(o) op.Affine(a).Add(o)
paint.ColorOp{Color: colornames.Red}.Add(o) paint.FillShape(o, colornames.Red, clip.Rect(image.Rect(10, 10, 30, 30)).Op())
paint.PaintOp{Rect: f32.Rect(10, 10, 30, 30)}.Add(o)
a = f32.Affine2D{}.Rotate(f32.Pt(20, 20), -math.Pi/4) a = f32.Affine2D{}.Rotate(f32.Pt(20, 20), -math.Pi/4)
op.Affine(a).Add(o) op.Affine(a).Add(o)
paint.ColorOp{Color: colornames.Black}.Add(o) paint.FillShape(o, colornames.Black, clip.Rect(image.Rect(0, 0, 50, 50)).Op())
paint.PaintOp{Rect: f32.Rect(0, 0, 50, 50)}.Add(o)
}, func(r result) { }, func(r result) {
r.expect(1, 1, colornames.Black) r.expect(1, 1, colornames.Black)
r.expect(20, 20, colornames.Black) r.expect(20, 20, colornames.Black)
+9 -17
View File
@@ -1,6 +1,7 @@
package rendertest package rendertest
import ( import (
"image"
"math" "math"
"testing" "testing"
@@ -14,8 +15,7 @@ import (
func TestPaintOffset(t *testing.T) { func TestPaintOffset(t *testing.T) {
run(t, func(o *op.Ops) { run(t, func(o *op.Ops) {
op.Offset(f32.Pt(10, 20)).Add(o) op.Offset(f32.Pt(10, 20)).Add(o)
paint.ColorOp{Color: colornames.Red}.Add(o) paint.FillShape(o, colornames.Red, clip.Rect(image.Rect(0, 0, 50, 50)).Op())
paint.PaintOp{Rect: f32.Rect(0, 0, 50, 50)}.Add(o)
}, func(r result) { }, func(r result) {
r.expect(0, 0, colornames.White) r.expect(0, 0, colornames.White)
r.expect(59, 30, colornames.Red) r.expect(59, 30, colornames.Red)
@@ -28,8 +28,7 @@ func TestPaintRotate(t *testing.T) {
run(t, func(o *op.Ops) { run(t, func(o *op.Ops) {
a := f32.Affine2D{}.Rotate(f32.Pt(40, 40), -math.Pi/8) a := f32.Affine2D{}.Rotate(f32.Pt(40, 40), -math.Pi/8)
op.Affine(a).Add(o) op.Affine(a).Add(o)
paint.ColorOp{Color: colornames.Red}.Add(o) paint.FillShape(o, colornames.Red, clip.Rect(image.Rect(20, 20, 60, 60)).Op())
paint.PaintOp{Rect: f32.Rect(20, 20, 60, 60)}.Add(o)
}, func(r result) { }, func(r result) {
r.expect(40, 40, colornames.Red) r.expect(40, 40, colornames.Red)
r.expect(50, 19, colornames.Red) r.expect(50, 19, colornames.Red)
@@ -42,8 +41,7 @@ func TestPaintShear(t *testing.T) {
run(t, func(o *op.Ops) { run(t, func(o *op.Ops) {
a := f32.Affine2D{}.Shear(f32.Point{}, math.Pi/4, 0) a := f32.Affine2D{}.Shear(f32.Point{}, math.Pi/4, 0)
op.Affine(a).Add(o) op.Affine(a).Add(o)
paint.ColorOp{Color: colornames.Red}.Add(o) paint.FillShape(o, colornames.Red, clip.Rect(image.Rect(0, 0, 40, 40)).Op())
paint.PaintOp{Rect: f32.Rect(0, 0, 40, 40)}.Add(o)
}, func(r result) { }, func(r result) {
r.expect(10, 30, colornames.White) r.expect(10, 30, colornames.White)
}) })
@@ -51,10 +49,9 @@ func TestPaintShear(t *testing.T) {
func TestClipPaintOffset(t *testing.T) { func TestClipPaintOffset(t *testing.T) {
run(t, func(o *op.Ops) { run(t, func(o *op.Ops) {
paint.ColorOp{Color: colornames.Red}.Add(o)
clip.RRect{Rect: f32.Rect(10, 10, 30, 30)}.Add(o) clip.RRect{Rect: f32.Rect(10, 10, 30, 30)}.Add(o)
op.Offset(f32.Pt(20, 20)).Add(o) op.Offset(f32.Pt(20, 20)).Add(o)
paint.PaintOp{Rect: f32.Rect(0, 0, 100, 100)}.Add(o) paint.FillShape(o, colornames.Red, clip.Rect(image.Rect(0, 0, 100, 100)).Op())
}, func(r result) { }, func(r result) {
r.expect(0, 0, colornames.White) r.expect(0, 0, colornames.White)
r.expect(19, 19, colornames.White) r.expect(19, 19, colornames.White)
@@ -65,10 +62,9 @@ func TestClipPaintOffset(t *testing.T) {
func TestClipOffset(t *testing.T) { func TestClipOffset(t *testing.T) {
run(t, func(o *op.Ops) { run(t, func(o *op.Ops) {
paint.ColorOp{Color: colornames.Red}.Add(o)
op.Offset(f32.Pt(20, 20)).Add(o) op.Offset(f32.Pt(20, 20)).Add(o)
clip.RRect{Rect: f32.Rect(10, 10, 30, 30)}.Add(o) clip.RRect{Rect: f32.Rect(10, 10, 30, 30)}.Add(o)
paint.PaintOp{Rect: f32.Rect(0, 0, 100, 100)}.Add(o) paint.FillShape(o, colornames.Red, clip.Rect(image.Rect(0, 0, 100, 100)).Op())
}, func(r result) { }, func(r result) {
r.expect(0, 0, colornames.White) r.expect(0, 0, colornames.White)
r.expect(29, 29, colornames.White) r.expect(29, 29, colornames.White)
@@ -80,11 +76,10 @@ func TestClipOffset(t *testing.T) {
func TestClipScale(t *testing.T) { func TestClipScale(t *testing.T) {
run(t, func(o *op.Ops) { run(t, func(o *op.Ops) {
paint.ColorOp{Color: colornames.Red}.Add(o)
a := f32.Affine2D{}.Scale(f32.Point{}, f32.Pt(2, 2)).Offset(f32.Pt(10, 10)) a := f32.Affine2D{}.Scale(f32.Point{}, f32.Pt(2, 2)).Offset(f32.Pt(10, 10))
op.Affine(a).Add(o) op.Affine(a).Add(o)
clip.RRect{Rect: f32.Rect(10, 10, 20, 20)}.Add(o) clip.RRect{Rect: f32.Rect(10, 10, 20, 20)}.Add(o)
paint.PaintOp{Rect: f32.Rect(0, 0, 1000, 1000)}.Add(o) paint.FillShape(o, colornames.Red, clip.Rect(image.Rect(0, 0, 1000, 1000)).Op())
}, func(r result) { }, func(r result) {
r.expect(19+10, 19+10, colornames.White) r.expect(19+10, 19+10, colornames.White)
r.expect(20+10, 20+10, colornames.Red) r.expect(20+10, 20+10, colornames.Red)
@@ -95,10 +90,9 @@ func TestClipScale(t *testing.T) {
func TestClipRotate(t *testing.T) { func TestClipRotate(t *testing.T) {
run(t, func(o *op.Ops) { run(t, func(o *op.Ops) {
paint.ColorOp{Color: colornames.Red}.Add(o)
op.Affine(f32.Affine2D{}.Rotate(f32.Pt(40, 40), -math.Pi/4)).Add(o) 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) clip.RRect{Rect: f32.Rect(30, 30, 50, 50)}.Add(o)
paint.PaintOp{Rect: f32.Rect(0, 40, 100, 100)}.Add(o) paint.FillShape(o, colornames.Red, clip.Rect(image.Rect(0, 40, 100, 100)).Op())
}, func(r result) { }, func(r result) {
r.expect(39, 39, colornames.White) r.expect(39, 39, colornames.White)
r.expect(41, 41, colornames.Red) r.expect(41, 41, colornames.Red)
@@ -180,8 +174,6 @@ func TestComplicatedTransform(t *testing.T) {
func TestTransformOrder(t *testing.T) { func TestTransformOrder(t *testing.T) {
// check the ordering of operations bot in affine and in gpu stack. // check the ordering of operations bot in affine and in gpu stack.
run(t, func(o *op.Ops) { run(t, func(o *op.Ops) {
paint.ColorOp{Color: colornames.Red}.Add(o)
a := f32.Affine2D{}.Offset(f32.Pt(64, 64)) a := f32.Affine2D{}.Offset(f32.Pt(64, 64))
op.Affine(a).Add(o) op.Affine(a).Add(o)
@@ -190,7 +182,7 @@ func TestTransformOrder(t *testing.T) {
c := f32.Affine2D{}.Offset(f32.Pt(-10, -10)).Scale(f32.Point{}, f32.Pt(0.5, 0.5)) c := f32.Affine2D{}.Offset(f32.Pt(-10, -10)).Scale(f32.Point{}, f32.Pt(0.5, 0.5))
op.Affine(c).Add(o) op.Affine(c).Add(o)
paint.PaintOp{Rect: f32.Rect(0, 0, 20, 20)}.Add(o) paint.FillShape(o, colornames.Red, clip.Rect(image.Rect(0, 0, 20, 20)).Op())
}, func(r result) { }, func(r result) {
// centered and with radius 40 // centered and with radius 40
r.expect(64-41, 64, colornames.White) r.expect(64-41, 64, colornames.White)
+1 -1
View File
@@ -160,7 +160,7 @@ func FillShape(ops *op.Ops, c color.RGBA, shape clip.Op) {
func Fill(ops *op.Ops, c color.RGBA) { func Fill(ops *op.Ops, c color.RGBA) {
defer op.Push(ops).Pop() defer op.Push(ops).Pop()
ColorOp{Color: c}.Add(ops) ColorOp{Color: c}.Add(ops)
inf := float32(math.Inf(+1)) inf := float32(1e6)
PaintOp{ PaintOp{
Rect: f32.Rectangle{ Rect: f32.Rectangle{
Min: f32.Pt(-inf, -inf), Min: f32.Pt(-inf, -inf),