blob: ace736c49425ed996e4cefd3ce7ff574c116a605 [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
egdaniel4ca2e602015-11-18 08:01:26 -080029 GrGLSLFragmentBuilder* 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
halcanary96fcdcc2015-08-27 07:41:13 -0700179 SkAutoTUnref<GrTexture> readTex(context->textureProvider()->createTexture(desc, true, nullptr, 0));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000180 if (!readTex.get()) {
181 return;
182 }
halcanary96fcdcc2015-08-27 07:41:13 -0700183 SkAutoTUnref<GrTexture> tempTex(context->textureProvider()->createTexture(desc, true, nullptr, 0));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000184 if (!tempTex.get()) {
185 return;
186 }
bsalomonf2703d82014-10-28 14:33:06 -0700187 desc.fFlags = kNone_GrSurfaceFlags;
bsalomond309e7a2015-04-30 14:18:54 -0700188 SkAutoTUnref<GrTexture> dataTex(context->textureProvider()->createTexture(desc, true, data, 0));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000189 if (!dataTex.get()) {
190 return;
191 }
192
193 static const PMConversion kConversionRules[][2] = {
194 {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion},
195 {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion},
196 };
197
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000198 bool failed = true;
199
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000200 for (size_t i = 0; i < SK_ARRAY_COUNT(kConversionRules) && failed; ++i) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000201 *pmToUPMRule = kConversionRules[i][0];
202 *upmToPMRule = kConversionRules[i][1];
203
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000204 static const SkRect kDstRect = SkRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256));
205 static const SkRect kSrcRect = SkRect::MakeWH(SK_Scalar1, SK_Scalar1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000206 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
207 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
208 // We then verify that two reads produced the same values.
209
joshualitt5f10b5c2015-07-09 10:24:35 -0700210 GrPaint paint1;
211 GrPaint paint2;
212 GrPaint paint3;
halcanary385fe4d2015-08-26 13:07:48 -0700213 SkAutoTUnref<GrFragmentProcessor> pmToUPM1(new GrConfigConversionEffect(
bsalomon6c9cd552016-01-22 07:17:34 -0800214 dataTex, GrSwizzle::RGBA(), *pmToUPMRule, SkMatrix::I()));
halcanary385fe4d2015-08-26 13:07:48 -0700215 SkAutoTUnref<GrFragmentProcessor> upmToPM(new GrConfigConversionEffect(
bsalomon6c9cd552016-01-22 07:17:34 -0800216 readTex, GrSwizzle::RGBA(), *upmToPMRule, SkMatrix::I()));
halcanary385fe4d2015-08-26 13:07:48 -0700217 SkAutoTUnref<GrFragmentProcessor> pmToUPM2(new GrConfigConversionEffect(
bsalomon6c9cd552016-01-22 07:17:34 -0800218 tempTex, GrSwizzle::RGBA(), *pmToUPMRule, SkMatrix::I()));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000219
bsalomonac856c92015-08-27 06:30:17 -0700220 paint1.addColorFragmentProcessor(pmToUPM1);
egdanielc4b72722015-11-23 13:20:41 -0800221 paint1.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700222
223
robertphillips77a2e522015-10-17 07:43:27 -0700224 SkAutoTUnref<GrDrawContext> readDrawContext(
225 context->drawContext(readTex->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700226 if (!readDrawContext) {
227 failed = true;
228 break;
229 }
230
bsalomona2e69fc2015-11-05 10:41:43 -0800231 readDrawContext->fillRectToRect(GrClip::WideOpen(),
232 paint1,
233 SkMatrix::I(),
234 kDstRect,
235 kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000236
237 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
238
bsalomonac856c92015-08-27 06:30:17 -0700239 paint2.addColorFragmentProcessor(upmToPM);
egdanielc4b72722015-11-23 13:20:41 -0800240 paint2.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700241
robertphillips77a2e522015-10-17 07:43:27 -0700242 SkAutoTUnref<GrDrawContext> tempDrawContext(
243 context->drawContext(tempTex->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700244 if (!tempDrawContext) {
245 failed = true;
246 break;
247 }
bsalomona2e69fc2015-11-05 10:41:43 -0800248 tempDrawContext->fillRectToRect(GrClip::WideOpen(),
249 paint2,
250 SkMatrix::I(),
251 kDstRect,
252 kSrcRect);
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000253
bsalomonac856c92015-08-27 06:30:17 -0700254 paint3.addColorFragmentProcessor(pmToUPM2);
egdanielc4b72722015-11-23 13:20:41 -0800255 paint3.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700256
robertphillips2e1e51f2015-10-15 08:01:48 -0700257 readDrawContext.reset(context->drawContext(readTex->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700258 if (!readDrawContext) {
259 failed = true;
260 break;
261 }
262
bsalomona2e69fc2015-11-05 10:41:43 -0800263 readDrawContext->fillRectToRect(GrClip::WideOpen(),
264 paint3,
265 SkMatrix::I(),
266 kDstRect,
267 kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000268
269 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
270
271 failed = false;
272 for (int y = 0; y < 256 && !failed; ++y) {
273 for (int x = 0; x <= y; ++x) {
274 if (firstRead[256 * y + x] != secondRead[256 * y + x]) {
275 failed = true;
276 break;
277 }
278 }
279 }
280 }
281 if (failed) {
282 *pmToUPMRule = kNone_PMConversion;
283 *upmToPMRule = kNone_PMConversion;
284 }
285}
286
bsalomon4a339522015-10-06 08:40:50 -0700287const GrFragmentProcessor* GrConfigConversionEffect::Create(GrTexture* texture,
bsalomon6c9cd552016-01-22 07:17:34 -0800288 const GrSwizzle& swizzle,
joshualitt5f10b5c2015-07-09 10:24:35 -0700289 PMConversion pmConversion,
290 const SkMatrix& matrix) {
bsalomon6c9cd552016-01-22 07:17:34 -0800291 if (swizzle == GrSwizzle::RGBA() && kNone_PMConversion == pmConversion) {
bsalomon@google.comadc65362013-01-28 14:26:09 +0000292 // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000293 // then we may pollute our texture cache with redundant shaders. So in the case that no
bsalomon@google.comadc65362013-01-28 14:26:09 +0000294 // conversions were requested we instead return a GrSimpleTextureEffect.
bsalomon4a339522015-10-06 08:40:50 -0700295 return GrSimpleTextureEffect::Create(texture, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000296 } else {
297 if (kRGBA_8888_GrPixelConfig != texture->config() &&
298 kBGRA_8888_GrPixelConfig != texture->config() &&
299 kNone_PMConversion != pmConversion) {
300 // The PM conversions assume colors are 0..255
halcanary96fcdcc2015-08-27 07:41:13 -0700301 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000302 }
bsalomon6c9cd552016-01-22 07:17:34 -0800303 return new GrConfigConversionEffect(texture, swizzle, pmConversion, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000304 }
305}