2011-02-22 20:59:41 +00:00
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2011-02-22 20:59:41 +00:00
|
|
|
*/
|
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
|
2011-02-22 20:59:41 +00:00
|
|
|
#ifndef GrPaint_DEFINED
|
|
|
|
#define GrPaint_DEFINED
|
|
|
|
|
|
|
|
#include "GrColor.h"
|
2012-10-26 13:01:20 +00:00
|
|
|
#include "GrEffectStage.h"
|
2011-02-22 20:59:41 +00:00
|
|
|
|
2011-05-11 14:05:25 +00:00
|
|
|
#include "SkXfermode.h"
|
|
|
|
|
2011-02-22 20:59:41 +00:00
|
|
|
/**
|
2012-10-05 19:04:13 +00:00
|
|
|
* The paint describes how color and coverage are computed at each pixel by GrContext draw
|
|
|
|
* functions and the how color is blended with the destination pixel.
|
|
|
|
*
|
|
|
|
* The paint allows installation of custom color and coverage stages. New types of stages are
|
2012-10-24 18:28:34 +00:00
|
|
|
* created by subclassing GrEffect.
|
2012-10-05 19:04:13 +00:00
|
|
|
*
|
|
|
|
* The primitive color computation starts with the color specified by setColor(). This color is the
|
|
|
|
* input to the first color stage. Each color stage feeds its output to the next color stage. The
|
|
|
|
* final color stage's output color is input to the color filter specified by
|
2012-10-17 13:36:14 +00:00
|
|
|
* setXfermodeColorFilter which produces the final source color, S.
|
2012-10-05 19:04:13 +00:00
|
|
|
*
|
|
|
|
* Fractional pixel coverage follows a similar flow. The coverage is initially the value specified
|
|
|
|
* by setCoverage(). This is input to the first coverage stage. Coverage stages are chained
|
|
|
|
* together in the same manner as color stages. The output of the last stage is modulated by any
|
|
|
|
* fractional coverage produced by anti-aliasing. This last step produces the final coverage, C.
|
|
|
|
*
|
|
|
|
* setBlendFunc() specifies blending coefficients for S (described above) and D, the initial value
|
|
|
|
* of the destination pixel, labeled Bs and Bd respectively. The final value of the destination
|
|
|
|
* pixel is then D' = (1-C)*D + C*(Bd*D + Bs*S).
|
|
|
|
*
|
|
|
|
* Note that the coverage is applied after the blend. This is why they are computed as distinct
|
|
|
|
* values.
|
|
|
|
*
|
2012-10-24 18:28:34 +00:00
|
|
|
* TODO: Encapsulate setXfermodeColorFilter in a GrEffect and remove from GrPaint.
|
2011-02-22 20:59:41 +00:00
|
|
|
*/
|
|
|
|
class GrPaint {
|
|
|
|
public:
|
2011-05-17 20:15:30 +00:00
|
|
|
enum {
|
2013-03-28 13:23:29 +00:00
|
|
|
kMaxColorStages = 3,
|
2012-10-05 14:54:42 +00:00
|
|
|
kMaxCoverageStages = 1,
|
2011-05-17 20:15:30 +00:00
|
|
|
};
|
2011-02-22 20:59:41 +00:00
|
|
|
|
2012-10-05 19:04:13 +00:00
|
|
|
GrPaint() { this->reset(); }
|
2011-02-22 20:59:41 +00:00
|
|
|
|
2012-10-05 19:04:13 +00:00
|
|
|
GrPaint(const GrPaint& paint) { *this = paint; }
|
2011-02-22 20:59:41 +00:00
|
|
|
|
2012-10-05 19:04:13 +00:00
|
|
|
~GrPaint() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the blending coefficients to use to blend the final primitive color with the
|
|
|
|
* destination color. Defaults to kOne for src and kZero for dst (i.e. src mode).
|
|
|
|
*/
|
|
|
|
void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) {
|
|
|
|
fSrcBlendCoeff = srcCoeff;
|
|
|
|
fDstBlendCoeff = dstCoeff;
|
|
|
|
}
|
|
|
|
GrBlendCoeff getSrcBlendCoeff() const { return fSrcBlendCoeff; }
|
|
|
|
GrBlendCoeff getDstBlendCoeff() const { return fDstBlendCoeff; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The initial color of the drawn primitive. Defaults to solid white.
|
|
|
|
*/
|
|
|
|
void setColor(GrColor color) { fColor = color; }
|
|
|
|
GrColor getColor() const { return fColor; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies fractional coverage to the entire drawn primitive. Defaults to 0xff.
|
|
|
|
*/
|
|
|
|
void setCoverage(uint8_t coverage) { fCoverage = coverage; }
|
|
|
|
uint8_t getCoverage() const { return fCoverage; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Should primitives be anti-aliased or not. Defaults to false.
|
|
|
|
*/
|
|
|
|
void setAntiAlias(bool aa) { fAntiAlias = aa; }
|
|
|
|
bool isAntiAlias() const { return fAntiAlias; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Should dithering be applied. Defaults to false.
|
|
|
|
*/
|
|
|
|
void setDither(bool dither) { fDither = dither; }
|
|
|
|
bool isDither() const { return fDither; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enables a SkXfermode::Mode-based color filter applied to the primitive color. The constant
|
|
|
|
* color passed to this function is considered the "src" color and the primitive's color is
|
|
|
|
* considered the "dst" color. Defaults to kDst_Mode which equates to simply passing through
|
|
|
|
* the primitive color unmodified.
|
|
|
|
*/
|
|
|
|
void setXfermodeColorFilter(SkXfermode::Mode mode, GrColor color) {
|
|
|
|
fColorFilterColor = color;
|
|
|
|
fColorFilterXfermode = mode;
|
|
|
|
}
|
|
|
|
SkXfermode::Mode getColorFilterMode() const { return fColorFilterXfermode; }
|
|
|
|
GrColor getColorFilterColor() const { return fColorFilterColor; }
|
|
|
|
|
|
|
|
/**
|
2012-10-17 13:36:14 +00:00
|
|
|
* Disables the SkXfermode::Mode color filter.
|
2012-10-05 19:04:13 +00:00
|
|
|
*/
|
|
|
|
void resetColorFilter() {
|
|
|
|
fColorFilterXfermode = SkXfermode::kDst_Mode;
|
|
|
|
fColorFilterColor = GrColorPackRGBA(0xff, 0xff, 0xff, 0xff);
|
|
|
|
}
|
2011-05-11 14:05:25 +00:00
|
|
|
|
2012-10-05 19:04:13 +00:00
|
|
|
/**
|
|
|
|
* Specifies a stage of the color pipeline. Usually the texture matrices of color stages apply
|
|
|
|
* to the primitive's positions. Some GrContext calls take explicit coords as an array or a
|
2012-10-26 13:01:20 +00:00
|
|
|
* rect. In this case these are the pre-matrix coords to colorStage(0).
|
2012-10-05 19:04:13 +00:00
|
|
|
*/
|
2012-10-26 13:01:20 +00:00
|
|
|
GrEffectStage* colorStage(int i) {
|
2012-10-05 14:54:42 +00:00
|
|
|
GrAssert((unsigned)i < kMaxColorStages);
|
2012-10-26 13:01:20 +00:00
|
|
|
return fColorStages + i;
|
2011-05-17 20:15:30 +00:00
|
|
|
}
|
|
|
|
|
2012-10-26 13:01:20 +00:00
|
|
|
const GrEffectStage& getColorStage(int i) const {
|
2012-10-05 14:54:42 +00:00
|
|
|
GrAssert((unsigned)i < kMaxColorStages);
|
2012-10-26 13:01:20 +00:00
|
|
|
return fColorStages[i];
|
2011-05-17 20:15:30 +00:00
|
|
|
}
|
|
|
|
|
2012-10-05 14:54:42 +00:00
|
|
|
bool isColorStageEnabled(int i) const {
|
|
|
|
GrAssert((unsigned)i < kMaxColorStages);
|
2012-10-26 13:01:20 +00:00
|
|
|
return (NULL != fColorStages[i].getEffect());
|
2011-05-17 20:15:30 +00:00
|
|
|
}
|
|
|
|
|
2012-10-05 19:04:13 +00:00
|
|
|
/**
|
|
|
|
* Specifies a stage of the coverage pipeline. Coverage stages' texture matrices are always
|
|
|
|
* applied to the primitive's position, never to explicit texture coords.
|
|
|
|
*/
|
2012-10-26 13:01:20 +00:00
|
|
|
GrEffectStage* coverageStage(int i) {
|
2012-10-05 14:54:42 +00:00
|
|
|
GrAssert((unsigned)i < kMaxCoverageStages);
|
2012-10-26 13:01:20 +00:00
|
|
|
return fCoverageStages + i;
|
2011-05-17 20:15:30 +00:00
|
|
|
}
|
|
|
|
|
2012-10-26 13:01:20 +00:00
|
|
|
const GrEffectStage& getCoverageStage(int i) const {
|
2012-10-05 14:54:42 +00:00
|
|
|
GrAssert((unsigned)i < kMaxCoverageStages);
|
2012-10-26 13:01:20 +00:00
|
|
|
return fCoverageStages[i];
|
2011-02-22 20:59:41 +00:00
|
|
|
}
|
|
|
|
|
2012-10-05 14:54:42 +00:00
|
|
|
bool isCoverageStageEnabled(int i) const {
|
|
|
|
GrAssert((unsigned)i < kMaxCoverageStages);
|
2012-10-26 13:01:20 +00:00
|
|
|
return (NULL != fCoverageStages[i].getEffect());
|
2012-06-25 17:27:28 +00:00
|
|
|
}
|
|
|
|
|
2012-10-05 14:54:42 +00:00
|
|
|
bool hasCoverageStage() const {
|
|
|
|
for (int i = 0; i < kMaxCoverageStages; ++i) {
|
|
|
|
if (this->isCoverageStageEnabled(i)) {
|
2012-07-20 13:37:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-05 14:54:42 +00:00
|
|
|
bool hasColorStage() const {
|
|
|
|
for (int i = 0; i < kMaxColorStages; ++i) {
|
|
|
|
if (this->isColorStageEnabled(i)) {
|
2012-07-20 13:37:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-10-05 14:54:42 +00:00
|
|
|
bool hasStage() const { return this->hasColorStage() || this->hasCoverageStage(); }
|
2012-07-20 13:37:06 +00:00
|
|
|
|
2011-09-12 14:30:27 +00:00
|
|
|
GrPaint& operator=(const GrPaint& paint) {
|
2011-02-22 20:59:41 +00:00
|
|
|
fSrcBlendCoeff = paint.fSrcBlendCoeff;
|
|
|
|
fDstBlendCoeff = paint.fDstBlendCoeff;
|
|
|
|
fAntiAlias = paint.fAntiAlias;
|
|
|
|
fDither = paint.fDither;
|
|
|
|
|
|
|
|
fColor = paint.fColor;
|
2012-01-18 20:34:00 +00:00
|
|
|
fCoverage = paint.fCoverage;
|
2011-02-22 20:59:41 +00:00
|
|
|
|
2011-05-11 14:05:25 +00:00
|
|
|
fColorFilterColor = paint.fColorFilterColor;
|
|
|
|
fColorFilterXfermode = paint.fColorFilterXfermode;
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-10-05 14:54:42 +00:00
|
|
|
for (int i = 0; i < kMaxColorStages; ++i) {
|
|
|
|
if (paint.isColorStageEnabled(i)) {
|
2012-10-26 13:01:20 +00:00
|
|
|
fColorStages[i] = paint.fColorStages[i];
|
2012-03-16 17:50:37 +00:00
|
|
|
}
|
2011-05-17 20:15:30 +00:00
|
|
|
}
|
2012-10-05 14:54:42 +00:00
|
|
|
for (int i = 0; i < kMaxCoverageStages; ++i) {
|
|
|
|
if (paint.isCoverageStageEnabled(i)) {
|
2012-10-26 13:01:20 +00:00
|
|
|
fCoverageStages[i] = paint.fCoverageStages[i];
|
2012-03-16 17:50:37 +00:00
|
|
|
}
|
2011-05-17 20:15:30 +00:00
|
|
|
}
|
2011-09-12 14:30:27 +00:00
|
|
|
return *this;
|
2011-02-22 20:59:41 +00:00
|
|
|
}
|
|
|
|
|
2012-10-05 19:04:13 +00:00
|
|
|
/**
|
|
|
|
* Resets the paint to the defaults.
|
|
|
|
*/
|
2011-02-22 20:59:41 +00:00
|
|
|
void reset() {
|
2011-05-17 20:15:30 +00:00
|
|
|
this->resetBlend();
|
|
|
|
this->resetOptions();
|
|
|
|
this->resetColor();
|
2012-01-18 20:34:00 +00:00
|
|
|
this->resetCoverage();
|
2012-10-26 13:01:20 +00:00
|
|
|
this->resetStages();
|
2011-05-17 20:15:30 +00:00
|
|
|
this->resetColorFilter();
|
2011-05-11 14:05:25 +00:00
|
|
|
}
|
|
|
|
|
2011-05-17 20:15:30 +00:00
|
|
|
// internal use
|
|
|
|
// GrPaint's textures and masks map to the first N stages
|
|
|
|
// of GrDrawTarget in that order (textures followed by masks)
|
|
|
|
enum {
|
2012-10-05 14:54:42 +00:00
|
|
|
kFirstColorStage = 0,
|
|
|
|
kFirstCoverageStage = kMaxColorStages,
|
|
|
|
kTotalStages = kFirstColorStage + kMaxColorStages + kMaxCoverageStages,
|
2011-05-17 20:15:30 +00:00
|
|
|
};
|
|
|
|
|
2011-02-22 20:59:41 +00:00
|
|
|
private:
|
2013-03-20 19:19:53 +00:00
|
|
|
/**
|
|
|
|
* Called when the source coord system from which geometry is rendered changes. It ensures that
|
|
|
|
* the local coordinates seen by effects remains unchanged. oldToNew gives the transformation
|
|
|
|
* from the previous coord system to the new coord system.
|
|
|
|
*/
|
|
|
|
void localCoordChange(const SkMatrix& oldToNew) {
|
|
|
|
for (int i = 0; i < kMaxColorStages; ++i) {
|
|
|
|
if (this->isColorStageEnabled(i)) {
|
|
|
|
fColorStages[i].localCoordChange(oldToNew);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 0; i < kMaxCoverageStages; ++i) {
|
|
|
|
if (this->isCoverageStageEnabled(i)) {
|
|
|
|
fCoverageStages[i].localCoordChange(oldToNew);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool localCoordChangeInverse(const SkMatrix& newToOld) {
|
|
|
|
SkMatrix oldToNew;
|
|
|
|
bool computed = false;
|
|
|
|
for (int i = 0; i < kMaxColorStages; ++i) {
|
|
|
|
if (this->isColorStageEnabled(i)) {
|
|
|
|
if (!computed && !newToOld.invert(&oldToNew)) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
computed = true;
|
|
|
|
}
|
|
|
|
fColorStages[i].localCoordChange(oldToNew);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 0; i < kMaxCoverageStages; ++i) {
|
|
|
|
if (this->isCoverageStageEnabled(i)) {
|
|
|
|
if (!computed && !newToOld.invert(&oldToNew)) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
computed = true;
|
|
|
|
}
|
|
|
|
fCoverageStages[i].localCoordChange(oldToNew);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend class GrContext; // To access above two functions
|
2011-05-17 20:15:30 +00:00
|
|
|
|
2012-10-26 13:01:20 +00:00
|
|
|
GrEffectStage fColorStages[kMaxColorStages];
|
|
|
|
GrEffectStage fCoverageStages[kMaxCoverageStages];
|
2011-05-17 20:15:30 +00:00
|
|
|
|
2012-10-05 19:04:13 +00:00
|
|
|
GrBlendCoeff fSrcBlendCoeff;
|
|
|
|
GrBlendCoeff fDstBlendCoeff;
|
|
|
|
bool fAntiAlias;
|
|
|
|
bool fDither;
|
|
|
|
|
|
|
|
GrColor fColor;
|
|
|
|
uint8_t fCoverage;
|
|
|
|
|
|
|
|
GrColor fColorFilterColor;
|
|
|
|
SkXfermode::Mode fColorFilterXfermode;
|
|
|
|
|
2011-02-22 20:59:41 +00:00
|
|
|
void resetBlend() {
|
2012-06-06 20:51:20 +00:00
|
|
|
fSrcBlendCoeff = kOne_GrBlendCoeff;
|
|
|
|
fDstBlendCoeff = kZero_GrBlendCoeff;
|
2011-02-22 20:59:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void resetOptions() {
|
|
|
|
fAntiAlias = false;
|
|
|
|
fDither = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void resetColor() {
|
|
|
|
fColor = GrColorPackRGBA(0xff, 0xff, 0xff, 0xff);
|
|
|
|
}
|
|
|
|
|
2012-01-18 20:34:00 +00:00
|
|
|
void resetCoverage() {
|
|
|
|
fCoverage = 0xff;
|
|
|
|
}
|
|
|
|
|
2012-10-26 13:01:20 +00:00
|
|
|
void resetStages() {
|
2012-10-05 14:54:42 +00:00
|
|
|
for (int i = 0; i < kMaxColorStages; ++i) {
|
2012-10-26 13:01:20 +00:00
|
|
|
fColorStages[i].reset();
|
2011-05-17 20:15:30 +00:00
|
|
|
}
|
2012-10-05 14:54:42 +00:00
|
|
|
for (int i = 0; i < kMaxCoverageStages; ++i) {
|
2012-10-26 13:01:20 +00:00
|
|
|
fCoverageStages[i].reset();
|
2011-05-17 20:15:30 +00:00
|
|
|
}
|
|
|
|
}
|
2011-02-22 20:59:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|