bool DCShader::asFragmentProcessor(GrContext*, const SkPaint& paint, const SkMatrix& viewM, const SkMatrix* localMatrix, GrColor* color, GrFragmentProcessor** fp) const { *fp = SkNEW_ARGS(DCFP, (fDeviceMatrix)); *color = GrColorPackA4(paint.getAlpha()); return true; }
void GrExtractAlphaFragmentProcessor::onComputeInvariantOutput(GrInvariantOutput* inout) const { if (inout->validFlags() & kA_GrColorComponentFlag) { GrColor color = GrColorPackA4(GrColorUnpackA(inout->color())); inout->setToOther(kRGBA_GrColorComponentFlags, color, GrInvariantOutput::kWill_ReadInput); } else { inout->setToUnknown(GrInvariantOutput::kWill_ReadInput); } this->childProcessor(0).computeInvariantOutput(inout); }
static void test_getConstantColorComponents(skiatest::Reporter* reporter, GrContext* grContext) { struct GetConstantComponentTestCase { // "Shape drawn with" uint32_t inputComponents; // "rgb of", "red of", "alpha of", ... GrColor inputColor; // "[color]" SkColor filterColor; // "with filter color [color]" SkXfermode::Mode filterMode; // "in mode [mode]" // "produces" uint32_t outputComponents; // "rgb of", "red of", "alpha of", ... GrColor outputColor; // "[color]" }; // Shorthands. enum { kR = kR_GrColorComponentFlag, kG = kG_GrColorComponentFlag, kB = kB_GrColorComponentFlag, kA = kA_GrColorComponentFlag, kRGB = kRGB_GrColorComponentFlags, kRGBA = kRGBA_GrColorComponentFlags }; // Note: below, SkColors are non-premultiplied, where as GrColors are premultiplied. const SkColor c1 = SkColorSetARGB(200, 200, 200, 200); const SkColor c2 = SkColorSetARGB(60, 60, 60, 60); const GrColor gr_c1 = SkColor2GrColor(c1); const GrColor gr_c2 = SkColor2GrColor(c2); const GrColor gr_black = GrColorPackA4(0); const GrColor gr_white = GrColorPackA4(255); const GrColor gr_whiteTrans = GrColorPackA4(128); GetConstantComponentTestCase filterTests[] = { // A color filtered with Clear produces black. { kRGBA, gr_white, SK_ColorBLACK, SkXfermode::kClear_Mode, kRGBA, gr_black }, { kRGBA, gr_c1, SK_ColorWHITE, SkXfermode::kClear_Mode, kRGBA, gr_black }, { kR, gr_white, c1, SkXfermode::kClear_Mode, kRGBA, gr_black }, // A color filtered with a color in mode Src, produces the filter color. { kRGBA, gr_c2, c1, SkXfermode::kSrc_Mode, kRGBA, gr_c1 }, { kA, gr_c1, c1, SkXfermode::kSrc_Mode, kRGBA, gr_c1 }, // A color filtered with SrcOver produces a color. { kRGBA, gr_whiteTrans, SkColorSetARGB(128, 200, 200, 200), SkXfermode::kSrcOver_Mode, kRGBA, GrColorPackRGBA(164, 164, 164, 192)}, // An unknown color with known alpha filtered with SrcOver produces an unknown color with known alpha. { kA , gr_whiteTrans, SkColorSetARGB(128, 200, 200, 200), SkXfermode::kSrcOver_Mode, kA , GrColorPackRGBA(0, 0, 0, 192)}, // A color with unknown alpha filtered with SrcOver produces a color with unknown alpha. { kRGB , gr_whiteTrans, SkColorSetARGB(128, 200, 200, 200), SkXfermode::kSrcOver_Mode, kRGB, GrColorPackRGBA(164, 164, 164, 0)}, // A color filtered with DstOver produces a color. { kRGBA, gr_whiteTrans, SkColorSetARGB(128, 200, 200, 200), SkXfermode::kDstOver_Mode, kRGBA, GrColorPackRGBA(178, 178, 178, 192)}, // An unknown color with known alpha filtered with DstOver produces an unknown color with known alpha. { kA , gr_whiteTrans, SkColorSetARGB(128, 200, 200, 200), SkXfermode::kDstOver_Mode, kA , GrColorPackRGBA(0, 0, 0, 192)}, // A color with unknown alpha filtered with DstOver produces an unknown color. { kRGB , gr_whiteTrans, SkColorSetARGB(128, 200, 200, 200), SkXfermode::kDstOver_Mode, 0 , gr_black}, // An unknown color with known alpha and red component filtered with Multiply produces an unknown color with known red and alpha. { kR|kA , gr_whiteTrans, SkColorSetARGB(128, 200, 200, 200), SkXfermode::kModulate_Mode, kR|kA, GrColorPackRGBA(50, 0, 0, 64) } }; GrPaint paint; for (size_t i = 0; i < SK_ARRAY_COUNT(filterTests); ++i) { const GetConstantComponentTestCase& test = filterTests[i]; SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(test.filterColor, test.filterMode)); SkTDArray<const GrFragmentProcessor*> array; bool hasFrag = cf->asFragmentProcessors(grContext, paint.getProcessorDataManager(), &array); REPORTER_ASSERT(reporter, hasFrag); REPORTER_ASSERT(reporter, 1 == array.count()); GrInvariantOutput inout(test.inputColor, static_cast<GrColorComponentFlags>(test.inputComponents), false); array[0]->computeInvariantOutput(&inout); REPORTER_ASSERT(reporter, filterColor(inout.color(), inout.validFlags()) == test.outputColor); REPORTER_ASSERT(reporter, test.outputComponents == inout.validFlags()); array[0]->unref(); } }
bool LightingShader::asFragmentProcessor(GrContext* context, const SkPaint& paint, const SkMatrix& viewM, const SkMatrix* localMatrix, GrColor* color, GrProcessorDataManager*, GrFragmentProcessor** fp) const { // we assume diffuse and normal maps have same width and height // TODO: support different sizes SkASSERT(fDiffuseMap.width() == fNormalMap.width() && fDiffuseMap.height() == fNormalMap.height()); SkMatrix matrix; matrix.setIDiv(fDiffuseMap.width(), fDiffuseMap.height()); SkMatrix lmInverse; if (!this->getLocalMatrix().invert(&lmInverse)) { return false; } if (localMatrix) { SkMatrix inv; if (!localMatrix->invert(&inv)) { return false; } lmInverse.postConcat(inv); } matrix.preConcat(lmInverse); // Must set wrap and filter on the sampler before requesting a texture. In two places below // we check the matrix scale factors to determine how to interpret the filter quality setting. // This completely ignores the complexity of the drawVertices case where explicit local coords // are provided by the caller. GrTextureParams::FilterMode textureFilterMode = GrTextureParams::kBilerp_FilterMode; switch (paint.getFilterQuality()) { case kNone_SkFilterQuality: textureFilterMode = GrTextureParams::kNone_FilterMode; break; case kLow_SkFilterQuality: textureFilterMode = GrTextureParams::kBilerp_FilterMode; break; case kMedium_SkFilterQuality:{ SkMatrix matrix; matrix.setConcat(viewM, this->getLocalMatrix()); if (matrix.getMinScale() < SK_Scalar1) { textureFilterMode = GrTextureParams::kMipMap_FilterMode; } else { // Don't trigger MIP level generation unnecessarily. textureFilterMode = GrTextureParams::kBilerp_FilterMode; } break; } case kHigh_SkFilterQuality: default: SkErrorInternals::SetError(kInvalidPaint_SkError, "Sorry, I don't understand the filtering " "mode you asked for. Falling back to " "MIPMaps."); textureFilterMode = GrTextureParams::kMipMap_FilterMode; break; } // TODO: support other tile modes GrTextureParams params(kClamp_TileMode, textureFilterMode); SkAutoTUnref<GrTexture> diffuseTexture(GrRefCachedBitmapTexture(context, fDiffuseMap, ¶ms)); if (!diffuseTexture) { SkErrorInternals::SetError(kInternalError_SkError, "Couldn't convert bitmap to texture."); return false; } SkAutoTUnref<GrTexture> normalTexture(GrRefCachedBitmapTexture(context, fNormalMap, ¶ms)); if (!normalTexture) { SkErrorInternals::SetError(kInternalError_SkError, "Couldn't convert bitmap to texture."); return false; } GrColor lightColor = GrColorPackRGBA(SkColorGetR(fLight.fColor), SkColorGetG(fLight.fColor), SkColorGetB(fLight.fColor), SkColorGetA(fLight.fColor)); GrColor ambientColor = GrColorPackRGBA(SkColorGetR(fAmbientColor), SkColorGetG(fAmbientColor), SkColorGetB(fAmbientColor), SkColorGetA(fAmbientColor)); *fp = SkNEW_ARGS(LightingFP, (diffuseTexture, normalTexture, matrix, fLight.fDirection, lightColor, ambientColor)); *color = GrColorPackA4(paint.getAlpha()); return true; }
static MaskedColor ExtractInverseAlpha(const MaskedColor& in) { GrColorComponentFlags flags = (in.fFlags & kA_GrColorComponentFlag) ? kRGBA_GrColorComponentFlags : kNone_GrColorComponentFlags; return MaskedColor(GrColorPackA4(0xFF - GrColorUnpackA(in.fColor)), flags); }