2011-07-28 14:26:00 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2006 The Android Open Source Project
|
2008-12-17 15:59:43 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#ifndef SkXfermode_DEFINED
|
|
|
|
#define SkXfermode_DEFINED
|
|
|
|
|
|
|
|
#include "SkFlattenable.h"
|
|
|
|
#include "SkColor.h"
|
|
|
|
|
2013-01-22 14:32:09 +00:00
|
|
|
class SkString;
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** \class SkXfermode
|
|
|
|
|
|
|
|
SkXfermode is the base class for objects that are called to implement custom
|
|
|
|
"transfer-modes" in the drawing pipeline. The static function Create(Modes)
|
|
|
|
can be called to return an instance of any of the predefined subclasses as
|
|
|
|
specified in the Modes enum. When an SkXfermode is assigned to an SkPaint,
|
|
|
|
then objects drawn with that paint have the xfermode applied.
|
|
|
|
*/
|
2011-03-15 21:27:08 +00:00
|
|
|
class SK_API SkXfermode : public SkFlattenable {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2012-06-27 14:03:26 +00:00
|
|
|
SK_DECLARE_INST_COUNT(SkXfermode)
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkXfermode() {}
|
|
|
|
|
|
|
|
virtual void xfer32(SkPMColor dst[], const SkPMColor src[], int count,
|
2012-12-17 19:55:24 +00:00
|
|
|
const SkAlpha aa[]) const;
|
2008-12-17 15:59:43 +00:00
|
|
|
virtual void xfer16(uint16_t dst[], const SkPMColor src[], int count,
|
2012-12-17 19:55:24 +00:00
|
|
|
const SkAlpha aa[]) const;
|
2008-12-17 15:59:43 +00:00
|
|
|
virtual void xfer4444(uint16_t dst[], const SkPMColor src[], int count,
|
2012-12-17 19:55:24 +00:00
|
|
|
const SkAlpha aa[]) const;
|
2008-12-17 15:59:43 +00:00
|
|
|
virtual void xferA8(SkAlpha dst[], const SkPMColor src[], int count,
|
2012-12-17 19:55:24 +00:00
|
|
|
const SkAlpha aa[]) const;
|
2011-04-27 14:09:52 +00:00
|
|
|
|
2009-04-01 18:31:44 +00:00
|
|
|
/** Enum of possible coefficients to describe some xfermodes
|
|
|
|
*/
|
2008-12-17 15:59:43 +00:00
|
|
|
enum Coeff {
|
2009-04-01 18:31:44 +00:00
|
|
|
kZero_Coeff, /** 0 */
|
|
|
|
kOne_Coeff, /** 1 */
|
|
|
|
kSC_Coeff, /** src color */
|
|
|
|
kISC_Coeff, /** inverse src color (i.e. 1 - sc) */
|
|
|
|
kDC_Coeff, /** dst color */
|
|
|
|
kIDC_Coeff, /** inverse dst color (i.e. 1 - dc) */
|
|
|
|
kSA_Coeff, /** src alpha */
|
|
|
|
kISA_Coeff, /** inverse src alpha (i.e. 1 - sa) */
|
|
|
|
kDA_Coeff, /** dst alpha */
|
|
|
|
kIDA_Coeff, /** inverse dst alpha (i.e. 1 - da) */
|
2011-04-27 14:09:52 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
kCoeffCount
|
|
|
|
};
|
2011-04-27 14:09:52 +00:00
|
|
|
|
2009-04-01 18:31:44 +00:00
|
|
|
/** If the xfermode can be expressed as an equation using the coefficients
|
|
|
|
in Coeff, then asCoeff() returns true, and sets (if not null) src and
|
|
|
|
dst accordingly.
|
2011-04-27 14:09:52 +00:00
|
|
|
|
2009-04-01 18:31:44 +00:00
|
|
|
result = src_coeff * src_color + dst_coeff * dst_color;
|
2011-04-27 14:09:52 +00:00
|
|
|
|
2009-04-01 18:31:44 +00:00
|
|
|
As examples, here are some of the porterduff coefficients
|
2011-04-27 14:09:52 +00:00
|
|
|
|
2009-04-01 18:31:44 +00:00
|
|
|
MODE SRC_COEFF DST_COEFF
|
|
|
|
clear zero zero
|
|
|
|
src one zero
|
|
|
|
dst zero one
|
|
|
|
srcover one isa
|
|
|
|
dstover ida one
|
|
|
|
*/
|
2012-12-17 19:55:24 +00:00
|
|
|
virtual bool asCoeff(Coeff* src, Coeff* dst) const;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-04-14 15:50:52 +00:00
|
|
|
/**
|
|
|
|
* The same as calling xfermode->asCoeff(..), except that this also checks
|
|
|
|
* if the xfermode is NULL, and if so, treats its as kSrcOver_Mode.
|
|
|
|
*/
|
2012-12-17 19:55:24 +00:00
|
|
|
static bool AsCoeff(const SkXfermode*, Coeff* src, Coeff* dst);
|
2011-04-27 14:09:52 +00:00
|
|
|
|
2009-06-22 17:38:10 +00:00
|
|
|
/** List of predefined xfermodes.
|
|
|
|
The algebra for the modes uses the following symbols:
|
|
|
|
Sa, Sc - source alpha and color
|
|
|
|
Da, Dc - destination alpha and color (before compositing)
|
|
|
|
[a, c] - Resulting (alpha, color) values
|
|
|
|
For these equations, the colors are in premultiplied state.
|
|
|
|
If no xfermode is specified, kSrcOver is assumed.
|
|
|
|
*/
|
|
|
|
enum Mode {
|
|
|
|
kClear_Mode, //!< [0, 0]
|
|
|
|
kSrc_Mode, //!< [Sa, Sc]
|
|
|
|
kDst_Mode, //!< [Da, Dc]
|
|
|
|
kSrcOver_Mode, //!< [Sa + Da - Sa*Da, Rc = Sc + (1 - Sa)*Dc]
|
|
|
|
kDstOver_Mode, //!< [Sa + Da - Sa*Da, Rc = Dc + (1 - Da)*Sc]
|
|
|
|
kSrcIn_Mode, //!< [Sa * Da, Sc * Da]
|
|
|
|
kDstIn_Mode, //!< [Sa * Da, Sa * Dc]
|
|
|
|
kSrcOut_Mode, //!< [Sa * (1 - Da), Sc * (1 - Da)]
|
|
|
|
kDstOut_Mode, //!< [Da * (1 - Sa), Dc * (1 - Sa)]
|
|
|
|
kSrcATop_Mode, //!< [Da, Sc * Da + (1 - Sa) * Dc]
|
|
|
|
kDstATop_Mode, //!< [Sa, Sa * Dc + Sc * (1 - Da)]
|
|
|
|
kXor_Mode, //!< [Sa + Da - 2 * Sa * Da, Sc * (1 - Da) + (1 - Sa) * Dc]
|
|
|
|
|
2011-05-16 20:56:06 +00:00
|
|
|
// all remaining modes are defined in the SVG Compositing standard
|
2009-06-22 17:38:10 +00:00
|
|
|
// http://www.w3.org/TR/2009/WD-SVGCompositing-20090430/
|
|
|
|
kPlus_Mode,
|
2013-01-30 21:36:11 +00:00
|
|
|
kModulate_Mode, // multiplies all components (= alpha and color)
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-05-16 20:56:06 +00:00
|
|
|
// all above modes can be expressed as pair of src/dst Coeffs
|
2012-08-23 18:09:54 +00:00
|
|
|
kCoeffModesCnt,
|
|
|
|
|
2011-05-16 20:56:06 +00:00
|
|
|
kScreen_Mode = kCoeffModesCnt,
|
2009-06-22 17:38:10 +00:00
|
|
|
kOverlay_Mode,
|
|
|
|
kDarken_Mode,
|
|
|
|
kLighten_Mode,
|
|
|
|
kColorDodge_Mode,
|
|
|
|
kColorBurn_Mode,
|
|
|
|
kHardLight_Mode,
|
|
|
|
kSoftLight_Mode,
|
|
|
|
kDifference_Mode,
|
|
|
|
kExclusion_Mode,
|
2013-02-04 20:06:00 +00:00
|
|
|
kMultiply_Mode,
|
2009-06-22 17:38:10 +00:00
|
|
|
|
2013-02-04 20:06:00 +00:00
|
|
|
kLastMode = kMultiply_Mode
|
2009-06-22 17:38:10 +00:00
|
|
|
};
|
|
|
|
|
2011-04-13 21:12:04 +00:00
|
|
|
/**
|
|
|
|
* If the xfermode is one of the modes in the Mode enum, then asMode()
|
|
|
|
* returns true and sets (if not null) mode accordingly. Otherwise it
|
|
|
|
* returns false and ignores the mode parameter.
|
2011-02-08 19:28:07 +00:00
|
|
|
*/
|
2012-12-17 19:55:24 +00:00
|
|
|
virtual bool asMode(Mode* mode) const;
|
2011-02-08 19:28:07 +00:00
|
|
|
|
2011-04-14 15:50:52 +00:00
|
|
|
/**
|
|
|
|
* The same as calling xfermode->asMode(mode), except that this also checks
|
|
|
|
* if the xfermode is NULL, and if so, treats its as kSrcOver_Mode.
|
|
|
|
*/
|
2012-12-17 19:55:24 +00:00
|
|
|
static bool AsMode(const SkXfermode*, Mode* mode);
|
2011-04-14 15:50:52 +00:00
|
|
|
|
2011-11-17 02:16:43 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the xfermode claims to be the specified Mode. This works
|
|
|
|
* correctly even if the xfermode is NULL (which equates to kSrcOver.) Thus
|
|
|
|
* you can say this without checking for a null...
|
|
|
|
*
|
|
|
|
* If (SkXfermode::IsMode(paint.getXfermode(),
|
|
|
|
* SkXfermode::kDstOver_Mode)) {
|
|
|
|
* ...
|
|
|
|
* }
|
|
|
|
*/
|
2012-12-17 19:55:24 +00:00
|
|
|
static bool IsMode(const SkXfermode* xfer, Mode mode);
|
2011-11-17 02:16:43 +00:00
|
|
|
|
2009-06-22 17:38:10 +00:00
|
|
|
/** Return an SkXfermode object for the specified mode.
|
|
|
|
*/
|
|
|
|
static SkXfermode* Create(Mode mode);
|
|
|
|
|
|
|
|
/** Return a function pointer to a routine that applies the specified
|
|
|
|
porter-duff transfer mode.
|
|
|
|
*/
|
|
|
|
static SkXfermodeProc GetProc(Mode mode);
|
|
|
|
|
|
|
|
/** Return a function pointer to a routine that applies the specified
|
|
|
|
porter-duff transfer mode and srcColor to a 16bit device color. Note,
|
|
|
|
if the mode+srcColor might return a non-opaque color, then there is not
|
|
|
|
16bit proc, and this will return NULL.
|
|
|
|
*/
|
|
|
|
static SkXfermodeProc16 GetProc16(Mode mode, SkColor srcColor);
|
2011-04-27 14:09:52 +00:00
|
|
|
|
2011-04-13 21:12:04 +00:00
|
|
|
/**
|
2011-04-14 15:50:52 +00:00
|
|
|
* If the specified mode can be represented by a pair of Coeff, then return
|
|
|
|
* true and set (if not NULL) the corresponding coeffs. If the mode is
|
|
|
|
* not representable as a pair of Coeffs, return false and ignore the
|
|
|
|
* src and dst parameters.
|
2009-06-22 17:38:10 +00:00
|
|
|
*/
|
2011-04-14 15:50:52 +00:00
|
|
|
static bool ModeAsCoeff(Mode mode, Coeff* src, Coeff* dst);
|
2009-06-22 17:38:10 +00:00
|
|
|
|
2011-04-14 15:50:52 +00:00
|
|
|
// DEPRECATED: call AsMode(...)
|
2012-12-17 19:55:24 +00:00
|
|
|
static bool IsMode(const SkXfermode* xfer, Mode* mode) {
|
2011-04-14 15:50:52 +00:00
|
|
|
return AsMode(xfer, mode);
|
|
|
|
}
|
2011-04-27 14:09:52 +00:00
|
|
|
|
2013-01-22 14:32:09 +00:00
|
|
|
SkDEVCODE(virtual void toString(SkString* str) const = 0;)
|
2012-03-23 19:00:34 +00:00
|
|
|
SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
|
2008-12-17 15:59:43 +00:00
|
|
|
protected:
|
|
|
|
SkXfermode(SkFlattenableReadBuffer& rb) : SkFlattenable(rb) {}
|
2011-04-27 14:09:52 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** The default implementation of xfer32/xfer16/xferA8 in turn call this
|
|
|
|
method, 1 color at a time (upscaled to a SkPMColor). The default
|
|
|
|
implmentation of this method just returns dst. If performance is
|
|
|
|
important, your subclass should override xfer32/xfer16/xferA8 directly.
|
2011-04-27 14:09:52 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
This method will not be called directly by the client, so it need not
|
|
|
|
be implemented if your subclass has overridden xfer32/xfer16/xferA8
|
|
|
|
*/
|
2012-12-17 19:55:24 +00:00
|
|
|
virtual SkPMColor xferColor(SkPMColor src, SkPMColor dst) const;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
private:
|
2009-06-22 17:38:10 +00:00
|
|
|
enum {
|
|
|
|
kModeCount = kLastMode + 1
|
|
|
|
};
|
2008-12-17 15:59:43 +00:00
|
|
|
typedef SkFlattenable INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/** \class SkProcXfermode
|
|
|
|
|
|
|
|
SkProcXfermode is a xfermode that applies the specified proc to its colors.
|
|
|
|
This class is not exported to java.
|
|
|
|
*/
|
|
|
|
class SkProcXfermode : public SkXfermode {
|
|
|
|
public:
|
|
|
|
SkProcXfermode(SkXfermodeProc proc) : fProc(proc) {}
|
|
|
|
|
|
|
|
// overrides from SkXfermode
|
|
|
|
virtual void xfer32(SkPMColor dst[], const SkPMColor src[], int count,
|
2012-12-17 19:55:24 +00:00
|
|
|
const SkAlpha aa[]) const SK_OVERRIDE;
|
2008-12-17 15:59:43 +00:00
|
|
|
virtual void xfer16(uint16_t dst[], const SkPMColor src[], int count,
|
2012-12-17 19:55:24 +00:00
|
|
|
const SkAlpha aa[]) const SK_OVERRIDE;
|
2008-12-17 15:59:43 +00:00
|
|
|
virtual void xfer4444(uint16_t dst[], const SkPMColor src[], int count,
|
2012-12-17 19:55:24 +00:00
|
|
|
const SkAlpha aa[]) const SK_OVERRIDE;
|
2008-12-17 15:59:43 +00:00
|
|
|
virtual void xferA8(SkAlpha dst[], const SkPMColor src[], int count,
|
2012-12-17 19:55:24 +00:00
|
|
|
const SkAlpha aa[]) const SK_OVERRIDE;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2013-01-22 14:32:09 +00:00
|
|
|
SK_DEVELOPER_TO_STRING()
|
2012-03-26 17:57:35 +00:00
|
|
|
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkProcXfermode)
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
SkProcXfermode(SkFlattenableReadBuffer&);
|
2012-03-29 15:18:04 +00:00
|
|
|
virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-08-09 22:42:10 +00:00
|
|
|
// allow subclasses to update this after we unflatten
|
|
|
|
void setProc(SkXfermodeProc proc) {
|
|
|
|
fProc = proc;
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
SkXfermodeProc fProc;
|
2011-04-27 14:09:52 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
typedef SkXfermode INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|