blob: d5b27aba136400a4c06e85a017cd13a14bc0ca6b [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"
Michael Ludwig72ab3462018-12-10 12:43:36 -050022#include "ops/GrFillRectOp.h"
Brian Salomon82ddc942017-07-14 12:00:13 -040023#include "ops/GrMeshDrawOp.h"
Michael Ludwige8e10752018-10-01 12:42:53 -040024#include "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
Brian Salomon7d94bb52018-10-12 14:37:19 -040042 void visitProxies(const VisitProxyFunc& func, VisitorType) 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 Dalton4b62aed2019-01-15 11:53:00 -070048 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
Brian Salomon82ddc942017-07-14 12:00:13 -040049 static constexpr GrProcessorAnalysisColor kUnknownColor;
Brian Osmancf860852018-10-31 14:04:39 -040050 SkPMColor4f overrideColor;
Chris Dalton4b62aed2019-01-15 11:53:00 -070051 return fProcessors.finalize(kUnknownColor, GrProcessorAnalysisCoverage::kNone, clip, false,
52 caps, &overrideColor);
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; }
Chris Dalton07cdcfc92019-02-26 11:13:22 -070064 void onExecute(GrOpFlushState*, const SkRect&) override { return; }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050065
Brian Salomon82ddc942017-07-14 12:00:13 -040066 GrProcessorSet fProcessors;
67
68 typedef GrMeshDrawOp INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -050069};
70
71/**
72 * FP used to test ref/IO counts on owned GrGpuResources. Can also be a parent FP to test counts
73 * of resources owned by child FPs.
74 */
75class TestFP : public GrFragmentProcessor {
76public:
Brian Salomonaff329b2017-08-11 09:40:37 -040077 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> child) {
78 return std::unique_ptr<GrFragmentProcessor>(new TestFP(std::move(child)));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050079 }
Brian Salomonaff329b2017-08-11 09:40:37 -040080 static std::unique_ptr<GrFragmentProcessor> Make(const SkTArray<sk_sp<GrTextureProxy>>& proxies,
Brian Salomondbf70722019-02-07 11:31:24 -050081 const SkTArray<sk_sp<GrGpuBuffer>>& buffers) {
Brian Salomon559f5562017-11-15 14:28:33 -050082 return std::unique_ptr<GrFragmentProcessor>(new TestFP(proxies, buffers));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050083 }
84
85 const char* name() const override { return "test"; }
86
87 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
Mike Klein0ec1c572018-12-04 11:52:51 -050088 static std::atomic<int32_t> nextKey{0};
89 b->add32(nextKey++);
Brian Salomonbc6b99d2017-01-11 10:32:34 -050090 }
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 Salomondbf70722019-02-07 11:31:24 -050097 TestFP(const SkTArray<sk_sp<GrTextureProxy>>& proxies,
98 const SkTArray<sk_sp<GrGpuBuffer>>& buffers)
Brian Salomon662ea4b2018-07-12 14:53:49 -040099 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4) {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500100 for (const auto& proxy : proxies) {
Brian Salomonf7dcd762018-07-30 14:48:15 -0400101 fSamplers.emplace_back(proxy);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500102 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400103 this->setTextureSamplerCnt(fSamplers.count());
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500104 }
105
Brian Salomonaff329b2017-08-11 09:40:37 -0400106 TestFP(std::unique_ptr<GrFragmentProcessor> child)
Brian Salomon662ea4b2018-07-12 14:53:49 -0400107 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4) {
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500108 this->registerChildProcessor(std::move(child));
109 }
110
Brian Salomon96271cd2017-07-31 16:27:23 -0400111 explicit TestFP(const TestFP& that)
Brian Salomon662ea4b2018-07-12 14:53:49 -0400112 : INHERITED(kTestFP_ClassID, that.optimizationFlags()), fSamplers(4) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400113 for (int i = 0; i < that.fSamplers.count(); ++i) {
114 fSamplers.emplace_back(that.fSamplers[i]);
Brian Salomonb17e6392017-07-28 13:41:51 -0400115 }
Brian Salomon96271cd2017-07-31 16:27:23 -0400116 for (int i = 0; i < that.numChildProcessors(); ++i) {
117 this->registerChildProcessor(that.childProcessor(i).clone());
Brian Salomonb17e6392017-07-28 13:41:51 -0400118 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400119 this->setTextureSamplerCnt(fSamplers.count());
Brian Salomonb17e6392017-07-28 13:41:51 -0400120 }
121
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500122 virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
123 class TestGLSLFP : public GrGLSLFragmentProcessor {
124 public:
125 TestGLSLFP() {}
126 void emitCode(EmitArgs& args) override {
127 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
128 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor);
129 }
130
131 private:
132 };
133 return new TestGLSLFP();
134 }
135
136 bool onIsEqual(const GrFragmentProcessor&) const override { return false; }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400137 const TextureSampler& onTextureSampler(int i) const override { return fSamplers[i]; }
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500138
139 GrTAllocator<TextureSampler> fSamplers;
Brian Salomon587e08f2017-01-27 10:59:27 -0500140 typedef GrFragmentProcessor INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500141};
142}
143
Brian Salomonf046e152017-01-11 15:24:47 -0500144template <typename T>
145inline void testingOnly_getIORefCnts(const T* resource, int* refCnt, int* readCnt, int* writeCnt) {
146 *refCnt = resource->fRefCnt;
147 *readCnt = resource->fPendingReads;
148 *writeCnt = resource->fPendingWrites;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500149}
150
Robert Phillips2f493142017-03-02 18:18:38 -0500151void testingOnly_getIORefCnts(GrTextureProxy* proxy, int* refCnt, int* readCnt, int* writeCnt) {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500152 *refCnt = proxy->getBackingRefCnt_TestOnly();
153 *readCnt = proxy->getPendingReadCnt_TestOnly();
154 *writeCnt = proxy->getPendingWriteCnt_TestOnly();
155}
156
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500157DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) {
158 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500159 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500160
Robert Phillips16d8ec62017-07-27 16:16:25 -0400161 GrSurfaceDesc desc;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500162 desc.fWidth = 10;
163 desc.fHeight = 10;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400164 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500165
Greg Daniel4065d452018-11-16 15:43:41 -0500166 const GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500167 context->priv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Greg Daniel4065d452018-11-16 15:43:41 -0500168
Brian Salomonb17e6392017-07-28 13:41:51 -0400169 for (bool makeClone : {false, true}) {
170 for (int parentCnt = 0; parentCnt < 2; parentCnt++) {
171 sk_sp<GrRenderTargetContext> renderTargetContext(
Robert Phillips9da87e02019-02-04 13:26:26 -0500172 context->priv().makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -0500173 format, SkBackingFit::kApprox, 1, 1,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500174 kRGBA_8888_GrPixelConfig, nullptr));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500175 {
Brian Salomon2a4f9832018-03-03 22:43:43 -0500176 sk_sp<GrTextureProxy> proxy1 = proxyProvider->createProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500177 format, desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact,
178 SkBudgeted::kYes);
Brian Salomon2a4f9832018-03-03 22:43:43 -0500179 sk_sp<GrTextureProxy> proxy2 = proxyProvider->createProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500180 format, desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact,
181 SkBudgeted::kYes);
Brian Salomon2a4f9832018-03-03 22:43:43 -0500182 sk_sp<GrTextureProxy> proxy3 = proxyProvider->createProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500183 format, desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact,
184 SkBudgeted::kYes);
Brian Salomon2a4f9832018-03-03 22:43:43 -0500185 sk_sp<GrTextureProxy> proxy4 = proxyProvider->createProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500186 format, desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact,
187 SkBudgeted::kYes);
Brian Salomonb17e6392017-07-28 13:41:51 -0400188 {
189 SkTArray<sk_sp<GrTextureProxy>> proxies;
Brian Salomondbf70722019-02-07 11:31:24 -0500190 SkTArray<sk_sp<GrGpuBuffer>> buffers;
Brian Salomonb17e6392017-07-28 13:41:51 -0400191 proxies.push_back(proxy1);
Brian Salomon559f5562017-11-15 14:28:33 -0500192 auto fp = TestFP::Make(std::move(proxies), std::move(buffers));
Brian Salomonb17e6392017-07-28 13:41:51 -0400193 for (int i = 0; i < parentCnt; ++i) {
194 fp = TestFP::Make(std::move(fp));
195 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400196 std::unique_ptr<GrFragmentProcessor> clone;
Brian Salomonb17e6392017-07-28 13:41:51 -0400197 if (makeClone) {
198 clone = fp->clone();
199 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400200 std::unique_ptr<GrDrawOp> op(TestOp::Make(context, std::move(fp)));
Brian Salomonb17e6392017-07-28 13:41:51 -0400201 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
202 if (clone) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400203 op = TestOp::Make(context, std::move(clone));
Brian Salomonb17e6392017-07-28 13:41:51 -0400204 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
205 }
206 }
207 int refCnt, readCnt, writeCnt;
208
209 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
210 // IO counts should be double if there is a clone of the FP.
211 int ioRefMul = makeClone ? 2 : 1;
Robert Phillips715d08c2018-07-18 13:56:48 -0400212 REPORTER_ASSERT(reporter, -1 == refCnt);
Brian Salomonb17e6392017-07-28 13:41:51 -0400213 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
214 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
215
Brian Salomonb17e6392017-07-28 13:41:51 -0400216 context->flush();
217
218 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
219 REPORTER_ASSERT(reporter, 1 == refCnt);
220 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
221 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
222
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500223 }
224 }
225 }
226}
Brian Salomon587e08f2017-01-27 10:59:27 -0500227
228// This test uses the random GrFragmentProcessor test factory, which relies on static initializers.
229#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
230
Brian Salomon0e05a822017-07-25 09:43:22 -0400231#include "SkCommandLineFlags.h"
232DEFINE_bool(randomProcessorTest, false, "Use non-deterministic seed for random processor tests?");
Michael Ludwig3ad86d02018-09-28 16:00:38 -0400233DEFINE_uint32(processorSeed, 0, "Use specific seed for processor tests. Overridden by " \
234 "--randomProcessorTest.");
Brian Salomon0e05a822017-07-25 09:43:22 -0400235
236#if GR_TEST_UTILS
237
Michael Ludwig7034f152018-10-08 16:43:58 -0400238static GrColor input_texel_color(int i, int j, SkScalar delta) {
239 // Delta must be less than 0.5 to prevent over/underflow issues with the input color
240 SkASSERT(delta <= 0.5);
Brian Salomon587e08f2017-01-27 10:59:27 -0500241
Brian Osman50ea3c02019-02-04 10:01:53 -0500242 SkColor color = SkColorSetARGB((uint8_t)(i & 0xFF),
243 (uint8_t)(j & 0xFF),
244 (uint8_t)((i + j) & 0xFF),
245 (uint8_t)((2 * j - i) & 0xFF));
Brian Osmancb3d0872018-10-16 15:19:28 -0400246 SkColor4f color4f = SkColor4f::FromColor(color);
Michael Ludwig7034f152018-10-08 16:43:58 -0400247 for (int i = 0; i < 4; i++) {
Brian Osmancb3d0872018-10-16 15:19:28 -0400248 if (color4f[i] > 0.5) {
249 color4f[i] -= delta;
Michael Ludwig7034f152018-10-08 16:43:58 -0400250 } else {
Brian Osmancb3d0872018-10-16 15:19:28 -0400251 color4f[i] += delta;
Michael Ludwig7034f152018-10-08 16:43:58 -0400252 }
253 }
Brian Osmancb3d0872018-10-16 15:19:28 -0400254 return color4f.premul().toBytes_RGBA();
Brian Salomon0e05a822017-07-25 09:43:22 -0400255}
Brian Salomon587e08f2017-01-27 10:59:27 -0500256
Robert Phillips7c525e62018-06-12 10:11:12 -0400257void test_draw_op(GrContext* context,
258 GrRenderTargetContext* rtc,
259 std::unique_ptr<GrFragmentProcessor> fp,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500260 sk_sp<GrTextureProxy> inputDataProxy) {
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500261 GrPaint paint;
Brian Osman2240be92017-10-18 13:15:13 -0400262 paint.addColorTextureProcessor(std::move(inputDataProxy), SkMatrix::I());
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500263 paint.addColorFragmentProcessor(std::move(fp));
264 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonac70f842017-05-08 10:43:33 -0400265
Michael Ludwig72ab3462018-12-10 12:43:36 -0500266 auto op = GrFillRectOp::Make(context, std::move(paint), GrAAType::kNone, SkMatrix::I(),
267 SkRect::MakeWH(rtc->width(), rtc->height()));
Brian Salomonac70f842017-05-08 10:43:33 -0400268 rtc->addDrawOp(GrNoClip(), std::move(op));
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500269}
270
Michael Ludwig7034f152018-10-08 16:43:58 -0400271// This assumes that the output buffer will be the same size as inputDataProxy
272void render_fp(GrContext* context, GrRenderTargetContext* rtc, GrFragmentProcessor* fp,
273 sk_sp<GrTextureProxy> inputDataProxy, GrColor* buffer) {
274 int width = inputDataProxy->width();
275 int height = inputDataProxy->height();
276
277 // test_draw_op needs to take ownership of an FP, so give it a clone that it can own
278 test_draw_op(context, rtc, fp->clone(), inputDataProxy);
279 memset(buffer, 0x0, sizeof(GrColor) * width * height);
280 rtc->readPixels(SkImageInfo::Make(width, height, kRGBA_8888_SkColorType,
281 kPremul_SkAlphaType),
282 buffer, 0, 0, 0);
283}
284
Brian Salomon0e05a822017-07-25 09:43:22 -0400285/** Initializes the two test texture proxies that are available to the FP test factories. */
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500286bool init_test_textures(GrProxyProvider* proxyProvider, SkRandom* random,
287 sk_sp<GrTextureProxy> proxies[2]) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400288 static const int kTestTextureSize = 256;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400289
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500290 {
291 // Put premul data into the RGBA texture that the test FPs can optionally use.
292 std::unique_ptr<GrColor[]> rgbaData(new GrColor[kTestTextureSize * kTestTextureSize]);
293 for (int y = 0; y < kTestTextureSize; ++y) {
294 for (int x = 0; x < kTestTextureSize; ++x) {
Michael Ludwig7034f152018-10-08 16:43:58 -0400295 rgbaData[kTestTextureSize * y + x] = input_texel_color(
296 random->nextULessThan(256), random->nextULessThan(256), 0.0f);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500297 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400298 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400299
Brian Osman2700abc2018-09-12 10:19:41 -0400300 SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize,
301 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
302 SkPixmap pixmap(ii, rgbaData.get(), ii.minRowBytes());
303 sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap);
304 proxies[0] = proxyProvider->createTextureProxy(img, kNone_GrSurfaceFlags, 1,
305 SkBudgeted::kYes, SkBackingFit::kExact);
Brian Salomon0e05a822017-07-25 09:43:22 -0400306 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500307
308 {
309 // Put random values into the alpha texture that the test FPs can optionally use.
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500310 std::unique_ptr<uint8_t[]> alphaData(new uint8_t[kTestTextureSize * kTestTextureSize]);
311 for (int y = 0; y < kTestTextureSize; ++y) {
312 for (int x = 0; x < kTestTextureSize; ++x) {
313 alphaData[kTestTextureSize * y + x] = random->nextULessThan(256);
314 }
315 }
316
Brian Osman2700abc2018-09-12 10:19:41 -0400317 SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize,
318 kAlpha_8_SkColorType, kPremul_SkAlphaType);
319 SkPixmap pixmap(ii, alphaData.get(), ii.minRowBytes());
320 sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap);
321 proxies[1] = proxyProvider->createTextureProxy(img, kNone_GrSurfaceFlags, 1,
322 SkBudgeted::kYes, SkBackingFit::kExact);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500323 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400324
325 return proxies[0] && proxies[1];
326}
327
328// Creates a texture of premul colors used as the output of the fragment processor that precedes
329// the fragment processor under test. Color values are those provided by input_texel_color().
Michael Ludwig7034f152018-10-08 16:43:58 -0400330sk_sp<GrTextureProxy> make_input_texture(GrProxyProvider* proxyProvider, int width, int height,
331 SkScalar delta) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400332 std::unique_ptr<GrColor[]> data(new GrColor[width * height]);
333 for (int y = 0; y < width; ++y) {
334 for (int x = 0; x < height; ++x) {
Michael Ludwig7034f152018-10-08 16:43:58 -0400335 data.get()[width * y + x] = input_texel_color(x, y, delta);
Brian Salomon0e05a822017-07-25 09:43:22 -0400336 }
337 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500338
Brian Osman2700abc2018-09-12 10:19:41 -0400339 SkImageInfo ii = SkImageInfo::Make(width, height, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
340 SkPixmap pixmap(ii, data.get(), ii.minRowBytes());
341 sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap);
342 return proxyProvider->createTextureProxy(img, kNone_GrSurfaceFlags, 1,
343 SkBudgeted::kYes, SkBackingFit::kExact);
Brian Salomon0e05a822017-07-25 09:43:22 -0400344}
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500345
Michael Ludwige8e10752018-10-01 12:42:53 -0400346bool log_surface_context(sk_sp<GrSurfaceContext> src, SkString* dst) {
347 SkImageInfo ii = SkImageInfo::Make(src->width(), src->height(), kRGBA_8888_SkColorType,
348 kPremul_SkAlphaType);
349 SkBitmap bm;
350 SkAssertResult(bm.tryAllocPixels(ii));
351 SkAssertResult(src->readPixels(ii, bm.getPixels(), bm.rowBytes(), 0, 0));
352
353 return bitmap_to_base64_data_uri(bm, dst);
354}
355
356bool log_surface_proxy(GrContext* context, sk_sp<GrSurfaceProxy> src, SkString* dst) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500357 sk_sp<GrSurfaceContext> sContext(context->priv().makeWrappedSurfaceContext(src));
Michael Ludwige8e10752018-10-01 12:42:53 -0400358 return log_surface_context(sContext, dst);
359}
360
Michael Ludwig7034f152018-10-08 16:43:58 -0400361bool fuzzy_color_equals(const SkPMColor4f& c1, const SkPMColor4f& c2) {
362 // With the loss of precision of rendering into 32-bit color, then estimating the FP's output
363 // from that, it is not uncommon for a valid output to differ from estimate by up to 0.01
364 // (really 1/128 ~ .0078, but frequently floating point issues make that tolerance a little
365 // too unforgiving).
366 static constexpr SkScalar kTolerance = 0.01f;
367 for (int i = 0; i < 4; i++) {
368 if (!SkScalarNearlyEqual(c1[i], c2[i], kTolerance)) {
369 return false;
370 }
371 }
372 return true;
373}
374
375int modulation_index(int channelIndex, bool alphaModulation) {
376 return alphaModulation ? 3 : channelIndex;
377}
378
379// Given three input colors (color preceding the FP being tested), and the output of the FP, this
380// ensures that the out1 = fp * in1.a, out2 = fp * in2.a, and out3 = fp * in3.a, where fp is the
381// pre-modulated color that should not be changing across frames (FP's state doesn't change).
382//
383// When alphaModulation is false, this tests the very similar conditions that out1 = fp * in1,
384// etc. using per-channel modulation instead of modulation by just the input alpha channel.
385// - This estimates the pre-modulated fp color from one of the input/output pairs and confirms the
386// conditions hold for the other two pairs.
387bool legal_modulation(const GrColor& in1, const GrColor& in2, const GrColor& in3,
388 const GrColor& out1, const GrColor& out2, const GrColor& out3,
389 bool alphaModulation) {
390 // Convert to floating point, which is the number space the FP operates in (more or less)
Brian Osmancb3d0872018-10-16 15:19:28 -0400391 SkPMColor4f in1f = SkPMColor4f::FromBytes_RGBA(in1);
392 SkPMColor4f in2f = SkPMColor4f::FromBytes_RGBA(in2);
393 SkPMColor4f in3f = SkPMColor4f::FromBytes_RGBA(in3);
394 SkPMColor4f out1f = SkPMColor4f::FromBytes_RGBA(out1);
395 SkPMColor4f out2f = SkPMColor4f::FromBytes_RGBA(out2);
396 SkPMColor4f out3f = SkPMColor4f::FromBytes_RGBA(out3);
Michael Ludwig7034f152018-10-08 16:43:58 -0400397
398 // Reconstruct the output of the FP before the shader modulated its color with the input value.
399 // When the original input is very small, it may cause the final output color to round
400 // to 0, in which case we estimate the pre-modulated color using one of the stepped frames that
401 // will then have a guaranteed larger channel value (since the offset will be added to it).
402 SkPMColor4f fpPreModulation;
403 for (int i = 0; i < 4; i++) {
404 int modulationIndex = modulation_index(i, alphaModulation);
405 if (in1f[modulationIndex] < 0.2f) {
406 // Use the stepped frame
407 fpPreModulation[i] = out2f[i] / in2f[modulationIndex];
408 } else {
409 fpPreModulation[i] = out1f[i] / in1f[modulationIndex];
410 }
411 }
412
413 // With reconstructed pre-modulated FP output, derive the expected value of fp * input for each
414 // of the transformed input colors.
Brian Osmancb3d0872018-10-16 15:19:28 -0400415 SkPMColor4f expected1 = alphaModulation ? (fpPreModulation * in1f.fA)
416 : (fpPreModulation * in1f);
417 SkPMColor4f expected2 = alphaModulation ? (fpPreModulation * in2f.fA)
418 : (fpPreModulation * in2f);
419 SkPMColor4f expected3 = alphaModulation ? (fpPreModulation * in3f.fA)
420 : (fpPreModulation * in3f);
Michael Ludwig7034f152018-10-08 16:43:58 -0400421
Brian Osmancb3d0872018-10-16 15:19:28 -0400422 return fuzzy_color_equals(out1f, expected1) &&
423 fuzzy_color_equals(out2f, expected2) &&
424 fuzzy_color_equals(out3f, expected3);
Michael Ludwig7034f152018-10-08 16:43:58 -0400425}
426
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500427DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500428 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500429 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
430 auto resourceProvider = context->priv().resourceProvider();
Brian Salomon1c053642017-07-24 10:16:19 -0400431 using FPFactory = GrFragmentProcessorTestFactory;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400432
Michael Ludwig3ad86d02018-09-28 16:00:38 -0400433 uint32_t seed = FLAGS_processorSeed;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400434 if (FLAGS_randomProcessorTest) {
435 std::random_device rd;
436 seed = rd();
437 }
438 // 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 -0400439 // use --processorSeed <seed> (without --randomProcessorTest) to reproduce.
Brian Osmanc35a2d42017-03-17 10:58:53 -0400440 SkRandom random(seed);
441
Greg Daniel4065d452018-11-16 15:43:41 -0500442 const GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500443 context->priv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Greg Daniel4065d452018-11-16 15:43:41 -0500444
Brian Salomon0e05a822017-07-25 09:43:22 -0400445 // Make the destination context for the test.
446 static constexpr int kRenderSize = 256;
Robert Phillips9da87e02019-02-04 13:26:26 -0500447 sk_sp<GrRenderTargetContext> rtc = context->priv().makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -0500448 format, SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig,
449 nullptr);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500450
Robert Phillipse78b7252017-04-06 07:59:41 -0400451 sk_sp<GrTextureProxy> proxies[2];
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500452 if (!init_test_textures(proxyProvider, &random, proxies)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400453 ERRORF(reporter, "Could not create test textures");
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400454 return;
455 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400456 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
Brian Salomon587e08f2017-01-27 10:59:27 -0500457
Michael Ludwig7034f152018-10-08 16:43:58 -0400458 // Coverage optimization uses three frames with a linearly transformed input texture. The first
459 // frame has no offset, second frames add .2 and .4, which should then be present as a fixed
460 // difference between the frame outputs if the FP is properly following the modulation
461 // requirements of the coverage optimization.
462 static constexpr SkScalar kInputDelta = 0.2f;
463 auto inputTexture1 = make_input_texture(proxyProvider, kRenderSize, kRenderSize, 0.0f);
464 auto inputTexture2 = make_input_texture(proxyProvider, kRenderSize, kRenderSize, kInputDelta);
465 auto inputTexture3 = make_input_texture(proxyProvider, kRenderSize, kRenderSize, 2*kInputDelta);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500466
Michael Ludwige8e10752018-10-01 12:42:53 -0400467 // Encoded images are very verbose and this tests many potential images, so only export the
468 // first failure (subsequent failures have a reasonable chance of being related).
469 bool loggedFirstFailure = false;
Michael Ludwig314d3772018-10-03 16:04:38 -0400470 bool loggedFirstWarning = false;
Michael Ludwig7034f152018-10-08 16:43:58 -0400471
472 // Storage for the three frames required for coverage compatibility optimization. Each frame
473 // uses the correspondingly numbered inputTextureX.
474 std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
475 std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
476 std::unique_ptr<GrColor[]> readData3(new GrColor[kRenderSize * kRenderSize]);
477
Brian Salomon0e05a822017-07-25 09:43:22 -0400478 // Because processor factories configure themselves in random ways, this is not exhaustive.
Brian Salomon587e08f2017-01-27 10:59:27 -0500479 for (int i = 0; i < FPFactory::Count(); ++i) {
480 int timesToInvokeFactory = 5;
481 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
482 // on child optimizations being present.
Brian Salomonaff329b2017-08-11 09:40:37 -0400483 std::unique_ptr<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
Brian Salomon587e08f2017-01-27 10:59:27 -0500484 for (int j = 0; j < fp->numChildProcessors(); ++j) {
485 // This value made a reasonable trade off between time and coverage when this test was
486 // written.
487 timesToInvokeFactory *= FPFactory::Count() / 2;
488 }
Brian Osman50ea3c02019-02-04 10:01:53 -0500489#if defined(__MSVC_RUNTIME_CHECKS)
490 // This test is infuriatingly slow with MSVC runtime checks enabled
491 timesToInvokeFactory = 1;
492#endif
Brian Salomon587e08f2017-01-27 10:59:27 -0500493 for (int j = 0; j < timesToInvokeFactory; ++j) {
494 fp = FPFactory::MakeIdx(i, &testData);
Robert Phillips6be756b2018-01-16 15:07:54 -0500495 if (!fp->instantiate(resourceProvider)) {
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400496 continue;
497 }
498
Brian Salomon587e08f2017-01-27 10:59:27 -0500499 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
Brian Salomonf3b995b2017-02-15 10:22:23 -0500500 !fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500501 continue;
502 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400503
Michael Ludwig7034f152018-10-08 16:43:58 -0400504 if (fp->compatibleWithCoverageAsAlpha()) {
505 // 2nd and 3rd frames are only used when checking coverage optimization
506 render_fp(context, rtc.get(), fp.get(), inputTexture2, readData2.get());
507 render_fp(context, rtc.get(), fp.get(), inputTexture3, readData3.get());
508 }
509 // Draw base frame last so that rtc holds the original FP behavior if we need to
510 // dump the image to the log.
511 render_fp(context, rtc.get(), fp.get(), inputTexture1, readData1.get());
Brian Salomonaff329b2017-08-11 09:40:37 -0400512
Brian Salomon587e08f2017-01-27 10:59:27 -0500513 if (0) { // Useful to see what FPs are being tested.
514 SkString children;
Michael Ludwig7034f152018-10-08 16:43:58 -0400515 for (int c = 0; c < fp->numChildProcessors(); ++c) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500516 if (!c) {
517 children.append("(");
518 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400519 children.append(fp->childProcessor(c).name());
520 children.append(c == fp->numChildProcessors() - 1 ? ")" : ", ");
Brian Salomon587e08f2017-01-27 10:59:27 -0500521 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400522 SkDebugf("%s %s\n", fp->name(), children.c_str());
Brian Salomon587e08f2017-01-27 10:59:27 -0500523 }
Michael Ludwig314d3772018-10-03 16:04:38 -0400524
525 // This test has a history of being flaky on a number of devices. If an FP is logically
526 // violating the optimizations, it's reasonable to expect it to violate requirements on
527 // a large number of pixels in the image. Sporadic pixel violations are more indicative
528 // of device errors and represents a separate problem.
Michael Ludwig72ab3462018-12-10 12:43:36 -0500529#if defined(SK_BUILD_FOR_SKQP)
Michael Ludwig314d3772018-10-03 16:04:38 -0400530 static constexpr int kMaxAcceptableFailedPixels = 0; // Strict when running as SKQP
531#else
532 static constexpr int kMaxAcceptableFailedPixels = 2 * kRenderSize; // ~0.7% of the image
533#endif
534
535 int failedPixelCount = 0;
536 // Collect first optimization failure message, to be output later as a warning or an
537 // error depending on whether the rendering "passed" or failed.
538 SkString coverageMessage;
539 SkString opaqueMessage;
540 SkString constMessage;
541 for (int y = 0; y < kRenderSize; ++y) {
542 for (int x = 0; x < kRenderSize; ++x) {
543 bool passing = true;
Michael Ludwig7034f152018-10-08 16:43:58 -0400544 GrColor input = input_texel_color(x, y, 0.0f);
545 GrColor output = readData1.get()[y * kRenderSize + x];
546
547 if (fp->compatibleWithCoverageAsAlpha()) {
548 GrColor i2 = input_texel_color(x, y, kInputDelta);
549 GrColor i3 = input_texel_color(x, y, 2 * kInputDelta);
550
551 GrColor o2 = readData2.get()[y * kRenderSize + x];
552 GrColor o3 = readData3.get()[y * kRenderSize + x];
553
554 // A compatible processor is allowed to modulate either the input color or
Brian Salomon587e08f2017-01-27 10:59:27 -0500555 // just the input alpha.
Michael Ludwig7034f152018-10-08 16:43:58 -0400556 bool legalAlphaModulation = legal_modulation(input, i2, i3, output, o2, o3,
557 /* alpha */ true);
558 bool legalColorModulation = legal_modulation(input, i2, i3, output, o2, o3,
559 /* alpha */ false);
560
Brian Salomon587e08f2017-01-27 10:59:27 -0500561 if (!legalColorModulation && !legalAlphaModulation) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500562 passing = false;
Michael Ludwig314d3772018-10-03 16:04:38 -0400563
564 if (coverageMessage.isEmpty()) {
Michael Ludwig7034f152018-10-08 16:43:58 -0400565 coverageMessage.printf("\"Modulating\" processor %s did not match "
566 "alpha-modulation nor color-modulation rules. "
567 "Input: 0x%08x, Output: 0x%08x, pixel (%d, %d).",
568 fp->name(), input, output, x, y);
Michael Ludwig314d3772018-10-03 16:04:38 -0400569 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500570 }
571 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400572
Brian Osmancb3d0872018-10-16 15:19:28 -0400573 SkPMColor4f input4f = SkPMColor4f::FromBytes_RGBA(input);
574 SkPMColor4f output4f = SkPMColor4f::FromBytes_RGBA(output);
Brian Osman1d5b5982018-10-01 13:41:39 -0400575 SkPMColor4f expected4f;
Michael Ludwig7034f152018-10-08 16:43:58 -0400576 if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) {
Brian Osmancb3d0872018-10-16 15:19:28 -0400577 float rDiff = fabsf(output4f.fR - expected4f.fR);
578 float gDiff = fabsf(output4f.fG - expected4f.fG);
579 float bDiff = fabsf(output4f.fB - expected4f.fB);
580 float aDiff = fabsf(output4f.fA - expected4f.fA);
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500581 static constexpr float kTol = 4 / 255.f;
Brian Salomon587e08f2017-01-27 10:59:27 -0500582 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
Michael Ludwig314d3772018-10-03 16:04:38 -0400583 if (constMessage.isEmpty()) {
584 passing = false;
585
586 constMessage.printf("Processor %s claimed output for const input "
587 "doesn't match actual output. Error: %f, Tolerance: %f, "
588 "input: (%f, %f, %f, %f), actual: (%f, %f, %f, %f), "
Michael Ludwig7034f152018-10-08 16:43:58 -0400589 "expected(%f, %f, %f, %f)", fp->name(),
Michael Ludwig314d3772018-10-03 16:04:38 -0400590 SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))), kTol,
591 input4f.fR, input4f.fG, input4f.fB, input4f.fA,
Brian Osmancb3d0872018-10-16 15:19:28 -0400592 output4f.fR, output4f.fG, output4f.fB, output4f.fA,
593 expected4f.fR, expected4f.fG, expected4f.fB, expected4f.fA);
Michael Ludwig314d3772018-10-03 16:04:38 -0400594 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500595 }
596 }
Brian Osman00b29392018-11-05 15:42:43 -0500597 if (input4f.isOpaque() && fp->preservesOpaqueInput() && !output4f.isOpaque()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500598 passing = false;
Michael Ludwig314d3772018-10-03 16:04:38 -0400599
600 if (opaqueMessage.isEmpty()) {
601 opaqueMessage.printf("Processor %s claimed opaqueness is preserved but "
602 "it is not. Input: 0x%08x, Output: 0x%08x.",
Michael Ludwig7034f152018-10-08 16:43:58 -0400603 fp->name(), input, output);
Michael Ludwige8e10752018-10-01 12:42:53 -0400604 }
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400605 }
Michael Ludwig314d3772018-10-03 16:04:38 -0400606
607 if (!passing) {
608 // Regardless of how many optimizations the pixel violates, count it as a
609 // single bad pixel.
610 failedPixelCount++;
611 }
612 }
613 }
614
615 // Finished analyzing the entire image, see if the number of pixel failures meets the
616 // threshold for an FP violating the optimization requirements.
617 if (failedPixelCount > kMaxAcceptableFailedPixels) {
Michael Ludwig4504a6522018-10-03 16:47:55 -0400618 ERRORF(reporter, "Processor violated %d of %d pixels, seed: 0x%08x, processor: %s"
Michael Ludwig314d3772018-10-03 16:04:38 -0400619 ", first failing pixel details are below:",
620 failedPixelCount, kRenderSize * kRenderSize, seed,
Michael Ludwig7034f152018-10-08 16:43:58 -0400621 fp->dumpInfo().c_str());
Michael Ludwig314d3772018-10-03 16:04:38 -0400622
623 // Print first failing pixel's details.
624 if (!coverageMessage.isEmpty()) {
625 ERRORF(reporter, coverageMessage.c_str());
626 }
627 if (!constMessage.isEmpty()) {
628 ERRORF(reporter, constMessage.c_str());
629 }
630 if (!opaqueMessage.isEmpty()) {
631 ERRORF(reporter, opaqueMessage.c_str());
632 }
633
634 if (!loggedFirstFailure) {
635 // Print with ERRORF to make sure the encoded image is output
636 SkString input;
Michael Ludwig7034f152018-10-08 16:43:58 -0400637 log_surface_proxy(context, inputTexture1, &input);
Michael Ludwig314d3772018-10-03 16:04:38 -0400638 SkString output;
639 log_surface_context(rtc, &output);
640 ERRORF(reporter, "Input image: %s\n\n"
641 "===========================================================\n\n"
642 "Output image: %s\n", input.c_str(), output.c_str());
643 loggedFirstFailure = true;
644 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400645 } else if(failedPixelCount > 0) {
Michael Ludwig314d3772018-10-03 16:04:38 -0400646 // Don't trigger an error, but don't just hide the failures either.
647 INFOF(reporter, "Processor violated %d of %d pixels (below error threshold), seed: "
648 "0x%08x, processor: %s", failedPixelCount, kRenderSize * kRenderSize,
Michael Ludwig7034f152018-10-08 16:43:58 -0400649 seed, fp->dumpInfo().c_str());
Michael Ludwig314d3772018-10-03 16:04:38 -0400650 if (!coverageMessage.isEmpty()) {
651 INFOF(reporter, coverageMessage.c_str());
652 }
653 if (!constMessage.isEmpty()) {
654 INFOF(reporter, constMessage.c_str());
655 }
656 if (!opaqueMessage.isEmpty()) {
657 INFOF(reporter, opaqueMessage.c_str());
658 }
659 if (!loggedFirstWarning) {
660 SkString input;
Michael Ludwig7034f152018-10-08 16:43:58 -0400661 log_surface_proxy(context, inputTexture1, &input);
Michael Ludwig314d3772018-10-03 16:04:38 -0400662 SkString output;
663 log_surface_context(rtc, &output);
664 INFOF(reporter, "Input image: %s\n\n"
665 "===========================================================\n\n"
666 "Output image: %s\n", input.c_str(), output.c_str());
667 loggedFirstWarning = true;
Brian Salomon587e08f2017-01-27 10:59:27 -0500668 }
669 }
670 }
671 }
672}
Robert Phillips18166ee2017-06-01 12:55:44 -0400673
Brian Salomon0e05a822017-07-25 09:43:22 -0400674// Tests that fragment processors returned by GrFragmentProcessor::clone() are equivalent to their
675// progenitors.
676DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) {
677 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500678 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
679 auto resourceProvider = context->priv().resourceProvider();
Brian Salomon0e05a822017-07-25 09:43:22 -0400680
681 SkRandom random;
682
Greg Daniel4065d452018-11-16 15:43:41 -0500683 const GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500684 context->priv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Greg Daniel4065d452018-11-16 15:43:41 -0500685
Brian Salomon0e05a822017-07-25 09:43:22 -0400686 // Make the destination context for the test.
687 static constexpr int kRenderSize = 1024;
Robert Phillips9da87e02019-02-04 13:26:26 -0500688 sk_sp<GrRenderTargetContext> rtc = context->priv().makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -0500689 format, SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig,
690 nullptr);
Brian Salomon0e05a822017-07-25 09:43:22 -0400691
692 sk_sp<GrTextureProxy> proxies[2];
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500693 if (!init_test_textures(proxyProvider, &random, proxies)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400694 ERRORF(reporter, "Could not create test textures");
695 return;
696 }
697 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
698
Michael Ludwig7034f152018-10-08 16:43:58 -0400699 auto inputTexture = make_input_texture(proxyProvider, kRenderSize, kRenderSize, 0.0f);
Brian Salomon0e05a822017-07-25 09:43:22 -0400700 std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
701 std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
702 auto readInfo = SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
703 kPremul_SkAlphaType);
704
705 // Because processor factories configure themselves in random ways, this is not exhaustive.
706 for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) {
707 static constexpr int kTimesToInvokeFactory = 10;
708 for (int j = 0; j < kTimesToInvokeFactory; ++j) {
709 auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &testData);
710 auto clone = fp->clone();
Brian Salomon0e05a822017-07-25 09:43:22 -0400711 if (!clone) {
Brian Salomon96271cd2017-07-31 16:27:23 -0400712 ERRORF(reporter, "Clone of processor %s failed.", fp->name());
Brian Salomon0e05a822017-07-25 09:43:22 -0400713 continue;
714 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400715 const char* name = fp->name();
Robert Phillips6be756b2018-01-16 15:07:54 -0500716 if (!fp->instantiate(resourceProvider) || !clone->instantiate(resourceProvider)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400717 continue;
718 }
Brian Salomonce06e262017-08-01 16:23:40 -0400719 REPORTER_ASSERT(reporter, !strcmp(fp->name(), clone->name()));
720 REPORTER_ASSERT(reporter, fp->compatibleWithCoverageAsAlpha() ==
721 clone->compatibleWithCoverageAsAlpha());
722 REPORTER_ASSERT(reporter, fp->isEqual(*clone));
723 REPORTER_ASSERT(reporter, fp->preservesOpaqueInput() == clone->preservesOpaqueInput());
724 REPORTER_ASSERT(reporter, fp->hasConstantOutputForConstantInput() ==
725 clone->hasConstantOutputForConstantInput());
726 REPORTER_ASSERT(reporter, fp->numChildProcessors() == clone->numChildProcessors());
727 REPORTER_ASSERT(reporter, fp->usesLocalCoords() == clone->usesLocalCoords());
Brian Salomon0e05a822017-07-25 09:43:22 -0400728 // Draw with original and read back the results.
Michael Ludwig7034f152018-10-08 16:43:58 -0400729 render_fp(context, rtc.get(), fp.get(), inputTexture, readData1.get());
Brian Salomon0e05a822017-07-25 09:43:22 -0400730
731 // Draw with clone and read back the results.
Michael Ludwig7034f152018-10-08 16:43:58 -0400732 render_fp(context, rtc.get(), clone.get(), inputTexture, readData2.get());
Brian Salomon0e05a822017-07-25 09:43:22 -0400733
734 // Check that the results are the same.
735 bool passing = true;
736 for (int y = 0; y < kRenderSize && passing; ++y) {
737 for (int x = 0; x < kRenderSize && passing; ++x) {
738 int idx = y * kRenderSize + x;
739 if (readData1[idx] != readData2[idx]) {
740 ERRORF(reporter,
741 "Processor %s made clone produced different output. "
742 "Input color: 0x%08x, Original Output Color: 0x%08x, "
743 "Clone Output Color: 0x%08x..",
Michael Ludwig7034f152018-10-08 16:43:58 -0400744 name, input_texel_color(x, y, 0.0f), readData1[idx], readData2[idx]);
Brian Salomon0e05a822017-07-25 09:43:22 -0400745 passing = false;
746 }
747 }
748 }
749 }
750 }
751}
752
Hal Canary6f6961e2017-01-31 13:50:44 -0500753#endif // GR_TEST_UTILS
754#endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS