blob: f7c80e2cb8872fff0b45919a78fa66d62d0400f3 [file] [log] [blame]
Brian Salomonbc6b99d2017-01-11 10:32:34 -05001/*
2 * Copyright 2016 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "tests/Test.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/GrContext.h"
11#include "include/gpu/GrGpuResource.h"
12#include "src/gpu/GrClip.h"
13#include "src/gpu/GrContextPriv.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040014#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrMemoryPool.h"
16#include "src/gpu/GrProxyProvider.h"
17#include "src/gpu/GrRenderTargetContext.h"
18#include "src/gpu/GrRenderTargetContextPriv.h"
19#include "src/gpu/GrResourceProvider.h"
20#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
21#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
22#include "src/gpu/ops/GrFillRectOp.h"
23#include "src/gpu/ops/GrMeshDrawOp.h"
24#include "tests/TestUtils.h"
Mike Klein0ec1c572018-12-04 11:52:51 -050025#include <atomic>
Hal Canary8a001442018-09-19 11:31:27 -040026#include <random>
27
Brian Salomonbc6b99d2017-01-11 10:32:34 -050028namespace {
Brian Salomon82ddc942017-07-14 12:00:13 -040029class TestOp : public GrMeshDrawOp {
Brian Salomonbc6b99d2017-01-11 10:32:34 -050030public:
31 DEFINE_OP_CLASS_ID
Robert Phillips7c525e62018-06-12 10:11:12 -040032 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
33 std::unique_ptr<GrFragmentProcessor> fp) {
Robert Phillips9da87e02019-02-04 13:26:26 -050034 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -040035
36 return pool->allocate<TestOp>(std::move(fp));
Brian Salomon82ddc942017-07-14 12:00:13 -040037 }
38
Robert Phillips5f567c72017-09-14 08:27:37 -040039 const char* name() const override { return "TestOp"; }
40
Chris Dalton1706cbf2019-05-21 19:35:29 -060041 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillips5f567c72017-09-14 08:27:37 -040042 fProcessors.visitProxies(func);
43 }
44
Brian Salomon82ddc942017-07-14 12:00:13 -040045 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
46
Chris Dalton6ce447a2019-06-23 18:07:38 -060047 GrProcessorSet::Analysis finalize(
48 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
49 GrClampType clampType) override {
Brian Salomon82ddc942017-07-14 12:00:13 -040050 static constexpr GrProcessorAnalysisColor kUnknownColor;
Brian Osmancf860852018-10-31 14:04:39 -040051 SkPMColor4f overrideColor;
Chris Daltonb8fff0d2019-03-05 10:11:58 -070052 return fProcessors.finalize(
53 kUnknownColor, GrProcessorAnalysisCoverage::kNone, clip,
Chris Dalton6ce447a2019-06-23 18:07:38 -060054 &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps, clampType,
55 &overrideColor);
Brian Salomon649a3412017-03-09 13:50:43 -050056 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050057
58private:
Robert Phillips7c525e62018-06-12 10:11:12 -040059 friend class ::GrOpMemoryPool; // for ctor
60
Brian Salomonaff329b2017-08-11 09:40:37 -040061 TestOp(std::unique_ptr<GrFragmentProcessor> fp)
62 : INHERITED(ClassID()), fProcessors(std::move(fp)) {
Greg Daniel5faf4742019-10-01 15:14:44 -040063 this->setBounds(SkRect::MakeWH(100, 100), HasAABloat::kNo, IsHairline::kNo);
Brian Salomon82ddc942017-07-14 12:00:13 -040064 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050065
Brian Salomon91326c32017-08-09 16:02:19 -040066 void onPrepareDraws(Target* target) override { return; }
Chris Dalton07cdcfc92019-02-26 11:13:22 -070067 void onExecute(GrOpFlushState*, const SkRect&) override { return; }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050068
Brian Salomon82ddc942017-07-14 12:00:13 -040069 GrProcessorSet fProcessors;
70
71 typedef GrMeshDrawOp INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -050072};
73
74/**
Robert Phillips3d4cac52019-06-11 08:08:08 -040075 * FP used to test ref counts on owned GrGpuResources. Can also be a parent FP to test counts
Brian Salomonbc6b99d2017-01-11 10:32:34 -050076 * of resources owned by child FPs.
77 */
78class TestFP : public GrFragmentProcessor {
79public:
Brian Salomonaff329b2017-08-11 09:40:37 -040080 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> child) {
81 return std::unique_ptr<GrFragmentProcessor>(new TestFP(std::move(child)));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050082 }
Brian Salomonaff329b2017-08-11 09:40:37 -040083 static std::unique_ptr<GrFragmentProcessor> Make(const SkTArray<sk_sp<GrTextureProxy>>& proxies,
Brian Salomondbf70722019-02-07 11:31:24 -050084 const SkTArray<sk_sp<GrGpuBuffer>>& buffers) {
Brian Salomon559f5562017-11-15 14:28:33 -050085 return std::unique_ptr<GrFragmentProcessor>(new TestFP(proxies, buffers));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050086 }
87
88 const char* name() const override { return "test"; }
89
90 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
Mike Klein0ec1c572018-12-04 11:52:51 -050091 static std::atomic<int32_t> nextKey{0};
92 b->add32(nextKey++);
Brian Salomonbc6b99d2017-01-11 10:32:34 -050093 }
94
Brian Salomonaff329b2017-08-11 09:40:37 -040095 std::unique_ptr<GrFragmentProcessor> clone() const override {
96 return std::unique_ptr<GrFragmentProcessor>(new TestFP(*this));
Brian Salomonb17e6392017-07-28 13:41:51 -040097 }
98
Brian Salomonbc6b99d2017-01-11 10:32:34 -050099private:
Brian Salomondbf70722019-02-07 11:31:24 -0500100 TestFP(const SkTArray<sk_sp<GrTextureProxy>>& proxies,
101 const SkTArray<sk_sp<GrGpuBuffer>>& buffers)
Brian Salomon662ea4b2018-07-12 14:53:49 -0400102 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4) {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500103 for (const auto& proxy : proxies) {
Brian Salomonf7dcd762018-07-30 14:48:15 -0400104 fSamplers.emplace_back(proxy);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500105 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400106 this->setTextureSamplerCnt(fSamplers.count());
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500107 }
108
Brian Salomonaff329b2017-08-11 09:40:37 -0400109 TestFP(std::unique_ptr<GrFragmentProcessor> child)
Brian Salomon662ea4b2018-07-12 14:53:49 -0400110 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4) {
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500111 this->registerChildProcessor(std::move(child));
112 }
113
Brian Salomon96271cd2017-07-31 16:27:23 -0400114 explicit TestFP(const TestFP& that)
Brian Salomon662ea4b2018-07-12 14:53:49 -0400115 : INHERITED(kTestFP_ClassID, that.optimizationFlags()), fSamplers(4) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400116 for (int i = 0; i < that.fSamplers.count(); ++i) {
117 fSamplers.emplace_back(that.fSamplers[i]);
Brian Salomonb17e6392017-07-28 13:41:51 -0400118 }
Brian Salomon96271cd2017-07-31 16:27:23 -0400119 for (int i = 0; i < that.numChildProcessors(); ++i) {
120 this->registerChildProcessor(that.childProcessor(i).clone());
Brian Salomonb17e6392017-07-28 13:41:51 -0400121 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400122 this->setTextureSamplerCnt(fSamplers.count());
Brian Salomonb17e6392017-07-28 13:41:51 -0400123 }
124
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500125 virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
126 class TestGLSLFP : public GrGLSLFragmentProcessor {
127 public:
128 TestGLSLFP() {}
129 void emitCode(EmitArgs& args) override {
130 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
131 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor);
132 }
133
134 private:
135 };
136 return new TestGLSLFP();
137 }
138
139 bool onIsEqual(const GrFragmentProcessor&) const override { return false; }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400140 const TextureSampler& onTextureSampler(int i) const override { return fSamplers[i]; }
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500141
142 GrTAllocator<TextureSampler> fSamplers;
Brian Salomon587e08f2017-01-27 10:59:27 -0500143 typedef GrFragmentProcessor INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500144};
145}
146
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500147DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) {
148 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500149 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500150
Robert Phillips16d8ec62017-07-27 16:16:25 -0400151 GrSurfaceDesc desc;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500152 desc.fWidth = 10;
153 desc.fHeight = 10;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400154 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500155
Greg Daniel4065d452018-11-16 15:43:41 -0500156 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -0400157 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
158 GrRenderable::kNo);
Greg Daniel4065d452018-11-16 15:43:41 -0500159
Brian Salomonb17e6392017-07-28 13:41:51 -0400160 for (bool makeClone : {false, true}) {
161 for (int parentCnt = 0; parentCnt < 2; parentCnt++) {
Brian Salomonbf6b9792019-08-21 09:38:10 -0400162 auto renderTargetContext = context->priv().makeDeferredRenderTargetContext(
163 SkBackingFit::kApprox, 1, 1, GrColorType::kRGBA_8888, nullptr);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500164 {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400165 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400166 format, desc, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin,
Brian Salomonbeb7f522019-08-30 16:19:42 -0400167 GrMipMapped::kNo, SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo);
Robert Phillips3d4cac52019-06-11 08:08:08 -0400168
Brian Salomonb17e6392017-07-28 13:41:51 -0400169 {
170 SkTArray<sk_sp<GrTextureProxy>> proxies;
Brian Salomondbf70722019-02-07 11:31:24 -0500171 SkTArray<sk_sp<GrGpuBuffer>> buffers;
Robert Phillips3d4cac52019-06-11 08:08:08 -0400172 proxies.push_back(proxy);
Brian Salomon559f5562017-11-15 14:28:33 -0500173 auto fp = TestFP::Make(std::move(proxies), std::move(buffers));
Brian Salomonb17e6392017-07-28 13:41:51 -0400174 for (int i = 0; i < parentCnt; ++i) {
175 fp = TestFP::Make(std::move(fp));
176 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400177 std::unique_ptr<GrFragmentProcessor> clone;
Brian Salomonb17e6392017-07-28 13:41:51 -0400178 if (makeClone) {
179 clone = fp->clone();
180 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400181 std::unique_ptr<GrDrawOp> op(TestOp::Make(context, std::move(fp)));
Brian Salomonb17e6392017-07-28 13:41:51 -0400182 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
183 if (clone) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400184 op = TestOp::Make(context, std::move(clone));
Brian Salomonb17e6392017-07-28 13:41:51 -0400185 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
186 }
187 }
Brian Salomonb17e6392017-07-28 13:41:51 -0400188
Robert Phillips3d4cac52019-06-11 08:08:08 -0400189 // If the fp is cloned the number of refs should increase by one (for the clone)
190 int expectedProxyRefs = makeClone ? 3 : 2;
191
Brian Salomon28a8f282019-10-24 20:07:39 -0400192 CheckSingleThreadedProxyRefs(reporter, proxy.get(), expectedProxyRefs, -1);
Brian Salomonb17e6392017-07-28 13:41:51 -0400193
Brian Salomonb17e6392017-07-28 13:41:51 -0400194 context->flush();
195
Brian Salomon557e8122019-10-24 10:37:08 -0400196 // just one from the 'proxy' sk_sp
Brian Salomon28a8f282019-10-24 20:07:39 -0400197 CheckSingleThreadedProxyRefs(reporter, proxy.get(), 1, 1);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500198 }
199 }
200 }
201}
Brian Salomon587e08f2017-01-27 10:59:27 -0500202
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500203#include "tools/flags/CommandLineFlags.h"
Mike Klein84836b72019-03-21 11:31:36 -0500204static DEFINE_bool(randomProcessorTest, false,
205 "Use non-deterministic seed for random processor tests?");
Mike Klein5b3f3432019-03-21 11:42:21 -0500206static DEFINE_int(processorSeed, 0,
207 "Use specific seed for processor tests. Overridden by --randomProcessorTest.");
Brian Salomon0e05a822017-07-25 09:43:22 -0400208
209#if GR_TEST_UTILS
210
Michael Ludwig7034f152018-10-08 16:43:58 -0400211static GrColor input_texel_color(int i, int j, SkScalar delta) {
212 // Delta must be less than 0.5 to prevent over/underflow issues with the input color
213 SkASSERT(delta <= 0.5);
Brian Salomon587e08f2017-01-27 10:59:27 -0500214
Brian Osman50ea3c02019-02-04 10:01:53 -0500215 SkColor color = SkColorSetARGB((uint8_t)(i & 0xFF),
216 (uint8_t)(j & 0xFF),
217 (uint8_t)((i + j) & 0xFF),
218 (uint8_t)((2 * j - i) & 0xFF));
Brian Osmancb3d0872018-10-16 15:19:28 -0400219 SkColor4f color4f = SkColor4f::FromColor(color);
Brian Salomonbc73eb42019-12-18 14:57:45 -0500220 // We only apply delta to the r,g, and b channels. This is because we're using this
221 // to test the canTweakAlphaForCoverage() optimization. A processor is allowed
222 // to use the input color's alpha in its calculation and report this optimization.
223 for (int i = 0; i < 3; i++) {
Brian Osmancb3d0872018-10-16 15:19:28 -0400224 if (color4f[i] > 0.5) {
225 color4f[i] -= delta;
Michael Ludwig7034f152018-10-08 16:43:58 -0400226 } else {
Brian Osmancb3d0872018-10-16 15:19:28 -0400227 color4f[i] += delta;
Michael Ludwig7034f152018-10-08 16:43:58 -0400228 }
229 }
Brian Osmancb3d0872018-10-16 15:19:28 -0400230 return color4f.premul().toBytes_RGBA();
Brian Salomon0e05a822017-07-25 09:43:22 -0400231}
Brian Salomon587e08f2017-01-27 10:59:27 -0500232
Robert Phillips7c525e62018-06-12 10:11:12 -0400233void test_draw_op(GrContext* context,
234 GrRenderTargetContext* rtc,
235 std::unique_ptr<GrFragmentProcessor> fp,
Greg Danielc594e622019-10-15 14:01:49 -0400236 sk_sp<GrTextureProxy> inputDataProxy,
Brian Salomonfc118442019-11-22 19:09:27 -0500237 SkAlphaType inputAlphaType) {
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500238 GrPaint paint;
Brian Salomonfc118442019-11-22 19:09:27 -0500239 paint.addColorTextureProcessor(std::move(inputDataProxy), inputAlphaType, SkMatrix::I());
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500240 paint.addColorFragmentProcessor(std::move(fp));
241 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonac70f842017-05-08 10:43:33 -0400242
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400243 auto op = GrFillRectOp::MakeNonAARect(context, std::move(paint), SkMatrix::I(),
244 SkRect::MakeWH(rtc->width(), rtc->height()));
Brian Salomonfc118442019-11-22 19:09:27 -0500245 rtc->priv().testingOnly_addDrawOp(std::move(op));
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500246}
247
Michael Ludwig7034f152018-10-08 16:43:58 -0400248// This assumes that the output buffer will be the same size as inputDataProxy
249void render_fp(GrContext* context, GrRenderTargetContext* rtc, GrFragmentProcessor* fp,
Brian Salomonfc118442019-11-22 19:09:27 -0500250 sk_sp<GrTextureProxy> inputDataProxy, SkAlphaType inputAlphaType, GrColor* buffer) {
Michael Ludwig7034f152018-10-08 16:43:58 -0400251 // test_draw_op needs to take ownership of an FP, so give it a clone that it can own
Brian Salomonfc118442019-11-22 19:09:27 -0500252 test_draw_op(context, rtc, fp->clone(), inputDataProxy, inputAlphaType);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400253 memset(buffer, 0x0, sizeof(GrColor) * inputDataProxy->width() * inputDataProxy->height());
254 rtc->readPixels(SkImageInfo::Make(inputDataProxy->dimensions(), kRGBA_8888_SkColorType,
255 kPremul_SkAlphaType),
Brian Salomon1d435302019-07-01 13:05:28 -0400256 buffer, 0, {0, 0});
Michael Ludwig7034f152018-10-08 16:43:58 -0400257}
258
Brian Salomon0e05a822017-07-25 09:43:22 -0400259/** Initializes the two test texture proxies that are available to the FP test factories. */
Robert Phillips7eeb74f2019-03-29 07:26:46 -0400260bool init_test_textures(GrResourceProvider* resourceProvider,
261 GrProxyProvider* proxyProvider,
262 SkRandom* random,
Brian Salomon766098d2019-12-18 10:41:58 -0500263 GrProcessorTestData::ProxyInfo proxies[2]) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400264 static const int kTestTextureSize = 256;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400265
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500266 {
267 // Put premul data into the RGBA texture that the test FPs can optionally use.
268 std::unique_ptr<GrColor[]> rgbaData(new GrColor[kTestTextureSize * kTestTextureSize]);
269 for (int y = 0; y < kTestTextureSize; ++y) {
270 for (int x = 0; x < kTestTextureSize; ++x) {
Michael Ludwig7034f152018-10-08 16:43:58 -0400271 rgbaData[kTestTextureSize * y + x] = input_texel_color(
272 random->nextULessThan(256), random->nextULessThan(256), 0.0f);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500273 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400274 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400275
Brian Osman2700abc2018-09-12 10:19:41 -0400276 SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize,
277 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
278 SkPixmap pixmap(ii, rgbaData.get(), ii.minRowBytes());
279 sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap);
Brian Salomon766098d2019-12-18 10:41:58 -0500280 auto proxy =
Brian Salomon96b383a2019-08-13 16:55:41 -0400281 proxyProvider->createTextureProxy(img, 1, SkBudgeted::kYes, SkBackingFit::kExact);
Brian Salomon766098d2019-12-18 10:41:58 -0500282 if (!proxy || !proxy->instantiate(resourceProvider)) {
283 return false;
284 }
285 proxies[0] = {std::move(proxy), GrColorType::kRGBA_8888, kPremul_SkAlphaType};
Brian Salomon0e05a822017-07-25 09:43:22 -0400286 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500287
288 {
289 // Put random values into the alpha texture that the test FPs can optionally use.
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500290 std::unique_ptr<uint8_t[]> alphaData(new uint8_t[kTestTextureSize * kTestTextureSize]);
291 for (int y = 0; y < kTestTextureSize; ++y) {
292 for (int x = 0; x < kTestTextureSize; ++x) {
293 alphaData[kTestTextureSize * y + x] = random->nextULessThan(256);
294 }
295 }
296
Brian Osman2700abc2018-09-12 10:19:41 -0400297 SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize,
298 kAlpha_8_SkColorType, kPremul_SkAlphaType);
299 SkPixmap pixmap(ii, alphaData.get(), ii.minRowBytes());
300 sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap);
Brian Salomon766098d2019-12-18 10:41:58 -0500301 auto proxy =
Brian Salomon96b383a2019-08-13 16:55:41 -0400302 proxyProvider->createTextureProxy(img, 1, SkBudgeted::kYes, SkBackingFit::kExact);
Brian Salomon766098d2019-12-18 10:41:58 -0500303 if (!proxy || !proxy->instantiate(resourceProvider)) {
304 return false;
305 }
306 proxies[1] = {std::move(proxy), GrColorType::kAlpha_8, kPremul_SkAlphaType};
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500307 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400308
Brian Salomon766098d2019-12-18 10:41:58 -0500309 return true;
Brian Salomon0e05a822017-07-25 09:43:22 -0400310}
311
312// Creates a texture of premul colors used as the output of the fragment processor that precedes
313// the fragment processor under test. Color values are those provided by input_texel_color().
Michael Ludwig7034f152018-10-08 16:43:58 -0400314sk_sp<GrTextureProxy> make_input_texture(GrProxyProvider* proxyProvider, int width, int height,
315 SkScalar delta) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400316 std::unique_ptr<GrColor[]> data(new GrColor[width * height]);
317 for (int y = 0; y < width; ++y) {
318 for (int x = 0; x < height; ++x) {
Michael Ludwig7034f152018-10-08 16:43:58 -0400319 data.get()[width * y + x] = input_texel_color(x, y, delta);
Brian Salomon0e05a822017-07-25 09:43:22 -0400320 }
321 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500322
Brian Osman2700abc2018-09-12 10:19:41 -0400323 SkImageInfo ii = SkImageInfo::Make(width, height, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
324 SkPixmap pixmap(ii, data.get(), ii.minRowBytes());
325 sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap);
Brian Salomon96b383a2019-08-13 16:55:41 -0400326 return proxyProvider->createTextureProxy(img, 1, SkBudgeted::kYes, SkBackingFit::kExact);
Brian Salomon0e05a822017-07-25 09:43:22 -0400327}
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500328
Brian Salomoncd8b6d52019-08-13 12:40:04 -0400329// We tag logged data as unpremul to avoid conversion when encoding as PNG. The input texture
330// actually contains unpremul data. Also, even though we made the result data by rendering into
331// a "unpremul" GrRenderTargetContext, our input texture is unpremul and outside of the random
332// effect configuration, we didn't do anything to ensure the output is actually premul. We just
333// don't currently allow kUnpremul GrRenderTargetContexts.
334static constexpr auto kLogAlphaType = kUnpremul_SkAlphaType;
Michael Ludwige8e10752018-10-01 12:42:53 -0400335
Brian Salomoncd8b6d52019-08-13 12:40:04 -0400336bool log_pixels(GrColor* pixels, int widthHeight, SkString* dst) {
337 auto info = SkImageInfo::Make(widthHeight, widthHeight, kRGBA_8888_SkColorType, kLogAlphaType);
338 SkBitmap bmp;
339 bmp.installPixels(info, pixels, widthHeight * sizeof(GrColor));
Brian Salomon28a8f282019-10-24 20:07:39 -0400340 return BipmapToBase64DataURI(bmp, dst);
Michael Ludwige8e10752018-10-01 12:42:53 -0400341}
342
Brian Salomoncd8b6d52019-08-13 12:40:04 -0400343bool log_texture_proxy(GrContext* context, sk_sp<GrTextureProxy> src, SkString* dst) {
Greg Danielbfa19c42019-12-19 16:41:40 -0500344 auto sContext = GrSurfaceContext::Make(context, src, GrColorType::kRGBA_8888, kLogAlphaType,
345 nullptr);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400346 SkImageInfo ii = SkImageInfo::Make(src->dimensions(), kRGBA_8888_SkColorType, kLogAlphaType);
Brian Salomoncd8b6d52019-08-13 12:40:04 -0400347 SkBitmap bm;
348 SkAssertResult(bm.tryAllocPixels(ii));
349 SkAssertResult(sContext->readPixels(ii, bm.getPixels(), bm.rowBytes(), {0, 0}));
Brian Salomon28a8f282019-10-24 20:07:39 -0400350 return BipmapToBase64DataURI(bm, dst);
Michael Ludwige8e10752018-10-01 12:42:53 -0400351}
352
Michael Ludwig7034f152018-10-08 16:43:58 -0400353bool fuzzy_color_equals(const SkPMColor4f& c1, const SkPMColor4f& c2) {
354 // With the loss of precision of rendering into 32-bit color, then estimating the FP's output
Brian Salomon682ba432019-12-17 20:20:23 +0000355 // from that, it is not uncommon for a valid output to differ from estimate by up to 0.01
Michael Ludwig7034f152018-10-08 16:43:58 -0400356 // (really 1/128 ~ .0078, but frequently floating point issues make that tolerance a little
357 // too unforgiving).
Brian Salomon682ba432019-12-17 20:20:23 +0000358 static constexpr SkScalar kTolerance = 0.01f;
Michael Ludwig7034f152018-10-08 16:43:58 -0400359 for (int i = 0; i < 4; i++) {
360 if (!SkScalarNearlyEqual(c1[i], c2[i], kTolerance)) {
361 return false;
362 }
363 }
364 return true;
365}
366
Brian Salomonbc73eb42019-12-18 14:57:45 -0500367// Given three input colors (color preceding the FP being tested) provided to the FP at the same
368// local coord and the three corresponding FP outputs, this ensures that either:
369// out[0] = fp * in[0].a, out[1] = fp * in[1].a, and out[2] = fp * in[2].a
370// where fp is the pre-modulated color that should not be changing across frames (FP's state doesn't
371// change), OR:
372// out[0] = fp * in[0], out[1] = fp * in[1], and out[2] = fp * in[2]
373// (per-channel modulation instead of modulation by just the alpha channel)
374// It does this by estimating the pre-modulated fp color from one of the input/output pairs and
375// confirms the conditions hold for the other two pairs.
376// It is required that the three input colors have the same alpha as fp is allowed to be a function
377// of the input alpha (but not r, g, or b).
378bool legal_modulation(const GrColor in[3], const GrColor out[3]) {
Michael Ludwig7034f152018-10-08 16:43:58 -0400379 // Convert to floating point, which is the number space the FP operates in (more or less)
Brian Salomonbc73eb42019-12-18 14:57:45 -0500380 SkPMColor4f inf[3], outf[3];
381 for (int i = 0; i < 3; ++i) {
382 inf[i] = SkPMColor4f::FromBytes_RGBA(in[i]);
383 outf[i] = SkPMColor4f::FromBytes_RGBA(out[i]);
384 }
385 // This test is only valid if all the input alphas are the same.
386 SkASSERT(inf[0].fA == inf[1].fA && inf[1].fA == inf[2].fA);
Michael Ludwig7034f152018-10-08 16:43:58 -0400387
388 // Reconstruct the output of the FP before the shader modulated its color with the input value.
389 // When the original input is very small, it may cause the final output color to round
390 // to 0, in which case we estimate the pre-modulated color using one of the stepped frames that
391 // will then have a guaranteed larger channel value (since the offset will be added to it).
Brian Salomon0a7ca7a2019-12-27 12:18:19 -0500392 SkPMColor4f fpPreColorModulation = {0,0,0,0};
393 SkPMColor4f fpPreAlphaModulation = {0,0,0,0};
Michael Ludwig7034f152018-10-08 16:43:58 -0400394 for (int i = 0; i < 4; i++) {
Brian Salomonbc73eb42019-12-18 14:57:45 -0500395 // Use the most stepped up frame
396 int maxInIdx = inf[0][i] > inf[1][i] ? 0 : 1;
397 maxInIdx = inf[maxInIdx][i] > inf[2][i] ? maxInIdx : 2;
398 const auto& in = inf[maxInIdx];
399 const auto& out = outf[maxInIdx];
Brian Salomon0a7ca7a2019-12-27 12:18:19 -0500400 if (in[i] > 0) {
401 fpPreColorModulation[i] = out[i] / in[i];
402 }
403 if (in[3] > 0) {
404 fpPreAlphaModulation[i] = out[i] / in[3];
405 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400406 }
407
408 // With reconstructed pre-modulated FP output, derive the expected value of fp * input for each
409 // of the transformed input colors.
Brian Salomonbc73eb42019-12-18 14:57:45 -0500410 SkPMColor4f expectedForAlphaModulation[3];
411 SkPMColor4f expectedForColorModulation[3];
412 for (int i = 0; i < 3; ++i) {
413 expectedForAlphaModulation[i] = fpPreAlphaModulation * inf[i].fA;
414 expectedForColorModulation[i] = fpPreColorModulation * inf[i];
415 // If the input alpha is 0 then the other channels should also be zero
416 // since the color is assumed to be premul. Modulating zeros by anything
417 // should produce zeros.
418 if (inf[i].fA == 0) {
419 SkASSERT(inf[i].fR == 0 && inf[i].fG == 0 && inf[i].fB == 0);
420 expectedForColorModulation[i] = expectedForAlphaModulation[i] = {0, 0, 0, 0};
421 }
422 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400423
Brian Salomonbc73eb42019-12-18 14:57:45 -0500424 bool isLegalColorModulation = fuzzy_color_equals(outf[0], expectedForColorModulation[0]) &&
425 fuzzy_color_equals(outf[1], expectedForColorModulation[1]) &&
426 fuzzy_color_equals(outf[2], expectedForColorModulation[2]);
427
428 bool isLegalAlphaModulation = fuzzy_color_equals(outf[0], expectedForAlphaModulation[0]) &&
429 fuzzy_color_equals(outf[1], expectedForAlphaModulation[1]) &&
430 fuzzy_color_equals(outf[2], expectedForAlphaModulation[2]);
431
432 // This can be enabled to print the values that caused this check to fail.
433 if (0 && !isLegalColorModulation && !isLegalAlphaModulation) {
434 SkDebugf("Color modulation test\n\timplied mod color: (%.03f, %.03f, %.03f, %.03f)\n",
435 fpPreColorModulation[0],
436 fpPreColorModulation[1],
437 fpPreColorModulation[2],
438 fpPreColorModulation[3]);
439 for (int i = 0; i < 3; ++i) {
440 SkDebugf("\t(%.03f, %.03f, %.03f, %.03f) -> "
441 "(%.03f, %.03f, %.03f, %.03f) | "
442 "(%.03f, %.03f, %.03f, %.03f), ok: %d\n",
443 inf[i].fR, inf[i].fG, inf[i].fB, inf[i].fA,
444 outf[i].fR, outf[i].fG, outf[i].fB, outf[i].fA,
445 expectedForColorModulation[i].fR, expectedForColorModulation[i].fG,
446 expectedForColorModulation[i].fB, expectedForColorModulation[i].fA,
447 fuzzy_color_equals(outf[i], expectedForColorModulation[i]));
448 }
449 SkDebugf("Alpha modulation test\n\timplied mod color: (%.03f, %.03f, %.03f, %.03f)\n",
450 fpPreAlphaModulation[0],
451 fpPreAlphaModulation[1],
452 fpPreAlphaModulation[2],
453 fpPreAlphaModulation[3]);
454 for (int i = 0; i < 3; ++i) {
455 SkDebugf("\t(%.03f, %.03f, %.03f, %.03f) -> "
456 "(%.03f, %.03f, %.03f, %.03f) | "
457 "(%.03f, %.03f, %.03f, %.03f), ok: %d\n",
458 inf[i].fR, inf[i].fG, inf[i].fB, inf[i].fA,
459 outf[i].fR, outf[i].fG, outf[i].fB, outf[i].fA,
460 expectedForAlphaModulation[i].fR, expectedForAlphaModulation[i].fG,
461 expectedForAlphaModulation[i].fB, expectedForAlphaModulation[i].fA,
462 fuzzy_color_equals(outf[i], expectedForAlphaModulation[i]));
463 }
464 }
465 return isLegalColorModulation || isLegalAlphaModulation;
Michael Ludwig7034f152018-10-08 16:43:58 -0400466}
467
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500468DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500469 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500470 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
471 auto resourceProvider = context->priv().resourceProvider();
Brian Salomon1c053642017-07-24 10:16:19 -0400472 using FPFactory = GrFragmentProcessorTestFactory;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400473
Michael Ludwig3ad86d02018-09-28 16:00:38 -0400474 uint32_t seed = FLAGS_processorSeed;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400475 if (FLAGS_randomProcessorTest) {
476 std::random_device rd;
477 seed = rd();
478 }
479 // If a non-deterministic bot fails this test, check the output to see what seed it used, then
Michael Ludwig3ad86d02018-09-28 16:00:38 -0400480 // use --processorSeed <seed> (without --randomProcessorTest) to reproduce.
Brian Osmanc35a2d42017-03-17 10:58:53 -0400481 SkRandom random(seed);
482
Brian Salomon0e05a822017-07-25 09:43:22 -0400483 // Make the destination context for the test.
484 static constexpr int kRenderSize = 256;
Brian Salomonbf6b9792019-08-21 09:38:10 -0400485 auto rtc = context->priv().makeDeferredRenderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -0400486 SkBackingFit::kExact, kRenderSize, kRenderSize, GrColorType::kRGBA_8888, nullptr);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500487
Brian Salomon766098d2019-12-18 10:41:58 -0500488 GrProcessorTestData::ProxyInfo proxies[2];
Brian Salomonfc118442019-11-22 19:09:27 -0500489 if (!init_test_textures(resourceProvider, proxyProvider, &random, proxies)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400490 ERRORF(reporter, "Could not create test textures");
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400491 return;
492 }
Brian Salomon766098d2019-12-18 10:41:58 -0500493 GrProcessorTestData testData(&random, context, 2, proxies);
Brian Salomon587e08f2017-01-27 10:59:27 -0500494
Michael Ludwig7034f152018-10-08 16:43:58 -0400495 // Coverage optimization uses three frames with a linearly transformed input texture. The first
496 // frame has no offset, second frames add .2 and .4, which should then be present as a fixed
497 // difference between the frame outputs if the FP is properly following the modulation
498 // requirements of the coverage optimization.
499 static constexpr SkScalar kInputDelta = 0.2f;
500 auto inputTexture1 = make_input_texture(proxyProvider, kRenderSize, kRenderSize, 0.0f);
501 auto inputTexture2 = make_input_texture(proxyProvider, kRenderSize, kRenderSize, kInputDelta);
502 auto inputTexture3 = make_input_texture(proxyProvider, kRenderSize, kRenderSize, 2*kInputDelta);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500503
Michael Ludwige8e10752018-10-01 12:42:53 -0400504 // Encoded images are very verbose and this tests many potential images, so only export the
505 // first failure (subsequent failures have a reasonable chance of being related).
506 bool loggedFirstFailure = false;
Michael Ludwig314d3772018-10-03 16:04:38 -0400507 bool loggedFirstWarning = false;
Michael Ludwig7034f152018-10-08 16:43:58 -0400508
509 // Storage for the three frames required for coverage compatibility optimization. Each frame
510 // uses the correspondingly numbered inputTextureX.
511 std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
512 std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
513 std::unique_ptr<GrColor[]> readData3(new GrColor[kRenderSize * kRenderSize]);
514
Brian Salomon0e05a822017-07-25 09:43:22 -0400515 // Because processor factories configure themselves in random ways, this is not exhaustive.
Brian Salomon587e08f2017-01-27 10:59:27 -0500516 for (int i = 0; i < FPFactory::Count(); ++i) {
517 int timesToInvokeFactory = 5;
518 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
519 // on child optimizations being present.
Brian Salomonaff329b2017-08-11 09:40:37 -0400520 std::unique_ptr<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
Brian Salomon587e08f2017-01-27 10:59:27 -0500521 for (int j = 0; j < fp->numChildProcessors(); ++j) {
522 // This value made a reasonable trade off between time and coverage when this test was
523 // written.
524 timesToInvokeFactory *= FPFactory::Count() / 2;
525 }
Brian Osman50ea3c02019-02-04 10:01:53 -0500526#if defined(__MSVC_RUNTIME_CHECKS)
527 // This test is infuriatingly slow with MSVC runtime checks enabled
528 timesToInvokeFactory = 1;
529#endif
Brian Salomon587e08f2017-01-27 10:59:27 -0500530 for (int j = 0; j < timesToInvokeFactory; ++j) {
531 fp = FPFactory::MakeIdx(i, &testData);
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400532
Brian Salomon587e08f2017-01-27 10:59:27 -0500533 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
Brian Salomonf3b995b2017-02-15 10:22:23 -0500534 !fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500535 continue;
536 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400537
Michael Ludwig7034f152018-10-08 16:43:58 -0400538 if (fp->compatibleWithCoverageAsAlpha()) {
539 // 2nd and 3rd frames are only used when checking coverage optimization
Brian Salomonfc118442019-11-22 19:09:27 -0500540 render_fp(context, rtc.get(), fp.get(), inputTexture2, kPremul_SkAlphaType,
Greg Danielc594e622019-10-15 14:01:49 -0400541 readData2.get());
Brian Salomonfc118442019-11-22 19:09:27 -0500542 render_fp(context, rtc.get(), fp.get(), inputTexture3, kPremul_SkAlphaType,
Greg Danielc594e622019-10-15 14:01:49 -0400543 readData3.get());
Michael Ludwig7034f152018-10-08 16:43:58 -0400544 }
545 // Draw base frame last so that rtc holds the original FP behavior if we need to
546 // dump the image to the log.
Brian Salomonfc118442019-11-22 19:09:27 -0500547 render_fp(context, rtc.get(), fp.get(), inputTexture1, kPremul_SkAlphaType,
Greg Danielc594e622019-10-15 14:01:49 -0400548 readData1.get());
Brian Salomonaff329b2017-08-11 09:40:37 -0400549
Brian Salomon587e08f2017-01-27 10:59:27 -0500550 if (0) { // Useful to see what FPs are being tested.
551 SkString children;
Michael Ludwig7034f152018-10-08 16:43:58 -0400552 for (int c = 0; c < fp->numChildProcessors(); ++c) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500553 if (!c) {
554 children.append("(");
555 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400556 children.append(fp->childProcessor(c).name());
557 children.append(c == fp->numChildProcessors() - 1 ? ")" : ", ");
Brian Salomon587e08f2017-01-27 10:59:27 -0500558 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400559 SkDebugf("%s %s\n", fp->name(), children.c_str());
Brian Salomon587e08f2017-01-27 10:59:27 -0500560 }
Michael Ludwig314d3772018-10-03 16:04:38 -0400561
562 // This test has a history of being flaky on a number of devices. If an FP is logically
563 // violating the optimizations, it's reasonable to expect it to violate requirements on
564 // a large number of pixels in the image. Sporadic pixel violations are more indicative
565 // of device errors and represents a separate problem.
Michael Ludwig72ab3462018-12-10 12:43:36 -0500566#if defined(SK_BUILD_FOR_SKQP)
Michael Ludwig314d3772018-10-03 16:04:38 -0400567 static constexpr int kMaxAcceptableFailedPixels = 0; // Strict when running as SKQP
568#else
569 static constexpr int kMaxAcceptableFailedPixels = 2 * kRenderSize; // ~0.7% of the image
570#endif
571
572 int failedPixelCount = 0;
573 // Collect first optimization failure message, to be output later as a warning or an
574 // error depending on whether the rendering "passed" or failed.
575 SkString coverageMessage;
576 SkString opaqueMessage;
577 SkString constMessage;
578 for (int y = 0; y < kRenderSize; ++y) {
579 for (int x = 0; x < kRenderSize; ++x) {
580 bool passing = true;
Michael Ludwig7034f152018-10-08 16:43:58 -0400581 GrColor input = input_texel_color(x, y, 0.0f);
582 GrColor output = readData1.get()[y * kRenderSize + x];
583
584 if (fp->compatibleWithCoverageAsAlpha()) {
Brian Salomonbc73eb42019-12-18 14:57:45 -0500585 GrColor ins[3];
586 ins[0] = input;
587 ins[1] = input_texel_color(x, y, kInputDelta);
588 ins[2] = input_texel_color(x, y, 2 * kInputDelta);
Michael Ludwig7034f152018-10-08 16:43:58 -0400589
Brian Salomonbc73eb42019-12-18 14:57:45 -0500590 GrColor outs[3];
591 outs[0] = output;
592 outs[1] = readData2.get()[y * kRenderSize + x];
593 outs[2] = readData3.get()[y * kRenderSize + x];
Michael Ludwig7034f152018-10-08 16:43:58 -0400594
Brian Salomonbc73eb42019-12-18 14:57:45 -0500595 if (!legal_modulation(ins, outs)) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500596 passing = false;
Michael Ludwig314d3772018-10-03 16:04:38 -0400597 if (coverageMessage.isEmpty()) {
Brian Salomonbc73eb42019-12-18 14:57:45 -0500598 coverageMessage.printf(
599 "\"Modulating\" processor %s did not match "
Michael Ludwig7034f152018-10-08 16:43:58 -0400600 "alpha-modulation nor color-modulation rules. "
601 "Input: 0x%08x, Output: 0x%08x, pixel (%d, %d).",
602 fp->name(), input, output, x, y);
Michael Ludwig314d3772018-10-03 16:04:38 -0400603 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500604 }
605 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400606
Brian Osmancb3d0872018-10-16 15:19:28 -0400607 SkPMColor4f input4f = SkPMColor4f::FromBytes_RGBA(input);
608 SkPMColor4f output4f = SkPMColor4f::FromBytes_RGBA(output);
Brian Osman1d5b5982018-10-01 13:41:39 -0400609 SkPMColor4f expected4f;
Michael Ludwig7034f152018-10-08 16:43:58 -0400610 if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) {
Brian Osmancb3d0872018-10-16 15:19:28 -0400611 float rDiff = fabsf(output4f.fR - expected4f.fR);
612 float gDiff = fabsf(output4f.fG - expected4f.fG);
613 float bDiff = fabsf(output4f.fB - expected4f.fB);
614 float aDiff = fabsf(output4f.fA - expected4f.fA);
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500615 static constexpr float kTol = 4 / 255.f;
Brian Salomon587e08f2017-01-27 10:59:27 -0500616 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
Michael Ludwig314d3772018-10-03 16:04:38 -0400617 if (constMessage.isEmpty()) {
618 passing = false;
619
620 constMessage.printf("Processor %s claimed output for const input "
621 "doesn't match actual output. Error: %f, Tolerance: %f, "
622 "input: (%f, %f, %f, %f), actual: (%f, %f, %f, %f), "
Michael Ludwig7034f152018-10-08 16:43:58 -0400623 "expected(%f, %f, %f, %f)", fp->name(),
Michael Ludwig314d3772018-10-03 16:04:38 -0400624 SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))), kTol,
625 input4f.fR, input4f.fG, input4f.fB, input4f.fA,
Brian Osmancb3d0872018-10-16 15:19:28 -0400626 output4f.fR, output4f.fG, output4f.fB, output4f.fA,
627 expected4f.fR, expected4f.fG, expected4f.fB, expected4f.fA);
Michael Ludwig314d3772018-10-03 16:04:38 -0400628 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500629 }
630 }
Brian Osman00b29392018-11-05 15:42:43 -0500631 if (input4f.isOpaque() && fp->preservesOpaqueInput() && !output4f.isOpaque()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500632 passing = false;
Michael Ludwig314d3772018-10-03 16:04:38 -0400633
634 if (opaqueMessage.isEmpty()) {
635 opaqueMessage.printf("Processor %s claimed opaqueness is preserved but "
636 "it is not. Input: 0x%08x, Output: 0x%08x.",
Michael Ludwig7034f152018-10-08 16:43:58 -0400637 fp->name(), input, output);
Michael Ludwige8e10752018-10-01 12:42:53 -0400638 }
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400639 }
Michael Ludwig314d3772018-10-03 16:04:38 -0400640
641 if (!passing) {
642 // Regardless of how many optimizations the pixel violates, count it as a
643 // single bad pixel.
644 failedPixelCount++;
645 }
646 }
647 }
648
649 // Finished analyzing the entire image, see if the number of pixel failures meets the
650 // threshold for an FP violating the optimization requirements.
651 if (failedPixelCount > kMaxAcceptableFailedPixels) {
Michael Ludwig4504a6522018-10-03 16:47:55 -0400652 ERRORF(reporter, "Processor violated %d of %d pixels, seed: 0x%08x, processor: %s"
Michael Ludwig314d3772018-10-03 16:04:38 -0400653 ", first failing pixel details are below:",
654 failedPixelCount, kRenderSize * kRenderSize, seed,
Michael Ludwig7034f152018-10-08 16:43:58 -0400655 fp->dumpInfo().c_str());
Michael Ludwig314d3772018-10-03 16:04:38 -0400656
657 // Print first failing pixel's details.
658 if (!coverageMessage.isEmpty()) {
659 ERRORF(reporter, coverageMessage.c_str());
660 }
661 if (!constMessage.isEmpty()) {
662 ERRORF(reporter, constMessage.c_str());
663 }
664 if (!opaqueMessage.isEmpty()) {
665 ERRORF(reporter, opaqueMessage.c_str());
666 }
667
668 if (!loggedFirstFailure) {
669 // Print with ERRORF to make sure the encoded image is output
670 SkString input;
Brian Salomoncd8b6d52019-08-13 12:40:04 -0400671 log_texture_proxy(context, inputTexture1, &input);
Michael Ludwig314d3772018-10-03 16:04:38 -0400672 SkString output;
Brian Salomoncd8b6d52019-08-13 12:40:04 -0400673 log_pixels(readData1.get(), kRenderSize, &output);
Michael Ludwig314d3772018-10-03 16:04:38 -0400674 ERRORF(reporter, "Input image: %s\n\n"
675 "===========================================================\n\n"
676 "Output image: %s\n", input.c_str(), output.c_str());
677 loggedFirstFailure = true;
678 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400679 } else if(failedPixelCount > 0) {
Michael Ludwig314d3772018-10-03 16:04:38 -0400680 // Don't trigger an error, but don't just hide the failures either.
681 INFOF(reporter, "Processor violated %d of %d pixels (below error threshold), seed: "
682 "0x%08x, processor: %s", failedPixelCount, kRenderSize * kRenderSize,
Michael Ludwig7034f152018-10-08 16:43:58 -0400683 seed, fp->dumpInfo().c_str());
Michael Ludwig314d3772018-10-03 16:04:38 -0400684 if (!coverageMessage.isEmpty()) {
685 INFOF(reporter, coverageMessage.c_str());
686 }
687 if (!constMessage.isEmpty()) {
688 INFOF(reporter, constMessage.c_str());
689 }
690 if (!opaqueMessage.isEmpty()) {
691 INFOF(reporter, opaqueMessage.c_str());
692 }
693 if (!loggedFirstWarning) {
694 SkString input;
Brian Salomoncd8b6d52019-08-13 12:40:04 -0400695 log_texture_proxy(context, inputTexture1, &input);
Michael Ludwig314d3772018-10-03 16:04:38 -0400696 SkString output;
Brian Salomoncd8b6d52019-08-13 12:40:04 -0400697 log_pixels(readData1.get(), kRenderSize, &output);
Michael Ludwig314d3772018-10-03 16:04:38 -0400698 INFOF(reporter, "Input image: %s\n\n"
699 "===========================================================\n\n"
700 "Output image: %s\n", input.c_str(), output.c_str());
701 loggedFirstWarning = true;
Brian Salomon587e08f2017-01-27 10:59:27 -0500702 }
703 }
704 }
705 }
706}
Robert Phillips18166ee2017-06-01 12:55:44 -0400707
Brian Salomon0e05a822017-07-25 09:43:22 -0400708// Tests that fragment processors returned by GrFragmentProcessor::clone() are equivalent to their
709// progenitors.
710DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) {
711 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500712 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
713 auto resourceProvider = context->priv().resourceProvider();
Brian Salomon0e05a822017-07-25 09:43:22 -0400714
715 SkRandom random;
716
717 // Make the destination context for the test.
718 static constexpr int kRenderSize = 1024;
Brian Salomonbf6b9792019-08-21 09:38:10 -0400719 auto rtc = context->priv().makeDeferredRenderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -0400720 SkBackingFit::kExact, kRenderSize, kRenderSize, GrColorType::kRGBA_8888, nullptr);
Brian Salomon0e05a822017-07-25 09:43:22 -0400721
Brian Salomon766098d2019-12-18 10:41:58 -0500722 GrProcessorTestData::ProxyInfo proxies[2];
Brian Salomonfc118442019-11-22 19:09:27 -0500723 if (!init_test_textures(resourceProvider, proxyProvider, &random, proxies)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400724 ERRORF(reporter, "Could not create test textures");
725 return;
726 }
Brian Salomon766098d2019-12-18 10:41:58 -0500727 GrProcessorTestData testData(&random, context, 2, proxies);
Brian Salomon0e05a822017-07-25 09:43:22 -0400728
Michael Ludwig7034f152018-10-08 16:43:58 -0400729 auto inputTexture = make_input_texture(proxyProvider, kRenderSize, kRenderSize, 0.0f);
Brian Salomon0e05a822017-07-25 09:43:22 -0400730 std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
731 std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
Brian Salomoncd8b6d52019-08-13 12:40:04 -0400732 // On failure we write out images, but just write the first failing set as the print is very
733 // large.
734 bool loggedFirstFailure = false;
735
736 // This test has a history of being flaky on a number of devices. If an FP clone is logically
737 // wrong, it's reasonable to expect it produce a large number of pixel differences in the image
738 // Sporadic pixel violations are more indicative device errors and represents a separate
739 // problem.
740#if defined(SK_BUILD_FOR_SKQP)
741 static constexpr int kMaxAcceptableFailedPixels = 0; // Strict when running as SKQP
742#else
743 static constexpr int kMaxAcceptableFailedPixels = 2 * kRenderSize; // ~0.7% of the image
744#endif
Brian Salomon0e05a822017-07-25 09:43:22 -0400745
746 // Because processor factories configure themselves in random ways, this is not exhaustive.
747 for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) {
748 static constexpr int kTimesToInvokeFactory = 10;
749 for (int j = 0; j < kTimesToInvokeFactory; ++j) {
750 auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &testData);
751 auto clone = fp->clone();
Brian Salomon0e05a822017-07-25 09:43:22 -0400752 if (!clone) {
Brian Salomon96271cd2017-07-31 16:27:23 -0400753 ERRORF(reporter, "Clone of processor %s failed.", fp->name());
Brian Salomon0e05a822017-07-25 09:43:22 -0400754 continue;
755 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400756 const char* name = fp->name();
Brian Salomonce06e262017-08-01 16:23:40 -0400757 REPORTER_ASSERT(reporter, !strcmp(fp->name(), clone->name()));
758 REPORTER_ASSERT(reporter, fp->compatibleWithCoverageAsAlpha() ==
759 clone->compatibleWithCoverageAsAlpha());
760 REPORTER_ASSERT(reporter, fp->isEqual(*clone));
761 REPORTER_ASSERT(reporter, fp->preservesOpaqueInput() == clone->preservesOpaqueInput());
762 REPORTER_ASSERT(reporter, fp->hasConstantOutputForConstantInput() ==
763 clone->hasConstantOutputForConstantInput());
764 REPORTER_ASSERT(reporter, fp->numChildProcessors() == clone->numChildProcessors());
765 REPORTER_ASSERT(reporter, fp->usesLocalCoords() == clone->usesLocalCoords());
Brian Salomon0e05a822017-07-25 09:43:22 -0400766 // Draw with original and read back the results.
Brian Salomonfc118442019-11-22 19:09:27 -0500767 render_fp(context, rtc.get(), fp.get(), inputTexture, kPremul_SkAlphaType,
Greg Danielc594e622019-10-15 14:01:49 -0400768 readData1.get());
Brian Salomon0e05a822017-07-25 09:43:22 -0400769
770 // Draw with clone and read back the results.
Brian Salomonfc118442019-11-22 19:09:27 -0500771 render_fp(context, rtc.get(), clone.get(), inputTexture, kPremul_SkAlphaType,
Greg Danielc594e622019-10-15 14:01:49 -0400772 readData2.get());
Brian Salomon0e05a822017-07-25 09:43:22 -0400773
774 // Check that the results are the same.
775 bool passing = true;
Brian Salomoncd8b6d52019-08-13 12:40:04 -0400776 int failedPixelCount = 0;
777 int firstWrongX = 0;
778 int firstWrongY = 0;
Brian Salomon0e05a822017-07-25 09:43:22 -0400779 for (int y = 0; y < kRenderSize && passing; ++y) {
780 for (int x = 0; x < kRenderSize && passing; ++x) {
781 int idx = y * kRenderSize + x;
782 if (readData1[idx] != readData2[idx]) {
Brian Salomoncd8b6d52019-08-13 12:40:04 -0400783 if (!failedPixelCount) {
784 firstWrongX = x;
785 firstWrongY = y;
786 }
787 ++failedPixelCount;
788 }
789 if (failedPixelCount > kMaxAcceptableFailedPixels) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400790 passing = false;
Brian Salomoncd8b6d52019-08-13 12:40:04 -0400791 idx = firstWrongY * kRenderSize + firstWrongX;
792 ERRORF(reporter,
793 "Processor %s made clone produced different output at (%d, %d). "
794 "Input color: 0x%08x, Original Output Color: 0x%08x, "
795 "Clone Output Color: 0x%08x.",
796 name, firstWrongX, firstWrongY, input_texel_color(x, y, 0.0f),
797 readData1[idx], readData2[idx]);
798 if (!loggedFirstFailure) {
799 // Write the images out as data urls for inspection.
800 // We mark the data as unpremul to avoid conversion when encoding as
801 // PNG. Also, even though we made the data by rendering into
802 // a "unpremul" GrRenderTargetContext, our input texture is unpremul and
803 // outside of the random effect configuration, we didn't do anything to
804 // ensure the output is actually premul.
805 auto info = SkImageInfo::Make(kRenderSize, kRenderSize,
806 kRGBA_8888_SkColorType,
807 kUnpremul_SkAlphaType);
808 SkString input, orig, clone;
809 if (log_texture_proxy(context, inputTexture, &input) &&
810 log_pixels(readData1.get(), kRenderSize, &orig) &&
811 log_pixels(readData2.get(), kRenderSize, &clone)) {
812 ERRORF(reporter,
813 "\nInput image:\n%s\n\n"
814 "==========================================================="
815 "\n\n"
816 "Orig output image:\n%s\n"
817 "==========================================================="
818 "\n\n"
819 "Clone output image:\n%s\n",
820 input.c_str(), orig.c_str(), clone.c_str());
821 loggedFirstFailure = true;
822 }
823 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400824 }
825 }
826 }
827 }
828 }
829}
830
Hal Canary6f6961e2017-01-31 13:50:44 -0500831#endif // GR_TEST_UTILS