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 "glsl/GrGLSLFragmentProcessor.h"
13#include "glsl/GrGLSLFragmentShaderBuilder.h"
14
15//////////////////////////////////////////////////////////////////////////////
16
17class DitherEffect : public GrFragmentProcessor {
18public:
19    static GrFragmentProcessor* Create() {
20        return new DitherEffect;
21    }
22
23    virtual ~DitherEffect() {};
24
25    const char* name() const override { return "Dither"; }
26
27private:
28    DitherEffect() {
29        this->initClassID<DitherEffect>();
30        this->setWillReadFragmentPosition();
31    }
32
33    GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
34
35    void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
36
37    // All dither effects are equal
38    bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
39
40    void onComputeInvariantOutput(GrInvariantOutput* inout) const override;
41
42    GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
43
44    typedef GrFragmentProcessor INHERITED;
45};
46
47void DitherEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
48    inout->setToUnknown(GrInvariantOutput::kWill_ReadInput);
49}
50
51//////////////////////////////////////////////////////////////////////////////
52
53GR_DEFINE_FRAGMENT_PROCESSOR_TEST(DitherEffect);
54
55const GrFragmentProcessor* DitherEffect::TestCreate(GrProcessorTestData*) {
56    return DitherEffect::Create();
57}
58
59//////////////////////////////////////////////////////////////////////////////
60
61class GLDitherEffect : public GrGLSLFragmentProcessor {
62public:
63    void emitCode(EmitArgs& args) override;
64
65private:
66    typedef GrGLSLFragmentProcessor INHERITED;
67};
68
69void GLDitherEffect::emitCode(EmitArgs& args) {
70    GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
71    // Generate a random number based on the fragment position. For this
72    // random number generator, we use the "GLSL rand" function
73    // that seems to be floating around on the internet. It works under
74    // the assumption that sin(<big number>) oscillates with high frequency
75    // and sampling it will generate "randomness". Since we're using this
76    // for rendering and not cryptography it should be OK.
77
78    // For each channel c, add the random offset to the pixel to either bump
79    // it up or let it remain constant during quantization.
80    fragBuilder->codeAppendf("\t\tfloat r = "
81                             "fract(sin(dot(%s.xy ,vec2(12.9898,78.233))) * 43758.5453);\n",
82                             fragBuilder->fragmentPosition());
83    fragBuilder->codeAppendf("\t\t%s = (1.0/255.0) * vec4(r, r, r, r) + %s;\n",
84                             args.fOutputColor, GrGLSLExpr4(args.fInputColor).c_str());
85}
86
87//////////////////////////////////////////////////////////////////////////////
88
89void DitherEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps,
90                                         GrProcessorKeyBuilder* b) const {
91    GLDitherEffect::GenKey(*this, caps, b);
92}
93
94GrGLSLFragmentProcessor* DitherEffect::onCreateGLSLInstance() const  {
95    return new GLDitherEffect;
96}
97
98GrFragmentProcessor* GrDitherEffect::Create() { return DitherEffect::Create(); }
99