blob: 29ba405cf2d26a601e85c87d7ac2e79bb185f938 [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.com2eaaefd2012-10-29 19:51:22 +00009#include "GrTBackendEffectFactory.h"
bsalomon@google.comd698f772012-10-25 13:22:00 +000010#include "gl/GrGLEffect.h"
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000011
bsalomon@google.com22a800a2012-10-26 19:16:46 +000012class GrGLConfigConversionEffect : public GrGLEffect {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000013public:
bsalomon@google.com396e61f2012-10-25 19:00:29 +000014 GrGLConfigConversionEffect(const GrBackendEffectFactory& factory,
bsalomon@google.coma469c282012-10-24 18:28:34 +000015 const GrEffect& s) : INHERITED (factory) {
bsalomon@google.com021fc732012-10-25 12:47:42 +000016 const GrConfigConversionEffect& effect = static_cast<const GrConfigConversionEffect&>(s);
17 fSwapRedAndBlue = effect.swapsRedAndBlue();
18 fPMConversion = effect.pmConversion();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000019 }
20
bsalomon@google.com22a800a2012-10-26 19:16:46 +000021 virtual void emitCode(GrGLShaderBuilder* builder,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000022 const GrEffectStage&,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000023 EffectKey,
24 const char* vertexCoords,
25 const char* outputColor,
26 const char* inputColor,
27 const TextureSamplerArray& samplers) SK_OVERRIDE {
bsalomon@google.com868a8e72012-08-30 19:11:34 +000028 builder->fFSCode.appendf("\t\t%s = ", outputColor);
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000029 builder->appendTextureLookup(&builder->fFSCode, samplers[0]);
bsalomon@google.com2d8edaf2012-09-07 14:47:31 +000030 builder->fFSCode.append(";\n");
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000031 if (GrConfigConversionEffect::kNone_PMConversion == fPMConversion) {
32 GrAssert(fSwapRedAndBlue);
bsalomon@google.com868a8e72012-08-30 19:11:34 +000033 builder->fFSCode.appendf("\t%s = %s.bgra;\n", outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000034 } else {
35 const char* swiz = fSwapRedAndBlue ? "bgr" : "rgb";
36 switch (fPMConversion) {
37 case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion:
38 builder->fFSCode.appendf(
bsalomon@google.com868a8e72012-08-30 19:11:34 +000039 "\t\t%s = vec4(ceil(%s.%s * %s.a * 255.0) / 255.0, %s.a);\n",
40 outputColor, outputColor, swiz, outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000041 break;
42 case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion:
43 builder->fFSCode.appendf(
bsalomon@google.com868a8e72012-08-30 19:11:34 +000044 "\t\t%s = vec4(floor(%s.%s * %s.a * 255.0) / 255.0, %s.a);\n",
45 outputColor, outputColor, swiz, outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000046 break;
47 case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion:
bsalomon@google.com868a8e72012-08-30 19:11:34 +000048 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",
49 outputColor, outputColor, outputColor, swiz, outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000050 break;
51 case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion:
bsalomon@google.com868a8e72012-08-30 19:11:34 +000052 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",
53 outputColor, outputColor, outputColor, swiz, outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000054 break;
robertphillips@google.com2af1b182012-08-28 11:23:09 +000055 default:
56 GrCrash("Unknown conversion op.");
57 break;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000058 }
59 }
bsalomon@google.com868a8e72012-08-30 19:11:34 +000060 GrGLSLMulVarBy4f(&builder->fFSCode, 2, outputColor, inputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000061 }
62
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000063 static inline EffectKey GenKey(const GrEffectStage& s, const GrGLCaps&) {
64 const GrConfigConversionEffect& effect =
65 static_cast<const GrConfigConversionEffect&>(*s.getEffect());
bsalomon@google.com021fc732012-10-25 12:47:42 +000066 return static_cast<int>(effect.swapsRedAndBlue()) | (effect.pmConversion() << 1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000067 }
68
69private:
70 bool fSwapRedAndBlue;
71 GrConfigConversionEffect::PMConversion fPMConversion;
72
bsalomon@google.com22a800a2012-10-26 19:16:46 +000073 typedef GrGLEffect INHERITED;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000074
75};
76
77///////////////////////////////////////////////////////////////////////////////
78
79GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture,
80 bool swapRedAndBlue,
81 PMConversion pmConversion)
82 : GrSingleTextureEffect(texture)
83 , fSwapRedAndBlue(swapRedAndBlue)
84 , fPMConversion(pmConversion) {
85 GrAssert(kRGBA_8888_GrPixelConfig == texture->config() ||
86 kBGRA_8888_GrPixelConfig == texture->config());
87 // Why did we pollute our texture cache instead of using a GrSingleTextureEffect?
88 GrAssert(swapRedAndBlue || kNone_PMConversion != pmConversion);
89}
90
bsalomon@google.com396e61f2012-10-25 19:00:29 +000091const GrBackendEffectFactory& GrConfigConversionEffect::getFactory() const {
92 return GrTBackendEffectFactory<GrConfigConversionEffect>::getInstance();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000093}
94
bsalomon@google.coma469c282012-10-24 18:28:34 +000095bool GrConfigConversionEffect::isEqual(const GrEffect& s) const {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000096 const GrConfigConversionEffect& other = static_cast<const GrConfigConversionEffect&>(s);
97 return other.fSwapRedAndBlue == fSwapRedAndBlue && other.fPMConversion == fPMConversion;
98}
99
100///////////////////////////////////////////////////////////////////////////////
101
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000102GR_DEFINE_EFFECT_TEST(GrConfigConversionEffect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000103
bsalomon@google.coma469c282012-10-24 18:28:34 +0000104GrEffect* GrConfigConversionEffect::TestCreate(SkRandom* random,
105 GrContext* context,
106 GrTexture* textures[]) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000107 PMConversion pmConv = static_cast<PMConversion>(random->nextULessThan(kPMConversionCnt));
108 bool swapRB;
109 if (kNone_PMConversion == pmConv) {
110 swapRB = true;
111 } else {
112 swapRB = random->nextBool();
113 }
114 return SkNEW_ARGS(GrConfigConversionEffect,
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000115 (textures[GrEffectUnitTest::kSkiaPMTextureIdx], swapRB, pmConv));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000116}
117
118///////////////////////////////////////////////////////////////////////////////
119void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context,
120 PMConversion* pmToUPMRule,
121 PMConversion* upmToPMRule) {
122 *pmToUPMRule = kNone_PMConversion;
123 *upmToPMRule = kNone_PMConversion;
124 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
125 uint32_t* srcData = data.get();
126 uint32_t* firstRead = data.get() + 256 * 256;
127 uint32_t* secondRead = data.get() + 2 * 256 * 256;
128
129 // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate
130 // values in row y. We set r,g, and b to the same value since they are handled identically.
131 for (int y = 0; y < 256; ++y) {
132 for (int x = 0; x < 256; ++x) {
133 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
134 color[3] = y;
135 color[2] = GrMin(x, y);
136 color[1] = GrMin(x, y);
137 color[0] = GrMin(x, y);
138 }
139 }
140
141 GrTextureDesc desc;
142 desc.fFlags = kRenderTarget_GrTextureFlagBit |
143 kNoStencil_GrTextureFlagBit;
144 desc.fWidth = 256;
145 desc.fHeight = 256;
146 desc.fConfig = kRGBA_8888_GrPixelConfig;
147
148 SkAutoTUnref<GrTexture> readTex(context->createUncachedTexture(desc, NULL, 0));
149 if (!readTex.get()) {
150 return;
151 }
152 SkAutoTUnref<GrTexture> tempTex(context->createUncachedTexture(desc, NULL, 0));
153 if (!tempTex.get()) {
154 return;
155 }
156 desc.fFlags = kNone_GrTextureFlags;
157 SkAutoTUnref<GrTexture> dataTex(context->createUncachedTexture(desc, data, 0));
158 if (!dataTex.get()) {
159 return;
160 }
161
162 static const PMConversion kConversionRules[][2] = {
163 {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion},
164 {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion},
165 };
166
167 GrContext::AutoWideOpenIdentityDraw awoid(context, NULL);
168
169 bool failed = true;
170
171 for (size_t i = 0; i < GR_ARRAY_COUNT(kConversionRules) && failed; ++i) {
172 *pmToUPMRule = kConversionRules[i][0];
173 *upmToPMRule = kConversionRules[i][1];
174
bsalomon@google.com81712882012-11-01 17:12:34 +0000175 static const GrRect kDstRect = GrRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256));
176 static const GrRect kSrcRect = GrRect::MakeWH(SK_Scalar1, SK_Scalar1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000177 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
178 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
179 // We then verify that two reads produced the same values.
180
181 GrPaint paint;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000182
bsalomon@google.com021fc732012-10-25 12:47:42 +0000183 SkAutoTUnref<GrEffect> pmToUPMEffect1(SkNEW_ARGS(GrConfigConversionEffect,
bsalomon@google.coma469c282012-10-24 18:28:34 +0000184 (dataTex, false, *pmToUPMRule)));
bsalomon@google.com021fc732012-10-25 12:47:42 +0000185 SkAutoTUnref<GrEffect> upmToPMEffect(SkNEW_ARGS(GrConfigConversionEffect,
bsalomon@google.coma469c282012-10-24 18:28:34 +0000186 (readTex, false, *upmToPMRule)));
bsalomon@google.com021fc732012-10-25 12:47:42 +0000187 SkAutoTUnref<GrEffect> pmToUPMEffect2(SkNEW_ARGS(GrConfigConversionEffect,
bsalomon@google.coma469c282012-10-24 18:28:34 +0000188 (tempTex, false, *pmToUPMRule)));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000189
190 context->setRenderTarget(readTex->asRenderTarget());
bsalomon@google.com08283af2012-10-26 13:01:20 +0000191 paint.colorStage(0)->setEffect(pmToUPMEffect1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000192 context->drawRectToRect(paint, kDstRect, kSrcRect);
193
194 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
195
196 context->setRenderTarget(tempTex->asRenderTarget());
bsalomon@google.com08283af2012-10-26 13:01:20 +0000197 paint.colorStage(0)->setEffect(upmToPMEffect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000198 context->drawRectToRect(paint, kDstRect, kSrcRect);
199 context->setRenderTarget(readTex->asRenderTarget());
bsalomon@google.com08283af2012-10-26 13:01:20 +0000200 paint.colorStage(0)->setEffect(pmToUPMEffect2);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000201 context->drawRectToRect(paint, kDstRect, kSrcRect);
202
203 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
204
205 failed = false;
206 for (int y = 0; y < 256 && !failed; ++y) {
207 for (int x = 0; x <= y; ++x) {
208 if (firstRead[256 * y + x] != secondRead[256 * y + x]) {
209 failed = true;
210 break;
211 }
212 }
213 }
214 }
215 if (failed) {
216 *pmToUPMRule = kNone_PMConversion;
217 *upmToPMRule = kNone_PMConversion;
218 }
219}
220
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000221bool GrConfigConversionEffect::InstallEffect(GrTexture* texture,
222 bool swapRedAndBlue,
223 PMConversion pmConversion,
224 const GrMatrix& matrix,
225 GrEffectStage* stage) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000226 if (!swapRedAndBlue && kNone_PMConversion == pmConversion) {
227 // If we returned a GrConfigConversionEffect that was equivalent to a GrSingleTextureEffect
228 // then we may pollute our texture cache with redundant shaders. So in the case that no
229 // conversions were requested we instead return a GrSingleTextureEffect.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000230 stage->setEffect(SkNEW_ARGS(GrSingleTextureEffect, (texture, matrix)), matrix)->unref();
231 return true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000232 } else {
233 if (kRGBA_8888_GrPixelConfig != texture->config() &&
234 kBGRA_8888_GrPixelConfig != texture->config() &&
235 kNone_PMConversion != pmConversion) {
236 // The PM conversions assume colors are 0..255
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000237 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000238 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000239 stage->setEffect(SkNEW_ARGS(GrConfigConversionEffect, (texture,
240 swapRedAndBlue,
241 pmConversion)), matrix)->unref();
242 return true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000243 }
244}