| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 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 "gl/builders/GrGLProgramBuilder.h" | 8 #include "gl/builders/GrGLProgramBuilder.h" |
| 9 #include "GrDitherEffect.h" | 9 #include "GrDitherEffect.h" |
| 10 | 10 |
| 11 #include "gl/GrGLEffect.h" | 11 #include "gl/GrGLProcessor.h" |
| 12 #include "gl/GrGLSL.h" | 12 #include "gl/GrGLSL.h" |
| 13 #include "GrTBackendEffectFactory.h" | 13 #include "GrTBackendProcessorFactory.h" |
| 14 | 14 |
| 15 #include "SkRect.h" | 15 #include "SkRect.h" |
| 16 | 16 |
| 17 ////////////////////////////////////////////////////////////////////////////// | 17 ////////////////////////////////////////////////////////////////////////////// |
| 18 | 18 |
| 19 class GLDitherEffect; | 19 class GLDitherEffect; |
| 20 | 20 |
| 21 class DitherEffect : public GrEffect { | 21 class DitherEffect : public GrFragmentProcessor { |
| 22 public: | 22 public: |
| 23 static GrEffect* Create() { | 23 static GrFragmentProcessor* Create() { |
| 24 GR_CREATE_STATIC_EFFECT(gDitherEffect, DitherEffect, ()) | 24 GR_CREATE_STATIC_FRAGMENT_PROCESSOR(gDitherEffect, DitherEffect, ()) |
| 25 return SkRef(gDitherEffect); | 25 return SkRef(gDitherEffect); |
| 26 } | 26 } |
| 27 | 27 |
| 28 virtual ~DitherEffect() {}; | 28 virtual ~DitherEffect() {}; |
| 29 static const char* Name() { return "Dither"; } | 29 static const char* Name() { return "Dither"; } |
| 30 | 30 |
| 31 typedef GLDitherEffect GLEffect; | 31 typedef GLDitherEffect GLProcessor; |
| 32 | 32 |
| 33 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags
) const SK_OVERRIDE; | 33 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags
) const SK_OVERRIDE; |
| 34 | 34 |
| 35 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { | 35 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERR
IDE { |
| 36 return GrTBackendEffectFactory<DitherEffect>::getInstance(); | 36 return GrTBackendFragmentProcessorFactory<DitherEffect>::getInstance(); |
| 37 } | 37 } |
| 38 | 38 |
| 39 private: | 39 private: |
| 40 DitherEffect() { | 40 DitherEffect() { |
| 41 this->setWillReadFragmentPosition(); | 41 this->setWillReadFragmentPosition(); |
| 42 } | 42 } |
| 43 | 43 |
| 44 // All dither effects are equal | 44 // All dither effects are equal |
| 45 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE { return true; } | 45 virtual bool onIsEqual(const GrProcessor&) const SK_OVERRIDE { return true;
} |
| 46 | 46 |
| 47 GR_DECLARE_EFFECT_TEST; | 47 GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
| 48 | 48 |
| 49 typedef GrEffect INHERITED; | 49 typedef GrFragmentProcessor INHERITED; |
| 50 }; | 50 }; |
| 51 | 51 |
| 52 void DitherEffect::getConstantColorComponents(GrColor* color, uint32_t* validFla
gs) const { | 52 void DitherEffect::getConstantColorComponents(GrColor* color, uint32_t* validFla
gs) const { |
| 53 *validFlags = 0; | 53 *validFlags = 0; |
| 54 } | 54 } |
| 55 | 55 |
| 56 ////////////////////////////////////////////////////////////////////////////// | 56 ////////////////////////////////////////////////////////////////////////////// |
| 57 | 57 |
| 58 GR_DEFINE_EFFECT_TEST(DitherEffect); | 58 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(DitherEffect); |
| 59 | 59 |
| 60 GrEffect* DitherEffect::TestCreate(SkRandom*, | 60 GrFragmentProcessor* DitherEffect::TestCreate(SkRandom*, |
| 61 GrContext*, | 61 GrContext*, |
| 62 const GrDrawTargetCaps&, | 62 const GrDrawTargetCaps&, |
| 63 GrTexture*[]) { | 63 GrTexture*[]) { |
| 64 return DitherEffect::Create(); | 64 return DitherEffect::Create(); |
| 65 } | 65 } |
| 66 | 66 |
| 67 ////////////////////////////////////////////////////////////////////////////// | 67 ////////////////////////////////////////////////////////////////////////////// |
| 68 | 68 |
| 69 class GLDitherEffect : public GrGLEffect { | 69 class GLDitherEffect : public GrGLFragmentProcessor { |
| 70 public: | 70 public: |
| 71 GLDitherEffect(const GrBackendEffectFactory&, const GrEffect&); | 71 GLDitherEffect(const GrBackendProcessorFactory&, const GrProcessor&); |
| 72 | 72 |
| 73 virtual void emitCode(GrGLProgramBuilder* builder, | 73 virtual void emitCode(GrGLProgramBuilder* builder, |
| 74 const GrEffect& effect, | 74 const GrFragmentProcessor& fp, |
| 75 const GrEffectKey& key, | 75 const GrProcessorKey& key, |
| 76 const char* outputColor, | 76 const char* outputColor, |
| 77 const char* inputColor, | 77 const char* inputColor, |
| 78 const TransformedCoordsArray&, | 78 const TransformedCoordsArray&, |
| 79 const TextureSamplerArray&) SK_OVERRIDE; | 79 const TextureSamplerArray&) SK_OVERRIDE; |
| 80 | 80 |
| 81 private: | 81 private: |
| 82 typedef GrGLEffect INHERITED; | 82 typedef GrGLFragmentProcessor INHERITED; |
| 83 }; | 83 }; |
| 84 | 84 |
| 85 GLDitherEffect::GLDitherEffect(const GrBackendEffectFactory& factory, | 85 GLDitherEffect::GLDitherEffect(const GrBackendProcessorFactory& factory, |
| 86 const GrEffect& effect) | 86 const GrProcessor&) |
| 87 : INHERITED (factory) { | 87 : INHERITED (factory) { |
| 88 } | 88 } |
| 89 | 89 |
| 90 void GLDitherEffect::emitCode(GrGLProgramBuilder* builder, | 90 void GLDitherEffect::emitCode(GrGLProgramBuilder* builder, |
| 91 const GrEffect& effect, | 91 const GrFragmentProcessor& fp, |
| 92 const GrEffectKey& key, | 92 const GrProcessorKey& key, |
| 93 const char* outputColor, | 93 const char* outputColor, |
| 94 const char* inputColor, | 94 const char* inputColor, |
| 95 const TransformedCoordsArray&, | 95 const TransformedCoordsArray&, |
| 96 const TextureSamplerArray& samplers) { | 96 const TextureSamplerArray& samplers) { |
| 97 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); | 97 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 98 // Generate a random number based on the fragment position. For this | 98 // Generate a random number based on the fragment position. For this |
| 99 // random number generator, we use the "GLSL rand" function | 99 // random number generator, we use the "GLSL rand" function |
| 100 // that seems to be floating around on the internet. It works under | 100 // that seems to be floating around on the internet. It works under |
| 101 // the assumption that sin(<big number>) oscillates with high frequency | 101 // the assumption that sin(<big number>) oscillates with high frequency |
| 102 // and sampling it will generate "randomness". Since we're using this | 102 // and sampling it will generate "randomness". Since we're using this |
| 103 // for rendering and not cryptography it should be OK. | 103 // for rendering and not cryptography it should be OK. |
| 104 | 104 |
| 105 // For each channel c, add the random offset to the pixel to either bump | 105 // For each channel c, add the random offset to the pixel to either bump |
| 106 // it up or let it remain constant during quantization. | 106 // it up or let it remain constant during quantization. |
| 107 fsBuilder->codeAppendf("\t\tfloat r = " | 107 fsBuilder->codeAppendf("\t\tfloat r = " |
| 108 "fract(sin(dot(%s.xy ,vec2(12.9898,78.233))) * 43758.
5453);\n", | 108 "fract(sin(dot(%s.xy ,vec2(12.9898,78.233))) * 43758.
5453);\n", |
| 109 fsBuilder->fragmentPosition()); | 109 fsBuilder->fragmentPosition()); |
| 110 fsBuilder->codeAppendf("\t\t%s = (1.0/255.0) * vec4(r, r, r, r) + %s;\n", | 110 fsBuilder->codeAppendf("\t\t%s = (1.0/255.0) * vec4(r, r, r, r) + %s;\n", |
| 111 outputColor, GrGLSLExpr4(inputColor).c_str()); | 111 outputColor, GrGLSLExpr4(inputColor).c_str()); |
| 112 } | 112 } |
| 113 | 113 |
| 114 ////////////////////////////////////////////////////////////////////////////// | 114 ////////////////////////////////////////////////////////////////////////////// |
| 115 | 115 |
| 116 GrEffect* GrDitherEffect::Create() { return DitherEffect::Create(); } | 116 GrFragmentProcessor* GrDitherEffect::Create() { return DitherEffect::Create(); } |
| OLD | NEW |