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