2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "GrConfigConversionEffect.h"
10 #include "GrTBackendEffectFactory.h"
11 #include "GrSimpleTextureEffect.h"
12 #include "gl/GrGLEffect.h"
13 #include "gl/builders/GrGLProgramBuilder.h"
16 class GrGLConfigConversionEffect : public GrGLEffect {
18 GrGLConfigConversionEffect(const GrBackendEffectFactory& factory,
19 const GrDrawEffect& drawEffect)
20 : INHERITED (factory) {
21 const GrConfigConversionEffect& effect = drawEffect.castEffect<GrConfigConversionEffect>();
22 fSwapRedAndBlue = effect.swapsRedAndBlue();
23 fPMConversion = effect.pmConversion();
26 virtual void emitCode(GrGLProgramBuilder* builder,
28 const GrEffectKey& key,
29 const char* outputColor,
30 const char* inputColor,
31 const TransformedCoordsArray& coords,
32 const TextureSamplerArray& samplers) SK_OVERRIDE {
33 // Using highp for GLES here in order to avoid some precision issues on specific GPUs.
34 GrGLShaderVar tmpVar("tmpColor", kVec4f_GrSLType, 0, GrGLShaderVar::kHigh_Precision);
36 tmpVar.appendDecl(builder->ctxInfo(), &tmpDecl);
38 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
40 fsBuilder->codeAppendf("%s;", tmpDecl.c_str());
42 fsBuilder->codeAppendf("%s = ", tmpVar.c_str());
43 fsBuilder->appendTextureLookup(samplers[0], coords[0].c_str(), coords[0].type());
44 fsBuilder->codeAppend(";");
46 if (GrConfigConversionEffect::kNone_PMConversion == fPMConversion) {
47 SkASSERT(fSwapRedAndBlue);
48 fsBuilder->codeAppendf("%s = %s.bgra;", outputColor, tmpVar.c_str());
50 const char* swiz = fSwapRedAndBlue ? "bgr" : "rgb";
51 switch (fPMConversion) {
52 case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion:
53 fsBuilder->codeAppendf(
54 "%s = vec4(ceil(%s.%s * %s.a * 255.0) / 255.0, %s.a);",
55 tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str());
57 case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion:
58 // Add a compensation(0.001) here to avoid the side effect of the floor operation.
59 // In Intel GPUs, the integer value converted from floor(%s.r * 255.0) / 255.0
60 // is less than the integer value converted from %s.r by 1 when the %s.r is
61 // converted from the integer value 2^n, such as 1, 2, 4, 8, etc.
62 fsBuilder->codeAppendf(
63 "%s = vec4(floor(%s.%s * %s.a * 255.0 + 0.001) / 255.0, %s.a);",
64 tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str());
66 case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion:
67 fsBuilder->codeAppendf(
68 "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(ceil(%s.%s / %s.a * 255.0) / 255.0, %s.a);",
69 tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str());
71 case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion:
72 fsBuilder->codeAppendf(
73 "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(floor(%s.%s / %s.a * 255.0) / 255.0, %s.a);",
74 tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str());
77 SkFAIL("Unknown conversion op.");
80 fsBuilder->codeAppendf("%s = %s;", outputColor, tmpVar.c_str());
83 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor);
84 fsBuilder->codeAppend(modulate.c_str());
87 static inline void GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&,
88 GrEffectKeyBuilder* b) {
89 const GrConfigConversionEffect& conv = drawEffect.castEffect<GrConfigConversionEffect>();
90 uint32_t key = (conv.swapsRedAndBlue() ? 0 : 1) | (conv.pmConversion() << 1);
96 GrConfigConversionEffect::PMConversion fPMConversion;
98 typedef GrGLEffect INHERITED;
102 ///////////////////////////////////////////////////////////////////////////////
104 GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture,
106 PMConversion pmConversion,
107 const SkMatrix& matrix)
108 : GrSingleTextureEffect(texture, matrix)
109 , fSwapRedAndBlue(swapRedAndBlue)
110 , fPMConversion(pmConversion) {
111 SkASSERT(kRGBA_8888_GrPixelConfig == texture->config() ||
112 kBGRA_8888_GrPixelConfig == texture->config());
113 // Why did we pollute our texture cache instead of using a GrSingleTextureEffect?
114 SkASSERT(swapRedAndBlue || kNone_PMConversion != pmConversion);
117 const GrBackendEffectFactory& GrConfigConversionEffect::getFactory() const {
118 return GrTBackendEffectFactory<GrConfigConversionEffect>::getInstance();
121 bool GrConfigConversionEffect::onIsEqual(const GrEffect& s) const {
122 const GrConfigConversionEffect& other = CastEffect<GrConfigConversionEffect>(s);
123 return this->texture(0) == s.texture(0) &&
124 other.fSwapRedAndBlue == fSwapRedAndBlue &&
125 other.fPMConversion == fPMConversion;
128 void GrConfigConversionEffect::getConstantColorComponents(GrColor* color,
129 uint32_t* validFlags) const {
130 this->updateConstantColorComponentsForModulation(color, validFlags);
133 ///////////////////////////////////////////////////////////////////////////////
135 GR_DEFINE_EFFECT_TEST(GrConfigConversionEffect);
137 GrEffect* GrConfigConversionEffect::TestCreate(SkRandom* random,
139 const GrDrawTargetCaps&,
140 GrTexture* textures[]) {
141 PMConversion pmConv = static_cast<PMConversion>(random->nextULessThan(kPMConversionCnt));
143 if (kNone_PMConversion == pmConv) {
146 swapRB = random->nextBool();
148 return SkNEW_ARGS(GrConfigConversionEffect,
149 (textures[GrEffectUnitTest::kSkiaPMTextureIdx],
152 GrEffectUnitTest::TestMatrix(random)));
155 ///////////////////////////////////////////////////////////////////////////////
156 void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context,
157 PMConversion* pmToUPMRule,
158 PMConversion* upmToPMRule) {
159 *pmToUPMRule = kNone_PMConversion;
160 *upmToPMRule = kNone_PMConversion;
161 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
162 uint32_t* srcData = data.get();
163 uint32_t* firstRead = data.get() + 256 * 256;
164 uint32_t* secondRead = data.get() + 2 * 256 * 256;
166 // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate
167 // values in row y. We set r,g, and b to the same value since they are handled identically.
168 for (int y = 0; y < 256; ++y) {
169 for (int x = 0; x < 256; ++x) {
170 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
172 color[2] = SkTMin(x, y);
173 color[1] = SkTMin(x, y);
174 color[0] = SkTMin(x, y);
179 desc.fFlags = kRenderTarget_GrTextureFlagBit |
180 kNoStencil_GrTextureFlagBit;
183 desc.fConfig = kRGBA_8888_GrPixelConfig;
185 SkAutoTUnref<GrTexture> readTex(context->createUncachedTexture(desc, NULL, 0));
186 if (!readTex.get()) {
189 SkAutoTUnref<GrTexture> tempTex(context->createUncachedTexture(desc, NULL, 0));
190 if (!tempTex.get()) {
193 desc.fFlags = kNone_GrTextureFlags;
194 SkAutoTUnref<GrTexture> dataTex(context->createUncachedTexture(desc, data, 0));
195 if (!dataTex.get()) {
199 static const PMConversion kConversionRules[][2] = {
200 {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion},
201 {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion},
204 GrContext::AutoWideOpenIdentityDraw awoid(context, NULL);
208 for (size_t i = 0; i < SK_ARRAY_COUNT(kConversionRules) && failed; ++i) {
209 *pmToUPMRule = kConversionRules[i][0];
210 *upmToPMRule = kConversionRules[i][1];
212 static const SkRect kDstRect = SkRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256));
213 static const SkRect kSrcRect = SkRect::MakeWH(SK_Scalar1, SK_Scalar1);
214 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
215 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
216 // We then verify that two reads produced the same values.
218 SkAutoTUnref<GrEffect> pmToUPM1(SkNEW_ARGS(GrConfigConversionEffect, (dataTex,
222 SkAutoTUnref<GrEffect> upmToPM(SkNEW_ARGS(GrConfigConversionEffect, (readTex,
226 SkAutoTUnref<GrEffect> pmToUPM2(SkNEW_ARGS(GrConfigConversionEffect, (tempTex,
231 context->setRenderTarget(readTex->asRenderTarget());
233 paint1.addColorEffect(pmToUPM1);
234 context->drawRectToRect(paint1, kDstRect, kSrcRect);
236 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
238 context->setRenderTarget(tempTex->asRenderTarget());
240 paint2.addColorEffect(upmToPM);
241 context->drawRectToRect(paint2, kDstRect, kSrcRect);
242 context->setRenderTarget(readTex->asRenderTarget());
245 paint3.addColorEffect(pmToUPM2);
246 context->drawRectToRect(paint3, kDstRect, kSrcRect);
248 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
251 for (int y = 0; y < 256 && !failed; ++y) {
252 for (int x = 0; x <= y; ++x) {
253 if (firstRead[256 * y + x] != secondRead[256 * y + x]) {
261 *pmToUPMRule = kNone_PMConversion;
262 *upmToPMRule = kNone_PMConversion;
266 const GrEffect* GrConfigConversionEffect::Create(GrTexture* texture,
268 PMConversion pmConversion,
269 const SkMatrix& matrix) {
270 if (!swapRedAndBlue && kNone_PMConversion == pmConversion) {
271 // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect
272 // then we may pollute our texture cache with redundant shaders. So in the case that no
273 // conversions were requested we instead return a GrSimpleTextureEffect.
274 return GrSimpleTextureEffect::Create(texture, matrix);
276 if (kRGBA_8888_GrPixelConfig != texture->config() &&
277 kBGRA_8888_GrPixelConfig != texture->config() &&
278 kNone_PMConversion != pmConversion) {
279 // The PM conversions assume colors are 0..255
282 return SkNEW_ARGS(GrConfigConversionEffect, (texture,