| 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 | b1456d7 | 2012-11-02 18:23:45 +0000 | [diff] [blame] | 9 | #include "GrContext.h" |
| bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 10 | #include "GrTBackendEffectFactory.h" |
| bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 11 | #include "GrSimpleTextureEffect.h" |
| bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 12 | #include "gl/GrGLEffect.h" |
| bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 13 | #include "gl/GrGLShaderBuilder.h" |
| bsalomon@google.com | b1456d7 | 2012-11-02 18:23:45 +0000 | [diff] [blame] | 14 | #include "SkMatrix.h" |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 15 | |
| bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 16 | class GrGLConfigConversionEffect : public GrGLEffect { |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 17 | public: |
| bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 18 | GrGLConfigConversionEffect(const GrBackendEffectFactory& factory, |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 19 | const GrDrawEffect& drawEffect) |
| bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 20 | : INHERITED (factory) { |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 21 | const GrConfigConversionEffect& effect = drawEffect.castEffect<GrConfigConversionEffect>(); |
| bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 22 | fSwapRedAndBlue = effect.swapsRedAndBlue(); |
| 23 | fPMConversion = effect.pmConversion(); |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 26 | virtual void emitCode(GrGLShaderBuilder* builder, |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 27 | const GrDrawEffect&, |
| bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 28 | const GrEffectKey& key, |
| bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 29 | const char* outputColor, |
| 30 | const char* inputColor, |
| bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 31 | const TransformedCoordsArray& coords, |
| bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 32 | const TextureSamplerArray& samplers) SK_OVERRIDE { |
| changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame^] | 33 | // Using highp for GLES here in order to avoid some precision issues on specific GPUs. |
| 34 | GrGLShaderVar tmpVar("tmpColor", kVec4f_GrSLType, 0, GrGLShaderVar::kHigh_Precision); |
| 35 | SkString tmpDecl; |
| 36 | tmpVar.appendDecl(builder->ctxInfo(), &tmpDecl); |
| 37 | builder->fsCodeAppendf("%s;", tmpDecl.c_str()); |
| 38 | |
| 39 | builder->fsCodeAppendf("%s = ", tmpVar.c_str()); |
| bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 40 | builder->fsAppendTextureLookup(samplers[0], coords[0].c_str(), coords[0].type()); |
| changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame^] | 41 | builder->fsCodeAppend(";"); |
| 42 | |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 43 | if (GrConfigConversionEffect::kNone_PMConversion == fPMConversion) { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 44 | SkASSERT(fSwapRedAndBlue); |
| changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame^] | 45 | builder->fsCodeAppendf("%s = %s.bgra;", outputColor, tmpVar.c_str()); |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 46 | } else { |
| 47 | const char* swiz = fSwapRedAndBlue ? "bgr" : "rgb"; |
| 48 | switch (fPMConversion) { |
| 49 | case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion: |
| bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 50 | builder->fsCodeAppendf( |
| changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame^] | 51 | "%s = vec4(ceil(%s.%s * %s.a * 255.0) / 255.0, %s.a);", |
| 52 | tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str()); |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 53 | break; |
| 54 | case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion: |
| commit-bot@chromium.org | b4e200e | 2013-06-19 11:41:02 +0000 | [diff] [blame] | 55 | // Add a compensation(0.001) here to avoid the side effect of the floor operation. |
| 56 | // In Intel GPUs, the integer value converted from floor(%s.r * 255.0) / 255.0 |
| 57 | // is less than the integer value converted from %s.r by 1 when the %s.r is |
| 58 | // converted from the integer value 2^n, such as 1, 2, 4, 8, etc. |
| bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 59 | builder->fsCodeAppendf( |
| changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame^] | 60 | "%s = vec4(floor(%s.%s * %s.a * 255.0 + 0.001) / 255.0, %s.a);", |
| 61 | tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str()); |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 62 | break; |
| 63 | case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion: |
| changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame^] | 64 | builder->fsCodeAppendf( |
| 65 | "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(ceil(%s.%s / %s.a * 255.0) / 255.0, %s.a);", |
| 66 | tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str()); |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 67 | break; |
| 68 | case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion: |
| changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame^] | 69 | builder->fsCodeAppendf( |
| 70 | "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(floor(%s.%s / %s.a * 255.0) / 255.0, %s.a);", |
| 71 | tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str()); |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 72 | break; |
| robertphillips@google.com | 2af1b18 | 2012-08-28 11:23:09 +0000 | [diff] [blame] | 73 | default: |
| commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 74 | SkFAIL("Unknown conversion op."); |
| robertphillips@google.com | 2af1b18 | 2012-08-28 11:23:09 +0000 | [diff] [blame] | 75 | break; |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 76 | } |
| changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame^] | 77 | builder->fsCodeAppendf("%s = %s;", outputColor, tmpVar.c_str()); |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 78 | } |
| bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 79 | SkString modulate; |
| 80 | GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); |
| 81 | builder->fsCodeAppend(modulate.c_str()); |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 84 | static inline void GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&, |
| 85 | GrEffectKeyBuilder* b) { |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 86 | const GrConfigConversionEffect& conv = drawEffect.castEffect<GrConfigConversionEffect>(); |
| bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 87 | uint32_t key = (conv.swapsRedAndBlue() ? 0 : 1) | (conv.pmConversion() << 1); |
| 88 | b->add32(key); |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | private: |
| 92 | bool fSwapRedAndBlue; |
| 93 | GrConfigConversionEffect::PMConversion fPMConversion; |
| 94 | |
| bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 95 | typedef GrGLEffect INHERITED; |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 96 | |
| 97 | }; |
| 98 | |
| 99 | /////////////////////////////////////////////////////////////////////////////// |
| 100 | |
| 101 | GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture, |
| 102 | bool swapRedAndBlue, |
| bsalomon@google.com | b1456d7 | 2012-11-02 18:23:45 +0000 | [diff] [blame] | 103 | PMConversion pmConversion, |
| 104 | const SkMatrix& matrix) |
| 105 | : GrSingleTextureEffect(texture, matrix) |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 106 | , fSwapRedAndBlue(swapRedAndBlue) |
| 107 | , fPMConversion(pmConversion) { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 108 | SkASSERT(kRGBA_8888_GrPixelConfig == texture->config() || |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 109 | kBGRA_8888_GrPixelConfig == texture->config()); |
| 110 | // Why did we pollute our texture cache instead of using a GrSingleTextureEffect? |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 111 | SkASSERT(swapRedAndBlue || kNone_PMConversion != pmConversion); |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 114 | const GrBackendEffectFactory& GrConfigConversionEffect::getFactory() const { |
| 115 | return GrTBackendEffectFactory<GrConfigConversionEffect>::getInstance(); |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 118 | bool GrConfigConversionEffect::onIsEqual(const GrEffect& s) const { |
| bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 119 | const GrConfigConversionEffect& other = CastEffect<GrConfigConversionEffect>(s); |
| bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 120 | return this->texture(0) == s.texture(0) && |
| bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 121 | other.fSwapRedAndBlue == fSwapRedAndBlue && |
| 122 | other.fPMConversion == fPMConversion; |
| 123 | } |
| 124 | |
| 125 | void GrConfigConversionEffect::getConstantColorComponents(GrColor* color, |
| 126 | uint32_t* validFlags) const { |
| 127 | this->updateConstantColorComponentsForModulation(color, validFlags); |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /////////////////////////////////////////////////////////////////////////////// |
| 131 | |
| bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 132 | GR_DEFINE_EFFECT_TEST(GrConfigConversionEffect); |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 133 | |
| bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 134 | GrEffect* GrConfigConversionEffect::TestCreate(SkRandom* random, |
| 135 | GrContext*, |
| 136 | const GrDrawTargetCaps&, |
| 137 | GrTexture* textures[]) { |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 138 | PMConversion pmConv = static_cast<PMConversion>(random->nextULessThan(kPMConversionCnt)); |
| 139 | bool swapRB; |
| 140 | if (kNone_PMConversion == pmConv) { |
| 141 | swapRB = true; |
| 142 | } else { |
| 143 | swapRB = random->nextBool(); |
| 144 | } |
| bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 145 | return SkNEW_ARGS(GrConfigConversionEffect, |
| bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 146 | (textures[GrEffectUnitTest::kSkiaPMTextureIdx], |
| 147 | swapRB, |
| 148 | pmConv, |
| bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 149 | GrEffectUnitTest::TestMatrix(random))); |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /////////////////////////////////////////////////////////////////////////////// |
| 153 | void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context, |
| 154 | PMConversion* pmToUPMRule, |
| 155 | PMConversion* upmToPMRule) { |
| 156 | *pmToUPMRule = kNone_PMConversion; |
| 157 | *upmToPMRule = kNone_PMConversion; |
| 158 | SkAutoTMalloc<uint32_t> data(256 * 256 * 3); |
| 159 | uint32_t* srcData = data.get(); |
| 160 | uint32_t* firstRead = data.get() + 256 * 256; |
| 161 | uint32_t* secondRead = data.get() + 2 * 256 * 256; |
| 162 | |
| 163 | // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate |
| 164 | // values in row y. We set r,g, and b to the same value since they are handled identically. |
| 165 | for (int y = 0; y < 256; ++y) { |
| 166 | for (int x = 0; x < 256; ++x) { |
| 167 | uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]); |
| 168 | color[3] = y; |
| commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 169 | color[2] = SkTMin(x, y); |
| 170 | color[1] = SkTMin(x, y); |
| 171 | color[0] = SkTMin(x, y); |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | GrTextureDesc desc; |
| 176 | desc.fFlags = kRenderTarget_GrTextureFlagBit | |
| 177 | kNoStencil_GrTextureFlagBit; |
| 178 | desc.fWidth = 256; |
| 179 | desc.fHeight = 256; |
| 180 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 181 | |
| 182 | SkAutoTUnref<GrTexture> readTex(context->createUncachedTexture(desc, NULL, 0)); |
| 183 | if (!readTex.get()) { |
| 184 | return; |
| 185 | } |
| 186 | SkAutoTUnref<GrTexture> tempTex(context->createUncachedTexture(desc, NULL, 0)); |
| 187 | if (!tempTex.get()) { |
| 188 | return; |
| 189 | } |
| 190 | desc.fFlags = kNone_GrTextureFlags; |
| 191 | SkAutoTUnref<GrTexture> dataTex(context->createUncachedTexture(desc, data, 0)); |
| 192 | if (!dataTex.get()) { |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | static const PMConversion kConversionRules[][2] = { |
| 197 | {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion}, |
| 198 | {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion}, |
| 199 | }; |
| 200 | |
| 201 | GrContext::AutoWideOpenIdentityDraw awoid(context, NULL); |
| 202 | |
| 203 | bool failed = true; |
| 204 | |
| commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 205 | for (size_t i = 0; i < SK_ARRAY_COUNT(kConversionRules) && failed; ++i) { |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 206 | *pmToUPMRule = kConversionRules[i][0]; |
| 207 | *upmToPMRule = kConversionRules[i][1]; |
| 208 | |
| commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 209 | static const SkRect kDstRect = SkRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256)); |
| 210 | static const SkRect kSrcRect = SkRect::MakeWH(SK_Scalar1, SK_Scalar1); |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 211 | // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw |
| 212 | // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data. |
| 213 | // We then verify that two reads produced the same values. |
| 214 | |
| bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 215 | SkAutoTUnref<GrEffect> pmToUPM1(SkNEW_ARGS(GrConfigConversionEffect, (dataTex, |
| 216 | false, |
| 217 | *pmToUPMRule, |
| 218 | SkMatrix::I()))); |
| 219 | SkAutoTUnref<GrEffect> upmToPM(SkNEW_ARGS(GrConfigConversionEffect, (readTex, |
| 220 | false, |
| 221 | *upmToPMRule, |
| 222 | SkMatrix::I()))); |
| 223 | SkAutoTUnref<GrEffect> pmToUPM2(SkNEW_ARGS(GrConfigConversionEffect, (tempTex, |
| 224 | false, |
| 225 | *pmToUPMRule, |
| 226 | SkMatrix::I()))); |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 227 | |
| 228 | context->setRenderTarget(readTex->asRenderTarget()); |
| commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 229 | GrPaint paint1; |
| bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 230 | paint1.addColorEffect(pmToUPM1); |
| commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 231 | context->drawRectToRect(paint1, kDstRect, kSrcRect); |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 232 | |
| 233 | readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead); |
| 234 | |
| 235 | context->setRenderTarget(tempTex->asRenderTarget()); |
| commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 236 | GrPaint paint2; |
| bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 237 | paint2.addColorEffect(upmToPM); |
| commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 238 | context->drawRectToRect(paint2, kDstRect, kSrcRect); |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 239 | context->setRenderTarget(readTex->asRenderTarget()); |
| commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 240 | |
| 241 | GrPaint paint3; |
| bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 242 | paint3.addColorEffect(pmToUPM2); |
| commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 243 | context->drawRectToRect(paint3, kDstRect, kSrcRect); |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 244 | |
| 245 | readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead); |
| 246 | |
| 247 | failed = false; |
| 248 | for (int y = 0; y < 256 && !failed; ++y) { |
| 249 | for (int x = 0; x <= y; ++x) { |
| 250 | if (firstRead[256 * y + x] != secondRead[256 * y + x]) { |
| 251 | failed = true; |
| 252 | break; |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | if (failed) { |
| 258 | *pmToUPMRule = kNone_PMConversion; |
| 259 | *upmToPMRule = kNone_PMConversion; |
| 260 | } |
| 261 | } |
| 262 | |
| bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 263 | const GrEffect* GrConfigConversionEffect::Create(GrTexture* texture, |
| 264 | bool swapRedAndBlue, |
| 265 | PMConversion pmConversion, |
| 266 | const SkMatrix& matrix) { |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 267 | if (!swapRedAndBlue && kNone_PMConversion == pmConversion) { |
| bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 268 | // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 269 | // then we may pollute our texture cache with redundant shaders. So in the case that no |
| bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 270 | // conversions were requested we instead return a GrSimpleTextureEffect. |
| 271 | return GrSimpleTextureEffect::Create(texture, matrix); |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 272 | } else { |
| 273 | if (kRGBA_8888_GrPixelConfig != texture->config() && |
| 274 | kBGRA_8888_GrPixelConfig != texture->config() && |
| 275 | kNone_PMConversion != pmConversion) { |
| 276 | // The PM conversions assume colors are 0..255 |
| bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 277 | return NULL; |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 278 | } |
| bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 279 | return SkNEW_ARGS(GrConfigConversionEffect, (texture, |
| 280 | swapRedAndBlue, |
| 281 | pmConversion, |
| 282 | matrix)); |
| bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 283 | } |
| 284 | } |