blob: 3c06dbe9d312d683f40ae6679a1dcb3618576af3 [file] [log] [blame]
Ethan Nicholas9fb036f2017-07-05 16:19:09 -04001in uniform sampler2D image;
2in uniform colorSpaceXform colorXform;
3in uniform sampler2D mask;
4in uniform float innerThreshold;
5in 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 Nicholas68990be2017-07-13 09:36:52 -040031@coordTransform(image) {
32 SkMatrix::I()
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040033}
34
Ethan Nicholas68990be2017-07-13 09:36:52 -040035@coordTransform(mask) {
36 SkMatrix::MakeTrans(SkIntToScalar(-bounds.x()), SkIntToScalar(-bounds.y()))
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040037}
38
39@header {
Brian Salomon0c26a9d2017-07-06 10:09:38 -040040 #include "GrColorSpaceXform.h"
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040041}
42
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040043@cpp {
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040044 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 Nicholas9fb036f2017-07-05 16:19:09 -040055void 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}