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"
|
2015-08-12 16:40:47 +00:00
|
|
|
#include "GrFragmentProcessor.h"
|
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"
|
2017-01-11 18:42:54 +00:00
|
|
|
#include "SkTLazy.h"
|
2011-05-11 14:05:25 +00:00
|
|
|
|
2017-01-24 21:24:41 +00:00
|
|
|
class GrTextureProxy;
|
2017-02-21 21:32:20 +00:00
|
|
|
class GrXPFactory;
|
2017-01-24 21:24:41 +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
|
|
|
*
|
2016-12-09 20:10:07 +00:00
|
|
|
* Fractional pixel coverage follows a similar flow. The GrGeometryProcessor (specified elsewhere)
|
|
|
|
* provides the initial coverage which is passed to the first coverage fragment processor, which
|
|
|
|
* feeds its output to next coverage fragment processor.
|
2012-10-05 19:04:13 +00:00
|
|
|
*
|
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:
|
2017-01-11 18:42:54 +00:00
|
|
|
GrPaint() = default;
|
|
|
|
explicit GrPaint(const GrPaint&) = default;
|
|
|
|
~GrPaint() = default;
|
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
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-01-09 16:46:10 +00:00
|
|
|
void setXPFactory(const GrXPFactory* xpFactory) { fXPFactory = xpFactory; }
|
2014-12-03 18:40:13 +00:00
|
|
|
|
2017-02-21 21:32:20 +00:00
|
|
|
void setPorterDuffXPFactory(SkBlendMode 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&,
|
2016-11-17 20:17:07 +00:00
|
|
|
const GrSamplerParams&);
|
|
|
|
void addCoverageTextureProcessor(GrTexture*, const SkMatrix&, const GrSamplerParams&);
|
2012-07-20 13:37:06 +00:00
|
|
|
|
2017-01-24 21:24:41 +00:00
|
|
|
|
|
|
|
void addColorTextureProcessor(GrContext*, sk_sp<GrTextureProxy>, sk_sp<GrColorSpaceXform>,
|
|
|
|
const SkMatrix&);
|
|
|
|
void addColorTextureProcessor(GrContext*, sk_sp<GrTextureProxy>, sk_sp<GrColorSpaceXform>,
|
|
|
|
const SkMatrix&, const GrSamplerParams&);
|
|
|
|
|
|
|
|
void addCoverageTextureProcessor(GrContext*, sk_sp<GrTextureProxy>, const SkMatrix&);
|
|
|
|
void addCoverageTextureProcessor(GrContext*, sk_sp<GrTextureProxy>,
|
|
|
|
const SkMatrix&, const GrSamplerParams&);
|
|
|
|
|
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
|
|
|
|
2017-01-09 16:46:10 +00:00
|
|
|
const GrXPFactory* getXPFactory() const { return fXPFactory; }
|
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
|
|
|
|
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
|
|
|
*/
|
2017-02-21 21:32:20 +00:00
|
|
|
bool isConstantBlendedColor(GrColor* constantColor) const;
|
2013-08-14 21:56:37 +00:00
|
|
|
|
2014-12-17 23:50:11 +00:00
|
|
|
private:
|
2017-01-11 18:42:54 +00:00
|
|
|
template <bool> class MoveOrImpl;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* A temporary instance of this class can be used to select between moving an existing paint or
|
|
|
|
* a temporary copy of an existing paint into a call site. MoveOrClone(paint, false) is a rvalue
|
|
|
|
* reference to paint while MoveOrClone(paint, true) is a rvalue reference to a copy of paint.
|
|
|
|
*/
|
|
|
|
using MoveOrClone = MoveOrImpl<true>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A temporary instance of this class can be used to select between moving an existing or a
|
|
|
|
* newly default constructed paint into a call site. MoveOrNew(paint, false) is a rvalue
|
|
|
|
* reference to paint while MoveOrNew(paint, true) is a rvalue reference to a default paint.
|
|
|
|
*/
|
|
|
|
using MoveOrNew = MoveOrImpl<false>;
|
|
|
|
|
|
|
|
private:
|
|
|
|
GrPaint& operator=(const GrPaint&) = delete;
|
|
|
|
|
2017-01-18 16:01:10 +00:00
|
|
|
friend class GrProcessorSet;
|
2017-01-11 18:42:54 +00:00
|
|
|
|
|
|
|
const GrXPFactory* fXPFactory = nullptr;
|
2016-06-09 15:01:03 +00:00
|
|
|
SkSTArray<4, sk_sp<GrFragmentProcessor>> fColorFragmentProcessors;
|
|
|
|
SkSTArray<2, sk_sp<GrFragmentProcessor>> fCoverageFragmentProcessors;
|
2017-01-11 18:42:54 +00:00
|
|
|
bool fDisableOutputConversionToSRGB = false;
|
|
|
|
bool fAllowSRGBInputs = false;
|
|
|
|
bool fUsesDistanceVectorField = false;
|
|
|
|
GrColor4f fColor = GrColor4f::OpaqueWhite();
|
|
|
|
};
|
2011-05-17 20:15:30 +00:00
|
|
|
|
2017-01-11 18:42:54 +00:00
|
|
|
/** This is the implementation of MoveOrCopy and MoveOrNew. */
|
|
|
|
template <bool COPY_IF_NEW>
|
|
|
|
class GrPaint::MoveOrImpl {
|
|
|
|
public:
|
|
|
|
MoveOrImpl(GrPaint& paint, bool newPaint) {
|
|
|
|
if (newPaint) {
|
|
|
|
if (COPY_IF_NEW) {
|
|
|
|
fStorage.init(paint);
|
|
|
|
} else {
|
|
|
|
fStorage.init();
|
|
|
|
};
|
|
|
|
fPaint = fStorage.get();
|
|
|
|
} else {
|
|
|
|
fPaint = &paint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-13 17:11:36 +00:00
|
|
|
operator GrPaint&&() && { return std::move(*fPaint); }
|
|
|
|
GrPaint& paint() { return *fPaint; }
|
2012-10-05 19:04:13 +00:00
|
|
|
|
2017-01-11 18:42:54 +00:00
|
|
|
private:
|
|
|
|
SkTLazy<GrPaint> fStorage;
|
|
|
|
GrPaint* fPaint;
|
2011-02-22 20:59:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|