blob: d42896d2102f80c4a1f4c3c9358bffa9ad3dae92 [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.coma469c282012-10-24 18:28:34 +0000127GrEffect* 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.comb1456d72012-11-02 18:23:45 +0000137 return SkNEW_ARGS(GrConfigConversionEffect, (textures[GrEffectUnitTest::kSkiaPMTextureIdx],
138 swapRB,
139 pmConv,
140 GrEffectUnitTest::TestMatrix(random)));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000141}
142
143///////////////////////////////////////////////////////////////////////////////
144void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context,
145 PMConversion* pmToUPMRule,
146 PMConversion* upmToPMRule) {
147 *pmToUPMRule = kNone_PMConversion;
148 *upmToPMRule = kNone_PMConversion;
149 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
150 uint32_t* srcData = data.get();
151 uint32_t* firstRead = data.get() + 256 * 256;
152 uint32_t* secondRead = data.get() + 2 * 256 * 256;
153
154 // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate
155 // values in row y. We set r,g, and b to the same value since they are handled identically.
156 for (int y = 0; y < 256; ++y) {
157 for (int x = 0; x < 256; ++x) {
158 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
159 color[3] = y;
160 color[2] = GrMin(x, y);
161 color[1] = GrMin(x, y);
162 color[0] = GrMin(x, y);
163 }
164 }
165
166 GrTextureDesc desc;
167 desc.fFlags = kRenderTarget_GrTextureFlagBit |
168 kNoStencil_GrTextureFlagBit;
169 desc.fWidth = 256;
170 desc.fHeight = 256;
171 desc.fConfig = kRGBA_8888_GrPixelConfig;
172
173 SkAutoTUnref<GrTexture> readTex(context->createUncachedTexture(desc, NULL, 0));
174 if (!readTex.get()) {
175 return;
176 }
177 SkAutoTUnref<GrTexture> tempTex(context->createUncachedTexture(desc, NULL, 0));
178 if (!tempTex.get()) {
179 return;
180 }
181 desc.fFlags = kNone_GrTextureFlags;
182 SkAutoTUnref<GrTexture> dataTex(context->createUncachedTexture(desc, data, 0));
183 if (!dataTex.get()) {
184 return;
185 }
186
187 static const PMConversion kConversionRules[][2] = {
188 {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion},
189 {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion},
190 };
191
192 GrContext::AutoWideOpenIdentityDraw awoid(context, NULL);
193
194 bool failed = true;
195
196 for (size_t i = 0; i < GR_ARRAY_COUNT(kConversionRules) && failed; ++i) {
197 *pmToUPMRule = kConversionRules[i][0];
198 *upmToPMRule = kConversionRules[i][1];
199
bsalomon@google.com81712882012-11-01 17:12:34 +0000200 static const GrRect kDstRect = GrRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256));
201 static const GrRect kSrcRect = GrRect::MakeWH(SK_Scalar1, SK_Scalar1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000202 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
203 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
204 // We then verify that two reads produced the same values.
205
206 GrPaint paint;
bsalomon@google.com021fc732012-10-25 12:47:42 +0000207 SkAutoTUnref<GrEffect> pmToUPMEffect1(SkNEW_ARGS(GrConfigConversionEffect,
bsalomon@google.comb1456d72012-11-02 18:23:45 +0000208 (dataTex,
209 false,
210 *pmToUPMRule,
211 SkMatrix::I())));
bsalomon@google.com021fc732012-10-25 12:47:42 +0000212 SkAutoTUnref<GrEffect> upmToPMEffect(SkNEW_ARGS(GrConfigConversionEffect,
bsalomon@google.comb1456d72012-11-02 18:23:45 +0000213 (readTex,
214 false,
215 *upmToPMRule,
216 SkMatrix::I())));
bsalomon@google.com021fc732012-10-25 12:47:42 +0000217 SkAutoTUnref<GrEffect> pmToUPMEffect2(SkNEW_ARGS(GrConfigConversionEffect,
bsalomon@google.comb1456d72012-11-02 18:23:45 +0000218 (tempTex,
219 false,
220 *pmToUPMRule,
221 SkMatrix::I())));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000222
223 context->setRenderTarget(readTex->asRenderTarget());
bsalomon@google.com08283af2012-10-26 13:01:20 +0000224 paint.colorStage(0)->setEffect(pmToUPMEffect1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000225 context->drawRectToRect(paint, kDstRect, kSrcRect);
226
227 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
228
229 context->setRenderTarget(tempTex->asRenderTarget());
bsalomon@google.com08283af2012-10-26 13:01:20 +0000230 paint.colorStage(0)->setEffect(upmToPMEffect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000231 context->drawRectToRect(paint, kDstRect, kSrcRect);
232 context->setRenderTarget(readTex->asRenderTarget());
bsalomon@google.com08283af2012-10-26 13:01:20 +0000233 paint.colorStage(0)->setEffect(pmToUPMEffect2);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000234 context->drawRectToRect(paint, kDstRect, kSrcRect);
235
236 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
237
238 failed = false;
239 for (int y = 0; y < 256 && !failed; ++y) {
240 for (int x = 0; x <= y; ++x) {
241 if (firstRead[256 * y + x] != secondRead[256 * y + x]) {
242 failed = true;
243 break;
244 }
245 }
246 }
247 }
248 if (failed) {
249 *pmToUPMRule = kNone_PMConversion;
250 *upmToPMRule = kNone_PMConversion;
251 }
252}
253
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000254bool GrConfigConversionEffect::InstallEffect(GrTexture* texture,
255 bool swapRedAndBlue,
256 PMConversion pmConversion,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000257 const SkMatrix& matrix,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000258 GrEffectStage* stage) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000259 if (!swapRedAndBlue && kNone_PMConversion == pmConversion) {
260 // If we returned a GrConfigConversionEffect that was equivalent to a GrSingleTextureEffect
261 // then we may pollute our texture cache with redundant shaders. So in the case that no
262 // conversions were requested we instead return a GrSingleTextureEffect.
bsalomon@google.comdbe49f72012-11-05 16:36:02 +0000263 stage->setEffect(SkNEW_ARGS(GrSingleTextureEffect, (texture, matrix)))->unref();
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000264 return true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000265 } else {
266 if (kRGBA_8888_GrPixelConfig != texture->config() &&
267 kBGRA_8888_GrPixelConfig != texture->config() &&
268 kNone_PMConversion != pmConversion) {
269 // The PM conversions assume colors are 0..255
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000270 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000271 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000272 stage->setEffect(SkNEW_ARGS(GrConfigConversionEffect, (texture,
273 swapRedAndBlue,
bsalomon@google.comb1456d72012-11-02 18:23:45 +0000274 pmConversion, matrix)))->unref();
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000275 return true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000276 }
277}