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 13 /** \class SkColorShader 14 A Shader that represents a single color. In general, this effect can be 15 accomplished by just using the color field on the paint, but if an 16 actual shader object is needed, this provides that feature. 17 */ 18 class SK_API SkColorShader : public SkShader { 19 public: 20 /** Create a ColorShader that ignores the color in the paint, and uses the 21 specified color. Note: like all shaders, at draw time the paint's alpha 22 will be respected, and is applied to the specified color. 23 */ 24 explicit SkColorShader(SkColor c); 25 26 bool isOpaque() const override; 27 contextSize()28 size_t contextSize() const override { 29 return sizeof(ColorShaderContext); 30 } 31 32 class ColorShaderContext : public SkShader::Context { 33 public: 34 ColorShaderContext(const SkColorShader& shader, const ContextRec&); 35 36 uint32_t getFlags() const override; 37 uint8_t getSpan16Alpha() const override; 38 void shadeSpan(int x, int y, SkPMColor span[], int count) override; 39 void shadeSpan16(int x, int y, uint16_t span[], int count) override; 40 void shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) override; 41 42 private: 43 SkPMColor fPMColor; 44 uint32_t fFlags; 45 uint16_t fColor16; 46 47 typedef SkShader::Context INHERITED; 48 }; 49 50 // we return false for this, use asAGradient 51 virtual BitmapType asABitmap(SkBitmap* outTexture, 52 SkMatrix* outMatrix, 53 TileMode xy[2]) const override; 54 55 GradientType asAGradient(GradientInfo* info) const override; 56 57 virtual bool asFragmentProcessor(GrContext*, const SkPaint&, const SkMatrix& viewM, 58 const SkMatrix*, GrColor*, 59 GrFragmentProcessor**) const override; 60 61 SK_TO_STRING_OVERRIDE() 62 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkColorShader) 63 64 protected: 65 SkColorShader(SkReadBuffer&); 66 void flatten(SkWriteBuffer&) const override; 67 Context* onCreateContext(const ContextRec&, void* storage) const override; onAsLuminanceColor(SkColor * lum)68 bool onAsLuminanceColor(SkColor* lum) const override { 69 *lum = fColor; 70 return true; 71 } 72 73 private: 74 SkColor fColor; 75 76 typedef SkShader INHERITED; 77 }; 78 79 #endif 80