michael@0: michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #ifndef GrPaint_DEFINED michael@0: #define GrPaint_DEFINED michael@0: michael@0: #include "GrColor.h" michael@0: #include "GrEffectStage.h" michael@0: michael@0: #include "SkXfermode.h" michael@0: michael@0: /** michael@0: * The paint describes how color and coverage are computed at each pixel by GrContext draw michael@0: * functions and the how color is blended with the destination pixel. michael@0: * michael@0: * The paint allows installation of custom color and coverage stages. New types of stages are michael@0: * created by subclassing GrEffect. michael@0: * michael@0: * The primitive color computation starts with the color specified by setColor(). This color is the michael@0: * input to the first color stage. Each color stage feeds its output to the next color stage. The michael@0: * final color stage's output color is input to the color filter specified by michael@0: * setXfermodeColorFilter which produces the final source color, S. michael@0: * michael@0: * Fractional pixel coverage follows a similar flow. The coverage is initially the value specified michael@0: * by setCoverage(). This is input to the first coverage stage. Coverage stages are chained michael@0: * together in the same manner as color stages. The output of the last stage is modulated by any michael@0: * fractional coverage produced by anti-aliasing. This last step produces the final coverage, C. michael@0: * michael@0: * setBlendFunc() specifies blending coefficients for S (described above) and D, the initial value michael@0: * of the destination pixel, labeled Bs and Bd respectively. The final value of the destination michael@0: * pixel is then D' = (1-C)*D + C*(Bd*D + Bs*S). michael@0: * michael@0: * Note that the coverage is applied after the blend. This is why they are computed as distinct michael@0: * values. michael@0: * michael@0: * TODO: Encapsulate setXfermodeColorFilter in a GrEffect and remove from GrPaint. michael@0: */ michael@0: class GrPaint { michael@0: public: michael@0: GrPaint() { this->reset(); } michael@0: michael@0: GrPaint(const GrPaint& paint) { *this = paint; } michael@0: michael@0: ~GrPaint() {} michael@0: michael@0: /** michael@0: * Sets the blending coefficients to use to blend the final primitive color with the michael@0: * destination color. Defaults to kOne for src and kZero for dst (i.e. src mode). michael@0: */ michael@0: void setBlendFunc(GrBlendCoeff srcCoeff, GrBlendCoeff dstCoeff) { michael@0: fSrcBlendCoeff = srcCoeff; michael@0: fDstBlendCoeff = dstCoeff; michael@0: } michael@0: GrBlendCoeff getSrcBlendCoeff() const { return fSrcBlendCoeff; } michael@0: GrBlendCoeff getDstBlendCoeff() const { return fDstBlendCoeff; } michael@0: michael@0: /** michael@0: * The initial color of the drawn primitive. Defaults to solid white. michael@0: */ michael@0: void setColor(GrColor color) { fColor = color; } michael@0: GrColor getColor() const { return fColor; } michael@0: michael@0: /** michael@0: * Applies fractional coverage to the entire drawn primitive. Defaults to 0xff. michael@0: */ michael@0: void setCoverage(uint8_t coverage) { fCoverage = coverage; } michael@0: uint8_t getCoverage() const { return fCoverage; } michael@0: michael@0: /** michael@0: * Should primitives be anti-aliased or not. Defaults to false. michael@0: */ michael@0: void setAntiAlias(bool aa) { fAntiAlias = aa; } michael@0: bool isAntiAlias() const { return fAntiAlias; } michael@0: michael@0: /** michael@0: * Should dithering be applied. Defaults to false. michael@0: */ michael@0: void setDither(bool dither) { fDither = dither; } michael@0: bool isDither() const { return fDither; } michael@0: michael@0: /** michael@0: * Appends an additional color effect to the color computation. michael@0: */ michael@0: const GrEffectRef* addColorEffect(const GrEffectRef* effect, int attr0 = -1, int attr1 = -1) { michael@0: SkASSERT(NULL != effect); michael@0: if (!(*effect)->willUseInputColor()) { michael@0: fColorStages.reset(); michael@0: } michael@0: SkNEW_APPEND_TO_TARRAY(&fColorStages, GrEffectStage, (effect, attr0, attr1)); michael@0: return effect; michael@0: } michael@0: michael@0: /** michael@0: * Appends an additional coverage effect to the coverage computation. michael@0: */ michael@0: const GrEffectRef* addCoverageEffect(const GrEffectRef* effect, int attr0 = -1, int attr1 = -1) { michael@0: SkASSERT(NULL != effect); michael@0: if (!(*effect)->willUseInputColor()) { michael@0: fCoverageStages.reset(); michael@0: } michael@0: SkNEW_APPEND_TO_TARRAY(&fCoverageStages, GrEffectStage, (effect, attr0, attr1)); michael@0: return effect; michael@0: } michael@0: michael@0: /** michael@0: * Helpers for adding color or coverage effects that sample a texture. The matrix is applied michael@0: * to the src space position to compute texture coordinates. michael@0: */ michael@0: void addColorTextureEffect(GrTexture* texture, const SkMatrix& matrix); michael@0: void addCoverageTextureEffect(GrTexture* texture, const SkMatrix& matrix); michael@0: michael@0: void addColorTextureEffect(GrTexture* texture, michael@0: const SkMatrix& matrix, michael@0: const GrTextureParams& params); michael@0: void addCoverageTextureEffect(GrTexture* texture, michael@0: const SkMatrix& matrix, michael@0: const GrTextureParams& params); michael@0: michael@0: int numColorStages() const { return fColorStages.count(); } michael@0: int numCoverageStages() const { return fCoverageStages.count(); } michael@0: int numTotalStages() const { return this->numColorStages() + this->numCoverageStages(); } michael@0: michael@0: const GrEffectStage& getColorStage(int s) const { return fColorStages[s]; } michael@0: const GrEffectStage& getCoverageStage(int s) const { return fCoverageStages[s]; } michael@0: michael@0: GrPaint& operator=(const GrPaint& paint) { michael@0: fSrcBlendCoeff = paint.fSrcBlendCoeff; michael@0: fDstBlendCoeff = paint.fDstBlendCoeff; michael@0: fAntiAlias = paint.fAntiAlias; michael@0: fDither = paint.fDither; michael@0: michael@0: fColor = paint.fColor; michael@0: fCoverage = paint.fCoverage; michael@0: michael@0: fColorStages = paint.fColorStages; michael@0: fCoverageStages = paint.fCoverageStages; michael@0: michael@0: return *this; michael@0: } michael@0: michael@0: /** michael@0: * Resets the paint to the defaults. michael@0: */ michael@0: void reset() { michael@0: this->resetBlend(); michael@0: this->resetOptions(); michael@0: this->resetColor(); michael@0: this->resetCoverage(); michael@0: this->resetStages(); michael@0: } michael@0: michael@0: /** michael@0: * Determines whether the drawing with this paint is opaque with respect to both color blending michael@0: * and fractional coverage. It does not consider whether AA has been enabled on the paint or michael@0: * not. Depending upon whether multisampling or coverage-based AA is in use, AA may make the michael@0: * result only apply to the interior of primitives. michael@0: * michael@0: */ michael@0: bool isOpaque() const; michael@0: michael@0: /** michael@0: * Returns true if isOpaque would return true and the paint represents a solid constant color michael@0: * draw. If the result is true, constantColor will be updated to contain the constant color. michael@0: */ michael@0: bool isOpaqueAndConstantColor(GrColor* constantColor) const; michael@0: michael@0: private: michael@0: michael@0: /** michael@0: * Helper for isOpaque and isOpaqueAndConstantColor. michael@0: */ michael@0: bool getOpaqueAndKnownColor(GrColor* solidColor, uint32_t* solidColorKnownComponents) const; michael@0: michael@0: /** michael@0: * Called when the source coord system from which geometry is rendered changes. It ensures that michael@0: * the local coordinates seen by effects remains unchanged. oldToNew gives the transformation michael@0: * from the previous coord system to the new coord system. michael@0: */ michael@0: void localCoordChange(const SkMatrix& oldToNew) { michael@0: for (int i = 0; i < fColorStages.count(); ++i) { michael@0: fColorStages[i].localCoordChange(oldToNew); michael@0: } michael@0: for (int i = 0; i < fCoverageStages.count(); ++i) { michael@0: fCoverageStages[i].localCoordChange(oldToNew); michael@0: } michael@0: } michael@0: michael@0: bool localCoordChangeInverse(const SkMatrix& newToOld) { michael@0: SkMatrix oldToNew; michael@0: bool computed = false; michael@0: for (int i = 0; i < fColorStages.count(); ++i) { michael@0: if (!computed && !newToOld.invert(&oldToNew)) { michael@0: return false; michael@0: } else { michael@0: computed = true; michael@0: } michael@0: fColorStages[i].localCoordChange(oldToNew); michael@0: } michael@0: for (int i = 0; i < fCoverageStages.count(); ++i) { michael@0: if (!computed && !newToOld.invert(&oldToNew)) { michael@0: return false; michael@0: } else { michael@0: computed = true; michael@0: } michael@0: fCoverageStages[i].localCoordChange(oldToNew); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: friend class GrContext; // To access above two functions michael@0: michael@0: SkSTArray<4, GrEffectStage> fColorStages; michael@0: SkSTArray<2, GrEffectStage> fCoverageStages; michael@0: michael@0: GrBlendCoeff fSrcBlendCoeff; michael@0: GrBlendCoeff fDstBlendCoeff; michael@0: bool fAntiAlias; michael@0: bool fDither; michael@0: michael@0: GrColor fColor; michael@0: uint8_t fCoverage; michael@0: michael@0: void resetBlend() { michael@0: fSrcBlendCoeff = kOne_GrBlendCoeff; michael@0: fDstBlendCoeff = kZero_GrBlendCoeff; michael@0: } michael@0: michael@0: void resetOptions() { michael@0: fAntiAlias = false; michael@0: fDither = false; michael@0: } michael@0: michael@0: void resetColor() { michael@0: fColor = GrColorPackRGBA(0xff, 0xff, 0xff, 0xff); michael@0: } michael@0: michael@0: void resetCoverage() { michael@0: fCoverage = 0xff; michael@0: } michael@0: michael@0: void resetStages() { michael@0: fColorStages.reset(); michael@0: fCoverageStages.reset(); michael@0: } michael@0: }; michael@0: michael@0: #endif