Debian Codegraph @

src/core/SkMaskBlurFilter.cpp

raw
1 /*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "src/core/SkMaskBlurFilter.h"
9
10 #include "include/private/base/SkFloatingPoint.h"
11 #include "include/private/base/SkMalloc.h"
12 #include "include/private/base/SkTPin.h"
13 #include "include/private/base/SkTemplates.h"
14 #include "include/private/base/SkTo.h"
15 #include "src/base/SkArenaAlloc.h"
16 #include "src/base/SkVx.h"
17 #include "src/core/SkColorPriv.h"
18 #include "src/core/SkGaussFilter.h"
19
20 #include <cmath>
21 #include <climits>
22
23 namespace {
24
25 class PlanGauss final {
26 public:
27 explicit PlanGauss(double sigma) {
28 auto possibleWindow = static_cast<int>(floor(sigma * 3 * sqrt(2 * SK_DoublePI) / 4 + 0.5));
29 auto window = std::max(1, possibleWindow);
30
31 fPass0Size = window - 1;
32 fPass1Size = window - 1;
33 fPass2Size = (window & 1) == 1 ? window - 1 : window;
34
35 // Calculating the border is tricky. I will go through the odd case which is simpler, and
36 // then through the even case. Given a stack of filters seven wide for the odd case of
37 // three passes.
38 //
39 // S
40 // aaaAaaa
41 // bbbBbbb
42 // cccCccc
43 // D
44 //
45 // The furthest changed pixel is when the filters are in the following configuration.
46 //
47 // S
48 // aaaAaaa
49 // bbbBbbb
50 // cccCccc
51 // D
52 //
53 // The A pixel is calculated using the value S, the B uses A, and the C uses B, and
54 // finally D is C. So, with a window size of seven the border is nine. In general, the
55 // border is 3*((window - 1)/2).
56 //
57 // For even cases the filter stack is more complicated. The spec specifies two passes
58 // of even filters and a final pass of odd filters. A stack for a width of six looks like
59 // this.
60 //
61 // S
62 // aaaAaa
63 // bbBbbb
64 // cccCccc
65 // D
66 //
67 // The furthest pixel looks like this.
68 //
69 // S
70 // aaaAaa
71 // bbBbbb
72 // cccCccc
73 // D
74 //
75 // For a window of size, the border value is seven. In general the border is 3 *
76 // (window/2) -1.
77 fBorder = (window & 1) == 1 ? 3 * ((window - 1) / 2) : 3 * (window / 2) - 1;
78 fSlidingWindow = 2 * fBorder + 1;
79
80 // If the window is odd then the divisor is just window ^ 3 otherwise,
81 // it is window * window * (window + 1) = window ^ 2 + window ^ 3;
82 auto window2 = window * window;
83 auto window3 = window2 * window;
84 auto divisor = (window & 1) == 1 ? window3 : window3 + window2;
85
86 fWeight = static_cast<uint64_t>(round(1.0 / divisor * (1ull << 32)));
87 }
88
89 size_t bufferSize() const { return fPass0Size + fPass1Size + fPass2Size; }
90
91 int border() const { return fBorder; }
92
93 public:
94 class Scan {
95 public:
96 Scan(uint64_t weight, int noChangeCount,
97 uint32_t* buffer0, uint32_t* buffer0End,
98 uint32_t* buffer1, uint32_t* buffer1End,
99 uint32_t* buffer2, uint32_t* buffer2End)
100 : fWeight{weight}
101 , fNoChangeCount{noChangeCount}
102 , fBuffer0{buffer0}
103 , fBuffer0End{buffer0End}
104 , fBuffer1{buffer1}
105 , fBuffer1End{buffer1End}
106 , fBuffer2{buffer2}
107 , fBuffer2End{buffer2End}
108 { }
109
110 template <typename AlphaIter> void blur(const AlphaIter srcBegin, const AlphaIter srcEnd,
111 uint8_t* dst, int dstStride, uint8_t* dstEnd) const {
112 auto buffer0Cursor = fBuffer0;
113 auto buffer1Cursor = fBuffer1;
114 auto buffer2Cursor = fBuffer2;
115
116 std::memset(fBuffer0, 0x00, (fBuffer2End - fBuffer0) * sizeof(*fBuffer0));
117
118 uint32_t sum0 = 0;
119 uint32_t sum1 = 0;
120 uint32_t sum2 = 0;
121
122 // Consume the source generating pixels.
123 for (AlphaIter src = srcBegin; src < srcEnd; ++src, dst += dstStride) {
124 uint32_t leadingEdge = *src;
125 sum0 += leadingEdge;
126 sum1 += sum0;
127 sum2 += sum1;
128
129 *dst = this->finalScale(sum2);
130
131 sum2 -= *buffer2Cursor;
132 *buffer2Cursor = sum1;
133 buffer2Cursor = (buffer2Cursor + 1) < fBuffer2End ? buffer2Cursor + 1 : fBuffer2;
134
135 sum1 -= *buffer1Cursor;
136 *buffer1Cursor = sum0;
137 buffer1Cursor = (buffer1Cursor + 1) < fBuffer1End ? buffer1Cursor + 1 : fBuffer1;
138
139 sum0 -= *buffer0Cursor;
140 *buffer0Cursor = leadingEdge;
141 buffer0Cursor = (buffer0Cursor + 1) < fBuffer0End ? buffer0Cursor + 1 : fBuffer0;
142 }
143
144 // The leading edge is off the right side of the mask.
145 for (int i = 0; i < fNoChangeCount; i++) {
146 uint32_t leadingEdge = 0;
147 sum0 += leadingEdge;
148 sum1 += sum0;
149 sum2 += sum1;
150
151 *dst = this->finalScale(sum2);
152
153 sum2 -= *buffer2Cursor;
154 *buffer2Cursor = sum1;
155 buffer2Cursor = (buffer2Cursor + 1) < fBuffer2End ? buffer2Cursor + 1 : fBuffer2;
156
157 sum1 -= *buffer1Cursor;
158 *buffer1Cursor = sum0;
159 buffer1Cursor = (buffer1Cursor + 1) < fBuffer1End ? buffer1Cursor + 1 : fBuffer1;
160
161 sum0 -= *buffer0Cursor;
162 *buffer0Cursor = leadingEdge;
163 buffer0Cursor = (buffer0Cursor + 1) < fBuffer0End ? buffer0Cursor + 1 : fBuffer0;
164
165 dst += dstStride;
166 }
167
168 // Starting from the right, fill in the rest of the buffer.
169 std::memset(fBuffer0, 0, (fBuffer2End - fBuffer0) * sizeof(*fBuffer0));
170
171 sum0 = sum1 = sum2 = 0;
172
173 uint8_t* dstCursor = dstEnd;
174 AlphaIter src = srcEnd;
175 while (dstCursor > dst) {
176 dstCursor -= dstStride;
177 uint32_t leadingEdge = *(--src);
178 sum0 += leadingEdge;
179 sum1 += sum0;
180 sum2 += sum1;
181
182 *dstCursor = this->finalScale(sum2);
183
184 sum2 -= *buffer2Cursor;
185 *buffer2Cursor = sum1;
186 buffer2Cursor = (buffer2Cursor + 1) < fBuffer2End ? buffer2Cursor + 1 : fBuffer2;
187
188 sum1 -= *buffer1Cursor;
189 *buffer1Cursor = sum0;
190 buffer1Cursor = (buffer1Cursor + 1) < fBuffer1End ? buffer1Cursor + 1 : fBuffer1;
191
192 sum0 -= *buffer0Cursor;
193 *buffer0Cursor = leadingEdge;
194 buffer0Cursor = (buffer0Cursor + 1) < fBuffer0End ? buffer0Cursor + 1 : fBuffer0;
195 }
196 }
197
198 private:
199 inline static constexpr uint64_t kHalf = static_cast<uint64_t>(1) << 31;
200
201 uint8_t finalScale(uint32_t sum) const {
202 return SkTo<uint8_t>((fWeight * sum + kHalf) >> 32);
203 }
204
205 uint64_t fWeight;
206 int fNoChangeCount;
207 uint32_t* fBuffer0;
208 uint32_t* fBuffer0End;
209 uint32_t* fBuffer1;
210 uint32_t* fBuffer1End;
211 uint32_t* fBuffer2;
212 uint32_t* fBuffer2End;
213 };
214
215 Scan makeBlurScan(int width, uint32_t* buffer) const {
216 uint32_t* buffer0, *buffer0End, *buffer1, *buffer1End, *buffer2, *buffer2End;
217 buffer0 = buffer;
218 buffer0End = buffer1 = buffer0 + fPass0Size;
219 buffer1End = buffer2 = buffer1 + fPass1Size;
220 buffer2End = buffer2 + fPass2Size;
221 int noChangeCount = fSlidingWindow > width ? fSlidingWindow - width : 0;
222
223 return Scan(
224 fWeight, noChangeCount,
225 buffer0, buffer0End,
226 buffer1, buffer1End,
227 buffer2, buffer2End);
228 }
229
230 uint64_t fWeight;
231 int fBorder;
232 int fSlidingWindow;
233 int fPass0Size;
234 int fPass1Size;
235 int fPass2Size;
236 };
237
238 } // namespace
239
240 // NB 135 is the largest sigma that will not cause a buffer full of 255 mask values to overflow
241 // using the Gauss filter. It also limits the size of buffers used hold intermediate values. The
242 // additional + 1 added to window represents adding one more leading element before subtracting the
243 // trailing element.
244 // Explanation of maximums:
245 // sum0 = (window + 1) * 255
246 // sum1 = (window + 1) * sum0 -> (window + 1) * (window + 1) * 255
247 // sum2 = (window + 1) * sum1 -> (window + 1) * (window + 1) * (window + 1) * 255 -> window^3 * 255
248 //
249 // The value (window + 1)^3 * 255 must fit in a uint32_t. So,
250 // (window + 1)^3 * 255 < 2^32. window = 255.
251 //
252 // window = floor(sigma * 3 * sqrt(2 * kPi) / 4)
253 // For window <= 255, the largest value for sigma is 135.
254 SkMaskBlurFilter::SkMaskBlurFilter(double sigmaW, double sigmaH)
255 : fSigmaW{SkTPin(sigmaW, 0.0, 135.0)}
256 , fSigmaH{SkTPin(sigmaH, 0.0, 135.0)}
257 {
258 SkASSERT(sigmaW >= 0);
259 SkASSERT(sigmaH >= 0);
260 }
261
262 bool SkMaskBlurFilter::hasNoBlur() const {
263 // If the sigma value is less than a certain amount, the window will be 0 which means
264 // there is effectively no blur. Using Wolfram alpha to solve the equation used for
265 // possibleWindow above shows that the threshold is (2 * sqrt(2/pi))/3
266 #if defined(SK_USE_LARGER_NO_BLUR_THRESHOLD)
267 constexpr double kNoWindowSigma = 0.531923;
268 #else
269 // However, historically we used 1/3 as the cutoff. Clients who might have pixel tests
270 // that depend on this can be updated one at a time.
271 constexpr double kNoWindowSigma = 1./3.;
272 #endif
273 return fSigmaW < kNoWindowSigma && fSigmaH <= kNoWindowSigma;
274 }
275
276 // We favor A8 masks, and if we need to work with another format, we'll convert to A8 first.
277 // Each of these converts width (up to 8) mask values to A8.
278 static void bw_to_a8(uint8_t* a8, const uint8_t* from, int width) {
279 SkASSERT(0 < width && width <= 8);
280
281 uint8_t masks = *from;
282 for (int i = 0; i < width; ++i) {
283 a8[i] = (masks >> (7 - i)) & 1 ? 0xFF
284 : 0x00;
285 }
286 }
287 static void lcd_to_a8(uint8_t* a8, const uint8_t* from, int width) {
288 SkASSERT(0 < width && width <= 8);
289
290 for (int i = 0; i < width; ++i) {
291 unsigned rgb = reinterpret_cast<const uint16_t*>(from)[i],
292 r = SkPacked16ToR32(rgb),
293 g = SkPacked16ToG32(rgb),
294 b = SkPacked16ToB32(rgb);
295 a8[i] = (r + g + b) / 3;
296 }
297 }
298 static void argb32_to_a8(uint8_t* a8, const uint8_t* from, int width) {
299 SkASSERT(0 < width && width <= 8);
300 for (int i = 0; i < width; ++i) {
301 uint32_t rgba = reinterpret_cast<const uint32_t*>(from)[i];
302 a8[i] = SkGetPackedA32(rgba);
303 }
304 }
305 using ToA8 = decltype(bw_to_a8);
306
307 using fp88 = skvx::Vec<8, uint16_t>; // 8-wide fixed point 8.8
308
309 static fp88 load(const uint8_t* from, int width, ToA8* toA8) {
310 // Our fast path is a full 8-byte load of A8.
311 // So we'll conditionally handle the two slow paths using tmp:
312 // - if we have a function to convert another mask to A8, use it;
313 // - if not but we have less than 8 bytes to load, load them one at a time.
314 uint8_t tmp[8] = {0,0,0,0, 0,0,0,0};
315 if (toA8) {
316 toA8(tmp, from, width);
317 from = tmp;
318 } else if (width < 8) {
319 for (int i = 0; i < width; ++i) {
320 tmp[i] = from[i];
321 }
322 from = tmp;
323 }
324
325 // Load A8 and convert to 8.8 fixed-point.
326 return skvx::cast<uint16_t>(skvx::byte8::Load(from)) << 8;
327 }
328
329 static void store(uint8_t* to, const fp88& v, int width) {
330 skvx::byte8 b = skvx::cast<uint8_t>(v >> 8);
331 if (width == 8) {
332 b.store(to);
333 } else {
334 uint8_t buffer[8];
335 b.store(buffer);
336 for (int i = 0; i < width; i++) {
337 to[i] = buffer[i];
338 }
339 }
340 }
341
342 static constexpr uint16_t _____ = 0u;
343 static constexpr uint16_t kHalf = 0x80u;
344
345 // In all the blur_x_radius_N and blur_y_radius_N functions the gaussian values are encoded
346 // in 0.16 format, none of the values is greater than one. The incoming mask values are in 8.8
347 // format. The resulting multiply has a 8.24 format, by the mulhi truncates the lower 16 bits
348 // resulting in a 8.8 format.
349 //
350 // The blur_x_radius_N function below blur along a row of pixels using a kernel with radius N. This
351 // system is setup to minimize the number of multiplies needed.
352 //
353 // Explanation:
354 // Blurring a specific mask value is given by the following equation where D_n is the resulting
355 // mask value and S_n is the source value. The example below is for a filter with a radius of 1
356 // and a width of 3 (radius == (width-1)/2). The indexes for the source and destination are
357 // aligned. The filter is given by G_n where n is the symmetric filter value.
358 //
359 // D[n] = S[n-1]*G[1] + S[n]*G[0] + S[n+1]*G[1].
360 //
361 // We can start the source index at an offset relative to the destination separated by the
362 // radius. This results in a non-traditional restating of the above filter.
363 //
364 // D[n] = S[n]*G[1] + S[n+1]*G[0] + S[n+2]*G[1]
365 //
366 // If we look at three specific consecutive destinations the following equations result:
367 //
368 // D[5] = S[5]*G[1] + S[6]*G[0] + S[7]*G[1]
369 // D[7] = S[6]*G[1] + S[7]*G[0] + S[8]*G[1]
370 // D[8] = S[7]*G[1] + S[8]*G[0] + S[9]*G[1].
371 //
372 // In the above equations, notice that S[7] is used in all three. In particular, two values are
373 // used: S[7]*G[0] and S[7]*G[1]. So, S[7] is only multiplied twice, but used in D[5], D[6] and
374 // D[7].
375 //
376 // From the point of view of a source value we end up with the following three equations.
377 //
378 // Given S[7]:
379 // D[5] += S[7]*G[1]
380 // D[6] += S[7]*G[0]
381 // D[7] += S[7]*G[1]
382 //
383 // In General:
384 // D[n] += S[n]*G[1]
385 // D[n+1] += S[n]*G[0]
386 // D[n+2] += S[n]*G[1]
387 //
388 // Now these equations can be ganged using SIMD to form:
389 // D[n..n+7] += S[n..n+7]*G[1]
390 // D[n+1..n+8] += S[n..n+7]*G[0]
391 // D[n+2..n+9] += S[n..n+7]*G[1]
392 // The next set of values becomes.
393 // D[n+8..n+15] += S[n+8..n+15]*G[1]
394 // D[n+9..n+16] += S[n+8..n+15]*G[0]
395 // D[n+10..n+17] += S[n+8..n+15]*G[1]
396 // You can see that the D[n+8] and D[n+9] values overlap the two sets, using parts of both
397 // S[n..7] and S[n+8..n+15].
398 //
399 // Just one more transformation allows the code to maintain all working values in
400 // registers. I introduce the notation {0, S[n..n+7] * G[k]} to mean that the value where 0 is
401 // prepended to the array of values to form {0, S[n] * G[k], ..., S[n+7]*G[k]}.
402 //
403 // D[n..n+7] += S[n..n+7] * G[1]
404 // D[n..n+8] += {0, S[n..n+7] * G[0]}
405 // D[n..n+9] += {0, 0, S[n..n+7] * G[1]}
406 //
407 // Now we can encode D[n..n+7] in a single Sk8h register called d0, and D[n+8..n+15] in a
408 // register d8. In addition, S[0..n+7] becomes s0.
409 //
410 // The translation of the {0, S[n..n+7] * G[k]} is translated in the following way below.
411 //
412 // Sk8h v0 = s0*G[0]
413 // Sk8h v1 = s0*G[1]
414 // /* D[n..n+7] += S[n..n+7] * G[1] */
415 // d0 += v1;
416 // /* D[n..n+8] += {0, S[n..n+7] * G[0]} */
417 // d0 += {_____, v0[0], v0[1], v0[2], v0[3], v0[4], v0[5], v0[6]}
418 // d1 += {v0[7], _____, _____, _____, _____, _____, _____, _____}
419 // /* D[n..n+9] += {0, 0, S[n..n+7] * G[1]} */
420 // d0 += {_____, _____, v1[0], v1[1], v1[2], v1[3], v1[4], v1[5]}
421 // d1 += {v1[6], v1[7], _____, _____, _____, _____, _____, _____}
422 // Where we rely on the compiler to generate efficient code for the {____, n, ....} notation.
423
424 static void blur_x_radius_1(
425 const fp88& s0,
426 const fp88& g0, const fp88& g1, const fp88&, const fp88&, const fp88&,
427 fp88* d0, fp88* d8) {
428
429 auto v1 = mulhi(s0, g1);
430 auto v0 = mulhi(s0, g0);
431
432 // D[n..n+7] += S[n..n+7] * G[1]
433 *d0 += v1;
434
435 //D[n..n+8] += {0, S[n..n+7] * G[0]}
436 *d0 += fp88{_____, v0[0], v0[1], v0[2], v0[3], v0[4], v0[5], v0[6]};
437 *d8 += fp88{v0[7], _____, _____, _____, _____, _____, _____, _____};
438
439 // D[n..n+9] += {0, 0, S[n..n+7] * G[1]}
440 *d0 += fp88{_____, _____, v1[0], v1[1], v1[2], v1[3], v1[4], v1[5]};
441 *d8 += fp88{v1[6], v1[7], _____, _____, _____, _____, _____, _____};
442
443 }
444
445 static void blur_x_radius_2(
446 const fp88& s0,
447 const fp88& g0, const fp88& g1, const fp88& g2, const fp88&, const fp88&,
448 fp88* d0, fp88* d8) {
449 auto v0 = mulhi(s0, g0);
450 auto v1 = mulhi(s0, g1);
451 auto v2 = mulhi(s0, g2);
452
453 // D[n..n+7] += S[n..n+7] * G[2]
454 *d0 += v2;
455
456 // D[n..n+8] += {0, S[n..n+7] * G[1]}
457 *d0 += fp88{_____, v1[0], v1[1], v1[2], v1[3], v1[4], v1[5], v1[6]};
458 *d8 += fp88{v1[7], _____, _____, _____, _____, _____, _____, _____};
459
460 // D[n..n+9] += {0, 0, S[n..n+7] * G[0]}
461 *d0 += fp88{_____, _____, v0[0], v0[1], v0[2], v0[3], v0[4], v0[5]};
462 *d8 += fp88{v0[6], v0[7], _____, _____, _____, _____, _____, _____};
463
464 // D[n..n+10] += {0, 0, 0, S[n..n+7] * G[1]}
465 *d0 += fp88{_____, _____, _____, v1[0], v1[1], v1[2], v1[3], v1[4]};
466 *d8 += fp88{v1[5], v1[6], v1[7], _____, _____, _____, _____, _____};
467
468 // D[n..n+11] += {0, 0, 0, 0, S[n..n+7] * G[2]}
469 *d0 += fp88{_____, _____, _____, _____, v2[0], v2[1], v2[2], v2[3]};
470 *d8 += fp88{v2[4], v2[5], v2[6], v2[7], _____, _____, _____, _____};
471 }
472
473 static void blur_x_radius_3(
474 const fp88& s0,
475 const fp88& g0, const fp88& g1, const fp88& g2, const fp88& g3, const fp88&,
476 fp88* d0, fp88* d8) {
477 auto v0 = mulhi(s0, g0);
478 auto v1 = mulhi(s0, g1);
479 auto v2 = mulhi(s0, g2);
480 auto v3 = mulhi(s0, g3);
481
482 // D[n..n+7] += S[n..n+7] * G[3]
483 *d0 += v3;
484
485 // D[n..n+8] += {0, S[n..n+7] * G[2]}
486 *d0 += fp88{_____, v2[0], v2[1], v2[2], v2[3], v2[4], v2[5], v2[6]};
487 *d8 += fp88{v2[7], _____, _____, _____, _____, _____, _____, _____};
488
489 // D[n..n+9] += {0, 0, S[n..n+7] * G[1]}
490 *d0 += fp88{_____, _____, v1[0], v1[1], v1[2], v1[3], v1[4], v1[5]};
491 *d8 += fp88{v1[6], v1[7], _____, _____, _____, _____, _____, _____};
492
493 // D[n..n+10] += {0, 0, 0, S[n..n+7] * G[0]}
494 *d0 += fp88{_____, _____, _____, v0[0], v0[1], v0[2], v0[3], v0[4]};
495 *d8 += fp88{v0[5], v0[6], v0[7], _____, _____, _____, _____, _____};
496
497 // D[n..n+11] += {0, 0, 0, 0, S[n..n+7] * G[1]}
498 *d0 += fp88{_____, _____, _____, _____, v1[0], v1[1], v1[2], v1[3]};
499 *d8 += fp88{v1[4], v1[5], v1[6], v1[7], _____, _____, _____, _____};
500
501 // D[n..n+12] += {0, 0, 0, 0, 0, S[n..n+7] * G[2]}
502 *d0 += fp88{_____, _____, _____, _____, _____, v2[0], v2[1], v2[2]};
503 *d8 += fp88{v2[3], v2[4], v2[5], v2[6], v2[7], _____, _____, _____};
504
505 // D[n..n+13] += {0, 0, 0, 0, 0, 0, S[n..n+7] * G[3]}
506 *d0 += fp88{_____, _____, _____, _____, _____, _____, v3[0], v3[1]};
507 *d8 += fp88{v3[2], v3[3], v3[4], v3[5], v3[6], v3[7], _____, _____};
508 }
509
510 static void blur_x_radius_4(
511 const fp88& s0,
512 const fp88& g0, const fp88& g1, const fp88& g2, const fp88& g3, const fp88& g4,
513 fp88* d0, fp88* d8) {
514 auto v0 = mulhi(s0, g0);
515 auto v1 = mulhi(s0, g1);
516 auto v2 = mulhi(s0, g2);
517 auto v3 = mulhi(s0, g3);
518 auto v4 = mulhi(s0, g4);
519
520 // D[n..n+7] += S[n..n+7] * G[4]
521 *d0 += v4;
522
523 // D[n..n+8] += {0, S[n..n+7] * G[3]}
524 *d0 += fp88{_____, v3[0], v3[1], v3[2], v3[3], v3[4], v3[5], v3[6]};
525 *d8 += fp88{v3[7], _____, _____, _____, _____, _____, _____, _____};
526
527 // D[n..n+9] += {0, 0, S[n..n+7] * G[2]}
528 *d0 += fp88{_____, _____, v2[0], v2[1], v2[2], v2[3], v2[4], v2[5]};
529 *d8 += fp88{v2[6], v2[7], _____, _____, _____, _____, _____, _____};
530
531 // D[n..n+10] += {0, 0, 0, S[n..n+7] * G[1]}
532 *d0 += fp88{_____, _____, _____, v1[0], v1[1], v1[2], v1[3], v1[4]};
533 *d8 += fp88{v1[5], v1[6], v1[7], _____, _____, _____, _____, _____};
534
535 // D[n..n+11] += {0, 0, 0, 0, S[n..n+7] * G[0]}
536 *d0 += fp88{_____, _____, _____, _____, v0[0], v0[1], v0[2], v0[3]};
537 *d8 += fp88{v0[4], v0[5], v0[6], v0[7], _____, _____, _____, _____};
538
539 // D[n..n+12] += {0, 0, 0, 0, 0, S[n..n+7] * G[1]}
540 *d0 += fp88{_____, _____, _____, _____, _____, v1[0], v1[1], v1[2]};
541 *d8 += fp88{v1[3], v1[4], v1[5], v1[6], v1[7], _____, _____, _____};
542
543 // D[n..n+13] += {0, 0, 0, 0, 0, 0, S[n..n+7] * G[2]}
544 *d0 += fp88{_____, _____, _____, _____, _____, _____, v2[0], v2[1]};
545 *d8 += fp88{v2[2], v2[3], v2[4], v2[5], v2[6], v2[7], _____, _____};
546
547 // D[n..n+14] += {0, 0, 0, 0, 0, 0, 0, S[n..n+7] * G[3]}
548 *d0 += fp88{_____, _____, _____, _____, _____, _____, _____, v3[0]};
549 *d8 += fp88{v3[1], v3[2], v3[3], v3[4], v3[5], v3[6], v3[7], _____};
550
551 // D[n..n+15] += {0, 0, 0, 0, 0, 0, 0, 0, S[n..n+7] * G[4]}
552 *d8 += v4;
553 }
554
555 using BlurX = decltype(blur_x_radius_1);
556
557 // BlurX will only be one of the functions blur_x_radius_(1|2|3|4).
558 static void blur_row(
559 BlurX blur,
560 const fp88& g0, const fp88& g1, const fp88& g2, const fp88& g3, const fp88& g4,
561 const uint8_t* src, int srcW,
562 uint8_t* dst, int dstW) {
563 // Clear the buffer to handle summing wider than source.
564 fp88 d0(kHalf), d8(kHalf);
565
566 // Go by multiples of 8 in src.
567 int x = 0;
568 for (; x <= srcW - 8; x += 8) {
569 blur(load(src, 8, nullptr), g0, g1, g2, g3, g4, &d0, &d8);
570
571 store(dst, d0, 8);
572
573 d0 = d8;
574 d8 = fp88(kHalf);
575
576 src += 8;
577 dst += 8;
578 }
579
580 // There are src values left, but the remainder of src values is not a multiple of 8.
581 int srcTail = srcW - x;
582 if (srcTail > 0) {
583
584 blur(load(src, srcTail, nullptr), g0, g1, g2, g3, g4, &d0, &d8);
585
586 int dstTail = std::min(8, dstW - x);
587 store(dst, d0, dstTail);
588
589 d0 = d8;
590 dst += dstTail;
591 x += dstTail;
592 }
593
594 // There are dst mask values to complete.
595 int dstTail = dstW - x;
596 if (dstTail > 0) {
597 store(dst, d0, dstTail);
598 }
599 }
600
601 // BlurX will only be one of the functions blur_x_radius_(1|2|3|4).
602 static void blur_x_rect(BlurX blur,
603 uint16_t* gauss,
604 const uint8_t* src, size_t srcStride, int srcW,
605 uint8_t* dst, size_t dstStride, int dstW, int dstH) {
606
607 fp88 g0(gauss[0]),
608 g1(gauss[1]),
609 g2(gauss[2]),
610 g3(gauss[3]),
611 g4(gauss[4]);
612
613 // Blur *ALL* the rows.
614 for (int y = 0; y < dstH; y++) {
615 blur_row(blur, g0, g1, g2, g3, g4, src, srcW, dst, dstW);
616 src += srcStride;
617 dst += dstStride;
618 }
619 }
620
621 static void direct_blur_x(int radius, uint16_t* gauss,
622 const uint8_t* src, size_t srcStride, int srcW,
623 uint8_t* dst, size_t dstStride, int dstW, int dstH) {
624
625 switch (radius) {
626 case 1:
627 blur_x_rect(blur_x_radius_1, gauss, src, srcStride, srcW, dst, dstStride, dstW, dstH);
628 break;
629
630 case 2:
631 blur_x_rect(blur_x_radius_2, gauss, src, srcStride, srcW, dst, dstStride, dstW, dstH);
632 break;
633
634 case 3:
635 blur_x_rect(blur_x_radius_3, gauss, src, srcStride, srcW, dst, dstStride, dstW, dstH);
636 break;
637
638 case 4:
639 blur_x_rect(blur_x_radius_4, gauss, src, srcStride, srcW, dst, dstStride, dstW, dstH);
640 break;
641
642 default:
643 SkASSERTF(false, "The radius %d is not handled\n", radius);
644 }
645 }
646
647 // The operations of the blur_y_radius_N functions work on a theme similar to the blur_x_radius_N
648 // functions, but end up being simpler because there is no complicated shift of registers. We
649 // start with the non-traditional form of the gaussian filter. In the following r is the value
650 // when added generates the next value in the column.
651 //
652 // D[n+0r] = S[n+0r]*G[1]
653 // + S[n+1r]*G[0]
654 // + S[n+2r]*G[1]
655 //
656 // Expanding out in a way similar to blur_x_radius_N for specific values of n.
657 //
658 // D[n+0r] = S[n-2r]*G[1] + S[n-1r]*G[0] + S[n+0r]*G[1]
659 // D[n+1r] = S[n-1r]*G[1] + S[n+0r]*G[0] + S[n+1r]*G[1]
660 // D[n+2r] = S[n+0r]*G[1] + S[n+1r]*G[0] + S[n+2r]*G[1]
661 //
662 // We can see that S[n+0r] is in all three D[] equations, but is only multiplied twice. Now we
663 // can look at the calculation form the point of view of a source value.
664 //
665 // Given S[n+0r]:
666 // D[n+0r] += S[n+0r]*G[1];
667 // /* D[n+0r] is done and can be stored now. */
668 // D[n+1r] += S[n+0r]*G[0];
669 // D[n+2r] = S[n+0r]*G[1];
670 //
671 // Remember, by induction, that D[n+0r] == S[n-2r]*G[1] + S[n-1r]*G[0] before adding in
672 // S[n+0r]*G[1]. So, after the addition D[n+0r] has finished calculation and can be stored. Also,
673 // notice that D[n+2r] is receiving its first value from S[n+0r]*G[1] and is not added in. Notice
674 // how values flow in the following two iterations in source.
675 //
676 // D[n+0r] += S[n+0r]*G[1]
677 // D[n+1r] += S[n+0r]*G[0]
678 // D[n+2r] = S[n+0r]*G[1]
679 // /* ------- */
680 // D[n+1r] += S[n+1r]*G[1]
681 // D[n+2r] += S[n+1r]*G[0]
682 // D[n+3r] = S[n+1r]*G[1]
683 //
684 // Instead of using memory we can introduce temporaries d01 and d12. The update step changes
685 // to the following.
686 //
687 // answer = d01 + S[n+0r]*G[1]
688 // d01 = d12 + S[n+0r]*G[0]
689 // d12 = S[n+0r]*G[1]
690 // return answer
691 //
692 // Finally, this can be ganged into SIMD style.
693 // answer[0..7] = d01[0..7] + S[n+0r..n+0r+7]*G[1]
694 // d01[0..7] = d12[0..7] + S[n+0r..n+0r+7]*G[0]
695 // d12[0..7] = S[n+0r..n+0r+7]*G[1]
696 // return answer[0..7]
697 static fp88 blur_y_radius_1(
698 const fp88& s0,
699 const fp88& g0, const fp88& g1, const fp88&, const fp88&, const fp88&,
700 fp88* d01, fp88* d12, fp88*, fp88*, fp88*, fp88*, fp88*, fp88*) {
701 auto v0 = mulhi(s0, g0);
702 auto v1 = mulhi(s0, g1);
703
704 fp88 answer = *d01 + v1;
705 *d01 = *d12 + v0;
706 *d12 = v1 + kHalf;
707
708 return answer;
709 }
710
711 static fp88 blur_y_radius_2(
712 const fp88& s0,
713 const fp88& g0, const fp88& g1, const fp88& g2, const fp88&, const fp88&,
714 fp88* d01, fp88* d12, fp88* d23, fp88* d34, fp88*, fp88*, fp88*, fp88*) {
715 auto v0 = mulhi(s0, g0);
716 auto v1 = mulhi(s0, g1);
717 auto v2 = mulhi(s0, g2);
718
719 fp88 answer = *d01 + v2;
720 *d01 = *d12 + v1;
721 *d12 = *d23 + v0;
722 *d23 = *d34 + v1;
723 *d34 = v2 + kHalf;
724
725 return answer;
726 }
727
728 static fp88 blur_y_radius_3(
729 const fp88& s0,
730 const fp88& g0, const fp88& g1, const fp88& g2, const fp88& g3, const fp88&,
731 fp88* d01, fp88* d12, fp88* d23, fp88* d34, fp88* d45, fp88* d56, fp88*, fp88*) {
732 auto v0 = mulhi(s0, g0);
733 auto v1 = mulhi(s0, g1);
734 auto v2 = mulhi(s0, g2);
735 auto v3 = mulhi(s0, g3);
736
737 fp88 answer = *d01 + v3;
738 *d01 = *d12 + v2;
739 *d12 = *d23 + v1;
740 *d23 = *d34 + v0;
741 *d34 = *d45 + v1;
742 *d45 = *d56 + v2;
743 *d56 = v3 + kHalf;
744
745 return answer;
746 }
747
748 static fp88 blur_y_radius_4(
749 const fp88& s0,
750 const fp88& g0, const fp88& g1, const fp88& g2, const fp88& g3, const fp88& g4,
751 fp88* d01, fp88* d12, fp88* d23, fp88* d34, fp88* d45, fp88* d56, fp88* d67, fp88* d78) {
752 auto v0 = mulhi(s0, g0);
753 auto v1 = mulhi(s0, g1);
754 auto v2 = mulhi(s0, g2);
755 auto v3 = mulhi(s0, g3);
756 auto v4 = mulhi(s0, g4);
757
758 fp88 answer = *d01 + v4;
759 *d01 = *d12 + v3;
760 *d12 = *d23 + v2;
761 *d23 = *d34 + v1;
762 *d34 = *d45 + v0;
763 *d45 = *d56 + v1;
764 *d56 = *d67 + v2;
765 *d67 = *d78 + v3;
766 *d78 = v4 + kHalf;
767
768 return answer;
769 }
770
771 using BlurY = decltype(blur_y_radius_1);
772
773 // BlurY will be one of blur_y_radius_(1|2|3|4).
774 static void blur_column(
775 ToA8 toA8,
776 BlurY blur, int radius, int width,
777 const fp88& g0, const fp88& g1, const fp88& g2, const fp88& g3, const fp88& g4,
778 const uint8_t* src, size_t srcRB, int srcH,
779 uint8_t* dst, size_t dstRB) {
780 fp88 d01(kHalf), d12(kHalf), d23(kHalf), d34(kHalf),
781 d45(kHalf), d56(kHalf), d67(kHalf), d78(kHalf);
782
783 auto flush = [&](uint8_t* to, const fp88& v0, const fp88& v1) {
784 store(to, v0, width);
785 to += dstRB;
786 store(to, v1, width);
787 return to + dstRB;
788 };
789
790 for (int y = 0; y < srcH; y += 1) {
791 auto s = load(src, width, toA8);
792 auto b = blur(s,
793 g0, g1, g2, g3, g4,
794 &d01, &d12, &d23, &d34, &d45, &d56, &d67, &d78);
795 store(dst, b, width);
796 src += srcRB;
797 dst += dstRB;
798 }
799
800 if (radius >= 1) {
801 dst = flush(dst, d01, d12);
802 }
803 if (radius >= 2) {
804 dst = flush(dst, d23, d34);
805 }
806 if (radius >= 3) {
807 dst = flush(dst, d45, d56);
808 }
809 if (radius >= 4) {
810 flush(dst, d67, d78);
811 }
812 }
813
814 // BlurY will be one of blur_y_radius_(1|2|3|4).
815 static void blur_y_rect(ToA8 toA8, const int strideOf8,
816 BlurY blur, int radius, uint16_t *gauss,
817 const uint8_t *src, size_t srcRB, int srcW, int srcH,
818 uint8_t *dst, size_t dstRB) {
819
820 fp88 g0(gauss[0]),
821 g1(gauss[1]),
822 g2(gauss[2]),
823 g3(gauss[3]),
824 g4(gauss[4]);
825
826 int x = 0;
827 for (; x <= srcW - 8; x += 8) {
828 blur_column(toA8, blur, radius, 8,
829 g0, g1, g2, g3, g4,
830 src, srcRB, srcH,
831 dst, dstRB);
832 src += strideOf8;
833 dst += 8;
834 }
835
836 int xTail = srcW - x;
837 if (xTail > 0) {
838 blur_column(toA8, blur, radius, xTail,
839 g0, g1, g2, g3, g4,
840 src, srcRB, srcH,
841 dst, dstRB);
842 }
843 }
844
845 static void direct_blur_y(ToA8 toA8, const int strideOf8,
846 int radius, uint16_t* gauss,
847 const uint8_t* src, size_t srcRB, int srcW, int srcH,
848 uint8_t* dst, size_t dstRB) {
849
850 switch (radius) {
851 case 1:
852 blur_y_rect(toA8, strideOf8, blur_y_radius_1, 1, gauss,
853 src, srcRB, srcW, srcH,
854 dst, dstRB);
855 break;
856
857 case 2:
858 blur_y_rect(toA8, strideOf8, blur_y_radius_2, 2, gauss,
859 src, srcRB, srcW, srcH,
860 dst, dstRB);
861 break;
862
863 case 3:
864 blur_y_rect(toA8, strideOf8, blur_y_radius_3, 3, gauss,
865 src, srcRB, srcW, srcH,
866 dst, dstRB);
867 break;
868
869 case 4:
870 blur_y_rect(toA8, strideOf8, blur_y_radius_4, 4, gauss,
871 src, srcRB, srcW, srcH,
872 dst, dstRB);
873 break;
874
875 default:
876 SkASSERTF(false, "The radius %d is not handled\n", radius);
877 }
878 }
879
880 static SkIPoint small_blur(double sigmaX, double sigmaY, const SkMask& src, SkMaskBuilder* dst) {
881 SkASSERT(sigmaX == sigmaY); // TODO
882 SkASSERT(0.01 <= sigmaX && sigmaX < 2);
883 SkASSERT(0.01 <= sigmaY && sigmaY < 2);
884
885 SkGaussFilter filterX{sigmaX},
886 filterY{sigmaY};
887
888 int radiusX = filterX.radius(),
889 radiusY = filterY.radius();
890
891 SkASSERT(radiusX <= 4 && radiusY <= 4);
892
893 auto prepareGauss = [](const SkGaussFilter& filter, uint16_t* factors) {
894 int i = 0;
895 for (double d : filter) {
896 factors[i++] = static_cast<uint16_t>(round(d * (1 << 16)));
897 }
898 };
899
900 uint16_t gaussFactorsX[SkGaussFilter::kGaussArrayMax],
901 gaussFactorsY[SkGaussFilter::kGaussArrayMax];
902
903 prepareGauss(filterX, gaussFactorsX);
904 prepareGauss(filterY, gaussFactorsY);
905
906 *dst = SkMaskBuilder::PrepareDestination(radiusX, radiusY, src);
907 if (src.fImage == nullptr) {
908 return {SkTo<int32_t>(radiusX), SkTo<int32_t>(radiusY)};
909 }
910 if (dst->fImage == nullptr) {
911 dst->bounds().setEmpty();
912 return {0, 0};
913 }
914
915 int srcW = src.fBounds.width(),
916 srcH = src.fBounds.height();
917
918 int dstW = dst->fBounds.width(),
919 dstH = dst->fBounds.height();
920
921 size_t srcRB = src.fRowBytes,
922 dstRB = dst->fRowBytes;
923
924 //TODO: handle bluring in only one direction.
925
926 // Blur vertically and copy to destination.
927 switch (src.fFormat) {
928 case SkMask::kBW_Format:
929 direct_blur_y(bw_to_a8, 1,
930 radiusY, gaussFactorsY,
931 src.fImage, srcRB, srcW, srcH,
932 dst->image() + radiusX, dstRB);
933 break;
934 case SkMask::kA8_Format:
935 direct_blur_y(nullptr, 8,
936 radiusY, gaussFactorsY,
937 src.fImage, srcRB, srcW, srcH,
938 dst->image() + radiusX, dstRB);
939 break;
940 case SkMask::kARGB32_Format:
941 direct_blur_y(argb32_to_a8, 32,
942 radiusY, gaussFactorsY,
943 src.fImage, srcRB, srcW, srcH,
944 dst->image() + radiusX, dstRB);
945 break;
946 case SkMask::kLCD16_Format:
947 direct_blur_y(lcd_to_a8, 16, radiusY, gaussFactorsY,
948 src.fImage, srcRB, srcW, srcH,
949 dst->image() + radiusX, dstRB);
950 break;
951 default:
952 SK_ABORT("Unhandled format.");
953 }
954
955 // Blur horizontally in place.
956 direct_blur_x(radiusX, gaussFactorsX,
957 dst->fImage + radiusX, dstRB, srcW,
958 dst->image(), dstRB, dstW, dstH);
959
960 return {radiusX, radiusY};
961 }
962
963 // TODO: assuming sigmaW = sigmaH. Allow different sigmas. Right now the
964 // API forces the sigmas to be the same.
965 SkIPoint SkMaskBlurFilter::blur(const SkMask& src, SkMaskBuilder* dst) const {
966
967 if (fSigmaW < 2.0 && fSigmaH < 2.0) {
968 return small_blur(fSigmaW, fSigmaH, src, dst);
969 }
970
971 // 1024 is a place holder guess until more analysis can be done.
972 SkSTArenaAlloc<1024> alloc;
973
974 PlanGauss planW(fSigmaW);
975 PlanGauss planH(fSigmaH);
976
977 int borderW = planW.border(),
978 borderH = planH.border();
979 SkASSERT(borderH >= 0 && borderW >= 0);
980
981 *dst = SkMaskBuilder::PrepareDestination(borderW, borderH, src);
982 if (src.fImage == nullptr) {
983 return {SkTo<int32_t>(borderW), SkTo<int32_t>(borderH)};
984 }
985 if (dst->fImage == nullptr) {
986 dst->bounds().setEmpty();
987 return {0, 0};
988 }
989
990 int srcW = src.fBounds.width(),
991 srcH = src.fBounds.height(),
992 dstW = dst->fBounds.width(),
993 dstH = dst->fBounds.height();
994 SkASSERT(srcW >= 0 && srcH >= 0 && dstW >= 0 && dstH >= 0);
995
996 auto bufferSize = std::max(planW.bufferSize(), planH.bufferSize());
997 auto buffer = alloc.makeArrayDefault<uint32_t>(bufferSize);
998
999 // Blur both directions.
1000 int tmpW = srcH,
1001 tmpH = dstW;
1002
1003 // Make sure not to overflow the multiply for the tmp buffer size.
1004 if (tmpH > std::numeric_limits<int>::max() / tmpW) {
1005 return {0, 0};
1006 }
1007 auto tmp = alloc.makeArrayDefault<uint8_t>(tmpW * tmpH);
1008
1009 // Blur horizontally, and transpose.
1010 const PlanGauss::Scan& scanW = planW.makeBlurScan(srcW, buffer);
1011 switch (src.fFormat) {
1012 case SkMask::kBW_Format: {
1013 const uint8_t* bwStart = src.fImage;
1014 auto start = SkMask::AlphaIter<SkMask::kBW_Format>(bwStart, 0);
1015 auto end = SkMask::AlphaIter<SkMask::kBW_Format>(bwStart + (srcW / 8), srcW % 8);
1016 for (int y = 0; y < srcH; ++y, start >>= src.fRowBytes, end >>= src.fRowBytes) {
1017 auto tmpStart = &tmp[y];
1018 scanW.blur(start, end, tmpStart, tmpW, tmpStart + tmpW * tmpH);
1019 }
1020 } break;
1021 case SkMask::kA8_Format: {
1022 const uint8_t* a8Start = src.fImage;
1023 auto start = SkMask::AlphaIter<SkMask::kA8_Format>(a8Start);
1024 auto end = SkMask::AlphaIter<SkMask::kA8_Format>(a8Start + srcW);
1025 for (int y = 0; y < srcH; ++y, start >>= src.fRowBytes, end >>= src.fRowBytes) {
1026 auto tmpStart = &tmp[y];
1027 scanW.blur(start, end, tmpStart, tmpW, tmpStart + tmpW * tmpH);
1028 }
1029 } break;
1030 case SkMask::kARGB32_Format: {
1031 const uint32_t* argbStart = reinterpret_cast<const uint32_t*>(src.fImage);
1032 auto start = SkMask::AlphaIter<SkMask::kARGB32_Format>(argbStart);
1033 auto end = SkMask::AlphaIter<SkMask::kARGB32_Format>(argbStart + srcW);
1034 for (int y = 0; y < srcH; ++y, start >>= src.fRowBytes, end >>= src.fRowBytes) {
1035 auto tmpStart = &tmp[y];
1036 scanW.blur(start, end, tmpStart, tmpW, tmpStart + tmpW * tmpH);
1037 }
1038 } break;
1039 case SkMask::kLCD16_Format: {
1040 const uint16_t* lcdStart = reinterpret_cast<const uint16_t*>(src.fImage);
1041 auto start = SkMask::AlphaIter<SkMask::kLCD16_Format>(lcdStart);
1042 auto end = SkMask::AlphaIter<SkMask::kLCD16_Format>(lcdStart + srcW);
1043 for (int y = 0; y < srcH; ++y, start >>= src.fRowBytes, end >>= src.fRowBytes) {
1044 auto tmpStart = &tmp[y];
1045 scanW.blur(start, end, tmpStart, tmpW, tmpStart + tmpW * tmpH);
1046 }
1047 } break;
1048 default:
1049 SK_ABORT("Unhandled format.");
1050 }
1051
1052 // Blur vertically (scan in memory order because of the transposition),
1053 // and transpose back to the original orientation.
1054 const PlanGauss::Scan& scanH = planH.makeBlurScan(tmpW, buffer);
1055 for (int y = 0; y < tmpH; y++) {
1056 auto tmpStart = &tmp[y * tmpW];
1057 auto dstStart = &dst->image()[y];
1058
1059 scanH.blur(tmpStart, tmpStart + tmpW,
1060 dstStart, dst->fRowBytes, dstStart + dst->fRowBytes * dstH);
1061 }
1062
1063 return {SkTo<int32_t>(borderW), SkTo<int32_t>(borderH)};
1064 }