Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 1 | in uniform sampler2D image; |
| 2 | in uniform colorSpaceXform colorXform; |
| 3 | in uniform sampler2D mask; |
| 4 | in uniform float innerThreshold; |
| 5 | in uniform float outerThreshold; |
| 6 | |
| 7 | @class { |
| 8 | inline OptimizationFlags optFlags(float outerThreshold); |
| 9 | } |
| 10 | |
| 11 | @constructorParams { |
| 12 | const SkIRect& bounds |
| 13 | } |
| 14 | |
| 15 | @make { |
| 16 | static sk_sp<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> image, |
| 17 | sk_sp<GrColorSpaceXform> colorXform, |
| 18 | sk_sp<GrTextureProxy> mask, |
| 19 | float innerThreshold, |
| 20 | float outerThreshold, |
| 21 | const SkIRect& bounds) { |
| 22 | return sk_sp<GrFragmentProcessor>(new GrAlphaThresholdFragmentProcessor(image, |
| 23 | colorXform, |
| 24 | mask, |
| 25 | innerThreshold, |
| 26 | outerThreshold, |
| 27 | bounds)); |
| 28 | } |
| 29 | } |
| 30 | |
Ethan Nicholas | baf981f | 2017-07-12 13:51:34 +0000 | [diff] [blame] | 31 | @fields { |
| 32 | GrCoordTransform fImageCoordTransform; |
| 33 | GrCoordTransform fMaskCoordTransform; |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 34 | } |
| 35 | |
Ethan Nicholas | baf981f | 2017-07-12 13:51:34 +0000 | [diff] [blame] | 36 | @initializers { |
| 37 | fImageCoordTransform(SkMatrix::I(), image.get()), |
| 38 | fMaskCoordTransform(SkMatrix::MakeTrans(SkIntToScalar(-bounds.x()), SkIntToScalar(-bounds.y())), |
| 39 | mask.get()) |
| 40 | } |
| 41 | |
| 42 | @constructorCode { |
| 43 | this->addCoordTransform(&fImageCoordTransform); |
| 44 | this->addCoordTransform(&fMaskCoordTransform); |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | @header { |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 48 | #include "GrColorSpaceXform.h" |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 49 | } |
| 50 | |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 51 | @cpp { |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 52 | inline GrFragmentProcessor::OptimizationFlags GrAlphaThresholdFragmentProcessor::optFlags( |
| 53 | float outerThreshold) { |
| 54 | if (outerThreshold >= 1.0) { |
| 55 | return kPreservesOpaqueInput_OptimizationFlag | |
| 56 | kCompatibleWithCoverageAsAlpha_OptimizationFlag; |
| 57 | } else { |
| 58 | return kCompatibleWithCoverageAsAlpha_OptimizationFlag; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 63 | void main() { |
| 64 | vec4 color = texture(image, sk_TransformedCoords2D[0], colorXform); |
| 65 | vec4 mask_color = texture(mask, sk_TransformedCoords2D[1]); |
| 66 | if (mask_color.a < 0.5) { |
| 67 | if (color.a > outerThreshold) { |
| 68 | float scale = outerThreshold / color.a; |
| 69 | color.rgb *= scale; |
| 70 | color.a = outerThreshold; |
| 71 | } |
| 72 | } else if (color.a < innerThreshold) { |
| 73 | float scale = innerThreshold / max(0.001, color.a); |
| 74 | color.rgb *= scale; |
| 75 | color.a = innerThreshold; |
| 76 | } |
| 77 | sk_OutColor = color; |
| 78 | } |
| 79 | |
| 80 | @test(testData) { |
| 81 | sk_sp<GrTextureProxy> bmpProxy = testData->textureProxy(GrProcessorUnitTest::kSkiaPMTextureIdx); |
| 82 | sk_sp<GrTextureProxy> maskProxy = testData->textureProxy(GrProcessorUnitTest::kAlphaTextureIdx); |
| 83 | // Make the inner and outer thresholds be in (0, 1) exclusive and be sorted correctly. |
| 84 | float innerThresh = testData->fRandom->nextUScalar1() * .99f + 0.005f; |
| 85 | float outerThresh = testData->fRandom->nextUScalar1() * .99f + 0.005f; |
| 86 | const int kMaxWidth = 1000; |
| 87 | const int kMaxHeight = 1000; |
| 88 | uint32_t width = testData->fRandom->nextULessThan(kMaxWidth); |
| 89 | uint32_t height = testData->fRandom->nextULessThan(kMaxHeight); |
| 90 | uint32_t x = testData->fRandom->nextULessThan(kMaxWidth - width); |
| 91 | uint32_t y = testData->fRandom->nextULessThan(kMaxHeight - height); |
| 92 | SkIRect bounds = SkIRect::MakeXYWH(x, y, width, height); |
| 93 | sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(testData->fRandom); |
| 94 | return GrAlphaThresholdFragmentProcessor::Make( |
| 95 | std::move(bmpProxy), |
| 96 | colorSpaceXform, |
| 97 | std::move(maskProxy), |
| 98 | innerThresh, outerThresh, |
| 99 | bounds); |
| 100 | } |