Home | History | Annotate | Download | only in shaders
      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 shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) override;
     38         void shadeSpan4f(int x, int y, SkPM4f[], int count) override;
     39 
     40     private:
     41         SkPM4f      fPM4f;
     42         SkPMColor   fPMColor;
     43         uint32_t    fFlags;
     44 
     45         typedef Context INHERITED;
     46     };
     47 
     48     GradientType asAGradient(GradientInfo* info) const override;
     49 
     50 #if SK_SUPPORT_GPU
     51     sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const override;
     52 #endif
     53 
     54     SK_TO_STRING_OVERRIDE()
     55     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkColorShader)
     56 
     57 protected:
     58     SkColorShader(SkReadBuffer&);
     59     void flatten(SkWriteBuffer&) const override;
     60     Context* onMakeContext(const ContextRec&, SkArenaAlloc* storage) const override;
     61 
     62     bool onAsLuminanceColor(SkColor* lum) const override {
     63         *lum = fColor;
     64         return true;
     65     }
     66 
     67     bool onAppendStages(SkRasterPipeline*, SkColorSpace*, SkArenaAlloc*,
     68                         const SkMatrix& ctm, const SkPaint&, const SkMatrix*) const override;
     69 
     70     sk_sp<SkShader> onMakeColorSpace(SkColorSpaceXformer* xformer) const override {
     71         return SkShader::MakeColorShader(xformer->apply(fColor));
     72     }
     73 
     74 private:
     75     SkColor fColor;
     76 
     77     typedef SkShaderBase INHERITED;
     78 };
     79 
     80 class SkColor4Shader : public SkShaderBase {
     81 public:
     82     SkColor4Shader(const SkColor4f&, sk_sp<SkColorSpace>);
     83 
     84     bool isOpaque() const override {
     85         return SkColorGetA(fCachedByteColor) == 255;
     86     }
     87     bool isConstant() const override { return true; }
     88 
     89     class Color4Context : public Context {
     90     public:
     91         Color4Context(const SkColor4Shader& shader, const ContextRec&);
     92 
     93         uint32_t getFlags() const override;
     94         void shadeSpan(int x, int y, SkPMColor span[], int count) override;
     95         void shadeSpanAlpha(int x, int y, uint8_t alpha[], int count) override;
     96         void shadeSpan4f(int x, int y, SkPM4f[], int count) override;
     97 
     98     private:
     99         SkPM4f      fPM4f;
    100         SkPMColor   fPMColor;
    101         uint32_t    fFlags;
    102 
    103         typedef Context INHERITED;
    104     };
    105 
    106     GradientType asAGradient(GradientInfo* info) const override;
    107 
    108 #if SK_SUPPORT_GPU
    109     sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const override;
    110 #endif
    111 
    112     SK_TO_STRING_OVERRIDE()
    113     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkColorShader)
    114 
    115 protected:
    116     SkColor4Shader(SkReadBuffer&);
    117     void flatten(SkWriteBuffer&) const override;
    118     Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const override;
    119     bool onAsLuminanceColor(SkColor* lum) const override {
    120         *lum = fCachedByteColor;
    121         return true;
    122     }
    123     bool onAppendStages(SkRasterPipeline*, SkColorSpace*, SkArenaAlloc*,
    124                         const SkMatrix& ctm, const SkPaint&, const SkMatrix*) const override;
    125 
    126     sk_sp<SkShader> onMakeColorSpace(SkColorSpaceXformer* xformer) const override;
    127 
    128 private:
    129     sk_sp<SkColorSpace> fColorSpace;
    130     const SkColor4f     fColor4;
    131     const SkColor       fCachedByteColor;
    132 
    133     typedef SkShaderBase INHERITED;
    134 };
    135 
    136 #endif
    137