Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2006 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 SkColorFilter_DEFINED
      9 #define SkColorFilter_DEFINED
     10 
     11 #include "SkColor.h"
     12 #include "SkFlattenable.h"
     13 #include "SkXfermode.h"
     14 
     15 class GrContext;
     16 class GrFragmentProcessor;
     17 class SkBitmap;
     18 
     19 /**
     20  *  ColorFilters are optional objects in the drawing pipeline. When present in
     21  *  a paint, they are called with the "src" colors, and return new colors, which
     22  *  are then passed onto the next stage (either ImageFilter or Xfermode).
     23  *
     24  *  All subclasses are required to be reentrant-safe : it must be legal to share
     25  *  the same instance between several threads.
     26  */
     27 class SK_API SkColorFilter : public SkFlattenable {
     28 public:
     29     /**
     30      *  If the filter can be represented by a source color plus Mode, this
     31      *  returns true, and sets (if not NULL) the color and mode appropriately.
     32      *  If not, this returns false and ignores the parameters.
     33      */
     34     virtual bool asColorMode(SkColor* color, SkXfermode::Mode* mode) const;
     35 
     36     /**
     37      *  If the filter can be represented by a 5x4 matrix, this
     38      *  returns true, and sets the matrix appropriately.
     39      *  If not, this returns false and ignores the parameter.
     40      */
     41     virtual bool asColorMatrix(SkScalar matrix[20]) const;
     42 
     43     /**
     44      *  If the filter can be represented by per-component table, return true,
     45      *  and if table is not null, copy the bitmap containing the table into it.
     46      *
     47      *  The table bitmap will be in SkBitmap::kA8_Config. Each row corresponding
     48      *  to each component in ARGB order. e.g. row[0] == alpha, row[1] == red,
     49      *  etc. To transform a color, you (logically) perform the following:
     50      *
     51      *      a' = *table.getAddr8(a, 0);
     52      *      r' = *table.getAddr8(r, 1);
     53      *      g' = *table.getAddr8(g, 2);
     54      *      b' = *table.getAddr8(b, 3);
     55      *
     56      *  The original component value is the horizontal index for a given row,
     57      *  and the stored value at that index is the new value for that component.
     58      */
     59     virtual bool asComponentTable(SkBitmap* table) const;
     60 
     61     /** Called with a scanline of colors, as if there was a shader installed.
     62         The implementation writes out its filtered version into result[].
     63         Note: shader and result may be the same buffer.
     64         @param src      array of colors, possibly generated by a shader
     65         @param count    the number of entries in the src[] and result[] arrays
     66         @param result   written by the filter
     67     */
     68     virtual void filterSpan(const SkPMColor src[], int count, SkPMColor result[]) const = 0;
     69 
     70     virtual void filterSpan4f(const SkPM4f src[], int count, SkPM4f result[]) const;
     71 
     72     enum Flags {
     73         /** If set the filter methods will not change the alpha channel of the colors.
     74         */
     75         kAlphaUnchanged_Flag = 1 << 0,
     76     };
     77 
     78     /** Returns the flags for this filter. Override in subclasses to return custom flags.
     79     */
     80     virtual uint32_t getFlags() const { return 0; }
     81 
     82     /**
     83      *  If this subclass can optimally createa composition with the inner filter, return it as
     84      *  a new filter (which the caller must unref() when it is done). If no such optimization
     85      *  is known, return NULL.
     86      *
     87      *  e.g. result(color) == this_filter(inner(color))
     88      */
     89     virtual SkColorFilter* newComposed(const SkColorFilter* /*inner*/) const { return NULL; }
     90 
     91     /**
     92      *  Apply this colorfilter to the specified SkColor. This routine handles
     93      *  converting to SkPMColor, calling the filter, and then converting back
     94      *  to SkColor. This method is not virtual, but will call filterSpan()
     95      *   which is virtual.
     96      */
     97     SkColor filterColor(SkColor) const;
     98 
     99     /**
    100      *  Filters a single color.
    101      */
    102     SkColor4f filterColor4f(const SkColor4f&) const;
    103 
    104     /** Create a colorfilter that uses the specified color and mode.
    105         If the Mode is DST, this function will return NULL (since that
    106         mode will have no effect on the result).
    107         @param c    The source color used with the specified mode
    108         @param mode The xfermode mode that is applied to each color in
    109                         the colorfilter's filterSpan[16,32] methods
    110         @return colorfilter object that applies the src color and mode,
    111                     or NULL if the mode will have no effect.
    112     */
    113     static SkColorFilter* CreateModeFilter(SkColor c, SkXfermode::Mode mode);
    114 
    115     /** Construct a colorfilter whose effect is to first apply the inner filter and then apply
    116      *  the outer filter to the result of the inner's.
    117      *  The reference counts for outer and inner are incremented.
    118      *
    119      *  Due to internal limits, it is possible that this will return NULL, so the caller must
    120      *  always check.
    121      */
    122     static SkColorFilter* CreateComposeFilter(SkColorFilter* outer, SkColorFilter* inner);
    123 
    124     /** Construct a color filter that transforms a color by a 4x5 matrix. The matrix is in row-
    125      *  major order and the translation column is specified in unnormalized, 0...255, space.
    126      */
    127     static SkColorFilter* CreateMatrixFilterRowMajor255(const SkScalar array[20]);
    128 
    129     /**
    130      *  A subclass may implement this factory function to work with the GPU backend. It returns
    131      *  a GrFragmentProcessor that implemets the color filter in GPU shader code.
    132      *
    133      *  The fragment processor receives a premultiplied input color and produces a premultiplied
    134      *  output color.
    135      *
    136      *  A null return indicates that the color filter isn't implemented for the GPU backend.
    137      */
    138     virtual const GrFragmentProcessor* asFragmentProcessor(GrContext*) const {
    139         return nullptr;
    140     }
    141 
    142     bool affectsTransparentBlack() const {
    143         return this->filterColor(0) != 0;
    144     }
    145 
    146     SK_TO_STRING_PUREVIRT()
    147 
    148     SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
    149     SK_DEFINE_FLATTENABLE_TYPE(SkColorFilter)
    150 
    151 protected:
    152     SkColorFilter() {}
    153 
    154 private:
    155     /*
    156      *  Returns 1 if this is a single filter (not a composition of other filters), otherwise it
    157      *  reutrns the number of leaf-node filters in a composition. This should be the same value
    158      *  as the number of GrFragmentProcessors returned by asFragmentProcessors's array parameter.
    159      *
    160      *  e.g. compose(filter, compose(compose(filter, filter), filter)) --> 4
    161      */
    162     virtual int privateComposedFilterCount() const { return 1; }
    163     friend class SkComposeColorFilter;
    164 
    165     typedef SkFlattenable INHERITED;
    166 };
    167 
    168 #endif
    169