blob: b576ecf0863815ab2878aefb4d0bf39509dc999a [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 Nicholasbaf981f2017-07-12 13:51:34 +000031@fields {
32 GrCoordTransform fImageCoordTransform;
33 GrCoordTransform fMaskCoordTransform;
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040034}
35
Ethan Nicholasbaf981f2017-07-12 13:51:34 +000036@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 Nicholas9fb036f2017-07-05 16:19:09 -040045}
46
47@header {
Brian Salomon0c26a9d2017-07-06 10:09:38 -040048 #include "GrColorSpaceXform.h"
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040049}
50
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040051@cpp {
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040052 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 Nicholas9fb036f2017-07-05 16:19:09 -040063void 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}