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"
16 #include "glsl/GrGLSLProgramBuilder.h"
18 class GrGLConfigConversionEffect : public GrGLSLFragmentProcessor {
20 GrGLConfigConversionEffect(const GrProcessor& processor) {
21 const GrConfigConversionEffect& configConversionEffect =
22 processor.cast<GrConfigConversionEffect>();
23 fSwapRedAndBlue = configConversionEffect.swapsRedAndBlue();
24 fPMConversion = configConversionEffect.pmConversion();
27 virtual void emitCode(EmitArgs& args) override {
28 // Using highp for GLES here in order to avoid some precision issues on specific GPUs.
29 GrGLSLShaderVar tmpVar("tmpColor", kVec4f_GrSLType, 0, kHigh_GrSLPrecision);
31 tmpVar.appendDecl(args.fGLSLCaps, &tmpDecl);
33 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
35 fragBuilder->codeAppendf("%s;", tmpDecl.c_str());
37 fragBuilder->codeAppendf("%s = ", tmpVar.c_str());
38 fragBuilder->appendTextureLookup(args.fSamplers[0], args.fCoords[0].c_str(),
39 args.fCoords[0].getType());
40 fragBuilder->codeAppend(";");
42 if (GrConfigConversionEffect::kNone_PMConversion == fPMConversion) {
43 SkASSERT(fSwapRedAndBlue);
44 fragBuilder->codeAppendf("%s = %s.bgra;", args.fOutputColor, tmpVar.c_str());
46 const char* swiz = fSwapRedAndBlue ? "bgr" : "rgb";
47 switch (fPMConversion) {
48 case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion:
49 fragBuilder->codeAppendf(
50 "%s = vec4(ceil(%s.%s * %s.a * 255.0) / 255.0, %s.a);",
51 tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str());
53 case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion:
54 // Add a compensation(0.001) here to avoid the side effect of the floor operation.
55 // In Intel GPUs, the integer value converted from floor(%s.r * 255.0) / 255.0
56 // is less than the integer value converted from %s.r by 1 when the %s.r is
57 // converted from the integer value 2^n, such as 1, 2, 4, 8, etc.
58 fragBuilder->codeAppendf(
59 "%s = vec4(floor(%s.%s * %s.a * 255.0 + 0.001) / 255.0, %s.a);",
60 tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str());
62 case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion:
63 fragBuilder->codeAppendf(
64 "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(ceil(%s.%s / %s.a * 255.0) / 255.0, %s.a);",
65 tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str());
67 case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion:
68 fragBuilder->codeAppendf(
69 "%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(floor(%s.%s / %s.a * 255.0) / 255.0, %s.a);",
70 tmpVar.c_str(), tmpVar.c_str(), tmpVar.c_str(), swiz, tmpVar.c_str(), tmpVar.c_str());
73 SkFAIL("Unknown conversion op.");
76 fragBuilder->codeAppendf("%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& conv = processor.cast<GrConfigConversionEffect>();
86 uint32_t key = (conv.swapsRedAndBlue() ? 0 : 1) | (conv.pmConversion() << 1);
92 GrConfigConversionEffect::PMConversion fPMConversion;
94 typedef GrGLSLFragmentProcessor INHERITED;
98 ///////////////////////////////////////////////////////////////////////////////
100 GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture,
102 PMConversion pmConversion,
103 const SkMatrix& matrix)
104 : INHERITED(texture, matrix)
105 , fSwapRedAndBlue(swapRedAndBlue)
106 , fPMConversion(pmConversion) {
107 this->initClassID<GrConfigConversionEffect>();
108 // We expect to get here with non-BGRA/RGBA only if we're doing not doing a premul/unpremul
110 SkASSERT((kRGBA_8888_GrPixelConfig == texture->config() ||
111 kBGRA_8888_GrPixelConfig == texture->config()) ||
112 kNone_PMConversion == pmConversion);
113 // Why did we pollute our texture cache instead of using a GrSingleTextureEffect?
114 SkASSERT(swapRedAndBlue || kNone_PMConversion != pmConversion);
117 bool GrConfigConversionEffect::onIsEqual(const GrFragmentProcessor& s) const {
118 const GrConfigConversionEffect& other = s.cast<GrConfigConversionEffect>();
119 return other.fSwapRedAndBlue == fSwapRedAndBlue &&
120 other.fPMConversion == fPMConversion;
123 void GrConfigConversionEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
124 this->updateInvariantOutputForModulation(inout);
127 ///////////////////////////////////////////////////////////////////////////////
129 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConfigConversionEffect);
131 const GrFragmentProcessor* GrConfigConversionEffect::TestCreate(GrProcessorTestData* d) {
132 PMConversion pmConv = static_cast<PMConversion>(d->fRandom->nextULessThan(kPMConversionCnt));
134 if (kNone_PMConversion == pmConv) {
137 swapRB = d->fRandom->nextBool();
139 return new GrConfigConversionEffect(d->fTextures[GrProcessorUnitTest::kSkiaPMTextureIdx],
140 swapRB, pmConv, GrTest::TestMatrix(d->fRandom));
143 ///////////////////////////////////////////////////////////////////////////////
145 void GrConfigConversionEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps,
146 GrProcessorKeyBuilder* b) const {
147 GrGLConfigConversionEffect::GenKey(*this, caps, b);
150 GrGLSLFragmentProcessor* GrConfigConversionEffect::onCreateGLSLInstance() const {
151 return new GrGLConfigConversionEffect(*this);
156 void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context,
157 PMConversion* pmToUPMRule,
158 PMConversion* upmToPMRule) {
159 *pmToUPMRule = kNone_PMConversion;
160 *upmToPMRule = kNone_PMConversion;
161 SkAutoTMalloc<uint32_t> data(256 * 256 * 3);
162 uint32_t* srcData = data.get();
163 uint32_t* firstRead = data.get() + 256 * 256;
164 uint32_t* secondRead = data.get() + 2 * 256 * 256;
166 // Fill with every possible premultiplied A, color channel value. There will be 256-y duplicate
167 // values in row y. We set r,g, and b to the same value since they are handled identically.
168 for (int y = 0; y < 256; ++y) {
169 for (int x = 0; x < 256; ++x) {
170 uint8_t* color = reinterpret_cast<uint8_t*>(&srcData[256*y + x]);
172 color[2] = SkTMin(x, y);
173 color[1] = SkTMin(x, y);
174 color[0] = SkTMin(x, y);
179 desc.fFlags = kRenderTarget_GrSurfaceFlag;
182 desc.fConfig = kRGBA_8888_GrPixelConfig;
184 SkAutoTUnref<GrTexture> readTex(context->textureProvider()->createTexture(desc, true, nullptr, 0));
185 if (!readTex.get()) {
188 SkAutoTUnref<GrTexture> tempTex(context->textureProvider()->createTexture(desc, true, nullptr, 0));
189 if (!tempTex.get()) {
192 desc.fFlags = kNone_GrSurfaceFlags;
193 SkAutoTUnref<GrTexture> dataTex(context->textureProvider()->createTexture(desc, true, data, 0));
194 if (!dataTex.get()) {
198 static const PMConversion kConversionRules[][2] = {
199 {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion},
200 {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion},
205 for (size_t i = 0; i < SK_ARRAY_COUNT(kConversionRules) && failed; ++i) {
206 *pmToUPMRule = kConversionRules[i][0];
207 *upmToPMRule = kConversionRules[i][1];
209 static const SkRect kDstRect = SkRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256));
210 static const SkRect kSrcRect = SkRect::MakeWH(SK_Scalar1, SK_Scalar1);
211 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
212 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
213 // We then verify that two reads produced the same values.
218 SkAutoTUnref<GrFragmentProcessor> pmToUPM1(new GrConfigConversionEffect(
219 dataTex, false, *pmToUPMRule, SkMatrix::I()));
220 SkAutoTUnref<GrFragmentProcessor> upmToPM(new GrConfigConversionEffect(
221 readTex, false, *upmToPMRule, SkMatrix::I()));
222 SkAutoTUnref<GrFragmentProcessor> pmToUPM2(new GrConfigConversionEffect(
223 tempTex, false, *pmToUPMRule, SkMatrix::I()));
225 paint1.addColorFragmentProcessor(pmToUPM1);
226 paint1.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
229 SkAutoTUnref<GrDrawContext> readDrawContext(
230 context->drawContext(readTex->asRenderTarget()));
231 if (!readDrawContext) {
236 readDrawContext->fillRectToRect(GrClip::WideOpen(),
242 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
244 paint2.addColorFragmentProcessor(upmToPM);
245 paint2.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
247 SkAutoTUnref<GrDrawContext> tempDrawContext(
248 context->drawContext(tempTex->asRenderTarget()));
249 if (!tempDrawContext) {
253 tempDrawContext->fillRectToRect(GrClip::WideOpen(),
259 paint3.addColorFragmentProcessor(pmToUPM2);
260 paint3.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
262 readDrawContext.reset(context->drawContext(readTex->asRenderTarget()));
263 if (!readDrawContext) {
268 readDrawContext->fillRectToRect(GrClip::WideOpen(),
274 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
277 for (int y = 0; y < 256 && !failed; ++y) {
278 for (int x = 0; x <= y; ++x) {
279 if (firstRead[256 * y + x] != secondRead[256 * y + x]) {
287 *pmToUPMRule = kNone_PMConversion;
288 *upmToPMRule = kNone_PMConversion;
292 const GrFragmentProcessor* GrConfigConversionEffect::Create(GrTexture* texture,
294 PMConversion pmConversion,
295 const SkMatrix& matrix) {
296 if (!swapRedAndBlue && kNone_PMConversion == pmConversion) {
297 // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect
298 // then we may pollute our texture cache with redundant shaders. So in the case that no
299 // conversions were requested we instead return a GrSimpleTextureEffect.
300 return GrSimpleTextureEffect::Create(texture, matrix);
302 if (kRGBA_8888_GrPixelConfig != texture->config() &&
303 kBGRA_8888_GrPixelConfig != texture->config() &&
304 kNone_PMConversion != pmConversion) {
305 // The PM conversions assume colors are 0..255
308 return new GrConfigConversionEffect(texture, swapRedAndBlue, pmConversion, matrix);