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"
|
2016-07-18 17:53:52 +00:00
|
|
|
#include "GrColorSpaceXform.h"
|
2014-12-03 18:40:13 +00:00
|
|
|
#include "GrXferProcessor.h"
|
2014-12-09 19:15:43 +00:00
|
|
|
#include "effects/GrPorterDuffXferProcessor.h"
|
2015-08-12 16:40:47 +00:00
|
|
|
#include "GrFragmentProcessor.h"
|
2011-02-22 20:59:41 +00:00
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
#include "SkBlendMode.h"
|
2016-06-09 15:01:03 +00:00
|
|
|
#include "SkRefCnt.h"
|
2015-02-17 15:34:43 +00:00
|
|
|
#include "SkRegion.h"
|
2011-05-11 14:05:25 +00:00
|
|
|
|
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
|
2014-09-23 16:50:21 +00:00
|
|
|
* created by subclassing GrProcessor.
|
2012-10-05 19:04:13 +00:00
|
|
|
*
|
|
|
|
* The primitive color computation starts with the color specified by setColor(). This color is the
|
2015-02-02 21:00:10 +00:00
|
|
|
* input to the first color stage. Each color stage feeds its output to the next color stage.
|
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.
|
|
|
|
*
|
2015-02-02 21:00:10 +00:00
|
|
|
* setXPFactory is used to control blending between the output color and dest. It also implements
|
|
|
|
* the application of fractional coverage from the coverage pipeline.
|
2011-02-22 20:59:41 +00:00
|
|
|
*/
|
|
|
|
class GrPaint {
|
|
|
|
public:
|
2015-01-22 15:11:44 +00:00
|
|
|
GrPaint();
|
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
|
|
|
|
2016-06-09 15:01:03 +00:00
|
|
|
~GrPaint() { }
|
2012-10-05 19:04:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The initial color of the drawn primitive. Defaults to solid white.
|
|
|
|
*/
|
2016-06-24 19:50:19 +00:00
|
|
|
void setColor4f(const GrColor4f& color) { fColor = color; }
|
|
|
|
const GrColor4f& getColor4f() const { return fColor; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Legacy getter, until all code handles 4f directly.
|
|
|
|
*/
|
|
|
|
GrColor getColor() const { return fColor.toGrColor(); }
|
2012-10-05 19:04:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Should primitives be anti-aliased or not. Defaults to false.
|
|
|
|
*/
|
|
|
|
void setAntiAlias(bool aa) { fAntiAlias = aa; }
|
|
|
|
bool isAntiAlias() const { return fAntiAlias; }
|
|
|
|
|
2016-03-25 13:01:59 +00:00
|
|
|
/**
|
|
|
|
* Should shader output conversion from linear to sRGB be disabled.
|
|
|
|
* Only relevant if the destination is sRGB. Defaults to false.
|
|
|
|
*/
|
|
|
|
void setDisableOutputConversionToSRGB(bool srgb) { fDisableOutputConversionToSRGB = srgb; }
|
|
|
|
bool getDisableOutputConversionToSRGB() const { return fDisableOutputConversionToSRGB; }
|
|
|
|
|
2016-04-06 14:38:23 +00:00
|
|
|
/**
|
|
|
|
* Should sRGB inputs be allowed to perform sRGB to linear conversion. With this flag
|
|
|
|
* set to false, sRGB textures will be treated as linear (including filtering).
|
|
|
|
*/
|
|
|
|
void setAllowSRGBInputs(bool allowSRGBInputs) { fAllowSRGBInputs = allowSRGBInputs; }
|
|
|
|
bool getAllowSRGBInputs() const { return fAllowSRGBInputs; }
|
|
|
|
|
2016-08-01 18:01:56 +00:00
|
|
|
/**
|
|
|
|
* Does one of the fragment processors need a field of distance vectors to the nearest edge?
|
|
|
|
*/
|
|
|
|
bool usesDistanceVectorField() const { return fUsesDistanceVectorField; }
|
|
|
|
|
2016-04-13 20:10:14 +00:00
|
|
|
/**
|
2016-04-13 20:56:21 +00:00
|
|
|
* Should rendering be gamma-correct, end-to-end. Causes sRGB render targets to behave
|
|
|
|
* as such (with linear blending), and sRGB inputs to be filtered and decoded correctly.
|
2016-04-13 20:10:14 +00:00
|
|
|
*/
|
|
|
|
void setGammaCorrect(bool gammaCorrect) {
|
|
|
|
setDisableOutputConversionToSRGB(!gammaCorrect);
|
|
|
|
setAllowSRGBInputs(gammaCorrect);
|
|
|
|
}
|
|
|
|
|
2016-06-09 15:01:03 +00:00
|
|
|
void setXPFactory(sk_sp<GrXPFactory> xpFactory) {
|
|
|
|
fXPFactory = std::move(xpFactory);
|
2014-12-03 18:40:13 +00:00
|
|
|
}
|
|
|
|
|
2016-10-06 00:33:02 +00:00
|
|
|
void setPorterDuffXPFactory(SkBlendMode mode) {
|
|
|
|
fXPFactory = GrPorterDuffXPFactory::Make((SkXfermode::Mode)mode);
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:15:43 +00:00
|
|
|
void setPorterDuffXPFactory(SkXfermode::Mode mode) {
|
2016-06-09 15:01:03 +00:00
|
|
|
fXPFactory = GrPorterDuffXPFactory::Make(mode);
|
2014-12-09 19:15:43 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 15:42:38 +00:00
|
|
|
void setCoverageSetOpXPFactory(SkRegion::Op, bool invertCoverage = false);
|
2014-12-09 19:15:43 +00:00
|
|
|
|
2012-10-05 19:04:13 +00:00
|
|
|
/**
|
2014-09-23 16:50:21 +00:00
|
|
|
* Appends an additional color processor to the color computation.
|
2012-10-05 19:04:13 +00:00
|
|
|
*/
|
2016-06-09 15:01:03 +00:00
|
|
|
void addColorFragmentProcessor(sk_sp<GrFragmentProcessor> fp) {
|
2014-09-23 16:50:21 +00:00
|
|
|
SkASSERT(fp);
|
2016-08-01 18:01:56 +00:00
|
|
|
fUsesDistanceVectorField |= fp->usesDistanceVectorField();
|
2016-06-09 15:01:03 +00:00
|
|
|
fColorFragmentProcessors.push_back(std::move(fp));
|
2011-05-17 20:15:30 +00:00
|
|
|
}
|
|
|
|
|
2012-10-05 19:04:13 +00:00
|
|
|
/**
|
2014-09-23 16:50:21 +00:00
|
|
|
* Appends an additional coverage processor to the coverage computation.
|
2012-10-05 19:04:13 +00:00
|
|
|
*/
|
2016-06-09 15:01:03 +00:00
|
|
|
void addCoverageFragmentProcessor(sk_sp<GrFragmentProcessor> fp) {
|
2014-09-23 16:50:21 +00:00
|
|
|
SkASSERT(fp);
|
2016-08-01 18:01:56 +00:00
|
|
|
fUsesDistanceVectorField |= fp->usesDistanceVectorField();
|
2016-06-09 15:01:03 +00:00
|
|
|
fCoverageFragmentProcessors.push_back(std::move(fp));
|
2011-02-22 20:59:41 +00:00
|
|
|
}
|
|
|
|
|
2013-07-13 17:24:24 +00:00
|
|
|
/**
|
|
|
|
* Helpers for adding color or coverage effects that sample a texture. The matrix is applied
|
|
|
|
* to the src space position to compute texture coordinates.
|
|
|
|
*/
|
2016-07-18 17:53:52 +00:00
|
|
|
void addColorTextureProcessor(GrTexture*, sk_sp<GrColorSpaceXform>, const SkMatrix&);
|
2014-09-23 16:50:21 +00:00
|
|
|
void addCoverageTextureProcessor(GrTexture*, const SkMatrix&);
|
2016-07-18 17:53:52 +00:00
|
|
|
void addColorTextureProcessor(GrTexture*, sk_sp<GrColorSpaceXform>, const SkMatrix&,
|
|
|
|
const GrTextureParams&);
|
2014-09-23 16:50:21 +00:00
|
|
|
void addCoverageTextureProcessor(GrTexture*, const SkMatrix&, const GrTextureParams&);
|
2012-07-20 13:37:06 +00:00
|
|
|
|
2015-08-27 13:30:17 +00:00
|
|
|
int numColorFragmentProcessors() const { return fColorFragmentProcessors.count(); }
|
|
|
|
int numCoverageFragmentProcessors() const { return fCoverageFragmentProcessors.count(); }
|
|
|
|
int numTotalFragmentProcessors() const { return this->numColorFragmentProcessors() +
|
|
|
|
this->numCoverageFragmentProcessors(); }
|
2012-07-20 13:37:06 +00:00
|
|
|
|
2016-06-09 15:01:03 +00:00
|
|
|
GrXPFactory* getXPFactory() const {
|
|
|
|
return fXPFactory.get();
|
2015-01-22 15:11:44 +00:00
|
|
|
}
|
2014-12-03 18:40:13 +00:00
|
|
|
|
2016-06-09 15:01:03 +00:00
|
|
|
GrFragmentProcessor* getColorFragmentProcessor(int i) const {
|
|
|
|
return fColorFragmentProcessors[i].get();
|
2015-08-27 13:30:17 +00:00
|
|
|
}
|
2016-06-09 15:01:03 +00:00
|
|
|
GrFragmentProcessor* getCoverageFragmentProcessor(int i) const {
|
|
|
|
return fCoverageFragmentProcessors[i].get();
|
2015-08-27 13:30:17 +00:00
|
|
|
}
|
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
|
|
|
fAntiAlias = paint.fAntiAlias;
|
2016-03-25 13:01:59 +00:00
|
|
|
fDisableOutputConversionToSRGB = paint.fDisableOutputConversionToSRGB;
|
2016-04-06 14:38:23 +00:00
|
|
|
fAllowSRGBInputs = paint.fAllowSRGBInputs;
|
2016-08-01 18:01:56 +00:00
|
|
|
fUsesDistanceVectorField = paint.fUsesDistanceVectorField;
|
2011-02-22 20:59:41 +00:00
|
|
|
|
|
|
|
fColor = paint.fColor;
|
2015-08-27 13:30:17 +00:00
|
|
|
fColorFragmentProcessors = paint.fColorFragmentProcessors;
|
|
|
|
fCoverageFragmentProcessors = paint.fCoverageFragmentProcessors;
|
2013-07-13 17:24:24 +00:00
|
|
|
|
2016-06-09 15:01:03 +00:00
|
|
|
fXPFactory = paint.fXPFactory;
|
2014-12-03 18:40:13 +00:00
|
|
|
|
2011-09-12 14:30:27 +00:00
|
|
|
return *this;
|
2011-02-22 20:59:41 +00:00
|
|
|
}
|
|
|
|
|
2013-08-14 21:56:37 +00:00
|
|
|
/**
|
2015-06-02 17:43:39 +00:00
|
|
|
* Returns true if the paint's output color will be constant after blending. If the result is
|
|
|
|
* true, constantColor will be updated to contain the constant color. Note that we can conflate
|
|
|
|
* coverage and color, so the actual values written to pixels with partial coverage may still
|
|
|
|
* not seem constant, even if this function returns true.
|
2013-08-14 21:56:37 +00:00
|
|
|
*/
|
2016-08-11 14:39:33 +00:00
|
|
|
bool isConstantBlendedColor(GrColor* constantColor) const {
|
|
|
|
GrColor paintColor = this->getColor();
|
|
|
|
if (!fXPFactory && fColorFragmentProcessors.empty()) {
|
|
|
|
if (!GrColorIsOpaque(paintColor)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*constantColor = paintColor;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->internalIsConstantBlendedColor(paintColor, constantColor);
|
|
|
|
}
|
2013-08-14 21:56:37 +00:00
|
|
|
|
2014-12-17 23:50:11 +00:00
|
|
|
private:
|
2016-08-11 14:39:33 +00:00
|
|
|
bool internalIsConstantBlendedColor(GrColor paintColor, GrColor* constantColor) const;
|
|
|
|
|
2016-06-09 15:01:03 +00:00
|
|
|
mutable sk_sp<GrXPFactory> fXPFactory;
|
|
|
|
SkSTArray<4, sk_sp<GrFragmentProcessor>> fColorFragmentProcessors;
|
|
|
|
SkSTArray<2, sk_sp<GrFragmentProcessor>> fCoverageFragmentProcessors;
|
2011-05-17 20:15:30 +00:00
|
|
|
|
2016-06-09 15:01:03 +00:00
|
|
|
bool fAntiAlias;
|
|
|
|
bool fDisableOutputConversionToSRGB;
|
|
|
|
bool fAllowSRGBInputs;
|
2016-08-01 18:01:56 +00:00
|
|
|
bool fUsesDistanceVectorField;
|
2012-10-05 19:04:13 +00:00
|
|
|
|
2016-06-24 19:50:19 +00:00
|
|
|
GrColor4f fColor;
|
2011-02-22 20:59:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|