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 | |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 10 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/gpu/GrClip.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrDirectContextPriv.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 13 | #include "src/gpu/GrGpuResource.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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrResourceProvider.h" |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 18 | #include "src/gpu/GrSurfaceDrawContext.h" |
Brian Salomon | 27c4202 | 2021-04-28 12:39:21 -0400 | [diff] [blame] | 19 | #include "src/gpu/SkGr.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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 | eebe735 | 2020-12-09 16:37:04 -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 |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 33 | static GrOp::Owner Make(GrRecordingContext* rContext, |
| 34 | std::unique_ptr<GrFragmentProcessor> fp) { |
| 35 | return GrOp::Make<TestOp>(rContext, std::move(fp)); |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 36 | } |
| 37 | |
Robert Phillips | 5f567c7 | 2017-09-14 08:27:37 -0400 | [diff] [blame] | 38 | const char* name() const override { return "TestOp"; } |
| 39 | |
Chris Dalton | 1706cbf | 2019-05-21 19:35:29 -0600 | [diff] [blame] | 40 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | 5f567c7 | 2017-09-14 08:27:37 -0400 | [diff] [blame] | 41 | fProcessors.visitProxies(func); |
| 42 | } |
| 43 | |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 44 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
| 45 | |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 46 | GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip, |
| 47 | GrClampType clampType) override { |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 48 | static constexpr GrProcessorAnalysisColor kUnknownColor; |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 49 | SkPMColor4f overrideColor; |
Chris Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 50 | return fProcessors.finalize( |
| 51 | kUnknownColor, GrProcessorAnalysisCoverage::kNone, clip, |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 52 | &GrUserStencilSettings::kUnused, caps, clampType, &overrideColor); |
Brian Salomon | 649a341 | 2017-03-09 13:50:43 -0500 | [diff] [blame] | 53 | } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 54 | |
| 55 | private: |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 56 | friend class ::GrOp; // for ctor |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 57 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 58 | TestOp(std::unique_ptr<GrFragmentProcessor> fp) |
| 59 | : INHERITED(ClassID()), fProcessors(std::move(fp)) { |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 60 | this->setBounds(SkRect::MakeWH(100, 100), HasAABloat::kNo, IsHairline::kNo); |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 61 | } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 62 | |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 63 | GrProgramInfo* programInfo() override { return nullptr; } |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 64 | void onCreateProgramInfo(const GrCaps*, |
| 65 | SkArenaAlloc*, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 66 | const GrSurfaceProxyView& writeView, |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 67 | GrAppliedClip&&, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 68 | const GrXferProcessor::DstProxyView&, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 69 | GrXferBarrierFlags renderPassXferBarriers, |
| 70 | GrLoadOp colorLoadOp) override {} |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 71 | void onPrePrepareDraws(GrRecordingContext*, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 72 | const GrSurfaceProxyView& writeView, |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 73 | GrAppliedClip*, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 74 | const GrXferProcessor::DstProxyView&, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 75 | GrXferBarrierFlags renderPassXferBarriers, |
| 76 | GrLoadOp colorLoadOp) override {} |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 77 | void onPrepareDraws(Target* target) override { return; } |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 78 | void onExecute(GrOpFlushState*, const SkRect&) override { return; } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 79 | |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 80 | GrProcessorSet fProcessors; |
| 81 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 82 | using INHERITED = GrMeshDrawOp; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | /** |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 86 | * 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] | 87 | * of resources owned by child FPs. |
| 88 | */ |
| 89 | class TestFP : public GrFragmentProcessor { |
| 90 | public: |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 91 | static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> child) { |
| 92 | return std::unique_ptr<GrFragmentProcessor>(new TestFP(std::move(child))); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 93 | } |
Brian Salomon | 92b9ccf | 2020-06-17 14:19:19 -0400 | [diff] [blame] | 94 | static std::unique_ptr<GrFragmentProcessor> Make(const SkTArray<GrSurfaceProxyView>& views) { |
| 95 | return std::unique_ptr<GrFragmentProcessor>(new TestFP(views)); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | const char* name() const override { return "test"; } |
| 99 | |
| 100 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override { |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 101 | static std::atomic<int32_t> nextKey{0}; |
| 102 | b->add32(nextKey++); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 103 | } |
| 104 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 105 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 106 | return std::unique_ptr<GrFragmentProcessor>(new TestFP(*this)); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 107 | } |
| 108 | |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 109 | private: |
Brian Salomon | 92b9ccf | 2020-06-17 14:19:19 -0400 | [diff] [blame] | 110 | TestFP(const SkTArray<GrSurfaceProxyView>& views) |
| 111 | : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags) { |
John Stiles | fe7aed6 | 2020-07-20 13:48:02 -0400 | [diff] [blame] | 112 | for (const GrSurfaceProxyView& view : views) { |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 113 | this->registerChild(GrTextureEffect::Make(view, kUnknown_SkAlphaType)); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 114 | } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 115 | } |
| 116 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 117 | TestFP(std::unique_ptr<GrFragmentProcessor> child) |
Brian Salomon | 92b9ccf | 2020-06-17 14:19:19 -0400 | [diff] [blame] | 118 | : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags) { |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 119 | this->registerChild(std::move(child)); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 120 | } |
| 121 | |
Brian Salomon | 92b9ccf | 2020-06-17 14:19:19 -0400 | [diff] [blame] | 122 | explicit TestFP(const TestFP& that) : INHERITED(kTestFP_ClassID, that.optimizationFlags()) { |
| 123 | this->cloneAndRegisterAllChildProcessors(that); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 124 | } |
| 125 | |
Brian Salomon | 18ab203 | 2021-02-23 10:07:05 -0500 | [diff] [blame] | 126 | std::unique_ptr<GrGLSLFragmentProcessor> onMakeProgramImpl() const override { |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 127 | class TestGLSLFP : public GrGLSLFragmentProcessor { |
| 128 | public: |
| 129 | TestGLSLFP() {} |
| 130 | void emitCode(EmitArgs& args) override { |
John Stiles | d1eab8b | 2020-12-15 09:47:26 -0500 | [diff] [blame] | 131 | args.fFragBuilder->codeAppendf("return half4(1);"); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | private: |
| 135 | }; |
Brian Salomon | 18ab203 | 2021-02-23 10:07:05 -0500 | [diff] [blame] | 136 | return std::make_unique<TestGLSLFP>(); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | bool onIsEqual(const GrFragmentProcessor&) const override { return false; } |
| 140 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 141 | using INHERITED = GrFragmentProcessor; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 142 | }; |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 143 | } // namespace |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 144 | |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 145 | DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 146 | auto context = ctxInfo.directContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 147 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 148 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 149 | static constexpr SkISize kDims = {10, 10}; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 150 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 151 | const GrBackendFormat format = |
Robert Phillips | 0a15cc6 | 2019-07-30 12:49:10 -0400 | [diff] [blame] | 152 | context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, |
| 153 | GrRenderable::kNo); |
Greg Daniel | 47c20e8 | 2020-01-21 14:29:57 -0500 | [diff] [blame] | 154 | GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888); |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 155 | |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 156 | for (bool makeClone : {false, true}) { |
| 157 | for (int parentCnt = 0; parentCnt < 2; parentCnt++) { |
Brian Salomon | 1aa1f5f | 2020-12-11 17:25:17 -0500 | [diff] [blame] | 158 | auto surfaceDrawContext = GrSurfaceDrawContext::Make( |
Chris Dalton | f5b87f9 | 2021-04-19 17:27:09 -0600 | [diff] [blame] | 159 | context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {1, 1}, |
| 160 | SkSurfaceProps()); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 161 | { |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 162 | sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy( |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 163 | format, kDims, GrRenderable::kNo, 1, GrMipmapped::kNo, SkBackingFit::kExact, |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 164 | SkBudgeted::kYes, GrProtected::kNo); |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 165 | |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 166 | { |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 167 | SkTArray<GrSurfaceProxyView> views; |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 168 | views.push_back({proxy, kTopLeft_GrSurfaceOrigin, swizzle}); |
Brian Salomon | 92b9ccf | 2020-06-17 14:19:19 -0400 | [diff] [blame] | 169 | auto fp = TestFP::Make(std::move(views)); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 170 | for (int i = 0; i < parentCnt; ++i) { |
| 171 | fp = TestFP::Make(std::move(fp)); |
| 172 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 173 | std::unique_ptr<GrFragmentProcessor> clone; |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 174 | if (makeClone) { |
| 175 | clone = fp->clone(); |
| 176 | } |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 177 | GrOp::Owner op = TestOp::Make(context, std::move(fp)); |
Brian Salomon | 1aa1f5f | 2020-12-11 17:25:17 -0500 | [diff] [blame] | 178 | surfaceDrawContext->addDrawOp(std::move(op)); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 179 | if (clone) { |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 180 | op = TestOp::Make(context, std::move(clone)); |
Brian Salomon | 1aa1f5f | 2020-12-11 17:25:17 -0500 | [diff] [blame] | 181 | surfaceDrawContext->addDrawOp(std::move(op)); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 182 | } |
| 183 | } |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 184 | |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 185 | // If the fp is cloned the number of refs should increase by one (for the clone) |
| 186 | int expectedProxyRefs = makeClone ? 3 : 2; |
| 187 | |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 188 | CheckSingleThreadedProxyRefs(reporter, proxy.get(), expectedProxyRefs, -1); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 189 | |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 190 | context->flushAndSubmit(); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 191 | |
Brian Salomon | 557e812 | 2019-10-24 10:37:08 -0400 | [diff] [blame] | 192 | // just one from the 'proxy' sk_sp |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 193 | CheckSingleThreadedProxyRefs(reporter, proxy.get(), 1, 1); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | } |
| 197 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 198 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 199 | #include "tools/flags/CommandLineFlags.h" |
Mike Klein | 84836b7 | 2019-03-21 11:31:36 -0500 | [diff] [blame] | 200 | static DEFINE_bool(randomProcessorTest, false, |
| 201 | "Use non-deterministic seed for random processor tests?"); |
Mike Klein | 5b3f343 | 2019-03-21 11:42:21 -0500 | [diff] [blame] | 202 | static DEFINE_int(processorSeed, 0, |
| 203 | "Use specific seed for processor tests. Overridden by --randomProcessorTest."); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 204 | |
| 205 | #if GR_TEST_UTILS |
| 206 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 207 | static GrColor input_texel_color(int i, int j, SkScalar delta) { |
| 208 | // Delta must be less than 0.5 to prevent over/underflow issues with the input color |
| 209 | SkASSERT(delta <= 0.5); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 210 | |
Brian Osman | 50ea3c0 | 2019-02-04 10:01:53 -0500 | [diff] [blame] | 211 | SkColor color = SkColorSetARGB((uint8_t)(i & 0xFF), |
| 212 | (uint8_t)(j & 0xFF), |
| 213 | (uint8_t)((i + j) & 0xFF), |
| 214 | (uint8_t)((2 * j - i) & 0xFF)); |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 215 | SkColor4f color4f = SkColor4f::FromColor(color); |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 216 | // We only apply delta to the r,g, and b channels. This is because we're using this |
| 217 | // to test the canTweakAlphaForCoverage() optimization. A processor is allowed |
| 218 | // to use the input color's alpha in its calculation and report this optimization. |
| 219 | for (int i = 0; i < 3; i++) { |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 220 | if (color4f[i] > 0.5) { |
| 221 | color4f[i] -= delta; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 222 | } else { |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 223 | color4f[i] += delta; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 224 | } |
| 225 | } |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 226 | return color4f.premul().toBytes_RGBA(); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 227 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 228 | |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 229 | void test_draw_op(GrRecordingContext* rContext, |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 230 | GrSurfaceDrawContext* rtc, |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 231 | std::unique_ptr<GrFragmentProcessor> fp) { |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 232 | GrPaint paint; |
John Stiles | 5933d7d | 2020-07-21 12:28:35 -0400 | [diff] [blame] | 233 | paint.setColorFragmentProcessor(std::move(fp)); |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 234 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 235 | |
Robert Phillips | 58adb34 | 2020-07-23 09:41:57 -0400 | [diff] [blame] | 236 | auto op = GrFillRectOp::MakeNonAARect(rContext, std::move(paint), SkMatrix::I(), |
Michael Ludwig | aa1b6b3 | 2019-05-29 14:43:13 -0400 | [diff] [blame] | 237 | SkRect::MakeWH(rtc->width(), rtc->height())); |
Brian Salomon | 70fe17e | 2020-11-30 14:33:58 -0500 | [diff] [blame] | 238 | rtc->addDrawOp(std::move(op)); |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 239 | } |
| 240 | |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 241 | // The output buffer must be the same size as the render-target context. |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 242 | void render_fp(GrDirectContext* dContext, |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 243 | GrSurfaceDrawContext* rtc, |
Brian Salomon | 3f4de78 | 2020-06-18 14:16:00 -0400 | [diff] [blame] | 244 | std::unique_ptr<GrFragmentProcessor> fp, |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 245 | GrColor* outBuffer) { |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 246 | test_draw_op(dContext, rtc, std::move(fp)); |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 247 | std::fill_n(outBuffer, rtc->width() * rtc->height(), 0); |
Brian Salomon | dd4087d | 2020-12-23 20:36:44 -0500 | [diff] [blame] | 248 | auto ii = SkImageInfo::Make(rtc->dimensions(), kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 249 | GrPixmap resultPM(ii, outBuffer, rtc->width()*sizeof(uint32_t)); |
| 250 | rtc->readPixels(dContext, resultPM, {0, 0}); |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 251 | } |
| 252 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 253 | // This class is responsible for reproducibly generating a random fragment processor. |
| 254 | // An identical randomly-designed FP can be generated as many times as needed. |
| 255 | class TestFPGenerator { |
| 256 | public: |
| 257 | TestFPGenerator() = delete; |
| 258 | TestFPGenerator(GrDirectContext* context, GrResourceProvider* resourceProvider) |
| 259 | : fContext(context) |
| 260 | , fResourceProvider(resourceProvider) |
| 261 | , fInitialSeed(synthesizeInitialSeed()) |
| 262 | , fRandomSeed(fInitialSeed) {} |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 263 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 264 | uint32_t initialSeed() { return fInitialSeed; } |
| 265 | |
| 266 | bool init() { |
| 267 | // Initializes the two test texture proxies that are available to the FP test factories. |
| 268 | SkRandom random{fRandomSeed}; |
| 269 | static constexpr int kTestTextureSize = 256; |
| 270 | |
| 271 | { |
| 272 | // Put premul data into the RGBA texture that the test FPs can optionally use. |
| 273 | GrColor* rgbaData = new GrColor[kTestTextureSize * kTestTextureSize]; |
| 274 | for (int y = 0; y < kTestTextureSize; ++y) { |
| 275 | for (int x = 0; x < kTestTextureSize; ++x) { |
| 276 | rgbaData[kTestTextureSize * y + x] = input_texel_color( |
| 277 | random.nextULessThan(256), random.nextULessThan(256), 0.0f); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize, |
| 282 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 283 | SkBitmap bitmap; |
| 284 | bitmap.installPixels( |
| 285 | ii, rgbaData, ii.minRowBytes(), |
| 286 | [](void* addr, void* context) { delete[](GrColor*) addr; }, nullptr); |
| 287 | bitmap.setImmutable(); |
Brian Salomon | 27c4202 | 2021-04-28 12:39:21 -0400 | [diff] [blame] | 288 | auto view = std::get<0>(GrMakeUncachedBitmapProxyView(fContext, bitmap)); |
| 289 | if (!view || !view.proxy()->instantiate(fResourceProvider)) { |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 290 | SkDebugf("Unable to instantiate RGBA8888 test texture."); |
| 291 | return false; |
| 292 | } |
| 293 | fTestViews[0] = GrProcessorTestData::ViewInfo{view, GrColorType::kRGBA_8888, |
| 294 | kPremul_SkAlphaType}; |
| 295 | } |
| 296 | |
| 297 | { |
| 298 | // Put random values into the alpha texture that the test FPs can optionally use. |
| 299 | 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 | |
| 306 | SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize, |
| 307 | kAlpha_8_SkColorType, kPremul_SkAlphaType); |
| 308 | SkBitmap bitmap; |
| 309 | bitmap.installPixels( |
| 310 | ii, alphaData, ii.minRowBytes(), |
| 311 | [](void* addr, void* context) { delete[](uint8_t*) addr; }, nullptr); |
| 312 | bitmap.setImmutable(); |
Brian Salomon | 27c4202 | 2021-04-28 12:39:21 -0400 | [diff] [blame] | 313 | auto view = std::get<0>(GrMakeUncachedBitmapProxyView(fContext, bitmap)); |
| 314 | if (!view || !view.proxy()->instantiate(fResourceProvider)) { |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 315 | SkDebugf("Unable to instantiate A8 test texture."); |
| 316 | return false; |
| 317 | } |
| 318 | fTestViews[1] = GrProcessorTestData::ViewInfo{view, GrColorType::kAlpha_8, |
| 319 | kPremul_SkAlphaType}; |
| 320 | } |
| 321 | |
| 322 | return true; |
| 323 | } |
| 324 | |
| 325 | void reroll() { |
| 326 | // Feed our current random seed into SkRandom to generate a new seed. |
| 327 | SkRandom random{fRandomSeed}; |
| 328 | fRandomSeed = random.nextU(); |
| 329 | } |
| 330 | |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 331 | std::unique_ptr<GrFragmentProcessor> make(int type, int randomTreeDepth, |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 332 | std::unique_ptr<GrFragmentProcessor> inputFP) { |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 333 | // This will generate the exact same randomized FP (of each requested type) each time |
| 334 | // it's called. Call `reroll` to get a different FP. |
| 335 | SkRandom random{fRandomSeed}; |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 336 | GrProcessorTestData testData{&random, fContext, randomTreeDepth, |
| 337 | SK_ARRAY_COUNT(fTestViews), fTestViews, |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 338 | std::move(inputFP)}; |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 339 | return GrFragmentProcessorTestFactory::MakeIdx(type, &testData); |
| 340 | } |
| 341 | |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 342 | std::unique_ptr<GrFragmentProcessor> make(int type, int randomTreeDepth, |
| 343 | GrSurfaceProxyView view, |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 344 | SkAlphaType alpha = kPremul_SkAlphaType) { |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 345 | return make(type, randomTreeDepth, GrTextureEffect::Make(view, alpha)); |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 346 | } |
| 347 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 348 | private: |
| 349 | static uint32_t synthesizeInitialSeed() { |
| 350 | if (FLAGS_randomProcessorTest) { |
| 351 | std::random_device rd; |
| 352 | return rd(); |
| 353 | } else { |
| 354 | return FLAGS_processorSeed; |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 355 | } |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 356 | } |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 357 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 358 | GrDirectContext* fContext; // owned by caller |
| 359 | GrResourceProvider* fResourceProvider; // owned by caller |
| 360 | const uint32_t fInitialSeed; |
| 361 | uint32_t fRandomSeed; |
| 362 | GrProcessorTestData::ViewInfo fTestViews[2]; |
| 363 | }; |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 364 | |
John Stiles | 4ed6947 | 2020-08-11 16:04:31 -0400 | [diff] [blame] | 365 | // Creates an array of color values from input_texel_color(), to be used as an input texture. |
| 366 | std::vector<GrColor> make_input_pixels(int width, int height, SkScalar delta) { |
| 367 | std::vector<GrColor> pixel(width * height); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 368 | for (int y = 0; y < width; ++y) { |
| 369 | for (int x = 0; x < height; ++x) { |
John Stiles | 4ed6947 | 2020-08-11 16:04:31 -0400 | [diff] [blame] | 370 | pixel[width * y + x] = input_texel_color(x, y, delta); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 371 | } |
| 372 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 373 | |
John Stiles | 4ed6947 | 2020-08-11 16:04:31 -0400 | [diff] [blame] | 374 | return pixel; |
| 375 | } |
| 376 | |
| 377 | // Creates a texture of premul colors used as the output of the fragment processor that precedes |
| 378 | // the fragment processor under test. An array of W*H colors are passed in as the texture data. |
| 379 | GrSurfaceProxyView make_input_texture(GrRecordingContext* context, |
| 380 | int width, int height, GrColor* pixel) { |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 381 | SkImageInfo ii = SkImageInfo::Make(width, height, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 382 | SkBitmap bitmap; |
John Stiles | 4ed6947 | 2020-08-11 16:04:31 -0400 | [diff] [blame] | 383 | bitmap.installPixels(ii, pixel, ii.minRowBytes()); |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 384 | bitmap.setImmutable(); |
Brian Salomon | 27c4202 | 2021-04-28 12:39:21 -0400 | [diff] [blame] | 385 | return std::get<0>(GrMakeUncachedBitmapProxyView(context, bitmap)); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 386 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 387 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 388 | // We tag logged data as unpremul to avoid conversion when encoding as PNG. The input texture |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 389 | // actually contains unpremul data. Also, even though we made the result data by rendering into |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 390 | // a "unpremul" GrSurfaceDrawContext, our input texture is unpremul and outside of the random |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 391 | // effect configuration, we didn't do anything to ensure the output is actually premul. We just |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 392 | // don't currently allow kUnpremul GrSurfaceDrawContexts. |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 393 | static constexpr auto kLogAlphaType = kUnpremul_SkAlphaType; |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 394 | |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 395 | bool log_pixels(GrColor* pixels, int widthHeight, SkString* dst) { |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 396 | SkImageInfo info = |
| 397 | SkImageInfo::Make(widthHeight, widthHeight, kRGBA_8888_SkColorType, kLogAlphaType); |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 398 | SkBitmap bmp; |
| 399 | bmp.installPixels(info, pixels, widthHeight * sizeof(GrColor)); |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 400 | return BipmapToBase64DataURI(bmp, dst); |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 401 | } |
| 402 | |
Adlai Holler | c95b589 | 2020-08-11 12:02:22 -0400 | [diff] [blame] | 403 | bool log_texture_view(GrDirectContext* dContext, GrSurfaceProxyView src, SkString* dst) { |
Greg Daniel | 124486b | 2020-02-11 11:54:55 -0500 | [diff] [blame] | 404 | SkImageInfo ii = SkImageInfo::Make(src.proxy()->dimensions(), kRGBA_8888_SkColorType, |
| 405 | kLogAlphaType); |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 406 | |
Brian Salomon | 14f99fc | 2020-12-07 12:19:47 -0500 | [diff] [blame] | 407 | auto sContext = GrSurfaceContext::Make(dContext, std::move(src), ii.colorInfo()); |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 408 | SkBitmap bm; |
| 409 | SkAssertResult(bm.tryAllocPixels(ii)); |
Brian Salomon | dd4087d | 2020-12-23 20:36:44 -0500 | [diff] [blame] | 410 | SkAssertResult(sContext->readPixels(dContext, bm.pixmap(), {0, 0})); |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 411 | return BipmapToBase64DataURI(bm, dst); |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 412 | } |
| 413 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 414 | bool fuzzy_color_equals(const SkPMColor4f& c1, const SkPMColor4f& c2) { |
| 415 | // 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] | 416 | // 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] | 417 | // (really 1/128 ~ .0078, but frequently floating point issues make that tolerance a little |
| 418 | // too unforgiving). |
Brian Salomon | 682ba43 | 2019-12-17 20:20:23 +0000 | [diff] [blame] | 419 | static constexpr SkScalar kTolerance = 0.01f; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 420 | for (int i = 0; i < 4; i++) { |
| 421 | if (!SkScalarNearlyEqual(c1[i], c2[i], kTolerance)) { |
| 422 | return false; |
| 423 | } |
| 424 | } |
| 425 | return true; |
| 426 | } |
| 427 | |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 428 | // Given three input colors (color preceding the FP being tested) provided to the FP at the same |
| 429 | // local coord and the three corresponding FP outputs, this ensures that either: |
| 430 | // out[0] = fp * in[0].a, out[1] = fp * in[1].a, and out[2] = fp * in[2].a |
| 431 | // where fp is the pre-modulated color that should not be changing across frames (FP's state doesn't |
| 432 | // change), OR: |
| 433 | // out[0] = fp * in[0], out[1] = fp * in[1], and out[2] = fp * in[2] |
| 434 | // (per-channel modulation instead of modulation by just the alpha channel) |
| 435 | // It does this by estimating the pre-modulated fp color from one of the input/output pairs and |
| 436 | // confirms the conditions hold for the other two pairs. |
| 437 | // It is required that the three input colors have the same alpha as fp is allowed to be a function |
| 438 | // of the input alpha (but not r, g, or b). |
| 439 | bool legal_modulation(const GrColor in[3], const GrColor out[3]) { |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 440 | // 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] | 441 | SkPMColor4f inf[3], outf[3]; |
| 442 | for (int i = 0; i < 3; ++i) { |
| 443 | inf[i] = SkPMColor4f::FromBytes_RGBA(in[i]); |
| 444 | outf[i] = SkPMColor4f::FromBytes_RGBA(out[i]); |
| 445 | } |
| 446 | // This test is only valid if all the input alphas are the same. |
| 447 | 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] | 448 | |
| 449 | // Reconstruct the output of the FP before the shader modulated its color with the input value. |
| 450 | // When the original input is very small, it may cause the final output color to round |
| 451 | // to 0, in which case we estimate the pre-modulated color using one of the stepped frames that |
| 452 | // 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] | 453 | SkPMColor4f fpPreColorModulation = {0,0,0,0}; |
| 454 | SkPMColor4f fpPreAlphaModulation = {0,0,0,0}; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 455 | for (int i = 0; i < 4; i++) { |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 456 | // Use the most stepped up frame |
| 457 | int maxInIdx = inf[0][i] > inf[1][i] ? 0 : 1; |
| 458 | maxInIdx = inf[maxInIdx][i] > inf[2][i] ? maxInIdx : 2; |
John Stiles | fe7aed6 | 2020-07-20 13:48:02 -0400 | [diff] [blame] | 459 | const SkPMColor4f& in = inf[maxInIdx]; |
| 460 | const SkPMColor4f& out = outf[maxInIdx]; |
Brian Salomon | 0a7ca7a | 2019-12-27 12:18:19 -0500 | [diff] [blame] | 461 | if (in[i] > 0) { |
| 462 | fpPreColorModulation[i] = out[i] / in[i]; |
| 463 | } |
| 464 | if (in[3] > 0) { |
| 465 | fpPreAlphaModulation[i] = out[i] / in[3]; |
| 466 | } |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | // With reconstructed pre-modulated FP output, derive the expected value of fp * input for each |
| 470 | // of the transformed input colors. |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 471 | SkPMColor4f expectedForAlphaModulation[3]; |
| 472 | SkPMColor4f expectedForColorModulation[3]; |
| 473 | for (int i = 0; i < 3; ++i) { |
| 474 | expectedForAlphaModulation[i] = fpPreAlphaModulation * inf[i].fA; |
| 475 | expectedForColorModulation[i] = fpPreColorModulation * inf[i]; |
| 476 | // If the input alpha is 0 then the other channels should also be zero |
| 477 | // since the color is assumed to be premul. Modulating zeros by anything |
| 478 | // should produce zeros. |
| 479 | if (inf[i].fA == 0) { |
| 480 | SkASSERT(inf[i].fR == 0 && inf[i].fG == 0 && inf[i].fB == 0); |
| 481 | expectedForColorModulation[i] = expectedForAlphaModulation[i] = {0, 0, 0, 0}; |
| 482 | } |
| 483 | } |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 484 | |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 485 | bool isLegalColorModulation = fuzzy_color_equals(outf[0], expectedForColorModulation[0]) && |
| 486 | fuzzy_color_equals(outf[1], expectedForColorModulation[1]) && |
| 487 | fuzzy_color_equals(outf[2], expectedForColorModulation[2]); |
| 488 | |
| 489 | bool isLegalAlphaModulation = fuzzy_color_equals(outf[0], expectedForAlphaModulation[0]) && |
| 490 | fuzzy_color_equals(outf[1], expectedForAlphaModulation[1]) && |
| 491 | fuzzy_color_equals(outf[2], expectedForAlphaModulation[2]); |
| 492 | |
| 493 | // This can be enabled to print the values that caused this check to fail. |
| 494 | if (0 && !isLegalColorModulation && !isLegalAlphaModulation) { |
| 495 | SkDebugf("Color modulation test\n\timplied mod color: (%.03f, %.03f, %.03f, %.03f)\n", |
| 496 | fpPreColorModulation[0], |
| 497 | fpPreColorModulation[1], |
| 498 | fpPreColorModulation[2], |
| 499 | fpPreColorModulation[3]); |
| 500 | for (int i = 0; i < 3; ++i) { |
| 501 | SkDebugf("\t(%.03f, %.03f, %.03f, %.03f) -> " |
| 502 | "(%.03f, %.03f, %.03f, %.03f) | " |
| 503 | "(%.03f, %.03f, %.03f, %.03f), ok: %d\n", |
| 504 | inf[i].fR, inf[i].fG, inf[i].fB, inf[i].fA, |
| 505 | outf[i].fR, outf[i].fG, outf[i].fB, outf[i].fA, |
| 506 | expectedForColorModulation[i].fR, expectedForColorModulation[i].fG, |
| 507 | expectedForColorModulation[i].fB, expectedForColorModulation[i].fA, |
| 508 | fuzzy_color_equals(outf[i], expectedForColorModulation[i])); |
| 509 | } |
| 510 | SkDebugf("Alpha modulation test\n\timplied mod color: (%.03f, %.03f, %.03f, %.03f)\n", |
| 511 | fpPreAlphaModulation[0], |
| 512 | fpPreAlphaModulation[1], |
| 513 | fpPreAlphaModulation[2], |
| 514 | fpPreAlphaModulation[3]); |
| 515 | for (int i = 0; i < 3; ++i) { |
| 516 | SkDebugf("\t(%.03f, %.03f, %.03f, %.03f) -> " |
| 517 | "(%.03f, %.03f, %.03f, %.03f) | " |
| 518 | "(%.03f, %.03f, %.03f, %.03f), ok: %d\n", |
| 519 | inf[i].fR, inf[i].fG, inf[i].fB, inf[i].fA, |
| 520 | outf[i].fR, outf[i].fG, outf[i].fB, outf[i].fA, |
| 521 | expectedForAlphaModulation[i].fR, expectedForAlphaModulation[i].fG, |
| 522 | expectedForAlphaModulation[i].fB, expectedForAlphaModulation[i].fA, |
| 523 | fuzzy_color_equals(outf[i], expectedForAlphaModulation[i])); |
| 524 | } |
| 525 | } |
| 526 | return isLegalColorModulation || isLegalAlphaModulation; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 527 | } |
| 528 | |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 529 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) { |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 530 | GrDirectContext* context = ctxInfo.directContext(); |
| 531 | GrResourceProvider* resourceProvider = context->priv().resourceProvider(); |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 532 | using FPFactory = GrFragmentProcessorTestFactory; |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 533 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 534 | TestFPGenerator fpGenerator{context, resourceProvider}; |
| 535 | if (!fpGenerator.init()) { |
| 536 | ERRORF(reporter, "Could not initialize TestFPGenerator"); |
| 537 | return; |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 538 | } |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 539 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 540 | // Make the destination context for the test. |
| 541 | static constexpr int kRenderSize = 256; |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 542 | auto rtc = GrSurfaceDrawContext::Make( |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 543 | context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact, |
Chris Dalton | f5b87f9 | 2021-04-19 17:27:09 -0600 | [diff] [blame] | 544 | {kRenderSize, kRenderSize}, SkSurfaceProps()); |
Brian Salomon | d1a8bdf | 2017-02-10 12:39:26 -0500 | [diff] [blame] | 545 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 546 | // Coverage optimization uses three frames with a linearly transformed input texture. The first |
| 547 | // frame has no offset, second frames add .2 and .4, which should then be present as a fixed |
| 548 | // difference between the frame outputs if the FP is properly following the modulation |
| 549 | // requirements of the coverage optimization. |
| 550 | static constexpr SkScalar kInputDelta = 0.2f; |
John Stiles | 4ed6947 | 2020-08-11 16:04:31 -0400 | [diff] [blame] | 551 | std::vector<GrColor> inputPixels1 = make_input_pixels(kRenderSize, kRenderSize, 0.0f); |
| 552 | std::vector<GrColor> inputPixels2 = |
| 553 | make_input_pixels(kRenderSize, kRenderSize, 1 * kInputDelta); |
| 554 | std::vector<GrColor> inputPixels3 = |
| 555 | make_input_pixels(kRenderSize, kRenderSize, 2 * kInputDelta); |
| 556 | GrSurfaceProxyView inputTexture1 = |
| 557 | make_input_texture(context, kRenderSize, kRenderSize, inputPixels1.data()); |
| 558 | GrSurfaceProxyView inputTexture2 = |
| 559 | make_input_texture(context, kRenderSize, kRenderSize, inputPixels2.data()); |
| 560 | GrSurfaceProxyView inputTexture3 = |
| 561 | make_input_texture(context, kRenderSize, kRenderSize, inputPixels3.data()); |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 562 | |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 563 | // Encoded images are very verbose and this tests many potential images, so only export the |
| 564 | // first failure (subsequent failures have a reasonable chance of being related). |
| 565 | bool loggedFirstFailure = false; |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 566 | bool loggedFirstWarning = false; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 567 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 568 | // Storage for the three frames required for coverage compatibility optimization testing. |
| 569 | // Each frame uses the correspondingly numbered inputTextureX. |
| 570 | std::vector<GrColor> readData1(kRenderSize * kRenderSize); |
| 571 | std::vector<GrColor> readData2(kRenderSize * kRenderSize); |
| 572 | std::vector<GrColor> readData3(kRenderSize * kRenderSize); |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 573 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 574 | // Because processor factories configure themselves in random ways, this is not exhaustive. |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 575 | for (int i = 0; i < FPFactory::Count(); ++i) { |
John Stiles | fe7aed6 | 2020-07-20 13:48:02 -0400 | [diff] [blame] | 576 | int optimizedForOpaqueInput = 0; |
| 577 | int optimizedForCoverageAsAlpha = 0; |
| 578 | int optimizedForConstantOutputForInput = 0; |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 579 | |
John Stiles | fe7aed6 | 2020-07-20 13:48:02 -0400 | [diff] [blame] | 580 | #ifdef __MSVC_RUNTIME_CHECKS |
Brian Osman | 50ea3c0 | 2019-02-04 10:01:53 -0500 | [diff] [blame] | 581 | // This test is infuriatingly slow with MSVC runtime checks enabled |
John Stiles | fe7aed6 | 2020-07-20 13:48:02 -0400 | [diff] [blame] | 582 | static constexpr int kMinimumTrials = 1; |
| 583 | static constexpr int kMaximumTrials = 1; |
| 584 | static constexpr int kExpectedSuccesses = 1; |
| 585 | #else |
| 586 | // We start by testing each fragment-processor 100 times, watching the optimization bits |
| 587 | // that appear. If we see an optimization bit appear in those first 100 trials, we keep |
| 588 | // running tests until we see at least five successful trials that have this optimization |
| 589 | // bit enabled. If we never see a particular optimization bit after 100 trials, we assume |
| 590 | // that this FP doesn't support that optimization at all. |
| 591 | static constexpr int kMinimumTrials = 100; |
| 592 | static constexpr int kMaximumTrials = 2000; |
| 593 | static constexpr int kExpectedSuccesses = 5; |
Brian Osman | 50ea3c0 | 2019-02-04 10:01:53 -0500 | [diff] [blame] | 594 | #endif |
Robert Phillips | a91e0b7 | 2017-05-01 13:12:20 -0400 | [diff] [blame] | 595 | |
John Stiles | fe7aed6 | 2020-07-20 13:48:02 -0400 | [diff] [blame] | 596 | for (int trial = 0;; ++trial) { |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 597 | // Create a randomly-configured FP. |
| 598 | fpGenerator.reroll(); |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 599 | std::unique_ptr<GrFragmentProcessor> fp = |
John Stiles | 9fbe9e9 | 2020-08-10 16:35:01 -0400 | [diff] [blame] | 600 | fpGenerator.make(i, /*randomTreeDepth=*/1, inputTexture1); |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 601 | |
John Stiles | fe7aed6 | 2020-07-20 13:48:02 -0400 | [diff] [blame] | 602 | // If we have iterated enough times and seen a sufficient number of successes on each |
| 603 | // optimization bit that can be returned, stop running trials. |
| 604 | if (trial >= kMinimumTrials) { |
| 605 | bool moreTrialsNeeded = (optimizedForOpaqueInput > 0 && |
| 606 | optimizedForOpaqueInput < kExpectedSuccesses) || |
| 607 | (optimizedForCoverageAsAlpha > 0 && |
| 608 | optimizedForCoverageAsAlpha < kExpectedSuccesses) || |
| 609 | (optimizedForConstantOutputForInput > 0 && |
| 610 | optimizedForConstantOutputForInput < kExpectedSuccesses); |
| 611 | if (!moreTrialsNeeded) break; |
| 612 | |
| 613 | if (trial >= kMaximumTrials) { |
| 614 | SkDebugf("Abandoning ProcessorOptimizationValidationTest after %d trials. " |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 615 | "Seed: 0x%08x, processor:\n%s", |
| 616 | kMaximumTrials, fpGenerator.initialSeed(), fp->dumpTreeInfo().c_str()); |
John Stiles | fe7aed6 | 2020-07-20 13:48:02 -0400 | [diff] [blame] | 617 | break; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | // Skip further testing if this trial has no optimization bits enabled. |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 622 | if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() && |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 623 | !fp->compatibleWithCoverageAsAlpha()) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 624 | continue; |
| 625 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 626 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 627 | // We can make identical copies of the test FP in order to test coverage-as-alpha. |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 628 | if (fp->compatibleWithCoverageAsAlpha()) { |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 629 | // Create and render two identical versions of this FP, but using different input |
| 630 | // textures, to check coverage optimization. We don't need to do this step for |
| 631 | // constant-output or preserving-opacity tests. |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 632 | render_fp(context, rtc.get(), |
John Stiles | 9fbe9e9 | 2020-08-10 16:35:01 -0400 | [diff] [blame] | 633 | fpGenerator.make(i, /*randomTreeDepth=*/1, inputTexture2), |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 634 | readData2.data()); |
| 635 | render_fp(context, rtc.get(), |
John Stiles | 9fbe9e9 | 2020-08-10 16:35:01 -0400 | [diff] [blame] | 636 | fpGenerator.make(i, /*randomTreeDepth=*/1, inputTexture3), |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 637 | readData3.data()); |
John Stiles | fe7aed6 | 2020-07-20 13:48:02 -0400 | [diff] [blame] | 638 | ++optimizedForCoverageAsAlpha; |
| 639 | } |
| 640 | |
| 641 | if (fp->hasConstantOutputForConstantInput()) { |
| 642 | ++optimizedForConstantOutputForInput; |
| 643 | } |
| 644 | |
| 645 | if (fp->preservesOpaqueInput()) { |
| 646 | ++optimizedForOpaqueInput; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 647 | } |
Brian Salomon | 3f4de78 | 2020-06-18 14:16:00 -0400 | [diff] [blame] | 648 | |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 649 | // Draw base frame last so that rtc holds the original FP behavior if we need to dump |
| 650 | // the image to the log. |
John Stiles | 9fbe9e9 | 2020-08-10 16:35:01 -0400 | [diff] [blame] | 651 | render_fp(context, rtc.get(), fpGenerator.make(i, /*randomTreeDepth=*/1, inputTexture1), |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 652 | readData1.data()); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 653 | |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 654 | // This test has a history of being flaky on a number of devices. If an FP is logically |
| 655 | // violating the optimizations, it's reasonable to expect it to violate requirements on |
| 656 | // a large number of pixels in the image. Sporadic pixel violations are more indicative |
| 657 | // of device errors and represents a separate problem. |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 658 | #if defined(SK_BUILD_FOR_SKQP) |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 659 | static constexpr int kMaxAcceptableFailedPixels = 0; // Strict when running as SKQP |
| 660 | #else |
| 661 | static constexpr int kMaxAcceptableFailedPixels = 2 * kRenderSize; // ~0.7% of the image |
| 662 | #endif |
| 663 | |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 664 | // Collect first optimization failure message, to be output later as a warning or an |
| 665 | // error depending on whether the rendering "passed" or failed. |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 666 | int failedPixelCount = 0; |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 667 | SkString coverageMessage; |
| 668 | SkString opaqueMessage; |
| 669 | SkString constMessage; |
| 670 | for (int y = 0; y < kRenderSize; ++y) { |
| 671 | for (int x = 0; x < kRenderSize; ++x) { |
| 672 | bool passing = true; |
John Stiles | 4ed6947 | 2020-08-11 16:04:31 -0400 | [diff] [blame] | 673 | GrColor input = inputPixels1[y * kRenderSize + x]; |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 674 | GrColor output = readData1[y * kRenderSize + x]; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 675 | |
| 676 | if (fp->compatibleWithCoverageAsAlpha()) { |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 677 | GrColor ins[3]; |
| 678 | ins[0] = input; |
John Stiles | 4ed6947 | 2020-08-11 16:04:31 -0400 | [diff] [blame] | 679 | ins[1] = inputPixels2[y * kRenderSize + x]; |
| 680 | ins[2] = inputPixels3[y * kRenderSize + x]; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 681 | |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 682 | GrColor outs[3]; |
| 683 | outs[0] = output; |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 684 | outs[1] = readData2[y * kRenderSize + x]; |
| 685 | outs[2] = readData3[y * kRenderSize + x]; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 686 | |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 687 | if (!legal_modulation(ins, outs)) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 688 | passing = false; |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 689 | if (coverageMessage.isEmpty()) { |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 690 | coverageMessage.printf( |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 691 | "\"Modulating\" processor did not match alpha-modulation " |
| 692 | "nor color-modulation rules.\n" |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 693 | "Input: 0x%08x, Output: 0x%08x, pixel (%d, %d).", |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 694 | input, output, x, y); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 695 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 696 | } |
| 697 | } |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 698 | |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 699 | SkPMColor4f input4f = SkPMColor4f::FromBytes_RGBA(input); |
| 700 | SkPMColor4f output4f = SkPMColor4f::FromBytes_RGBA(output); |
Brian Osman | 1d5b598 | 2018-10-01 13:41:39 -0400 | [diff] [blame] | 701 | SkPMColor4f expected4f; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 702 | if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) { |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 703 | float rDiff = fabsf(output4f.fR - expected4f.fR); |
| 704 | float gDiff = fabsf(output4f.fG - expected4f.fG); |
| 705 | float bDiff = fabsf(output4f.fB - expected4f.fB); |
| 706 | float aDiff = fabsf(output4f.fA - expected4f.fA); |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 707 | static constexpr float kTol = 4 / 255.f; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 708 | if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) { |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 709 | if (constMessage.isEmpty()) { |
| 710 | passing = false; |
| 711 | |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 712 | constMessage.printf( |
| 713 | "Processor claimed output for const input doesn't match " |
| 714 | "actual output.\n" |
| 715 | "Error: %f, Tolerance: %f, input: (%f, %f, %f, %f), " |
| 716 | "actual: (%f, %f, %f, %f), expected(%f, %f, %f, %f).", |
| 717 | std::max(rDiff, std::max(gDiff, std::max(bDiff, aDiff))), |
| 718 | kTol, input4f.fR, input4f.fG, input4f.fB, input4f.fA, |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 719 | output4f.fR, output4f.fG, output4f.fB, output4f.fA, |
| 720 | expected4f.fR, expected4f.fG, expected4f.fB, expected4f.fA); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 721 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 722 | } |
| 723 | } |
Brian Osman | 00b2939 | 2018-11-05 15:42:43 -0500 | [diff] [blame] | 724 | if (input4f.isOpaque() && fp->preservesOpaqueInput() && !output4f.isOpaque()) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 725 | passing = false; |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 726 | |
| 727 | if (opaqueMessage.isEmpty()) { |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 728 | opaqueMessage.printf( |
| 729 | "Processor claimed opaqueness is preserved but " |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 730 | "it is not. Input: 0x%08x, Output: 0x%08x.", |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 731 | input, output); |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 732 | } |
Brian Osman | bd1f76f | 2017-03-15 11:33:12 -0400 | [diff] [blame] | 733 | } |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 734 | |
| 735 | if (!passing) { |
| 736 | // Regardless of how many optimizations the pixel violates, count it as a |
| 737 | // single bad pixel. |
| 738 | failedPixelCount++; |
| 739 | } |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | // Finished analyzing the entire image, see if the number of pixel failures meets the |
| 744 | // threshold for an FP violating the optimization requirements. |
| 745 | if (failedPixelCount > kMaxAcceptableFailedPixels) { |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 746 | ERRORF(reporter, |
| 747 | "Processor violated %d of %d pixels, seed: 0x%08x.\n" |
| 748 | "Processor:\n%s\nFirst failing pixel details are below:", |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 749 | failedPixelCount, kRenderSize * kRenderSize, fpGenerator.initialSeed(), |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 750 | fp->dumpTreeInfo().c_str()); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 751 | |
| 752 | // Print first failing pixel's details. |
| 753 | if (!coverageMessage.isEmpty()) { |
| 754 | ERRORF(reporter, coverageMessage.c_str()); |
| 755 | } |
| 756 | if (!constMessage.isEmpty()) { |
| 757 | ERRORF(reporter, constMessage.c_str()); |
| 758 | } |
| 759 | if (!opaqueMessage.isEmpty()) { |
| 760 | ERRORF(reporter, opaqueMessage.c_str()); |
| 761 | } |
| 762 | |
| 763 | if (!loggedFirstFailure) { |
| 764 | // Print with ERRORF to make sure the encoded image is output |
| 765 | SkString input; |
Greg Daniel | 124486b | 2020-02-11 11:54:55 -0500 | [diff] [blame] | 766 | log_texture_view(context, inputTexture1, &input); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 767 | SkString output; |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 768 | log_pixels(readData1.data(), kRenderSize, &output); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 769 | ERRORF(reporter, "Input image: %s\n\n" |
| 770 | "===========================================================\n\n" |
| 771 | "Output image: %s\n", input.c_str(), output.c_str()); |
| 772 | loggedFirstFailure = true; |
| 773 | } |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 774 | } else if (failedPixelCount > 0) { |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 775 | // Don't trigger an error, but don't just hide the failures either. |
| 776 | INFOF(reporter, "Processor violated %d of %d pixels (below error threshold), seed: " |
| 777 | "0x%08x, processor: %s", failedPixelCount, kRenderSize * kRenderSize, |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 778 | fpGenerator.initialSeed(), fp->dumpInfo().c_str()); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 779 | if (!coverageMessage.isEmpty()) { |
| 780 | INFOF(reporter, coverageMessage.c_str()); |
| 781 | } |
| 782 | if (!constMessage.isEmpty()) { |
| 783 | INFOF(reporter, constMessage.c_str()); |
| 784 | } |
| 785 | if (!opaqueMessage.isEmpty()) { |
| 786 | INFOF(reporter, opaqueMessage.c_str()); |
| 787 | } |
| 788 | if (!loggedFirstWarning) { |
| 789 | SkString input; |
Greg Daniel | 124486b | 2020-02-11 11:54:55 -0500 | [diff] [blame] | 790 | log_texture_view(context, inputTexture1, &input); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 791 | SkString output; |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 792 | log_pixels(readData1.data(), kRenderSize, &output); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 793 | INFOF(reporter, "Input image: %s\n\n" |
| 794 | "===========================================================\n\n" |
| 795 | "Output image: %s\n", input.c_str(), output.c_str()); |
| 796 | loggedFirstWarning = true; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | } |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 802 | |
John Stiles | ea8be21 | 2020-08-10 11:38:41 -0400 | [diff] [blame] | 803 | static void assert_processor_equality(skiatest::Reporter* reporter, |
| 804 | const GrFragmentProcessor& fp, |
| 805 | const GrFragmentProcessor& clone) { |
| 806 | REPORTER_ASSERT(reporter, !strcmp(fp.name(), clone.name()), |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 807 | "\n%s", fp.dumpTreeInfo().c_str()); |
John Stiles | ea8be21 | 2020-08-10 11:38:41 -0400 | [diff] [blame] | 808 | REPORTER_ASSERT(reporter, fp.compatibleWithCoverageAsAlpha() == |
| 809 | clone.compatibleWithCoverageAsAlpha(), |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 810 | "\n%s", fp.dumpTreeInfo().c_str()); |
John Stiles | ea8be21 | 2020-08-10 11:38:41 -0400 | [diff] [blame] | 811 | REPORTER_ASSERT(reporter, fp.isEqual(clone), |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 812 | "\n%s", fp.dumpTreeInfo().c_str()); |
John Stiles | ea8be21 | 2020-08-10 11:38:41 -0400 | [diff] [blame] | 813 | REPORTER_ASSERT(reporter, fp.preservesOpaqueInput() == clone.preservesOpaqueInput(), |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 814 | "\n%s", fp.dumpTreeInfo().c_str()); |
John Stiles | ea8be21 | 2020-08-10 11:38:41 -0400 | [diff] [blame] | 815 | REPORTER_ASSERT(reporter, fp.hasConstantOutputForConstantInput() == |
| 816 | clone.hasConstantOutputForConstantInput(), |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 817 | "\n%s", fp.dumpTreeInfo().c_str()); |
John Stiles | ea8be21 | 2020-08-10 11:38:41 -0400 | [diff] [blame] | 818 | REPORTER_ASSERT(reporter, fp.numChildProcessors() == clone.numChildProcessors(), |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 819 | "\n%s", fp.dumpTreeInfo().c_str()); |
John Stiles | ea8be21 | 2020-08-10 11:38:41 -0400 | [diff] [blame] | 820 | REPORTER_ASSERT(reporter, fp.usesVaryingCoords() == clone.usesVaryingCoords(), |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 821 | "\n%s", fp.dumpTreeInfo().c_str()); |
John Stiles | ea8be21 | 2020-08-10 11:38:41 -0400 | [diff] [blame] | 822 | REPORTER_ASSERT(reporter, fp.referencesSampleCoords() == clone.referencesSampleCoords(), |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 823 | "\n%s", fp.dumpTreeInfo().c_str()); |
John Stiles | ea8be21 | 2020-08-10 11:38:41 -0400 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | static bool verify_identical_render(skiatest::Reporter* reporter, int renderSize, |
| 827 | const char* processorType, |
| 828 | const GrColor readData1[], const GrColor readData2[]) { |
| 829 | // The ProcessorClone test has a history of being flaky on a number of devices. If an FP clone |
| 830 | // is logically wrong, it's reasonable to expect it produce a large number of pixel differences |
| 831 | // in the image. Sporadic pixel violations are more indicative device errors and represents a |
| 832 | // separate problem. |
| 833 | #if defined(SK_BUILD_FOR_SKQP) |
| 834 | const int maxAcceptableFailedPixels = 0; // Strict when running as SKQP |
| 835 | #else |
| 836 | const int maxAcceptableFailedPixels = 2 * renderSize; // ~0.002% of the pixels (size 1024*1024) |
| 837 | #endif |
| 838 | |
| 839 | int failedPixelCount = 0; |
| 840 | int firstWrongX = 0; |
| 841 | int firstWrongY = 0; |
| 842 | int idx = 0; |
| 843 | for (int y = 0; y < renderSize; ++y) { |
| 844 | for (int x = 0; x < renderSize; ++x, ++idx) { |
| 845 | if (readData1[idx] != readData2[idx]) { |
| 846 | if (!failedPixelCount) { |
| 847 | firstWrongX = x; |
| 848 | firstWrongY = y; |
| 849 | } |
| 850 | ++failedPixelCount; |
| 851 | } |
| 852 | if (failedPixelCount > maxAcceptableFailedPixels) { |
| 853 | idx = firstWrongY * renderSize + firstWrongX; |
| 854 | ERRORF(reporter, |
| 855 | "%s produced different output at (%d, %d). " |
| 856 | "Input color: 0x%08x, Original Output Color: 0x%08x, " |
| 857 | "Clone Output Color: 0x%08x.", |
| 858 | processorType, firstWrongX, firstWrongY, input_texel_color(x, y, 0.0f), |
| 859 | readData1[idx], readData2[idx]); |
| 860 | |
| 861 | return false; |
| 862 | } |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | return true; |
| 867 | } |
| 868 | |
| 869 | static void log_clone_failure(skiatest::Reporter* reporter, int renderSize, |
| 870 | GrDirectContext* context, const GrSurfaceProxyView& inputTexture, |
| 871 | GrColor pixelsFP[], GrColor pixelsClone[], GrColor pixelsRegen[]) { |
| 872 | // Write the images out as data URLs for inspection. |
| 873 | SkString inputURL, origURL, cloneURL, regenURL; |
| 874 | if (log_texture_view(context, inputTexture, &inputURL) && |
| 875 | log_pixels(pixelsFP, renderSize, &origURL) && |
| 876 | log_pixels(pixelsClone, renderSize, &cloneURL) && |
| 877 | log_pixels(pixelsRegen, renderSize, ®enURL)) { |
| 878 | ERRORF(reporter, |
| 879 | "\nInput image:\n%s\n\n" |
| 880 | "===========================================================" |
| 881 | "\n\n" |
| 882 | "Orig output image:\n%s\n" |
| 883 | "===========================================================" |
| 884 | "\n\n" |
| 885 | "Clone output image:\n%s\n" |
| 886 | "===========================================================" |
| 887 | "\n\n" |
| 888 | "Regen output image:\n%s\n", |
| 889 | inputURL.c_str(), origURL.c_str(), cloneURL.c_str(), regenURL.c_str()); |
| 890 | } |
| 891 | } |
| 892 | |
Brian Salomon | 3f4de78 | 2020-06-18 14:16:00 -0400 | [diff] [blame] | 893 | // Tests that a fragment processor returned by GrFragmentProcessor::clone() is equivalent to its |
| 894 | // progenitor. |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 895 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) { |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 896 | GrDirectContext* context = ctxInfo.directContext(); |
| 897 | GrResourceProvider* resourceProvider = context->priv().resourceProvider(); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 898 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 899 | TestFPGenerator fpGenerator{context, resourceProvider}; |
| 900 | if (!fpGenerator.init()) { |
| 901 | ERRORF(reporter, "Could not initialize TestFPGenerator"); |
| 902 | return; |
| 903 | } |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 904 | |
| 905 | // Make the destination context for the test. |
| 906 | static constexpr int kRenderSize = 1024; |
Brian Salomon | eebe735 | 2020-12-09 16:37:04 -0500 | [diff] [blame] | 907 | auto rtc = GrSurfaceDrawContext::Make( |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 908 | context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact, |
Chris Dalton | f5b87f9 | 2021-04-19 17:27:09 -0600 | [diff] [blame] | 909 | {kRenderSize, kRenderSize}, SkSurfaceProps()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 910 | |
John Stiles | 4ed6947 | 2020-08-11 16:04:31 -0400 | [diff] [blame] | 911 | std::vector<GrColor> inputPixels = make_input_pixels(kRenderSize, kRenderSize, 0.0f); |
| 912 | GrSurfaceProxyView inputTexture = |
| 913 | make_input_texture(context, kRenderSize, kRenderSize, inputPixels.data()); |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 914 | |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 915 | // On failure we write out images, but just write the first failing set as the print is very |
| 916 | // large. |
| 917 | bool loggedFirstFailure = false; |
| 918 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 919 | // Storage for the original frame's readback and the readback of its clone. |
John Stiles | ea8be21 | 2020-08-10 11:38:41 -0400 | [diff] [blame] | 920 | std::vector<GrColor> readDataFP(kRenderSize * kRenderSize); |
| 921 | std::vector<GrColor> readDataClone(kRenderSize * kRenderSize); |
| 922 | std::vector<GrColor> readDataRegen(kRenderSize * kRenderSize); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 923 | |
| 924 | // Because processor factories configure themselves in random ways, this is not exhaustive. |
| 925 | for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) { |
| 926 | static constexpr int kTimesToInvokeFactory = 10; |
| 927 | for (int j = 0; j < kTimesToInvokeFactory; ++j) { |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 928 | fpGenerator.reroll(); |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 929 | std::unique_ptr<GrFragmentProcessor> fp = |
John Stiles | 9fbe9e9 | 2020-08-10 16:35:01 -0400 | [diff] [blame] | 930 | fpGenerator.make(i, /*randomTreeDepth=*/1, /*inputFP=*/nullptr); |
John Stiles | 87d0a2f | 2020-08-10 13:12:41 -0400 | [diff] [blame] | 931 | std::unique_ptr<GrFragmentProcessor> regen = |
John Stiles | 9fbe9e9 | 2020-08-10 16:35:01 -0400 | [diff] [blame] | 932 | fpGenerator.make(i, /*randomTreeDepth=*/1, /*inputFP=*/nullptr); |
John Stiles | 8c71f3e | 2020-06-15 15:07:19 -0400 | [diff] [blame] | 933 | std::unique_ptr<GrFragmentProcessor> clone = fp->clone(); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 934 | if (!clone) { |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 935 | ERRORF(reporter, "Clone of processor %s failed.", fp->dumpTreeInfo().c_str()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 936 | continue; |
| 937 | } |
John Stiles | ea8be21 | 2020-08-10 11:38:41 -0400 | [diff] [blame] | 938 | assert_processor_equality(reporter, *fp, *clone); |
| 939 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 940 | // Draw with original and read back the results. |
John Stiles | ea8be21 | 2020-08-10 11:38:41 -0400 | [diff] [blame] | 941 | render_fp(context, rtc.get(), std::move(fp), readDataFP.data()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 942 | |
| 943 | // Draw with clone and read back the results. |
John Stiles | ea8be21 | 2020-08-10 11:38:41 -0400 | [diff] [blame] | 944 | render_fp(context, rtc.get(), std::move(clone), readDataClone.data()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 945 | |
| 946 | // Check that the results are the same. |
John Stiles | ea8be21 | 2020-08-10 11:38:41 -0400 | [diff] [blame] | 947 | if (!verify_identical_render(reporter, kRenderSize, "Processor clone", |
| 948 | readDataFP.data(), readDataClone.data())) { |
| 949 | // Dump a description from the regenerated processor (since the original FP has |
| 950 | // already been consumed). |
John Stiles | ba1879d | 2020-08-11 13:58:32 -0400 | [diff] [blame] | 951 | ERRORF(reporter, "FP hierarchy:\n%s", regen->dumpTreeInfo().c_str()); |
John Stiles | ea8be21 | 2020-08-10 11:38:41 -0400 | [diff] [blame] | 952 | |
| 953 | // Render and readback output from the regenerated FP. If this also mismatches, the |
| 954 | // FP itself doesn't generate consistent output. This could happen if: |
| 955 | // - the FP's TestCreate() does not always generate the same FP from a given seed |
| 956 | // - the FP's Make() does not always generate the same FP when given the same inputs |
| 957 | // - the FP itself generates inconsistent pixels (shader UB?) |
| 958 | // - the driver has a bug |
| 959 | render_fp(context, rtc.get(), std::move(regen), readDataRegen.data()); |
| 960 | |
| 961 | if (!verify_identical_render(reporter, kRenderSize, "Regenerated processor", |
| 962 | readDataFP.data(), readDataRegen.data())) { |
| 963 | ERRORF(reporter, "Output from regen did not match original!\n"); |
| 964 | } else { |
| 965 | ERRORF(reporter, "Regenerated processor output matches original results.\n"); |
| 966 | } |
| 967 | |
| 968 | // If this is the first time we've encountered a cloning failure, log the generated |
| 969 | // images to the reporter as data URLs. |
| 970 | if (!loggedFirstFailure) { |
| 971 | log_clone_failure(reporter, kRenderSize, context, inputTexture, |
| 972 | readDataFP.data(), readDataClone.data(), |
| 973 | readDataRegen.data()); |
| 974 | loggedFirstFailure = true; |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 975 | } |
| 976 | } |
| 977 | } |
| 978 | } |
| 979 | } |
| 980 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 981 | #endif // GR_TEST_UTILS |