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 const char* outputColorH = "outputColorH";
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);
64 case GrConfigConversionEffect::kMulByAlpha_RoundUp_HIGHP_PMConversion:
65 builder->fsCodeAppendf("\thighp vec4 %s;\n", outputColorH);
66 builder->fsCodeAppendf("\t\t%s = %s;\n", outputColorH, outputColor);
67 builder->fsCodeAppendf(
68 "\t\t%s = vec4(ceil(%s.%s * %s.a * 255.0) / 255.0, %s.a);\n",
69 outputColorH, outputColorH, swiz, outputColorH, outputColorH);
70 builder->fsCodeAppendf("\t\t%s = %s;\n", outputColor, outputColorH);
72 case GrConfigConversionEffect::kMulByAlpha_RoundDown_HIGHP_PMConversion:
73 builder->fsCodeAppendf("\thighp vec4 %s;\n", outputColorH);
74 builder->fsCodeAppendf("\t\t%s = %s;\n", outputColorH, outputColor);
75 builder->fsCodeAppendf(
76 "\t\t%s = vec4(floor(%s.%s * %s.a * 255.0) / 255.0, %s.a);\n",
77 outputColorH, outputColorH, swiz, outputColorH, outputColorH);
78 builder->fsCodeAppendf("\t\t%s = %s;\n", outputColor, outputColorH);
80 case GrConfigConversionEffect::kDivByAlpha_RoundUp_HIGHP_PMConversion:
81 builder->fsCodeAppendf("\thighp vec4 %s;\n", outputColorH);
82 builder->fsCodeAppendf("\t\t%s = %s;\n", outputColorH, outputColor);
83 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",
84 outputColorH, outputColorH, outputColorH, swiz, outputColorH, outputColorH);
85 builder->fsCodeAppendf("\t\t%s = %s;\n", outputColor, outputColorH);
87 case GrConfigConversionEffect::kDivByAlpha_RoundDown_HIGHP_PMConversion:
88 builder->fsCodeAppendf("\thighp vec4 %s;\n", outputColorH);
89 builder->fsCodeAppendf("\t\t%s = %s;\n", outputColorH, outputColor);
90 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",
91 outputColorH, outputColorH, outputColorH, swiz, outputColorH, outputColorH);
92 builder->fsCodeAppendf("\t\t%s = %s;\n", outputColor, outputColorH);
95 SkFAIL("Unknown conversion op.");
100 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor);
101 builder->fsCodeAppend(modulate.c_str());
104 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
105 const GrConfigConversionEffect& conv = drawEffect.castEffect<GrConfigConversionEffect>();
106 return static_cast<EffectKey>(conv.swapsRedAndBlue()) | (conv.pmConversion() << 1);
110 bool fSwapRedAndBlue;
111 GrConfigConversionEffect::PMConversion fPMConversion;
113 typedef GrGLEffect INHERITED;
117 ///////////////////////////////////////////////////////////////////////////////
119 GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture,
121 PMConversion pmConversion,
122 const SkMatrix& matrix)
123 : GrSingleTextureEffect(texture, matrix)
124 , fSwapRedAndBlue(swapRedAndBlue)
125 , fPMConversion(pmConversion) {
126 SkASSERT(kRGBA_8888_GrPixelConfig == texture->config() ||
127 kBGRA_8888_GrPixelConfig == texture->config());
128 // Why did we pollute our texture cache instead of using a GrSingleTextureEffect?
129 SkASSERT(swapRedAndBlue || kNone_PMConversion != pmConversion);
132 const GrBackendEffectFactory& GrConfigConversionEffect::getFactory() const {
133 return GrTBackendEffectFactory<GrConfigConversionEffect>::getInstance();
136 bool GrConfigConversionEffect::onIsEqual(const GrEffect& s) const {
137 const GrConfigConversionEffect& other = CastEffect<GrConfigConversionEffect>(s);
138 return this->texture(0) == s.texture(0) &&
139 other.fSwapRedAndBlue == fSwapRedAndBlue &&
140 other.fPMConversion == fPMConversion;
143 void GrConfigConversionEffect::getConstantColorComponents(GrColor* color,
144 uint32_t* validFlags) const {
145 this->updateConstantColorComponentsForModulation(color, validFlags);
148 ///////////////////////////////////////////////////////////////////////////////
150 GR_DEFINE_EFFECT_TEST(GrConfigConversionEffect);
152 GrEffect* GrConfigConversionEffect::TestCreate(SkRandom* random,
154 const GrDrawTargetCaps&,
155 GrTexture* textures[]) {
156 PMConversion pmConv = static_cast<PMConversion>(random->nextULessThan(kPMConversionCnt));
158 if (kNone_PMConversion == pmConv) {
161 swapRB = random->nextBool();
163 return SkNEW_ARGS(GrConfigConversionEffect,
164 (textures[GrEffectUnitTest::kSkiaPMTextureIdx],
167 GrEffectUnitTest::TestMatrix(random)));
170 ///////////////////////////////////////////////////////////////////////////////
171 void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context,
172 PMConversion* pmToUPMRule,
173 PMConversion* upmToPMRule) {
174 *pmToUPMRule = kNone_PMConversion;
175 *upmToPMRule = kNone_PMConversion;
176 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
177 uint32_t* srcData = data.get();
178 uint32_t* firstRead = data.get() + 256 * 256;
179 uint32_t* secondRead = data.get() + 2 * 256 * 256;
181 // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate
182 // values in row y. We set r,g, and b to the same value since they are handled identically.
183 for (int y = 0; y < 256; ++y) {
184 for (int x = 0; x < 256; ++x) {
185 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
187 color[2] = SkTMin(x, y);
188 color[1] = SkTMin(x, y);
189 color[0] = SkTMin(x, y);
194 desc.fFlags = kRenderTarget_GrTextureFlagBit |
195 kNoStencil_GrTextureFlagBit;
198 desc.fConfig = kRGBA_8888_GrPixelConfig;
200 SkAutoTUnref<GrTexture> readTex(context->createUncachedTexture(desc, NULL, 0));
201 if (!readTex.get()) {
204 SkAutoTUnref<GrTexture> tempTex(context->createUncachedTexture(desc, NULL, 0));
205 if (!tempTex.get()) {
208 desc.fFlags = kNone_GrTextureFlags;
209 SkAutoTUnref<GrTexture> dataTex(context->createUncachedTexture(desc, data, 0));
210 if (!dataTex.get()) {
214 static const PMConversion kConversionRules[][2] = {
215 {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion},
216 {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion},
217 {kDivByAlpha_RoundDown_HIGHP_PMConversion, kMulByAlpha_RoundUp_HIGHP_PMConversion},
218 {kDivByAlpha_RoundUp_HIGHP_PMConversion, kMulByAlpha_RoundDown_HIGHP_PMConversion},
221 GrContext::AutoWideOpenIdentityDraw awoid(context, NULL);
225 for (size_t i = 0; i < SK_ARRAY_COUNT(kConversionRules) && failed; ++i) {
226 *pmToUPMRule = kConversionRules[i][0];
227 *upmToPMRule = kConversionRules[i][1];
229 static const SkRect kDstRect = SkRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256));
230 static const SkRect kSrcRect = SkRect::MakeWH(SK_Scalar1, SK_Scalar1);
231 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
232 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
233 // We then verify that two reads produced the same values.
235 SkAutoTUnref<GrEffect> pmToUPM1(SkNEW_ARGS(GrConfigConversionEffect, (dataTex,
239 SkAutoTUnref<GrEffect> upmToPM(SkNEW_ARGS(GrConfigConversionEffect, (readTex,
243 SkAutoTUnref<GrEffect> pmToUPM2(SkNEW_ARGS(GrConfigConversionEffect, (tempTex,
248 context->setRenderTarget(readTex->asRenderTarget());
250 paint1.addColorEffect(pmToUPM1);
251 context->drawRectToRect(paint1, kDstRect, kSrcRect);
253 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
255 context->setRenderTarget(tempTex->asRenderTarget());
257 paint2.addColorEffect(upmToPM);
258 context->drawRectToRect(paint2, kDstRect, kSrcRect);
259 context->setRenderTarget(readTex->asRenderTarget());
262 paint3.addColorEffect(pmToUPM2);
263 context->drawRectToRect(paint3, kDstRect, kSrcRect);
265 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
268 for (int y = 0; y < 256 && !failed; ++y) {
269 for (int x = 0; x <= y; ++x) {
270 if (firstRead[256 * y + x] != secondRead[256 * y + x]) {
278 *pmToUPMRule = kNone_PMConversion;
279 *upmToPMRule = kNone_PMConversion;
283 const GrEffect* GrConfigConversionEffect::Create(GrTexture* texture,
285 PMConversion pmConversion,
286 const SkMatrix& matrix) {
287 if (!swapRedAndBlue && kNone_PMConversion == pmConversion) {
288 // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect
289 // then we may pollute our texture cache with redundant shaders. So in the case that no
290 // conversions were requested we instead return a GrSimpleTextureEffect.
291 return GrSimpleTextureEffect::Create(texture, matrix);
293 if (kRGBA_8888_GrPixelConfig != texture->config() &&
294 kBGRA_8888_GrPixelConfig != texture->config() &&
295 kNone_PMConversion != pmConversion) {
296 // The PM conversions assume colors are 0..255
299 return SkNEW_ARGS(GrConfigConversionEffect, (texture,