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 "glsl/GrGLSLFragmentProcessor.h"
15 #include "glsl/GrGLSLFragmentShaderBuilder.h"
17 class GrGLConfigConversionEffect : public GrGLSLFragmentProcessor {
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();
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);
27 tmpVar.appendDecl(args.fGLSLCaps, &tmpDecl);
29 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
31 fragBuilder->codeAppendf("%s;", tmpDecl.c_str());
33 fragBuilder->codeAppendf("%s = ", tmpVar.c_str());
34 fragBuilder->appendTextureLookup(args.fSamplers[0], args.fCoords[0].c_str(),
35 args.fCoords[0].getType());
36 fragBuilder->codeAppend(";");
38 if (GrConfigConversionEffect::kNone_PMConversion == pmConversion) {
39 SkASSERT(GrSwizzle::RGBA() != swizzle);
40 fragBuilder->codeAppendf("%s = %s.%s;", args.fOutputColor, tmpVar.c_str(),
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());
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());
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(),
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(),
72 SkFAIL("Unknown conversion op.");
75 fragBuilder->codeAppendf("%s = %s.%s;", args.fOutputColor, tmpVar.c_str(),
79 GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor);
80 fragBuilder->codeAppend(modulate.c_str());
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);
91 typedef GrGLSLFragmentProcessor INHERITED;
95 ///////////////////////////////////////////////////////////////////////////////
97 GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture,
98 const GrSwizzle& swizzle,
99 PMConversion pmConversion,
100 const SkMatrix& matrix)
101 : INHERITED(texture, matrix)
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
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);
114 bool GrConfigConversionEffect::onIsEqual(const GrFragmentProcessor& s) const {
115 const GrConfigConversionEffect& other = s.cast<GrConfigConversionEffect>();
116 return other.fSwizzle == fSwizzle &&
117 other.fPMConversion == fPMConversion;
120 void GrConfigConversionEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
121 this->updateInvariantOutputForModulation(inout);
124 ///////////////////////////////////////////////////////////////////////////////
126 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConfigConversionEffect);
128 const GrFragmentProcessor* GrConfigConversionEffect::TestCreate(GrProcessorTestData* d) {
129 PMConversion pmConv = static_cast<PMConversion>(d->fRandom->nextULessThan(kPMConversionCnt));
132 swizzle = GrSwizzle::CreateRandom(d->fRandom);
133 } while (pmConv == kNone_PMConversion && swizzle == GrSwizzle::RGBA());
134 return new GrConfigConversionEffect(d->fTextures[GrProcessorUnitTest::kSkiaPMTextureIdx],
135 swizzle, pmConv, GrTest::TestMatrix(d->fRandom));
138 ///////////////////////////////////////////////////////////////////////////////
140 void GrConfigConversionEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps,
141 GrProcessorKeyBuilder* b) const {
142 GrGLConfigConversionEffect::GenKey(*this, caps, b);
145 GrGLSLFragmentProcessor* GrConfigConversionEffect::onCreateGLSLInstance() const {
146 return new GrGLConfigConversionEffect();
151 void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context,
152 PMConversion* pmToUPMRule,
153 PMConversion* upmToPMRule) {
154 *pmToUPMRule = kNone_PMConversion;
155 *upmToPMRule = kNone_PMConversion;
156 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
157 uint32_t* srcData = data.get();
158 uint32_t* firstRead = data.get() + 256 * 256;
159 uint32_t* secondRead = data.get() + 2 * 256 * 256;
161 // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate
162 // values in row y. We set r,g, and b to the same value since they are handled identically.
163 for (int y = 0; y < 256; ++y) {
164 for (int x = 0; x < 256; ++x) {
165 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
167 color[2] = SkTMin(x, y);
168 color[1] = SkTMin(x, y);
169 color[0] = SkTMin(x, y);
174 desc.fFlags = kRenderTarget_GrSurfaceFlag;
177 desc.fConfig = kRGBA_8888_GrPixelConfig;
179 SkAutoTUnref<GrTexture> readTex(context->textureProvider()->createTexture(
180 desc, SkBudgeted::kYes, nullptr, 0));
181 if (!readTex.get()) {
184 SkAutoTUnref<GrTexture> tempTex(context->textureProvider()->createTexture(
185 desc, SkBudgeted::kYes, nullptr, 0));
186 if (!tempTex.get()) {
189 desc.fFlags = kNone_GrSurfaceFlags;
190 SkAutoTUnref<GrTexture> dataTex(context->textureProvider()->createTexture(
191 desc, SkBudgeted::kYes, data, 0));
192 if (!dataTex.get()) {
196 static const PMConversion kConversionRules[][2] = {
197 {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion},
198 {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion},
203 for (size_t i = 0; i < SK_ARRAY_COUNT(kConversionRules) && failed; ++i) {
204 *pmToUPMRule = kConversionRules[i][0];
205 *upmToPMRule = kConversionRules[i][1];
207 static const SkRect kDstRect = SkRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256));
208 static const SkRect kSrcRect = SkRect::MakeWH(SK_Scalar1, SK_Scalar1);
209 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
210 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
211 // We then verify that two reads produced the same values.
216 SkAutoTUnref<GrFragmentProcessor> pmToUPM1(new GrConfigConversionEffect(
217 dataTex, GrSwizzle::RGBA(), *pmToUPMRule, SkMatrix::I()));
218 SkAutoTUnref<GrFragmentProcessor> upmToPM(new GrConfigConversionEffect(
219 readTex, GrSwizzle::RGBA(), *upmToPMRule, SkMatrix::I()));
220 SkAutoTUnref<GrFragmentProcessor> pmToUPM2(new GrConfigConversionEffect(
221 tempTex, GrSwizzle::RGBA(), *pmToUPMRule, SkMatrix::I()));
223 paint1.addColorFragmentProcessor(pmToUPM1);
224 paint1.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
227 SkAutoTUnref<GrDrawContext> readDrawContext(
228 context->drawContext(readTex->asRenderTarget()));
229 if (!readDrawContext) {
234 readDrawContext->fillRectToRect(GrClip::WideOpen(),
240 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
242 paint2.addColorFragmentProcessor(upmToPM);
243 paint2.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
245 SkAutoTUnref<GrDrawContext> tempDrawContext(
246 context->drawContext(tempTex->asRenderTarget()));
247 if (!tempDrawContext) {
251 tempDrawContext->fillRectToRect(GrClip::WideOpen(),
257 paint3.addColorFragmentProcessor(pmToUPM2);
258 paint3.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
260 readDrawContext.reset(context->drawContext(readTex->asRenderTarget()));
261 if (!readDrawContext) {
266 readDrawContext->fillRectToRect(GrClip::WideOpen(),
272 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
275 for (int y = 0; y < 256 && !failed; ++y) {
276 for (int x = 0; x <= y; ++x) {
277 if (firstRead[256 * y + x] != secondRead[256 * y + x]) {
285 *pmToUPMRule = kNone_PMConversion;
286 *upmToPMRule = kNone_PMConversion;
290 const GrFragmentProcessor* GrConfigConversionEffect::Create(GrTexture* texture,
291 const GrSwizzle& swizzle,
292 PMConversion pmConversion,
293 const SkMatrix& matrix) {
294 if (swizzle == GrSwizzle::RGBA() && kNone_PMConversion == pmConversion) {
295 // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect
296 // then we may pollute our texture cache with redundant shaders. So in the case that no
297 // conversions were requested we instead return a GrSimpleTextureEffect.
298 return GrSimpleTextureEffect::Create(texture, matrix);
300 if (kRGBA_8888_GrPixelConfig != texture->config() &&
301 kBGRA_8888_GrPixelConfig != texture->config() &&
302 kNone_PMConversion != pmConversion) {
303 // The PM conversions assume colors are 0..255
306 return new GrConfigConversionEffect(texture, swizzle, pmConversion, matrix);