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