blob: 2380ad343599a1a55b55629a28ac0b82197193b4 [file] [log] [blame]
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001/*
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.comb1456d72012-11-02 18:23:45 +00009#include "GrContext.h"
Brian Osman11052242016-10-27 14:47:55 -040010#include "GrRenderTargetContext.h"
egdaniel605dd0f2014-11-12 08:35:25 -080011#include "GrInvariantOutput.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000012#include "GrSimpleTextureEffect.h"
joshualitteb2a6762014-12-04 11:35:33 -080013#include "SkMatrix.h"
egdaniel64c47282015-11-13 06:54:19 -080014#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080015#include "glsl/GrGLSLFragmentShaderBuilder.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050016#include "../private/GrGLSL.h"
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000017
egdaniel64c47282015-11-13 06:54:19 -080018class GrGLConfigConversionEffect : public GrGLSLFragmentProcessor {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000019public:
robertphillips9cdb9922016-02-03 12:25:40 -080020 void emitCode(EmitArgs& args) override {
bsalomon6c9cd552016-01-22 07:17:34 -080021 const GrConfigConversionEffect& cce = args.fFp.cast<GrConfigConversionEffect>();
22 const GrSwizzle& swizzle = cce.swizzle();
23 GrConfigConversionEffect::PMConversion pmConversion = cce.pmConversion();
24
changjun.yangcecc91c2014-08-19 18:24:30 -070025 // Using highp for GLES here in order to avoid some precision issues on specific GPUs.
Brian Salomon99938a82016-11-21 13:41:08 -050026 GrShaderVar tmpVar("tmpColor", kVec4f_GrSLType, 0, kHigh_GrSLPrecision);
changjun.yangcecc91c2014-08-19 18:24:30 -070027 SkString tmpDecl;
Brian Salomon1edc5b92016-11-29 13:43:46 -050028 tmpVar.appendDecl(args.fShaderCaps, &tmpDecl);
changjun.yangcecc91c2014-08-19 18:24:30 -070029
cdalton85285412016-02-18 12:37:07 -080030 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualitt30ba4362014-08-21 20:18:45 -070031
egdaniel4ca2e602015-11-18 08:01:26 -080032 fragBuilder->codeAppendf("%s;", tmpDecl.c_str());
joshualitt30ba4362014-08-21 20:18:45 -070033
egdaniel4ca2e602015-11-18 08:01:26 -080034 fragBuilder->codeAppendf("%s = ", tmpVar.c_str());
bsalomon1a1aa932016-09-12 09:30:36 -070035 fragBuilder->appendTextureLookup(args.fTexSamplers[0], args.fTransformedCoords[0].c_str(),
36 args.fTransformedCoords[0].getType());
egdaniel4ca2e602015-11-18 08:01:26 -080037 fragBuilder->codeAppend(";");
changjun.yangcecc91c2014-08-19 18:24:30 -070038
bsalomon6c9cd552016-01-22 07:17:34 -080039 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.coma04e8e82012-08-27 12:53:13 +000043 } else {
bsalomon6c9cd552016-01-22 07:17:34 -080044 switch (pmConversion) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000045 case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion:
egdaniel4ca2e602015-11-18 08:01:26 -080046 fragBuilder->codeAppendf(
bsalomon6c9cd552016-01-22 07:17:34 -080047 "%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.coma04e8e82012-08-27 12:53:13 +000049 break;
50 case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion:
commit-bot@chromium.orgb4e200e2013-06-19 11:41:02 +000051 // 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.
egdaniel4ca2e602015-11-18 08:01:26 -080055 fragBuilder->codeAppendf(
bsalomon6c9cd552016-01-22 07:17:34 -080056 "%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.coma04e8e82012-08-27 12:53:13 +000059 break;
60 case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion:
egdaniel4ca2e602015-11-18 08:01:26 -080061 fragBuilder->codeAppendf(
bsalomon6c9cd552016-01-22 07:17:34 -080062 "%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.coma04e8e82012-08-27 12:53:13 +000065 break;
66 case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion:
egdaniel4ca2e602015-11-18 08:01:26 -080067 fragBuilder->codeAppendf(
bsalomon6c9cd552016-01-22 07:17:34 -080068 "%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.coma04e8e82012-08-27 12:53:13 +000071 break;
robertphillips@google.com2af1b182012-08-28 11:23:09 +000072 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +000073 SkFAIL("Unknown conversion op.");
robertphillips@google.com2af1b182012-08-28 11:23:09 +000074 break;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000075 }
bsalomon6c9cd552016-01-22 07:17:34 -080076 fragBuilder->codeAppendf("%s = %s.%s;", args.fOutputColor, tmpVar.c_str(),
77 swizzle.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000078 }
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000079 SkString modulate;
wangyix7c157a92015-07-22 15:08:53 -070080 GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor);
egdaniel4ca2e602015-11-18 08:01:26 -080081 fragBuilder->codeAppend(modulate.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000082 }
83
Brian Salomon94efbf52016-11-29 13:43:05 -050084 static inline void GenKey(const GrProcessor& processor, const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -070085 GrProcessorKeyBuilder* b) {
bsalomon6c9cd552016-01-22 07:17:34 -080086 const GrConfigConversionEffect& cce = processor.cast<GrConfigConversionEffect>();
87 uint32_t key = (cce.swizzle().asKey()) | (cce.pmConversion() << 16);
bsalomon63e99f72014-07-21 08:03:14 -070088 b->add32(key);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000089 }
90
91private:
egdaniel64c47282015-11-13 06:54:19 -080092 typedef GrGLSLFragmentProcessor INHERITED;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000093
94};
95
96///////////////////////////////////////////////////////////////////////////////
bsalomon4a339522015-10-06 08:40:50 -070097GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture,
bsalomon6c9cd552016-01-22 07:17:34 -080098 const GrSwizzle& swizzle,
bsalomon@google.comb1456d72012-11-02 18:23:45 +000099 PMConversion pmConversion,
100 const SkMatrix& matrix)
brianosman54f30c12016-07-18 10:53:52 -0700101 : INHERITED(texture, nullptr, matrix)
bsalomon6c9cd552016-01-22 07:17:34 -0800102 , fSwizzle(swizzle)
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000103 , fPMConversion(pmConversion) {
joshualitteb2a6762014-12-04 11:35:33 -0800104 this->initClassID<GrConfigConversionEffect>();
bsalomon445fc432015-08-03 10:15:25 -0700105 // 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.coma04e8e82012-08-27 12:53:13 +0000110 // Why did we pollute our texture cache instead of using a GrSingleTextureEffect?
bsalomon6c9cd552016-01-22 07:17:34 -0800111 SkASSERT(swizzle != GrSwizzle::RGBA() || kNone_PMConversion != pmConversion);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000112}
113
Robert Phillips757914d2017-01-25 15:48:30 -0500114GrConfigConversionEffect::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
bsalomon0e08fc12014-10-15 08:19:04 -0700132bool GrConfigConversionEffect::onIsEqual(const GrFragmentProcessor& s) const {
joshualitt49586be2014-09-16 08:21:41 -0700133 const GrConfigConversionEffect& other = s.cast<GrConfigConversionEffect>();
bsalomon6c9cd552016-01-22 07:17:34 -0800134 return other.fSwizzle == fSwizzle &&
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000135 other.fPMConversion == fPMConversion;
136}
137
egdaniel605dd0f2014-11-12 08:35:25 -0800138void GrConfigConversionEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
egdaniel1a8ecdf2014-10-03 06:24:12 -0700139 this->updateInvariantOutputForModulation(inout);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000140}
141
142///////////////////////////////////////////////////////////////////////////////
143
joshualittb0a8a372014-09-23 09:50:21 -0700144GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConfigConversionEffect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000145
brucedawson3c3e0d72016-09-06 09:54:44 -0700146#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
bungeman06ca8ec2016-06-09 08:01:03 -0700152sk_sp<GrFragmentProcessor> GrConfigConversionEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700153 PMConversion pmConv = static_cast<PMConversion>(d->fRandom->nextULessThan(kPMConversionCnt));
bsalomon6c9cd552016-01-22 07:17:34 -0800154 GrSwizzle swizzle;
155 do {
156 swizzle = GrSwizzle::CreateRandom(d->fRandom);
157 } while (pmConv == kNone_PMConversion && swizzle == GrSwizzle::RGBA());
Robert Phillips757914d2017-01-25 15:48:30 -0500158 return sk_sp<GrFragmentProcessor>(new GrConfigConversionEffect(
159 d->context(),
160 d->textureProxy(GrProcessorUnitTest::kSkiaPMTextureIdx),
161 swizzle, pmConv, GrTest::TestMatrix(d->fRandom)));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000162}
163
brucedawson3c3e0d72016-09-06 09:54:44 -0700164#if !defined(__clang__) && _MSC_FULL_VER >= 190024213
165// Restore optimization settings.
166#pragma optimize("", on)
167#endif
168
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000169///////////////////////////////////////////////////////////////////////////////
joshualitteb2a6762014-12-04 11:35:33 -0800170
Brian Salomon94efbf52016-11-29 13:43:05 -0500171void GrConfigConversionEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800172 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800173 GrGLConfigConversionEffect::GenKey(*this, caps, b);
174}
175
egdaniel57d3b032015-11-13 11:57:27 -0800176GrGLSLFragmentProcessor* GrConfigConversionEffect::onCreateGLSLInstance() const {
bsalomon6c9cd552016-01-22 07:17:34 -0800177 return new GrGLConfigConversionEffect();
joshualitteb2a6762014-12-04 11:35:33 -0800178}
179
robertphillipse85a32d2015-02-10 08:16:55 -0800180
181
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000182void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context,
183 PMConversion* pmToUPMRule,
184 PMConversion* upmToPMRule) {
185 *pmToUPMRule = kNone_PMConversion;
186 *upmToPMRule = kNone_PMConversion;
bsalomon49b264c2016-07-19 08:38:09 -0700187 static constexpr int kSize = 256;
188 static constexpr GrPixelConfig kConfig = kRGBA_8888_GrPixelConfig;
189 SkAutoTMalloc<uint32_t> data(kSize * kSize * 3);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000190 uint32_t* srcData = data.get();
bsalomon49b264c2016-07-19 08:38:09 -0700191 uint32_t* firstRead = data.get() + kSize * kSize;
192 uint32_t* secondRead = data.get() + 2 * kSize * kSize;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000193
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.
bsalomon49b264c2016-07-19 08:38:09 -0700196 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.coma04e8e82012-08-27 12:53:13 +0000199 color[3] = y;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000200 color[2] = SkTMin(x, y);
201 color[1] = SkTMin(x, y);
202 color[0] = SkTMin(x, y);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000203 }
204 }
205
Robert Phillipsc949ce92017-01-19 16:59:04 -0500206 const SkImageInfo ii = SkImageInfo::Make(kSize, kSize,
207 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
208
Brian Osman693a5402016-10-27 15:13:22 -0400209 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) {
bsalomon49b264c2016-07-19 08:38:09 -0700216 return;
217 }
bsalomonf2703d82014-10-28 14:33:06 -0700218 GrSurfaceDesc desc;
bsalomon49b264c2016-07-19 08:38:09 -0700219 desc.fWidth = kSize;
220 desc.fHeight = kSize;
221 desc.fConfig = kConfig;
Robert Phillips757914d2017-01-25 15:48:30 -0500222
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.coma04e8e82012-08-27 12:53:13 +0000227 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.coma04e8e82012-08-27 12:53:13 +0000235 bool failed = true;
236
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000237 for (size_t i = 0; i < SK_ARRAY_COUNT(kConversionRules) && failed; ++i) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000238 *pmToUPMRule = kConversionRules[i][0];
239 *upmToPMRule = kConversionRules[i][1];
240
bsalomon49b264c2016-07-19 08:38:09 -0700241 static const SkRect kDstRect = SkRect::MakeIWH(kSize, kSize);
Robert Phillips67c18d62017-01-20 12:44:06 -0500242 static const SkRect kSrcRect = SkRect::MakeIWH(kSize, kSize);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000243 // 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 Phillips833dcf42016-11-18 08:44:13 -0500247 if (!readRTC->asTexture()) {
248 continue;
249 }
joshualitt5f10b5c2015-07-09 10:24:35 -0700250 GrPaint paint1;
251 GrPaint paint2;
252 GrPaint paint3;
bungeman06ca8ec2016-06-09 08:01:03 -0700253 sk_sp<GrFragmentProcessor> pmToUPM1(new GrConfigConversionEffect(
Robert Phillips757914d2017-01-25 15:48:30 -0500254 context, sk_ref_sp(dataProxy->asTextureProxy()), GrSwizzle::RGBA(),
255 *pmToUPMRule, SkMatrix::I()));
bungeman06ca8ec2016-06-09 08:01:03 -0700256 sk_sp<GrFragmentProcessor> upmToPM(new GrConfigConversionEffect(
Robert Phillips757914d2017-01-25 15:48:30 -0500257 context, sk_ref_sp(readRTC->asDeferredTexture()), GrSwizzle::RGBA(),
258 *upmToPMRule, SkMatrix::I()));
bungeman06ca8ec2016-06-09 08:01:03 -0700259 sk_sp<GrFragmentProcessor> pmToUPM2(new GrConfigConversionEffect(
Robert Phillips757914d2017-01-25 15:48:30 -0500260 context, sk_ref_sp(tempRTC->asDeferredTexture()), GrSwizzle::RGBA(),
261 *pmToUPMRule, SkMatrix::I()));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000262
bungeman06ca8ec2016-06-09 08:01:03 -0700263 paint1.addColorFragmentProcessor(std::move(pmToUPM1));
reed374772b2016-10-05 17:33:02 -0700264 paint1.setPorterDuffXPFactory(SkBlendMode::kSrc);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700265
Brian Salomon82f44312017-01-11 13:42:54 -0500266 readRTC->fillRectToRect(GrNoClip(), std::move(paint1), GrAA::kNo, SkMatrix::I(), kDstRect,
267 kSrcRect);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700268
Robert Phillipsc949ce92017-01-19 16:59:04 -0500269 if (!readRTC->readPixels(ii, firstRead, 0, 0, 0)) {
270 continue;
271 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000272
bungeman06ca8ec2016-06-09 08:01:03 -0700273 paint2.addColorFragmentProcessor(std::move(upmToPM));
reed374772b2016-10-05 17:33:02 -0700274 paint2.setPorterDuffXPFactory(SkBlendMode::kSrc);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700275
Brian Salomon82f44312017-01-11 13:42:54 -0500276 tempRTC->fillRectToRect(GrNoClip(), std::move(paint2), GrAA::kNo, SkMatrix::I(), kDstRect,
277 kSrcRect);
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000278
bungeman06ca8ec2016-06-09 08:01:03 -0700279 paint3.addColorFragmentProcessor(std::move(pmToUPM2));
reed374772b2016-10-05 17:33:02 -0700280 paint3.setPorterDuffXPFactory(SkBlendMode::kSrc);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700281
Brian Salomon82f44312017-01-11 13:42:54 -0500282 readRTC->fillRectToRect(GrNoClip(), std::move(paint3), GrAA::kNo, SkMatrix::I(), kDstRect,
283 kSrcRect);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700284
Robert Phillipsc949ce92017-01-19 16:59:04 -0500285 if (!readRTC->readPixels(ii, secondRead, 0, 0, 0)) {
286 continue;
287 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000288
289 failed = false;
bsalomon49b264c2016-07-19 08:38:09 -0700290 for (int y = 0; y < kSize && !failed; ++y) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000291 for (int x = 0; x <= y; ++x) {
bsalomon49b264c2016-07-19 08:38:09 -0700292 if (firstRead[kSize * y + x] != secondRead[kSize * y + x]) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000293 failed = true;
294 break;
295 }
296 }
297 }
298 }
299 if (failed) {
300 *pmToUPMRule = kNone_PMConversion;
301 *upmToPMRule = kNone_PMConversion;
302 }
303}
304
bungeman06ca8ec2016-06-09 08:01:03 -0700305sk_sp<GrFragmentProcessor> GrConfigConversionEffect::Make(GrTexture* texture,
306 const GrSwizzle& swizzle,
307 PMConversion pmConversion,
308 const SkMatrix& matrix) {
bsalomon6c9cd552016-01-22 07:17:34 -0800309 if (swizzle == GrSwizzle::RGBA() && kNone_PMConversion == pmConversion) {
bsalomon@google.comadc65362013-01-28 14:26:09 +0000310 // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000311 // then we may pollute our texture cache with redundant shaders. So in the case that no
bsalomon@google.comadc65362013-01-28 14:26:09 +0000312 // conversions were requested we instead return a GrSimpleTextureEffect.
brianosman54f30c12016-07-18 10:53:52 -0700313 return GrSimpleTextureEffect::Make(texture, nullptr, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000314 } 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
halcanary96fcdcc2015-08-27 07:41:13 -0700319 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000320 }
bungeman06ca8ec2016-06-09 08:01:03 -0700321 return sk_sp<GrFragmentProcessor>(
322 new GrConfigConversionEffect(texture, swizzle, pmConversion, matrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000323 }
324}
Robert Phillips757914d2017-01-25 15:48:30 -0500325
326sk_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}