OSDN Git Service

2be91185af2e384c0a048216cc6a2de97faf0dd2
[pf3gnuchains/gcc-fork.git] / libgo / go / image / draw / bench_test.go
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.
4
5 package draw
6
7 import (
8         "image"
9         "image/color"
10         "image/ycbcr"
11         "testing"
12 )
13
14 const (
15         dstw, dsth = 640, 480
16         srcw, srch = 400, 300
17 )
18
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) {
23         b.StopTimer()
24
25         var dst Image
26         switch dcm {
27         case color.RGBAModel:
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{
32                                         uint8(5 * x % 0x100),
33                                         uint8(7 * y % 0x100),
34                                         uint8((7*x + 5*y) % 0x100),
35                                         0xff,
36                                 })
37                         }
38                 }
39                 dst = dst1
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),
48                                         0xffff,
49                                 })
50                         }
51                 }
52                 dst = dst1
53         default:
54                 panic("unreachable")
55         }
56
57         var src image.Image
58         switch scm {
59         case nil:
60                 src = &image.Uniform{color.RGBA{0x11, 0x22, 0x33, 0xff}}
61         case color.RGBAModel:
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{
66                                         uint8(13 * x % 0x80),
67                                         uint8(11 * y % 0x80),
68                                         uint8((11*x + 13*y) % 0x80),
69                                         0x7f,
70                                 })
71                         }
72                 }
73                 src = src1
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),
82                                         0x7fff,
83                                 })
84                         }
85                 }
86                 src = src1
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),
95                                         0x7f,
96                                 })
97                         }
98                 }
99                 src = src1
100         case ycbcr.YCbCrColorModel:
101                 yy := make([]uint8, srcw*srch)
102                 cb := make([]uint8, srcw*srch)
103                 cr := make([]uint8, srcw*srch)
104                 for i := range yy {
105                         yy[i] = uint8(3 * i % 0x100)
106                         cb[i] = uint8(5 * i % 0x100)
107                         cr[i] = uint8(7 * i % 0x100)
108                 }
109                 src = &ycbcr.YCbCr{
110                         Y:              yy,
111                         Cb:             cb,
112                         Cr:             cr,
113                         YStride:        srcw,
114                         CStride:        srcw,
115                         SubsampleRatio: ycbcr.SubsampleRatio444,
116                         Rect:           image.Rect(0, 0, srcw, srch),
117                 }
118         default:
119                 panic("unreachable")
120         }
121
122         var mask image.Image
123         switch mcm {
124         case nil:
125                 // No-op.
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.
133                                 if a >= 0xc0 {
134                                         mask1.SetAlpha(x, y, color.Alpha{a})
135                                 }
136                         }
137                 }
138                 mask = mask1
139         default:
140                 panic("unreachable")
141         }
142
143         b.StartTimer()
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)
148
149                 DrawMask(dst, dst.Bounds().Add(image.Point{x, y}), src, image.ZP, mask, image.ZP, op)
150         }
151 }
152
153 // The BenchmarkFoo functions exercise a drawFoo fast-path function in draw.go.
154
155 func BenchmarkFillOver(b *testing.B) {
156         bench(b, color.RGBAModel, nil, nil, Over)
157 }
158
159 func BenchmarkFillSrc(b *testing.B) {
160         bench(b, color.RGBAModel, nil, nil, Src)
161 }
162
163 func BenchmarkCopyOver(b *testing.B) {
164         bench(b, color.RGBAModel, color.RGBAModel, nil, Over)
165 }
166
167 func BenchmarkCopySrc(b *testing.B) {
168         bench(b, color.RGBAModel, color.RGBAModel, nil, Src)
169 }
170
171 func BenchmarkNRGBAOver(b *testing.B) {
172         bench(b, color.RGBAModel, color.NRGBAModel, nil, Over)
173 }
174
175 func BenchmarkNRGBASrc(b *testing.B) {
176         bench(b, color.RGBAModel, color.NRGBAModel, nil, Src)
177 }
178
179 func BenchmarkYCbCr(b *testing.B) {
180         bench(b, color.RGBAModel, ycbcr.YCbCrColorModel, nil, Over)
181 }
182
183 func BenchmarkGlyphOver(b *testing.B) {
184         bench(b, color.RGBAModel, nil, color.AlphaModel, Over)
185 }
186
187 func BenchmarkRGBA(b *testing.B) {
188         bench(b, color.RGBAModel, color.RGBA64Model, nil, Src)
189 }
190
191 // The BenchmarkGenericFoo functions exercise the generic, slow-path code.
192
193 func BenchmarkGenericOver(b *testing.B) {
194         bench(b, color.RGBA64Model, color.RGBA64Model, nil, Over)
195 }
196
197 func BenchmarkGenericMaskOver(b *testing.B) {
198         bench(b, color.RGBA64Model, color.RGBA64Model, color.AlphaModel, Over)
199 }
200
201 func BenchmarkGenericSrc(b *testing.B) {
202         bench(b, color.RGBA64Model, color.RGBA64Model, nil, Src)
203 }
204
205 func BenchmarkGenericMaskSrc(b *testing.B) {
206         bench(b, color.RGBA64Model, color.RGBA64Model, color.AlphaModel, Src)
207 }