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 #include "GrFragmentProcessor.h"
10 #include "GrInvariantOutput.h"
11 #include "SkRect.h"
12 #include "gl/GrGLProcessor.h"
13 #include "gl/GrGLSL.h"
14 #include "gl/builders/GrGLProgramBuilder.h"
15 
16 //////////////////////////////////////////////////////////////////////////////
17 
18 class DitherEffect : public GrFragmentProcessor {
19 public:
Create()20     static GrFragmentProcessor* Create() {
21         GR_CREATE_STATIC_PROCESSOR(gDitherEffect, DitherEffect, ())
22         return SkRef(gDitherEffect);
23     }
24 
~DitherEffect()25     virtual ~DitherEffect() {};
26 
name() const27     const char* name() const override { return "Dither"; }
28 
29     void getGLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
30 
31     GrGLFragmentProcessor* createGLInstance() const override;
32 
33 private:
DitherEffect()34     DitherEffect() {
35         this->initClassID<DitherEffect>();
36         this->setWillReadFragmentPosition();
37     }
38 
39     // All dither effects are equal
onIsEqual(const GrFragmentProcessor &) const40     bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
41 
42     void onComputeInvariantOutput(GrInvariantOutput* inout) const override;
43 
44     GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
45 
46     typedef GrFragmentProcessor INHERITED;
47 };
48 
onComputeInvariantOutput(GrInvariantOutput * inout) const49 void DitherEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
50     inout->setToUnknown(GrInvariantOutput::kWill_ReadInput);
51 }
52 
53 //////////////////////////////////////////////////////////////////////////////
54 
55 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(DitherEffect);
56 
TestCreate(SkRandom *,GrContext *,const GrDrawTargetCaps &,GrTexture * [])57 GrFragmentProcessor* DitherEffect::TestCreate(SkRandom*,
58                                               GrContext*,
59                                               const GrDrawTargetCaps&,
60                                               GrTexture*[]) {
61     return DitherEffect::Create();
62 }
63 
64 //////////////////////////////////////////////////////////////////////////////
65 
66 class GLDitherEffect : public GrGLFragmentProcessor {
67 public:
68     GLDitherEffect(const GrProcessor&);
69 
70     virtual void emitCode(GrGLFPBuilder* builder,
71                           const GrFragmentProcessor& fp,
72                           const char* outputColor,
73                           const char* inputColor,
74                           const TransformedCoordsArray&,
75                           const TextureSamplerArray&) override;
76 
77 private:
78     typedef GrGLFragmentProcessor INHERITED;
79 };
80 
GLDitherEffect(const GrProcessor &)81 GLDitherEffect::GLDitherEffect(const GrProcessor&) {
82 }
83 
emitCode(GrGLFPBuilder * builder,const GrFragmentProcessor & fp,const char * outputColor,const char * inputColor,const TransformedCoordsArray &,const TextureSamplerArray & samplers)84 void GLDitherEffect::emitCode(GrGLFPBuilder* builder,
85                               const GrFragmentProcessor& fp,
86                               const char* outputColor,
87                               const char* inputColor,
88                               const TransformedCoordsArray&,
89                               const TextureSamplerArray& samplers) {
90     GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
91     // Generate a random number based on the fragment position. For this
92     // random number generator, we use the "GLSL rand" function
93     // that seems to be floating around on the internet. It works under
94     // the assumption that sin(<big number>) oscillates with high frequency
95     // and sampling it will generate "randomness". Since we're using this
96     // for rendering and not cryptography it should be OK.
97 
98     // For each channel c, add the random offset to the pixel to either bump
99     // it up or let it remain constant during quantization.
100     fsBuilder->codeAppendf("\t\tfloat r = "
101                            "fract(sin(dot(%s.xy ,vec2(12.9898,78.233))) * 43758.5453);\n",
102                            fsBuilder->fragmentPosition());
103     fsBuilder->codeAppendf("\t\t%s = (1.0/255.0) * vec4(r, r, r, r) + %s;\n",
104                            outputColor, GrGLSLExpr4(inputColor).c_str());
105 }
106 
107 //////////////////////////////////////////////////////////////////////////////
108 
getGLProcessorKey(const GrGLSLCaps & caps,GrProcessorKeyBuilder * b) const109 void DitherEffect::getGLProcessorKey(const GrGLSLCaps& caps,
110                                      GrProcessorKeyBuilder* b) const {
111     GLDitherEffect::GenKey(*this, caps, b);
112 }
113 
createGLInstance() const114 GrGLFragmentProcessor* DitherEffect::createGLInstance() const  {
115     return SkNEW_ARGS(GLDitherEffect, (*this));
116 }
117 
Create()118 GrFragmentProcessor* GrDitherEffect::Create() { return DitherEffect::Create(); }
119