blob: 3042d86f032b552e3a2e3fb5f4b973545fc5567b [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"
joshualittb0a8a372014-09-23 09:50:21 -070010#include "GrTBackendProcessorFactory.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000011#include "GrSimpleTextureEffect.h"
joshualittb0a8a372014-09-23 09:50:21 -070012#include "gl/GrGLProcessor.h"
joshualitt30ba4362014-08-21 20:18:45 -070013#include "gl/builders/GrGLProgramBuilder.h"
bsalomon@google.comb1456d72012-11-02 18:23:45 +000014#include "SkMatrix.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:
joshualittb0a8a372014-09-23 09:50:21 -070018 GrGLConfigConversionEffect(const GrBackendProcessorFactory& factory,
19 const GrProcessor& processor)
bsalomon@google.com77af6802013-10-02 13:04:56 +000020 : INHERITED (factory) {
joshualittb0a8a372014-09-23 09:50:21 -070021 const GrConfigConversionEffect& configConversionEffect =
22 processor.cast<GrConfigConversionEffect>();
joshualitt49586be2014-09-16 08:21:41 -070023 fSwapRedAndBlue = configConversionEffect.swapsRedAndBlue();
24 fPMConversion = configConversionEffect.pmConversion();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000025 }
26
joshualitt30ba4362014-08-21 20:18:45 -070027 virtual void emitCode(GrGLProgramBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -070028 const GrFragmentProcessor&,
29 const GrProcessorKey& key,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000030 const char* outputColor,
31 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000032 const TransformedCoordsArray& coords,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000033 const TextureSamplerArray& samplers) SK_OVERRIDE {
changjun.yangcecc91c2014-08-19 18:24:30 -070034 // Using highp for GLES here in order to avoid some precision issues on specific GPUs.
35 GrGLShaderVar tmpVar("tmpColor", kVec4f_GrSLType, 0, GrGLShaderVar::kHigh_Precision);
36 SkString tmpDecl;
37 tmpVar.appendDecl(builder->ctxInfo(), &tmpDecl);
changjun.yangcecc91c2014-08-19 18:24:30 -070038
joshualitt30ba4362014-08-21 20:18:45 -070039 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
40
41 fsBuilder->codeAppendf("%s;", tmpDecl.c_str());
42
43 fsBuilder->codeAppendf("%s = ", tmpVar.c_str());
joshualitt23e280d2014-09-18 12:26:38 -070044 fsBuilder->appendTextureLookup(samplers[0], coords[0].c_str(), coords[0].getType());
joshualitt30ba4362014-08-21 20:18:45 -070045 fsBuilder->codeAppend(";");
changjun.yangcecc91c2014-08-19 18:24:30 -070046
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000047 if (GrConfigConversionEffect::kNone_PMConversion == fPMConversion) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000048 SkASSERT(fSwapRedAndBlue);
joshualitt30ba4362014-08-21 20:18:45 -070049 fsBuilder->codeAppendf("%s = %s.bgra;", outputColor, tmpVar.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000050 } else {
51 const char* swiz = fSwapRedAndBlue ? "bgr" : "rgb";
52 switch (fPMConversion) {
53 case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion:
joshualitt30ba4362014-08-21 20:18:45 -070054 fsBuilder->codeAppendf(
changjun.yangcecc91c2014-08-19 18:24:30 -070055 "%s = vec4(ceil(%s.%s * %s.a * 255.0) / 255.0, %s.a);",
56 tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000057 break;
58 case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion:
commit-bot@chromium.orgb4e200e2013-06-19 11:41:02 +000059 // Add a compensation(0.001) here to avoid the side effect of the floor operation.
60 // In Intel GPUs, the integer value converted from floor(%s.r * 255.0) / 255.0
61 // is less than the integer value converted from %s.r by 1 when the %s.r is
62 // converted from the integer value 2^n, such as 1, 2, 4, 8, etc.
joshualitt30ba4362014-08-21 20:18:45 -070063 fsBuilder->codeAppendf(
changjun.yangcecc91c2014-08-19 18:24:30 -070064 "%s = vec4(floor(%s.%s * %s.a * 255.0 + 0.001) / 255.0, %s.a);",
65 tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000066 break;
67 case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion:
joshualitt30ba4362014-08-21 20:18:45 -070068 fsBuilder->codeAppendf(
changjun.yangcecc91c2014-08-19 18:24:30 -070069 "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(ceil(%s.%s / %s.a * 255.0) / 255.0, %s.a);",
70 tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000071 break;
72 case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion:
joshualitt30ba4362014-08-21 20:18:45 -070073 fsBuilder->codeAppendf(
changjun.yangcecc91c2014-08-19 18:24:30 -070074 "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(floor(%s.%s / %s.a * 255.0) / 255.0, %s.a);",
75 tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000076 break;
robertphillips@google.com2af1b182012-08-28 11:23:09 +000077 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +000078 SkFAIL("Unknown conversion op.");
robertphillips@google.com2af1b182012-08-28 11:23:09 +000079 break;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000080 }
joshualitt30ba4362014-08-21 20:18:45 -070081 fsBuilder->codeAppendf("%s = %s;", outputColor, tmpVar.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000082 }
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000083 SkString modulate;
84 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor);
joshualitt30ba4362014-08-21 20:18:45 -070085 fsBuilder->codeAppend(modulate.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000086 }
87
joshualittb0a8a372014-09-23 09:50:21 -070088 static inline void GenKey(const GrProcessor& processor, const GrGLCaps&,
89 GrProcessorKeyBuilder* b) {
90 const GrConfigConversionEffect& conv = processor.cast<GrConfigConversionEffect>();
bsalomon63e99f72014-07-21 08:03:14 -070091 uint32_t key = (conv.swapsRedAndBlue() ? 0 : 1) | (conv.pmConversion() << 1);
92 b->add32(key);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000093 }
94
95private:
96 bool fSwapRedAndBlue;
97 GrConfigConversionEffect::PMConversion fPMConversion;
98
joshualittb0a8a372014-09-23 09:50:21 -070099 typedef GrGLFragmentProcessor INHERITED;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000100
101};
102
103///////////////////////////////////////////////////////////////////////////////
104
105GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture,
106 bool swapRedAndBlue,
bsalomon@google.comb1456d72012-11-02 18:23:45 +0000107 PMConversion pmConversion,
108 const SkMatrix& matrix)
109 : GrSingleTextureEffect(texture, matrix)
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000110 , fSwapRedAndBlue(swapRedAndBlue)
111 , fPMConversion(pmConversion) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000112 SkASSERT(kRGBA_8888_GrPixelConfig == texture->config() ||
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000113 kBGRA_8888_GrPixelConfig == texture->config());
114 // Why did we pollute our texture cache instead of using a GrSingleTextureEffect?
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000115 SkASSERT(swapRedAndBlue || kNone_PMConversion != pmConversion);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000116}
117
joshualittb0a8a372014-09-23 09:50:21 -0700118const GrBackendFragmentProcessorFactory& GrConfigConversionEffect::getFactory() const {
119 return GrTBackendFragmentProcessorFactory<GrConfigConversionEffect>::getInstance();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000120}
121
joshualittb0a8a372014-09-23 09:50:21 -0700122bool GrConfigConversionEffect::onIsEqual(const GrProcessor& s) const {
joshualitt49586be2014-09-16 08:21:41 -0700123 const GrConfigConversionEffect& other = s.cast<GrConfigConversionEffect>();
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000124 return this->texture(0) == s.texture(0) &&
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000125 other.fSwapRedAndBlue == fSwapRedAndBlue &&
126 other.fPMConversion == fPMConversion;
127}
128
129void GrConfigConversionEffect::getConstantColorComponents(GrColor* color,
130 uint32_t* validFlags) const {
131 this->updateConstantColorComponentsForModulation(color, validFlags);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000132}
133
134///////////////////////////////////////////////////////////////////////////////
135
joshualittb0a8a372014-09-23 09:50:21 -0700136GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConfigConversionEffect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000137
joshualittb0a8a372014-09-23 09:50:21 -0700138GrFragmentProcessor* GrConfigConversionEffect::TestCreate(SkRandom* random,
139 GrContext*,
140 const GrDrawTargetCaps&,
141 GrTexture* textures[]) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000142 PMConversion pmConv = static_cast<PMConversion>(random->nextULessThan(kPMConversionCnt));
143 bool swapRB;
144 if (kNone_PMConversion == pmConv) {
145 swapRB = true;
146 } else {
147 swapRB = random->nextBool();
148 }
bsalomon55fad7a2014-07-08 07:34:20 -0700149 return SkNEW_ARGS(GrConfigConversionEffect,
joshualittb0a8a372014-09-23 09:50:21 -0700150 (textures[GrProcessorUnitTest::kSkiaPMTextureIdx],
bsalomon@google.com6340a412013-01-22 19:55:59 +0000151 swapRB,
152 pmConv,
joshualittb0a8a372014-09-23 09:50:21 -0700153 GrProcessorUnitTest::TestMatrix(random)));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000154}
155
156///////////////////////////////////////////////////////////////////////////////
157void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context,
158 PMConversion* pmToUPMRule,
159 PMConversion* upmToPMRule) {
160 *pmToUPMRule = kNone_PMConversion;
161 *upmToPMRule = kNone_PMConversion;
162 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
163 uint32_t* srcData = data.get();
164 uint32_t* firstRead = data.get() + 256 * 256;
165 uint32_t* secondRead = data.get() + 2 * 256 * 256;
166
167 // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate
168 // values in row y. We set r,g, and b to the same value since they are handled identically.
169 for (int y = 0; y < 256; ++y) {
170 for (int x = 0; x < 256; ++x) {
171 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
172 color[3] = y;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000173 color[2] = SkTMin(x, y);
174 color[1] = SkTMin(x, y);
175 color[0] = SkTMin(x, y);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000176 }
177 }
178
179 GrTextureDesc desc;
180 desc.fFlags = kRenderTarget_GrTextureFlagBit |
181 kNoStencil_GrTextureFlagBit;
182 desc.fWidth = 256;
183 desc.fHeight = 256;
184 desc.fConfig = kRGBA_8888_GrPixelConfig;
185
186 SkAutoTUnref<GrTexture> readTex(context->createUncachedTexture(desc, NULL, 0));
187 if (!readTex.get()) {
188 return;
189 }
190 SkAutoTUnref<GrTexture> tempTex(context->createUncachedTexture(desc, NULL, 0));
191 if (!tempTex.get()) {
192 return;
193 }
194 desc.fFlags = kNone_GrTextureFlags;
195 SkAutoTUnref<GrTexture> dataTex(context->createUncachedTexture(desc, data, 0));
196 if (!dataTex.get()) {
197 return;
198 }
199
200 static const PMConversion kConversionRules[][2] = {
201 {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion},
202 {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion},
203 };
204
205 GrContext::AutoWideOpenIdentityDraw awoid(context, NULL);
206
207 bool failed = true;
208
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000209 for (size_t i = 0; i < SK_ARRAY_COUNT(kConversionRules) && failed; ++i) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000210 *pmToUPMRule = kConversionRules[i][0];
211 *upmToPMRule = kConversionRules[i][1];
212
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000213 static const SkRect kDstRect = SkRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256));
214 static const SkRect kSrcRect = SkRect::MakeWH(SK_Scalar1, SK_Scalar1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000215 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
216 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
217 // We then verify that two reads produced the same values.
218
joshualittb0a8a372014-09-23 09:50:21 -0700219 SkAutoTUnref<GrFragmentProcessor> pmToUPM1(
220 SkNEW_ARGS(GrConfigConversionEffect,
221 (dataTex, false, *pmToUPMRule, SkMatrix::I())));
222 SkAutoTUnref<GrFragmentProcessor> upmToPM(
223 SkNEW_ARGS(GrConfigConversionEffect,
224 (readTex, false, *upmToPMRule, SkMatrix::I())));
225 SkAutoTUnref<GrFragmentProcessor> pmToUPM2(
226 SkNEW_ARGS(GrConfigConversionEffect,
227 (tempTex, false, *pmToUPMRule, SkMatrix::I())));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000228
229 context->setRenderTarget(readTex->asRenderTarget());
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000230 GrPaint paint1;
joshualittb0a8a372014-09-23 09:50:21 -0700231 paint1.addColorProcessor(pmToUPM1);
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000232 context->drawRectToRect(paint1, kDstRect, kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000233
234 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
235
236 context->setRenderTarget(tempTex->asRenderTarget());
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000237 GrPaint paint2;
joshualittb0a8a372014-09-23 09:50:21 -0700238 paint2.addColorProcessor(upmToPM);
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000239 context->drawRectToRect(paint2, kDstRect, kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000240 context->setRenderTarget(readTex->asRenderTarget());
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000241
242 GrPaint paint3;
joshualittb0a8a372014-09-23 09:50:21 -0700243 paint3.addColorProcessor(pmToUPM2);
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000244 context->drawRectToRect(paint3, kDstRect, kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000245
246 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
247
248 failed = false;
249 for (int y = 0; y < 256 && !failed; ++y) {
250 for (int x = 0; x <= y; ++x) {
251 if (firstRead[256 * y + x] != secondRead[256 * y + x]) {
252 failed = true;
253 break;
254 }
255 }
256 }
257 }
258 if (failed) {
259 *pmToUPMRule = kNone_PMConversion;
260 *upmToPMRule = kNone_PMConversion;
261 }
262}
263
joshualittb0a8a372014-09-23 09:50:21 -0700264const GrFragmentProcessor* GrConfigConversionEffect::Create(GrTexture* texture,
bsalomon83d081a2014-07-08 09:56:10 -0700265 bool swapRedAndBlue,
266 PMConversion pmConversion,
267 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000268 if (!swapRedAndBlue && kNone_PMConversion == pmConversion) {
bsalomon@google.comadc65362013-01-28 14:26:09 +0000269 // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000270 // then we may pollute our texture cache with redundant shaders. So in the case that no
bsalomon@google.comadc65362013-01-28 14:26:09 +0000271 // conversions were requested we instead return a GrSimpleTextureEffect.
272 return GrSimpleTextureEffect::Create(texture, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000273 } else {
274 if (kRGBA_8888_GrPixelConfig != texture->config() &&
275 kBGRA_8888_GrPixelConfig != texture->config() &&
276 kNone_PMConversion != pmConversion) {
277 // The PM conversions assume colors are 0..255
bsalomon@google.comadc65362013-01-28 14:26:09 +0000278 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000279 }
bsalomon55fad7a2014-07-08 07:34:20 -0700280 return SkNEW_ARGS(GrConfigConversionEffect, (texture,
281 swapRedAndBlue,
282 pmConversion,
283 matrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000284 }
285}