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/GrGLShaderBuilder.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(GrGLShaderBuilder* builder,
29 const char* outputColor,
30 const char* inputColor,
31 const TransformedCoordsArray& coords,
32 const TextureSamplerArray& samplers) SK_OVERRIDE {
33 builder->fsCodeAppendf("\t\t%s = ", outputColor);
34 builder->fsAppendTextureLookup(samplers[0], coords[0].c_str(), coords[0].type());
35 builder->fsCodeAppend(";\n");
36 if (GrConfigConversionEffect::kNone_PMConversion == fPMConversion) {
37 SkASSERT(fSwapRedAndBlue);
38 builder->fsCodeAppendf("\t%s = %s.bgra;\n", outputColor, outputColor);
40 const char* swiz = fSwapRedAndBlue ? "bgr" : "rgb";
41 switch (fPMConversion) {
42 case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion:
43 builder->fsCodeAppendf(
44 "\t\t%s = vec4(ceil(%s.%s * %s.a * 255.0) / 255.0, %s.a);\n",
45 outputColor, outputColor, swiz, outputColor, outputColor);
47 case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion:
48 // 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.
52 builder->fsCodeAppendf(
53 "\t\t%s = vec4(floor(%s.%s * %s.a * 255.0 + 0.001) / 255.0, %s.a);\n",
54 outputColor, outputColor, swiz, outputColor, outputColor);
56 case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion:
57 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",
58 outputColor, outputColor, outputColor, swiz, outputColor, outputColor);
60 case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion:
61 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",
62 outputColor, outputColor, outputColor, swiz, outputColor, outputColor);
65 SkFAIL("Unknown conversion op.");
70 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor);
71 builder->fsCodeAppend(modulate.c_str());
74 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
75 const GrConfigConversionEffect& conv = drawEffect.castEffect<GrConfigConversionEffect>();
76 return static_cast<EffectKey>(conv.swapsRedAndBlue()) | (conv.pmConversion() << 1);
81 GrConfigConversionEffect::PMConversion fPMConversion;
83 typedef GrGLEffect INHERITED;
87 ///////////////////////////////////////////////////////////////////////////////
89 GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture,
91 PMConversion pmConversion,
92 const SkMatrix& matrix)
93 : GrSingleTextureEffect(texture, matrix)
94 , fSwapRedAndBlue(swapRedAndBlue)
95 , fPMConversion(pmConversion) {
96 SkASSERT(kRGBA_8888_GrPixelConfig == texture->config() ||
97 kBGRA_8888_GrPixelConfig == texture->config());
98 // Why did we pollute our texture cache instead of using a GrSingleTextureEffect?
99 SkASSERT(swapRedAndBlue || kNone_PMConversion != pmConversion);
102 const GrBackendEffectFactory& GrConfigConversionEffect::getFactory() const {
103 return GrTBackendEffectFactory<GrConfigConversionEffect>::getInstance();
106 bool GrConfigConversionEffect::onIsEqual(const GrEffect& s) const {
107 const GrConfigConversionEffect& other = CastEffect<GrConfigConversionEffect>(s);
108 return this->texture(0) == s.texture(0) &&
109 other.fSwapRedAndBlue == fSwapRedAndBlue &&
110 other.fPMConversion == fPMConversion;
113 void GrConfigConversionEffect::getConstantColorComponents(GrColor* color,
114 uint32_t* validFlags) const {
115 this->updateConstantColorComponentsForModulation(color, validFlags);
118 ///////////////////////////////////////////////////////////////////////////////
120 GR_DEFINE_EFFECT_TEST(GrConfigConversionEffect);
122 GrEffect* GrConfigConversionEffect::TestCreate(SkRandom* random,
124 const GrDrawTargetCaps&,
125 GrTexture* textures[]) {
126 PMConversion pmConv = static_cast<PMConversion>(random->nextULessThan(kPMConversionCnt));
128 if (kNone_PMConversion == pmConv) {
131 swapRB = random->nextBool();
133 return SkNEW_ARGS(GrConfigConversionEffect,
134 (textures[GrEffectUnitTest::kSkiaPMTextureIdx],
137 GrEffectUnitTest::TestMatrix(random)));
140 ///////////////////////////////////////////////////////////////////////////////
141 void 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;
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]);
157 color[2] = SkTMin(x, y);
158 color[1] = SkTMin(x, y);
159 color[0] = SkTMin(x, y);
164 desc.fFlags = kRenderTarget_GrTextureFlagBit |
165 kNoStencil_GrTextureFlagBit;
168 desc.fConfig = kRGBA_8888_GrPixelConfig;
170 SkAutoTUnref<GrTexture> readTex(context->createUncachedTexture(desc, NULL, 0));
171 if (!readTex.get()) {
174 SkAutoTUnref<GrTexture> tempTex(context->createUncachedTexture(desc, NULL, 0));
175 if (!tempTex.get()) {
178 desc.fFlags = kNone_GrTextureFlags;
179 SkAutoTUnref<GrTexture> dataTex(context->createUncachedTexture(desc, data, 0));
180 if (!dataTex.get()) {
184 static const PMConversion kConversionRules[][2] = {
185 {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion},
186 {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion},
189 GrContext::AutoWideOpenIdentityDraw awoid(context, NULL);
193 for (size_t i = 0; i < SK_ARRAY_COUNT(kConversionRules) && failed; ++i) {
194 *pmToUPMRule = kConversionRules[i][0];
195 *upmToPMRule = kConversionRules[i][1];
197 static const SkRect kDstRect = SkRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256));
198 static const SkRect kSrcRect = SkRect::MakeWH(SK_Scalar1, SK_Scalar1);
199 // 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.
203 SkAutoTUnref<GrEffect> pmToUPM1(SkNEW_ARGS(GrConfigConversionEffect, (dataTex,
207 SkAutoTUnref<GrEffect> upmToPM(SkNEW_ARGS(GrConfigConversionEffect, (readTex,
211 SkAutoTUnref<GrEffect> pmToUPM2(SkNEW_ARGS(GrConfigConversionEffect, (tempTex,
216 context->setRenderTarget(readTex->asRenderTarget());
218 paint1.addColorEffect(pmToUPM1);
219 context->drawRectToRect(paint1, kDstRect, kSrcRect);
221 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
223 context->setRenderTarget(tempTex->asRenderTarget());
225 paint2.addColorEffect(upmToPM);
226 context->drawRectToRect(paint2, kDstRect, kSrcRect);
227 context->setRenderTarget(readTex->asRenderTarget());
230 paint3.addColorEffect(pmToUPM2);
231 context->drawRectToRect(paint3, kDstRect, kSrcRect);
233 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
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]) {
246 *pmToUPMRule = kNone_PMConversion;
247 *upmToPMRule = kNone_PMConversion;
251 const GrEffect* GrConfigConversionEffect::Create(GrTexture* texture,
253 PMConversion pmConversion,
254 const SkMatrix& matrix) {
255 if (!swapRedAndBlue && kNone_PMConversion == pmConversion) {
256 // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect
257 // then we may pollute our texture cache with redundant shaders. So in the case that no
258 // conversions were requested we instead return a GrSimpleTextureEffect.
259 return GrSimpleTextureEffect::Create(texture, matrix);
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
267 return SkNEW_ARGS(GrConfigConversionEffect, (texture,