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