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