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"
15 class GrGLConfigConversionEffect : public GrGLEffect {
17 GrGLConfigConversionEffect(const GrBackendEffectFactory& factory,
18 const GrDrawEffect& drawEffect)
19 : INHERITED (factory) {
20 const GrConfigConversionEffect& effect = drawEffect.castEffect<GrConfigConversionEffect>();
21 fSwapRedAndBlue = effect.swapsRedAndBlue();
22 fPMConversion = effect.pmConversion();
25 virtual void emitCode(GrGLShaderBuilder* builder,
28 const char* outputColor,
29 const char* inputColor,
30 const TransformedCoordsArray& coords,
31 const TextureSamplerArray& samplers) SK_OVERRIDE {
32 builder->fsCodeAppendf("\t\t%s = ", outputColor);
33 builder->fsAppendTextureLookup(samplers[0], coords[0].c_str(), coords[0].type());
34 builder->fsCodeAppend(";\n");
35 if (GrConfigConversionEffect::kNone_PMConversion == fPMConversion) {
36 SkASSERT(fSwapRedAndBlue);
37 builder->fsCodeAppendf("\t%s = %s.bgra;\n", outputColor, outputColor);
39 const char* swiz = fSwapRedAndBlue ? "bgr" : "rgb";
40 switch (fPMConversion) {
41 case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion:
42 builder->fsCodeAppendf(
43 "\t\t%s = vec4(ceil(%s.%s * %s.a * 255.0) / 255.0, %s.a);\n",
44 outputColor, outputColor, swiz, outputColor, outputColor);
46 case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion:
47 // 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.
51 builder->fsCodeAppendf(
52 "\t\t%s = vec4(floor(%s.%s * %s.a * 255.0 + 0.001) / 255.0, %s.a);\n",
53 outputColor, outputColor, swiz, outputColor, outputColor);
55 case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion:
56 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",
57 outputColor, outputColor, outputColor, swiz, outputColor, outputColor);
59 case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion:
60 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",
61 outputColor, outputColor, outputColor, swiz, outputColor, outputColor);
64 SkFAIL("Unknown conversion op.");
69 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor);
70 builder->fsCodeAppend(modulate.c_str());
73 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
74 const GrConfigConversionEffect& conv = drawEffect.castEffect<GrConfigConversionEffect>();
75 return static_cast<EffectKey>(conv.swapsRedAndBlue()) | (conv.pmConversion() << 1);
80 GrConfigConversionEffect::PMConversion fPMConversion;
82 typedef GrGLEffect INHERITED;
86 ///////////////////////////////////////////////////////////////////////////////
88 GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture,
90 PMConversion pmConversion,
91 const SkMatrix& matrix)
92 : GrSingleTextureEffect(texture, matrix)
93 , fSwapRedAndBlue(swapRedAndBlue)
94 , fPMConversion(pmConversion) {
95 SkASSERT(kRGBA_8888_GrPixelConfig == texture->config() ||
96 kBGRA_8888_GrPixelConfig == texture->config());
97 // Why did we pollute our texture cache instead of using a GrSingleTextureEffect?
98 SkASSERT(swapRedAndBlue || kNone_PMConversion != pmConversion);
101 const GrBackendEffectFactory& GrConfigConversionEffect::getFactory() const {
102 return GrTBackendEffectFactory<GrConfigConversionEffect>::getInstance();
105 bool GrConfigConversionEffect::onIsEqual(const GrEffect& s) const {
106 const GrConfigConversionEffect& other = CastEffect<GrConfigConversionEffect>(s);
107 return this->texture(0) == s.texture(0) &&
108 other.fSwapRedAndBlue == fSwapRedAndBlue &&
109 other.fPMConversion == fPMConversion;
112 void GrConfigConversionEffect::getConstantColorComponents(GrColor* color,
113 uint32_t* validFlags) const {
114 this->updateConstantColorComponentsForModulation(color, validFlags);
117 ///////////////////////////////////////////////////////////////////////////////
119 GR_DEFINE_EFFECT_TEST(GrConfigConversionEffect);
121 GrEffect* GrConfigConversionEffect::TestCreate(SkRandom* random,
123 const GrDrawTargetCaps&,
124 GrTexture* textures[]) {
125 PMConversion pmConv = static_cast<PMConversion>(random->nextULessThan(kPMConversionCnt));
127 if (kNone_PMConversion == pmConv) {
130 swapRB = random->nextBool();
132 return SkNEW_ARGS(GrConfigConversionEffect,
133 (textures[GrEffectUnitTest::kSkiaPMTextureIdx],
136 GrEffectUnitTest::TestMatrix(random)));
139 ///////////////////////////////////////////////////////////////////////////////
140 void 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;
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]);
156 color[2] = SkTMin(x, y);
157 color[1] = SkTMin(x, y);
158 color[0] = SkTMin(x, y);
163 desc.fFlags = kRenderTarget_GrTextureFlagBit |
164 kNoStencil_GrTextureFlagBit;
167 desc.fConfig = kRGBA_8888_GrPixelConfig;
169 SkAutoTUnref<GrTexture> readTex(context->createUncachedTexture(desc, NULL, 0));
170 if (!readTex.get()) {
173 SkAutoTUnref<GrTexture> tempTex(context->createUncachedTexture(desc, NULL, 0));
174 if (!tempTex.get()) {
177 desc.fFlags = kNone_GrTextureFlags;
178 SkAutoTUnref<GrTexture> dataTex(context->createUncachedTexture(desc, data, 0));
179 if (!dataTex.get()) {
183 static const PMConversion kConversionRules[][2] = {
184 {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion},
185 {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion},
188 GrContext::AutoWideOpenIdentityDraw awoid(context, NULL);
192 for (size_t i = 0; i < SK_ARRAY_COUNT(kConversionRules) && failed; ++i) {
193 *pmToUPMRule = kConversionRules[i][0];
194 *upmToPMRule = kConversionRules[i][1];
196 static const SkRect kDstRect = SkRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256));
197 static const SkRect kSrcRect = SkRect::MakeWH(SK_Scalar1, SK_Scalar1);
198 // 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.
202 SkAutoTUnref<GrEffect> pmToUPM1(SkNEW_ARGS(GrConfigConversionEffect, (dataTex,
206 SkAutoTUnref<GrEffect> upmToPM(SkNEW_ARGS(GrConfigConversionEffect, (readTex,
210 SkAutoTUnref<GrEffect> pmToUPM2(SkNEW_ARGS(GrConfigConversionEffect, (tempTex,
215 context->setRenderTarget(readTex->asRenderTarget());
217 paint1.addColorEffect(pmToUPM1);
218 context->drawRectToRect(paint1, kDstRect, kSrcRect);
220 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
222 context->setRenderTarget(tempTex->asRenderTarget());
224 paint2.addColorEffect(upmToPM);
225 context->drawRectToRect(paint2, kDstRect, kSrcRect);
226 context->setRenderTarget(readTex->asRenderTarget());
229 paint3.addColorEffect(pmToUPM2);
230 context->drawRectToRect(paint3, kDstRect, kSrcRect);
232 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
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]) {
245 *pmToUPMRule = kNone_PMConversion;
246 *upmToPMRule = kNone_PMConversion;
250 const GrEffect* GrConfigConversionEffect::Create(GrTexture* texture,
252 PMConversion pmConversion,
253 const SkMatrix& matrix) {
254 if (!swapRedAndBlue && kNone_PMConversion == pmConversion) {
255 // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect
256 // then we may pollute our texture cache with redundant shaders. So in the case that no
257 // conversions were requested we instead return a GrSimpleTextureEffect.
258 return GrSimpleTextureEffect::Create(texture, matrix);
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
266 return SkNEW_ARGS(GrConfigConversionEffect, (texture,