1 /* 2 * Copyright 2007 The Android Open Source Project 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 SkColorShader_DEFINED 9 #define SkColorShader_DEFINED 10 11 #include "SkColorSpaceXformer.h" 12 #include "SkShaderBase.h" 13 #include "SkPM4f.h" 14 15 /** \class SkColorShader 16 A Shader that represents a single color. In general, this effect can be 17 accomplished by just using the color field on the paint, but if an 18 actual shader object is needed, this provides that feature. 19 */ 20 class SkColorShader : public SkShaderBase { 21 public: 22 /** Create a ColorShader that ignores the color in the paint, and uses the 23 specified color. Note: like all shaders, at draw time the paint's alpha 24 will be respected, and is applied to the specified color. 25 */ 26 explicit SkColorShader(SkColor c); 27 28 bool isOpaque() const override; 29 bool isConstant() const override { return true; } 30 31 class ColorShaderContext : public Context { 32 public: 33 ColorShaderContext(const SkColorShader& shader, const ContextRec&); 34 35 uint32_t getFlags() const override; 36 void shadeSpan(int x, int y, SkPMColor span[], int count) override; 37 void shadeSpan4f(int x, int y, SkPM4f[], int count) override; 38 39 private: 40 SkPM4f fPM4f; 41 SkPMColor fPMColor; 42 uint32_t fFlags; 43 44 typedef Context INHERITED; 45 }; 46 47 GradientType asAGradient(GradientInfo* info) const override; 48 49 #if SK_SUPPORT_GPU 50 std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const override; 51 #endif 52 53 SK_TO_STRING_OVERRIDE() 54 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkColorShader) 55 56 protected: 57 SkColorShader(SkReadBuffer&); 58 void flatten(SkWriteBuffer&) const override; 59 Context* onMakeContext(const ContextRec&, SkArenaAlloc* storage) const override; 60 61 bool onAsLuminanceColor(SkColor* lum) const override { 62 *lum = fColor; 63 return true; 64 } 65 66 bool onAppendStages(const StageRec&) const override; 67 68 sk_sp<SkShader> onMakeColorSpace(SkColorSpaceXformer* xformer) const override { 69 return SkShader::MakeColorShader(xformer->apply(fColor)); 70 } 71 72 private: 73 SkColor fColor; 74 75 typedef SkShaderBase INHERITED; 76 }; 77 78 class SkColor4Shader : public SkShaderBase { 79 public: 80 SkColor4Shader(const SkColor4f&, sk_sp<SkColorSpace>); 81 82 bool isOpaque() const override { 83 return SkColorGetA(fCachedByteColor) == 255; 84 } 85 bool isConstant() const override { return true; } 86 87 class Color4Context : public Context { 88 public: 89 Color4Context(const SkColor4Shader& shader, const ContextRec&); 90 91 uint32_t getFlags() const override; 92 void shadeSpan(int x, int y, SkPMColor span[], int count) override; 93 void shadeSpan4f(int x, int y, SkPM4f[], int count) override; 94 95 private: 96 SkPM4f fPM4f; 97 SkPMColor fPMColor; 98 uint32_t fFlags; 99 100 typedef Context INHERITED; 101 }; 102 103 GradientType asAGradient(GradientInfo* info) const override; 104 105 #if SK_SUPPORT_GPU 106 std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const override; 107 #endif 108 109 SK_TO_STRING_OVERRIDE() 110 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkColorShader) 111 112 protected: 113 SkColor4Shader(SkReadBuffer&); 114 void flatten(SkWriteBuffer&) const override; 115 Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const override; 116 bool onAsLuminanceColor(SkColor* lum) const override { 117 *lum = fCachedByteColor; 118 return true; 119 } 120 bool onAppendStages(const StageRec&) const override; 121 122 sk_sp<SkShader> onMakeColorSpace(SkColorSpaceXformer* xformer) const override; 123 124 private: 125 sk_sp<SkColorSpace> fColorSpace; 126 const SkColor4f fColor4; 127 const SkColor fCachedByteColor; 128 129 typedef SkShaderBase INHERITED; 130 }; 131 132 #endif 133