| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "gl/builders/GrGLProgramBuilder.h" | |
| 9 #include "GrDitherEffect.h" | 8 #include "GrDitherEffect.h" |
| 9 #include "GrFragmentProcessor.h" |
| 10 #include "GrInvariantOutput.h" | 10 #include "GrInvariantOutput.h" |
| 11 #include "SkRect.h" |
| 11 #include "gl/GrGLProcessor.h" | 12 #include "gl/GrGLProcessor.h" |
| 12 #include "gl/GrGLSL.h" | 13 #include "gl/GrGLSL.h" |
| 13 #include "GrTBackendProcessorFactory.h" | 14 #include "gl/builders/GrGLProgramBuilder.h" |
| 14 | |
| 15 #include "SkRect.h" | |
| 16 | 15 |
| 17 ////////////////////////////////////////////////////////////////////////////// | 16 ////////////////////////////////////////////////////////////////////////////// |
| 18 | 17 |
| 19 class GLDitherEffect; | |
| 20 | |
| 21 class DitherEffect : public GrFragmentProcessor { | 18 class DitherEffect : public GrFragmentProcessor { |
| 22 public: | 19 public: |
| 23 static GrFragmentProcessor* Create() { | 20 static GrFragmentProcessor* Create() { |
| 24 GR_CREATE_STATIC_PROCESSOR(gDitherEffect, DitherEffect, ()) | 21 GR_CREATE_STATIC_PROCESSOR(gDitherEffect, DitherEffect, ()) |
| 25 return SkRef(gDitherEffect); | 22 return SkRef(gDitherEffect); |
| 26 } | 23 } |
| 27 | 24 |
| 28 virtual ~DitherEffect() {}; | 25 virtual ~DitherEffect() {}; |
| 29 static const char* Name() { return "Dither"; } | |
| 30 | 26 |
| 31 typedef GLDitherEffect GLProcessor; | 27 virtual const char* name() const SK_OVERRIDE { return "Dither"; } |
| 32 | 28 |
| 33 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERR
IDE { | 29 virtual void getGLProcessorKey(const GrGLCaps&, GrProcessorKeyBuilder*) cons
t SK_OVERRIDE; |
| 34 return GrTBackendFragmentProcessorFactory<DitherEffect>::getInstance(); | 30 |
| 31 virtual GrGLFragmentProcessor* createGLInstance() const SK_OVERRIDE; |
| 32 |
| 33 virtual uint32_t classID() const { |
| 34 static uint32_t id = GenClassID(); |
| 35 return id; |
| 35 } | 36 } |
| 36 | 37 |
| 37 private: | 38 private: |
| 38 DitherEffect() { | 39 DitherEffect() { |
| 39 this->setWillReadFragmentPosition(); | 40 this->setWillReadFragmentPosition(); |
| 40 } | 41 } |
| 41 | 42 |
| 42 // All dither effects are equal | 43 // All dither effects are equal |
| 43 virtual bool onIsEqual(const GrFragmentProcessor&) const SK_OVERRIDE { retur
n true; } | 44 virtual bool onIsEqual(const GrFragmentProcessor&) const SK_OVERRIDE { retur
n true; } |
| 44 | 45 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 61 GrContext*, | 62 GrContext*, |
| 62 const GrDrawTargetCaps&, | 63 const GrDrawTargetCaps&, |
| 63 GrTexture*[]) { | 64 GrTexture*[]) { |
| 64 return DitherEffect::Create(); | 65 return DitherEffect::Create(); |
| 65 } | 66 } |
| 66 | 67 |
| 67 ////////////////////////////////////////////////////////////////////////////// | 68 ////////////////////////////////////////////////////////////////////////////// |
| 68 | 69 |
| 69 class GLDitherEffect : public GrGLFragmentProcessor { | 70 class GLDitherEffect : public GrGLFragmentProcessor { |
| 70 public: | 71 public: |
| 71 GLDitherEffect(const GrBackendProcessorFactory&, const GrProcessor&); | 72 GLDitherEffect(const GrProcessor&); |
| 72 | 73 |
| 73 virtual void emitCode(GrGLFPBuilder* builder, | 74 virtual void emitCode(GrGLFPBuilder* builder, |
| 74 const GrFragmentProcessor& fp, | 75 const GrFragmentProcessor& fp, |
| 75 const char* outputColor, | 76 const char* outputColor, |
| 76 const char* inputColor, | 77 const char* inputColor, |
| 77 const TransformedCoordsArray&, | 78 const TransformedCoordsArray&, |
| 78 const TextureSamplerArray&) SK_OVERRIDE; | 79 const TextureSamplerArray&) SK_OVERRIDE; |
| 79 | 80 |
| 80 private: | 81 private: |
| 81 typedef GrGLFragmentProcessor INHERITED; | 82 typedef GrGLFragmentProcessor INHERITED; |
| 82 }; | 83 }; |
| 83 | 84 |
| 84 GLDitherEffect::GLDitherEffect(const GrBackendProcessorFactory& factory, | 85 GLDitherEffect::GLDitherEffect(const GrProcessor&) { |
| 85 const GrProcessor&) | |
| 86 : INHERITED (factory) { | |
| 87 } | 86 } |
| 88 | 87 |
| 89 void GLDitherEffect::emitCode(GrGLFPBuilder* builder, | 88 void GLDitherEffect::emitCode(GrGLFPBuilder* builder, |
| 90 const GrFragmentProcessor& fp, | 89 const GrFragmentProcessor& fp, |
| 91 const char* outputColor, | 90 const char* outputColor, |
| 92 const char* inputColor, | 91 const char* inputColor, |
| 93 const TransformedCoordsArray&, | 92 const TransformedCoordsArray&, |
| 94 const TextureSamplerArray& samplers) { | 93 const TextureSamplerArray& samplers) { |
| 95 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); | 94 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 96 // Generate a random number based on the fragment position. For this | 95 // Generate a random number based on the fragment position. For this |
| 97 // random number generator, we use the "GLSL rand" function | 96 // random number generator, we use the "GLSL rand" function |
| 98 // that seems to be floating around on the internet. It works under | 97 // that seems to be floating around on the internet. It works under |
| 99 // the assumption that sin(<big number>) oscillates with high frequency | 98 // the assumption that sin(<big number>) oscillates with high frequency |
| 100 // and sampling it will generate "randomness". Since we're using this | 99 // and sampling it will generate "randomness". Since we're using this |
| 101 // for rendering and not cryptography it should be OK. | 100 // for rendering and not cryptography it should be OK. |
| 102 | 101 |
| 103 // For each channel c, add the random offset to the pixel to either bump | 102 // For each channel c, add the random offset to the pixel to either bump |
| 104 // it up or let it remain constant during quantization. | 103 // it up or let it remain constant during quantization. |
| 105 fsBuilder->codeAppendf("\t\tfloat r = " | 104 fsBuilder->codeAppendf("\t\tfloat r = " |
| 106 "fract(sin(dot(%s.xy ,vec2(12.9898,78.233))) * 43758.
5453);\n", | 105 "fract(sin(dot(%s.xy ,vec2(12.9898,78.233))) * 43758.
5453);\n", |
| 107 fsBuilder->fragmentPosition()); | 106 fsBuilder->fragmentPosition()); |
| 108 fsBuilder->codeAppendf("\t\t%s = (1.0/255.0) * vec4(r, r, r, r) + %s;\n", | 107 fsBuilder->codeAppendf("\t\t%s = (1.0/255.0) * vec4(r, r, r, r) + %s;\n", |
| 109 outputColor, GrGLSLExpr4(inputColor).c_str()); | 108 outputColor, GrGLSLExpr4(inputColor).c_str()); |
| 110 } | 109 } |
| 111 | 110 |
| 112 ////////////////////////////////////////////////////////////////////////////// | 111 ////////////////////////////////////////////////////////////////////////////// |
| 113 | 112 |
| 113 void DitherEffect::getGLProcessorKey(const GrGLCaps& caps, |
| 114 GrProcessorKeyBuilder* b) const { |
| 115 GLDitherEffect::GenKey(*this, caps, b); |
| 116 } |
| 117 |
| 118 GrGLFragmentProcessor* DitherEffect::createGLInstance() const { |
| 119 return SkNEW_ARGS(GLDitherEffect, (*this)); |
| 120 } |
| 121 |
| 114 GrFragmentProcessor* GrDitherEffect::Create() { return DitherEffect::Create(); } | 122 GrFragmentProcessor* GrDitherEffect::Create() { return DitherEffect::Create(); } |
| OLD | NEW |