blob: 2725e5631d603560c15dd3a20b98cf322540ce00 [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"
robertphillipsea461502015-05-26 11:38:03 -070010#include "GrDrawContext.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"
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000016
egdaniel64c47282015-11-13 06:54:19 -080017class GrGLConfigConversionEffect : public GrGLSLFragmentProcessor {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000018public:
robertphillips9cdb9922016-02-03 12:25:40 -080019 void emitCode(EmitArgs& args) override {
bsalomon6c9cd552016-01-22 07:17:34 -080020 const GrConfigConversionEffect& cce = args.fFp.cast<GrConfigConversionEffect>();
21 const GrSwizzle& swizzle = cce.swizzle();
22 GrConfigConversionEffect::PMConversion pmConversion = cce.pmConversion();
23
changjun.yangcecc91c2014-08-19 18:24:30 -070024 // Using highp for GLES here in order to avoid some precision issues on specific GPUs.
egdaniel0d3f0612015-10-21 10:45:48 -070025 GrGLSLShaderVar tmpVar("tmpColor", kVec4f_GrSLType, 0, kHigh_GrSLPrecision);
changjun.yangcecc91c2014-08-19 18:24:30 -070026 SkString tmpDecl;
egdaniela2e3e0f2015-11-19 07:23:45 -080027 tmpVar.appendDecl(args.fGLSLCaps, &tmpDecl);
changjun.yangcecc91c2014-08-19 18:24:30 -070028
cdalton85285412016-02-18 12:37:07 -080029 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualitt30ba4362014-08-21 20:18:45 -070030
egdaniel4ca2e602015-11-18 08:01:26 -080031 fragBuilder->codeAppendf("%s;", tmpDecl.c_str());
joshualitt30ba4362014-08-21 20:18:45 -070032
egdaniel4ca2e602015-11-18 08:01:26 -080033 fragBuilder->codeAppendf("%s = ", tmpVar.c_str());
34 fragBuilder->appendTextureLookup(args.fSamplers[0], args.fCoords[0].c_str(),
wangyix7c157a92015-07-22 15:08:53 -070035 args.fCoords[0].getType());
egdaniel4ca2e602015-11-18 08:01:26 -080036 fragBuilder->codeAppend(";");
changjun.yangcecc91c2014-08-19 18:24:30 -070037
bsalomon6c9cd552016-01-22 07:17:34 -080038 if (GrConfigConversionEffect::kNone_PMConversion == pmConversion) {
39 SkASSERT(GrSwizzle::RGBA() != swizzle);
40 fragBuilder->codeAppendf("%s = %s.%s;", args.fOutputColor, tmpVar.c_str(),
41 swizzle.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000042 } else {
bsalomon6c9cd552016-01-22 07:17:34 -080043 switch (pmConversion) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000044 case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion:
egdaniel4ca2e602015-11-18 08:01:26 -080045 fragBuilder->codeAppendf(
bsalomon6c9cd552016-01-22 07:17:34 -080046 "%s = vec4(ceil(%s.rgb * %s.a * 255.0) / 255.0, %s.a);",
47 tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000048 break;
49 case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion:
commit-bot@chromium.orgb4e200e2013-06-19 11:41:02 +000050 // Add a compensation(0.001) here to avoid the side effect of the floor operation.
51 // In Intel GPUs, the integer value converted from floor(%s.r * 255.0) / 255.0
52 // is less than the integer value converted from %s.r by 1 when the %s.r is
53 // converted from the integer value 2^n, such as 1, 2, 4, 8, etc.
egdaniel4ca2e602015-11-18 08:01:26 -080054 fragBuilder->codeAppendf(
bsalomon6c9cd552016-01-22 07:17:34 -080055 "%s = vec4(floor(%s.rgb * %s.a * 255.0 + 0.001) / 255.0, %s.a);",
56 tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str());
57
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000058 break;
59 case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion:
egdaniel4ca2e602015-11-18 08:01:26 -080060 fragBuilder->codeAppendf(
bsalomon6c9cd552016-01-22 07:17:34 -080061 "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(ceil(%s.rgb / %s.a * 255.0) / 255.0, %s.a);",
62 tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(),
63 tmpVar.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000064 break;
65 case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion:
egdaniel4ca2e602015-11-18 08:01:26 -080066 fragBuilder->codeAppendf(
bsalomon6c9cd552016-01-22 07:17:34 -080067 "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(floor(%s.rgb / %s.a * 255.0) / 255.0, %s.a);",
68 tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(),
69 tmpVar.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000070 break;
robertphillips@google.com2af1b182012-08-28 11:23:09 +000071 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +000072 SkFAIL("Unknown conversion op.");
robertphillips@google.com2af1b182012-08-28 11:23:09 +000073 break;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000074 }
bsalomon6c9cd552016-01-22 07:17:34 -080075 fragBuilder->codeAppendf("%s = %s.%s;", args.fOutputColor, tmpVar.c_str(),
76 swizzle.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000077 }
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000078 SkString modulate;
wangyix7c157a92015-07-22 15:08:53 -070079 GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor);
egdaniel4ca2e602015-11-18 08:01:26 -080080 fragBuilder->codeAppend(modulate.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000081 }
82
jvanverthcfc18862015-04-28 08:48:20 -070083 static inline void GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -070084 GrProcessorKeyBuilder* b) {
bsalomon6c9cd552016-01-22 07:17:34 -080085 const GrConfigConversionEffect& cce = processor.cast<GrConfigConversionEffect>();
86 uint32_t key = (cce.swizzle().asKey()) | (cce.pmConversion() << 16);
bsalomon63e99f72014-07-21 08:03:14 -070087 b->add32(key);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000088 }
89
90private:
egdaniel64c47282015-11-13 06:54:19 -080091 typedef GrGLSLFragmentProcessor INHERITED;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000092
93};
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)
bsalomon4a339522015-10-06 08:40:50 -0700101 : INHERITED(texture, 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
bsalomon0e08fc12014-10-15 08:19:04 -0700114bool GrConfigConversionEffect::onIsEqual(const GrFragmentProcessor& s) const {
joshualitt49586be2014-09-16 08:21:41 -0700115 const GrConfigConversionEffect& other = s.cast<GrConfigConversionEffect>();
bsalomon6c9cd552016-01-22 07:17:34 -0800116 return other.fSwizzle == fSwizzle &&
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000117 other.fPMConversion == fPMConversion;
118}
119
egdaniel605dd0f2014-11-12 08:35:25 -0800120void GrConfigConversionEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
egdaniel1a8ecdf2014-10-03 06:24:12 -0700121 this->updateInvariantOutputForModulation(inout);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000122}
123
124///////////////////////////////////////////////////////////////////////////////
125
joshualittb0a8a372014-09-23 09:50:21 -0700126GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConfigConversionEffect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000127
bsalomonc21b09e2015-08-28 18:46:56 -0700128const GrFragmentProcessor* GrConfigConversionEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700129 PMConversion pmConv = static_cast<PMConversion>(d->fRandom->nextULessThan(kPMConversionCnt));
bsalomon6c9cd552016-01-22 07:17:34 -0800130 GrSwizzle swizzle;
131 do {
132 swizzle = GrSwizzle::CreateRandom(d->fRandom);
133 } while (pmConv == kNone_PMConversion && swizzle == GrSwizzle::RGBA());
bsalomon4a339522015-10-06 08:40:50 -0700134 return new GrConfigConversionEffect(d->fTextures[GrProcessorUnitTest::kSkiaPMTextureIdx],
bsalomon6c9cd552016-01-22 07:17:34 -0800135 swizzle, pmConv, GrTest::TestMatrix(d->fRandom));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000136}
137
138///////////////////////////////////////////////////////////////////////////////
joshualitteb2a6762014-12-04 11:35:33 -0800139
egdaniel57d3b032015-11-13 11:57:27 -0800140void GrConfigConversionEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps,
141 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800142 GrGLConfigConversionEffect::GenKey(*this, caps, b);
143}
144
egdaniel57d3b032015-11-13 11:57:27 -0800145GrGLSLFragmentProcessor* GrConfigConversionEffect::onCreateGLSLInstance() const {
bsalomon6c9cd552016-01-22 07:17:34 -0800146 return new GrGLConfigConversionEffect();
joshualitteb2a6762014-12-04 11:35:33 -0800147}
148
robertphillipse85a32d2015-02-10 08:16:55 -0800149
150
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000151void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context,
152 PMConversion* pmToUPMRule,
153 PMConversion* upmToPMRule) {
154 *pmToUPMRule = kNone_PMConversion;
155 *upmToPMRule = kNone_PMConversion;
156 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
157 uint32_t* srcData = data.get();
158 uint32_t* firstRead = data.get() + 256 * 256;
159 uint32_t* secondRead = data.get() + 2 * 256 * 256;
160
161 // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate
162 // values in row y. We set r,g, and b to the same value since they are handled identically.
163 for (int y = 0; y < 256; ++y) {
164 for (int x = 0; x < 256; ++x) {
165 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
166 color[3] = y;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000167 color[2] = SkTMin(x, y);
168 color[1] = SkTMin(x, y);
169 color[0] = SkTMin(x, y);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000170 }
171 }
172
bsalomonf2703d82014-10-28 14:33:06 -0700173 GrSurfaceDesc desc;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800174 desc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000175 desc.fWidth = 256;
176 desc.fHeight = 256;
177 desc.fConfig = kRGBA_8888_GrPixelConfig;
178
bsalomon5ec26ae2016-02-25 08:33:02 -0800179 SkAutoTUnref<GrTexture> readTex(context->textureProvider()->createTexture(
180 desc, SkBudgeted::kYes, nullptr, 0));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000181 if (!readTex.get()) {
182 return;
183 }
bsalomon5ec26ae2016-02-25 08:33:02 -0800184 SkAutoTUnref<GrTexture> tempTex(context->textureProvider()->createTexture(
185 desc, SkBudgeted::kYes, nullptr, 0));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000186 if (!tempTex.get()) {
187 return;
188 }
bsalomonf2703d82014-10-28 14:33:06 -0700189 desc.fFlags = kNone_GrSurfaceFlags;
bsalomon5ec26ae2016-02-25 08:33:02 -0800190 SkAutoTUnref<GrTexture> dataTex(context->textureProvider()->createTexture(
191 desc, SkBudgeted::kYes, data, 0));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000192 if (!dataTex.get()) {
193 return;
194 }
195
196 static const PMConversion kConversionRules[][2] = {
197 {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion},
198 {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion},
199 };
200
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000201 bool failed = true;
202
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000203 for (size_t i = 0; i < SK_ARRAY_COUNT(kConversionRules) && failed; ++i) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000204 *pmToUPMRule = kConversionRules[i][0];
205 *upmToPMRule = kConversionRules[i][1];
206
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000207 static const SkRect kDstRect = SkRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256));
208 static const SkRect kSrcRect = SkRect::MakeWH(SK_Scalar1, SK_Scalar1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000209 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
210 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
211 // We then verify that two reads produced the same values.
212
joshualitt5f10b5c2015-07-09 10:24:35 -0700213 GrPaint paint1;
214 GrPaint paint2;
215 GrPaint paint3;
halcanary385fe4d2015-08-26 13:07:48 -0700216 SkAutoTUnref<GrFragmentProcessor> pmToUPM1(new GrConfigConversionEffect(
bsalomon6c9cd552016-01-22 07:17:34 -0800217 dataTex, GrSwizzle::RGBA(), *pmToUPMRule, SkMatrix::I()));
halcanary385fe4d2015-08-26 13:07:48 -0700218 SkAutoTUnref<GrFragmentProcessor> upmToPM(new GrConfigConversionEffect(
bsalomon6c9cd552016-01-22 07:17:34 -0800219 readTex, GrSwizzle::RGBA(), *upmToPMRule, SkMatrix::I()));
halcanary385fe4d2015-08-26 13:07:48 -0700220 SkAutoTUnref<GrFragmentProcessor> pmToUPM2(new GrConfigConversionEffect(
bsalomon6c9cd552016-01-22 07:17:34 -0800221 tempTex, GrSwizzle::RGBA(), *pmToUPMRule, SkMatrix::I()));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000222
bsalomonac856c92015-08-27 06:30:17 -0700223 paint1.addColorFragmentProcessor(pmToUPM1);
egdanielc4b72722015-11-23 13:20:41 -0800224 paint1.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700225
226
robertphillips77a2e522015-10-17 07:43:27 -0700227 SkAutoTUnref<GrDrawContext> readDrawContext(
228 context->drawContext(readTex->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700229 if (!readDrawContext) {
230 failed = true;
231 break;
232 }
233
bsalomona2e69fc2015-11-05 10:41:43 -0800234 readDrawContext->fillRectToRect(GrClip::WideOpen(),
235 paint1,
236 SkMatrix::I(),
237 kDstRect,
238 kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000239
240 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
241
bsalomonac856c92015-08-27 06:30:17 -0700242 paint2.addColorFragmentProcessor(upmToPM);
egdanielc4b72722015-11-23 13:20:41 -0800243 paint2.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700244
robertphillips77a2e522015-10-17 07:43:27 -0700245 SkAutoTUnref<GrDrawContext> tempDrawContext(
246 context->drawContext(tempTex->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700247 if (!tempDrawContext) {
248 failed = true;
249 break;
250 }
bsalomona2e69fc2015-11-05 10:41:43 -0800251 tempDrawContext->fillRectToRect(GrClip::WideOpen(),
252 paint2,
253 SkMatrix::I(),
254 kDstRect,
255 kSrcRect);
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000256
bsalomonac856c92015-08-27 06:30:17 -0700257 paint3.addColorFragmentProcessor(pmToUPM2);
egdanielc4b72722015-11-23 13:20:41 -0800258 paint3.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700259
robertphillips2e1e51f2015-10-15 08:01:48 -0700260 readDrawContext.reset(context->drawContext(readTex->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700261 if (!readDrawContext) {
262 failed = true;
263 break;
264 }
265
bsalomona2e69fc2015-11-05 10:41:43 -0800266 readDrawContext->fillRectToRect(GrClip::WideOpen(),
267 paint3,
268 SkMatrix::I(),
269 kDstRect,
270 kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000271
272 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
273
274 failed = false;
275 for (int y = 0; y < 256 && !failed; ++y) {
276 for (int x = 0; x <= y; ++x) {
277 if (firstRead[256 * y + x] != secondRead[256 * y + x]) {
278 failed = true;
279 break;
280 }
281 }
282 }
283 }
284 if (failed) {
285 *pmToUPMRule = kNone_PMConversion;
286 *upmToPMRule = kNone_PMConversion;
287 }
288}
289
bsalomon4a339522015-10-06 08:40:50 -0700290const GrFragmentProcessor* GrConfigConversionEffect::Create(GrTexture* texture,
bsalomon6c9cd552016-01-22 07:17:34 -0800291 const GrSwizzle& swizzle,
joshualitt5f10b5c2015-07-09 10:24:35 -0700292 PMConversion pmConversion,
293 const SkMatrix& matrix) {
bsalomon6c9cd552016-01-22 07:17:34 -0800294 if (swizzle == GrSwizzle::RGBA() && kNone_PMConversion == pmConversion) {
bsalomon@google.comadc65362013-01-28 14:26:09 +0000295 // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000296 // then we may pollute our texture cache with redundant shaders. So in the case that no
bsalomon@google.comadc65362013-01-28 14:26:09 +0000297 // conversions were requested we instead return a GrSimpleTextureEffect.
bsalomon4a339522015-10-06 08:40:50 -0700298 return GrSimpleTextureEffect::Create(texture, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000299 } else {
300 if (kRGBA_8888_GrPixelConfig != texture->config() &&
301 kBGRA_8888_GrPixelConfig != texture->config() &&
302 kNone_PMConversion != pmConversion) {
303 // The PM conversions assume colors are 0..255
halcanary96fcdcc2015-08-27 07:41:13 -0700304 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000305 }
bsalomon6c9cd552016-01-22 07:17:34 -0800306 return new GrConfigConversionEffect(texture, swizzle, pmConversion, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000307 }
308}