| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 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 "SkColorMatrixFilterRowMajor255.h" | 8 #include "SkColorMatrixFilterRowMajor255.h" |
| 9 #include "SkColorPriv.h" | 9 #include "SkColorPriv.h" |
| 10 #include "SkNx.h" | 10 #include "SkNx.h" |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 return new ColorMatrixEffect(matrix); | 246 return new ColorMatrixEffect(matrix); |
| 247 } | 247 } |
| 248 | 248 |
| 249 const char* name() const override { return "Color Matrix"; } | 249 const char* name() const override { return "Color Matrix"; } |
| 250 | 250 |
| 251 GR_DECLARE_FRAGMENT_PROCESSOR_TEST; | 251 GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
| 252 | 252 |
| 253 class GLSLProcessor : public GrGLSLFragmentProcessor { | 253 class GLSLProcessor : public GrGLSLFragmentProcessor { |
| 254 public: | 254 public: |
| 255 // this class always generates the same code. | 255 // this class always generates the same code. |
| 256 static void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKey
Builder* b) {} | 256 static void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKey
Builder*) {} |
| 257 | 257 |
| 258 GLSLProcessor(const GrProcessor&) {} | 258 void emitCode(EmitArgs& args) override { |
| 259 | |
| 260 virtual void emitCode(EmitArgs& args) override { | |
| 261 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; | 259 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 262 fMatrixHandle = uniformHandler->addUniform(GrGLSLUniformHandler::kFr
agment_Visibility, | 260 fMatrixHandle = uniformHandler->addUniform(GrGLSLUniformHandler::kFr
agment_Visibility, |
| 263 kMat44f_GrSLType, kDefaul
t_GrSLPrecision, | 261 kMat44f_GrSLType, kDefaul
t_GrSLPrecision, |
| 264 "ColorMatrix"); | 262 "ColorMatrix"); |
| 265 fVectorHandle = uniformHandler->addUniform(GrGLSLUniformHandler::kFr
agment_Visibility, | 263 fVectorHandle = uniformHandler->addUniform(GrGLSLUniformHandler::kFr
agment_Visibility, |
| 266 kVec4f_GrSLType, kDefault
_GrSLPrecision, | 264 kVec4f_GrSLType, kDefault
_GrSLPrecision, |
| 267 "ColorMatrixVector"); | 265 "ColorMatrixVector"); |
| 268 | 266 |
| 269 if (nullptr == args.fInputColor) { | 267 if (nullptr == args.fInputColor) { |
| 270 // could optimize this case, but we aren't for now. | 268 // could optimize this case, but we aren't for now. |
| 271 args.fInputColor = "vec4(1)"; | 269 args.fInputColor = "vec4(1)"; |
| 272 } | 270 } |
| 273 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; | 271 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 274 // The max() is to guard against 0 / 0 during unpremul when the inco
ming color is | 272 // The max() is to guard against 0 / 0 during unpremul when the inco
ming color is |
| 275 // transparent black. | 273 // transparent black. |
| 276 fragBuilder->codeAppendf("\tfloat nonZeroAlpha = max(%s.a, 0.00001);
\n", | 274 fragBuilder->codeAppendf("\tfloat nonZeroAlpha = max(%s.a, 0.00001);
\n", |
| 277 args.fInputColor); | 275 args.fInputColor); |
| 278 fragBuilder->codeAppendf("\t%s = %s * vec4(%s.rgb / nonZeroAlpha, no
nZeroAlpha) + %s;\n", | 276 fragBuilder->codeAppendf("\t%s = %s * vec4(%s.rgb / nonZeroAlpha, no
nZeroAlpha) + %s;\n", |
| 279 args.fOutputColor, | 277 args.fOutputColor, |
| 280 uniformHandler->getUniformCStr(fMatrixHandl
e), | 278 uniformHandler->getUniformCStr(fMatrixHandl
e), |
| 281 args.fInputColor, | 279 args.fInputColor, |
| 282 uniformHandler->getUniformCStr(fVectorHandl
e)); | 280 uniformHandler->getUniformCStr(fVectorHandl
e)); |
| 283 fragBuilder->codeAppendf("\t%s = clamp(%s, 0.0, 1.0);\n", | 281 fragBuilder->codeAppendf("\t%s = clamp(%s, 0.0, 1.0);\n", |
| 284 args.fOutputColor, args.fOutputColor); | 282 args.fOutputColor, args.fOutputColor); |
| 285 fragBuilder->codeAppendf("\t%s.rgb *= %s.a;\n", args.fOutputColor, a
rgs.fOutputColor); | 283 fragBuilder->codeAppendf("\t%s.rgb *= %s.a;\n", args.fOutputColor, a
rgs.fOutputColor); |
| 286 } | 284 } |
| 287 | 285 |
| 288 protected: | 286 protected: |
| 289 virtual void onSetData(const GrGLSLProgramDataManager& uniManager, | 287 void onSetData(const GrGLSLProgramDataManager& uniManager, |
| 290 const GrProcessor& proc) override { | 288 const GrProcessor& proc) override { |
| 291 const ColorMatrixEffect& cme = proc.cast<ColorMatrixEffect>(); | 289 const ColorMatrixEffect& cme = proc.cast<ColorMatrixEffect>(); |
| 292 const float* m = cme.fMatrix; | 290 const float* m = cme.fMatrix; |
| 293 // The GL matrix is transposed from SkColorMatrix. | 291 // The GL matrix is transposed from SkColorMatrix. |
| 294 float mt[] = { | 292 float mt[] = { |
| 295 m[0], m[5], m[10], m[15], | 293 m[0], m[5], m[10], m[15], |
| 296 m[1], m[6], m[11], m[16], | 294 m[1], m[6], m[11], m[16], |
| 297 m[2], m[7], m[12], m[17], | 295 m[2], m[7], m[12], m[17], |
| 298 m[3], m[8], m[13], m[18], | 296 m[3], m[8], m[13], m[18], |
| 299 }; | 297 }; |
| 300 static const float kScale = 1.0f / 255.0f; | 298 static const float kScale = 1.0f / 255.0f; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 312 typedef GrGLSLFragmentProcessor INHERITED; | 310 typedef GrGLSLFragmentProcessor INHERITED; |
| 313 }; | 311 }; |
| 314 | 312 |
| 315 private: | 313 private: |
| 316 ColorMatrixEffect(const SkScalar matrix[20]) { | 314 ColorMatrixEffect(const SkScalar matrix[20]) { |
| 317 memcpy(fMatrix, matrix, sizeof(SkScalar) * 20); | 315 memcpy(fMatrix, matrix, sizeof(SkScalar) * 20); |
| 318 this->initClassID<ColorMatrixEffect>(); | 316 this->initClassID<ColorMatrixEffect>(); |
| 319 } | 317 } |
| 320 | 318 |
| 321 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { | 319 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
| 322 return new GLSLProcessor(*this); | 320 return new GLSLProcessor; |
| 323 } | 321 } |
| 324 | 322 |
| 325 virtual void onGetGLSLProcessorKey(const GrGLSLCaps& caps, | 323 virtual void onGetGLSLProcessorKey(const GrGLSLCaps& caps, |
| 326 GrProcessorKeyBuilder* b) const override
{ | 324 GrProcessorKeyBuilder* b) const override
{ |
| 327 GLSLProcessor::GenKey(*this, caps, b); | 325 GLSLProcessor::GenKey(*this, caps, b); |
| 328 } | 326 } |
| 329 | 327 |
| 330 bool onIsEqual(const GrFragmentProcessor& s) const override { | 328 bool onIsEqual(const GrFragmentProcessor& s) const override { |
| 331 const ColorMatrixEffect& cme = s.cast<ColorMatrixEffect>(); | 329 const ColorMatrixEffect& cme = s.cast<ColorMatrixEffect>(); |
| 332 return 0 == memcmp(fMatrix, cme.fMatrix, sizeof(fMatrix)); | 330 return 0 == memcmp(fMatrix, cme.fMatrix, sizeof(fMatrix)); |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 SkColorFilter* SkColorMatrixFilterRowMajor255::CreateSingleChannelOutput(const S
kScalar row[5]) { | 422 SkColorFilter* SkColorMatrixFilterRowMajor255::CreateSingleChannelOutput(const S
kScalar row[5]) { |
| 425 SkASSERT(row); | 423 SkASSERT(row); |
| 426 SkColorMatrixFilterRowMajor255* cf = new SkColorMatrixFilterRowMajor255(); | 424 SkColorMatrixFilterRowMajor255* cf = new SkColorMatrixFilterRowMajor255(); |
| 427 static_assert(sizeof(SkScalar) * 5 * 4 == sizeof(cf->fMatrix), "sizes don't
match"); | 425 static_assert(sizeof(SkScalar) * 5 * 4 == sizeof(cf->fMatrix), "sizes don't
match"); |
| 428 for (int i = 0; i < 4; ++i) { | 426 for (int i = 0; i < 4; ++i) { |
| 429 memcpy(cf->fMatrix + 5 * i, row, sizeof(SkScalar) * 5); | 427 memcpy(cf->fMatrix + 5 * i, row, sizeof(SkScalar) * 5); |
| 430 } | 428 } |
| 431 cf->initState(); | 429 cf->initState(); |
| 432 return cf; | 430 return cf; |
| 433 } | 431 } |
| OLD | NEW |