Home | History | Annotate | Download | only in core
      1 
      2 /*
      3  * Copyright 2006 The Android Open Source Project
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 
      9 
     10 #ifndef SkColorFilter_DEFINED
     11 #define SkColorFilter_DEFINED
     12 
     13 #include "SkColor.h"
     14 #include "SkFlattenable.h"
     15 #include "SkXfermode.h"
     16 
     17 class SkBitmap;
     18 class GrEffectRef;
     19 class GrContext;
     20 
     21 class SK_API SkColorFilter : public SkFlattenable {
     22 public:
     23     SK_DECLARE_INST_COUNT(SkColorFilter)
     24 
     25     /**
     26      *  If the filter can be represented by a source color plus Mode, this
     27      *  returns true, and sets (if not NULL) the color and mode appropriately.
     28      *  If not, this returns false and ignores the parameters.
     29      */
     30     virtual bool asColorMode(SkColor* color, SkXfermode::Mode* mode) const;
     31 
     32     /**
     33      *  If the filter can be represented by a 5x4 matrix, this
     34      *  returns true, and sets the matrix appropriately.
     35      *  If not, this returns false and ignores the parameter.
     36      */
     37     virtual bool asColorMatrix(SkScalar matrix[20]) const;
     38 
     39     /**
     40      *  If the filter can be represented by per-component table, return true,
     41      *  and if table is not null, copy the bitmap containing the table into it.
     42      *
     43      *  The table bitmap will be in SkBitmap::kA8_Config. Each row corresponding
     44      *  to each component in ARGB order. e.g. row[0] == alpha, row[1] == red,
     45      *  etc. To transform a color, you (logically) perform the following:
     46      *
     47      *      a' = *table.getAddr8(a, 0);
     48      *      r' = *table.getAddr8(r, 1);
     49      *      g' = *table.getAddr8(g, 2);
     50      *      b' = *table.getAddr8(b, 3);
     51      *
     52      *  The original component value is the horizontal index for a given row,
     53      *  and the stored value at that index is the new value for that component.
     54      */
     55     virtual bool asComponentTable(SkBitmap* table) const;
     56 
     57     /** Called with a scanline of colors, as if there was a shader installed.
     58         The implementation writes out its filtered version into result[].
     59         Note: shader and result may be the same buffer.
     60         @param src      array of colors, possibly generated by a shader
     61         @param count    the number of entries in the src[] and result[] arrays
     62         @param result   written by the filter
     63     */
     64     virtual void filterSpan(const SkPMColor src[], int count,
     65                             SkPMColor result[]) const = 0;
     66     /** Called with a scanline of colors, as if there was a shader installed.
     67         The implementation writes out its filtered version into result[].
     68         Note: shader and result may be the same buffer.
     69         @param src      array of colors, possibly generated by a shader
     70         @param count    the number of entries in the src[] and result[] arrays
     71         @param result   written by the filter
     72     */
     73     virtual void filterSpan16(const uint16_t shader[], int count,
     74                               uint16_t result[]) const;
     75 
     76     enum Flags {
     77         /** If set the filter methods will not change the alpha channel of the
     78             colors.
     79         */
     80         kAlphaUnchanged_Flag = 0x01,
     81         /** If set, this subclass implements filterSpan16(). If this flag is
     82             set, then kAlphaUnchanged_Flag must also be set.
     83         */
     84         kHasFilter16_Flag    = 0x02
     85     };
     86 
     87     /** Returns the flags for this filter. Override in subclasses to return
     88         custom flags.
     89     */
     90     virtual uint32_t getFlags() const { return 0; }
     91 
     92     /**
     93      *  Apply this colorfilter to the specified SkColor. This routine handles
     94      *  converting to SkPMColor, calling the filter, and then converting back
     95      *  to SkColor. This method is not virtual, but will call filterSpan()
     96      *   which is virtual.
     97      */
     98     SkColor filterColor(SkColor) const;
     99 
    100     /** Create a colorfilter that uses the specified color and mode.
    101         If the Mode is DST, this function will return NULL (since that
    102         mode will have no effect on the result).
    103         @param c    The source color used with the specified mode
    104         @param mode The xfermode mode that is applied to each color in
    105                         the colorfilter's filterSpan[16,32] methods
    106         @return colorfilter object that applies the src color and mode,
    107                     or NULL if the mode will have no effect.
    108     */
    109     static SkColorFilter* CreateModeFilter(SkColor c, SkXfermode::Mode mode);
    110 
    111     /** Create a colorfilter that multiplies the RGB channels by one color, and
    112         then adds a second color, pinning the result for each component to
    113         [0..255]. The alpha components of the mul and add arguments
    114         are ignored.
    115     */
    116     static SkColorFilter* CreateLightingFilter(SkColor mul, SkColor add);
    117 
    118     /** A subclass may implement this factory function to work with the GPU backend. If the return
    119         is non-NULL then the caller owns a ref on the returned object.
    120      */
    121     virtual GrEffectRef* asNewEffect(GrContext*) const;
    122 
    123     SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
    124 protected:
    125     SkColorFilter() {}
    126     SkColorFilter(SkFlattenableReadBuffer& rb) : INHERITED(rb) {}
    127 
    128 private:
    129     typedef SkFlattenable INHERITED;
    130 };
    131 
    132 #endif
    133