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" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 10 | #include "GrRenderTargetContext.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" |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 16 | #include "../private/GrGLSL.h" |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 17 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 18 | class GrGLConfigConversionEffect : public GrGLSLFragmentProcessor { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 19 | public: |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 20 | void emitCode(EmitArgs& args) override { |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 21 | const GrConfigConversionEffect& cce = args.fFp.cast<GrConfigConversionEffect>(); |
| 22 | const GrSwizzle& swizzle = cce.swizzle(); |
| 23 | GrConfigConversionEffect::PMConversion pmConversion = cce.pmConversion(); |
| 24 | |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 25 | // Using highp for GLES here in order to avoid some precision issues on specific GPUs. |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 26 | GrShaderVar tmpVar("tmpColor", kVec4f_GrSLType, 0, kHigh_GrSLPrecision); |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 27 | SkString tmpDecl; |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 28 | tmpVar.appendDecl(args.fShaderCaps, &tmpDecl); |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 29 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 30 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 31 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 32 | fragBuilder->codeAppendf("%s;", tmpDecl.c_str()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 33 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 34 | fragBuilder->codeAppendf("%s = ", tmpVar.c_str()); |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 35 | fragBuilder->appendTextureLookup(args.fTexSamplers[0], args.fTransformedCoords[0].c_str(), |
| 36 | args.fTransformedCoords[0].getType()); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 37 | fragBuilder->codeAppend(";"); |
changjun.yang | cecc91c | 2014-08-19 18:24:30 -0700 | [diff] [blame] | 38 | |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 39 | if (GrConfigConversionEffect::kNone_PMConversion == pmConversion) { |
| 40 | SkASSERT(GrSwizzle::RGBA() != swizzle); |
| 41 | fragBuilder->codeAppendf("%s = %s.%s;", args.fOutputColor, tmpVar.c_str(), |
| 42 | swizzle.c_str()); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 43 | } else { |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 44 | switch (pmConversion) { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 45 | case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion: |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 46 | fragBuilder->codeAppendf( |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 47 | "%s = vec4(ceil(%s.rgb * %s.a * 255.0) / 255.0, %s.a);", |
| 48 | 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] | 49 | break; |
| 50 | case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion: |
commit-bot@chromium.org | b4e200e | 2013-06-19 11:41:02 +0000 | [diff] [blame] | 51 | // Add a compensation(0.001) here to avoid the side effect of the floor operation. |
| 52 | // In Intel GPUs, the integer value converted from floor(%s.r * 255.0) / 255.0 |
| 53 | // is less than the integer value converted from %s.r by 1 when the %s.r is |
| 54 | // 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] | 55 | fragBuilder->codeAppendf( |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 56 | "%s = vec4(floor(%s.rgb * %s.a * 255.0 + 0.001) / 255.0, %s.a);", |
| 57 | tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str()); |
| 58 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 59 | break; |
| 60 | case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion: |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 61 | fragBuilder->codeAppendf( |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 62 | "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(ceil(%s.rgb / %s.a * 255.0) / 255.0, %s.a);", |
| 63 | tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), |
| 64 | tmpVar.c_str()); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 65 | break; |
| 66 | case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion: |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 67 | fragBuilder->codeAppendf( |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 68 | "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(floor(%s.rgb / %s.a * 255.0) / 255.0, %s.a);", |
| 69 | tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), |
| 70 | 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 | } |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 76 | fragBuilder->codeAppendf("%s = %s.%s;", args.fOutputColor, tmpVar.c_str(), |
| 77 | swizzle.c_str()); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 78 | } |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 79 | SkString modulate; |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 80 | GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 81 | fragBuilder->codeAppend(modulate.c_str()); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 84 | static inline void GenKey(const GrProcessor& processor, const GrShaderCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 85 | GrProcessorKeyBuilder* b) { |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 86 | const GrConfigConversionEffect& cce = processor.cast<GrConfigConversionEffect>(); |
| 87 | uint32_t key = (cce.swizzle().asKey()) | (cce.pmConversion() << 16); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 88 | b->add32(key); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | private: |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 92 | typedef GrGLSLFragmentProcessor INHERITED; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 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) |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 101 | : INHERITED(texture, nullptr, 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 | |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame^] | 114 | GrConfigConversionEffect::GrConfigConversionEffect(GrContext* context, |
| 115 | sk_sp<GrTextureProxy> proxy, |
| 116 | const GrSwizzle& swizzle, |
| 117 | PMConversion pmConversion, |
| 118 | const SkMatrix& matrix) |
| 119 | : INHERITED(context, proxy, nullptr, matrix) |
| 120 | , fSwizzle(swizzle) |
| 121 | , fPMConversion(pmConversion) { |
| 122 | this->initClassID<GrConfigConversionEffect>(); |
| 123 | // We expect to get here with non-BGRA/RGBA only if we're doing not doing a premul/unpremul |
| 124 | // conversion. |
| 125 | SkASSERT((kRGBA_8888_GrPixelConfig == proxy->config() || |
| 126 | kBGRA_8888_GrPixelConfig == proxy->config()) || |
| 127 | kNone_PMConversion == pmConversion); |
| 128 | // Why did we pollute our texture cache instead of using a GrSingleTextureEffect? |
| 129 | SkASSERT(swizzle != GrSwizzle::RGBA() || kNone_PMConversion != pmConversion); |
| 130 | } |
| 131 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 132 | bool GrConfigConversionEffect::onIsEqual(const GrFragmentProcessor& s) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 133 | const GrConfigConversionEffect& other = s.cast<GrConfigConversionEffect>(); |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 134 | return other.fSwizzle == fSwizzle && |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 135 | other.fPMConversion == fPMConversion; |
| 136 | } |
| 137 | |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 138 | void GrConfigConversionEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 139 | this->updateInvariantOutputForModulation(inout); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /////////////////////////////////////////////////////////////////////////////// |
| 143 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 144 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConfigConversionEffect); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 145 | |
brucedawson | 3c3e0d7 | 2016-09-06 09:54:44 -0700 | [diff] [blame] | 146 | #if !defined(__clang__) && _MSC_FULL_VER >= 190024213 |
| 147 | // Work around VS 2015 Update 3 optimizer bug that causes internal compiler error |
| 148 | //https://connect.microsoft.com/VisualStudio/feedback/details/3100520/internal-compiler-error |
| 149 | #pragma optimize("t", off) |
| 150 | #endif |
| 151 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 152 | sk_sp<GrFragmentProcessor> GrConfigConversionEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 153 | PMConversion pmConv = static_cast<PMConversion>(d->fRandom->nextULessThan(kPMConversionCnt)); |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 154 | GrSwizzle swizzle; |
| 155 | do { |
| 156 | swizzle = GrSwizzle::CreateRandom(d->fRandom); |
| 157 | } while (pmConv == kNone_PMConversion && swizzle == GrSwizzle::RGBA()); |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame^] | 158 | return sk_sp<GrFragmentProcessor>(new GrConfigConversionEffect( |
| 159 | d->context(), |
| 160 | d->textureProxy(GrProcessorUnitTest::kSkiaPMTextureIdx), |
| 161 | swizzle, pmConv, GrTest::TestMatrix(d->fRandom))); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 162 | } |
| 163 | |
brucedawson | 3c3e0d7 | 2016-09-06 09:54:44 -0700 | [diff] [blame] | 164 | #if !defined(__clang__) && _MSC_FULL_VER >= 190024213 |
| 165 | // Restore optimization settings. |
| 166 | #pragma optimize("", on) |
| 167 | #endif |
| 168 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 169 | /////////////////////////////////////////////////////////////////////////////// |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 170 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 171 | void GrConfigConversionEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 172 | GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 173 | GrGLConfigConversionEffect::GenKey(*this, caps, b); |
| 174 | } |
| 175 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 176 | GrGLSLFragmentProcessor* GrConfigConversionEffect::onCreateGLSLInstance() const { |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 177 | return new GrGLConfigConversionEffect(); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 178 | } |
| 179 | |
robertphillips | e85a32d | 2015-02-10 08:16:55 -0800 | [diff] [blame] | 180 | |
| 181 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 182 | void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context, |
| 183 | PMConversion* pmToUPMRule, |
| 184 | PMConversion* upmToPMRule) { |
| 185 | *pmToUPMRule = kNone_PMConversion; |
| 186 | *upmToPMRule = kNone_PMConversion; |
bsalomon | 49b264c | 2016-07-19 08:38:09 -0700 | [diff] [blame] | 187 | static constexpr int kSize = 256; |
| 188 | static constexpr GrPixelConfig kConfig = kRGBA_8888_GrPixelConfig; |
| 189 | SkAutoTMalloc<uint32_t> data(kSize * kSize * 3); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 190 | uint32_t* srcData = data.get(); |
bsalomon | 49b264c | 2016-07-19 08:38:09 -0700 | [diff] [blame] | 191 | uint32_t* firstRead = data.get() + kSize * kSize; |
| 192 | uint32_t* secondRead = data.get() + 2 * kSize * kSize; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 193 | |
| 194 | // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate |
| 195 | // values in row y. We set r,g, and b to the same value since they are handled identically. |
bsalomon | 49b264c | 2016-07-19 08:38:09 -0700 | [diff] [blame] | 196 | for (int y = 0; y < kSize; ++y) { |
| 197 | for (int x = 0; x < kSize; ++x) { |
| 198 | uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[kSize*y + x]); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 199 | color[3] = y; |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 200 | color[2] = SkTMin(x, y); |
| 201 | color[1] = SkTMin(x, y); |
| 202 | color[0] = SkTMin(x, y); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
Robert Phillips | c949ce9 | 2017-01-19 16:59:04 -0500 | [diff] [blame] | 206 | const SkImageInfo ii = SkImageInfo::Make(kSize, kSize, |
| 207 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 208 | |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame] | 209 | sk_sp<GrRenderTargetContext> readRTC(context->makeRenderTargetContext(SkBackingFit::kExact, |
| 210 | kSize, kSize, |
| 211 | kConfig, nullptr)); |
| 212 | sk_sp<GrRenderTargetContext> tempRTC(context->makeRenderTargetContext(SkBackingFit::kExact, |
| 213 | kSize, kSize, |
| 214 | kConfig, nullptr)); |
| 215 | if (!readRTC || !tempRTC) { |
bsalomon | 49b264c | 2016-07-19 08:38:09 -0700 | [diff] [blame] | 216 | return; |
| 217 | } |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 218 | GrSurfaceDesc desc; |
bsalomon | 49b264c | 2016-07-19 08:38:09 -0700 | [diff] [blame] | 219 | desc.fWidth = kSize; |
| 220 | desc.fHeight = kSize; |
| 221 | desc.fConfig = kConfig; |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame^] | 222 | |
| 223 | sk_sp<GrSurfaceProxy> dataProxy = GrSurfaceProxy::MakeDeferred(*context->caps(), |
| 224 | context->textureProvider(), |
| 225 | desc, SkBudgeted::kYes, data, 0); |
| 226 | if (!dataProxy || !dataProxy->asTextureProxy()) { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 227 | return; |
| 228 | } |
| 229 | |
| 230 | static const PMConversion kConversionRules[][2] = { |
| 231 | {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion}, |
| 232 | {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion}, |
| 233 | }; |
| 234 | |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 235 | bool failed = true; |
| 236 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 237 | 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] | 238 | *pmToUPMRule = kConversionRules[i][0]; |
| 239 | *upmToPMRule = kConversionRules[i][1]; |
| 240 | |
bsalomon | 49b264c | 2016-07-19 08:38:09 -0700 | [diff] [blame] | 241 | static const SkRect kDstRect = SkRect::MakeIWH(kSize, kSize); |
Robert Phillips | 67c18d6 | 2017-01-20 12:44:06 -0500 | [diff] [blame] | 242 | static const SkRect kSrcRect = SkRect::MakeIWH(kSize, kSize); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 243 | // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw |
| 244 | // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data. |
| 245 | // We then verify that two reads produced the same values. |
| 246 | |
Robert Phillips | 833dcf4 | 2016-11-18 08:44:13 -0500 | [diff] [blame] | 247 | if (!readRTC->asTexture()) { |
| 248 | continue; |
| 249 | } |
joshualitt | 5f10b5c | 2015-07-09 10:24:35 -0700 | [diff] [blame] | 250 | GrPaint paint1; |
| 251 | GrPaint paint2; |
| 252 | GrPaint paint3; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 253 | sk_sp<GrFragmentProcessor> pmToUPM1(new GrConfigConversionEffect( |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame^] | 254 | context, sk_ref_sp(dataProxy->asTextureProxy()), GrSwizzle::RGBA(), |
| 255 | *pmToUPMRule, SkMatrix::I())); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 256 | sk_sp<GrFragmentProcessor> upmToPM(new GrConfigConversionEffect( |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame^] | 257 | context, sk_ref_sp(readRTC->asDeferredTexture()), GrSwizzle::RGBA(), |
| 258 | *upmToPMRule, SkMatrix::I())); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 259 | sk_sp<GrFragmentProcessor> pmToUPM2(new GrConfigConversionEffect( |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame^] | 260 | context, sk_ref_sp(tempRTC->asDeferredTexture()), GrSwizzle::RGBA(), |
| 261 | *pmToUPMRule, SkMatrix::I())); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 262 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 263 | paint1.addColorFragmentProcessor(std::move(pmToUPM1)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 264 | paint1.setPorterDuffXPFactory(SkBlendMode::kSrc); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 265 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 266 | readRTC->fillRectToRect(GrNoClip(), std::move(paint1), GrAA::kNo, SkMatrix::I(), kDstRect, |
| 267 | kSrcRect); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 268 | |
Robert Phillips | c949ce9 | 2017-01-19 16:59:04 -0500 | [diff] [blame] | 269 | if (!readRTC->readPixels(ii, firstRead, 0, 0, 0)) { |
| 270 | continue; |
| 271 | } |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 272 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 273 | paint2.addColorFragmentProcessor(std::move(upmToPM)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 274 | paint2.setPorterDuffXPFactory(SkBlendMode::kSrc); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 275 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 276 | tempRTC->fillRectToRect(GrNoClip(), std::move(paint2), GrAA::kNo, SkMatrix::I(), kDstRect, |
| 277 | kSrcRect); |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 278 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 279 | paint3.addColorFragmentProcessor(std::move(pmToUPM2)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 280 | paint3.setPorterDuffXPFactory(SkBlendMode::kSrc); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 281 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 282 | readRTC->fillRectToRect(GrNoClip(), std::move(paint3), GrAA::kNo, SkMatrix::I(), kDstRect, |
| 283 | kSrcRect); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 284 | |
Robert Phillips | c949ce9 | 2017-01-19 16:59:04 -0500 | [diff] [blame] | 285 | if (!readRTC->readPixels(ii, secondRead, 0, 0, 0)) { |
| 286 | continue; |
| 287 | } |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 288 | |
| 289 | failed = false; |
bsalomon | 49b264c | 2016-07-19 08:38:09 -0700 | [diff] [blame] | 290 | for (int y = 0; y < kSize && !failed; ++y) { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 291 | for (int x = 0; x <= y; ++x) { |
bsalomon | 49b264c | 2016-07-19 08:38:09 -0700 | [diff] [blame] | 292 | if (firstRead[kSize * y + x] != secondRead[kSize * y + x]) { |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 293 | failed = true; |
| 294 | break; |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | if (failed) { |
| 300 | *pmToUPMRule = kNone_PMConversion; |
| 301 | *upmToPMRule = kNone_PMConversion; |
| 302 | } |
| 303 | } |
| 304 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 305 | sk_sp<GrFragmentProcessor> GrConfigConversionEffect::Make(GrTexture* texture, |
| 306 | const GrSwizzle& swizzle, |
| 307 | PMConversion pmConversion, |
| 308 | const SkMatrix& matrix) { |
bsalomon | 6c9cd55 | 2016-01-22 07:17:34 -0800 | [diff] [blame] | 309 | if (swizzle == GrSwizzle::RGBA() && kNone_PMConversion == pmConversion) { |
bsalomon@google.com | adc6536 | 2013-01-28 14:26:09 +0000 | [diff] [blame] | 310 | // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 311 | // 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] | 312 | // conversions were requested we instead return a GrSimpleTextureEffect. |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 313 | return GrSimpleTextureEffect::Make(texture, nullptr, matrix); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 314 | } else { |
| 315 | if (kRGBA_8888_GrPixelConfig != texture->config() && |
| 316 | kBGRA_8888_GrPixelConfig != texture->config() && |
| 317 | kNone_PMConversion != pmConversion) { |
| 318 | // The PM conversions assume colors are 0..255 |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 319 | return nullptr; |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 320 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 321 | return sk_sp<GrFragmentProcessor>( |
| 322 | new GrConfigConversionEffect(texture, swizzle, pmConversion, matrix)); |
bsalomon@google.com | a04e8e8 | 2012-08-27 12:53:13 +0000 | [diff] [blame] | 323 | } |
| 324 | } |
Robert Phillips | 757914d | 2017-01-25 15:48:30 -0500 | [diff] [blame^] | 325 | |
| 326 | sk_sp<GrFragmentProcessor> GrConfigConversionEffect::Make(GrContext* context, |
| 327 | sk_sp<GrTextureProxy> proxy, |
| 328 | const GrSwizzle& swizzle, |
| 329 | PMConversion pmConversion, |
| 330 | const SkMatrix& matrix) { |
| 331 | if (swizzle == GrSwizzle::RGBA() && kNone_PMConversion == pmConversion) { |
| 332 | // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect |
| 333 | // then we may pollute our texture cache with redundant shaders. So in the case that no |
| 334 | // conversions were requested we instead return a GrSimpleTextureEffect. |
| 335 | return GrSimpleTextureEffect::Make(context, std::move(proxy), nullptr, matrix); |
| 336 | } else { |
| 337 | if (kRGBA_8888_GrPixelConfig != proxy->config() && |
| 338 | kBGRA_8888_GrPixelConfig != proxy->config() && |
| 339 | kNone_PMConversion != pmConversion) { |
| 340 | // The PM conversions assume colors are 0..255 |
| 341 | return nullptr; |
| 342 | } |
| 343 | return sk_sp<GrFragmentProcessor>( |
| 344 | new GrConfigConversionEffect(context, std::move(proxy), swizzle, pmConversion, matrix)); |
| 345 | } |
| 346 | } |