Compare commits

...

3 Commits

Author SHA1 Message Date
Hajime Hoshi
fc37cdedeb vector: reuse previous allocated subpaths
Closes #3060
2024-08-10 15:13:23 +09:00
Hajime Hoshi
309c886c2e vector: use value type for subpath
Updates #3060
2024-08-10 15:04:13 +09:00
Hajime Hoshi
68380e506e vector: reduce memory allocations by reusing the same Path objects 2024-08-10 14:02:49 +09:00

View File

@ -65,11 +65,18 @@ type subpath struct {
closed bool closed bool
} }
func (s *subpath) pointCount() int { // reset resets the subpath.
// reset doesn't release the allocated memory so that the memory can be reused.
func (s *subpath) reset() {
s.points = s.points[:0]
s.closed = false
}
func (s subpath) pointCount() int {
return len(s.points) return len(s.points)
} }
func (s *subpath) lastPoint() point { func (s subpath) lastPoint() point {
return s.points[len(s.points)-1] return s.points[len(s.points)-1]
} }
@ -78,10 +85,12 @@ func (s *subpath) appendPoint(pt point) {
panic("vector: a closed subpathment cannot append a new point") panic("vector: a closed subpathment cannot append a new point")
} }
// Do not add a too close point to the last point. if len(s.points) > 0 {
// This can cause unexpected rendering results. // Do not add a too close point to the last point.
if lp := s.lastPoint(); abs(lp.x-pt.x) < 1e-2 && abs(lp.y-pt.y) < 1e-2 { // This can cause unexpected rendering results.
return if lp := s.lastPoint(); abs(lp.x-pt.x) < 1e-2 && abs(lp.y-pt.y) < 1e-2 {
return
}
} }
s.points = append(s.points, pt) s.points = append(s.points, pt)
@ -100,13 +109,30 @@ func (s *subpath) close() {
type Path struct { type Path struct {
ops []op ops []op
subpaths []*subpath subpaths []subpath
} }
func (p *Path) ensureSubpaths() []*subpath { // reset resets the path.
// TODO: Probably it is better to avoid returning a slice since allocation is heavy. // reset doesn't release the allocated memory so that the memory can be reused.
// What about walkSubpaths(func(*subpath))? func (p *Path) reset() {
p.ops = p.ops[:0]
p.subpaths = p.subpaths[:0]
}
func (p *Path) appendNewSubpath(pt point) {
if cap(p.subpaths) > len(p.subpaths) {
// Reuse the last subpath since the last subpath might have an already allocated slice.
p.subpaths = p.subpaths[:len(p.subpaths)+1]
p.subpaths[len(p.subpaths)-1].reset()
p.subpaths[len(p.subpaths)-1].appendPoint(pt)
return
}
p.subpaths = append(p.subpaths, subpath{
points: []point{pt},
})
}
func (p *Path) ensureSubpaths() []subpath {
if len(p.subpaths) > 0 || len(p.ops) == 0 { if len(p.subpaths) > 0 || len(p.ops) == 0 {
return p.subpaths return p.subpaths
} }
@ -115,9 +141,7 @@ func (p *Path) ensureSubpaths() []*subpath {
for _, op := range p.ops { for _, op := range p.ops {
switch op.typ { switch op.typ {
case opTypeMoveTo: case opTypeMoveTo:
p.subpaths = append(p.subpaths, &subpath{ p.appendNewSubpath(op.p1)
points: []point{op.p1},
})
cur = op.p1 cur = op.p1
case opTypeLineTo: case opTypeLineTo:
p.lineTo(op.p1) p.lineTo(op.p1)
@ -192,9 +216,7 @@ func (p *Path) Close() {
func (p *Path) lineTo(pt point) { func (p *Path) lineTo(pt point) {
if len(p.subpaths) == 0 || p.subpaths[len(p.subpaths)-1].closed { if len(p.subpaths) == 0 || p.subpaths[len(p.subpaths)-1].closed {
p.subpaths = append(p.subpaths, &subpath{ p.appendNewSubpath(pt)
points: []point{pt},
})
return return
} }
p.subpaths[len(p.subpaths)-1].appendPoint(pt) p.subpaths[len(p.subpaths)-1].appendPoint(pt)
@ -457,8 +479,7 @@ func (p *Path) close() {
if len(p.subpaths) == 0 { if len(p.subpaths) == 0 {
return return
} }
subpath := p.subpaths[len(p.subpaths)-1] p.subpaths[len(p.subpaths)-1].close()
subpath.close()
} }
// AppendVerticesAndIndicesForFilling appends vertices and indices to fill this path and returns them. // AppendVerticesAndIndicesForFilling appends vertices and indices to fill this path and returns them.
@ -558,6 +579,7 @@ func (p *Path) AppendVerticesAndIndicesForStroke(vertices []ebiten.Vertex, indic
} }
var rects [][4]point var rects [][4]point
var tmpPath Path
for _, subpath := range p.ensureSubpaths() { for _, subpath := range p.ensureSubpaths() {
if subpath.pointCount() < 2 { if subpath.pointCount() < 2 {
continue continue
@ -643,46 +665,49 @@ func (p *Path) AppendVerticesAndIndicesForStroke(vertices []ebiten.Vertex, indic
delta := math.Pi - da delta := math.Pi - da
exceed := float32(math.Abs(1/math.Sin(float64(delta/2)))) > op.MiterLimit exceed := float32(math.Abs(1/math.Sin(float64(delta/2)))) > op.MiterLimit
var quad Path // Quadrilateral
quad.MoveTo(c.x, c.y) tmpPath.reset()
tmpPath.MoveTo(c.x, c.y)
if da < math.Pi { if da < math.Pi {
quad.LineTo(rect[1].x, rect[1].y) tmpPath.LineTo(rect[1].x, rect[1].y)
if !exceed { if !exceed {
pt := crossingPointForTwoLines(rect[0], rect[1], nextRect[0], nextRect[1]) pt := crossingPointForTwoLines(rect[0], rect[1], nextRect[0], nextRect[1])
quad.LineTo(pt.x, pt.y) tmpPath.LineTo(pt.x, pt.y)
} }
quad.LineTo(nextRect[0].x, nextRect[0].y) tmpPath.LineTo(nextRect[0].x, nextRect[0].y)
} else { } else {
quad.LineTo(rect[3].x, rect[3].y) tmpPath.LineTo(rect[3].x, rect[3].y)
if !exceed { if !exceed {
pt := crossingPointForTwoLines(rect[2], rect[3], nextRect[2], nextRect[3]) pt := crossingPointForTwoLines(rect[2], rect[3], nextRect[2], nextRect[3])
quad.LineTo(pt.x, pt.y) tmpPath.LineTo(pt.x, pt.y)
} }
quad.LineTo(nextRect[2].x, nextRect[2].y) tmpPath.LineTo(nextRect[2].x, nextRect[2].y)
} }
vertices, indices = quad.AppendVerticesAndIndicesForFilling(vertices, indices) vertices, indices = tmpPath.AppendVerticesAndIndicesForFilling(vertices, indices)
case LineJoinBevel: case LineJoinBevel:
var tri Path // Triangle
tri.MoveTo(c.x, c.y) tmpPath.reset()
tmpPath.MoveTo(c.x, c.y)
if da < math.Pi { if da < math.Pi {
tri.LineTo(rect[1].x, rect[1].y) tmpPath.LineTo(rect[1].x, rect[1].y)
tri.LineTo(nextRect[0].x, nextRect[0].y) tmpPath.LineTo(nextRect[0].x, nextRect[0].y)
} else { } else {
tri.LineTo(rect[3].x, rect[3].y) tmpPath.LineTo(rect[3].x, rect[3].y)
tri.LineTo(nextRect[2].x, nextRect[2].y) tmpPath.LineTo(nextRect[2].x, nextRect[2].y)
} }
vertices, indices = tri.AppendVerticesAndIndicesForFilling(vertices, indices) vertices, indices = tmpPath.AppendVerticesAndIndicesForFilling(vertices, indices)
case LineJoinRound: case LineJoinRound:
var arc Path // Arc
arc.MoveTo(c.x, c.y) tmpPath.reset()
tmpPath.MoveTo(c.x, c.y)
if da < math.Pi { if da < math.Pi {
arc.Arc(c.x, c.y, op.Width/2, a0, a1, Clockwise) tmpPath.Arc(c.x, c.y, op.Width/2, a0, a1, Clockwise)
} else { } else {
arc.Arc(c.x, c.y, op.Width/2, a0+math.Pi, a1+math.Pi, CounterClockwise) tmpPath.Arc(c.x, c.y, op.Width/2, a0+math.Pi, a1+math.Pi, CounterClockwise)
} }
vertices, indices = arc.AppendVerticesAndIndicesForFilling(vertices, indices) vertices, indices = tmpPath.AppendVerticesAndIndicesForFilling(vertices, indices)
} }
} }
@ -707,10 +732,11 @@ func (p *Path) AppendVerticesAndIndicesForStroke(vertices []ebiten.Vertex, indic
y: (startR[0].y + startR[2].y) / 2, y: (startR[0].y + startR[2].y) / 2,
} }
a := float32(math.Atan2(float64(startR[0].y-startR[2].y), float64(startR[0].x-startR[2].x))) a := float32(math.Atan2(float64(startR[0].y-startR[2].y), float64(startR[0].x-startR[2].x)))
var arc Path // Arc
arc.MoveTo(startR[0].x, startR[0].y) tmpPath.reset()
arc.Arc(c.x, c.y, op.Width/2, a, a+math.Pi, CounterClockwise) tmpPath.MoveTo(startR[0].x, startR[0].y)
vertices, indices = arc.AppendVerticesAndIndicesForFilling(vertices, indices) tmpPath.Arc(c.x, c.y, op.Width/2, a, a+math.Pi, CounterClockwise)
vertices, indices = tmpPath.AppendVerticesAndIndicesForFilling(vertices, indices)
} }
{ {
c := point{ c := point{
@ -718,10 +744,11 @@ func (p *Path) AppendVerticesAndIndicesForStroke(vertices []ebiten.Vertex, indic
y: (endR[1].y + endR[3].y) / 2, y: (endR[1].y + endR[3].y) / 2,
} }
a := float32(math.Atan2(float64(endR[1].y-endR[3].y), float64(endR[1].x-endR[3].x))) a := float32(math.Atan2(float64(endR[1].y-endR[3].y), float64(endR[1].x-endR[3].x)))
var arc Path // Arc
arc.MoveTo(endR[1].x, endR[1].y) tmpPath.reset()
arc.Arc(c.x, c.y, op.Width/2, a, a+math.Pi, Clockwise) tmpPath.MoveTo(endR[1].x, endR[1].y)
vertices, indices = arc.AppendVerticesAndIndicesForFilling(vertices, indices) tmpPath.Arc(c.x, c.y, op.Width/2, a, a+math.Pi, Clockwise)
vertices, indices = tmpPath.AppendVerticesAndIndicesForFilling(vertices, indices)
} }
case LineCapSquare: case LineCapSquare:
@ -731,24 +758,26 @@ func (p *Path) AppendVerticesAndIndicesForStroke(vertices []ebiten.Vertex, indic
s, c := math.Sincos(a) s, c := math.Sincos(a)
dx, dy := float32(c)*op.Width/2, float32(s)*op.Width/2 dx, dy := float32(c)*op.Width/2, float32(s)*op.Width/2
var quad Path // Quadrilateral
quad.MoveTo(startR[0].x, startR[0].y) tmpPath.reset()
quad.LineTo(startR[0].x+dx, startR[0].y+dy) tmpPath.MoveTo(startR[0].x, startR[0].y)
quad.LineTo(startR[2].x+dx, startR[2].y+dy) tmpPath.LineTo(startR[0].x+dx, startR[0].y+dy)
quad.LineTo(startR[2].x, startR[2].y) tmpPath.LineTo(startR[2].x+dx, startR[2].y+dy)
vertices, indices = quad.AppendVerticesAndIndicesForFilling(vertices, indices) tmpPath.LineTo(startR[2].x, startR[2].y)
vertices, indices = tmpPath.AppendVerticesAndIndicesForFilling(vertices, indices)
} }
{ {
a := math.Atan2(float64(endR[1].y-endR[0].y), float64(endR[1].x-endR[0].x)) a := math.Atan2(float64(endR[1].y-endR[0].y), float64(endR[1].x-endR[0].x))
s, c := math.Sincos(a) s, c := math.Sincos(a)
dx, dy := float32(c)*op.Width/2, float32(s)*op.Width/2 dx, dy := float32(c)*op.Width/2, float32(s)*op.Width/2
var quad Path // Quadrilateral
quad.MoveTo(endR[1].x, endR[1].y) tmpPath.reset()
quad.LineTo(endR[1].x+dx, endR[1].y+dy) tmpPath.MoveTo(endR[1].x, endR[1].y)
quad.LineTo(endR[3].x+dx, endR[3].y+dy) tmpPath.LineTo(endR[1].x+dx, endR[1].y+dy)
quad.LineTo(endR[3].x, endR[3].y) tmpPath.LineTo(endR[3].x+dx, endR[3].y+dy)
vertices, indices = quad.AppendVerticesAndIndicesForFilling(vertices, indices) tmpPath.LineTo(endR[3].x, endR[3].y)
vertices, indices = tmpPath.AppendVerticesAndIndicesForFilling(vertices, indices)
} }
} }
} }