OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 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 #ifndef SkLinearBitmapPipeline_sampler_DEFINED | 8 #ifndef SkLinearBitmapPipeline_sampler_DEFINED |
9 #define SkLinearBitmapPipeline_sampler_DEFINED | 9 #define SkLinearBitmapPipeline_sampler_DEFINED |
10 | 10 |
11 #include <tuple> | |
12 | |
11 #include "SkFixed.h" | 13 #include "SkFixed.h" |
12 #include "SkHalf.h" | 14 #include "SkHalf.h" |
13 #include "SkLinearBitmapPipeline_core.h" | 15 #include "SkLinearBitmapPipeline_core.h" |
14 #include <array> | 16 #include "SkPM4fPriv.h" |
15 #include <tuple> | |
16 | 17 |
17 namespace { | 18 namespace { |
18 // Explaination of the math: | 19 // Explaination of the math: |
19 // 1 - x x | 20 // 1 - x x |
20 // +--------+--------+ | 21 // +--------+--------+ |
21 // | | | | 22 // | | | |
22 // 1 - y | px00 | px10 | | 23 // 1 - y | px00 | px10 | |
23 // | | | | 24 // | | | |
24 // +--------+--------+ | 25 // +--------+--------+ |
25 // | | | | 26 // | | | |
(...skipping 15 matching lines...) Expand all Loading... | |
41 Sk4s fxs = xs - xs.floor(); | 42 Sk4s fxs = xs - xs.floor(); |
42 Sk4s fys = ys - ys.floor(); | 43 Sk4s fys = ys - ys.floor(); |
43 Sk4s fxys{fxs * fys}; | 44 Sk4s fxys{fxs * fys}; |
44 Sk4f sum = px11 * fxys; | 45 Sk4f sum = px11 * fxys; |
45 sum = sum + px01 * (fys - fxys); | 46 sum = sum + px01 * (fys - fxys); |
46 sum = sum + px10 * (fxs - fxys); | 47 sum = sum + px10 * (fxs - fxys); |
47 sum = sum + px00 * (Sk4f{1.0f} - fxs - fys + fxys); | 48 sum = sum + px00 * (Sk4f{1.0f} - fxs - fys + fxys); |
48 return sum; | 49 return sum; |
49 } | 50 } |
50 | 51 |
51 // The GeneralSampler class | |
52 template<typename SourceStrategy, typename Next> | 52 template<typename SourceStrategy, typename Next> |
53 class GeneralSampler { | 53 class GeneralSampler { |
54 public: | 54 public: |
55 template<typename... Args> | 55 template<typename... Args> |
56 GeneralSampler(SkLinearBitmapPipeline::BlendProcessorInterface* next, Args&& ... args) | 56 GeneralSampler(SkLinearBitmapPipeline::BlendProcessorInterface* next, Args&& ... args) |
57 : fNext{next}, fStrategy{std::forward<Args>(args)...} { } | 57 : fNext{next}, fStrategy{std::forward<Args>(args)...} { } |
58 | 58 |
59 GeneralSampler(SkLinearBitmapPipeline::BlendProcessorInterface* next, | 59 GeneralSampler(SkLinearBitmapPipeline::BlendProcessorInterface* next, |
60 const GeneralSampler& sampler) | 60 const GeneralSampler& sampler) |
61 : fNext{next}, fStrategy{sampler.fStrategy} { } | 61 : fNext{next}, fStrategy{sampler.fStrategy} { } |
(...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
553 x += dx; | 553 x += dx; |
554 count -= 1; | 554 count -= 1; |
555 } | 555 } |
556 } | 556 } |
557 } | 557 } |
558 | 558 |
559 Next* const fNext; | 559 Next* const fNext; |
560 SourceStrategy fStrategy; | 560 SourceStrategy fStrategy; |
561 }; | 561 }; |
562 | 562 |
563 class sRGBFast { | |
564 public: | |
565 static Sk4s VECTORCALL sRGBToLinear(Sk4s pixel) { | |
566 Sk4s l = pixel * pixel; | |
567 return Sk4s{l[0], l[1], l[2], pixel[3]}; | |
568 } | |
569 }; | |
570 | |
571 template <typename PixelGetter> | 563 template <typename PixelGetter> |
572 class PixelAccessor { | 564 class PixelAccessor { |
573 public: | 565 public: |
574 PixelAccessor(const SkPixmap& srcPixmap) | 566 PixelAccessor(const SkPixmap& srcPixmap) |
575 : fWidth{srcPixmap.rowBytesAsPixels()} | 567 : fWidth{srcPixmap.rowBytesAsPixels()} |
576 , fGetter{srcPixmap} { } | 568 , fGetter{srcPixmap} { } |
577 | 569 |
578 void VECTORCALL getFewPixels(int n, Sk4s xs, Sk4s ys, Sk4f* px0, Sk4f* px1, Sk4f* px2) { | 570 void VECTORCALL getFewPixels(int n, Sk4s xs, Sk4s ys, Sk4f* px0, Sk4f* px1, Sk4f* px2) { |
579 Sk4i XIs = SkNx_cast<int, SkScalar>(xs); | 571 Sk4i XIs = SkNx_cast<int, SkScalar>(xs); |
580 Sk4i YIs = SkNx_cast<int, SkScalar>(ys); | 572 Sk4i YIs = SkNx_cast<int, SkScalar>(ys); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
623 | 615 |
624 template <SkColorProfileType colorProfile> | 616 template <SkColorProfileType colorProfile> |
625 class PixelGetter<kRGBA_8888_SkColorType, colorProfile> { | 617 class PixelGetter<kRGBA_8888_SkColorType, colorProfile> { |
626 public: | 618 public: |
627 PixelGetter(const SkPixmap& srcPixmap) | 619 PixelGetter(const SkPixmap& srcPixmap) |
628 : fSrc{srcPixmap.addr32()} | 620 : fSrc{srcPixmap.addr32()} |
629 , fWidth{srcPixmap.rowBytesAsPixels()} { } | 621 , fWidth{srcPixmap.rowBytesAsPixels()} { } |
630 | 622 |
631 Sk4f getPixelFromRow(const void* row, int index) { | 623 Sk4f getPixelFromRow(const void* row, int index) { |
632 const uint32_t* src = static_cast<const uint32_t*>(row); | 624 const uint32_t* src = static_cast<const uint32_t*>(row); |
633 Sk4b bytePixel = Sk4b::Load((uint8_t *)(&src[index])); | 625 Sk4f pixel = to_4f(*src); |
634 Sk4f pixel = SkNx_cast<float, uint8_t>(bytePixel); | |
635 pixel = pixel * (1.0f/255.0f); | 626 pixel = pixel * (1.0f/255.0f); |
f(malita)
2016/05/11 16:54:03
Nit: these two lines are equivalent to Sk4f_fromL3
herb_g
2016/05/11 18:15:17
Done.
| |
636 if (colorProfile == kSRGB_SkColorProfileType) { | 627 if (colorProfile == kSRGB_SkColorProfileType) { |
637 pixel = sRGBFast::sRGBToLinear(pixel); | 628 pixel = srgb_to_linear(pixel); |
638 } | 629 } |
639 return pixel; | 630 return pixel; |
640 } | 631 } |
641 | 632 |
642 Sk4f getPixelAt(int index) { | 633 Sk4f getPixelAt(int index) { |
643 return this->getPixelFromRow(fSrc, index); | 634 return this->getPixelFromRow(fSrc, index); |
644 } | 635 } |
645 | 636 |
646 const void* row(int y) { return fSrc + y * fWidth; } | 637 const void* row(int y) { return fSrc + y * fWidth; } |
647 | 638 |
648 private: | 639 private: |
649 const uint32_t* const fSrc; | 640 const uint32_t* const fSrc; |
650 const int fWidth; | 641 const int fWidth; |
651 }; | 642 }; |
652 | 643 |
653 using Pixel8888SRGB = PixelAccessor<PixelGetter<kRGBA_8888_SkColorType, kSRGB_Sk ColorProfileType>>; | 644 using Pixel8888SRGB = PixelAccessor<PixelGetter<kRGBA_8888_SkColorType, kSRGB_Sk ColorProfileType>>; |
654 using Pixel8888LRGB = PixelAccessor<PixelGetter<kRGBA_8888_SkColorType, kLinear_ SkColorProfileType>>; | 645 using Pixel8888LRGB = PixelAccessor<PixelGetter<kRGBA_8888_SkColorType, kLinear_ SkColorProfileType>>; |
655 | 646 |
656 template <SkColorProfileType colorProfile> | 647 template <SkColorProfileType colorProfile> |
657 class PixelGetter<kBGRA_8888_SkColorType, colorProfile> { | 648 class PixelGetter<kBGRA_8888_SkColorType, colorProfile> { |
658 public: | 649 public: |
659 PixelGetter(const SkPixmap& srcPixmap) | 650 PixelGetter(const SkPixmap& srcPixmap) |
660 : fSrc{srcPixmap.addr32()} | 651 : fSrc{srcPixmap.addr32()} |
661 , fWidth{srcPixmap.rowBytesAsPixels()} { } | 652 , fWidth{srcPixmap.rowBytesAsPixels()} { } |
662 | 653 |
663 Sk4f getPixelFromRow(const void* row, int index) { | 654 Sk4f getPixelFromRow(const void* row, int index) { |
664 const uint32_t* src = static_cast<const uint32_t*>(row); | 655 const uint32_t* src = static_cast<const uint32_t*>(row); |
665 Sk4b bytePixel = Sk4b::Load((uint8_t *)(&src[index])); | 656 Sk4f pixel = to_4f(*src); |
666 Sk4f pixel = SkNx_cast<float, uint8_t>(bytePixel); | |
667 pixel = SkNx_shuffle<2, 1, 0, 3>(pixel); | 657 pixel = SkNx_shuffle<2, 1, 0, 3>(pixel); |
668 pixel = pixel * (1.0f/255.0f); | 658 pixel = pixel * (1.0f/255.0f); |
669 if (colorProfile == kSRGB_SkColorProfileType) { | 659 if (colorProfile == kSRGB_SkColorProfileType) { |
670 pixel = sRGBFast::sRGBToLinear(pixel); | 660 pixel = srgb_to_linear(pixel); |
671 } | 661 } |
f(malita)
2016/05/11 16:54:03
Nit: maybe also rewrite as
Sk4f pixel = colorPr
herb_g
2016/05/11 18:15:17
Done.
| |
672 return pixel; | 662 return pixel; |
673 } | 663 } |
674 | 664 |
675 Sk4f getPixelAt(int index) { | 665 Sk4f getPixelAt(int index) { |
676 return this->getPixelFromRow(fSrc, index); | 666 return this->getPixelFromRow(fSrc, index); |
677 } | 667 } |
678 | 668 |
679 const void* row(int y) { return fSrc + y * fWidth; } | 669 const void* row(int y) { return fSrc + y * fWidth; } |
680 | 670 |
681 private: | 671 private: |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
718 | 708 |
719 Sk4f getPixelAt(int index) { | 709 Sk4f getPixelAt(int index) { |
720 return this->getPixelFromRow(fSrc, index); | 710 return this->getPixelFromRow(fSrc, index); |
721 } | 711 } |
722 | 712 |
723 const void* row(int y) { return fSrc + y * fWidth; } | 713 const void* row(int y) { return fSrc + y * fWidth; } |
724 | 714 |
725 private: | 715 private: |
726 static const size_t kColorTableSize = sizeof(Sk4f[256]) + 12; | 716 static const size_t kColorTableSize = sizeof(Sk4f[256]) + 12; |
727 Sk4f convertPixel(SkPMColor pmColor) { | 717 Sk4f convertPixel(SkPMColor pmColor) { |
728 Sk4b bPixel = Sk4b::Load(&pmColor); | 718 Sk4f pixel = to_4f(pmColor); |
729 Sk4f pixel = SkNx_cast<float, uint8_t>(bPixel); | 719 float alpha = get_alpha(pixel); |
730 float alpha = pixel[3]; | |
731 if (alpha != 0.0f) { | 720 if (alpha != 0.0f) { |
732 float invAlpha = 1.0f / pixel[3]; | 721 float invAlpha = 1.0f / alpha; |
733 Sk4f normalize = {invAlpha, invAlpha, invAlpha, 1.0f / 255.0f}; | 722 Sk4f normalize = {invAlpha, invAlpha, invAlpha, 1.0f / 255.0f}; |
734 pixel = pixel * normalize; | 723 pixel = pixel * normalize; |
735 if (colorProfile == kSRGB_SkColorProfileType) { | 724 if (colorProfile == kSRGB_SkColorProfileType) { |
736 pixel = sRGBFast::sRGBToLinear(pixel); | 725 pixel = linear_to_srgb(pixel); |
737 } | 726 } |
738 return pixel; | 727 return pixel; |
739 } else { | 728 } else { |
740 return Sk4f{0.0f}; | 729 return Sk4f{0.0f}; |
741 } | 730 } |
742 } | 731 } |
743 const uint8_t* const fSrc; | 732 const uint8_t* const fSrc; |
744 const int fWidth; | 733 const int fWidth; |
745 SkAutoMalloc fColorTableStorage{kColorTableSize}; | 734 SkAutoMalloc fColorTableStorage{kColorTableSize}; |
746 Sk4f* fColorTable; | 735 Sk4f* fColorTable; |
(...skipping 23 matching lines...) Expand all Loading... | |
770 private: | 759 private: |
771 const uint64_t* const fSrc; | 760 const uint64_t* const fSrc; |
772 const int fWidth; | 761 const int fWidth; |
773 }; | 762 }; |
774 | 763 |
775 using PixelHalfLinear = PixelAccessor<PixelGetter<kRGBA_F16_SkColorType, kLinear _SkColorProfileType>>; | 764 using PixelHalfLinear = PixelAccessor<PixelGetter<kRGBA_F16_SkColorType, kLinear _SkColorProfileType>>; |
776 | 765 |
777 } // namespace | 766 } // namespace |
778 | 767 |
779 #endif // SkLinearBitmapPipeline_sampler_DEFINED | 768 #endif // SkLinearBitmapPipeline_sampler_DEFINED |
OLD | NEW |