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