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