| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkCodecPriv.h" | 8 #include "SkCodecPriv.h" |
| 9 #include "SkColorPriv.h" | 9 #include "SkColorPriv.h" |
| 10 #include "SkOpts.h" |
| 10 #include "SkSwizzler.h" | 11 #include "SkSwizzler.h" |
| 11 #include "SkTemplates.h" | 12 #include "SkTemplates.h" |
| 12 | 13 |
| 13 // samples the row. Does not do anything else but sampling | 14 // samples the row. Does not do anything else but sampling |
| 14 static void sample565(void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, | 15 static void sample565(void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, |
| 15 int width, int bpp, int deltaSrc, int offset, const SkPMColor ctable[]){ | 16 int width, int bpp, int deltaSrc, int offset, const SkPMColor ctable[]){ |
| 16 | 17 |
| 17 src += offset; | 18 src += offset; |
| 18 uint16_t* SK_RESTRICT dst = (uint16_t*) dstRow; | 19 uint16_t* SK_RESTRICT dst = (uint16_t*) dstRow; |
| 19 for (int x = 0; x < width; x++) { | 20 for (int x = 0; x < width; x++) { |
| (...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 int bytesPerPixel, int deltaSrc, int offset, const SkPMColor ctable[]) { | 361 int bytesPerPixel, int deltaSrc, int offset, const SkPMColor ctable[]) { |
| 361 | 362 |
| 362 src += offset; | 363 src += offset; |
| 363 uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow; | 364 uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow; |
| 364 for (int x = 0; x < dstWidth; x++) { | 365 for (int x = 0; x < dstWidth; x++) { |
| 365 dst[x] = SkPack888ToRGB16(src[0], src[1], src[2]); | 366 dst[x] = SkPack888ToRGB16(src[0], src[1], src[2]); |
| 366 src += deltaSrc; | 367 src += deltaSrc; |
| 367 } | 368 } |
| 368 } | 369 } |
| 369 | 370 |
| 370 | |
| 371 // kRGBA | 371 // kRGBA |
| 372 static void swizzle_rgba_to_n32_premul( | 372 static void swizzle_rgba_to_n32_premul( |
| 373 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth, | 373 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth, |
| 374 int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) { | 374 int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) { |
| 375 | 375 |
| 376 src += offset; | 376 src += offset; |
| 377 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; | 377 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
| 378 for (int x = 0; x < dstWidth; x++) { | 378 for (int x = 0; x < dstWidth; x++) { |
| 379 unsigned alpha = src[3]; | 379 unsigned alpha = src[3]; |
| 380 dst[x] = SkPremultiplyARGBInline(alpha, src[0], src[1], src[2]); | 380 dst[x] = SkPremultiplyARGBInline(alpha, src[0], src[1], src[2]); |
| 381 src += deltaSrc; | 381 src += deltaSrc; |
| 382 } | 382 } |
| 383 } | 383 } |
| 384 | 384 |
| 385 static void fast_swizzle_rgba_to_n32_premul( |
| 386 void* dst, const uint8_t* src, int width, int bpp, int deltaSrc, |
| 387 int offset, const SkPMColor ctable[]) { |
| 388 |
| 389 // This function must not be called if we are sampling. If we are not |
| 390 // sampling, deltaSrc should equal bpp. |
| 391 SkASSERT(deltaSrc == bpp); |
| 392 |
| 393 #ifdef SK_PMCOLOR_IS_RGBA |
| 394 SkOpts::premul_xxxa((uint32_t*) dst, (const uint32_t*) (src + offset), width
); |
| 395 #else |
| 396 SkOpts::premul_swaprb_xxxa((uint32_t*) dst, (const uint32_t*) (src + offset)
, width); |
| 397 #endif |
| 398 } |
| 399 |
| 385 static void swizzle_rgba_to_n32_unpremul( | 400 static void swizzle_rgba_to_n32_unpremul( |
| 386 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth, | 401 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth, |
| 387 int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) { | 402 int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) { |
| 388 | 403 |
| 389 src += offset; | 404 src += offset; |
| 390 uint32_t* SK_RESTRICT dst = reinterpret_cast<uint32_t*>(dstRow); | 405 uint32_t* SK_RESTRICT dst = reinterpret_cast<uint32_t*>(dstRow); |
| 391 for (int x = 0; x < dstWidth; x++) { | 406 for (int x = 0; x < dstWidth; x++) { |
| 392 unsigned alpha = src[3]; | 407 unsigned alpha = src[3]; |
| 393 dst[x] = SkPackARGB32NoCheck(alpha, src[0], src[1], src[2]); | 408 dst[x] = SkPackARGB32NoCheck(alpha, src[0], src[1], src[2]); |
| 394 src += deltaSrc; | 409 src += deltaSrc; |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 const SkImageInfo& dstInfo, | 511 const SkImageInfo& dstInfo, |
| 497 const SkCodec::Options& options, | 512 const SkCodec::Options& options, |
| 498 const SkIRect* frame) { | 513 const SkIRect* frame) { |
| 499 if (dstInfo.colorType() == kUnknown_SkColorType || kUnknown == sc) { | 514 if (dstInfo.colorType() == kUnknown_SkColorType || kUnknown == sc) { |
| 500 return nullptr; | 515 return nullptr; |
| 501 } | 516 } |
| 502 if ((kIndex == sc || kIndex4 == sc || kIndex2 == sc || kIndex1 == sc) | 517 if ((kIndex == sc || kIndex4 == sc || kIndex2 == sc || kIndex1 == sc) |
| 503 && nullptr == ctable) { | 518 && nullptr == ctable) { |
| 504 return nullptr; | 519 return nullptr; |
| 505 } | 520 } |
| 521 RowProc fastProc = nullptr; |
| 506 RowProc proc = nullptr; | 522 RowProc proc = nullptr; |
| 507 SkCodec::ZeroInitialized zeroInit = options.fZeroInitialized; | 523 SkCodec::ZeroInitialized zeroInit = options.fZeroInitialized; |
| 508 switch (sc) { | 524 switch (sc) { |
| 509 case kBit: | 525 case kBit: |
| 510 switch (dstInfo.colorType()) { | 526 switch (dstInfo.colorType()) { |
| 511 case kN32_SkColorType: | 527 case kN32_SkColorType: |
| 512 proc = &swizzle_bit_to_n32; | 528 proc = &swizzle_bit_to_n32; |
| 513 break; | 529 break; |
| 514 case kIndex_8_SkColorType: | 530 case kIndex_8_SkColorType: |
| 515 proc = &swizzle_bit_to_index; | 531 proc = &swizzle_bit_to_index; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 603 break; | 619 break; |
| 604 default: | 620 default: |
| 605 break; | 621 break; |
| 606 } | 622 } |
| 607 break; | 623 break; |
| 608 default: | 624 default: |
| 609 break; | 625 break; |
| 610 } | 626 } |
| 611 break; | 627 break; |
| 612 case kRGBX: | 628 case kRGBX: |
| 613 // TODO: Support other swizzles. | |
| 614 switch (dstInfo.colorType()) { | 629 switch (dstInfo.colorType()) { |
| 615 case kN32_SkColorType: | 630 case kN32_SkColorType: |
| 616 proc = &swizzle_rgbx_to_n32; | 631 proc = &swizzle_rgbx_to_n32; |
| 617 break; | 632 break; |
| 618 case kRGB_565_SkColorType: | 633 case kRGB_565_SkColorType: |
| 619 proc = &swizzle_rgbx_to_565; | 634 proc = &swizzle_rgbx_to_565; |
| 620 default: | 635 default: |
| 621 break; | 636 break; |
| 622 } | 637 } |
| 623 break; | 638 break; |
| 624 case kRGBA: | 639 case kRGBA: |
| 625 switch (dstInfo.colorType()) { | 640 switch (dstInfo.colorType()) { |
| 626 case kN32_SkColorType: | 641 case kN32_SkColorType: |
| 627 if (dstInfo.alphaType() == kUnpremul_SkAlphaType) { | 642 if (dstInfo.alphaType() == kUnpremul_SkAlphaType) { |
| 628 if (SkCodec::kYes_ZeroInitialized == zeroInit) { | 643 if (SkCodec::kYes_ZeroInitialized == zeroInit) { |
| 629 proc = &SkipLeading8888ZerosThen<swizzle_rgba_to_n32
_unpremul>; | 644 proc = &SkipLeading8888ZerosThen<swizzle_rgba_to_n32
_unpremul>; |
| 630 } else { | 645 } else { |
| 631 proc = &swizzle_rgba_to_n32_unpremul; | 646 proc = &swizzle_rgba_to_n32_unpremul; |
| 632 } | 647 } |
| 633 } else { | 648 } else { |
| 634 if (SkCodec::kYes_ZeroInitialized == zeroInit) { | 649 if (SkCodec::kYes_ZeroInitialized == zeroInit) { |
| 635 proc = &SkipLeading8888ZerosThen<swizzle_rgba_to_n32
_premul>; | 650 proc = &SkipLeading8888ZerosThen<swizzle_rgba_to_n32
_premul>; |
| 651 fastProc = &SkipLeading8888ZerosThen<fast_swizzle_rg
ba_to_n32_premul>; |
| 636 } else { | 652 } else { |
| 637 proc = &swizzle_rgba_to_n32_premul; | 653 proc = &swizzle_rgba_to_n32_premul; |
| 654 fastProc = &fast_swizzle_rgba_to_n32_premul; |
| 638 } | 655 } |
| 639 } | 656 } |
| 640 break; | 657 break; |
| 641 default: | 658 default: |
| 642 break; | 659 break; |
| 643 } | 660 } |
| 644 break; | 661 break; |
| 645 case kRGB: | 662 case kRGB: |
| 646 switch (dstInfo.colorType()) { | 663 switch (dstInfo.colorType()) { |
| 647 case kN32_SkColorType: | 664 case kN32_SkColorType: |
| (...skipping 20 matching lines...) Expand all Loading... |
| 668 case kRGB_565_SkColorType: | 685 case kRGB_565_SkColorType: |
| 669 proc = &swizzle_cmyk_to_565; | 686 proc = &swizzle_cmyk_to_565; |
| 670 break; | 687 break; |
| 671 default: | 688 default: |
| 672 break; | 689 break; |
| 673 } | 690 } |
| 674 break; | 691 break; |
| 675 default: | 692 default: |
| 676 break; | 693 break; |
| 677 } | 694 } |
| 678 if (nullptr == proc) { | |
| 679 return nullptr; | |
| 680 } | |
| 681 | 695 |
| 682 // Store bpp in bytes if it is an even multiple, otherwise use bits | 696 // Store bpp in bytes if it is an even multiple, otherwise use bits |
| 683 int srcBPP = SkIsAlign8(BitsPerPixel(sc)) ? BytesPerPixel(sc) : BitsPerPixel
(sc); | 697 int srcBPP = SkIsAlign8(BitsPerPixel(sc)) ? BytesPerPixel(sc) : BitsPerPixel
(sc); |
| 684 int dstBPP = SkColorTypeBytesPerPixel(dstInfo.colorType()); | 698 int dstBPP = SkColorTypeBytesPerPixel(dstInfo.colorType()); |
| 685 | 699 |
| 686 int srcOffset = 0; | 700 int srcOffset = 0; |
| 687 int srcWidth = dstInfo.width(); | 701 int srcWidth = dstInfo.width(); |
| 688 int dstOffset = 0; | 702 int dstOffset = 0; |
| 689 int dstWidth = srcWidth; | 703 int dstWidth = srcWidth; |
| 690 if (options.fSubset) { | 704 if (options.fSubset) { |
| 691 // We do not currently support subset decodes for image types that may h
ave | 705 // We do not currently support subset decodes for image types that may h
ave |
| 692 // frames (gif). | 706 // frames (gif). |
| 693 SkASSERT(!frame); | 707 SkASSERT(!frame); |
| 694 srcOffset = options.fSubset->left(); | 708 srcOffset = options.fSubset->left(); |
| 695 srcWidth = options.fSubset->width(); | 709 srcWidth = options.fSubset->width(); |
| 696 dstWidth = srcWidth; | 710 dstWidth = srcWidth; |
| 697 } else if (frame) { | 711 } else if (frame) { |
| 698 dstOffset = frame->left(); | 712 dstOffset = frame->left(); |
| 699 srcWidth = frame->width(); | 713 srcWidth = frame->width(); |
| 700 } | 714 } |
| 701 | 715 |
| 702 return new SkSwizzler(proc, ctable, srcOffset, srcWidth, dstOffset, dstWidth
, srcBPP, dstBPP); | 716 return new SkSwizzler(fastProc, proc, ctable, srcOffset, srcWidth, dstOffset
, dstWidth, |
| 717 srcBPP, dstBPP); |
| 703 } | 718 } |
| 704 | 719 |
| 705 SkSwizzler::SkSwizzler(RowProc proc, const SkPMColor* ctable, int srcOffset, int
srcWidth, | 720 SkSwizzler::SkSwizzler(RowProc fastProc, RowProc proc, const SkPMColor* ctable,
int srcOffset, |
| 706 int dstOffset, int dstWidth, int srcBPP, int dstBPP) | 721 int srcWidth, int dstOffset, int dstWidth, int srcBPP, int dstBPP) |
| 707 : fRowProc(proc) | 722 : fFastProc(fastProc) |
| 723 , fProc(proc) |
| 708 , fColorTable(ctable) | 724 , fColorTable(ctable) |
| 709 , fSrcOffset(srcOffset) | 725 , fSrcOffset(srcOffset) |
| 710 , fDstOffset(dstOffset) | 726 , fDstOffset(dstOffset) |
| 711 , fSrcOffsetUnits(srcOffset * srcBPP) | 727 , fSrcOffsetUnits(srcOffset * srcBPP) |
| 712 , fDstOffsetBytes(dstOffset * dstBPP) | 728 , fDstOffsetBytes(dstOffset * dstBPP) |
| 713 , fSrcWidth(srcWidth) | 729 , fSrcWidth(srcWidth) |
| 714 , fDstWidth(dstWidth) | 730 , fDstWidth(dstWidth) |
| 715 , fSwizzleWidth(srcWidth) | 731 , fSwizzleWidth(srcWidth) |
| 716 , fAllocatedWidth(dstWidth) | 732 , fAllocatedWidth(dstWidth) |
| 717 , fSampleX(1) | 733 , fSampleX(1) |
| 718 , fSrcBPP(srcBPP) | 734 , fSrcBPP(srcBPP) |
| 719 , fDstBPP(dstBPP) | 735 , fDstBPP(dstBPP) |
| 720 {} | 736 {} |
| 721 | 737 |
| 722 int SkSwizzler::onSetSampleX(int sampleX) { | 738 int SkSwizzler::onSetSampleX(int sampleX) { |
| 723 SkASSERT(sampleX > 0); // Surely there is an upper limit? Should there be | 739 SkASSERT(sampleX > 0); // Surely there is an upper limit? Should there be |
| 724 // way to report failure? | 740 // way to report failure? |
| 725 fSampleX = sampleX; | 741 fSampleX = sampleX; |
| 726 fSrcOffsetUnits = (get_start_coord(sampleX) + fSrcOffset) * fSrcBPP; | 742 fSrcOffsetUnits = (get_start_coord(sampleX) + fSrcOffset) * fSrcBPP; |
| 727 fDstOffsetBytes = (fDstOffset / sampleX) * fDstBPP; | 743 fDstOffsetBytes = (fDstOffset / sampleX) * fDstBPP; |
| 728 fSwizzleWidth = get_scaled_dimension(fSrcWidth, sampleX); | 744 fSwizzleWidth = get_scaled_dimension(fSrcWidth, sampleX); |
| 729 fAllocatedWidth = get_scaled_dimension(fDstWidth, sampleX); | 745 fAllocatedWidth = get_scaled_dimension(fDstWidth, sampleX); |
| 730 | 746 |
| 747 // The optimized swizzler routines do not (yet) support sampling. |
| 748 fFastProc = nullptr; |
| 749 |
| 731 return fAllocatedWidth; | 750 return fAllocatedWidth; |
| 732 } | 751 } |
| 733 | 752 |
| 734 void SkSwizzler::swizzle(void* dst, const uint8_t* SK_RESTRICT src) { | 753 void SkSwizzler::swizzle(void* dst, const uint8_t* SK_RESTRICT src) { |
| 735 SkASSERT(nullptr != dst && nullptr != src); | 754 SkASSERT(nullptr != dst && nullptr != src); |
| 736 fRowProc(SkTAddOffset<void>(dst, fDstOffsetBytes), src, fSwizzleWidth, fSrcB
PP, | 755 RowProc proc = fFastProc ? fFastProc : fProc; |
| 756 proc(SkTAddOffset<void>(dst, fDstOffsetBytes), src, fSwizzleWidth, fSrcBPP, |
| 737 fSampleX * fSrcBPP, fSrcOffsetUnits, fColorTable); | 757 fSampleX * fSrcBPP, fSrcOffsetUnits, fColorTable); |
| 738 } | 758 } |
| OLD | NEW |