blob: 1dd51c888b606f27b083269184f81d674390c3f2 [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.com868a8e72012-08-30 19:11:34 +000034 builder->fFSCode.appendf("\t\t%s = ", outputColor);
bsalomon@google.comb1456d72012-11-02 18:23:45 +000035 builder->appendTextureLookup(&builder->fFSCode, samplers[0], coords, coordsType);
bsalomon@google.com2d8edaf2012-09-07 14:47:31 +000036 builder->fFSCode.append(";\n");
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000037 if (GrConfigConversionEffect::kNone_PMConversion == fPMConversion) {
38 GrAssert(fSwapRedAndBlue);
bsalomon@google.com868a8e72012-08-30 19:11:34 +000039 builder->fFSCode.appendf("\t%s = %s.bgra;\n", outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000040 } else {
41 const char* swiz = fSwapRedAndBlue ? "bgr" : "rgb";
42 switch (fPMConversion) {
43 case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion:
44 builder->fFSCode.appendf(
bsalomon@google.com868a8e72012-08-30 19:11:34 +000045 "\t\t%s = vec4(ceil(%s.%s * %s.a * 255.0) / 255.0, %s.a);\n",
46 outputColor, outputColor, swiz, outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000047 break;
48 case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion:
49 builder->fFSCode.appendf(
bsalomon@google.com868a8e72012-08-30 19:11:34 +000050 "\t\t%s = vec4(floor(%s.%s * %s.a * 255.0) / 255.0, %s.a);\n",
51 outputColor, outputColor, swiz, outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000052 break;
53 case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion:
bsalomon@google.com868a8e72012-08-30 19:11:34 +000054 builder->fFSCode.appendf("\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",
55 outputColor, outputColor, outputColor, swiz, outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000056 break;
57 case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion:
bsalomon@google.com868a8e72012-08-30 19:11:34 +000058 builder->fFSCode.appendf("\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",
59 outputColor, outputColor, outputColor, swiz, outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000060 break;
robertphillips@google.com2af1b182012-08-28 11:23:09 +000061 default:
62 GrCrash("Unknown conversion op.");
63 break;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000064 }
65 }
bsalomon@google.com868a8e72012-08-30 19:11:34 +000066 GrGLSLMulVarBy4f(&builder->fFSCode, 2, outputColor, inputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000067 }
68
bsalomon@google.comb1456d72012-11-02 18:23:45 +000069 void setData(const GrGLUniformManager& uman, const GrEffectStage& stage) {
70 const GrConfigConversionEffect& effect =
bsalomon@google.com6340a412013-01-22 19:55:59 +000071 GetEffectFromStage<GrConfigConversionEffect>(stage);
bsalomon@google.comb1456d72012-11-02 18:23:45 +000072 fEffectMatrix.setData(uman,
73 effect.getMatrix(),
74 stage.getCoordChangeMatrix(),
75 effect.texture(0));
76 }
77
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000078 static inline EffectKey GenKey(const GrEffectStage& s, const GrGLCaps&) {
bsalomon@google.com6340a412013-01-22 19:55:59 +000079 const GrConfigConversionEffect& effect = GetEffectFromStage<GrConfigConversionEffect>(s);
bsalomon@google.comb1456d72012-11-02 18:23:45 +000080 EffectKey key = static_cast<EffectKey>(effect.swapsRedAndBlue()) |
81 (effect.pmConversion() << 1);
82 key <<= GrGLEffectMatrix::kKeyBits;
83 EffectKey matrixKey = GrGLEffectMatrix::GenKey(effect.getMatrix(),
84 s.getCoordChangeMatrix(),
85 effect.texture(0));
86 GrAssert(!(matrixKey & key));
87 return matrixKey | key;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000088 }
89
90private:
91 bool fSwapRedAndBlue;
92 GrConfigConversionEffect::PMConversion fPMConversion;
bsalomon@google.comb1456d72012-11-02 18:23:45 +000093 GrGLEffectMatrix fEffectMatrix;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000094
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) {
108 GrAssert(kRGBA_8888_GrPixelConfig == texture->config() ||
109 kBGRA_8888_GrPixelConfig == texture->config());
110 // Why did we pollute our texture cache instead of using a GrSingleTextureEffect?
111 GrAssert(swapRedAndBlue || kNone_PMConversion != pmConversion);
112}
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
bsalomon@google.com73a96942013-02-13 16:31:19 +0000134GrEffectRef* GrConfigConversionEffect::TestCreate(SkMWCRandom* random,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000135 GrContext* context,
136 GrTexture* textures[]) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000137 PMConversion pmConv = static_cast<PMConversion>(random->nextULessThan(kPMConversionCnt));
138 bool swapRB;
139 if (kNone_PMConversion == pmConv) {
140 swapRB = true;
141 } else {
142 swapRB = random->nextBool();
143 }
bsalomon@google.com6340a412013-01-22 19:55:59 +0000144 AutoEffectUnref effect(SkNEW_ARGS(GrConfigConversionEffect,
145 (textures[GrEffectUnitTest::kSkiaPMTextureIdx],
146 swapRB,
147 pmConv,
148 GrEffectUnitTest::TestMatrix(random))));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000149 return CreateEffectRef(effect);
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;
169 color[2] = GrMin(x, y);
170 color[1] = GrMin(x, y);
171 color[0] = GrMin(x, y);
172 }
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
205 for (size_t i = 0; i < GR_ARRAY_COUNT(kConversionRules) && failed; ++i) {
206 *pmToUPMRule = kConversionRules[i][0];
207 *upmToPMRule = kConversionRules[i][1];
208
bsalomon@google.com81712882012-11-01 17:12:34 +0000209 static const GrRect kDstRect = GrRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256));
210 static const GrRect kSrcRect = GrRect::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
215 GrPaint paint;
bsalomon@google.com6340a412013-01-22 19:55:59 +0000216 AutoEffectUnref pmToUPM1(SkNEW_ARGS(GrConfigConversionEffect, (dataTex,
217 false,
218 *pmToUPMRule,
219 SkMatrix::I())));
220 AutoEffectUnref upmToPM(SkNEW_ARGS(GrConfigConversionEffect, (readTex,
221 false,
222 *upmToPMRule,
223 SkMatrix::I())));
224 AutoEffectUnref pmToUPM2(SkNEW_ARGS(GrConfigConversionEffect, (tempTex,
225 false,
226 *pmToUPMRule,
227 SkMatrix::I())));
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000228
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000229 SkAutoTUnref<GrEffectRef> pmToUPMEffect1(CreateEffectRef(pmToUPM1));
230 SkAutoTUnref<GrEffectRef> upmToPMEffect(CreateEffectRef(upmToPM));
231 SkAutoTUnref<GrEffectRef> pmToUPMEffect2(CreateEffectRef(pmToUPM2));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000232
233 context->setRenderTarget(readTex->asRenderTarget());
bsalomon@google.com08283af2012-10-26 13:01:20 +0000234 paint.colorStage(0)->setEffect(pmToUPMEffect1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000235 context->drawRectToRect(paint, kDstRect, kSrcRect);
236
237 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
238
239 context->setRenderTarget(tempTex->asRenderTarget());
bsalomon@google.com08283af2012-10-26 13:01:20 +0000240 paint.colorStage(0)->setEffect(upmToPMEffect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000241 context->drawRectToRect(paint, kDstRect, kSrcRect);
242 context->setRenderTarget(readTex->asRenderTarget());
bsalomon@google.com08283af2012-10-26 13:01:20 +0000243 paint.colorStage(0)->setEffect(pmToUPMEffect2);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000244 context->drawRectToRect(paint, kDstRect, kSrcRect);
245
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
bsalomon@google.comadc65362013-01-28 14:26:09 +0000264const GrEffectRef* GrConfigConversionEffect::Create(GrTexture* texture,
265 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 }
bsalomon@google.com6340a412013-01-22 19:55:59 +0000280 AutoEffectUnref effect(SkNEW_ARGS(GrConfigConversionEffect, (texture,
281 swapRedAndBlue,
282 pmConversion,
283 matrix)));
bsalomon@google.comadc65362013-01-28 14:26:09 +0000284 return CreateEffectRef(effect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000285 }
286}