GrDitherEffect.cpp revision 267ce482b54f46097584e0f9350ec74aa6a2cd44
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 "gl/builders/GrGLProgramBuilder.h"
9#include "GrDitherEffect.h"
10#include "GrInvariantOutput.h"
11#include "gl/GrGLProcessor.h"
12#include "gl/GrGLSL.h"
13#include "GrTBackendProcessorFactory.h"
14
15#include "SkRect.h"
16
17//////////////////////////////////////////////////////////////////////////////
18
19class GLDitherEffect;
20
21class DitherEffect : public GrFragmentProcessor {
22public:
23    static GrFragmentProcessor* Create() {
24        GR_CREATE_STATIC_PROCESSOR(gDitherEffect, DitherEffect, ())
25        return SkRef(gDitherEffect);
26    }
27
28    virtual ~DitherEffect() {};
29    static const char* Name() { return "Dither"; }
30
31    typedef GLDitherEffect GLProcessor;
32
33    virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERRIDE {
34        return GrTBackendFragmentProcessorFactory<DitherEffect>::getInstance();
35    }
36
37private:
38    DitherEffect() {
39        this->setWillReadFragmentPosition();
40    }
41
42    // All dither effects are equal
43    virtual bool onIsEqual(const GrFragmentProcessor&) const SK_OVERRIDE { return true; }
44
45    virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE;
46
47    GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
48
49    typedef GrFragmentProcessor INHERITED;
50};
51
52void DitherEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
53    inout->setToUnknown(GrInvariantOutput::kWill_ReadInput);
54}
55
56//////////////////////////////////////////////////////////////////////////////
57
58GR_DEFINE_FRAGMENT_PROCESSOR_TEST(DitherEffect);
59
60GrFragmentProcessor* DitherEffect::TestCreate(SkRandom*,
61                                              GrContext*,
62                                              const GrDrawTargetCaps&,
63                                              GrTexture*[]) {
64    return DitherEffect::Create();
65}
66
67//////////////////////////////////////////////////////////////////////////////
68
69class GLDitherEffect : public GrGLFragmentProcessor {
70public:
71    GLDitherEffect(const GrBackendProcessorFactory&, const GrProcessor&);
72
73    virtual void emitCode(GrGLFPBuilder* builder,
74                          const GrFragmentProcessor& fp,
75                          const char* outputColor,
76                          const char* inputColor,
77                          const TransformedCoordsArray&,
78                          const TextureSamplerArray&) SK_OVERRIDE;
79
80private:
81    typedef GrGLFragmentProcessor INHERITED;
82};
83
84GLDitherEffect::GLDitherEffect(const GrBackendProcessorFactory& factory,
85                               const GrProcessor&)
86    : INHERITED (factory) {
87}
88
89void GLDitherEffect::emitCode(GrGLFPBuilder* builder,
90                              const GrFragmentProcessor& fp,
91                              const char* outputColor,
92                              const char* inputColor,
93                              const TransformedCoordsArray&,
94                              const TextureSamplerArray& samplers) {
95    GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
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    fsBuilder->codeAppendf("\t\tfloat r = "
106                           "fract(sin(dot(%s.xy ,vec2(12.9898,78.233))) * 43758.5453);\n",
107                           fsBuilder->fragmentPosition());
108    fsBuilder->codeAppendf("\t\t%s = (1.0/255.0) * vec4(r, r, r, r) + %s;\n",
109                           outputColor, GrGLSLExpr4(inputColor).c_str());
110}
111
112//////////////////////////////////////////////////////////////////////////////
113
114GrFragmentProcessor* GrDitherEffect::Create() { return DitherEffect::Create(); }
115