blob: 6e241a24c27b1cfdb5c739d5ae09fb79ddc70a11 [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"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050024
Hal Canary8a001442018-09-19 11:31:27 -040025#include <random>
26
Brian Salomonbc6b99d2017-01-11 10:32:34 -050027namespace {
Brian Salomon82ddc942017-07-14 12:00:13 -040028class TestOp : public GrMeshDrawOp {
Brian Salomonbc6b99d2017-01-11 10:32:34 -050029public:
30 DEFINE_OP_CLASS_ID
Robert Phillips7c525e62018-06-12 10:11:12 -040031 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
32 std::unique_ptr<GrFragmentProcessor> fp) {
Robert Phillipsc994a932018-06-19 13:09:54 -040033 GrOpMemoryPool* pool = context->contextPriv().opMemoryPool();
34
35 return pool->allocate<TestOp>(std::move(fp));
Brian Salomon82ddc942017-07-14 12:00:13 -040036 }
37
Robert Phillips5f567c72017-09-14 08:27:37 -040038 const char* name() const override { return "TestOp"; }
39
Robert Phillipsf1748f52017-09-14 14:11:24 -040040 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillips5f567c72017-09-14 08:27:37 -040041 fProcessors.visitProxies(func);
42 }
43
Brian Salomon82ddc942017-07-14 12:00:13 -040044 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
45
Brian Osman532b3f92018-07-11 10:02:07 -040046 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
Brian Salomon82ddc942017-07-14 12:00:13 -040047 static constexpr GrProcessorAnalysisColor kUnknownColor;
48 GrColor overrideColor;
49 fProcessors.finalize(kUnknownColor, GrProcessorAnalysisCoverage::kNone, clip, false, caps,
Brian Osman532b3f92018-07-11 10:02:07 -040050 &overrideColor);
Brian Salomon82ddc942017-07-14 12:00:13 -040051 return RequiresDstTexture::kNo;
Brian Salomon649a3412017-03-09 13:50:43 -050052 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050053
54private:
Robert Phillips7c525e62018-06-12 10:11:12 -040055 friend class ::GrOpMemoryPool; // for ctor
56
Brian Salomonaff329b2017-08-11 09:40:37 -040057 TestOp(std::unique_ptr<GrFragmentProcessor> fp)
58 : INHERITED(ClassID()), fProcessors(std::move(fp)) {
Brian Salomon82ddc942017-07-14 12:00:13 -040059 this->setBounds(SkRect::MakeWH(100, 100), HasAABloat::kNo, IsZeroArea::kNo);
60 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050061
Brian Salomon91326c32017-08-09 16:02:19 -040062 void onPrepareDraws(Target* target) override { return; }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050063
Brian Salomon82ddc942017-07-14 12:00:13 -040064 GrProcessorSet fProcessors;
65
66 typedef GrMeshDrawOp INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -050067};
68
69/**
70 * FP used to test ref/IO counts on owned GrGpuResources. Can also be a parent FP to test counts
71 * of resources owned by child FPs.
72 */
73class TestFP : public GrFragmentProcessor {
74public:
Brian Salomonaff329b2017-08-11 09:40:37 -040075 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> child) {
76 return std::unique_ptr<GrFragmentProcessor>(new TestFP(std::move(child)));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050077 }
Brian Salomonaff329b2017-08-11 09:40:37 -040078 static std::unique_ptr<GrFragmentProcessor> Make(const SkTArray<sk_sp<GrTextureProxy>>& proxies,
Brian Salomon559f5562017-11-15 14:28:33 -050079 const SkTArray<sk_sp<GrBuffer>>& buffers) {
80 return std::unique_ptr<GrFragmentProcessor>(new TestFP(proxies, buffers));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050081 }
82
83 const char* name() const override { return "test"; }
84
85 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
86 // We don't really care about reusing these.
87 static int32_t gKey = 0;
88 b->add32(sk_atomic_inc(&gKey));
89 }
90
Brian Salomonaff329b2017-08-11 09:40:37 -040091 std::unique_ptr<GrFragmentProcessor> clone() const override {
92 return std::unique_ptr<GrFragmentProcessor>(new TestFP(*this));
Brian Salomonb17e6392017-07-28 13:41:51 -040093 }
94
Brian Salomonbc6b99d2017-01-11 10:32:34 -050095private:
Brian Salomon559f5562017-11-15 14:28:33 -050096 TestFP(const SkTArray<sk_sp<GrTextureProxy>>& proxies, const SkTArray<sk_sp<GrBuffer>>& buffers)
Brian Salomon662ea4b2018-07-12 14:53:49 -040097 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4) {
Robert Phillips30f9bc62017-02-22 15:28:38 -050098 for (const auto& proxy : proxies) {
Brian Salomonf7dcd762018-07-30 14:48:15 -040099 fSamplers.emplace_back(proxy);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500100 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400101 this->setTextureSamplerCnt(fSamplers.count());
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500102 }
103
Brian Salomonaff329b2017-08-11 09:40:37 -0400104 TestFP(std::unique_ptr<GrFragmentProcessor> child)
Brian Salomon662ea4b2018-07-12 14:53:49 -0400105 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4) {
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500106 this->registerChildProcessor(std::move(child));
107 }
108
Brian Salomon96271cd2017-07-31 16:27:23 -0400109 explicit TestFP(const TestFP& that)
Brian Salomon662ea4b2018-07-12 14:53:49 -0400110 : INHERITED(kTestFP_ClassID, that.optimizationFlags()), fSamplers(4) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400111 for (int i = 0; i < that.fSamplers.count(); ++i) {
112 fSamplers.emplace_back(that.fSamplers[i]);
Brian Salomonb17e6392017-07-28 13:41:51 -0400113 }
Brian Salomon96271cd2017-07-31 16:27:23 -0400114 for (int i = 0; i < that.numChildProcessors(); ++i) {
115 this->registerChildProcessor(that.childProcessor(i).clone());
Brian Salomonb17e6392017-07-28 13:41:51 -0400116 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400117 this->setTextureSamplerCnt(fSamplers.count());
Brian Salomonb17e6392017-07-28 13:41:51 -0400118 }
119
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500120 virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
121 class TestGLSLFP : public GrGLSLFragmentProcessor {
122 public:
123 TestGLSLFP() {}
124 void emitCode(EmitArgs& args) override {
125 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
126 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor);
127 }
128
129 private:
130 };
131 return new TestGLSLFP();
132 }
133
134 bool onIsEqual(const GrFragmentProcessor&) const override { return false; }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400135 const TextureSampler& onTextureSampler(int i) const override { return fSamplers[i]; }
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500136
137 GrTAllocator<TextureSampler> fSamplers;
Brian Salomon587e08f2017-01-27 10:59:27 -0500138 typedef GrFragmentProcessor INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500139};
140}
141
Brian Salomonf046e152017-01-11 15:24:47 -0500142template <typename T>
143inline void testingOnly_getIORefCnts(const T* resource, int* refCnt, int* readCnt, int* writeCnt) {
144 *refCnt = resource->fRefCnt;
145 *readCnt = resource->fPendingReads;
146 *writeCnt = resource->fPendingWrites;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500147}
148
Robert Phillips2f493142017-03-02 18:18:38 -0500149void testingOnly_getIORefCnts(GrTextureProxy* proxy, int* refCnt, int* readCnt, int* writeCnt) {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500150 *refCnt = proxy->getBackingRefCnt_TestOnly();
151 *readCnt = proxy->getPendingReadCnt_TestOnly();
152 *writeCnt = proxy->getPendingWriteCnt_TestOnly();
153}
154
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500155DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) {
156 GrContext* context = ctxInfo.grContext();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500157 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500158
Robert Phillips16d8ec62017-07-27 16:16:25 -0400159 GrSurfaceDesc desc;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500160 desc.fWidth = 10;
161 desc.fHeight = 10;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400162 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500163
Brian Salomonb17e6392017-07-28 13:41:51 -0400164 for (bool makeClone : {false, true}) {
165 for (int parentCnt = 0; parentCnt < 2; parentCnt++) {
166 sk_sp<GrRenderTargetContext> renderTargetContext(
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500167 context->contextPriv().makeDeferredRenderTargetContext(
168 SkBackingFit::kApprox, 1, 1,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500169 kRGBA_8888_GrPixelConfig, nullptr));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500170 {
Brian Salomon2a4f9832018-03-03 22:43:43 -0500171 sk_sp<GrTextureProxy> proxy1 = proxyProvider->createProxy(
172 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
173 sk_sp<GrTextureProxy> proxy2 = proxyProvider->createProxy(
174 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
175 sk_sp<GrTextureProxy> proxy3 = proxyProvider->createProxy(
176 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
177 sk_sp<GrTextureProxy> proxy4 = proxyProvider->createProxy(
178 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
Brian Salomonb17e6392017-07-28 13:41:51 -0400179 {
180 SkTArray<sk_sp<GrTextureProxy>> proxies;
181 SkTArray<sk_sp<GrBuffer>> buffers;
Brian Salomonb17e6392017-07-28 13:41:51 -0400182 proxies.push_back(proxy1);
Brian Salomon559f5562017-11-15 14:28:33 -0500183 auto fp = TestFP::Make(std::move(proxies), std::move(buffers));
Brian Salomonb17e6392017-07-28 13:41:51 -0400184 for (int i = 0; i < parentCnt; ++i) {
185 fp = TestFP::Make(std::move(fp));
186 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400187 std::unique_ptr<GrFragmentProcessor> clone;
Brian Salomonb17e6392017-07-28 13:41:51 -0400188 if (makeClone) {
189 clone = fp->clone();
190 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400191 std::unique_ptr<GrDrawOp> op(TestOp::Make(context, std::move(fp)));
Brian Salomonb17e6392017-07-28 13:41:51 -0400192 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
193 if (clone) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400194 op = TestOp::Make(context, std::move(clone));
Brian Salomonb17e6392017-07-28 13:41:51 -0400195 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
196 }
197 }
198 int refCnt, readCnt, writeCnt;
199
200 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
201 // IO counts should be double if there is a clone of the FP.
202 int ioRefMul = makeClone ? 2 : 1;
Robert Phillips715d08c2018-07-18 13:56:48 -0400203 REPORTER_ASSERT(reporter, -1 == refCnt);
Brian Salomonb17e6392017-07-28 13:41:51 -0400204 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
205 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
206
Brian Salomonb17e6392017-07-28 13:41:51 -0400207 context->flush();
208
209 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
210 REPORTER_ASSERT(reporter, 1 == refCnt);
211 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
212 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
213
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500214 }
215 }
216 }
217}
Brian Salomon587e08f2017-01-27 10:59:27 -0500218
219// This test uses the random GrFragmentProcessor test factory, which relies on static initializers.
220#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
221
Brian Salomon0e05a822017-07-25 09:43:22 -0400222#include "SkCommandLineFlags.h"
223DEFINE_bool(randomProcessorTest, false, "Use non-deterministic seed for random processor tests?");
Michael Ludwig3ad86d02018-09-28 16:00:38 -0400224DEFINE_uint32(processorSeed, 0, "Use specific seed for processor tests. Overridden by " \
225 "--randomProcessorTest.");
Brian Salomon0e05a822017-07-25 09:43:22 -0400226
227#if GR_TEST_UTILS
228
229static GrColor input_texel_color(int i, int j) {
230 GrColor color = GrColorPackRGBA((uint8_t)j, (uint8_t)(i + j), (uint8_t)(2 * j - i), (uint8_t)i);
Brian Salomon587e08f2017-01-27 10:59:27 -0500231 return GrPremulColor(color);
232}
233
Brian Salomon0e05a822017-07-25 09:43:22 -0400234static GrColor4f input_texel_color4f(int i, int j) {
235 return GrColor4f::FromGrColor(input_texel_color(i, j));
236}
Brian Salomon587e08f2017-01-27 10:59:27 -0500237
Robert Phillips7c525e62018-06-12 10:11:12 -0400238void test_draw_op(GrContext* context,
239 GrRenderTargetContext* rtc,
240 std::unique_ptr<GrFragmentProcessor> fp,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500241 sk_sp<GrTextureProxy> inputDataProxy) {
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500242 GrPaint paint;
Brian Osman2240be92017-10-18 13:15:13 -0400243 paint.addColorTextureProcessor(std::move(inputDataProxy), SkMatrix::I());
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500244 paint.addColorFragmentProcessor(std::move(fp));
245 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonac70f842017-05-08 10:43:33 -0400246
Robert Phillips7c525e62018-06-12 10:11:12 -0400247 auto op = GrRectOpFactory::MakeNonAAFill(context, std::move(paint), SkMatrix::I(),
Brian Salomonbaaf4392017-06-15 09:59:23 -0400248 SkRect::MakeWH(rtc->width(), rtc->height()),
249 GrAAType::kNone);
Brian Salomonac70f842017-05-08 10:43:33 -0400250 rtc->addDrawOp(GrNoClip(), std::move(op));
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500251}
252
Brian Salomon0e05a822017-07-25 09:43:22 -0400253/** Initializes the two test texture proxies that are available to the FP test factories. */
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500254bool init_test_textures(GrProxyProvider* proxyProvider, SkRandom* random,
255 sk_sp<GrTextureProxy> proxies[2]) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400256 static const int kTestTextureSize = 256;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400257
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500258 {
259 // Put premul data into the RGBA texture that the test FPs can optionally use.
260 std::unique_ptr<GrColor[]> rgbaData(new GrColor[kTestTextureSize * kTestTextureSize]);
261 for (int y = 0; y < kTestTextureSize; ++y) {
262 for (int x = 0; x < kTestTextureSize; ++x) {
263 rgbaData[kTestTextureSize * y + x] =
264 input_texel_color(random->nextULessThan(256), random->nextULessThan(256));
265 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400266 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400267
Brian Osman2700abc2018-09-12 10:19:41 -0400268 SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize,
269 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
270 SkPixmap pixmap(ii, rgbaData.get(), ii.minRowBytes());
271 sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap);
272 proxies[0] = proxyProvider->createTextureProxy(img, kNone_GrSurfaceFlags, 1,
273 SkBudgeted::kYes, SkBackingFit::kExact);
Brian Salomon0e05a822017-07-25 09:43:22 -0400274 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500275
276 {
277 // Put random values into the alpha texture that the test FPs can optionally use.
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500278 std::unique_ptr<uint8_t[]> alphaData(new uint8_t[kTestTextureSize * kTestTextureSize]);
279 for (int y = 0; y < kTestTextureSize; ++y) {
280 for (int x = 0; x < kTestTextureSize; ++x) {
281 alphaData[kTestTextureSize * y + x] = random->nextULessThan(256);
282 }
283 }
284
Brian Osman2700abc2018-09-12 10:19:41 -0400285 SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize,
286 kAlpha_8_SkColorType, kPremul_SkAlphaType);
287 SkPixmap pixmap(ii, alphaData.get(), ii.minRowBytes());
288 sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap);
289 proxies[1] = proxyProvider->createTextureProxy(img, kNone_GrSurfaceFlags, 1,
290 SkBudgeted::kYes, SkBackingFit::kExact);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500291 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400292
293 return proxies[0] && proxies[1];
294}
295
296// Creates a texture of premul colors used as the output of the fragment processor that precedes
297// the fragment processor under test. Color values are those provided by input_texel_color().
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500298sk_sp<GrTextureProxy> make_input_texture(GrProxyProvider* proxyProvider, int width, int height) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400299 std::unique_ptr<GrColor[]> data(new GrColor[width * height]);
300 for (int y = 0; y < width; ++y) {
301 for (int x = 0; x < height; ++x) {
302 data.get()[width * y + x] = input_texel_color(x, y);
303 }
304 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500305
Brian Osman2700abc2018-09-12 10:19:41 -0400306 SkImageInfo ii = SkImageInfo::Make(width, height, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
307 SkPixmap pixmap(ii, data.get(), ii.minRowBytes());
308 sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap);
309 return proxyProvider->createTextureProxy(img, kNone_GrSurfaceFlags, 1,
310 SkBudgeted::kYes, SkBackingFit::kExact);
Brian Salomon0e05a822017-07-25 09:43:22 -0400311}
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500312
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500313DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500314 GrContext* context = ctxInfo.grContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500315 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500316 auto resourceProvider = context->contextPriv().resourceProvider();
Brian Salomon1c053642017-07-24 10:16:19 -0400317 using FPFactory = GrFragmentProcessorTestFactory;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400318
Michael Ludwig3ad86d02018-09-28 16:00:38 -0400319 uint32_t seed = FLAGS_processorSeed;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400320 if (FLAGS_randomProcessorTest) {
321 std::random_device rd;
322 seed = rd();
323 }
324 // 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 -0400325 // use --processorSeed <seed> (without --randomProcessorTest) to reproduce.
Brian Osmanc35a2d42017-03-17 10:58:53 -0400326 SkRandom random(seed);
327
Brian Salomon0e05a822017-07-25 09:43:22 -0400328 // Make the destination context for the test.
329 static constexpr int kRenderSize = 256;
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500330 sk_sp<GrRenderTargetContext> rtc = context->contextPriv().makeDeferredRenderTargetContext(
Brian Salomon0e05a822017-07-25 09:43:22 -0400331 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500332
Robert Phillipse78b7252017-04-06 07:59:41 -0400333 sk_sp<GrTextureProxy> proxies[2];
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500334 if (!init_test_textures(proxyProvider, &random, proxies)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400335 ERRORF(reporter, "Could not create test textures");
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400336 return;
337 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400338 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
Brian Salomon587e08f2017-01-27 10:59:27 -0500339
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500340 auto inputTexture = make_input_texture(proxyProvider, kRenderSize, kRenderSize);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500341
Ben Wagner8c791942017-07-25 11:47:48 -0400342 std::unique_ptr<GrColor[]> readData(new GrColor[kRenderSize * kRenderSize]);
Brian Salomon0e05a822017-07-25 09:43:22 -0400343 // Because processor factories configure themselves in random ways, this is not exhaustive.
Brian Salomon587e08f2017-01-27 10:59:27 -0500344 for (int i = 0; i < FPFactory::Count(); ++i) {
345 int timesToInvokeFactory = 5;
346 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
347 // on child optimizations being present.
Brian Salomonaff329b2017-08-11 09:40:37 -0400348 std::unique_ptr<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
Brian Salomon587e08f2017-01-27 10:59:27 -0500349 for (int j = 0; j < fp->numChildProcessors(); ++j) {
350 // This value made a reasonable trade off between time and coverage when this test was
351 // written.
352 timesToInvokeFactory *= FPFactory::Count() / 2;
353 }
354 for (int j = 0; j < timesToInvokeFactory; ++j) {
355 fp = FPFactory::MakeIdx(i, &testData);
Robert Phillips6be756b2018-01-16 15:07:54 -0500356 if (!fp->instantiate(resourceProvider)) {
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400357 continue;
358 }
359
Brian Salomon587e08f2017-01-27 10:59:27 -0500360 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
Brian Salomonf3b995b2017-02-15 10:22:23 -0500361 !fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500362 continue;
363 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400364
365 // Since we transfer away ownership of the original FP, we make a clone.
366 auto clone = fp->clone();
367
Robert Phillips7c525e62018-06-12 10:11:12 -0400368 test_draw_op(context, rtc.get(), std::move(fp), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400369 memset(readData.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
370 rtc->readPixels(SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
371 kPremul_SkAlphaType),
372 readData.get(), 0, 0, 0);
Brian Salomon587e08f2017-01-27 10:59:27 -0500373 bool passing = true;
374 if (0) { // Useful to see what FPs are being tested.
375 SkString children;
Brian Salomonaff329b2017-08-11 09:40:37 -0400376 for (int c = 0; c < clone->numChildProcessors(); ++c) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500377 if (!c) {
378 children.append("(");
379 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400380 children.append(clone->name());
381 children.append(c == clone->numChildProcessors() - 1 ? ")" : ", ");
Brian Salomon587e08f2017-01-27 10:59:27 -0500382 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400383 SkDebugf("%s %s\n", clone->name(), children.c_str());
Brian Salomon587e08f2017-01-27 10:59:27 -0500384 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400385 for (int y = 0; y < kRenderSize && passing; ++y) {
386 for (int x = 0; x < kRenderSize && passing; ++x) {
387 GrColor input = input_texel_color(x, y);
388 GrColor output = readData.get()[y * kRenderSize + x];
Brian Salomonaff329b2017-08-11 09:40:37 -0400389 if (clone->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500390 // A modulating processor is allowed to modulate either the input color or
391 // just the input alpha.
392 bool legalColorModulation =
393 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
394 GrColorUnpackR(output) <= GrColorUnpackR(input) &&
395 GrColorUnpackG(output) <= GrColorUnpackG(input) &&
396 GrColorUnpackB(output) <= GrColorUnpackB(input);
397 bool legalAlphaModulation =
398 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
399 GrColorUnpackR(output) <= GrColorUnpackA(input) &&
400 GrColorUnpackG(output) <= GrColorUnpackA(input) &&
401 GrColorUnpackB(output) <= GrColorUnpackA(input);
402 if (!legalColorModulation && !legalAlphaModulation) {
403 ERRORF(reporter,
404 "\"Modulating\" processor %s made color/alpha value larger. "
Greg Daniel10ed2432017-12-01 16:19:43 -0500405 "Input: 0x%08x, Output: 0x%08x, pixel (%d, %d).",
406 clone->name(), input, output, x, y);
Brian Salomon587e08f2017-01-27 10:59:27 -0500407 passing = false;
408 }
409 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400410 GrColor4f input4f = input_texel_color4f(x, y);
Brian Salomon587e08f2017-01-27 10:59:27 -0500411 GrColor4f output4f = GrColor4f::FromGrColor(output);
412 GrColor4f expected4f;
Brian Salomonaff329b2017-08-11 09:40:37 -0400413 if (clone->hasConstantOutputForConstantInput(input4f, &expected4f)) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500414 float rDiff = fabsf(output4f.fRGBA[0] - expected4f.fRGBA[0]);
415 float gDiff = fabsf(output4f.fRGBA[1] - expected4f.fRGBA[1]);
416 float bDiff = fabsf(output4f.fRGBA[2] - expected4f.fRGBA[2]);
417 float aDiff = fabsf(output4f.fRGBA[3] - expected4f.fRGBA[3]);
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500418 static constexpr float kTol = 4 / 255.f;
Brian Salomon587e08f2017-01-27 10:59:27 -0500419 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
420 ERRORF(reporter,
421 "Processor %s claimed output for const input doesn't match "
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500422 "actual output. Error: %f, Tolerance: %f, input: (%f, %f, %f, "
423 "%f), actual: (%f, %f, %f, %f), expected(%f, %f, %f, %f)",
Brian Salomonf61711a2018-02-16 10:44:28 -0500424 clone->name(),
425 SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))), kTol,
426 input4f.fRGBA[0], input4f.fRGBA[1], input4f.fRGBA[2],
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500427 input4f.fRGBA[3], output4f.fRGBA[0], output4f.fRGBA[1],
428 output4f.fRGBA[2], output4f.fRGBA[3], expected4f.fRGBA[0],
429 expected4f.fRGBA[1], expected4f.fRGBA[2], expected4f.fRGBA[3]);
Brian Salomon587e08f2017-01-27 10:59:27 -0500430 passing = false;
431 }
432 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400433 if (GrColorIsOpaque(input) && clone->preservesOpaqueInput() &&
Brian Salomon587e08f2017-01-27 10:59:27 -0500434 !GrColorIsOpaque(output)) {
435 ERRORF(reporter,
436 "Processor %s claimed opaqueness is preserved but it is not. Input: "
Brian Osman2f3865b2017-03-15 13:35:51 -0400437 "0x%08x, Output: 0x%08x.",
Brian Salomonaff329b2017-08-11 09:40:37 -0400438 clone->name(), input, output);
Brian Salomon587e08f2017-01-27 10:59:27 -0500439 passing = false;
440 }
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400441 if (!passing) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400442 ERRORF(reporter, "Seed: 0x%08x, Processor details: %s", seed,
443 clone->dumpInfo().c_str());
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400444 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500445 }
446 }
447 }
448 }
449}
Robert Phillips18166ee2017-06-01 12:55:44 -0400450
Brian Salomon0e05a822017-07-25 09:43:22 -0400451// Tests that fragment processors returned by GrFragmentProcessor::clone() are equivalent to their
452// progenitors.
453DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) {
454 GrContext* context = ctxInfo.grContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500455 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500456 auto resourceProvider = context->contextPriv().resourceProvider();
Brian Salomon0e05a822017-07-25 09:43:22 -0400457
458 SkRandom random;
459
460 // Make the destination context for the test.
461 static constexpr int kRenderSize = 1024;
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500462 sk_sp<GrRenderTargetContext> rtc = context->contextPriv().makeDeferredRenderTargetContext(
Brian Salomon0e05a822017-07-25 09:43:22 -0400463 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
464
465 sk_sp<GrTextureProxy> proxies[2];
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500466 if (!init_test_textures(proxyProvider, &random, proxies)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400467 ERRORF(reporter, "Could not create test textures");
468 return;
469 }
470 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
471
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500472 auto inputTexture = make_input_texture(proxyProvider, kRenderSize, kRenderSize);
Brian Salomon0e05a822017-07-25 09:43:22 -0400473 std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
474 std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
475 auto readInfo = SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
476 kPremul_SkAlphaType);
477
478 // Because processor factories configure themselves in random ways, this is not exhaustive.
479 for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) {
480 static constexpr int kTimesToInvokeFactory = 10;
481 for (int j = 0; j < kTimesToInvokeFactory; ++j) {
482 auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &testData);
483 auto clone = fp->clone();
Brian Salomon0e05a822017-07-25 09:43:22 -0400484 if (!clone) {
Brian Salomon96271cd2017-07-31 16:27:23 -0400485 ERRORF(reporter, "Clone of processor %s failed.", fp->name());
Brian Salomon0e05a822017-07-25 09:43:22 -0400486 continue;
487 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400488 const char* name = fp->name();
Robert Phillips6be756b2018-01-16 15:07:54 -0500489 if (!fp->instantiate(resourceProvider) || !clone->instantiate(resourceProvider)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400490 continue;
491 }
Brian Salomonce06e262017-08-01 16:23:40 -0400492 REPORTER_ASSERT(reporter, !strcmp(fp->name(), clone->name()));
493 REPORTER_ASSERT(reporter, fp->compatibleWithCoverageAsAlpha() ==
494 clone->compatibleWithCoverageAsAlpha());
495 REPORTER_ASSERT(reporter, fp->isEqual(*clone));
496 REPORTER_ASSERT(reporter, fp->preservesOpaqueInput() == clone->preservesOpaqueInput());
497 REPORTER_ASSERT(reporter, fp->hasConstantOutputForConstantInput() ==
498 clone->hasConstantOutputForConstantInput());
499 REPORTER_ASSERT(reporter, fp->numChildProcessors() == clone->numChildProcessors());
500 REPORTER_ASSERT(reporter, fp->usesLocalCoords() == clone->usesLocalCoords());
Brian Salomon0e05a822017-07-25 09:43:22 -0400501 // Draw with original and read back the results.
Robert Phillips7c525e62018-06-12 10:11:12 -0400502 test_draw_op(context, rtc.get(), std::move(fp), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400503 memset(readData1.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
504 rtc->readPixels(readInfo, readData1.get(), 0, 0, 0);
505
506 // Draw with clone and read back the results.
Robert Phillips7c525e62018-06-12 10:11:12 -0400507 test_draw_op(context, rtc.get(), std::move(clone), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400508 memset(readData2.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
509 rtc->readPixels(readInfo, readData2.get(), 0, 0, 0);
510
511 // Check that the results are the same.
512 bool passing = true;
513 for (int y = 0; y < kRenderSize && passing; ++y) {
514 for (int x = 0; x < kRenderSize && passing; ++x) {
515 int idx = y * kRenderSize + x;
516 if (readData1[idx] != readData2[idx]) {
517 ERRORF(reporter,
518 "Processor %s made clone produced different output. "
519 "Input color: 0x%08x, Original Output Color: 0x%08x, "
520 "Clone Output Color: 0x%08x..",
Brian Salomonaff329b2017-08-11 09:40:37 -0400521 name, input_texel_color(x, y), readData1[idx], readData2[idx]);
Brian Salomon0e05a822017-07-25 09:43:22 -0400522 passing = false;
523 }
524 }
525 }
526 }
527 }
528}
529
Hal Canary6f6961e2017-01-31 13:50:44 -0500530#endif // GR_TEST_UTILS
531#endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS