blob: 8b75a884c08540b52e6dba0abf48689848814bc4 [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"
egdaniel605dd0f2014-11-12 08:35:25 -080010#include "GrInvariantOutput.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000011#include "GrSimpleTextureEffect.h"
joshualitteb2a6762014-12-04 11:35:33 -080012#include "SkMatrix.h"
joshualittb0a8a372014-09-23 09:50:21 -070013#include "gl/GrGLProcessor.h"
joshualitt30ba4362014-08-21 20:18:45 -070014#include "gl/builders/GrGLProgramBuilder.h"
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000015
joshualittb0a8a372014-09-23 09:50:21 -070016class GrGLConfigConversionEffect : public GrGLFragmentProcessor {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000017public:
joshualitteb2a6762014-12-04 11:35:33 -080018 GrGLConfigConversionEffect(const GrProcessor& processor) {
joshualittb0a8a372014-09-23 09:50:21 -070019 const GrConfigConversionEffect& configConversionEffect =
20 processor.cast<GrConfigConversionEffect>();
joshualitt49586be2014-09-16 08:21:41 -070021 fSwapRedAndBlue = configConversionEffect.swapsRedAndBlue();
22 fPMConversion = configConversionEffect.pmConversion();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000023 }
24
joshualitt15988992014-10-09 15:04:05 -070025 virtual void emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -070026 const GrFragmentProcessor&,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000027 const char* outputColor,
28 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +000029 const TransformedCoordsArray& coords,
mtklein36352bf2015-03-25 18:17:31 -070030 const TextureSamplerArray& samplers) override {
changjun.yangcecc91c2014-08-19 18:24:30 -070031 // Using highp for GLES here in order to avoid some precision issues on specific GPUs.
bsalomonc0bd6482014-12-09 10:04:14 -080032 GrGLShaderVar tmpVar("tmpColor", kVec4f_GrSLType, 0, kHigh_GrSLPrecision);
changjun.yangcecc91c2014-08-19 18:24:30 -070033 SkString tmpDecl;
34 tmpVar.appendDecl(builder->ctxInfo(), &tmpDecl);
changjun.yangcecc91c2014-08-19 18:24:30 -070035
egdaniel29bee0f2015-04-29 11:54:42 -070036 GrGLFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -070037
38 fsBuilder->codeAppendf("%s;", tmpDecl.c_str());
39
40 fsBuilder->codeAppendf("%s = ", tmpVar.c_str());
joshualitt23e280d2014-09-18 12:26:38 -070041 fsBuilder->appendTextureLookup(samplers[0], coords[0].c_str(), coords[0].getType());
joshualitt30ba4362014-08-21 20:18:45 -070042 fsBuilder->codeAppend(";");
changjun.yangcecc91c2014-08-19 18:24:30 -070043
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000044 if (GrConfigConversionEffect::kNone_PMConversion == fPMConversion) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000045 SkASSERT(fSwapRedAndBlue);
joshualitt30ba4362014-08-21 20:18:45 -070046 fsBuilder->codeAppendf("%s = %s.bgra;", outputColor, tmpVar.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000047 } else {
48 const char* swiz = fSwapRedAndBlue ? "bgr" : "rgb";
49 switch (fPMConversion) {
50 case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion:
joshualitt30ba4362014-08-21 20:18:45 -070051 fsBuilder->codeAppendf(
changjun.yangcecc91c2014-08-19 18:24:30 -070052 "%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());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000054 break;
55 case GrConfigConversionEffect::kMulByAlpha_RoundDown_PMConversion:
commit-bot@chromium.orgb4e200e2013-06-19 11:41:02 +000056 // 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.
joshualitt30ba4362014-08-21 20:18:45 -070060 fsBuilder->codeAppendf(
changjun.yangcecc91c2014-08-19 18:24:30 -070061 "%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());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000063 break;
64 case GrConfigConversionEffect::kDivByAlpha_RoundUp_PMConversion:
joshualitt30ba4362014-08-21 20:18:45 -070065 fsBuilder->codeAppendf(
changjun.yangcecc91c2014-08-19 18:24:30 -070066 "%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());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000068 break;
69 case GrConfigConversionEffect::kDivByAlpha_RoundDown_PMConversion:
joshualitt30ba4362014-08-21 20:18:45 -070070 fsBuilder->codeAppendf(
changjun.yangcecc91c2014-08-19 18:24:30 -070071 "%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());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000073 break;
robertphillips@google.com2af1b182012-08-28 11:23:09 +000074 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +000075 SkFAIL("Unknown conversion op.");
robertphillips@google.com2af1b182012-08-28 11:23:09 +000076 break;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000077 }
joshualitt30ba4362014-08-21 20:18:45 -070078 fsBuilder->codeAppendf("%s = %s;", outputColor, tmpVar.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000079 }
bsalomon@google.comf910d3b2013-03-07 17:06:57 +000080 SkString modulate;
egdaniel089f8de2014-10-09 10:34:58 -070081 GrGLSLMulVarBy4f(&modulate, outputColor, inputColor);
joshualitt30ba4362014-08-21 20:18:45 -070082 fsBuilder->codeAppend(modulate.c_str());
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000083 }
84
jvanverthcfc18862015-04-28 08:48:20 -070085 static inline void GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -070086 GrProcessorKeyBuilder* b) {
87 const GrConfigConversionEffect& conv = processor.cast<GrConfigConversionEffect>();
bsalomon63e99f72014-07-21 08:03:14 -070088 uint32_t key = (conv.swapsRedAndBlue() ? 0 : 1) | (conv.pmConversion() << 1);
89 b->add32(key);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000090 }
91
92private:
93 bool fSwapRedAndBlue;
94 GrConfigConversionEffect::PMConversion fPMConversion;
95
joshualittb0a8a372014-09-23 09:50:21 -070096 typedef GrGLFragmentProcessor INHERITED;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000097
98};
99
100///////////////////////////////////////////////////////////////////////////////
101
102GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture,
103 bool swapRedAndBlue,
bsalomon@google.comb1456d72012-11-02 18:23:45 +0000104 PMConversion pmConversion,
105 const SkMatrix& matrix)
106 : GrSingleTextureEffect(texture, matrix)
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000107 , fSwapRedAndBlue(swapRedAndBlue)
108 , fPMConversion(pmConversion) {
joshualitteb2a6762014-12-04 11:35:33 -0800109 this->initClassID<GrConfigConversionEffect>();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000110 SkASSERT(kRGBA_8888_GrPixelConfig == texture->config() ||
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000111 kBGRA_8888_GrPixelConfig == texture->config());
112 // Why did we pollute our texture cache instead of using a GrSingleTextureEffect?
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000113 SkASSERT(swapRedAndBlue || kNone_PMConversion != pmConversion);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000114}
115
bsalomon0e08fc12014-10-15 08:19:04 -0700116bool GrConfigConversionEffect::onIsEqual(const GrFragmentProcessor& s) const {
joshualitt49586be2014-09-16 08:21:41 -0700117 const GrConfigConversionEffect& other = s.cast<GrConfigConversionEffect>();
bsalomon420d7e92014-10-16 09:18:09 -0700118 return other.fSwapRedAndBlue == fSwapRedAndBlue &&
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000119 other.fPMConversion == fPMConversion;
120}
121
egdaniel605dd0f2014-11-12 08:35:25 -0800122void GrConfigConversionEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
egdaniel1a8ecdf2014-10-03 06:24:12 -0700123 this->updateInvariantOutputForModulation(inout);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000124}
125
126///////////////////////////////////////////////////////////////////////////////
127
joshualittb0a8a372014-09-23 09:50:21 -0700128GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrConfigConversionEffect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000129
joshualittb0a8a372014-09-23 09:50:21 -0700130GrFragmentProcessor* GrConfigConversionEffect::TestCreate(SkRandom* random,
131 GrContext*,
132 const GrDrawTargetCaps&,
133 GrTexture* textures[]) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000134 PMConversion pmConv = static_cast<PMConversion>(random->nextULessThan(kPMConversionCnt));
135 bool swapRB;
136 if (kNone_PMConversion == pmConv) {
137 swapRB = true;
138 } else {
139 swapRB = random->nextBool();
140 }
bsalomon55fad7a2014-07-08 07:34:20 -0700141 return SkNEW_ARGS(GrConfigConversionEffect,
joshualittb0a8a372014-09-23 09:50:21 -0700142 (textures[GrProcessorUnitTest::kSkiaPMTextureIdx],
bsalomon@google.com6340a412013-01-22 19:55:59 +0000143 swapRB,
144 pmConv,
joshualitt4eaf9ce2015-04-28 13:31:18 -0700145 GrTest::TestMatrix(random)));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000146}
147
148///////////////////////////////////////////////////////////////////////////////
joshualitteb2a6762014-12-04 11:35:33 -0800149
jvanverthcfc18862015-04-28 08:48:20 -0700150void GrConfigConversionEffect::getGLProcessorKey(const GrGLSLCaps& caps,
joshualitteb2a6762014-12-04 11:35:33 -0800151 GrProcessorKeyBuilder* b) const {
152 GrGLConfigConversionEffect::GenKey(*this, caps, b);
153}
154
155GrGLFragmentProcessor* GrConfigConversionEffect::createGLInstance() const {
156 return SkNEW_ARGS(GrGLConfigConversionEffect, (*this));
157}
158
robertphillipse85a32d2015-02-10 08:16:55 -0800159
160
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000161void 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;
170
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]);
176 color[3] = y;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000177 color[2] = SkTMin(x, y);
178 color[1] = SkTMin(x, y);
179 color[0] = SkTMin(x, y);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000180 }
181 }
182
bsalomonf2703d82014-10-28 14:33:06 -0700183 GrSurfaceDesc desc;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800184 desc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000185 desc.fWidth = 256;
186 desc.fHeight = 256;
187 desc.fConfig = kRGBA_8888_GrPixelConfig;
188
bsalomond309e7a2015-04-30 14:18:54 -0700189 SkAutoTUnref<GrTexture> readTex(context->textureProvider()->createTexture(desc, true, NULL, 0));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000190 if (!readTex.get()) {
191 return;
192 }
bsalomond309e7a2015-04-30 14:18:54 -0700193 SkAutoTUnref<GrTexture> tempTex(context->textureProvider()->createTexture(desc, true, NULL, 0));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000194 if (!tempTex.get()) {
195 return;
196 }
bsalomonf2703d82014-10-28 14:33:06 -0700197 desc.fFlags = kNone_GrSurfaceFlags;
bsalomond309e7a2015-04-30 14:18:54 -0700198 SkAutoTUnref<GrTexture> dataTex(context->textureProvider()->createTexture(desc, true, data, 0));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000199 if (!dataTex.get()) {
200 return;
201 }
202
203 static const PMConversion kConversionRules[][2] = {
204 {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion},
205 {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion},
206 };
207
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000208 bool failed = true;
209
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000210 for (size_t i = 0; i < SK_ARRAY_COUNT(kConversionRules) && failed; ++i) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000211 *pmToUPMRule = kConversionRules[i][0];
212 *upmToPMRule = kConversionRules[i][1];
213
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000214 static const SkRect kDstRect = SkRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256));
215 static const SkRect kSrcRect = SkRect::MakeWH(SK_Scalar1, SK_Scalar1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000216 // 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.
219
joshualittb0a8a372014-09-23 09:50:21 -0700220 SkAutoTUnref<GrFragmentProcessor> pmToUPM1(
221 SkNEW_ARGS(GrConfigConversionEffect,
222 (dataTex, false, *pmToUPMRule, SkMatrix::I())));
223 SkAutoTUnref<GrFragmentProcessor> upmToPM(
224 SkNEW_ARGS(GrConfigConversionEffect,
225 (readTex, false, *upmToPMRule, SkMatrix::I())));
226 SkAutoTUnref<GrFragmentProcessor> pmToUPM2(
227 SkNEW_ARGS(GrConfigConversionEffect,
228 (tempTex, false, *pmToUPMRule, SkMatrix::I())));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000229
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000230 GrPaint paint1;
joshualittb0a8a372014-09-23 09:50:21 -0700231 paint1.addColorProcessor(pmToUPM1);
joshualitt570d2f82015-02-25 13:19:48 -0800232 context->drawNonAARectToRect(readTex->asRenderTarget(),
233 GrClip::WideOpen(),
234 paint1,
235 SkMatrix::I(),
236 kDstRect,
joshualitt25d9c152015-02-18 12:29:52 -0800237 kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000238
239 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
240
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000241 GrPaint paint2;
joshualittb0a8a372014-09-23 09:50:21 -0700242 paint2.addColorProcessor(upmToPM);
joshualitt570d2f82015-02-25 13:19:48 -0800243 context->drawNonAARectToRect(tempTex->asRenderTarget(),
244 GrClip::WideOpen(),
245 paint2,
246 SkMatrix::I(),
247 kDstRect,
joshualitt25d9c152015-02-18 12:29:52 -0800248 kSrcRect);
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000249
250 GrPaint paint3;
joshualittb0a8a372014-09-23 09:50:21 -0700251 paint3.addColorProcessor(pmToUPM2);
joshualitt570d2f82015-02-25 13:19:48 -0800252 context->drawNonAARectToRect(readTex->asRenderTarget(),
253 GrClip::WideOpen(),
254 paint3,
255 SkMatrix::I(),
256 kDstRect,
joshualitt25d9c152015-02-18 12:29:52 -0800257 kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000258
259 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
260
261 failed = false;
262 for (int y = 0; y < 256 && !failed; ++y) {
263 for (int x = 0; x <= y; ++x) {
264 if (firstRead[256 * y + x] != secondRead[256 * y + x]) {
265 failed = true;
266 break;
267 }
268 }
269 }
270 }
271 if (failed) {
272 *pmToUPMRule = kNone_PMConversion;
273 *upmToPMRule = kNone_PMConversion;
274 }
275}
276
joshualittb0a8a372014-09-23 09:50:21 -0700277const GrFragmentProcessor* GrConfigConversionEffect::Create(GrTexture* texture,
bsalomon83d081a2014-07-08 09:56:10 -0700278 bool swapRedAndBlue,
279 PMConversion pmConversion,
280 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000281 if (!swapRedAndBlue && kNone_PMConversion == pmConversion) {
bsalomon@google.comadc65362013-01-28 14:26:09 +0000282 // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000283 // then we may pollute our texture cache with redundant shaders. So in the case that no
bsalomon@google.comadc65362013-01-28 14:26:09 +0000284 // conversions were requested we instead return a GrSimpleTextureEffect.
285 return GrSimpleTextureEffect::Create(texture, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000286 } else {
287 if (kRGBA_8888_GrPixelConfig != texture->config() &&
288 kBGRA_8888_GrPixelConfig != texture->config() &&
289 kNone_PMConversion != pmConversion) {
290 // The PM conversions assume colors are 0..255
bsalomon@google.comadc65362013-01-28 14:26:09 +0000291 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000292 }
bsalomon55fad7a2014-07-08 07:34:20 -0700293 return SkNEW_ARGS(GrConfigConversionEffect, (texture,
294 swapRedAndBlue,
295 pmConversion,
296 matrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000297 }
298}