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
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkXfermode_DEFINED
|
|
|
|
#define SkXfermode_DEFINED
|
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
#include "SkBlendMode.h"
|
2016-10-05 19:59:51 +00:00
|
|
|
#include "SkColor.h"
|
2016-10-06 00:33:02 +00:00
|
|
|
#include "SkFlattenable.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2014-09-23 16:50:21 +00:00
|
|
|
class GrFragmentProcessor;
|
2013-05-31 17:49:12 +00:00
|
|
|
class GrTexture;
|
2014-12-03 18:40:13 +00:00
|
|
|
class GrXPFactory;
|
2016-07-22 18:05:04 +00:00
|
|
|
class SkRasterPipeline;
|
2013-01-22 14:32:09 +00:00
|
|
|
class SkString;
|
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
struct SkArithmeticParams;
|
|
|
|
|
2016-02-18 20:39:14 +00:00
|
|
|
struct SkPM4f;
|
|
|
|
typedef SkPM4f (*SkXfermodeProc4f)(const SkPM4f& src, const SkPM4f& dst);
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** \class SkXfermode
|
2013-10-10 17:35:58 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* All subclasses are required to be reentrant-safe : it must be legal to share
|
|
|
|
* the same instance between several threads.
|
|
|
|
*/
|
2011-03-15 21:27:08 +00:00
|
|
|
class SK_API SkXfermode : public SkFlattenable {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
|
|
|
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 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-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.
|
2013-04-19 15:03:21 +00:00
|
|
|
The modes are ordered by those that can be expressed as a pair of Coeffs, followed by those
|
|
|
|
that aren't Coeffs but have separable r,g,b computations, and finally
|
|
|
|
those that are not separable.
|
2009-06-22 17:38:10 +00:00
|
|
|
*/
|
|
|
|
enum Mode {
|
|
|
|
kClear_Mode, //!< [0, 0]
|
|
|
|
kSrc_Mode, //!< [Sa, Sc]
|
|
|
|
kDst_Mode, //!< [Da, Dc]
|
2015-10-29 15:41:15 +00:00
|
|
|
kSrcOver_Mode, //!< [Sa + Da * (1 - Sa), Sc + Dc * (1 - Sa)]
|
|
|
|
kDstOver_Mode, //!< [Da + Sa * (1 - Da), Dc + Sc * (1 - Da)]
|
2009-06-22 17:38:10 +00:00
|
|
|
kSrcIn_Mode, //!< [Sa * Da, Sc * Da]
|
2015-10-29 15:41:15 +00:00
|
|
|
kDstIn_Mode, //!< [Da * Sa, Dc * Sa]
|
2009-06-22 17:38:10 +00:00
|
|
|
kSrcOut_Mode, //!< [Sa * (1 - Da), Sc * (1 - Da)]
|
|
|
|
kDstOut_Mode, //!< [Da * (1 - Sa), Dc * (1 - Sa)]
|
2015-10-29 15:41:15 +00:00
|
|
|
kSrcATop_Mode, //!< [Da, Sc * Da + Dc * (1 - Sa)]
|
|
|
|
kDstATop_Mode, //!< [Sa, Dc * Sa + Sc * (1 - Da)]
|
|
|
|
kXor_Mode, //!< [Sa + Da - 2 * Sa * Da, Sc * (1 - Da) + Dc * (1 - Sa)]
|
2013-03-05 16:23:59 +00:00
|
|
|
kPlus_Mode, //!< [Sa + Da, Sc + Dc]
|
2013-01-30 21:36:11 +00:00
|
|
|
kModulate_Mode, // multiplies all components (= alpha and color)
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2013-03-06 07:01:46 +00:00
|
|
|
// Following blend modes are defined in the CSS Compositing standard:
|
2013-03-05 16:23:59 +00:00
|
|
|
// https://dvcs.w3.org/hg/FXTF/rawfile/tip/compositing/index.html#blending
|
2013-04-15 15:16:47 +00:00
|
|
|
kScreen_Mode,
|
2013-04-19 15:03:21 +00:00
|
|
|
kLastCoeffMode = kScreen_Mode,
|
|
|
|
|
|
|
|
kOverlay_Mode,
|
2009-06-22 17:38:10 +00:00
|
|
|
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,
|
2013-04-19 15:03:21 +00:00
|
|
|
kLastSeparableMode = kMultiply_Mode,
|
2009-06-22 17:38:10 +00:00
|
|
|
|
2013-03-05 16:23:59 +00:00
|
|
|
kHue_Mode,
|
|
|
|
kSaturation_Mode,
|
|
|
|
kColor_Mode,
|
|
|
|
kLuminosity_Mode,
|
|
|
|
kLastMode = kLuminosity_Mode
|
2009-06-22 17:38:10 +00:00
|
|
|
};
|
2013-04-02 07:01:34 +00:00
|
|
|
|
2013-04-01 12:51:34 +00:00
|
|
|
/**
|
|
|
|
* Gets the name of the Mode as a string.
|
|
|
|
*/
|
|
|
|
static const char* ModeName(Mode);
|
2016-10-06 00:33:02 +00:00
|
|
|
static const char* ModeName(SkBlendMode mode) {
|
|
|
|
return ModeName(Mode(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
|
2013-03-27 18:31:15 +00:00
|
|
|
* if the xfermode is NULL, and if so, treats it as kSrcOver_Mode.
|
2011-04-14 15:50:52 +00:00
|
|
|
*/
|
2012-12-17 19:55:24 +00:00
|
|
|
static bool AsMode(const SkXfermode*, Mode* mode);
|
2016-03-29 18:32:50 +00:00
|
|
|
static bool AsMode(const sk_sp<SkXfermode>& xfer, Mode* mode) {
|
|
|
|
return AsMode(xfer.get(), 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);
|
2016-03-29 18:32:50 +00:00
|
|
|
static bool IsMode(const sk_sp<SkXfermode>& xfer, Mode mode) {
|
|
|
|
return IsMode(xfer.get(), 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.
|
|
|
|
*/
|
2016-10-28 19:42:34 +00:00
|
|
|
static sk_sp<SkXfermode> Make(SkBlendMode);
|
|
|
|
static sk_sp<SkXfermode> Make(Mode m) { return Make((SkBlendMode)m); }
|
2009-06-22 17:38:10 +00:00
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
/**
|
|
|
|
* Skia maintains global xfermode objects corresponding to each BlendMode. This returns a
|
|
|
|
* ptr to that global xfermode (or null if the mode is srcover). Thus the caller may use
|
|
|
|
* the returned ptr, but it should leave its refcnt untouched.
|
|
|
|
*/
|
|
|
|
static SkXfermode* Peek(SkBlendMode mode) {
|
|
|
|
sk_sp<SkXfermode> xfer = Make(mode);
|
|
|
|
if (!xfer) {
|
|
|
|
SkASSERT(SkBlendMode::kSrcOver == mode);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
SkASSERT(!xfer->unique());
|
|
|
|
return xfer.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
SkBlendMode blend() const {
|
|
|
|
Mode mode;
|
|
|
|
SkAssertResult(this->asMode(&mode));
|
|
|
|
return (SkBlendMode)mode;
|
|
|
|
}
|
|
|
|
|
2016-11-09 15:38:09 +00:00
|
|
|
static SkXfermodeProc GetProc(SkBlendMode);
|
|
|
|
static SkXfermodeProc4f GetProc4f(SkBlendMode);
|
2016-02-22 14:42:31 +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);
|
2016-10-28 19:42:34 +00:00
|
|
|
static bool ModeAsCoeff(SkBlendMode mode, Coeff* src, Coeff* dst) {
|
|
|
|
return ModeAsCoeff((Mode)mode, src, dst);
|
|
|
|
}
|
2009-06-22 17:38:10 +00:00
|
|
|
|
2015-01-22 14:52:29 +00:00
|
|
|
/**
|
|
|
|
* Returns whether or not the xfer mode can support treating coverage as alpha
|
2015-06-26 18:45:03 +00:00
|
|
|
*/
|
2015-01-22 14:52:29 +00:00
|
|
|
virtual bool supportsCoverageAsAlpha() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The same as calling xfermode->supportsCoverageAsAlpha(), except that this also checks if
|
|
|
|
* the xfermode is NULL, and if so, treats it as kSrcOver_Mode.
|
|
|
|
*/
|
|
|
|
static bool SupportsCoverageAsAlpha(const SkXfermode* xfer);
|
2016-03-29 18:32:50 +00:00
|
|
|
static bool SupportsCoverageAsAlpha(const sk_sp<SkXfermode>& xfer) {
|
|
|
|
return SupportsCoverageAsAlpha(xfer.get());
|
|
|
|
}
|
2015-01-22 14:52:29 +00:00
|
|
|
|
|
|
|
enum SrcColorOpacity {
|
|
|
|
// The src color is known to be opaque (alpha == 255)
|
|
|
|
kOpaque_SrcColorOpacity = 0,
|
|
|
|
// The src color is known to be fully transparent (color == 0)
|
|
|
|
kTransparentBlack_SrcColorOpacity = 1,
|
|
|
|
// The src alpha is known to be fully transparent (alpha == 0)
|
|
|
|
kTransparentAlpha_SrcColorOpacity = 2,
|
|
|
|
// The src color opacity is unknown
|
|
|
|
kUnknown_SrcColorOpacity = 3
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether or not the result of the draw with the xfer mode will be opaque or not. The
|
|
|
|
* input to this call is an enum describing known information about the opacity of the src color
|
|
|
|
* that will be given to the xfer mode.
|
|
|
|
*/
|
|
|
|
virtual bool isOpaque(SrcColorOpacity opacityType) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The same as calling xfermode->isOpaque(...), except that this also checks if
|
|
|
|
* the xfermode is NULL, and if so, treats it as kSrcOver_Mode.
|
|
|
|
*/
|
|
|
|
static bool IsOpaque(const SkXfermode* xfer, SrcColorOpacity opacityType);
|
2016-03-29 18:32:50 +00:00
|
|
|
static bool IsOpaque(const sk_sp<SkXfermode>& xfer, SrcColorOpacity opacityType) {
|
|
|
|
return IsOpaque(xfer.get(), opacityType);
|
|
|
|
}
|
2016-10-06 00:33:02 +00:00
|
|
|
static bool IsOpaque(SkBlendMode, SrcColorOpacity);
|
2015-01-22 14:52:29 +00:00
|
|
|
|
2016-02-09 13:09:27 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
/** Used by the SkXfermodeImageFilter to blend two colors via a GrFragmentProcessor.
|
|
|
|
The input to the returned FP is the src color. The dst color is
|
2016-07-22 18:05:04 +00:00
|
|
|
provided by the dst param which becomes a child FP of the returned FP.
|
2016-02-09 13:09:27 +00:00
|
|
|
It is legal for the function to return a null output. This indicates that
|
2015-09-15 22:33:27 +00:00
|
|
|
the output of the blend is simply the src color.
|
2013-03-27 18:31:15 +00:00
|
|
|
*/
|
2016-06-09 15:01:03 +00:00
|
|
|
virtual sk_sp<GrFragmentProcessor> makeFragmentProcessorForImageFilter(
|
|
|
|
sk_sp<GrFragmentProcessor> dst) const;
|
2014-12-03 18:40:13 +00:00
|
|
|
|
2016-07-22 18:05:04 +00:00
|
|
|
/** A subclass must implement this factory function to work with the GPU backend.
|
|
|
|
The xfermode will return a factory for which the caller will get a ref. It is up
|
2016-02-09 13:09:27 +00:00
|
|
|
to the caller to install it. XferProcessors cannot use a background texture.
|
2015-11-23 21:20:41 +00:00
|
|
|
*/
|
2016-06-09 15:01:03 +00:00
|
|
|
virtual sk_sp<GrXPFactory> asXPFactory() const;
|
2016-02-09 13:09:27 +00:00
|
|
|
#endif
|
2013-03-27 18:31:15 +00:00
|
|
|
|
2014-03-13 18:02:17 +00:00
|
|
|
SK_TO_STRING_PUREVIRT()
|
2012-03-23 19:00:34 +00:00
|
|
|
SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
|
2016-04-04 21:57:19 +00:00
|
|
|
SK_DEFINE_FLATTENABLE_TYPE(SkXfermode)
|
2013-10-23 17:06:21 +00:00
|
|
|
|
2016-02-24 14:21:36 +00:00
|
|
|
enum D32Flags {
|
|
|
|
kSrcIsOpaque_D32Flag = 1 << 0,
|
|
|
|
kSrcIsSingle_D32Flag = 1 << 1,
|
|
|
|
kDstIsSRGB_D32Flag = 1 << 2,
|
2016-01-31 02:52:31 +00:00
|
|
|
};
|
2016-11-09 15:38:09 +00:00
|
|
|
typedef void (*D32Proc)(SkBlendMode, uint32_t dst[], const SkPM4f src[],
|
2016-02-24 14:21:36 +00:00
|
|
|
int count, const SkAlpha coverage[]);
|
2016-11-09 15:38:09 +00:00
|
|
|
static D32Proc GetD32Proc(SkBlendMode, uint32_t flags);
|
2016-02-24 14:21:36 +00:00
|
|
|
|
2016-04-14 16:02:14 +00:00
|
|
|
enum F16Flags {
|
|
|
|
kSrcIsOpaque_F16Flag = 1 << 0,
|
|
|
|
kSrcIsSingle_F16Flag = 1 << 1,
|
2016-01-31 02:52:31 +00:00
|
|
|
};
|
2016-11-09 15:38:09 +00:00
|
|
|
typedef void (*F16Proc)(SkBlendMode, uint64_t dst[], const SkPM4f src[], int count,
|
2016-02-24 14:21:36 +00:00
|
|
|
const SkAlpha coverage[]);
|
2016-11-09 15:38:09 +00:00
|
|
|
static F16Proc GetF16Proc(SkBlendMode, uint32_t flags);
|
2016-02-09 17:14:59 +00:00
|
|
|
|
2016-02-18 16:16:33 +00:00
|
|
|
enum LCDFlags {
|
|
|
|
kSrcIsOpaque_LCDFlag = 1 << 0, // else src(s) may have alpha < 1
|
|
|
|
kSrcIsSingle_LCDFlag = 1 << 1, // else src[count]
|
2016-04-14 16:02:14 +00:00
|
|
|
kDstIsSRGB_LCDFlag = 1 << 2, // else l32 or f16
|
2016-02-18 16:16:33 +00:00
|
|
|
};
|
|
|
|
typedef void (*LCD32Proc)(uint32_t* dst, const SkPM4f* src, int count, const uint16_t lcd[]);
|
2016-04-14 16:02:14 +00:00
|
|
|
typedef void (*LCDF16Proc)(uint64_t* dst, const SkPM4f* src, int count, const uint16_t lcd[]);
|
2016-02-18 16:16:33 +00:00
|
|
|
static LCD32Proc GetLCD32Proc(uint32_t flags);
|
2016-04-14 16:02:14 +00:00
|
|
|
static LCDF16Proc GetLCDF16Proc(uint32_t) { return nullptr; }
|
2016-02-18 16:16:33 +00:00
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
virtual bool isArithmetic(SkArithmeticParams*) const { return false; }
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
protected:
|
2014-05-15 15:40:41 +00:00
|
|
|
SkXfermode() {}
|
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
|
2015-09-15 22:33:27 +00:00
|
|
|
implementation of this method just returns dst. If performance is
|
2008-12-17 15:59:43 +00:00
|
|
|
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
|
|
|
|
};
|
2013-10-04 16:52:55 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
typedef SkFlattenable INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|