OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <algorithm> | 5 #include <algorithm> |
6 | 6 |
7 #include "skia/ext/convolver.h" | 7 #include "skia/ext/convolver.h" |
8 #include "third_party/skia/include/core/SkTypes.h" | 8 #include "third_party/skia/include/core/SkTypes.h" |
9 | 9 |
10 #if defined(SIMD_SSE2) | |
11 #include <emmintrin.h> // ARCH_CPU_X86_FAMILY was defined in build/config.h | |
12 #endif | |
13 | |
14 namespace skia { | 10 namespace skia { |
15 | 11 |
16 namespace { | 12 namespace { |
17 | 13 |
18 // Converts the argument to an 8-bit unsigned value by clamping to the range | 14 // Converts the argument to an 8-bit unsigned value by clamping to the range |
19 // 0-255. | 15 // 0-255. |
20 inline unsigned char ClampTo8(int a) { | 16 inline unsigned char ClampTo8(int a) { |
21 if (static_cast<unsigned>(a) < 256) | 17 if (static_cast<unsigned>(a) < 256) |
22 return a; // Avoid the extra check in the common case. | 18 return a; // Avoid the extra check in the common case. |
23 if (a < 0) | 19 if (a < 0) |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
216 out_row[byte_offset + 3] = max_color_channel; | 212 out_row[byte_offset + 3] = max_color_channel; |
217 else | 213 else |
218 out_row[byte_offset + 3] = alpha; | 214 out_row[byte_offset + 3] = alpha; |
219 } else { | 215 } else { |
220 // No alpha channel, the image is opaque. | 216 // No alpha channel, the image is opaque. |
221 out_row[byte_offset + 3] = 0xff; | 217 out_row[byte_offset + 3] = 0xff; |
222 } | 218 } |
223 } | 219 } |
224 } | 220 } |
225 | 221 |
226 | |
227 // Convolves horizontally along a single row. The row data is given in | |
228 // |src_data| and continues for the num_values() of the filter. | |
229 void ConvolveHorizontally_SSE2(const unsigned char* src_data, | |
230 const ConvolutionFilter1D& filter, | |
231 unsigned char* out_row) { | |
232 #if defined(SIMD_SSE2) | |
233 int num_values = filter.num_values(); | |
234 | |
235 int filter_offset, filter_length; | |
236 __m128i zero = _mm_setzero_si128(); | |
237 __m128i mask[4]; | |
238 // |mask| will be used to decimate all extra filter coefficients that are | |
239 // loaded by SIMD when |filter_length| is not divisible by 4. | |
240 // mask[0] is not used in following algorithm. | |
241 mask[1] = _mm_set_epi16(0, 0, 0, 0, 0, 0, 0, -1); | |
242 mask[2] = _mm_set_epi16(0, 0, 0, 0, 0, 0, -1, -1); | |
243 mask[3] = _mm_set_epi16(0, 0, 0, 0, 0, -1, -1, -1); | |
244 | |
245 // Output one pixel each iteration, calculating all channels (RGBA) together. | |
246 for (int out_x = 0; out_x < num_values; out_x++) { | |
247 const ConvolutionFilter1D::Fixed* filter_values = | |
248 filter.FilterForValue(out_x, &filter_offset, &filter_length); | |
249 | |
250 __m128i accum = _mm_setzero_si128(); | |
251 | |
252 // Compute the first pixel in this row that the filter affects. It will | |
253 // touch |filter_length| pixels (4 bytes each) after this. | |
254 const __m128i* row_to_filter = | |
255 reinterpret_cast<const __m128i*>(&src_data[filter_offset << 2]); | |
256 | |
257 // We will load and accumulate with four coefficients per iteration. | |
258 for (int filter_x = 0; filter_x < filter_length >> 2; filter_x++) { | |
259 | |
260 // Load 4 coefficients => duplicate 1st and 2nd of them for all channels. | |
261 __m128i coeff, coeff16; | |
262 // [16] xx xx xx xx c3 c2 c1 c0 | |
263 coeff = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(filter_values)); | |
264 // [16] xx xx xx xx c1 c1 c0 c0 | |
265 coeff16 = _mm_shufflelo_epi16(coeff, _MM_SHUFFLE(1, 1, 0, 0)); | |
266 // [16] c1 c1 c1 c1 c0 c0 c0 c0 | |
267 coeff16 = _mm_unpacklo_epi16(coeff16, coeff16); | |
268 | |
269 // Load four pixels => unpack the first two pixels to 16 bits => | |
270 // multiply with coefficients => accumulate the convolution result. | |
271 // [8] a3 b3 g3 r3 a2 b2 g2 r2 a1 b1 g1 r1 a0 b0 g0 r0 | |
272 __m128i src8 = _mm_loadu_si128(row_to_filter); | |
273 // [16] a1 b1 g1 r1 a0 b0 g0 r0 | |
274 __m128i src16 = _mm_unpacklo_epi8(src8, zero); | |
275 __m128i mul_hi = _mm_mulhi_epi16(src16, coeff16); | |
276 __m128i mul_lo = _mm_mullo_epi16(src16, coeff16); | |
277 // [32] a0*c0 b0*c0 g0*c0 r0*c0 | |
278 __m128i t = _mm_unpacklo_epi16(mul_lo, mul_hi); | |
279 accum = _mm_add_epi32(accum, t); | |
280 // [32] a1*c1 b1*c1 g1*c1 r1*c1 | |
281 t = _mm_unpackhi_epi16(mul_lo, mul_hi); | |
282 accum = _mm_add_epi32(accum, t); | |
283 | |
284 // Duplicate 3rd and 4th coefficients for all channels => | |
285 // unpack the 3rd and 4th pixels to 16 bits => multiply with coefficients | |
286 // => accumulate the convolution results. | |
287 // [16] xx xx xx xx c3 c3 c2 c2 | |
288 coeff16 = _mm_shufflelo_epi16(coeff, _MM_SHUFFLE(3, 3, 2, 2)); | |
289 // [16] c3 c3 c3 c3 c2 c2 c2 c2 | |
290 coeff16 = _mm_unpacklo_epi16(coeff16, coeff16); | |
291 // [16] a3 g3 b3 r3 a2 g2 b2 r2 | |
292 src16 = _mm_unpackhi_epi8(src8, zero); | |
293 mul_hi = _mm_mulhi_epi16(src16, coeff16); | |
294 mul_lo = _mm_mullo_epi16(src16, coeff16); | |
295 // [32] a2*c2 b2*c2 g2*c2 r2*c2 | |
296 t = _mm_unpacklo_epi16(mul_lo, mul_hi); | |
297 accum = _mm_add_epi32(accum, t); | |
298 // [32] a3*c3 b3*c3 g3*c3 r3*c3 | |
299 t = _mm_unpackhi_epi16(mul_lo, mul_hi); | |
300 accum = _mm_add_epi32(accum, t); | |
301 | |
302 // Advance the pixel and coefficients pointers. | |
303 row_to_filter += 1; | |
304 filter_values += 4; | |
305 } | |
306 | |
307 // When |filter_length| is not divisible by 4, we need to decimate some of | |
308 // the filter coefficient that was loaded incorrectly to zero; Other than | |
309 // that the algorithm is same with above, exceot that the 4th pixel will be | |
310 // always absent. | |
311 int r = filter_length&3; | |
312 if (r) { | |
313 // Note: filter_values must be padded to align_up(filter_offset, 8). | |
314 __m128i coeff, coeff16; | |
315 coeff = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(filter_values)); | |
316 // Mask out extra filter taps. | |
317 coeff = _mm_and_si128(coeff, mask[r]); | |
318 coeff16 = _mm_shufflelo_epi16(coeff, _MM_SHUFFLE(1, 1, 0, 0)); | |
319 coeff16 = _mm_unpacklo_epi16(coeff16, coeff16); | |
320 | |
321 // Note: line buffer must be padded to align_up(filter_offset, 16). | |
322 // We resolve this by use C-version for the last horizontal line. | |
323 __m128i src8 = _mm_loadu_si128(row_to_filter); | |
324 __m128i src16 = _mm_unpacklo_epi8(src8, zero); | |
325 __m128i mul_hi = _mm_mulhi_epi16(src16, coeff16); | |
326 __m128i mul_lo = _mm_mullo_epi16(src16, coeff16); | |
327 __m128i t = _mm_unpacklo_epi16(mul_lo, mul_hi); | |
328 accum = _mm_add_epi32(accum, t); | |
329 t = _mm_unpackhi_epi16(mul_lo, mul_hi); | |
330 accum = _mm_add_epi32(accum, t); | |
331 | |
332 src16 = _mm_unpackhi_epi8(src8, zero); | |
333 coeff16 = _mm_shufflelo_epi16(coeff, _MM_SHUFFLE(3, 3, 2, 2)); | |
334 coeff16 = _mm_unpacklo_epi16(coeff16, coeff16); | |
335 mul_hi = _mm_mulhi_epi16(src16, coeff16); | |
336 mul_lo = _mm_mullo_epi16(src16, coeff16); | |
337 t = _mm_unpacklo_epi16(mul_lo, mul_hi); | |
338 accum = _mm_add_epi32(accum, t); | |
339 } | |
340 | |
341 // Shift right for fixed point implementation. | |
342 accum = _mm_srai_epi32(accum, ConvolutionFilter1D::kShiftBits); | |
343 | |
344 // Packing 32 bits |accum| to 16 bits per channel (signed saturation). | |
345 accum = _mm_packs_epi32(accum, zero); | |
346 // Packing 16 bits |accum| to 8 bits per channel (unsigned saturation). | |
347 accum = _mm_packus_epi16(accum, zero); | |
348 | |
349 // Store the pixel value of 32 bits. | |
350 *(reinterpret_cast<int*>(out_row)) = _mm_cvtsi128_si32(accum); | |
351 out_row += 4; | |
352 } | |
353 #endif | |
354 } | |
355 | |
356 // Convolves horizontally along four rows. The row data is given in | |
357 // |src_data| and continues for the num_values() of the filter. | |
358 // The algorithm is almost same as |ConvolveHorizontally_SSE2|. Please | |
359 // refer to that function for detailed comments. | |
360 void ConvolveHorizontally4_SSE2(const unsigned char* src_data[4], | |
361 const ConvolutionFilter1D& filter, | |
362 unsigned char* out_row[4]) { | |
363 #if defined(SIMD_SSE2) | |
364 int num_values = filter.num_values(); | |
365 | |
366 int filter_offset, filter_length; | |
367 __m128i zero = _mm_setzero_si128(); | |
368 __m128i mask[4]; | |
369 // |mask| will be used to decimate all extra filter coefficients that are | |
370 // loaded by SIMD when |filter_length| is not divisible by 4. | |
371 // mask[0] is not used in following algorithm. | |
372 mask[1] = _mm_set_epi16(0, 0, 0, 0, 0, 0, 0, -1); | |
373 mask[2] = _mm_set_epi16(0, 0, 0, 0, 0, 0, -1, -1); | |
374 mask[3] = _mm_set_epi16(0, 0, 0, 0, 0, -1, -1, -1); | |
375 | |
376 // Output one pixel each iteration, calculating all channels (RGBA) together. | |
377 for (int out_x = 0; out_x < num_values; out_x++) { | |
378 const ConvolutionFilter1D::Fixed* filter_values = | |
379 filter.FilterForValue(out_x, &filter_offset, &filter_length); | |
380 | |
381 // four pixels in a column per iteration. | |
382 __m128i accum0 = _mm_setzero_si128(); | |
383 __m128i accum1 = _mm_setzero_si128(); | |
384 __m128i accum2 = _mm_setzero_si128(); | |
385 __m128i accum3 = _mm_setzero_si128(); | |
386 int start = (filter_offset<<2); | |
387 // We will load and accumulate with four coefficients per iteration. | |
388 for (int filter_x = 0; filter_x < (filter_length >> 2); filter_x++) { | |
389 __m128i coeff, coeff16lo, coeff16hi; | |
390 // [16] xx xx xx xx c3 c2 c1 c0 | |
391 coeff = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(filter_values)); | |
392 // [16] xx xx xx xx c1 c1 c0 c0 | |
393 coeff16lo = _mm_shufflelo_epi16(coeff, _MM_SHUFFLE(1, 1, 0, 0)); | |
394 // [16] c1 c1 c1 c1 c0 c0 c0 c0 | |
395 coeff16lo = _mm_unpacklo_epi16(coeff16lo, coeff16lo); | |
396 // [16] xx xx xx xx c3 c3 c2 c2 | |
397 coeff16hi = _mm_shufflelo_epi16(coeff, _MM_SHUFFLE(3, 3, 2, 2)); | |
398 // [16] c3 c3 c3 c3 c2 c2 c2 c2 | |
399 coeff16hi = _mm_unpacklo_epi16(coeff16hi, coeff16hi); | |
400 | |
401 __m128i src8, src16, mul_hi, mul_lo, t; | |
402 | |
403 #define ITERATION(src, accum) \ | |
404 src8 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(src)); \ | |
405 src16 = _mm_unpacklo_epi8(src8, zero); \ | |
406 mul_hi = _mm_mulhi_epi16(src16, coeff16lo); \ | |
407 mul_lo = _mm_mullo_epi16(src16, coeff16lo); \ | |
408 t = _mm_unpacklo_epi16(mul_lo, mul_hi); \ | |
409 accum = _mm_add_epi32(accum, t); \ | |
410 t = _mm_unpackhi_epi16(mul_lo, mul_hi); \ | |
411 accum = _mm_add_epi32(accum, t); \ | |
412 src16 = _mm_unpackhi_epi8(src8, zero); \ | |
413 mul_hi = _mm_mulhi_epi16(src16, coeff16hi); \ | |
414 mul_lo = _mm_mullo_epi16(src16, coeff16hi); \ | |
415 t = _mm_unpacklo_epi16(mul_lo, mul_hi); \ | |
416 accum = _mm_add_epi32(accum, t); \ | |
417 t = _mm_unpackhi_epi16(mul_lo, mul_hi); \ | |
418 accum = _mm_add_epi32(accum, t) | |
419 | |
420 ITERATION(src_data[0] + start, accum0); | |
421 ITERATION(src_data[1] + start, accum1); | |
422 ITERATION(src_data[2] + start, accum2); | |
423 ITERATION(src_data[3] + start, accum3); | |
424 | |
425 start += 16; | |
426 filter_values += 4; | |
427 } | |
428 | |
429 int r = filter_length & 3; | |
430 if (r) { | |
431 // Note: filter_values must be padded to align_up(filter_offset, 8); | |
432 __m128i coeff; | |
433 coeff = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(filter_values)); | |
434 // Mask out extra filter taps. | |
435 coeff = _mm_and_si128(coeff, mask[r]); | |
436 | |
437 __m128i coeff16lo = _mm_shufflelo_epi16(coeff, _MM_SHUFFLE(1, 1, 0, 0)); | |
438 /* c1 c1 c1 c1 c0 c0 c0 c0 */ | |
439 coeff16lo = _mm_unpacklo_epi16(coeff16lo, coeff16lo); | |
440 __m128i coeff16hi = _mm_shufflelo_epi16(coeff, _MM_SHUFFLE(3, 3, 2, 2)); | |
441 coeff16hi = _mm_unpacklo_epi16(coeff16hi, coeff16hi); | |
442 | |
443 __m128i src8, src16, mul_hi, mul_lo, t; | |
444 | |
445 ITERATION(src_data[0] + start, accum0); | |
446 ITERATION(src_data[1] + start, accum1); | |
447 ITERATION(src_data[2] + start, accum2); | |
448 ITERATION(src_data[3] + start, accum3); | |
449 } | |
450 | |
451 accum0 = _mm_srai_epi32(accum0, ConvolutionFilter1D::kShiftBits); | |
452 accum0 = _mm_packs_epi32(accum0, zero); | |
453 accum0 = _mm_packus_epi16(accum0, zero); | |
454 accum1 = _mm_srai_epi32(accum1, ConvolutionFilter1D::kShiftBits); | |
455 accum1 = _mm_packs_epi32(accum1, zero); | |
456 accum1 = _mm_packus_epi16(accum1, zero); | |
457 accum2 = _mm_srai_epi32(accum2, ConvolutionFilter1D::kShiftBits); | |
458 accum2 = _mm_packs_epi32(accum2, zero); | |
459 accum2 = _mm_packus_epi16(accum2, zero); | |
460 accum3 = _mm_srai_epi32(accum3, ConvolutionFilter1D::kShiftBits); | |
461 accum3 = _mm_packs_epi32(accum3, zero); | |
462 accum3 = _mm_packus_epi16(accum3, zero); | |
463 | |
464 *(reinterpret_cast<int*>(out_row[0])) = _mm_cvtsi128_si32(accum0); | |
465 *(reinterpret_cast<int*>(out_row[1])) = _mm_cvtsi128_si32(accum1); | |
466 *(reinterpret_cast<int*>(out_row[2])) = _mm_cvtsi128_si32(accum2); | |
467 *(reinterpret_cast<int*>(out_row[3])) = _mm_cvtsi128_si32(accum3); | |
468 | |
469 out_row[0] += 4; | |
470 out_row[1] += 4; | |
471 out_row[2] += 4; | |
472 out_row[3] += 4; | |
473 } | |
474 #endif | |
475 } | |
476 | |
477 // Does vertical convolution to produce one output row. The filter values and | |
478 // length are given in the first two parameters. These are applied to each | |
479 // of the rows pointed to in the |source_data_rows| array, with each row | |
480 // being |pixel_width| wide. | |
481 // | |
482 // The output must have room for |pixel_width * 4| bytes. | |
483 template<bool has_alpha> | |
484 void ConvolveVertically_SSE2(const ConvolutionFilter1D::Fixed* filter_values, | |
485 int filter_length, | |
486 unsigned char* const* source_data_rows, | |
487 int pixel_width, | |
488 unsigned char* out_row) { | |
489 #if defined(SIMD_SSE2) | |
490 int width = pixel_width & ~3; | |
491 | |
492 __m128i zero = _mm_setzero_si128(); | |
493 __m128i accum0, accum1, accum2, accum3, coeff16; | |
494 const __m128i* src; | |
495 // Output four pixels per iteration (16 bytes). | |
496 for (int out_x = 0; out_x < width; out_x += 4) { | |
497 | |
498 // Accumulated result for each pixel. 32 bits per RGBA channel. | |
499 accum0 = _mm_setzero_si128(); | |
500 accum1 = _mm_setzero_si128(); | |
501 accum2 = _mm_setzero_si128(); | |
502 accum3 = _mm_setzero_si128(); | |
503 | |
504 // Convolve with one filter coefficient per iteration. | |
505 for (int filter_y = 0; filter_y < filter_length; filter_y++) { | |
506 | |
507 // Duplicate the filter coefficient 8 times. | |
508 // [16] cj cj cj cj cj cj cj cj | |
509 coeff16 = _mm_set1_epi16(filter_values[filter_y]); | |
510 | |
511 // Load four pixels (16 bytes) together. | |
512 // [8] a3 b3 g3 r3 a2 b2 g2 r2 a1 b1 g1 r1 a0 b0 g0 r0 | |
513 src = reinterpret_cast<const __m128i*>( | |
514 &source_data_rows[filter_y][out_x << 2]); | |
515 __m128i src8 = _mm_loadu_si128(src); | |
516 | |
517 // Unpack 1st and 2nd pixels from 8 bits to 16 bits for each channels => | |
518 // multiply with current coefficient => accumulate the result. | |
519 // [16] a1 b1 g1 r1 a0 b0 g0 r0 | |
520 __m128i src16 = _mm_unpacklo_epi8(src8, zero); | |
521 __m128i mul_hi = _mm_mulhi_epi16(src16, coeff16); | |
522 __m128i mul_lo = _mm_mullo_epi16(src16, coeff16); | |
523 // [32] a0 b0 g0 r0 | |
524 __m128i t = _mm_unpacklo_epi16(mul_lo, mul_hi); | |
525 accum0 = _mm_add_epi32(accum0, t); | |
526 // [32] a1 b1 g1 r1 | |
527 t = _mm_unpackhi_epi16(mul_lo, mul_hi); | |
528 accum1 = _mm_add_epi32(accum1, t); | |
529 | |
530 // Unpack 3rd and 4th pixels from 8 bits to 16 bits for each channels => | |
531 // multiply with current coefficient => accumulate the result. | |
532 // [16] a3 b3 g3 r3 a2 b2 g2 r2 | |
533 src16 = _mm_unpackhi_epi8(src8, zero); | |
534 mul_hi = _mm_mulhi_epi16(src16, coeff16); | |
535 mul_lo = _mm_mullo_epi16(src16, coeff16); | |
536 // [32] a2 b2 g2 r2 | |
537 t = _mm_unpacklo_epi16(mul_lo, mul_hi); | |
538 accum2 = _mm_add_epi32(accum2, t); | |
539 // [32] a3 b3 g3 r3 | |
540 t = _mm_unpackhi_epi16(mul_lo, mul_hi); | |
541 accum3 = _mm_add_epi32(accum3, t); | |
542 } | |
543 | |
544 // Shift right for fixed point implementation. | |
545 accum0 = _mm_srai_epi32(accum0, ConvolutionFilter1D::kShiftBits); | |
546 accum1 = _mm_srai_epi32(accum1, ConvolutionFilter1D::kShiftBits); | |
547 accum2 = _mm_srai_epi32(accum2, ConvolutionFilter1D::kShiftBits); | |
548 accum3 = _mm_srai_epi32(accum3, ConvolutionFilter1D::kShiftBits); | |
549 | |
550 // Packing 32 bits |accum| to 16 bits per channel (signed saturation). | |
551 // [16] a1 b1 g1 r1 a0 b0 g0 r0 | |
552 accum0 = _mm_packs_epi32(accum0, accum1); | |
553 // [16] a3 b3 g3 r3 a2 b2 g2 r2 | |
554 accum2 = _mm_packs_epi32(accum2, accum3); | |
555 | |
556 // Packing 16 bits |accum| to 8 bits per channel (unsigned saturation). | |
557 // [8] a3 b3 g3 r3 a2 b2 g2 r2 a1 b1 g1 r1 a0 b0 g0 r0 | |
558 accum0 = _mm_packus_epi16(accum0, accum2); | |
559 | |
560 if (has_alpha) { | |
561 // Compute the max(ri, gi, bi) for each pixel. | |
562 // [8] xx a3 b3 g3 xx a2 b2 g2 xx a1 b1 g1 xx a0 b0 g0 | |
563 __m128i a = _mm_srli_epi32(accum0, 8); | |
564 // [8] xx xx xx max3 xx xx xx max2 xx xx xx max1 xx xx xx max0 | |
565 __m128i b = _mm_max_epu8(a, accum0); // Max of r and g. | |
566 // [8] xx xx a3 b3 xx xx a2 b2 xx xx a1 b1 xx xx a0 b0 | |
567 a = _mm_srli_epi32(accum0, 16); | |
568 // [8] xx xx xx max3 xx xx xx max2 xx xx xx max1 xx xx xx max0 | |
569 b = _mm_max_epu8(a, b); // Max of r and g and b. | |
570 // [8] max3 00 00 00 max2 00 00 00 max1 00 00 00 max0 00 00 00 | |
571 b = _mm_slli_epi32(b, 24); | |
572 | |
573 // Make sure the value of alpha channel is always larger than maximum | |
574 // value of color channels. | |
575 accum0 = _mm_max_epu8(b, accum0); | |
576 } else { | |
577 // Set value of alpha channels to 0xFF. | |
578 __m128i mask = _mm_set1_epi32(0xff000000); | |
579 accum0 = _mm_or_si128(accum0, mask); | |
580 } | |
581 | |
582 // Store the convolution result (16 bytes) and advance the pixel pointers. | |
583 _mm_storeu_si128(reinterpret_cast<__m128i*>(out_row), accum0); | |
584 out_row += 16; | |
585 } | |
586 | |
587 // When the width of the output is not divisible by 4, We need to save one | |
588 // pixel (4 bytes) each time. And also the fourth pixel is always absent. | |
589 if (pixel_width & 3) { | |
590 accum0 = _mm_setzero_si128(); | |
591 accum1 = _mm_setzero_si128(); | |
592 accum2 = _mm_setzero_si128(); | |
593 for (int filter_y = 0; filter_y < filter_length; ++filter_y) { | |
594 coeff16 = _mm_set1_epi16(filter_values[filter_y]); | |
595 // [8] a3 b3 g3 r3 a2 b2 g2 r2 a1 b1 g1 r1 a0 b0 g0 r0 | |
596 src = reinterpret_cast<const __m128i*>( | |
597 &source_data_rows[filter_y][width<<2]); | |
598 __m128i src8 = _mm_loadu_si128(src); | |
599 // [16] a1 b1 g1 r1 a0 b0 g0 r0 | |
600 __m128i src16 = _mm_unpacklo_epi8(src8, zero); | |
601 __m128i mul_hi = _mm_mulhi_epi16(src16, coeff16); | |
602 __m128i mul_lo = _mm_mullo_epi16(src16, coeff16); | |
603 // [32] a0 b0 g0 r0 | |
604 __m128i t = _mm_unpacklo_epi16(mul_lo, mul_hi); | |
605 accum0 = _mm_add_epi32(accum0, t); | |
606 // [32] a1 b1 g1 r1 | |
607 t = _mm_unpackhi_epi16(mul_lo, mul_hi); | |
608 accum1 = _mm_add_epi32(accum1, t); | |
609 // [16] a3 b3 g3 r3 a2 b2 g2 r2 | |
610 src16 = _mm_unpackhi_epi8(src8, zero); | |
611 mul_hi = _mm_mulhi_epi16(src16, coeff16); | |
612 mul_lo = _mm_mullo_epi16(src16, coeff16); | |
613 // [32] a2 b2 g2 r2 | |
614 t = _mm_unpacklo_epi16(mul_lo, mul_hi); | |
615 accum2 = _mm_add_epi32(accum2, t); | |
616 } | |
617 | |
618 accum0 = _mm_srai_epi32(accum0, ConvolutionFilter1D::kShiftBits); | |
619 accum1 = _mm_srai_epi32(accum1, ConvolutionFilter1D::kShiftBits); | |
620 accum2 = _mm_srai_epi32(accum2, ConvolutionFilter1D::kShiftBits); | |
621 // [16] a1 b1 g1 r1 a0 b0 g0 r0 | |
622 accum0 = _mm_packs_epi32(accum0, accum1); | |
623 // [16] a3 b3 g3 r3 a2 b2 g2 r2 | |
624 accum2 = _mm_packs_epi32(accum2, zero); | |
625 // [8] a3 b3 g3 r3 a2 b2 g2 r2 a1 b1 g1 r1 a0 b0 g0 r0 | |
626 accum0 = _mm_packus_epi16(accum0, accum2); | |
627 if (has_alpha) { | |
628 // [8] xx a3 b3 g3 xx a2 b2 g2 xx a1 b1 g1 xx a0 b0 g0 | |
629 __m128i a = _mm_srli_epi32(accum0, 8); | |
630 // [8] xx xx xx max3 xx xx xx max2 xx xx xx max1 xx xx xx max0 | |
631 __m128i b = _mm_max_epu8(a, accum0); // Max of r and g. | |
632 // [8] xx xx a3 b3 xx xx a2 b2 xx xx a1 b1 xx xx a0 b0 | |
633 a = _mm_srli_epi32(accum0, 16); | |
634 // [8] xx xx xx max3 xx xx xx max2 xx xx xx max1 xx xx xx max0 | |
635 b = _mm_max_epu8(a, b); // Max of r and g and b. | |
636 // [8] max3 00 00 00 max2 00 00 00 max1 00 00 00 max0 00 00 00 | |
637 b = _mm_slli_epi32(b, 24); | |
638 accum0 = _mm_max_epu8(b, accum0); | |
639 } else { | |
640 __m128i mask = _mm_set1_epi32(0xff000000); | |
641 accum0 = _mm_or_si128(accum0, mask); | |
642 } | |
643 | |
644 for (int out_x = width; out_x < pixel_width; out_x++) { | |
645 *(reinterpret_cast<int*>(out_row)) = _mm_cvtsi128_si32(accum0); | |
646 accum0 = _mm_srli_si128(accum0, 4); | |
647 out_row += 4; | |
648 } | |
649 } | |
650 #endif | |
651 } | |
652 | |
653 } // namespace | 222 } // namespace |
654 | 223 |
655 // ConvolutionFilter1D --------------------------------------------------------- | 224 // ConvolutionFilter1D --------------------------------------------------------- |
656 | 225 |
657 ConvolutionFilter1D::ConvolutionFilter1D() | 226 ConvolutionFilter1D::ConvolutionFilter1D() |
658 : max_filter_(0) { | 227 : max_filter_(0) { |
659 } | 228 } |
660 | 229 |
661 ConvolutionFilter1D::~ConvolutionFilter1D() { | 230 ConvolutionFilter1D::~ConvolutionFilter1D() { |
662 } | 231 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
708 // We pushed filter_length elements onto filter_values_ | 277 // We pushed filter_length elements onto filter_values_ |
709 instance.data_location = (static_cast<int>(filter_values_.size()) - | 278 instance.data_location = (static_cast<int>(filter_values_.size()) - |
710 filter_length); | 279 filter_length); |
711 instance.offset = filter_offset; | 280 instance.offset = filter_offset; |
712 instance.length = filter_length; | 281 instance.length = filter_length; |
713 filters_.push_back(instance); | 282 filters_.push_back(instance); |
714 | 283 |
715 max_filter_ = std::max(max_filter_, filter_length); | 284 max_filter_ = std::max(max_filter_, filter_length); |
716 } | 285 } |
717 | 286 |
287 #ifdef SIMD_SSE2 | |
288 #define PROTOTYPE | |
289 #else | |
290 #define PROTOTYPE { SkASSERT(false); } | |
291 #endif | |
292 | |
293 | |
294 | |
295 typedef void (*ConvolveVertically_pointer)( | |
296 const ConvolutionFilter1D::Fixed* filter_values, | |
297 int filter_length, | |
298 unsigned char* const* source_data_rows, | |
299 int pixel_width, | |
300 unsigned char* out_row, | |
301 bool has_alpha); | |
302 typedef void (*Convolve4RowsHorizontally_pointer)( | |
303 const unsigned char* src_data[4], | |
304 const ConvolutionFilter1D& filter, | |
305 unsigned char* out_row[4]); | |
306 typedef void (*ConvolveHorizontally_pointer)( | |
307 const unsigned char* src_data, | |
308 const ConvolutionFilter1D& filter, | |
309 unsigned char* out_row); | |
310 | |
311 struct ConvolveProcs { | |
312 // This is how many extra pixels may be read by the | |
313 // conolve*horizontally functions. | |
314 int extra_horizontal_reads; | |
315 ConvolveVertically_pointer convolve_vertically; | |
316 Convolve4RowsHorizontally_pointer convolve_4rows_horizontally; | |
317 ConvolveHorizontally_pointer convolve_horizontally; | |
318 }; | |
319 | |
320 void SetupSIMD(ConvolveProcs *procs) { | |
321 #ifdef SIMD_SSE2 | |
322 base::CPU cpu; | |
323 if (cpu.has_sse2()) { | |
324 extern void ConvolveVertically_SSE2( | |
325 const ConvolutionFilter1D::Fixed* filter_values, | |
326 int filter_length, | |
327 unsigned char* const* source_data_rows, | |
328 int pixel_width, | |
329 unsigned char* out_row, | |
330 bool has_alpha); | |
331 extern void Convolve4RowsHorizontally_SSE2( | |
332 const unsigned char* src_data[4], | |
333 const ConvolutionFilter1D& filter, | |
334 unsigned char* out_row[4]); | |
335 extern void ConvolveHorizontally_SSE2( | |
Stephen White
2013/04/04 20:19:44
These externs are really gross. Is there a reason
hubbe
2013/04/05 08:18:05
Done.
I agree actually. When I first wrote it I th
| |
336 const unsigned char* src_data, | |
337 const ConvolutionFilter1D& filter, | |
338 unsigned char* out_row); | |
339 | |
340 procs->extra_horizontal_reads = 3; | |
341 procs->convolve_vertically = &ConvolveVertically_SSE2; | |
342 procs->convolve_4rows_horizontally = &Convolve4RowsHorizontally_SSE2; | |
343 procs->convolve_horizontally = &ConvolveHorizontally_SSE2; | |
344 } | |
345 #endif | |
346 } | |
347 | |
348 | |
718 void BGRAConvolve2D(const unsigned char* source_data, | 349 void BGRAConvolve2D(const unsigned char* source_data, |
719 int source_byte_row_stride, | 350 int source_byte_row_stride, |
720 bool source_has_alpha, | 351 bool source_has_alpha, |
721 const ConvolutionFilter1D& filter_x, | 352 const ConvolutionFilter1D& filter_x, |
722 const ConvolutionFilter1D& filter_y, | 353 const ConvolutionFilter1D& filter_y, |
723 int output_byte_row_stride, | 354 int output_byte_row_stride, |
724 unsigned char* output, | 355 unsigned char* output, |
725 bool use_sse2) { | 356 bool use_simd_if_possible) { |
726 #if !defined(SIMD_SSE2) | 357 ConvolveProcs simd; |
727 // Even we have runtime support for SSE2 instructions, since the binary | 358 simd.extra_horizontal_reads = 0; |
728 // was not built with SSE2 support, we had to fallback to C version. | 359 simd.convolve_vertically = NULL; |
729 use_sse2 = false; | 360 simd.convolve_4rows_horizontally = NULL; |
730 #endif | 361 simd.convolve_horizontally = NULL; |
362 if (use_simd_if_possible) { | |
363 SetupSIMD(&simd); | |
364 } | |
731 | 365 |
732 int max_y_filter_size = filter_y.max_filter(); | 366 int max_y_filter_size = filter_y.max_filter(); |
733 | 367 |
734 // The next row in the input that we will generate a horizontally | 368 // The next row in the input that we will generate a horizontally |
735 // convolved row for. If the filter doesn't start at the beginning of the | 369 // convolved row for. If the filter doesn't start at the beginning of the |
736 // image (this is the case when we are only resizing a subset), then we | 370 // image (this is the case when we are only resizing a subset), then we |
737 // don't want to generate any output rows before that. Compute the starting | 371 // don't want to generate any output rows before that. Compute the starting |
738 // row for convolution as the first pixel for the first vertical filter. | 372 // row for convolution as the first pixel for the first vertical filter. |
739 int filter_offset, filter_length; | 373 int filter_offset, filter_length; |
740 const ConvolutionFilter1D::Fixed* filter_values = | 374 const ConvolutionFilter1D::Fixed* filter_values = |
741 filter_y.FilterForValue(0, &filter_offset, &filter_length); | 375 filter_y.FilterForValue(0, &filter_offset, &filter_length); |
742 int next_x_row = filter_offset; | 376 int next_x_row = filter_offset; |
743 | 377 |
744 // We loop over each row in the input doing a horizontal convolution. This | 378 // We loop over each row in the input doing a horizontal convolution. This |
745 // will result in a horizontally convolved image. We write the results into | 379 // will result in a horizontally convolved image. We write the results into |
746 // a circular buffer of convolved rows and do vertical convolution as rows | 380 // a circular buffer of convolved rows and do vertical convolution as rows |
747 // are available. This prevents us from having to store the entire | 381 // are available. This prevents us from having to store the entire |
748 // intermediate image and helps cache coherency. | 382 // intermediate image and helps cache coherency. |
749 // We will need four extra rows to allow horizontal convolution could be done | 383 // We will need four extra rows to allow horizontal convolution could be done |
750 // simultaneously. We also padding each row in row buffer to be aligned-up to | 384 // simultaneously. We also padding each row in row buffer to be aligned-up to |
751 // 16 bytes. | 385 // 16 bytes. |
752 // TODO(jiesun): We do not use aligned load from row buffer in vertical | 386 // TODO(jiesun): We do not use aligned load from row buffer in vertical |
753 // convolution pass yet. Somehow Windows does not like it. | 387 // convolution pass yet. Somehow Windows does not like it. |
754 int row_buffer_width = (filter_x.num_values() + 15) & ~0xF; | 388 int row_buffer_width = (filter_x.num_values() + 15) & ~0xF; |
755 int row_buffer_height = max_y_filter_size + (use_sse2 ? 4 : 0); | 389 int row_buffer_height = max_y_filter_size + |
390 (simd.convolve_4rows_horizontally ? 4 : 0); | |
756 CircularRowBuffer row_buffer(row_buffer_width, | 391 CircularRowBuffer row_buffer(row_buffer_width, |
757 row_buffer_height, | 392 row_buffer_height, |
758 filter_offset); | 393 filter_offset); |
759 | 394 |
760 // Loop over every possible output row, processing just enough horizontal | 395 // Loop over every possible output row, processing just enough horizontal |
761 // convolutions to run each subsequent vertical convolution. | 396 // convolutions to run each subsequent vertical convolution. |
762 SkASSERT(output_byte_row_stride >= filter_x.num_values() * 4); | 397 SkASSERT(output_byte_row_stride >= filter_x.num_values() * 4); |
763 int num_output_rows = filter_y.num_values(); | 398 int num_output_rows = filter_y.num_values(); |
764 | 399 |
765 // We need to check which is the last line to convolve before we advance 4 | 400 // We need to check which is the last line to convolve before we advance 4 |
766 // lines in one iteration. | 401 // lines in one iteration. |
767 int last_filter_offset, last_filter_length; | 402 int last_filter_offset, last_filter_length; |
768 | 403 |
769 // SSE2 can access up to 3 extra pixels past the end of the | 404 // SSE2 can access up to 3 extra pixels past the end of the |
770 // buffer. At the bottom of the image, we have to be careful | 405 // buffer. At the bottom of the image, we have to be careful |
771 // not to access data past the end of the buffer. Normally | 406 // not to access data past the end of the buffer. Normally |
772 // we fall back to the C++ implementation for the last row. | 407 // we fall back to the C++ implementation for the last row. |
773 // If the last row is less than 3 pixels wide, we may have to fall | 408 // If the last row is less than 3 pixels wide, we may have to fall |
774 // back to the C++ version for more rows. Compute how many | 409 // back to the C++ version for more rows. Compute how many |
775 // rows we need to avoid the SSE implementation for here. | 410 // rows we need to avoid the SSE implementation for here. |
776 filter_x.FilterForValue(filter_x.num_values() - 1, &last_filter_offset, | 411 filter_x.FilterForValue(filter_x.num_values() - 1, &last_filter_offset, |
777 &last_filter_length); | 412 &last_filter_length); |
778 int avoid_sse_rows = 1 + 3/(last_filter_offset + last_filter_length); | 413 int avoid_simd_rows = 1 + simd.extra_horizontal_reads / |
414 (last_filter_offset + last_filter_length); | |
779 | 415 |
780 filter_y.FilterForValue(num_output_rows - 1, &last_filter_offset, | 416 filter_y.FilterForValue(num_output_rows - 1, &last_filter_offset, |
781 &last_filter_length); | 417 &last_filter_length); |
782 | 418 |
783 for (int out_y = 0; out_y < num_output_rows; out_y++) { | 419 for (int out_y = 0; out_y < num_output_rows; out_y++) { |
784 filter_values = filter_y.FilterForValue(out_y, | 420 filter_values = filter_y.FilterForValue(out_y, |
785 &filter_offset, &filter_length); | 421 &filter_offset, &filter_length); |
786 | 422 |
787 // Generate output rows until we have enough to run the current filter. | 423 // Generate output rows until we have enough to run the current filter. |
788 if (use_sse2) { | 424 while (next_x_row < filter_offset + filter_length) { |
789 while (next_x_row < filter_offset + filter_length) { | 425 if (simd.convolve_4rows_horizontally && |
790 if (next_x_row + 3 < last_filter_offset + last_filter_length - | 426 next_x_row + 3 < last_filter_offset + last_filter_length - |
791 avoid_sse_rows) { | 427 avoid_simd_rows) { |
792 const unsigned char* src[4]; | 428 const unsigned char* src[4]; |
793 unsigned char* out_row[4]; | 429 unsigned char* out_row[4]; |
794 for (int i = 0; i < 4; ++i) { | 430 for (int i = 0; i < 4; ++i) { |
795 src[i] = &source_data[(next_x_row + i) * source_byte_row_stride]; | 431 src[i] = &source_data[(next_x_row + i) * source_byte_row_stride]; |
796 out_row[i] = row_buffer.AdvanceRow(); | 432 out_row[i] = row_buffer.AdvanceRow(); |
797 } | 433 } |
798 ConvolveHorizontally4_SSE2(src, filter_x, out_row); | 434 simd.convolve_4rows_horizontally(src, filter_x, out_row); |
799 next_x_row += 4; | 435 next_x_row += 4; |
436 } else { | |
437 // Check if we need to avoid SSE2 for this row. | |
438 if (simd.convolve_horizontally && | |
439 next_x_row < last_filter_offset + last_filter_length - | |
440 avoid_simd_rows) { | |
441 simd.convolve_horizontally( | |
442 &source_data[next_x_row * source_byte_row_stride], | |
443 filter_x, row_buffer.AdvanceRow()); | |
800 } else { | 444 } else { |
801 // Check if we need to avoid SSE2 for this row. | 445 if (source_has_alpha) { |
802 if (next_x_row >= last_filter_offset + last_filter_length - | 446 ConvolveHorizontally<true>( |
803 avoid_sse_rows) { | 447 &source_data[next_x_row * source_byte_row_stride], |
804 if (source_has_alpha) { | 448 filter_x, row_buffer.AdvanceRow()); |
805 ConvolveHorizontally<true>( | |
806 &source_data[next_x_row * source_byte_row_stride], | |
807 filter_x, row_buffer.AdvanceRow()); | |
808 } else { | |
809 ConvolveHorizontally<false>( | |
810 &source_data[next_x_row * source_byte_row_stride], | |
811 filter_x, row_buffer.AdvanceRow()); | |
812 } | |
813 } else { | 449 } else { |
814 ConvolveHorizontally_SSE2( | 450 ConvolveHorizontally<false>( |
815 &source_data[next_x_row * source_byte_row_stride], | 451 &source_data[next_x_row * source_byte_row_stride], |
816 filter_x, row_buffer.AdvanceRow()); | 452 filter_x, row_buffer.AdvanceRow()); |
817 } | 453 } |
818 next_x_row++; | |
819 } | |
820 } | |
821 } else { | |
822 while (next_x_row < filter_offset + filter_length) { | |
823 if (source_has_alpha) { | |
824 ConvolveHorizontally<true>( | |
825 &source_data[next_x_row * source_byte_row_stride], | |
826 filter_x, row_buffer.AdvanceRow()); | |
827 } else { | |
828 ConvolveHorizontally<false>( | |
829 &source_data[next_x_row * source_byte_row_stride], | |
830 filter_x, row_buffer.AdvanceRow()); | |
831 } | 454 } |
832 next_x_row++; | 455 next_x_row++; |
833 } | 456 } |
834 } | 457 } |
835 | 458 |
836 // Compute where in the output image this row of final data will go. | 459 // Compute where in the output image this row of final data will go. |
837 unsigned char* cur_output_row = &output[out_y * output_byte_row_stride]; | 460 unsigned char* cur_output_row = &output[out_y * output_byte_row_stride]; |
838 | 461 |
839 // Get the list of rows that the circular buffer has, in order. | 462 // Get the list of rows that the circular buffer has, in order. |
840 int first_row_in_circular_buffer; | 463 int first_row_in_circular_buffer; |
841 unsigned char* const* rows_to_convolve = | 464 unsigned char* const* rows_to_convolve = |
842 row_buffer.GetRowAddresses(&first_row_in_circular_buffer); | 465 row_buffer.GetRowAddresses(&first_row_in_circular_buffer); |
843 | 466 |
844 // Now compute the start of the subset of those rows that the filter | 467 // Now compute the start of the subset of those rows that the filter |
845 // needs. | 468 // needs. |
846 unsigned char* const* first_row_for_filter = | 469 unsigned char* const* first_row_for_filter = |
847 &rows_to_convolve[filter_offset - first_row_in_circular_buffer]; | 470 &rows_to_convolve[filter_offset - first_row_in_circular_buffer]; |
848 | 471 |
849 if (source_has_alpha) { | 472 if (simd.convolve_vertically) { |
850 if (use_sse2) { | 473 simd.convolve_vertically(filter_values, filter_length, |
851 ConvolveVertically_SSE2<true>(filter_values, filter_length, | 474 first_row_for_filter, |
852 first_row_for_filter, | 475 filter_x.num_values(), cur_output_row, |
853 filter_x.num_values(), cur_output_row); | 476 source_has_alpha); |
854 } else { | 477 } else { |
478 if (source_has_alpha) { | |
855 ConvolveVertically<true>(filter_values, filter_length, | 479 ConvolveVertically<true>(filter_values, filter_length, |
856 first_row_for_filter, | 480 first_row_for_filter, |
857 filter_x.num_values(), cur_output_row); | 481 filter_x.num_values(), cur_output_row); |
858 } | |
859 } else { | |
860 if (use_sse2) { | |
861 ConvolveVertically_SSE2<false>(filter_values, filter_length, | |
862 first_row_for_filter, | |
863 filter_x.num_values(), cur_output_row); | |
864 } else { | 482 } else { |
865 ConvolveVertically<false>(filter_values, filter_length, | 483 ConvolveVertically<false>(filter_values, filter_length, |
866 first_row_for_filter, | 484 first_row_for_filter, |
867 filter_x.num_values(), cur_output_row); | 485 filter_x.num_values(), cur_output_row); |
868 } | 486 } |
869 } | 487 } |
870 } | 488 } |
871 } | 489 } |
872 | 490 |
873 } // namespace skia | 491 } // namespace skia |
OLD | NEW |