blob: 5e99dad60117924e2a48f61c25d0536cf9e5caf0 [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.comd698f772012-10-25 13:22:00 +000011#include "gl/GrGLEffect.h"
bsalomon@google.comb1456d72012-11-02 18:23:45 +000012#include "gl/GrGLEffectMatrix.h"
13#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.coma469c282012-10-24 18:28:34 +000018 const GrEffect& s) : INHERITED (factory) {
bsalomon@google.com021fc732012-10-25 12:47:42 +000019 const GrConfigConversionEffect& effect = static_cast<const GrConfigConversionEffect&>(s);
20 fSwapRedAndBlue = effect.swapsRedAndBlue();
21 fPMConversion = effect.pmConversion();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000022 }
23
bsalomon@google.com22a800a2012-10-26 19:16:46 +000024 virtual void emitCode(GrGLShaderBuilder* builder,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000025 const GrEffectStage&,
bsalomon@google.comb1456d72012-11-02 18:23:45 +000026 EffectKey key,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000027 const char* vertexCoords,
28 const char* outputColor,
29 const char* inputColor,
30 const TextureSamplerArray& samplers) SK_OVERRIDE {
bsalomon@google.comb1456d72012-11-02 18:23:45 +000031 const char* coords;
32 GrSLType coordsType = fEffectMatrix.emitCode(builder, key, vertexCoords, &coords);
bsalomon@google.com868a8e72012-08-30 19:11:34 +000033 builder->fFSCode.appendf("\t\t%s = ", outputColor);
bsalomon@google.comb1456d72012-11-02 18:23:45 +000034 builder->appendTextureLookup(&builder->fFSCode, samplers[0], coords, coordsType);
bsalomon@google.com2d8edaf2012-09-07 14:47:31 +000035 builder->fFSCode.append(";\n");
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000036 if (GrConfigConversionEffect::kNone_PMConversion == fPMConversion) {
37 GrAssert(fSwapRedAndBlue);
bsalomon@google.com868a8e72012-08-30 19:11:34 +000038 builder->fFSCode.appendf("\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:
43 builder->fFSCode.appendf(
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:
48 builder->fFSCode.appendf(
bsalomon@google.com868a8e72012-08-30 19:11:34 +000049 "\t\t%s = vec4(floor(%s.%s * %s.a * 255.0) / 255.0, %s.a);\n",
50 outputColor, outputColor, swiz, outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000051 break;
52 case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion:
bsalomon@google.com868a8e72012-08-30 19:11:34 +000053 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",
54 outputColor, outputColor, outputColor, swiz, outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000055 break;
56 case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion:
bsalomon@google.com868a8e72012-08-30 19:11:34 +000057 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",
58 outputColor, outputColor, outputColor, swiz, outputColor, outputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000059 break;
robertphillips@google.com2af1b182012-08-28 11:23:09 +000060 default:
61 GrCrash("Unknown conversion op.");
62 break;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000063 }
64 }
bsalomon@google.com868a8e72012-08-30 19:11:34 +000065 GrGLSLMulVarBy4f(&builder->fFSCode, 2, outputColor, inputColor);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000066 }
67
bsalomon@google.comb1456d72012-11-02 18:23:45 +000068 void setData(const GrGLUniformManager& uman, const GrEffectStage& stage) {
69 const GrConfigConversionEffect& effect =
70 static_cast<const GrConfigConversionEffect&>(*stage.getEffect());
71 fEffectMatrix.setData(uman,
72 effect.getMatrix(),
73 stage.getCoordChangeMatrix(),
74 effect.texture(0));
75 }
76
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000077 static inline EffectKey GenKey(const GrEffectStage& s, const GrGLCaps&) {
78 const GrConfigConversionEffect& effect =
79 static_cast<const GrConfigConversionEffect&>(*s.getEffect());
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.coma469c282012-10-24 18:28:34 +0000118bool GrConfigConversionEffect::isEqual(const GrEffect& s) const {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000119 const GrConfigConversionEffect& other = static_cast<const GrConfigConversionEffect&>(s);
120 return other.fSwapRedAndBlue == fSwapRedAndBlue && other.fPMConversion == fPMConversion;
121}
122
123///////////////////////////////////////////////////////////////////////////////
124
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000125GR_DEFINE_EFFECT_TEST(GrConfigConversionEffect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000126
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000127GrEffectRef* GrConfigConversionEffect::TestCreate(SkRandom* random,
128 GrContext* context,
129 GrTexture* textures[]) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000130 PMConversion pmConv = static_cast<PMConversion>(random->nextULessThan(kPMConversionCnt));
131 bool swapRB;
132 if (kNone_PMConversion == pmConv) {
133 swapRB = true;
134 } else {
135 swapRB = random->nextBool();
136 }
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000137 SkAutoTUnref<GrEffect> effect(SkNEW_ARGS(GrConfigConversionEffect,
138 (textures[GrEffectUnitTest::kSkiaPMTextureIdx],
139 swapRB,
140 pmConv,
141 GrEffectUnitTest::TestMatrix(random))));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000142 return CreateEffectRef(effect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000143}
144
145///////////////////////////////////////////////////////////////////////////////
146void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context,
147 PMConversion* pmToUPMRule,
148 PMConversion* upmToPMRule) {
149 *pmToUPMRule = kNone_PMConversion;
150 *upmToPMRule = kNone_PMConversion;
151 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
152 uint32_t* srcData = data.get();
153 uint32_t* firstRead = data.get() + 256 * 256;
154 uint32_t* secondRead = data.get() + 2 * 256 * 256;
155
156 // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate
157 // values in row y. We set r,g, and b to the same value since they are handled identically.
158 for (int y = 0; y < 256; ++y) {
159 for (int x = 0; x < 256; ++x) {
160 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
161 color[3] = y;
162 color[2] = GrMin(x, y);
163 color[1] = GrMin(x, y);
164 color[0] = GrMin(x, y);
165 }
166 }
167
168 GrTextureDesc desc;
169 desc.fFlags = kRenderTarget_GrTextureFlagBit |
170 kNoStencil_GrTextureFlagBit;
171 desc.fWidth = 256;
172 desc.fHeight = 256;
173 desc.fConfig = kRGBA_8888_GrPixelConfig;
174
175 SkAutoTUnref<GrTexture> readTex(context->createUncachedTexture(desc, NULL, 0));
176 if (!readTex.get()) {
177 return;
178 }
179 SkAutoTUnref<GrTexture> tempTex(context->createUncachedTexture(desc, NULL, 0));
180 if (!tempTex.get()) {
181 return;
182 }
183 desc.fFlags = kNone_GrTextureFlags;
184 SkAutoTUnref<GrTexture> dataTex(context->createUncachedTexture(desc, data, 0));
185 if (!dataTex.get()) {
186 return;
187 }
188
189 static const PMConversion kConversionRules[][2] = {
190 {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion},
191 {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion},
192 };
193
194 GrContext::AutoWideOpenIdentityDraw awoid(context, NULL);
195
196 bool failed = true;
197
198 for (size_t i = 0; i < GR_ARRAY_COUNT(kConversionRules) && failed; ++i) {
199 *pmToUPMRule = kConversionRules[i][0];
200 *upmToPMRule = kConversionRules[i][1];
201
bsalomon@google.com81712882012-11-01 17:12:34 +0000202 static const GrRect kDstRect = GrRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256));
203 static const GrRect kSrcRect = GrRect::MakeWH(SK_Scalar1, SK_Scalar1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000204 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
205 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
206 // We then verify that two reads produced the same values.
207
208 GrPaint paint;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000209 SkAutoTUnref<GrEffect> pmToUPM1(SkNEW_ARGS(GrConfigConversionEffect, (dataTex,
210 false,
211 *pmToUPMRule,
212 SkMatrix::I())));
213 SkAutoTUnref<GrEffect> upmToPM(SkNEW_ARGS(GrConfigConversionEffect, (readTex,
214 false,
215 *upmToPMRule,
216 SkMatrix::I())));
217 SkAutoTUnref<GrEffect> pmToUPM2(SkNEW_ARGS(GrConfigConversionEffect, (tempTex,
218 false,
219 *pmToUPMRule,
220 SkMatrix::I())));
221
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000222 SkAutoTUnref<GrEffectRef> pmToUPMEffect1(CreateEffectRef(pmToUPM1));
223 SkAutoTUnref<GrEffectRef> upmToPMEffect(CreateEffectRef(upmToPM));
224 SkAutoTUnref<GrEffectRef> pmToUPMEffect2(CreateEffectRef(pmToUPM2));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000225
226 context->setRenderTarget(readTex->asRenderTarget());
bsalomon@google.com08283af2012-10-26 13:01:20 +0000227 paint.colorStage(0)->setEffect(pmToUPMEffect1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000228 context->drawRectToRect(paint, kDstRect, kSrcRect);
229
230 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
231
232 context->setRenderTarget(tempTex->asRenderTarget());
bsalomon@google.com08283af2012-10-26 13:01:20 +0000233 paint.colorStage(0)->setEffect(upmToPMEffect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000234 context->drawRectToRect(paint, kDstRect, kSrcRect);
235 context->setRenderTarget(readTex->asRenderTarget());
bsalomon@google.com08283af2012-10-26 13:01:20 +0000236 paint.colorStage(0)->setEffect(pmToUPMEffect2);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000237 context->drawRectToRect(paint, kDstRect, kSrcRect);
238
239 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
240
241 failed = false;
242 for (int y = 0; y < 256 && !failed; ++y) {
243 for (int x = 0; x <= y; ++x) {
244 if (firstRead[256 * y + x] != secondRead[256 * y + x]) {
245 failed = true;
246 break;
247 }
248 }
249 }
250 }
251 if (failed) {
252 *pmToUPMRule = kNone_PMConversion;
253 *upmToPMRule = kNone_PMConversion;
254 }
255}
256
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000257bool GrConfigConversionEffect::InstallEffect(GrTexture* texture,
258 bool swapRedAndBlue,
259 PMConversion pmConversion,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000260 const SkMatrix& matrix,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000261 GrEffectStage* stage) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000262 if (!swapRedAndBlue && kNone_PMConversion == pmConversion) {
263 // If we returned a GrConfigConversionEffect that was equivalent to a GrSingleTextureEffect
264 // then we may pollute our texture cache with redundant shaders. So in the case that no
265 // conversions were requested we instead return a GrSingleTextureEffect.
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000266 stage->setEffect(GrSingleTextureEffect::Create(texture, matrix))->unref();
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000267 return true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000268 } else {
269 if (kRGBA_8888_GrPixelConfig != texture->config() &&
270 kBGRA_8888_GrPixelConfig != texture->config() &&
271 kNone_PMConversion != pmConversion) {
272 // The PM conversions assume colors are 0..255
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000273 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000274 }
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000275 SkAutoTUnref<GrEffect> effect(SkNEW_ARGS(GrConfigConversionEffect, (texture,
276 swapRedAndBlue,
277 pmConversion,
278 matrix)));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000279 stage->setEffect(CreateEffectRef(effect))->unref();
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000280 return true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000281 }
282}