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"
|
2015-07-09 13:28:14 +00:00
|
|
|
#include "GrProcessorDataManager.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
|
|
|
|
2015-02-17 15:34:43 +00:00
|
|
|
#include "SkRegion.h"
|
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
|
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
|
|
|
|
2015-08-27 13:30:17 +00:00
|
|
|
~GrPaint() { this->resetFragmentProcessors(); }
|
2012-10-05 19:04:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The initial color of the drawn primitive. Defaults to solid white.
|
|
|
|
*/
|
|
|
|
void setColor(GrColor color) { fColor = color; }
|
|
|
|
GrColor getColor() const { return fColor; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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; }
|
|
|
|
|
2014-12-03 18:40:13 +00:00
|
|
|
const GrXPFactory* setXPFactory(const GrXPFactory* xpFactory) {
|
|
|
|
fXPFactory.reset(SkRef(xpFactory));
|
|
|
|
return xpFactory;
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:15:43 +00:00
|
|
|
void setPorterDuffXPFactory(SkXfermode::Mode mode) {
|
|
|
|
fXPFactory.reset(GrPorterDuffXPFactory::Create(mode));
|
|
|
|
}
|
|
|
|
|
2015-02-17 15:34:43 +00:00
|
|
|
void setCoverageSetOpXPFactory(SkRegion::Op regionOp, 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
|
|
|
*/
|
2015-08-27 13:30:17 +00:00
|
|
|
const GrFragmentProcessor* addColorFragmentProcessor(const GrFragmentProcessor* fp) {
|
2014-09-23 16:50:21 +00:00
|
|
|
SkASSERT(fp);
|
2015-08-27 13:30:17 +00:00
|
|
|
fColorFragmentProcessors.push_back(SkRef(fp));
|
2014-09-23 16:50:21 +00:00
|
|
|
return 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
|
|
|
*/
|
2015-08-27 13:30:17 +00:00
|
|
|
const GrFragmentProcessor* addCoverageFragmentProcessor(const GrFragmentProcessor* fp) {
|
2014-09-23 16:50:21 +00:00
|
|
|
SkASSERT(fp);
|
2015-08-27 13:30:17 +00:00
|
|
|
fCoverageFragmentProcessors.push_back(SkRef(fp));
|
2014-09-23 16:50:21 +00:00
|
|
|
return 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.
|
|
|
|
*/
|
2014-09-23 16:50:21 +00:00
|
|
|
void addColorTextureProcessor(GrTexture*, const SkMatrix&);
|
|
|
|
void addCoverageTextureProcessor(GrTexture*, const SkMatrix&);
|
|
|
|
void addColorTextureProcessor(GrTexture*, const SkMatrix&, const GrTextureParams&);
|
|
|
|
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
|
|
|
|
2015-01-22 15:11:44 +00:00
|
|
|
const GrXPFactory* getXPFactory() const {
|
|
|
|
if (!fXPFactory) {
|
|
|
|
fXPFactory.reset(GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode));
|
|
|
|
}
|
|
|
|
return fXPFactory.get();
|
|
|
|
}
|
2014-12-03 18:40:13 +00:00
|
|
|
|
2015-08-27 13:30:17 +00:00
|
|
|
const GrFragmentProcessor* getColorFragmentProcessor(int i) const {
|
|
|
|
return fColorFragmentProcessors[i];
|
|
|
|
}
|
|
|
|
const GrFragmentProcessor* getCoverageFragmentProcessor(int i) const {
|
|
|
|
return fCoverageFragmentProcessors[i];
|
|
|
|
}
|
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;
|
|
|
|
fDither = paint.fDither;
|
|
|
|
|
|
|
|
fColor = paint.fColor;
|
2015-08-27 13:30:17 +00:00
|
|
|
this->resetFragmentProcessors();
|
|
|
|
fColorFragmentProcessors = paint.fColorFragmentProcessors;
|
|
|
|
fCoverageFragmentProcessors = paint.fCoverageFragmentProcessors;
|
|
|
|
for (int i = 0; i < fColorFragmentProcessors.count(); ++i) {
|
|
|
|
fColorFragmentProcessors[i]->ref();
|
|
|
|
}
|
|
|
|
for (int i = 0; i < fCoverageFragmentProcessors.count(); ++i) {
|
|
|
|
fCoverageFragmentProcessors[i]->ref();
|
|
|
|
}
|
2013-07-13 17:24:24 +00:00
|
|
|
|
2014-12-03 18:40:13 +00:00
|
|
|
fXPFactory.reset(SkRef(paint.getXPFactory()));
|
2015-08-26 20:07:48 +00:00
|
|
|
fProcDataManager.reset(new GrProcessorDataManager(*paint.processorDataManager()));
|
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
|
|
|
*/
|
2015-06-02 17:43:39 +00:00
|
|
|
bool isConstantBlendedColor(GrColor* constantColor) const;
|
2013-08-14 21:56:37 +00:00
|
|
|
|
2015-07-10 14:26:21 +00:00
|
|
|
GrProcessorDataManager* getProcessorDataManager() { return fProcDataManager.get(); }
|
|
|
|
|
|
|
|
const GrProcessorDataManager* processorDataManager() const { return fProcDataManager.get(); }
|
2015-07-08 13:51:43 +00:00
|
|
|
|
2014-12-17 23:50:11 +00:00
|
|
|
private:
|
2015-08-27 13:30:17 +00:00
|
|
|
void resetFragmentProcessors() {
|
|
|
|
for (int i = 0; i < fColorFragmentProcessors.count(); ++i) {
|
|
|
|
fColorFragmentProcessors[i]->unref();
|
|
|
|
}
|
|
|
|
for (int i = 0; i < fCoverageFragmentProcessors.count(); ++i) {
|
|
|
|
fCoverageFragmentProcessors[i]->unref();
|
|
|
|
}
|
|
|
|
fColorFragmentProcessors.reset();
|
|
|
|
fCoverageFragmentProcessors.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
mutable SkAutoTUnref<const GrXPFactory> fXPFactory;
|
|
|
|
SkSTArray<4, const GrFragmentProcessor*, true> fColorFragmentProcessors;
|
|
|
|
SkSTArray<2, const GrFragmentProcessor*, true> fCoverageFragmentProcessors;
|
2011-05-17 20:15:30 +00:00
|
|
|
|
2015-08-27 13:30:17 +00:00
|
|
|
bool fAntiAlias;
|
|
|
|
bool fDither;
|
2012-10-05 19:04:13 +00:00
|
|
|
|
2015-08-27 13:30:17 +00:00
|
|
|
GrColor fColor;
|
|
|
|
SkAutoTUnref<GrProcessorDataManager> fProcDataManager;
|
2011-02-22 20:59:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|