bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GrConfigConversionEffect.h" |
| 9 | #include "gl/GrGLProgramStage.h" |
| 10 | |
| 11 | class GrGLConfigConversionEffect : public GrGLProgramStage { |
| 12 | public: |
| 13 | GrGLConfigConversionEffect(const GrProgramStageFactory& factory, |
| 14 | const GrCustomStage& s) : INHERITED (factory) { |
| 15 | const GrConfigConversionEffect& stage = static_cast<const GrConfigConversionEffect&>(s); |
| 16 | fSwapRedAndBlue = stage.swapsRedAndBlue(); |
| 17 | fPMConversion = stage.pmConversion(); |
| 18 | } |
| 19 | |
| 20 | virtual void emitVS(GrGLShaderBuilder* builder, |
| 21 | const char* vertexCoords) SK_OVERRIDE { } |
| 22 | virtual void emitFS(GrGLShaderBuilder* builder, |
| 23 | const char* outputColor, |
| 24 | const char* inputColor, |
| 25 | const char* samplerName) SK_OVERRIDE { |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame^] | 26 | builder->fFSCode.appendf("\t\t%s = ", outputColor); |
| 27 | builder->appendTextureLookup(&builder->fFSCode, samplerName); |
| 28 | builder->fFSCode.appendf("%s;\n", builder->fSwizzle.c_str()); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 29 | if (GrConfigConversionEffect::kNone_PMConversion == fPMConversion) { |
| 30 | GrAssert(fSwapRedAndBlue); |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame^] | 31 | builder->fFSCode.appendf("\t%s = %s.bgra;\n", outputColor, outputColor); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 32 | } else { |
| 33 | const char* swiz = fSwapRedAndBlue ? "bgr" : "rgb"; |
| 34 | switch (fPMConversion) { |
| 35 | case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion: |
| 36 | builder->fFSCode.appendf( |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame^] | 37 | "\t\t%s = vec4(ceil(%s.%s * %s.a * 255.0) / 255.0, %s.a);\n", |
| 38 | outputColor, outputColor, swiz, outputColor, outputColor); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 39 | break; |
| 40 | case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion: |
| 41 | builder->fFSCode.appendf( |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame^] | 42 | "\t\t%s = vec4(floor(%s.%s * %s.a * 255.0) / 255.0, %s.a);\n", |
| 43 | outputColor, outputColor, swiz, outputColor, outputColor); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 44 | break; |
| 45 | case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion: |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame^] | 46 | builder->fFSCode.appendf("\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", |
| 47 | outputColor, outputColor, outputColor, swiz, outputColor, outputColor); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 48 | break; |
| 49 | case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion: |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame^] | 50 | builder->fFSCode.appendf("\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", |
| 51 | outputColor, outputColor, outputColor, swiz, outputColor, outputColor); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 52 | break; |
robertphillips@google.com | 2af1b18 | 2012-08-28 11:23:09 +0000 | [diff] [blame] | 53 | default: |
| 54 | GrCrash("Unknown conversion op."); |
| 55 | break; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 56 | } |
| 57 | } |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame^] | 58 | GrGLSLMulVarBy4f(&builder->fFSCode, 2, outputColor, inputColor); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static inline StageKey GenKey(const GrCustomStage& s, const GrGLCaps&) { |
| 62 | const GrConfigConversionEffect& stage = static_cast<const GrConfigConversionEffect&>(s); |
| 63 | return static_cast<int>(stage.swapsRedAndBlue()) | (stage.pmConversion() << 1); |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | bool fSwapRedAndBlue; |
| 68 | GrConfigConversionEffect::PMConversion fPMConversion; |
| 69 | |
| 70 | typedef GrGLProgramStage INHERITED; |
| 71 | |
| 72 | }; |
| 73 | |
| 74 | /////////////////////////////////////////////////////////////////////////////// |
| 75 | |
| 76 | GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture, |
| 77 | bool swapRedAndBlue, |
| 78 | PMConversion pmConversion) |
| 79 | : GrSingleTextureEffect(texture) |
| 80 | , fSwapRedAndBlue(swapRedAndBlue) |
| 81 | , fPMConversion(pmConversion) { |
| 82 | GrAssert(kRGBA_8888_GrPixelConfig == texture->config() || |
| 83 | kBGRA_8888_GrPixelConfig == texture->config()); |
| 84 | // Why did we pollute our texture cache instead of using a GrSingleTextureEffect? |
| 85 | GrAssert(swapRedAndBlue || kNone_PMConversion != pmConversion); |
| 86 | } |
| 87 | |
| 88 | const GrProgramStageFactory& GrConfigConversionEffect::getFactory() const { |
| 89 | return GrTProgramStageFactory<GrConfigConversionEffect>::getInstance(); |
| 90 | } |
| 91 | |
| 92 | bool GrConfigConversionEffect::isEqual(const GrCustomStage& s) const { |
| 93 | const GrConfigConversionEffect& other = static_cast<const GrConfigConversionEffect&>(s); |
| 94 | return other.fSwapRedAndBlue == fSwapRedAndBlue && other.fPMConversion == fPMConversion; |
| 95 | } |
| 96 | |
| 97 | /////////////////////////////////////////////////////////////////////////////// |
| 98 | |
| 99 | GR_DEFINE_CUSTOM_STAGE_TEST(GrConfigConversionEffect); |
| 100 | |
| 101 | GrCustomStage* GrConfigConversionEffect::TestCreate(SkRandom* random, |
| 102 | GrContext* context, |
| 103 | GrTexture* textures[]) { |
| 104 | PMConversion pmConv = static_cast<PMConversion>(random->nextULessThan(kPMConversionCnt)); |
| 105 | bool swapRB; |
| 106 | if (kNone_PMConversion == pmConv) { |
| 107 | swapRB = true; |
| 108 | } else { |
| 109 | swapRB = random->nextBool(); |
| 110 | } |
| 111 | return SkNEW_ARGS(GrConfigConversionEffect, |
| 112 | (textures[GrCustomStageUnitTest::kSkiaPMTextureIdx], swapRB, pmConv)); |
| 113 | } |
| 114 | |
| 115 | /////////////////////////////////////////////////////////////////////////////// |
| 116 | void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context, |
| 117 | PMConversion* pmToUPMRule, |
| 118 | PMConversion* upmToPMRule) { |
| 119 | *pmToUPMRule = kNone_PMConversion; |
| 120 | *upmToPMRule = kNone_PMConversion; |
| 121 | SkAutoTMalloc<uint32_t> data(256 * 256 * 3); |
| 122 | uint32_t* srcData = data.get(); |
| 123 | uint32_t* firstRead = data.get() + 256 * 256; |
| 124 | uint32_t* secondRead = data.get() + 2 * 256 * 256; |
| 125 | |
| 126 | // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate |
| 127 | // values in row y. We set r,g, and b to the same value since they are handled identically. |
| 128 | for (int y = 0; y < 256; ++y) { |
| 129 | for (int x = 0; x < 256; ++x) { |
| 130 | uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]); |
| 131 | color[3] = y; |
| 132 | color[2] = GrMin(x, y); |
| 133 | color[1] = GrMin(x, y); |
| 134 | color[0] = GrMin(x, y); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | GrTextureDesc desc; |
| 139 | desc.fFlags = kRenderTarget_GrTextureFlagBit | |
| 140 | kNoStencil_GrTextureFlagBit; |
| 141 | desc.fWidth = 256; |
| 142 | desc.fHeight = 256; |
| 143 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 144 | |
| 145 | SkAutoTUnref<GrTexture> readTex(context->createUncachedTexture(desc, NULL, 0)); |
| 146 | if (!readTex.get()) { |
| 147 | return; |
| 148 | } |
| 149 | SkAutoTUnref<GrTexture> tempTex(context->createUncachedTexture(desc, NULL, 0)); |
| 150 | if (!tempTex.get()) { |
| 151 | return; |
| 152 | } |
| 153 | desc.fFlags = kNone_GrTextureFlags; |
| 154 | SkAutoTUnref<GrTexture> dataTex(context->createUncachedTexture(desc, data, 0)); |
| 155 | if (!dataTex.get()) { |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | static const PMConversion kConversionRules[][2] = { |
| 160 | {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion}, |
| 161 | {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion}, |
| 162 | }; |
| 163 | |
| 164 | GrContext::AutoWideOpenIdentityDraw awoid(context, NULL); |
| 165 | |
| 166 | bool failed = true; |
| 167 | |
| 168 | for (size_t i = 0; i < GR_ARRAY_COUNT(kConversionRules) && failed; ++i) { |
| 169 | *pmToUPMRule = kConversionRules[i][0]; |
| 170 | *upmToPMRule = kConversionRules[i][1]; |
| 171 | |
| 172 | static const GrRect kDstRect = GrRect::MakeWH(GrIntToScalar(256), GrIntToScalar(256)); |
| 173 | static const GrRect kSrcRect = GrRect::MakeWH(GR_Scalar1, GR_Scalar1); |
| 174 | // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw |
| 175 | // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data. |
| 176 | // We then verify that two reads produced the same values. |
| 177 | |
| 178 | GrPaint paint; |
| 179 | paint.reset(); |
| 180 | |
| 181 | SkAutoTUnref<GrCustomStage> pmToUPMStage1(SkNEW_ARGS(GrConfigConversionEffect, |
| 182 | (dataTex, false, *pmToUPMRule))); |
| 183 | SkAutoTUnref<GrCustomStage> upmToPMStage(SkNEW_ARGS(GrConfigConversionEffect, |
| 184 | (readTex, false, *upmToPMRule))); |
| 185 | SkAutoTUnref<GrCustomStage> pmToUPMStage2(SkNEW_ARGS(GrConfigConversionEffect, |
| 186 | (tempTex, false, *pmToUPMRule))); |
| 187 | |
| 188 | context->setRenderTarget(readTex->asRenderTarget()); |
| 189 | paint.textureSampler(0)->setCustomStage(pmToUPMStage1); |
| 190 | context->drawRectToRect(paint, kDstRect, kSrcRect); |
| 191 | |
| 192 | readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead); |
| 193 | |
| 194 | context->setRenderTarget(tempTex->asRenderTarget()); |
| 195 | paint.textureSampler(0)->setCustomStage(upmToPMStage); |
| 196 | context->drawRectToRect(paint, kDstRect, kSrcRect); |
| 197 | context->setRenderTarget(readTex->asRenderTarget()); |
| 198 | paint.textureSampler(0)->setCustomStage(pmToUPMStage2); |
| 199 | context->drawRectToRect(paint, kDstRect, kSrcRect); |
| 200 | |
| 201 | readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead); |
| 202 | |
| 203 | failed = false; |
| 204 | for (int y = 0; y < 256 && !failed; ++y) { |
| 205 | for (int x = 0; x <= y; ++x) { |
| 206 | if (firstRead[256 * y + x] != secondRead[256 * y + x]) { |
| 207 | failed = true; |
| 208 | break; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | if (failed) { |
| 214 | *pmToUPMRule = kNone_PMConversion; |
| 215 | *upmToPMRule = kNone_PMConversion; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | GrCustomStage* GrConfigConversionEffect::Create(GrTexture* texture, |
| 220 | bool swapRedAndBlue, |
| 221 | PMConversion pmConversion) { |
| 222 | if (!swapRedAndBlue && kNone_PMConversion == pmConversion) { |
| 223 | // If we returned a GrConfigConversionEffect that was equivalent to a GrSingleTextureEffect |
| 224 | // then we may pollute our texture cache with redundant shaders. So in the case that no |
| 225 | // conversions were requested we instead return a GrSingleTextureEffect. |
| 226 | return SkNEW_ARGS(GrSingleTextureEffect, (texture)); |
| 227 | } else { |
| 228 | if (kRGBA_8888_GrPixelConfig != texture->config() && |
| 229 | kBGRA_8888_GrPixelConfig != texture->config() && |
| 230 | kNone_PMConversion != pmConversion) { |
| 231 | // The PM conversions assume colors are 0..255 |
| 232 | return NULL; |
| 233 | } |
| 234 | return SkNEW_ARGS(GrConfigConversionEffect, (texture, swapRedAndBlue, pmConversion)); |
| 235 | } |
| 236 | } |