1 /* 2 * Copyright 2015 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 #ifndef GrColorProcessor_DEFINED 9 #define GrColorProcessor_DEFINED 10 11 #include "GrFragmentProcessor.h" 12 13 /** 14 * This is a simple GrFragmentProcessor that outputs a constant color. It may do one of the 15 * following with its input color: ignore it, or multiply it by the constant color, multiply its 16 * alpha by the constant color and ignore the input color's r, g, and b. 17 */ 18 class GrConstColorProcessor : public GrFragmentProcessor { 19 public: 20 enum InputMode { 21 kIgnore_InputMode, 22 kModulateRGBA_InputMode, 23 kModulateA_InputMode, 24 25 kLastInputMode = kModulateA_InputMode 26 }; 27 static const int kInputModeCnt = kLastInputMode + 1; 28 Make(GrColor4f color,InputMode mode)29 static sk_sp<GrFragmentProcessor> Make(GrColor4f color, InputMode mode) { 30 return sk_sp<GrFragmentProcessor>(new GrConstColorProcessor(color, mode)); 31 } 32 name()33 const char* name() const override { return "Color"; } 34 dumpInfo()35 SkString dumpInfo() const override { 36 SkString str; 37 str.appendf("Color: 0x%08x", fColor.toGrColor()); 38 return str; 39 } 40 color()41 GrColor4f color() const { return fColor; } 42 inputMode()43 InputMode inputMode() const { return fMode; } 44 45 private: OptFlags(GrColor4f color,InputMode mode)46 static OptimizationFlags OptFlags(GrColor4f color, InputMode mode) { 47 OptimizationFlags flags = kConstantOutputForConstantInput_OptimizationFlag; 48 if (mode != kIgnore_InputMode) { 49 flags |= kCompatibleWithCoverageAsAlpha_OptimizationFlag; 50 } 51 if (color.isOpaque()) { 52 flags |= kPreservesOpaqueInput_OptimizationFlag; 53 } 54 return flags; 55 } 56 GrConstColorProcessor(GrColor4f color,InputMode mode)57 GrConstColorProcessor(GrColor4f color, InputMode mode) 58 : INHERITED(OptFlags(color, mode)), fColor(color), fMode(mode) { 59 this->initClassID<GrConstColorProcessor>(); 60 } 61 62 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; 63 64 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; 65 66 bool onIsEqual(const GrFragmentProcessor&) const override; 67 68 GrColor4f constantOutputForConstantInput(GrColor4f input) const override; 69 70 GR_DECLARE_FRAGMENT_PROCESSOR_TEST 71 72 GrColor4f fColor; 73 InputMode fMode; 74 75 typedef GrFragmentProcessor INHERITED; 76 }; 77 78 #endif 79