blob: 5cc05e2f39edb04abae81711ff0631ae180f92b5 [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.com6340a412013-01-22 19:55:59 +000019 const GrEffectRef& s) : INHERITED (factory) {
20 const GrConfigConversionEffect& effect = CastEffect<GrConfigConversionEffect>(s);
bsalomon@google.com021fc732012-10-25 12:47:42 +000021 fSwapRedAndBlue = effect.swapsRedAndBlue();
22 fPMConversion = effect.pmConversion();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000023 }
24
bsalomon@google.com22a800a2012-10-26 19:16:46 +000025 virtual void emitCode(GrGLShaderBuilder* builder,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000026 const GrEffectStage&,
bsalomon@google.comb1456d72012-11-02 18:23:45 +000027 EffectKey key,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000028 const char* vertexCoords,
29 const char* outputColor,
30 const char* inputColor,
31 const TextureSamplerArray& samplers) SK_OVERRIDE {
bsalomon@google.comb1456d72012-11-02 18:23:45 +000032 const char* coords;
33 GrSLType coordsType = fEffectMatrix.emitCode(builder, key, vertexCoords, &coords);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000034 builder->fsCodeAppendf("\t\t%s = ", outputColor);
35 builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType,
36 samplers[0],
37 coords,
38 coordsType);
39 builder->fsCodeAppend(";\n");
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000040 if (GrConfigConversionEffect::kNone_PMConversion == fPMConversion) {
41 GrAssert(fSwapRedAndBlue);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000042 builder->fsCodeAppendf("\t%s = %s.bgra;\n", outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000043 } else {
44 const char* swiz = fSwapRedAndBlue ? "bgr" : "rgb";
45 switch (fPMConversion) {
46 case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion:
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000047 builder->fsCodeAppendf(
bsalomon@google.com868a8e72012-08-30 19:11:34 +000048 "\t\t%s = vec4(ceil(%s.%s * %s.a * 255.0) / 255.0, %s.a);\n",
49 outputColor, outputColor, swiz, outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000050 break;
51 case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion:
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000052 builder->fsCodeAppendf(
bsalomon@google.com868a8e72012-08-30 19:11:34 +000053 "\t\t%s = vec4(floor(%s.%s * %s.a * 255.0) / 255.0, %s.a);\n",
54 outputColor, outputColor, swiz, outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000055 break;
56 case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion:
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000057 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 +000058 outputColor, outputColor, outputColor, swiz, outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000059 break;
60 case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion:
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000061 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 +000062 outputColor, outputColor, outputColor, swiz, outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000063 break;
robertphillips@google.com2af1b182012-08-28 11:23:09 +000064 default:
65 GrCrash("Unknown conversion op.");
66 break;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000067 }
68 }
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000069 SkString modulate;
70 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor);
71 builder->fsCodeAppend(modulate.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000072 }
73
bsalomon@google.comb1456d72012-11-02 18:23:45 +000074 void setData(const GrGLUniformManager& uman, const GrEffectStage& stage) {
75 const GrConfigConversionEffect& effect =
bsalomon@google.com6340a412013-01-22 19:55:59 +000076 GetEffectFromStage<GrConfigConversionEffect>(stage);
bsalomon@google.comb1456d72012-11-02 18:23:45 +000077 fEffectMatrix.setData(uman,
78 effect.getMatrix(),
79 stage.getCoordChangeMatrix(),
80 effect.texture(0));
81 }
82
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000083 static inline EffectKey GenKey(const GrEffectStage& s, const GrGLCaps&) {
bsalomon@google.com6340a412013-01-22 19:55:59 +000084 const GrConfigConversionEffect& effect = GetEffectFromStage<GrConfigConversionEffect>(s);
bsalomon@google.comb1456d72012-11-02 18:23:45 +000085 EffectKey key = static_cast<EffectKey>(effect.swapsRedAndBlue()) |
86 (effect.pmConversion() << 1);
87 key <<= GrGLEffectMatrix::kKeyBits;
88 EffectKey matrixKey = GrGLEffectMatrix::GenKey(effect.getMatrix(),
89 s.getCoordChangeMatrix(),
90 effect.texture(0));
91 GrAssert(!(matrixKey & key));
92 return matrixKey | key;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000093 }
94
95private:
96 bool fSwapRedAndBlue;
97 GrConfigConversionEffect::PMConversion fPMConversion;
bsalomon@google.comb1456d72012-11-02 18:23:45 +000098 GrGLEffectMatrix fEffectMatrix;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000099
bsalomon@google.com22a800a2012-10-26 19:16:46 +0000100 typedef GrGLEffect 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) {
113 GrAssert(kRGBA_8888_GrPixelConfig == texture->config() ||
114 kBGRA_8888_GrPixelConfig == texture->config());
115 // Why did we pollute our texture cache instead of using a GrSingleTextureEffect?
116 GrAssert(swapRedAndBlue || kNone_PMConversion != pmConversion);
117}
118
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000119const GrBackendEffectFactory& GrConfigConversionEffect::getFactory() const {
120 return GrTBackendEffectFactory<GrConfigConversionEffect>::getInstance();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000121}
122
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000123bool GrConfigConversionEffect::onIsEqual(const GrEffect& s) const {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000124 const GrConfigConversionEffect& other = CastEffect<GrConfigConversionEffect>(s);
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000125 return this->texture(0) == s.texture(0) &&
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000126 other.fSwapRedAndBlue == fSwapRedAndBlue &&
127 other.fPMConversion == fPMConversion;
128}
129
130void GrConfigConversionEffect::getConstantColorComponents(GrColor* color,
131 uint32_t* validFlags) const {
132 this->updateConstantColorComponentsForModulation(color, validFlags);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000133}
134
135///////////////////////////////////////////////////////////////////////////////
136
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000137GR_DEFINE_EFFECT_TEST(GrConfigConversionEffect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000138
bsalomon@google.com73a96942013-02-13 16:31:19 +0000139GrEffectRef* GrConfigConversionEffect::TestCreate(SkMWCRandom* random,
sugoi@google.come0e385c2013-03-11 18:50:03 +0000140 GrContext*,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000141 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 }
bsalomon@google.com6340a412013-01-22 19:55:59 +0000149 AutoEffectUnref effect(SkNEW_ARGS(GrConfigConversionEffect,
150 (textures[GrEffectUnitTest::kSkiaPMTextureIdx],
151 swapRB,
152 pmConv,
153 GrEffectUnitTest::TestMatrix(random))));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000154 return CreateEffectRef(effect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000155}
156
157///////////////////////////////////////////////////////////////////////////////
158void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context,
159 PMConversion* pmToUPMRule,
160 PMConversion* upmToPMRule) {
161 *pmToUPMRule = kNone_PMConversion;
162 *upmToPMRule = kNone_PMConversion;
163 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
164 uint32_t* srcData = data.get();
165 uint32_t* firstRead = data.get() + 256 * 256;
166 uint32_t* secondRead = data.get() + 2 * 256 * 256;
167
168 // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate
169 // values in row y. We set r,g, and b to the same value since they are handled identically.
170 for (int y = 0; y < 256; ++y) {
171 for (int x = 0; x < 256; ++x) {
172 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
173 color[3] = y;
174 color[2] = GrMin(x, y);
175 color[1] = GrMin(x, y);
176 color[0] = GrMin(x, y);
177 }
178 }
179
180 GrTextureDesc desc;
181 desc.fFlags = kRenderTarget_GrTextureFlagBit |
182 kNoStencil_GrTextureFlagBit;
183 desc.fWidth = 256;
184 desc.fHeight = 256;
185 desc.fConfig = kRGBA_8888_GrPixelConfig;
186
187 SkAutoTUnref<GrTexture> readTex(context->createUncachedTexture(desc, NULL, 0));
188 if (!readTex.get()) {
189 return;
190 }
191 SkAutoTUnref<GrTexture> tempTex(context->createUncachedTexture(desc, NULL, 0));
192 if (!tempTex.get()) {
193 return;
194 }
195 desc.fFlags = kNone_GrTextureFlags;
196 SkAutoTUnref<GrTexture> dataTex(context->createUncachedTexture(desc, data, 0));
197 if (!dataTex.get()) {
198 return;
199 }
200
201 static const PMConversion kConversionRules[][2] = {
202 {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion},
203 {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion},
204 };
205
206 GrContext::AutoWideOpenIdentityDraw awoid(context, NULL);
207
208 bool failed = true;
209
210 for (size_t i = 0; i < GR_ARRAY_COUNT(kConversionRules) && failed; ++i) {
211 *pmToUPMRule = kConversionRules[i][0];
212 *upmToPMRule = kConversionRules[i][1];
213
bsalomon@google.com81712882012-11-01 17:12:34 +0000214 static const GrRect kDstRect = GrRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256));
215 static const GrRect kSrcRect = GrRect::MakeWH(SK_Scalar1, SK_Scalar1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000216 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
217 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
218 // We then verify that two reads produced the same values.
219
220 GrPaint paint;
bsalomon@google.com6340a412013-01-22 19:55:59 +0000221 AutoEffectUnref pmToUPM1(SkNEW_ARGS(GrConfigConversionEffect, (dataTex,
222 false,
223 *pmToUPMRule,
224 SkMatrix::I())));
225 AutoEffectUnref upmToPM(SkNEW_ARGS(GrConfigConversionEffect, (readTex,
226 false,
227 *upmToPMRule,
228 SkMatrix::I())));
229 AutoEffectUnref pmToUPM2(SkNEW_ARGS(GrConfigConversionEffect, (tempTex,
230 false,
231 *pmToUPMRule,
232 SkMatrix::I())));
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000233
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000234 SkAutoTUnref<GrEffectRef> pmToUPMEffect1(CreateEffectRef(pmToUPM1));
235 SkAutoTUnref<GrEffectRef> upmToPMEffect(CreateEffectRef(upmToPM));
236 SkAutoTUnref<GrEffectRef> pmToUPMEffect2(CreateEffectRef(pmToUPM2));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000237
238 context->setRenderTarget(readTex->asRenderTarget());
bsalomon@google.com08283af2012-10-26 13:01:20 +0000239 paint.colorStage(0)->setEffect(pmToUPMEffect1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000240 context->drawRectToRect(paint, kDstRect, kSrcRect);
241
242 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
243
244 context->setRenderTarget(tempTex->asRenderTarget());
bsalomon@google.com08283af2012-10-26 13:01:20 +0000245 paint.colorStage(0)->setEffect(upmToPMEffect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000246 context->drawRectToRect(paint, kDstRect, kSrcRect);
247 context->setRenderTarget(readTex->asRenderTarget());
bsalomon@google.com08283af2012-10-26 13:01:20 +0000248 paint.colorStage(0)->setEffect(pmToUPMEffect2);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000249 context->drawRectToRect(paint, kDstRect, kSrcRect);
250
251 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
252
253 failed = false;
254 for (int y = 0; y < 256 && !failed; ++y) {
255 for (int x = 0; x <= y; ++x) {
256 if (firstRead[256 * y + x] != secondRead[256 * y + x]) {
257 failed = true;
258 break;
259 }
260 }
261 }
262 }
263 if (failed) {
264 *pmToUPMRule = kNone_PMConversion;
265 *upmToPMRule = kNone_PMConversion;
266 }
267}
268
bsalomon@google.comadc65362013-01-28 14:26:09 +0000269const GrEffectRef* GrConfigConversionEffect::Create(GrTexture* texture,
270 bool swapRedAndBlue,
271 PMConversion pmConversion,
272 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000273 if (!swapRedAndBlue && kNone_PMConversion == pmConversion) {
bsalomon@google.comadc65362013-01-28 14:26:09 +0000274 // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000275 // then we may pollute our texture cache with redundant shaders. So in the case that no
bsalomon@google.comadc65362013-01-28 14:26:09 +0000276 // conversions were requested we instead return a GrSimpleTextureEffect.
277 return GrSimpleTextureEffect::Create(texture, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000278 } else {
279 if (kRGBA_8888_GrPixelConfig != texture->config() &&
280 kBGRA_8888_GrPixelConfig != texture->config() &&
281 kNone_PMConversion != pmConversion) {
282 // The PM conversions assume colors are 0..255
bsalomon@google.comadc65362013-01-28 14:26:09 +0000283 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000284 }
bsalomon@google.com6340a412013-01-22 19:55:59 +0000285 AutoEffectUnref effect(SkNEW_ARGS(GrConfigConversionEffect, (texture,
286 swapRedAndBlue,
287 pmConversion,
288 matrix)));
bsalomon@google.comadc65362013-01-28 14:26:09 +0000289 return CreateEffectRef(effect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000290 }
291}