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, |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 30 | const TextureSamplerArray& samplers) SK_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 | |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 36 | GrGLFPFragmentBuilder* 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 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 85 | static inline void GenKey(const GrProcessor& processor, const GrGLCaps&, |
| 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 | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 145 | GrProcessorUnitTest::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 | |
| 150 | void GrConfigConversionEffect::getGLProcessorKey(const GrGLCaps& caps, |
| 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; |
| 184 | desc.fFlags = kRenderTarget_GrSurfaceFlag | |
| 185 | kNoStencil_GrSurfaceFlag; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 186 | desc.fWidth = 256; |
| 187 | desc.fHeight = 256; |
| 188 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 189 | |
bsalomon | d042358 | 2015-02-06 08:49:24 -0800 | [diff] [blame] | 190 | SkAutoTUnref<GrTexture> readTex(context->createTexture(desc, true, NULL, 0)); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 191 | if (!readTex.get()) { |
| 192 | return; |
| 193 | } |
bsalomon | d042358 | 2015-02-06 08:49:24 -0800 | [diff] [blame] | 194 | SkAutoTUnref<GrTexture> tempTex(context->createTexture(desc, true, NULL, 0)); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 195 | if (!tempTex.get()) { |
| 196 | return; |
| 197 | } |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 198 | desc.fFlags = kNone_GrSurfaceFlags; |
bsalomon | d042358 | 2015-02-06 08:49:24 -0800 | [diff] [blame] | 199 | SkAutoTUnref<GrTexture> dataTex(context->createTexture(desc, true, data, 0)); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 200 | if (!dataTex.get()) { |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | static const PMConversion kConversionRules[][2] = { |
| 205 | {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion}, |
| 206 | {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion}, |
| 207 | }; |
| 208 | |
| 209 | GrContext::AutoWideOpenIdentityDraw awoid(context, NULL); |
| 210 | |
| 211 | bool failed = true; |
| 212 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 213 | 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] | 214 | *pmToUPMRule = kConversionRules[i][0]; |
| 215 | *upmToPMRule = kConversionRules[i][1]; |
| 216 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 217 | static const SkRect kDstRect = SkRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256)); |
| 218 | static const SkRect kSrcRect = SkRect::MakeWH(SK_Scalar1, SK_Scalar1); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 219 | // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw |
| 220 | // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data. |
| 221 | // We then verify that two reads produced the same values. |
| 222 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 223 | SkAutoTUnref<GrFragmentProcessor> pmToUPM1( |
| 224 | SkNEW_ARGS(GrConfigConversionEffect, |
| 225 | (dataTex, false, *pmToUPMRule, SkMatrix::I()))); |
| 226 | SkAutoTUnref<GrFragmentProcessor> upmToPM( |
| 227 | SkNEW_ARGS(GrConfigConversionEffect, |
| 228 | (readTex, false, *upmToPMRule, SkMatrix::I()))); |
| 229 | SkAutoTUnref<GrFragmentProcessor> pmToUPM2( |
| 230 | SkNEW_ARGS(GrConfigConversionEffect, |
| 231 | (tempTex, false, *pmToUPMRule, SkMatrix::I()))); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 232 | |
| 233 | context->setRenderTarget(readTex->asRenderTarget()); |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 234 | GrPaint paint1; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 235 | paint1.addColorProcessor(pmToUPM1); |
joshualitt | 16b2789 | 2014-12-18 07:47:16 -0800 | [diff] [blame] | 236 | context->drawNonAARectToRect(paint1, SkMatrix::I(), kDstRect, kSrcRect); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 237 | |
| 238 | readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead); |
| 239 | |
| 240 | context->setRenderTarget(tempTex->asRenderTarget()); |
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 | 16b2789 | 2014-12-18 07:47:16 -0800 | [diff] [blame] | 243 | context->drawNonAARectToRect(paint2, SkMatrix::I(), kDstRect, kSrcRect); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 244 | context->setRenderTarget(readTex->asRenderTarget()); |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 245 | |
| 246 | GrPaint paint3; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 247 | paint3.addColorProcessor(pmToUPM2); |
joshualitt | 16b2789 | 2014-12-18 07:47:16 -0800 | [diff] [blame] | 248 | context->drawNonAARectToRect(paint3, SkMatrix::I(), kDstRect, kSrcRect); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 249 | |
| 250 | readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead); |
| 251 | |
| 252 | failed = false; |
| 253 | for (int y = 0; y < 256 && !failed; ++y) { |
| 254 | for (int x = 0; x <= y; ++x) { |
| 255 | if (firstRead[256 * y + x] != secondRead[256 * y + x]) { |
| 256 | failed = true; |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | if (failed) { |
| 263 | *pmToUPMRule = kNone_PMConversion; |
| 264 | *upmToPMRule = kNone_PMConversion; |
| 265 | } |
| 266 | } |
| 267 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 268 | const GrFragmentProcessor* GrConfigConversionEffect::Create(GrTexture* texture, |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 269 | bool swapRedAndBlue, |
| 270 | PMConversion pmConversion, |
| 271 | const SkMatrix& matrix) { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 272 | if (!swapRedAndBlue && kNone_PMConversion == pmConversion) { |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 273 | // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 274 | // 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] | 275 | // conversions were requested we instead return a GrSimpleTextureEffect. |
| 276 | return GrSimpleTextureEffect::Create(texture, matrix); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 277 | } else { |
| 278 | if (kRGBA_8888_GrPixelConfig != texture->config() && |
| 279 | kBGRA_8888_GrPixelConfig != texture->config() && |
| 280 | kNone_PMConversion != pmConversion) { |
| 281 | // The PM conversions assume colors are 0..255 |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 282 | return NULL; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 283 | } |
bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 284 | return SkNEW_ARGS(GrConfigConversionEffect, (texture, |
| 285 | swapRedAndBlue, |
| 286 | pmConversion, |
| 287 | matrix)); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 288 | } |
| 289 | } |