blob: 309dcd88760d9c7d5c6fb04f54293f434381d5fe [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.coma469c282012-10-24 18:28:34 +000019 const GrEffect& s) : INHERITED (factory) {
bsalomon@google.com021fc732012-10-25 12:47:42 +000020 const GrConfigConversionEffect& effect = static_cast<const GrConfigConversionEffect&>(s);
21 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 =
71 static_cast<const GrConfigConversionEffect&>(*stage.getEffect());
72 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&) {
79 const GrConfigConversionEffect& effect =
80 static_cast<const GrConfigConversionEffect&>(*s.getEffect());
bsalomon@google.comb1456d72012-11-02 18:23:45 +000081 EffectKey key = static_cast<EffectKey>(effect.swapsRedAndBlue()) |
82 (effect.pmConversion() << 1);
83 key <<= GrGLEffectMatrix::kKeyBits;
84 EffectKey matrixKey = GrGLEffectMatrix::GenKey(effect.getMatrix(),
85 s.getCoordChangeMatrix(),
86 effect.texture(0));
87 GrAssert(!(matrixKey & key));
88 return matrixKey | key;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000089 }
90
91private:
92 bool fSwapRedAndBlue;
93 GrConfigConversionEffect::PMConversion fPMConversion;
bsalomon@google.comb1456d72012-11-02 18:23:45 +000094 GrGLEffectMatrix fEffectMatrix;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000095
bsalomon@google.com22a800a2012-10-26 19:16:46 +000096 typedef GrGLEffect INHERITED;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000097
98};
99
100///////////////////////////////////////////////////////////////////////////////
101
102GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture,
103 bool swapRedAndBlue,
bsalomon@google.comb1456d72012-11-02 18:23:45 +0000104 PMConversion pmConversion,
105 const SkMatrix& matrix)
106 : GrSingleTextureEffect(texture, matrix)
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000107 , fSwapRedAndBlue(swapRedAndBlue)
108 , fPMConversion(pmConversion) {
109 GrAssert(kRGBA_8888_GrPixelConfig == texture->config() ||
110 kBGRA_8888_GrPixelConfig == texture->config());
111 // Why did we pollute our texture cache instead of using a GrSingleTextureEffect?
112 GrAssert(swapRedAndBlue || kNone_PMConversion != pmConversion);
113}
114
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000115const GrBackendEffectFactory& GrConfigConversionEffect::getFactory() const {
116 return GrTBackendEffectFactory<GrConfigConversionEffect>::getInstance();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000117}
118
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000119bool GrConfigConversionEffect::onIsEqual(const GrEffect& s) const {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000120 const GrConfigConversionEffect& other = static_cast<const GrConfigConversionEffect&>(s);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000121 return this->texture(0) == s.texture(0) &&
122 other.fSwapRedAndBlue == fSwapRedAndBlue &&
123 other.fPMConversion == fPMConversion;
124}
125
126void GrConfigConversionEffect::getConstantColorComponents(GrColor* color,
127 uint32_t* validFlags) const {
128 this->updateConstantColorComponentsForModulation(color, validFlags);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000129}
130
131///////////////////////////////////////////////////////////////////////////////
132
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000133GR_DEFINE_EFFECT_TEST(GrConfigConversionEffect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000134
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000135GrEffectRef* GrConfigConversionEffect::TestCreate(SkRandom* random,
136 GrContext* context,
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 }
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000145 SkAutoTUnref<GrEffect> effect(SkNEW_ARGS(GrConfigConversionEffect,
146 (textures[GrEffectUnitTest::kSkiaPMTextureIdx],
147 swapRB,
148 pmConv,
149 GrEffectUnitTest::TestMatrix(random))));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000150 return CreateEffectRef(effect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000151}
152
153///////////////////////////////////////////////////////////////////////////////
154void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context,
155 PMConversion* pmToUPMRule,
156 PMConversion* upmToPMRule) {
157 *pmToUPMRule = kNone_PMConversion;
158 *upmToPMRule = kNone_PMConversion;
159 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
160 uint32_t* srcData = data.get();
161 uint32_t* firstRead = data.get() + 256 * 256;
162 uint32_t* secondRead = data.get() + 2 * 256 * 256;
163
164 // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate
165 // values in row y. We set r,g, and b to the same value since they are handled identically.
166 for (int y = 0; y < 256; ++y) {
167 for (int x = 0; x < 256; ++x) {
168 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
169 color[3] = y;
170 color[2] = GrMin(x, y);
171 color[1] = GrMin(x, y);
172 color[0] = GrMin(x, y);
173 }
174 }
175
176 GrTextureDesc desc;
177 desc.fFlags = kRenderTarget_GrTextureFlagBit |
178 kNoStencil_GrTextureFlagBit;
179 desc.fWidth = 256;
180 desc.fHeight = 256;
181 desc.fConfig = kRGBA_8888_GrPixelConfig;
182
183 SkAutoTUnref<GrTexture> readTex(context->createUncachedTexture(desc, NULL, 0));
184 if (!readTex.get()) {
185 return;
186 }
187 SkAutoTUnref<GrTexture> tempTex(context->createUncachedTexture(desc, NULL, 0));
188 if (!tempTex.get()) {
189 return;
190 }
191 desc.fFlags = kNone_GrTextureFlags;
192 SkAutoTUnref<GrTexture> dataTex(context->createUncachedTexture(desc, data, 0));
193 if (!dataTex.get()) {
194 return;
195 }
196
197 static const PMConversion kConversionRules[][2] = {
198 {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion},
199 {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion},
200 };
201
202 GrContext::AutoWideOpenIdentityDraw awoid(context, NULL);
203
204 bool failed = true;
205
206 for (size_t i = 0; i < GR_ARRAY_COUNT(kConversionRules) && failed; ++i) {
207 *pmToUPMRule = kConversionRules[i][0];
208 *upmToPMRule = kConversionRules[i][1];
209
bsalomon@google.com81712882012-11-01 17:12:34 +0000210 static const GrRect kDstRect = GrRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256));
211 static const GrRect kSrcRect = GrRect::MakeWH(SK_Scalar1, SK_Scalar1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000212 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
213 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
214 // We then verify that two reads produced the same values.
215
216 GrPaint paint;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000217 SkAutoTUnref<GrEffect> pmToUPM1(SkNEW_ARGS(GrConfigConversionEffect, (dataTex,
218 false,
219 *pmToUPMRule,
220 SkMatrix::I())));
221 SkAutoTUnref<GrEffect> upmToPM(SkNEW_ARGS(GrConfigConversionEffect, (readTex,
222 false,
223 *upmToPMRule,
224 SkMatrix::I())));
225 SkAutoTUnref<GrEffect> pmToUPM2(SkNEW_ARGS(GrConfigConversionEffect, (tempTex,
226 false,
227 *pmToUPMRule,
228 SkMatrix::I())));
229
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000230 SkAutoTUnref<GrEffectRef> pmToUPMEffect1(CreateEffectRef(pmToUPM1));
231 SkAutoTUnref<GrEffectRef> upmToPMEffect(CreateEffectRef(upmToPM));
232 SkAutoTUnref<GrEffectRef> pmToUPMEffect2(CreateEffectRef(pmToUPM2));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000233
234 context->setRenderTarget(readTex->asRenderTarget());
bsalomon@google.com08283af2012-10-26 13:01:20 +0000235 paint.colorStage(0)->setEffect(pmToUPMEffect1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000236 context->drawRectToRect(paint, kDstRect, kSrcRect);
237
238 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
239
240 context->setRenderTarget(tempTex->asRenderTarget());
bsalomon@google.com08283af2012-10-26 13:01:20 +0000241 paint.colorStage(0)->setEffect(upmToPMEffect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000242 context->drawRectToRect(paint, kDstRect, kSrcRect);
243 context->setRenderTarget(readTex->asRenderTarget());
bsalomon@google.com08283af2012-10-26 13:01:20 +0000244 paint.colorStage(0)->setEffect(pmToUPMEffect2);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000245 context->drawRectToRect(paint, kDstRect, kSrcRect);
246
247 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
248
249 failed = false;
250 for (int y = 0; y < 256 && !failed; ++y) {
251 for (int x = 0; x <= y; ++x) {
252 if (firstRead[256 * y + x] != secondRead[256 * y + x]) {
253 failed = true;
254 break;
255 }
256 }
257 }
258 }
259 if (failed) {
260 *pmToUPMRule = kNone_PMConversion;
261 *upmToPMRule = kNone_PMConversion;
262 }
263}
264
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000265bool GrConfigConversionEffect::InstallEffect(GrTexture* texture,
266 bool swapRedAndBlue,
267 PMConversion pmConversion,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000268 const SkMatrix& matrix,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000269 GrEffectStage* stage) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000270 if (!swapRedAndBlue && kNone_PMConversion == pmConversion) {
271 // If we returned a GrConfigConversionEffect that was equivalent to a GrSingleTextureEffect
272 // then we may pollute our texture cache with redundant shaders. So in the case that no
273 // conversions were requested we instead return a GrSingleTextureEffect.
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000274 stage->setEffect(GrSimpleTextureEffect::Create(texture, matrix))->unref();
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000275 return true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000276 } else {
277 if (kRGBA_8888_GrPixelConfig != texture->config() &&
278 kBGRA_8888_GrPixelConfig != texture->config() &&
279 kNone_PMConversion != pmConversion) {
280 // The PM conversions assume colors are 0..255
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000281 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000282 }
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000283 SkAutoTUnref<GrEffect> effect(SkNEW_ARGS(GrConfigConversionEffect, (texture,
284 swapRedAndBlue,
285 pmConversion,
286 matrix)));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000287 stage->setEffect(CreateEffectRef(effect))->unref();
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000288 return true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000289 }
290}