1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
19 // bench benchmarks drawing src and mask images onto a dst image with the
20 // given op and the color models to create those images from.
21 // The created images' pixels are initialized to non-zero values.
22 func bench(b *testing.B, dcm, scm, mcm color.Model, op Op) {
28 dst1 := image.NewRGBA(image.Rect(0, 0, dstw, dsth))
29 for y := 0; y < dsth; y++ {
30 for x := 0; x < dstw; x++ {
31 dst1.SetRGBA(x, y, color.RGBA{
34 uint8((7*x + 5*y) % 0x100),
40 case color.RGBA64Model:
41 dst1 := image.NewRGBA64(image.Rect(0, 0, dstw, dsth))
42 for y := 0; y < dsth; y++ {
43 for x := 0; x < dstw; x++ {
44 dst1.SetRGBA64(x, y, color.RGBA64{
45 uint16(53 * x % 0x10000),
46 uint16(59 * y % 0x10000),
47 uint16((59*x + 53*y) % 0x10000),
60 src = &image.Uniform{color.RGBA{0x11, 0x22, 0x33, 0xff}}
62 src1 := image.NewRGBA(image.Rect(0, 0, srcw, srch))
63 for y := 0; y < srch; y++ {
64 for x := 0; x < srcw; x++ {
65 src1.SetRGBA(x, y, color.RGBA{
68 uint8((11*x + 13*y) % 0x80),
74 case color.RGBA64Model:
75 src1 := image.NewRGBA64(image.Rect(0, 0, srcw, srch))
76 for y := 0; y < srch; y++ {
77 for x := 0; x < srcw; x++ {
78 src1.SetRGBA64(x, y, color.RGBA64{
79 uint16(103 * x % 0x8000),
80 uint16(101 * y % 0x8000),
81 uint16((101*x + 103*y) % 0x8000),
87 case color.NRGBAModel:
88 src1 := image.NewNRGBA(image.Rect(0, 0, srcw, srch))
89 for y := 0; y < srch; y++ {
90 for x := 0; x < srcw; x++ {
91 src1.SetNRGBA(x, y, color.NRGBA{
92 uint8(13 * x % 0x100),
93 uint8(11 * y % 0x100),
94 uint8((11*x + 13*y) % 0x100),
100 case ycbcr.YCbCrColorModel:
101 yy := make([]uint8, srcw*srch)
102 cb := make([]uint8, srcw*srch)
103 cr := make([]uint8, srcw*srch)
105 yy[i] = uint8(3 * i % 0x100)
106 cb[i] = uint8(5 * i % 0x100)
107 cr[i] = uint8(7 * i % 0x100)
115 SubsampleRatio: ycbcr.SubsampleRatio444,
116 Rect: image.Rect(0, 0, srcw, srch),
126 case color.AlphaModel:
127 mask1 := image.NewAlpha(image.Rect(0, 0, srcw, srch))
128 for y := 0; y < srch; y++ {
129 for x := 0; x < srcw; x++ {
130 a := uint8((23*x + 29*y) % 0x100)
131 // Glyph masks are typically mostly zero,
132 // so we only set a quarter of mask1's pixels.
134 mask1.SetAlpha(x, y, color.Alpha{a})
144 for i := 0; i < b.N; i++ {
145 // Scatter the destination rectangle to draw into.
146 x := 3 * i % (dstw - srcw)
147 y := 7 * i % (dsth - srch)
149 DrawMask(dst, dst.Bounds().Add(image.Point{x, y}), src, image.ZP, mask, image.ZP, op)
153 // The BenchmarkFoo functions exercise a drawFoo fast-path function in draw.go.
155 func BenchmarkFillOver(b *testing.B) {
156 bench(b, color.RGBAModel, nil, nil, Over)
159 func BenchmarkFillSrc(b *testing.B) {
160 bench(b, color.RGBAModel, nil, nil, Src)
163 func BenchmarkCopyOver(b *testing.B) {
164 bench(b, color.RGBAModel, color.RGBAModel, nil, Over)
167 func BenchmarkCopySrc(b *testing.B) {
168 bench(b, color.RGBAModel, color.RGBAModel, nil, Src)
171 func BenchmarkNRGBAOver(b *testing.B) {
172 bench(b, color.RGBAModel, color.NRGBAModel, nil, Over)
175 func BenchmarkNRGBASrc(b *testing.B) {
176 bench(b, color.RGBAModel, color.NRGBAModel, nil, Src)
179 func BenchmarkYCbCr(b *testing.B) {
180 bench(b, color.RGBAModel, ycbcr.YCbCrColorModel, nil, Over)
183 func BenchmarkGlyphOver(b *testing.B) {
184 bench(b, color.RGBAModel, nil, color.AlphaModel, Over)
187 func BenchmarkRGBA(b *testing.B) {
188 bench(b, color.RGBAModel, color.RGBA64Model, nil, Src)
191 // The BenchmarkGenericFoo functions exercise the generic, slow-path code.
193 func BenchmarkGenericOver(b *testing.B) {
194 bench(b, color.RGBA64Model, color.RGBA64Model, nil, Over)
197 func BenchmarkGenericMaskOver(b *testing.B) {
198 bench(b, color.RGBA64Model, color.RGBA64Model, color.AlphaModel, Over)
201 func BenchmarkGenericSrc(b *testing.B) {
202 bench(b, color.RGBA64Model, color.RGBA64Model, nil, Src)
205 func BenchmarkGenericMaskSrc(b *testing.B) {
206 bench(b, color.RGBA64Model, color.RGBA64Model, color.AlphaModel, Src)