Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 1 | /* |
| 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 Salomon | c65aec9 | 2017-03-09 09:03:58 -0500 | [diff] [blame] | 11 | #include "GrClip.h" |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 12 | #include "GrContext.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 13 | #include "GrContextPriv.h" |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 14 | #include "GrGpuResource.h" |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 15 | #include "GrMemoryPool.h" |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 16 | #include "GrProxyProvider.h" |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 17 | #include "GrRenderTargetContext.h" |
| 18 | #include "GrRenderTargetContextPriv.h" |
| 19 | #include "GrResourceProvider.h" |
| 20 | #include "glsl/GrGLSLFragmentProcessor.h" |
| 21 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 22 | #include "ops/GrMeshDrawOp.h" |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 23 | #include "ops/GrRectOpFactory.h" |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 24 | #include "TestUtils.h" |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 25 | |
Hal Canary | 8a00144 | 2018-09-19 11:31:27 -0400 | [diff] [blame] | 26 | #include <random> |
| 27 | |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 28 | namespace { |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 29 | class TestOp : public GrMeshDrawOp { |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 30 | public: |
| 31 | DEFINE_OP_CLASS_ID |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 32 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, |
| 33 | std::unique_ptr<GrFragmentProcessor> fp) { |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 34 | GrOpMemoryPool* pool = context->contextPriv().opMemoryPool(); |
| 35 | |
| 36 | return pool->allocate<TestOp>(std::move(fp)); |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 37 | } |
| 38 | |
Robert Phillips | 5f567c7 | 2017-09-14 08:27:37 -0400 | [diff] [blame] | 39 | const char* name() const override { return "TestOp"; } |
| 40 | |
Robert Phillips | f1748f5 | 2017-09-14 14:11:24 -0400 | [diff] [blame] | 41 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | 5f567c7 | 2017-09-14 08:27:37 -0400 | [diff] [blame] | 42 | fProcessors.visitProxies(func); |
| 43 | } |
| 44 | |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 45 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
| 46 | |
Brian Osman | 532b3f9 | 2018-07-11 10:02:07 -0400 | [diff] [blame] | 47 | RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override { |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 48 | static constexpr GrProcessorAnalysisColor kUnknownColor; |
| 49 | GrColor overrideColor; |
| 50 | fProcessors.finalize(kUnknownColor, GrProcessorAnalysisCoverage::kNone, clip, false, caps, |
Brian Osman | 532b3f9 | 2018-07-11 10:02:07 -0400 | [diff] [blame] | 51 | &overrideColor); |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 52 | return RequiresDstTexture::kNo; |
Brian Salomon | 649a341 | 2017-03-09 13:50:43 -0500 | [diff] [blame] | 53 | } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 54 | |
| 55 | private: |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 56 | friend class ::GrOpMemoryPool; // for ctor |
| 57 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 58 | TestOp(std::unique_ptr<GrFragmentProcessor> fp) |
| 59 | : INHERITED(ClassID()), fProcessors(std::move(fp)) { |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 60 | this->setBounds(SkRect::MakeWH(100, 100), HasAABloat::kNo, IsZeroArea::kNo); |
| 61 | } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 62 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 63 | void onPrepareDraws(Target* target) override { return; } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 64 | |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 65 | GrProcessorSet fProcessors; |
| 66 | |
| 67 | typedef GrMeshDrawOp INHERITED; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 68 | }; |
| 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 | */ |
| 74 | class TestFP : public GrFragmentProcessor { |
| 75 | public: |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 76 | static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> child) { |
| 77 | return std::unique_ptr<GrFragmentProcessor>(new TestFP(std::move(child))); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 78 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 79 | static std::unique_ptr<GrFragmentProcessor> Make(const SkTArray<sk_sp<GrTextureProxy>>& proxies, |
Brian Salomon | 559f556 | 2017-11-15 14:28:33 -0500 | [diff] [blame] | 80 | const SkTArray<sk_sp<GrBuffer>>& buffers) { |
| 81 | return std::unique_ptr<GrFragmentProcessor>(new TestFP(proxies, buffers)); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 82 | } |
| 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 Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 92 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 93 | return std::unique_ptr<GrFragmentProcessor>(new TestFP(*this)); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 94 | } |
| 95 | |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 96 | private: |
Brian Salomon | 559f556 | 2017-11-15 14:28:33 -0500 | [diff] [blame] | 97 | TestFP(const SkTArray<sk_sp<GrTextureProxy>>& proxies, const SkTArray<sk_sp<GrBuffer>>& buffers) |
Brian Salomon | 662ea4b | 2018-07-12 14:53:49 -0400 | [diff] [blame] | 98 | : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4) { |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 99 | for (const auto& proxy : proxies) { |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 100 | fSamplers.emplace_back(proxy); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 101 | } |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 102 | this->setTextureSamplerCnt(fSamplers.count()); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 103 | } |
| 104 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 105 | TestFP(std::unique_ptr<GrFragmentProcessor> child) |
Brian Salomon | 662ea4b | 2018-07-12 14:53:49 -0400 | [diff] [blame] | 106 | : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4) { |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 107 | this->registerChildProcessor(std::move(child)); |
| 108 | } |
| 109 | |
Brian Salomon | 96271cd | 2017-07-31 16:27:23 -0400 | [diff] [blame] | 110 | explicit TestFP(const TestFP& that) |
Brian Salomon | 662ea4b | 2018-07-12 14:53:49 -0400 | [diff] [blame] | 111 | : INHERITED(kTestFP_ClassID, that.optimizationFlags()), fSamplers(4) { |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 112 | for (int i = 0; i < that.fSamplers.count(); ++i) { |
| 113 | fSamplers.emplace_back(that.fSamplers[i]); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 114 | } |
Brian Salomon | 96271cd | 2017-07-31 16:27:23 -0400 | [diff] [blame] | 115 | for (int i = 0; i < that.numChildProcessors(); ++i) { |
| 116 | this->registerChildProcessor(that.childProcessor(i).clone()); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 117 | } |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 118 | this->setTextureSamplerCnt(fSamplers.count()); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 119 | } |
| 120 | |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 121 | 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 Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 136 | const TextureSampler& onTextureSampler(int i) const override { return fSamplers[i]; } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 137 | |
| 138 | GrTAllocator<TextureSampler> fSamplers; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 139 | typedef GrFragmentProcessor INHERITED; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 140 | }; |
| 141 | } |
| 142 | |
Brian Salomon | f046e15 | 2017-01-11 15:24:47 -0500 | [diff] [blame] | 143 | template <typename T> |
| 144 | inline 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 Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 148 | } |
| 149 | |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 150 | void testingOnly_getIORefCnts(GrTextureProxy* proxy, int* refCnt, int* readCnt, int* writeCnt) { |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 151 | *refCnt = proxy->getBackingRefCnt_TestOnly(); |
| 152 | *readCnt = proxy->getPendingReadCnt_TestOnly(); |
| 153 | *writeCnt = proxy->getPendingWriteCnt_TestOnly(); |
| 154 | } |
| 155 | |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 156 | DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) { |
| 157 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 158 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 159 | |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 160 | GrSurfaceDesc desc; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 161 | desc.fWidth = 10; |
| 162 | desc.fHeight = 10; |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 163 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 164 | |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 165 | for (bool makeClone : {false, true}) { |
| 166 | for (int parentCnt = 0; parentCnt < 2; parentCnt++) { |
| 167 | sk_sp<GrRenderTargetContext> renderTargetContext( |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 168 | context->contextPriv().makeDeferredRenderTargetContext( |
| 169 | SkBackingFit::kApprox, 1, 1, |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 170 | kRGBA_8888_GrPixelConfig, nullptr)); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 171 | { |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 172 | 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 Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 180 | { |
| 181 | SkTArray<sk_sp<GrTextureProxy>> proxies; |
| 182 | SkTArray<sk_sp<GrBuffer>> buffers; |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 183 | proxies.push_back(proxy1); |
Brian Salomon | 559f556 | 2017-11-15 14:28:33 -0500 | [diff] [blame] | 184 | auto fp = TestFP::Make(std::move(proxies), std::move(buffers)); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 185 | for (int i = 0; i < parentCnt; ++i) { |
| 186 | fp = TestFP::Make(std::move(fp)); |
| 187 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 188 | std::unique_ptr<GrFragmentProcessor> clone; |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 189 | if (makeClone) { |
| 190 | clone = fp->clone(); |
| 191 | } |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 192 | std::unique_ptr<GrDrawOp> op(TestOp::Make(context, std::move(fp))); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 193 | renderTargetContext->priv().testingOnly_addDrawOp(std::move(op)); |
| 194 | if (clone) { |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 195 | op = TestOp::Make(context, std::move(clone)); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 196 | 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 Phillips | 715d08c | 2018-07-18 13:56:48 -0400 | [diff] [blame] | 204 | REPORTER_ASSERT(reporter, -1 == refCnt); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 205 | REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt); |
| 206 | REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt); |
| 207 | |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 208 | 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 Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | } |
| 218 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 219 | |
| 220 | // This test uses the random GrFragmentProcessor test factory, which relies on static initializers. |
| 221 | #if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 222 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 223 | #include "SkCommandLineFlags.h" |
| 224 | DEFINE_bool(randomProcessorTest, false, "Use non-deterministic seed for random processor tests?"); |
Michael Ludwig | 3ad86d0 | 2018-09-28 16:00:38 -0400 | [diff] [blame] | 225 | DEFINE_uint32(processorSeed, 0, "Use specific seed for processor tests. Overridden by " \ |
| 226 | "--randomProcessorTest."); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 227 | |
| 228 | #if GR_TEST_UTILS |
| 229 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 230 | static GrColor input_texel_color(int i, int j, SkScalar delta) { |
| 231 | // Delta must be less than 0.5 to prevent over/underflow issues with the input color |
| 232 | SkASSERT(delta <= 0.5); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 233 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 234 | GrColor color = GrColorPackRGBA((uint8_t)j, (uint8_t)(i + j), (uint8_t)(2 * j - i), (uint8_t)i); |
| 235 | GrColor4f color4f = GrColor4f::FromGrColor(color); |
| 236 | for (int i = 0; i < 4; i++) { |
| 237 | if (color4f.fRGBA[i] > 0.5) { |
| 238 | color4f.fRGBA[i] -= delta; |
| 239 | } else { |
| 240 | color4f.fRGBA[i] += delta; |
| 241 | } |
| 242 | } |
| 243 | return color4f.premul().toGrColor(); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 244 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 245 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 246 | void test_draw_op(GrContext* context, |
| 247 | GrRenderTargetContext* rtc, |
| 248 | std::unique_ptr<GrFragmentProcessor> fp, |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 249 | sk_sp<GrTextureProxy> inputDataProxy) { |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 250 | GrPaint paint; |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 251 | paint.addColorTextureProcessor(std::move(inputDataProxy), SkMatrix::I()); |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 252 | paint.addColorFragmentProcessor(std::move(fp)); |
| 253 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 254 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 255 | auto op = GrRectOpFactory::MakeNonAAFill(context, std::move(paint), SkMatrix::I(), |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 256 | SkRect::MakeWH(rtc->width(), rtc->height()), |
| 257 | GrAAType::kNone); |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 258 | rtc->addDrawOp(GrNoClip(), std::move(op)); |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 259 | } |
| 260 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 261 | // This assumes that the output buffer will be the same size as inputDataProxy |
| 262 | void render_fp(GrContext* context, GrRenderTargetContext* rtc, GrFragmentProcessor* fp, |
| 263 | sk_sp<GrTextureProxy> inputDataProxy, GrColor* buffer) { |
| 264 | int width = inputDataProxy->width(); |
| 265 | int height = inputDataProxy->height(); |
| 266 | |
| 267 | // test_draw_op needs to take ownership of an FP, so give it a clone that it can own |
| 268 | test_draw_op(context, rtc, fp->clone(), inputDataProxy); |
| 269 | memset(buffer, 0x0, sizeof(GrColor) * width * height); |
| 270 | rtc->readPixels(SkImageInfo::Make(width, height, kRGBA_8888_SkColorType, |
| 271 | kPremul_SkAlphaType), |
| 272 | buffer, 0, 0, 0); |
| 273 | } |
| 274 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 275 | /** Initializes the two test texture proxies that are available to the FP test factories. */ |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 276 | bool init_test_textures(GrProxyProvider* proxyProvider, SkRandom* random, |
| 277 | sk_sp<GrTextureProxy> proxies[2]) { |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 278 | static const int kTestTextureSize = 256; |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 279 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 280 | { |
| 281 | // Put premul data into the RGBA texture that the test FPs can optionally use. |
| 282 | std::unique_ptr<GrColor[]> rgbaData(new GrColor[kTestTextureSize * kTestTextureSize]); |
| 283 | for (int y = 0; y < kTestTextureSize; ++y) { |
| 284 | for (int x = 0; x < kTestTextureSize; ++x) { |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 285 | rgbaData[kTestTextureSize * y + x] = input_texel_color( |
| 286 | random->nextULessThan(256), random->nextULessThan(256), 0.0f); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 287 | } |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 288 | } |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 289 | |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 290 | SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize, |
| 291 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 292 | SkPixmap pixmap(ii, rgbaData.get(), ii.minRowBytes()); |
| 293 | sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap); |
| 294 | proxies[0] = proxyProvider->createTextureProxy(img, kNone_GrSurfaceFlags, 1, |
| 295 | SkBudgeted::kYes, SkBackingFit::kExact); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 296 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 297 | |
| 298 | { |
| 299 | // Put random values into the alpha texture that the test FPs can optionally use. |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 300 | std::unique_ptr<uint8_t[]> alphaData(new uint8_t[kTestTextureSize * kTestTextureSize]); |
| 301 | for (int y = 0; y < kTestTextureSize; ++y) { |
| 302 | for (int x = 0; x < kTestTextureSize; ++x) { |
| 303 | alphaData[kTestTextureSize * y + x] = random->nextULessThan(256); |
| 304 | } |
| 305 | } |
| 306 | |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 307 | SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize, |
| 308 | kAlpha_8_SkColorType, kPremul_SkAlphaType); |
| 309 | SkPixmap pixmap(ii, alphaData.get(), ii.minRowBytes()); |
| 310 | sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap); |
| 311 | proxies[1] = proxyProvider->createTextureProxy(img, kNone_GrSurfaceFlags, 1, |
| 312 | SkBudgeted::kYes, SkBackingFit::kExact); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 313 | } |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 314 | |
| 315 | return proxies[0] && proxies[1]; |
| 316 | } |
| 317 | |
| 318 | // Creates a texture of premul colors used as the output of the fragment processor that precedes |
| 319 | // the fragment processor under test. Color values are those provided by input_texel_color(). |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 320 | sk_sp<GrTextureProxy> make_input_texture(GrProxyProvider* proxyProvider, int width, int height, |
| 321 | SkScalar delta) { |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 322 | std::unique_ptr<GrColor[]> data(new GrColor[width * height]); |
| 323 | for (int y = 0; y < width; ++y) { |
| 324 | for (int x = 0; x < height; ++x) { |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 325 | data.get()[width * y + x] = input_texel_color(x, y, delta); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 326 | } |
| 327 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 328 | |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 329 | SkImageInfo ii = SkImageInfo::Make(width, height, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 330 | SkPixmap pixmap(ii, data.get(), ii.minRowBytes()); |
| 331 | sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap); |
| 332 | return proxyProvider->createTextureProxy(img, kNone_GrSurfaceFlags, 1, |
| 333 | SkBudgeted::kYes, SkBackingFit::kExact); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 334 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 335 | |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 336 | bool log_surface_context(sk_sp<GrSurfaceContext> src, SkString* dst) { |
| 337 | SkImageInfo ii = SkImageInfo::Make(src->width(), src->height(), kRGBA_8888_SkColorType, |
| 338 | kPremul_SkAlphaType); |
| 339 | SkBitmap bm; |
| 340 | SkAssertResult(bm.tryAllocPixels(ii)); |
| 341 | SkAssertResult(src->readPixels(ii, bm.getPixels(), bm.rowBytes(), 0, 0)); |
| 342 | |
| 343 | return bitmap_to_base64_data_uri(bm, dst); |
| 344 | } |
| 345 | |
| 346 | bool log_surface_proxy(GrContext* context, sk_sp<GrSurfaceProxy> src, SkString* dst) { |
| 347 | sk_sp<GrSurfaceContext> sContext(context->contextPriv().makeWrappedSurfaceContext(src)); |
| 348 | return log_surface_context(sContext, dst); |
| 349 | } |
| 350 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 351 | bool fuzzy_color_equals(const SkPMColor4f& c1, const SkPMColor4f& c2) { |
| 352 | // With the loss of precision of rendering into 32-bit color, then estimating the FP's output |
| 353 | // from that, it is not uncommon for a valid output to differ from estimate by up to 0.01 |
| 354 | // (really 1/128 ~ .0078, but frequently floating point issues make that tolerance a little |
| 355 | // too unforgiving). |
| 356 | static constexpr SkScalar kTolerance = 0.01f; |
| 357 | for (int i = 0; i < 4; i++) { |
| 358 | if (!SkScalarNearlyEqual(c1[i], c2[i], kTolerance)) { |
| 359 | return false; |
| 360 | } |
| 361 | } |
| 362 | return true; |
| 363 | } |
| 364 | |
| 365 | int modulation_index(int channelIndex, bool alphaModulation) { |
| 366 | return alphaModulation ? 3 : channelIndex; |
| 367 | } |
| 368 | |
| 369 | // Given three input colors (color preceding the FP being tested), and the output of the FP, this |
| 370 | // ensures that the out1 = fp * in1.a, out2 = fp * in2.a, and out3 = fp * in3.a, where fp is the |
| 371 | // pre-modulated color that should not be changing across frames (FP's state doesn't change). |
| 372 | // |
| 373 | // When alphaModulation is false, this tests the very similar conditions that out1 = fp * in1, |
| 374 | // etc. using per-channel modulation instead of modulation by just the input alpha channel. |
| 375 | // - This estimates the pre-modulated fp color from one of the input/output pairs and confirms the |
| 376 | // conditions hold for the other two pairs. |
| 377 | bool legal_modulation(const GrColor& in1, const GrColor& in2, const GrColor& in3, |
| 378 | const GrColor& out1, const GrColor& out2, const GrColor& out3, |
| 379 | bool alphaModulation) { |
| 380 | // Convert to floating point, which is the number space the FP operates in (more or less) |
| 381 | SkPMColor4f in1f = GrColor4f::FromGrColor(in1).asRGBA4f<kPremul_SkAlphaType>(); |
| 382 | SkPMColor4f in2f = GrColor4f::FromGrColor(in2).asRGBA4f<kPremul_SkAlphaType>(); |
| 383 | SkPMColor4f in3f = GrColor4f::FromGrColor(in3).asRGBA4f<kPremul_SkAlphaType>(); |
| 384 | SkPMColor4f out1f = GrColor4f::FromGrColor(out1).asRGBA4f<kPremul_SkAlphaType>(); |
| 385 | SkPMColor4f out2f = GrColor4f::FromGrColor(out2).asRGBA4f<kPremul_SkAlphaType>(); |
| 386 | SkPMColor4f out3f = GrColor4f::FromGrColor(out3).asRGBA4f<kPremul_SkAlphaType>(); |
| 387 | |
| 388 | // Reconstruct the output of the FP before the shader modulated its color with the input value. |
| 389 | // When the original input is very small, it may cause the final output color to round |
| 390 | // to 0, in which case we estimate the pre-modulated color using one of the stepped frames that |
| 391 | // will then have a guaranteed larger channel value (since the offset will be added to it). |
| 392 | SkPMColor4f fpPreModulation; |
| 393 | for (int i = 0; i < 4; i++) { |
| 394 | int modulationIndex = modulation_index(i, alphaModulation); |
| 395 | if (in1f[modulationIndex] < 0.2f) { |
| 396 | // Use the stepped frame |
| 397 | fpPreModulation[i] = out2f[i] / in2f[modulationIndex]; |
| 398 | } else { |
| 399 | fpPreModulation[i] = out1f[i] / in1f[modulationIndex]; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | // With reconstructed pre-modulated FP output, derive the expected value of fp * input for each |
| 404 | // of the transformed input colors. |
| 405 | int mR = modulation_index(0, alphaModulation); |
| 406 | int mG = modulation_index(1, alphaModulation); |
| 407 | int mB = modulation_index(2, alphaModulation); |
| 408 | GrColor4f expected1 = GrColor4f(fpPreModulation.fR * in1f[mR], fpPreModulation.fG * in1f[mG], |
| 409 | fpPreModulation.fB * in1f[mB], fpPreModulation.fA * in1f.fA); |
| 410 | GrColor4f expected2 = GrColor4f(fpPreModulation.fR * in2f[mR], fpPreModulation.fG * in2f[mG], |
| 411 | fpPreModulation.fB * in2f[mB], fpPreModulation.fA * in2f.fA); |
| 412 | GrColor4f expected3 = GrColor4f(fpPreModulation.fR * in3f[mR], fpPreModulation.fG * in3f[mG], |
| 413 | fpPreModulation.fB * in3f[mB], fpPreModulation.fA * in3f.fA); |
| 414 | |
| 415 | return fuzzy_color_equals(out1f, expected1.asRGBA4f<kPremul_SkAlphaType>()) && |
| 416 | fuzzy_color_equals(out2f, expected2.asRGBA4f<kPremul_SkAlphaType>()) && |
| 417 | fuzzy_color_equals(out3f, expected3.asRGBA4f<kPremul_SkAlphaType>()); |
| 418 | } |
| 419 | |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 420 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 421 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 422 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 423 | auto resourceProvider = context->contextPriv().resourceProvider(); |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 424 | using FPFactory = GrFragmentProcessorTestFactory; |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 425 | |
Michael Ludwig | 3ad86d0 | 2018-09-28 16:00:38 -0400 | [diff] [blame] | 426 | uint32_t seed = FLAGS_processorSeed; |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 427 | if (FLAGS_randomProcessorTest) { |
| 428 | std::random_device rd; |
| 429 | seed = rd(); |
| 430 | } |
| 431 | // If a non-deterministic bot fails this test, check the output to see what seed it used, then |
Michael Ludwig | 3ad86d0 | 2018-09-28 16:00:38 -0400 | [diff] [blame] | 432 | // use --processorSeed <seed> (without --randomProcessorTest) to reproduce. |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 433 | SkRandom random(seed); |
| 434 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 435 | // Make the destination context for the test. |
| 436 | static constexpr int kRenderSize = 256; |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 437 | sk_sp<GrRenderTargetContext> rtc = context->contextPriv().makeDeferredRenderTargetContext( |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 438 | SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr); |
Brian Salomon | d1a8bdf | 2017-02-10 12:39:26 -0500 | [diff] [blame] | 439 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 440 | sk_sp<GrTextureProxy> proxies[2]; |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 441 | if (!init_test_textures(proxyProvider, &random, proxies)) { |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 442 | ERRORF(reporter, "Could not create test textures"); |
Robert Phillips | bb581ce | 2017-05-29 15:05:15 -0400 | [diff] [blame] | 443 | return; |
| 444 | } |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 445 | GrProcessorTestData testData(&random, context, rtc.get(), proxies); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 446 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 447 | // Coverage optimization uses three frames with a linearly transformed input texture. The first |
| 448 | // frame has no offset, second frames add .2 and .4, which should then be present as a fixed |
| 449 | // difference between the frame outputs if the FP is properly following the modulation |
| 450 | // requirements of the coverage optimization. |
| 451 | static constexpr SkScalar kInputDelta = 0.2f; |
| 452 | auto inputTexture1 = make_input_texture(proxyProvider, kRenderSize, kRenderSize, 0.0f); |
| 453 | auto inputTexture2 = make_input_texture(proxyProvider, kRenderSize, kRenderSize, kInputDelta); |
| 454 | auto inputTexture3 = make_input_texture(proxyProvider, kRenderSize, kRenderSize, 2*kInputDelta); |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 455 | |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 456 | // Encoded images are very verbose and this tests many potential images, so only export the |
| 457 | // first failure (subsequent failures have a reasonable chance of being related). |
| 458 | bool loggedFirstFailure = false; |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 459 | bool loggedFirstWarning = false; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 460 | |
| 461 | // Storage for the three frames required for coverage compatibility optimization. Each frame |
| 462 | // uses the correspondingly numbered inputTextureX. |
| 463 | std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]); |
| 464 | std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]); |
| 465 | std::unique_ptr<GrColor[]> readData3(new GrColor[kRenderSize * kRenderSize]); |
| 466 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 467 | // Because processor factories configure themselves in random ways, this is not exhaustive. |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 468 | for (int i = 0; i < FPFactory::Count(); ++i) { |
| 469 | int timesToInvokeFactory = 5; |
| 470 | // Increase the number of attempts if the FP has child FPs since optimizations likely depend |
| 471 | // on child optimizations being present. |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 472 | std::unique_ptr<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 473 | for (int j = 0; j < fp->numChildProcessors(); ++j) { |
| 474 | // This value made a reasonable trade off between time and coverage when this test was |
| 475 | // written. |
| 476 | timesToInvokeFactory *= FPFactory::Count() / 2; |
| 477 | } |
| 478 | for (int j = 0; j < timesToInvokeFactory; ++j) { |
| 479 | fp = FPFactory::MakeIdx(i, &testData); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 480 | if (!fp->instantiate(resourceProvider)) { |
Robert Phillips | a91e0b7 | 2017-05-01 13:12:20 -0400 | [diff] [blame] | 481 | continue; |
| 482 | } |
| 483 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 484 | if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() && |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 485 | !fp->compatibleWithCoverageAsAlpha()) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 486 | continue; |
| 487 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 488 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 489 | if (fp->compatibleWithCoverageAsAlpha()) { |
| 490 | // 2nd and 3rd frames are only used when checking coverage optimization |
| 491 | render_fp(context, rtc.get(), fp.get(), inputTexture2, readData2.get()); |
| 492 | render_fp(context, rtc.get(), fp.get(), inputTexture3, readData3.get()); |
| 493 | } |
| 494 | // Draw base frame last so that rtc holds the original FP behavior if we need to |
| 495 | // dump the image to the log. |
| 496 | render_fp(context, rtc.get(), fp.get(), inputTexture1, readData1.get()); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 497 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 498 | if (0) { // Useful to see what FPs are being tested. |
| 499 | SkString children; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 500 | for (int c = 0; c < fp->numChildProcessors(); ++c) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 501 | if (!c) { |
| 502 | children.append("("); |
| 503 | } |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 504 | children.append(fp->childProcessor(c).name()); |
| 505 | children.append(c == fp->numChildProcessors() - 1 ? ")" : ", "); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 506 | } |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 507 | SkDebugf("%s %s\n", fp->name(), children.c_str()); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 508 | } |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 509 | |
| 510 | // This test has a history of being flaky on a number of devices. If an FP is logically |
| 511 | // violating the optimizations, it's reasonable to expect it to violate requirements on |
| 512 | // a large number of pixels in the image. Sporadic pixel violations are more indicative |
| 513 | // of device errors and represents a separate problem. |
| 514 | #if defined(SK_SKQP_GLOBAL_ERROR_TOLERANCE) |
| 515 | static constexpr int kMaxAcceptableFailedPixels = 0; // Strict when running as SKQP |
| 516 | #else |
| 517 | static constexpr int kMaxAcceptableFailedPixels = 2 * kRenderSize; // ~0.7% of the image |
| 518 | #endif |
| 519 | |
| 520 | int failedPixelCount = 0; |
| 521 | // Collect first optimization failure message, to be output later as a warning or an |
| 522 | // error depending on whether the rendering "passed" or failed. |
| 523 | SkString coverageMessage; |
| 524 | SkString opaqueMessage; |
| 525 | SkString constMessage; |
| 526 | for (int y = 0; y < kRenderSize; ++y) { |
| 527 | for (int x = 0; x < kRenderSize; ++x) { |
| 528 | bool passing = true; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 529 | GrColor input = input_texel_color(x, y, 0.0f); |
| 530 | GrColor output = readData1.get()[y * kRenderSize + x]; |
| 531 | |
| 532 | if (fp->compatibleWithCoverageAsAlpha()) { |
| 533 | GrColor i2 = input_texel_color(x, y, kInputDelta); |
| 534 | GrColor i3 = input_texel_color(x, y, 2 * kInputDelta); |
| 535 | |
| 536 | GrColor o2 = readData2.get()[y * kRenderSize + x]; |
| 537 | GrColor o3 = readData3.get()[y * kRenderSize + x]; |
| 538 | |
| 539 | // A compatible processor is allowed to modulate either the input color or |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 540 | // just the input alpha. |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 541 | bool legalAlphaModulation = legal_modulation(input, i2, i3, output, o2, o3, |
| 542 | /* alpha */ true); |
| 543 | bool legalColorModulation = legal_modulation(input, i2, i3, output, o2, o3, |
| 544 | /* alpha */ false); |
| 545 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 546 | if (!legalColorModulation && !legalAlphaModulation) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 547 | passing = false; |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 548 | |
| 549 | if (coverageMessage.isEmpty()) { |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 550 | coverageMessage.printf("\"Modulating\" processor %s did not match " |
| 551 | "alpha-modulation nor color-modulation rules. " |
| 552 | "Input: 0x%08x, Output: 0x%08x, pixel (%d, %d).", |
| 553 | fp->name(), input, output, x, y); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 554 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 555 | } |
| 556 | } |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 557 | |
| 558 | SkPMColor4f input4f = |
| 559 | GrColor4f::FromGrColor(input).asRGBA4f<kPremul_SkAlphaType>(); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 560 | GrColor4f output4f = GrColor4f::FromGrColor(output); |
Brian Osman | 1d5b598 | 2018-10-01 13:41:39 -0400 | [diff] [blame] | 561 | SkPMColor4f expected4f; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 562 | if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) { |
Brian Osman | 1d5b598 | 2018-10-01 13:41:39 -0400 | [diff] [blame] | 563 | float rDiff = fabsf(output4f.fRGBA[0] - expected4f.fR); |
| 564 | float gDiff = fabsf(output4f.fRGBA[1] - expected4f.fG); |
| 565 | float bDiff = fabsf(output4f.fRGBA[2] - expected4f.fB); |
| 566 | float aDiff = fabsf(output4f.fRGBA[3] - expected4f.fA); |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 567 | static constexpr float kTol = 4 / 255.f; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 568 | if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) { |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 569 | if (constMessage.isEmpty()) { |
| 570 | passing = false; |
| 571 | |
| 572 | constMessage.printf("Processor %s claimed output for const input " |
| 573 | "doesn't match actual output. Error: %f, Tolerance: %f, " |
| 574 | "input: (%f, %f, %f, %f), actual: (%f, %f, %f, %f), " |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 575 | "expected(%f, %f, %f, %f)", fp->name(), |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 576 | SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))), kTol, |
| 577 | input4f.fR, input4f.fG, input4f.fB, input4f.fA, |
| 578 | output4f.fRGBA[0], output4f.fRGBA[1], output4f.fRGBA[2], |
| 579 | output4f.fRGBA[3], expected4f.fR, expected4f.fG, |
| 580 | expected4f.fB, expected4f.fA); |
| 581 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 582 | } |
| 583 | } |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 584 | if (GrColorIsOpaque(input) && fp->preservesOpaqueInput() && |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 585 | !GrColorIsOpaque(output)) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 586 | passing = false; |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 587 | |
| 588 | if (opaqueMessage.isEmpty()) { |
| 589 | opaqueMessage.printf("Processor %s claimed opaqueness is preserved but " |
| 590 | "it is not. Input: 0x%08x, Output: 0x%08x.", |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 591 | fp->name(), input, output); |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 592 | } |
Brian Osman | bd1f76f | 2017-03-15 11:33:12 -0400 | [diff] [blame] | 593 | } |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 594 | |
| 595 | if (!passing) { |
| 596 | // Regardless of how many optimizations the pixel violates, count it as a |
| 597 | // single bad pixel. |
| 598 | failedPixelCount++; |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | // Finished analyzing the entire image, see if the number of pixel failures meets the |
| 604 | // threshold for an FP violating the optimization requirements. |
| 605 | if (failedPixelCount > kMaxAcceptableFailedPixels) { |
Michael Ludwig | 4504a652 | 2018-10-03 16:47:55 -0400 | [diff] [blame] | 606 | ERRORF(reporter, "Processor violated %d of %d pixels, seed: 0x%08x, processor: %s" |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 607 | ", first failing pixel details are below:", |
| 608 | failedPixelCount, kRenderSize * kRenderSize, seed, |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 609 | fp->dumpInfo().c_str()); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 610 | |
| 611 | // Print first failing pixel's details. |
| 612 | if (!coverageMessage.isEmpty()) { |
| 613 | ERRORF(reporter, coverageMessage.c_str()); |
| 614 | } |
| 615 | if (!constMessage.isEmpty()) { |
| 616 | ERRORF(reporter, constMessage.c_str()); |
| 617 | } |
| 618 | if (!opaqueMessage.isEmpty()) { |
| 619 | ERRORF(reporter, opaqueMessage.c_str()); |
| 620 | } |
| 621 | |
| 622 | if (!loggedFirstFailure) { |
| 623 | // Print with ERRORF to make sure the encoded image is output |
| 624 | SkString input; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 625 | log_surface_proxy(context, inputTexture1, &input); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 626 | SkString output; |
| 627 | log_surface_context(rtc, &output); |
| 628 | ERRORF(reporter, "Input image: %s\n\n" |
| 629 | "===========================================================\n\n" |
| 630 | "Output image: %s\n", input.c_str(), output.c_str()); |
| 631 | loggedFirstFailure = true; |
| 632 | } |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 633 | } else if(failedPixelCount > 0) { |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 634 | // Don't trigger an error, but don't just hide the failures either. |
| 635 | INFOF(reporter, "Processor violated %d of %d pixels (below error threshold), seed: " |
| 636 | "0x%08x, processor: %s", failedPixelCount, kRenderSize * kRenderSize, |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 637 | seed, fp->dumpInfo().c_str()); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 638 | if (!coverageMessage.isEmpty()) { |
| 639 | INFOF(reporter, coverageMessage.c_str()); |
| 640 | } |
| 641 | if (!constMessage.isEmpty()) { |
| 642 | INFOF(reporter, constMessage.c_str()); |
| 643 | } |
| 644 | if (!opaqueMessage.isEmpty()) { |
| 645 | INFOF(reporter, opaqueMessage.c_str()); |
| 646 | } |
| 647 | if (!loggedFirstWarning) { |
| 648 | SkString input; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 649 | log_surface_proxy(context, inputTexture1, &input); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 650 | SkString output; |
| 651 | log_surface_context(rtc, &output); |
| 652 | INFOF(reporter, "Input image: %s\n\n" |
| 653 | "===========================================================\n\n" |
| 654 | "Output image: %s\n", input.c_str(), output.c_str()); |
| 655 | loggedFirstWarning = true; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | } |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 661 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 662 | // Tests that fragment processors returned by GrFragmentProcessor::clone() are equivalent to their |
| 663 | // progenitors. |
| 664 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) { |
| 665 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 666 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 667 | auto resourceProvider = context->contextPriv().resourceProvider(); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 668 | |
| 669 | SkRandom random; |
| 670 | |
| 671 | // Make the destination context for the test. |
| 672 | static constexpr int kRenderSize = 1024; |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 673 | sk_sp<GrRenderTargetContext> rtc = context->contextPriv().makeDeferredRenderTargetContext( |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 674 | SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr); |
| 675 | |
| 676 | sk_sp<GrTextureProxy> proxies[2]; |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 677 | if (!init_test_textures(proxyProvider, &random, proxies)) { |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 678 | ERRORF(reporter, "Could not create test textures"); |
| 679 | return; |
| 680 | } |
| 681 | GrProcessorTestData testData(&random, context, rtc.get(), proxies); |
| 682 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 683 | auto inputTexture = make_input_texture(proxyProvider, kRenderSize, kRenderSize, 0.0f); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 684 | std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]); |
| 685 | std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]); |
| 686 | auto readInfo = SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType, |
| 687 | kPremul_SkAlphaType); |
| 688 | |
| 689 | // Because processor factories configure themselves in random ways, this is not exhaustive. |
| 690 | for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) { |
| 691 | static constexpr int kTimesToInvokeFactory = 10; |
| 692 | for (int j = 0; j < kTimesToInvokeFactory; ++j) { |
| 693 | auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &testData); |
| 694 | auto clone = fp->clone(); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 695 | if (!clone) { |
Brian Salomon | 96271cd | 2017-07-31 16:27:23 -0400 | [diff] [blame] | 696 | ERRORF(reporter, "Clone of processor %s failed.", fp->name()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 697 | continue; |
| 698 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 699 | const char* name = fp->name(); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 700 | if (!fp->instantiate(resourceProvider) || !clone->instantiate(resourceProvider)) { |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 701 | continue; |
| 702 | } |
Brian Salomon | ce06e26 | 2017-08-01 16:23:40 -0400 | [diff] [blame] | 703 | REPORTER_ASSERT(reporter, !strcmp(fp->name(), clone->name())); |
| 704 | REPORTER_ASSERT(reporter, fp->compatibleWithCoverageAsAlpha() == |
| 705 | clone->compatibleWithCoverageAsAlpha()); |
| 706 | REPORTER_ASSERT(reporter, fp->isEqual(*clone)); |
| 707 | REPORTER_ASSERT(reporter, fp->preservesOpaqueInput() == clone->preservesOpaqueInput()); |
| 708 | REPORTER_ASSERT(reporter, fp->hasConstantOutputForConstantInput() == |
| 709 | clone->hasConstantOutputForConstantInput()); |
| 710 | REPORTER_ASSERT(reporter, fp->numChildProcessors() == clone->numChildProcessors()); |
| 711 | REPORTER_ASSERT(reporter, fp->usesLocalCoords() == clone->usesLocalCoords()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 712 | // Draw with original and read back the results. |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 713 | render_fp(context, rtc.get(), fp.get(), inputTexture, readData1.get()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 714 | |
| 715 | // Draw with clone and read back the results. |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 716 | render_fp(context, rtc.get(), clone.get(), inputTexture, readData2.get()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 717 | |
| 718 | // Check that the results are the same. |
| 719 | bool passing = true; |
| 720 | for (int y = 0; y < kRenderSize && passing; ++y) { |
| 721 | for (int x = 0; x < kRenderSize && passing; ++x) { |
| 722 | int idx = y * kRenderSize + x; |
| 723 | if (readData1[idx] != readData2[idx]) { |
| 724 | ERRORF(reporter, |
| 725 | "Processor %s made clone produced different output. " |
| 726 | "Input color: 0x%08x, Original Output Color: 0x%08x, " |
| 727 | "Clone Output Color: 0x%08x..", |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame^] | 728 | name, input_texel_color(x, y, 0.0f), readData1[idx], readData2[idx]); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 729 | passing = false; |
| 730 | } |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | } |
| 736 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 737 | #endif // GR_TEST_UTILS |
| 738 | #endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |