blob: 935e074e03dbb85aba40b9b0ba6eb8c042fb0cb7 [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 {
commit-bot@chromium.org7ab7ca42013-08-28 15:59:13 +000033 SkString 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);
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +000036 builder->fsAppendTextureLookup(samplers[0], coords.c_str(), coordsType);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000037 builder->fsCodeAppend(";\n");
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000038 if (GrConfigConversionEffect::kNone_PMConversion == fPMConversion) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000039 SkASSERT(fSwapRedAndBlue);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000040 builder->fsCodeAppendf("\t%s = %s.bgra;\n", outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000041 } else {
42 const char* swiz = fSwapRedAndBlue ? "bgr" : "rgb";
43 switch (fPMConversion) {
44 case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion:
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000045 builder->fsCodeAppendf(
bsalomon@google.com868a8e72012-08-30 19:11:34 +000046 "\t\t%s = vec4(ceil(%s.%s * %s.a * 255.0) / 255.0, %s.a);\n",
47 outputColor, outputColor, swiz, outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000048 break;
49 case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion:
commit-bot@chromium.orgb4e200e2013-06-19 11:41:02 +000050 // Add a compensation(0.001) here to avoid the side effect of the floor operation.
51 // In Intel GPUs, the integer value converted from floor(%s.r * 255.0) / 255.0
52 // is less than the integer value converted from %s.r by 1 when the %s.r is
53 // converted from the integer value 2^n, such as 1, 2, 4, 8, etc.
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000054 builder->fsCodeAppendf(
commit-bot@chromium.orgb4e200e2013-06-19 11:41:02 +000055 "\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 +000056 outputColor, outputColor, swiz, outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000057 break;
58 case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion:
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000059 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 +000060 outputColor, outputColor, outputColor, swiz, outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000061 break;
62 case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion:
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000063 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 +000064 outputColor, outputColor, outputColor, swiz, outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000065 break;
robertphillips@google.com2af1b182012-08-28 11:23:09 +000066 default:
67 GrCrash("Unknown conversion op.");
68 break;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000069 }
70 }
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000071 SkString modulate;
72 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor);
73 builder->fsCodeAppend(modulate.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000074 }
75
bsalomon@google.comc7818882013-03-20 19:19:53 +000076 void setData(const GrGLUniformManager& uman, const GrDrawEffect& drawEffect) {
77 const GrConfigConversionEffect& conv = drawEffect.castEffect<GrConfigConversionEffect>();
78 fEffectMatrix.setData(uman, conv.getMatrix(), drawEffect, conv.texture(0));
bsalomon@google.comb1456d72012-11-02 18:23:45 +000079 }
80
bsalomon@google.comc7818882013-03-20 19:19:53 +000081 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
82 const GrConfigConversionEffect& conv = drawEffect.castEffect<GrConfigConversionEffect>();
83 EffectKey key = static_cast<EffectKey>(conv.swapsRedAndBlue()) | (conv.pmConversion() << 1);
bsalomon@google.comb1456d72012-11-02 18:23:45 +000084 key <<= GrGLEffectMatrix::kKeyBits;
bsalomon@google.comc7818882013-03-20 19:19:53 +000085 EffectKey matrixKey = GrGLEffectMatrix::GenKey(conv.getMatrix(),
86 drawEffect,
87 conv.coordsType(),
88 conv.texture(0));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000089 SkASSERT(!(matrixKey & key));
bsalomon@google.comb1456d72012-11-02 18:23:45 +000090 return matrixKey | key;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000091 }
92
93private:
94 bool fSwapRedAndBlue;
95 GrConfigConversionEffect::PMConversion fPMConversion;
bsalomon@google.comb1456d72012-11-02 18:23:45 +000096 GrGLEffectMatrix fEffectMatrix;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000097
bsalomon@google.com22a800a2012-10-26 19:16:46 +000098 typedef GrGLEffect INHERITED;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000099
100};
101
102///////////////////////////////////////////////////////////////////////////////
103
104GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture,
105 bool swapRedAndBlue,
bsalomon@google.comb1456d72012-11-02 18:23:45 +0000106 PMConversion pmConversion,
107 const SkMatrix& matrix)
108 : GrSingleTextureEffect(texture, matrix)
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000109 , fSwapRedAndBlue(swapRedAndBlue)
110 , fPMConversion(pmConversion) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000111 SkASSERT(kRGBA_8888_GrPixelConfig == texture->config() ||
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000112 kBGRA_8888_GrPixelConfig == texture->config());
113 // Why did we pollute our texture cache instead of using a GrSingleTextureEffect?
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000114 SkASSERT(swapRedAndBlue || kNone_PMConversion != pmConversion);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000115}
116
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000117const GrBackendEffectFactory& GrConfigConversionEffect::getFactory() const {
118 return GrTBackendEffectFactory<GrConfigConversionEffect>::getInstance();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000119}
120
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000121bool GrConfigConversionEffect::onIsEqual(const GrEffect& s) const {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000122 const GrConfigConversionEffect& other = CastEffect<GrConfigConversionEffect>(s);
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000123 return this->texture(0) == s.texture(0) &&
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000124 other.fSwapRedAndBlue == fSwapRedAndBlue &&
125 other.fPMConversion == fPMConversion;
126}
127
128void GrConfigConversionEffect::getConstantColorComponents(GrColor* color,
129 uint32_t* validFlags) const {
130 this->updateConstantColorComponentsForModulation(color, validFlags);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000131}
132
133///////////////////////////////////////////////////////////////////////////////
134
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000135GR_DEFINE_EFFECT_TEST(GrConfigConversionEffect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000136
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000137GrEffectRef* GrConfigConversionEffect::TestCreate(SkRandom* random,
sugoi@google.come0e385c2013-03-11 18:50:03 +0000138 GrContext*,
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000139 const GrDrawTargetCaps&,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000140 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 }
bsalomon@google.com6340a412013-01-22 19:55:59 +0000148 AutoEffectUnref effect(SkNEW_ARGS(GrConfigConversionEffect,
149 (textures[GrEffectUnitTest::kSkiaPMTextureIdx],
150 swapRB,
151 pmConv,
152 GrEffectUnitTest::TestMatrix(random))));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000153 return CreateEffectRef(effect);
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;
173 color[2] = GrMin(x, y);
174 color[1] = GrMin(x, y);
175 color[0] = GrMin(x, y);
176 }
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
209 for (size_t i = 0; i < GR_ARRAY_COUNT(kConversionRules) && failed; ++i) {
210 *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
bsalomon@google.com6340a412013-01-22 19:55:59 +0000219 AutoEffectUnref pmToUPM1(SkNEW_ARGS(GrConfigConversionEffect, (dataTex,
220 false,
221 *pmToUPMRule,
222 SkMatrix::I())));
223 AutoEffectUnref upmToPM(SkNEW_ARGS(GrConfigConversionEffect, (readTex,
224 false,
225 *upmToPMRule,
226 SkMatrix::I())));
227 AutoEffectUnref pmToUPM2(SkNEW_ARGS(GrConfigConversionEffect, (tempTex,
228 false,
229 *pmToUPMRule,
230 SkMatrix::I())));
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000231
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000232 SkAutoTUnref<GrEffectRef> pmToUPMEffect1(CreateEffectRef(pmToUPM1));
233 SkAutoTUnref<GrEffectRef> upmToPMEffect(CreateEffectRef(upmToPM));
234 SkAutoTUnref<GrEffectRef> pmToUPMEffect2(CreateEffectRef(pmToUPM2));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000235
236 context->setRenderTarget(readTex->asRenderTarget());
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000237 GrPaint paint1;
238 paint1.addColorEffect(pmToUPMEffect1);
239 context->drawRectToRect(paint1, kDstRect, kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000240
241 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
242
243 context->setRenderTarget(tempTex->asRenderTarget());
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000244 GrPaint paint2;
245 paint2.addColorEffect(upmToPMEffect);
246 context->drawRectToRect(paint2, kDstRect, kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000247 context->setRenderTarget(readTex->asRenderTarget());
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000248
249 GrPaint paint3;
250 paint3.addColorEffect(pmToUPMEffect2);
251 context->drawRectToRect(paint3, kDstRect, kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000252
253 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
254
255 failed = false;
256 for (int y = 0; y < 256 && !failed; ++y) {
257 for (int x = 0; x <= y; ++x) {
258 if (firstRead[256 * y + x] != secondRead[256 * y + x]) {
259 failed = true;
260 break;
261 }
262 }
263 }
264 }
265 if (failed) {
266 *pmToUPMRule = kNone_PMConversion;
267 *upmToPMRule = kNone_PMConversion;
268 }
269}
270
bsalomon@google.comadc65362013-01-28 14:26:09 +0000271const GrEffectRef* GrConfigConversionEffect::Create(GrTexture* texture,
272 bool swapRedAndBlue,
273 PMConversion pmConversion,
274 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000275 if (!swapRedAndBlue && kNone_PMConversion == pmConversion) {
bsalomon@google.comadc65362013-01-28 14:26:09 +0000276 // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000277 // then we may pollute our texture cache with redundant shaders. So in the case that no
bsalomon@google.comadc65362013-01-28 14:26:09 +0000278 // conversions were requested we instead return a GrSimpleTextureEffect.
279 return GrSimpleTextureEffect::Create(texture, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000280 } else {
281 if (kRGBA_8888_GrPixelConfig != texture->config() &&
282 kBGRA_8888_GrPixelConfig != texture->config() &&
283 kNone_PMConversion != pmConversion) {
284 // The PM conversions assume colors are 0..255
bsalomon@google.comadc65362013-01-28 14:26:09 +0000285 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000286 }
bsalomon@google.com6340a412013-01-22 19:55:59 +0000287 AutoEffectUnref effect(SkNEW_ARGS(GrConfigConversionEffect, (texture,
288 swapRedAndBlue,
289 pmConversion,
290 matrix)));
bsalomon@google.comadc65362013-01-28 14:26:09 +0000291 return CreateEffectRef(effect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000292 }
293}