| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkAlphaThresholdFilter.h" | 8 #include "SkAlphaThresholdFilter.h" |
| 9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
| 10 #include "SkReadBuffer.h" | 10 #include "SkReadBuffer.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 SkScalar outerThreshold, | 41 SkScalar outerThreshold, |
| 42 SkImageFilter* input) { | 42 SkImageFilter* input) { |
| 43 return SkNEW_ARGS(SkAlphaThresholdFilterImpl, (region, innerThreshold, outer
Threshold, input)); | 43 return SkNEW_ARGS(SkAlphaThresholdFilterImpl, (region, innerThreshold, outer
Threshold, input)); |
| 44 } | 44 } |
| 45 | 45 |
| 46 #if SK_SUPPORT_GPU | 46 #if SK_SUPPORT_GPU |
| 47 #include "GrContext.h" | 47 #include "GrContext.h" |
| 48 #include "GrCoordTransform.h" | 48 #include "GrCoordTransform.h" |
| 49 #include "GrEffect.h" | 49 #include "GrEffect.h" |
| 50 #include "gl/GrGLEffect.h" | 50 #include "gl/GrGLEffect.h" |
| 51 #include "gl/GrGLShaderBuilder.h" | 51 #include "gl/builders/GrGLProgramBuilder.h" |
| 52 #include "GrTBackendEffectFactory.h" | 52 #include "GrTBackendEffectFactory.h" |
| 53 #include "GrTextureAccess.h" | 53 #include "GrTextureAccess.h" |
| 54 | 54 |
| 55 #include "SkGr.h" | 55 #include "SkGr.h" |
| 56 | 56 |
| 57 class GrGLAlphaThresholdEffect; | 57 class GrGLAlphaThresholdEffect; |
| 58 | 58 |
| 59 class AlphaThresholdEffect : public GrEffect { | 59 class AlphaThresholdEffect : public GrEffect { |
| 60 | 60 |
| 61 public: | 61 public: |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 GrCoordTransform fMaskCoordTransform; | 109 GrCoordTransform fMaskCoordTransform; |
| 110 GrTextureAccess fMaskTextureAccess; | 110 GrTextureAccess fMaskTextureAccess; |
| 111 | 111 |
| 112 typedef GrEffect INHERITED; | 112 typedef GrEffect INHERITED; |
| 113 }; | 113 }; |
| 114 | 114 |
| 115 class GrGLAlphaThresholdEffect : public GrGLEffect { | 115 class GrGLAlphaThresholdEffect : public GrGLEffect { |
| 116 public: | 116 public: |
| 117 GrGLAlphaThresholdEffect(const GrBackendEffectFactory&, const GrDrawEffect&)
; | 117 GrGLAlphaThresholdEffect(const GrBackendEffectFactory&, const GrDrawEffect&)
; |
| 118 | 118 |
| 119 virtual void emitCode(GrGLShaderBuilder*, | 119 virtual void emitCode(GrGLProgramBuilder*, |
| 120 const GrDrawEffect&, | 120 const GrDrawEffect&, |
| 121 const GrEffectKey&, | 121 const GrEffectKey&, |
| 122 const char* outputColor, | 122 const char* outputColor, |
| 123 const char* inputColor, | 123 const char* inputColor, |
| 124 const TransformedCoordsArray&, | 124 const TransformedCoordsArray&, |
| 125 const TextureSamplerArray&) SK_OVERRIDE; | 125 const TextureSamplerArray&) SK_OVERRIDE; |
| 126 | 126 |
| 127 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_
OVERRIDE; | 127 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_
OVERRIDE; |
| 128 | 128 |
| 129 private: | 129 private: |
| 130 | 130 |
| 131 GrGLProgramDataManager::UniformHandle fInnerThresholdVar; | 131 GrGLProgramDataManager::UniformHandle fInnerThresholdVar; |
| 132 GrGLProgramDataManager::UniformHandle fOuterThresholdVar; | 132 GrGLProgramDataManager::UniformHandle fOuterThresholdVar; |
| 133 | 133 |
| 134 typedef GrGLEffect INHERITED; | 134 typedef GrGLEffect INHERITED; |
| 135 }; | 135 }; |
| 136 | 136 |
| 137 GrGLAlphaThresholdEffect::GrGLAlphaThresholdEffect(const GrBackendEffectFactory&
factory, const GrDrawEffect&) | 137 GrGLAlphaThresholdEffect::GrGLAlphaThresholdEffect(const GrBackendEffectFactory&
factory, const GrDrawEffect&) |
| 138 : INHERITED(factory) { | 138 : INHERITED(factory) { |
| 139 } | 139 } |
| 140 | 140 |
| 141 void GrGLAlphaThresholdEffect::emitCode(GrGLShaderBuilder* builder, | 141 void GrGLAlphaThresholdEffect::emitCode(GrGLProgramBuilder* builder, |
| 142 const GrDrawEffect&, | 142 const GrDrawEffect&, |
| 143 const GrEffectKey& key, | 143 const GrEffectKey& key, |
| 144 const char* outputColor, | 144 const char* outputColor, |
| 145 const char* inputColor, | 145 const char* inputColor, |
| 146 const TransformedCoordsArray& coords, | 146 const TransformedCoordsArray& coords, |
| 147 const TextureSamplerArray& samplers) { | 147 const TextureSamplerArray& samplers) { |
| 148 SkString coords2D = builder->ensureFSCoords2D(coords, 0); | |
| 149 SkString maskCoords2D = builder->ensureFSCoords2D(coords, 1); | |
| 150 fInnerThresholdVar = builder->addUniform( | 148 fInnerThresholdVar = builder->addUniform( |
| 151 GrGLShaderBuilder::kFragment_Visibility, | 149 GrGLProgramBuilder::kFragment_Visibility, |
| 152 kFloat_GrSLType, "inner_threshold"); | 150 kFloat_GrSLType, "inner_threshold"); |
| 153 fOuterThresholdVar = builder->addUniform( | 151 fOuterThresholdVar = builder->addUniform( |
| 154 GrGLShaderBuilder::kFragment_Visibility, | 152 GrGLProgramBuilder::kFragment_Visibility, |
| 155 kFloat_GrSLType, "outer_threshold"); | 153 kFloat_GrSLType, "outer_threshold"); |
| 156 | 154 |
| 157 builder->fsCodeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str()); | 155 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 158 builder->fsCodeAppendf("\t\tvec2 mask_coord = %s;\n", maskCoords2D.c_str()); | 156 SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0); |
| 159 builder->fsCodeAppend("\t\tvec4 input_color = "); | 157 SkString maskCoords2D = fsBuilder->ensureFSCoords2D(coords, 1); |
| 160 builder->fsAppendTextureLookup(samplers[0], "coord"); | |
| 161 builder->fsCodeAppend(";\n"); | |
| 162 builder->fsCodeAppend("\t\tvec4 mask_color = "); | |
| 163 builder->fsAppendTextureLookup(samplers[1], "mask_coord"); | |
| 164 builder->fsCodeAppend(";\n"); | |
| 165 | 158 |
| 166 builder->fsCodeAppendf("\t\tfloat inner_thresh = %s;\n", | 159 fsBuilder->codeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str()); |
| 160 fsBuilder->codeAppendf("\t\tvec2 mask_coord = %s;\n", maskCoords2D.c_str()); |
| 161 fsBuilder->codeAppend("\t\tvec4 input_color = "); |
| 162 fsBuilder->appendTextureLookup(samplers[0], "coord"); |
| 163 fsBuilder->codeAppend(";\n"); |
| 164 fsBuilder->codeAppend("\t\tvec4 mask_color = "); |
| 165 fsBuilder->appendTextureLookup(samplers[1], "mask_coord"); |
| 166 fsBuilder->codeAppend(";\n"); |
| 167 |
| 168 fsBuilder->codeAppendf("\t\tfloat inner_thresh = %s;\n", |
| 167 builder->getUniformCStr(fInnerThresholdVar)); | 169 builder->getUniformCStr(fInnerThresholdVar)); |
| 168 builder->fsCodeAppendf("\t\tfloat outer_thresh = %s;\n", | 170 fsBuilder->codeAppendf("\t\tfloat outer_thresh = %s;\n", |
| 169 builder->getUniformCStr(fOuterThresholdVar)); | 171 builder->getUniformCStr(fOuterThresholdVar)); |
| 170 builder->fsCodeAppend("\t\tfloat mask = mask_color.a;\n"); | 172 fsBuilder->codeAppend("\t\tfloat mask = mask_color.a;\n"); |
| 171 | 173 |
| 172 builder->fsCodeAppend("vec4 color = input_color;\n"); | 174 fsBuilder->codeAppend("vec4 color = input_color;\n"); |
| 173 builder->fsCodeAppend("\t\tif (mask < 0.5) {\n" | 175 fsBuilder->codeAppend("\t\tif (mask < 0.5) {\n" |
| 174 "\t\t\tif (color.a > outer_thresh) {\n" | 176 "\t\t\tif (color.a > outer_thresh) {\n" |
| 175 "\t\t\t\tfloat scale = outer_thresh / color.a;\n" | 177 "\t\t\t\tfloat scale = outer_thresh / color.a;\n" |
| 176 "\t\t\t\tcolor.rgb *= scale;\n" | 178 "\t\t\t\tcolor.rgb *= scale;\n" |
| 177 "\t\t\t\tcolor.a = outer_thresh;\n" | 179 "\t\t\t\tcolor.a = outer_thresh;\n" |
| 178 "\t\t\t}\n" | 180 "\t\t\t}\n" |
| 179 "\t\t} else if (color.a < inner_thresh) {\n" | 181 "\t\t} else if (color.a < inner_thresh) {\n" |
| 180 "\t\t\tfloat scale = inner_thresh / max(0.001, color.a
);\n" | 182 "\t\t\tfloat scale = inner_thresh / max(0.001, color.a
);\n" |
| 181 "\t\t\tcolor.rgb *= scale;\n" | 183 "\t\t\tcolor.rgb *= scale;\n" |
| 182 "\t\t\tcolor.a = inner_thresh;\n" | 184 "\t\t\tcolor.a = inner_thresh;\n" |
| 183 "\t\t}\n"); | 185 "\t\t}\n"); |
| 184 | 186 |
| 185 builder->fsCodeAppendf("%s = %s;\n", outputColor, | 187 fsBuilder->codeAppendf("%s = %s;\n", outputColor, |
| 186 (GrGLSLExpr4(inputColor) * GrGLSLExpr4("color")).c_st
r()); | 188 (GrGLSLExpr4(inputColor) * GrGLSLExpr4("color")).c_st
r()); |
| 187 } | 189 } |
| 188 | 190 |
| 189 void GrGLAlphaThresholdEffect::setData(const GrGLProgramDataManager& pdman, | 191 void GrGLAlphaThresholdEffect::setData(const GrGLProgramDataManager& pdman, |
| 190 const GrDrawEffect& drawEffect) { | 192 const GrDrawEffect& drawEffect) { |
| 191 const AlphaThresholdEffect& alpha_threshold = | 193 const AlphaThresholdEffect& alpha_threshold = |
| 192 drawEffect.castEffect<AlphaThresholdEffect>(); | 194 drawEffect.castEffect<AlphaThresholdEffect>(); |
| 193 pdman.set1f(fInnerThresholdVar, alpha_threshold.innerThreshold()); | 195 pdman.set1f(fInnerThresholdVar, alpha_threshold.innerThreshold()); |
| 194 pdman.set1f(fOuterThresholdVar, alpha_threshold.outerThreshold()); | 196 pdman.set1f(fOuterThresholdVar, alpha_threshold.outerThreshold()); |
| 195 } | 197 } |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 (U8CPU)(SkColorGetG(source) *
scale), | 362 (U8CPU)(SkColorGetG(source) *
scale), |
| 361 (U8CPU)(SkColorGetB(source) *
scale)); | 363 (U8CPU)(SkColorGetB(source) *
scale)); |
| 362 } | 364 } |
| 363 } | 365 } |
| 364 dptr[y * dst->width() + x] = output_color; | 366 dptr[y * dst->width() + x] = output_color; |
| 365 } | 367 } |
| 366 } | 368 } |
| 367 | 369 |
| 368 return true; | 370 return true; |
| 369 } | 371 } |
| OLD | NEW |