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