blob: ff18ef266dad55e2eca2a55587a6f9dde7648164 [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 "SkMatrix.h"
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000014
bsalomon@google.com22a800a2012-10-26 19:16:46 +000015class GrGLConfigConversionEffect : public GrGLEffect {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000016public:
bsalomon@google.com396e61f2012-10-25 19:00:29 +000017 GrGLConfigConversionEffect(const GrBackendEffectFactory& factory,
bsalomon@google.comc7818882013-03-20 19:19:53 +000018 const GrDrawEffect& drawEffect)
bsalomon@google.com77af6802013-10-02 13:04:56 +000019 : INHERITED (factory) {
bsalomon@google.comc7818882013-03-20 19:19:53 +000020 const GrConfigConversionEffect& effect = drawEffect.castEffect<GrConfigConversionEffect>();
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.comc7818882013-03-20 19:19:53 +000026 const GrDrawEffect&,
bsalomon@google.comb1456d72012-11-02 18:23:45 +000027 EffectKey key,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000028 const char* outputColor,
29 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000030 const TransformedCoordsArray& coords,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000031 const TextureSamplerArray& samplers) SK_OVERRIDE {
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000032 builder->fsCodeAppendf("\t\t%s = ", outputColor);
bsalomon@google.com77af6802013-10-02 13:04:56 +000033 builder->fsAppendTextureLookup(samplers[0], coords[0].c_str(), coords[0].type());
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000034 builder->fsCodeAppend(";\n");
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000035 if (GrConfigConversionEffect::kNone_PMConversion == fPMConversion) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000036 SkASSERT(fSwapRedAndBlue);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000037 builder->fsCodeAppendf("\t%s = %s.bgra;\n", outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000038 } else {
39 const char* swiz = fSwapRedAndBlue ? "bgr" : "rgb";
40 switch (fPMConversion) {
41 case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion:
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000042 builder->fsCodeAppendf(
bsalomon@google.com868a8e72012-08-30 19:11:34 +000043 "\t\t%s = vec4(ceil(%s.%s * %s.a * 255.0) / 255.0, %s.a);\n",
44 outputColor, outputColor, swiz, outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000045 break;
46 case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion:
commit-bot@chromium.orgb4e200e2013-06-19 11:41:02 +000047 // Add a compensation(0.001) here to avoid the side effect of the floor operation.
48 // In Intel GPUs, the integer value converted from floor(%s.r * 255.0) / 255.0
49 // is less than the integer value converted from %s.r by 1 when the %s.r is
50 // converted from the integer value 2^n, such as 1, 2, 4, 8, etc.
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000051 builder->fsCodeAppendf(
commit-bot@chromium.orgb4e200e2013-06-19 11:41:02 +000052 "\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 +000053 outputColor, outputColor, swiz, outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000054 break;
55 case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion:
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000056 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 +000057 outputColor, outputColor, outputColor, swiz, outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000058 break;
59 case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion:
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000060 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 +000061 outputColor, outputColor, outputColor, swiz, outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000062 break;
robertphillips@google.com2af1b182012-08-28 11:23:09 +000063 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +000064 SkFAIL("Unknown conversion op.");
robertphillips@google.com2af1b182012-08-28 11:23:09 +000065 break;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000066 }
67 }
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000068 SkString modulate;
69 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor);
70 builder->fsCodeAppend(modulate.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000071 }
72
bsalomon@google.comc7818882013-03-20 19:19:53 +000073 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
74 const GrConfigConversionEffect& conv = drawEffect.castEffect<GrConfigConversionEffect>();
bsalomon@google.com77af6802013-10-02 13:04:56 +000075 return static_cast<EffectKey>(conv.swapsRedAndBlue()) | (conv.pmConversion() << 1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000076 }
77
78private:
79 bool fSwapRedAndBlue;
80 GrConfigConversionEffect::PMConversion fPMConversion;
81
bsalomon@google.com22a800a2012-10-26 19:16:46 +000082 typedef GrGLEffect INHERITED;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000083
84};
85
86///////////////////////////////////////////////////////////////////////////////
87
88GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture,
89 bool swapRedAndBlue,
bsalomon@google.comb1456d72012-11-02 18:23:45 +000090 PMConversion pmConversion,
91 const SkMatrix& matrix)
92 : GrSingleTextureEffect(texture, matrix)
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000093 , fSwapRedAndBlue(swapRedAndBlue)
94 , fPMConversion(pmConversion) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000095 SkASSERT(kRGBA_8888_GrPixelConfig == texture->config() ||
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000096 kBGRA_8888_GrPixelConfig == texture->config());
97 // Why did we pollute our texture cache instead of using a GrSingleTextureEffect?
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000098 SkASSERT(swapRedAndBlue || kNone_PMConversion != pmConversion);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000099}
100
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000101const GrBackendEffectFactory& GrConfigConversionEffect::getFactory() const {
102 return GrTBackendEffectFactory<GrConfigConversionEffect>::getInstance();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000103}
104
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000105bool GrConfigConversionEffect::onIsEqual(const GrEffect& s) const {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000106 const GrConfigConversionEffect& other = CastEffect<GrConfigConversionEffect>(s);
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000107 return this->texture(0) == s.texture(0) &&
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000108 other.fSwapRedAndBlue == fSwapRedAndBlue &&
109 other.fPMConversion == fPMConversion;
110}
111
112void GrConfigConversionEffect::getConstantColorComponents(GrColor* color,
113 uint32_t* validFlags) const {
114 this->updateConstantColorComponentsForModulation(color, validFlags);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000115}
116
117///////////////////////////////////////////////////////////////////////////////
118
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000119GR_DEFINE_EFFECT_TEST(GrConfigConversionEffect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000120
bsalomon83d081a2014-07-08 09:56:10 -0700121GrEffect* GrConfigConversionEffect::TestCreate(SkRandom* random,
122 GrContext*,
123 const GrDrawTargetCaps&,
124 GrTexture* textures[]) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000125 PMConversion pmConv = static_cast<PMConversion>(random->nextULessThan(kPMConversionCnt));
126 bool swapRB;
127 if (kNone_PMConversion == pmConv) {
128 swapRB = true;
129 } else {
130 swapRB = random->nextBool();
131 }
bsalomon55fad7a2014-07-08 07:34:20 -0700132 return SkNEW_ARGS(GrConfigConversionEffect,
bsalomon@google.com6340a412013-01-22 19:55:59 +0000133 (textures[GrEffectUnitTest::kSkiaPMTextureIdx],
134 swapRB,
135 pmConv,
bsalomon55fad7a2014-07-08 07:34:20 -0700136 GrEffectUnitTest::TestMatrix(random)));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000137}
138
139///////////////////////////////////////////////////////////////////////////////
140void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context,
141 PMConversion* pmToUPMRule,
142 PMConversion* upmToPMRule) {
143 *pmToUPMRule = kNone_PMConversion;
144 *upmToPMRule = kNone_PMConversion;
145 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
146 uint32_t* srcData = data.get();
147 uint32_t* firstRead = data.get() + 256 * 256;
148 uint32_t* secondRead = data.get() + 2 * 256 * 256;
149
150 // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate
151 // values in row y. We set r,g, and b to the same value since they are handled identically.
152 for (int y = 0; y < 256; ++y) {
153 for (int x = 0; x < 256; ++x) {
154 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
155 color[3] = y;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000156 color[2] = SkTMin(x, y);
157 color[1] = SkTMin(x, y);
158 color[0] = SkTMin(x, y);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000159 }
160 }
161
162 GrTextureDesc desc;
163 desc.fFlags = kRenderTarget_GrTextureFlagBit |
164 kNoStencil_GrTextureFlagBit;
165 desc.fWidth = 256;
166 desc.fHeight = 256;
167 desc.fConfig = kRGBA_8888_GrPixelConfig;
168
169 SkAutoTUnref<GrTexture> readTex(context->createUncachedTexture(desc, NULL, 0));
170 if (!readTex.get()) {
171 return;
172 }
173 SkAutoTUnref<GrTexture> tempTex(context->createUncachedTexture(desc, NULL, 0));
174 if (!tempTex.get()) {
175 return;
176 }
177 desc.fFlags = kNone_GrTextureFlags;
178 SkAutoTUnref<GrTexture> dataTex(context->createUncachedTexture(desc, data, 0));
179 if (!dataTex.get()) {
180 return;
181 }
182
183 static const PMConversion kConversionRules[][2] = {
184 {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion},
185 {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion},
186 };
187
188 GrContext::AutoWideOpenIdentityDraw awoid(context, NULL);
189
190 bool failed = true;
191
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000192 for (size_t i = 0; i < SK_ARRAY_COUNT(kConversionRules) && failed; ++i) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000193 *pmToUPMRule = kConversionRules[i][0];
194 *upmToPMRule = kConversionRules[i][1];
195
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000196 static const SkRect kDstRect = SkRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256));
197 static const SkRect kSrcRect = SkRect::MakeWH(SK_Scalar1, SK_Scalar1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000198 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
199 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
200 // We then verify that two reads produced the same values.
201
bsalomon55fad7a2014-07-08 07:34:20 -0700202 SkAutoTUnref<GrEffect> pmToUPM1(SkNEW_ARGS(GrConfigConversionEffect, (dataTex,
203 false,
204 *pmToUPMRule,
205 SkMatrix::I())));
206 SkAutoTUnref<GrEffect> upmToPM(SkNEW_ARGS(GrConfigConversionEffect, (readTex,
207 false,
208 *upmToPMRule,
209 SkMatrix::I())));
210 SkAutoTUnref<GrEffect> pmToUPM2(SkNEW_ARGS(GrConfigConversionEffect, (tempTex,
211 false,
212 *pmToUPMRule,
213 SkMatrix::I())));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000214
215 context->setRenderTarget(readTex->asRenderTarget());
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000216 GrPaint paint1;
bsalomon55fad7a2014-07-08 07:34:20 -0700217 paint1.addColorEffect(pmToUPM1);
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000218 context->drawRectToRect(paint1, kDstRect, kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000219
220 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
221
222 context->setRenderTarget(tempTex->asRenderTarget());
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000223 GrPaint paint2;
bsalomon55fad7a2014-07-08 07:34:20 -0700224 paint2.addColorEffect(upmToPM);
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000225 context->drawRectToRect(paint2, kDstRect, kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000226 context->setRenderTarget(readTex->asRenderTarget());
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000227
228 GrPaint paint3;
bsalomon55fad7a2014-07-08 07:34:20 -0700229 paint3.addColorEffect(pmToUPM2);
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000230 context->drawRectToRect(paint3, kDstRect, kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000231
232 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
233
234 failed = false;
235 for (int y = 0; y < 256 && !failed; ++y) {
236 for (int x = 0; x <= y; ++x) {
237 if (firstRead[256 * y + x] != secondRead[256 * y + x]) {
238 failed = true;
239 break;
240 }
241 }
242 }
243 }
244 if (failed) {
245 *pmToUPMRule = kNone_PMConversion;
246 *upmToPMRule = kNone_PMConversion;
247 }
248}
249
bsalomon83d081a2014-07-08 09:56:10 -0700250const GrEffect* GrConfigConversionEffect::Create(GrTexture* texture,
251 bool swapRedAndBlue,
252 PMConversion pmConversion,
253 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000254 if (!swapRedAndBlue && kNone_PMConversion == pmConversion) {
bsalomon@google.comadc65362013-01-28 14:26:09 +0000255 // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000256 // then we may pollute our texture cache with redundant shaders. So in the case that no
bsalomon@google.comadc65362013-01-28 14:26:09 +0000257 // conversions were requested we instead return a GrSimpleTextureEffect.
258 return GrSimpleTextureEffect::Create(texture, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000259 } else {
260 if (kRGBA_8888_GrPixelConfig != texture->config() &&
261 kBGRA_8888_GrPixelConfig != texture->config() &&
262 kNone_PMConversion != pmConversion) {
263 // The PM conversions assume colors are 0..255
bsalomon@google.comadc65362013-01-28 14:26:09 +0000264 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000265 }
bsalomon55fad7a2014-07-08 07:34:20 -0700266 return SkNEW_ARGS(GrConfigConversionEffect, (texture,
267 swapRedAndBlue,
268 pmConversion,
269 matrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000270 }
271}