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