blob: e76099ca5f974cb3155a6f3db7183a0f4b331515 [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
8#include "SkTypes.h"
9#include "Test.h"
10
Brian Salomonc65aec92017-03-09 09:03:58 -050011#include "GrClip.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050012#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050013#include "GrContextPriv.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050014#include "GrGpuResource.h"
Robert Phillips7c525e62018-06-12 10:11:12 -040015#include "GrMemoryPool.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050016#include "GrProxyProvider.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050017#include "GrRenderTargetContext.h"
18#include "GrRenderTargetContextPriv.h"
19#include "GrResourceProvider.h"
20#include "glsl/GrGLSLFragmentProcessor.h"
21#include "glsl/GrGLSLFragmentShaderBuilder.h"
Brian Salomon82ddc942017-07-14 12:00:13 -040022#include "ops/GrMeshDrawOp.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040023#include "ops/GrRectOpFactory.h"
Michael Ludwige8e10752018-10-01 12:42:53 -040024#include "TestUtils.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050025
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 Phillipsc994a932018-06-19 13:09:54 -040034 GrOpMemoryPool* pool = context->contextPriv().opMemoryPool();
35
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
Brian Salomon7d94bb52018-10-12 14:37:19 -040041 void visitProxies(const VisitProxyFunc& func, VisitorType) 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
Brian Osman532b3f92018-07-11 10:02:07 -040047 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
Brian Salomon82ddc942017-07-14 12:00:13 -040048 static constexpr GrProcessorAnalysisColor kUnknownColor;
Brian Osmancf860852018-10-31 14:04:39 -040049 SkPMColor4f overrideColor;
Brian Salomon82ddc942017-07-14 12:00:13 -040050 fProcessors.finalize(kUnknownColor, GrProcessorAnalysisCoverage::kNone, clip, false, caps,
Brian Osman532b3f92018-07-11 10:02:07 -040051 &overrideColor);
Brian Salomon82ddc942017-07-14 12:00:13 -040052 return RequiresDstTexture::kNo;
Brian Salomon649a3412017-03-09 13:50:43 -050053 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050054
55private:
Robert Phillips7c525e62018-06-12 10:11:12 -040056 friend class ::GrOpMemoryPool; // for ctor
57
Brian Salomonaff329b2017-08-11 09:40:37 -040058 TestOp(std::unique_ptr<GrFragmentProcessor> fp)
59 : INHERITED(ClassID()), fProcessors(std::move(fp)) {
Brian Salomon82ddc942017-07-14 12:00:13 -040060 this->setBounds(SkRect::MakeWH(100, 100), HasAABloat::kNo, IsZeroArea::kNo);
61 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050062
Brian Salomon91326c32017-08-09 16:02:19 -040063 void onPrepareDraws(Target* target) override { return; }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050064
Brian Salomon82ddc942017-07-14 12:00:13 -040065 GrProcessorSet fProcessors;
66
67 typedef GrMeshDrawOp INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -050068};
69
70/**
71 * FP used to test ref/IO counts on owned GrGpuResources. Can also be a parent FP to test counts
72 * of resources owned by child FPs.
73 */
74class TestFP : public GrFragmentProcessor {
75public:
Brian Salomonaff329b2017-08-11 09:40:37 -040076 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> child) {
77 return std::unique_ptr<GrFragmentProcessor>(new TestFP(std::move(child)));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050078 }
Brian Salomonaff329b2017-08-11 09:40:37 -040079 static std::unique_ptr<GrFragmentProcessor> Make(const SkTArray<sk_sp<GrTextureProxy>>& proxies,
Brian Salomon559f5562017-11-15 14:28:33 -050080 const SkTArray<sk_sp<GrBuffer>>& buffers) {
81 return std::unique_ptr<GrFragmentProcessor>(new TestFP(proxies, buffers));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050082 }
83
84 const char* name() const override { return "test"; }
85
86 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
87 // We don't really care about reusing these.
88 static int32_t gKey = 0;
89 b->add32(sk_atomic_inc(&gKey));
90 }
91
Brian Salomonaff329b2017-08-11 09:40:37 -040092 std::unique_ptr<GrFragmentProcessor> clone() const override {
93 return std::unique_ptr<GrFragmentProcessor>(new TestFP(*this));
Brian Salomonb17e6392017-07-28 13:41:51 -040094 }
95
Brian Salomonbc6b99d2017-01-11 10:32:34 -050096private:
Brian Salomon559f5562017-11-15 14:28:33 -050097 TestFP(const SkTArray<sk_sp<GrTextureProxy>>& proxies, const SkTArray<sk_sp<GrBuffer>>& buffers)
Brian Salomon662ea4b2018-07-12 14:53:49 -040098 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4) {
Robert Phillips30f9bc62017-02-22 15:28:38 -050099 for (const auto& proxy : proxies) {
Brian Salomonf7dcd762018-07-30 14:48:15 -0400100 fSamplers.emplace_back(proxy);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500101 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400102 this->setTextureSamplerCnt(fSamplers.count());
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500103 }
104
Brian Salomonaff329b2017-08-11 09:40:37 -0400105 TestFP(std::unique_ptr<GrFragmentProcessor> child)
Brian Salomon662ea4b2018-07-12 14:53:49 -0400106 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4) {
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500107 this->registerChildProcessor(std::move(child));
108 }
109
Brian Salomon96271cd2017-07-31 16:27:23 -0400110 explicit TestFP(const TestFP& that)
Brian Salomon662ea4b2018-07-12 14:53:49 -0400111 : INHERITED(kTestFP_ClassID, that.optimizationFlags()), fSamplers(4) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400112 for (int i = 0; i < that.fSamplers.count(); ++i) {
113 fSamplers.emplace_back(that.fSamplers[i]);
Brian Salomonb17e6392017-07-28 13:41:51 -0400114 }
Brian Salomon96271cd2017-07-31 16:27:23 -0400115 for (int i = 0; i < that.numChildProcessors(); ++i) {
116 this->registerChildProcessor(that.childProcessor(i).clone());
Brian Salomonb17e6392017-07-28 13:41:51 -0400117 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400118 this->setTextureSamplerCnt(fSamplers.count());
Brian Salomonb17e6392017-07-28 13:41:51 -0400119 }
120
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500121 virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
122 class TestGLSLFP : public GrGLSLFragmentProcessor {
123 public:
124 TestGLSLFP() {}
125 void emitCode(EmitArgs& args) override {
126 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
127 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor);
128 }
129
130 private:
131 };
132 return new TestGLSLFP();
133 }
134
135 bool onIsEqual(const GrFragmentProcessor&) const override { return false; }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400136 const TextureSampler& onTextureSampler(int i) const override { return fSamplers[i]; }
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500137
138 GrTAllocator<TextureSampler> fSamplers;
Brian Salomon587e08f2017-01-27 10:59:27 -0500139 typedef GrFragmentProcessor INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500140};
141}
142
Brian Salomonf046e152017-01-11 15:24:47 -0500143template <typename T>
144inline void testingOnly_getIORefCnts(const T* resource, int* refCnt, int* readCnt, int* writeCnt) {
145 *refCnt = resource->fRefCnt;
146 *readCnt = resource->fPendingReads;
147 *writeCnt = resource->fPendingWrites;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500148}
149
Robert Phillips2f493142017-03-02 18:18:38 -0500150void testingOnly_getIORefCnts(GrTextureProxy* proxy, int* refCnt, int* readCnt, int* writeCnt) {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500151 *refCnt = proxy->getBackingRefCnt_TestOnly();
152 *readCnt = proxy->getPendingReadCnt_TestOnly();
153 *writeCnt = proxy->getPendingWriteCnt_TestOnly();
154}
155
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500156DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) {
157 GrContext* context = ctxInfo.grContext();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500158 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500159
Robert Phillips16d8ec62017-07-27 16:16:25 -0400160 GrSurfaceDesc desc;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500161 desc.fWidth = 10;
162 desc.fHeight = 10;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400163 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500164
Brian Salomonb17e6392017-07-28 13:41:51 -0400165 for (bool makeClone : {false, true}) {
166 for (int parentCnt = 0; parentCnt < 2; parentCnt++) {
167 sk_sp<GrRenderTargetContext> renderTargetContext(
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500168 context->contextPriv().makeDeferredRenderTargetContext(
169 SkBackingFit::kApprox, 1, 1,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500170 kRGBA_8888_GrPixelConfig, nullptr));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500171 {
Brian Salomon2a4f9832018-03-03 22:43:43 -0500172 sk_sp<GrTextureProxy> proxy1 = proxyProvider->createProxy(
173 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
174 sk_sp<GrTextureProxy> proxy2 = proxyProvider->createProxy(
175 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
176 sk_sp<GrTextureProxy> proxy3 = proxyProvider->createProxy(
177 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
178 sk_sp<GrTextureProxy> proxy4 = proxyProvider->createProxy(
179 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
Brian Salomonb17e6392017-07-28 13:41:51 -0400180 {
181 SkTArray<sk_sp<GrTextureProxy>> proxies;
182 SkTArray<sk_sp<GrBuffer>> buffers;
Brian Salomonb17e6392017-07-28 13:41:51 -0400183 proxies.push_back(proxy1);
Brian Salomon559f5562017-11-15 14:28:33 -0500184 auto fp = TestFP::Make(std::move(proxies), std::move(buffers));
Brian Salomonb17e6392017-07-28 13:41:51 -0400185 for (int i = 0; i < parentCnt; ++i) {
186 fp = TestFP::Make(std::move(fp));
187 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400188 std::unique_ptr<GrFragmentProcessor> clone;
Brian Salomonb17e6392017-07-28 13:41:51 -0400189 if (makeClone) {
190 clone = fp->clone();
191 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400192 std::unique_ptr<GrDrawOp> op(TestOp::Make(context, std::move(fp)));
Brian Salomonb17e6392017-07-28 13:41:51 -0400193 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
194 if (clone) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400195 op = TestOp::Make(context, std::move(clone));
Brian Salomonb17e6392017-07-28 13:41:51 -0400196 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
197 }
198 }
199 int refCnt, readCnt, writeCnt;
200
201 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
202 // IO counts should be double if there is a clone of the FP.
203 int ioRefMul = makeClone ? 2 : 1;
Robert Phillips715d08c2018-07-18 13:56:48 -0400204 REPORTER_ASSERT(reporter, -1 == refCnt);
Brian Salomonb17e6392017-07-28 13:41:51 -0400205 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
206 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
207
Brian Salomonb17e6392017-07-28 13:41:51 -0400208 context->flush();
209
210 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
211 REPORTER_ASSERT(reporter, 1 == refCnt);
212 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
213 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
214
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500215 }
216 }
217 }
218}
Brian Salomon587e08f2017-01-27 10:59:27 -0500219
220// This test uses the random GrFragmentProcessor test factory, which relies on static initializers.
221#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
222
Brian Salomon0e05a822017-07-25 09:43:22 -0400223#include "SkCommandLineFlags.h"
224DEFINE_bool(randomProcessorTest, false, "Use non-deterministic seed for random processor tests?");
Michael Ludwig3ad86d02018-09-28 16:00:38 -0400225DEFINE_uint32(processorSeed, 0, "Use specific seed for processor tests. Overridden by " \
226 "--randomProcessorTest.");
Brian Salomon0e05a822017-07-25 09:43:22 -0400227
228#if GR_TEST_UTILS
229
Michael Ludwig7034f152018-10-08 16:43:58 -0400230static GrColor input_texel_color(int i, int j, SkScalar delta) {
231 // Delta must be less than 0.5 to prevent over/underflow issues with the input color
232 SkASSERT(delta <= 0.5);
Brian Salomon587e08f2017-01-27 10:59:27 -0500233
Brian Osmancb3d0872018-10-16 15:19:28 -0400234 SkColor color = SkColorSetARGB((uint8_t)i, (uint8_t)j, (uint8_t)(i + j), (uint8_t)(2 * j - i));
235 SkColor4f color4f = SkColor4f::FromColor(color);
Michael Ludwig7034f152018-10-08 16:43:58 -0400236 for (int i = 0; i < 4; i++) {
Brian Osmancb3d0872018-10-16 15:19:28 -0400237 if (color4f[i] > 0.5) {
238 color4f[i] -= delta;
Michael Ludwig7034f152018-10-08 16:43:58 -0400239 } else {
Brian Osmancb3d0872018-10-16 15:19:28 -0400240 color4f[i] += delta;
Michael Ludwig7034f152018-10-08 16:43:58 -0400241 }
242 }
Brian Osmancb3d0872018-10-16 15:19:28 -0400243 return color4f.premul().toBytes_RGBA();
Brian Salomon0e05a822017-07-25 09:43:22 -0400244}
Brian Salomon587e08f2017-01-27 10:59:27 -0500245
Robert Phillips7c525e62018-06-12 10:11:12 -0400246void test_draw_op(GrContext* context,
247 GrRenderTargetContext* rtc,
248 std::unique_ptr<GrFragmentProcessor> fp,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500249 sk_sp<GrTextureProxy> inputDataProxy) {
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500250 GrPaint paint;
Brian Osman2240be92017-10-18 13:15:13 -0400251 paint.addColorTextureProcessor(std::move(inputDataProxy), SkMatrix::I());
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500252 paint.addColorFragmentProcessor(std::move(fp));
253 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonac70f842017-05-08 10:43:33 -0400254
Robert Phillips7c525e62018-06-12 10:11:12 -0400255 auto op = GrRectOpFactory::MakeNonAAFill(context, std::move(paint), SkMatrix::I(),
Brian Salomonbaaf4392017-06-15 09:59:23 -0400256 SkRect::MakeWH(rtc->width(), rtc->height()),
257 GrAAType::kNone);
Brian Salomonac70f842017-05-08 10:43:33 -0400258 rtc->addDrawOp(GrNoClip(), std::move(op));
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500259}
260
Michael Ludwig7034f152018-10-08 16:43:58 -0400261// This assumes that the output buffer will be the same size as inputDataProxy
262void render_fp(GrContext* context, GrRenderTargetContext* rtc, GrFragmentProcessor* fp,
263 sk_sp<GrTextureProxy> inputDataProxy, GrColor* buffer) {
264 int width = inputDataProxy->width();
265 int height = inputDataProxy->height();
266
267 // test_draw_op needs to take ownership of an FP, so give it a clone that it can own
268 test_draw_op(context, rtc, fp->clone(), inputDataProxy);
269 memset(buffer, 0x0, sizeof(GrColor) * width * height);
270 rtc->readPixels(SkImageInfo::Make(width, height, kRGBA_8888_SkColorType,
271 kPremul_SkAlphaType),
272 buffer, 0, 0, 0);
273}
274
Brian Salomon0e05a822017-07-25 09:43:22 -0400275/** Initializes the two test texture proxies that are available to the FP test factories. */
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500276bool init_test_textures(GrProxyProvider* proxyProvider, SkRandom* random,
277 sk_sp<GrTextureProxy> proxies[2]) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400278 static const int kTestTextureSize = 256;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400279
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500280 {
281 // Put premul data into the RGBA texture that the test FPs can optionally use.
282 std::unique_ptr<GrColor[]> rgbaData(new GrColor[kTestTextureSize * kTestTextureSize]);
283 for (int y = 0; y < kTestTextureSize; ++y) {
284 for (int x = 0; x < kTestTextureSize; ++x) {
Michael Ludwig7034f152018-10-08 16:43:58 -0400285 rgbaData[kTestTextureSize * y + x] = input_texel_color(
286 random->nextULessThan(256), random->nextULessThan(256), 0.0f);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500287 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400288 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400289
Brian Osman2700abc2018-09-12 10:19:41 -0400290 SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize,
291 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
292 SkPixmap pixmap(ii, rgbaData.get(), ii.minRowBytes());
293 sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap);
294 proxies[0] = proxyProvider->createTextureProxy(img, kNone_GrSurfaceFlags, 1,
295 SkBudgeted::kYes, SkBackingFit::kExact);
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,
312 SkBudgeted::kYes, SkBackingFit::kExact);
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);
332 return proxyProvider->createTextureProxy(img, kNone_GrSurfaceFlags, 1,
333 SkBudgeted::kYes, 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));
341 SkAssertResult(src->readPixels(ii, bm.getPixels(), bm.rowBytes(), 0, 0));
342
343 return bitmap_to_base64_data_uri(bm, dst);
344}
345
346bool log_surface_proxy(GrContext* context, sk_sp<GrSurfaceProxy> src, SkString* dst) {
347 sk_sp<GrSurfaceContext> sContext(context->contextPriv().makeWrappedSurfaceContext(src));
348 return log_surface_context(sContext, dst);
349}
350
Michael Ludwig7034f152018-10-08 16:43:58 -0400351bool fuzzy_color_equals(const SkPMColor4f& c1, const SkPMColor4f& c2) {
352 // With the loss of precision of rendering into 32-bit color, then estimating the FP's output
353 // from that, it is not uncommon for a valid output to differ from estimate by up to 0.01
354 // (really 1/128 ~ .0078, but frequently floating point issues make that tolerance a little
355 // too unforgiving).
356 static constexpr SkScalar kTolerance = 0.01f;
357 for (int i = 0; i < 4; i++) {
358 if (!SkScalarNearlyEqual(c1[i], c2[i], kTolerance)) {
359 return false;
360 }
361 }
362 return true;
363}
364
365int modulation_index(int channelIndex, bool alphaModulation) {
366 return alphaModulation ? 3 : channelIndex;
367}
368
369// Given three input colors (color preceding the FP being tested), and the output of the FP, this
370// ensures that the out1 = fp * in1.a, out2 = fp * in2.a, and out3 = fp * in3.a, where fp is the
371// pre-modulated color that should not be changing across frames (FP's state doesn't change).
372//
373// When alphaModulation is false, this tests the very similar conditions that out1 = fp * in1,
374// etc. using per-channel modulation instead of modulation by just the input alpha channel.
375// - This estimates the pre-modulated fp color from one of the input/output pairs and confirms the
376// conditions hold for the other two pairs.
377bool legal_modulation(const GrColor& in1, const GrColor& in2, const GrColor& in3,
378 const GrColor& out1, const GrColor& out2, const GrColor& out3,
379 bool alphaModulation) {
380 // Convert to floating point, which is the number space the FP operates in (more or less)
Brian Osmancb3d0872018-10-16 15:19:28 -0400381 SkPMColor4f in1f = SkPMColor4f::FromBytes_RGBA(in1);
382 SkPMColor4f in2f = SkPMColor4f::FromBytes_RGBA(in2);
383 SkPMColor4f in3f = SkPMColor4f::FromBytes_RGBA(in3);
384 SkPMColor4f out1f = SkPMColor4f::FromBytes_RGBA(out1);
385 SkPMColor4f out2f = SkPMColor4f::FromBytes_RGBA(out2);
386 SkPMColor4f out3f = SkPMColor4f::FromBytes_RGBA(out3);
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).
392 SkPMColor4f fpPreModulation;
393 for (int i = 0; i < 4; i++) {
394 int modulationIndex = modulation_index(i, alphaModulation);
395 if (in1f[modulationIndex] < 0.2f) {
396 // Use the stepped frame
397 fpPreModulation[i] = out2f[i] / in2f[modulationIndex];
398 } else {
399 fpPreModulation[i] = out1f[i] / in1f[modulationIndex];
400 }
401 }
402
403 // With reconstructed pre-modulated FP output, derive the expected value of fp * input for each
404 // of the transformed input colors.
Brian Osmancb3d0872018-10-16 15:19:28 -0400405 SkPMColor4f expected1 = alphaModulation ? (fpPreModulation * in1f.fA)
406 : (fpPreModulation * in1f);
407 SkPMColor4f expected2 = alphaModulation ? (fpPreModulation * in2f.fA)
408 : (fpPreModulation * in2f);
409 SkPMColor4f expected3 = alphaModulation ? (fpPreModulation * in3f.fA)
410 : (fpPreModulation * in3f);
Michael Ludwig7034f152018-10-08 16:43:58 -0400411
Brian Osmancb3d0872018-10-16 15:19:28 -0400412 return fuzzy_color_equals(out1f, expected1) &&
413 fuzzy_color_equals(out2f, expected2) &&
414 fuzzy_color_equals(out3f, expected3);
Michael Ludwig7034f152018-10-08 16:43:58 -0400415}
416
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500417DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500418 GrContext* context = ctxInfo.grContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500419 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500420 auto resourceProvider = context->contextPriv().resourceProvider();
Brian Salomon1c053642017-07-24 10:16:19 -0400421 using FPFactory = GrFragmentProcessorTestFactory;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400422
Michael Ludwig3ad86d02018-09-28 16:00:38 -0400423 uint32_t seed = FLAGS_processorSeed;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400424 if (FLAGS_randomProcessorTest) {
425 std::random_device rd;
426 seed = rd();
427 }
428 // 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 -0400429 // use --processorSeed <seed> (without --randomProcessorTest) to reproduce.
Brian Osmanc35a2d42017-03-17 10:58:53 -0400430 SkRandom random(seed);
431
Brian Salomon0e05a822017-07-25 09:43:22 -0400432 // Make the destination context for the test.
433 static constexpr int kRenderSize = 256;
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500434 sk_sp<GrRenderTargetContext> rtc = context->contextPriv().makeDeferredRenderTargetContext(
Brian Salomon0e05a822017-07-25 09:43:22 -0400435 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500436
Robert Phillipse78b7252017-04-06 07:59:41 -0400437 sk_sp<GrTextureProxy> proxies[2];
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500438 if (!init_test_textures(proxyProvider, &random, proxies)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400439 ERRORF(reporter, "Could not create test textures");
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400440 return;
441 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400442 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
Brian Salomon587e08f2017-01-27 10:59:27 -0500443
Michael Ludwig7034f152018-10-08 16:43:58 -0400444 // Coverage optimization uses three frames with a linearly transformed input texture. The first
445 // frame has no offset, second frames add .2 and .4, which should then be present as a fixed
446 // difference between the frame outputs if the FP is properly following the modulation
447 // requirements of the coverage optimization.
448 static constexpr SkScalar kInputDelta = 0.2f;
449 auto inputTexture1 = make_input_texture(proxyProvider, kRenderSize, kRenderSize, 0.0f);
450 auto inputTexture2 = make_input_texture(proxyProvider, kRenderSize, kRenderSize, kInputDelta);
451 auto inputTexture3 = make_input_texture(proxyProvider, kRenderSize, kRenderSize, 2*kInputDelta);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500452
Michael Ludwige8e10752018-10-01 12:42:53 -0400453 // Encoded images are very verbose and this tests many potential images, so only export the
454 // first failure (subsequent failures have a reasonable chance of being related).
455 bool loggedFirstFailure = false;
Michael Ludwig314d3772018-10-03 16:04:38 -0400456 bool loggedFirstWarning = false;
Michael Ludwig7034f152018-10-08 16:43:58 -0400457
458 // Storage for the three frames required for coverage compatibility optimization. Each frame
459 // uses the correspondingly numbered inputTextureX.
460 std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
461 std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
462 std::unique_ptr<GrColor[]> readData3(new GrColor[kRenderSize * kRenderSize]);
463
Brian Salomon0e05a822017-07-25 09:43:22 -0400464 // Because processor factories configure themselves in random ways, this is not exhaustive.
Brian Salomon587e08f2017-01-27 10:59:27 -0500465 for (int i = 0; i < FPFactory::Count(); ++i) {
466 int timesToInvokeFactory = 5;
467 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
468 // on child optimizations being present.
Brian Salomonaff329b2017-08-11 09:40:37 -0400469 std::unique_ptr<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
Brian Salomon587e08f2017-01-27 10:59:27 -0500470 for (int j = 0; j < fp->numChildProcessors(); ++j) {
471 // This value made a reasonable trade off between time and coverage when this test was
472 // written.
473 timesToInvokeFactory *= FPFactory::Count() / 2;
474 }
475 for (int j = 0; j < timesToInvokeFactory; ++j) {
476 fp = FPFactory::MakeIdx(i, &testData);
Robert Phillips6be756b2018-01-16 15:07:54 -0500477 if (!fp->instantiate(resourceProvider)) {
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400478 continue;
479 }
480
Brian Salomon587e08f2017-01-27 10:59:27 -0500481 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
Brian Salomonf3b995b2017-02-15 10:22:23 -0500482 !fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500483 continue;
484 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400485
Michael Ludwig7034f152018-10-08 16:43:58 -0400486 if (fp->compatibleWithCoverageAsAlpha()) {
487 // 2nd and 3rd frames are only used when checking coverage optimization
488 render_fp(context, rtc.get(), fp.get(), inputTexture2, readData2.get());
489 render_fp(context, rtc.get(), fp.get(), inputTexture3, readData3.get());
490 }
491 // Draw base frame last so that rtc holds the original FP behavior if we need to
492 // dump the image to the log.
493 render_fp(context, rtc.get(), fp.get(), inputTexture1, readData1.get());
Brian Salomonaff329b2017-08-11 09:40:37 -0400494
Brian Salomon587e08f2017-01-27 10:59:27 -0500495 if (0) { // Useful to see what FPs are being tested.
496 SkString children;
Michael Ludwig7034f152018-10-08 16:43:58 -0400497 for (int c = 0; c < fp->numChildProcessors(); ++c) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500498 if (!c) {
499 children.append("(");
500 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400501 children.append(fp->childProcessor(c).name());
502 children.append(c == fp->numChildProcessors() - 1 ? ")" : ", ");
Brian Salomon587e08f2017-01-27 10:59:27 -0500503 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400504 SkDebugf("%s %s\n", fp->name(), children.c_str());
Brian Salomon587e08f2017-01-27 10:59:27 -0500505 }
Michael Ludwig314d3772018-10-03 16:04:38 -0400506
507 // This test has a history of being flaky on a number of devices. If an FP is logically
508 // violating the optimizations, it's reasonable to expect it to violate requirements on
509 // a large number of pixels in the image. Sporadic pixel violations are more indicative
510 // of device errors and represents a separate problem.
511#if defined(SK_SKQP_GLOBAL_ERROR_TOLERANCE)
512 static constexpr int kMaxAcceptableFailedPixels = 0; // Strict when running as SKQP
513#else
514 static constexpr int kMaxAcceptableFailedPixels = 2 * kRenderSize; // ~0.7% of the image
515#endif
516
517 int failedPixelCount = 0;
518 // Collect first optimization failure message, to be output later as a warning or an
519 // error depending on whether the rendering "passed" or failed.
520 SkString coverageMessage;
521 SkString opaqueMessage;
522 SkString constMessage;
523 for (int y = 0; y < kRenderSize; ++y) {
524 for (int x = 0; x < kRenderSize; ++x) {
525 bool passing = true;
Michael Ludwig7034f152018-10-08 16:43:58 -0400526 GrColor input = input_texel_color(x, y, 0.0f);
527 GrColor output = readData1.get()[y * kRenderSize + x];
528
529 if (fp->compatibleWithCoverageAsAlpha()) {
530 GrColor i2 = input_texel_color(x, y, kInputDelta);
531 GrColor i3 = input_texel_color(x, y, 2 * kInputDelta);
532
533 GrColor o2 = readData2.get()[y * kRenderSize + x];
534 GrColor o3 = readData3.get()[y * kRenderSize + x];
535
536 // A compatible processor is allowed to modulate either the input color or
Brian Salomon587e08f2017-01-27 10:59:27 -0500537 // just the input alpha.
Michael Ludwig7034f152018-10-08 16:43:58 -0400538 bool legalAlphaModulation = legal_modulation(input, i2, i3, output, o2, o3,
539 /* alpha */ true);
540 bool legalColorModulation = legal_modulation(input, i2, i3, output, o2, o3,
541 /* alpha */ false);
542
Brian Salomon587e08f2017-01-27 10:59:27 -0500543 if (!legalColorModulation && !legalAlphaModulation) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500544 passing = false;
Michael Ludwig314d3772018-10-03 16:04:38 -0400545
546 if (coverageMessage.isEmpty()) {
Michael Ludwig7034f152018-10-08 16:43:58 -0400547 coverageMessage.printf("\"Modulating\" processor %s did not match "
548 "alpha-modulation nor color-modulation rules. "
549 "Input: 0x%08x, Output: 0x%08x, pixel (%d, %d).",
550 fp->name(), input, output, x, y);
Michael Ludwig314d3772018-10-03 16:04:38 -0400551 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500552 }
553 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400554
Brian Osmancb3d0872018-10-16 15:19:28 -0400555 SkPMColor4f input4f = SkPMColor4f::FromBytes_RGBA(input);
556 SkPMColor4f output4f = SkPMColor4f::FromBytes_RGBA(output);
Brian Osman1d5b5982018-10-01 13:41:39 -0400557 SkPMColor4f expected4f;
Michael Ludwig7034f152018-10-08 16:43:58 -0400558 if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) {
Brian Osmancb3d0872018-10-16 15:19:28 -0400559 float rDiff = fabsf(output4f.fR - expected4f.fR);
560 float gDiff = fabsf(output4f.fG - expected4f.fG);
561 float bDiff = fabsf(output4f.fB - expected4f.fB);
562 float aDiff = fabsf(output4f.fA - expected4f.fA);
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500563 static constexpr float kTol = 4 / 255.f;
Brian Salomon587e08f2017-01-27 10:59:27 -0500564 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
Michael Ludwig314d3772018-10-03 16:04:38 -0400565 if (constMessage.isEmpty()) {
566 passing = false;
567
568 constMessage.printf("Processor %s claimed output for const input "
569 "doesn't match actual output. Error: %f, Tolerance: %f, "
570 "input: (%f, %f, %f, %f), actual: (%f, %f, %f, %f), "
Michael Ludwig7034f152018-10-08 16:43:58 -0400571 "expected(%f, %f, %f, %f)", fp->name(),
Michael Ludwig314d3772018-10-03 16:04:38 -0400572 SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))), kTol,
573 input4f.fR, input4f.fG, input4f.fB, input4f.fA,
Brian Osmancb3d0872018-10-16 15:19:28 -0400574 output4f.fR, output4f.fG, output4f.fB, output4f.fA,
575 expected4f.fR, expected4f.fG, expected4f.fB, expected4f.fA);
Michael Ludwig314d3772018-10-03 16:04:38 -0400576 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500577 }
578 }
Brian Osman00b29392018-11-05 15:42:43 -0500579 if (input4f.isOpaque() && fp->preservesOpaqueInput() && !output4f.isOpaque()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500580 passing = false;
Michael Ludwig314d3772018-10-03 16:04:38 -0400581
582 if (opaqueMessage.isEmpty()) {
583 opaqueMessage.printf("Processor %s claimed opaqueness is preserved but "
584 "it is not. Input: 0x%08x, Output: 0x%08x.",
Michael Ludwig7034f152018-10-08 16:43:58 -0400585 fp->name(), input, output);
Michael Ludwige8e10752018-10-01 12:42:53 -0400586 }
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400587 }
Michael Ludwig314d3772018-10-03 16:04:38 -0400588
589 if (!passing) {
590 // Regardless of how many optimizations the pixel violates, count it as a
591 // single bad pixel.
592 failedPixelCount++;
593 }
594 }
595 }
596
597 // Finished analyzing the entire image, see if the number of pixel failures meets the
598 // threshold for an FP violating the optimization requirements.
599 if (failedPixelCount > kMaxAcceptableFailedPixels) {
Michael Ludwig4504a6522018-10-03 16:47:55 -0400600 ERRORF(reporter, "Processor violated %d of %d pixels, seed: 0x%08x, processor: %s"
Michael Ludwig314d3772018-10-03 16:04:38 -0400601 ", first failing pixel details are below:",
602 failedPixelCount, kRenderSize * kRenderSize, seed,
Michael Ludwig7034f152018-10-08 16:43:58 -0400603 fp->dumpInfo().c_str());
Michael Ludwig314d3772018-10-03 16:04:38 -0400604
605 // Print first failing pixel's details.
606 if (!coverageMessage.isEmpty()) {
607 ERRORF(reporter, coverageMessage.c_str());
608 }
609 if (!constMessage.isEmpty()) {
610 ERRORF(reporter, constMessage.c_str());
611 }
612 if (!opaqueMessage.isEmpty()) {
613 ERRORF(reporter, opaqueMessage.c_str());
614 }
615
616 if (!loggedFirstFailure) {
617 // Print with ERRORF to make sure the encoded image is output
618 SkString input;
Michael Ludwig7034f152018-10-08 16:43:58 -0400619 log_surface_proxy(context, inputTexture1, &input);
Michael Ludwig314d3772018-10-03 16:04:38 -0400620 SkString output;
621 log_surface_context(rtc, &output);
622 ERRORF(reporter, "Input image: %s\n\n"
623 "===========================================================\n\n"
624 "Output image: %s\n", input.c_str(), output.c_str());
625 loggedFirstFailure = true;
626 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400627 } else if(failedPixelCount > 0) {
Michael Ludwig314d3772018-10-03 16:04:38 -0400628 // Don't trigger an error, but don't just hide the failures either.
629 INFOF(reporter, "Processor violated %d of %d pixels (below error threshold), seed: "
630 "0x%08x, processor: %s", failedPixelCount, kRenderSize * kRenderSize,
Michael Ludwig7034f152018-10-08 16:43:58 -0400631 seed, fp->dumpInfo().c_str());
Michael Ludwig314d3772018-10-03 16:04:38 -0400632 if (!coverageMessage.isEmpty()) {
633 INFOF(reporter, coverageMessage.c_str());
634 }
635 if (!constMessage.isEmpty()) {
636 INFOF(reporter, constMessage.c_str());
637 }
638 if (!opaqueMessage.isEmpty()) {
639 INFOF(reporter, opaqueMessage.c_str());
640 }
641 if (!loggedFirstWarning) {
642 SkString input;
Michael Ludwig7034f152018-10-08 16:43:58 -0400643 log_surface_proxy(context, inputTexture1, &input);
Michael Ludwig314d3772018-10-03 16:04:38 -0400644 SkString output;
645 log_surface_context(rtc, &output);
646 INFOF(reporter, "Input image: %s\n\n"
647 "===========================================================\n\n"
648 "Output image: %s\n", input.c_str(), output.c_str());
649 loggedFirstWarning = true;
Brian Salomon587e08f2017-01-27 10:59:27 -0500650 }
651 }
652 }
653 }
654}
Robert Phillips18166ee2017-06-01 12:55:44 -0400655
Brian Salomon0e05a822017-07-25 09:43:22 -0400656// Tests that fragment processors returned by GrFragmentProcessor::clone() are equivalent to their
657// progenitors.
658DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) {
659 GrContext* context = ctxInfo.grContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500660 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500661 auto resourceProvider = context->contextPriv().resourceProvider();
Brian Salomon0e05a822017-07-25 09:43:22 -0400662
663 SkRandom random;
664
665 // Make the destination context for the test.
666 static constexpr int kRenderSize = 1024;
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500667 sk_sp<GrRenderTargetContext> rtc = context->contextPriv().makeDeferredRenderTargetContext(
Brian Salomon0e05a822017-07-25 09:43:22 -0400668 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
669
670 sk_sp<GrTextureProxy> proxies[2];
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500671 if (!init_test_textures(proxyProvider, &random, proxies)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400672 ERRORF(reporter, "Could not create test textures");
673 return;
674 }
675 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
676
Michael Ludwig7034f152018-10-08 16:43:58 -0400677 auto inputTexture = make_input_texture(proxyProvider, kRenderSize, kRenderSize, 0.0f);
Brian Salomon0e05a822017-07-25 09:43:22 -0400678 std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
679 std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
680 auto readInfo = SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
681 kPremul_SkAlphaType);
682
683 // Because processor factories configure themselves in random ways, this is not exhaustive.
684 for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) {
685 static constexpr int kTimesToInvokeFactory = 10;
686 for (int j = 0; j < kTimesToInvokeFactory; ++j) {
687 auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &testData);
688 auto clone = fp->clone();
Brian Salomon0e05a822017-07-25 09:43:22 -0400689 if (!clone) {
Brian Salomon96271cd2017-07-31 16:27:23 -0400690 ERRORF(reporter, "Clone of processor %s failed.", fp->name());
Brian Salomon0e05a822017-07-25 09:43:22 -0400691 continue;
692 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400693 const char* name = fp->name();
Robert Phillips6be756b2018-01-16 15:07:54 -0500694 if (!fp->instantiate(resourceProvider) || !clone->instantiate(resourceProvider)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400695 continue;
696 }
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
732#endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS