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