Fix #42. Performance improvement by reusing Rasterizer

This commit is contained in:
Michael Fogleman 2018-08-16 22:18:45 -04:00
parent 6bee5281ff
commit 9a34078211

View File

@ -54,6 +54,7 @@ var (
type Context struct { type Context struct {
width int width int
height int height int
rasterizer *raster.Rasterizer
im *image.RGBA im *image.RGBA
mask *image.Alpha mask *image.Alpha
color color.Color color color.Color
@ -90,9 +91,12 @@ func NewContextForImage(im image.Image) *Context {
// NewContextForRGBA prepares a context for rendering onto the specified image. // NewContextForRGBA prepares a context for rendering onto the specified image.
// No copy is made. // No copy is made.
func NewContextForRGBA(im *image.RGBA) *Context { func NewContextForRGBA(im *image.RGBA) *Context {
w := im.Bounds().Size().X
h := im.Bounds().Size().Y
return &Context{ return &Context{
width: im.Bounds().Size().X, width: w,
height: im.Bounds().Size().Y, height: h,
rasterizer: raster.NewRasterizer(w, h),
im: im, im: im,
color: color.Transparent, color: color.Transparent,
fillPattern: defaultFillStyle, fillPattern: defaultFillStyle,
@ -381,8 +385,9 @@ func (dc *Context) stroke(painter raster.Painter) {
// that result in rendering issues // that result in rendering issues
path = rasterPath(flattenPath(path)) path = rasterPath(flattenPath(path))
} }
r := raster.NewRasterizer(dc.width, dc.height) r := dc.rasterizer
r.UseNonZeroWinding = true r.UseNonZeroWinding = true
r.Clear()
r.AddStroke(path, fix(dc.lineWidth), dc.capper(), dc.joiner()) r.AddStroke(path, fix(dc.lineWidth), dc.capper(), dc.joiner())
r.Rasterize(painter) r.Rasterize(painter)
} }
@ -394,8 +399,9 @@ func (dc *Context) fill(painter raster.Painter) {
copy(path, dc.fillPath) copy(path, dc.fillPath)
path.Add1(dc.start.Fixed()) path.Add1(dc.start.Fixed())
} }
r := raster.NewRasterizer(dc.width, dc.height) r := dc.rasterizer
r.UseNonZeroWinding = dc.fillRule == FillRuleWinding r.UseNonZeroWinding = dc.fillRule == FillRuleWinding
r.Clear()
r.AddPath(path) r.AddPath(path)
r.Rasterize(painter) r.Rasterize(painter)
} }