blob: c421867dd76e71a1bc0044f30921ccee34af2964 [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"
bsalomon848faf02014-07-11 10:01:02 -070013#include "gl/GrGLShaderBuilder.h"
bsalomon@google.comb1456d72012-11-02 18:23:45 +000014#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.comc7818882013-03-20 19:19:53 +000019 const GrDrawEffect& drawEffect)
bsalomon@google.com77af6802013-10-02 13:04:56 +000020 : INHERITED (factory) {
bsalomon@google.comc7818882013-03-20 19:19:53 +000021 const GrConfigConversionEffect& effect = drawEffect.castEffect<GrConfigConversionEffect>();
bsalomon@google.com021fc732012-10-25 12:47:42 +000022 fSwapRedAndBlue = effect.swapsRedAndBlue();
23 fPMConversion = effect.pmConversion();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000024 }
25
bsalomon@google.com22a800a2012-10-26 19:16:46 +000026 virtual void emitCode(GrGLShaderBuilder* builder,
bsalomon@google.comc7818882013-03-20 19:19:53 +000027 const GrDrawEffect&,
bsalomon@google.comb1456d72012-11-02 18:23:45 +000028 EffectKey key,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000029 const char* outputColor,
30 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000031 const TransformedCoordsArray& coords,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000032 const TextureSamplerArray& samplers) SK_OVERRIDE {
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000033 builder->fsCodeAppendf("\t\t%s = ", outputColor);
bsalomon@google.com77af6802013-10-02 13:04:56 +000034 builder->fsAppendTextureLookup(samplers[0], coords[0].c_str(), coords[0].type());
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000035 builder->fsCodeAppend(";\n");
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000036 if (GrConfigConversionEffect::kNone_PMConversion == fPMConversion) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000037 SkASSERT(fSwapRedAndBlue);
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000038 builder->fsCodeAppendf("\t%s = %s.bgra;\n", outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000039 } else {
40 const char* swiz = fSwapRedAndBlue ? "bgr" : "rgb";
41 switch (fPMConversion) {
42 case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion:
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000043 builder->fsCodeAppendf(
bsalomon@google.com868a8e72012-08-30 19:11:34 +000044 "\t\t%s = vec4(ceil(%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::kMulByAlpha_RoundDown_PMConversion:
commit-bot@chromium.orgb4e200e2013-06-19 11:41:02 +000048 // Add a compensation(0.001) here to avoid the side effect of the floor operation.
49 // In Intel GPUs, the integer value converted from floor(%s.r * 255.0) / 255.0
50 // is less than the integer value converted from %s.r by 1 when the %s.r is
51 // converted from the integer value 2^n, such as 1, 2, 4, 8, etc.
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000052 builder->fsCodeAppendf(
commit-bot@chromium.orgb4e200e2013-06-19 11:41:02 +000053 "\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 +000054 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:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +000065 SkFAIL("Unknown conversion op.");
robertphillips@google.com2af1b182012-08-28 11:23:09 +000066 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.comc7818882013-03-20 19:19:53 +000074 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
75 const GrConfigConversionEffect& conv = drawEffect.castEffect<GrConfigConversionEffect>();
bsalomon@google.com77af6802013-10-02 13:04:56 +000076 return static_cast<EffectKey>(conv.swapsRedAndBlue()) | (conv.pmConversion() << 1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000077 }
78
79private:
80 bool fSwapRedAndBlue;
81 GrConfigConversionEffect::PMConversion fPMConversion;
82
bsalomon@google.com22a800a2012-10-26 19:16:46 +000083 typedef GrGLEffect INHERITED;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000084
85};
86
87///////////////////////////////////////////////////////////////////////////////
88
89GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture,
90 bool swapRedAndBlue,
bsalomon@google.comb1456d72012-11-02 18:23:45 +000091 PMConversion pmConversion,
92 const SkMatrix& matrix)
93 : GrSingleTextureEffect(texture, matrix)
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000094 , fSwapRedAndBlue(swapRedAndBlue)
95 , fPMConversion(pmConversion) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000096 SkASSERT(kRGBA_8888_GrPixelConfig == texture->config() ||
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000097 kBGRA_8888_GrPixelConfig == texture->config());
98 // Why did we pollute our texture cache instead of using a GrSingleTextureEffect?
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000099 SkASSERT(swapRedAndBlue || kNone_PMConversion != pmConversion);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000100}
101
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000102const GrBackendEffectFactory& GrConfigConversionEffect::getFactory() const {
103 return GrTBackendEffectFactory<GrConfigConversionEffect>::getInstance();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000104}
105
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000106bool GrConfigConversionEffect::onIsEqual(const GrEffect& s) const {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000107 const GrConfigConversionEffect& other = CastEffect<GrConfigConversionEffect>(s);
bsalomon@google.com8a252f72013-01-22 20:35:13 +0000108 return this->texture(0) == s.texture(0) &&
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000109 other.fSwapRedAndBlue == fSwapRedAndBlue &&
110 other.fPMConversion == fPMConversion;
111}
112
113void GrConfigConversionEffect::getConstantColorComponents(GrColor* color,
114 uint32_t* validFlags) const {
115 this->updateConstantColorComponentsForModulation(color, validFlags);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000116}
117
118///////////////////////////////////////////////////////////////////////////////
119
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000120GR_DEFINE_EFFECT_TEST(GrConfigConversionEffect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000121
bsalomon83d081a2014-07-08 09:56:10 -0700122GrEffect* GrConfigConversionEffect::TestCreate(SkRandom* random,
123 GrContext*,
124 const GrDrawTargetCaps&,
125 GrTexture* textures[]) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000126 PMConversion pmConv = static_cast<PMConversion>(random->nextULessThan(kPMConversionCnt));
127 bool swapRB;
128 if (kNone_PMConversion == pmConv) {
129 swapRB = true;
130 } else {
131 swapRB = random->nextBool();
132 }
bsalomon55fad7a2014-07-08 07:34:20 -0700133 return SkNEW_ARGS(GrConfigConversionEffect,
bsalomon@google.com6340a412013-01-22 19:55:59 +0000134 (textures[GrEffectUnitTest::kSkiaPMTextureIdx],
135 swapRB,
136 pmConv,
bsalomon55fad7a2014-07-08 07:34:20 -0700137 GrEffectUnitTest::TestMatrix(random)));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000138}
139
140///////////////////////////////////////////////////////////////////////////////
141void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context,
142 PMConversion* pmToUPMRule,
143 PMConversion* upmToPMRule) {
144 *pmToUPMRule = kNone_PMConversion;
145 *upmToPMRule = kNone_PMConversion;
146 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
147 uint32_t* srcData = data.get();
148 uint32_t* firstRead = data.get() + 256 * 256;
149 uint32_t* secondRead = data.get() + 2 * 256 * 256;
150
151 // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate
152 // values in row y. We set r,g, and b to the same value since they are handled identically.
153 for (int y = 0; y < 256; ++y) {
154 for (int x = 0; x < 256; ++x) {
155 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
156 color[3] = y;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000157 color[2] = SkTMin(x, y);
158 color[1] = SkTMin(x, y);
159 color[0] = SkTMin(x, y);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000160 }
161 }
162
163 GrTextureDesc desc;
164 desc.fFlags = kRenderTarget_GrTextureFlagBit |
165 kNoStencil_GrTextureFlagBit;
166 desc.fWidth = 256;
167 desc.fHeight = 256;
168 desc.fConfig = kRGBA_8888_GrPixelConfig;
169
170 SkAutoTUnref<GrTexture> readTex(context->createUncachedTexture(desc, NULL, 0));
171 if (!readTex.get()) {
172 return;
173 }
174 SkAutoTUnref<GrTexture> tempTex(context->createUncachedTexture(desc, NULL, 0));
175 if (!tempTex.get()) {
176 return;
177 }
178 desc.fFlags = kNone_GrTextureFlags;
179 SkAutoTUnref<GrTexture> dataTex(context->createUncachedTexture(desc, data, 0));
180 if (!dataTex.get()) {
181 return;
182 }
183
184 static const PMConversion kConversionRules[][2] = {
185 {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion},
186 {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion},
187 };
188
189 GrContext::AutoWideOpenIdentityDraw awoid(context, NULL);
190
191 bool failed = true;
192
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000193 for (size_t i = 0; i < SK_ARRAY_COUNT(kConversionRules) && failed; ++i) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000194 *pmToUPMRule = kConversionRules[i][0];
195 *upmToPMRule = kConversionRules[i][1];
196
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000197 static const SkRect kDstRect = SkRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256));
198 static const SkRect kSrcRect = SkRect::MakeWH(SK_Scalar1, SK_Scalar1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000199 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
200 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
201 // We then verify that two reads produced the same values.
202
bsalomon55fad7a2014-07-08 07:34:20 -0700203 SkAutoTUnref<GrEffect> pmToUPM1(SkNEW_ARGS(GrConfigConversionEffect, (dataTex,
204 false,
205 *pmToUPMRule,
206 SkMatrix::I())));
207 SkAutoTUnref<GrEffect> upmToPM(SkNEW_ARGS(GrConfigConversionEffect, (readTex,
208 false,
209 *upmToPMRule,
210 SkMatrix::I())));
211 SkAutoTUnref<GrEffect> pmToUPM2(SkNEW_ARGS(GrConfigConversionEffect, (tempTex,
212 false,
213 *pmToUPMRule,
214 SkMatrix::I())));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000215
216 context->setRenderTarget(readTex->asRenderTarget());
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000217 GrPaint paint1;
bsalomon55fad7a2014-07-08 07:34:20 -0700218 paint1.addColorEffect(pmToUPM1);
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000219 context->drawRectToRect(paint1, kDstRect, kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000220
221 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
222
223 context->setRenderTarget(tempTex->asRenderTarget());
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000224 GrPaint paint2;
bsalomon55fad7a2014-07-08 07:34:20 -0700225 paint2.addColorEffect(upmToPM);
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000226 context->drawRectToRect(paint2, kDstRect, kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000227 context->setRenderTarget(readTex->asRenderTarget());
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000228
229 GrPaint paint3;
bsalomon55fad7a2014-07-08 07:34:20 -0700230 paint3.addColorEffect(pmToUPM2);
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000231 context->drawRectToRect(paint3, kDstRect, kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000232
233 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
234
235 failed = false;
236 for (int y = 0; y < 256 && !failed; ++y) {
237 for (int x = 0; x <= y; ++x) {
238 if (firstRead[256 * y + x] != secondRead[256 * y + x]) {
239 failed = true;
240 break;
241 }
242 }
243 }
244 }
245 if (failed) {
246 *pmToUPMRule = kNone_PMConversion;
247 *upmToPMRule = kNone_PMConversion;
248 }
249}
250
bsalomon83d081a2014-07-08 09:56:10 -0700251const GrEffect* GrConfigConversionEffect::Create(GrTexture* texture,
252 bool swapRedAndBlue,
253 PMConversion pmConversion,
254 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000255 if (!swapRedAndBlue && kNone_PMConversion == pmConversion) {
bsalomon@google.comadc65362013-01-28 14:26:09 +0000256 // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000257 // then we may pollute our texture cache with redundant shaders. So in the case that no
bsalomon@google.comadc65362013-01-28 14:26:09 +0000258 // conversions were requested we instead return a GrSimpleTextureEffect.
259 return GrSimpleTextureEffect::Create(texture, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000260 } else {
261 if (kRGBA_8888_GrPixelConfig != texture->config() &&
262 kBGRA_8888_GrPixelConfig != texture->config() &&
263 kNone_PMConversion != pmConversion) {
264 // The PM conversions assume colors are 0..255
bsalomon@google.comadc65362013-01-28 14:26:09 +0000265 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000266 }
bsalomon55fad7a2014-07-08 07:34:20 -0700267 return SkNEW_ARGS(GrConfigConversionEffect, (texture,
268 swapRedAndBlue,
269 pmConversion,
270 matrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000271 }
272}