blob: 7aad50f5f881c2864fe3c26f2f4550a386f95fed [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"
egdaniel605dd0f2014-11-12 08:35:25 -080010#include "GrInvariantOutput.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000011#include "GrSimpleTextureEffect.h"
joshualitteb2a6762014-12-04 11:35:33 -080012#include "SkMatrix.h"
joshualittb0a8a372014-09-23 09:50:21 -070013#include "gl/GrGLProcessor.h"
joshualitt30ba4362014-08-21 20:18:45 -070014#include "gl/builders/GrGLProgramBuilder.h"
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000015
joshualittb0a8a372014-09-23 09:50:21 -070016class GrGLConfigConversionEffect : public GrGLFragmentProcessor {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000017public:
joshualitteb2a6762014-12-04 11:35:33 -080018 GrGLConfigConversionEffect(const GrProcessor& processor) {
joshualittb0a8a372014-09-23 09:50:21 -070019 const GrConfigConversionEffect& configConversionEffect =
20 processor.cast<GrConfigConversionEffect>();
joshualitt49586be2014-09-16 08:21:41 -070021 fSwapRedAndBlue = configConversionEffect.swapsRedAndBlue();
22 fPMConversion = configConversionEffect.pmConversion();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000023 }
24
joshualitt15988992014-10-09 15:04:05 -070025 virtual void emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -070026 const GrFragmentProcessor&,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000027 const char* outputColor,
28 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000029 const TransformedCoordsArray& coords,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000030 const TextureSamplerArray& samplers) SK_OVERRIDE {
changjun.yangcecc91c2014-08-19 18:24:30 -070031 // Using highp for GLES here in order to avoid some precision issues on specific GPUs.
bsalomonc0bd6482014-12-09 10:04:14 -080032 GrGLShaderVar tmpVar("tmpColor", kVec4f_GrSLType, 0, kHigh_GrSLPrecision);
changjun.yangcecc91c2014-08-19 18:24:30 -070033 SkString tmpDecl;
34 tmpVar.appendDecl(builder->ctxInfo(), &tmpDecl);
changjun.yangcecc91c2014-08-19 18:24:30 -070035
joshualitt15988992014-10-09 15:04:05 -070036 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -070037
38 fsBuilder->codeAppendf("%s;", tmpDecl.c_str());
39
40 fsBuilder->codeAppendf("%s = ", tmpVar.c_str());
joshualitt23e280d2014-09-18 12:26:38 -070041 fsBuilder->appendTextureLookup(samplers[0], coords[0].c_str(), coords[0].getType());
joshualitt30ba4362014-08-21 20:18:45 -070042 fsBuilder->codeAppend(";");
changjun.yangcecc91c2014-08-19 18:24:30 -070043
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000044 if (GrConfigConversionEffect::kNone_PMConversion == fPMConversion) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000045 SkASSERT(fSwapRedAndBlue);
joshualitt30ba4362014-08-21 20:18:45 -070046 fsBuilder->codeAppendf("%s = %s.bgra;", outputColor, tmpVar.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000047 } else {
48 const char* swiz = fSwapRedAndBlue ? "bgr" : "rgb";
49 switch (fPMConversion) {
50 case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion:
joshualitt30ba4362014-08-21 20:18:45 -070051 fsBuilder->codeAppendf(
changjun.yangcecc91c2014-08-19 18:24:30 -070052 "%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.coma04e8e82012-08-27 12:53:13 +000054 break;
55 case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion:
commit-bot@chromium.orgb4e200e2013-06-19 11:41:02 +000056 // 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.
joshualitt30ba4362014-08-21 20:18:45 -070060 fsBuilder->codeAppendf(
changjun.yangcecc91c2014-08-19 18:24:30 -070061 "%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.coma04e8e82012-08-27 12:53:13 +000063 break;
64 case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion:
joshualitt30ba4362014-08-21 20:18:45 -070065 fsBuilder->codeAppendf(
changjun.yangcecc91c2014-08-19 18:24:30 -070066 "%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.coma04e8e82012-08-27 12:53:13 +000068 break;
69 case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion:
joshualitt30ba4362014-08-21 20:18:45 -070070 fsBuilder->codeAppendf(
changjun.yangcecc91c2014-08-19 18:24:30 -070071 "%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.coma04e8e82012-08-27 12:53:13 +000073 break;
robertphillips@google.com2af1b182012-08-28 11:23:09 +000074 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +000075 SkFAIL("Unknown conversion op.");
robertphillips@google.com2af1b182012-08-28 11:23:09 +000076 break;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000077 }
joshualitt30ba4362014-08-21 20:18:45 -070078 fsBuilder->codeAppendf("%s = %s;", outputColor, tmpVar.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000079 }
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000080 SkString modulate;
egdaniel089f8de2014-10-09 10:34:58 -070081 GrGLSLMulVarBy4f(&modulate, outputColor, inputColor);
joshualitt30ba4362014-08-21 20:18:45 -070082 fsBuilder->codeAppend(modulate.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000083 }
84
joshualittb0a8a372014-09-23 09:50:21 -070085 static inline void GenKey(const GrProcessor& processor, const GrGLCaps&,
86 GrProcessorKeyBuilder* b) {
87 const GrConfigConversionEffect& conv = processor.cast<GrConfigConversionEffect>();
bsalomon63e99f72014-07-21 08:03:14 -070088 uint32_t key = (conv.swapsRedAndBlue() ? 0 : 1) | (conv.pmConversion() << 1);
89 b->add32(key);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000090 }
91
92private:
93 bool fSwapRedAndBlue;
94 GrConfigConversionEffect::PMConversion fPMConversion;
95
joshualittb0a8a372014-09-23 09:50:21 -070096 typedef GrGLFragmentProcessor INHERITED;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000097
98};
99
100///////////////////////////////////////////////////////////////////////////////
101
102GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture,
103 bool swapRedAndBlue,
bsalomon@google.comb1456d72012-11-02 18:23:45 +0000104 PMConversion pmConversion,
105 const SkMatrix& matrix)
106 : GrSingleTextureEffect(texture, matrix)
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000107 , fSwapRedAndBlue(swapRedAndBlue)
108 , fPMConversion(pmConversion) {
joshualitteb2a6762014-12-04 11:35:33 -0800109 this->initClassID<GrConfigConversionEffect>();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000110 SkASSERT(kRGBA_8888_GrPixelConfig == texture->config() ||
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000111 kBGRA_8888_GrPixelConfig == texture->config());
112 // Why did we pollute our texture cache instead of using a GrSingleTextureEffect?
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000113 SkASSERT(swapRedAndBlue || kNone_PMConversion != pmConversion);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000114}
115
bsalomon0e08fc12014-10-15 08:19:04 -0700116bool GrConfigConversionEffect::onIsEqual(const GrFragmentProcessor& s) const {
joshualitt49586be2014-09-16 08:21:41 -0700117 const GrConfigConversionEffect& other = s.cast<GrConfigConversionEffect>();
bsalomon420d7e92014-10-16 09:18:09 -0700118 return other.fSwapRedAndBlue == fSwapRedAndBlue &&
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000119 other.fPMConversion == fPMConversion;
120}
121
egdaniel605dd0f2014-11-12 08:35:25 -0800122void GrConfigConversionEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
egdaniel1a8ecdf2014-10-03 06:24:12 -0700123 this->updateInvariantOutputForModulation(inout);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000124}
125
126///////////////////////////////////////////////////////////////////////////////
127
joshualittb0a8a372014-09-23 09:50:21 -0700128GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConfigConversionEffect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000129
joshualittb0a8a372014-09-23 09:50:21 -0700130GrFragmentProcessor* GrConfigConversionEffect::TestCreate(SkRandom* random,
131 GrContext*,
132 const GrDrawTargetCaps&,
133 GrTexture* textures[]) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000134 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 }
bsalomon55fad7a2014-07-08 07:34:20 -0700141 return SkNEW_ARGS(GrConfigConversionEffect,
joshualittb0a8a372014-09-23 09:50:21 -0700142 (textures[GrProcessorUnitTest::kSkiaPMTextureIdx],
bsalomon@google.com6340a412013-01-22 19:55:59 +0000143 swapRB,
144 pmConv,
joshualittb0a8a372014-09-23 09:50:21 -0700145 GrProcessorUnitTest::TestMatrix(random)));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000146}
147
148///////////////////////////////////////////////////////////////////////////////
joshualitteb2a6762014-12-04 11:35:33 -0800149
150void GrConfigConversionEffect::getGLProcessorKey(const GrGLCaps& caps,
151 GrProcessorKeyBuilder* b) const {
152 GrGLConfigConversionEffect::GenKey(*this, caps, b);
153}
154
155GrGLFragmentProcessor* GrConfigConversionEffect::createGLInstance() const {
156 return SkNEW_ARGS(GrGLConfigConversionEffect, (*this));
157}
158
robertphillipse85a32d2015-02-10 08:16:55 -0800159
160
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000161void 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.org972f9cd2014-03-28 17:58:28 +0000177 color[2] = SkTMin(x, y);
178 color[1] = SkTMin(x, y);
179 color[0] = SkTMin(x, y);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000180 }
181 }
182
bsalomonf2703d82014-10-28 14:33:06 -0700183 GrSurfaceDesc desc;
184 desc.fFlags = kRenderTarget_GrSurfaceFlag |
185 kNoStencil_GrSurfaceFlag;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000186 desc.fWidth = 256;
187 desc.fHeight = 256;
188 desc.fConfig = kRGBA_8888_GrPixelConfig;
189
bsalomond0423582015-02-06 08:49:24 -0800190 SkAutoTUnref<GrTexture> readTex(context->createTexture(desc, true, NULL, 0));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000191 if (!readTex.get()) {
192 return;
193 }
bsalomond0423582015-02-06 08:49:24 -0800194 SkAutoTUnref<GrTexture> tempTex(context->createTexture(desc, true, NULL, 0));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000195 if (!tempTex.get()) {
196 return;
197 }
bsalomonf2703d82014-10-28 14:33:06 -0700198 desc.fFlags = kNone_GrSurfaceFlags;
bsalomond0423582015-02-06 08:49:24 -0800199 SkAutoTUnref<GrTexture> dataTex(context->createTexture(desc, true, data, 0));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000200 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.org972f9cd2014-03-28 17:58:28 +0000213 for (size_t i = 0; i < SK_ARRAY_COUNT(kConversionRules) && failed; ++i) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000214 *pmToUPMRule = kConversionRules[i][0];
215 *upmToPMRule = kConversionRules[i][1];
216
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000217 static const SkRect kDstRect = SkRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256));
218 static const SkRect kSrcRect = SkRect::MakeWH(SK_Scalar1, SK_Scalar1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000219 // 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
joshualittb0a8a372014-09-23 09:50:21 -0700223 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.coma04e8e82012-08-27 12:53:13 +0000232
233 context->setRenderTarget(readTex->asRenderTarget());
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000234 GrPaint paint1;
joshualittb0a8a372014-09-23 09:50:21 -0700235 paint1.addColorProcessor(pmToUPM1);
joshualitt16b27892014-12-18 07:47:16 -0800236 context->drawNonAARectToRect(paint1, SkMatrix::I(), kDstRect, kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000237
238 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
239
240 context->setRenderTarget(tempTex->asRenderTarget());
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000241 GrPaint paint2;
joshualittb0a8a372014-09-23 09:50:21 -0700242 paint2.addColorProcessor(upmToPM);
joshualitt16b27892014-12-18 07:47:16 -0800243 context->drawNonAARectToRect(paint2, SkMatrix::I(), kDstRect, kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000244 context->setRenderTarget(readTex->asRenderTarget());
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000245
246 GrPaint paint3;
joshualittb0a8a372014-09-23 09:50:21 -0700247 paint3.addColorProcessor(pmToUPM2);
joshualitt16b27892014-12-18 07:47:16 -0800248 context->drawNonAARectToRect(paint3, SkMatrix::I(), kDstRect, kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000249
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
joshualittb0a8a372014-09-23 09:50:21 -0700268const GrFragmentProcessor* GrConfigConversionEffect::Create(GrTexture* texture,
bsalomon83d081a2014-07-08 09:56:10 -0700269 bool swapRedAndBlue,
270 PMConversion pmConversion,
271 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000272 if (!swapRedAndBlue && kNone_PMConversion == pmConversion) {
bsalomon@google.comadc65362013-01-28 14:26:09 +0000273 // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000274 // then we may pollute our texture cache with redundant shaders. So in the case that no
bsalomon@google.comadc65362013-01-28 14:26:09 +0000275 // conversions were requested we instead return a GrSimpleTextureEffect.
276 return GrSimpleTextureEffect::Create(texture, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000277 } 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.comadc65362013-01-28 14:26:09 +0000282 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000283 }
bsalomon55fad7a2014-07-08 07:34:20 -0700284 return SkNEW_ARGS(GrConfigConversionEffect, (texture,
285 swapRedAndBlue,
286 pmConversion,
287 matrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000288 }
289}