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             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);
46                     break;
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);
55                     break;
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);
59                     break;
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);
63                     break;
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);
71                     break;
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);
79                     break;
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);
86                     break;
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);
93                     break;
94                 default:
95                     SkFAIL("Unknown conversion op.");
96                     break;
97             }
98         }
99         SkString modulate;
100         GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor);
101         builder->fsCodeAppend(modulate.c_str());
102     }
103
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);
107     }
108
109 private:
110     bool                                    fSwapRedAndBlue;
111     GrConfigConversionEffect::PMConversion  fPMConversion;
112
113     typedef GrGLEffect INHERITED;
114
115 };
116
117 ///////////////////////////////////////////////////////////////////////////////
118
119 GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture,
120                                                    bool swapRedAndBlue,
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);
130 }
131
132 const GrBackendEffectFactory& GrConfigConversionEffect::getFactory() const {
133     return GrTBackendEffectFactory<GrConfigConversionEffect>::getInstance();
134 }
135
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;
141 }
142
143 void GrConfigConversionEffect::getConstantColorComponents(GrColor* color,
144                                                           uint32_t* validFlags) const {
145     this->updateConstantColorComponentsForModulation(color, validFlags);
146 }
147
148 ///////////////////////////////////////////////////////////////////////////////
149
150 GR_DEFINE_EFFECT_TEST(GrConfigConversionEffect);
151
152 GrEffect* GrConfigConversionEffect::TestCreate(SkRandom* random,
153                                                GrContext*,
154                                                const GrDrawTargetCaps&,
155                                                GrTexture* textures[]) {
156     PMConversion pmConv = static_cast<PMConversion>(random->nextULessThan(kPMConversionCnt));
157     bool swapRB;
158     if (kNone_PMConversion == pmConv) {
159         swapRB = true;
160     } else {
161         swapRB = random->nextBool();
162     }
163     return SkNEW_ARGS(GrConfigConversionEffect,
164                                       (textures[GrEffectUnitTest::kSkiaPMTextureIdx],
165                                        swapRB,
166                                        pmConv,
167                                        GrEffectUnitTest::TestMatrix(random)));
168 }
169
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;
180
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]);
186             color[3] = y;
187             color[2] = SkTMin(x, y);
188             color[1] = SkTMin(x, y);
189             color[0] = SkTMin(x, y);
190         }
191     }
192
193     GrTextureDesc desc;
194     desc.fFlags = kRenderTarget_GrTextureFlagBit |
195                   kNoStencil_GrTextureFlagBit;
196     desc.fWidth = 256;
197     desc.fHeight = 256;
198     desc.fConfig = kRGBA_8888_GrPixelConfig;
199
200     SkAutoTUnref<GrTexture> readTex(context->createUncachedTexture(desc, NULL, 0));
201     if (!readTex.get()) {
202         return;
203     }
204     SkAutoTUnref<GrTexture> tempTex(context->createUncachedTexture(desc, NULL, 0));
205     if (!tempTex.get()) {
206         return;
207     }
208     desc.fFlags = kNone_GrTextureFlags;
209     SkAutoTUnref<GrTexture> dataTex(context->createUncachedTexture(desc, data, 0));
210     if (!dataTex.get()) {
211         return;
212     }
213
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},
219     };
220
221     GrContext::AutoWideOpenIdentityDraw awoid(context, NULL);
222
223     bool failed = true;
224
225     for (size_t i = 0; i < SK_ARRAY_COUNT(kConversionRules) && failed; ++i) {
226         *pmToUPMRule = kConversionRules[i][0];
227         *upmToPMRule = kConversionRules[i][1];
228
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.
234
235         SkAutoTUnref<GrEffect> pmToUPM1(SkNEW_ARGS(GrConfigConversionEffect, (dataTex,
236                                                                               false,
237                                                                               *pmToUPMRule,
238                                                                               SkMatrix::I())));
239         SkAutoTUnref<GrEffect> upmToPM(SkNEW_ARGS(GrConfigConversionEffect, (readTex,
240                                                                              false,
241                                                                              *upmToPMRule,
242                                                                              SkMatrix::I())));
243         SkAutoTUnref<GrEffect> pmToUPM2(SkNEW_ARGS(GrConfigConversionEffect, (tempTex,
244                                                                               false,
245                                                                               *pmToUPMRule,
246                                                                               SkMatrix::I())));
247
248         context->setRenderTarget(readTex->asRenderTarget());
249         GrPaint paint1;
250         paint1.addColorEffect(pmToUPM1);
251         context->drawRectToRect(paint1, kDstRect, kSrcRect);
252
253         readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
254
255         context->setRenderTarget(tempTex->asRenderTarget());
256         GrPaint paint2;
257         paint2.addColorEffect(upmToPM);
258         context->drawRectToRect(paint2, kDstRect, kSrcRect);
259         context->setRenderTarget(readTex->asRenderTarget());
260
261         GrPaint paint3;
262         paint3.addColorEffect(pmToUPM2);
263         context->drawRectToRect(paint3, kDstRect, kSrcRect);
264
265         readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
266
267         failed = false;
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]) {
271                     failed = true;
272                     break;
273                 }
274             }
275         }
276     }
277     if (failed) {
278         *pmToUPMRule = kNone_PMConversion;
279         *upmToPMRule = kNone_PMConversion;
280     }
281 }
282
283 const GrEffect* GrConfigConversionEffect::Create(GrTexture* texture,
284                                                  bool swapRedAndBlue,
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);
292     } else {
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
297             return NULL;
298         }
299         return SkNEW_ARGS(GrConfigConversionEffect, (texture,
300                                                      swapRedAndBlue,
301                                                      pmConversion,
302                                                      matrix));
303     }
304 }