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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkTypes.h" |
| 9 | #include "tests/Test.h" |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/gpu/GrContext.h" |
| 12 | #include "include/gpu/GrGpuResource.h" |
| 13 | #include "src/gpu/GrClip.h" |
| 14 | #include "src/gpu/GrContextPriv.h" |
| 15 | #include "src/gpu/GrMemoryPool.h" |
| 16 | #include "src/gpu/GrProxyProvider.h" |
| 17 | #include "src/gpu/GrRenderTargetContext.h" |
| 18 | #include "src/gpu/GrRenderTargetContextPriv.h" |
| 19 | #include "src/gpu/GrResourceProvider.h" |
| 20 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 21 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 22 | #include "src/gpu/ops/GrFillRectOp.h" |
| 23 | #include "src/gpu/ops/GrMeshDrawOp.h" |
| 24 | #include "tests/TestUtils.h" |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 25 | |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 26 | #include <atomic> |
Hal Canary | 8a00144 | 2018-09-19 11:31:27 -0400 | [diff] [blame] | 27 | #include <random> |
| 28 | |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 29 | namespace { |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 30 | class TestOp : public GrMeshDrawOp { |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 31 | public: |
| 32 | DEFINE_OP_CLASS_ID |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 33 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, |
| 34 | std::unique_ptr<GrFragmentProcessor> fp) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 35 | GrOpMemoryPool* pool = context->priv().opMemoryPool(); |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 36 | |
| 37 | return pool->allocate<TestOp>(std::move(fp)); |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 38 | } |
| 39 | |
Robert Phillips | 5f567c7 | 2017-09-14 08:27:37 -0400 | [diff] [blame] | 40 | const char* name() const override { return "TestOp"; } |
| 41 | |
Chris Dalton | 1706cbf | 2019-05-21 19:35:29 -0600 | [diff] [blame] | 42 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | 5f567c7 | 2017-09-14 08:27:37 -0400 | [diff] [blame] | 43 | fProcessors.visitProxies(func); |
| 44 | } |
| 45 | |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 46 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
| 47 | |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 48 | GrProcessorSet::Analysis finalize( |
| 49 | const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage, |
| 50 | GrClampType clampType) override { |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 51 | static constexpr GrProcessorAnalysisColor kUnknownColor; |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 52 | SkPMColor4f overrideColor; |
Chris Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 53 | return fProcessors.finalize( |
| 54 | kUnknownColor, GrProcessorAnalysisCoverage::kNone, clip, |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 55 | &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps, clampType, |
| 56 | &overrideColor); |
Brian Salomon | 649a341 | 2017-03-09 13:50:43 -0500 | [diff] [blame] | 57 | } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 58 | |
| 59 | private: |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 60 | friend class ::GrOpMemoryPool; // for ctor |
| 61 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 62 | TestOp(std::unique_ptr<GrFragmentProcessor> fp) |
| 63 | : INHERITED(ClassID()), fProcessors(std::move(fp)) { |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 64 | this->setBounds(SkRect::MakeWH(100, 100), HasAABloat::kNo, IsZeroArea::kNo); |
| 65 | } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 66 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 67 | void onPrepareDraws(Target* target) override { return; } |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 68 | void onExecute(GrOpFlushState*, const SkRect&) override { return; } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 69 | |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 70 | GrProcessorSet fProcessors; |
| 71 | |
| 72 | typedef GrMeshDrawOp INHERITED; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | /** |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 76 | * FP used to test ref counts on owned GrGpuResources. Can also be a parent FP to test counts |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 77 | * of resources owned by child FPs. |
| 78 | */ |
| 79 | class TestFP : public GrFragmentProcessor { |
| 80 | public: |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 81 | static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> child) { |
| 82 | return std::unique_ptr<GrFragmentProcessor>(new TestFP(std::move(child))); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 83 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 84 | static std::unique_ptr<GrFragmentProcessor> Make(const SkTArray<sk_sp<GrTextureProxy>>& proxies, |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 85 | const SkTArray<sk_sp<GrGpuBuffer>>& buffers) { |
Brian Salomon | 559f556 | 2017-11-15 14:28:33 -0500 | [diff] [blame] | 86 | return std::unique_ptr<GrFragmentProcessor>(new TestFP(proxies, buffers)); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | const char* name() const override { return "test"; } |
| 90 | |
| 91 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override { |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 92 | static std::atomic<int32_t> nextKey{0}; |
| 93 | b->add32(nextKey++); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 94 | } |
| 95 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 96 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 97 | return std::unique_ptr<GrFragmentProcessor>(new TestFP(*this)); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 98 | } |
| 99 | |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 100 | private: |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 101 | TestFP(const SkTArray<sk_sp<GrTextureProxy>>& proxies, |
| 102 | const SkTArray<sk_sp<GrGpuBuffer>>& buffers) |
Brian Salomon | 662ea4b | 2018-07-12 14:53:49 -0400 | [diff] [blame] | 103 | : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4) { |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 104 | for (const auto& proxy : proxies) { |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 105 | fSamplers.emplace_back(proxy); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 106 | } |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 107 | this->setTextureSamplerCnt(fSamplers.count()); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 108 | } |
| 109 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 110 | TestFP(std::unique_ptr<GrFragmentProcessor> child) |
Brian Salomon | 662ea4b | 2018-07-12 14:53:49 -0400 | [diff] [blame] | 111 | : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4) { |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 112 | this->registerChildProcessor(std::move(child)); |
| 113 | } |
| 114 | |
Brian Salomon | 96271cd | 2017-07-31 16:27:23 -0400 | [diff] [blame] | 115 | explicit TestFP(const TestFP& that) |
Brian Salomon | 662ea4b | 2018-07-12 14:53:49 -0400 | [diff] [blame] | 116 | : INHERITED(kTestFP_ClassID, that.optimizationFlags()), fSamplers(4) { |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 117 | for (int i = 0; i < that.fSamplers.count(); ++i) { |
| 118 | fSamplers.emplace_back(that.fSamplers[i]); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 119 | } |
Brian Salomon | 96271cd | 2017-07-31 16:27:23 -0400 | [diff] [blame] | 120 | for (int i = 0; i < that.numChildProcessors(); ++i) { |
| 121 | this->registerChildProcessor(that.childProcessor(i).clone()); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 122 | } |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 123 | this->setTextureSamplerCnt(fSamplers.count()); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 124 | } |
| 125 | |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 126 | virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
| 127 | class TestGLSLFP : public GrGLSLFragmentProcessor { |
| 128 | public: |
| 129 | TestGLSLFP() {} |
| 130 | void emitCode(EmitArgs& args) override { |
| 131 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 132 | fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor); |
| 133 | } |
| 134 | |
| 135 | private: |
| 136 | }; |
| 137 | return new TestGLSLFP(); |
| 138 | } |
| 139 | |
| 140 | bool onIsEqual(const GrFragmentProcessor&) const override { return false; } |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 141 | const TextureSampler& onTextureSampler(int i) const override { return fSamplers[i]; } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 142 | |
| 143 | GrTAllocator<TextureSampler> fSamplers; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 144 | typedef GrFragmentProcessor INHERITED; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 145 | }; |
| 146 | } |
| 147 | |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 148 | static void check_refs(skiatest::Reporter* reporter, |
| 149 | GrTextureProxy* proxy, |
| 150 | int32_t expectedProxyRefs, |
| 151 | int32_t expectedBackingRefs) { |
| 152 | int32_t actualProxyRefs = proxy->priv().getProxyRefCnt(); |
Robert Phillips | b520476 | 2019-06-19 14:12:13 -0400 | [diff] [blame] | 153 | int32_t actualBackingRefs = proxy->testingOnly_getBackingRefCnt(); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 154 | |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 155 | SkASSERT(actualProxyRefs == expectedProxyRefs); |
| 156 | SkASSERT(actualBackingRefs == expectedBackingRefs); |
| 157 | |
| 158 | REPORTER_ASSERT(reporter, actualProxyRefs == expectedProxyRefs); |
| 159 | REPORTER_ASSERT(reporter, actualBackingRefs == expectedBackingRefs); |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 160 | } |
| 161 | |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 162 | DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) { |
| 163 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 164 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 165 | |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 166 | GrSurfaceDesc desc; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 167 | desc.fWidth = 10; |
| 168 | desc.fHeight = 10; |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 169 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 170 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 171 | const GrBackendFormat format = |
Greg Daniel | 627d053 | 2019-07-08 16:48:14 -0400 | [diff] [blame] | 172 | context->priv().caps()->getBackendFormatFromColorType(GrColorType::kRGBA_8888); |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 173 | |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 174 | for (bool makeClone : {false, true}) { |
| 175 | for (int parentCnt = 0; parentCnt < 2; parentCnt++) { |
| 176 | sk_sp<GrRenderTargetContext> renderTargetContext( |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 177 | context->priv().makeDeferredRenderTargetContext( |
Brian Salomon | 27ae52c | 2019-07-03 11:27:44 -0400 | [diff] [blame] | 178 | SkBackingFit::kApprox, 1, 1, GrColorType::kRGBA_8888, nullptr)); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 179 | { |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 180 | sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy( |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame^] | 181 | format, desc, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame] | 182 | SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo); |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 183 | |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 184 | { |
| 185 | SkTArray<sk_sp<GrTextureProxy>> proxies; |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 186 | SkTArray<sk_sp<GrGpuBuffer>> buffers; |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 187 | proxies.push_back(proxy); |
Brian Salomon | 559f556 | 2017-11-15 14:28:33 -0500 | [diff] [blame] | 188 | auto fp = TestFP::Make(std::move(proxies), std::move(buffers)); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 189 | for (int i = 0; i < parentCnt; ++i) { |
| 190 | fp = TestFP::Make(std::move(fp)); |
| 191 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 192 | std::unique_ptr<GrFragmentProcessor> clone; |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 193 | if (makeClone) { |
| 194 | clone = fp->clone(); |
| 195 | } |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 196 | std::unique_ptr<GrDrawOp> op(TestOp::Make(context, std::move(fp))); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 197 | renderTargetContext->priv().testingOnly_addDrawOp(std::move(op)); |
| 198 | if (clone) { |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 199 | op = TestOp::Make(context, std::move(clone)); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 200 | renderTargetContext->priv().testingOnly_addDrawOp(std::move(op)); |
| 201 | } |
| 202 | } |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 203 | |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 204 | // If the fp is cloned the number of refs should increase by one (for the clone) |
| 205 | int expectedProxyRefs = makeClone ? 3 : 2; |
| 206 | |
| 207 | check_refs(reporter, proxy.get(), expectedProxyRefs, -1); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 208 | |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 209 | context->flush(); |
| 210 | |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 211 | check_refs(reporter, proxy.get(), 1, 1); // just one from the 'proxy' sk_sp |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 216 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 217 | #include "tools/flags/CommandLineFlags.h" |
Mike Klein | 84836b7 | 2019-03-21 11:31:36 -0500 | [diff] [blame] | 218 | static DEFINE_bool(randomProcessorTest, false, |
| 219 | "Use non-deterministic seed for random processor tests?"); |
Mike Klein | 5b3f343 | 2019-03-21 11:42:21 -0500 | [diff] [blame] | 220 | static DEFINE_int(processorSeed, 0, |
| 221 | "Use specific seed for processor tests. Overridden by --randomProcessorTest."); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 222 | |
| 223 | #if GR_TEST_UTILS |
| 224 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 225 | static GrColor input_texel_color(int i, int j, SkScalar delta) { |
| 226 | // Delta must be less than 0.5 to prevent over/underflow issues with the input color |
| 227 | SkASSERT(delta <= 0.5); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 228 | |
Brian Osman | 50ea3c0 | 2019-02-04 10:01:53 -0500 | [diff] [blame] | 229 | SkColor color = SkColorSetARGB((uint8_t)(i & 0xFF), |
| 230 | (uint8_t)(j & 0xFF), |
| 231 | (uint8_t)((i + j) & 0xFF), |
| 232 | (uint8_t)((2 * j - i) & 0xFF)); |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 233 | SkColor4f color4f = SkColor4f::FromColor(color); |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 234 | for (int i = 0; i < 4; i++) { |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 235 | if (color4f[i] > 0.5) { |
| 236 | color4f[i] -= delta; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 237 | } else { |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 238 | color4f[i] += delta; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 239 | } |
| 240 | } |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 241 | return color4f.premul().toBytes_RGBA(); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 242 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 243 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 244 | void test_draw_op(GrContext* context, |
| 245 | GrRenderTargetContext* rtc, |
| 246 | std::unique_ptr<GrFragmentProcessor> fp, |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 247 | sk_sp<GrTextureProxy> inputDataProxy) { |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 248 | GrPaint paint; |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 249 | paint.addColorTextureProcessor(std::move(inputDataProxy), SkMatrix::I()); |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 250 | paint.addColorFragmentProcessor(std::move(fp)); |
| 251 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 252 | |
Michael Ludwig | aa1b6b3 | 2019-05-29 14:43:13 -0400 | [diff] [blame] | 253 | auto op = GrFillRectOp::MakeNonAARect(context, std::move(paint), SkMatrix::I(), |
| 254 | SkRect::MakeWH(rtc->width(), rtc->height())); |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 255 | rtc->addDrawOp(GrNoClip(), std::move(op)); |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 256 | } |
| 257 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 258 | // This assumes that the output buffer will be the same size as inputDataProxy |
| 259 | void render_fp(GrContext* context, GrRenderTargetContext* rtc, GrFragmentProcessor* fp, |
| 260 | sk_sp<GrTextureProxy> inputDataProxy, GrColor* buffer) { |
| 261 | int width = inputDataProxy->width(); |
| 262 | int height = inputDataProxy->height(); |
| 263 | |
| 264 | // test_draw_op needs to take ownership of an FP, so give it a clone that it can own |
| 265 | test_draw_op(context, rtc, fp->clone(), inputDataProxy); |
| 266 | memset(buffer, 0x0, sizeof(GrColor) * width * height); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 267 | rtc->readPixels(SkImageInfo::Make(width, height, kRGBA_8888_SkColorType, kPremul_SkAlphaType), |
| 268 | buffer, 0, {0, 0}); |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 269 | } |
| 270 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 271 | /** Initializes the two test texture proxies that are available to the FP test factories. */ |
Robert Phillips | 7eeb74f | 2019-03-29 07:26:46 -0400 | [diff] [blame] | 272 | bool init_test_textures(GrResourceProvider* resourceProvider, |
| 273 | GrProxyProvider* proxyProvider, |
| 274 | SkRandom* random, |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 275 | sk_sp<GrTextureProxy> proxies[2]) { |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 276 | static const int kTestTextureSize = 256; |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 277 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 278 | { |
| 279 | // Put premul data into the RGBA texture that the test FPs can optionally use. |
| 280 | std::unique_ptr<GrColor[]> rgbaData(new GrColor[kTestTextureSize * kTestTextureSize]); |
| 281 | for (int y = 0; y < kTestTextureSize; ++y) { |
| 282 | for (int x = 0; x < kTestTextureSize; ++x) { |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 283 | rgbaData[kTestTextureSize * y + x] = input_texel_color( |
| 284 | random->nextULessThan(256), random->nextULessThan(256), 0.0f); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 285 | } |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 286 | } |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 287 | |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 288 | SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize, |
| 289 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 290 | SkPixmap pixmap(ii, rgbaData.get(), ii.minRowBytes()); |
| 291 | sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap); |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 292 | proxies[0] = proxyProvider->createTextureProxy(img, GrRenderable::kNo, 1, SkBudgeted::kYes, |
| 293 | SkBackingFit::kExact); |
Robert Phillips | 12c4629 | 2019-04-23 07:36:17 -0400 | [diff] [blame] | 294 | proxies[0]->instantiate(resourceProvider); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 295 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 296 | |
| 297 | { |
| 298 | // 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] | 299 | std::unique_ptr<uint8_t[]> alphaData(new uint8_t[kTestTextureSize * kTestTextureSize]); |
| 300 | for (int y = 0; y < kTestTextureSize; ++y) { |
| 301 | for (int x = 0; x < kTestTextureSize; ++x) { |
| 302 | alphaData[kTestTextureSize * y + x] = random->nextULessThan(256); |
| 303 | } |
| 304 | } |
| 305 | |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 306 | SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize, |
| 307 | kAlpha_8_SkColorType, kPremul_SkAlphaType); |
| 308 | SkPixmap pixmap(ii, alphaData.get(), ii.minRowBytes()); |
| 309 | sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap); |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 310 | proxies[1] = proxyProvider->createTextureProxy(img, GrRenderable::kNo, 1, SkBudgeted::kYes, |
| 311 | SkBackingFit::kExact); |
Robert Phillips | 12c4629 | 2019-04-23 07:36:17 -0400 | [diff] [blame] | 312 | proxies[1]->instantiate(resourceProvider); |
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); |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 332 | return proxyProvider->createTextureProxy(img, GrRenderable::kNo, 1, SkBudgeted::kYes, |
| 333 | 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)); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 341 | SkAssertResult(src->readPixels(ii, bm.getPixels(), bm.rowBytes(), {0, 0})); |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 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) { |
Brian Salomon | d628747 | 2019-06-24 15:50:07 -0400 | [diff] [blame] | 347 | // All the inputs are made from kRGBA_8888_SkColorType/kPremul_SkAlphaType bitmaps. |
| 348 | sk_sp<GrSurfaceContext> sContext(context->priv().makeWrappedSurfaceContext( |
| 349 | src, GrColorType::kRGBA_8888, kPremul_SkAlphaType)); |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 350 | return log_surface_context(sContext, dst); |
| 351 | } |
| 352 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 353 | bool fuzzy_color_equals(const SkPMColor4f& c1, const SkPMColor4f& c2) { |
| 354 | // With the loss of precision of rendering into 32-bit color, then estimating the FP's output |
| 355 | // from that, it is not uncommon for a valid output to differ from estimate by up to 0.01 |
| 356 | // (really 1/128 ~ .0078, but frequently floating point issues make that tolerance a little |
| 357 | // too unforgiving). |
| 358 | static constexpr SkScalar kTolerance = 0.01f; |
| 359 | for (int i = 0; i < 4; i++) { |
| 360 | if (!SkScalarNearlyEqual(c1[i], c2[i], kTolerance)) { |
| 361 | return false; |
| 362 | } |
| 363 | } |
| 364 | return true; |
| 365 | } |
| 366 | |
| 367 | int modulation_index(int channelIndex, bool alphaModulation) { |
| 368 | return alphaModulation ? 3 : channelIndex; |
| 369 | } |
| 370 | |
| 371 | // Given three input colors (color preceding the FP being tested), and the output of the FP, this |
| 372 | // ensures that the out1 = fp * in1.a, out2 = fp * in2.a, and out3 = fp * in3.a, where fp is the |
| 373 | // pre-modulated color that should not be changing across frames (FP's state doesn't change). |
| 374 | // |
| 375 | // When alphaModulation is false, this tests the very similar conditions that out1 = fp * in1, |
| 376 | // etc. using per-channel modulation instead of modulation by just the input alpha channel. |
| 377 | // - This estimates the pre-modulated fp color from one of the input/output pairs and confirms the |
| 378 | // conditions hold for the other two pairs. |
| 379 | bool legal_modulation(const GrColor& in1, const GrColor& in2, const GrColor& in3, |
| 380 | const GrColor& out1, const GrColor& out2, const GrColor& out3, |
| 381 | bool alphaModulation) { |
| 382 | // Convert to floating point, which is the number space the FP operates in (more or less) |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 383 | SkPMColor4f in1f = SkPMColor4f::FromBytes_RGBA(in1); |
| 384 | SkPMColor4f in2f = SkPMColor4f::FromBytes_RGBA(in2); |
| 385 | SkPMColor4f in3f = SkPMColor4f::FromBytes_RGBA(in3); |
| 386 | SkPMColor4f out1f = SkPMColor4f::FromBytes_RGBA(out1); |
| 387 | SkPMColor4f out2f = SkPMColor4f::FromBytes_RGBA(out2); |
| 388 | SkPMColor4f out3f = SkPMColor4f::FromBytes_RGBA(out3); |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 389 | |
| 390 | // Reconstruct the output of the FP before the shader modulated its color with the input value. |
| 391 | // When the original input is very small, it may cause the final output color to round |
| 392 | // to 0, in which case we estimate the pre-modulated color using one of the stepped frames that |
| 393 | // will then have a guaranteed larger channel value (since the offset will be added to it). |
| 394 | SkPMColor4f fpPreModulation; |
| 395 | for (int i = 0; i < 4; i++) { |
| 396 | int modulationIndex = modulation_index(i, alphaModulation); |
| 397 | if (in1f[modulationIndex] < 0.2f) { |
| 398 | // Use the stepped frame |
| 399 | fpPreModulation[i] = out2f[i] / in2f[modulationIndex]; |
| 400 | } else { |
| 401 | fpPreModulation[i] = out1f[i] / in1f[modulationIndex]; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | // With reconstructed pre-modulated FP output, derive the expected value of fp * input for each |
| 406 | // of the transformed input colors. |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 407 | SkPMColor4f expected1 = alphaModulation ? (fpPreModulation * in1f.fA) |
| 408 | : (fpPreModulation * in1f); |
| 409 | SkPMColor4f expected2 = alphaModulation ? (fpPreModulation * in2f.fA) |
| 410 | : (fpPreModulation * in2f); |
| 411 | SkPMColor4f expected3 = alphaModulation ? (fpPreModulation * in3f.fA) |
| 412 | : (fpPreModulation * in3f); |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 413 | |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 414 | return fuzzy_color_equals(out1f, expected1) && |
| 415 | fuzzy_color_equals(out2f, expected2) && |
| 416 | fuzzy_color_equals(out3f, expected3); |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 417 | } |
| 418 | |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 419 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 420 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 421 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
| 422 | auto resourceProvider = context->priv().resourceProvider(); |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 423 | using FPFactory = GrFragmentProcessorTestFactory; |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 424 | |
Michael Ludwig | 3ad86d0 | 2018-09-28 16:00:38 -0400 | [diff] [blame] | 425 | uint32_t seed = FLAGS_processorSeed; |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 426 | if (FLAGS_randomProcessorTest) { |
| 427 | std::random_device rd; |
| 428 | seed = rd(); |
| 429 | } |
| 430 | // 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] | 431 | // use --processorSeed <seed> (without --randomProcessorTest) to reproduce. |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 432 | SkRandom random(seed); |
| 433 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 434 | // Make the destination context for the test. |
| 435 | static constexpr int kRenderSize = 256; |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 436 | sk_sp<GrRenderTargetContext> rtc = context->priv().makeDeferredRenderTargetContext( |
Brian Salomon | 27ae52c | 2019-07-03 11:27:44 -0400 | [diff] [blame] | 437 | SkBackingFit::kExact, kRenderSize, kRenderSize, GrColorType::kRGBA_8888, nullptr); |
Brian Salomon | d1a8bdf | 2017-02-10 12:39:26 -0500 | [diff] [blame] | 438 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 439 | sk_sp<GrTextureProxy> proxies[2]; |
Robert Phillips | 7eeb74f | 2019-03-29 07:26:46 -0400 | [diff] [blame] | 440 | if (!init_test_textures(resourceProvider, proxyProvider, &random, proxies)) { |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 441 | ERRORF(reporter, "Could not create test textures"); |
Robert Phillips | bb581ce | 2017-05-29 15:05:15 -0400 | [diff] [blame] | 442 | return; |
| 443 | } |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 444 | GrProcessorTestData testData(&random, context, rtc.get(), proxies); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 445 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 446 | // Coverage optimization uses three frames with a linearly transformed input texture. The first |
| 447 | // frame has no offset, second frames add .2 and .4, which should then be present as a fixed |
| 448 | // difference between the frame outputs if the FP is properly following the modulation |
| 449 | // requirements of the coverage optimization. |
| 450 | static constexpr SkScalar kInputDelta = 0.2f; |
| 451 | auto inputTexture1 = make_input_texture(proxyProvider, kRenderSize, kRenderSize, 0.0f); |
| 452 | auto inputTexture2 = make_input_texture(proxyProvider, kRenderSize, kRenderSize, kInputDelta); |
| 453 | auto inputTexture3 = make_input_texture(proxyProvider, kRenderSize, kRenderSize, 2*kInputDelta); |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 454 | |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 455 | // Encoded images are very verbose and this tests many potential images, so only export the |
| 456 | // first failure (subsequent failures have a reasonable chance of being related). |
| 457 | bool loggedFirstFailure = false; |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 458 | bool loggedFirstWarning = false; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 459 | |
| 460 | // Storage for the three frames required for coverage compatibility optimization. Each frame |
| 461 | // uses the correspondingly numbered inputTextureX. |
| 462 | std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]); |
| 463 | std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]); |
| 464 | std::unique_ptr<GrColor[]> readData3(new GrColor[kRenderSize * kRenderSize]); |
| 465 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 466 | // Because processor factories configure themselves in random ways, this is not exhaustive. |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 467 | for (int i = 0; i < FPFactory::Count(); ++i) { |
| 468 | int timesToInvokeFactory = 5; |
| 469 | // Increase the number of attempts if the FP has child FPs since optimizations likely depend |
| 470 | // on child optimizations being present. |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 471 | std::unique_ptr<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 472 | for (int j = 0; j < fp->numChildProcessors(); ++j) { |
| 473 | // This value made a reasonable trade off between time and coverage when this test was |
| 474 | // written. |
| 475 | timesToInvokeFactory *= FPFactory::Count() / 2; |
| 476 | } |
Brian Osman | 50ea3c0 | 2019-02-04 10:01:53 -0500 | [diff] [blame] | 477 | #if defined(__MSVC_RUNTIME_CHECKS) |
| 478 | // This test is infuriatingly slow with MSVC runtime checks enabled |
| 479 | timesToInvokeFactory = 1; |
| 480 | #endif |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 481 | for (int j = 0; j < timesToInvokeFactory; ++j) { |
| 482 | fp = FPFactory::MakeIdx(i, &testData); |
Robert Phillips | a91e0b7 | 2017-05-01 13:12:20 -0400 | [diff] [blame] | 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. |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 514 | #if defined(SK_BUILD_FOR_SKQP) |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 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 | |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 558 | SkPMColor4f input4f = SkPMColor4f::FromBytes_RGBA(input); |
| 559 | SkPMColor4f output4f = SkPMColor4f::FromBytes_RGBA(output); |
Brian Osman | 1d5b598 | 2018-10-01 13:41:39 -0400 | [diff] [blame] | 560 | SkPMColor4f expected4f; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 561 | if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) { |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 562 | float rDiff = fabsf(output4f.fR - expected4f.fR); |
| 563 | float gDiff = fabsf(output4f.fG - expected4f.fG); |
| 564 | float bDiff = fabsf(output4f.fB - expected4f.fB); |
| 565 | float aDiff = fabsf(output4f.fA - expected4f.fA); |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 566 | static constexpr float kTol = 4 / 255.f; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 567 | if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) { |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 568 | if (constMessage.isEmpty()) { |
| 569 | passing = false; |
| 570 | |
| 571 | constMessage.printf("Processor %s claimed output for const input " |
| 572 | "doesn't match actual output. Error: %f, Tolerance: %f, " |
| 573 | "input: (%f, %f, %f, %f), actual: (%f, %f, %f, %f), " |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 574 | "expected(%f, %f, %f, %f)", fp->name(), |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 575 | SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))), kTol, |
| 576 | input4f.fR, input4f.fG, input4f.fB, input4f.fA, |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 577 | output4f.fR, output4f.fG, output4f.fB, output4f.fA, |
| 578 | expected4f.fR, expected4f.fG, expected4f.fB, expected4f.fA); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 579 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 580 | } |
| 581 | } |
Brian Osman | 00b2939 | 2018-11-05 15:42:43 -0500 | [diff] [blame] | 582 | if (input4f.isOpaque() && fp->preservesOpaqueInput() && !output4f.isOpaque()) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 583 | passing = false; |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 584 | |
| 585 | if (opaqueMessage.isEmpty()) { |
| 586 | opaqueMessage.printf("Processor %s claimed opaqueness is preserved but " |
| 587 | "it is not. Input: 0x%08x, Output: 0x%08x.", |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 588 | fp->name(), input, output); |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 589 | } |
Brian Osman | bd1f76f | 2017-03-15 11:33:12 -0400 | [diff] [blame] | 590 | } |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 591 | |
| 592 | if (!passing) { |
| 593 | // Regardless of how many optimizations the pixel violates, count it as a |
| 594 | // single bad pixel. |
| 595 | failedPixelCount++; |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | // Finished analyzing the entire image, see if the number of pixel failures meets the |
| 601 | // threshold for an FP violating the optimization requirements. |
| 602 | if (failedPixelCount > kMaxAcceptableFailedPixels) { |
Michael Ludwig | 4504a652 | 2018-10-03 16:47:55 -0400 | [diff] [blame] | 603 | 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] | 604 | ", first failing pixel details are below:", |
| 605 | failedPixelCount, kRenderSize * kRenderSize, seed, |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 606 | fp->dumpInfo().c_str()); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 607 | |
| 608 | // Print first failing pixel's details. |
| 609 | if (!coverageMessage.isEmpty()) { |
| 610 | ERRORF(reporter, coverageMessage.c_str()); |
| 611 | } |
| 612 | if (!constMessage.isEmpty()) { |
| 613 | ERRORF(reporter, constMessage.c_str()); |
| 614 | } |
| 615 | if (!opaqueMessage.isEmpty()) { |
| 616 | ERRORF(reporter, opaqueMessage.c_str()); |
| 617 | } |
| 618 | |
| 619 | if (!loggedFirstFailure) { |
| 620 | // Print with ERRORF to make sure the encoded image is output |
| 621 | SkString input; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 622 | log_surface_proxy(context, inputTexture1, &input); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 623 | SkString output; |
| 624 | log_surface_context(rtc, &output); |
| 625 | ERRORF(reporter, "Input image: %s\n\n" |
| 626 | "===========================================================\n\n" |
| 627 | "Output image: %s\n", input.c_str(), output.c_str()); |
| 628 | loggedFirstFailure = true; |
| 629 | } |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 630 | } else if(failedPixelCount > 0) { |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 631 | // Don't trigger an error, but don't just hide the failures either. |
| 632 | INFOF(reporter, "Processor violated %d of %d pixels (below error threshold), seed: " |
| 633 | "0x%08x, processor: %s", failedPixelCount, kRenderSize * kRenderSize, |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 634 | seed, fp->dumpInfo().c_str()); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 635 | if (!coverageMessage.isEmpty()) { |
| 636 | INFOF(reporter, coverageMessage.c_str()); |
| 637 | } |
| 638 | if (!constMessage.isEmpty()) { |
| 639 | INFOF(reporter, constMessage.c_str()); |
| 640 | } |
| 641 | if (!opaqueMessage.isEmpty()) { |
| 642 | INFOF(reporter, opaqueMessage.c_str()); |
| 643 | } |
| 644 | if (!loggedFirstWarning) { |
| 645 | SkString input; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 646 | log_surface_proxy(context, inputTexture1, &input); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 647 | SkString output; |
| 648 | log_surface_context(rtc, &output); |
| 649 | INFOF(reporter, "Input image: %s\n\n" |
| 650 | "===========================================================\n\n" |
| 651 | "Output image: %s\n", input.c_str(), output.c_str()); |
| 652 | loggedFirstWarning = true; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | } |
| 656 | } |
| 657 | } |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 658 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 659 | // Tests that fragment processors returned by GrFragmentProcessor::clone() are equivalent to their |
| 660 | // progenitors. |
| 661 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) { |
| 662 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 663 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
| 664 | auto resourceProvider = context->priv().resourceProvider(); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 665 | |
| 666 | SkRandom random; |
| 667 | |
| 668 | // Make the destination context for the test. |
| 669 | static constexpr int kRenderSize = 1024; |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 670 | sk_sp<GrRenderTargetContext> rtc = context->priv().makeDeferredRenderTargetContext( |
Brian Salomon | 27ae52c | 2019-07-03 11:27:44 -0400 | [diff] [blame] | 671 | SkBackingFit::kExact, kRenderSize, kRenderSize, GrColorType::kRGBA_8888, nullptr); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 672 | |
| 673 | sk_sp<GrTextureProxy> proxies[2]; |
Robert Phillips | 7eeb74f | 2019-03-29 07:26:46 -0400 | [diff] [blame] | 674 | if (!init_test_textures(resourceProvider, proxyProvider, &random, proxies)) { |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 675 | ERRORF(reporter, "Could not create test textures"); |
| 676 | return; |
| 677 | } |
| 678 | GrProcessorTestData testData(&random, context, rtc.get(), proxies); |
| 679 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 680 | auto inputTexture = make_input_texture(proxyProvider, kRenderSize, kRenderSize, 0.0f); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 681 | std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]); |
| 682 | std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]); |
| 683 | auto readInfo = SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType, |
| 684 | kPremul_SkAlphaType); |
| 685 | |
| 686 | // Because processor factories configure themselves in random ways, this is not exhaustive. |
| 687 | for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) { |
| 688 | static constexpr int kTimesToInvokeFactory = 10; |
| 689 | for (int j = 0; j < kTimesToInvokeFactory; ++j) { |
| 690 | auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &testData); |
| 691 | auto clone = fp->clone(); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 692 | if (!clone) { |
Brian Salomon | 96271cd | 2017-07-31 16:27:23 -0400 | [diff] [blame] | 693 | ERRORF(reporter, "Clone of processor %s failed.", fp->name()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 694 | continue; |
| 695 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 696 | const char* name = fp->name(); |
Brian Salomon | ce06e26 | 2017-08-01 16:23:40 -0400 | [diff] [blame] | 697 | REPORTER_ASSERT(reporter, !strcmp(fp->name(), clone->name())); |
| 698 | REPORTER_ASSERT(reporter, fp->compatibleWithCoverageAsAlpha() == |
| 699 | clone->compatibleWithCoverageAsAlpha()); |
| 700 | REPORTER_ASSERT(reporter, fp->isEqual(*clone)); |
| 701 | REPORTER_ASSERT(reporter, fp->preservesOpaqueInput() == clone->preservesOpaqueInput()); |
| 702 | REPORTER_ASSERT(reporter, fp->hasConstantOutputForConstantInput() == |
| 703 | clone->hasConstantOutputForConstantInput()); |
| 704 | REPORTER_ASSERT(reporter, fp->numChildProcessors() == clone->numChildProcessors()); |
| 705 | REPORTER_ASSERT(reporter, fp->usesLocalCoords() == clone->usesLocalCoords()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 706 | // Draw with original and read back the results. |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 707 | render_fp(context, rtc.get(), fp.get(), inputTexture, readData1.get()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 708 | |
| 709 | // Draw with clone and read back the results. |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 710 | render_fp(context, rtc.get(), clone.get(), inputTexture, readData2.get()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 711 | |
| 712 | // Check that the results are the same. |
| 713 | bool passing = true; |
| 714 | for (int y = 0; y < kRenderSize && passing; ++y) { |
| 715 | for (int x = 0; x < kRenderSize && passing; ++x) { |
| 716 | int idx = y * kRenderSize + x; |
| 717 | if (readData1[idx] != readData2[idx]) { |
| 718 | ERRORF(reporter, |
| 719 | "Processor %s made clone produced different output. " |
| 720 | "Input color: 0x%08x, Original Output Color: 0x%08x, " |
| 721 | "Clone Output Color: 0x%08x..", |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 722 | name, input_texel_color(x, y, 0.0f), readData1[idx], readData2[idx]); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 723 | passing = false; |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 731 | #endif // GR_TEST_UTILS |