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