blob: 1db61ba146dc3f3dfe86f88d9ebaea6114f3ab72 [file] [log] [blame]
krajcevskif461a8f2014-06-19 14:14:06 -07001/*
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
18class GLDitherEffect;
19
20class DitherEffect : public GrEffect {
21public:
22 static GrEffectRef* Create() {
bsalomon55fad7a2014-07-08 07:34:20 -070023 GR_CREATE_STATIC_EFFECT(gDitherEffect, DitherEffect, ())
24 return SkRef(gDitherEffect);
krajcevskif461a8f2014-06-19 14:14:06 -070025 }
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
38private:
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
51void DitherEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
52 *validFlags = 0;
53}
54
55//////////////////////////////////////////////////////////////////////////////
56
57GR_DEFINE_EFFECT_TEST(DitherEffect);
58
59GrEffectRef* DitherEffect::TestCreate(SkRandom*,
60 GrContext*,
61 const GrDrawTargetCaps&,
62 GrTexture*[]) {
63 return DitherEffect::Create();
64}
65
66//////////////////////////////////////////////////////////////////////////////
67
68class GLDitherEffect : public GrGLEffect {
69public:
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
80private:
81 typedef GrGLEffect INHERITED;
82};
83
84GLDitherEffect::GLDitherEffect(const GrBackendEffectFactory& factory,
85 const GrDrawEffect& drawEffect)
86 : INHERITED (factory) {
87}
88
89void 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 = "
bsalomon22900002014-06-24 11:16:52 -0700106 "fract(sin(dot(%s.xy ,vec2(12.9898,78.233))) * 43758.5453);\n",
krajcevskif461a8f2014-06-19 14:14:06 -0700107 builder->fragmentPosition());
krajcevskiaed70072014-06-20 05:46:12 -0700108 builder->fsCodeAppendf("\t\t%s = (1.0/255.0) * vec4(r, r, r, r) + %s;\n",
krajcevskif461a8f2014-06-19 14:14:06 -0700109 outputColor, GrGLSLExpr4(inputColor).c_str());
110}
111
112//////////////////////////////////////////////////////////////////////////////
113
114GrEffectRef* GrDitherEffect::Create() {
115 return DitherEffect::Create();
116}