op/paint: remove support for PaintOp.Rect

PaintOp.Rect is the wrong abstraction; it implies a clip operation
better handled by package clip, and not all paints need it (colors).
Furthermore, it's awkward to specify a PaintOp that fills up the
current clip area, regardless of its size.

Redefine PathOp to mean "fill current clip area".

API change. Replace uses of PaintOp.Rect with a TransformOp applied
before the PaintOp.

Leave a TODO for the PathOp infinity area.

Fixes gio#167

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-11-05 16:09:55 +01:00
parent afb52194d1
commit 94d242d18c
19 changed files with 93 additions and 106 deletions
+1 -1
View File
@@ -36,7 +36,7 @@ const (
TypeLayerLen = 1
TypeRedrawLen = 1 + 8
TypeImageLen = 1
TypePaintLen = 1 + 4*4
TypePaintLen = 1
TypeColorLen = 1 + 4
TypeLinearGradientLen = 1 + 8*2 + 4*2
TypeAreaLen = 1 + 1 + 4*4
+2 -2
View File
@@ -171,7 +171,7 @@ func draw1000CirclesInstanced(gtx layout.Context) {
r := op.Record(ops)
clip.RRect{Rect: f32.Rect(0, 0, 10, 10), NE: 5, SE: 5, SW: 5, NW: 5}.Add(ops)
paint.PaintOp{Rect: f32.Rect(0, 0, 10, 10)}.Add(ops)
paint.PaintOp{}.Add(ops)
c := r.Stop()
for x := 0; x < 100; x++ {
@@ -228,7 +228,7 @@ func drawShapeInstances(gtx layout.Context, th *material.Theme) chan op.CallOp {
r := op.Record(ops)
clip.RRect{Rect: f32.Rect(0, 0, 25, 25), NE: 10, SE: 10, SW: 10, NW: 10}.Add(ops)
paint.PaintOp{Rect: f32.Rect(0, 0, 25, 25)}.Add(ops)
paint.PaintOp{}.Add(ops)
c := r.Stop()
squares.Add(ops)
+4 -2
View File
@@ -81,7 +81,8 @@ func TestPaintArc(t *testing.T) {
func TestPaintTexture(t *testing.T) {
run(t, func(o *op.Ops) {
squares.Add(o)
paint.PaintOp{Rect: f32.Rect(0, 0, 80, 80)}.Add(o)
scale(80.0/512, 80.0/512).Add(o)
paint.PaintOp{}.Add(o)
}, func(r result) {
r.expect(0, 0, colornames.Blue)
r.expect(79, 10, colornames.Green)
@@ -94,7 +95,8 @@ 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)
paint.PaintOp{Rect: f32.Rect(0, 0, 80, 80)}.Add(o)
scale(80.0/512, 80.0/512).Add(o)
paint.PaintOp{}.Add(o)
}, func(r result) {
r.expect(40, 40, colornames.White)
r.expect(25, 35, colornames.Blue)
+31 -11
View File
@@ -35,10 +35,10 @@ func TestTransformMacro(t *testing.T) {
stack := op.Push(o)
op.Offset(f32.Pt(0, 10)).Add(o)
// Actually create the text clip-path
// Apply the clip-path.
c.Add(o)
paint.PaintOp{Rect: f32.Rect(0, 0, 10, 10)}.Add(o)
paint.PaintOp{}.Add(o)
stack.Pop()
c2 := m2.Stop()
@@ -124,7 +124,7 @@ func constSqCirc() op.CallOp {
func drawChild(ops *op.Ops, text op.CallOp) op.CallOp {
r1 := op.Record(ops)
text.Add(ops)
paint.PaintOp{Rect: f32.Rect(0, 0, 10, 10)}.Add(ops)
paint.PaintOp{}.Add(ops)
return r1.Stop()
}
@@ -159,7 +159,7 @@ func TestBuildOffscreen(t *testing.T) {
s := op.Push(o)
op.Offset(f32.Pt(0, off)).Add(o)
txt.Add(o)
paint.PaintOp{Rect: f32.Rect(0, 0, 40, 40)}.Add(o)
paint.PaintOp{}.Add(o)
s.Pop()
}
@@ -184,7 +184,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)
paint.PaintOp{Rect: f32.Rect(0, 120, 100, 122)}.Add(ops)
clip.Rect(image.Rect(0, 120, 100, 122)).Add(ops)
paint.PaintOp{}.Add(ops)
}, func(r result) {
r.expect(60, 60, colornames.White)
r.expect(60, 110, colornames.White)
@@ -207,6 +208,8 @@ var gradients = []Gradient{
}
func TestLinearGradient(t *testing.T) {
t.Skip("linear gradients don't support transformations")
const gradienth = 8
// 0.5 offset from ends to ensure that the center of the pixel
// aligns with gradient from and to colors.
@@ -214,7 +217,7 @@ func TestLinearGradient(t *testing.T) {
samples := []int{0, 12, 32, 64, 96, 115, 127}
run(t, func(ops *op.Ops) {
gr := pixelAligned
gr := f32.Rect(0, 0, 128, gradienth)
for _, g := range gradients {
paint.LinearGradientOp{
Stop1: f32.Pt(gr.Min.X, gr.Min.Y),
@@ -222,7 +225,12 @@ func TestLinearGradient(t *testing.T) {
Stop2: f32.Pt(gr.Max.X, gr.Min.Y),
Color2: g.To,
}.Add(ops)
paint.PaintOp{Rect: gr}.Add(ops)
st := op.Push(ops)
clip.RRect{Rect: gr}.Add(ops)
op.Affine(f32.Affine2D{}.Offset(pixelAligned.Min)).Add(ops)
scale(pixelAligned.Dx()/128, 1).Add(ops)
paint.PaintOp{}.Add(ops)
st.Pop()
gr = gr.Add(f32.Pt(0, gradienth))
}
}, func(r result) {
@@ -247,7 +255,10 @@ func TestLinearGradientAngled(t *testing.T) {
Stop2: f32.Pt(0, 0),
Color2: colornames.Red,
}.Add(ops)
paint.PaintOp{Rect: f32.Rect(0, 0, 64, 64)}.Add(ops)
st := op.Push(ops)
clip.Rect(image.Rect(0, 0, 64, 64)).Add(ops)
paint.PaintOp{}.Add(ops)
st.Pop()
paint.LinearGradientOp{
Stop1: f32.Pt(64, 64),
@@ -255,7 +266,10 @@ func TestLinearGradientAngled(t *testing.T) {
Stop2: f32.Pt(128, 0),
Color2: colornames.Green,
}.Add(ops)
paint.PaintOp{Rect: f32.Rect(64, 0, 128, 64)}.Add(ops)
st = op.Push(ops)
clip.Rect(image.Rect(64, 0, 128, 64)).Add(ops)
paint.PaintOp{}.Add(ops)
st.Pop()
paint.LinearGradientOp{
Stop1: f32.Pt(64, 64),
@@ -263,7 +277,10 @@ func TestLinearGradientAngled(t *testing.T) {
Stop2: f32.Pt(128, 128),
Color2: colornames.Blue,
}.Add(ops)
paint.PaintOp{Rect: f32.Rect(64, 64, 128, 128)}.Add(ops)
st = op.Push(ops)
clip.Rect(image.Rect(64, 64, 128, 128)).Add(ops)
paint.PaintOp{}.Add(ops)
st.Pop()
paint.LinearGradientOp{
Stop1: f32.Pt(64, 64),
@@ -271,7 +288,10 @@ func TestLinearGradientAngled(t *testing.T) {
Stop2: f32.Pt(0, 128),
Color2: colornames.Magenta,
}.Add(ops)
paint.PaintOp{Rect: f32.Rect(0, 64, 64, 128)}.Add(ops)
st = op.Push(ops)
clip.Rect(image.Rect(0, 64, 64, 128)).Add(ops)
paint.PaintOp{}.Add(ops)
st.Pop()
}, func(r result) {})
}
+13 -7
View File
@@ -104,7 +104,8 @@ func TestOffsetTexture(t *testing.T) {
run(t, func(o *op.Ops) {
op.Offset(f32.Pt(15, 15)).Add(o)
squares.Add(o)
paint.PaintOp{Rect: f32.Rect(0, 0, 50, 50)}.Add(o)
scale(50.0/512, 50.0/512).Add(o)
paint.PaintOp{}.Add(o)
}, func(r result) {
r.expect(14, 20, colornames.White)
r.expect(66, 20, colornames.White)
@@ -118,7 +119,8 @@ func TestOffsetScaleTexture(t *testing.T) {
op.Offset(f32.Pt(15, 15)).Add(o)
squares.Add(o)
op.Affine(f32.Affine2D{}.Scale(f32.Point{}, f32.Pt(2, 1))).Add(o)
paint.PaintOp{Rect: f32.Rect(0, 0, 50, 50)}.Add(o)
scale(50.0/512, 50.0/512).Add(o)
paint.PaintOp{}.Add(o)
}, func(r result) {
r.expect(114, 64, colornames.Blue)
r.expect(116, 64, colornames.White)
@@ -127,10 +129,12 @@ func TestOffsetScaleTexture(t *testing.T) {
func TestRotateTexture(t *testing.T) {
run(t, func(o *op.Ops) {
defer op.Push(o).Pop()
squares.Add(o)
a := f32.Affine2D{}.Rotate(f32.Pt(40, 40), math.Pi/4)
a := f32.Affine2D{}.Offset(f32.Pt(30, 30)).Rotate(f32.Pt(40, 40), math.Pi/4)
op.Affine(a).Add(o)
paint.PaintOp{Rect: f32.Rect(30, 30, 50, 50)}.Add(o)
scale(20.0/512, 20.0/512).Add(o)
paint.PaintOp{}.Add(o)
}, func(r result) {
r.expect(40, 40-12, colornames.Blue)
r.expect(40+12, 40, colornames.Green)
@@ -143,7 +147,9 @@ func TestRotateClipTexture(t *testing.T) {
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)
paint.PaintOp{Rect: f32.Rect(10, 10, 70, 70)}.Add(o)
op.Affine(f32.Affine2D{}.Offset(f32.Pt(10, 10))).Add(o)
scale(60.0/512, 60.0/512).Add(o)
paint.PaintOp{}.Add(o)
}, func(r result) {
r.expect(0, 0, colornames.White)
r.expect(37, 39, colornames.Green)
@@ -164,8 +170,8 @@ func TestComplicatedTransform(t *testing.T) {
op.Affine(a).Add(o)
clip.RRect{Rect: f32.Rect(0, 0, 50, 40)}.Add(o)
op.Offset(f32.Pt(-100, -100)).Add(o)
paint.PaintOp{Rect: f32.Rect(100, 100, 150, 150)}.Add(o)
scale(50.0/512, 50.0/512).Add(o)
paint.PaintOp{}.Add(o)
}, func(r result) {
r.expect(20, 5, colornames.White)
})
+5
View File
@@ -14,6 +14,7 @@ import (
"testing"
"gioui.org/app/headless"
"gioui.org/f32"
"gioui.org/op"
"gioui.org/op/paint"
"golang.org/x/image/colornames"
@@ -210,3 +211,7 @@ func newWindow(t testing.TB, width, height int) *headless.Window {
t.Cleanup(w.Release)
return w
}
func scale(sx, sy float32) op.TransformOp {
return op.Affine(f32.Affine2D{}.Scale(f32.Point{}, f32.Pt(sx, sy)))
}