blob: d7ba6865c96be6ce8724f9be29004958bc18e958 [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"
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000010#include "GrTBackendEffectFactory.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000011#include "GrSimpleTextureEffect.h"
bsalomon@google.comd698f772012-10-25 13:22:00 +000012#include "gl/GrGLEffect.h"
bsalomon848faf02014-07-11 10:01:02 -070013#include "gl/GrGLShaderBuilder.h"
bsalomon@google.comb1456d72012-11-02 18:23:45 +000014#include "SkMatrix.h"
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000015
bsalomon@google.com22a800a2012-10-26 19:16:46 +000016class GrGLConfigConversionEffect : public GrGLEffect {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000017public:
bsalomon@google.com396e61f2012-10-25 19:00:29 +000018 GrGLConfigConversionEffect(const GrBackendEffectFactory& factory,
bsalomon@google.comc7818882013-03-20 19:19:53 +000019 const GrDrawEffect& drawEffect)
bsalomon@google.com77af6802013-10-02 13:04:56 +000020 : INHERITED (factory) {
bsalomon@google.comc7818882013-03-20 19:19:53 +000021 const GrConfigConversionEffect& effect = drawEffect.castEffect<GrConfigConversionEffect>();
bsalomon@google.com021fc732012-10-25 12:47:42 +000022 fSwapRedAndBlue = effect.swapsRedAndBlue();
23 fPMConversion = effect.pmConversion();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000024 }
25
bsalomon@google.com22a800a2012-10-26 19:16:46 +000026 virtual void emitCode(GrGLShaderBuilder* builder,
bsalomon@google.comc7818882013-03-20 19:19:53 +000027 const GrDrawEffect&,
bsalomon63e99f72014-07-21 08:03:14 -070028 const GrEffectKey& key,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000029 const char* outputColor,
30 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000031 const TransformedCoordsArray& coords,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000032 const TextureSamplerArray& samplers) SK_OVERRIDE {
changjun.yangcecc91c2014-08-19 18:24:30 -070033 // Using highp for GLES here in order to avoid some precision issues on specific GPUs.
34 GrGLShaderVar tmpVar("tmpColor", kVec4f_GrSLType, 0, GrGLShaderVar::kHigh_Precision);
35 SkString tmpDecl;
36 tmpVar.appendDecl(builder->ctxInfo(), &tmpDecl);
37 builder->fsCodeAppendf("%s;", tmpDecl.c_str());
38
39 builder->fsCodeAppendf("%s = ", tmpVar.c_str());
bsalomon@google.com77af6802013-10-02 13:04:56 +000040 builder->fsAppendTextureLookup(samplers[0], coords[0].c_str(), coords[0].type());
changjun.yangcecc91c2014-08-19 18:24:30 -070041 builder->fsCodeAppend(";");
42
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000043 if (GrConfigConversionEffect::kNone_PMConversion == fPMConversion) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000044 SkASSERT(fSwapRedAndBlue);
changjun.yangcecc91c2014-08-19 18:24:30 -070045 builder->fsCodeAppendf("%s = %s.bgra;", outputColor, tmpVar.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000046 } else {
47 const char* swiz = fSwapRedAndBlue ? "bgr" : "rgb";
48 switch (fPMConversion) {
49 case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion:
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000050 builder->fsCodeAppendf(
changjun.yangcecc91c2014-08-19 18:24:30 -070051 "%s = vec4(ceil(%s.%s * %s.a * 255.0) / 255.0, %s.a);",
52 tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000053 break;
54 case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion:
commit-bot@chromium.orgb4e200e2013-06-19 11:41:02 +000055 // Add a compensation(0.001) here to avoid the side effect of the floor operation.
56 // In Intel GPUs, the integer value converted from floor(%s.r * 255.0) / 255.0
57 // is less than the integer value converted from %s.r by 1 when the %s.r is
58 // converted from the integer value 2^n, such as 1, 2, 4, 8, etc.
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000059 builder->fsCodeAppendf(
changjun.yangcecc91c2014-08-19 18:24:30 -070060 "%s = vec4(floor(%s.%s * %s.a * 255.0 + 0.001) / 255.0, %s.a);",
61 tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000062 break;
63 case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion:
changjun.yangcecc91c2014-08-19 18:24:30 -070064 builder->fsCodeAppendf(
65 "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(ceil(%s.%s / %s.a * 255.0) / 255.0, %s.a);",
66 tmpVar.c_str(), 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_RoundDown_PMConversion:
changjun.yangcecc91c2014-08-19 18:24:30 -070069 builder->fsCodeAppendf(
70 "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(floor(%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;
robertphillips@google.com2af1b182012-08-28 11:23:09 +000073 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +000074 SkFAIL("Unknown conversion op.");
robertphillips@google.com2af1b182012-08-28 11:23:09 +000075 break;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000076 }
changjun.yangcecc91c2014-08-19 18:24:30 -070077 builder->fsCodeAppendf("%s = %s;", outputColor, tmpVar.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000078 }
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000079 SkString modulate;
80 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor);
81 builder->fsCodeAppend(modulate.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000082 }
83
bsalomon63e99f72014-07-21 08:03:14 -070084 static inline void GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&,
85 GrEffectKeyBuilder* b) {
bsalomon@google.comc7818882013-03-20 19:19:53 +000086 const GrConfigConversionEffect& conv = drawEffect.castEffect<GrConfigConversionEffect>();
bsalomon63e99f72014-07-21 08:03:14 -070087 uint32_t key = (conv.swapsRedAndBlue() ? 0 : 1) | (conv.pmConversion() << 1);
88 b->add32(key);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000089 }
90
91private:
92 bool fSwapRedAndBlue;
93 GrConfigConversionEffect::PMConversion fPMConversion;
94
bsalomon@google.com22a800a2012-10-26 19:16:46 +000095 typedef GrGLEffect INHERITED;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000096
97};
98
99///////////////////////////////////////////////////////////////////////////////
100
101GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture,
102 bool swapRedAndBlue,
bsalomon@google.comb1456d72012-11-02 18:23:45 +0000103 PMConversion pmConversion,
104 const SkMatrix& matrix)
105 : GrSingleTextureEffect(texture, matrix)
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000106 , fSwapRedAndBlue(swapRedAndBlue)
107 , fPMConversion(pmConversion) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000108 SkASSERT(kRGBA_8888_GrPixelConfig == texture->config() ||
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000109 kBGRA_8888_GrPixelConfig == texture->config());
110 // Why did we pollute our texture cache instead of using a GrSingleTextureEffect?
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000111 SkASSERT(swapRedAndBlue || kNone_PMConversion != pmConversion);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000112}
113
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000114const GrBackendEffectFactory& GrConfigConversionEffect::getFactory() const {
115 return GrTBackendEffectFactory<GrConfigConversionEffect>::getInstance();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000116}
117
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000118bool GrConfigConversionEffect::onIsEqual(const GrEffect& s) const {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000119 const GrConfigConversionEffect& other = CastEffect<GrConfigConversionEffect>(s);
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000120 return this->texture(0) == s.texture(0) &&
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000121 other.fSwapRedAndBlue == fSwapRedAndBlue &&
122 other.fPMConversion == fPMConversion;
123}
124
125void GrConfigConversionEffect::getConstantColorComponents(GrColor* color,
126 uint32_t* validFlags) const {
127 this->updateConstantColorComponentsForModulation(color, validFlags);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000128}
129
130///////////////////////////////////////////////////////////////////////////////
131
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000132GR_DEFINE_EFFECT_TEST(GrConfigConversionEffect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000133
bsalomon83d081a2014-07-08 09:56:10 -0700134GrEffect* GrConfigConversionEffect::TestCreate(SkRandom* random,
135 GrContext*,
136 const GrDrawTargetCaps&,
137 GrTexture* textures[]) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000138 PMConversion pmConv = static_cast<PMConversion>(random->nextULessThan(kPMConversionCnt));
139 bool swapRB;
140 if (kNone_PMConversion == pmConv) {
141 swapRB = true;
142 } else {
143 swapRB = random->nextBool();
144 }
bsalomon55fad7a2014-07-08 07:34:20 -0700145 return SkNEW_ARGS(GrConfigConversionEffect,
bsalomon@google.com6340a412013-01-22 19:55:59 +0000146 (textures[GrEffectUnitTest::kSkiaPMTextureIdx],
147 swapRB,
148 pmConv,
bsalomon55fad7a2014-07-08 07:34:20 -0700149 GrEffectUnitTest::TestMatrix(random)));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000150}
151
152///////////////////////////////////////////////////////////////////////////////
153void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context,
154 PMConversion* pmToUPMRule,
155 PMConversion* upmToPMRule) {
156 *pmToUPMRule = kNone_PMConversion;
157 *upmToPMRule = kNone_PMConversion;
158 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
159 uint32_t* srcData = data.get();
160 uint32_t* firstRead = data.get() + 256 * 256;
161 uint32_t* secondRead = data.get() + 2 * 256 * 256;
162
163 // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate
164 // values in row y. We set r,g, and b to the same value since they are handled identically.
165 for (int y = 0; y < 256; ++y) {
166 for (int x = 0; x < 256; ++x) {
167 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
168 color[3] = y;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000169 color[2] = SkTMin(x, y);
170 color[1] = SkTMin(x, y);
171 color[0] = SkTMin(x, y);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000172 }
173 }
174
175 GrTextureDesc desc;
176 desc.fFlags = kRenderTarget_GrTextureFlagBit |
177 kNoStencil_GrTextureFlagBit;
178 desc.fWidth = 256;
179 desc.fHeight = 256;
180 desc.fConfig = kRGBA_8888_GrPixelConfig;
181
182 SkAutoTUnref<GrTexture> readTex(context->createUncachedTexture(desc, NULL, 0));
183 if (!readTex.get()) {
184 return;
185 }
186 SkAutoTUnref<GrTexture> tempTex(context->createUncachedTexture(desc, NULL, 0));
187 if (!tempTex.get()) {
188 return;
189 }
190 desc.fFlags = kNone_GrTextureFlags;
191 SkAutoTUnref<GrTexture> dataTex(context->createUncachedTexture(desc, data, 0));
192 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
201 GrContext::AutoWideOpenIdentityDraw awoid(context, NULL);
202
203 bool failed = true;
204
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000205 for (size_t i = 0; i < SK_ARRAY_COUNT(kConversionRules) && failed; ++i) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000206 *pmToUPMRule = kConversionRules[i][0];
207 *upmToPMRule = kConversionRules[i][1];
208
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000209 static const SkRect kDstRect = SkRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256));
210 static const SkRect kSrcRect = SkRect::MakeWH(SK_Scalar1, SK_Scalar1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000211 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
212 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
213 // We then verify that two reads produced the same values.
214
bsalomon55fad7a2014-07-08 07:34:20 -0700215 SkAutoTUnref<GrEffect> pmToUPM1(SkNEW_ARGS(GrConfigConversionEffect, (dataTex,
216 false,
217 *pmToUPMRule,
218 SkMatrix::I())));
219 SkAutoTUnref<GrEffect> upmToPM(SkNEW_ARGS(GrConfigConversionEffect, (readTex,
220 false,
221 *upmToPMRule,
222 SkMatrix::I())));
223 SkAutoTUnref<GrEffect> pmToUPM2(SkNEW_ARGS(GrConfigConversionEffect, (tempTex,
224 false,
225 *pmToUPMRule,
226 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;
bsalomon55fad7a2014-07-08 07:34:20 -0700230 paint1.addColorEffect(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;
bsalomon55fad7a2014-07-08 07:34:20 -0700237 paint2.addColorEffect(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;
bsalomon55fad7a2014-07-08 07:34:20 -0700242 paint3.addColorEffect(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
bsalomon83d081a2014-07-08 09:56:10 -0700263const GrEffect* GrConfigConversionEffect::Create(GrTexture* texture,
264 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}