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 | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame^] | 31 | @coordTransform(image) { |
| 32 | SkMatrix::I() |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 33 | } |
| 34 | |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame^] | 35 | @coordTransform(mask) { |
| 36 | SkMatrix::MakeTrans(SkIntToScalar(-bounds.x()), SkIntToScalar(-bounds.y())) |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | @header { |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 40 | #include "GrColorSpaceXform.h" |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 41 | } |
| 42 | |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 43 | @cpp { |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 44 | inline GrFragmentProcessor::OptimizationFlags GrAlphaThresholdFragmentProcessor::optFlags( |
| 45 | float outerThreshold) { |
| 46 | if (outerThreshold >= 1.0) { |
| 47 | return kPreservesOpaqueInput_OptimizationFlag | |
| 48 | kCompatibleWithCoverageAsAlpha_OptimizationFlag; |
| 49 | } else { |
| 50 | return kCompatibleWithCoverageAsAlpha_OptimizationFlag; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
Ethan Nicholas | 9fb036f | 2017-07-05 16:19:09 -0400 | [diff] [blame] | 55 | void main() { |
| 56 | vec4 color = texture(image, sk_TransformedCoords2D[0], colorXform); |
| 57 | vec4 mask_color = texture(mask, sk_TransformedCoords2D[1]); |
| 58 | if (mask_color.a < 0.5) { |
| 59 | if (color.a > outerThreshold) { |
| 60 | float scale = outerThreshold / color.a; |
| 61 | color.rgb *= scale; |
| 62 | color.a = outerThreshold; |
| 63 | } |
| 64 | } else if (color.a < innerThreshold) { |
| 65 | float scale = innerThreshold / max(0.001, color.a); |
| 66 | color.rgb *= scale; |
| 67 | color.a = innerThreshold; |
| 68 | } |
| 69 | sk_OutColor = color; |
| 70 | } |
| 71 | |
| 72 | @test(testData) { |
| 73 | sk_sp<GrTextureProxy> bmpProxy = testData->textureProxy(GrProcessorUnitTest::kSkiaPMTextureIdx); |
| 74 | sk_sp<GrTextureProxy> maskProxy = testData->textureProxy(GrProcessorUnitTest::kAlphaTextureIdx); |
| 75 | // Make the inner and outer thresholds be in (0, 1) exclusive and be sorted correctly. |
| 76 | float innerThresh = testData->fRandom->nextUScalar1() * .99f + 0.005f; |
| 77 | float outerThresh = testData->fRandom->nextUScalar1() * .99f + 0.005f; |
| 78 | const int kMaxWidth = 1000; |
| 79 | const int kMaxHeight = 1000; |
| 80 | uint32_t width = testData->fRandom->nextULessThan(kMaxWidth); |
| 81 | uint32_t height = testData->fRandom->nextULessThan(kMaxHeight); |
| 82 | uint32_t x = testData->fRandom->nextULessThan(kMaxWidth - width); |
| 83 | uint32_t y = testData->fRandom->nextULessThan(kMaxHeight - height); |
| 84 | SkIRect bounds = SkIRect::MakeXYWH(x, y, width, height); |
| 85 | sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(testData->fRandom); |
| 86 | return GrAlphaThresholdFragmentProcessor::Make( |
| 87 | std::move(bmpProxy), |
| 88 | colorSpaceXform, |
| 89 | std::move(maskProxy), |
| 90 | innerThresh, outerThresh, |
| 91 | bounds); |
| 92 | } |