blob: 6f02f3073fdc71f6f9d53a77f04e2c89e0d826ca [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"
joshualittb0a8a372014-09-23 09:50:21 -070011#include "GrTBackendProcessorFactory.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000012#include "GrSimpleTextureEffect.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.comb1456d72012-11-02 18:23:45 +000015#include "SkMatrix.h"
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000016
joshualittb0a8a372014-09-23 09:50:21 -070017class GrGLConfigConversionEffect : public GrGLFragmentProcessor {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000018public:
joshualittb0a8a372014-09-23 09:50:21 -070019 GrGLConfigConversionEffect(const GrBackendProcessorFactory& factory,
20 const GrProcessor& processor)
bsalomon@google.com77af6802013-10-02 13:04:56 +000021 : INHERITED (factory) {
joshualittb0a8a372014-09-23 09:50:21 -070022 const GrConfigConversionEffect& configConversionEffect =
23 processor.cast<GrConfigConversionEffect>();
joshualitt49586be2014-09-16 08:21:41 -070024 fSwapRedAndBlue = configConversionEffect.swapsRedAndBlue();
25 fPMConversion = configConversionEffect.pmConversion();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000026 }
27
joshualitt15988992014-10-09 15:04:05 -070028 virtual void emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -070029 const GrFragmentProcessor&,
30 const GrProcessorKey& key,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000031 const char* outputColor,
32 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000033 const TransformedCoordsArray& coords,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000034 const TextureSamplerArray& samplers) SK_OVERRIDE {
changjun.yangcecc91c2014-08-19 18:24:30 -070035 // Using highp for GLES here in order to avoid some precision issues on specific GPUs.
36 GrGLShaderVar tmpVar("tmpColor", kVec4f_GrSLType, 0, GrGLShaderVar::kHigh_Precision);
37 SkString tmpDecl;
38 tmpVar.appendDecl(builder->ctxInfo(), &tmpDecl);
changjun.yangcecc91c2014-08-19 18:24:30 -070039
joshualitt15988992014-10-09 15:04:05 -070040 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -070041
42 fsBuilder->codeAppendf("%s;", tmpDecl.c_str());
43
44 fsBuilder->codeAppendf("%s = ", tmpVar.c_str());
joshualitt23e280d2014-09-18 12:26:38 -070045 fsBuilder->appendTextureLookup(samplers[0], coords[0].c_str(), coords[0].getType());
joshualitt30ba4362014-08-21 20:18:45 -070046 fsBuilder->codeAppend(";");
changjun.yangcecc91c2014-08-19 18:24:30 -070047
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000048 if (GrConfigConversionEffect::kNone_PMConversion == fPMConversion) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000049 SkASSERT(fSwapRedAndBlue);
joshualitt30ba4362014-08-21 20:18:45 -070050 fsBuilder->codeAppendf("%s = %s.bgra;", outputColor, tmpVar.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000051 } else {
52 const char* swiz = fSwapRedAndBlue ? "bgr" : "rgb";
53 switch (fPMConversion) {
54 case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion:
joshualitt30ba4362014-08-21 20:18:45 -070055 fsBuilder->codeAppendf(
changjun.yangcecc91c2014-08-19 18:24:30 -070056 "%s = vec4(ceil(%s.%s * %s.a * 255.0) / 255.0, %s.a);",
57 tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000058 break;
59 case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion:
commit-bot@chromium.orgb4e200e2013-06-19 11:41:02 +000060 // Add a compensation(0.001) here to avoid the side effect of the floor operation.
61 // In Intel GPUs, the integer value converted from floor(%s.r * 255.0) / 255.0
62 // is less than the integer value converted from %s.r by 1 when the %s.r is
63 // converted from the integer value 2^n, such as 1, 2, 4, 8, etc.
joshualitt30ba4362014-08-21 20:18:45 -070064 fsBuilder->codeAppendf(
changjun.yangcecc91c2014-08-19 18:24:30 -070065 "%s = vec4(floor(%s.%s * %s.a * 255.0 + 0.001) / 255.0, %s.a);",
66 tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000067 break;
68 case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion:
joshualitt30ba4362014-08-21 20:18:45 -070069 fsBuilder->codeAppendf(
changjun.yangcecc91c2014-08-19 18:24:30 -070070 "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(ceil(%s.%s / %s.a * 255.0) / 255.0, %s.a);",
71 tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000072 break;
73 case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion:
joshualitt30ba4362014-08-21 20:18:45 -070074 fsBuilder->codeAppendf(
changjun.yangcecc91c2014-08-19 18:24:30 -070075 "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(floor(%s.%s / %s.a * 255.0) / 255.0, %s.a);",
76 tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000077 break;
robertphillips@google.com2af1b182012-08-28 11:23:09 +000078 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +000079 SkFAIL("Unknown conversion op.");
robertphillips@google.com2af1b182012-08-28 11:23:09 +000080 break;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000081 }
joshualitt30ba4362014-08-21 20:18:45 -070082 fsBuilder->codeAppendf("%s = %s;", outputColor, tmpVar.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000083 }
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000084 SkString modulate;
egdaniel089f8de2014-10-09 10:34:58 -070085 GrGLSLMulVarBy4f(&modulate, outputColor, inputColor);
joshualitt30ba4362014-08-21 20:18:45 -070086 fsBuilder->codeAppend(modulate.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000087 }
88
joshualittb0a8a372014-09-23 09:50:21 -070089 static inline void GenKey(const GrProcessor& processor, const GrGLCaps&,
90 GrProcessorKeyBuilder* b) {
91 const GrConfigConversionEffect& conv = processor.cast<GrConfigConversionEffect>();
bsalomon63e99f72014-07-21 08:03:14 -070092 uint32_t key = (conv.swapsRedAndBlue() ? 0 : 1) | (conv.pmConversion() << 1);
93 b->add32(key);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000094 }
95
96private:
97 bool fSwapRedAndBlue;
98 GrConfigConversionEffect::PMConversion fPMConversion;
99
joshualittb0a8a372014-09-23 09:50:21 -0700100 typedef GrGLFragmentProcessor INHERITED;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000101
102};
103
104///////////////////////////////////////////////////////////////////////////////
105
106GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture,
107 bool swapRedAndBlue,
bsalomon@google.comb1456d72012-11-02 18:23:45 +0000108 PMConversion pmConversion,
109 const SkMatrix& matrix)
110 : GrSingleTextureEffect(texture, matrix)
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000111 , fSwapRedAndBlue(swapRedAndBlue)
112 , fPMConversion(pmConversion) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000113 SkASSERT(kRGBA_8888_GrPixelConfig == texture->config() ||
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000114 kBGRA_8888_GrPixelConfig == texture->config());
115 // Why did we pollute our texture cache instead of using a GrSingleTextureEffect?
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000116 SkASSERT(swapRedAndBlue || kNone_PMConversion != pmConversion);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000117}
118
joshualittb0a8a372014-09-23 09:50:21 -0700119const GrBackendFragmentProcessorFactory& GrConfigConversionEffect::getFactory() const {
120 return GrTBackendFragmentProcessorFactory<GrConfigConversionEffect>::getInstance();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000121}
122
bsalomon0e08fc12014-10-15 08:19:04 -0700123bool GrConfigConversionEffect::onIsEqual(const GrFragmentProcessor& s) const {
joshualitt49586be2014-09-16 08:21:41 -0700124 const GrConfigConversionEffect& other = s.cast<GrConfigConversionEffect>();
bsalomon420d7e92014-10-16 09:18:09 -0700125 return other.fSwapRedAndBlue == fSwapRedAndBlue &&
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000126 other.fPMConversion == fPMConversion;
127}
128
egdaniel605dd0f2014-11-12 08:35:25 -0800129void GrConfigConversionEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
egdaniel1a8ecdf2014-10-03 06:24:12 -0700130 this->updateInvariantOutputForModulation(inout);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000131}
132
133///////////////////////////////////////////////////////////////////////////////
134
joshualittb0a8a372014-09-23 09:50:21 -0700135GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConfigConversionEffect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000136
joshualittb0a8a372014-09-23 09:50:21 -0700137GrFragmentProcessor* GrConfigConversionEffect::TestCreate(SkRandom* random,
138 GrContext*,
139 const GrDrawTargetCaps&,
140 GrTexture* textures[]) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000141 PMConversion pmConv = static_cast<PMConversion>(random->nextULessThan(kPMConversionCnt));
142 bool swapRB;
143 if (kNone_PMConversion == pmConv) {
144 swapRB = true;
145 } else {
146 swapRB = random->nextBool();
147 }
bsalomon55fad7a2014-07-08 07:34:20 -0700148 return SkNEW_ARGS(GrConfigConversionEffect,
joshualittb0a8a372014-09-23 09:50:21 -0700149 (textures[GrProcessorUnitTest::kSkiaPMTextureIdx],
bsalomon@google.com6340a412013-01-22 19:55:59 +0000150 swapRB,
151 pmConv,
joshualittb0a8a372014-09-23 09:50:21 -0700152 GrProcessorUnitTest::TestMatrix(random)));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000153}
154
155///////////////////////////////////////////////////////////////////////////////
156void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context,
157 PMConversion* pmToUPMRule,
158 PMConversion* upmToPMRule) {
159 *pmToUPMRule = kNone_PMConversion;
160 *upmToPMRule = kNone_PMConversion;
161 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
162 uint32_t* srcData = data.get();
163 uint32_t* firstRead = data.get() + 256 * 256;
164 uint32_t* secondRead = data.get() + 2 * 256 * 256;
165
166 // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate
167 // values in row y. We set r,g, and b to the same value since they are handled identically.
168 for (int y = 0; y < 256; ++y) {
169 for (int x = 0; x < 256; ++x) {
170 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
171 color[3] = y;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000172 color[2] = SkTMin(x, y);
173 color[1] = SkTMin(x, y);
174 color[0] = SkTMin(x, y);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000175 }
176 }
177
bsalomonf2703d82014-10-28 14:33:06 -0700178 GrSurfaceDesc desc;
179 desc.fFlags = kRenderTarget_GrSurfaceFlag |
180 kNoStencil_GrSurfaceFlag;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000181 desc.fWidth = 256;
182 desc.fHeight = 256;
183 desc.fConfig = kRGBA_8888_GrPixelConfig;
184
185 SkAutoTUnref<GrTexture> readTex(context->createUncachedTexture(desc, NULL, 0));
186 if (!readTex.get()) {
187 return;
188 }
189 SkAutoTUnref<GrTexture> tempTex(context->createUncachedTexture(desc, NULL, 0));
190 if (!tempTex.get()) {
191 return;
192 }
bsalomonf2703d82014-10-28 14:33:06 -0700193 desc.fFlags = kNone_GrSurfaceFlags;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000194 SkAutoTUnref<GrTexture> dataTex(context->createUncachedTexture(desc, data, 0));
195 if (!dataTex.get()) {
196 return;
197 }
198
199 static const PMConversion kConversionRules[][2] = {
200 {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion},
201 {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion},
202 };
203
204 GrContext::AutoWideOpenIdentityDraw awoid(context, NULL);
205
206 bool failed = true;
207
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000208 for (size_t i = 0; i < SK_ARRAY_COUNT(kConversionRules) && failed; ++i) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000209 *pmToUPMRule = kConversionRules[i][0];
210 *upmToPMRule = kConversionRules[i][1];
211
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000212 static const SkRect kDstRect = SkRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256));
213 static const SkRect kSrcRect = SkRect::MakeWH(SK_Scalar1, SK_Scalar1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000214 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
215 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
216 // We then verify that two reads produced the same values.
217
joshualittb0a8a372014-09-23 09:50:21 -0700218 SkAutoTUnref<GrFragmentProcessor> pmToUPM1(
219 SkNEW_ARGS(GrConfigConversionEffect,
220 (dataTex, false, *pmToUPMRule, SkMatrix::I())));
221 SkAutoTUnref<GrFragmentProcessor> upmToPM(
222 SkNEW_ARGS(GrConfigConversionEffect,
223 (readTex, false, *upmToPMRule, SkMatrix::I())));
224 SkAutoTUnref<GrFragmentProcessor> pmToUPM2(
225 SkNEW_ARGS(GrConfigConversionEffect,
226 (tempTex, false, *pmToUPMRule, SkMatrix::I())));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000227
228 context->setRenderTarget(readTex->asRenderTarget());
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000229 GrPaint paint1;
joshualittb0a8a372014-09-23 09:50:21 -0700230 paint1.addColorProcessor(pmToUPM1);
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000231 context->drawRectToRect(paint1, kDstRect, kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000232
233 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
234
235 context->setRenderTarget(tempTex->asRenderTarget());
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000236 GrPaint paint2;
joshualittb0a8a372014-09-23 09:50:21 -0700237 paint2.addColorProcessor(upmToPM);
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000238 context->drawRectToRect(paint2, kDstRect, kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000239 context->setRenderTarget(readTex->asRenderTarget());
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000240
241 GrPaint paint3;
joshualittb0a8a372014-09-23 09:50:21 -0700242 paint3.addColorProcessor(pmToUPM2);
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000243 context->drawRectToRect(paint3, kDstRect, kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000244
245 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
246
247 failed = false;
248 for (int y = 0; y < 256 && !failed; ++y) {
249 for (int x = 0; x <= y; ++x) {
250 if (firstRead[256 * y + x] != secondRead[256 * y + x]) {
251 failed = true;
252 break;
253 }
254 }
255 }
256 }
257 if (failed) {
258 *pmToUPMRule = kNone_PMConversion;
259 *upmToPMRule = kNone_PMConversion;
260 }
261}
262
joshualittb0a8a372014-09-23 09:50:21 -0700263const GrFragmentProcessor* GrConfigConversionEffect::Create(GrTexture* texture,
bsalomon83d081a2014-07-08 09:56:10 -0700264 bool swapRedAndBlue,
265 PMConversion pmConversion,
266 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000267 if (!swapRedAndBlue && kNone_PMConversion == pmConversion) {
bsalomon@google.comadc65362013-01-28 14:26:09 +0000268 // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000269 // then we may pollute our texture cache with redundant shaders. So in the case that no
bsalomon@google.comadc65362013-01-28 14:26:09 +0000270 // conversions were requested we instead return a GrSimpleTextureEffect.
271 return GrSimpleTextureEffect::Create(texture, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000272 } else {
273 if (kRGBA_8888_GrPixelConfig != texture->config() &&
274 kBGRA_8888_GrPixelConfig != texture->config() &&
275 kNone_PMConversion != pmConversion) {
276 // The PM conversions assume colors are 0..255
bsalomon@google.comadc65362013-01-28 14:26:09 +0000277 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000278 }
bsalomon55fad7a2014-07-08 07:34:20 -0700279 return SkNEW_ARGS(GrConfigConversionEffect, (texture,
280 swapRedAndBlue,
281 pmConversion,
282 matrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000283 }
284}