Work around VS 2015 Update 3 optimizer internal compiler error
[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 "GrDrawContext.h"
11 #include "GrInvariantOutput.h"
12 #include "GrSimpleTextureEffect.h"
13 #include "SkMatrix.h"
14 #include "glsl/GrGLSLFragmentProcessor.h"
15 #include "glsl/GrGLSLFragmentShaderBuilder.h"
16
17 class GrGLConfigConversionEffect : public GrGLSLFragmentProcessor {
18 public:
19     void emitCode(EmitArgs& args) override {
20         const GrConfigConversionEffect& cce = args.fFp.cast<GrConfigConversionEffect>();
21         const GrSwizzle& swizzle = cce.swizzle();
22         GrConfigConversionEffect::PMConversion pmConversion = cce.pmConversion();
23
24         // Using highp for GLES here in order to avoid some precision issues on specific GPUs.
25         GrGLSLShaderVar tmpVar("tmpColor", kVec4f_GrSLType, 0, kHigh_GrSLPrecision);
26         SkString tmpDecl;
27         tmpVar.appendDecl(args.fGLSLCaps, &tmpDecl);
28
29         GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
30
31         fragBuilder->codeAppendf("%s;", tmpDecl.c_str());
32
33         fragBuilder->codeAppendf("%s = ", tmpVar.c_str());
34         fragBuilder->appendTextureLookup(args.fTexSamplers[0], args.fCoords[0].c_str(),
35                                        args.fCoords[0].getType());
36         fragBuilder->codeAppend(";");
37
38         if (GrConfigConversionEffect::kNone_PMConversion == pmConversion) {
39             SkASSERT(GrSwizzle::RGBA() != swizzle);
40             fragBuilder->codeAppendf("%s = %s.%s;", args.fOutputColor, tmpVar.c_str(),
41                                      swizzle.c_str());
42         } else {
43             switch (pmConversion) {
44                 case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion:
45                     fragBuilder->codeAppendf(
46                         "%s = vec4(ceil(%s.rgb * %s.a * 255.0) / 255.0, %s.a);",
47                         tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str());
48                     break;
49                 case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion:
50                     // Add a compensation(0.001) here to avoid the side effect of the floor operation.
51                     // In Intel GPUs, the integer value converted from floor(%s.r * 255.0) / 255.0
52                     // is less than the integer value converted from  %s.r by 1 when the %s.r is
53                     // converted from the integer value 2^n, such as 1, 2, 4, 8, etc.
54                     fragBuilder->codeAppendf(
55                         "%s = vec4(floor(%s.rgb * %s.a * 255.0 + 0.001) / 255.0, %s.a);",
56                         tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str());
57
58                     break;
59                 case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion:
60                     fragBuilder->codeAppendf(
61                         "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(ceil(%s.rgb / %s.a * 255.0) / 255.0, %s.a);",
62                         tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(),
63                         tmpVar.c_str());
64                     break;
65                 case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion:
66                     fragBuilder->codeAppendf(
67                         "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(floor(%s.rgb / %s.a * 255.0) / 255.0, %s.a);",
68                         tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(),
69                         tmpVar.c_str());
70                     break;
71                 default:
72                     SkFAIL("Unknown conversion op.");
73                     break;
74             }
75             fragBuilder->codeAppendf("%s = %s.%s;", args.fOutputColor, tmpVar.c_str(),
76                                      swizzle.c_str());
77         }
78         SkString modulate;
79         GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor);
80         fragBuilder->codeAppend(modulate.c_str());
81     }
82
83     static inline void GenKey(const GrProcessor& processor, const GrGLSLCaps&,
84                               GrProcessorKeyBuilder* b) {
85         const GrConfigConversionEffect& cce = processor.cast<GrConfigConversionEffect>();
86         uint32_t key = (cce.swizzle().asKey()) | (cce.pmConversion() << 16);
87         b->add32(key);
88     }
89
90 private:
91     typedef GrGLSLFragmentProcessor INHERITED;
92
93 };
94
95 ///////////////////////////////////////////////////////////////////////////////
96
97 GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture,
98                                                    const GrSwizzle& swizzle,
99                                                    PMConversion pmConversion,
100                                                    const SkMatrix& matrix)
101     : INHERITED(texture, nullptr, matrix)
102     , fSwizzle(swizzle)
103     , fPMConversion(pmConversion) {
104     this->initClassID<GrConfigConversionEffect>();
105     // We expect to get here with non-BGRA/RGBA only if we're doing not doing a premul/unpremul
106     // conversion.
107     SkASSERT((kRGBA_8888_GrPixelConfig == texture->config() ||
108               kBGRA_8888_GrPixelConfig == texture->config()) ||
109               kNone_PMConversion == pmConversion);
110     // Why did we pollute our texture cache instead of using a GrSingleTextureEffect?
111     SkASSERT(swizzle != GrSwizzle::RGBA() || kNone_PMConversion != pmConversion);
112 }
113
114 bool GrConfigConversionEffect::onIsEqual(const GrFragmentProcessor& s) const {
115     const GrConfigConversionEffect& other = s.cast<GrConfigConversionEffect>();
116     return other.fSwizzle == fSwizzle &&
117            other.fPMConversion == fPMConversion;
118 }
119
120 void GrConfigConversionEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
121     this->updateInvariantOutputForModulation(inout);
122 }
123
124 ///////////////////////////////////////////////////////////////////////////////
125
126 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConfigConversionEffect);
127
128 #if !defined(__clang__) && _MSC_FULL_VER >= 190024213
129 // Work around VS 2015 Update 3 optimizer bug that causes internal compiler error
130 //https://connect.microsoft.com/VisualStudio/feedback/details/3100520/internal-compiler-error
131 #pragma optimize("t", off)
132 #endif
133
134 sk_sp<GrFragmentProcessor> GrConfigConversionEffect::TestCreate(GrProcessorTestData* d) {
135     PMConversion pmConv = static_cast<PMConversion>(d->fRandom->nextULessThan(kPMConversionCnt));
136     GrSwizzle swizzle;
137     do {
138         swizzle = GrSwizzle::CreateRandom(d->fRandom);
139     } while (pmConv == kNone_PMConversion && swizzle == GrSwizzle::RGBA());
140     return sk_sp<GrFragmentProcessor>(
141         new GrConfigConversionEffect(d->fTextures[GrProcessorUnitTest::kSkiaPMTextureIdx],
142                                      swizzle, pmConv, GrTest::TestMatrix(d->fRandom)));
143 }
144
145 #if !defined(__clang__) && _MSC_FULL_VER >= 190024213
146 // Restore optimization settings.
147 #pragma optimize("", on)
148 #endif
149
150 ///////////////////////////////////////////////////////////////////////////////
151
152 void GrConfigConversionEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps,
153                                                      GrProcessorKeyBuilder* b) const {
154     GrGLConfigConversionEffect::GenKey(*this, caps, b);
155 }
156
157 GrGLSLFragmentProcessor* GrConfigConversionEffect::onCreateGLSLInstance() const {
158     return new GrGLConfigConversionEffect();
159 }
160
161
162
163 void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context,
164                                                               PMConversion* pmToUPMRule,
165                                                               PMConversion* upmToPMRule) {
166     *pmToUPMRule = kNone_PMConversion;
167     *upmToPMRule = kNone_PMConversion;
168     static constexpr int kSize = 256;
169     static constexpr GrPixelConfig kConfig = kRGBA_8888_GrPixelConfig;
170     SkAutoTMalloc<uint32_t> data(kSize * kSize * 3);
171     uint32_t* srcData = data.get();
172     uint32_t* firstRead = data.get() + kSize * kSize;
173     uint32_t* secondRead = data.get() + 2 * kSize * kSize;
174
175     // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate
176     // values in row y. We set r,g, and b to the same value since they are handled identically.
177     for (int y = 0; y < kSize; ++y) {
178         for (int x = 0; x < kSize; ++x) {
179             uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[kSize*y + x]);
180             color[3] = y;
181             color[2] = SkTMin(x, y);
182             color[1] = SkTMin(x, y);
183             color[0] = SkTMin(x, y);
184         }
185     }
186
187     sk_sp<GrDrawContext> readDC(context->makeDrawContext(SkBackingFit::kExact, kSize, kSize,
188                                                          kConfig, nullptr));
189     sk_sp<GrDrawContext> tempDC(context->makeDrawContext(SkBackingFit::kExact, kSize, kSize,
190                                                          kConfig, nullptr));
191     if (!readDC || !tempDC) {
192         return;
193     }
194     GrSurfaceDesc desc;
195     desc.fWidth = kSize;
196     desc.fHeight = kSize;
197     desc.fConfig = kConfig;
198     SkAutoTUnref<GrTexture> dataTex(context->textureProvider()->createTexture(
199         desc, SkBudgeted::kYes, data, 0));
200     if (!dataTex.get()) {
201         return;
202     }
203
204     static const PMConversion kConversionRules[][2] = {
205         {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion},
206         {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion},
207     };
208
209     bool failed = true;
210
211     for (size_t i = 0; i < SK_ARRAY_COUNT(kConversionRules) && failed; ++i) {
212         *pmToUPMRule = kConversionRules[i][0];
213         *upmToPMRule = kConversionRules[i][1];
214
215         static const SkRect kDstRect = SkRect::MakeIWH(kSize, kSize);
216         static const SkRect kSrcRect = SkRect::MakeIWH(1, 1);
217         // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
218         // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
219         // We then verify that two reads produced the same values.
220
221         GrPaint paint1;
222         GrPaint paint2;
223         GrPaint paint3;
224         sk_sp<GrFragmentProcessor> pmToUPM1(new GrConfigConversionEffect(
225                 dataTex, GrSwizzle::RGBA(), *pmToUPMRule, SkMatrix::I()));
226         sk_sp<GrFragmentProcessor> upmToPM(new GrConfigConversionEffect(
227                 readDC->asTexture().get(), GrSwizzle::RGBA(), *upmToPMRule, SkMatrix::I()));
228         sk_sp<GrFragmentProcessor> pmToUPM2(new GrConfigConversionEffect(
229                 tempDC->asTexture().get(), GrSwizzle::RGBA(), *pmToUPMRule, SkMatrix::I()));
230
231         paint1.addColorFragmentProcessor(std::move(pmToUPM1));
232         paint1.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
233
234         readDC->fillRectToRect(GrNoClip(), paint1, SkMatrix::I(), kDstRect, kSrcRect);
235
236         readDC->asTexture()->readPixels(0, 0, kSize, kSize, kConfig, firstRead);
237
238         paint2.addColorFragmentProcessor(std::move(upmToPM));
239         paint2.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
240
241         tempDC->fillRectToRect(GrNoClip(), paint2, SkMatrix::I(), kDstRect, kSrcRect);
242
243         paint3.addColorFragmentProcessor(std::move(pmToUPM2));
244         paint3.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
245
246         readDC->fillRectToRect(GrNoClip(), paint3, SkMatrix::I(), kDstRect, kSrcRect);
247
248         readDC->asTexture()->readPixels(0, 0, kSize, kSize, kConfig, secondRead);
249
250         failed = false;
251         for (int y = 0; y < kSize && !failed; ++y) {
252             for (int x = 0; x <= y; ++x) {
253                 if (firstRead[kSize * y + x] != secondRead[kSize * y + x]) {
254                     failed = true;
255                     break;
256                 }
257             }
258         }
259     }
260     if (failed) {
261         *pmToUPMRule = kNone_PMConversion;
262         *upmToPMRule = kNone_PMConversion;
263     }
264 }
265
266 sk_sp<GrFragmentProcessor> GrConfigConversionEffect::Make(GrTexture* texture,
267                                                           const GrSwizzle& swizzle,
268                                                           PMConversion pmConversion,
269                                                           const SkMatrix& matrix) {
270     if (swizzle == GrSwizzle::RGBA() && kNone_PMConversion == pmConversion) {
271         // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect
272         // then we may pollute our texture cache with redundant shaders. So in the case that no
273         // conversions were requested we instead return a GrSimpleTextureEffect.
274         return GrSimpleTextureEffect::Make(texture, nullptr, matrix);
275     } else {
276         if (kRGBA_8888_GrPixelConfig != texture->config() &&
277             kBGRA_8888_GrPixelConfig != texture->config() &&
278             kNone_PMConversion != pmConversion) {
279             // The PM conversions assume colors are 0..255
280             return nullptr;
281         }
282         return sk_sp<GrFragmentProcessor>(
283             new GrConfigConversionEffect(texture, swizzle, pmConversion, matrix));
284     }
285 }