blob: 9503e32e5139bd6c7b0ea29182a3652dc45ea040 [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 "include/core/SkTypes.h"
9#include "tests/Test.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrContext.h"
12#include "include/gpu/GrGpuResource.h"
13#include "src/gpu/GrClip.h"
14#include "src/gpu/GrContextPriv.h"
15#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"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050025
Mike Klein0ec1c572018-12-04 11:52:51 -050026#include <atomic>
Hal Canary8a001442018-09-19 11:31:27 -040027#include <random>
28
Brian Salomonbc6b99d2017-01-11 10:32:34 -050029namespace {
Brian Salomon82ddc942017-07-14 12:00:13 -040030class TestOp : public GrMeshDrawOp {
Brian Salomonbc6b99d2017-01-11 10:32:34 -050031public:
32 DEFINE_OP_CLASS_ID
Robert Phillips7c525e62018-06-12 10:11:12 -040033 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
34 std::unique_ptr<GrFragmentProcessor> fp) {
Robert Phillips9da87e02019-02-04 13:26:26 -050035 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -040036
37 return pool->allocate<TestOp>(std::move(fp));
Brian Salomon82ddc942017-07-14 12:00:13 -040038 }
39
Robert Phillips5f567c72017-09-14 08:27:37 -040040 const char* name() const override { return "TestOp"; }
41
Chris Dalton1706cbf2019-05-21 19:35:29 -060042 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillips5f567c72017-09-14 08:27:37 -040043 fProcessors.visitProxies(func);
44 }
45
Brian Salomon82ddc942017-07-14 12:00:13 -040046 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
47
Chris Dalton6ce447a2019-06-23 18:07:38 -060048 GrProcessorSet::Analysis finalize(
49 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
50 GrClampType clampType) override {
Brian Salomon82ddc942017-07-14 12:00:13 -040051 static constexpr GrProcessorAnalysisColor kUnknownColor;
Brian Osmancf860852018-10-31 14:04:39 -040052 SkPMColor4f overrideColor;
Chris Daltonb8fff0d2019-03-05 10:11:58 -070053 return fProcessors.finalize(
54 kUnknownColor, GrProcessorAnalysisCoverage::kNone, clip,
Chris Dalton6ce447a2019-06-23 18:07:38 -060055 &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps, clampType,
56 &overrideColor);
Brian Salomon649a3412017-03-09 13:50:43 -050057 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050058
59private:
Robert Phillips7c525e62018-06-12 10:11:12 -040060 friend class ::GrOpMemoryPool; // for ctor
61
Brian Salomonaff329b2017-08-11 09:40:37 -040062 TestOp(std::unique_ptr<GrFragmentProcessor> fp)
63 : INHERITED(ClassID()), fProcessors(std::move(fp)) {
Brian Salomon82ddc942017-07-14 12:00:13 -040064 this->setBounds(SkRect::MakeWH(100, 100), HasAABloat::kNo, IsZeroArea::kNo);
65 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050066
Brian Salomon91326c32017-08-09 16:02:19 -040067 void onPrepareDraws(Target* target) override { return; }
Chris Dalton07cdcfc92019-02-26 11:13:22 -070068 void onExecute(GrOpFlushState*, const SkRect&) override { return; }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050069
Brian Salomon82ddc942017-07-14 12:00:13 -040070 GrProcessorSet fProcessors;
71
72 typedef GrMeshDrawOp INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -050073};
74
75/**
Robert Phillips3d4cac52019-06-11 08:08:08 -040076 * 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 -050077 * of resources owned by child FPs.
78 */
79class TestFP : public GrFragmentProcessor {
80public:
Brian Salomonaff329b2017-08-11 09:40:37 -040081 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> child) {
82 return std::unique_ptr<GrFragmentProcessor>(new TestFP(std::move(child)));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050083 }
Brian Salomonaff329b2017-08-11 09:40:37 -040084 static std::unique_ptr<GrFragmentProcessor> Make(const SkTArray<sk_sp<GrTextureProxy>>& proxies,
Brian Salomondbf70722019-02-07 11:31:24 -050085 const SkTArray<sk_sp<GrGpuBuffer>>& buffers) {
Brian Salomon559f5562017-11-15 14:28:33 -050086 return std::unique_ptr<GrFragmentProcessor>(new TestFP(proxies, buffers));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050087 }
88
89 const char* name() const override { return "test"; }
90
91 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
Mike Klein0ec1c572018-12-04 11:52:51 -050092 static std::atomic<int32_t> nextKey{0};
93 b->add32(nextKey++);
Brian Salomonbc6b99d2017-01-11 10:32:34 -050094 }
95
Brian Salomonaff329b2017-08-11 09:40:37 -040096 std::unique_ptr<GrFragmentProcessor> clone() const override {
97 return std::unique_ptr<GrFragmentProcessor>(new TestFP(*this));
Brian Salomonb17e6392017-07-28 13:41:51 -040098 }
99
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500100private:
Brian Salomondbf70722019-02-07 11:31:24 -0500101 TestFP(const SkTArray<sk_sp<GrTextureProxy>>& proxies,
102 const SkTArray<sk_sp<GrGpuBuffer>>& buffers)
Brian Salomon662ea4b2018-07-12 14:53:49 -0400103 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4) {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500104 for (const auto& proxy : proxies) {
Brian Salomonf7dcd762018-07-30 14:48:15 -0400105 fSamplers.emplace_back(proxy);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500106 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400107 this->setTextureSamplerCnt(fSamplers.count());
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500108 }
109
Brian Salomonaff329b2017-08-11 09:40:37 -0400110 TestFP(std::unique_ptr<GrFragmentProcessor> child)
Brian Salomon662ea4b2018-07-12 14:53:49 -0400111 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4) {
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500112 this->registerChildProcessor(std::move(child));
113 }
114
Brian Salomon96271cd2017-07-31 16:27:23 -0400115 explicit TestFP(const TestFP& that)
Brian Salomon662ea4b2018-07-12 14:53:49 -0400116 : INHERITED(kTestFP_ClassID, that.optimizationFlags()), fSamplers(4) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400117 for (int i = 0; i < that.fSamplers.count(); ++i) {
118 fSamplers.emplace_back(that.fSamplers[i]);
Brian Salomonb17e6392017-07-28 13:41:51 -0400119 }
Brian Salomon96271cd2017-07-31 16:27:23 -0400120 for (int i = 0; i < that.numChildProcessors(); ++i) {
121 this->registerChildProcessor(that.childProcessor(i).clone());
Brian Salomonb17e6392017-07-28 13:41:51 -0400122 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400123 this->setTextureSamplerCnt(fSamplers.count());
Brian Salomonb17e6392017-07-28 13:41:51 -0400124 }
125
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500126 virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
127 class TestGLSLFP : public GrGLSLFragmentProcessor {
128 public:
129 TestGLSLFP() {}
130 void emitCode(EmitArgs& args) override {
131 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
132 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor);
133 }
134
135 private:
136 };
137 return new TestGLSLFP();
138 }
139
140 bool onIsEqual(const GrFragmentProcessor&) const override { return false; }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400141 const TextureSampler& onTextureSampler(int i) const override { return fSamplers[i]; }
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500142
143 GrTAllocator<TextureSampler> fSamplers;
Brian Salomon587e08f2017-01-27 10:59:27 -0500144 typedef GrFragmentProcessor INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500145};
146}
147
Robert Phillips3d4cac52019-06-11 08:08:08 -0400148static void check_refs(skiatest::Reporter* reporter,
149 GrTextureProxy* proxy,
150 int32_t expectedProxyRefs,
151 int32_t expectedBackingRefs) {
152 int32_t actualProxyRefs = proxy->priv().getProxyRefCnt();
Robert Phillipsb5204762019-06-19 14:12:13 -0400153 int32_t actualBackingRefs = proxy->testingOnly_getBackingRefCnt();
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500154
Robert Phillips3d4cac52019-06-11 08:08:08 -0400155 SkASSERT(actualProxyRefs == expectedProxyRefs);
156 SkASSERT(actualBackingRefs == expectedBackingRefs);
157
158 REPORTER_ASSERT(reporter, actualProxyRefs == expectedProxyRefs);
159 REPORTER_ASSERT(reporter, actualBackingRefs == expectedBackingRefs);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500160}
161
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500162DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) {
163 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500164 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500165
Robert Phillips16d8ec62017-07-27 16:16:25 -0400166 GrSurfaceDesc desc;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500167 desc.fWidth = 10;
168 desc.fHeight = 10;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400169 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500170
Greg Daniel4065d452018-11-16 15:43:41 -0500171 const GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500172 context->priv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Greg Daniel4065d452018-11-16 15:43:41 -0500173
Brian Salomonb17e6392017-07-28 13:41:51 -0400174 for (bool makeClone : {false, true}) {
175 for (int parentCnt = 0; parentCnt < 2; parentCnt++) {
176 sk_sp<GrRenderTargetContext> renderTargetContext(
Robert Phillips9da87e02019-02-04 13:26:26 -0500177 context->priv().makeDeferredRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -0400178 format, SkBackingFit::kApprox, 1, 1, kRGBA_8888_GrPixelConfig,
179 GrColorType::kRGBA_8888, nullptr));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500180 {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400181 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500182 format, desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact,
183 SkBudgeted::kYes);
Robert Phillips3d4cac52019-06-11 08:08:08 -0400184
Brian Salomonb17e6392017-07-28 13:41:51 -0400185 {
186 SkTArray<sk_sp<GrTextureProxy>> proxies;
Brian Salomondbf70722019-02-07 11:31:24 -0500187 SkTArray<sk_sp<GrGpuBuffer>> buffers;
Robert Phillips3d4cac52019-06-11 08:08:08 -0400188 proxies.push_back(proxy);
Brian Salomon559f5562017-11-15 14:28:33 -0500189 auto fp = TestFP::Make(std::move(proxies), std::move(buffers));
Brian Salomonb17e6392017-07-28 13:41:51 -0400190 for (int i = 0; i < parentCnt; ++i) {
191 fp = TestFP::Make(std::move(fp));
192 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400193 std::unique_ptr<GrFragmentProcessor> clone;
Brian Salomonb17e6392017-07-28 13:41:51 -0400194 if (makeClone) {
195 clone = fp->clone();
196 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400197 std::unique_ptr<GrDrawOp> op(TestOp::Make(context, std::move(fp)));
Brian Salomonb17e6392017-07-28 13:41:51 -0400198 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
199 if (clone) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400200 op = TestOp::Make(context, std::move(clone));
Brian Salomonb17e6392017-07-28 13:41:51 -0400201 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
202 }
203 }
Brian Salomonb17e6392017-07-28 13:41:51 -0400204
Robert Phillips3d4cac52019-06-11 08:08:08 -0400205 // If the fp is cloned the number of refs should increase by one (for the clone)
206 int expectedProxyRefs = makeClone ? 3 : 2;
207
208 check_refs(reporter, proxy.get(), expectedProxyRefs, -1);
Brian Salomonb17e6392017-07-28 13:41:51 -0400209
Brian Salomonb17e6392017-07-28 13:41:51 -0400210 context->flush();
211
Robert Phillips3d4cac52019-06-11 08:08:08 -0400212 check_refs(reporter, proxy.get(), 1, 1); // just one from the 'proxy' sk_sp
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500213 }
214 }
215 }
216}
Brian Salomon587e08f2017-01-27 10:59:27 -0500217
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500218#include "tools/flags/CommandLineFlags.h"
Mike Klein84836b72019-03-21 11:31:36 -0500219static DEFINE_bool(randomProcessorTest, false,
220 "Use non-deterministic seed for random processor tests?");
Mike Klein5b3f3432019-03-21 11:42:21 -0500221static DEFINE_int(processorSeed, 0,
222 "Use specific seed for processor tests. Overridden by --randomProcessorTest.");
Brian Salomon0e05a822017-07-25 09:43:22 -0400223
224#if GR_TEST_UTILS
225
Michael Ludwig7034f152018-10-08 16:43:58 -0400226static GrColor input_texel_color(int i, int j, SkScalar delta) {
227 // Delta must be less than 0.5 to prevent over/underflow issues with the input color
228 SkASSERT(delta <= 0.5);
Brian Salomon587e08f2017-01-27 10:59:27 -0500229
Brian Osman50ea3c02019-02-04 10:01:53 -0500230 SkColor color = SkColorSetARGB((uint8_t)(i & 0xFF),
231 (uint8_t)(j & 0xFF),
232 (uint8_t)((i + j) & 0xFF),
233 (uint8_t)((2 * j - i) & 0xFF));
Brian Osmancb3d0872018-10-16 15:19:28 -0400234 SkColor4f color4f = SkColor4f::FromColor(color);
Michael Ludwig7034f152018-10-08 16:43:58 -0400235 for (int i = 0; i < 4; i++) {
Brian Osmancb3d0872018-10-16 15:19:28 -0400236 if (color4f[i] > 0.5) {
237 color4f[i] -= delta;
Michael Ludwig7034f152018-10-08 16:43:58 -0400238 } else {
Brian Osmancb3d0872018-10-16 15:19:28 -0400239 color4f[i] += delta;
Michael Ludwig7034f152018-10-08 16:43:58 -0400240 }
241 }
Brian Osmancb3d0872018-10-16 15:19:28 -0400242 return color4f.premul().toBytes_RGBA();
Brian Salomon0e05a822017-07-25 09:43:22 -0400243}
Brian Salomon587e08f2017-01-27 10:59:27 -0500244
Robert Phillips7c525e62018-06-12 10:11:12 -0400245void test_draw_op(GrContext* context,
246 GrRenderTargetContext* rtc,
247 std::unique_ptr<GrFragmentProcessor> fp,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500248 sk_sp<GrTextureProxy> inputDataProxy) {
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500249 GrPaint paint;
Brian Osman2240be92017-10-18 13:15:13 -0400250 paint.addColorTextureProcessor(std::move(inputDataProxy), SkMatrix::I());
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500251 paint.addColorFragmentProcessor(std::move(fp));
252 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonac70f842017-05-08 10:43:33 -0400253
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400254 auto op = GrFillRectOp::MakeNonAARect(context, std::move(paint), SkMatrix::I(),
255 SkRect::MakeWH(rtc->width(), rtc->height()));
Brian Salomonac70f842017-05-08 10:43:33 -0400256 rtc->addDrawOp(GrNoClip(), std::move(op));
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500257}
258
Michael Ludwig7034f152018-10-08 16:43:58 -0400259// This assumes that the output buffer will be the same size as inputDataProxy
260void render_fp(GrContext* context, GrRenderTargetContext* rtc, GrFragmentProcessor* fp,
261 sk_sp<GrTextureProxy> inputDataProxy, GrColor* buffer) {
262 int width = inputDataProxy->width();
263 int height = inputDataProxy->height();
264
265 // test_draw_op needs to take ownership of an FP, so give it a clone that it can own
266 test_draw_op(context, rtc, fp->clone(), inputDataProxy);
267 memset(buffer, 0x0, sizeof(GrColor) * width * height);
Brian Salomon1d435302019-07-01 13:05:28 -0400268 rtc->readPixels(SkImageInfo::Make(width, height, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
269 buffer, 0, {0, 0});
Michael Ludwig7034f152018-10-08 16:43:58 -0400270}
271
Brian Salomon0e05a822017-07-25 09:43:22 -0400272/** Initializes the two test texture proxies that are available to the FP test factories. */
Robert Phillips7eeb74f2019-03-29 07:26:46 -0400273bool init_test_textures(GrResourceProvider* resourceProvider,
274 GrProxyProvider* proxyProvider,
275 SkRandom* random,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500276 sk_sp<GrTextureProxy> proxies[2]) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400277 static const int kTestTextureSize = 256;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400278
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500279 {
280 // Put premul data into the RGBA texture that the test FPs can optionally use.
281 std::unique_ptr<GrColor[]> rgbaData(new GrColor[kTestTextureSize * kTestTextureSize]);
282 for (int y = 0; y < kTestTextureSize; ++y) {
283 for (int x = 0; x < kTestTextureSize; ++x) {
Michael Ludwig7034f152018-10-08 16:43:58 -0400284 rgbaData[kTestTextureSize * y + x] = input_texel_color(
285 random->nextULessThan(256), random->nextULessThan(256), 0.0f);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500286 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400287 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400288
Brian Osman2700abc2018-09-12 10:19:41 -0400289 SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize,
290 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
291 SkPixmap pixmap(ii, rgbaData.get(), ii.minRowBytes());
292 sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap);
293 proxies[0] = proxyProvider->createTextureProxy(img, kNone_GrSurfaceFlags, 1,
Robert Phillips10d17212019-04-24 14:09:10 -0400294 SkBudgeted::kYes, SkBackingFit::kExact);
Robert Phillips12c46292019-04-23 07:36:17 -0400295 proxies[0]->instantiate(resourceProvider);
Brian Salomon0e05a822017-07-25 09:43:22 -0400296 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500297
298 {
299 // Put random values into the alpha texture that the test FPs can optionally use.
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500300 std::unique_ptr<uint8_t[]> alphaData(new uint8_t[kTestTextureSize * kTestTextureSize]);
301 for (int y = 0; y < kTestTextureSize; ++y) {
302 for (int x = 0; x < kTestTextureSize; ++x) {
303 alphaData[kTestTextureSize * y + x] = random->nextULessThan(256);
304 }
305 }
306
Brian Osman2700abc2018-09-12 10:19:41 -0400307 SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize,
308 kAlpha_8_SkColorType, kPremul_SkAlphaType);
309 SkPixmap pixmap(ii, alphaData.get(), ii.minRowBytes());
310 sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap);
311 proxies[1] = proxyProvider->createTextureProxy(img, kNone_GrSurfaceFlags, 1,
Robert Phillips10d17212019-04-24 14:09:10 -0400312 SkBudgeted::kYes, SkBackingFit::kExact);
Robert Phillips12c46292019-04-23 07:36:17 -0400313 proxies[1]->instantiate(resourceProvider);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500314 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400315
316 return proxies[0] && proxies[1];
317}
318
319// Creates a texture of premul colors used as the output of the fragment processor that precedes
320// the fragment processor under test. Color values are those provided by input_texel_color().
Michael Ludwig7034f152018-10-08 16:43:58 -0400321sk_sp<GrTextureProxy> make_input_texture(GrProxyProvider* proxyProvider, int width, int height,
322 SkScalar delta) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400323 std::unique_ptr<GrColor[]> data(new GrColor[width * height]);
324 for (int y = 0; y < width; ++y) {
325 for (int x = 0; x < height; ++x) {
Michael Ludwig7034f152018-10-08 16:43:58 -0400326 data.get()[width * y + x] = input_texel_color(x, y, delta);
Brian Salomon0e05a822017-07-25 09:43:22 -0400327 }
328 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500329
Brian Osman2700abc2018-09-12 10:19:41 -0400330 SkImageInfo ii = SkImageInfo::Make(width, height, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
331 SkPixmap pixmap(ii, data.get(), ii.minRowBytes());
332 sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap);
333 return proxyProvider->createTextureProxy(img, kNone_GrSurfaceFlags, 1,
334 SkBudgeted::kYes, SkBackingFit::kExact);
Brian Salomon0e05a822017-07-25 09:43:22 -0400335}
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500336
Michael Ludwige8e10752018-10-01 12:42:53 -0400337bool log_surface_context(sk_sp<GrSurfaceContext> src, SkString* dst) {
338 SkImageInfo ii = SkImageInfo::Make(src->width(), src->height(), kRGBA_8888_SkColorType,
339 kPremul_SkAlphaType);
340 SkBitmap bm;
341 SkAssertResult(bm.tryAllocPixels(ii));
Brian Salomon1d435302019-07-01 13:05:28 -0400342 SkAssertResult(src->readPixels(ii, bm.getPixels(), bm.rowBytes(), {0, 0}));
Michael Ludwige8e10752018-10-01 12:42:53 -0400343
344 return bitmap_to_base64_data_uri(bm, dst);
345}
346
347bool log_surface_proxy(GrContext* context, sk_sp<GrSurfaceProxy> src, SkString* dst) {
Brian Salomond6287472019-06-24 15:50:07 -0400348 // All the inputs are made from kRGBA_8888_SkColorType/kPremul_SkAlphaType bitmaps.
349 sk_sp<GrSurfaceContext> sContext(context->priv().makeWrappedSurfaceContext(
350 src, GrColorType::kRGBA_8888, kPremul_SkAlphaType));
Michael Ludwige8e10752018-10-01 12:42:53 -0400351 return log_surface_context(sContext, dst);
352}
353
Michael Ludwig7034f152018-10-08 16:43:58 -0400354bool fuzzy_color_equals(const SkPMColor4f& c1, const SkPMColor4f& c2) {
355 // With the loss of precision of rendering into 32-bit color, then estimating the FP's output
356 // from that, it is not uncommon for a valid output to differ from estimate by up to 0.01
357 // (really 1/128 ~ .0078, but frequently floating point issues make that tolerance a little
358 // too unforgiving).
359 static constexpr SkScalar kTolerance = 0.01f;
360 for (int i = 0; i < 4; i++) {
361 if (!SkScalarNearlyEqual(c1[i], c2[i], kTolerance)) {
362 return false;
363 }
364 }
365 return true;
366}
367
368int modulation_index(int channelIndex, bool alphaModulation) {
369 return alphaModulation ? 3 : channelIndex;
370}
371
372// Given three input colors (color preceding the FP being tested), and the output of the FP, this
373// ensures that the out1 = fp * in1.a, out2 = fp * in2.a, and out3 = fp * in3.a, where fp is the
374// pre-modulated color that should not be changing across frames (FP's state doesn't change).
375//
376// When alphaModulation is false, this tests the very similar conditions that out1 = fp * in1,
377// etc. using per-channel modulation instead of modulation by just the input alpha channel.
378// - This estimates the pre-modulated fp color from one of the input/output pairs and confirms the
379// conditions hold for the other two pairs.
380bool legal_modulation(const GrColor& in1, const GrColor& in2, const GrColor& in3,
381 const GrColor& out1, const GrColor& out2, const GrColor& out3,
382 bool alphaModulation) {
383 // Convert to floating point, which is the number space the FP operates in (more or less)
Brian Osmancb3d0872018-10-16 15:19:28 -0400384 SkPMColor4f in1f = SkPMColor4f::FromBytes_RGBA(in1);
385 SkPMColor4f in2f = SkPMColor4f::FromBytes_RGBA(in2);
386 SkPMColor4f in3f = SkPMColor4f::FromBytes_RGBA(in3);
387 SkPMColor4f out1f = SkPMColor4f::FromBytes_RGBA(out1);
388 SkPMColor4f out2f = SkPMColor4f::FromBytes_RGBA(out2);
389 SkPMColor4f out3f = SkPMColor4f::FromBytes_RGBA(out3);
Michael Ludwig7034f152018-10-08 16:43:58 -0400390
391 // Reconstruct the output of the FP before the shader modulated its color with the input value.
392 // When the original input is very small, it may cause the final output color to round
393 // to 0, in which case we estimate the pre-modulated color using one of the stepped frames that
394 // will then have a guaranteed larger channel value (since the offset will be added to it).
395 SkPMColor4f fpPreModulation;
396 for (int i = 0; i < 4; i++) {
397 int modulationIndex = modulation_index(i, alphaModulation);
398 if (in1f[modulationIndex] < 0.2f) {
399 // Use the stepped frame
400 fpPreModulation[i] = out2f[i] / in2f[modulationIndex];
401 } else {
402 fpPreModulation[i] = out1f[i] / in1f[modulationIndex];
403 }
404 }
405
406 // With reconstructed pre-modulated FP output, derive the expected value of fp * input for each
407 // of the transformed input colors.
Brian Osmancb3d0872018-10-16 15:19:28 -0400408 SkPMColor4f expected1 = alphaModulation ? (fpPreModulation * in1f.fA)
409 : (fpPreModulation * in1f);
410 SkPMColor4f expected2 = alphaModulation ? (fpPreModulation * in2f.fA)
411 : (fpPreModulation * in2f);
412 SkPMColor4f expected3 = alphaModulation ? (fpPreModulation * in3f.fA)
413 : (fpPreModulation * in3f);
Michael Ludwig7034f152018-10-08 16:43:58 -0400414
Brian Osmancb3d0872018-10-16 15:19:28 -0400415 return fuzzy_color_equals(out1f, expected1) &&
416 fuzzy_color_equals(out2f, expected2) &&
417 fuzzy_color_equals(out3f, expected3);
Michael Ludwig7034f152018-10-08 16:43:58 -0400418}
419
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500420DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500421 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500422 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
423 auto resourceProvider = context->priv().resourceProvider();
Brian Salomon1c053642017-07-24 10:16:19 -0400424 using FPFactory = GrFragmentProcessorTestFactory;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400425
Michael Ludwig3ad86d02018-09-28 16:00:38 -0400426 uint32_t seed = FLAGS_processorSeed;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400427 if (FLAGS_randomProcessorTest) {
428 std::random_device rd;
429 seed = rd();
430 }
431 // 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 -0400432 // use --processorSeed <seed> (without --randomProcessorTest) to reproduce.
Brian Osmanc35a2d42017-03-17 10:58:53 -0400433 SkRandom random(seed);
434
Greg Daniel4065d452018-11-16 15:43:41 -0500435 const GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500436 context->priv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Greg Daniel4065d452018-11-16 15:43:41 -0500437
Brian Salomon0e05a822017-07-25 09:43:22 -0400438 // Make the destination context for the test.
439 static constexpr int kRenderSize = 256;
Robert Phillips9da87e02019-02-04 13:26:26 -0500440 sk_sp<GrRenderTargetContext> rtc = context->priv().makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -0500441 format, SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig,
Brian Salomond6287472019-06-24 15:50:07 -0400442 GrColorType::kRGBA_8888, nullptr);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500443
Robert Phillipse78b7252017-04-06 07:59:41 -0400444 sk_sp<GrTextureProxy> proxies[2];
Robert Phillips7eeb74f2019-03-29 07:26:46 -0400445 if (!init_test_textures(resourceProvider, proxyProvider, &random, proxies)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400446 ERRORF(reporter, "Could not create test textures");
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400447 return;
448 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400449 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
Brian Salomon587e08f2017-01-27 10:59:27 -0500450
Michael Ludwig7034f152018-10-08 16:43:58 -0400451 // Coverage optimization uses three frames with a linearly transformed input texture. The first
452 // frame has no offset, second frames add .2 and .4, which should then be present as a fixed
453 // difference between the frame outputs if the FP is properly following the modulation
454 // requirements of the coverage optimization.
455 static constexpr SkScalar kInputDelta = 0.2f;
456 auto inputTexture1 = make_input_texture(proxyProvider, kRenderSize, kRenderSize, 0.0f);
457 auto inputTexture2 = make_input_texture(proxyProvider, kRenderSize, kRenderSize, kInputDelta);
458 auto inputTexture3 = make_input_texture(proxyProvider, kRenderSize, kRenderSize, 2*kInputDelta);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500459
Michael Ludwige8e10752018-10-01 12:42:53 -0400460 // Encoded images are very verbose and this tests many potential images, so only export the
461 // first failure (subsequent failures have a reasonable chance of being related).
462 bool loggedFirstFailure = false;
Michael Ludwig314d3772018-10-03 16:04:38 -0400463 bool loggedFirstWarning = false;
Michael Ludwig7034f152018-10-08 16:43:58 -0400464
465 // Storage for the three frames required for coverage compatibility optimization. Each frame
466 // uses the correspondingly numbered inputTextureX.
467 std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
468 std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
469 std::unique_ptr<GrColor[]> readData3(new GrColor[kRenderSize * kRenderSize]);
470
Brian Salomon0e05a822017-07-25 09:43:22 -0400471 // Because processor factories configure themselves in random ways, this is not exhaustive.
Brian Salomon587e08f2017-01-27 10:59:27 -0500472 for (int i = 0; i < FPFactory::Count(); ++i) {
473 int timesToInvokeFactory = 5;
474 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
475 // on child optimizations being present.
Brian Salomonaff329b2017-08-11 09:40:37 -0400476 std::unique_ptr<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
Brian Salomon587e08f2017-01-27 10:59:27 -0500477 for (int j = 0; j < fp->numChildProcessors(); ++j) {
478 // This value made a reasonable trade off between time and coverage when this test was
479 // written.
480 timesToInvokeFactory *= FPFactory::Count() / 2;
481 }
Brian Osman50ea3c02019-02-04 10:01:53 -0500482#if defined(__MSVC_RUNTIME_CHECKS)
483 // This test is infuriatingly slow with MSVC runtime checks enabled
484 timesToInvokeFactory = 1;
485#endif
Brian Salomon587e08f2017-01-27 10:59:27 -0500486 for (int j = 0; j < timesToInvokeFactory; ++j) {
487 fp = FPFactory::MakeIdx(i, &testData);
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400488
Brian Salomon587e08f2017-01-27 10:59:27 -0500489 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
Brian Salomonf3b995b2017-02-15 10:22:23 -0500490 !fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500491 continue;
492 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400493
Michael Ludwig7034f152018-10-08 16:43:58 -0400494 if (fp->compatibleWithCoverageAsAlpha()) {
495 // 2nd and 3rd frames are only used when checking coverage optimization
496 render_fp(context, rtc.get(), fp.get(), inputTexture2, readData2.get());
497 render_fp(context, rtc.get(), fp.get(), inputTexture3, readData3.get());
498 }
499 // Draw base frame last so that rtc holds the original FP behavior if we need to
500 // dump the image to the log.
501 render_fp(context, rtc.get(), fp.get(), inputTexture1, readData1.get());
Brian Salomonaff329b2017-08-11 09:40:37 -0400502
Brian Salomon587e08f2017-01-27 10:59:27 -0500503 if (0) { // Useful to see what FPs are being tested.
504 SkString children;
Michael Ludwig7034f152018-10-08 16:43:58 -0400505 for (int c = 0; c < fp->numChildProcessors(); ++c) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500506 if (!c) {
507 children.append("(");
508 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400509 children.append(fp->childProcessor(c).name());
510 children.append(c == fp->numChildProcessors() - 1 ? ")" : ", ");
Brian Salomon587e08f2017-01-27 10:59:27 -0500511 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400512 SkDebugf("%s %s\n", fp->name(), children.c_str());
Brian Salomon587e08f2017-01-27 10:59:27 -0500513 }
Michael Ludwig314d3772018-10-03 16:04:38 -0400514
515 // This test has a history of being flaky on a number of devices. If an FP is logically
516 // violating the optimizations, it's reasonable to expect it to violate requirements on
517 // a large number of pixels in the image. Sporadic pixel violations are more indicative
518 // of device errors and represents a separate problem.
Michael Ludwig72ab3462018-12-10 12:43:36 -0500519#if defined(SK_BUILD_FOR_SKQP)
Michael Ludwig314d3772018-10-03 16:04:38 -0400520 static constexpr int kMaxAcceptableFailedPixels = 0; // Strict when running as SKQP
521#else
522 static constexpr int kMaxAcceptableFailedPixels = 2 * kRenderSize; // ~0.7% of the image
523#endif
524
525 int failedPixelCount = 0;
526 // Collect first optimization failure message, to be output later as a warning or an
527 // error depending on whether the rendering "passed" or failed.
528 SkString coverageMessage;
529 SkString opaqueMessage;
530 SkString constMessage;
531 for (int y = 0; y < kRenderSize; ++y) {
532 for (int x = 0; x < kRenderSize; ++x) {
533 bool passing = true;
Michael Ludwig7034f152018-10-08 16:43:58 -0400534 GrColor input = input_texel_color(x, y, 0.0f);
535 GrColor output = readData1.get()[y * kRenderSize + x];
536
537 if (fp->compatibleWithCoverageAsAlpha()) {
538 GrColor i2 = input_texel_color(x, y, kInputDelta);
539 GrColor i3 = input_texel_color(x, y, 2 * kInputDelta);
540
541 GrColor o2 = readData2.get()[y * kRenderSize + x];
542 GrColor o3 = readData3.get()[y * kRenderSize + x];
543
544 // A compatible processor is allowed to modulate either the input color or
Brian Salomon587e08f2017-01-27 10:59:27 -0500545 // just the input alpha.
Michael Ludwig7034f152018-10-08 16:43:58 -0400546 bool legalAlphaModulation = legal_modulation(input, i2, i3, output, o2, o3,
547 /* alpha */ true);
548 bool legalColorModulation = legal_modulation(input, i2, i3, output, o2, o3,
549 /* alpha */ false);
550
Brian Salomon587e08f2017-01-27 10:59:27 -0500551 if (!legalColorModulation && !legalAlphaModulation) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500552 passing = false;
Michael Ludwig314d3772018-10-03 16:04:38 -0400553
554 if (coverageMessage.isEmpty()) {
Michael Ludwig7034f152018-10-08 16:43:58 -0400555 coverageMessage.printf("\"Modulating\" processor %s did not match "
556 "alpha-modulation nor color-modulation rules. "
557 "Input: 0x%08x, Output: 0x%08x, pixel (%d, %d).",
558 fp->name(), input, output, x, y);
Michael Ludwig314d3772018-10-03 16:04:38 -0400559 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500560 }
561 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400562
Brian Osmancb3d0872018-10-16 15:19:28 -0400563 SkPMColor4f input4f = SkPMColor4f::FromBytes_RGBA(input);
564 SkPMColor4f output4f = SkPMColor4f::FromBytes_RGBA(output);
Brian Osman1d5b5982018-10-01 13:41:39 -0400565 SkPMColor4f expected4f;
Michael Ludwig7034f152018-10-08 16:43:58 -0400566 if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) {
Brian Osmancb3d0872018-10-16 15:19:28 -0400567 float rDiff = fabsf(output4f.fR - expected4f.fR);
568 float gDiff = fabsf(output4f.fG - expected4f.fG);
569 float bDiff = fabsf(output4f.fB - expected4f.fB);
570 float aDiff = fabsf(output4f.fA - expected4f.fA);
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500571 static constexpr float kTol = 4 / 255.f;
Brian Salomon587e08f2017-01-27 10:59:27 -0500572 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
Michael Ludwig314d3772018-10-03 16:04:38 -0400573 if (constMessage.isEmpty()) {
574 passing = false;
575
576 constMessage.printf("Processor %s claimed output for const input "
577 "doesn't match actual output. Error: %f, Tolerance: %f, "
578 "input: (%f, %f, %f, %f), actual: (%f, %f, %f, %f), "
Michael Ludwig7034f152018-10-08 16:43:58 -0400579 "expected(%f, %f, %f, %f)", fp->name(),
Michael Ludwig314d3772018-10-03 16:04:38 -0400580 SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))), kTol,
581 input4f.fR, input4f.fG, input4f.fB, input4f.fA,
Brian Osmancb3d0872018-10-16 15:19:28 -0400582 output4f.fR, output4f.fG, output4f.fB, output4f.fA,
583 expected4f.fR, expected4f.fG, expected4f.fB, expected4f.fA);
Michael Ludwig314d3772018-10-03 16:04:38 -0400584 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500585 }
586 }
Brian Osman00b29392018-11-05 15:42:43 -0500587 if (input4f.isOpaque() && fp->preservesOpaqueInput() && !output4f.isOpaque()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500588 passing = false;
Michael Ludwig314d3772018-10-03 16:04:38 -0400589
590 if (opaqueMessage.isEmpty()) {
591 opaqueMessage.printf("Processor %s claimed opaqueness is preserved but "
592 "it is not. Input: 0x%08x, Output: 0x%08x.",
Michael Ludwig7034f152018-10-08 16:43:58 -0400593 fp->name(), input, output);
Michael Ludwige8e10752018-10-01 12:42:53 -0400594 }
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400595 }
Michael Ludwig314d3772018-10-03 16:04:38 -0400596
597 if (!passing) {
598 // Regardless of how many optimizations the pixel violates, count it as a
599 // single bad pixel.
600 failedPixelCount++;
601 }
602 }
603 }
604
605 // Finished analyzing the entire image, see if the number of pixel failures meets the
606 // threshold for an FP violating the optimization requirements.
607 if (failedPixelCount > kMaxAcceptableFailedPixels) {
Michael Ludwig4504a6522018-10-03 16:47:55 -0400608 ERRORF(reporter, "Processor violated %d of %d pixels, seed: 0x%08x, processor: %s"
Michael Ludwig314d3772018-10-03 16:04:38 -0400609 ", first failing pixel details are below:",
610 failedPixelCount, kRenderSize * kRenderSize, seed,
Michael Ludwig7034f152018-10-08 16:43:58 -0400611 fp->dumpInfo().c_str());
Michael Ludwig314d3772018-10-03 16:04:38 -0400612
613 // Print first failing pixel's details.
614 if (!coverageMessage.isEmpty()) {
615 ERRORF(reporter, coverageMessage.c_str());
616 }
617 if (!constMessage.isEmpty()) {
618 ERRORF(reporter, constMessage.c_str());
619 }
620 if (!opaqueMessage.isEmpty()) {
621 ERRORF(reporter, opaqueMessage.c_str());
622 }
623
624 if (!loggedFirstFailure) {
625 // Print with ERRORF to make sure the encoded image is output
626 SkString input;
Michael Ludwig7034f152018-10-08 16:43:58 -0400627 log_surface_proxy(context, inputTexture1, &input);
Michael Ludwig314d3772018-10-03 16:04:38 -0400628 SkString output;
629 log_surface_context(rtc, &output);
630 ERRORF(reporter, "Input image: %s\n\n"
631 "===========================================================\n\n"
632 "Output image: %s\n", input.c_str(), output.c_str());
633 loggedFirstFailure = true;
634 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400635 } else if(failedPixelCount > 0) {
Michael Ludwig314d3772018-10-03 16:04:38 -0400636 // Don't trigger an error, but don't just hide the failures either.
637 INFOF(reporter, "Processor violated %d of %d pixels (below error threshold), seed: "
638 "0x%08x, processor: %s", failedPixelCount, kRenderSize * kRenderSize,
Michael Ludwig7034f152018-10-08 16:43:58 -0400639 seed, fp->dumpInfo().c_str());
Michael Ludwig314d3772018-10-03 16:04:38 -0400640 if (!coverageMessage.isEmpty()) {
641 INFOF(reporter, coverageMessage.c_str());
642 }
643 if (!constMessage.isEmpty()) {
644 INFOF(reporter, constMessage.c_str());
645 }
646 if (!opaqueMessage.isEmpty()) {
647 INFOF(reporter, opaqueMessage.c_str());
648 }
649 if (!loggedFirstWarning) {
650 SkString input;
Michael Ludwig7034f152018-10-08 16:43:58 -0400651 log_surface_proxy(context, inputTexture1, &input);
Michael Ludwig314d3772018-10-03 16:04:38 -0400652 SkString output;
653 log_surface_context(rtc, &output);
654 INFOF(reporter, "Input image: %s\n\n"
655 "===========================================================\n\n"
656 "Output image: %s\n", input.c_str(), output.c_str());
657 loggedFirstWarning = true;
Brian Salomon587e08f2017-01-27 10:59:27 -0500658 }
659 }
660 }
661 }
662}
Robert Phillips18166ee2017-06-01 12:55:44 -0400663
Brian Salomon0e05a822017-07-25 09:43:22 -0400664// Tests that fragment processors returned by GrFragmentProcessor::clone() are equivalent to their
665// progenitors.
666DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) {
667 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500668 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
669 auto resourceProvider = context->priv().resourceProvider();
Brian Salomon0e05a822017-07-25 09:43:22 -0400670
671 SkRandom random;
672
Greg Daniel4065d452018-11-16 15:43:41 -0500673 const GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500674 context->priv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Greg Daniel4065d452018-11-16 15:43:41 -0500675
Brian Salomon0e05a822017-07-25 09:43:22 -0400676 // Make the destination context for the test.
677 static constexpr int kRenderSize = 1024;
Robert Phillips9da87e02019-02-04 13:26:26 -0500678 sk_sp<GrRenderTargetContext> rtc = context->priv().makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -0500679 format, SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig,
Brian Salomond6287472019-06-24 15:50:07 -0400680 GrColorType::kRGBA_8888, nullptr);
Brian Salomon0e05a822017-07-25 09:43:22 -0400681
682 sk_sp<GrTextureProxy> proxies[2];
Robert Phillips7eeb74f2019-03-29 07:26:46 -0400683 if (!init_test_textures(resourceProvider, proxyProvider, &random, proxies)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400684 ERRORF(reporter, "Could not create test textures");
685 return;
686 }
687 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
688
Michael Ludwig7034f152018-10-08 16:43:58 -0400689 auto inputTexture = make_input_texture(proxyProvider, kRenderSize, kRenderSize, 0.0f);
Brian Salomon0e05a822017-07-25 09:43:22 -0400690 std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
691 std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
692 auto readInfo = SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
693 kPremul_SkAlphaType);
694
695 // Because processor factories configure themselves in random ways, this is not exhaustive.
696 for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) {
697 static constexpr int kTimesToInvokeFactory = 10;
698 for (int j = 0; j < kTimesToInvokeFactory; ++j) {
699 auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &testData);
700 auto clone = fp->clone();
Brian Salomon0e05a822017-07-25 09:43:22 -0400701 if (!clone) {
Brian Salomon96271cd2017-07-31 16:27:23 -0400702 ERRORF(reporter, "Clone of processor %s failed.", fp->name());
Brian Salomon0e05a822017-07-25 09:43:22 -0400703 continue;
704 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400705 const char* name = fp->name();
Brian Salomonce06e262017-08-01 16:23:40 -0400706 REPORTER_ASSERT(reporter, !strcmp(fp->name(), clone->name()));
707 REPORTER_ASSERT(reporter, fp->compatibleWithCoverageAsAlpha() ==
708 clone->compatibleWithCoverageAsAlpha());
709 REPORTER_ASSERT(reporter, fp->isEqual(*clone));
710 REPORTER_ASSERT(reporter, fp->preservesOpaqueInput() == clone->preservesOpaqueInput());
711 REPORTER_ASSERT(reporter, fp->hasConstantOutputForConstantInput() ==
712 clone->hasConstantOutputForConstantInput());
713 REPORTER_ASSERT(reporter, fp->numChildProcessors() == clone->numChildProcessors());
714 REPORTER_ASSERT(reporter, fp->usesLocalCoords() == clone->usesLocalCoords());
Brian Salomon0e05a822017-07-25 09:43:22 -0400715 // Draw with original and read back the results.
Michael Ludwig7034f152018-10-08 16:43:58 -0400716 render_fp(context, rtc.get(), fp.get(), inputTexture, readData1.get());
Brian Salomon0e05a822017-07-25 09:43:22 -0400717
718 // Draw with clone and read back the results.
Michael Ludwig7034f152018-10-08 16:43:58 -0400719 render_fp(context, rtc.get(), clone.get(), inputTexture, readData2.get());
Brian Salomon0e05a822017-07-25 09:43:22 -0400720
721 // Check that the results are the same.
722 bool passing = true;
723 for (int y = 0; y < kRenderSize && passing; ++y) {
724 for (int x = 0; x < kRenderSize && passing; ++x) {
725 int idx = y * kRenderSize + x;
726 if (readData1[idx] != readData2[idx]) {
727 ERRORF(reporter,
728 "Processor %s made clone produced different output. "
729 "Input color: 0x%08x, Original Output Color: 0x%08x, "
730 "Clone Output Color: 0x%08x..",
Michael Ludwig7034f152018-10-08 16:43:58 -0400731 name, input_texel_color(x, y, 0.0f), readData1[idx], readData2[idx]);
Brian Salomon0e05a822017-07-25 09:43:22 -0400732 passing = false;
733 }
734 }
735 }
736 }
737 }
738}
739
Hal Canary6f6961e2017-01-31 13:50:44 -0500740#endif // GR_TEST_UTILS