blob: db6141f8e9a8f922c1ed2f77894fa35e95dfac1b [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"
bsalomon@google.comb1456d72012-11-02 18:23:45 +000013#include "gl/GrGLEffectMatrix.h"
14#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)
20 : INHERITED (factory)
21 , fEffectMatrix(drawEffect.castEffect<GrConfigConversionEffect>().coordsType()) {
22 const GrConfigConversionEffect& effect = drawEffect.castEffect<GrConfigConversionEffect>();
bsalomon@google.com021fc732012-10-25 12:47:42 +000023 fSwapRedAndBlue = effect.swapsRedAndBlue();
24 fPMConversion = effect.pmConversion();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000025 }
26
bsalomon@google.com22a800a2012-10-26 19:16:46 +000027 virtual void emitCode(GrGLShaderBuilder* builder,
bsalomon@google.comc7818882013-03-20 19:19:53 +000028 const GrDrawEffect&,
bsalomon@google.comb1456d72012-11-02 18:23:45 +000029 EffectKey key,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000030 const char* outputColor,
31 const char* inputColor,
32 const TextureSamplerArray& samplers) SK_OVERRIDE {
bsalomon@google.comb1456d72012-11-02 18:23:45 +000033 const char* coords;
bsalomon@google.comc7818882013-03-20 19:19:53 +000034 GrSLType coordsType = fEffectMatrix.emitCode(builder, key, &coords);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000035 builder->fsCodeAppendf("\t\t%s = ", outputColor);
36 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType,
37 samplers[0],
38 coords,
39 coordsType);
40 builder->fsCodeAppend(";\n");
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000041 if (GrConfigConversionEffect::kNone_PMConversion == fPMConversion) {
42 GrAssert(fSwapRedAndBlue);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000043 builder->fsCodeAppendf("\t%s = %s.bgra;\n", outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000044 } else {
45 const char* swiz = fSwapRedAndBlue ? "bgr" : "rgb";
46 switch (fPMConversion) {
47 case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion:
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000048 builder->fsCodeAppendf(
bsalomon@google.com868a8e72012-08-30 19:11:34 +000049 "\t\t%s = vec4(ceil(%s.%s * %s.a * 255.0) / 255.0, %s.a);\n",
50 outputColor, outputColor, swiz, outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000051 break;
52 case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion:
commit-bot@chromium.orgb4e200e2013-06-19 11:41:02 +000053 // Add a compensation(0.001) here to avoid the side effect of the floor operation.
54 // In Intel GPUs, the integer value converted from floor(%s.r * 255.0) / 255.0
55 // is less than the integer value converted from %s.r by 1 when the %s.r is
56 // converted from the integer value 2^n, such as 1, 2, 4, 8, etc.
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000057 builder->fsCodeAppendf(
commit-bot@chromium.orgb4e200e2013-06-19 11:41:02 +000058 "\t\t%s = vec4(floor(%s.%s * %s.a * 255.0 + 0.001) / 255.0, %s.a);\n",
bsalomon@google.com868a8e72012-08-30 19:11:34 +000059 outputColor, outputColor, swiz, outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000060 break;
61 case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion:
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000062 builder->fsCodeAppendf("\t\t%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(ceil(%s.%s / %s.a * 255.0) / 255.0, %s.a);\n",
bsalomon@google.com868a8e72012-08-30 19:11:34 +000063 outputColor, outputColor, outputColor, swiz, outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000064 break;
65 case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion:
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000066 builder->fsCodeAppendf("\t\t%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(floor(%s.%s / %s.a * 255.0) / 255.0, %s.a);\n",
bsalomon@google.com868a8e72012-08-30 19:11:34 +000067 outputColor, outputColor, outputColor, swiz, outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000068 break;
robertphillips@google.com2af1b182012-08-28 11:23:09 +000069 default:
70 GrCrash("Unknown conversion op.");
71 break;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000072 }
73 }
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000074 SkString modulate;
75 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor);
76 builder->fsCodeAppend(modulate.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000077 }
78
bsalomon@google.comc7818882013-03-20 19:19:53 +000079 void setData(const GrGLUniformManager& uman, const GrDrawEffect& drawEffect) {
80 const GrConfigConversionEffect& conv = drawEffect.castEffect<GrConfigConversionEffect>();
81 fEffectMatrix.setData(uman, conv.getMatrix(), drawEffect, conv.texture(0));
bsalomon@google.comb1456d72012-11-02 18:23:45 +000082 }
83
bsalomon@google.comc7818882013-03-20 19:19:53 +000084 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
85 const GrConfigConversionEffect& conv = drawEffect.castEffect<GrConfigConversionEffect>();
86 EffectKey key = static_cast<EffectKey>(conv.swapsRedAndBlue()) | (conv.pmConversion() << 1);
bsalomon@google.comb1456d72012-11-02 18:23:45 +000087 key <<= GrGLEffectMatrix::kKeyBits;
bsalomon@google.comc7818882013-03-20 19:19:53 +000088 EffectKey matrixKey = GrGLEffectMatrix::GenKey(conv.getMatrix(),
89 drawEffect,
90 conv.coordsType(),
91 conv.texture(0));
bsalomon@google.comb1456d72012-11-02 18:23:45 +000092 GrAssert(!(matrixKey & key));
93 return matrixKey | key;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000094 }
95
96private:
97 bool fSwapRedAndBlue;
98 GrConfigConversionEffect::PMConversion fPMConversion;
bsalomon@google.comb1456d72012-11-02 18:23:45 +000099 GrGLEffectMatrix fEffectMatrix;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000100
bsalomon@google.com22a800a2012-10-26 19:16:46 +0000101 typedef GrGLEffect INHERITED;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000102
103};
104
105///////////////////////////////////////////////////////////////////////////////
106
107GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture,
108 bool swapRedAndBlue,
bsalomon@google.comb1456d72012-11-02 18:23:45 +0000109 PMConversion pmConversion,
110 const SkMatrix& matrix)
111 : GrSingleTextureEffect(texture, matrix)
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000112 , fSwapRedAndBlue(swapRedAndBlue)
113 , fPMConversion(pmConversion) {
114 GrAssert(kRGBA_8888_GrPixelConfig == texture->config() ||
115 kBGRA_8888_GrPixelConfig == texture->config());
116 // Why did we pollute our texture cache instead of using a GrSingleTextureEffect?
117 GrAssert(swapRedAndBlue || kNone_PMConversion != pmConversion);
118}
119
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000120const GrBackendEffectFactory& GrConfigConversionEffect::getFactory() const {
121 return GrTBackendEffectFactory<GrConfigConversionEffect>::getInstance();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000122}
123
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000124bool GrConfigConversionEffect::onIsEqual(const GrEffect& s) const {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000125 const GrConfigConversionEffect& other = CastEffect<GrConfigConversionEffect>(s);
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000126 return this->texture(0) == s.texture(0) &&
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000127 other.fSwapRedAndBlue == fSwapRedAndBlue &&
128 other.fPMConversion == fPMConversion;
129}
130
131void GrConfigConversionEffect::getConstantColorComponents(GrColor* color,
132 uint32_t* validFlags) const {
133 this->updateConstantColorComponentsForModulation(color, validFlags);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000134}
135
136///////////////////////////////////////////////////////////////////////////////
137
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000138GR_DEFINE_EFFECT_TEST(GrConfigConversionEffect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000139
bsalomon@google.com73a96942013-02-13 16:31:19 +0000140GrEffectRef* GrConfigConversionEffect::TestCreate(SkMWCRandom* random,
sugoi@google.come0e385c2013-03-11 18:50:03 +0000141 GrContext*,
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000142 const GrDrawTargetCaps&,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000143 GrTexture* textures[]) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000144 PMConversion pmConv = static_cast<PMConversion>(random->nextULessThan(kPMConversionCnt));
145 bool swapRB;
146 if (kNone_PMConversion == pmConv) {
147 swapRB = true;
148 } else {
149 swapRB = random->nextBool();
150 }
bsalomon@google.com6340a412013-01-22 19:55:59 +0000151 AutoEffectUnref effect(SkNEW_ARGS(GrConfigConversionEffect,
152 (textures[GrEffectUnitTest::kSkiaPMTextureIdx],
153 swapRB,
154 pmConv,
155 GrEffectUnitTest::TestMatrix(random))));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000156 return CreateEffectRef(effect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000157}
158
159///////////////////////////////////////////////////////////////////////////////
160void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context,
161 PMConversion* pmToUPMRule,
162 PMConversion* upmToPMRule) {
163 *pmToUPMRule = kNone_PMConversion;
164 *upmToPMRule = kNone_PMConversion;
165 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
166 uint32_t* srcData = data.get();
167 uint32_t* firstRead = data.get() + 256 * 256;
168 uint32_t* secondRead = data.get() + 2 * 256 * 256;
169
170 // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate
171 // values in row y. We set r,g, and b to the same value since they are handled identically.
172 for (int y = 0; y < 256; ++y) {
173 for (int x = 0; x < 256; ++x) {
174 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
175 color[3] = y;
176 color[2] = GrMin(x, y);
177 color[1] = GrMin(x, y);
178 color[0] = GrMin(x, y);
179 }
180 }
181
182 GrTextureDesc desc;
183 desc.fFlags = kRenderTarget_GrTextureFlagBit |
184 kNoStencil_GrTextureFlagBit;
185 desc.fWidth = 256;
186 desc.fHeight = 256;
187 desc.fConfig = kRGBA_8888_GrPixelConfig;
188
189 SkAutoTUnref<GrTexture> readTex(context->createUncachedTexture(desc, NULL, 0));
190 if (!readTex.get()) {
191 return;
192 }
193 SkAutoTUnref<GrTexture> tempTex(context->createUncachedTexture(desc, NULL, 0));
194 if (!tempTex.get()) {
195 return;
196 }
197 desc.fFlags = kNone_GrTextureFlags;
198 SkAutoTUnref<GrTexture> dataTex(context->createUncachedTexture(desc, data, 0));
199 if (!dataTex.get()) {
200 return;
201 }
202
203 static const PMConversion kConversionRules[][2] = {
204 {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion},
205 {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion},
206 };
207
208 GrContext::AutoWideOpenIdentityDraw awoid(context, NULL);
209
210 bool failed = true;
211
212 for (size_t i = 0; i < GR_ARRAY_COUNT(kConversionRules) && failed; ++i) {
213 *pmToUPMRule = kConversionRules[i][0];
214 *upmToPMRule = kConversionRules[i][1];
215
bsalomon@google.com81712882012-11-01 17:12:34 +0000216 static const GrRect kDstRect = GrRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256));
217 static const GrRect kSrcRect = GrRect::MakeWH(SK_Scalar1, SK_Scalar1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000218 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
219 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
220 // We then verify that two reads produced the same values.
221
bsalomon@google.com6340a412013-01-22 19:55:59 +0000222 AutoEffectUnref pmToUPM1(SkNEW_ARGS(GrConfigConversionEffect, (dataTex,
223 false,
224 *pmToUPMRule,
225 SkMatrix::I())));
226 AutoEffectUnref upmToPM(SkNEW_ARGS(GrConfigConversionEffect, (readTex,
227 false,
228 *upmToPMRule,
229 SkMatrix::I())));
230 AutoEffectUnref pmToUPM2(SkNEW_ARGS(GrConfigConversionEffect, (tempTex,
231 false,
232 *pmToUPMRule,
233 SkMatrix::I())));
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000234
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000235 SkAutoTUnref<GrEffectRef> pmToUPMEffect1(CreateEffectRef(pmToUPM1));
236 SkAutoTUnref<GrEffectRef> upmToPMEffect(CreateEffectRef(upmToPM));
237 SkAutoTUnref<GrEffectRef> pmToUPMEffect2(CreateEffectRef(pmToUPM2));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000238
239 context->setRenderTarget(readTex->asRenderTarget());
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000240 GrPaint paint1;
241 paint1.addColorEffect(pmToUPMEffect1);
242 context->drawRectToRect(paint1, kDstRect, kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000243
244 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
245
246 context->setRenderTarget(tempTex->asRenderTarget());
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000247 GrPaint paint2;
248 paint2.addColorEffect(upmToPMEffect);
249 context->drawRectToRect(paint2, kDstRect, kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000250 context->setRenderTarget(readTex->asRenderTarget());
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000251
252 GrPaint paint3;
253 paint3.addColorEffect(pmToUPMEffect2);
254 context->drawRectToRect(paint3, kDstRect, kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000255
256 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
257
258 failed = false;
259 for (int y = 0; y < 256 && !failed; ++y) {
260 for (int x = 0; x <= y; ++x) {
261 if (firstRead[256 * y + x] != secondRead[256 * y + x]) {
262 failed = true;
263 break;
264 }
265 }
266 }
267 }
268 if (failed) {
269 *pmToUPMRule = kNone_PMConversion;
270 *upmToPMRule = kNone_PMConversion;
271 }
272}
273
bsalomon@google.comadc65362013-01-28 14:26:09 +0000274const GrEffectRef* GrConfigConversionEffect::Create(GrTexture* texture,
275 bool swapRedAndBlue,
276 PMConversion pmConversion,
277 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000278 if (!swapRedAndBlue && kNone_PMConversion == pmConversion) {
bsalomon@google.comadc65362013-01-28 14:26:09 +0000279 // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000280 // then we may pollute our texture cache with redundant shaders. So in the case that no
bsalomon@google.comadc65362013-01-28 14:26:09 +0000281 // conversions were requested we instead return a GrSimpleTextureEffect.
282 return GrSimpleTextureEffect::Create(texture, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000283 } else {
284 if (kRGBA_8888_GrPixelConfig != texture->config() &&
285 kBGRA_8888_GrPixelConfig != texture->config() &&
286 kNone_PMConversion != pmConversion) {
287 // The PM conversions assume colors are 0..255
bsalomon@google.comadc65362013-01-28 14:26:09 +0000288 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000289 }
bsalomon@google.com6340a412013-01-22 19:55:59 +0000290 AutoEffectUnref effect(SkNEW_ARGS(GrConfigConversionEffect, (texture,
291 swapRedAndBlue,
292 pmConversion,
293 matrix)));
bsalomon@google.comadc65362013-01-28 14:26:09 +0000294 return CreateEffectRef(effect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000295 }
296}