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 "tests/Test.h" |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/gpu/GrContext.h" |
| 11 | #include "include/gpu/GrGpuResource.h" |
| 12 | #include "src/gpu/GrClip.h" |
| 13 | #include "src/gpu/GrContextPriv.h" |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrImageInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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" |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 25 | #include <atomic> |
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 | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 34 | GrOpMemoryPool* pool = context->priv().opMemoryPool(); |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 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 | |
Chris Dalton | 1706cbf | 2019-05-21 19:35:29 -0600 | [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 | |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 47 | GrProcessorSet::Analysis finalize( |
| 48 | const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage, |
| 49 | GrClampType clampType) override { |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 50 | static constexpr GrProcessorAnalysisColor kUnknownColor; |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 51 | SkPMColor4f overrideColor; |
Chris Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 52 | return fProcessors.finalize( |
| 53 | kUnknownColor, GrProcessorAnalysisCoverage::kNone, clip, |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 54 | &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps, clampType, |
| 55 | &overrideColor); |
Brian Salomon | 649a341 | 2017-03-09 13:50:43 -0500 | [diff] [blame] | 56 | } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 57 | |
| 58 | private: |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 59 | friend class ::GrOpMemoryPool; // for ctor |
| 60 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 61 | TestOp(std::unique_ptr<GrFragmentProcessor> fp) |
| 62 | : INHERITED(ClassID()), fProcessors(std::move(fp)) { |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 63 | this->setBounds(SkRect::MakeWH(100, 100), HasAABloat::kNo, IsHairline::kNo); |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 64 | } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 65 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 66 | void onPrepareDraws(Target* target) override { return; } |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 67 | void onExecute(GrOpFlushState*, const SkRect&) override { return; } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 68 | |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 69 | GrProcessorSet fProcessors; |
| 70 | |
| 71 | typedef GrMeshDrawOp INHERITED; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | /** |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 75 | * 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] | 76 | * of resources owned by child FPs. |
| 77 | */ |
| 78 | class TestFP : public GrFragmentProcessor { |
| 79 | public: |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 80 | static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> child) { |
| 81 | return std::unique_ptr<GrFragmentProcessor>(new TestFP(std::move(child))); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 82 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 83 | static std::unique_ptr<GrFragmentProcessor> Make(const SkTArray<sk_sp<GrTextureProxy>>& proxies, |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 84 | const SkTArray<sk_sp<GrGpuBuffer>>& buffers) { |
Brian Salomon | 559f556 | 2017-11-15 14:28:33 -0500 | [diff] [blame] | 85 | return std::unique_ptr<GrFragmentProcessor>(new TestFP(proxies, buffers)); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | const char* name() const override { return "test"; } |
| 89 | |
| 90 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override { |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 91 | static std::atomic<int32_t> nextKey{0}; |
| 92 | b->add32(nextKey++); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 93 | } |
| 94 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 95 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 96 | return std::unique_ptr<GrFragmentProcessor>(new TestFP(*this)); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 97 | } |
| 98 | |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 99 | private: |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 100 | TestFP(const SkTArray<sk_sp<GrTextureProxy>>& proxies, |
| 101 | const SkTArray<sk_sp<GrGpuBuffer>>& buffers) |
Brian Salomon | 662ea4b | 2018-07-12 14:53:49 -0400 | [diff] [blame] | 102 | : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4) { |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 103 | for (const auto& proxy : proxies) { |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 104 | fSamplers.emplace_back(proxy); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 105 | } |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 106 | this->setTextureSamplerCnt(fSamplers.count()); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 107 | } |
| 108 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 109 | TestFP(std::unique_ptr<GrFragmentProcessor> child) |
Brian Salomon | 662ea4b | 2018-07-12 14:53:49 -0400 | [diff] [blame] | 110 | : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4) { |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 111 | this->registerChildProcessor(std::move(child)); |
| 112 | } |
| 113 | |
Brian Salomon | 96271cd | 2017-07-31 16:27:23 -0400 | [diff] [blame] | 114 | explicit TestFP(const TestFP& that) |
Brian Salomon | 662ea4b | 2018-07-12 14:53:49 -0400 | [diff] [blame] | 115 | : INHERITED(kTestFP_ClassID, that.optimizationFlags()), fSamplers(4) { |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 116 | for (int i = 0; i < that.fSamplers.count(); ++i) { |
| 117 | fSamplers.emplace_back(that.fSamplers[i]); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 118 | } |
Brian Salomon | 96271cd | 2017-07-31 16:27:23 -0400 | [diff] [blame] | 119 | for (int i = 0; i < that.numChildProcessors(); ++i) { |
| 120 | this->registerChildProcessor(that.childProcessor(i).clone()); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 121 | } |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 122 | this->setTextureSamplerCnt(fSamplers.count()); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 123 | } |
| 124 | |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 125 | virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
| 126 | class TestGLSLFP : public GrGLSLFragmentProcessor { |
| 127 | public: |
| 128 | TestGLSLFP() {} |
| 129 | void emitCode(EmitArgs& args) override { |
| 130 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 131 | fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor); |
| 132 | } |
| 133 | |
| 134 | private: |
| 135 | }; |
| 136 | return new TestGLSLFP(); |
| 137 | } |
| 138 | |
| 139 | bool onIsEqual(const GrFragmentProcessor&) const override { return false; } |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 140 | const TextureSampler& onTextureSampler(int i) const override { return fSamplers[i]; } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 141 | |
| 142 | GrTAllocator<TextureSampler> fSamplers; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 143 | typedef GrFragmentProcessor INHERITED; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 144 | }; |
| 145 | } |
| 146 | |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 147 | DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) { |
| 148 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 149 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 150 | |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 151 | GrSurfaceDesc desc; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 152 | desc.fWidth = 10; |
| 153 | desc.fHeight = 10; |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 154 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 155 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 156 | const GrBackendFormat format = |
Robert Phillips | 0a15cc6 | 2019-07-30 12:49:10 -0400 | [diff] [blame] | 157 | context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, |
| 158 | GrRenderable::kNo); |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 159 | |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 160 | for (bool makeClone : {false, true}) { |
| 161 | for (int parentCnt = 0; parentCnt < 2; parentCnt++) { |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 162 | auto renderTargetContext = context->priv().makeDeferredRenderTargetContext( |
| 163 | SkBackingFit::kApprox, 1, 1, GrColorType::kRGBA_8888, nullptr); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 164 | { |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 165 | sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy( |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 166 | format, desc, GrRenderable::kNo, 1, kTopLeft_GrSurfaceOrigin, |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 167 | GrMipMapped::kNo, SkBackingFit::kExact, SkBudgeted::kYes, GrProtected::kNo); |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 168 | |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 169 | { |
| 170 | SkTArray<sk_sp<GrTextureProxy>> proxies; |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 171 | SkTArray<sk_sp<GrGpuBuffer>> buffers; |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 172 | proxies.push_back(proxy); |
Brian Salomon | 559f556 | 2017-11-15 14:28:33 -0500 | [diff] [blame] | 173 | auto fp = TestFP::Make(std::move(proxies), std::move(buffers)); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 174 | for (int i = 0; i < parentCnt; ++i) { |
| 175 | fp = TestFP::Make(std::move(fp)); |
| 176 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 177 | std::unique_ptr<GrFragmentProcessor> clone; |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 178 | if (makeClone) { |
| 179 | clone = fp->clone(); |
| 180 | } |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 181 | std::unique_ptr<GrDrawOp> op(TestOp::Make(context, std::move(fp))); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 182 | renderTargetContext->priv().testingOnly_addDrawOp(std::move(op)); |
| 183 | if (clone) { |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 184 | op = TestOp::Make(context, std::move(clone)); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 185 | renderTargetContext->priv().testingOnly_addDrawOp(std::move(op)); |
| 186 | } |
| 187 | } |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 188 | |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 189 | // If the fp is cloned the number of refs should increase by one (for the clone) |
| 190 | int expectedProxyRefs = makeClone ? 3 : 2; |
| 191 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 192 | CheckSingleThreadedProxyRefs(reporter, proxy.get(), expectedProxyRefs, -1); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 193 | |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 194 | context->flush(); |
| 195 | |
Brian Salomon | 557e812 | 2019-10-24 10:37:08 -0400 | [diff] [blame] | 196 | // just one from the 'proxy' sk_sp |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 197 | CheckSingleThreadedProxyRefs(reporter, proxy.get(), 1, 1); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | } |
| 201 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 202 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 203 | #include "tools/flags/CommandLineFlags.h" |
Mike Klein | 84836b7 | 2019-03-21 11:31:36 -0500 | [diff] [blame] | 204 | static DEFINE_bool(randomProcessorTest, false, |
| 205 | "Use non-deterministic seed for random processor tests?"); |
Mike Klein | 5b3f343 | 2019-03-21 11:42:21 -0500 | [diff] [blame] | 206 | static DEFINE_int(processorSeed, 0, |
| 207 | "Use specific seed for processor tests. Overridden by --randomProcessorTest."); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 208 | |
| 209 | #if GR_TEST_UTILS |
| 210 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 211 | static GrColor input_texel_color(int i, int j, SkScalar delta) { |
| 212 | // Delta must be less than 0.5 to prevent over/underflow issues with the input color |
| 213 | SkASSERT(delta <= 0.5); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 214 | |
Brian Osman | 50ea3c0 | 2019-02-04 10:01:53 -0500 | [diff] [blame] | 215 | SkColor color = SkColorSetARGB((uint8_t)(i & 0xFF), |
| 216 | (uint8_t)(j & 0xFF), |
| 217 | (uint8_t)((i + j) & 0xFF), |
| 218 | (uint8_t)((2 * j - i) & 0xFF)); |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 219 | SkColor4f color4f = SkColor4f::FromColor(color); |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 220 | // We only apply delta to the r,g, and b channels. This is because we're using this |
| 221 | // to test the canTweakAlphaForCoverage() optimization. A processor is allowed |
| 222 | // to use the input color's alpha in its calculation and report this optimization. |
| 223 | for (int i = 0; i < 3; i++) { |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 224 | if (color4f[i] > 0.5) { |
| 225 | color4f[i] -= delta; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 226 | } else { |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 227 | color4f[i] += delta; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 228 | } |
| 229 | } |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 230 | return color4f.premul().toBytes_RGBA(); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 231 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 232 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 233 | void test_draw_op(GrContext* context, |
| 234 | GrRenderTargetContext* rtc, |
| 235 | std::unique_ptr<GrFragmentProcessor> fp, |
Greg Daniel | c594e62 | 2019-10-15 14:01:49 -0400 | [diff] [blame] | 236 | sk_sp<GrTextureProxy> inputDataProxy, |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 237 | SkAlphaType inputAlphaType) { |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 238 | GrPaint paint; |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 239 | paint.addColorTextureProcessor(std::move(inputDataProxy), inputAlphaType, SkMatrix::I()); |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 240 | paint.addColorFragmentProcessor(std::move(fp)); |
| 241 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 242 | |
Michael Ludwig | aa1b6b3 | 2019-05-29 14:43:13 -0400 | [diff] [blame] | 243 | auto op = GrFillRectOp::MakeNonAARect(context, std::move(paint), SkMatrix::I(), |
| 244 | SkRect::MakeWH(rtc->width(), rtc->height())); |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 245 | rtc->priv().testingOnly_addDrawOp(std::move(op)); |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 246 | } |
| 247 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 248 | // This assumes that the output buffer will be the same size as inputDataProxy |
| 249 | void render_fp(GrContext* context, GrRenderTargetContext* rtc, GrFragmentProcessor* fp, |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 250 | sk_sp<GrTextureProxy> inputDataProxy, SkAlphaType inputAlphaType, GrColor* buffer) { |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 251 | // test_draw_op needs to take ownership of an FP, so give it a clone that it can own |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 252 | test_draw_op(context, rtc, fp->clone(), inputDataProxy, inputAlphaType); |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 253 | memset(buffer, 0x0, sizeof(GrColor) * inputDataProxy->width() * inputDataProxy->height()); |
| 254 | rtc->readPixels(SkImageInfo::Make(inputDataProxy->dimensions(), kRGBA_8888_SkColorType, |
| 255 | kPremul_SkAlphaType), |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 256 | buffer, 0, {0, 0}); |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 257 | } |
| 258 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 259 | /** 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] | 260 | bool init_test_textures(GrResourceProvider* resourceProvider, |
| 261 | GrProxyProvider* proxyProvider, |
| 262 | SkRandom* random, |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 263 | GrProcessorTestData::ProxyInfo proxies[2]) { |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 264 | static const int kTestTextureSize = 256; |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 265 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 266 | { |
| 267 | // Put premul data into the RGBA texture that the test FPs can optionally use. |
| 268 | std::unique_ptr<GrColor[]> rgbaData(new GrColor[kTestTextureSize * kTestTextureSize]); |
| 269 | for (int y = 0; y < kTestTextureSize; ++y) { |
| 270 | for (int x = 0; x < kTestTextureSize; ++x) { |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 271 | rgbaData[kTestTextureSize * y + x] = input_texel_color( |
| 272 | random->nextULessThan(256), random->nextULessThan(256), 0.0f); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 273 | } |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 274 | } |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 275 | |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 276 | SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize, |
| 277 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 278 | SkPixmap pixmap(ii, rgbaData.get(), ii.minRowBytes()); |
| 279 | sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap); |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 280 | auto proxy = |
Brian Salomon | 96b383a | 2019-08-13 16:55:41 -0400 | [diff] [blame] | 281 | proxyProvider->createTextureProxy(img, 1, SkBudgeted::kYes, SkBackingFit::kExact); |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 282 | if (!proxy || !proxy->instantiate(resourceProvider)) { |
| 283 | return false; |
| 284 | } |
| 285 | proxies[0] = {std::move(proxy), GrColorType::kRGBA_8888, kPremul_SkAlphaType}; |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 286 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 287 | |
| 288 | { |
| 289 | // 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] | 290 | std::unique_ptr<uint8_t[]> alphaData(new uint8_t[kTestTextureSize * kTestTextureSize]); |
| 291 | for (int y = 0; y < kTestTextureSize; ++y) { |
| 292 | for (int x = 0; x < kTestTextureSize; ++x) { |
| 293 | alphaData[kTestTextureSize * y + x] = random->nextULessThan(256); |
| 294 | } |
| 295 | } |
| 296 | |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 297 | SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize, |
| 298 | kAlpha_8_SkColorType, kPremul_SkAlphaType); |
| 299 | SkPixmap pixmap(ii, alphaData.get(), ii.minRowBytes()); |
| 300 | sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap); |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 301 | auto proxy = |
Brian Salomon | 96b383a | 2019-08-13 16:55:41 -0400 | [diff] [blame] | 302 | proxyProvider->createTextureProxy(img, 1, SkBudgeted::kYes, SkBackingFit::kExact); |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 303 | if (!proxy || !proxy->instantiate(resourceProvider)) { |
| 304 | return false; |
| 305 | } |
| 306 | proxies[1] = {std::move(proxy), GrColorType::kAlpha_8, kPremul_SkAlphaType}; |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 307 | } |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 308 | |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 309 | return true; |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | // Creates a texture of premul colors used as the output of the fragment processor that precedes |
| 313 | // 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] | 314 | sk_sp<GrTextureProxy> make_input_texture(GrProxyProvider* proxyProvider, int width, int height, |
| 315 | SkScalar delta) { |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 316 | std::unique_ptr<GrColor[]> data(new GrColor[width * height]); |
| 317 | for (int y = 0; y < width; ++y) { |
| 318 | for (int x = 0; x < height; ++x) { |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 319 | data.get()[width * y + x] = input_texel_color(x, y, delta); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 320 | } |
| 321 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 322 | |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 323 | SkImageInfo ii = SkImageInfo::Make(width, height, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 324 | SkPixmap pixmap(ii, data.get(), ii.minRowBytes()); |
| 325 | sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap); |
Brian Salomon | 96b383a | 2019-08-13 16:55:41 -0400 | [diff] [blame] | 326 | return proxyProvider->createTextureProxy(img, 1, SkBudgeted::kYes, SkBackingFit::kExact); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 327 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 328 | |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 329 | // We tag logged data as unpremul to avoid conversion when encoding as PNG. The input texture |
| 330 | // actually contains unpremul data. Also, even though we made the result data by rendering into |
| 331 | // a "unpremul" GrRenderTargetContext, our input texture is unpremul and outside of the random |
| 332 | // effect configuration, we didn't do anything to ensure the output is actually premul. We just |
| 333 | // don't currently allow kUnpremul GrRenderTargetContexts. |
| 334 | static constexpr auto kLogAlphaType = kUnpremul_SkAlphaType; |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 335 | |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 336 | bool log_pixels(GrColor* pixels, int widthHeight, SkString* dst) { |
| 337 | auto info = SkImageInfo::Make(widthHeight, widthHeight, kRGBA_8888_SkColorType, kLogAlphaType); |
| 338 | SkBitmap bmp; |
| 339 | bmp.installPixels(info, pixels, widthHeight * sizeof(GrColor)); |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 340 | return BipmapToBase64DataURI(bmp, dst); |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 341 | } |
| 342 | |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 343 | bool log_texture_proxy(GrContext* context, sk_sp<GrTextureProxy> src, SkString* dst) { |
Greg Daniel | bfa19c4 | 2019-12-19 16:41:40 -0500 | [diff] [blame] | 344 | auto sContext = GrSurfaceContext::Make(context, src, GrColorType::kRGBA_8888, kLogAlphaType, |
| 345 | nullptr); |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 346 | SkImageInfo ii = SkImageInfo::Make(src->dimensions(), kRGBA_8888_SkColorType, kLogAlphaType); |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 347 | SkBitmap bm; |
| 348 | SkAssertResult(bm.tryAllocPixels(ii)); |
| 349 | SkAssertResult(sContext->readPixels(ii, bm.getPixels(), bm.rowBytes(), {0, 0})); |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 350 | return BipmapToBase64DataURI(bm, dst); |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 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 |
Brian Salomon | 682ba43 | 2019-12-17 20:20:23 +0000 | [diff] [blame] | 355 | // from that, it is not uncommon for a valid output to differ from estimate by up to 0.01 |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 356 | // (really 1/128 ~ .0078, but frequently floating point issues make that tolerance a little |
| 357 | // too unforgiving). |
Brian Salomon | 682ba43 | 2019-12-17 20:20:23 +0000 | [diff] [blame] | 358 | static constexpr SkScalar kTolerance = 0.01f; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 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 | |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 367 | // Given three input colors (color preceding the FP being tested) provided to the FP at the same |
| 368 | // local coord and the three corresponding FP outputs, this ensures that either: |
| 369 | // out[0] = fp * in[0].a, out[1] = fp * in[1].a, and out[2] = fp * in[2].a |
| 370 | // where fp is the pre-modulated color that should not be changing across frames (FP's state doesn't |
| 371 | // change), OR: |
| 372 | // out[0] = fp * in[0], out[1] = fp * in[1], and out[2] = fp * in[2] |
| 373 | // (per-channel modulation instead of modulation by just the alpha channel) |
| 374 | // It does this by estimating the pre-modulated fp color from one of the input/output pairs and |
| 375 | // confirms the conditions hold for the other two pairs. |
| 376 | // It is required that the three input colors have the same alpha as fp is allowed to be a function |
| 377 | // of the input alpha (but not r, g, or b). |
| 378 | bool legal_modulation(const GrColor in[3], const GrColor out[3]) { |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 379 | // Convert to floating point, which is the number space the FP operates in (more or less) |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 380 | SkPMColor4f inf[3], outf[3]; |
| 381 | for (int i = 0; i < 3; ++i) { |
| 382 | inf[i] = SkPMColor4f::FromBytes_RGBA(in[i]); |
| 383 | outf[i] = SkPMColor4f::FromBytes_RGBA(out[i]); |
| 384 | } |
| 385 | // This test is only valid if all the input alphas are the same. |
| 386 | SkASSERT(inf[0].fA == inf[1].fA && inf[1].fA == inf[2].fA); |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 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). |
Brian Salomon | 0a7ca7a | 2019-12-27 12:18:19 -0500 | [diff] [blame] | 392 | SkPMColor4f fpPreColorModulation = {0,0,0,0}; |
| 393 | SkPMColor4f fpPreAlphaModulation = {0,0,0,0}; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 394 | for (int i = 0; i < 4; i++) { |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 395 | // Use the most stepped up frame |
| 396 | int maxInIdx = inf[0][i] > inf[1][i] ? 0 : 1; |
| 397 | maxInIdx = inf[maxInIdx][i] > inf[2][i] ? maxInIdx : 2; |
| 398 | const auto& in = inf[maxInIdx]; |
| 399 | const auto& out = outf[maxInIdx]; |
Brian Salomon | 0a7ca7a | 2019-12-27 12:18:19 -0500 | [diff] [blame] | 400 | if (in[i] > 0) { |
| 401 | fpPreColorModulation[i] = out[i] / in[i]; |
| 402 | } |
| 403 | if (in[3] > 0) { |
| 404 | fpPreAlphaModulation[i] = out[i] / in[3]; |
| 405 | } |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | // With reconstructed pre-modulated FP output, derive the expected value of fp * input for each |
| 409 | // of the transformed input colors. |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 410 | SkPMColor4f expectedForAlphaModulation[3]; |
| 411 | SkPMColor4f expectedForColorModulation[3]; |
| 412 | for (int i = 0; i < 3; ++i) { |
| 413 | expectedForAlphaModulation[i] = fpPreAlphaModulation * inf[i].fA; |
| 414 | expectedForColorModulation[i] = fpPreColorModulation * inf[i]; |
| 415 | // If the input alpha is 0 then the other channels should also be zero |
| 416 | // since the color is assumed to be premul. Modulating zeros by anything |
| 417 | // should produce zeros. |
| 418 | if (inf[i].fA == 0) { |
| 419 | SkASSERT(inf[i].fR == 0 && inf[i].fG == 0 && inf[i].fB == 0); |
| 420 | expectedForColorModulation[i] = expectedForAlphaModulation[i] = {0, 0, 0, 0}; |
| 421 | } |
| 422 | } |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 423 | |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 424 | bool isLegalColorModulation = fuzzy_color_equals(outf[0], expectedForColorModulation[0]) && |
| 425 | fuzzy_color_equals(outf[1], expectedForColorModulation[1]) && |
| 426 | fuzzy_color_equals(outf[2], expectedForColorModulation[2]); |
| 427 | |
| 428 | bool isLegalAlphaModulation = fuzzy_color_equals(outf[0], expectedForAlphaModulation[0]) && |
| 429 | fuzzy_color_equals(outf[1], expectedForAlphaModulation[1]) && |
| 430 | fuzzy_color_equals(outf[2], expectedForAlphaModulation[2]); |
| 431 | |
| 432 | // This can be enabled to print the values that caused this check to fail. |
| 433 | if (0 && !isLegalColorModulation && !isLegalAlphaModulation) { |
| 434 | SkDebugf("Color modulation test\n\timplied mod color: (%.03f, %.03f, %.03f, %.03f)\n", |
| 435 | fpPreColorModulation[0], |
| 436 | fpPreColorModulation[1], |
| 437 | fpPreColorModulation[2], |
| 438 | fpPreColorModulation[3]); |
| 439 | for (int i = 0; i < 3; ++i) { |
| 440 | SkDebugf("\t(%.03f, %.03f, %.03f, %.03f) -> " |
| 441 | "(%.03f, %.03f, %.03f, %.03f) | " |
| 442 | "(%.03f, %.03f, %.03f, %.03f), ok: %d\n", |
| 443 | inf[i].fR, inf[i].fG, inf[i].fB, inf[i].fA, |
| 444 | outf[i].fR, outf[i].fG, outf[i].fB, outf[i].fA, |
| 445 | expectedForColorModulation[i].fR, expectedForColorModulation[i].fG, |
| 446 | expectedForColorModulation[i].fB, expectedForColorModulation[i].fA, |
| 447 | fuzzy_color_equals(outf[i], expectedForColorModulation[i])); |
| 448 | } |
| 449 | SkDebugf("Alpha modulation test\n\timplied mod color: (%.03f, %.03f, %.03f, %.03f)\n", |
| 450 | fpPreAlphaModulation[0], |
| 451 | fpPreAlphaModulation[1], |
| 452 | fpPreAlphaModulation[2], |
| 453 | fpPreAlphaModulation[3]); |
| 454 | for (int i = 0; i < 3; ++i) { |
| 455 | SkDebugf("\t(%.03f, %.03f, %.03f, %.03f) -> " |
| 456 | "(%.03f, %.03f, %.03f, %.03f) | " |
| 457 | "(%.03f, %.03f, %.03f, %.03f), ok: %d\n", |
| 458 | inf[i].fR, inf[i].fG, inf[i].fB, inf[i].fA, |
| 459 | outf[i].fR, outf[i].fG, outf[i].fB, outf[i].fA, |
| 460 | expectedForAlphaModulation[i].fR, expectedForAlphaModulation[i].fG, |
| 461 | expectedForAlphaModulation[i].fB, expectedForAlphaModulation[i].fA, |
| 462 | fuzzy_color_equals(outf[i], expectedForAlphaModulation[i])); |
| 463 | } |
| 464 | } |
| 465 | return isLegalColorModulation || isLegalAlphaModulation; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 466 | } |
| 467 | |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 468 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 469 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 470 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
| 471 | auto resourceProvider = context->priv().resourceProvider(); |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 472 | using FPFactory = GrFragmentProcessorTestFactory; |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 473 | |
Michael Ludwig | 3ad86d0 | 2018-09-28 16:00:38 -0400 | [diff] [blame] | 474 | uint32_t seed = FLAGS_processorSeed; |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 475 | if (FLAGS_randomProcessorTest) { |
| 476 | std::random_device rd; |
| 477 | seed = rd(); |
| 478 | } |
| 479 | // 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] | 480 | // use --processorSeed <seed> (without --randomProcessorTest) to reproduce. |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 481 | SkRandom random(seed); |
| 482 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 483 | // Make the destination context for the test. |
| 484 | static constexpr int kRenderSize = 256; |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 485 | auto rtc = context->priv().makeDeferredRenderTargetContext( |
Brian Salomon | 27ae52c | 2019-07-03 11:27:44 -0400 | [diff] [blame] | 486 | SkBackingFit::kExact, kRenderSize, kRenderSize, GrColorType::kRGBA_8888, nullptr); |
Brian Salomon | d1a8bdf | 2017-02-10 12:39:26 -0500 | [diff] [blame] | 487 | |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 488 | GrProcessorTestData::ProxyInfo proxies[2]; |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 489 | if (!init_test_textures(resourceProvider, proxyProvider, &random, proxies)) { |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 490 | ERRORF(reporter, "Could not create test textures"); |
Robert Phillips | bb581ce | 2017-05-29 15:05:15 -0400 | [diff] [blame] | 491 | return; |
| 492 | } |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 493 | GrProcessorTestData testData(&random, context, 2, proxies); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 494 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 495 | // Coverage optimization uses three frames with a linearly transformed input texture. The first |
| 496 | // frame has no offset, second frames add .2 and .4, which should then be present as a fixed |
| 497 | // difference between the frame outputs if the FP is properly following the modulation |
| 498 | // requirements of the coverage optimization. |
| 499 | static constexpr SkScalar kInputDelta = 0.2f; |
| 500 | auto inputTexture1 = make_input_texture(proxyProvider, kRenderSize, kRenderSize, 0.0f); |
| 501 | auto inputTexture2 = make_input_texture(proxyProvider, kRenderSize, kRenderSize, kInputDelta); |
| 502 | auto inputTexture3 = make_input_texture(proxyProvider, kRenderSize, kRenderSize, 2*kInputDelta); |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 503 | |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 504 | // Encoded images are very verbose and this tests many potential images, so only export the |
| 505 | // first failure (subsequent failures have a reasonable chance of being related). |
| 506 | bool loggedFirstFailure = false; |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 507 | bool loggedFirstWarning = false; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 508 | |
| 509 | // Storage for the three frames required for coverage compatibility optimization. Each frame |
| 510 | // uses the correspondingly numbered inputTextureX. |
| 511 | std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]); |
| 512 | std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]); |
| 513 | std::unique_ptr<GrColor[]> readData3(new GrColor[kRenderSize * kRenderSize]); |
| 514 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 515 | // Because processor factories configure themselves in random ways, this is not exhaustive. |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 516 | for (int i = 0; i < FPFactory::Count(); ++i) { |
| 517 | int timesToInvokeFactory = 5; |
| 518 | // Increase the number of attempts if the FP has child FPs since optimizations likely depend |
| 519 | // on child optimizations being present. |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 520 | std::unique_ptr<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 521 | for (int j = 0; j < fp->numChildProcessors(); ++j) { |
| 522 | // This value made a reasonable trade off between time and coverage when this test was |
| 523 | // written. |
| 524 | timesToInvokeFactory *= FPFactory::Count() / 2; |
| 525 | } |
Brian Osman | 50ea3c0 | 2019-02-04 10:01:53 -0500 | [diff] [blame] | 526 | #if defined(__MSVC_RUNTIME_CHECKS) |
| 527 | // This test is infuriatingly slow with MSVC runtime checks enabled |
| 528 | timesToInvokeFactory = 1; |
| 529 | #endif |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 530 | for (int j = 0; j < timesToInvokeFactory; ++j) { |
| 531 | fp = FPFactory::MakeIdx(i, &testData); |
Robert Phillips | a91e0b7 | 2017-05-01 13:12:20 -0400 | [diff] [blame] | 532 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 533 | if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() && |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 534 | !fp->compatibleWithCoverageAsAlpha()) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 535 | continue; |
| 536 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 537 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 538 | if (fp->compatibleWithCoverageAsAlpha()) { |
| 539 | // 2nd and 3rd frames are only used when checking coverage optimization |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 540 | render_fp(context, rtc.get(), fp.get(), inputTexture2, kPremul_SkAlphaType, |
Greg Daniel | c594e62 | 2019-10-15 14:01:49 -0400 | [diff] [blame] | 541 | readData2.get()); |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 542 | render_fp(context, rtc.get(), fp.get(), inputTexture3, kPremul_SkAlphaType, |
Greg Daniel | c594e62 | 2019-10-15 14:01:49 -0400 | [diff] [blame] | 543 | readData3.get()); |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 544 | } |
| 545 | // Draw base frame last so that rtc holds the original FP behavior if we need to |
| 546 | // dump the image to the log. |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 547 | render_fp(context, rtc.get(), fp.get(), inputTexture1, kPremul_SkAlphaType, |
Greg Daniel | c594e62 | 2019-10-15 14:01:49 -0400 | [diff] [blame] | 548 | readData1.get()); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 549 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 550 | if (0) { // Useful to see what FPs are being tested. |
| 551 | SkString children; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 552 | for (int c = 0; c < fp->numChildProcessors(); ++c) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 553 | if (!c) { |
| 554 | children.append("("); |
| 555 | } |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 556 | children.append(fp->childProcessor(c).name()); |
| 557 | children.append(c == fp->numChildProcessors() - 1 ? ")" : ", "); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 558 | } |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 559 | SkDebugf("%s %s\n", fp->name(), children.c_str()); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 560 | } |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 561 | |
| 562 | // This test has a history of being flaky on a number of devices. If an FP is logically |
| 563 | // violating the optimizations, it's reasonable to expect it to violate requirements on |
| 564 | // a large number of pixels in the image. Sporadic pixel violations are more indicative |
| 565 | // of device errors and represents a separate problem. |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 566 | #if defined(SK_BUILD_FOR_SKQP) |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 567 | static constexpr int kMaxAcceptableFailedPixels = 0; // Strict when running as SKQP |
| 568 | #else |
| 569 | static constexpr int kMaxAcceptableFailedPixels = 2 * kRenderSize; // ~0.7% of the image |
| 570 | #endif |
| 571 | |
| 572 | int failedPixelCount = 0; |
| 573 | // Collect first optimization failure message, to be output later as a warning or an |
| 574 | // error depending on whether the rendering "passed" or failed. |
| 575 | SkString coverageMessage; |
| 576 | SkString opaqueMessage; |
| 577 | SkString constMessage; |
| 578 | for (int y = 0; y < kRenderSize; ++y) { |
| 579 | for (int x = 0; x < kRenderSize; ++x) { |
| 580 | bool passing = true; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 581 | GrColor input = input_texel_color(x, y, 0.0f); |
| 582 | GrColor output = readData1.get()[y * kRenderSize + x]; |
| 583 | |
| 584 | if (fp->compatibleWithCoverageAsAlpha()) { |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 585 | GrColor ins[3]; |
| 586 | ins[0] = input; |
| 587 | ins[1] = input_texel_color(x, y, kInputDelta); |
| 588 | ins[2] = input_texel_color(x, y, 2 * kInputDelta); |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 589 | |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 590 | GrColor outs[3]; |
| 591 | outs[0] = output; |
| 592 | outs[1] = readData2.get()[y * kRenderSize + x]; |
| 593 | outs[2] = readData3.get()[y * kRenderSize + x]; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 594 | |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 595 | if (!legal_modulation(ins, outs)) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 596 | passing = false; |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 597 | if (coverageMessage.isEmpty()) { |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 598 | coverageMessage.printf( |
| 599 | "\"Modulating\" processor %s did not match " |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 600 | "alpha-modulation nor color-modulation rules. " |
| 601 | "Input: 0x%08x, Output: 0x%08x, pixel (%d, %d).", |
| 602 | fp->name(), input, output, x, y); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 603 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 604 | } |
| 605 | } |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 606 | |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 607 | SkPMColor4f input4f = SkPMColor4f::FromBytes_RGBA(input); |
| 608 | SkPMColor4f output4f = SkPMColor4f::FromBytes_RGBA(output); |
Brian Osman | 1d5b598 | 2018-10-01 13:41:39 -0400 | [diff] [blame] | 609 | SkPMColor4f expected4f; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 610 | if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) { |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 611 | float rDiff = fabsf(output4f.fR - expected4f.fR); |
| 612 | float gDiff = fabsf(output4f.fG - expected4f.fG); |
| 613 | float bDiff = fabsf(output4f.fB - expected4f.fB); |
| 614 | float aDiff = fabsf(output4f.fA - expected4f.fA); |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 615 | static constexpr float kTol = 4 / 255.f; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 616 | if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) { |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 617 | if (constMessage.isEmpty()) { |
| 618 | passing = false; |
| 619 | |
| 620 | constMessage.printf("Processor %s claimed output for const input " |
| 621 | "doesn't match actual output. Error: %f, Tolerance: %f, " |
| 622 | "input: (%f, %f, %f, %f), actual: (%f, %f, %f, %f), " |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 623 | "expected(%f, %f, %f, %f)", fp->name(), |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 624 | SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))), kTol, |
| 625 | input4f.fR, input4f.fG, input4f.fB, input4f.fA, |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 626 | output4f.fR, output4f.fG, output4f.fB, output4f.fA, |
| 627 | expected4f.fR, expected4f.fG, expected4f.fB, expected4f.fA); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 628 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 629 | } |
| 630 | } |
Brian Osman | 00b2939 | 2018-11-05 15:42:43 -0500 | [diff] [blame] | 631 | if (input4f.isOpaque() && fp->preservesOpaqueInput() && !output4f.isOpaque()) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 632 | passing = false; |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 633 | |
| 634 | if (opaqueMessage.isEmpty()) { |
| 635 | opaqueMessage.printf("Processor %s claimed opaqueness is preserved but " |
| 636 | "it is not. Input: 0x%08x, Output: 0x%08x.", |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 637 | fp->name(), input, output); |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 638 | } |
Brian Osman | bd1f76f | 2017-03-15 11:33:12 -0400 | [diff] [blame] | 639 | } |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 640 | |
| 641 | if (!passing) { |
| 642 | // Regardless of how many optimizations the pixel violates, count it as a |
| 643 | // single bad pixel. |
| 644 | failedPixelCount++; |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | // Finished analyzing the entire image, see if the number of pixel failures meets the |
| 650 | // threshold for an FP violating the optimization requirements. |
| 651 | if (failedPixelCount > kMaxAcceptableFailedPixels) { |
Michael Ludwig | 4504a652 | 2018-10-03 16:47:55 -0400 | [diff] [blame] | 652 | 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] | 653 | ", first failing pixel details are below:", |
| 654 | failedPixelCount, kRenderSize * kRenderSize, seed, |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 655 | fp->dumpInfo().c_str()); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 656 | |
| 657 | // Print first failing pixel's details. |
| 658 | if (!coverageMessage.isEmpty()) { |
| 659 | ERRORF(reporter, coverageMessage.c_str()); |
| 660 | } |
| 661 | if (!constMessage.isEmpty()) { |
| 662 | ERRORF(reporter, constMessage.c_str()); |
| 663 | } |
| 664 | if (!opaqueMessage.isEmpty()) { |
| 665 | ERRORF(reporter, opaqueMessage.c_str()); |
| 666 | } |
| 667 | |
| 668 | if (!loggedFirstFailure) { |
| 669 | // Print with ERRORF to make sure the encoded image is output |
| 670 | SkString input; |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 671 | log_texture_proxy(context, inputTexture1, &input); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 672 | SkString output; |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 673 | log_pixels(readData1.get(), kRenderSize, &output); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 674 | ERRORF(reporter, "Input image: %s\n\n" |
| 675 | "===========================================================\n\n" |
| 676 | "Output image: %s\n", input.c_str(), output.c_str()); |
| 677 | loggedFirstFailure = true; |
| 678 | } |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 679 | } else if(failedPixelCount > 0) { |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 680 | // Don't trigger an error, but don't just hide the failures either. |
| 681 | INFOF(reporter, "Processor violated %d of %d pixels (below error threshold), seed: " |
| 682 | "0x%08x, processor: %s", failedPixelCount, kRenderSize * kRenderSize, |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 683 | seed, fp->dumpInfo().c_str()); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 684 | if (!coverageMessage.isEmpty()) { |
| 685 | INFOF(reporter, coverageMessage.c_str()); |
| 686 | } |
| 687 | if (!constMessage.isEmpty()) { |
| 688 | INFOF(reporter, constMessage.c_str()); |
| 689 | } |
| 690 | if (!opaqueMessage.isEmpty()) { |
| 691 | INFOF(reporter, opaqueMessage.c_str()); |
| 692 | } |
| 693 | if (!loggedFirstWarning) { |
| 694 | SkString input; |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 695 | log_texture_proxy(context, inputTexture1, &input); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 696 | SkString output; |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 697 | log_pixels(readData1.get(), kRenderSize, &output); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 698 | INFOF(reporter, "Input image: %s\n\n" |
| 699 | "===========================================================\n\n" |
| 700 | "Output image: %s\n", input.c_str(), output.c_str()); |
| 701 | loggedFirstWarning = true; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 702 | } |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | } |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 707 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 708 | // Tests that fragment processors returned by GrFragmentProcessor::clone() are equivalent to their |
| 709 | // progenitors. |
| 710 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) { |
| 711 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 712 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
| 713 | auto resourceProvider = context->priv().resourceProvider(); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 714 | |
| 715 | SkRandom random; |
| 716 | |
| 717 | // Make the destination context for the test. |
| 718 | static constexpr int kRenderSize = 1024; |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 719 | auto rtc = context->priv().makeDeferredRenderTargetContext( |
Brian Salomon | 27ae52c | 2019-07-03 11:27:44 -0400 | [diff] [blame] | 720 | SkBackingFit::kExact, kRenderSize, kRenderSize, GrColorType::kRGBA_8888, nullptr); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 721 | |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 722 | GrProcessorTestData::ProxyInfo proxies[2]; |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 723 | if (!init_test_textures(resourceProvider, proxyProvider, &random, proxies)) { |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 724 | ERRORF(reporter, "Could not create test textures"); |
| 725 | return; |
| 726 | } |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 727 | GrProcessorTestData testData(&random, context, 2, proxies); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 728 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 729 | auto inputTexture = make_input_texture(proxyProvider, kRenderSize, kRenderSize, 0.0f); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 730 | std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]); |
| 731 | std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]); |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 732 | // On failure we write out images, but just write the first failing set as the print is very |
| 733 | // large. |
| 734 | bool loggedFirstFailure = false; |
| 735 | |
| 736 | // This test has a history of being flaky on a number of devices. If an FP clone is logically |
| 737 | // wrong, it's reasonable to expect it produce a large number of pixel differences in the image |
| 738 | // Sporadic pixel violations are more indicative device errors and represents a separate |
| 739 | // problem. |
| 740 | #if defined(SK_BUILD_FOR_SKQP) |
| 741 | static constexpr int kMaxAcceptableFailedPixels = 0; // Strict when running as SKQP |
| 742 | #else |
| 743 | static constexpr int kMaxAcceptableFailedPixels = 2 * kRenderSize; // ~0.7% of the image |
| 744 | #endif |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 745 | |
| 746 | // Because processor factories configure themselves in random ways, this is not exhaustive. |
| 747 | for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) { |
| 748 | static constexpr int kTimesToInvokeFactory = 10; |
| 749 | for (int j = 0; j < kTimesToInvokeFactory; ++j) { |
| 750 | auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &testData); |
| 751 | auto clone = fp->clone(); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 752 | if (!clone) { |
Brian Salomon | 96271cd | 2017-07-31 16:27:23 -0400 | [diff] [blame] | 753 | ERRORF(reporter, "Clone of processor %s failed.", fp->name()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 754 | continue; |
| 755 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 756 | const char* name = fp->name(); |
Brian Salomon | ce06e26 | 2017-08-01 16:23:40 -0400 | [diff] [blame] | 757 | REPORTER_ASSERT(reporter, !strcmp(fp->name(), clone->name())); |
| 758 | REPORTER_ASSERT(reporter, fp->compatibleWithCoverageAsAlpha() == |
| 759 | clone->compatibleWithCoverageAsAlpha()); |
| 760 | REPORTER_ASSERT(reporter, fp->isEqual(*clone)); |
| 761 | REPORTER_ASSERT(reporter, fp->preservesOpaqueInput() == clone->preservesOpaqueInput()); |
| 762 | REPORTER_ASSERT(reporter, fp->hasConstantOutputForConstantInput() == |
| 763 | clone->hasConstantOutputForConstantInput()); |
| 764 | REPORTER_ASSERT(reporter, fp->numChildProcessors() == clone->numChildProcessors()); |
| 765 | REPORTER_ASSERT(reporter, fp->usesLocalCoords() == clone->usesLocalCoords()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 766 | // Draw with original and read back the results. |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 767 | render_fp(context, rtc.get(), fp.get(), inputTexture, kPremul_SkAlphaType, |
Greg Daniel | c594e62 | 2019-10-15 14:01:49 -0400 | [diff] [blame] | 768 | readData1.get()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 769 | |
| 770 | // Draw with clone and read back the results. |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 771 | render_fp(context, rtc.get(), clone.get(), inputTexture, kPremul_SkAlphaType, |
Greg Daniel | c594e62 | 2019-10-15 14:01:49 -0400 | [diff] [blame] | 772 | readData2.get()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 773 | |
| 774 | // Check that the results are the same. |
| 775 | bool passing = true; |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 776 | int failedPixelCount = 0; |
| 777 | int firstWrongX = 0; |
| 778 | int firstWrongY = 0; |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 779 | for (int y = 0; y < kRenderSize && passing; ++y) { |
| 780 | for (int x = 0; x < kRenderSize && passing; ++x) { |
| 781 | int idx = y * kRenderSize + x; |
| 782 | if (readData1[idx] != readData2[idx]) { |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 783 | if (!failedPixelCount) { |
| 784 | firstWrongX = x; |
| 785 | firstWrongY = y; |
| 786 | } |
| 787 | ++failedPixelCount; |
| 788 | } |
| 789 | if (failedPixelCount > kMaxAcceptableFailedPixels) { |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 790 | passing = false; |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 791 | idx = firstWrongY * kRenderSize + firstWrongX; |
| 792 | ERRORF(reporter, |
| 793 | "Processor %s made clone produced different output at (%d, %d). " |
| 794 | "Input color: 0x%08x, Original Output Color: 0x%08x, " |
| 795 | "Clone Output Color: 0x%08x.", |
| 796 | name, firstWrongX, firstWrongY, input_texel_color(x, y, 0.0f), |
| 797 | readData1[idx], readData2[idx]); |
| 798 | if (!loggedFirstFailure) { |
| 799 | // Write the images out as data urls for inspection. |
| 800 | // We mark the data as unpremul to avoid conversion when encoding as |
| 801 | // PNG. Also, even though we made the data by rendering into |
| 802 | // a "unpremul" GrRenderTargetContext, our input texture is unpremul and |
| 803 | // outside of the random effect configuration, we didn't do anything to |
| 804 | // ensure the output is actually premul. |
| 805 | auto info = SkImageInfo::Make(kRenderSize, kRenderSize, |
| 806 | kRGBA_8888_SkColorType, |
| 807 | kUnpremul_SkAlphaType); |
| 808 | SkString input, orig, clone; |
| 809 | if (log_texture_proxy(context, inputTexture, &input) && |
| 810 | log_pixels(readData1.get(), kRenderSize, &orig) && |
| 811 | log_pixels(readData2.get(), kRenderSize, &clone)) { |
| 812 | ERRORF(reporter, |
| 813 | "\nInput image:\n%s\n\n" |
| 814 | "===========================================================" |
| 815 | "\n\n" |
| 816 | "Orig output image:\n%s\n" |
| 817 | "===========================================================" |
| 818 | "\n\n" |
| 819 | "Clone output image:\n%s\n", |
| 820 | input.c_str(), orig.c_str(), clone.c_str()); |
| 821 | loggedFirstFailure = true; |
| 822 | } |
| 823 | } |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 824 | } |
| 825 | } |
| 826 | } |
| 827 | } |
| 828 | } |
| 829 | } |
| 830 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 831 | #endif // GR_TEST_UTILS |