blob: 8b072f627b6fe594830f2c9057e0c600eac51678 [file] [log] [blame]
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001/*
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"
bsalomon@google.comb1456d72012-11-02 18:23:45 +00009#include "GrContext.h"
robertphillipsea461502015-05-26 11:38:03 -070010#include "GrDrawContext.h"
egdaniel605dd0f2014-11-12 08:35:25 -080011#include "GrInvariantOutput.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000012#include "GrSimpleTextureEffect.h"
joshualitteb2a6762014-12-04 11:35:33 -080013#include "SkMatrix.h"
joshualittb0a8a372014-09-23 09:50:21 -070014#include "gl/GrGLProcessor.h"
joshualitt30ba4362014-08-21 20:18:45 -070015#include "gl/builders/GrGLProgramBuilder.h"
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000016
joshualittb0a8a372014-09-23 09:50:21 -070017class GrGLConfigConversionEffect : public GrGLFragmentProcessor {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000018public:
joshualitteb2a6762014-12-04 11:35:33 -080019 GrGLConfigConversionEffect(const GrProcessor& processor) {
joshualittb0a8a372014-09-23 09:50:21 -070020 const GrConfigConversionEffect& configConversionEffect =
21 processor.cast<GrConfigConversionEffect>();
joshualitt49586be2014-09-16 08:21:41 -070022 fSwapRedAndBlue = configConversionEffect.swapsRedAndBlue();
23 fPMConversion = configConversionEffect.pmConversion();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000024 }
25
joshualitt15988992014-10-09 15:04:05 -070026 virtual void emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -070027 const GrFragmentProcessor&,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000028 const char* outputColor,
29 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000030 const TransformedCoordsArray& coords,
mtklein36352bf2015-03-25 18:17:31 -070031 const TextureSamplerArray& samplers) override {
changjun.yangcecc91c2014-08-19 18:24:30 -070032 // Using highp for GLES here in order to avoid some precision issues on specific GPUs.
bsalomonc0bd6482014-12-09 10:04:14 -080033 GrGLShaderVar tmpVar("tmpColor", kVec4f_GrSLType, 0, kHigh_GrSLPrecision);
changjun.yangcecc91c2014-08-19 18:24:30 -070034 SkString tmpDecl;
35 tmpVar.appendDecl(builder->ctxInfo(), &tmpDecl);
changjun.yangcecc91c2014-08-19 18:24:30 -070036
egdaniel29bee0f2015-04-29 11:54:42 -070037 GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -070038
39 fsBuilder->codeAppendf("%s;", tmpDecl.c_str());
40
41 fsBuilder->codeAppendf("%s = ", tmpVar.c_str());
joshualitt23e280d2014-09-18 12:26:38 -070042 fsBuilder->appendTextureLookup(samplers[0], coords[0].c_str(), coords[0].getType());
joshualitt30ba4362014-08-21 20:18:45 -070043 fsBuilder->codeAppend(";");
changjun.yangcecc91c2014-08-19 18:24:30 -070044
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000045 if (GrConfigConversionEffect::kNone_PMConversion == fPMConversion) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000046 SkASSERT(fSwapRedAndBlue);
joshualitt30ba4362014-08-21 20:18:45 -070047 fsBuilder->codeAppendf("%s = %s.bgra;", outputColor, tmpVar.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000048 } else {
49 const char* swiz = fSwapRedAndBlue ? "bgr" : "rgb";
50 switch (fPMConversion) {
51 case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion:
joshualitt30ba4362014-08-21 20:18:45 -070052 fsBuilder->codeAppendf(
changjun.yangcecc91c2014-08-19 18:24:30 -070053 "%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());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000055 break;
56 case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion:
commit-bot@chromium.orgb4e200e2013-06-19 11:41:02 +000057 // 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.
joshualitt30ba4362014-08-21 20:18:45 -070061 fsBuilder->codeAppendf(
changjun.yangcecc91c2014-08-19 18:24:30 -070062 "%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());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000064 break;
65 case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion:
joshualitt30ba4362014-08-21 20:18:45 -070066 fsBuilder->codeAppendf(
changjun.yangcecc91c2014-08-19 18:24:30 -070067 "%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());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000069 break;
70 case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion:
joshualitt30ba4362014-08-21 20:18:45 -070071 fsBuilder->codeAppendf(
changjun.yangcecc91c2014-08-19 18:24:30 -070072 "%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());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000074 break;
robertphillips@google.com2af1b182012-08-28 11:23:09 +000075 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +000076 SkFAIL("Unknown conversion op.");
robertphillips@google.com2af1b182012-08-28 11:23:09 +000077 break;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000078 }
joshualitt30ba4362014-08-21 20:18:45 -070079 fsBuilder->codeAppendf("%s = %s;", outputColor, tmpVar.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000080 }
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000081 SkString modulate;
egdaniel089f8de2014-10-09 10:34:58 -070082 GrGLSLMulVarBy4f(&modulate, outputColor, inputColor);
joshualitt30ba4362014-08-21 20:18:45 -070083 fsBuilder->codeAppend(modulate.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000084 }
85
jvanverthcfc18862015-04-28 08:48:20 -070086 static inline void GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -070087 GrProcessorKeyBuilder* b) {
88 const GrConfigConversionEffect& conv = processor.cast<GrConfigConversionEffect>();
bsalomon63e99f72014-07-21 08:03:14 -070089 uint32_t key = (conv.swapsRedAndBlue() ? 0 : 1) | (conv.pmConversion() << 1);
90 b->add32(key);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000091 }
92
93private:
94 bool fSwapRedAndBlue;
95 GrConfigConversionEffect::PMConversion fPMConversion;
96
joshualittb0a8a372014-09-23 09:50:21 -070097 typedef GrGLFragmentProcessor INHERITED;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000098
99};
100
101///////////////////////////////////////////////////////////////////////////////
102
103GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture,
104 bool swapRedAndBlue,
bsalomon@google.comb1456d72012-11-02 18:23:45 +0000105 PMConversion pmConversion,
106 const SkMatrix& matrix)
107 : GrSingleTextureEffect(texture, matrix)
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000108 , fSwapRedAndBlue(swapRedAndBlue)
109 , fPMConversion(pmConversion) {
joshualitteb2a6762014-12-04 11:35:33 -0800110 this->initClassID<GrConfigConversionEffect>();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000111 SkASSERT(kRGBA_8888_GrPixelConfig == texture->config() ||
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000112 kBGRA_8888_GrPixelConfig == texture->config());
113 // Why did we pollute our texture cache instead of using a GrSingleTextureEffect?
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000114 SkASSERT(swapRedAndBlue || kNone_PMConversion != pmConversion);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000115}
116
bsalomon0e08fc12014-10-15 08:19:04 -0700117bool GrConfigConversionEffect::onIsEqual(const GrFragmentProcessor& s) const {
joshualitt49586be2014-09-16 08:21:41 -0700118 const GrConfigConversionEffect& other = s.cast<GrConfigConversionEffect>();
bsalomon420d7e92014-10-16 09:18:09 -0700119 return other.fSwapRedAndBlue == fSwapRedAndBlue &&
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000120 other.fPMConversion == fPMConversion;
121}
122
egdaniel605dd0f2014-11-12 08:35:25 -0800123void GrConfigConversionEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
egdaniel1a8ecdf2014-10-03 06:24:12 -0700124 this->updateInvariantOutputForModulation(inout);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000125}
126
127///////////////////////////////////////////////////////////////////////////////
128
joshualittb0a8a372014-09-23 09:50:21 -0700129GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConfigConversionEffect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000130
joshualitt0067ff52015-07-08 14:26:19 -0700131GrFragmentProcessor* GrConfigConversionEffect::TestCreate(GrProcessorTestData* d) {
132 PMConversion pmConv = static_cast<PMConversion>(d->fRandom->nextULessThan(kPMConversionCnt));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000133 bool swapRB;
134 if (kNone_PMConversion == pmConv) {
135 swapRB = true;
136 } else {
joshualitt0067ff52015-07-08 14:26:19 -0700137 swapRB = d->fRandom->nextBool();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000138 }
bsalomon55fad7a2014-07-08 07:34:20 -0700139 return SkNEW_ARGS(GrConfigConversionEffect,
joshualitt0067ff52015-07-08 14:26:19 -0700140 (d->fTextures[GrProcessorUnitTest::kSkiaPMTextureIdx],
bsalomon@google.com6340a412013-01-22 19:55:59 +0000141 swapRB,
142 pmConv,
joshualitt0067ff52015-07-08 14:26:19 -0700143 GrTest::TestMatrix(d->fRandom)));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000144}
145
146///////////////////////////////////////////////////////////////////////////////
joshualitteb2a6762014-12-04 11:35:33 -0800147
jvanverthcfc18862015-04-28 08:48:20 -0700148void GrConfigConversionEffect::getGLProcessorKey(const GrGLSLCaps& caps,
joshualitteb2a6762014-12-04 11:35:33 -0800149 GrProcessorKeyBuilder* b) const {
150 GrGLConfigConversionEffect::GenKey(*this, caps, b);
151}
152
153GrGLFragmentProcessor* GrConfigConversionEffect::createGLInstance() const {
154 return SkNEW_ARGS(GrGLConfigConversionEffect, (*this));
155}
156
robertphillipse85a32d2015-02-10 08:16:55 -0800157
158
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000159void 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;
168
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]);
174 color[3] = y;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000175 color[2] = SkTMin(x, y);
176 color[1] = SkTMin(x, y);
177 color[0] = SkTMin(x, y);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000178 }
179 }
180
bsalomonf2703d82014-10-28 14:33:06 -0700181 GrSurfaceDesc desc;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800182 desc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000183 desc.fWidth = 256;
184 desc.fHeight = 256;
185 desc.fConfig = kRGBA_8888_GrPixelConfig;
186
bsalomond309e7a2015-04-30 14:18:54 -0700187 SkAutoTUnref<GrTexture> readTex(context->textureProvider()->createTexture(desc, true, NULL, 0));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000188 if (!readTex.get()) {
189 return;
190 }
bsalomond309e7a2015-04-30 14:18:54 -0700191 SkAutoTUnref<GrTexture> tempTex(context->textureProvider()->createTexture(desc, true, NULL, 0));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000192 if (!tempTex.get()) {
193 return;
194 }
bsalomonf2703d82014-10-28 14:33:06 -0700195 desc.fFlags = kNone_GrSurfaceFlags;
bsalomond309e7a2015-04-30 14:18:54 -0700196 SkAutoTUnref<GrTexture> dataTex(context->textureProvider()->createTexture(desc, true, data, 0));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000197 if (!dataTex.get()) {
198 return;
199 }
200
201 static const PMConversion kConversionRules[][2] = {
202 {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion},
203 {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion},
204 };
205
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000206 bool failed = true;
207
robertphillipsea461502015-05-26 11:38:03 -0700208 GrDrawContext* drawContext = context->drawContext();
209 if (!drawContext) {
210 return;
211 }
212
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000213 for (size_t i = 0; i < SK_ARRAY_COUNT(kConversionRules) && failed; ++i) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000214 *pmToUPMRule = kConversionRules[i][0];
215 *upmToPMRule = kConversionRules[i][1];
216
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000217 static const SkRect kDstRect = SkRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256));
218 static const SkRect kSrcRect = SkRect::MakeWH(SK_Scalar1, SK_Scalar1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000219 // We do a PM->UPM draw from dataTex to readTex and read the data. Then we do a UPM->PM draw
220 // from readTex to tempTex followed by a PM->UPM draw to readTex and finally read the data.
221 // We then verify that two reads produced the same values.
222
joshualittb0a8a372014-09-23 09:50:21 -0700223 SkAutoTUnref<GrFragmentProcessor> pmToUPM1(
224 SkNEW_ARGS(GrConfigConversionEffect,
225 (dataTex, false, *pmToUPMRule, SkMatrix::I())));
226 SkAutoTUnref<GrFragmentProcessor> upmToPM(
227 SkNEW_ARGS(GrConfigConversionEffect,
228 (readTex, false, *upmToPMRule, SkMatrix::I())));
229 SkAutoTUnref<GrFragmentProcessor> pmToUPM2(
230 SkNEW_ARGS(GrConfigConversionEffect,
231 (tempTex, false, *pmToUPMRule, SkMatrix::I())));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000232
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000233 GrPaint paint1;
joshualittb0a8a372014-09-23 09:50:21 -0700234 paint1.addColorProcessor(pmToUPM1);
robertphillipsea461502015-05-26 11:38:03 -0700235 drawContext->drawNonAARectToRect(readTex->asRenderTarget(),
236 GrClip::WideOpen(),
237 paint1,
238 SkMatrix::I(),
239 kDstRect,
240 kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000241
242 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
243
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000244 GrPaint paint2;
joshualittb0a8a372014-09-23 09:50:21 -0700245 paint2.addColorProcessor(upmToPM);
robertphillipsea461502015-05-26 11:38:03 -0700246 drawContext->drawNonAARectToRect(tempTex->asRenderTarget(),
247 GrClip::WideOpen(),
248 paint2,
249 SkMatrix::I(),
250 kDstRect,
251 kSrcRect);
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000252
253 GrPaint paint3;
joshualittb0a8a372014-09-23 09:50:21 -0700254 paint3.addColorProcessor(pmToUPM2);
robertphillipsea461502015-05-26 11:38:03 -0700255 drawContext->drawNonAARectToRect(readTex->asRenderTarget(),
256 GrClip::WideOpen(),
257 paint3,
258 SkMatrix::I(),
259 kDstRect,
260 kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000261
262 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
263
264 failed = false;
265 for (int y = 0; y < 256 && !failed; ++y) {
266 for (int x = 0; x <= y; ++x) {
267 if (firstRead[256 * y + x] != secondRead[256 * y + x]) {
268 failed = true;
269 break;
270 }
271 }
272 }
273 }
274 if (failed) {
275 *pmToUPMRule = kNone_PMConversion;
276 *upmToPMRule = kNone_PMConversion;
277 }
278}
279
joshualittb0a8a372014-09-23 09:50:21 -0700280const GrFragmentProcessor* GrConfigConversionEffect::Create(GrTexture* texture,
bsalomon83d081a2014-07-08 09:56:10 -0700281 bool swapRedAndBlue,
282 PMConversion pmConversion,
283 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000284 if (!swapRedAndBlue && kNone_PMConversion == pmConversion) {
bsalomon@google.comadc65362013-01-28 14:26:09 +0000285 // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000286 // then we may pollute our texture cache with redundant shaders. So in the case that no
bsalomon@google.comadc65362013-01-28 14:26:09 +0000287 // conversions were requested we instead return a GrSimpleTextureEffect.
288 return GrSimpleTextureEffect::Create(texture, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000289 } else {
290 if (kRGBA_8888_GrPixelConfig != texture->config() &&
291 kBGRA_8888_GrPixelConfig != texture->config() &&
292 kNone_PMConversion != pmConversion) {
293 // The PM conversions assume colors are 0..255
bsalomon@google.comadc65362013-01-28 14:26:09 +0000294 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000295 }
bsalomon55fad7a2014-07-08 07:34:20 -0700296 return SkNEW_ARGS(GrConfigConversionEffect, (texture,
297 swapRedAndBlue,
298 pmConversion,
299 matrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000300 }
301}