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" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 10 | #include "GrTBackendProcessorFactory.h" |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 11 | #include "GrSimpleTextureEffect.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 12 | #include "gl/GrGLProcessor.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 13 | #include "gl/builders/GrGLProgramBuilder.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 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 16 | class GrGLConfigConversionEffect : public GrGLFragmentProcessor { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 17 | public: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 18 | GrGLConfigConversionEffect(const GrBackendProcessorFactory& factory, |
| 19 | const GrProcessor& processor) |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 20 | : INHERITED (factory) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 21 | const GrConfigConversionEffect& configConversionEffect = |
| 22 | processor.cast<GrConfigConversionEffect>(); |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 23 | fSwapRedAndBlue = configConversionEffect.swapsRedAndBlue(); |
| 24 | fPMConversion = configConversionEffect.pmConversion(); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 25 | } |
| 26 | |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 27 | virtual void emitCode(GrGLFPBuilder* builder, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 28 | const GrFragmentProcessor&, |
| 29 | const GrProcessorKey& key, |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 30 | const char* outputColor, |
| 31 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 32 | const TransformedCoordsArray& coords, |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 33 | const TextureSamplerArray& samplers) SK_OVERRIDE { |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 34 | // Using highp for GLES here in order to avoid some precision issues on specific GPUs. |
| 35 | GrGLShaderVar tmpVar("tmpColor", kVec4f_GrSLType, 0, GrGLShaderVar::kHigh_Precision); |
| 36 | SkString tmpDecl; |
| 37 | tmpVar.appendDecl(builder->ctxInfo(), &tmpDecl); |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 38 | |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 39 | GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 40 | |
| 41 | fsBuilder->codeAppendf("%s;", tmpDecl.c_str()); |
| 42 | |
| 43 | fsBuilder->codeAppendf("%s = ", tmpVar.c_str()); |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 44 | fsBuilder->appendTextureLookup(samplers[0], coords[0].c_str(), coords[0].getType()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 45 | fsBuilder->codeAppend(";"); |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 46 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 47 | if (GrConfigConversionEffect::kNone_PMConversion == fPMConversion) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 48 | SkASSERT(fSwapRedAndBlue); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 49 | fsBuilder->codeAppendf("%s = %s.bgra;", outputColor, tmpVar.c_str()); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 50 | } else { |
| 51 | const char* swiz = fSwapRedAndBlue ? "bgr" : "rgb"; |
| 52 | switch (fPMConversion) { |
| 53 | case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion: |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 54 | fsBuilder->codeAppendf( |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 55 | "%s = vec4(ceil(%s.%s * %s.a * 255.0) / 255.0, %s.a);", |
| 56 | 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] | 57 | break; |
| 58 | case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion: |
commit-bot@chromium.org | b4e200e | 2013-06-19 11:41:02 +0000 | [diff] [blame] | 59 | // Add a compensation(0.001) here to avoid the side effect of the floor operation. |
| 60 | // In Intel GPUs, the integer value converted from floor(%s.r * 255.0) / 255.0 |
| 61 | // is less than the integer value converted from %s.r by 1 when the %s.r is |
| 62 | // converted from the integer value 2^n, such as 1, 2, 4, 8, etc. |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 63 | fsBuilder->codeAppendf( |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 64 | "%s = vec4(floor(%s.%s * %s.a * 255.0 + 0.001) / 255.0, %s.a);", |
| 65 | 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] | 66 | break; |
| 67 | case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion: |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 68 | fsBuilder->codeAppendf( |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 69 | "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(ceil(%s.%s / %s.a * 255.0) / 255.0, %s.a);", |
| 70 | 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] | 71 | break; |
| 72 | case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion: |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 73 | fsBuilder->codeAppendf( |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 74 | "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(floor(%s.%s / %s.a * 255.0) / 255.0, %s.a);", |
| 75 | 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] | 76 | break; |
robertphillips@google.com | 2af1b18 | 2012-08-28 11:23:09 +0000 | [diff] [blame] | 77 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 78 | SkFAIL("Unknown conversion op."); |
robertphillips@google.com | 2af1b18 | 2012-08-28 11:23:09 +0000 | [diff] [blame] | 79 | break; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 80 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 81 | fsBuilder->codeAppendf("%s = %s;", outputColor, tmpVar.c_str()); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 82 | } |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 83 | SkString modulate; |
egdaniel | 089f8de | 2014-10-09 10:34:58 -0700 | [diff] [blame] | 84 | GrGLSLMulVarBy4f(&modulate, outputColor, inputColor); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 85 | fsBuilder->codeAppend(modulate.c_str()); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 86 | } |
| 87 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 88 | static inline void GenKey(const GrProcessor& processor, const GrGLCaps&, |
| 89 | GrProcessorKeyBuilder* b) { |
| 90 | const GrConfigConversionEffect& conv = processor.cast<GrConfigConversionEffect>(); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 91 | uint32_t key = (conv.swapsRedAndBlue() ? 0 : 1) | (conv.pmConversion() << 1); |
| 92 | b->add32(key); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | private: |
| 96 | bool fSwapRedAndBlue; |
| 97 | GrConfigConversionEffect::PMConversion fPMConversion; |
| 98 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 99 | typedef GrGLFragmentProcessor INHERITED; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 100 | |
| 101 | }; |
| 102 | |
| 103 | /////////////////////////////////////////////////////////////////////////////// |
| 104 | |
| 105 | GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture, |
| 106 | bool swapRedAndBlue, |
bsalomon@google.com | b1456d7 | 2012-11-02 18:23:45 +0000 | [diff] [blame] | 107 | PMConversion pmConversion, |
| 108 | const SkMatrix& matrix) |
| 109 | : GrSingleTextureEffect(texture, matrix) |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 110 | , fSwapRedAndBlue(swapRedAndBlue) |
| 111 | , fPMConversion(pmConversion) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 112 | SkASSERT(kRGBA_8888_GrPixelConfig == texture->config() || |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 113 | kBGRA_8888_GrPixelConfig == texture->config()); |
| 114 | // 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] | 115 | SkASSERT(swapRedAndBlue || kNone_PMConversion != pmConversion); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 116 | } |
| 117 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 118 | const GrBackendFragmentProcessorFactory& GrConfigConversionEffect::getFactory() const { |
| 119 | return GrTBackendFragmentProcessorFactory<GrConfigConversionEffect>::getInstance(); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 120 | } |
| 121 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame^] | 122 | bool GrConfigConversionEffect::onIsEqual(const GrFragmentProcessor& s) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 123 | const GrConfigConversionEffect& other = s.cast<GrConfigConversionEffect>(); |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 124 | return this->texture(0) == s.texture(0) && |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 125 | other.fSwapRedAndBlue == fSwapRedAndBlue && |
| 126 | other.fPMConversion == fPMConversion; |
| 127 | } |
| 128 | |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 129 | void GrConfigConversionEffect::onComputeInvariantOutput(InvariantOutput* inout) const { |
| 130 | this->updateInvariantOutputForModulation(inout); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /////////////////////////////////////////////////////////////////////////////// |
| 134 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 135 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConfigConversionEffect); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 136 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 137 | GrFragmentProcessor* GrConfigConversionEffect::TestCreate(SkRandom* random, |
| 138 | GrContext*, |
| 139 | const GrDrawTargetCaps&, |
| 140 | GrTexture* textures[]) { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 141 | PMConversion pmConv = static_cast<PMConversion>(random->nextULessThan(kPMConversionCnt)); |
| 142 | bool swapRB; |
| 143 | if (kNone_PMConversion == pmConv) { |
| 144 | swapRB = true; |
| 145 | } else { |
| 146 | swapRB = random->nextBool(); |
| 147 | } |
bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 148 | return SkNEW_ARGS(GrConfigConversionEffect, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 149 | (textures[GrProcessorUnitTest::kSkiaPMTextureIdx], |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 150 | swapRB, |
| 151 | pmConv, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 152 | GrProcessorUnitTest::TestMatrix(random))); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /////////////////////////////////////////////////////////////////////////////// |
| 156 | void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context, |
| 157 | PMConversion* pmToUPMRule, |
| 158 | PMConversion* upmToPMRule) { |
| 159 | *pmToUPMRule = kNone_PMConversion; |
| 160 | *upmToPMRule = kNone_PMConversion; |
| 161 | SkAutoTMalloc<uint32_t> data(256 * 256 * 3); |
| 162 | uint32_t* srcData = data.get(); |
| 163 | uint32_t* firstRead = data.get() + 256 * 256; |
| 164 | uint32_t* secondRead = data.get() + 2 * 256 * 256; |
| 165 | |
| 166 | // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate |
| 167 | // values in row y. We set r,g, and b to the same value since they are handled identically. |
| 168 | for (int y = 0; y < 256; ++y) { |
| 169 | for (int x = 0; x < 256; ++x) { |
| 170 | uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]); |
| 171 | color[3] = y; |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 172 | color[2] = SkTMin(x, y); |
| 173 | color[1] = SkTMin(x, y); |
| 174 | color[0] = SkTMin(x, y); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | GrTextureDesc desc; |
| 179 | desc.fFlags = kRenderTarget_GrTextureFlagBit | |
| 180 | kNoStencil_GrTextureFlagBit; |
| 181 | desc.fWidth = 256; |
| 182 | desc.fHeight = 256; |
| 183 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 184 | |
| 185 | SkAutoTUnref<GrTexture> readTex(context->createUncachedTexture(desc, NULL, 0)); |
| 186 | if (!readTex.get()) { |
| 187 | return; |
| 188 | } |
| 189 | SkAutoTUnref<GrTexture> tempTex(context->createUncachedTexture(desc, NULL, 0)); |
| 190 | if (!tempTex.get()) { |
| 191 | return; |
| 192 | } |
| 193 | desc.fFlags = kNone_GrTextureFlags; |
| 194 | SkAutoTUnref<GrTexture> dataTex(context->createUncachedTexture(desc, data, 0)); |
| 195 | if (!dataTex.get()) { |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | static const PMConversion kConversionRules[][2] = { |
| 200 | {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion}, |
| 201 | {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion}, |
| 202 | }; |
| 203 | |
| 204 | GrContext::AutoWideOpenIdentityDraw awoid(context, NULL); |
| 205 | |
| 206 | bool failed = true; |
| 207 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 208 | 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] | 209 | *pmToUPMRule = kConversionRules[i][0]; |
| 210 | *upmToPMRule = kConversionRules[i][1]; |
| 211 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 212 | static const SkRect kDstRect = SkRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256)); |
| 213 | static const SkRect kSrcRect = SkRect::MakeWH(SK_Scalar1, SK_Scalar1); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 214 | // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw |
| 215 | // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data. |
| 216 | // We then verify that two reads produced the same values. |
| 217 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 218 | SkAutoTUnref<GrFragmentProcessor> pmToUPM1( |
| 219 | SkNEW_ARGS(GrConfigConversionEffect, |
| 220 | (dataTex, false, *pmToUPMRule, SkMatrix::I()))); |
| 221 | SkAutoTUnref<GrFragmentProcessor> upmToPM( |
| 222 | SkNEW_ARGS(GrConfigConversionEffect, |
| 223 | (readTex, false, *upmToPMRule, SkMatrix::I()))); |
| 224 | SkAutoTUnref<GrFragmentProcessor> pmToUPM2( |
| 225 | SkNEW_ARGS(GrConfigConversionEffect, |
| 226 | (tempTex, false, *pmToUPMRule, 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; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 230 | paint1.addColorProcessor(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; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 237 | paint2.addColorProcessor(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; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 242 | paint3.addColorProcessor(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 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 263 | const GrFragmentProcessor* GrConfigConversionEffect::Create(GrTexture* texture, |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 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 | } |