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" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 10 | #include "SkReadBuffer.h" |
| 11 | #include "SkWriteBuffer.h" |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 12 | #include "SkRegion.h" |
| 13 | |
| 14 | class SK_API SkAlphaThresholdFilterImpl : public SkImageFilter { |
| 15 | public: |
senorblanco | 9ea3d57 | 2014-07-08 09:16:22 -0700 | [diff] [blame] | 16 | SkAlphaThresholdFilterImpl(const SkRegion& region, SkScalar innerThreshold, |
| 17 | SkScalar outerThreshold, SkImageFilter* input); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 18 | |
| 19 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkAlphaThresholdFilterImpl) |
| 20 | |
| 21 | protected: |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 22 | virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 23 | |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 24 | virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 25 | SkBitmap* result, SkIPoint* offset) const SK_OVERRIDE; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 26 | #if SK_SUPPORT_GPU |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 27 | virtual bool asFragmentProcessor(GrFragmentProcessor**, GrTexture*, const SkMatrix&, |
| 28 | const SkIRect& bounds) const SK_OVERRIDE; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 29 | #endif |
| 30 | |
| 31 | private: |
| 32 | SkRegion fRegion; |
| 33 | SkScalar fInnerThreshold; |
| 34 | SkScalar fOuterThreshold; |
| 35 | typedef SkImageFilter INHERITED; |
| 36 | }; |
| 37 | |
| 38 | SkImageFilter* SkAlphaThresholdFilter::Create(const SkRegion& region, |
| 39 | SkScalar innerThreshold, |
senorblanco | 9ea3d57 | 2014-07-08 09:16:22 -0700 | [diff] [blame] | 40 | SkScalar outerThreshold, |
| 41 | SkImageFilter* input) { |
| 42 | return SkNEW_ARGS(SkAlphaThresholdFilterImpl, (region, innerThreshold, outerThreshold, input)); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | #if SK_SUPPORT_GPU |
| 46 | #include "GrContext.h" |
| 47 | #include "GrCoordTransform.h" |
bsalomon | 6251d17 | 2014-10-15 10:50:36 -0700 | [diff] [blame] | 48 | #include "GrFragmentProcessor.h" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 49 | #include "GrInvariantOutput.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 50 | #include "gl/GrGLProcessor.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 51 | #include "gl/builders/GrGLProgramBuilder.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 52 | #include "GrTBackendProcessorFactory.h" |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 53 | #include "GrTextureAccess.h" |
| 54 | |
| 55 | #include "SkGr.h" |
| 56 | |
| 57 | class GrGLAlphaThresholdEffect; |
| 58 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 59 | class AlphaThresholdEffect : public GrFragmentProcessor { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 60 | |
| 61 | public: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 62 | static GrFragmentProcessor* Create(GrTexture* texture, |
| 63 | GrTexture* maskTexture, |
| 64 | float innerThreshold, |
| 65 | float outerThreshold) { |
bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 66 | return SkNEW_ARGS(AlphaThresholdEffect, (texture, |
| 67 | maskTexture, |
| 68 | innerThreshold, |
| 69 | outerThreshold)); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | virtual ~AlphaThresholdEffect() {}; |
| 73 | |
| 74 | static const char* Name() { return "Alpha Threshold"; } |
| 75 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 76 | virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERRIDE; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 77 | float innerThreshold() const { return fInnerThreshold; } |
| 78 | float outerThreshold() const { return fOuterThreshold; } |
| 79 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 80 | typedef GrGLAlphaThresholdEffect GLProcessor; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 81 | |
| 82 | private: |
| 83 | AlphaThresholdEffect(GrTexture* texture, |
| 84 | GrTexture* maskTexture, |
| 85 | float innerThreshold, |
| 86 | float outerThreshold) |
| 87 | : fInnerThreshold(innerThreshold) |
| 88 | , fOuterThreshold(outerThreshold) |
bsalomon | 6267f81 | 2014-08-29 15:05:53 -0700 | [diff] [blame] | 89 | , fImageCoordTransform(kLocal_GrCoordSet, |
| 90 | GrCoordTransform::MakeDivByTextureWHMatrix(texture), texture) |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 91 | , fImageTextureAccess(texture) |
bsalomon | 6267f81 | 2014-08-29 15:05:53 -0700 | [diff] [blame] | 92 | , fMaskCoordTransform(kLocal_GrCoordSet, |
| 93 | GrCoordTransform::MakeDivByTextureWHMatrix(maskTexture), maskTexture) |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 94 | , fMaskTextureAccess(maskTexture) { |
| 95 | this->addCoordTransform(&fImageCoordTransform); |
| 96 | this->addTextureAccess(&fImageTextureAccess); |
| 97 | this->addCoordTransform(&fMaskCoordTransform); |
| 98 | this->addTextureAccess(&fMaskTextureAccess); |
| 99 | } |
| 100 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 101 | virtual bool onIsEqual(const GrFragmentProcessor&) const SK_OVERRIDE; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 102 | |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 103 | virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE; |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 104 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 105 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 106 | |
| 107 | float fInnerThreshold; |
| 108 | float fOuterThreshold; |
| 109 | GrCoordTransform fImageCoordTransform; |
| 110 | GrTextureAccess fImageTextureAccess; |
| 111 | GrCoordTransform fMaskCoordTransform; |
| 112 | GrTextureAccess fMaskTextureAccess; |
| 113 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 114 | typedef GrFragmentProcessor INHERITED; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 117 | class GrGLAlphaThresholdEffect : public GrGLFragmentProcessor { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 118 | public: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 119 | GrGLAlphaThresholdEffect(const GrBackendProcessorFactory&, const GrProcessor&); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 120 | |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 121 | virtual void emitCode(GrGLFPBuilder*, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 122 | const GrFragmentProcessor&, |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 123 | const char* outputColor, |
| 124 | const char* inputColor, |
| 125 | const TransformedCoordsArray&, |
| 126 | const TextureSamplerArray&) SK_OVERRIDE; |
| 127 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 128 | virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 129 | |
| 130 | private: |
| 131 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 132 | GrGLProgramDataManager::UniformHandle fInnerThresholdVar; |
| 133 | GrGLProgramDataManager::UniformHandle fOuterThresholdVar; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 134 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 135 | typedef GrGLFragmentProcessor INHERITED; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 136 | }; |
| 137 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 138 | GrGLAlphaThresholdEffect::GrGLAlphaThresholdEffect(const GrBackendProcessorFactory& factory, |
| 139 | const GrProcessor&) |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 140 | : INHERITED(factory) { |
| 141 | } |
| 142 | |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 143 | void GrGLAlphaThresholdEffect::emitCode(GrGLFPBuilder* builder, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 144 | const GrFragmentProcessor&, |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 145 | const char* outputColor, |
| 146 | const char* inputColor, |
| 147 | const TransformedCoordsArray& coords, |
| 148 | const TextureSamplerArray& samplers) { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 149 | fInnerThresholdVar = builder->addUniform( |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 150 | GrGLProgramBuilder::kFragment_Visibility, |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 151 | kFloat_GrSLType, "inner_threshold"); |
| 152 | fOuterThresholdVar = builder->addUniform( |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 153 | GrGLProgramBuilder::kFragment_Visibility, |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 154 | kFloat_GrSLType, "outer_threshold"); |
| 155 | |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 156 | GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 157 | SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0); |
| 158 | SkString maskCoords2D = fsBuilder->ensureFSCoords2D(coords, 1); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 159 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 160 | fsBuilder->codeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str()); |
| 161 | fsBuilder->codeAppendf("\t\tvec2 mask_coord = %s;\n", maskCoords2D.c_str()); |
| 162 | fsBuilder->codeAppend("\t\tvec4 input_color = "); |
| 163 | fsBuilder->appendTextureLookup(samplers[0], "coord"); |
| 164 | fsBuilder->codeAppend(";\n"); |
| 165 | fsBuilder->codeAppend("\t\tvec4 mask_color = "); |
| 166 | fsBuilder->appendTextureLookup(samplers[1], "mask_coord"); |
| 167 | fsBuilder->codeAppend(";\n"); |
| 168 | |
| 169 | fsBuilder->codeAppendf("\t\tfloat inner_thresh = %s;\n", |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 170 | builder->getUniformCStr(fInnerThresholdVar)); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 171 | fsBuilder->codeAppendf("\t\tfloat outer_thresh = %s;\n", |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 172 | builder->getUniformCStr(fOuterThresholdVar)); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 173 | fsBuilder->codeAppend("\t\tfloat mask = mask_color.a;\n"); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 174 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 175 | fsBuilder->codeAppend("vec4 color = input_color;\n"); |
| 176 | fsBuilder->codeAppend("\t\tif (mask < 0.5) {\n" |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 177 | "\t\t\tif (color.a > outer_thresh) {\n" |
| 178 | "\t\t\t\tfloat scale = outer_thresh / color.a;\n" |
| 179 | "\t\t\t\tcolor.rgb *= scale;\n" |
| 180 | "\t\t\t\tcolor.a = outer_thresh;\n" |
| 181 | "\t\t\t}\n" |
| 182 | "\t\t} else if (color.a < inner_thresh) {\n" |
| 183 | "\t\t\tfloat scale = inner_thresh / max(0.001, color.a);\n" |
| 184 | "\t\t\tcolor.rgb *= scale;\n" |
| 185 | "\t\t\tcolor.a = inner_thresh;\n" |
| 186 | "\t\t}\n"); |
| 187 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 188 | fsBuilder->codeAppendf("%s = %s;\n", outputColor, |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 189 | (GrGLSLExpr4(inputColor) * GrGLSLExpr4("color")).c_str()); |
| 190 | } |
| 191 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 192 | void GrGLAlphaThresholdEffect::setData(const GrGLProgramDataManager& pdman, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 193 | const GrProcessor& proc) { |
| 194 | const AlphaThresholdEffect& alpha_threshold = proc.cast<AlphaThresholdEffect>(); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 195 | pdman.set1f(fInnerThresholdVar, alpha_threshold.innerThreshold()); |
| 196 | pdman.set1f(fOuterThresholdVar, alpha_threshold.outerThreshold()); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | ///////////////////////////////////////////////////////////////////// |
| 200 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 201 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(AlphaThresholdEffect); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 202 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 203 | GrFragmentProcessor* AlphaThresholdEffect::TestCreate(SkRandom* random, |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 204 | GrContext* context, |
| 205 | const GrDrawTargetCaps&, |
| 206 | GrTexture** textures) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 207 | GrTexture* bmpTex = textures[GrProcessorUnitTest::kSkiaPMTextureIdx]; |
| 208 | GrTexture* maskTex = textures[GrProcessorUnitTest::kAlphaTextureIdx]; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 209 | float inner_thresh = random->nextUScalar1(); |
| 210 | float outer_thresh = random->nextUScalar1(); |
| 211 | return AlphaThresholdEffect::Create(bmpTex, maskTex, inner_thresh, outer_thresh); |
| 212 | } |
| 213 | |
| 214 | /////////////////////////////////////////////////////////////////////////////// |
| 215 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 216 | const GrBackendFragmentProcessorFactory& AlphaThresholdEffect::getFactory() const { |
| 217 | return GrTBackendFragmentProcessorFactory<AlphaThresholdEffect>::getInstance(); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 218 | } |
| 219 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 220 | bool AlphaThresholdEffect::onIsEqual(const GrFragmentProcessor& sBase) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 221 | const AlphaThresholdEffect& s = sBase.cast<AlphaThresholdEffect>(); |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 222 | return (this->fInnerThreshold == s.fInnerThreshold && |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 223 | this->fOuterThreshold == s.fOuterThreshold); |
| 224 | } |
| 225 | |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 226 | void AlphaThresholdEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { |
egdaniel | f8449ba | 2014-11-25 10:24:56 -0800 | [diff] [blame] | 227 | if (GrPixelConfigIsAlphaOnly(this->texture(0)->config())) { |
| 228 | inout->mulByUnknownAlpha(); |
| 229 | } else if (GrPixelConfigIsOpaque(this->texture(0)->config()) && fOuterThreshold >= 1.f) { |
egdaniel | ccb2e38 | 2014-10-13 12:53:46 -0700 | [diff] [blame] | 230 | inout->mulByUnknownOpaqueColor(); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 231 | } else { |
egdaniel | ccb2e38 | 2014-10-13 12:53:46 -0700 | [diff] [blame] | 232 | inout->mulByUnknownColor(); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
| 236 | #endif |
| 237 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 238 | SkFlattenable* SkAlphaThresholdFilterImpl::CreateProc(SkReadBuffer& buffer) { |
| 239 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
| 240 | SkScalar inner = buffer.readScalar(); |
| 241 | SkScalar outer = buffer.readScalar(); |
| 242 | SkRegion rgn; |
| 243 | buffer.readRegion(&rgn); |
| 244 | return SkAlphaThresholdFilter::Create(rgn, inner, outer, common.getInput(0)); |
| 245 | } |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 246 | |
| 247 | SkAlphaThresholdFilterImpl::SkAlphaThresholdFilterImpl(const SkRegion& region, |
| 248 | SkScalar innerThreshold, |
senorblanco | 9ea3d57 | 2014-07-08 09:16:22 -0700 | [diff] [blame] | 249 | SkScalar outerThreshold, |
| 250 | SkImageFilter* input) |
| 251 | : INHERITED(1, &input) |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 252 | , fRegion(region) |
| 253 | , fInnerThreshold(innerThreshold) |
| 254 | , fOuterThreshold(outerThreshold) { |
| 255 | } |
| 256 | |
| 257 | #if SK_SUPPORT_GPU |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 258 | bool SkAlphaThresholdFilterImpl::asFragmentProcessor(GrFragmentProcessor** fp, |
| 259 | GrTexture* texture, |
| 260 | const SkMatrix& in_matrix, |
| 261 | const SkIRect&) const { |
| 262 | if (fp) { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 263 | GrContext* context = texture->getContext(); |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 264 | GrSurfaceDesc maskDesc; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 265 | if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) { |
| 266 | maskDesc.fConfig = kAlpha_8_GrPixelConfig; |
| 267 | } else { |
| 268 | maskDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 269 | } |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 270 | maskDesc.fFlags = kRenderTarget_GrSurfaceFlag | kNoStencil_GrSurfaceFlag; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 271 | // Add one pixel of border to ensure that clamp mode will be all zeros |
| 272 | // the outside. |
| 273 | maskDesc.fWidth = texture->width(); |
| 274 | maskDesc.fHeight = texture->height(); |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 275 | SkAutoTUnref<GrTexture> maskTexture( |
| 276 | context->refScratchTexture(maskDesc, GrContext::kApprox_ScratchTexMatch)); |
| 277 | if (!maskTexture) { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 278 | return false; |
| 279 | } |
| 280 | |
| 281 | { |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 282 | GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget()); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 283 | GrPaint grPaint; |
| 284 | grPaint.setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff); |
| 285 | SkRegion::Iterator iter(fRegion); |
bsalomon | 89c6298 | 2014-11-03 12:08:42 -0800 | [diff] [blame] | 286 | context->clear(NULL, 0x0, true, maskTexture->asRenderTarget()); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 287 | |
| 288 | SkMatrix old_matrix = context->getMatrix(); |
| 289 | context->setMatrix(in_matrix); |
| 290 | |
| 291 | while (!iter.done()) { |
| 292 | SkRect rect = SkRect::Make(iter.rect()); |
| 293 | context->drawRect(grPaint, rect); |
| 294 | iter.next(); |
| 295 | } |
| 296 | context->setMatrix(old_matrix); |
| 297 | } |
| 298 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 299 | *fp = AlphaThresholdEffect::Create(texture, |
| 300 | maskTexture, |
| 301 | fInnerThreshold, |
| 302 | fOuterThreshold); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 303 | } |
| 304 | return true; |
| 305 | } |
| 306 | #endif |
| 307 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 308 | void SkAlphaThresholdFilterImpl::flatten(SkWriteBuffer& buffer) const { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 309 | this->INHERITED::flatten(buffer); |
| 310 | buffer.writeScalar(fInnerThreshold); |
| 311 | buffer.writeScalar(fOuterThreshold); |
| 312 | buffer.writeRegion(fRegion); |
| 313 | } |
| 314 | |
| 315 | bool SkAlphaThresholdFilterImpl::onFilterImage(Proxy*, const SkBitmap& src, |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 316 | const Context& ctx, SkBitmap* dst, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 317 | SkIPoint* offset) const { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 318 | SkASSERT(src.colorType() == kN32_SkColorType); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 319 | |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 320 | if (src.colorType() != kN32_SkColorType) { |
commit-bot@chromium.org | 9109e18 | 2014-01-07 16:04:01 +0000 | [diff] [blame] | 321 | return false; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | SkMatrix localInverse; |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 325 | if (!ctx.ctm().invert(&localInverse)) { |
commit-bot@chromium.org | 9109e18 | 2014-01-07 16:04:01 +0000 | [diff] [blame] | 326 | return false; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | SkAutoLockPixels alp(src); |
| 330 | SkASSERT(src.getPixels()); |
| 331 | if (!src.getPixels() || src.width() <= 0 || src.height() <= 0) { |
commit-bot@chromium.org | 9109e18 | 2014-01-07 16:04:01 +0000 | [diff] [blame] | 332 | return false; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 333 | } |
| 334 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 335 | if (!dst->tryAllocPixels(src.info())) { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 336 | return false; |
| 337 | } |
| 338 | |
commit-bot@chromium.org | 9109e18 | 2014-01-07 16:04:01 +0000 | [diff] [blame] | 339 | U8CPU innerThreshold = (U8CPU)(fInnerThreshold * 0xFF); |
| 340 | U8CPU outerThreshold = (U8CPU)(fOuterThreshold * 0xFF); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 341 | SkColor* sptr = src.getAddr32(0, 0); |
| 342 | SkColor* dptr = dst->getAddr32(0, 0); |
| 343 | int width = src.width(), height = src.height(); |
| 344 | for (int y = 0; y < height; ++y) { |
| 345 | for (int x = 0; x < width; ++x) { |
| 346 | const SkColor& source = sptr[y * width + x]; |
| 347 | SkColor output_color(source); |
| 348 | SkPoint position; |
commit-bot@chromium.org | 9109e18 | 2014-01-07 16:04:01 +0000 | [diff] [blame] | 349 | localInverse.mapXY((SkScalar)x, (SkScalar)y, &position); |
| 350 | 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] | 351 | if (SkColorGetA(source) < innerThreshold) { |
| 352 | U8CPU alpha = SkColorGetA(source); |
| 353 | if (alpha == 0) |
| 354 | alpha = 1; |
| 355 | float scale = (float)innerThreshold / alpha; |
| 356 | output_color = SkColorSetARGB(innerThreshold, |
commit-bot@chromium.org | 9109e18 | 2014-01-07 16:04:01 +0000 | [diff] [blame] | 357 | (U8CPU)(SkColorGetR(source) * scale), |
| 358 | (U8CPU)(SkColorGetG(source) * scale), |
| 359 | (U8CPU)(SkColorGetB(source) * scale)); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 360 | } |
| 361 | } else { |
| 362 | if (SkColorGetA(source) > outerThreshold) { |
| 363 | float scale = (float)outerThreshold / SkColorGetA(source); |
| 364 | output_color = SkColorSetARGB(outerThreshold, |
commit-bot@chromium.org | 9109e18 | 2014-01-07 16:04:01 +0000 | [diff] [blame] | 365 | (U8CPU)(SkColorGetR(source) * scale), |
| 366 | (U8CPU)(SkColorGetG(source) * scale), |
| 367 | (U8CPU)(SkColorGetB(source) * scale)); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | dptr[y * dst->width() + x] = output_color; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | return true; |
| 375 | } |