blob: 0caf47ffcdb1984553b68ed234de9d51c91be70e [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 =
Greg Daniel627d0532019-07-08 16:48:14 -0400172 context->priv().caps()->getBackendFormatFromColorType(GrColorType::kRGBA_8888);
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 Salomon27ae52c2019-07-03 11:27:44 -0400178 SkBackingFit::kApprox, 1, 1, GrColorType::kRGBA_8888, nullptr));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500179 {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400180 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400181 format, desc, GrRenderable::kNo, kTopLeft_GrSurfaceOrigin,
Brian Salomone8a766b2019-07-19 14:24:36 -0400182 SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo);
Robert Phillips3d4cac52019-06-11 08:08:08 -0400183
Brian Salomonb17e6392017-07-28 13:41:51 -0400184 {
185 SkTArray<sk_sp<GrTextureProxy>> proxies;
Brian Salomondbf70722019-02-07 11:31:24 -0500186 SkTArray<sk_sp<GrGpuBuffer>> buffers;
Robert Phillips3d4cac52019-06-11 08:08:08 -0400187 proxies.push_back(proxy);
Brian Salomon559f5562017-11-15 14:28:33 -0500188 auto fp = TestFP::Make(std::move(proxies), std::move(buffers));
Brian Salomonb17e6392017-07-28 13:41:51 -0400189 for (int i = 0; i < parentCnt; ++i) {
190 fp = TestFP::Make(std::move(fp));
191 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400192 std::unique_ptr<GrFragmentProcessor> clone;
Brian Salomonb17e6392017-07-28 13:41:51 -0400193 if (makeClone) {
194 clone = fp->clone();
195 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400196 std::unique_ptr<GrDrawOp> op(TestOp::Make(context, std::move(fp)));
Brian Salomonb17e6392017-07-28 13:41:51 -0400197 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
198 if (clone) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400199 op = TestOp::Make(context, std::move(clone));
Brian Salomonb17e6392017-07-28 13:41:51 -0400200 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
201 }
202 }
Brian Salomonb17e6392017-07-28 13:41:51 -0400203
Robert Phillips3d4cac52019-06-11 08:08:08 -0400204 // If the fp is cloned the number of refs should increase by one (for the clone)
205 int expectedProxyRefs = makeClone ? 3 : 2;
206
207 check_refs(reporter, proxy.get(), expectedProxyRefs, -1);
Brian Salomonb17e6392017-07-28 13:41:51 -0400208
Brian Salomonb17e6392017-07-28 13:41:51 -0400209 context->flush();
210
Robert Phillips3d4cac52019-06-11 08:08:08 -0400211 check_refs(reporter, proxy.get(), 1, 1); // just one from the 'proxy' sk_sp
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500212 }
213 }
214 }
215}
Brian Salomon587e08f2017-01-27 10:59:27 -0500216
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500217#include "tools/flags/CommandLineFlags.h"
Mike Klein84836b72019-03-21 11:31:36 -0500218static DEFINE_bool(randomProcessorTest, false,
219 "Use non-deterministic seed for random processor tests?");
Mike Klein5b3f3432019-03-21 11:42:21 -0500220static DEFINE_int(processorSeed, 0,
221 "Use specific seed for processor tests. Overridden by --randomProcessorTest.");
Brian Salomon0e05a822017-07-25 09:43:22 -0400222
223#if GR_TEST_UTILS
224
Michael Ludwig7034f152018-10-08 16:43:58 -0400225static GrColor input_texel_color(int i, int j, SkScalar delta) {
226 // Delta must be less than 0.5 to prevent over/underflow issues with the input color
227 SkASSERT(delta <= 0.5);
Brian Salomon587e08f2017-01-27 10:59:27 -0500228
Brian Osman50ea3c02019-02-04 10:01:53 -0500229 SkColor color = SkColorSetARGB((uint8_t)(i & 0xFF),
230 (uint8_t)(j & 0xFF),
231 (uint8_t)((i + j) & 0xFF),
232 (uint8_t)((2 * j - i) & 0xFF));
Brian Osmancb3d0872018-10-16 15:19:28 -0400233 SkColor4f color4f = SkColor4f::FromColor(color);
Michael Ludwig7034f152018-10-08 16:43:58 -0400234 for (int i = 0; i < 4; i++) {
Brian Osmancb3d0872018-10-16 15:19:28 -0400235 if (color4f[i] > 0.5) {
236 color4f[i] -= delta;
Michael Ludwig7034f152018-10-08 16:43:58 -0400237 } else {
Brian Osmancb3d0872018-10-16 15:19:28 -0400238 color4f[i] += delta;
Michael Ludwig7034f152018-10-08 16:43:58 -0400239 }
240 }
Brian Osmancb3d0872018-10-16 15:19:28 -0400241 return color4f.premul().toBytes_RGBA();
Brian Salomon0e05a822017-07-25 09:43:22 -0400242}
Brian Salomon587e08f2017-01-27 10:59:27 -0500243
Robert Phillips7c525e62018-06-12 10:11:12 -0400244void test_draw_op(GrContext* context,
245 GrRenderTargetContext* rtc,
246 std::unique_ptr<GrFragmentProcessor> fp,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500247 sk_sp<GrTextureProxy> inputDataProxy) {
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500248 GrPaint paint;
Brian Osman2240be92017-10-18 13:15:13 -0400249 paint.addColorTextureProcessor(std::move(inputDataProxy), SkMatrix::I());
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500250 paint.addColorFragmentProcessor(std::move(fp));
251 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonac70f842017-05-08 10:43:33 -0400252
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400253 auto op = GrFillRectOp::MakeNonAARect(context, std::move(paint), SkMatrix::I(),
254 SkRect::MakeWH(rtc->width(), rtc->height()));
Brian Salomonac70f842017-05-08 10:43:33 -0400255 rtc->addDrawOp(GrNoClip(), std::move(op));
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500256}
257
Michael Ludwig7034f152018-10-08 16:43:58 -0400258// This assumes that the output buffer will be the same size as inputDataProxy
259void render_fp(GrContext* context, GrRenderTargetContext* rtc, GrFragmentProcessor* fp,
260 sk_sp<GrTextureProxy> inputDataProxy, GrColor* buffer) {
261 int width = inputDataProxy->width();
262 int height = inputDataProxy->height();
263
264 // test_draw_op needs to take ownership of an FP, so give it a clone that it can own
265 test_draw_op(context, rtc, fp->clone(), inputDataProxy);
266 memset(buffer, 0x0, sizeof(GrColor) * width * height);
Brian Salomon1d435302019-07-01 13:05:28 -0400267 rtc->readPixels(SkImageInfo::Make(width, height, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
268 buffer, 0, {0, 0});
Michael Ludwig7034f152018-10-08 16:43:58 -0400269}
270
Brian Salomon0e05a822017-07-25 09:43:22 -0400271/** Initializes the two test texture proxies that are available to the FP test factories. */
Robert Phillips7eeb74f2019-03-29 07:26:46 -0400272bool init_test_textures(GrResourceProvider* resourceProvider,
273 GrProxyProvider* proxyProvider,
274 SkRandom* random,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500275 sk_sp<GrTextureProxy> proxies[2]) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400276 static const int kTestTextureSize = 256;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400277
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500278 {
279 // Put premul data into the RGBA texture that the test FPs can optionally use.
280 std::unique_ptr<GrColor[]> rgbaData(new GrColor[kTestTextureSize * kTestTextureSize]);
281 for (int y = 0; y < kTestTextureSize; ++y) {
282 for (int x = 0; x < kTestTextureSize; ++x) {
Michael Ludwig7034f152018-10-08 16:43:58 -0400283 rgbaData[kTestTextureSize * y + x] = input_texel_color(
284 random->nextULessThan(256), random->nextULessThan(256), 0.0f);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500285 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400286 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400287
Brian Osman2700abc2018-09-12 10:19:41 -0400288 SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize,
289 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
290 SkPixmap pixmap(ii, rgbaData.get(), ii.minRowBytes());
291 sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap);
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400292 proxies[0] = proxyProvider->createTextureProxy(img, GrRenderable::kNo, 1, SkBudgeted::kYes,
293 SkBackingFit::kExact);
Robert Phillips12c46292019-04-23 07:36:17 -0400294 proxies[0]->instantiate(resourceProvider);
Brian Salomon0e05a822017-07-25 09:43:22 -0400295 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500296
297 {
298 // Put random values into the alpha texture that the test FPs can optionally use.
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500299 std::unique_ptr<uint8_t[]> alphaData(new uint8_t[kTestTextureSize * kTestTextureSize]);
300 for (int y = 0; y < kTestTextureSize; ++y) {
301 for (int x = 0; x < kTestTextureSize; ++x) {
302 alphaData[kTestTextureSize * y + x] = random->nextULessThan(256);
303 }
304 }
305
Brian Osman2700abc2018-09-12 10:19:41 -0400306 SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize,
307 kAlpha_8_SkColorType, kPremul_SkAlphaType);
308 SkPixmap pixmap(ii, alphaData.get(), ii.minRowBytes());
309 sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap);
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400310 proxies[1] = proxyProvider->createTextureProxy(img, GrRenderable::kNo, 1, SkBudgeted::kYes,
311 SkBackingFit::kExact);
Robert Phillips12c46292019-04-23 07:36:17 -0400312 proxies[1]->instantiate(resourceProvider);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500313 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400314
315 return proxies[0] && proxies[1];
316}
317
318// Creates a texture of premul colors used as the output of the fragment processor that precedes
319// the fragment processor under test. Color values are those provided by input_texel_color().
Michael Ludwig7034f152018-10-08 16:43:58 -0400320sk_sp<GrTextureProxy> make_input_texture(GrProxyProvider* proxyProvider, int width, int height,
321 SkScalar delta) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400322 std::unique_ptr<GrColor[]> data(new GrColor[width * height]);
323 for (int y = 0; y < width; ++y) {
324 for (int x = 0; x < height; ++x) {
Michael Ludwig7034f152018-10-08 16:43:58 -0400325 data.get()[width * y + x] = input_texel_color(x, y, delta);
Brian Salomon0e05a822017-07-25 09:43:22 -0400326 }
327 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500328
Brian Osman2700abc2018-09-12 10:19:41 -0400329 SkImageInfo ii = SkImageInfo::Make(width, height, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
330 SkPixmap pixmap(ii, data.get(), ii.minRowBytes());
331 sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap);
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400332 return proxyProvider->createTextureProxy(img, GrRenderable::kNo, 1, SkBudgeted::kYes,
333 SkBackingFit::kExact);
Brian Salomon0e05a822017-07-25 09:43:22 -0400334}
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500335
Michael Ludwige8e10752018-10-01 12:42:53 -0400336bool log_surface_context(sk_sp<GrSurfaceContext> src, SkString* dst) {
337 SkImageInfo ii = SkImageInfo::Make(src->width(), src->height(), kRGBA_8888_SkColorType,
338 kPremul_SkAlphaType);
339 SkBitmap bm;
340 SkAssertResult(bm.tryAllocPixels(ii));
Brian Salomon1d435302019-07-01 13:05:28 -0400341 SkAssertResult(src->readPixels(ii, bm.getPixels(), bm.rowBytes(), {0, 0}));
Michael Ludwige8e10752018-10-01 12:42:53 -0400342
343 return bitmap_to_base64_data_uri(bm, dst);
344}
345
346bool log_surface_proxy(GrContext* context, sk_sp<GrSurfaceProxy> src, SkString* dst) {
Brian Salomond6287472019-06-24 15:50:07 -0400347 // All the inputs are made from kRGBA_8888_SkColorType/kPremul_SkAlphaType bitmaps.
348 sk_sp<GrSurfaceContext> sContext(context->priv().makeWrappedSurfaceContext(
349 src, GrColorType::kRGBA_8888, kPremul_SkAlphaType));
Michael Ludwige8e10752018-10-01 12:42:53 -0400350 return log_surface_context(sContext, dst);
351}
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
355 // from that, it is not uncommon for a valid output to differ from estimate by up to 0.01
356 // (really 1/128 ~ .0078, but frequently floating point issues make that tolerance a little
357 // too unforgiving).
358 static constexpr SkScalar kTolerance = 0.01f;
359 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
367int modulation_index(int channelIndex, bool alphaModulation) {
368 return alphaModulation ? 3 : channelIndex;
369}
370
371// Given three input colors (color preceding the FP being tested), and the output of the FP, this
372// ensures that the out1 = fp * in1.a, out2 = fp * in2.a, and out3 = fp * in3.a, where fp is the
373// pre-modulated color that should not be changing across frames (FP's state doesn't change).
374//
375// When alphaModulation is false, this tests the very similar conditions that out1 = fp * in1,
376// etc. using per-channel modulation instead of modulation by just the input alpha channel.
377// - This estimates the pre-modulated fp color from one of the input/output pairs and confirms the
378// conditions hold for the other two pairs.
379bool legal_modulation(const GrColor& in1, const GrColor& in2, const GrColor& in3,
380 const GrColor& out1, const GrColor& out2, const GrColor& out3,
381 bool alphaModulation) {
382 // Convert to floating point, which is the number space the FP operates in (more or less)
Brian Osmancb3d0872018-10-16 15:19:28 -0400383 SkPMColor4f in1f = SkPMColor4f::FromBytes_RGBA(in1);
384 SkPMColor4f in2f = SkPMColor4f::FromBytes_RGBA(in2);
385 SkPMColor4f in3f = SkPMColor4f::FromBytes_RGBA(in3);
386 SkPMColor4f out1f = SkPMColor4f::FromBytes_RGBA(out1);
387 SkPMColor4f out2f = SkPMColor4f::FromBytes_RGBA(out2);
388 SkPMColor4f out3f = SkPMColor4f::FromBytes_RGBA(out3);
Michael Ludwig7034f152018-10-08 16:43:58 -0400389
390 // Reconstruct the output of the FP before the shader modulated its color with the input value.
391 // When the original input is very small, it may cause the final output color to round
392 // to 0, in which case we estimate the pre-modulated color using one of the stepped frames that
393 // will then have a guaranteed larger channel value (since the offset will be added to it).
394 SkPMColor4f fpPreModulation;
395 for (int i = 0; i < 4; i++) {
396 int modulationIndex = modulation_index(i, alphaModulation);
397 if (in1f[modulationIndex] < 0.2f) {
398 // Use the stepped frame
399 fpPreModulation[i] = out2f[i] / in2f[modulationIndex];
400 } else {
401 fpPreModulation[i] = out1f[i] / in1f[modulationIndex];
402 }
403 }
404
405 // With reconstructed pre-modulated FP output, derive the expected value of fp * input for each
406 // of the transformed input colors.
Brian Osmancb3d0872018-10-16 15:19:28 -0400407 SkPMColor4f expected1 = alphaModulation ? (fpPreModulation * in1f.fA)
408 : (fpPreModulation * in1f);
409 SkPMColor4f expected2 = alphaModulation ? (fpPreModulation * in2f.fA)
410 : (fpPreModulation * in2f);
411 SkPMColor4f expected3 = alphaModulation ? (fpPreModulation * in3f.fA)
412 : (fpPreModulation * in3f);
Michael Ludwig7034f152018-10-08 16:43:58 -0400413
Brian Osmancb3d0872018-10-16 15:19:28 -0400414 return fuzzy_color_equals(out1f, expected1) &&
415 fuzzy_color_equals(out2f, expected2) &&
416 fuzzy_color_equals(out3f, expected3);
Michael Ludwig7034f152018-10-08 16:43:58 -0400417}
418
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500419DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500420 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500421 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
422 auto resourceProvider = context->priv().resourceProvider();
Brian Salomon1c053642017-07-24 10:16:19 -0400423 using FPFactory = GrFragmentProcessorTestFactory;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400424
Michael Ludwig3ad86d02018-09-28 16:00:38 -0400425 uint32_t seed = FLAGS_processorSeed;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400426 if (FLAGS_randomProcessorTest) {
427 std::random_device rd;
428 seed = rd();
429 }
430 // 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 -0400431 // use --processorSeed <seed> (without --randomProcessorTest) to reproduce.
Brian Osmanc35a2d42017-03-17 10:58:53 -0400432 SkRandom random(seed);
433
Brian Salomon0e05a822017-07-25 09:43:22 -0400434 // Make the destination context for the test.
435 static constexpr int kRenderSize = 256;
Robert Phillips9da87e02019-02-04 13:26:26 -0500436 sk_sp<GrRenderTargetContext> rtc = context->priv().makeDeferredRenderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -0400437 SkBackingFit::kExact, kRenderSize, kRenderSize, GrColorType::kRGBA_8888, nullptr);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500438
Robert Phillipse78b7252017-04-06 07:59:41 -0400439 sk_sp<GrTextureProxy> proxies[2];
Robert Phillips7eeb74f2019-03-29 07:26:46 -0400440 if (!init_test_textures(resourceProvider, proxyProvider, &random, proxies)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400441 ERRORF(reporter, "Could not create test textures");
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400442 return;
443 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400444 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
Brian Salomon587e08f2017-01-27 10:59:27 -0500445
Michael Ludwig7034f152018-10-08 16:43:58 -0400446 // Coverage optimization uses three frames with a linearly transformed input texture. The first
447 // frame has no offset, second frames add .2 and .4, which should then be present as a fixed
448 // difference between the frame outputs if the FP is properly following the modulation
449 // requirements of the coverage optimization.
450 static constexpr SkScalar kInputDelta = 0.2f;
451 auto inputTexture1 = make_input_texture(proxyProvider, kRenderSize, kRenderSize, 0.0f);
452 auto inputTexture2 = make_input_texture(proxyProvider, kRenderSize, kRenderSize, kInputDelta);
453 auto inputTexture3 = make_input_texture(proxyProvider, kRenderSize, kRenderSize, 2*kInputDelta);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500454
Michael Ludwige8e10752018-10-01 12:42:53 -0400455 // Encoded images are very verbose and this tests many potential images, so only export the
456 // first failure (subsequent failures have a reasonable chance of being related).
457 bool loggedFirstFailure = false;
Michael Ludwig314d3772018-10-03 16:04:38 -0400458 bool loggedFirstWarning = false;
Michael Ludwig7034f152018-10-08 16:43:58 -0400459
460 // Storage for the three frames required for coverage compatibility optimization. Each frame
461 // uses the correspondingly numbered inputTextureX.
462 std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
463 std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
464 std::unique_ptr<GrColor[]> readData3(new GrColor[kRenderSize * kRenderSize]);
465
Brian Salomon0e05a822017-07-25 09:43:22 -0400466 // Because processor factories configure themselves in random ways, this is not exhaustive.
Brian Salomon587e08f2017-01-27 10:59:27 -0500467 for (int i = 0; i < FPFactory::Count(); ++i) {
468 int timesToInvokeFactory = 5;
469 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
470 // on child optimizations being present.
Brian Salomonaff329b2017-08-11 09:40:37 -0400471 std::unique_ptr<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
Brian Salomon587e08f2017-01-27 10:59:27 -0500472 for (int j = 0; j < fp->numChildProcessors(); ++j) {
473 // This value made a reasonable trade off between time and coverage when this test was
474 // written.
475 timesToInvokeFactory *= FPFactory::Count() / 2;
476 }
Brian Osman50ea3c02019-02-04 10:01:53 -0500477#if defined(__MSVC_RUNTIME_CHECKS)
478 // This test is infuriatingly slow with MSVC runtime checks enabled
479 timesToInvokeFactory = 1;
480#endif
Brian Salomon587e08f2017-01-27 10:59:27 -0500481 for (int j = 0; j < timesToInvokeFactory; ++j) {
482 fp = FPFactory::MakeIdx(i, &testData);
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400483
Brian Salomon587e08f2017-01-27 10:59:27 -0500484 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
Brian Salomonf3b995b2017-02-15 10:22:23 -0500485 !fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500486 continue;
487 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400488
Michael Ludwig7034f152018-10-08 16:43:58 -0400489 if (fp->compatibleWithCoverageAsAlpha()) {
490 // 2nd and 3rd frames are only used when checking coverage optimization
491 render_fp(context, rtc.get(), fp.get(), inputTexture2, readData2.get());
492 render_fp(context, rtc.get(), fp.get(), inputTexture3, readData3.get());
493 }
494 // Draw base frame last so that rtc holds the original FP behavior if we need to
495 // dump the image to the log.
496 render_fp(context, rtc.get(), fp.get(), inputTexture1, readData1.get());
Brian Salomonaff329b2017-08-11 09:40:37 -0400497
Brian Salomon587e08f2017-01-27 10:59:27 -0500498 if (0) { // Useful to see what FPs are being tested.
499 SkString children;
Michael Ludwig7034f152018-10-08 16:43:58 -0400500 for (int c = 0; c < fp->numChildProcessors(); ++c) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500501 if (!c) {
502 children.append("(");
503 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400504 children.append(fp->childProcessor(c).name());
505 children.append(c == fp->numChildProcessors() - 1 ? ")" : ", ");
Brian Salomon587e08f2017-01-27 10:59:27 -0500506 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400507 SkDebugf("%s %s\n", fp->name(), children.c_str());
Brian Salomon587e08f2017-01-27 10:59:27 -0500508 }
Michael Ludwig314d3772018-10-03 16:04:38 -0400509
510 // This test has a history of being flaky on a number of devices. If an FP is logically
511 // violating the optimizations, it's reasonable to expect it to violate requirements on
512 // a large number of pixels in the image. Sporadic pixel violations are more indicative
513 // of device errors and represents a separate problem.
Michael Ludwig72ab3462018-12-10 12:43:36 -0500514#if defined(SK_BUILD_FOR_SKQP)
Michael Ludwig314d3772018-10-03 16:04:38 -0400515 static constexpr int kMaxAcceptableFailedPixels = 0; // Strict when running as SKQP
516#else
517 static constexpr int kMaxAcceptableFailedPixels = 2 * kRenderSize; // ~0.7% of the image
518#endif
519
520 int failedPixelCount = 0;
521 // Collect first optimization failure message, to be output later as a warning or an
522 // error depending on whether the rendering "passed" or failed.
523 SkString coverageMessage;
524 SkString opaqueMessage;
525 SkString constMessage;
526 for (int y = 0; y < kRenderSize; ++y) {
527 for (int x = 0; x < kRenderSize; ++x) {
528 bool passing = true;
Michael Ludwig7034f152018-10-08 16:43:58 -0400529 GrColor input = input_texel_color(x, y, 0.0f);
530 GrColor output = readData1.get()[y * kRenderSize + x];
531
532 if (fp->compatibleWithCoverageAsAlpha()) {
533 GrColor i2 = input_texel_color(x, y, kInputDelta);
534 GrColor i3 = input_texel_color(x, y, 2 * kInputDelta);
535
536 GrColor o2 = readData2.get()[y * kRenderSize + x];
537 GrColor o3 = readData3.get()[y * kRenderSize + x];
538
539 // A compatible processor is allowed to modulate either the input color or
Brian Salomon587e08f2017-01-27 10:59:27 -0500540 // just the input alpha.
Michael Ludwig7034f152018-10-08 16:43:58 -0400541 bool legalAlphaModulation = legal_modulation(input, i2, i3, output, o2, o3,
542 /* alpha */ true);
543 bool legalColorModulation = legal_modulation(input, i2, i3, output, o2, o3,
544 /* alpha */ false);
545
Brian Salomon587e08f2017-01-27 10:59:27 -0500546 if (!legalColorModulation && !legalAlphaModulation) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500547 passing = false;
Michael Ludwig314d3772018-10-03 16:04:38 -0400548
549 if (coverageMessage.isEmpty()) {
Michael Ludwig7034f152018-10-08 16:43:58 -0400550 coverageMessage.printf("\"Modulating\" processor %s did not match "
551 "alpha-modulation nor color-modulation rules. "
552 "Input: 0x%08x, Output: 0x%08x, pixel (%d, %d).",
553 fp->name(), input, output, x, y);
Michael Ludwig314d3772018-10-03 16:04:38 -0400554 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500555 }
556 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400557
Brian Osmancb3d0872018-10-16 15:19:28 -0400558 SkPMColor4f input4f = SkPMColor4f::FromBytes_RGBA(input);
559 SkPMColor4f output4f = SkPMColor4f::FromBytes_RGBA(output);
Brian Osman1d5b5982018-10-01 13:41:39 -0400560 SkPMColor4f expected4f;
Michael Ludwig7034f152018-10-08 16:43:58 -0400561 if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) {
Brian Osmancb3d0872018-10-16 15:19:28 -0400562 float rDiff = fabsf(output4f.fR - expected4f.fR);
563 float gDiff = fabsf(output4f.fG - expected4f.fG);
564 float bDiff = fabsf(output4f.fB - expected4f.fB);
565 float aDiff = fabsf(output4f.fA - expected4f.fA);
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500566 static constexpr float kTol = 4 / 255.f;
Brian Salomon587e08f2017-01-27 10:59:27 -0500567 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
Michael Ludwig314d3772018-10-03 16:04:38 -0400568 if (constMessage.isEmpty()) {
569 passing = false;
570
571 constMessage.printf("Processor %s claimed output for const input "
572 "doesn't match actual output. Error: %f, Tolerance: %f, "
573 "input: (%f, %f, %f, %f), actual: (%f, %f, %f, %f), "
Michael Ludwig7034f152018-10-08 16:43:58 -0400574 "expected(%f, %f, %f, %f)", fp->name(),
Michael Ludwig314d3772018-10-03 16:04:38 -0400575 SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))), kTol,
576 input4f.fR, input4f.fG, input4f.fB, input4f.fA,
Brian Osmancb3d0872018-10-16 15:19:28 -0400577 output4f.fR, output4f.fG, output4f.fB, output4f.fA,
578 expected4f.fR, expected4f.fG, expected4f.fB, expected4f.fA);
Michael Ludwig314d3772018-10-03 16:04:38 -0400579 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500580 }
581 }
Brian Osman00b29392018-11-05 15:42:43 -0500582 if (input4f.isOpaque() && fp->preservesOpaqueInput() && !output4f.isOpaque()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500583 passing = false;
Michael Ludwig314d3772018-10-03 16:04:38 -0400584
585 if (opaqueMessage.isEmpty()) {
586 opaqueMessage.printf("Processor %s claimed opaqueness is preserved but "
587 "it is not. Input: 0x%08x, Output: 0x%08x.",
Michael Ludwig7034f152018-10-08 16:43:58 -0400588 fp->name(), input, output);
Michael Ludwige8e10752018-10-01 12:42:53 -0400589 }
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400590 }
Michael Ludwig314d3772018-10-03 16:04:38 -0400591
592 if (!passing) {
593 // Regardless of how many optimizations the pixel violates, count it as a
594 // single bad pixel.
595 failedPixelCount++;
596 }
597 }
598 }
599
600 // Finished analyzing the entire image, see if the number of pixel failures meets the
601 // threshold for an FP violating the optimization requirements.
602 if (failedPixelCount > kMaxAcceptableFailedPixels) {
Michael Ludwig4504a6522018-10-03 16:47:55 -0400603 ERRORF(reporter, "Processor violated %d of %d pixels, seed: 0x%08x, processor: %s"
Michael Ludwig314d3772018-10-03 16:04:38 -0400604 ", first failing pixel details are below:",
605 failedPixelCount, kRenderSize * kRenderSize, seed,
Michael Ludwig7034f152018-10-08 16:43:58 -0400606 fp->dumpInfo().c_str());
Michael Ludwig314d3772018-10-03 16:04:38 -0400607
608 // Print first failing pixel's details.
609 if (!coverageMessage.isEmpty()) {
610 ERRORF(reporter, coverageMessage.c_str());
611 }
612 if (!constMessage.isEmpty()) {
613 ERRORF(reporter, constMessage.c_str());
614 }
615 if (!opaqueMessage.isEmpty()) {
616 ERRORF(reporter, opaqueMessage.c_str());
617 }
618
619 if (!loggedFirstFailure) {
620 // Print with ERRORF to make sure the encoded image is output
621 SkString input;
Michael Ludwig7034f152018-10-08 16:43:58 -0400622 log_surface_proxy(context, inputTexture1, &input);
Michael Ludwig314d3772018-10-03 16:04:38 -0400623 SkString output;
624 log_surface_context(rtc, &output);
625 ERRORF(reporter, "Input image: %s\n\n"
626 "===========================================================\n\n"
627 "Output image: %s\n", input.c_str(), output.c_str());
628 loggedFirstFailure = true;
629 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400630 } else if(failedPixelCount > 0) {
Michael Ludwig314d3772018-10-03 16:04:38 -0400631 // Don't trigger an error, but don't just hide the failures either.
632 INFOF(reporter, "Processor violated %d of %d pixels (below error threshold), seed: "
633 "0x%08x, processor: %s", failedPixelCount, kRenderSize * kRenderSize,
Michael Ludwig7034f152018-10-08 16:43:58 -0400634 seed, fp->dumpInfo().c_str());
Michael Ludwig314d3772018-10-03 16:04:38 -0400635 if (!coverageMessage.isEmpty()) {
636 INFOF(reporter, coverageMessage.c_str());
637 }
638 if (!constMessage.isEmpty()) {
639 INFOF(reporter, constMessage.c_str());
640 }
641 if (!opaqueMessage.isEmpty()) {
642 INFOF(reporter, opaqueMessage.c_str());
643 }
644 if (!loggedFirstWarning) {
645 SkString input;
Michael Ludwig7034f152018-10-08 16:43:58 -0400646 log_surface_proxy(context, inputTexture1, &input);
Michael Ludwig314d3772018-10-03 16:04:38 -0400647 SkString output;
648 log_surface_context(rtc, &output);
649 INFOF(reporter, "Input image: %s\n\n"
650 "===========================================================\n\n"
651 "Output image: %s\n", input.c_str(), output.c_str());
652 loggedFirstWarning = true;
Brian Salomon587e08f2017-01-27 10:59:27 -0500653 }
654 }
655 }
656 }
657}
Robert Phillips18166ee2017-06-01 12:55:44 -0400658
Brian Salomon0e05a822017-07-25 09:43:22 -0400659// Tests that fragment processors returned by GrFragmentProcessor::clone() are equivalent to their
660// progenitors.
661DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) {
662 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500663 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
664 auto resourceProvider = context->priv().resourceProvider();
Brian Salomon0e05a822017-07-25 09:43:22 -0400665
666 SkRandom random;
667
668 // Make the destination context for the test.
669 static constexpr int kRenderSize = 1024;
Robert Phillips9da87e02019-02-04 13:26:26 -0500670 sk_sp<GrRenderTargetContext> rtc = context->priv().makeDeferredRenderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -0400671 SkBackingFit::kExact, kRenderSize, kRenderSize, GrColorType::kRGBA_8888, nullptr);
Brian Salomon0e05a822017-07-25 09:43:22 -0400672
673 sk_sp<GrTextureProxy> proxies[2];
Robert Phillips7eeb74f2019-03-29 07:26:46 -0400674 if (!init_test_textures(resourceProvider, proxyProvider, &random, proxies)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400675 ERRORF(reporter, "Could not create test textures");
676 return;
677 }
678 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
679
Michael Ludwig7034f152018-10-08 16:43:58 -0400680 auto inputTexture = make_input_texture(proxyProvider, kRenderSize, kRenderSize, 0.0f);
Brian Salomon0e05a822017-07-25 09:43:22 -0400681 std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
682 std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
683 auto readInfo = SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
684 kPremul_SkAlphaType);
685
686 // Because processor factories configure themselves in random ways, this is not exhaustive.
687 for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) {
688 static constexpr int kTimesToInvokeFactory = 10;
689 for (int j = 0; j < kTimesToInvokeFactory; ++j) {
690 auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &testData);
691 auto clone = fp->clone();
Brian Salomon0e05a822017-07-25 09:43:22 -0400692 if (!clone) {
Brian Salomon96271cd2017-07-31 16:27:23 -0400693 ERRORF(reporter, "Clone of processor %s failed.", fp->name());
Brian Salomon0e05a822017-07-25 09:43:22 -0400694 continue;
695 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400696 const char* name = fp->name();
Brian Salomonce06e262017-08-01 16:23:40 -0400697 REPORTER_ASSERT(reporter, !strcmp(fp->name(), clone->name()));
698 REPORTER_ASSERT(reporter, fp->compatibleWithCoverageAsAlpha() ==
699 clone->compatibleWithCoverageAsAlpha());
700 REPORTER_ASSERT(reporter, fp->isEqual(*clone));
701 REPORTER_ASSERT(reporter, fp->preservesOpaqueInput() == clone->preservesOpaqueInput());
702 REPORTER_ASSERT(reporter, fp->hasConstantOutputForConstantInput() ==
703 clone->hasConstantOutputForConstantInput());
704 REPORTER_ASSERT(reporter, fp->numChildProcessors() == clone->numChildProcessors());
705 REPORTER_ASSERT(reporter, fp->usesLocalCoords() == clone->usesLocalCoords());
Brian Salomon0e05a822017-07-25 09:43:22 -0400706 // Draw with original and read back the results.
Michael Ludwig7034f152018-10-08 16:43:58 -0400707 render_fp(context, rtc.get(), fp.get(), inputTexture, readData1.get());
Brian Salomon0e05a822017-07-25 09:43:22 -0400708
709 // Draw with clone and read back the results.
Michael Ludwig7034f152018-10-08 16:43:58 -0400710 render_fp(context, rtc.get(), clone.get(), inputTexture, readData2.get());
Brian Salomon0e05a822017-07-25 09:43:22 -0400711
712 // Check that the results are the same.
713 bool passing = true;
714 for (int y = 0; y < kRenderSize && passing; ++y) {
715 for (int x = 0; x < kRenderSize && passing; ++x) {
716 int idx = y * kRenderSize + x;
717 if (readData1[idx] != readData2[idx]) {
718 ERRORF(reporter,
719 "Processor %s made clone produced different output. "
720 "Input color: 0x%08x, Original Output Color: 0x%08x, "
721 "Clone Output Color: 0x%08x..",
Michael Ludwig7034f152018-10-08 16:43:58 -0400722 name, input_texel_color(x, y, 0.0f), readData1[idx], readData2[idx]);
Brian Salomon0e05a822017-07-25 09:43:22 -0400723 passing = false;
724 }
725 }
726 }
727 }
728 }
729}
730
Hal Canary6f6961e2017-01-31 13:50:44 -0500731#endif // GR_TEST_UTILS