2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "GrConfigConversionEffect.h"
10 #include "GrDrawContext.h"
11 #include "GrInvariantOutput.h"
12 #include "GrSimpleTextureEffect.h"
14 #include "gl/GrGLFragmentProcessor.h"
15 #include "gl/builders/GrGLProgramBuilder.h"
17 class GrGLConfigConversionEffect : public GrGLFragmentProcessor {
19 GrGLConfigConversionEffect(const GrProcessor& processor) {
20 const GrConfigConversionEffect& configConversionEffect =
21 processor.cast<GrConfigConversionEffect>();
22 fSwapRedAndBlue = configConversionEffect.swapsRedAndBlue();
23 fPMConversion = configConversionEffect.pmConversion();
26 virtual void emitCode(GrGLFPBuilder* builder,
27 const GrFragmentProcessor&,
28 const char* outputColor,
29 const char* inputColor,
30 const TransformedCoordsArray& coords,
31 const TextureSamplerArray& samplers) override {
32 // Using highp for GLES here in order to avoid some precision issues on specific GPUs.
33 GrGLShaderVar tmpVar("tmpColor", kVec4f_GrSLType, 0, kHigh_GrSLPrecision);
35 tmpVar.appendDecl(builder->ctxInfo(), &tmpDecl);
37 GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
39 fsBuilder->codeAppendf("%s;", tmpDecl.c_str());
41 fsBuilder->codeAppendf("%s = ", tmpVar.c_str());
42 fsBuilder->appendTextureLookup(samplers[0], coords[0].c_str(), coords[0].getType());
43 fsBuilder->codeAppend(";");
45 if (GrConfigConversionEffect::kNone_PMConversion == fPMConversion) {
46 SkASSERT(fSwapRedAndBlue);
47 fsBuilder->codeAppendf("%s = %s.bgra;", outputColor, tmpVar.c_str());
49 const char* swiz = fSwapRedAndBlue ? "bgr" : "rgb";
50 switch (fPMConversion) {
51 case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion:
52 fsBuilder->codeAppendf(
53 "%s = vec4(ceil(%s.%s * %s.a * 255.0) / 255.0, %s.a);",
54 tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str());
56 case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion:
57 // Add a compensation(0.001) here to avoid the side effect of the floor operation.
58 // In Intel GPUs, the integer value converted from floor(%s.r * 255.0) / 255.0
59 // is less than the integer value converted from %s.r by 1 when the %s.r is
60 // converted from the integer value 2^n, such as 1, 2, 4, 8, etc.
61 fsBuilder->codeAppendf(
62 "%s = vec4(floor(%s.%s * %s.a * 255.0 + 0.001) / 255.0, %s.a);",
63 tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str());
65 case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion:
66 fsBuilder->codeAppendf(
67 "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(ceil(%s.%s / %s.a * 255.0) / 255.0, %s.a);",
68 tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str());
70 case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion:
71 fsBuilder->codeAppendf(
72 "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(floor(%s.%s / %s.a * 255.0) / 255.0, %s.a);",
73 tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str());
76 SkFAIL("Unknown conversion op.");
79 fsBuilder->codeAppendf("%s = %s;", outputColor, tmpVar.c_str());
82 GrGLSLMulVarBy4f(&modulate, outputColor, inputColor);
83 fsBuilder->codeAppend(modulate.c_str());
86 static inline void GenKey(const GrProcessor& processor, const GrGLSLCaps&,
87 GrProcessorKeyBuilder* b) {
88 const GrConfigConversionEffect& conv = processor.cast<GrConfigConversionEffect>();
89 uint32_t key = (conv.swapsRedAndBlue() ? 0 : 1) | (conv.pmConversion() << 1);
95 GrConfigConversionEffect::PMConversion fPMConversion;
97 typedef GrGLFragmentProcessor INHERITED;
101 ///////////////////////////////////////////////////////////////////////////////
103 GrConfigConversionEffect::GrConfigConversionEffect(GrProcessorDataManager* procDataManager,
106 PMConversion pmConversion,
107 const SkMatrix& matrix)
108 : INHERITED(procDataManager, texture, matrix)
109 , fSwapRedAndBlue(swapRedAndBlue)
110 , fPMConversion(pmConversion) {
111 this->initClassID<GrConfigConversionEffect>();
112 SkASSERT(kRGBA_8888_GrPixelConfig == texture->config() ||
113 kBGRA_8888_GrPixelConfig == texture->config());
114 // Why did we pollute our texture cache instead of using a GrSingleTextureEffect?
115 SkASSERT(swapRedAndBlue || kNone_PMConversion != pmConversion);
118 bool GrConfigConversionEffect::onIsEqual(const GrFragmentProcessor& s) const {
119 const GrConfigConversionEffect& other = s.cast<GrConfigConversionEffect>();
120 return other.fSwapRedAndBlue == fSwapRedAndBlue &&
121 other.fPMConversion == fPMConversion;
124 void GrConfigConversionEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
125 this->updateInvariantOutputForModulation(inout);
128 ///////////////////////////////////////////////////////////////////////////////
130 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConfigConversionEffect);
132 GrFragmentProcessor* GrConfigConversionEffect::TestCreate(GrProcessorTestData* d) {
133 PMConversion pmConv = static_cast<PMConversion>(d->fRandom->nextULessThan(kPMConversionCnt));
135 if (kNone_PMConversion == pmConv) {
138 swapRB = d->fRandom->nextBool();
140 return SkNEW_ARGS(GrConfigConversionEffect,
141 (d->fProcDataManager,
142 d->fTextures[GrProcessorUnitTest::kSkiaPMTextureIdx],
145 GrTest::TestMatrix(d->fRandom)));
148 ///////////////////////////////////////////////////////////////////////////////
150 void GrConfigConversionEffect::getGLProcessorKey(const GrGLSLCaps& caps,
151 GrProcessorKeyBuilder* b) const {
152 GrGLConfigConversionEffect::GenKey(*this, caps, b);
155 GrGLFragmentProcessor* GrConfigConversionEffect::createGLInstance() const {
156 return SkNEW_ARGS(GrGLConfigConversionEffect, (*this));
161 void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context,
162 PMConversion* pmToUPMRule,
163 PMConversion* upmToPMRule) {
164 *pmToUPMRule = kNone_PMConversion;
165 *upmToPMRule = kNone_PMConversion;
166 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
167 uint32_t* srcData = data.get();
168 uint32_t* firstRead = data.get() + 256 * 256;
169 uint32_t* secondRead = data.get() + 2 * 256 * 256;
171 // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate
172 // values in row y. We set r,g, and b to the same value since they are handled identically.
173 for (int y = 0; y < 256; ++y) {
174 for (int x = 0; x < 256; ++x) {
175 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
177 color[2] = SkTMin(x, y);
178 color[1] = SkTMin(x, y);
179 color[0] = SkTMin(x, y);
184 desc.fFlags = kRenderTarget_GrSurfaceFlag;
187 desc.fConfig = kRGBA_8888_GrPixelConfig;
189 SkAutoTUnref<GrTexture> readTex(context->textureProvider()->createTexture(desc, true, NULL, 0));
190 if (!readTex.get()) {
193 SkAutoTUnref<GrTexture> tempTex(context->textureProvider()->createTexture(desc, true, NULL, 0));
194 if (!tempTex.get()) {
197 desc.fFlags = kNone_GrSurfaceFlags;
198 SkAutoTUnref<GrTexture> dataTex(context->textureProvider()->createTexture(desc, true, data, 0));
199 if (!dataTex.get()) {
203 static const PMConversion kConversionRules[][2] = {
204 {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion},
205 {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion},
210 for (size_t i = 0; i < SK_ARRAY_COUNT(kConversionRules) && failed; ++i) {
211 *pmToUPMRule = kConversionRules[i][0];
212 *upmToPMRule = kConversionRules[i][1];
214 static const SkRect kDstRect = SkRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256));
215 static const SkRect kSrcRect = SkRect::MakeWH(SK_Scalar1, SK_Scalar1);
216 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
217 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
218 // We then verify that two reads produced the same values.
223 SkAutoTUnref<GrFragmentProcessor> pmToUPM1(
224 SkNEW_ARGS(GrConfigConversionEffect,
225 (paint1.getProcessorDataManager(), dataTex, false, *pmToUPMRule,
227 SkAutoTUnref<GrFragmentProcessor> upmToPM(
228 SkNEW_ARGS(GrConfigConversionEffect,
229 (paint2.getProcessorDataManager(), readTex, false, *upmToPMRule,
231 SkAutoTUnref<GrFragmentProcessor> pmToUPM2(
232 SkNEW_ARGS(GrConfigConversionEffect,
233 (paint3.getProcessorDataManager(), tempTex, false, *pmToUPMRule,
236 paint1.addColorProcessor(pmToUPM1);
239 GrDrawContext* readDrawContext = context->drawContext();
240 if (!readDrawContext) {
245 readDrawContext->drawNonAARectToRect(readTex->asRenderTarget(),
252 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
254 paint2.addColorProcessor(upmToPM);
256 GrDrawContext* tempDrawContext = context->drawContext();
257 if (!tempDrawContext) {
261 tempDrawContext->drawNonAARectToRect(tempTex->asRenderTarget(),
268 paint3.addColorProcessor(pmToUPM2);
270 readDrawContext = context->drawContext();
271 if (!readDrawContext) {
276 readDrawContext->drawNonAARectToRect(readTex->asRenderTarget(),
283 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
286 for (int y = 0; y < 256 && !failed; ++y) {
287 for (int x = 0; x <= y; ++x) {
288 if (firstRead[256 * y + x] != secondRead[256 * y + x]) {
296 *pmToUPMRule = kNone_PMConversion;
297 *upmToPMRule = kNone_PMConversion;
301 const GrFragmentProcessor* GrConfigConversionEffect::Create(GrProcessorDataManager* procDataManager,
304 PMConversion pmConversion,
305 const SkMatrix& matrix) {
306 if (!swapRedAndBlue && kNone_PMConversion == pmConversion) {
307 // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect
308 // then we may pollute our texture cache with redundant shaders. So in the case that no
309 // conversions were requested we instead return a GrSimpleTextureEffect.
310 return GrSimpleTextureEffect::Create(procDataManager, texture, matrix);
312 if (kRGBA_8888_GrPixelConfig != texture->config() &&
313 kBGRA_8888_GrPixelConfig != texture->config() &&
314 kNone_PMConversion != pmConversion) {
315 // The PM conversions assume colors are 0..255
318 return SkNEW_ARGS(GrConfigConversionEffect, (procDataManager,