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 "GrInvariantOutput.h"
11 #include "GrSimpleTextureEffect.h"
13 #include "gl/GrGLProcessor.h"
14 #include "gl/builders/GrGLProgramBuilder.h"
16 class GrGLConfigConversionEffect : public GrGLFragmentProcessor {
18 GrGLConfigConversionEffect(const GrProcessor& processor) {
19 const GrConfigConversionEffect& configConversionEffect =
20 processor.cast<GrConfigConversionEffect>();
21 fSwapRedAndBlue = configConversionEffect.swapsRedAndBlue();
22 fPMConversion = configConversionEffect.pmConversion();
25 virtual void emitCode(GrGLFPBuilder* builder,
26 const GrFragmentProcessor&,
27 const char* outputColor,
28 const char* inputColor,
29 const TransformedCoordsArray& coords,
30 const TextureSamplerArray& samplers) SK_OVERRIDE {
31 // Using highp for GLES here in order to avoid some precision issues on specific GPUs.
32 GrGLShaderVar tmpVar("tmpColor", kVec4f_GrSLType, 0, kHigh_GrSLPrecision);
34 tmpVar.appendDecl(builder->ctxInfo(), &tmpDecl);
36 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
38 fsBuilder->codeAppendf("%s;", tmpDecl.c_str());
40 fsBuilder->codeAppendf("%s = ", tmpVar.c_str());
41 fsBuilder->appendTextureLookup(samplers[0], coords[0].c_str(), coords[0].getType());
42 fsBuilder->codeAppend(";");
44 if (GrConfigConversionEffect::kNone_PMConversion == fPMConversion) {
45 SkASSERT(fSwapRedAndBlue);
46 fsBuilder->codeAppendf("%s = %s.bgra;", outputColor, tmpVar.c_str());
48 const char* swiz = fSwapRedAndBlue ? "bgr" : "rgb";
49 switch (fPMConversion) {
50 case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion:
51 fsBuilder->codeAppendf(
52 "%s = vec4(ceil(%s.%s * %s.a * 255.0) / 255.0, %s.a);",
53 tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str());
55 case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion:
56 // Add a compensation(0.001) here to avoid the side effect of the floor operation.
57 // In Intel GPUs, the integer value converted from floor(%s.r * 255.0) / 255.0
58 // is less than the integer value converted from %s.r by 1 when the %s.r is
59 // converted from the integer value 2^n, such as 1, 2, 4, 8, etc.
60 fsBuilder->codeAppendf(
61 "%s = vec4(floor(%s.%s * %s.a * 255.0 + 0.001) / 255.0, %s.a);",
62 tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str());
64 case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion:
65 fsBuilder->codeAppendf(
66 "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(ceil(%s.%s / %s.a * 255.0) / 255.0, %s.a);",
67 tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str());
69 case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion:
70 fsBuilder->codeAppendf(
71 "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(floor(%s.%s / %s.a * 255.0) / 255.0, %s.a);",
72 tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str());
75 SkFAIL("Unknown conversion op.");
78 fsBuilder->codeAppendf("%s = %s;", outputColor, tmpVar.c_str());
81 GrGLSLMulVarBy4f(&modulate, outputColor, inputColor);
82 fsBuilder->codeAppend(modulate.c_str());
85 static inline void GenKey(const GrProcessor& processor, const GrGLCaps&,
86 GrProcessorKeyBuilder* b) {
87 const GrConfigConversionEffect& conv = processor.cast<GrConfigConversionEffect>();
88 uint32_t key = (conv.swapsRedAndBlue() ? 0 : 1) | (conv.pmConversion() << 1);
94 GrConfigConversionEffect::PMConversion fPMConversion;
96 typedef GrGLFragmentProcessor INHERITED;
100 ///////////////////////////////////////////////////////////////////////////////
102 GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture,
104 PMConversion pmConversion,
105 const SkMatrix& matrix)
106 : GrSingleTextureEffect(texture, matrix)
107 , fSwapRedAndBlue(swapRedAndBlue)
108 , fPMConversion(pmConversion) {
109 this->initClassID<GrConfigConversionEffect>();
110 SkASSERT(kRGBA_8888_GrPixelConfig == texture->config() ||
111 kBGRA_8888_GrPixelConfig == texture->config());
112 // Why did we pollute our texture cache instead of using a GrSingleTextureEffect?
113 SkASSERT(swapRedAndBlue || kNone_PMConversion != pmConversion);
116 bool GrConfigConversionEffect::onIsEqual(const GrFragmentProcessor& s) const {
117 const GrConfigConversionEffect& other = s.cast<GrConfigConversionEffect>();
118 return other.fSwapRedAndBlue == fSwapRedAndBlue &&
119 other.fPMConversion == fPMConversion;
122 void GrConfigConversionEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
123 this->updateInvariantOutputForModulation(inout);
126 ///////////////////////////////////////////////////////////////////////////////
128 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConfigConversionEffect);
130 GrFragmentProcessor* GrConfigConversionEffect::TestCreate(SkRandom* random,
132 const GrDrawTargetCaps&,
133 GrTexture* textures[]) {
134 PMConversion pmConv = static_cast<PMConversion>(random->nextULessThan(kPMConversionCnt));
136 if (kNone_PMConversion == pmConv) {
139 swapRB = random->nextBool();
141 return SkNEW_ARGS(GrConfigConversionEffect,
142 (textures[GrProcessorUnitTest::kSkiaPMTextureIdx],
145 GrProcessorUnitTest::TestMatrix(random)));
148 ///////////////////////////////////////////////////////////////////////////////
150 void GrConfigConversionEffect::getGLProcessorKey(const GrGLCaps& caps,
151 GrProcessorKeyBuilder* b) const {
152 GrGLConfigConversionEffect::GenKey(*this, caps, b);
155 GrGLFragmentProcessor* GrConfigConversionEffect::createGLInstance() const {
156 return SkNEW_ARGS(GrGLConfigConversionEffect, (*this));
159 void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context,
160 PMConversion* pmToUPMRule,
161 PMConversion* upmToPMRule) {
162 *pmToUPMRule = kNone_PMConversion;
163 *upmToPMRule = kNone_PMConversion;
164 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
165 uint32_t* srcData = data.get();
166 uint32_t* firstRead = data.get() + 256 * 256;
167 uint32_t* secondRead = data.get() + 2 * 256 * 256;
169 // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate
170 // values in row y. We set r,g, and b to the same value since they are handled identically.
171 for (int y = 0; y < 256; ++y) {
172 for (int x = 0; x < 256; ++x) {
173 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
175 color[2] = SkTMin(x, y);
176 color[1] = SkTMin(x, y);
177 color[0] = SkTMin(x, y);
182 desc.fFlags = kRenderTarget_GrSurfaceFlag |
183 kNoStencil_GrSurfaceFlag;
186 desc.fConfig = kRGBA_8888_GrPixelConfig;
188 SkAutoTUnref<GrTexture> readTex(context->createUncachedTexture(desc, NULL, 0));
189 if (!readTex.get()) {
192 SkAutoTUnref<GrTexture> tempTex(context->createUncachedTexture(desc, NULL, 0));
193 if (!tempTex.get()) {
196 desc.fFlags = kNone_GrSurfaceFlags;
197 SkAutoTUnref<GrTexture> dataTex(context->createUncachedTexture(desc, data, 0));
198 if (!dataTex.get()) {
202 static const PMConversion kConversionRules[][2] = {
203 {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion},
204 {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion},
207 GrContext::AutoWideOpenIdentityDraw awoid(context, NULL);
211 for (size_t i = 0; i < SK_ARRAY_COUNT(kConversionRules) && failed; ++i) {
212 *pmToUPMRule = kConversionRules[i][0];
213 *upmToPMRule = kConversionRules[i][1];
215 static const SkRect kDstRect = SkRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256));
216 static const SkRect kSrcRect = SkRect::MakeWH(SK_Scalar1, SK_Scalar1);
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.
221 SkAutoTUnref<GrFragmentProcessor> pmToUPM1(
222 SkNEW_ARGS(GrConfigConversionEffect,
223 (dataTex, false, *pmToUPMRule, SkMatrix::I())));
224 SkAutoTUnref<GrFragmentProcessor> upmToPM(
225 SkNEW_ARGS(GrConfigConversionEffect,
226 (readTex, false, *upmToPMRule, SkMatrix::I())));
227 SkAutoTUnref<GrFragmentProcessor> pmToUPM2(
228 SkNEW_ARGS(GrConfigConversionEffect,
229 (tempTex, false, *pmToUPMRule, SkMatrix::I())));
231 context->setRenderTarget(readTex->asRenderTarget());
233 paint1.addColorProcessor(pmToUPM1);
234 context->drawRectToRect(paint1, kDstRect, kSrcRect);
236 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
238 context->setRenderTarget(tempTex->asRenderTarget());
240 paint2.addColorProcessor(upmToPM);
241 context->drawRectToRect(paint2, kDstRect, kSrcRect);
242 context->setRenderTarget(readTex->asRenderTarget());
245 paint3.addColorProcessor(pmToUPM2);
246 context->drawRectToRect(paint3, kDstRect, kSrcRect);
248 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
251 for (int y = 0; y < 256 && !failed; ++y) {
252 for (int x = 0; x <= y; ++x) {
253 if (firstRead[256 * y + x] != secondRead[256 * y + x]) {
261 *pmToUPMRule = kNone_PMConversion;
262 *upmToPMRule = kNone_PMConversion;
266 const GrFragmentProcessor* GrConfigConversionEffect::Create(GrTexture* texture,
268 PMConversion pmConversion,
269 const SkMatrix& matrix) {
270 if (!swapRedAndBlue && 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::Create(texture, matrix);
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
282 return SkNEW_ARGS(GrConfigConversionEffect, (texture,