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,
28 const GrEffectKey& key,
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 void GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&,
75 GrEffectKeyBuilder* b) {
76 const GrConfigConversionEffect& conv = drawEffect.castEffect<GrConfigConversionEffect>();
77 uint32_t key = (conv.swapsRedAndBlue() ? 0 : 1) | (conv.pmConversion() << 1);
83 GrConfigConversionEffect::PMConversion fPMConversion;
85 typedef GrGLEffect INHERITED;
89 ///////////////////////////////////////////////////////////////////////////////
91 GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture,
93 PMConversion pmConversion,
94 const SkMatrix& matrix)
95 : GrSingleTextureEffect(texture, matrix)
96 , fSwapRedAndBlue(swapRedAndBlue)
97 , fPMConversion(pmConversion) {
98 SkASSERT(kRGBA_8888_GrPixelConfig == texture->config() ||
99 kBGRA_8888_GrPixelConfig == texture->config());
100 // Why did we pollute our texture cache instead of using a GrSingleTextureEffect?
101 SkASSERT(swapRedAndBlue || kNone_PMConversion != pmConversion);
104 const GrBackendEffectFactory& GrConfigConversionEffect::getFactory() const {
105 return GrTBackendEffectFactory<GrConfigConversionEffect>::getInstance();
108 bool GrConfigConversionEffect::onIsEqual(const GrEffect& s) const {
109 const GrConfigConversionEffect& other = CastEffect<GrConfigConversionEffect>(s);
110 return this->texture(0) == s.texture(0) &&
111 other.fSwapRedAndBlue == fSwapRedAndBlue &&
112 other.fPMConversion == fPMConversion;
115 void GrConfigConversionEffect::getConstantColorComponents(GrColor* color,
116 uint32_t* validFlags) const {
117 this->updateConstantColorComponentsForModulation(color, validFlags);
120 ///////////////////////////////////////////////////////////////////////////////
122 GR_DEFINE_EFFECT_TEST(GrConfigConversionEffect);
124 GrEffect* GrConfigConversionEffect::TestCreate(SkRandom* random,
126 const GrDrawTargetCaps&,
127 GrTexture* textures[]) {
128 PMConversion pmConv = static_cast<PMConversion>(random->nextULessThan(kPMConversionCnt));
130 if (kNone_PMConversion == pmConv) {
133 swapRB = random->nextBool();
135 return SkNEW_ARGS(GrConfigConversionEffect,
136 (textures[GrEffectUnitTest::kSkiaPMTextureIdx],
139 GrEffectUnitTest::TestMatrix(random)));
142 ///////////////////////////////////////////////////////////////////////////////
143 void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context,
144 PMConversion* pmToUPMRule,
145 PMConversion* upmToPMRule) {
146 *pmToUPMRule = kNone_PMConversion;
147 *upmToPMRule = kNone_PMConversion;
148 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
149 uint32_t* srcData = data.get();
150 uint32_t* firstRead = data.get() + 256 * 256;
151 uint32_t* secondRead = data.get() + 2 * 256 * 256;
153 // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate
154 // values in row y. We set r,g, and b to the same value since they are handled identically.
155 for (int y = 0; y < 256; ++y) {
156 for (int x = 0; x < 256; ++x) {
157 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
159 color[2] = SkTMin(x, y);
160 color[1] = SkTMin(x, y);
161 color[0] = SkTMin(x, y);
166 desc.fFlags = kRenderTarget_GrTextureFlagBit |
167 kNoStencil_GrTextureFlagBit;
170 desc.fConfig = kRGBA_8888_GrPixelConfig;
172 SkAutoTUnref<GrTexture> readTex(context->createUncachedTexture(desc, NULL, 0));
173 if (!readTex.get()) {
176 SkAutoTUnref<GrTexture> tempTex(context->createUncachedTexture(desc, NULL, 0));
177 if (!tempTex.get()) {
180 desc.fFlags = kNone_GrTextureFlags;
181 SkAutoTUnref<GrTexture> dataTex(context->createUncachedTexture(desc, data, 0));
182 if (!dataTex.get()) {
186 static const PMConversion kConversionRules[][2] = {
187 {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion},
188 {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion},
191 GrContext::AutoWideOpenIdentityDraw awoid(context, NULL);
195 for (size_t i = 0; i < SK_ARRAY_COUNT(kConversionRules) && failed; ++i) {
196 *pmToUPMRule = kConversionRules[i][0];
197 *upmToPMRule = kConversionRules[i][1];
199 static const SkRect kDstRect = SkRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256));
200 static const SkRect kSrcRect = SkRect::MakeWH(SK_Scalar1, SK_Scalar1);
201 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
202 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
203 // We then verify that two reads produced the same values.
205 SkAutoTUnref<GrEffect> pmToUPM1(SkNEW_ARGS(GrConfigConversionEffect, (dataTex,
209 SkAutoTUnref<GrEffect> upmToPM(SkNEW_ARGS(GrConfigConversionEffect, (readTex,
213 SkAutoTUnref<GrEffect> pmToUPM2(SkNEW_ARGS(GrConfigConversionEffect, (tempTex,
218 context->setRenderTarget(readTex->asRenderTarget());
220 paint1.addColorEffect(pmToUPM1);
221 context->drawRectToRect(paint1, kDstRect, kSrcRect);
223 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
225 context->setRenderTarget(tempTex->asRenderTarget());
227 paint2.addColorEffect(upmToPM);
228 context->drawRectToRect(paint2, kDstRect, kSrcRect);
229 context->setRenderTarget(readTex->asRenderTarget());
232 paint3.addColorEffect(pmToUPM2);
233 context->drawRectToRect(paint3, kDstRect, kSrcRect);
235 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
238 for (int y = 0; y < 256 && !failed; ++y) {
239 for (int x = 0; x <= y; ++x) {
240 if (firstRead[256 * y + x] != secondRead[256 * y + x]) {
248 *pmToUPMRule = kNone_PMConversion;
249 *upmToPMRule = kNone_PMConversion;
253 const GrEffect* GrConfigConversionEffect::Create(GrTexture* texture,
255 PMConversion pmConversion,
256 const SkMatrix& matrix) {
257 if (!swapRedAndBlue && kNone_PMConversion == pmConversion) {
258 // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect
259 // then we may pollute our texture cache with redundant shaders. So in the case that no
260 // conversions were requested we instead return a GrSimpleTextureEffect.
261 return GrSimpleTextureEffect::Create(texture, matrix);
263 if (kRGBA_8888_GrPixelConfig != texture->config() &&
264 kBGRA_8888_GrPixelConfig != texture->config() &&
265 kNone_PMConversion != pmConversion) {
266 // The PM conversions assume colors are 0..255
269 return SkNEW_ARGS(GrConfigConversionEffect, (texture,