| 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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 SkScalar outerThreshold, | 43 SkScalar outerThreshold, |
| 44 SkImageFilter* input) { | 44 SkImageFilter* input) { |
| 45 return SkNEW_ARGS(SkAlphaThresholdFilterImpl, (region, innerThreshold, outer
Threshold, input)); | 45 return SkNEW_ARGS(SkAlphaThresholdFilterImpl, (region, innerThreshold, outer
Threshold, input)); |
| 46 } | 46 } |
| 47 | 47 |
| 48 #if SK_SUPPORT_GPU | 48 #if SK_SUPPORT_GPU |
| 49 #include "GrContext.h" | 49 #include "GrContext.h" |
| 50 #include "GrCoordTransform.h" | 50 #include "GrCoordTransform.h" |
| 51 #include "GrEffect.h" | 51 #include "GrEffect.h" |
| 52 #include "gl/GrGLEffect.h" | 52 #include "gl/GrGLEffect.h" |
| 53 #include "gl/GrGLShaderBuilder.h" | 53 #include "gl/builders/GrGLProgramBuilder.h" |
| 54 #include "GrTBackendEffectFactory.h" | 54 #include "GrTBackendEffectFactory.h" |
| 55 #include "GrTextureAccess.h" | 55 #include "GrTextureAccess.h" |
| 56 | 56 |
| 57 #include "SkGr.h" | 57 #include "SkGr.h" |
| 58 | 58 |
| 59 class GrGLAlphaThresholdEffect; | 59 class GrGLAlphaThresholdEffect; |
| 60 | 60 |
| 61 class AlphaThresholdEffect : public GrEffect { | 61 class AlphaThresholdEffect : public GrEffect { |
| 62 | 62 |
| 63 public: | 63 public: |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 GrCoordTransform fMaskCoordTransform; | 111 GrCoordTransform fMaskCoordTransform; |
| 112 GrTextureAccess fMaskTextureAccess; | 112 GrTextureAccess fMaskTextureAccess; |
| 113 | 113 |
| 114 typedef GrEffect INHERITED; | 114 typedef GrEffect INHERITED; |
| 115 }; | 115 }; |
| 116 | 116 |
| 117 class GrGLAlphaThresholdEffect : public GrGLEffect { | 117 class GrGLAlphaThresholdEffect : public GrGLEffect { |
| 118 public: | 118 public: |
| 119 GrGLAlphaThresholdEffect(const GrBackendEffectFactory&, const GrDrawEffect&)
; | 119 GrGLAlphaThresholdEffect(const GrBackendEffectFactory&, const GrDrawEffect&)
; |
| 120 | 120 |
| 121 virtual void emitCode(GrGLShaderBuilder*, | 121 virtual void emitCode(GrGLProgramBuilder*, |
| 122 const GrDrawEffect&, | 122 const GrDrawEffect&, |
| 123 const GrEffectKey&, | 123 const GrEffectKey&, |
| 124 const char* outputColor, | 124 const char* outputColor, |
| 125 const char* inputColor, | 125 const char* inputColor, |
| 126 const TransformedCoordsArray&, | 126 const TransformedCoordsArray&, |
| 127 const TextureSamplerArray&) SK_OVERRIDE; | 127 const TextureSamplerArray&) SK_OVERRIDE; |
| 128 | 128 |
| 129 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_
OVERRIDE; | 129 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_
OVERRIDE; |
| 130 | 130 |
| 131 private: | 131 private: |
| 132 | 132 |
| 133 GrGLProgramDataManager::UniformHandle fInnerThresholdVar; | 133 GrGLProgramDataManager::UniformHandle fInnerThresholdVar; |
| 134 GrGLProgramDataManager::UniformHandle fOuterThresholdVar; | 134 GrGLProgramDataManager::UniformHandle fOuterThresholdVar; |
| 135 | 135 |
| 136 typedef GrGLEffect INHERITED; | 136 typedef GrGLEffect INHERITED; |
| 137 }; | 137 }; |
| 138 | 138 |
| 139 GrGLAlphaThresholdEffect::GrGLAlphaThresholdEffect(const GrBackendEffectFactory&
factory, const GrDrawEffect&) | 139 GrGLAlphaThresholdEffect::GrGLAlphaThresholdEffect(const GrBackendEffectFactory&
factory, const GrDrawEffect&) |
| 140 : INHERITED(factory) { | 140 : INHERITED(factory) { |
| 141 } | 141 } |
| 142 | 142 |
| 143 void GrGLAlphaThresholdEffect::emitCode(GrGLShaderBuilder* builder, | 143 void GrGLAlphaThresholdEffect::emitCode(GrGLProgramBuilder* builder, |
| 144 const GrDrawEffect&, | 144 const GrDrawEffect&, |
| 145 const GrEffectKey& key, | 145 const GrEffectKey& key, |
| 146 const char* outputColor, | 146 const char* outputColor, |
| 147 const char* inputColor, | 147 const char* inputColor, |
| 148 const TransformedCoordsArray& coords, | 148 const TransformedCoordsArray& coords, |
| 149 const TextureSamplerArray& samplers) { | 149 const TextureSamplerArray& samplers) { |
| 150 SkString coords2D = builder->ensureFSCoords2D(coords, 0); | |
| 151 SkString maskCoords2D = builder->ensureFSCoords2D(coords, 1); | |
| 152 fInnerThresholdVar = builder->addUniform( | 150 fInnerThresholdVar = builder->addUniform( |
| 153 GrGLShaderBuilder::kFragment_Visibility, | 151 GrGLProgramBuilder::kFragment_Visibility, |
| 154 kFloat_GrSLType, "inner_threshold"); | 152 kFloat_GrSLType, "inner_threshold"); |
| 155 fOuterThresholdVar = builder->addUniform( | 153 fOuterThresholdVar = builder->addUniform( |
| 156 GrGLShaderBuilder::kFragment_Visibility, | 154 GrGLProgramBuilder::kFragment_Visibility, |
| 157 kFloat_GrSLType, "outer_threshold"); | 155 kFloat_GrSLType, "outer_threshold"); |
| 158 | 156 |
| 159 builder->fsCodeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str()); | 157 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 160 builder->fsCodeAppendf("\t\tvec2 mask_coord = %s;\n", maskCoords2D.c_str()); | 158 SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0); |
| 161 builder->fsCodeAppend("\t\tvec4 input_color = "); | 159 SkString maskCoords2D = fsBuilder->ensureFSCoords2D(coords, 1); |
| 162 builder->fsAppendTextureLookup(samplers[0], "coord"); | |
| 163 builder->fsCodeAppend(";\n"); | |
| 164 builder->fsCodeAppend("\t\tvec4 mask_color = "); | |
| 165 builder->fsAppendTextureLookup(samplers[1], "mask_coord"); | |
| 166 builder->fsCodeAppend(";\n"); | |
| 167 | 160 |
| 168 builder->fsCodeAppendf("\t\tfloat inner_thresh = %s;\n", | 161 fsBuilder->codeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str()); |
| 162 fsBuilder->codeAppendf("\t\tvec2 mask_coord = %s;\n", maskCoords2D.c_str()); |
| 163 fsBuilder->codeAppend("\t\tvec4 input_color = "); |
| 164 fsBuilder->appendTextureLookup(samplers[0], "coord"); |
| 165 fsBuilder->codeAppend(";\n"); |
| 166 fsBuilder->codeAppend("\t\tvec4 mask_color = "); |
| 167 fsBuilder->appendTextureLookup(samplers[1], "mask_coord"); |
| 168 fsBuilder->codeAppend(";\n"); |
| 169 |
| 170 fsBuilder->codeAppendf("\t\tfloat inner_thresh = %s;\n", |
| 169 builder->getUniformCStr(fInnerThresholdVar)); | 171 builder->getUniformCStr(fInnerThresholdVar)); |
| 170 builder->fsCodeAppendf("\t\tfloat outer_thresh = %s;\n", | 172 fsBuilder->codeAppendf("\t\tfloat outer_thresh = %s;\n", |
| 171 builder->getUniformCStr(fOuterThresholdVar)); | 173 builder->getUniformCStr(fOuterThresholdVar)); |
| 172 builder->fsCodeAppend("\t\tfloat mask = mask_color.a;\n"); | 174 fsBuilder->codeAppend("\t\tfloat mask = mask_color.a;\n"); |
| 173 | 175 |
| 174 builder->fsCodeAppend("vec4 color = input_color;\n"); | 176 fsBuilder->codeAppend("vec4 color = input_color;\n"); |
| 175 builder->fsCodeAppend("\t\tif (mask < 0.5) {\n" | 177 fsBuilder->codeAppend("\t\tif (mask < 0.5) {\n" |
| 176 "\t\t\tif (color.a > outer_thresh) {\n" | 178 "\t\t\tif (color.a > outer_thresh) {\n" |
| 177 "\t\t\t\tfloat scale = outer_thresh / color.a;\n" | 179 "\t\t\t\tfloat scale = outer_thresh / color.a;\n" |
| 178 "\t\t\t\tcolor.rgb *= scale;\n" | 180 "\t\t\t\tcolor.rgb *= scale;\n" |
| 179 "\t\t\t\tcolor.a = outer_thresh;\n" | 181 "\t\t\t\tcolor.a = outer_thresh;\n" |
| 180 "\t\t\t}\n" | 182 "\t\t\t}\n" |
| 181 "\t\t} else if (color.a < inner_thresh) {\n" | 183 "\t\t} else if (color.a < inner_thresh) {\n" |
| 182 "\t\t\tfloat scale = inner_thresh / max(0.001, color.a
);\n" | 184 "\t\t\tfloat scale = inner_thresh / max(0.001, color.a
);\n" |
| 183 "\t\t\tcolor.rgb *= scale;\n" | 185 "\t\t\tcolor.rgb *= scale;\n" |
| 184 "\t\t\tcolor.a = inner_thresh;\n" | 186 "\t\t\tcolor.a = inner_thresh;\n" |
| 185 "\t\t}\n"); | 187 "\t\t}\n"); |
| 186 | 188 |
| 187 builder->fsCodeAppendf("%s = %s;\n", outputColor, | 189 fsBuilder->codeAppendf("%s = %s;\n", outputColor, |
| 188 (GrGLSLExpr4(inputColor) * GrGLSLExpr4("color")).c_st
r()); | 190 (GrGLSLExpr4(inputColor) * GrGLSLExpr4("color")).c_st
r()); |
| 189 } | 191 } |
| 190 | 192 |
| 191 void GrGLAlphaThresholdEffect::setData(const GrGLProgramDataManager& pdman, | 193 void GrGLAlphaThresholdEffect::setData(const GrGLProgramDataManager& pdman, |
| 192 const GrDrawEffect& drawEffect) { | 194 const GrDrawEffect& drawEffect) { |
| 193 const AlphaThresholdEffect& alpha_threshold = | 195 const AlphaThresholdEffect& alpha_threshold = |
| 194 drawEffect.castEffect<AlphaThresholdEffect>(); | 196 drawEffect.castEffect<AlphaThresholdEffect>(); |
| 195 pdman.set1f(fInnerThresholdVar, alpha_threshold.innerThreshold()); | 197 pdman.set1f(fInnerThresholdVar, alpha_threshold.innerThreshold()); |
| 196 pdman.set1f(fOuterThresholdVar, alpha_threshold.outerThreshold()); | 198 pdman.set1f(fOuterThresholdVar, alpha_threshold.outerThreshold()); |
| 197 } | 199 } |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 (U8CPU)(SkColorGetG(source) *
scale), | 375 (U8CPU)(SkColorGetG(source) *
scale), |
| 374 (U8CPU)(SkColorGetB(source) *
scale)); | 376 (U8CPU)(SkColorGetB(source) *
scale)); |
| 375 } | 377 } |
| 376 } | 378 } |
| 377 dptr[y * dst->width() + x] = output_color; | 379 dptr[y * dst->width() + x] = output_color; |
| 378 } | 380 } |
| 379 } | 381 } |
| 380 | 382 |
| 381 return true; | 383 return true; |
| 382 } | 384 } |
| OLD | NEW |