blob: 9b7e93bc5656086e6b7f7cf1ac05b41347d5d1e0 [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 Salomonac70f842017-05-08 10:43:33 -040011#include <random>
Brian Salomonc65aec92017-03-09 09:03:58 -050012#include "GrClip.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050013#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050014#include "GrContextPriv.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050015#include "GrGpuResource.h"
Robert Phillips7c525e62018-06-12 10:11:12 -040016#include "GrMemoryPool.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050017#include "GrProxyProvider.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050018#include "GrRenderTargetContext.h"
19#include "GrRenderTargetContextPriv.h"
20#include "GrResourceProvider.h"
21#include "glsl/GrGLSLFragmentProcessor.h"
22#include "glsl/GrGLSLFragmentShaderBuilder.h"
Brian Salomon82ddc942017-07-14 12:00:13 -040023#include "ops/GrMeshDrawOp.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040024#include "ops/GrRectOpFactory.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050025
26namespace {
Brian Salomon82ddc942017-07-14 12:00:13 -040027class TestOp : public GrMeshDrawOp {
Brian Salomonbc6b99d2017-01-11 10:32:34 -050028public:
29 DEFINE_OP_CLASS_ID
Robert Phillips7c525e62018-06-12 10:11:12 -040030 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
31 std::unique_ptr<GrFragmentProcessor> fp) {
Robert Phillipsc994a932018-06-19 13:09:54 -040032 GrOpMemoryPool* pool = context->contextPriv().opMemoryPool();
33
34 return pool->allocate<TestOp>(std::move(fp));
Brian Salomon82ddc942017-07-14 12:00:13 -040035 }
36
Robert Phillips5f567c72017-09-14 08:27:37 -040037 const char* name() const override { return "TestOp"; }
38
Robert Phillipsf1748f52017-09-14 14:11:24 -040039 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillips5f567c72017-09-14 08:27:37 -040040 fProcessors.visitProxies(func);
41 }
42
Brian Salomon82ddc942017-07-14 12:00:13 -040043 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
44
Brian Osman532b3f92018-07-11 10:02:07 -040045 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
Brian Salomon82ddc942017-07-14 12:00:13 -040046 static constexpr GrProcessorAnalysisColor kUnknownColor;
47 GrColor overrideColor;
48 fProcessors.finalize(kUnknownColor, GrProcessorAnalysisCoverage::kNone, clip, false, caps,
Brian Osman532b3f92018-07-11 10:02:07 -040049 &overrideColor);
Brian Salomon82ddc942017-07-14 12:00:13 -040050 return RequiresDstTexture::kNo;
Brian Salomon649a3412017-03-09 13:50:43 -050051 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050052
53private:
Robert Phillips7c525e62018-06-12 10:11:12 -040054 friend class ::GrOpMemoryPool; // for ctor
55
Brian Salomonaff329b2017-08-11 09:40:37 -040056 TestOp(std::unique_ptr<GrFragmentProcessor> fp)
57 : INHERITED(ClassID()), fProcessors(std::move(fp)) {
Brian Salomon82ddc942017-07-14 12:00:13 -040058 this->setBounds(SkRect::MakeWH(100, 100), HasAABloat::kNo, IsZeroArea::kNo);
59 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050060
Brian Salomon91326c32017-08-09 16:02:19 -040061 void onPrepareDraws(Target* target) override { return; }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050062
Brian Salomon82ddc942017-07-14 12:00:13 -040063 bool onCombineIfPossible(GrOp* op, const GrCaps& caps) override { return false; }
64
65 GrProcessorSet fProcessors;
66
67 typedef GrMeshDrawOp INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -050068};
69
70/**
71 * FP used to test ref/IO counts on owned GrGpuResources. Can also be a parent FP to test counts
72 * of resources owned by child FPs.
73 */
74class TestFP : public GrFragmentProcessor {
75public:
Brian Salomonaff329b2017-08-11 09:40:37 -040076 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> child) {
77 return std::unique_ptr<GrFragmentProcessor>(new TestFP(std::move(child)));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050078 }
Brian Salomonaff329b2017-08-11 09:40:37 -040079 static std::unique_ptr<GrFragmentProcessor> Make(const SkTArray<sk_sp<GrTextureProxy>>& proxies,
Brian Salomon559f5562017-11-15 14:28:33 -050080 const SkTArray<sk_sp<GrBuffer>>& buffers) {
81 return std::unique_ptr<GrFragmentProcessor>(new TestFP(proxies, buffers));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050082 }
83
84 const char* name() const override { return "test"; }
85
86 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
87 // We don't really care about reusing these.
88 static int32_t gKey = 0;
89 b->add32(sk_atomic_inc(&gKey));
90 }
91
Brian Salomonaff329b2017-08-11 09:40:37 -040092 std::unique_ptr<GrFragmentProcessor> clone() const override {
93 return std::unique_ptr<GrFragmentProcessor>(new TestFP(*this));
Brian Salomonb17e6392017-07-28 13:41:51 -040094 }
95
Brian Salomonbc6b99d2017-01-11 10:32:34 -050096private:
Brian Salomon559f5562017-11-15 14:28:33 -050097 TestFP(const SkTArray<sk_sp<GrTextureProxy>>& proxies, const SkTArray<sk_sp<GrBuffer>>& buffers)
Brian Salomon662ea4b2018-07-12 14:53:49 -040098 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4) {
Robert Phillips30f9bc62017-02-22 15:28:38 -050099 for (const auto& proxy : proxies) {
Brian Salomonf7dcd762018-07-30 14:48:15 -0400100 fSamplers.emplace_back(proxy);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500101 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400102 this->setTextureSamplerCnt(fSamplers.count());
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500103 }
104
Brian Salomonaff329b2017-08-11 09:40:37 -0400105 TestFP(std::unique_ptr<GrFragmentProcessor> child)
Brian Salomon662ea4b2018-07-12 14:53:49 -0400106 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4) {
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500107 this->registerChildProcessor(std::move(child));
108 }
109
Brian Salomon96271cd2017-07-31 16:27:23 -0400110 explicit TestFP(const TestFP& that)
Brian Salomon662ea4b2018-07-12 14:53:49 -0400111 : INHERITED(kTestFP_ClassID, that.optimizationFlags()), fSamplers(4) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400112 for (int i = 0; i < that.fSamplers.count(); ++i) {
113 fSamplers.emplace_back(that.fSamplers[i]);
Brian Salomonb17e6392017-07-28 13:41:51 -0400114 }
Brian Salomon96271cd2017-07-31 16:27:23 -0400115 for (int i = 0; i < that.numChildProcessors(); ++i) {
116 this->registerChildProcessor(that.childProcessor(i).clone());
Brian Salomonb17e6392017-07-28 13:41:51 -0400117 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400118 this->setTextureSamplerCnt(fSamplers.count());
Brian Salomonb17e6392017-07-28 13:41:51 -0400119 }
120
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500121 virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
122 class TestGLSLFP : public GrGLSLFragmentProcessor {
123 public:
124 TestGLSLFP() {}
125 void emitCode(EmitArgs& args) override {
126 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
127 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor);
128 }
129
130 private:
131 };
132 return new TestGLSLFP();
133 }
134
135 bool onIsEqual(const GrFragmentProcessor&) const override { return false; }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400136 const TextureSampler& onTextureSampler(int i) const override { return fSamplers[i]; }
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500137
138 GrTAllocator<TextureSampler> fSamplers;
Brian Salomon587e08f2017-01-27 10:59:27 -0500139 typedef GrFragmentProcessor INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500140};
141}
142
Brian Salomonf046e152017-01-11 15:24:47 -0500143template <typename T>
144inline void testingOnly_getIORefCnts(const T* resource, int* refCnt, int* readCnt, int* writeCnt) {
145 *refCnt = resource->fRefCnt;
146 *readCnt = resource->fPendingReads;
147 *writeCnt = resource->fPendingWrites;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500148}
149
Robert Phillips2f493142017-03-02 18:18:38 -0500150void testingOnly_getIORefCnts(GrTextureProxy* proxy, int* refCnt, int* readCnt, int* writeCnt) {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500151 *refCnt = proxy->getBackingRefCnt_TestOnly();
152 *readCnt = proxy->getPendingReadCnt_TestOnly();
153 *writeCnt = proxy->getPendingWriteCnt_TestOnly();
154}
155
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500156DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) {
157 GrContext* context = ctxInfo.grContext();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500158 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500159
Robert Phillips16d8ec62017-07-27 16:16:25 -0400160 GrSurfaceDesc desc;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500161 desc.fWidth = 10;
162 desc.fHeight = 10;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400163 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500164
Brian Salomonb17e6392017-07-28 13:41:51 -0400165 for (bool makeClone : {false, true}) {
166 for (int parentCnt = 0; parentCnt < 2; parentCnt++) {
167 sk_sp<GrRenderTargetContext> renderTargetContext(
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500168 context->contextPriv().makeDeferredRenderTargetContext(
169 SkBackingFit::kApprox, 1, 1,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500170 kRGBA_8888_GrPixelConfig, nullptr));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500171 {
Brian Salomon2a4f9832018-03-03 22:43:43 -0500172 sk_sp<GrTextureProxy> proxy1 = proxyProvider->createProxy(
173 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
174 sk_sp<GrTextureProxy> proxy2 = proxyProvider->createProxy(
175 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
176 sk_sp<GrTextureProxy> proxy3 = proxyProvider->createProxy(
177 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
178 sk_sp<GrTextureProxy> proxy4 = proxyProvider->createProxy(
179 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
Brian Salomonb17e6392017-07-28 13:41:51 -0400180 {
181 SkTArray<sk_sp<GrTextureProxy>> proxies;
182 SkTArray<sk_sp<GrBuffer>> buffers;
Brian Salomonb17e6392017-07-28 13:41:51 -0400183 proxies.push_back(proxy1);
Brian Salomon559f5562017-11-15 14:28:33 -0500184 auto fp = TestFP::Make(std::move(proxies), std::move(buffers));
Brian Salomonb17e6392017-07-28 13:41:51 -0400185 for (int i = 0; i < parentCnt; ++i) {
186 fp = TestFP::Make(std::move(fp));
187 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400188 std::unique_ptr<GrFragmentProcessor> clone;
Brian Salomonb17e6392017-07-28 13:41:51 -0400189 if (makeClone) {
190 clone = fp->clone();
191 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400192 std::unique_ptr<GrDrawOp> op(TestOp::Make(context, std::move(fp)));
Brian Salomonb17e6392017-07-28 13:41:51 -0400193 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
194 if (clone) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400195 op = TestOp::Make(context, std::move(clone));
Brian Salomonb17e6392017-07-28 13:41:51 -0400196 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
197 }
198 }
199 int refCnt, readCnt, writeCnt;
200
201 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
202 // IO counts should be double if there is a clone of the FP.
203 int ioRefMul = makeClone ? 2 : 1;
Robert Phillips715d08c2018-07-18 13:56:48 -0400204 REPORTER_ASSERT(reporter, -1 == refCnt);
Brian Salomonb17e6392017-07-28 13:41:51 -0400205 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
206 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
207
Brian Salomonb17e6392017-07-28 13:41:51 -0400208 context->flush();
209
210 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
211 REPORTER_ASSERT(reporter, 1 == refCnt);
212 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
213 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
214
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500215 }
216 }
217 }
218}
Brian Salomon587e08f2017-01-27 10:59:27 -0500219
220// This test uses the random GrFragmentProcessor test factory, which relies on static initializers.
221#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
222
Brian Salomon0e05a822017-07-25 09:43:22 -0400223#include "SkCommandLineFlags.h"
224DEFINE_bool(randomProcessorTest, false, "Use non-deterministic seed for random processor tests?");
225
226#if GR_TEST_UTILS
227
228static GrColor input_texel_color(int i, int j) {
229 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 -0500230 return GrPremulColor(color);
231}
232
Brian Salomon0e05a822017-07-25 09:43:22 -0400233static GrColor4f input_texel_color4f(int i, int j) {
234 return GrColor4f::FromGrColor(input_texel_color(i, j));
235}
Brian Salomon587e08f2017-01-27 10:59:27 -0500236
Robert Phillips7c525e62018-06-12 10:11:12 -0400237void test_draw_op(GrContext* context,
238 GrRenderTargetContext* rtc,
239 std::unique_ptr<GrFragmentProcessor> fp,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500240 sk_sp<GrTextureProxy> inputDataProxy) {
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500241 GrPaint paint;
Brian Osman2240be92017-10-18 13:15:13 -0400242 paint.addColorTextureProcessor(std::move(inputDataProxy), SkMatrix::I());
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500243 paint.addColorFragmentProcessor(std::move(fp));
244 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonac70f842017-05-08 10:43:33 -0400245
Robert Phillips7c525e62018-06-12 10:11:12 -0400246 auto op = GrRectOpFactory::MakeNonAAFill(context, std::move(paint), SkMatrix::I(),
Brian Salomonbaaf4392017-06-15 09:59:23 -0400247 SkRect::MakeWH(rtc->width(), rtc->height()),
248 GrAAType::kNone);
Brian Salomonac70f842017-05-08 10:43:33 -0400249 rtc->addDrawOp(GrNoClip(), std::move(op));
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500250}
251
Brian Salomon0e05a822017-07-25 09:43:22 -0400252/** Initializes the two test texture proxies that are available to the FP test factories. */
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500253bool init_test_textures(GrProxyProvider* proxyProvider, SkRandom* random,
254 sk_sp<GrTextureProxy> proxies[2]) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400255 static const int kTestTextureSize = 256;
256 GrSurfaceDesc desc;
Brian Salomon0e05a822017-07-25 09:43:22 -0400257 desc.fWidth = kTestTextureSize;
258 desc.fHeight = kTestTextureSize;
259 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400260
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500261 {
262 // Put premul data into the RGBA texture that the test FPs can optionally use.
263 std::unique_ptr<GrColor[]> rgbaData(new GrColor[kTestTextureSize * kTestTextureSize]);
264 for (int y = 0; y < kTestTextureSize; ++y) {
265 for (int x = 0; x < kTestTextureSize; ++x) {
266 rgbaData[kTestTextureSize * y + x] =
267 input_texel_color(random->nextULessThan(256), random->nextULessThan(256));
268 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400269 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400270
Brian Salomon58389b92018-03-07 13:01:25 -0500271 proxies[0] = proxyProvider->createTextureProxy(desc, SkBudgeted::kYes, rgbaData.get(),
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500272 kTestTextureSize * sizeof(GrColor));
Brian Salomon0e05a822017-07-25 09:43:22 -0400273 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500274
275 {
276 // Put random values into the alpha texture that the test FPs can optionally use.
277 desc.fConfig = kAlpha_8_GrPixelConfig;
278 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 Salomon58389b92018-03-07 13:01:25 -0500285 proxies[1] = proxyProvider->createTextureProxy(desc, SkBudgeted::kYes, alphaData.get(),
Brian Salomon2a4f9832018-03-03 22:43:43 -0500286 kTestTextureSize);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500287 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400288
289 return proxies[0] && proxies[1];
290}
291
292// Creates a texture of premul colors used as the output of the fragment processor that precedes
293// the fragment processor under test. Color values are those provided by input_texel_color().
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500294sk_sp<GrTextureProxy> make_input_texture(GrProxyProvider* proxyProvider, int width, int height) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400295 std::unique_ptr<GrColor[]> data(new GrColor[width * height]);
296 for (int y = 0; y < width; ++y) {
297 for (int x = 0; x < height; ++x) {
298 data.get()[width * y + x] = input_texel_color(x, y);
299 }
300 }
301 GrSurfaceDesc desc;
Brian Salomon0e05a822017-07-25 09:43:22 -0400302 desc.fWidth = width;
303 desc.fHeight = height;
304 desc.fConfig = kRGBA_8888_GrPixelConfig;
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500305
Brian Salomon58389b92018-03-07 13:01:25 -0500306 return proxyProvider->createTextureProxy(desc, SkBudgeted::kYes, data.get(),
307 width * sizeof(GrColor));
Brian Salomon0e05a822017-07-25 09:43:22 -0400308}
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500309
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500310DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500311 GrContext* context = ctxInfo.grContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500312 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500313 auto resourceProvider = context->contextPriv().resourceProvider();
Brian Salomon1c053642017-07-24 10:16:19 -0400314 using FPFactory = GrFragmentProcessorTestFactory;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400315
316 uint32_t seed = 0;
317 if (FLAGS_randomProcessorTest) {
318 std::random_device rd;
319 seed = rd();
320 }
321 // If a non-deterministic bot fails this test, check the output to see what seed it used, then
322 // hard-code that value here:
323 SkRandom random(seed);
324
Brian Salomon0e05a822017-07-25 09:43:22 -0400325 // Make the destination context for the test.
326 static constexpr int kRenderSize = 256;
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500327 sk_sp<GrRenderTargetContext> rtc = context->contextPriv().makeDeferredRenderTargetContext(
Brian Salomon0e05a822017-07-25 09:43:22 -0400328 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500329
Robert Phillipse78b7252017-04-06 07:59:41 -0400330 sk_sp<GrTextureProxy> proxies[2];
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500331 if (!init_test_textures(proxyProvider, &random, proxies)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400332 ERRORF(reporter, "Could not create test textures");
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400333 return;
334 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400335 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
Brian Salomon587e08f2017-01-27 10:59:27 -0500336
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500337 auto inputTexture = make_input_texture(proxyProvider, kRenderSize, kRenderSize);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500338
Ben Wagner8c791942017-07-25 11:47:48 -0400339 std::unique_ptr<GrColor[]> readData(new GrColor[kRenderSize * kRenderSize]);
Brian Salomon0e05a822017-07-25 09:43:22 -0400340 // Because processor factories configure themselves in random ways, this is not exhaustive.
Brian Salomon587e08f2017-01-27 10:59:27 -0500341 for (int i = 0; i < FPFactory::Count(); ++i) {
342 int timesToInvokeFactory = 5;
343 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
344 // on child optimizations being present.
Brian Salomonaff329b2017-08-11 09:40:37 -0400345 std::unique_ptr<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
Brian Salomon587e08f2017-01-27 10:59:27 -0500346 for (int j = 0; j < fp->numChildProcessors(); ++j) {
347 // This value made a reasonable trade off between time and coverage when this test was
348 // written.
349 timesToInvokeFactory *= FPFactory::Count() / 2;
350 }
351 for (int j = 0; j < timesToInvokeFactory; ++j) {
352 fp = FPFactory::MakeIdx(i, &testData);
Robert Phillips6be756b2018-01-16 15:07:54 -0500353 if (!fp->instantiate(resourceProvider)) {
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400354 continue;
355 }
356
Brian Salomon587e08f2017-01-27 10:59:27 -0500357 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
Brian Salomonf3b995b2017-02-15 10:22:23 -0500358 !fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500359 continue;
360 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400361
362 // Since we transfer away ownership of the original FP, we make a clone.
363 auto clone = fp->clone();
364
Robert Phillips7c525e62018-06-12 10:11:12 -0400365 test_draw_op(context, rtc.get(), std::move(fp), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400366 memset(readData.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
367 rtc->readPixels(SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
368 kPremul_SkAlphaType),
369 readData.get(), 0, 0, 0);
Brian Salomon587e08f2017-01-27 10:59:27 -0500370 bool passing = true;
371 if (0) { // Useful to see what FPs are being tested.
372 SkString children;
Brian Salomonaff329b2017-08-11 09:40:37 -0400373 for (int c = 0; c < clone->numChildProcessors(); ++c) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500374 if (!c) {
375 children.append("(");
376 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400377 children.append(clone->name());
378 children.append(c == clone->numChildProcessors() - 1 ? ")" : ", ");
Brian Salomon587e08f2017-01-27 10:59:27 -0500379 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400380 SkDebugf("%s %s\n", clone->name(), children.c_str());
Brian Salomon587e08f2017-01-27 10:59:27 -0500381 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400382 for (int y = 0; y < kRenderSize && passing; ++y) {
383 for (int x = 0; x < kRenderSize && passing; ++x) {
384 GrColor input = input_texel_color(x, y);
385 GrColor output = readData.get()[y * kRenderSize + x];
Brian Salomonaff329b2017-08-11 09:40:37 -0400386 if (clone->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500387 // A modulating processor is allowed to modulate either the input color or
388 // just the input alpha.
389 bool legalColorModulation =
390 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
391 GrColorUnpackR(output) <= GrColorUnpackR(input) &&
392 GrColorUnpackG(output) <= GrColorUnpackG(input) &&
393 GrColorUnpackB(output) <= GrColorUnpackB(input);
394 bool legalAlphaModulation =
395 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
396 GrColorUnpackR(output) <= GrColorUnpackA(input) &&
397 GrColorUnpackG(output) <= GrColorUnpackA(input) &&
398 GrColorUnpackB(output) <= GrColorUnpackA(input);
399 if (!legalColorModulation && !legalAlphaModulation) {
400 ERRORF(reporter,
401 "\"Modulating\" processor %s made color/alpha value larger. "
Greg Daniel10ed2432017-12-01 16:19:43 -0500402 "Input: 0x%08x, Output: 0x%08x, pixel (%d, %d).",
403 clone->name(), input, output, x, y);
Brian Salomon587e08f2017-01-27 10:59:27 -0500404 passing = false;
405 }
406 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400407 GrColor4f input4f = input_texel_color4f(x, y);
Brian Salomon587e08f2017-01-27 10:59:27 -0500408 GrColor4f output4f = GrColor4f::FromGrColor(output);
409 GrColor4f expected4f;
Brian Salomonaff329b2017-08-11 09:40:37 -0400410 if (clone->hasConstantOutputForConstantInput(input4f, &expected4f)) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500411 float rDiff = fabsf(output4f.fRGBA[0] - expected4f.fRGBA[0]);
412 float gDiff = fabsf(output4f.fRGBA[1] - expected4f.fRGBA[1]);
413 float bDiff = fabsf(output4f.fRGBA[2] - expected4f.fRGBA[2]);
414 float aDiff = fabsf(output4f.fRGBA[3] - expected4f.fRGBA[3]);
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500415 static constexpr float kTol = 4 / 255.f;
Brian Salomon587e08f2017-01-27 10:59:27 -0500416 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
417 ERRORF(reporter,
418 "Processor %s claimed output for const input doesn't match "
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500419 "actual output. Error: %f, Tolerance: %f, input: (%f, %f, %f, "
420 "%f), actual: (%f, %f, %f, %f), expected(%f, %f, %f, %f)",
Brian Salomonf61711a2018-02-16 10:44:28 -0500421 clone->name(),
422 SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))), kTol,
423 input4f.fRGBA[0], input4f.fRGBA[1], input4f.fRGBA[2],
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500424 input4f.fRGBA[3], output4f.fRGBA[0], output4f.fRGBA[1],
425 output4f.fRGBA[2], output4f.fRGBA[3], expected4f.fRGBA[0],
426 expected4f.fRGBA[1], expected4f.fRGBA[2], expected4f.fRGBA[3]);
Brian Salomon587e08f2017-01-27 10:59:27 -0500427 passing = false;
428 }
429 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400430 if (GrColorIsOpaque(input) && clone->preservesOpaqueInput() &&
Brian Salomon587e08f2017-01-27 10:59:27 -0500431 !GrColorIsOpaque(output)) {
432 ERRORF(reporter,
433 "Processor %s claimed opaqueness is preserved but it is not. Input: "
Brian Osman2f3865b2017-03-15 13:35:51 -0400434 "0x%08x, Output: 0x%08x.",
Brian Salomonaff329b2017-08-11 09:40:37 -0400435 clone->name(), input, output);
Brian Salomon587e08f2017-01-27 10:59:27 -0500436 passing = false;
437 }
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400438 if (!passing) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400439 ERRORF(reporter, "Seed: 0x%08x, Processor details: %s", seed,
440 clone->dumpInfo().c_str());
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400441 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500442 }
443 }
444 }
445 }
446}
Robert Phillips18166ee2017-06-01 12:55:44 -0400447
Brian Salomon0e05a822017-07-25 09:43:22 -0400448// Tests that fragment processors returned by GrFragmentProcessor::clone() are equivalent to their
449// progenitors.
450DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) {
451 GrContext* context = ctxInfo.grContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500452 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500453 auto resourceProvider = context->contextPriv().resourceProvider();
Brian Salomon0e05a822017-07-25 09:43:22 -0400454
455 SkRandom random;
456
457 // Make the destination context for the test.
458 static constexpr int kRenderSize = 1024;
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500459 sk_sp<GrRenderTargetContext> rtc = context->contextPriv().makeDeferredRenderTargetContext(
Brian Salomon0e05a822017-07-25 09:43:22 -0400460 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
461
462 sk_sp<GrTextureProxy> proxies[2];
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500463 if (!init_test_textures(proxyProvider, &random, proxies)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400464 ERRORF(reporter, "Could not create test textures");
465 return;
466 }
467 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
468
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500469 auto inputTexture = make_input_texture(proxyProvider, kRenderSize, kRenderSize);
Brian Salomon0e05a822017-07-25 09:43:22 -0400470 std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
471 std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
472 auto readInfo = SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
473 kPremul_SkAlphaType);
474
475 // Because processor factories configure themselves in random ways, this is not exhaustive.
476 for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) {
477 static constexpr int kTimesToInvokeFactory = 10;
478 for (int j = 0; j < kTimesToInvokeFactory; ++j) {
479 auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &testData);
480 auto clone = fp->clone();
Brian Salomon0e05a822017-07-25 09:43:22 -0400481 if (!clone) {
Brian Salomon96271cd2017-07-31 16:27:23 -0400482 ERRORF(reporter, "Clone of processor %s failed.", fp->name());
Brian Salomon0e05a822017-07-25 09:43:22 -0400483 continue;
484 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400485 const char* name = fp->name();
Robert Phillips6be756b2018-01-16 15:07:54 -0500486 if (!fp->instantiate(resourceProvider) || !clone->instantiate(resourceProvider)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400487 continue;
488 }
Brian Salomonce06e262017-08-01 16:23:40 -0400489 REPORTER_ASSERT(reporter, !strcmp(fp->name(), clone->name()));
490 REPORTER_ASSERT(reporter, fp->compatibleWithCoverageAsAlpha() ==
491 clone->compatibleWithCoverageAsAlpha());
492 REPORTER_ASSERT(reporter, fp->isEqual(*clone));
493 REPORTER_ASSERT(reporter, fp->preservesOpaqueInput() == clone->preservesOpaqueInput());
494 REPORTER_ASSERT(reporter, fp->hasConstantOutputForConstantInput() ==
495 clone->hasConstantOutputForConstantInput());
496 REPORTER_ASSERT(reporter, fp->numChildProcessors() == clone->numChildProcessors());
497 REPORTER_ASSERT(reporter, fp->usesLocalCoords() == clone->usesLocalCoords());
Brian Salomon0e05a822017-07-25 09:43:22 -0400498 // Draw with original and read back the results.
Robert Phillips7c525e62018-06-12 10:11:12 -0400499 test_draw_op(context, rtc.get(), std::move(fp), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400500 memset(readData1.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
501 rtc->readPixels(readInfo, readData1.get(), 0, 0, 0);
502
503 // Draw with clone and read back the results.
Robert Phillips7c525e62018-06-12 10:11:12 -0400504 test_draw_op(context, rtc.get(), std::move(clone), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400505 memset(readData2.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
506 rtc->readPixels(readInfo, readData2.get(), 0, 0, 0);
507
508 // Check that the results are the same.
509 bool passing = true;
510 for (int y = 0; y < kRenderSize && passing; ++y) {
511 for (int x = 0; x < kRenderSize && passing; ++x) {
512 int idx = y * kRenderSize + x;
513 if (readData1[idx] != readData2[idx]) {
514 ERRORF(reporter,
515 "Processor %s made clone produced different output. "
516 "Input color: 0x%08x, Original Output Color: 0x%08x, "
517 "Clone Output Color: 0x%08x..",
Brian Salomonaff329b2017-08-11 09:40:37 -0400518 name, input_texel_color(x, y), readData1[idx], readData2[idx]);
Brian Salomon0e05a822017-07-25 09:43:22 -0400519 passing = false;
520 }
521 }
522 }
523 }
524 }
525}
526
Hal Canary6f6961e2017-01-31 13:50:44 -0500527#endif // GR_TEST_UTILS
528#endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS