blob: 0ff350ae8b4253de115150ec3b12a2235bd899a7 [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
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 Phillipsc994a932018-06-19 13:09:54 -040035 GrOpMemoryPool* pool = context->contextPriv().opMemoryPool();
36
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
Brian Osman532b3f92018-07-11 10:02:07 -040048 RequiresDstTexture 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;
Brian Salomon82ddc942017-07-14 12:00:13 -040051 fProcessors.finalize(kUnknownColor, GrProcessorAnalysisCoverage::kNone, clip, false, caps,
Brian Osman532b3f92018-07-11 10:02:07 -040052 &overrideColor);
Brian Salomon82ddc942017-07-14 12:00:13 -040053 return RequiresDstTexture::kNo;
Brian Salomon649a3412017-03-09 13:50:43 -050054 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050055
56private:
Robert Phillips7c525e62018-06-12 10:11:12 -040057 friend class ::GrOpMemoryPool; // for ctor
58
Brian Salomonaff329b2017-08-11 09:40:37 -040059 TestOp(std::unique_ptr<GrFragmentProcessor> fp)
60 : INHERITED(ClassID()), fProcessors(std::move(fp)) {
Brian Salomon82ddc942017-07-14 12:00:13 -040061 this->setBounds(SkRect::MakeWH(100, 100), HasAABloat::kNo, IsZeroArea::kNo);
62 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050063
Brian Salomon91326c32017-08-09 16:02:19 -040064 void onPrepareDraws(Target* target) 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 Salomon559f5562017-11-15 14:28:33 -050081 const SkTArray<sk_sp<GrBuffer>>& buffers) {
82 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 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
Greg Daniel4065d452018-11-16 15:43:41 -0500165 const GrBackendFormat format =
166 context->contextPriv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
167
Brian Salomonb17e6392017-07-28 13:41:51 -0400168 for (bool makeClone : {false, true}) {
169 for (int parentCnt = 0; parentCnt < 2; parentCnt++) {
170 sk_sp<GrRenderTargetContext> renderTargetContext(
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500171 context->contextPriv().makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -0500172 format, SkBackingFit::kApprox, 1, 1,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500173 kRGBA_8888_GrPixelConfig, nullptr));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500174 {
Brian Salomon2a4f9832018-03-03 22:43:43 -0500175 sk_sp<GrTextureProxy> proxy1 = proxyProvider->createProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500176 format, desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact,
177 SkBudgeted::kYes);
Brian Salomon2a4f9832018-03-03 22:43:43 -0500178 sk_sp<GrTextureProxy> proxy2 = proxyProvider->createProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500179 format, desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact,
180 SkBudgeted::kYes);
Brian Salomon2a4f9832018-03-03 22:43:43 -0500181 sk_sp<GrTextureProxy> proxy3 = proxyProvider->createProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500182 format, desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact,
183 SkBudgeted::kYes);
Brian Salomon2a4f9832018-03-03 22:43:43 -0500184 sk_sp<GrTextureProxy> proxy4 = proxyProvider->createProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500185 format, desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact,
186 SkBudgeted::kYes);
Brian Salomonb17e6392017-07-28 13:41:51 -0400187 {
188 SkTArray<sk_sp<GrTextureProxy>> proxies;
189 SkTArray<sk_sp<GrBuffer>> buffers;
Brian Salomonb17e6392017-07-28 13:41:51 -0400190 proxies.push_back(proxy1);
Brian Salomon559f5562017-11-15 14:28:33 -0500191 auto fp = TestFP::Make(std::move(proxies), std::move(buffers));
Brian Salomonb17e6392017-07-28 13:41:51 -0400192 for (int i = 0; i < parentCnt; ++i) {
193 fp = TestFP::Make(std::move(fp));
194 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400195 std::unique_ptr<GrFragmentProcessor> clone;
Brian Salomonb17e6392017-07-28 13:41:51 -0400196 if (makeClone) {
197 clone = fp->clone();
198 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400199 std::unique_ptr<GrDrawOp> op(TestOp::Make(context, std::move(fp)));
Brian Salomonb17e6392017-07-28 13:41:51 -0400200 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
201 if (clone) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400202 op = TestOp::Make(context, std::move(clone));
Brian Salomonb17e6392017-07-28 13:41:51 -0400203 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
204 }
205 }
206 int refCnt, readCnt, writeCnt;
207
208 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
209 // IO counts should be double if there is a clone of the FP.
210 int ioRefMul = makeClone ? 2 : 1;
Robert Phillips715d08c2018-07-18 13:56:48 -0400211 REPORTER_ASSERT(reporter, -1 == refCnt);
Brian Salomonb17e6392017-07-28 13:41:51 -0400212 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
213 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
214
Brian Salomonb17e6392017-07-28 13:41:51 -0400215 context->flush();
216
217 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
218 REPORTER_ASSERT(reporter, 1 == refCnt);
219 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
220 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
221
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500222 }
223 }
224 }
225}
Brian Salomon587e08f2017-01-27 10:59:27 -0500226
227// This test uses the random GrFragmentProcessor test factory, which relies on static initializers.
228#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
229
Brian Salomon0e05a822017-07-25 09:43:22 -0400230#include "SkCommandLineFlags.h"
231DEFINE_bool(randomProcessorTest, false, "Use non-deterministic seed for random processor tests?");
Michael Ludwig3ad86d02018-09-28 16:00:38 -0400232DEFINE_uint32(processorSeed, 0, "Use specific seed for processor tests. Overridden by " \
233 "--randomProcessorTest.");
Brian Salomon0e05a822017-07-25 09:43:22 -0400234
235#if GR_TEST_UTILS
236
Michael Ludwig7034f152018-10-08 16:43:58 -0400237static GrColor input_texel_color(int i, int j, SkScalar delta) {
238 // Delta must be less than 0.5 to prevent over/underflow issues with the input color
239 SkASSERT(delta <= 0.5);
Brian Salomon587e08f2017-01-27 10:59:27 -0500240
Brian Osmancb3d0872018-10-16 15:19:28 -0400241 SkColor color = SkColorSetARGB((uint8_t)i, (uint8_t)j, (uint8_t)(i + j), (uint8_t)(2 * j - i));
242 SkColor4f color4f = SkColor4f::FromColor(color);
Michael Ludwig7034f152018-10-08 16:43:58 -0400243 for (int i = 0; i < 4; i++) {
Brian Osmancb3d0872018-10-16 15:19:28 -0400244 if (color4f[i] > 0.5) {
245 color4f[i] -= delta;
Michael Ludwig7034f152018-10-08 16:43:58 -0400246 } else {
Brian Osmancb3d0872018-10-16 15:19:28 -0400247 color4f[i] += delta;
Michael Ludwig7034f152018-10-08 16:43:58 -0400248 }
249 }
Brian Osmancb3d0872018-10-16 15:19:28 -0400250 return color4f.premul().toBytes_RGBA();
Brian Salomon0e05a822017-07-25 09:43:22 -0400251}
Brian Salomon587e08f2017-01-27 10:59:27 -0500252
Robert Phillips7c525e62018-06-12 10:11:12 -0400253void test_draw_op(GrContext* context,
254 GrRenderTargetContext* rtc,
255 std::unique_ptr<GrFragmentProcessor> fp,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500256 sk_sp<GrTextureProxy> inputDataProxy) {
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500257 GrPaint paint;
Brian Osman2240be92017-10-18 13:15:13 -0400258 paint.addColorTextureProcessor(std::move(inputDataProxy), SkMatrix::I());
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500259 paint.addColorFragmentProcessor(std::move(fp));
260 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonac70f842017-05-08 10:43:33 -0400261
Robert Phillips7c525e62018-06-12 10:11:12 -0400262 auto op = GrRectOpFactory::MakeNonAAFill(context, std::move(paint), SkMatrix::I(),
Brian Salomonbaaf4392017-06-15 09:59:23 -0400263 SkRect::MakeWH(rtc->width(), rtc->height()),
264 GrAAType::kNone);
Brian Salomonac70f842017-05-08 10:43:33 -0400265 rtc->addDrawOp(GrNoClip(), std::move(op));
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500266}
267
Michael Ludwig7034f152018-10-08 16:43:58 -0400268// This assumes that the output buffer will be the same size as inputDataProxy
269void render_fp(GrContext* context, GrRenderTargetContext* rtc, GrFragmentProcessor* fp,
270 sk_sp<GrTextureProxy> inputDataProxy, GrColor* buffer) {
271 int width = inputDataProxy->width();
272 int height = inputDataProxy->height();
273
274 // test_draw_op needs to take ownership of an FP, so give it a clone that it can own
275 test_draw_op(context, rtc, fp->clone(), inputDataProxy);
276 memset(buffer, 0x0, sizeof(GrColor) * width * height);
277 rtc->readPixels(SkImageInfo::Make(width, height, kRGBA_8888_SkColorType,
278 kPremul_SkAlphaType),
279 buffer, 0, 0, 0);
280}
281
Brian Salomon0e05a822017-07-25 09:43:22 -0400282/** Initializes the two test texture proxies that are available to the FP test factories. */
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500283bool init_test_textures(GrProxyProvider* proxyProvider, SkRandom* random,
284 sk_sp<GrTextureProxy> proxies[2]) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400285 static const int kTestTextureSize = 256;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400286
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500287 {
288 // Put premul data into the RGBA texture that the test FPs can optionally use.
289 std::unique_ptr<GrColor[]> rgbaData(new GrColor[kTestTextureSize * kTestTextureSize]);
290 for (int y = 0; y < kTestTextureSize; ++y) {
291 for (int x = 0; x < kTestTextureSize; ++x) {
Michael Ludwig7034f152018-10-08 16:43:58 -0400292 rgbaData[kTestTextureSize * y + x] = input_texel_color(
293 random->nextULessThan(256), random->nextULessThan(256), 0.0f);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500294 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400295 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400296
Brian Osman2700abc2018-09-12 10:19:41 -0400297 SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize,
298 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
299 SkPixmap pixmap(ii, rgbaData.get(), ii.minRowBytes());
300 sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap);
301 proxies[0] = proxyProvider->createTextureProxy(img, kNone_GrSurfaceFlags, 1,
302 SkBudgeted::kYes, SkBackingFit::kExact);
Brian Salomon0e05a822017-07-25 09:43:22 -0400303 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500304
305 {
306 // Put random values into the alpha texture that the test FPs can optionally use.
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500307 std::unique_ptr<uint8_t[]> alphaData(new uint8_t[kTestTextureSize * kTestTextureSize]);
308 for (int y = 0; y < kTestTextureSize; ++y) {
309 for (int x = 0; x < kTestTextureSize; ++x) {
310 alphaData[kTestTextureSize * y + x] = random->nextULessThan(256);
311 }
312 }
313
Brian Osman2700abc2018-09-12 10:19:41 -0400314 SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize,
315 kAlpha_8_SkColorType, kPremul_SkAlphaType);
316 SkPixmap pixmap(ii, alphaData.get(), ii.minRowBytes());
317 sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap);
318 proxies[1] = proxyProvider->createTextureProxy(img, kNone_GrSurfaceFlags, 1,
319 SkBudgeted::kYes, SkBackingFit::kExact);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500320 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400321
322 return proxies[0] && proxies[1];
323}
324
325// Creates a texture of premul colors used as the output of the fragment processor that precedes
326// the fragment processor under test. Color values are those provided by input_texel_color().
Michael Ludwig7034f152018-10-08 16:43:58 -0400327sk_sp<GrTextureProxy> make_input_texture(GrProxyProvider* proxyProvider, int width, int height,
328 SkScalar delta) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400329 std::unique_ptr<GrColor[]> data(new GrColor[width * height]);
330 for (int y = 0; y < width; ++y) {
331 for (int x = 0; x < height; ++x) {
Michael Ludwig7034f152018-10-08 16:43:58 -0400332 data.get()[width * y + x] = input_texel_color(x, y, delta);
Brian Salomon0e05a822017-07-25 09:43:22 -0400333 }
334 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500335
Brian Osman2700abc2018-09-12 10:19:41 -0400336 SkImageInfo ii = SkImageInfo::Make(width, height, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
337 SkPixmap pixmap(ii, data.get(), ii.minRowBytes());
338 sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap);
339 return proxyProvider->createTextureProxy(img, kNone_GrSurfaceFlags, 1,
340 SkBudgeted::kYes, SkBackingFit::kExact);
Brian Salomon0e05a822017-07-25 09:43:22 -0400341}
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500342
Michael Ludwige8e10752018-10-01 12:42:53 -0400343bool log_surface_context(sk_sp<GrSurfaceContext> src, SkString* dst) {
344 SkImageInfo ii = SkImageInfo::Make(src->width(), src->height(), kRGBA_8888_SkColorType,
345 kPremul_SkAlphaType);
346 SkBitmap bm;
347 SkAssertResult(bm.tryAllocPixels(ii));
348 SkAssertResult(src->readPixels(ii, bm.getPixels(), bm.rowBytes(), 0, 0));
349
350 return bitmap_to_base64_data_uri(bm, dst);
351}
352
353bool log_surface_proxy(GrContext* context, sk_sp<GrSurfaceProxy> src, SkString* dst) {
354 sk_sp<GrSurfaceContext> sContext(context->contextPriv().makeWrappedSurfaceContext(src));
355 return log_surface_context(sContext, dst);
356}
357
Michael Ludwig7034f152018-10-08 16:43:58 -0400358bool fuzzy_color_equals(const SkPMColor4f& c1, const SkPMColor4f& c2) {
359 // With the loss of precision of rendering into 32-bit color, then estimating the FP's output
360 // from that, it is not uncommon for a valid output to differ from estimate by up to 0.01
361 // (really 1/128 ~ .0078, but frequently floating point issues make that tolerance a little
362 // too unforgiving).
363 static constexpr SkScalar kTolerance = 0.01f;
364 for (int i = 0; i < 4; i++) {
365 if (!SkScalarNearlyEqual(c1[i], c2[i], kTolerance)) {
366 return false;
367 }
368 }
369 return true;
370}
371
372int modulation_index(int channelIndex, bool alphaModulation) {
373 return alphaModulation ? 3 : channelIndex;
374}
375
376// Given three input colors (color preceding the FP being tested), and the output of the FP, this
377// ensures that the out1 = fp * in1.a, out2 = fp * in2.a, and out3 = fp * in3.a, where fp is the
378// pre-modulated color that should not be changing across frames (FP's state doesn't change).
379//
380// When alphaModulation is false, this tests the very similar conditions that out1 = fp * in1,
381// etc. using per-channel modulation instead of modulation by just the input alpha channel.
382// - This estimates the pre-modulated fp color from one of the input/output pairs and confirms the
383// conditions hold for the other two pairs.
384bool legal_modulation(const GrColor& in1, const GrColor& in2, const GrColor& in3,
385 const GrColor& out1, const GrColor& out2, const GrColor& out3,
386 bool alphaModulation) {
387 // Convert to floating point, which is the number space the FP operates in (more or less)
Brian Osmancb3d0872018-10-16 15:19:28 -0400388 SkPMColor4f in1f = SkPMColor4f::FromBytes_RGBA(in1);
389 SkPMColor4f in2f = SkPMColor4f::FromBytes_RGBA(in2);
390 SkPMColor4f in3f = SkPMColor4f::FromBytes_RGBA(in3);
391 SkPMColor4f out1f = SkPMColor4f::FromBytes_RGBA(out1);
392 SkPMColor4f out2f = SkPMColor4f::FromBytes_RGBA(out2);
393 SkPMColor4f out3f = SkPMColor4f::FromBytes_RGBA(out3);
Michael Ludwig7034f152018-10-08 16:43:58 -0400394
395 // Reconstruct the output of the FP before the shader modulated its color with the input value.
396 // When the original input is very small, it may cause the final output color to round
397 // to 0, in which case we estimate the pre-modulated color using one of the stepped frames that
398 // will then have a guaranteed larger channel value (since the offset will be added to it).
399 SkPMColor4f fpPreModulation;
400 for (int i = 0; i < 4; i++) {
401 int modulationIndex = modulation_index(i, alphaModulation);
402 if (in1f[modulationIndex] < 0.2f) {
403 // Use the stepped frame
404 fpPreModulation[i] = out2f[i] / in2f[modulationIndex];
405 } else {
406 fpPreModulation[i] = out1f[i] / in1f[modulationIndex];
407 }
408 }
409
410 // With reconstructed pre-modulated FP output, derive the expected value of fp * input for each
411 // of the transformed input colors.
Brian Osmancb3d0872018-10-16 15:19:28 -0400412 SkPMColor4f expected1 = alphaModulation ? (fpPreModulation * in1f.fA)
413 : (fpPreModulation * in1f);
414 SkPMColor4f expected2 = alphaModulation ? (fpPreModulation * in2f.fA)
415 : (fpPreModulation * in2f);
416 SkPMColor4f expected3 = alphaModulation ? (fpPreModulation * in3f.fA)
417 : (fpPreModulation * in3f);
Michael Ludwig7034f152018-10-08 16:43:58 -0400418
Brian Osmancb3d0872018-10-16 15:19:28 -0400419 return fuzzy_color_equals(out1f, expected1) &&
420 fuzzy_color_equals(out2f, expected2) &&
421 fuzzy_color_equals(out3f, expected3);
Michael Ludwig7034f152018-10-08 16:43:58 -0400422}
423
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500424DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500425 GrContext* context = ctxInfo.grContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500426 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500427 auto resourceProvider = context->contextPriv().resourceProvider();
Brian Salomon1c053642017-07-24 10:16:19 -0400428 using FPFactory = GrFragmentProcessorTestFactory;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400429
Michael Ludwig3ad86d02018-09-28 16:00:38 -0400430 uint32_t seed = FLAGS_processorSeed;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400431 if (FLAGS_randomProcessorTest) {
432 std::random_device rd;
433 seed = rd();
434 }
435 // 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 -0400436 // use --processorSeed <seed> (without --randomProcessorTest) to reproduce.
Brian Osmanc35a2d42017-03-17 10:58:53 -0400437 SkRandom random(seed);
438
Greg Daniel4065d452018-11-16 15:43:41 -0500439 const GrBackendFormat format =
440 context->contextPriv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
441
Brian Salomon0e05a822017-07-25 09:43:22 -0400442 // Make the destination context for the test.
443 static constexpr int kRenderSize = 256;
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500444 sk_sp<GrRenderTargetContext> rtc = context->contextPriv().makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -0500445 format, SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig,
446 nullptr);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500447
Robert Phillipse78b7252017-04-06 07:59:41 -0400448 sk_sp<GrTextureProxy> proxies[2];
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500449 if (!init_test_textures(proxyProvider, &random, proxies)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400450 ERRORF(reporter, "Could not create test textures");
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400451 return;
452 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400453 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
Brian Salomon587e08f2017-01-27 10:59:27 -0500454
Michael Ludwig7034f152018-10-08 16:43:58 -0400455 // Coverage optimization uses three frames with a linearly transformed input texture. The first
456 // frame has no offset, second frames add .2 and .4, which should then be present as a fixed
457 // difference between the frame outputs if the FP is properly following the modulation
458 // requirements of the coverage optimization.
459 static constexpr SkScalar kInputDelta = 0.2f;
460 auto inputTexture1 = make_input_texture(proxyProvider, kRenderSize, kRenderSize, 0.0f);
461 auto inputTexture2 = make_input_texture(proxyProvider, kRenderSize, kRenderSize, kInputDelta);
462 auto inputTexture3 = make_input_texture(proxyProvider, kRenderSize, kRenderSize, 2*kInputDelta);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500463
Michael Ludwige8e10752018-10-01 12:42:53 -0400464 // Encoded images are very verbose and this tests many potential images, so only export the
465 // first failure (subsequent failures have a reasonable chance of being related).
466 bool loggedFirstFailure = false;
Michael Ludwig314d3772018-10-03 16:04:38 -0400467 bool loggedFirstWarning = false;
Michael Ludwig7034f152018-10-08 16:43:58 -0400468
469 // Storage for the three frames required for coverage compatibility optimization. Each frame
470 // uses the correspondingly numbered inputTextureX.
471 std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
472 std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
473 std::unique_ptr<GrColor[]> readData3(new GrColor[kRenderSize * kRenderSize]);
474
Brian Salomon0e05a822017-07-25 09:43:22 -0400475 // Because processor factories configure themselves in random ways, this is not exhaustive.
Brian Salomon587e08f2017-01-27 10:59:27 -0500476 for (int i = 0; i < FPFactory::Count(); ++i) {
477 int timesToInvokeFactory = 5;
478 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
479 // on child optimizations being present.
Brian Salomonaff329b2017-08-11 09:40:37 -0400480 std::unique_ptr<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
Brian Salomon587e08f2017-01-27 10:59:27 -0500481 for (int j = 0; j < fp->numChildProcessors(); ++j) {
482 // This value made a reasonable trade off between time and coverage when this test was
483 // written.
484 timesToInvokeFactory *= FPFactory::Count() / 2;
485 }
486 for (int j = 0; j < timesToInvokeFactory; ++j) {
487 fp = FPFactory::MakeIdx(i, &testData);
Robert Phillips6be756b2018-01-16 15:07:54 -0500488 if (!fp->instantiate(resourceProvider)) {
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400489 continue;
490 }
491
Brian Salomon587e08f2017-01-27 10:59:27 -0500492 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
Brian Salomonf3b995b2017-02-15 10:22:23 -0500493 !fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500494 continue;
495 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400496
Michael Ludwig7034f152018-10-08 16:43:58 -0400497 if (fp->compatibleWithCoverageAsAlpha()) {
498 // 2nd and 3rd frames are only used when checking coverage optimization
499 render_fp(context, rtc.get(), fp.get(), inputTexture2, readData2.get());
500 render_fp(context, rtc.get(), fp.get(), inputTexture3, readData3.get());
501 }
502 // Draw base frame last so that rtc holds the original FP behavior if we need to
503 // dump the image to the log.
504 render_fp(context, rtc.get(), fp.get(), inputTexture1, readData1.get());
Brian Salomonaff329b2017-08-11 09:40:37 -0400505
Brian Salomon587e08f2017-01-27 10:59:27 -0500506 if (0) { // Useful to see what FPs are being tested.
507 SkString children;
Michael Ludwig7034f152018-10-08 16:43:58 -0400508 for (int c = 0; c < fp->numChildProcessors(); ++c) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500509 if (!c) {
510 children.append("(");
511 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400512 children.append(fp->childProcessor(c).name());
513 children.append(c == fp->numChildProcessors() - 1 ? ")" : ", ");
Brian Salomon587e08f2017-01-27 10:59:27 -0500514 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400515 SkDebugf("%s %s\n", fp->name(), children.c_str());
Brian Salomon587e08f2017-01-27 10:59:27 -0500516 }
Michael Ludwig314d3772018-10-03 16:04:38 -0400517
518 // This test has a history of being flaky on a number of devices. If an FP is logically
519 // violating the optimizations, it's reasonable to expect it to violate requirements on
520 // a large number of pixels in the image. Sporadic pixel violations are more indicative
521 // of device errors and represents a separate problem.
522#if defined(SK_SKQP_GLOBAL_ERROR_TOLERANCE)
523 static constexpr int kMaxAcceptableFailedPixels = 0; // Strict when running as SKQP
524#else
525 static constexpr int kMaxAcceptableFailedPixels = 2 * kRenderSize; // ~0.7% of the image
526#endif
527
528 int failedPixelCount = 0;
529 // Collect first optimization failure message, to be output later as a warning or an
530 // error depending on whether the rendering "passed" or failed.
531 SkString coverageMessage;
532 SkString opaqueMessage;
533 SkString constMessage;
534 for (int y = 0; y < kRenderSize; ++y) {
535 for (int x = 0; x < kRenderSize; ++x) {
536 bool passing = true;
Michael Ludwig7034f152018-10-08 16:43:58 -0400537 GrColor input = input_texel_color(x, y, 0.0f);
538 GrColor output = readData1.get()[y * kRenderSize + x];
539
540 if (fp->compatibleWithCoverageAsAlpha()) {
541 GrColor i2 = input_texel_color(x, y, kInputDelta);
542 GrColor i3 = input_texel_color(x, y, 2 * kInputDelta);
543
544 GrColor o2 = readData2.get()[y * kRenderSize + x];
545 GrColor o3 = readData3.get()[y * kRenderSize + x];
546
547 // A compatible processor is allowed to modulate either the input color or
Brian Salomon587e08f2017-01-27 10:59:27 -0500548 // just the input alpha.
Michael Ludwig7034f152018-10-08 16:43:58 -0400549 bool legalAlphaModulation = legal_modulation(input, i2, i3, output, o2, o3,
550 /* alpha */ true);
551 bool legalColorModulation = legal_modulation(input, i2, i3, output, o2, o3,
552 /* alpha */ false);
553
Brian Salomon587e08f2017-01-27 10:59:27 -0500554 if (!legalColorModulation && !legalAlphaModulation) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500555 passing = false;
Michael Ludwig314d3772018-10-03 16:04:38 -0400556
557 if (coverageMessage.isEmpty()) {
Michael Ludwig7034f152018-10-08 16:43:58 -0400558 coverageMessage.printf("\"Modulating\" processor %s did not match "
559 "alpha-modulation nor color-modulation rules. "
560 "Input: 0x%08x, Output: 0x%08x, pixel (%d, %d).",
561 fp->name(), input, output, x, y);
Michael Ludwig314d3772018-10-03 16:04:38 -0400562 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500563 }
564 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400565
Brian Osmancb3d0872018-10-16 15:19:28 -0400566 SkPMColor4f input4f = SkPMColor4f::FromBytes_RGBA(input);
567 SkPMColor4f output4f = SkPMColor4f::FromBytes_RGBA(output);
Brian Osman1d5b5982018-10-01 13:41:39 -0400568 SkPMColor4f expected4f;
Michael Ludwig7034f152018-10-08 16:43:58 -0400569 if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) {
Brian Osmancb3d0872018-10-16 15:19:28 -0400570 float rDiff = fabsf(output4f.fR - expected4f.fR);
571 float gDiff = fabsf(output4f.fG - expected4f.fG);
572 float bDiff = fabsf(output4f.fB - expected4f.fB);
573 float aDiff = fabsf(output4f.fA - expected4f.fA);
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500574 static constexpr float kTol = 4 / 255.f;
Brian Salomon587e08f2017-01-27 10:59:27 -0500575 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
Michael Ludwig314d3772018-10-03 16:04:38 -0400576 if (constMessage.isEmpty()) {
577 passing = false;
578
579 constMessage.printf("Processor %s claimed output for const input "
580 "doesn't match actual output. Error: %f, Tolerance: %f, "
581 "input: (%f, %f, %f, %f), actual: (%f, %f, %f, %f), "
Michael Ludwig7034f152018-10-08 16:43:58 -0400582 "expected(%f, %f, %f, %f)", fp->name(),
Michael Ludwig314d3772018-10-03 16:04:38 -0400583 SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))), kTol,
584 input4f.fR, input4f.fG, input4f.fB, input4f.fA,
Brian Osmancb3d0872018-10-16 15:19:28 -0400585 output4f.fR, output4f.fG, output4f.fB, output4f.fA,
586 expected4f.fR, expected4f.fG, expected4f.fB, expected4f.fA);
Michael Ludwig314d3772018-10-03 16:04:38 -0400587 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500588 }
589 }
Brian Osman00b29392018-11-05 15:42:43 -0500590 if (input4f.isOpaque() && fp->preservesOpaqueInput() && !output4f.isOpaque()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500591 passing = false;
Michael Ludwig314d3772018-10-03 16:04:38 -0400592
593 if (opaqueMessage.isEmpty()) {
594 opaqueMessage.printf("Processor %s claimed opaqueness is preserved but "
595 "it is not. Input: 0x%08x, Output: 0x%08x.",
Michael Ludwig7034f152018-10-08 16:43:58 -0400596 fp->name(), input, output);
Michael Ludwige8e10752018-10-01 12:42:53 -0400597 }
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400598 }
Michael Ludwig314d3772018-10-03 16:04:38 -0400599
600 if (!passing) {
601 // Regardless of how many optimizations the pixel violates, count it as a
602 // single bad pixel.
603 failedPixelCount++;
604 }
605 }
606 }
607
608 // Finished analyzing the entire image, see if the number of pixel failures meets the
609 // threshold for an FP violating the optimization requirements.
610 if (failedPixelCount > kMaxAcceptableFailedPixels) {
Michael Ludwig4504a6522018-10-03 16:47:55 -0400611 ERRORF(reporter, "Processor violated %d of %d pixels, seed: 0x%08x, processor: %s"
Michael Ludwig314d3772018-10-03 16:04:38 -0400612 ", first failing pixel details are below:",
613 failedPixelCount, kRenderSize * kRenderSize, seed,
Michael Ludwig7034f152018-10-08 16:43:58 -0400614 fp->dumpInfo().c_str());
Michael Ludwig314d3772018-10-03 16:04:38 -0400615
616 // Print first failing pixel's details.
617 if (!coverageMessage.isEmpty()) {
618 ERRORF(reporter, coverageMessage.c_str());
619 }
620 if (!constMessage.isEmpty()) {
621 ERRORF(reporter, constMessage.c_str());
622 }
623 if (!opaqueMessage.isEmpty()) {
624 ERRORF(reporter, opaqueMessage.c_str());
625 }
626
627 if (!loggedFirstFailure) {
628 // Print with ERRORF to make sure the encoded image is output
629 SkString input;
Michael Ludwig7034f152018-10-08 16:43:58 -0400630 log_surface_proxy(context, inputTexture1, &input);
Michael Ludwig314d3772018-10-03 16:04:38 -0400631 SkString output;
632 log_surface_context(rtc, &output);
633 ERRORF(reporter, "Input image: %s\n\n"
634 "===========================================================\n\n"
635 "Output image: %s\n", input.c_str(), output.c_str());
636 loggedFirstFailure = true;
637 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400638 } else if(failedPixelCount > 0) {
Michael Ludwig314d3772018-10-03 16:04:38 -0400639 // Don't trigger an error, but don't just hide the failures either.
640 INFOF(reporter, "Processor violated %d of %d pixels (below error threshold), seed: "
641 "0x%08x, processor: %s", failedPixelCount, kRenderSize * kRenderSize,
Michael Ludwig7034f152018-10-08 16:43:58 -0400642 seed, fp->dumpInfo().c_str());
Michael Ludwig314d3772018-10-03 16:04:38 -0400643 if (!coverageMessage.isEmpty()) {
644 INFOF(reporter, coverageMessage.c_str());
645 }
646 if (!constMessage.isEmpty()) {
647 INFOF(reporter, constMessage.c_str());
648 }
649 if (!opaqueMessage.isEmpty()) {
650 INFOF(reporter, opaqueMessage.c_str());
651 }
652 if (!loggedFirstWarning) {
653 SkString input;
Michael Ludwig7034f152018-10-08 16:43:58 -0400654 log_surface_proxy(context, inputTexture1, &input);
Michael Ludwig314d3772018-10-03 16:04:38 -0400655 SkString output;
656 log_surface_context(rtc, &output);
657 INFOF(reporter, "Input image: %s\n\n"
658 "===========================================================\n\n"
659 "Output image: %s\n", input.c_str(), output.c_str());
660 loggedFirstWarning = true;
Brian Salomon587e08f2017-01-27 10:59:27 -0500661 }
662 }
663 }
664 }
665}
Robert Phillips18166ee2017-06-01 12:55:44 -0400666
Brian Salomon0e05a822017-07-25 09:43:22 -0400667// Tests that fragment processors returned by GrFragmentProcessor::clone() are equivalent to their
668// progenitors.
669DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) {
670 GrContext* context = ctxInfo.grContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500671 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500672 auto resourceProvider = context->contextPriv().resourceProvider();
Brian Salomon0e05a822017-07-25 09:43:22 -0400673
674 SkRandom random;
675
Greg Daniel4065d452018-11-16 15:43:41 -0500676 const GrBackendFormat format =
677 context->contextPriv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
678
Brian Salomon0e05a822017-07-25 09:43:22 -0400679 // Make the destination context for the test.
680 static constexpr int kRenderSize = 1024;
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500681 sk_sp<GrRenderTargetContext> rtc = context->contextPriv().makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -0500682 format, SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig,
683 nullptr);
Brian Salomon0e05a822017-07-25 09:43:22 -0400684
685 sk_sp<GrTextureProxy> proxies[2];
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500686 if (!init_test_textures(proxyProvider, &random, proxies)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400687 ERRORF(reporter, "Could not create test textures");
688 return;
689 }
690 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
691
Michael Ludwig7034f152018-10-08 16:43:58 -0400692 auto inputTexture = make_input_texture(proxyProvider, kRenderSize, kRenderSize, 0.0f);
Brian Salomon0e05a822017-07-25 09:43:22 -0400693 std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
694 std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
695 auto readInfo = SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
696 kPremul_SkAlphaType);
697
698 // Because processor factories configure themselves in random ways, this is not exhaustive.
699 for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) {
700 static constexpr int kTimesToInvokeFactory = 10;
701 for (int j = 0; j < kTimesToInvokeFactory; ++j) {
702 auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &testData);
703 auto clone = fp->clone();
Brian Salomon0e05a822017-07-25 09:43:22 -0400704 if (!clone) {
Brian Salomon96271cd2017-07-31 16:27:23 -0400705 ERRORF(reporter, "Clone of processor %s failed.", fp->name());
Brian Salomon0e05a822017-07-25 09:43:22 -0400706 continue;
707 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400708 const char* name = fp->name();
Robert Phillips6be756b2018-01-16 15:07:54 -0500709 if (!fp->instantiate(resourceProvider) || !clone->instantiate(resourceProvider)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400710 continue;
711 }
Brian Salomonce06e262017-08-01 16:23:40 -0400712 REPORTER_ASSERT(reporter, !strcmp(fp->name(), clone->name()));
713 REPORTER_ASSERT(reporter, fp->compatibleWithCoverageAsAlpha() ==
714 clone->compatibleWithCoverageAsAlpha());
715 REPORTER_ASSERT(reporter, fp->isEqual(*clone));
716 REPORTER_ASSERT(reporter, fp->preservesOpaqueInput() == clone->preservesOpaqueInput());
717 REPORTER_ASSERT(reporter, fp->hasConstantOutputForConstantInput() ==
718 clone->hasConstantOutputForConstantInput());
719 REPORTER_ASSERT(reporter, fp->numChildProcessors() == clone->numChildProcessors());
720 REPORTER_ASSERT(reporter, fp->usesLocalCoords() == clone->usesLocalCoords());
Brian Salomon0e05a822017-07-25 09:43:22 -0400721 // Draw with original and read back the results.
Michael Ludwig7034f152018-10-08 16:43:58 -0400722 render_fp(context, rtc.get(), fp.get(), inputTexture, readData1.get());
Brian Salomon0e05a822017-07-25 09:43:22 -0400723
724 // Draw with clone and read back the results.
Michael Ludwig7034f152018-10-08 16:43:58 -0400725 render_fp(context, rtc.get(), clone.get(), inputTexture, readData2.get());
Brian Salomon0e05a822017-07-25 09:43:22 -0400726
727 // Check that the results are the same.
728 bool passing = true;
729 for (int y = 0; y < kRenderSize && passing; ++y) {
730 for (int x = 0; x < kRenderSize && passing; ++x) {
731 int idx = y * kRenderSize + x;
732 if (readData1[idx] != readData2[idx]) {
733 ERRORF(reporter,
734 "Processor %s made clone produced different output. "
735 "Input color: 0x%08x, Original Output Color: 0x%08x, "
736 "Clone Output Color: 0x%08x..",
Michael Ludwig7034f152018-10-08 16:43:58 -0400737 name, input_texel_color(x, y, 0.0f), readData1[idx], readData2[idx]);
Brian Salomon0e05a822017-07-25 09:43:22 -0400738 passing = false;
739 }
740 }
741 }
742 }
743 }
744}
745
Hal Canary6f6961e2017-01-31 13:50:44 -0500746#endif // GR_TEST_UTILS
747#endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS