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" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 10 | #include "GrInvariantOutput.h" |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 11 | #include "GrSimpleTextureEffect.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 12 | #include "SkMatrix.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 13 | #include "gl/GrGLProcessor.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 14 | #include "gl/builders/GrGLProgramBuilder.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 | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 18 | GrGLConfigConversionEffect(const GrProcessor& processor) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 19 | const GrConfigConversionEffect& configConversionEffect = |
| 20 | processor.cast<GrConfigConversionEffect>(); |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 21 | fSwapRedAndBlue = configConversionEffect.swapsRedAndBlue(); |
| 22 | fPMConversion = configConversionEffect.pmConversion(); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 23 | } |
| 24 | |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 25 | virtual void emitCode(GrGLFPBuilder* builder, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 26 | const GrFragmentProcessor&, |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 27 | const char* outputColor, |
| 28 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 29 | const TransformedCoordsArray& coords, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 30 | const TextureSamplerArray& samplers) override { |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 31 | // Using highp for GLES here in order to avoid some precision issues on specific GPUs. |
bsalomon | c0bd648 | 2014-12-09 10:04:14 -0800 | [diff] [blame] | 32 | GrGLShaderVar tmpVar("tmpColor", kVec4f_GrSLType, 0, kHigh_GrSLPrecision); |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 33 | SkString tmpDecl; |
| 34 | tmpVar.appendDecl(builder->ctxInfo(), &tmpDecl); |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 35 | |
egdaniel | 29bee0f | 2015-04-29 11:54:42 -0700 | [diff] [blame] | 36 | GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 37 | |
| 38 | fsBuilder->codeAppendf("%s;", tmpDecl.c_str()); |
| 39 | |
| 40 | fsBuilder->codeAppendf("%s = ", tmpVar.c_str()); |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 41 | fsBuilder->appendTextureLookup(samplers[0], coords[0].c_str(), coords[0].getType()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 42 | fsBuilder->codeAppend(";"); |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 43 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 44 | if (GrConfigConversionEffect::kNone_PMConversion == fPMConversion) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 45 | SkASSERT(fSwapRedAndBlue); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 46 | fsBuilder->codeAppendf("%s = %s.bgra;", outputColor, tmpVar.c_str()); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 47 | } else { |
| 48 | const char* swiz = fSwapRedAndBlue ? "bgr" : "rgb"; |
| 49 | switch (fPMConversion) { |
| 50 | case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion: |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 51 | fsBuilder->codeAppendf( |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 52 | "%s = vec4(ceil(%s.%s * %s.a * 255.0) / 255.0, %s.a);", |
| 53 | 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] | 54 | break; |
| 55 | case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion: |
commit-bot@chromium.org | b4e200e | 2013-06-19 11:41:02 +0000 | [diff] [blame] | 56 | // Add a compensation(0.001) here to avoid the side effect of the floor operation. |
| 57 | // In Intel GPUs, the integer value converted from floor(%s.r * 255.0) / 255.0 |
| 58 | // is less than the integer value converted from %s.r by 1 when the %s.r is |
| 59 | // 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] | 60 | fsBuilder->codeAppendf( |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 61 | "%s = vec4(floor(%s.%s * %s.a * 255.0 + 0.001) / 255.0, %s.a);", |
| 62 | 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] | 63 | break; |
| 64 | case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion: |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 65 | fsBuilder->codeAppendf( |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 66 | "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(ceil(%s.%s / %s.a * 255.0) / 255.0, %s.a);", |
| 67 | 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] | 68 | break; |
| 69 | case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion: |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 70 | fsBuilder->codeAppendf( |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 71 | "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(floor(%s.%s / %s.a * 255.0) / 255.0, %s.a);", |
| 72 | 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] | 73 | break; |
robertphillips@google.com | 2af1b18 | 2012-08-28 11:23:09 +0000 | [diff] [blame] | 74 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 75 | SkFAIL("Unknown conversion op."); |
robertphillips@google.com | 2af1b18 | 2012-08-28 11:23:09 +0000 | [diff] [blame] | 76 | break; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 77 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 78 | fsBuilder->codeAppendf("%s = %s;", outputColor, tmpVar.c_str()); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 79 | } |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 80 | SkString modulate; |
egdaniel | 089f8de | 2014-10-09 10:34:58 -0700 | [diff] [blame] | 81 | GrGLSLMulVarBy4f(&modulate, outputColor, inputColor); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 82 | fsBuilder->codeAppend(modulate.c_str()); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 83 | } |
| 84 | |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 85 | static inline void GenKey(const GrProcessor& processor, const GrGLSLCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 86 | GrProcessorKeyBuilder* b) { |
| 87 | const GrConfigConversionEffect& conv = processor.cast<GrConfigConversionEffect>(); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 88 | uint32_t key = (conv.swapsRedAndBlue() ? 0 : 1) | (conv.pmConversion() << 1); |
| 89 | b->add32(key); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | private: |
| 93 | bool fSwapRedAndBlue; |
| 94 | GrConfigConversionEffect::PMConversion fPMConversion; |
| 95 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 96 | typedef GrGLFragmentProcessor INHERITED; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 97 | |
| 98 | }; |
| 99 | |
| 100 | /////////////////////////////////////////////////////////////////////////////// |
| 101 | |
| 102 | GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture, |
| 103 | bool swapRedAndBlue, |
bsalomon@google.com | b1456d7 | 2012-11-02 18:23:45 +0000 | [diff] [blame] | 104 | PMConversion pmConversion, |
| 105 | const SkMatrix& matrix) |
| 106 | : GrSingleTextureEffect(texture, matrix) |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 107 | , fSwapRedAndBlue(swapRedAndBlue) |
| 108 | , fPMConversion(pmConversion) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 109 | this->initClassID<GrConfigConversionEffect>(); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 110 | SkASSERT(kRGBA_8888_GrPixelConfig == texture->config() || |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 111 | kBGRA_8888_GrPixelConfig == texture->config()); |
| 112 | // 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] | 113 | SkASSERT(swapRedAndBlue || kNone_PMConversion != pmConversion); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 114 | } |
| 115 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 116 | bool GrConfigConversionEffect::onIsEqual(const GrFragmentProcessor& s) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 117 | const GrConfigConversionEffect& other = s.cast<GrConfigConversionEffect>(); |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 118 | return other.fSwapRedAndBlue == fSwapRedAndBlue && |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 119 | other.fPMConversion == fPMConversion; |
| 120 | } |
| 121 | |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 122 | void GrConfigConversionEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 123 | this->updateInvariantOutputForModulation(inout); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /////////////////////////////////////////////////////////////////////////////// |
| 127 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 128 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConfigConversionEffect); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 129 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 130 | GrFragmentProcessor* GrConfigConversionEffect::TestCreate(SkRandom* random, |
| 131 | GrContext*, |
| 132 | const GrDrawTargetCaps&, |
| 133 | GrTexture* textures[]) { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 134 | PMConversion pmConv = static_cast<PMConversion>(random->nextULessThan(kPMConversionCnt)); |
| 135 | bool swapRB; |
| 136 | if (kNone_PMConversion == pmConv) { |
| 137 | swapRB = true; |
| 138 | } else { |
| 139 | swapRB = random->nextBool(); |
| 140 | } |
bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 141 | return SkNEW_ARGS(GrConfigConversionEffect, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 142 | (textures[GrProcessorUnitTest::kSkiaPMTextureIdx], |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 143 | swapRB, |
| 144 | pmConv, |
joshualitt | 4eaf9ce | 2015-04-28 13:31:18 -0700 | [diff] [blame] | 145 | GrTest::TestMatrix(random))); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /////////////////////////////////////////////////////////////////////////////// |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 149 | |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 150 | void GrConfigConversionEffect::getGLProcessorKey(const GrGLSLCaps& caps, |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 151 | GrProcessorKeyBuilder* b) const { |
| 152 | GrGLConfigConversionEffect::GenKey(*this, caps, b); |
| 153 | } |
| 154 | |
| 155 | GrGLFragmentProcessor* GrConfigConversionEffect::createGLInstance() const { |
| 156 | return SkNEW_ARGS(GrGLConfigConversionEffect, (*this)); |
| 157 | } |
| 158 | |
robertphillips | e85a32d | 2015-02-10 08:16:55 -0800 | [diff] [blame] | 159 | |
| 160 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 161 | void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context, |
| 162 | PMConversion* pmToUPMRule, |
| 163 | PMConversion* upmToPMRule) { |
| 164 | *pmToUPMRule = kNone_PMConversion; |
| 165 | *upmToPMRule = kNone_PMConversion; |
| 166 | SkAutoTMalloc<uint32_t> data(256 * 256 * 3); |
| 167 | uint32_t* srcData = data.get(); |
| 168 | uint32_t* firstRead = data.get() + 256 * 256; |
| 169 | uint32_t* secondRead = data.get() + 2 * 256 * 256; |
| 170 | |
| 171 | // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate |
| 172 | // values in row y. We set r,g, and b to the same value since they are handled identically. |
| 173 | for (int y = 0; y < 256; ++y) { |
| 174 | for (int x = 0; x < 256; ++x) { |
| 175 | uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]); |
| 176 | color[3] = y; |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 177 | color[2] = SkTMin(x, y); |
| 178 | color[1] = SkTMin(x, y); |
| 179 | color[0] = SkTMin(x, y); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 183 | GrSurfaceDesc desc; |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 184 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 185 | desc.fWidth = 256; |
| 186 | desc.fHeight = 256; |
| 187 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 188 | |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame^] | 189 | SkAutoTUnref<GrTexture> readTex(context->textureProvider()->createTexture(desc, true, NULL, 0)); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 190 | if (!readTex.get()) { |
| 191 | return; |
| 192 | } |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame^] | 193 | SkAutoTUnref<GrTexture> tempTex(context->textureProvider()->createTexture(desc, true, NULL, 0)); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 194 | if (!tempTex.get()) { |
| 195 | return; |
| 196 | } |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 197 | desc.fFlags = kNone_GrSurfaceFlags; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame^] | 198 | SkAutoTUnref<GrTexture> dataTex(context->textureProvider()->createTexture(desc, true, data, 0)); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 199 | if (!dataTex.get()) { |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | static const PMConversion kConversionRules[][2] = { |
| 204 | {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion}, |
| 205 | {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion}, |
| 206 | }; |
| 207 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 208 | bool failed = true; |
| 209 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 210 | 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] | 211 | *pmToUPMRule = kConversionRules[i][0]; |
| 212 | *upmToPMRule = kConversionRules[i][1]; |
| 213 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 214 | static const SkRect kDstRect = SkRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256)); |
| 215 | static const SkRect kSrcRect = SkRect::MakeWH(SK_Scalar1, SK_Scalar1); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 216 | // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw |
| 217 | // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data. |
| 218 | // We then verify that two reads produced the same values. |
| 219 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 220 | SkAutoTUnref<GrFragmentProcessor> pmToUPM1( |
| 221 | SkNEW_ARGS(GrConfigConversionEffect, |
| 222 | (dataTex, false, *pmToUPMRule, SkMatrix::I()))); |
| 223 | SkAutoTUnref<GrFragmentProcessor> upmToPM( |
| 224 | SkNEW_ARGS(GrConfigConversionEffect, |
| 225 | (readTex, false, *upmToPMRule, SkMatrix::I()))); |
| 226 | SkAutoTUnref<GrFragmentProcessor> pmToUPM2( |
| 227 | SkNEW_ARGS(GrConfigConversionEffect, |
| 228 | (tempTex, false, *pmToUPMRule, SkMatrix::I()))); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 229 | |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 230 | GrPaint paint1; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 231 | paint1.addColorProcessor(pmToUPM1); |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 232 | context->drawNonAARectToRect(readTex->asRenderTarget(), |
| 233 | GrClip::WideOpen(), |
| 234 | paint1, |
| 235 | SkMatrix::I(), |
| 236 | kDstRect, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 237 | kSrcRect); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 238 | |
| 239 | readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead); |
| 240 | |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 241 | GrPaint paint2; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 242 | paint2.addColorProcessor(upmToPM); |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 243 | context->drawNonAARectToRect(tempTex->asRenderTarget(), |
| 244 | GrClip::WideOpen(), |
| 245 | paint2, |
| 246 | SkMatrix::I(), |
| 247 | kDstRect, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 248 | kSrcRect); |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 249 | |
| 250 | GrPaint paint3; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 251 | paint3.addColorProcessor(pmToUPM2); |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 252 | context->drawNonAARectToRect(readTex->asRenderTarget(), |
| 253 | GrClip::WideOpen(), |
| 254 | paint3, |
| 255 | SkMatrix::I(), |
| 256 | kDstRect, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 257 | kSrcRect); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 258 | |
| 259 | readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead); |
| 260 | |
| 261 | failed = false; |
| 262 | for (int y = 0; y < 256 && !failed; ++y) { |
| 263 | for (int x = 0; x <= y; ++x) { |
| 264 | if (firstRead[256 * y + x] != secondRead[256 * y + x]) { |
| 265 | failed = true; |
| 266 | break; |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | if (failed) { |
| 272 | *pmToUPMRule = kNone_PMConversion; |
| 273 | *upmToPMRule = kNone_PMConversion; |
| 274 | } |
| 275 | } |
| 276 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 277 | const GrFragmentProcessor* GrConfigConversionEffect::Create(GrTexture* texture, |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 278 | bool swapRedAndBlue, |
| 279 | PMConversion pmConversion, |
| 280 | const SkMatrix& matrix) { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 281 | if (!swapRedAndBlue && kNone_PMConversion == pmConversion) { |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 282 | // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 283 | // 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] | 284 | // conversions were requested we instead return a GrSimpleTextureEffect. |
| 285 | return GrSimpleTextureEffect::Create(texture, matrix); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 286 | } else { |
| 287 | if (kRGBA_8888_GrPixelConfig != texture->config() && |
| 288 | kBGRA_8888_GrPixelConfig != texture->config() && |
| 289 | kNone_PMConversion != pmConversion) { |
| 290 | // The PM conversions assume colors are 0..255 |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 291 | return NULL; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 292 | } |
bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 293 | return SkNEW_ARGS(GrConfigConversionEffect, (texture, |
| 294 | swapRedAndBlue, |
| 295 | pmConversion, |
| 296 | matrix)); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 297 | } |
| 298 | } |