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" |
wangyix | 6af0c93 | 2015-07-22 10:21:17 -0700 | [diff] [blame] | 14 | #include "gl/GrGLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame^] | 15 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 16 | #include "glsl/GrGLSLProgramBuilder.h" |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 17 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 18 | class GrGLConfigConversionEffect : public GrGLFragmentProcessor { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 19 | public: |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 20 | GrGLConfigConversionEffect(const GrProcessor& processor) { |
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 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 27 | virtual void emitCode(EmitArgs& args) override { |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 28 | // 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] | 29 | GrGLSLShaderVar tmpVar("tmpColor", kVec4f_GrSLType, 0, kHigh_GrSLPrecision); |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 30 | SkString tmpDecl; |
egdaniel | f529439 | 2015-10-21 07:14:17 -0700 | [diff] [blame] | 31 | tmpVar.appendDecl(args.fBuilder->glslCaps(), &tmpDecl); |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 32 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame^] | 33 | GrGLSLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 34 | |
| 35 | fsBuilder->codeAppendf("%s;", tmpDecl.c_str()); |
| 36 | |
| 37 | fsBuilder->codeAppendf("%s = ", tmpVar.c_str()); |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 38 | fsBuilder->appendTextureLookup(args.fSamplers[0], args.fCoords[0].c_str(), |
| 39 | args.fCoords[0].getType()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 40 | fsBuilder->codeAppend(";"); |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 41 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 42 | if (GrConfigConversionEffect::kNone_PMConversion == fPMConversion) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 43 | SkASSERT(fSwapRedAndBlue); |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 44 | fsBuilder->codeAppendf("%s = %s.bgra;", args.fOutputColor, tmpVar.c_str()); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 45 | } else { |
| 46 | const char* swiz = fSwapRedAndBlue ? "bgr" : "rgb"; |
| 47 | switch (fPMConversion) { |
| 48 | case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion: |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 49 | fsBuilder->codeAppendf( |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 50 | "%s = vec4(ceil(%s.%s * %s.a * 255.0) / 255.0, %s.a);", |
| 51 | 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] | 52 | break; |
| 53 | case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion: |
commit-bot@chromium.org | b4e200e | 2013-06-19 11:41:02 +0000 | [diff] [blame] | 54 | // Add a compensation(0.001) here to avoid the side effect of the floor operation. |
| 55 | // In Intel GPUs, the integer value converted from floor(%s.r * 255.0) / 255.0 |
| 56 | // is less than the integer value converted from %s.r by 1 when the %s.r is |
| 57 | // 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] | 58 | fsBuilder->codeAppendf( |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 59 | "%s = vec4(floor(%s.%s * %s.a * 255.0 + 0.001) / 255.0, %s.a);", |
| 60 | 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] | 61 | break; |
| 62 | case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion: |
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 = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(ceil(%s.%s / %s.a * 255.0) / 255.0, %s.a);", |
| 65 | 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] | 66 | break; |
| 67 | case GrConfigConversionEffect::kDivByAlpha_RoundDown_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(floor(%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; |
robertphillips@google.com | 2af1b18 | 2012-08-28 11:23:09 +0000 | [diff] [blame] | 72 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 73 | SkFAIL("Unknown conversion op."); |
robertphillips@google.com | 2af1b18 | 2012-08-28 11:23:09 +0000 | [diff] [blame] | 74 | break; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 75 | } |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 76 | fsBuilder->codeAppendf("%s = %s;", args.fOutputColor, tmpVar.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); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 80 | fsBuilder->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) { |
| 85 | const GrConfigConversionEffect& conv = processor.cast<GrConfigConversionEffect>(); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 86 | uint32_t key = (conv.swapsRedAndBlue() ? 0 : 1) | (conv.pmConversion() << 1); |
| 87 | b->add32(key); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | private: |
| 91 | bool fSwapRedAndBlue; |
| 92 | GrConfigConversionEffect::PMConversion fPMConversion; |
| 93 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 94 | typedef GrGLFragmentProcessor INHERITED; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 95 | |
| 96 | }; |
| 97 | |
| 98 | /////////////////////////////////////////////////////////////////////////////// |
| 99 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 100 | GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture, |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 101 | bool swapRedAndBlue, |
bsalomon@google.com | b1456d7 | 2012-11-02 18:23:45 +0000 | [diff] [blame] | 102 | PMConversion pmConversion, |
| 103 | const SkMatrix& matrix) |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 104 | : INHERITED(texture, matrix) |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 105 | , fSwapRedAndBlue(swapRedAndBlue) |
| 106 | , fPMConversion(pmConversion) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 107 | this->initClassID<GrConfigConversionEffect>(); |
bsalomon | 445fc43 | 2015-08-03 10:15:25 -0700 | [diff] [blame] | 108 | // We expect to get here with non-BGRA/RGBA only if we're doing not doing a premul/unpremul |
| 109 | // conversion. |
| 110 | SkASSERT((kRGBA_8888_GrPixelConfig == texture->config() || |
| 111 | kBGRA_8888_GrPixelConfig == texture->config()) || |
| 112 | kNone_PMConversion == pmConversion); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 113 | // 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] | 114 | SkASSERT(swapRedAndBlue || kNone_PMConversion != pmConversion); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 115 | } |
| 116 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 117 | bool GrConfigConversionEffect::onIsEqual(const GrFragmentProcessor& s) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 118 | const GrConfigConversionEffect& other = s.cast<GrConfigConversionEffect>(); |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 119 | return other.fSwapRedAndBlue == fSwapRedAndBlue && |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 120 | other.fPMConversion == fPMConversion; |
| 121 | } |
| 122 | |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 123 | void GrConfigConversionEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 124 | this->updateInvariantOutputForModulation(inout); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /////////////////////////////////////////////////////////////////////////////// |
| 128 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 129 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConfigConversionEffect); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 130 | |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 131 | const GrFragmentProcessor* GrConfigConversionEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 132 | PMConversion pmConv = static_cast<PMConversion>(d->fRandom->nextULessThan(kPMConversionCnt)); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 133 | bool swapRB; |
| 134 | if (kNone_PMConversion == pmConv) { |
| 135 | swapRB = true; |
| 136 | } else { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 137 | swapRB = d->fRandom->nextBool(); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 138 | } |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 139 | return new GrConfigConversionEffect(d->fTextures[GrProcessorUnitTest::kSkiaPMTextureIdx], |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 140 | swapRB, pmConv, GrTest::TestMatrix(d->fRandom)); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /////////////////////////////////////////////////////////////////////////////// |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 144 | |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 145 | void GrConfigConversionEffect::onGetGLProcessorKey(const GrGLSLCaps& caps, |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame^] | 146 | GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 147 | GrGLConfigConversionEffect::GenKey(*this, caps, b); |
| 148 | } |
| 149 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 150 | GrGLFragmentProcessor* GrConfigConversionEffect::onCreateGLInstance() const { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 151 | return new GrGLConfigConversionEffect(*this); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 152 | } |
| 153 | |
robertphillips | e85a32d | 2015-02-10 08:16:55 -0800 | [diff] [blame] | 154 | |
| 155 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 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 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 178 | GrSurfaceDesc desc; |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 179 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 180 | desc.fWidth = 256; |
| 181 | desc.fHeight = 256; |
| 182 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 183 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 184 | SkAutoTUnref<GrTexture> readTex(context->textureProvider()->createTexture(desc, true, nullptr, 0)); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 185 | if (!readTex.get()) { |
| 186 | return; |
| 187 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 188 | SkAutoTUnref<GrTexture> tempTex(context->textureProvider()->createTexture(desc, true, nullptr, 0)); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 189 | if (!tempTex.get()) { |
| 190 | return; |
| 191 | } |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 192 | desc.fFlags = kNone_GrSurfaceFlags; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 193 | SkAutoTUnref<GrTexture> dataTex(context->textureProvider()->createTexture(desc, true, data, 0)); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 194 | if (!dataTex.get()) { |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | static const PMConversion kConversionRules[][2] = { |
| 199 | {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion}, |
| 200 | {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion}, |
| 201 | }; |
| 202 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 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 | |
joshualitt | 5f10b5c | 2015-07-09 10:24:35 -0700 | [diff] [blame] | 215 | GrPaint paint1; |
| 216 | GrPaint paint2; |
| 217 | GrPaint paint3; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 218 | SkAutoTUnref<GrFragmentProcessor> pmToUPM1(new GrConfigConversionEffect( |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 219 | dataTex, false, *pmToUPMRule, SkMatrix::I())); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 220 | SkAutoTUnref<GrFragmentProcessor> upmToPM(new GrConfigConversionEffect( |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 221 | readTex, false, *upmToPMRule, SkMatrix::I())); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 222 | SkAutoTUnref<GrFragmentProcessor> pmToUPM2(new GrConfigConversionEffect( |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 223 | tempTex, false, *pmToUPMRule, SkMatrix::I())); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 224 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 225 | paint1.addColorFragmentProcessor(pmToUPM1); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 226 | |
| 227 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 228 | SkAutoTUnref<GrDrawContext> readDrawContext( |
| 229 | context->drawContext(readTex->asRenderTarget())); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 230 | if (!readDrawContext) { |
| 231 | failed = true; |
| 232 | break; |
| 233 | } |
| 234 | |
bsalomon | a2e69fc | 2015-11-05 10:41:43 -0800 | [diff] [blame] | 235 | readDrawContext->fillRectToRect(GrClip::WideOpen(), |
| 236 | paint1, |
| 237 | SkMatrix::I(), |
| 238 | kDstRect, |
| 239 | kSrcRect); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 240 | |
| 241 | readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead); |
| 242 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 243 | paint2.addColorFragmentProcessor(upmToPM); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 244 | |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 245 | SkAutoTUnref<GrDrawContext> tempDrawContext( |
| 246 | context->drawContext(tempTex->asRenderTarget())); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 247 | if (!tempDrawContext) { |
| 248 | failed = true; |
| 249 | break; |
| 250 | } |
bsalomon | a2e69fc | 2015-11-05 10:41:43 -0800 | [diff] [blame] | 251 | tempDrawContext->fillRectToRect(GrClip::WideOpen(), |
| 252 | paint2, |
| 253 | SkMatrix::I(), |
| 254 | kDstRect, |
| 255 | kSrcRect); |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 256 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 257 | paint3.addColorFragmentProcessor(pmToUPM2); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 258 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 259 | readDrawContext.reset(context->drawContext(readTex->asRenderTarget())); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 260 | if (!readDrawContext) { |
| 261 | failed = true; |
| 262 | break; |
| 263 | } |
| 264 | |
bsalomon | a2e69fc | 2015-11-05 10:41:43 -0800 | [diff] [blame] | 265 | readDrawContext->fillRectToRect(GrClip::WideOpen(), |
| 266 | paint3, |
| 267 | SkMatrix::I(), |
| 268 | kDstRect, |
| 269 | kSrcRect); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 270 | |
| 271 | readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead); |
| 272 | |
| 273 | failed = false; |
| 274 | for (int y = 0; y < 256 && !failed; ++y) { |
| 275 | for (int x = 0; x <= y; ++x) { |
| 276 | if (firstRead[256 * y + x] != secondRead[256 * y + x]) { |
| 277 | failed = true; |
| 278 | break; |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | if (failed) { |
| 284 | *pmToUPMRule = kNone_PMConversion; |
| 285 | *upmToPMRule = kNone_PMConversion; |
| 286 | } |
| 287 | } |
| 288 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 289 | const GrFragmentProcessor* GrConfigConversionEffect::Create(GrTexture* texture, |
joshualitt | 5f10b5c | 2015-07-09 10:24:35 -0700 | [diff] [blame] | 290 | bool swapRedAndBlue, |
| 291 | PMConversion pmConversion, |
| 292 | const SkMatrix& matrix) { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 293 | if (!swapRedAndBlue && kNone_PMConversion == pmConversion) { |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 294 | // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 295 | // 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] | 296 | // conversions were requested we instead return a GrSimpleTextureEffect. |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 297 | return GrSimpleTextureEffect::Create(texture, matrix); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 298 | } else { |
| 299 | if (kRGBA_8888_GrPixelConfig != texture->config() && |
| 300 | kBGRA_8888_GrPixelConfig != texture->config() && |
| 301 | kNone_PMConversion != pmConversion) { |
| 302 | // The PM conversions assume colors are 0..255 |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 303 | return nullptr; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 304 | } |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 305 | return new GrConfigConversionEffect(texture, swapRedAndBlue, pmConversion, matrix); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 306 | } |
| 307 | } |