Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(256)

Side by Side Diff: src/core/SkLinearBitmapPipeline_sample.h

Issue 1967793003: Use common code from SkPM4fPriv (Closed) Base URL: https://skia.googlesource.com/skia.git@refactor-LP-sample
Patch Set: Address Florin's comments. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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
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 return colorProfile == kSRGB_SkColorProfileType
634 Sk4f pixel = SkNx_cast<float, uint8_t>(bytePixel); 626 ? Sk4f_fromS32(*src)
635 pixel = pixel * (1.0f/255.0f); 627 : Sk4f_fromL32(*src);
636 if (colorProfile == kSRGB_SkColorProfileType) {
637 pixel = sRGBFast::sRGBToLinear(pixel);
638 }
639 return pixel;
640 } 628 }
641 629
642 Sk4f getPixelAt(int index) { 630 Sk4f getPixelAt(int index) {
643 return this->getPixelFromRow(fSrc, index); 631 return this->getPixelFromRow(fSrc, index);
644 } 632 }
645 633
646 const void* row(int y) { return fSrc + y * fWidth; } 634 const void* row(int y) { return fSrc + y * fWidth; }
647 635
648 private: 636 private:
649 const uint32_t* const fSrc; 637 const uint32_t* const fSrc;
650 const int fWidth; 638 const int fWidth;
651 }; 639 };
652 640
653 using Pixel8888SRGB = PixelAccessor<PixelGetter<kRGBA_8888_SkColorType, kSRGB_Sk ColorProfileType>>; 641 using Pixel8888SRGB = PixelAccessor<PixelGetter<kRGBA_8888_SkColorType, kSRGB_Sk ColorProfileType>>;
654 using Pixel8888LRGB = PixelAccessor<PixelGetter<kRGBA_8888_SkColorType, kLinear_ SkColorProfileType>>; 642 using Pixel8888LRGB = PixelAccessor<PixelGetter<kRGBA_8888_SkColorType, kLinear_ SkColorProfileType>>;
655 643
656 template <SkColorProfileType colorProfile> 644 template <SkColorProfileType colorProfile>
657 class PixelGetter<kBGRA_8888_SkColorType, colorProfile> { 645 class PixelGetter<kBGRA_8888_SkColorType, colorProfile> {
658 public: 646 public:
659 PixelGetter(const SkPixmap& srcPixmap) 647 PixelGetter(const SkPixmap& srcPixmap)
660 : fSrc{srcPixmap.addr32()} 648 : fSrc{srcPixmap.addr32()}
661 , fWidth{srcPixmap.rowBytesAsPixels()} { } 649 , fWidth{srcPixmap.rowBytesAsPixels()} { }
662 650
663 Sk4f getPixelFromRow(const void* row, int index) { 651 Sk4f getPixelFromRow(const void* row, int index) {
664 const uint32_t* src = static_cast<const uint32_t*>(row); 652 const uint32_t* src = static_cast<const uint32_t*>(row);
665 Sk4b bytePixel = Sk4b::Load((uint8_t *)(&src[index])); 653 Sk4f pixel = colorProfile == kSRGB_SkColorProfileType
666 Sk4f pixel = SkNx_cast<float, uint8_t>(bytePixel); 654 ? Sk4f_fromS32(*src)
667 pixel = SkNx_shuffle<2, 1, 0, 3>(pixel); 655 : Sk4f_fromL32(*src);
668 pixel = pixel * (1.0f/255.0f); 656 return SkNx_shuffle<2, 1, 0, 3>(pixel);
669 if (colorProfile == kSRGB_SkColorProfileType) {
670 pixel = sRGBFast::sRGBToLinear(pixel);
671 }
672 return pixel;
673 } 657 }
674 658
675 Sk4f getPixelAt(int index) { 659 Sk4f getPixelAt(int index) {
676 return this->getPixelFromRow(fSrc, index); 660 return this->getPixelFromRow(fSrc, index);
677 } 661 }
678 662
679 const void* row(int y) { return fSrc + y * fWidth; } 663 const void* row(int y) { return fSrc + y * fWidth; }
680 664
681 private: 665 private:
682 const uint32_t* const fSrc; 666 const uint32_t* const fSrc;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
718 702
719 Sk4f getPixelAt(int index) { 703 Sk4f getPixelAt(int index) {
720 return this->getPixelFromRow(fSrc, index); 704 return this->getPixelFromRow(fSrc, index);
721 } 705 }
722 706
723 const void* row(int y) { return fSrc + y * fWidth; } 707 const void* row(int y) { return fSrc + y * fWidth; }
724 708
725 private: 709 private:
726 static const size_t kColorTableSize = sizeof(Sk4f[256]) + 12; 710 static const size_t kColorTableSize = sizeof(Sk4f[256]) + 12;
727 Sk4f convertPixel(SkPMColor pmColor) { 711 Sk4f convertPixel(SkPMColor pmColor) {
728 Sk4b bPixel = Sk4b::Load(&pmColor); 712 Sk4f pixel = to_4f(pmColor);
729 Sk4f pixel = SkNx_cast<float, uint8_t>(bPixel); 713 float alpha = get_alpha(pixel);
730 float alpha = pixel[3];
731 if (alpha != 0.0f) { 714 if (alpha != 0.0f) {
732 float invAlpha = 1.0f / pixel[3]; 715 float invAlpha = 1.0f / alpha;
733 Sk4f normalize = {invAlpha, invAlpha, invAlpha, 1.0f / 255.0f}; 716 Sk4f normalize = {invAlpha, invAlpha, invAlpha, 1.0f / 255.0f};
734 pixel = pixel * normalize; 717 pixel = pixel * normalize;
735 if (colorProfile == kSRGB_SkColorProfileType) { 718 if (colorProfile == kSRGB_SkColorProfileType) {
736 pixel = sRGBFast::sRGBToLinear(pixel); 719 pixel = linear_to_srgb(pixel);
737 } 720 }
738 return pixel; 721 return pixel;
739 } else { 722 } else {
740 return Sk4f{0.0f}; 723 return Sk4f{0.0f};
741 } 724 }
742 } 725 }
743 const uint8_t* const fSrc; 726 const uint8_t* const fSrc;
744 const int fWidth; 727 const int fWidth;
745 SkAutoMalloc fColorTableStorage{kColorTableSize}; 728 SkAutoMalloc fColorTableStorage{kColorTableSize};
746 Sk4f* fColorTable; 729 Sk4f* fColorTable;
(...skipping 23 matching lines...) Expand all
770 private: 753 private:
771 const uint64_t* const fSrc; 754 const uint64_t* const fSrc;
772 const int fWidth; 755 const int fWidth;
773 }; 756 };
774 757
775 using PixelHalfLinear = PixelAccessor<PixelGetter<kRGBA_F16_SkColorType, kLinear _SkColorProfileType>>; 758 using PixelHalfLinear = PixelAccessor<PixelGetter<kRGBA_F16_SkColorType, kLinear _SkColorProfileType>>;
776 759
777 } // namespace 760 } // namespace
778 761
779 #endif // SkLinearBitmapPipeline_sampler_DEFINED 762 #endif // SkLinearBitmapPipeline_sampler_DEFINED
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698