Commit 48cdb63b authored by Nigel Tao's avatar Nigel Tao

draw.Draw fast paths for a nil mask (and RGBA dst).

Averaged times (in microseconds) for drawing an 800x600 rectangle
are listed below. The summary is: around a 100x improvement.

draw.Draw call times were typically linear in the number of pixels
touched (i.e. drawing an 800x600 rectangle took 100x as much time as
drawing an 80x60 rectangle).

Before this change, there was only the general-but-slow code path.
When drawing any src with a 50%-opaque mask: 237300 us
When drawing any src with a nil mask: 50100 us

After this change, the 50%-opaque mask case is unchanged.
For an *image.RGBA dst and nil mask and...
...a uniform color (i.e. an image.ColorImage) src: 282 us
...another *image.RGBA src: 615 us.

For the curious, an intermediate implementation detected the special
cases but used simple nested for loops instead of the built-in copy
function. The respective times (compared to 282 and 615 for the
final implementation, or 50100 for the original) were 3110 and 3573.

Times were measured with 8g/8l on my laptop. I haven't tried gccgo
or other architectures.

R=r, rsc
CC=golang-dev
https://golang.org/cl/201048
parent 3c686bd2
...@@ -51,6 +51,26 @@ func DrawMask(dst Image, r Rectangle, src image.Image, sp Point, mask image.Imag ...@@ -51,6 +51,26 @@ func DrawMask(dst Image, r Rectangle, src image.Image, sp Point, mask image.Imag
} }
// TODO(nigeltao): Clip r to dst's bounding box, and handle the case when sp or mp has negative X or Y. // TODO(nigeltao): Clip r to dst's bounding box, and handle the case when sp or mp has negative X or Y.
// TODO(nigeltao): Ensure that r is well formed, i.e. r.Max.X >= r.Min.X and likewise for Y.
// Fast paths for special cases. If none of them apply, then we fall back to a general but slow implementation.
if dst0, ok := dst.(*image.RGBA); ok && op == SoverD {
if mask == nil {
if src0, ok := src.(image.ColorImage); ok {
drawFill(dst0, r, src0)
return
}
if src0, ok := src.(*image.RGBA); ok {
if dst0 == src0 && r.Overlaps(r.Add(sp.Sub(r.Min))) {
// TODO(nigeltao): Implement a fast path for the overlapping case.
} else {
drawCopy(dst0, r, src0, sp)
return
}
}
}
// TODO(nigeltao): Implement a fast path for font glyphs (i.e. when mask is an image.Alpha).
}
x0, x1, dx := r.Min.X, r.Max.X, 1 x0, x1, dx := r.Min.X, r.Max.X, 1
y0, y1, dy := r.Min.Y, r.Max.Y, 1 y0, y1, dy := r.Min.Y, r.Max.Y, 1
...@@ -111,6 +131,36 @@ func DrawMask(dst Image, r Rectangle, src image.Image, sp Point, mask image.Imag ...@@ -111,6 +131,36 @@ func DrawMask(dst Image, r Rectangle, src image.Image, sp Point, mask image.Imag
} }
} }
func drawFill(dst *image.RGBA, r Rectangle, src image.ColorImage) {
if r.Dy() < 1 {
return
}
cr, cg, cb, ca := src.RGBA()
color := image.RGBAColor{uint8(cr >> 24), uint8(cg >> 24), uint8(cb >> 24), uint8(ca >> 24)}
// The built-in copy function is faster than a straightforward for loop to fill the destination with
// the color, but copy requires a slice source. We therefore use a for loop to fill the first row, and
// then use the first row as the slice source for the remaining rows.
dx0, dx1 := r.Min.X, r.Max.X
dy0, dy1 := r.Min.Y, r.Max.Y
firstRow := dst.Pixel[dy0]
for x := dx0; x < dx1; x++ {
firstRow[x] = color
}
copySrc := firstRow[dx0:dx1]
for y := dy0 + 1; y < dy1; y++ {
copy(dst.Pixel[y][dx0:dx1], copySrc)
}
}
func drawCopy(dst *image.RGBA, r Rectangle, src *image.RGBA, sp Point) {
dx0, dx1 := r.Min.X, r.Max.X
dy0, dy1 := r.Min.Y, r.Max.Y
sx0, sx1 := sp.X, sp.X+dx1-dx0
for y, sy := dy0, sp.Y; y < dy1; y, sy = y+1, sy+1 {
copy(dst.Pixel[y][dx0:dx1], src.Pixel[sy][sx0:sx1])
}
}
// Border aligns r.Min in dst with sp in src and then replaces pixels // Border aligns r.Min in dst with sp in src and then replaces pixels
// in a w-pixel border around r in dst with the result of the Porter-Duff compositing // in a w-pixel border around r in dst with the result of the Porter-Duff compositing
// operation ``src over dst.'' If w is positive, the border extends w pixels inside r. // operation ``src over dst.'' If w is positive, the border extends w pixels inside r.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment