blob: 4412c43736694c462500ae84193856f8582ddaa8 [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,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000030 const TextureSamplerArray& samplers) SK_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
joshualitt15988992014-10-09 15:04:05 -070036 GrGLFPFragmentBuilder* 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
joshualittb0a8a372014-09-23 09:50:21 -070085 static inline void GenKey(const GrProcessor& processor, const GrGLCaps&,
86 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,
joshualittb0a8a372014-09-23 09:50:21 -0700145 GrProcessorUnitTest::TestMatrix(random)));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000146}
147
148///////////////////////////////////////////////////////////////////////////////
joshualitteb2a6762014-12-04 11:35:33 -0800149
150void GrConfigConversionEffect::getGLProcessorKey(const GrGLCaps& caps,
151 GrProcessorKeyBuilder* b) const {
152 GrGLConfigConversionEffect::GenKey(*this, caps, b);
153}
154
155GrGLFragmentProcessor* GrConfigConversionEffect::createGLInstance() const {
156 return SkNEW_ARGS(GrGLConfigConversionEffect, (*this));
157}
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;
182 desc.fFlags = kRenderTarget_GrSurfaceFlag |
183 kNoStencil_GrSurfaceFlag;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000184 desc.fWidth = 256;
185 desc.fHeight = 256;
186 desc.fConfig = kRGBA_8888_GrPixelConfig;
187
188 SkAutoTUnref<GrTexture> readTex(context->createUncachedTexture(desc, NULL, 0));
189 if (!readTex.get()) {
190 return;
191 }
192 SkAutoTUnref<GrTexture> tempTex(context->createUncachedTexture(desc, NULL, 0));
193 if (!tempTex.get()) {
194 return;
195 }
bsalomonf2703d82014-10-28 14:33:06 -0700196 desc.fFlags = kNone_GrSurfaceFlags;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000197 SkAutoTUnref<GrTexture> dataTex(context->createUncachedTexture(desc, data, 0));
198 if (!dataTex.get()) {
199 return;
200 }
201
202 static const PMConversion kConversionRules[][2] = {
203 {kDivByAlpha_RoundDown_PMConversion, kMulByAlpha_RoundUp_PMConversion},
204 {kDivByAlpha_RoundUp_PMConversion, kMulByAlpha_RoundDown_PMConversion},
205 };
206
207 GrContext::AutoWideOpenIdentityDraw awoid(context, NULL);
208
209 bool failed = true;
210
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000211 for (size_t i = 0; i < SK_ARRAY_COUNT(kConversionRules) && failed; ++i) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000212 *pmToUPMRule = kConversionRules[i][0];
213 *upmToPMRule = kConversionRules[i][1];
214
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000215 static const SkRect kDstRect = SkRect::MakeWH(SkIntToScalar(256), SkIntToScalar(256));
216 static const SkRect kSrcRect = SkRect::MakeWH(SK_Scalar1, SK_Scalar1);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000217 // 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.
220
joshualittb0a8a372014-09-23 09:50:21 -0700221 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())));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000230
231 context->setRenderTarget(readTex->asRenderTarget());
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000232 GrPaint paint1;
joshualittb0a8a372014-09-23 09:50:21 -0700233 paint1.addColorProcessor(pmToUPM1);
joshualitt16b27892014-12-18 07:47:16 -0800234 context->drawNonAARectToRect(paint1, SkMatrix::I(), kDstRect, kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000235
236 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, firstRead);
237
238 context->setRenderTarget(tempTex->asRenderTarget());
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000239 GrPaint paint2;
joshualittb0a8a372014-09-23 09:50:21 -0700240 paint2.addColorProcessor(upmToPM);
joshualitt16b27892014-12-18 07:47:16 -0800241 context->drawNonAARectToRect(paint2, SkMatrix::I(), kDstRect, kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000242 context->setRenderTarget(readTex->asRenderTarget());
commit-bot@chromium.org42dacab2013-07-13 17:24:24 +0000243
244 GrPaint paint3;
joshualittb0a8a372014-09-23 09:50:21 -0700245 paint3.addColorProcessor(pmToUPM2);
joshualitt16b27892014-12-18 07:47:16 -0800246 context->drawNonAARectToRect(paint3, SkMatrix::I(), kDstRect, kSrcRect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000247
248 readTex->readPixels(0, 0, 256, 256, kRGBA_8888_GrPixelConfig, secondRead);
249
250 failed = false;
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]) {
254 failed = true;
255 break;
256 }
257 }
258 }
259 }
260 if (failed) {
261 *pmToUPMRule = kNone_PMConversion;
262 *upmToPMRule = kNone_PMConversion;
263 }
264}
265
joshualittb0a8a372014-09-23 09:50:21 -0700266const GrFragmentProcessor* GrConfigConversionEffect::Create(GrTexture* texture,
bsalomon83d081a2014-07-08 09:56:10 -0700267 bool swapRedAndBlue,
268 PMConversion pmConversion,
269 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000270 if (!swapRedAndBlue && kNone_PMConversion == pmConversion) {
bsalomon@google.comadc65362013-01-28 14:26:09 +0000271 // If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000272 // then we may pollute our texture cache with redundant shaders. So in the case that no
bsalomon@google.comadc65362013-01-28 14:26:09 +0000273 // conversions were requested we instead return a GrSimpleTextureEffect.
274 return GrSimpleTextureEffect::Create(texture, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000275 } else {
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
bsalomon@google.comadc65362013-01-28 14:26:09 +0000280 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000281 }
bsalomon55fad7a2014-07-08 07:34:20 -0700282 return SkNEW_ARGS(GrConfigConversionEffect, (texture,
283 swapRedAndBlue,
284 pmConversion,
285 matrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000286 }
287}