Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / 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 "gl/GrGLShaderBuilder.h"
14 #include "SkMatrix.h"
15
16 class GrGLConfigConversionEffect : public GrGLEffect {
17 public:
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();
24     }
25
26     virtual void emitCode(GrGLShaderBuilder* builder,
27                           const GrDrawEffect&,
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);
39         } else {
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);
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                 default:
65                     SkFAIL("Unknown conversion op.");
66                     break;
67             }
68         }
69         SkString modulate;
70         GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor);
71         builder->fsCodeAppend(modulate.c_str());
72     }
73
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);
78         b->add32(key);
79     }
80
81 private:
82     bool                                    fSwapRedAndBlue;
83     GrConfigConversionEffect::PMConversion  fPMConversion;
84
85     typedef GrGLEffect INHERITED;
86
87 };
88
89 ///////////////////////////////////////////////////////////////////////////////
90
91 GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture,
92                                                    bool swapRedAndBlue,
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);
102 }
103
104 const GrBackendEffectFactory& GrConfigConversionEffect::getFactory() const {
105     return GrTBackendEffectFactory<GrConfigConversionEffect>::getInstance();
106 }
107
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;
113 }
114
115 void GrConfigConversionEffect::getConstantColorComponents(GrColor* color,
116                                                           uint32_t* validFlags) const {
117     this->updateConstantColorComponentsForModulation(color, validFlags);
118 }
119
120 ///////////////////////////////////////////////////////////////////////////////
121
122 GR_DEFINE_EFFECT_TEST(GrConfigConversionEffect);
123
124 GrEffect* GrConfigConversionEffect::TestCreate(SkRandom* random,
125                                                GrContext*,
126                                                const GrDrawTargetCaps&,
127                                                GrTexture* textures[]) {
128     PMConversion pmConv = static_cast<PMConversion>(random->nextULessThan(kPMConversionCnt));
129     bool swapRB;
130     if (kNone_PMConversion == pmConv) {
131         swapRB = true;
132     } else {
133         swapRB = random->nextBool();
134     }
135     return SkNEW_ARGS(GrConfigConversionEffect,
136                                       (textures[GrEffectUnitTest::kSkiaPMTextureIdx],
137                                        swapRB,
138                                        pmConv,
139                                        GrEffectUnitTest::TestMatrix(random)));
140 }
141
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;
152
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]);
158             color[3] = y;
159             color[2] = SkTMin(x, y);
160             color[1] = SkTMin(x, y);
161             color[0] = SkTMin(x, y);
162         }
163     }
164
165     GrTextureDesc desc;
166     desc.fFlags = kRenderTarget_GrTextureFlagBit |
167                   kNoStencil_GrTextureFlagBit;
168     desc.fWidth = 256;
169     desc.fHeight = 256;
170     desc.fConfig = kRGBA_8888_GrPixelConfig;
171
172     SkAutoTUnref<GrTexture> readTex(context->createUncachedTexture(desc, NULL, 0));
173     if (!readTex.get()) {
174         return;
175     }
176     SkAutoTUnref<GrTexture> tempTex(context->createUncachedTexture(desc, NULL, 0));
177     if (!tempTex.get()) {
178         return;
179     }
180     desc.fFlags = kNone_GrTextureFlags;
181     SkAutoTUnref<GrTexture> dataTex(context->createUncachedTexture(desc, data, 0));
182     if (!dataTex.get()) {
183         return;
184     }
185
186     static const PMConversion kConversionRules[][2] = {
187         {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion},
188         {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion},
189     };
190
191     GrContext::AutoWideOpenIdentityDraw awoid(context, NULL);
192
193     bool failed = true;
194
195     for (size_t i = 0; i < SK_ARRAY_COUNT(kConversionRules) && failed; ++i) {
196         *pmToUPMRule = kConversionRules[i][0];
197         *upmToPMRule = kConversionRules[i][1];
198
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.
204
205         SkAutoTUnref<GrEffect> pmToUPM1(SkNEW_ARGS(GrConfigConversionEffect, (dataTex,
206                                                                               false,
207                                                                               *pmToUPMRule,
208                                                                               SkMatrix::I())));
209         SkAutoTUnref<GrEffect> upmToPM(SkNEW_ARGS(GrConfigConversionEffect, (readTex,
210                                                                              false,
211                                                                              *upmToPMRule,
212                                                                              SkMatrix::I())));
213         SkAutoTUnref<GrEffect> pmToUPM2(SkNEW_ARGS(GrConfigConversionEffect, (tempTex,
214                                                                               false,
215                                                                               *pmToUPMRule,
216                                                                               SkMatrix::I())));
217
218         context->setRenderTarget(readTex->asRenderTarget());
219         GrPaint paint1;
220         paint1.addColorEffect(pmToUPM1);
221         context->drawRectToRect(paint1, kDstRect, kSrcRect);
222
223         readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
224
225         context->setRenderTarget(tempTex->asRenderTarget());
226         GrPaint paint2;
227         paint2.addColorEffect(upmToPM);
228         context->drawRectToRect(paint2, kDstRect, kSrcRect);
229         context->setRenderTarget(readTex->asRenderTarget());
230
231         GrPaint paint3;
232         paint3.addColorEffect(pmToUPM2);
233         context->drawRectToRect(paint3, kDstRect, kSrcRect);
234
235         readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
236
237         failed = false;
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]) {
241                     failed = true;
242                     break;
243                 }
244             }
245         }
246     }
247     if (failed) {
248         *pmToUPMRule = kNone_PMConversion;
249         *upmToPMRule = kNone_PMConversion;
250     }
251 }
252
253 const GrEffect* GrConfigConversionEffect::Create(GrTexture* texture,
254                                                  bool swapRedAndBlue,
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);
262     } else {
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
267             return NULL;
268         }
269         return SkNEW_ARGS(GrConfigConversionEffect, (texture,
270                                                      swapRedAndBlue,
271                                                      pmConversion,
272                                                      matrix));
273     }
274 }