michael@0: /* michael@0: * Copyright 2012 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: #include "GrConfigConversionEffect.h" michael@0: #include "GrContext.h" michael@0: #include "GrTBackendEffectFactory.h" michael@0: #include "GrSimpleTextureEffect.h" michael@0: #include "gl/GrGLEffect.h" michael@0: #include "SkMatrix.h" michael@0: michael@0: class GrGLConfigConversionEffect : public GrGLEffect { michael@0: public: michael@0: GrGLConfigConversionEffect(const GrBackendEffectFactory& factory, michael@0: const GrDrawEffect& drawEffect) michael@0: : INHERITED (factory) { michael@0: const GrConfigConversionEffect& effect = drawEffect.castEffect(); michael@0: fSwapRedAndBlue = effect.swapsRedAndBlue(); michael@0: fPMConversion = effect.pmConversion(); michael@0: } michael@0: michael@0: virtual void emitCode(GrGLShaderBuilder* builder, michael@0: const GrDrawEffect&, michael@0: EffectKey key, michael@0: const char* outputColor, michael@0: const char* inputColor, michael@0: const TransformedCoordsArray& coords, michael@0: const TextureSamplerArray& samplers) SK_OVERRIDE { michael@0: builder->fsCodeAppendf("\t\t%s = ", outputColor); michael@0: builder->fsAppendTextureLookup(samplers[0], coords[0].c_str(), coords[0].type()); michael@0: builder->fsCodeAppend(";\n"); michael@0: if (GrConfigConversionEffect::kNone_PMConversion == fPMConversion) { michael@0: SkASSERT(fSwapRedAndBlue); michael@0: builder->fsCodeAppendf("\t%s = %s.bgra;\n", outputColor, outputColor); michael@0: } else { michael@0: const char* swiz = fSwapRedAndBlue ? "bgr" : "rgb"; michael@0: switch (fPMConversion) { michael@0: case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion: michael@0: builder->fsCodeAppendf( michael@0: "\t\t%s = vec4(ceil(%s.%s * %s.a * 255.0) / 255.0, %s.a);\n", michael@0: outputColor, outputColor, swiz, outputColor, outputColor); michael@0: break; michael@0: case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion: michael@0: // Add a compensation(0.001) here to avoid the side effect of the floor operation. michael@0: // In Intel GPUs, the integer value converted from floor(%s.r * 255.0) / 255.0 michael@0: // is less than the integer value converted from %s.r by 1 when the %s.r is michael@0: // converted from the integer value 2^n, such as 1, 2, 4, 8, etc. michael@0: builder->fsCodeAppendf( michael@0: "\t\t%s = vec4(floor(%s.%s * %s.a * 255.0 + 0.001) / 255.0, %s.a);\n", michael@0: outputColor, outputColor, swiz, outputColor, outputColor); michael@0: break; michael@0: case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion: michael@0: builder->fsCodeAppendf("\t\t%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(ceil(%s.%s / %s.a * 255.0) / 255.0, %s.a);\n", michael@0: outputColor, outputColor, outputColor, swiz, outputColor, outputColor); michael@0: break; michael@0: case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion: michael@0: builder->fsCodeAppendf("\t\t%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(floor(%s.%s / %s.a * 255.0) / 255.0, %s.a);\n", michael@0: outputColor, outputColor, outputColor, swiz, outputColor, outputColor); michael@0: break; michael@0: default: michael@0: GrCrash("Unknown conversion op."); michael@0: break; michael@0: } michael@0: } michael@0: SkString modulate; michael@0: GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); michael@0: builder->fsCodeAppend(modulate.c_str()); michael@0: } michael@0: michael@0: static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { michael@0: const GrConfigConversionEffect& conv = drawEffect.castEffect(); michael@0: return static_cast(conv.swapsRedAndBlue()) | (conv.pmConversion() << 1); michael@0: } michael@0: michael@0: private: michael@0: bool fSwapRedAndBlue; michael@0: GrConfigConversionEffect::PMConversion fPMConversion; michael@0: michael@0: typedef GrGLEffect INHERITED; michael@0: michael@0: }; michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture, michael@0: bool swapRedAndBlue, michael@0: PMConversion pmConversion, michael@0: const SkMatrix& matrix) michael@0: : GrSingleTextureEffect(texture, matrix) michael@0: , fSwapRedAndBlue(swapRedAndBlue) michael@0: , fPMConversion(pmConversion) { michael@0: SkASSERT(kRGBA_8888_GrPixelConfig == texture->config() || michael@0: kBGRA_8888_GrPixelConfig == texture->config()); michael@0: // Why did we pollute our texture cache instead of using a GrSingleTextureEffect? michael@0: SkASSERT(swapRedAndBlue || kNone_PMConversion != pmConversion); michael@0: } michael@0: michael@0: const GrBackendEffectFactory& GrConfigConversionEffect::getFactory() const { michael@0: return GrTBackendEffectFactory::getInstance(); michael@0: } michael@0: michael@0: bool GrConfigConversionEffect::onIsEqual(const GrEffect& s) const { michael@0: const GrConfigConversionEffect& other = CastEffect(s); michael@0: return this->texture(0) == s.texture(0) && michael@0: other.fSwapRedAndBlue == fSwapRedAndBlue && michael@0: other.fPMConversion == fPMConversion; michael@0: } michael@0: michael@0: void GrConfigConversionEffect::getConstantColorComponents(GrColor* color, michael@0: uint32_t* validFlags) const { michael@0: this->updateConstantColorComponentsForModulation(color, validFlags); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: GR_DEFINE_EFFECT_TEST(GrConfigConversionEffect); michael@0: michael@0: GrEffectRef* GrConfigConversionEffect::TestCreate(SkRandom* random, michael@0: GrContext*, michael@0: const GrDrawTargetCaps&, michael@0: GrTexture* textures[]) { michael@0: PMConversion pmConv = static_cast(random->nextULessThan(kPMConversionCnt)); michael@0: bool swapRB; michael@0: if (kNone_PMConversion == pmConv) { michael@0: swapRB = true; michael@0: } else { michael@0: swapRB = random->nextBool(); michael@0: } michael@0: AutoEffectUnref effect(SkNEW_ARGS(GrConfigConversionEffect, michael@0: (textures[GrEffectUnitTest::kSkiaPMTextureIdx], michael@0: swapRB, michael@0: pmConv, michael@0: GrEffectUnitTest::TestMatrix(random)))); michael@0: return CreateEffectRef(effect); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context, michael@0: PMConversion* pmToUPMRule, michael@0: PMConversion* upmToPMRule) { michael@0: *pmToUPMRule = kNone_PMConversion; michael@0: *upmToPMRule = kNone_PMConversion; michael@0: SkAutoTMalloc data(256 * 256 * 3); michael@0: uint32_t* srcData = data.get(); michael@0: uint32_t* firstRead = data.get() + 256 * 256; michael@0: uint32_t* secondRead = data.get() + 2 * 256 * 256; michael@0: michael@0: // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate michael@0: // values in row y. We set r,g, and b to the same value since they are handled identically. michael@0: for (int y = 0; y < 256; ++y) { michael@0: for (int x = 0; x < 256; ++x) { michael@0: uint8_t* color = reinterpret_cast(&srcData[256*y + x]); michael@0: color[3] = y; michael@0: color[2] = GrMin(x, y); michael@0: color[1] = GrMin(x, y); michael@0: color[0] = GrMin(x, y); michael@0: } michael@0: } michael@0: michael@0: GrTextureDesc desc; michael@0: desc.fFlags = kRenderTarget_GrTextureFlagBit | michael@0: kNoStencil_GrTextureFlagBit; michael@0: desc.fWidth = 256; michael@0: desc.fHeight = 256; michael@0: desc.fConfig = kRGBA_8888_GrPixelConfig; michael@0: michael@0: SkAutoTUnref readTex(context->createUncachedTexture(desc, NULL, 0)); michael@0: if (!readTex.get()) { michael@0: return; michael@0: } michael@0: SkAutoTUnref tempTex(context->createUncachedTexture(desc, NULL, 0)); michael@0: if (!tempTex.get()) { michael@0: return; michael@0: } michael@0: desc.fFlags = kNone_GrTextureFlags; michael@0: SkAutoTUnref dataTex(context->createUncachedTexture(desc, data, 0)); michael@0: if (!dataTex.get()) { michael@0: return; michael@0: } michael@0: michael@0: static const PMConversion kConversionRules[][2] = { michael@0: {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion}, michael@0: {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion}, michael@0: }; michael@0: michael@0: GrContext::AutoWideOpenIdentityDraw awoid(context, NULL); michael@0: michael@0: bool failed = true; michael@0: michael@0: for (size_t i = 0; i < GR_ARRAY_COUNT(kConversionRules) && failed; ++i) { michael@0: *pmToUPMRule = kConversionRules[i][0]; michael@0: *upmToPMRule = kConversionRules[i][1]; michael@0: michael@0: static const SkRect kDstRect = SkRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256)); michael@0: static const SkRect kSrcRect = SkRect::MakeWH(SK_Scalar1, SK_Scalar1); michael@0: // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw michael@0: // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data. michael@0: // We then verify that two reads produced the same values. michael@0: michael@0: AutoEffectUnref pmToUPM1(SkNEW_ARGS(GrConfigConversionEffect, (dataTex, michael@0: false, michael@0: *pmToUPMRule, michael@0: SkMatrix::I()))); michael@0: AutoEffectUnref upmToPM(SkNEW_ARGS(GrConfigConversionEffect, (readTex, michael@0: false, michael@0: *upmToPMRule, michael@0: SkMatrix::I()))); michael@0: AutoEffectUnref pmToUPM2(SkNEW_ARGS(GrConfigConversionEffect, (tempTex, michael@0: false, michael@0: *pmToUPMRule, michael@0: SkMatrix::I()))); michael@0: michael@0: SkAutoTUnref pmToUPMEffect1(CreateEffectRef(pmToUPM1)); michael@0: SkAutoTUnref upmToPMEffect(CreateEffectRef(upmToPM)); michael@0: SkAutoTUnref pmToUPMEffect2(CreateEffectRef(pmToUPM2)); michael@0: michael@0: context->setRenderTarget(readTex->asRenderTarget()); michael@0: GrPaint paint1; michael@0: paint1.addColorEffect(pmToUPMEffect1); michael@0: context->drawRectToRect(paint1, kDstRect, kSrcRect); michael@0: michael@0: readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead); michael@0: michael@0: context->setRenderTarget(tempTex->asRenderTarget()); michael@0: GrPaint paint2; michael@0: paint2.addColorEffect(upmToPMEffect); michael@0: context->drawRectToRect(paint2, kDstRect, kSrcRect); michael@0: context->setRenderTarget(readTex->asRenderTarget()); michael@0: michael@0: GrPaint paint3; michael@0: paint3.addColorEffect(pmToUPMEffect2); michael@0: context->drawRectToRect(paint3, kDstRect, kSrcRect); michael@0: michael@0: readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead); michael@0: michael@0: failed = false; michael@0: for (int y = 0; y < 256 && !failed; ++y) { michael@0: for (int x = 0; x <= y; ++x) { michael@0: if (firstRead[256 * y + x] != secondRead[256 * y + x]) { michael@0: failed = true; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: if (failed) { michael@0: *pmToUPMRule = kNone_PMConversion; michael@0: *upmToPMRule = kNone_PMConversion; michael@0: } michael@0: } michael@0: michael@0: const GrEffectRef* GrConfigConversionEffect::Create(GrTexture* texture, michael@0: bool swapRedAndBlue, michael@0: PMConversion pmConversion, michael@0: const SkMatrix& matrix) { michael@0: if (!swapRedAndBlue && kNone_PMConversion == pmConversion) { michael@0: // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect michael@0: // then we may pollute our texture cache with redundant shaders. So in the case that no michael@0: // conversions were requested we instead return a GrSimpleTextureEffect. michael@0: return GrSimpleTextureEffect::Create(texture, matrix); michael@0: } else { michael@0: if (kRGBA_8888_GrPixelConfig != texture->config() && michael@0: kBGRA_8888_GrPixelConfig != texture->config() && michael@0: kNone_PMConversion != pmConversion) { michael@0: // The PM conversions assume colors are 0..255 michael@0: return NULL; michael@0: } michael@0: AutoEffectUnref effect(SkNEW_ARGS(GrConfigConversionEffect, (texture, michael@0: swapRedAndBlue, michael@0: pmConversion, michael@0: matrix))); michael@0: return CreateEffectRef(effect); michael@0: } michael@0: }