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" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 10 | #include "GrDrawContext.h" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 11 | #include "GrInvariantOutput.h" |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 12 | #include "GrSimpleTextureEffect.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 13 | #include "SkMatrix.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 14 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 15 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 16 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 17 | class GrGLConfigConversionEffect : public GrGLSLFragmentProcessor { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 18 | public: |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame^] | 19 | void emitCode(EmitArgs& args) override { |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 20 | const GrConfigConversionEffect& cce = args.fFp.cast<GrConfigConversionEffect>(); |
| 21 | const GrSwizzle& swizzle = cce.swizzle(); |
| 22 | GrConfigConversionEffect::PMConversion pmConversion = cce.pmConversion(); |
| 23 | |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 24 | // Using highp for GLES here in order to avoid some precision issues on specific GPUs. |
egdaniel | 0d3f061 | 2015-10-21 10:45:48 -0700 | [diff] [blame] | 25 | GrGLSLShaderVar tmpVar("tmpColor", kVec4f_GrSLType, 0, kHigh_GrSLPrecision); |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 26 | SkString tmpDecl; |
egdaniel | a2e3e0f | 2015-11-19 07:23:45 -0800 | [diff] [blame] | 27 | tmpVar.appendDecl(args.fGLSLCaps, &tmpDecl); |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 28 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 29 | GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 30 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 31 | fragBuilder->codeAppendf("%s;", tmpDecl.c_str()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 32 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 33 | fragBuilder->codeAppendf("%s = ", tmpVar.c_str()); |
| 34 | fragBuilder->appendTextureLookup(args.fSamplers[0], args.fCoords[0].c_str(), |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 35 | args.fCoords[0].getType()); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 36 | fragBuilder->codeAppend(";"); |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 37 | |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 38 | if (GrConfigConversionEffect::kNone_PMConversion == pmConversion) { |
| 39 | SkASSERT(GrSwizzle::RGBA() != swizzle); |
| 40 | fragBuilder->codeAppendf("%s = %s.%s;", args.fOutputColor, tmpVar.c_str(), |
| 41 | swizzle.c_str()); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 42 | } else { |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 43 | switch (pmConversion) { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 44 | case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion: |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 45 | fragBuilder->codeAppendf( |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 46 | "%s = vec4(ceil(%s.rgb * %s.a * 255.0) / 255.0, %s.a);", |
| 47 | tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str()); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 48 | break; |
| 49 | case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion: |
commit-bot@chromium.org | b4e200e | 2013-06-19 11:41:02 +0000 | [diff] [blame] | 50 | // Add a compensation(0.001) here to avoid the side effect of the floor operation. |
| 51 | // In Intel GPUs, the integer value converted from floor(%s.r * 255.0) / 255.0 |
| 52 | // is less than the integer value converted from %s.r by 1 when the %s.r is |
| 53 | // converted from the integer value 2^n, such as 1, 2, 4, 8, etc. |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 54 | fragBuilder->codeAppendf( |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 55 | "%s = vec4(floor(%s.rgb * %s.a * 255.0 + 0.001) / 255.0, %s.a);", |
| 56 | tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str()); |
| 57 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 58 | break; |
| 59 | case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion: |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 60 | fragBuilder->codeAppendf( |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 61 | "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(ceil(%s.rgb / %s.a * 255.0) / 255.0, %s.a);", |
| 62 | tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), |
| 63 | tmpVar.c_str()); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 64 | break; |
| 65 | case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion: |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 66 | fragBuilder->codeAppendf( |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 67 | "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(floor(%s.rgb / %s.a * 255.0) / 255.0, %s.a);", |
| 68 | tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), |
| 69 | tmpVar.c_str()); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 70 | break; |
robertphillips@google.com | 2af1b18 | 2012-08-28 11:23:09 +0000 | [diff] [blame] | 71 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 72 | SkFAIL("Unknown conversion op."); |
robertphillips@google.com | 2af1b18 | 2012-08-28 11:23:09 +0000 | [diff] [blame] | 73 | break; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 74 | } |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 75 | fragBuilder->codeAppendf("%s = %s.%s;", args.fOutputColor, tmpVar.c_str(), |
| 76 | swizzle.c_str()); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 77 | } |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 78 | SkString modulate; |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 79 | GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 80 | fragBuilder->codeAppend(modulate.c_str()); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 81 | } |
| 82 | |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 83 | static inline void GenKey(const GrProcessor& processor, const GrGLSLCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 84 | GrProcessorKeyBuilder* b) { |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 85 | const GrConfigConversionEffect& cce = processor.cast<GrConfigConversionEffect>(); |
| 86 | uint32_t key = (cce.swizzle().asKey()) | (cce.pmConversion() << 16); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 87 | b->add32(key); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | private: |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 91 | typedef GrGLSLFragmentProcessor INHERITED; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 92 | |
| 93 | }; |
| 94 | |
| 95 | /////////////////////////////////////////////////////////////////////////////// |
| 96 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 97 | GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture, |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 98 | const GrSwizzle& swizzle, |
bsalomon@google.com | b1456d7 | 2012-11-02 18:23:45 +0000 | [diff] [blame] | 99 | PMConversion pmConversion, |
| 100 | const SkMatrix& matrix) |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 101 | : INHERITED(texture, matrix) |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 102 | , fSwizzle(swizzle) |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 103 | , fPMConversion(pmConversion) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 104 | this->initClassID<GrConfigConversionEffect>(); |
bsalomon | 445fc43 | 2015-08-03 10:15:25 -0700 | [diff] [blame] | 105 | // We expect to get here with non-BGRA/RGBA only if we're doing not doing a premul/unpremul |
| 106 | // conversion. |
| 107 | SkASSERT((kRGBA_8888_GrPixelConfig == texture->config() || |
| 108 | kBGRA_8888_GrPixelConfig == texture->config()) || |
| 109 | kNone_PMConversion == pmConversion); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 110 | // Why did we pollute our texture cache instead of using a GrSingleTextureEffect? |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 111 | SkASSERT(swizzle != GrSwizzle::RGBA() || kNone_PMConversion != pmConversion); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 112 | } |
| 113 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 114 | bool GrConfigConversionEffect::onIsEqual(const GrFragmentProcessor& s) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 115 | const GrConfigConversionEffect& other = s.cast<GrConfigConversionEffect>(); |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 116 | return other.fSwizzle == fSwizzle && |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 117 | other.fPMConversion == fPMConversion; |
| 118 | } |
| 119 | |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 120 | void GrConfigConversionEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 121 | this->updateInvariantOutputForModulation(inout); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /////////////////////////////////////////////////////////////////////////////// |
| 125 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 126 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConfigConversionEffect); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 127 | |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 128 | const GrFragmentProcessor* GrConfigConversionEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 129 | PMConversion pmConv = static_cast<PMConversion>(d->fRandom->nextULessThan(kPMConversionCnt)); |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 130 | GrSwizzle swizzle; |
| 131 | do { |
| 132 | swizzle = GrSwizzle::CreateRandom(d->fRandom); |
| 133 | } while (pmConv == kNone_PMConversion && swizzle == GrSwizzle::RGBA()); |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 134 | return new GrConfigConversionEffect(d->fTextures[GrProcessorUnitTest::kSkiaPMTextureIdx], |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 135 | swizzle, pmConv, GrTest::TestMatrix(d->fRandom)); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /////////////////////////////////////////////////////////////////////////////// |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 139 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 140 | void GrConfigConversionEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, |
| 141 | GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 142 | GrGLConfigConversionEffect::GenKey(*this, caps, b); |
| 143 | } |
| 144 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 145 | GrGLSLFragmentProcessor* GrConfigConversionEffect::onCreateGLSLInstance() const { |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 146 | return new GrGLConfigConversionEffect(); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 147 | } |
| 148 | |
robertphillips | e85a32d | 2015-02-10 08:16:55 -0800 | [diff] [blame] | 149 | |
| 150 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 151 | void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context, |
| 152 | PMConversion* pmToUPMRule, |
| 153 | PMConversion* upmToPMRule) { |
| 154 | *pmToUPMRule = kNone_PMConversion; |
| 155 | *upmToPMRule = kNone_PMConversion; |
| 156 | SkAutoTMalloc<uint32_t> data(256 * 256 * 3); |
| 157 | uint32_t* srcData = data.get(); |
| 158 | uint32_t* firstRead = data.get() + 256 * 256; |
| 159 | uint32_t* secondRead = data.get() + 2 * 256 * 256; |
| 160 | |
| 161 | // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate |
| 162 | // values in row y. We set r,g, and b to the same value since they are handled identically. |
| 163 | for (int y = 0; y < 256; ++y) { |
| 164 | for (int x = 0; x < 256; ++x) { |
| 165 | uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]); |
| 166 | color[3] = y; |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 167 | color[2] = SkTMin(x, y); |
| 168 | color[1] = SkTMin(x, y); |
| 169 | color[0] = SkTMin(x, y); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 173 | GrSurfaceDesc desc; |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 174 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 175 | desc.fWidth = 256; |
| 176 | desc.fHeight = 256; |
| 177 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 178 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 179 | SkAutoTUnref<GrTexture> readTex(context->textureProvider()->createTexture(desc, true, nullptr, 0)); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 180 | if (!readTex.get()) { |
| 181 | return; |
| 182 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 183 | SkAutoTUnref<GrTexture> tempTex(context->textureProvider()->createTexture(desc, true, nullptr, 0)); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 184 | if (!tempTex.get()) { |
| 185 | return; |
| 186 | } |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 187 | desc.fFlags = kNone_GrSurfaceFlags; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 188 | SkAutoTUnref<GrTexture> dataTex(context->textureProvider()->createTexture(desc, true, data, 0)); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 189 | if (!dataTex.get()) { |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | static const PMConversion kConversionRules[][2] = { |
| 194 | {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion}, |
| 195 | {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion}, |
| 196 | }; |
| 197 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 198 | bool failed = true; |
| 199 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 200 | 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] | 201 | *pmToUPMRule = kConversionRules[i][0]; |
| 202 | *upmToPMRule = kConversionRules[i][1]; |
| 203 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 204 | static const SkRect kDstRect = SkRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256)); |
| 205 | static const SkRect kSrcRect = SkRect::MakeWH(SK_Scalar1, SK_Scalar1); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 206 | // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw |
| 207 | // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data. |
| 208 | // We then verify that two reads produced the same values. |
| 209 | |
joshualitt | 5f10b5c | 2015-07-09 10:24:35 -0700 | [diff] [blame] | 210 | GrPaint paint1; |
| 211 | GrPaint paint2; |
| 212 | GrPaint paint3; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 213 | SkAutoTUnref<GrFragmentProcessor> pmToUPM1(new GrConfigConversionEffect( |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 214 | dataTex, GrSwizzle::RGBA(), *pmToUPMRule, SkMatrix::I())); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 215 | SkAutoTUnref<GrFragmentProcessor> upmToPM(new GrConfigConversionEffect( |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 216 | readTex, GrSwizzle::RGBA(), *upmToPMRule, SkMatrix::I())); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 217 | SkAutoTUnref<GrFragmentProcessor> pmToUPM2(new GrConfigConversionEffect( |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 218 | tempTex, GrSwizzle::RGBA(), *pmToUPMRule, SkMatrix::I())); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 219 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 220 | paint1.addColorFragmentProcessor(pmToUPM1); |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 221 | paint1.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 222 | |
| 223 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 224 | SkAutoTUnref<GrDrawContext> readDrawContext( |
| 225 | context->drawContext(readTex->asRenderTarget())); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 226 | if (!readDrawContext) { |
| 227 | failed = true; |
| 228 | break; |
| 229 | } |
| 230 | |
bsalomon | a2e69fc | 2015-11-05 10:41:43 -0800 | [diff] [blame] | 231 | readDrawContext->fillRectToRect(GrClip::WideOpen(), |
| 232 | paint1, |
| 233 | SkMatrix::I(), |
| 234 | kDstRect, |
| 235 | kSrcRect); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 236 | |
| 237 | readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead); |
| 238 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 239 | paint2.addColorFragmentProcessor(upmToPM); |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 240 | paint2.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 241 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 242 | SkAutoTUnref<GrDrawContext> tempDrawContext( |
| 243 | context->drawContext(tempTex->asRenderTarget())); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 244 | if (!tempDrawContext) { |
| 245 | failed = true; |
| 246 | break; |
| 247 | } |
bsalomon | a2e69fc | 2015-11-05 10:41:43 -0800 | [diff] [blame] | 248 | tempDrawContext->fillRectToRect(GrClip::WideOpen(), |
| 249 | paint2, |
| 250 | SkMatrix::I(), |
| 251 | kDstRect, |
| 252 | kSrcRect); |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 253 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 254 | paint3.addColorFragmentProcessor(pmToUPM2); |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 255 | paint3.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 256 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 257 | readDrawContext.reset(context->drawContext(readTex->asRenderTarget())); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 258 | if (!readDrawContext) { |
| 259 | failed = true; |
| 260 | break; |
| 261 | } |
| 262 | |
bsalomon | a2e69fc | 2015-11-05 10:41:43 -0800 | [diff] [blame] | 263 | readDrawContext->fillRectToRect(GrClip::WideOpen(), |
| 264 | paint3, |
| 265 | SkMatrix::I(), |
| 266 | kDstRect, |
| 267 | kSrcRect); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 268 | |
| 269 | readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead); |
| 270 | |
| 271 | failed = false; |
| 272 | for (int y = 0; y < 256 && !failed; ++y) { |
| 273 | for (int x = 0; x <= y; ++x) { |
| 274 | if (firstRead[256 * y + x] != secondRead[256 * y + x]) { |
| 275 | failed = true; |
| 276 | break; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | if (failed) { |
| 282 | *pmToUPMRule = kNone_PMConversion; |
| 283 | *upmToPMRule = kNone_PMConversion; |
| 284 | } |
| 285 | } |
| 286 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 287 | const GrFragmentProcessor* GrConfigConversionEffect::Create(GrTexture* texture, |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 288 | const GrSwizzle& swizzle, |
joshualitt | 5f10b5c | 2015-07-09 10:24:35 -0700 | [diff] [blame] | 289 | PMConversion pmConversion, |
| 290 | const SkMatrix& matrix) { |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 291 | if (swizzle == GrSwizzle::RGBA() && kNone_PMConversion == pmConversion) { |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 292 | // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 293 | // 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] | 294 | // conversions were requested we instead return a GrSimpleTextureEffect. |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 295 | return GrSimpleTextureEffect::Create(texture, matrix); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 296 | } else { |
| 297 | if (kRGBA_8888_GrPixelConfig != texture->config() && |
| 298 | kBGRA_8888_GrPixelConfig != texture->config() && |
| 299 | kNone_PMConversion != pmConversion) { |
| 300 | // The PM conversions assume colors are 0..255 |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 301 | return nullptr; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 302 | } |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 303 | return new GrConfigConversionEffect(texture, swizzle, pmConversion, matrix); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 304 | } |
| 305 | } |