Revert of Make GrGLConfigConversionEffect work for Imagination and some other GPUs...
[platform/upstream/libSkiaSharp.git] / src / gpu / effects / GrConfigConversionEffect.cpp
1 /*
2  * Copyright 2012 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7
8 #include "GrConfigConversionEffect.h"
9 #include "GrContext.h"
10 #include "GrTBackendEffectFactory.h"
11 #include "GrSimpleTextureEffect.h"
12 #include "gl/GrGLEffect.h"
13 #include "SkMatrix.h"
14
15 class GrGLConfigConversionEffect : public GrGLEffect {
16 public:
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();
23     }
24
25     virtual void emitCode(GrGLShaderBuilder* builder,
26                           const GrDrawEffect&,
27                           EffectKey key,
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);
38         } else {
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);
45                     break;
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);
54                     break;
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);
58                     break;
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);
62                     break;
63                 default:
64                     SkFAIL("Unknown conversion op.");
65                     break;
66             }
67         }
68         SkString modulate;
69         GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor);
70         builder->fsCodeAppend(modulate.c_str());
71     }
72
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);
76     }
77
78 private:
79     bool                                    fSwapRedAndBlue;
80     GrConfigConversionEffect::PMConversion  fPMConversion;
81
82     typedef GrGLEffect INHERITED;
83
84 };
85
86 ///////////////////////////////////////////////////////////////////////////////
87
88 GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture,
89                                                    bool swapRedAndBlue,
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);
99 }
100
101 const GrBackendEffectFactory& GrConfigConversionEffect::getFactory() const {
102     return GrTBackendEffectFactory<GrConfigConversionEffect>::getInstance();
103 }
104
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;
110 }
111
112 void GrConfigConversionEffect::getConstantColorComponents(GrColor* color,
113                                                           uint32_t* validFlags) const {
114     this->updateConstantColorComponentsForModulation(color, validFlags);
115 }
116
117 ///////////////////////////////////////////////////////////////////////////////
118
119 GR_DEFINE_EFFECT_TEST(GrConfigConversionEffect);
120
121 GrEffect* GrConfigConversionEffect::TestCreate(SkRandom* random,
122                                                GrContext*,
123                                                const GrDrawTargetCaps&,
124                                                GrTexture* textures[]) {
125     PMConversion pmConv = static_cast<PMConversion>(random->nextULessThan(kPMConversionCnt));
126     bool swapRB;
127     if (kNone_PMConversion == pmConv) {
128         swapRB = true;
129     } else {
130         swapRB = random->nextBool();
131     }
132     return SkNEW_ARGS(GrConfigConversionEffect,
133                                       (textures[GrEffectUnitTest::kSkiaPMTextureIdx],
134                                        swapRB,
135                                        pmConv,
136                                        GrEffectUnitTest::TestMatrix(random)));
137 }
138
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;
149
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]);
155             color[3] = y;
156             color[2] = SkTMin(x, y);
157             color[1] = SkTMin(x, y);
158             color[0] = SkTMin(x, y);
159         }
160     }
161
162     GrTextureDesc desc;
163     desc.fFlags = kRenderTarget_GrTextureFlagBit |
164                   kNoStencil_GrTextureFlagBit;
165     desc.fWidth = 256;
166     desc.fHeight = 256;
167     desc.fConfig = kRGBA_8888_GrPixelConfig;
168
169     SkAutoTUnref<GrTexture> readTex(context->createUncachedTexture(desc, NULL, 0));
170     if (!readTex.get()) {
171         return;
172     }
173     SkAutoTUnref<GrTexture> tempTex(context->createUncachedTexture(desc, NULL, 0));
174     if (!tempTex.get()) {
175         return;
176     }
177     desc.fFlags = kNone_GrTextureFlags;
178     SkAutoTUnref<GrTexture> dataTex(context->createUncachedTexture(desc, data, 0));
179     if (!dataTex.get()) {
180         return;
181     }
182
183     static const PMConversion kConversionRules[][2] = {
184         {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion},
185         {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion},
186     };
187
188     GrContext::AutoWideOpenIdentityDraw awoid(context, NULL);
189
190     bool failed = true;
191
192     for (size_t i = 0; i < SK_ARRAY_COUNT(kConversionRules) && failed; ++i) {
193         *pmToUPMRule = kConversionRules[i][0];
194         *upmToPMRule = kConversionRules[i][1];
195
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.
201
202         SkAutoTUnref<GrEffect> pmToUPM1(SkNEW_ARGS(GrConfigConversionEffect, (dataTex,
203                                                                               false,
204                                                                               *pmToUPMRule,
205                                                                               SkMatrix::I())));
206         SkAutoTUnref<GrEffect> upmToPM(SkNEW_ARGS(GrConfigConversionEffect, (readTex,
207                                                                              false,
208                                                                              *upmToPMRule,
209                                                                              SkMatrix::I())));
210         SkAutoTUnref<GrEffect> pmToUPM2(SkNEW_ARGS(GrConfigConversionEffect, (tempTex,
211                                                                               false,
212                                                                               *pmToUPMRule,
213                                                                               SkMatrix::I())));
214
215         context->setRenderTarget(readTex->asRenderTarget());
216         GrPaint paint1;
217         paint1.addColorEffect(pmToUPM1);
218         context->drawRectToRect(paint1, kDstRect, kSrcRect);
219
220         readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
221
222         context->setRenderTarget(tempTex->asRenderTarget());
223         GrPaint paint2;
224         paint2.addColorEffect(upmToPM);
225         context->drawRectToRect(paint2, kDstRect, kSrcRect);
226         context->setRenderTarget(readTex->asRenderTarget());
227
228         GrPaint paint3;
229         paint3.addColorEffect(pmToUPM2);
230         context->drawRectToRect(paint3, kDstRect, kSrcRect);
231
232         readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
233
234         failed = false;
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]) {
238                     failed = true;
239                     break;
240                 }
241             }
242         }
243     }
244     if (failed) {
245         *pmToUPMRule = kNone_PMConversion;
246         *upmToPMRule = kNone_PMConversion;
247     }
248 }
249
250 const GrEffect* GrConfigConversionEffect::Create(GrTexture* texture,
251                                                  bool swapRedAndBlue,
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);
259     } else {
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
264             return NULL;
265         }
266         return SkNEW_ARGS(GrConfigConversionEffect, (texture,
267                                                      swapRedAndBlue,
268                                                      pmConversion,
269                                                      matrix));
270     }
271 }