-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathdraw.go
234 lines (222 loc) · 6.46 KB
/
draw.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package blend
import (
"image"
"image/draw"
"runtime"
"sync"
)
type Drawer interface {
draw.Drawer
DrawMask(dst draw.Image, r image.Rectangle, src image.Image, sp image.Point, mask image.Image, mp image.Point)
}
type drawer interface {
drawRGBAToRGBAUniform(dst *image.RGBA, r image.Rectangle, src *image.RGBA, sp image.Point, mask *image.Uniform)
drawNRGBAToRGBAUniform(dst *image.RGBA, r image.Rectangle, src *image.NRGBA, sp image.Point, mask *image.Uniform)
drawRGBAToNRGBAUniform(dst *image.NRGBA, r image.Rectangle, src *image.RGBA, sp image.Point, mask *image.Uniform)
drawNRGBAToNRGBAUniform(dst *image.NRGBA, r image.Rectangle, src *image.NRGBA, sp image.Point, mask *image.Uniform)
drawFallback(dst draw.Image, r image.Rectangle, src image.Image, sp image.Point, mask image.Image, mp image.Point)
}
type alphaDrawer interface {
drawAlphaToRGBAUniform(dst *image.RGBA, r image.Rectangle, src *image.Alpha, sp image.Point, mask *image.Uniform)
drawAlphaToNRGBAUniform(dst *image.NRGBA, r image.Rectangle, src *image.Alpha, sp image.Point, mask *image.Uniform)
}
type uniformDrawer interface {
drawUniformToRGBAUniform(dst *image.RGBA, r image.Rectangle, src *image.Uniform, sp image.Point, mask *image.Uniform)
drawUniformToNRGBAUniform(dst *image.NRGBA, r image.Rectangle, src *image.Uniform, sp image.Point, mask *image.Uniform)
}
type drawFunc func(dest []byte, src []byte, alpha uint32, d0 int, s0 int, y int, sx0 int, sx1 int, sxDelta int, syDelta int, dx0 int, dx1 int, dxDelta int, dyDelta int)
func (f drawFunc) Parallel(dest []byte, src []byte, alpha uint32, d0 int, s0 int, y int, sx0 int, sx1 int, sxDelta int, syDelta int, dx0 int, dx1 int, dxDelta int, dyDelta int) {
n := runtime.GOMAXPROCS(0)
for n > 1 && n<<1 > y {
n--
}
if n == 1 {
f(dest, src, alpha, d0, s0, y, sx0, sx1, sxDelta, syDelta, dx0, dx1, dxDelta, dyDelta)
return
}
var wg sync.WaitGroup
wg.Add(n)
step := y / n
for i := 0; i < n-1; i++ {
go func(d0, s0 int) {
defer wg.Done()
f(dest, src, alpha, d0, s0, step, sx0, sx1, sxDelta, syDelta, dx0, dx1, dxDelta, dyDelta)
}(d0, s0)
d0 += dyDelta * step
s0 += syDelta * step
y -= step
}
go func() {
defer wg.Done()
f(dest, src, alpha, d0, s0, y, sx0, sx1, sxDelta, syDelta, dx0, dx1, dxDelta, dyDelta)
}()
wg.Wait()
}
type drawUniformFunc func(dest []byte, sr, sg, sb, sa uint32, y int, dx0 int, dx1 int, dxDelta int, dyDelta int)
func (f drawUniformFunc) Parallel(dest []byte, sr, sg, sb, sa uint32, y int, dx0 int, dx1 int, dxDelta int, dyDelta int) {
n := runtime.GOMAXPROCS(0)
for n > 1 && n<<1 > y {
n--
}
if n == 1 {
f(dest, sr, sg, sb, sa, y, dx0, dx1, dxDelta, dyDelta)
return
}
var wg sync.WaitGroup
wg.Add(n)
step := y / n
for i := 1; i < n; i++ {
go func(d []byte) {
defer wg.Done()
f(d, sr, sg, sb, sa, step, dx0, dx1, dxDelta, dyDelta)
}(dest)
dest = dest[dyDelta*step:]
y -= step
}
go func() {
defer wg.Done()
f(dest, sr, sg, sb, sa, y, dx0, dx1, dxDelta, dyDelta)
}()
wg.Wait()
}
type drawFallbackFunc func(dst draw.Image, pX int, pY int, src image.Image, spX int, spY int, mask image.Image, mpX int, mpY int, endX int, endY int, dx int, dy int)
func (f drawFallbackFunc) Parallel(dst draw.Image, pX int, pY int, src image.Image, spX int, spY int, mask image.Image, mpX int, mpY int, endX int, endY int, dx int, dy int) {
var y int
if dy > 0 {
y = endY - pY
} else {
y = pY - endY
}
n := runtime.GOMAXPROCS(0)
for n > 1 && n<<1 > y {
n--
}
if n == 1 {
f(dst, pX, pY, src, spX, spY, mask, mpX, mpY, endX, endY, dx, dy)
return
}
var wg sync.WaitGroup
wg.Add(n)
step := (y / n) * dy
endY2 := pY + step
for i := 1; i < n; i++ {
go func(pY int, spY int, mpY int, endY2 int) {
defer wg.Done()
f(dst, pX, pY, src, spX, spY, mask, mpX, mpY, endX, endY2, dx, dy)
}(pY, spY, mpY, endY2)
pY += step
spY += step
mpY += step
endY2 += step
}
go func() {
defer wg.Done()
f(dst, pX, pY, src, spX, spY, mask, mpX, mpY, endX, endY, dx, dy)
}()
wg.Wait()
}
func drawMask(d drawer, dst draw.Image, r image.Rectangle, src image.Image, sp image.Point, mask image.Image, mp image.Point) {
clip(dst, &r, src, &sp, mask, &mp)
if r.Empty() {
return
}
switch src0 := src.(type) {
case *image.RGBA:
if mask == nil {
switch dst0 := dst.(type) {
case *image.RGBA:
d.drawRGBAToRGBAUniform(dst0, r, src0, sp, nil)
return
case *image.NRGBA:
d.drawRGBAToNRGBAUniform(dst0, r, src0, sp, nil)
return
}
} else {
switch mask0 := mask.(type) {
case *image.Uniform:
switch dst0 := dst.(type) {
case *image.RGBA:
d.drawRGBAToRGBAUniform(dst0, r, src0, sp, mask0)
return
case *image.NRGBA:
d.drawRGBAToNRGBAUniform(dst0, r, src0, sp, mask0)
return
}
}
}
case *image.NRGBA:
if mask == nil {
switch dst0 := dst.(type) {
case *image.RGBA:
d.drawNRGBAToRGBAUniform(dst0, r, src0, sp, nil)
return
case *image.NRGBA:
d.drawNRGBAToNRGBAUniform(dst0, r, src0, sp, nil)
return
}
} else {
switch mask0 := mask.(type) {
case *image.Uniform:
switch dst0 := dst.(type) {
case *image.RGBA:
d.drawNRGBAToRGBAUniform(dst0, r, src0, sp, mask0)
return
case *image.NRGBA:
d.drawNRGBAToNRGBAUniform(dst0, r, src0, sp, mask0)
return
}
}
}
case *image.Alpha:
if d, ok := d.(alphaDrawer); ok {
if mask == nil {
switch dst0 := dst.(type) {
case *image.RGBA:
d.drawAlphaToRGBAUniform(dst0, r, src0, sp, nil)
return
case *image.NRGBA:
d.drawAlphaToNRGBAUniform(dst0, r, src0, sp, nil)
return
}
} else {
switch mask0 := mask.(type) {
case *image.Uniform:
switch dst0 := dst.(type) {
case *image.RGBA:
d.drawAlphaToRGBAUniform(dst0, r, src0, sp, mask0)
return
case *image.NRGBA:
d.drawAlphaToNRGBAUniform(dst0, r, src0, sp, mask0)
return
}
}
}
}
case *image.Uniform:
if d, ok := d.(uniformDrawer); ok {
if mask == nil {
switch dst0 := dst.(type) {
case *image.RGBA:
d.drawUniformToRGBAUniform(dst0, r, src0, sp, nil)
return
case *image.NRGBA:
d.drawUniformToNRGBAUniform(dst0, r, src0, sp, nil)
return
}
} else {
switch mask0 := mask.(type) {
case *image.Uniform:
switch dst0 := dst.(type) {
case *image.RGBA:
d.drawUniformToRGBAUniform(dst0, r, src0, sp, mask0)
return
case *image.NRGBA:
d.drawUniformToNRGBAUniform(dst0, r, src0, sp, mask0)
return
}
}
}
}
}
d.drawFallback(dst, r, src, sp, mask, mp)
}