commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkAlphaThresholdFilter.h" |
| 9 | #include "SkBitmap.h" |
senorblanco | 1d3ff43 | 2015-10-20 10:17:34 -0700 | [diff] [blame] | 10 | #include "SkDevice.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 11 | #include "SkReadBuffer.h" |
| 12 | #include "SkWriteBuffer.h" |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 13 | #include "SkRegion.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 14 | #if SK_SUPPORT_GPU |
| 15 | #include "GrDrawContext.h" |
| 16 | #endif |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 17 | |
| 18 | class SK_API SkAlphaThresholdFilterImpl : public SkImageFilter { |
| 19 | public: |
senorblanco | 9ea3d57 | 2014-07-08 09:16:22 -0700 | [diff] [blame] | 20 | SkAlphaThresholdFilterImpl(const SkRegion& region, SkScalar innerThreshold, |
| 21 | SkScalar outerThreshold, SkImageFilter* input); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 22 | |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 23 | SK_TO_STRING_OVERRIDE() |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 24 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkAlphaThresholdFilterImpl) |
halcanary | 0745653 | 2015-12-07 10:29:54 -0800 | [diff] [blame] | 25 | friend void SkAlphaThresholdFilter::InitializeFlattenables(); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 26 | |
| 27 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 28 | void flatten(SkWriteBuffer&) const override; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 29 | |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame] | 30 | bool onFilterImageDeprecated(Proxy*, const SkBitmap& src, const Context&, |
| 31 | SkBitmap* result, SkIPoint* offset) const override; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 32 | #if SK_SUPPORT_GPU |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 33 | bool asFragmentProcessor(GrFragmentProcessor**, GrTexture*, const SkMatrix&, |
| 34 | const SkIRect& bounds) const override; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 35 | #endif |
| 36 | |
| 37 | private: |
| 38 | SkRegion fRegion; |
| 39 | SkScalar fInnerThreshold; |
| 40 | SkScalar fOuterThreshold; |
| 41 | typedef SkImageFilter INHERITED; |
| 42 | }; |
| 43 | |
halcanary | 0745653 | 2015-12-07 10:29:54 -0800 | [diff] [blame] | 44 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkAlphaThresholdFilter) |
| 45 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkAlphaThresholdFilterImpl) |
| 46 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
| 47 | |
reed | ca726ab | 2016-02-22 12:50:25 -0800 | [diff] [blame] | 48 | static SkScalar pin_0_1(SkScalar x) { |
| 49 | return SkMinScalar(SkMaxScalar(x, 0), 1); |
| 50 | } |
halcanary | 0745653 | 2015-12-07 10:29:54 -0800 | [diff] [blame] | 51 | |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 52 | SkImageFilter* SkAlphaThresholdFilter::Create(const SkRegion& region, |
| 53 | SkScalar innerThreshold, |
senorblanco | 9ea3d57 | 2014-07-08 09:16:22 -0700 | [diff] [blame] | 54 | SkScalar outerThreshold, |
| 55 | SkImageFilter* input) { |
reed | ca726ab | 2016-02-22 12:50:25 -0800 | [diff] [blame] | 56 | innerThreshold = pin_0_1(innerThreshold); |
| 57 | outerThreshold = pin_0_1(outerThreshold); |
| 58 | if (!SkScalarIsFinite(innerThreshold) || !SkScalarIsFinite(outerThreshold)) { |
| 59 | return nullptr; |
| 60 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 61 | return new SkAlphaThresholdFilterImpl(region, innerThreshold, outerThreshold, input); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | #if SK_SUPPORT_GPU |
| 65 | #include "GrContext.h" |
| 66 | #include "GrCoordTransform.h" |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 67 | #include "GrFragmentProcessor.h" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 68 | #include "GrInvariantOutput.h" |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 69 | #include "GrTextureAccess.h" |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 70 | #include "effects/GrPorterDuffXferProcessor.h" |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 71 | |
| 72 | #include "SkGr.h" |
| 73 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 74 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 75 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 76 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 77 | #include "glsl/GrGLSLUniformHandler.h" |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 78 | |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 79 | namespace { |
| 80 | |
| 81 | SkMatrix make_div_and_translate_matrix(GrTexture* texture, int x, int y) { |
| 82 | SkMatrix matrix = GrCoordTransform::MakeDivByTextureWHMatrix(texture); |
| 83 | matrix.preTranslate(SkIntToScalar(x), SkIntToScalar(y)); |
| 84 | return matrix; |
| 85 | } |
| 86 | |
| 87 | }; |
| 88 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 89 | class AlphaThresholdEffect : public GrFragmentProcessor { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 90 | |
| 91 | public: |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 92 | static GrFragmentProcessor* Create(GrTexture* texture, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 93 | GrTexture* maskTexture, |
| 94 | float innerThreshold, |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 95 | float outerThreshold, |
| 96 | const SkIRect& bounds) { |
| 97 | return new AlphaThresholdEffect(texture, maskTexture, innerThreshold, outerThreshold, |
| 98 | bounds); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | virtual ~AlphaThresholdEffect() {}; |
| 102 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 103 | const char* name() const override { return "Alpha Threshold"; } |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 104 | |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 105 | float innerThreshold() const { return fInnerThreshold; } |
| 106 | float outerThreshold() const { return fOuterThreshold; } |
| 107 | |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 108 | private: |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 109 | AlphaThresholdEffect(GrTexture* texture, |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 110 | GrTexture* maskTexture, |
| 111 | float innerThreshold, |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 112 | float outerThreshold, |
| 113 | const SkIRect& bounds) |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 114 | : fInnerThreshold(innerThreshold) |
| 115 | , fOuterThreshold(outerThreshold) |
bsalomon | 6267f81 | 2014-08-29 15:05:53 -0700 | [diff] [blame] | 116 | , fImageCoordTransform(kLocal_GrCoordSet, |
bsalomon | 9f876a3 | 2014-12-09 10:51:07 -0800 | [diff] [blame] | 117 | GrCoordTransform::MakeDivByTextureWHMatrix(texture), texture, |
| 118 | GrTextureParams::kNone_FilterMode) |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 119 | , fImageTextureAccess(texture) |
bsalomon | 6267f81 | 2014-08-29 15:05:53 -0700 | [diff] [blame] | 120 | , fMaskCoordTransform(kLocal_GrCoordSet, |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 121 | make_div_and_translate_matrix(maskTexture, -bounds.x(), -bounds.y()), |
| 122 | maskTexture, |
bsalomon | 9f876a3 | 2014-12-09 10:51:07 -0800 | [diff] [blame] | 123 | GrTextureParams::kNone_FilterMode) |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 124 | , fMaskTextureAccess(maskTexture) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 125 | this->initClassID<AlphaThresholdEffect>(); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 126 | this->addCoordTransform(&fImageCoordTransform); |
| 127 | this->addTextureAccess(&fImageTextureAccess); |
| 128 | this->addCoordTransform(&fMaskCoordTransform); |
| 129 | this->addTextureAccess(&fMaskTextureAccess); |
| 130 | } |
| 131 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 132 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 133 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 134 | void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 135 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 136 | bool onIsEqual(const GrFragmentProcessor&) const override; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 137 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 138 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override; |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 139 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 140 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 141 | |
| 142 | float fInnerThreshold; |
| 143 | float fOuterThreshold; |
| 144 | GrCoordTransform fImageCoordTransform; |
| 145 | GrTextureAccess fImageTextureAccess; |
| 146 | GrCoordTransform fMaskCoordTransform; |
| 147 | GrTextureAccess fMaskTextureAccess; |
| 148 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 149 | typedef GrFragmentProcessor INHERITED; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 152 | class GrGLAlphaThresholdEffect : public GrGLSLFragmentProcessor { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 153 | public: |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 154 | void emitCode(EmitArgs&) override; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 155 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 156 | protected: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 157 | void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 158 | |
| 159 | private: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 160 | GrGLSLProgramDataManager::UniformHandle fInnerThresholdVar; |
| 161 | GrGLSLProgramDataManager::UniformHandle fOuterThresholdVar; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 162 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 163 | typedef GrGLSLFragmentProcessor INHERITED; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 166 | void GrGLAlphaThresholdEffect::emitCode(EmitArgs& args) { |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 167 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 168 | fInnerThresholdVar = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 169 | kFloat_GrSLType, kDefault_GrSLPrecision, |
| 170 | "inner_threshold"); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 171 | fOuterThresholdVar = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 172 | kFloat_GrSLType, kDefault_GrSLPrecision, |
| 173 | "outer_threshold"); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 174 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 175 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 176 | SkString coords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 0); |
| 177 | SkString maskCoords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 1); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 178 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 179 | fragBuilder->codeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str()); |
| 180 | fragBuilder->codeAppendf("\t\tvec2 mask_coord = %s;\n", maskCoords2D.c_str()); |
| 181 | fragBuilder->codeAppend("\t\tvec4 input_color = "); |
| 182 | fragBuilder->appendTextureLookup(args.fSamplers[0], "coord"); |
| 183 | fragBuilder->codeAppend(";\n"); |
| 184 | fragBuilder->codeAppend("\t\tvec4 mask_color = "); |
| 185 | fragBuilder->appendTextureLookup(args.fSamplers[1], "mask_coord"); |
| 186 | fragBuilder->codeAppend(";\n"); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 187 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 188 | fragBuilder->codeAppendf("\t\tfloat inner_thresh = %s;\n", |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 189 | uniformHandler->getUniformCStr(fInnerThresholdVar)); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 190 | fragBuilder->codeAppendf("\t\tfloat outer_thresh = %s;\n", |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 191 | uniformHandler->getUniformCStr(fOuterThresholdVar)); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 192 | fragBuilder->codeAppend("\t\tfloat mask = mask_color.a;\n"); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 193 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 194 | fragBuilder->codeAppend("vec4 color = input_color;\n"); |
| 195 | fragBuilder->codeAppend("\t\tif (mask < 0.5) {\n" |
| 196 | "\t\t\tif (color.a > outer_thresh) {\n" |
| 197 | "\t\t\t\tfloat scale = outer_thresh / color.a;\n" |
| 198 | "\t\t\t\tcolor.rgb *= scale;\n" |
| 199 | "\t\t\t\tcolor.a = outer_thresh;\n" |
| 200 | "\t\t\t}\n" |
| 201 | "\t\t} else if (color.a < inner_thresh) {\n" |
| 202 | "\t\t\tfloat scale = inner_thresh / max(0.001, color.a);\n" |
| 203 | "\t\t\tcolor.rgb *= scale;\n" |
| 204 | "\t\t\tcolor.a = inner_thresh;\n" |
| 205 | "\t\t}\n"); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 206 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 207 | fragBuilder->codeAppendf("%s = %s;\n", args.fOutputColor, |
| 208 | (GrGLSLExpr4(args.fInputColor) * GrGLSLExpr4("color")).c_str()); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 209 | } |
| 210 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 211 | void GrGLAlphaThresholdEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 212 | const GrProcessor& proc) { |
| 213 | const AlphaThresholdEffect& alpha_threshold = proc.cast<AlphaThresholdEffect>(); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 214 | pdman.set1f(fInnerThresholdVar, alpha_threshold.innerThreshold()); |
| 215 | pdman.set1f(fOuterThresholdVar, alpha_threshold.outerThreshold()); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | ///////////////////////////////////////////////////////////////////// |
| 219 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 220 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(AlphaThresholdEffect); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 221 | |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 222 | const GrFragmentProcessor* AlphaThresholdEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 223 | GrTexture* bmpTex = d->fTextures[GrProcessorUnitTest::kSkiaPMTextureIdx]; |
| 224 | GrTexture* maskTex = d->fTextures[GrProcessorUnitTest::kAlphaTextureIdx]; |
| 225 | float innerThresh = d->fRandom->nextUScalar1(); |
| 226 | float outerThresh = d->fRandom->nextUScalar1(); |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 227 | const int kMaxWidth = 1000; |
| 228 | const int kMaxHeight = 1000; |
| 229 | uint32_t width = d->fRandom->nextULessThan(kMaxWidth); |
| 230 | uint32_t height = d->fRandom->nextULessThan(kMaxHeight); |
| 231 | uint32_t x = d->fRandom->nextULessThan(kMaxWidth - width); |
| 232 | uint32_t y = d->fRandom->nextULessThan(kMaxHeight - height); |
| 233 | SkIRect bounds = SkIRect::MakeXYWH(x, y, width, height); |
| 234 | return AlphaThresholdEffect::Create(bmpTex, maskTex, innerThresh, outerThresh, bounds); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /////////////////////////////////////////////////////////////////////////////// |
| 238 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 239 | void AlphaThresholdEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, |
| 240 | GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 241 | GrGLAlphaThresholdEffect::GenKey(*this, caps, b); |
| 242 | } |
| 243 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 244 | GrGLSLFragmentProcessor* AlphaThresholdEffect::onCreateGLSLInstance() const { |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 245 | return new GrGLAlphaThresholdEffect; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 246 | } |
| 247 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 248 | bool AlphaThresholdEffect::onIsEqual(const GrFragmentProcessor& sBase) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 249 | const AlphaThresholdEffect& s = sBase.cast<AlphaThresholdEffect>(); |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 250 | return (this->fInnerThreshold == s.fInnerThreshold && |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 251 | this->fOuterThreshold == s.fOuterThreshold); |
| 252 | } |
| 253 | |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 254 | void AlphaThresholdEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { |
egdaniel | f8449ba | 2014-11-25 10:24:56 -0800 | [diff] [blame] | 255 | if (GrPixelConfigIsAlphaOnly(this->texture(0)->config())) { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 256 | inout->mulByUnknownSingleComponent(); |
egdaniel | f8449ba | 2014-11-25 10:24:56 -0800 | [diff] [blame] | 257 | } else if (GrPixelConfigIsOpaque(this->texture(0)->config()) && fOuterThreshold >= 1.f) { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 258 | inout->mulByUnknownOpaqueFourComponents(); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 259 | } else { |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 260 | inout->mulByUnknownFourComponents(); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
| 264 | #endif |
| 265 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 266 | SkFlattenable* SkAlphaThresholdFilterImpl::CreateProc(SkReadBuffer& buffer) { |
| 267 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
| 268 | SkScalar inner = buffer.readScalar(); |
| 269 | SkScalar outer = buffer.readScalar(); |
| 270 | SkRegion rgn; |
| 271 | buffer.readRegion(&rgn); |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame^] | 272 | return SkAlphaThresholdFilter::Create(rgn, inner, outer, common.getInput(0).get()); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 273 | } |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 274 | |
| 275 | SkAlphaThresholdFilterImpl::SkAlphaThresholdFilterImpl(const SkRegion& region, |
| 276 | SkScalar innerThreshold, |
senorblanco | 9ea3d57 | 2014-07-08 09:16:22 -0700 | [diff] [blame] | 277 | SkScalar outerThreshold, |
| 278 | SkImageFilter* input) |
| 279 | : INHERITED(1, &input) |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 280 | , fRegion(region) |
| 281 | , fInnerThreshold(innerThreshold) |
| 282 | , fOuterThreshold(outerThreshold) { |
| 283 | } |
| 284 | |
| 285 | #if SK_SUPPORT_GPU |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 286 | bool SkAlphaThresholdFilterImpl::asFragmentProcessor(GrFragmentProcessor** fp, |
| 287 | GrTexture* texture, |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 288 | const SkMatrix& inMatrix, |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 289 | const SkIRect& bounds) const { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 290 | if (fp) { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 291 | GrContext* context = texture->getContext(); |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 292 | GrSurfaceDesc maskDesc; |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 293 | if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 294 | maskDesc.fConfig = kAlpha_8_GrPixelConfig; |
| 295 | } else { |
| 296 | maskDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 297 | } |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 298 | maskDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 299 | // Add one pixel of border to ensure that clamp mode will be all zeros |
| 300 | // the outside. |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 301 | maskDesc.fWidth = bounds.width(); |
| 302 | maskDesc.fHeight = bounds.height(); |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 303 | SkAutoTUnref<GrTexture> maskTexture( |
| 304 | context->textureProvider()->createApproxTexture(maskDesc)); |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 305 | if (!maskTexture) { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 306 | return false; |
| 307 | } |
| 308 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 309 | SkAutoTUnref<GrDrawContext> drawContext( |
| 310 | context->drawContext(maskTexture->asRenderTarget())); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 311 | if (drawContext) { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 312 | GrPaint grPaint; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 313 | grPaint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 314 | SkRegion::Iterator iter(fRegion); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 315 | drawContext->clear(nullptr, 0x0, true); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 316 | |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 317 | GrClip clip(SkRect::Make(SkIRect::MakeWH(bounds.width(), bounds.height()))); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 318 | while (!iter.done()) { |
| 319 | SkRect rect = SkRect::Make(iter.rect()); |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 320 | drawContext->drawRect(clip, grPaint, inMatrix, rect); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 321 | iter.next(); |
| 322 | } |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 323 | } |
| 324 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 325 | *fp = AlphaThresholdEffect::Create(texture, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 326 | maskTexture, |
| 327 | fInnerThreshold, |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 328 | fOuterThreshold, |
| 329 | bounds); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 330 | } |
| 331 | return true; |
| 332 | } |
| 333 | #endif |
| 334 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 335 | void SkAlphaThresholdFilterImpl::flatten(SkWriteBuffer& buffer) const { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 336 | this->INHERITED::flatten(buffer); |
| 337 | buffer.writeScalar(fInnerThreshold); |
| 338 | buffer.writeScalar(fOuterThreshold); |
| 339 | buffer.writeRegion(fRegion); |
| 340 | } |
| 341 | |
robertphillips | 48e7846 | 2016-02-17 13:57:16 -0800 | [diff] [blame] | 342 | bool SkAlphaThresholdFilterImpl::onFilterImageDeprecated(Proxy* proxy, const SkBitmap& src, |
| 343 | const Context& ctx, SkBitmap* dst, |
| 344 | SkIPoint* offset) const { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 345 | |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 346 | if (src.colorType() != kN32_SkColorType) { |
commit-bot@chromium.org | 9109e18 | 2014-01-07 16:04:01 +0000 | [diff] [blame] | 347 | return false; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | SkMatrix localInverse; |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 351 | if (!ctx.ctm().invert(&localInverse)) { |
commit-bot@chromium.org | 9109e18 | 2014-01-07 16:04:01 +0000 | [diff] [blame] | 352 | return false; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | SkAutoLockPixels alp(src); |
| 356 | SkASSERT(src.getPixels()); |
| 357 | if (!src.getPixels() || src.width() <= 0 || src.height() <= 0) { |
commit-bot@chromium.org | 9109e18 | 2014-01-07 16:04:01 +0000 | [diff] [blame] | 358 | return false; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 359 | } |
| 360 | |
senorblanco | 1d3ff43 | 2015-10-20 10:17:34 -0700 | [diff] [blame] | 361 | SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(src.width(), src.height())); |
| 362 | if (!device) { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 363 | return false; |
| 364 | } |
senorblanco | 1d3ff43 | 2015-10-20 10:17:34 -0700 | [diff] [blame] | 365 | *dst = device->accessBitmap(false); |
| 366 | SkAutoLockPixels alp_dst(*dst); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 367 | |
commit-bot@chromium.org | 9109e18 | 2014-01-07 16:04:01 +0000 | [diff] [blame] | 368 | U8CPU innerThreshold = (U8CPU)(fInnerThreshold * 0xFF); |
| 369 | U8CPU outerThreshold = (U8CPU)(fOuterThreshold * 0xFF); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 370 | SkColor* sptr = src.getAddr32(0, 0); |
| 371 | SkColor* dptr = dst->getAddr32(0, 0); |
| 372 | int width = src.width(), height = src.height(); |
| 373 | for (int y = 0; y < height; ++y) { |
| 374 | for (int x = 0; x < width; ++x) { |
| 375 | const SkColor& source = sptr[y * width + x]; |
| 376 | SkColor output_color(source); |
| 377 | SkPoint position; |
commit-bot@chromium.org | 9109e18 | 2014-01-07 16:04:01 +0000 | [diff] [blame] | 378 | localInverse.mapXY((SkScalar)x, (SkScalar)y, &position); |
| 379 | if (fRegion.contains((int32_t)position.x(), (int32_t)position.y())) { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 380 | if (SkColorGetA(source) < innerThreshold) { |
| 381 | U8CPU alpha = SkColorGetA(source); |
| 382 | if (alpha == 0) |
| 383 | alpha = 1; |
| 384 | float scale = (float)innerThreshold / alpha; |
| 385 | output_color = SkColorSetARGB(innerThreshold, |
commit-bot@chromium.org | 9109e18 | 2014-01-07 16:04:01 +0000 | [diff] [blame] | 386 | (U8CPU)(SkColorGetR(source) * scale), |
| 387 | (U8CPU)(SkColorGetG(source) * scale), |
| 388 | (U8CPU)(SkColorGetB(source) * scale)); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 389 | } |
| 390 | } else { |
| 391 | if (SkColorGetA(source) > outerThreshold) { |
| 392 | float scale = (float)outerThreshold / SkColorGetA(source); |
| 393 | output_color = SkColorSetARGB(outerThreshold, |
commit-bot@chromium.org | 9109e18 | 2014-01-07 16:04:01 +0000 | [diff] [blame] | 394 | (U8CPU)(SkColorGetR(source) * scale), |
| 395 | (U8CPU)(SkColorGetG(source) * scale), |
| 396 | (U8CPU)(SkColorGetB(source) * scale)); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | dptr[y * dst->width() + x] = output_color; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | return true; |
| 404 | } |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 405 | |
| 406 | #ifndef SK_IGNORE_TO_STRING |
| 407 | void SkAlphaThresholdFilterImpl::toString(SkString* str) const { |
| 408 | str->appendf("SkAlphaThresholdImageFilter: ("); |
| 409 | str->appendf("inner: %f outer: %f", fInnerThreshold, fOuterThreshold); |
| 410 | str->append(")"); |
| 411 | } |
| 412 | #endif |