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" |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 11 | #include "src/gpu/GrBitmapTextureMaker.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrClip.h" |
| 13 | #include "src/gpu/GrContextPriv.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 14 | #include "src/gpu/GrGpuResource.h" |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrImageInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/gpu/GrMemoryPool.h" |
| 17 | #include "src/gpu/GrProxyProvider.h" |
| 18 | #include "src/gpu/GrRenderTargetContext.h" |
| 19 | #include "src/gpu/GrRenderTargetContextPriv.h" |
| 20 | #include "src/gpu/GrResourceProvider.h" |
| 21 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 22 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 23 | #include "src/gpu/ops/GrFillRectOp.h" |
| 24 | #include "src/gpu/ops/GrMeshDrawOp.h" |
| 25 | #include "tests/TestUtils.h" |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 26 | #include <atomic> |
Hal Canary | 8a00144 | 2018-09-19 11:31:27 -0400 | [diff] [blame] | 27 | #include <random> |
| 28 | |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 29 | namespace { |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 30 | class TestOp : public GrMeshDrawOp { |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 31 | public: |
| 32 | DEFINE_OP_CLASS_ID |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 33 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, |
| 34 | std::unique_ptr<GrFragmentProcessor> fp) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 35 | GrOpMemoryPool* pool = context->priv().opMemoryPool(); |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 36 | |
| 37 | return pool->allocate<TestOp>(std::move(fp)); |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 38 | } |
| 39 | |
Robert Phillips | 5f567c7 | 2017-09-14 08:27:37 -0400 | [diff] [blame] | 40 | const char* name() const override { return "TestOp"; } |
| 41 | |
Chris Dalton | 1706cbf | 2019-05-21 19:35:29 -0600 | [diff] [blame] | 42 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | 5f567c7 | 2017-09-14 08:27:37 -0400 | [diff] [blame] | 43 | fProcessors.visitProxies(func); |
| 44 | } |
| 45 | |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 46 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
| 47 | |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 48 | GrProcessorSet::Analysis finalize( |
| 49 | const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage, |
| 50 | GrClampType clampType) override { |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 51 | static constexpr GrProcessorAnalysisColor kUnknownColor; |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 52 | SkPMColor4f overrideColor; |
Chris Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 53 | return fProcessors.finalize( |
| 54 | kUnknownColor, GrProcessorAnalysisCoverage::kNone, clip, |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 55 | &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps, clampType, |
| 56 | &overrideColor); |
Brian Salomon | 649a341 | 2017-03-09 13:50:43 -0500 | [diff] [blame] | 57 | } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 58 | |
| 59 | private: |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 60 | friend class ::GrOpMemoryPool; // for ctor |
| 61 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 62 | TestOp(std::unique_ptr<GrFragmentProcessor> fp) |
| 63 | : INHERITED(ClassID()), fProcessors(std::move(fp)) { |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 64 | this->setBounds(SkRect::MakeWH(100, 100), HasAABloat::kNo, IsHairline::kNo); |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 65 | } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 66 | |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 67 | GrProgramInfo* programInfo() override { return nullptr; } |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 68 | void onCreateProgramInfo(const GrCaps*, |
| 69 | SkArenaAlloc*, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 70 | const GrSurfaceProxyView* writeView, |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 71 | GrAppliedClip&&, |
| 72 | const GrXferProcessor::DstProxyView&) override { return; } |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 73 | void onPrePrepareDraws(GrRecordingContext*, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 74 | const GrSurfaceProxyView* writeView, |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 75 | GrAppliedClip*, |
| 76 | const GrXferProcessor::DstProxyView&) override { return; } |
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 | |
| 82 | typedef GrMeshDrawOp INHERITED; |
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) { |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 112 | for (const auto& 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 | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 126 | virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
| 127 | class TestGLSLFP : public GrGLSLFragmentProcessor { |
| 128 | public: |
| 129 | TestGLSLFP() {} |
| 130 | void emitCode(EmitArgs& args) override { |
| 131 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 132 | fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor); |
| 133 | } |
| 134 | |
| 135 | private: |
| 136 | }; |
| 137 | return new TestGLSLFP(); |
| 138 | } |
| 139 | |
| 140 | bool onIsEqual(const GrFragmentProcessor&) const override { return false; } |
| 141 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 142 | typedef GrFragmentProcessor INHERITED; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 143 | }; |
| 144 | } |
| 145 | |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 146 | DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 147 | auto context = ctxInfo.directContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 148 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 149 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 150 | static constexpr SkISize kDims = {10, 10}; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 151 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 152 | const GrBackendFormat format = |
Robert Phillips | 0a15cc6 | 2019-07-30 12:49:10 -0400 | [diff] [blame] | 153 | context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, |
| 154 | GrRenderable::kNo); |
Greg Daniel | 47c20e8 | 2020-01-21 14:29:57 -0500 | [diff] [blame] | 155 | GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888); |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 156 | |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 157 | for (bool makeClone : {false, true}) { |
| 158 | for (int parentCnt = 0; parentCnt < 2; parentCnt++) { |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 159 | auto renderTargetContext = GrRenderTargetContext::Make( |
| 160 | context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {1, 1}); |
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 | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 163 | format, kDims, GrRenderable::kNo, 1, GrMipMapped::kNo, SkBackingFit::kExact, |
| 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 | } |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 177 | std::unique_ptr<GrDrawOp> op(TestOp::Make(context, std::move(fp))); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 178 | renderTargetContext->priv().testingOnly_addDrawOp(std::move(op)); |
| 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 | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 181 | renderTargetContext->priv().testingOnly_addDrawOp(std::move(op)); |
| 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 | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 229 | void test_draw_op(GrContext* context, |
| 230 | GrRenderTargetContext* rtc, |
| 231 | std::unique_ptr<GrFragmentProcessor> fp, |
Greg Daniel | 124486b | 2020-02-11 11:54:55 -0500 | [diff] [blame] | 232 | GrSurfaceProxyView inputDataView, |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 233 | SkAlphaType inputAlphaType) { |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 234 | GrPaint paint; |
Greg Daniel | 124486b | 2020-02-11 11:54:55 -0500 | [diff] [blame] | 235 | paint.addColorFragmentProcessor(GrTextureEffect::Make(std::move(inputDataView), |
| 236 | inputAlphaType)); |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 237 | paint.addColorFragmentProcessor(std::move(fp)); |
| 238 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 239 | |
Michael Ludwig | aa1b6b3 | 2019-05-29 14:43:13 -0400 | [diff] [blame] | 240 | auto op = GrFillRectOp::MakeNonAARect(context, std::move(paint), SkMatrix::I(), |
| 241 | SkRect::MakeWH(rtc->width(), rtc->height())); |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 242 | rtc->priv().testingOnly_addDrawOp(std::move(op)); |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 243 | } |
| 244 | |
Greg Daniel | 124486b | 2020-02-11 11:54:55 -0500 | [diff] [blame] | 245 | // This assumes that the output buffer will be the same size as inputDataView |
Brian Salomon | 3f4de78 | 2020-06-18 14:16:00 -0400 | [diff] [blame] | 246 | void render_fp(GrContext* context, |
| 247 | GrRenderTargetContext* rtc, |
| 248 | std::unique_ptr<GrFragmentProcessor> fp, |
| 249 | GrSurfaceProxyView inputDataView, |
| 250 | SkAlphaType inputAlphaType, |
| 251 | GrColor* buffer) { |
| 252 | test_draw_op(context, rtc, std::move(fp), inputDataView, inputAlphaType); |
Greg Daniel | 124486b | 2020-02-11 11:54:55 -0500 | [diff] [blame] | 253 | memset(buffer, 0x0, |
| 254 | sizeof(GrColor) * inputDataView.proxy()->width() * inputDataView.proxy()->height()); |
| 255 | rtc->readPixels(SkImageInfo::Make(inputDataView.proxy()->dimensions(), kRGBA_8888_SkColorType, |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 256 | kPremul_SkAlphaType), |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 257 | buffer, 0, {0, 0}); |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 258 | } |
| 259 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 260 | /** 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] | 261 | bool init_test_textures(GrResourceProvider* resourceProvider, |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 262 | GrRecordingContext* context, |
Robert Phillips | 7eeb74f | 2019-03-29 07:26:46 -0400 | [diff] [blame] | 263 | SkRandom* random, |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 264 | GrProcessorTestData::ViewInfo views[2]) { |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 265 | static const int kTestTextureSize = 256; |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 266 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 267 | { |
| 268 | // Put premul data into the RGBA texture that the test FPs can optionally use. |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 269 | GrColor* rgbaData = new GrColor[kTestTextureSize * kTestTextureSize]; |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 270 | for (int y = 0; y < kTestTextureSize; ++y) { |
| 271 | for (int x = 0; x < kTestTextureSize; ++x) { |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 272 | rgbaData[kTestTextureSize * y + x] = input_texel_color( |
| 273 | random->nextULessThan(256), random->nextULessThan(256), 0.0f); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 274 | } |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 275 | } |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 276 | |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 277 | SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize, |
| 278 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 279 | SkBitmap bitmap; |
| 280 | bitmap.installPixels(ii, rgbaData, ii.minRowBytes(), |
| 281 | [](void* addr, void* context) { delete[] (GrColor*)addr; }, nullptr); |
| 282 | bitmap.setImmutable(); |
Brian Salomon | bc074a6 | 2020-03-18 10:06:13 -0400 | [diff] [blame] | 283 | GrBitmapTextureMaker maker(context, bitmap, GrImageTexGenPolicy::kNew_Uncached_Budgeted); |
Brian Salomon | ecbb0fb | 2020-02-28 18:07:32 -0500 | [diff] [blame] | 284 | auto view = maker.view(GrMipMapped::kNo); |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 285 | if (!view.proxy() || !view.proxy()->instantiate(resourceProvider)) { |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 286 | return false; |
| 287 | } |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 288 | views[0] = {view, GrColorType::kRGBA_8888, kPremul_SkAlphaType}; |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 289 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 290 | |
| 291 | { |
| 292 | // Put random values into the alpha texture that the test FPs can optionally use. |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 293 | uint8_t* alphaData = new uint8_t[kTestTextureSize * kTestTextureSize]; |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 294 | for (int y = 0; y < kTestTextureSize; ++y) { |
| 295 | for (int x = 0; x < kTestTextureSize; ++x) { |
| 296 | alphaData[kTestTextureSize * y + x] = random->nextULessThan(256); |
| 297 | } |
| 298 | } |
| 299 | |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 300 | SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize, |
| 301 | kAlpha_8_SkColorType, kPremul_SkAlphaType); |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 302 | SkBitmap bitmap; |
| 303 | bitmap.installPixels(ii, alphaData, ii.minRowBytes(), |
| 304 | [](void* addr, void* context) { delete[] (uint8_t*)addr; }, nullptr); |
| 305 | bitmap.setImmutable(); |
Brian Salomon | bc074a6 | 2020-03-18 10:06:13 -0400 | [diff] [blame] | 306 | GrBitmapTextureMaker maker(context, bitmap, GrImageTexGenPolicy::kNew_Uncached_Budgeted); |
Brian Salomon | ecbb0fb | 2020-02-28 18:07:32 -0500 | [diff] [blame] | 307 | auto view = maker.view(GrMipMapped::kNo); |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 308 | if (!view.proxy() || !view.proxy()->instantiate(resourceProvider)) { |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 309 | return false; |
| 310 | } |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 311 | views[1] = {view, GrColorType::kAlpha_8, kPremul_SkAlphaType}; |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 312 | } |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 313 | |
Brian Salomon | 766098d | 2019-12-18 10:41:58 -0500 | [diff] [blame] | 314 | return true; |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | // Creates a texture of premul colors used as the output of the fragment processor that precedes |
| 318 | // the fragment processor under test. Color values are those provided by input_texel_color(). |
Greg Daniel | 124486b | 2020-02-11 11:54:55 -0500 | [diff] [blame] | 319 | GrSurfaceProxyView make_input_texture(GrRecordingContext* context, int width, int height, |
| 320 | SkScalar delta) { |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 321 | GrColor* data = new GrColor[width * height]; |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 322 | for (int y = 0; y < width; ++y) { |
| 323 | for (int x = 0; x < height; ++x) { |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 324 | data[width * y + x] = input_texel_color(x, y, delta); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 325 | } |
| 326 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 327 | |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 328 | SkImageInfo ii = SkImageInfo::Make(width, height, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 329 | SkBitmap bitmap; |
| 330 | bitmap.installPixels(ii, data, ii.minRowBytes(), |
| 331 | [](void* addr, void* context) { delete[] (GrColor*)addr; }, nullptr); |
| 332 | bitmap.setImmutable(); |
Brian Salomon | bc074a6 | 2020-03-18 10:06:13 -0400 | [diff] [blame] | 333 | GrBitmapTextureMaker maker(context, bitmap, GrImageTexGenPolicy::kNew_Uncached_Budgeted); |
Brian Salomon | ecbb0fb | 2020-02-28 18:07:32 -0500 | [diff] [blame] | 334 | return maker.view(GrMipMapped::kNo); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 335 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 336 | |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 337 | // We tag logged data as unpremul to avoid conversion when encoding as PNG. The input texture |
| 338 | // actually contains unpremul data. Also, even though we made the result data by rendering into |
| 339 | // a "unpremul" GrRenderTargetContext, our input texture is unpremul and outside of the random |
| 340 | // effect configuration, we didn't do anything to ensure the output is actually premul. We just |
| 341 | // don't currently allow kUnpremul GrRenderTargetContexts. |
| 342 | static constexpr auto kLogAlphaType = kUnpremul_SkAlphaType; |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 343 | |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 344 | bool log_pixels(GrColor* pixels, int widthHeight, SkString* dst) { |
| 345 | auto info = SkImageInfo::Make(widthHeight, widthHeight, kRGBA_8888_SkColorType, kLogAlphaType); |
| 346 | SkBitmap bmp; |
| 347 | bmp.installPixels(info, pixels, widthHeight * sizeof(GrColor)); |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 348 | return BipmapToBase64DataURI(bmp, dst); |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 349 | } |
| 350 | |
Greg Daniel | 124486b | 2020-02-11 11:54:55 -0500 | [diff] [blame] | 351 | bool log_texture_view(GrContext* context, GrSurfaceProxyView src, SkString* dst) { |
| 352 | SkImageInfo ii = SkImageInfo::Make(src.proxy()->dimensions(), kRGBA_8888_SkColorType, |
| 353 | kLogAlphaType); |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 354 | |
Greg Daniel | 124486b | 2020-02-11 11:54:55 -0500 | [diff] [blame] | 355 | auto sContext = GrSurfaceContext::Make(context, std::move(src), GrColorType::kRGBA_8888, |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 356 | kLogAlphaType, nullptr); |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 357 | SkBitmap bm; |
| 358 | SkAssertResult(bm.tryAllocPixels(ii)); |
| 359 | SkAssertResult(sContext->readPixels(ii, bm.getPixels(), bm.rowBytes(), {0, 0})); |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 360 | return BipmapToBase64DataURI(bm, dst); |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 361 | } |
| 362 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 363 | bool fuzzy_color_equals(const SkPMColor4f& c1, const SkPMColor4f& c2) { |
| 364 | // 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] | 365 | // 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] | 366 | // (really 1/128 ~ .0078, but frequently floating point issues make that tolerance a little |
| 367 | // too unforgiving). |
Brian Salomon | 682ba43 | 2019-12-17 20:20:23 +0000 | [diff] [blame] | 368 | static constexpr SkScalar kTolerance = 0.01f; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 369 | for (int i = 0; i < 4; i++) { |
| 370 | if (!SkScalarNearlyEqual(c1[i], c2[i], kTolerance)) { |
| 371 | return false; |
| 372 | } |
| 373 | } |
| 374 | return true; |
| 375 | } |
| 376 | |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 377 | // Given three input colors (color preceding the FP being tested) provided to the FP at the same |
| 378 | // local coord and the three corresponding FP outputs, this ensures that either: |
| 379 | // out[0] = fp * in[0].a, out[1] = fp * in[1].a, and out[2] = fp * in[2].a |
| 380 | // where fp is the pre-modulated color that should not be changing across frames (FP's state doesn't |
| 381 | // change), OR: |
| 382 | // out[0] = fp * in[0], out[1] = fp * in[1], and out[2] = fp * in[2] |
| 383 | // (per-channel modulation instead of modulation by just the alpha channel) |
| 384 | // It does this by estimating the pre-modulated fp color from one of the input/output pairs and |
| 385 | // confirms the conditions hold for the other two pairs. |
| 386 | // It is required that the three input colors have the same alpha as fp is allowed to be a function |
| 387 | // of the input alpha (but not r, g, or b). |
| 388 | bool legal_modulation(const GrColor in[3], const GrColor out[3]) { |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 389 | // 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] | 390 | SkPMColor4f inf[3], outf[3]; |
| 391 | for (int i = 0; i < 3; ++i) { |
| 392 | inf[i] = SkPMColor4f::FromBytes_RGBA(in[i]); |
| 393 | outf[i] = SkPMColor4f::FromBytes_RGBA(out[i]); |
| 394 | } |
| 395 | // This test is only valid if all the input alphas are the same. |
| 396 | 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] | 397 | |
| 398 | // Reconstruct the output of the FP before the shader modulated its color with the input value. |
| 399 | // When the original input is very small, it may cause the final output color to round |
| 400 | // to 0, in which case we estimate the pre-modulated color using one of the stepped frames that |
| 401 | // 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] | 402 | SkPMColor4f fpPreColorModulation = {0,0,0,0}; |
| 403 | SkPMColor4f fpPreAlphaModulation = {0,0,0,0}; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 404 | for (int i = 0; i < 4; i++) { |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 405 | // Use the most stepped up frame |
| 406 | int maxInIdx = inf[0][i] > inf[1][i] ? 0 : 1; |
| 407 | maxInIdx = inf[maxInIdx][i] > inf[2][i] ? maxInIdx : 2; |
| 408 | const auto& in = inf[maxInIdx]; |
| 409 | const auto& out = outf[maxInIdx]; |
Brian Salomon | 0a7ca7a | 2019-12-27 12:18:19 -0500 | [diff] [blame] | 410 | if (in[i] > 0) { |
| 411 | fpPreColorModulation[i] = out[i] / in[i]; |
| 412 | } |
| 413 | if (in[3] > 0) { |
| 414 | fpPreAlphaModulation[i] = out[i] / in[3]; |
| 415 | } |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | // With reconstructed pre-modulated FP output, derive the expected value of fp * input for each |
| 419 | // of the transformed input colors. |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 420 | SkPMColor4f expectedForAlphaModulation[3]; |
| 421 | SkPMColor4f expectedForColorModulation[3]; |
| 422 | for (int i = 0; i < 3; ++i) { |
| 423 | expectedForAlphaModulation[i] = fpPreAlphaModulation * inf[i].fA; |
| 424 | expectedForColorModulation[i] = fpPreColorModulation * inf[i]; |
| 425 | // If the input alpha is 0 then the other channels should also be zero |
| 426 | // since the color is assumed to be premul. Modulating zeros by anything |
| 427 | // should produce zeros. |
| 428 | if (inf[i].fA == 0) { |
| 429 | SkASSERT(inf[i].fR == 0 && inf[i].fG == 0 && inf[i].fB == 0); |
| 430 | expectedForColorModulation[i] = expectedForAlphaModulation[i] = {0, 0, 0, 0}; |
| 431 | } |
| 432 | } |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 433 | |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 434 | bool isLegalColorModulation = fuzzy_color_equals(outf[0], expectedForColorModulation[0]) && |
| 435 | fuzzy_color_equals(outf[1], expectedForColorModulation[1]) && |
| 436 | fuzzy_color_equals(outf[2], expectedForColorModulation[2]); |
| 437 | |
| 438 | bool isLegalAlphaModulation = fuzzy_color_equals(outf[0], expectedForAlphaModulation[0]) && |
| 439 | fuzzy_color_equals(outf[1], expectedForAlphaModulation[1]) && |
| 440 | fuzzy_color_equals(outf[2], expectedForAlphaModulation[2]); |
| 441 | |
| 442 | // This can be enabled to print the values that caused this check to fail. |
| 443 | if (0 && !isLegalColorModulation && !isLegalAlphaModulation) { |
| 444 | SkDebugf("Color modulation test\n\timplied mod color: (%.03f, %.03f, %.03f, %.03f)\n", |
| 445 | fpPreColorModulation[0], |
| 446 | fpPreColorModulation[1], |
| 447 | fpPreColorModulation[2], |
| 448 | fpPreColorModulation[3]); |
| 449 | for (int i = 0; i < 3; ++i) { |
| 450 | SkDebugf("\t(%.03f, %.03f, %.03f, %.03f) -> " |
| 451 | "(%.03f, %.03f, %.03f, %.03f) | " |
| 452 | "(%.03f, %.03f, %.03f, %.03f), ok: %d\n", |
| 453 | inf[i].fR, inf[i].fG, inf[i].fB, inf[i].fA, |
| 454 | outf[i].fR, outf[i].fG, outf[i].fB, outf[i].fA, |
| 455 | expectedForColorModulation[i].fR, expectedForColorModulation[i].fG, |
| 456 | expectedForColorModulation[i].fB, expectedForColorModulation[i].fA, |
| 457 | fuzzy_color_equals(outf[i], expectedForColorModulation[i])); |
| 458 | } |
| 459 | SkDebugf("Alpha modulation test\n\timplied mod color: (%.03f, %.03f, %.03f, %.03f)\n", |
| 460 | fpPreAlphaModulation[0], |
| 461 | fpPreAlphaModulation[1], |
| 462 | fpPreAlphaModulation[2], |
| 463 | fpPreAlphaModulation[3]); |
| 464 | for (int i = 0; i < 3; ++i) { |
| 465 | SkDebugf("\t(%.03f, %.03f, %.03f, %.03f) -> " |
| 466 | "(%.03f, %.03f, %.03f, %.03f) | " |
| 467 | "(%.03f, %.03f, %.03f, %.03f), ok: %d\n", |
| 468 | inf[i].fR, inf[i].fG, inf[i].fB, inf[i].fA, |
| 469 | outf[i].fR, outf[i].fG, outf[i].fB, outf[i].fA, |
| 470 | expectedForAlphaModulation[i].fR, expectedForAlphaModulation[i].fG, |
| 471 | expectedForAlphaModulation[i].fB, expectedForAlphaModulation[i].fA, |
| 472 | fuzzy_color_equals(outf[i], expectedForAlphaModulation[i])); |
| 473 | } |
| 474 | } |
| 475 | return isLegalColorModulation || isLegalAlphaModulation; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 476 | } |
| 477 | |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 478 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 479 | auto context = ctxInfo.directContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 480 | auto resourceProvider = context->priv().resourceProvider(); |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 481 | using FPFactory = GrFragmentProcessorTestFactory; |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 482 | |
Michael Ludwig | 3ad86d0 | 2018-09-28 16:00:38 -0400 | [diff] [blame] | 483 | uint32_t seed = FLAGS_processorSeed; |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 484 | if (FLAGS_randomProcessorTest) { |
| 485 | std::random_device rd; |
| 486 | seed = rd(); |
| 487 | } |
| 488 | // 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] | 489 | // use --processorSeed <seed> (without --randomProcessorTest) to reproduce. |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 490 | SkRandom random(seed); |
| 491 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 492 | // Make the destination context for the test. |
| 493 | static constexpr int kRenderSize = 256; |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 494 | auto rtc = GrRenderTargetContext::Make( |
| 495 | context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact, |
| 496 | {kRenderSize, kRenderSize}); |
Brian Salomon | d1a8bdf | 2017-02-10 12:39:26 -0500 | [diff] [blame] | 497 | |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 498 | GrProcessorTestData::ViewInfo views[2]; |
| 499 | if (!init_test_textures(resourceProvider, context, &random, views)) { |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 500 | ERRORF(reporter, "Could not create test textures"); |
Robert Phillips | bb581ce | 2017-05-29 15:05:15 -0400 | [diff] [blame] | 501 | return; |
| 502 | } |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 503 | GrProcessorTestData testData(&random, context, 2, views); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 504 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 505 | // Coverage optimization uses three frames with a linearly transformed input texture. The first |
| 506 | // frame has no offset, second frames add .2 and .4, which should then be present as a fixed |
| 507 | // difference between the frame outputs if the FP is properly following the modulation |
| 508 | // requirements of the coverage optimization. |
| 509 | static constexpr SkScalar kInputDelta = 0.2f; |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 510 | auto inputTexture1 = make_input_texture(context, kRenderSize, kRenderSize, 0.0f); |
| 511 | auto inputTexture2 = make_input_texture(context, kRenderSize, kRenderSize, kInputDelta); |
| 512 | auto inputTexture3 = make_input_texture(context, kRenderSize, kRenderSize, 2*kInputDelta); |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 513 | |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 514 | // Encoded images are very verbose and this tests many potential images, so only export the |
| 515 | // first failure (subsequent failures have a reasonable chance of being related). |
| 516 | bool loggedFirstFailure = false; |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 517 | bool loggedFirstWarning = false; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 518 | |
| 519 | // Storage for the three frames required for coverage compatibility optimization. Each frame |
| 520 | // uses the correspondingly numbered inputTextureX. |
| 521 | std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]); |
| 522 | std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]); |
| 523 | std::unique_ptr<GrColor[]> readData3(new GrColor[kRenderSize * kRenderSize]); |
| 524 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 525 | // Because processor factories configure themselves in random ways, this is not exhaustive. |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 526 | for (int i = 0; i < FPFactory::Count(); ++i) { |
| 527 | int timesToInvokeFactory = 5; |
| 528 | // Increase the number of attempts if the FP has child FPs since optimizations likely depend |
| 529 | // on child optimizations being present. |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 530 | std::unique_ptr<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 531 | for (int j = 0; j < fp->numChildProcessors(); ++j) { |
| 532 | // This value made a reasonable trade off between time and coverage when this test was |
| 533 | // written. |
| 534 | timesToInvokeFactory *= FPFactory::Count() / 2; |
| 535 | } |
Brian Osman | 50ea3c0 | 2019-02-04 10:01:53 -0500 | [diff] [blame] | 536 | #if defined(__MSVC_RUNTIME_CHECKS) |
| 537 | // This test is infuriatingly slow with MSVC runtime checks enabled |
| 538 | timesToInvokeFactory = 1; |
| 539 | #endif |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 540 | for (int j = 0; j < timesToInvokeFactory; ++j) { |
| 541 | fp = FPFactory::MakeIdx(i, &testData); |
Robert Phillips | a91e0b7 | 2017-05-01 13:12:20 -0400 | [diff] [blame] | 542 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 543 | if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() && |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 544 | !fp->compatibleWithCoverageAsAlpha()) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 545 | continue; |
| 546 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 547 | |
Brian Salomon | 3f4de78 | 2020-06-18 14:16:00 -0400 | [diff] [blame] | 548 | // All draws use a clone so that we can continue to query fp. ProcessorCloneTest should |
| 549 | // validate that clones are equivalent to the original. |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 550 | if (fp->compatibleWithCoverageAsAlpha()) { |
| 551 | // 2nd and 3rd frames are only used when checking coverage optimization |
Brian Salomon | 3f4de78 | 2020-06-18 14:16:00 -0400 | [diff] [blame] | 552 | render_fp(context, rtc.get(), fp->clone(), inputTexture2, kPremul_SkAlphaType, |
Greg Daniel | c594e62 | 2019-10-15 14:01:49 -0400 | [diff] [blame] | 553 | readData2.get()); |
Brian Salomon | 3f4de78 | 2020-06-18 14:16:00 -0400 | [diff] [blame] | 554 | render_fp(context, rtc.get(), fp->clone(), inputTexture3, kPremul_SkAlphaType, |
Greg Daniel | c594e62 | 2019-10-15 14:01:49 -0400 | [diff] [blame] | 555 | readData3.get()); |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 556 | } |
Brian Salomon | 3f4de78 | 2020-06-18 14:16:00 -0400 | [diff] [blame] | 557 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 558 | // Draw base frame last so that rtc holds the original FP behavior if we need to |
| 559 | // dump the image to the log. |
Brian Salomon | 3f4de78 | 2020-06-18 14:16:00 -0400 | [diff] [blame] | 560 | render_fp(context, rtc.get(), fp->clone(), inputTexture1, kPremul_SkAlphaType, |
Greg Daniel | c594e62 | 2019-10-15 14:01:49 -0400 | [diff] [blame] | 561 | readData1.get()); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 562 | |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 563 | // This test has a history of being flaky on a number of devices. If an FP is logically |
| 564 | // violating the optimizations, it's reasonable to expect it to violate requirements on |
| 565 | // a large number of pixels in the image. Sporadic pixel violations are more indicative |
| 566 | // of device errors and represents a separate problem. |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 567 | #if defined(SK_BUILD_FOR_SKQP) |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 568 | static constexpr int kMaxAcceptableFailedPixels = 0; // Strict when running as SKQP |
| 569 | #else |
| 570 | static constexpr int kMaxAcceptableFailedPixels = 2 * kRenderSize; // ~0.7% of the image |
| 571 | #endif |
| 572 | |
| 573 | int failedPixelCount = 0; |
| 574 | // Collect first optimization failure message, to be output later as a warning or an |
| 575 | // error depending on whether the rendering "passed" or failed. |
| 576 | SkString coverageMessage; |
| 577 | SkString opaqueMessage; |
| 578 | SkString constMessage; |
| 579 | for (int y = 0; y < kRenderSize; ++y) { |
| 580 | for (int x = 0; x < kRenderSize; ++x) { |
| 581 | bool passing = true; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 582 | GrColor input = input_texel_color(x, y, 0.0f); |
| 583 | GrColor output = readData1.get()[y * kRenderSize + x]; |
| 584 | |
| 585 | if (fp->compatibleWithCoverageAsAlpha()) { |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 586 | GrColor ins[3]; |
| 587 | ins[0] = input; |
| 588 | ins[1] = input_texel_color(x, y, kInputDelta); |
| 589 | ins[2] = input_texel_color(x, y, 2 * kInputDelta); |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 590 | |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 591 | GrColor outs[3]; |
| 592 | outs[0] = output; |
| 593 | outs[1] = readData2.get()[y * kRenderSize + x]; |
| 594 | outs[2] = readData3.get()[y * kRenderSize + x]; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 595 | |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 596 | if (!legal_modulation(ins, outs)) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 597 | passing = false; |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 598 | if (coverageMessage.isEmpty()) { |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 599 | coverageMessage.printf( |
| 600 | "\"Modulating\" processor %s did not match " |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 601 | "alpha-modulation nor color-modulation rules. " |
| 602 | "Input: 0x%08x, Output: 0x%08x, pixel (%d, %d).", |
| 603 | fp->name(), input, output, x, y); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 604 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 605 | } |
| 606 | } |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 607 | |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 608 | SkPMColor4f input4f = SkPMColor4f::FromBytes_RGBA(input); |
| 609 | SkPMColor4f output4f = SkPMColor4f::FromBytes_RGBA(output); |
Brian Osman | 1d5b598 | 2018-10-01 13:41:39 -0400 | [diff] [blame] | 610 | SkPMColor4f expected4f; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 611 | if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) { |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 612 | float rDiff = fabsf(output4f.fR - expected4f.fR); |
| 613 | float gDiff = fabsf(output4f.fG - expected4f.fG); |
| 614 | float bDiff = fabsf(output4f.fB - expected4f.fB); |
| 615 | float aDiff = fabsf(output4f.fA - expected4f.fA); |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 616 | static constexpr float kTol = 4 / 255.f; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 617 | if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) { |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 618 | if (constMessage.isEmpty()) { |
| 619 | passing = false; |
| 620 | |
| 621 | constMessage.printf("Processor %s claimed output for const input " |
| 622 | "doesn't match actual output. Error: %f, Tolerance: %f, " |
| 623 | "input: (%f, %f, %f, %f), actual: (%f, %f, %f, %f), " |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 624 | "expected(%f, %f, %f, %f)", fp->name(), |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 625 | std::max(rDiff, std::max(gDiff, std::max(bDiff, aDiff))), kTol, |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 626 | input4f.fR, input4f.fG, input4f.fB, input4f.fA, |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 627 | output4f.fR, output4f.fG, output4f.fB, output4f.fA, |
| 628 | expected4f.fR, expected4f.fG, expected4f.fB, expected4f.fA); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 629 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 630 | } |
| 631 | } |
Brian Osman | 00b2939 | 2018-11-05 15:42:43 -0500 | [diff] [blame] | 632 | if (input4f.isOpaque() && fp->preservesOpaqueInput() && !output4f.isOpaque()) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 633 | passing = false; |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 634 | |
| 635 | if (opaqueMessage.isEmpty()) { |
| 636 | opaqueMessage.printf("Processor %s claimed opaqueness is preserved but " |
| 637 | "it is not. Input: 0x%08x, Output: 0x%08x.", |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 638 | fp->name(), input, output); |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 639 | } |
Brian Osman | bd1f76f | 2017-03-15 11:33:12 -0400 | [diff] [blame] | 640 | } |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 641 | |
| 642 | if (!passing) { |
| 643 | // Regardless of how many optimizations the pixel violates, count it as a |
| 644 | // single bad pixel. |
| 645 | failedPixelCount++; |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | // Finished analyzing the entire image, see if the number of pixel failures meets the |
| 651 | // threshold for an FP violating the optimization requirements. |
| 652 | if (failedPixelCount > kMaxAcceptableFailedPixels) { |
Michael Ludwig | 4504a652 | 2018-10-03 16:47:55 -0400 | [diff] [blame] | 653 | 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] | 654 | ", first failing pixel details are below:", |
| 655 | failedPixelCount, kRenderSize * kRenderSize, seed, |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 656 | fp->dumpInfo().c_str()); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 657 | |
| 658 | // Print first failing pixel's details. |
| 659 | if (!coverageMessage.isEmpty()) { |
| 660 | ERRORF(reporter, coverageMessage.c_str()); |
| 661 | } |
| 662 | if (!constMessage.isEmpty()) { |
| 663 | ERRORF(reporter, constMessage.c_str()); |
| 664 | } |
| 665 | if (!opaqueMessage.isEmpty()) { |
| 666 | ERRORF(reporter, opaqueMessage.c_str()); |
| 667 | } |
| 668 | |
| 669 | if (!loggedFirstFailure) { |
| 670 | // Print with ERRORF to make sure the encoded image is output |
| 671 | SkString input; |
Greg Daniel | 124486b | 2020-02-11 11:54:55 -0500 | [diff] [blame] | 672 | log_texture_view(context, inputTexture1, &input); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 673 | SkString output; |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 674 | log_pixels(readData1.get(), kRenderSize, &output); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 675 | ERRORF(reporter, "Input image: %s\n\n" |
| 676 | "===========================================================\n\n" |
| 677 | "Output image: %s\n", input.c_str(), output.c_str()); |
| 678 | loggedFirstFailure = true; |
| 679 | } |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 680 | } else if(failedPixelCount > 0) { |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 681 | // Don't trigger an error, but don't just hide the failures either. |
| 682 | INFOF(reporter, "Processor violated %d of %d pixels (below error threshold), seed: " |
| 683 | "0x%08x, processor: %s", failedPixelCount, kRenderSize * kRenderSize, |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 684 | seed, fp->dumpInfo().c_str()); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 685 | if (!coverageMessage.isEmpty()) { |
| 686 | INFOF(reporter, coverageMessage.c_str()); |
| 687 | } |
| 688 | if (!constMessage.isEmpty()) { |
| 689 | INFOF(reporter, constMessage.c_str()); |
| 690 | } |
| 691 | if (!opaqueMessage.isEmpty()) { |
| 692 | INFOF(reporter, opaqueMessage.c_str()); |
| 693 | } |
| 694 | if (!loggedFirstWarning) { |
| 695 | SkString input; |
Greg Daniel | 124486b | 2020-02-11 11:54:55 -0500 | [diff] [blame] | 696 | log_texture_view(context, inputTexture1, &input); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 697 | SkString output; |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 698 | log_pixels(readData1.get(), kRenderSize, &output); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 699 | INFOF(reporter, "Input image: %s\n\n" |
| 700 | "===========================================================\n\n" |
| 701 | "Output image: %s\n", input.c_str(), output.c_str()); |
| 702 | loggedFirstWarning = true; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | } |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 708 | |
John Stiles | 8c71f3e | 2020-06-15 15:07:19 -0400 | [diff] [blame] | 709 | static void describe_fp_children(const GrFragmentProcessor& fp, |
| 710 | std::string indent, |
| 711 | SkString* text) { |
| 712 | for (int index = 0; index < fp.numChildProcessors(); ++index) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 713 | const GrFragmentProcessor* childFP = fp.childProcessor(index); |
| 714 | text->appendf("\n%s(#%d) -> %s", indent.c_str(), index, childFP ? childFP->name() : "null"); |
| 715 | if (childFP) { |
| 716 | describe_fp_children(*childFP, indent + "\t", text); |
| 717 | } |
John Stiles | 8c71f3e | 2020-06-15 15:07:19 -0400 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | |
| 721 | static SkString describe_fp(const GrFragmentProcessor& fp) { |
| 722 | SkString text; |
| 723 | text.printf("\n%s", fp.name()); |
| 724 | describe_fp_children(fp, "\t", &text); |
| 725 | return text; |
| 726 | } |
| 727 | |
Brian Salomon | 3f4de78 | 2020-06-18 14:16:00 -0400 | [diff] [blame] | 728 | // Tests that a fragment processor returned by GrFragmentProcessor::clone() is equivalent to its |
| 729 | // progenitor. |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 730 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 731 | auto context = ctxInfo.directContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 732 | auto resourceProvider = context->priv().resourceProvider(); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 733 | |
| 734 | SkRandom random; |
| 735 | |
| 736 | // Make the destination context for the test. |
| 737 | static constexpr int kRenderSize = 1024; |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 738 | auto rtc = GrRenderTargetContext::Make( |
| 739 | context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact, |
| 740 | {kRenderSize, kRenderSize}); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 741 | |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 742 | GrProcessorTestData::ViewInfo views[2]; |
| 743 | if (!init_test_textures(resourceProvider, context, &random, views)) { |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 744 | ERRORF(reporter, "Could not create test textures"); |
| 745 | return; |
| 746 | } |
Greg Daniel | 026a60c | 2020-02-12 10:53:51 -0500 | [diff] [blame] | 747 | GrProcessorTestData testData(&random, context, 2, views); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 748 | |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 749 | auto inputTexture = make_input_texture(context, kRenderSize, kRenderSize, 0.0f); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 750 | std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]); |
| 751 | std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]); |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 752 | // On failure we write out images, but just write the first failing set as the print is very |
| 753 | // large. |
| 754 | bool loggedFirstFailure = false; |
| 755 | |
| 756 | // This test has a history of being flaky on a number of devices. If an FP clone is logically |
| 757 | // wrong, it's reasonable to expect it produce a large number of pixel differences in the image |
| 758 | // Sporadic pixel violations are more indicative device errors and represents a separate |
| 759 | // problem. |
| 760 | #if defined(SK_BUILD_FOR_SKQP) |
| 761 | static constexpr int kMaxAcceptableFailedPixels = 0; // Strict when running as SKQP |
| 762 | #else |
| 763 | static constexpr int kMaxAcceptableFailedPixels = 2 * kRenderSize; // ~0.7% of the image |
| 764 | #endif |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 765 | |
| 766 | // Because processor factories configure themselves in random ways, this is not exhaustive. |
| 767 | for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) { |
| 768 | static constexpr int kTimesToInvokeFactory = 10; |
| 769 | for (int j = 0; j < kTimesToInvokeFactory; ++j) { |
John Stiles | 8c71f3e | 2020-06-15 15:07:19 -0400 | [diff] [blame] | 770 | std::unique_ptr<GrFragmentProcessor> fp = |
| 771 | GrFragmentProcessorTestFactory::MakeIdx(i, &testData); |
| 772 | std::unique_ptr<GrFragmentProcessor> clone = fp->clone(); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 773 | if (!clone) { |
Brian Salomon | 96271cd | 2017-07-31 16:27:23 -0400 | [diff] [blame] | 774 | ERRORF(reporter, "Clone of processor %s failed.", fp->name()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 775 | continue; |
| 776 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 777 | const char* name = fp->name(); |
John Stiles | 8c71f3e | 2020-06-15 15:07:19 -0400 | [diff] [blame] | 778 | REPORTER_ASSERT(reporter, !strcmp(fp->name(), clone->name()), |
| 779 | "%s\n", describe_fp(*fp).c_str()); |
Brian Salomon | ce06e26 | 2017-08-01 16:23:40 -0400 | [diff] [blame] | 780 | REPORTER_ASSERT(reporter, fp->compatibleWithCoverageAsAlpha() == |
John Stiles | 8c71f3e | 2020-06-15 15:07:19 -0400 | [diff] [blame] | 781 | clone->compatibleWithCoverageAsAlpha(), |
| 782 | "%s\n", describe_fp(*fp).c_str()); |
| 783 | REPORTER_ASSERT(reporter, fp->isEqual(*clone), |
| 784 | "%s\n", describe_fp(*fp).c_str()); |
| 785 | REPORTER_ASSERT(reporter, fp->preservesOpaqueInput() == clone->preservesOpaqueInput(), |
| 786 | "%s\n", describe_fp(*fp).c_str()); |
Brian Salomon | ce06e26 | 2017-08-01 16:23:40 -0400 | [diff] [blame] | 787 | REPORTER_ASSERT(reporter, fp->hasConstantOutputForConstantInput() == |
John Stiles | 8c71f3e | 2020-06-15 15:07:19 -0400 | [diff] [blame] | 788 | clone->hasConstantOutputForConstantInput(), |
| 789 | "%s\n", describe_fp(*fp).c_str()); |
| 790 | REPORTER_ASSERT(reporter, fp->numChildProcessors() == clone->numChildProcessors(), |
| 791 | "%s\n", describe_fp(*fp).c_str()); |
Brian Osman | 9cf98dc | 2020-07-01 17:21:27 -0400 | [diff] [blame] | 792 | REPORTER_ASSERT(reporter, fp->usesVaryingCoords() == clone->usesVaryingCoords(), |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 793 | "%s\n", describe_fp(*fp).c_str()); |
| 794 | REPORTER_ASSERT(reporter, fp->referencesSampleCoords() == |
| 795 | clone->referencesSampleCoords(), |
John Stiles | 8c71f3e | 2020-06-15 15:07:19 -0400 | [diff] [blame] | 796 | "%s\n", describe_fp(*fp).c_str()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 797 | // Draw with original and read back the results. |
Brian Salomon | 3f4de78 | 2020-06-18 14:16:00 -0400 | [diff] [blame] | 798 | render_fp(context, rtc.get(), std::move(fp), inputTexture, kPremul_SkAlphaType, |
Greg Daniel | c594e62 | 2019-10-15 14:01:49 -0400 | [diff] [blame] | 799 | readData1.get()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 800 | |
| 801 | // Draw with clone and read back the results. |
Brian Salomon | 3f4de78 | 2020-06-18 14:16:00 -0400 | [diff] [blame] | 802 | render_fp(context, rtc.get(), std::move(clone), inputTexture, kPremul_SkAlphaType, |
Greg Daniel | c594e62 | 2019-10-15 14:01:49 -0400 | [diff] [blame] | 803 | readData2.get()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 804 | |
| 805 | // Check that the results are the same. |
| 806 | bool passing = true; |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 807 | int failedPixelCount = 0; |
| 808 | int firstWrongX = 0; |
| 809 | int firstWrongY = 0; |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 810 | for (int y = 0; y < kRenderSize && passing; ++y) { |
| 811 | for (int x = 0; x < kRenderSize && passing; ++x) { |
| 812 | int idx = y * kRenderSize + x; |
| 813 | if (readData1[idx] != readData2[idx]) { |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 814 | if (!failedPixelCount) { |
| 815 | firstWrongX = x; |
| 816 | firstWrongY = y; |
| 817 | } |
| 818 | ++failedPixelCount; |
| 819 | } |
| 820 | if (failedPixelCount > kMaxAcceptableFailedPixels) { |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 821 | passing = false; |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 822 | idx = firstWrongY * kRenderSize + firstWrongX; |
| 823 | ERRORF(reporter, |
| 824 | "Processor %s made clone produced different output at (%d, %d). " |
| 825 | "Input color: 0x%08x, Original Output Color: 0x%08x, " |
| 826 | "Clone Output Color: 0x%08x.", |
| 827 | name, firstWrongX, firstWrongY, input_texel_color(x, y, 0.0f), |
| 828 | readData1[idx], readData2[idx]); |
| 829 | if (!loggedFirstFailure) { |
| 830 | // Write the images out as data urls for inspection. |
| 831 | // We mark the data as unpremul to avoid conversion when encoding as |
| 832 | // PNG. Also, even though we made the data by rendering into |
| 833 | // a "unpremul" GrRenderTargetContext, our input texture is unpremul and |
| 834 | // outside of the random effect configuration, we didn't do anything to |
| 835 | // ensure the output is actually premul. |
| 836 | auto info = SkImageInfo::Make(kRenderSize, kRenderSize, |
| 837 | kRGBA_8888_SkColorType, |
| 838 | kUnpremul_SkAlphaType); |
Brian Salomon | 3f4de78 | 2020-06-18 14:16:00 -0400 | [diff] [blame] | 839 | SkString inputURL, origURL, cloneURL; |
| 840 | if (log_texture_view(context, inputTexture, &inputURL) && |
| 841 | log_pixels(readData1.get(), kRenderSize, &origURL) && |
| 842 | log_pixels(readData2.get(), kRenderSize, &cloneURL)) { |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 843 | ERRORF(reporter, |
| 844 | "\nInput image:\n%s\n\n" |
| 845 | "===========================================================" |
| 846 | "\n\n" |
| 847 | "Orig output image:\n%s\n" |
| 848 | "===========================================================" |
| 849 | "\n\n" |
| 850 | "Clone output image:\n%s\n", |
Brian Salomon | 3f4de78 | 2020-06-18 14:16:00 -0400 | [diff] [blame] | 851 | inputURL.c_str(), origURL.c_str(), cloneURL.c_str()); |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 852 | loggedFirstFailure = true; |
| 853 | } |
| 854 | } |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 855 | } |
| 856 | } |
| 857 | } |
| 858 | } |
| 859 | } |
| 860 | } |
| 861 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 862 | #endif // GR_TEST_UTILS |