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) { |
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 | 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 | |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 229 | void test_draw_op(GrContext* context, GrRenderTargetContext* rtc, |
| 230 | std::unique_ptr<GrFragmentProcessor> fp) { |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 231 | GrPaint paint; |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 232 | paint.addColorFragmentProcessor(std::move(fp)); |
| 233 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 234 | |
Michael Ludwig | aa1b6b3 | 2019-05-29 14:43:13 -0400 | [diff] [blame] | 235 | auto op = GrFillRectOp::MakeNonAARect(context, std::move(paint), SkMatrix::I(), |
| 236 | SkRect::MakeWH(rtc->width(), rtc->height())); |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 237 | rtc->priv().testingOnly_addDrawOp(std::move(op)); |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 238 | } |
| 239 | |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 240 | // The output buffer must be the same size as the render-target context. |
| 241 | void render_fp(GrContext* context, GrRenderTargetContext* rtc, |
Brian Salomon | 3f4de78 | 2020-06-18 14:16:00 -0400 | [diff] [blame] | 242 | std::unique_ptr<GrFragmentProcessor> fp, |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 243 | GrColor* outBuffer) { |
| 244 | test_draw_op(context, rtc, std::move(fp)); |
| 245 | std::fill_n(outBuffer, rtc->width() * rtc->height(), 0); |
| 246 | rtc->readPixels(SkImageInfo::Make(rtc->width(), rtc->height(), kRGBA_8888_SkColorType, |
Brian Salomon | 9f2b86c | 2019-10-22 10:37:46 -0400 | [diff] [blame] | 247 | kPremul_SkAlphaType), |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 248 | outBuffer, /*rowBytes=*/0, /*srcPt=*/{0, 0}); |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 249 | } |
| 250 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 251 | // This class is responsible for reproducibly generating a random fragment processor. |
| 252 | // An identical randomly-designed FP can be generated as many times as needed. |
| 253 | class TestFPGenerator { |
| 254 | public: |
| 255 | TestFPGenerator() = delete; |
| 256 | TestFPGenerator(GrDirectContext* context, GrResourceProvider* resourceProvider) |
| 257 | : fContext(context) |
| 258 | , fResourceProvider(resourceProvider) |
| 259 | , fInitialSeed(synthesizeInitialSeed()) |
| 260 | , fRandomSeed(fInitialSeed) {} |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 261 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 262 | uint32_t initialSeed() { return fInitialSeed; } |
| 263 | |
| 264 | bool init() { |
| 265 | // Initializes the two test texture proxies that are available to the FP test factories. |
| 266 | SkRandom random{fRandomSeed}; |
| 267 | static constexpr int kTestTextureSize = 256; |
| 268 | |
| 269 | { |
| 270 | // Put premul data into the RGBA texture that the test FPs can optionally use. |
| 271 | GrColor* rgbaData = new GrColor[kTestTextureSize * kTestTextureSize]; |
| 272 | for (int y = 0; y < kTestTextureSize; ++y) { |
| 273 | for (int x = 0; x < kTestTextureSize; ++x) { |
| 274 | rgbaData[kTestTextureSize * y + x] = input_texel_color( |
| 275 | random.nextULessThan(256), random.nextULessThan(256), 0.0f); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize, |
| 280 | kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 281 | SkBitmap bitmap; |
| 282 | bitmap.installPixels( |
| 283 | ii, rgbaData, ii.minRowBytes(), |
| 284 | [](void* addr, void* context) { delete[](GrColor*) addr; }, nullptr); |
| 285 | bitmap.setImmutable(); |
| 286 | GrBitmapTextureMaker maker(fContext, bitmap, |
| 287 | GrImageTexGenPolicy::kNew_Uncached_Budgeted); |
| 288 | GrSurfaceProxyView view = maker.view(GrMipMapped::kNo); |
| 289 | if (!view.proxy() || !view.proxy()->instantiate(fResourceProvider)) { |
| 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(); |
| 313 | GrBitmapTextureMaker maker(fContext, bitmap, |
| 314 | GrImageTexGenPolicy::kNew_Uncached_Budgeted); |
| 315 | GrSurfaceProxyView view = maker.view(GrMipMapped::kNo); |
| 316 | if (!view.proxy() || !view.proxy()->instantiate(fResourceProvider)) { |
| 317 | SkDebugf("Unable to instantiate A8 test texture."); |
| 318 | return false; |
| 319 | } |
| 320 | fTestViews[1] = GrProcessorTestData::ViewInfo{view, GrColorType::kAlpha_8, |
| 321 | kPremul_SkAlphaType}; |
| 322 | } |
| 323 | |
| 324 | return true; |
| 325 | } |
| 326 | |
| 327 | void reroll() { |
| 328 | // Feed our current random seed into SkRandom to generate a new seed. |
| 329 | SkRandom random{fRandomSeed}; |
| 330 | fRandomSeed = random.nextU(); |
| 331 | } |
| 332 | |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 333 | std::unique_ptr<GrFragmentProcessor> make(int type, |
| 334 | std::unique_ptr<GrFragmentProcessor> inputFP) { |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 335 | // This will generate the exact same randomized FP (of each requested type) each time |
| 336 | // it's called. Call `reroll` to get a different FP. |
| 337 | SkRandom random{fRandomSeed}; |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 338 | GrProcessorTestData testData{&random, fContext, SK_ARRAY_COUNT(fTestViews), fTestViews, |
| 339 | std::move(inputFP)}; |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 340 | return GrFragmentProcessorTestFactory::MakeIdx(type, &testData); |
| 341 | } |
| 342 | |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 343 | std::unique_ptr<GrFragmentProcessor> make(int type, GrSurfaceProxyView view, |
| 344 | SkAlphaType alpha = kPremul_SkAlphaType) { |
| 345 | return make(type, GrTextureEffect::Make(view, alpha)); |
| 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 | |
| 365 | // Creates a texture of premul colors used as the output of the fragment processor that precedes |
| 366 | // 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] | 367 | GrSurfaceProxyView make_input_texture(GrRecordingContext* context, int width, int height, |
| 368 | SkScalar delta) { |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 369 | GrColor* data = new GrColor[width * height]; |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 370 | for (int y = 0; y < width; ++y) { |
| 371 | for (int x = 0; x < height; ++x) { |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 372 | data[width * y + x] = input_texel_color(x, y, delta); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 373 | } |
| 374 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 375 | |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 376 | SkImageInfo ii = SkImageInfo::Make(width, height, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 377 | SkBitmap bitmap; |
| 378 | bitmap.installPixels(ii, data, ii.minRowBytes(), |
| 379 | [](void* addr, void* context) { delete[] (GrColor*)addr; }, nullptr); |
| 380 | bitmap.setImmutable(); |
Brian Salomon | bc074a6 | 2020-03-18 10:06:13 -0400 | [diff] [blame] | 381 | GrBitmapTextureMaker maker(context, bitmap, GrImageTexGenPolicy::kNew_Uncached_Budgeted); |
Brian Salomon | ecbb0fb | 2020-02-28 18:07:32 -0500 | [diff] [blame] | 382 | return maker.view(GrMipMapped::kNo); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 383 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 384 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 385 | // 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] | 386 | // actually contains unpremul data. Also, even though we made the result data by rendering into |
| 387 | // a "unpremul" GrRenderTargetContext, our input texture is unpremul and outside of the random |
| 388 | // effect configuration, we didn't do anything to ensure the output is actually premul. We just |
| 389 | // don't currently allow kUnpremul GrRenderTargetContexts. |
| 390 | static constexpr auto kLogAlphaType = kUnpremul_SkAlphaType; |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 391 | |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 392 | bool log_pixels(GrColor* pixels, int widthHeight, SkString* dst) { |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 393 | SkImageInfo info = |
| 394 | SkImageInfo::Make(widthHeight, widthHeight, kRGBA_8888_SkColorType, kLogAlphaType); |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 395 | SkBitmap bmp; |
| 396 | bmp.installPixels(info, pixels, widthHeight * sizeof(GrColor)); |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 397 | return BipmapToBase64DataURI(bmp, dst); |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 398 | } |
| 399 | |
Greg Daniel | 124486b | 2020-02-11 11:54:55 -0500 | [diff] [blame] | 400 | bool log_texture_view(GrContext* context, GrSurfaceProxyView src, SkString* dst) { |
| 401 | SkImageInfo ii = SkImageInfo::Make(src.proxy()->dimensions(), kRGBA_8888_SkColorType, |
| 402 | kLogAlphaType); |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 403 | |
Greg Daniel | 124486b | 2020-02-11 11:54:55 -0500 | [diff] [blame] | 404 | auto sContext = GrSurfaceContext::Make(context, std::move(src), GrColorType::kRGBA_8888, |
Greg Daniel | 3912a4b | 2020-01-14 09:56:04 -0500 | [diff] [blame] | 405 | kLogAlphaType, nullptr); |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 406 | SkBitmap bm; |
| 407 | SkAssertResult(bm.tryAllocPixels(ii)); |
| 408 | SkAssertResult(sContext->readPixels(ii, bm.getPixels(), bm.rowBytes(), {0, 0})); |
Brian Salomon | 28a8f28 | 2019-10-24 20:07:39 -0400 | [diff] [blame] | 409 | return BipmapToBase64DataURI(bm, dst); |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 410 | } |
| 411 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 412 | bool fuzzy_color_equals(const SkPMColor4f& c1, const SkPMColor4f& c2) { |
| 413 | // 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] | 414 | // 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] | 415 | // (really 1/128 ~ .0078, but frequently floating point issues make that tolerance a little |
| 416 | // too unforgiving). |
Brian Salomon | 682ba43 | 2019-12-17 20:20:23 +0000 | [diff] [blame] | 417 | static constexpr SkScalar kTolerance = 0.01f; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 418 | for (int i = 0; i < 4; i++) { |
| 419 | if (!SkScalarNearlyEqual(c1[i], c2[i], kTolerance)) { |
| 420 | return false; |
| 421 | } |
| 422 | } |
| 423 | return true; |
| 424 | } |
| 425 | |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 426 | // Given three input colors (color preceding the FP being tested) provided to the FP at the same |
| 427 | // local coord and the three corresponding FP outputs, this ensures that either: |
| 428 | // out[0] = fp * in[0].a, out[1] = fp * in[1].a, and out[2] = fp * in[2].a |
| 429 | // where fp is the pre-modulated color that should not be changing across frames (FP's state doesn't |
| 430 | // change), OR: |
| 431 | // out[0] = fp * in[0], out[1] = fp * in[1], and out[2] = fp * in[2] |
| 432 | // (per-channel modulation instead of modulation by just the alpha channel) |
| 433 | // It does this by estimating the pre-modulated fp color from one of the input/output pairs and |
| 434 | // confirms the conditions hold for the other two pairs. |
| 435 | // It is required that the three input colors have the same alpha as fp is allowed to be a function |
| 436 | // of the input alpha (but not r, g, or b). |
| 437 | bool legal_modulation(const GrColor in[3], const GrColor out[3]) { |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 438 | // 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] | 439 | SkPMColor4f inf[3], outf[3]; |
| 440 | for (int i = 0; i < 3; ++i) { |
| 441 | inf[i] = SkPMColor4f::FromBytes_RGBA(in[i]); |
| 442 | outf[i] = SkPMColor4f::FromBytes_RGBA(out[i]); |
| 443 | } |
| 444 | // This test is only valid if all the input alphas are the same. |
| 445 | 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] | 446 | |
| 447 | // Reconstruct the output of the FP before the shader modulated its color with the input value. |
| 448 | // When the original input is very small, it may cause the final output color to round |
| 449 | // to 0, in which case we estimate the pre-modulated color using one of the stepped frames that |
| 450 | // 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] | 451 | SkPMColor4f fpPreColorModulation = {0,0,0,0}; |
| 452 | SkPMColor4f fpPreAlphaModulation = {0,0,0,0}; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 453 | for (int i = 0; i < 4; i++) { |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 454 | // Use the most stepped up frame |
| 455 | int maxInIdx = inf[0][i] > inf[1][i] ? 0 : 1; |
| 456 | maxInIdx = inf[maxInIdx][i] > inf[2][i] ? maxInIdx : 2; |
John Stiles | fe7aed6 | 2020-07-20 13:48:02 -0400 | [diff] [blame] | 457 | const SkPMColor4f& in = inf[maxInIdx]; |
| 458 | const SkPMColor4f& out = outf[maxInIdx]; |
Brian Salomon | 0a7ca7a | 2019-12-27 12:18:19 -0500 | [diff] [blame] | 459 | if (in[i] > 0) { |
| 460 | fpPreColorModulation[i] = out[i] / in[i]; |
| 461 | } |
| 462 | if (in[3] > 0) { |
| 463 | fpPreAlphaModulation[i] = out[i] / in[3]; |
| 464 | } |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | // With reconstructed pre-modulated FP output, derive the expected value of fp * input for each |
| 468 | // of the transformed input colors. |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 469 | SkPMColor4f expectedForAlphaModulation[3]; |
| 470 | SkPMColor4f expectedForColorModulation[3]; |
| 471 | for (int i = 0; i < 3; ++i) { |
| 472 | expectedForAlphaModulation[i] = fpPreAlphaModulation * inf[i].fA; |
| 473 | expectedForColorModulation[i] = fpPreColorModulation * inf[i]; |
| 474 | // If the input alpha is 0 then the other channels should also be zero |
| 475 | // since the color is assumed to be premul. Modulating zeros by anything |
| 476 | // should produce zeros. |
| 477 | if (inf[i].fA == 0) { |
| 478 | SkASSERT(inf[i].fR == 0 && inf[i].fG == 0 && inf[i].fB == 0); |
| 479 | expectedForColorModulation[i] = expectedForAlphaModulation[i] = {0, 0, 0, 0}; |
| 480 | } |
| 481 | } |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 482 | |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 483 | bool isLegalColorModulation = fuzzy_color_equals(outf[0], expectedForColorModulation[0]) && |
| 484 | fuzzy_color_equals(outf[1], expectedForColorModulation[1]) && |
| 485 | fuzzy_color_equals(outf[2], expectedForColorModulation[2]); |
| 486 | |
| 487 | bool isLegalAlphaModulation = fuzzy_color_equals(outf[0], expectedForAlphaModulation[0]) && |
| 488 | fuzzy_color_equals(outf[1], expectedForAlphaModulation[1]) && |
| 489 | fuzzy_color_equals(outf[2], expectedForAlphaModulation[2]); |
| 490 | |
| 491 | // This can be enabled to print the values that caused this check to fail. |
| 492 | if (0 && !isLegalColorModulation && !isLegalAlphaModulation) { |
| 493 | SkDebugf("Color modulation test\n\timplied mod color: (%.03f, %.03f, %.03f, %.03f)\n", |
| 494 | fpPreColorModulation[0], |
| 495 | fpPreColorModulation[1], |
| 496 | fpPreColorModulation[2], |
| 497 | fpPreColorModulation[3]); |
| 498 | for (int i = 0; i < 3; ++i) { |
| 499 | SkDebugf("\t(%.03f, %.03f, %.03f, %.03f) -> " |
| 500 | "(%.03f, %.03f, %.03f, %.03f) | " |
| 501 | "(%.03f, %.03f, %.03f, %.03f), ok: %d\n", |
| 502 | inf[i].fR, inf[i].fG, inf[i].fB, inf[i].fA, |
| 503 | outf[i].fR, outf[i].fG, outf[i].fB, outf[i].fA, |
| 504 | expectedForColorModulation[i].fR, expectedForColorModulation[i].fG, |
| 505 | expectedForColorModulation[i].fB, expectedForColorModulation[i].fA, |
| 506 | fuzzy_color_equals(outf[i], expectedForColorModulation[i])); |
| 507 | } |
| 508 | SkDebugf("Alpha modulation test\n\timplied mod color: (%.03f, %.03f, %.03f, %.03f)\n", |
| 509 | fpPreAlphaModulation[0], |
| 510 | fpPreAlphaModulation[1], |
| 511 | fpPreAlphaModulation[2], |
| 512 | fpPreAlphaModulation[3]); |
| 513 | for (int i = 0; i < 3; ++i) { |
| 514 | SkDebugf("\t(%.03f, %.03f, %.03f, %.03f) -> " |
| 515 | "(%.03f, %.03f, %.03f, %.03f) | " |
| 516 | "(%.03f, %.03f, %.03f, %.03f), ok: %d\n", |
| 517 | inf[i].fR, inf[i].fG, inf[i].fB, inf[i].fA, |
| 518 | outf[i].fR, outf[i].fG, outf[i].fB, outf[i].fA, |
| 519 | expectedForAlphaModulation[i].fR, expectedForAlphaModulation[i].fG, |
| 520 | expectedForAlphaModulation[i].fB, expectedForAlphaModulation[i].fA, |
| 521 | fuzzy_color_equals(outf[i], expectedForAlphaModulation[i])); |
| 522 | } |
| 523 | } |
| 524 | return isLegalColorModulation || isLegalAlphaModulation; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 525 | } |
| 526 | |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 527 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) { |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 528 | GrDirectContext* context = ctxInfo.directContext(); |
| 529 | GrResourceProvider* resourceProvider = context->priv().resourceProvider(); |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 530 | using FPFactory = GrFragmentProcessorTestFactory; |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 531 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 532 | TestFPGenerator fpGenerator{context, resourceProvider}; |
| 533 | if (!fpGenerator.init()) { |
| 534 | ERRORF(reporter, "Could not initialize TestFPGenerator"); |
| 535 | return; |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 536 | } |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 537 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 538 | // Make the destination context for the test. |
| 539 | static constexpr int kRenderSize = 256; |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 540 | auto rtc = GrRenderTargetContext::Make( |
| 541 | context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact, |
| 542 | {kRenderSize, kRenderSize}); |
Brian Salomon | d1a8bdf | 2017-02-10 12:39:26 -0500 | [diff] [blame] | 543 | |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 544 | // Coverage optimization uses three frames with a linearly transformed input texture. The first |
| 545 | // frame has no offset, second frames add .2 and .4, which should then be present as a fixed |
| 546 | // difference between the frame outputs if the FP is properly following the modulation |
| 547 | // requirements of the coverage optimization. |
| 548 | static constexpr SkScalar kInputDelta = 0.2f; |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 549 | GrSurfaceProxyView inputTexture1 = make_input_texture(context, kRenderSize, kRenderSize, 0.0f); |
| 550 | GrSurfaceProxyView inputTexture2 = make_input_texture(context, kRenderSize, kRenderSize, |
| 551 | kInputDelta); |
| 552 | GrSurfaceProxyView inputTexture3 = make_input_texture(context, kRenderSize, kRenderSize, |
| 553 | 2 * kInputDelta); |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 554 | |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 555 | // Encoded images are very verbose and this tests many potential images, so only export the |
| 556 | // first failure (subsequent failures have a reasonable chance of being related). |
| 557 | bool loggedFirstFailure = false; |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 558 | bool loggedFirstWarning = false; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 559 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 560 | // Storage for the three frames required for coverage compatibility optimization testing. |
| 561 | // Each frame uses the correspondingly numbered inputTextureX. |
| 562 | std::vector<GrColor> readData1(kRenderSize * kRenderSize); |
| 563 | std::vector<GrColor> readData2(kRenderSize * kRenderSize); |
| 564 | std::vector<GrColor> readData3(kRenderSize * kRenderSize); |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 565 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 566 | // Because processor factories configure themselves in random ways, this is not exhaustive. |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 567 | for (int i = 0; i < FPFactory::Count(); ++i) { |
John Stiles | fe7aed6 | 2020-07-20 13:48:02 -0400 | [diff] [blame] | 568 | int optimizedForOpaqueInput = 0; |
| 569 | int optimizedForCoverageAsAlpha = 0; |
| 570 | int optimizedForConstantOutputForInput = 0; |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 571 | |
John Stiles | fe7aed6 | 2020-07-20 13:48:02 -0400 | [diff] [blame] | 572 | #ifdef __MSVC_RUNTIME_CHECKS |
Brian Osman | 50ea3c0 | 2019-02-04 10:01:53 -0500 | [diff] [blame] | 573 | // This test is infuriatingly slow with MSVC runtime checks enabled |
John Stiles | fe7aed6 | 2020-07-20 13:48:02 -0400 | [diff] [blame] | 574 | static constexpr int kMinimumTrials = 1; |
| 575 | static constexpr int kMaximumTrials = 1; |
| 576 | static constexpr int kExpectedSuccesses = 1; |
| 577 | #else |
| 578 | // We start by testing each fragment-processor 100 times, watching the optimization bits |
| 579 | // that appear. If we see an optimization bit appear in those first 100 trials, we keep |
| 580 | // running tests until we see at least five successful trials that have this optimization |
| 581 | // bit enabled. If we never see a particular optimization bit after 100 trials, we assume |
| 582 | // that this FP doesn't support that optimization at all. |
| 583 | static constexpr int kMinimumTrials = 100; |
| 584 | static constexpr int kMaximumTrials = 2000; |
| 585 | static constexpr int kExpectedSuccesses = 5; |
Brian Osman | 50ea3c0 | 2019-02-04 10:01:53 -0500 | [diff] [blame] | 586 | #endif |
Robert Phillips | a91e0b7 | 2017-05-01 13:12:20 -0400 | [diff] [blame] | 587 | |
John Stiles | fe7aed6 | 2020-07-20 13:48:02 -0400 | [diff] [blame] | 588 | for (int trial = 0;; ++trial) { |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 589 | // Create a randomly-configured FP. |
| 590 | fpGenerator.reroll(); |
John Stiles | fe7aed6 | 2020-07-20 13:48:02 -0400 | [diff] [blame] | 591 | std::unique_ptr<GrFragmentProcessor> fp = fpGenerator.make(i, inputTexture1); |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 592 | |
John Stiles | fe7aed6 | 2020-07-20 13:48:02 -0400 | [diff] [blame] | 593 | // If we have iterated enough times and seen a sufficient number of successes on each |
| 594 | // optimization bit that can be returned, stop running trials. |
| 595 | if (trial >= kMinimumTrials) { |
| 596 | bool moreTrialsNeeded = (optimizedForOpaqueInput > 0 && |
| 597 | optimizedForOpaqueInput < kExpectedSuccesses) || |
| 598 | (optimizedForCoverageAsAlpha > 0 && |
| 599 | optimizedForCoverageAsAlpha < kExpectedSuccesses) || |
| 600 | (optimizedForConstantOutputForInput > 0 && |
| 601 | optimizedForConstantOutputForInput < kExpectedSuccesses); |
| 602 | if (!moreTrialsNeeded) break; |
| 603 | |
| 604 | if (trial >= kMaximumTrials) { |
| 605 | SkDebugf("Abandoning ProcessorOptimizationValidationTest after %d trials. " |
| 606 | "Seed: 0x%08x, processor: %s.", |
| 607 | kMaximumTrials, fpGenerator.initialSeed(), fp->name()); |
| 608 | break; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | // Skip further testing if this trial has no optimization bits enabled. |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 613 | if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() && |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 614 | !fp->compatibleWithCoverageAsAlpha()) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 615 | continue; |
| 616 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 617 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 618 | // 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] | 619 | if (fp->compatibleWithCoverageAsAlpha()) { |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 620 | // Create and render two identical versions of this FP, but using different input |
| 621 | // textures, to check coverage optimization. We don't need to do this step for |
| 622 | // constant-output or preserving-opacity tests. |
| 623 | render_fp(context, rtc.get(), fpGenerator.make(i, inputTexture2), readData2.data()); |
| 624 | render_fp(context, rtc.get(), fpGenerator.make(i, inputTexture3), readData3.data()); |
John Stiles | fe7aed6 | 2020-07-20 13:48:02 -0400 | [diff] [blame] | 625 | ++optimizedForCoverageAsAlpha; |
| 626 | } |
| 627 | |
| 628 | if (fp->hasConstantOutputForConstantInput()) { |
| 629 | ++optimizedForConstantOutputForInput; |
| 630 | } |
| 631 | |
| 632 | if (fp->preservesOpaqueInput()) { |
| 633 | ++optimizedForOpaqueInput; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 634 | } |
Brian Salomon | 3f4de78 | 2020-06-18 14:16:00 -0400 | [diff] [blame] | 635 | |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 636 | // Draw base frame last so that rtc holds the original FP behavior if we need to dump |
| 637 | // the image to the log. |
| 638 | render_fp(context, rtc.get(), fpGenerator.make(i, inputTexture1), readData1.data()); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 639 | |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 640 | // This test has a history of being flaky on a number of devices. If an FP is logically |
| 641 | // violating the optimizations, it's reasonable to expect it to violate requirements on |
| 642 | // a large number of pixels in the image. Sporadic pixel violations are more indicative |
| 643 | // of device errors and represents a separate problem. |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 644 | #if defined(SK_BUILD_FOR_SKQP) |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 645 | static constexpr int kMaxAcceptableFailedPixels = 0; // Strict when running as SKQP |
| 646 | #else |
| 647 | static constexpr int kMaxAcceptableFailedPixels = 2 * kRenderSize; // ~0.7% of the image |
| 648 | #endif |
| 649 | |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 650 | // Collect first optimization failure message, to be output later as a warning or an |
| 651 | // error depending on whether the rendering "passed" or failed. |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 652 | int failedPixelCount = 0; |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 653 | SkString coverageMessage; |
| 654 | SkString opaqueMessage; |
| 655 | SkString constMessage; |
| 656 | for (int y = 0; y < kRenderSize; ++y) { |
| 657 | for (int x = 0; x < kRenderSize; ++x) { |
| 658 | bool passing = true; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 659 | GrColor input = input_texel_color(x, y, 0.0f); |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 660 | GrColor output = readData1[y * kRenderSize + x]; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 661 | |
| 662 | if (fp->compatibleWithCoverageAsAlpha()) { |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 663 | GrColor ins[3]; |
| 664 | ins[0] = input; |
| 665 | ins[1] = input_texel_color(x, y, kInputDelta); |
| 666 | ins[2] = input_texel_color(x, y, 2 * kInputDelta); |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 667 | |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 668 | GrColor outs[3]; |
| 669 | outs[0] = output; |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 670 | outs[1] = readData2[y * kRenderSize + x]; |
| 671 | outs[2] = readData3[y * kRenderSize + x]; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 672 | |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 673 | if (!legal_modulation(ins, outs)) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 674 | passing = false; |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 675 | if (coverageMessage.isEmpty()) { |
Brian Salomon | bc73eb4 | 2019-12-18 14:57:45 -0500 | [diff] [blame] | 676 | coverageMessage.printf( |
| 677 | "\"Modulating\" processor %s did not match " |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 678 | "alpha-modulation nor color-modulation rules. " |
| 679 | "Input: 0x%08x, Output: 0x%08x, pixel (%d, %d).", |
| 680 | fp->name(), input, output, x, y); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 681 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 682 | } |
| 683 | } |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 684 | |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 685 | SkPMColor4f input4f = SkPMColor4f::FromBytes_RGBA(input); |
| 686 | SkPMColor4f output4f = SkPMColor4f::FromBytes_RGBA(output); |
Brian Osman | 1d5b598 | 2018-10-01 13:41:39 -0400 | [diff] [blame] | 687 | SkPMColor4f expected4f; |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 688 | if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) { |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 689 | float rDiff = fabsf(output4f.fR - expected4f.fR); |
| 690 | float gDiff = fabsf(output4f.fG - expected4f.fG); |
| 691 | float bDiff = fabsf(output4f.fB - expected4f.fB); |
| 692 | float aDiff = fabsf(output4f.fA - expected4f.fA); |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 693 | static constexpr float kTol = 4 / 255.f; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 694 | if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) { |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 695 | if (constMessage.isEmpty()) { |
| 696 | passing = false; |
| 697 | |
| 698 | constMessage.printf("Processor %s claimed output for const input " |
| 699 | "doesn't match actual output. Error: %f, Tolerance: %f, " |
| 700 | "input: (%f, %f, %f, %f), actual: (%f, %f, %f, %f), " |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 701 | "expected(%f, %f, %f, %f)", fp->name(), |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 702 | std::max(rDiff, std::max(gDiff, std::max(bDiff, aDiff))), kTol, |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 703 | input4f.fR, input4f.fG, input4f.fB, input4f.fA, |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 704 | output4f.fR, output4f.fG, output4f.fB, output4f.fA, |
| 705 | expected4f.fR, expected4f.fG, expected4f.fB, expected4f.fA); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 706 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 707 | } |
| 708 | } |
Brian Osman | 00b2939 | 2018-11-05 15:42:43 -0500 | [diff] [blame] | 709 | if (input4f.isOpaque() && fp->preservesOpaqueInput() && !output4f.isOpaque()) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 710 | passing = false; |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 711 | |
| 712 | if (opaqueMessage.isEmpty()) { |
| 713 | opaqueMessage.printf("Processor %s claimed opaqueness is preserved but " |
| 714 | "it is not. Input: 0x%08x, Output: 0x%08x.", |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 715 | fp->name(), input, output); |
Michael Ludwig | e8e1075 | 2018-10-01 12:42:53 -0400 | [diff] [blame] | 716 | } |
Brian Osman | bd1f76f | 2017-03-15 11:33:12 -0400 | [diff] [blame] | 717 | } |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 718 | |
| 719 | if (!passing) { |
| 720 | // Regardless of how many optimizations the pixel violates, count it as a |
| 721 | // single bad pixel. |
| 722 | failedPixelCount++; |
| 723 | } |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | // Finished analyzing the entire image, see if the number of pixel failures meets the |
| 728 | // threshold for an FP violating the optimization requirements. |
| 729 | if (failedPixelCount > kMaxAcceptableFailedPixels) { |
Michael Ludwig | 4504a652 | 2018-10-03 16:47:55 -0400 | [diff] [blame] | 730 | 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] | 731 | ", first failing pixel details are below:", |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 732 | failedPixelCount, kRenderSize * kRenderSize, fpGenerator.initialSeed(), |
Michael Ludwig | 7034f15 | 2018-10-08 16:43:58 -0400 | [diff] [blame] | 733 | fp->dumpInfo().c_str()); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 734 | |
| 735 | // Print first failing pixel's details. |
| 736 | if (!coverageMessage.isEmpty()) { |
| 737 | ERRORF(reporter, coverageMessage.c_str()); |
| 738 | } |
| 739 | if (!constMessage.isEmpty()) { |
| 740 | ERRORF(reporter, constMessage.c_str()); |
| 741 | } |
| 742 | if (!opaqueMessage.isEmpty()) { |
| 743 | ERRORF(reporter, opaqueMessage.c_str()); |
| 744 | } |
| 745 | |
| 746 | if (!loggedFirstFailure) { |
| 747 | // Print with ERRORF to make sure the encoded image is output |
| 748 | SkString input; |
Greg Daniel | 124486b | 2020-02-11 11:54:55 -0500 | [diff] [blame] | 749 | log_texture_view(context, inputTexture1, &input); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 750 | SkString output; |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 751 | log_pixels(readData1.data(), kRenderSize, &output); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 752 | ERRORF(reporter, "Input image: %s\n\n" |
| 753 | "===========================================================\n\n" |
| 754 | "Output image: %s\n", input.c_str(), output.c_str()); |
| 755 | loggedFirstFailure = true; |
| 756 | } |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 757 | } else if (failedPixelCount > 0) { |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 758 | // Don't trigger an error, but don't just hide the failures either. |
| 759 | INFOF(reporter, "Processor violated %d of %d pixels (below error threshold), seed: " |
| 760 | "0x%08x, processor: %s", failedPixelCount, kRenderSize * kRenderSize, |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 761 | fpGenerator.initialSeed(), fp->dumpInfo().c_str()); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 762 | if (!coverageMessage.isEmpty()) { |
| 763 | INFOF(reporter, coverageMessage.c_str()); |
| 764 | } |
| 765 | if (!constMessage.isEmpty()) { |
| 766 | INFOF(reporter, constMessage.c_str()); |
| 767 | } |
| 768 | if (!opaqueMessage.isEmpty()) { |
| 769 | INFOF(reporter, opaqueMessage.c_str()); |
| 770 | } |
| 771 | if (!loggedFirstWarning) { |
| 772 | SkString input; |
Greg Daniel | 124486b | 2020-02-11 11:54:55 -0500 | [diff] [blame] | 773 | log_texture_view(context, inputTexture1, &input); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 774 | SkString output; |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 775 | log_pixels(readData1.data(), kRenderSize, &output); |
Michael Ludwig | 314d377 | 2018-10-03 16:04:38 -0400 | [diff] [blame] | 776 | INFOF(reporter, "Input image: %s\n\n" |
| 777 | "===========================================================\n\n" |
| 778 | "Output image: %s\n", input.c_str(), output.c_str()); |
| 779 | loggedFirstWarning = true; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | } |
| 783 | } |
| 784 | } |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 785 | |
John Stiles | 8c71f3e | 2020-06-15 15:07:19 -0400 | [diff] [blame] | 786 | static void describe_fp_children(const GrFragmentProcessor& fp, |
| 787 | std::string indent, |
| 788 | SkString* text) { |
| 789 | for (int index = 0; index < fp.numChildProcessors(); ++index) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 790 | const GrFragmentProcessor* childFP = fp.childProcessor(index); |
| 791 | text->appendf("\n%s(#%d) -> %s", indent.c_str(), index, childFP ? childFP->name() : "null"); |
| 792 | if (childFP) { |
| 793 | describe_fp_children(*childFP, indent + "\t", text); |
| 794 | } |
John Stiles | 8c71f3e | 2020-06-15 15:07:19 -0400 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | |
| 798 | static SkString describe_fp(const GrFragmentProcessor& fp) { |
| 799 | SkString text; |
| 800 | text.printf("\n%s", fp.name()); |
| 801 | describe_fp_children(fp, "\t", &text); |
| 802 | return text; |
| 803 | } |
| 804 | |
Brian Salomon | 3f4de78 | 2020-06-18 14:16:00 -0400 | [diff] [blame] | 805 | // Tests that a fragment processor returned by GrFragmentProcessor::clone() is equivalent to its |
| 806 | // progenitor. |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 807 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) { |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 808 | GrDirectContext* context = ctxInfo.directContext(); |
| 809 | GrResourceProvider* resourceProvider = context->priv().resourceProvider(); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 810 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 811 | TestFPGenerator fpGenerator{context, resourceProvider}; |
| 812 | if (!fpGenerator.init()) { |
| 813 | ERRORF(reporter, "Could not initialize TestFPGenerator"); |
| 814 | return; |
| 815 | } |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 816 | |
| 817 | // Make the destination context for the test. |
| 818 | static constexpr int kRenderSize = 1024; |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 819 | auto rtc = GrRenderTargetContext::Make( |
| 820 | context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact, |
| 821 | {kRenderSize, kRenderSize}); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 822 | |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 823 | GrSurfaceProxyView inputTexture = make_input_texture(context, kRenderSize, kRenderSize, 0.0f); |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 824 | |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 825 | // On failure we write out images, but just write the first failing set as the print is very |
| 826 | // large. |
| 827 | bool loggedFirstFailure = false; |
| 828 | |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 829 | // Storage for the original frame's readback and the readback of its clone. |
| 830 | std::vector<GrColor> readData1(kRenderSize * kRenderSize); |
| 831 | std::vector<GrColor> readData2(kRenderSize * kRenderSize); |
| 832 | |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 833 | // This test has a history of being flaky on a number of devices. If an FP clone is logically |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 834 | // wrong, it's reasonable to expect it produce a large number of pixel differences in the image. |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 835 | // Sporadic pixel violations are more indicative device errors and represents a separate |
| 836 | // problem. |
| 837 | #if defined(SK_BUILD_FOR_SKQP) |
| 838 | static constexpr int kMaxAcceptableFailedPixels = 0; // Strict when running as SKQP |
| 839 | #else |
| 840 | static constexpr int kMaxAcceptableFailedPixels = 2 * kRenderSize; // ~0.7% of the image |
| 841 | #endif |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 842 | |
| 843 | // Because processor factories configure themselves in random ways, this is not exhaustive. |
| 844 | for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) { |
| 845 | static constexpr int kTimesToInvokeFactory = 10; |
| 846 | for (int j = 0; j < kTimesToInvokeFactory; ++j) { |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 847 | fpGenerator.reroll(); |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 848 | std::unique_ptr<GrFragmentProcessor> fp = fpGenerator.make(i, /*inputFP=*/nullptr); |
John Stiles | 8c71f3e | 2020-06-15 15:07:19 -0400 | [diff] [blame] | 849 | std::unique_ptr<GrFragmentProcessor> clone = fp->clone(); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 850 | if (!clone) { |
Brian Salomon | 96271cd | 2017-07-31 16:27:23 -0400 | [diff] [blame] | 851 | ERRORF(reporter, "Clone of processor %s failed.", fp->name()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 852 | continue; |
| 853 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 854 | const char* name = fp->name(); |
John Stiles | 8c71f3e | 2020-06-15 15:07:19 -0400 | [diff] [blame] | 855 | REPORTER_ASSERT(reporter, !strcmp(fp->name(), clone->name()), |
| 856 | "%s\n", describe_fp(*fp).c_str()); |
Brian Salomon | ce06e26 | 2017-08-01 16:23:40 -0400 | [diff] [blame] | 857 | REPORTER_ASSERT(reporter, fp->compatibleWithCoverageAsAlpha() == |
John Stiles | 8c71f3e | 2020-06-15 15:07:19 -0400 | [diff] [blame] | 858 | clone->compatibleWithCoverageAsAlpha(), |
| 859 | "%s\n", describe_fp(*fp).c_str()); |
| 860 | REPORTER_ASSERT(reporter, fp->isEqual(*clone), |
| 861 | "%s\n", describe_fp(*fp).c_str()); |
| 862 | REPORTER_ASSERT(reporter, fp->preservesOpaqueInput() == clone->preservesOpaqueInput(), |
| 863 | "%s\n", describe_fp(*fp).c_str()); |
Brian Salomon | ce06e26 | 2017-08-01 16:23:40 -0400 | [diff] [blame] | 864 | REPORTER_ASSERT(reporter, fp->hasConstantOutputForConstantInput() == |
John Stiles | 8c71f3e | 2020-06-15 15:07:19 -0400 | [diff] [blame] | 865 | clone->hasConstantOutputForConstantInput(), |
| 866 | "%s\n", describe_fp(*fp).c_str()); |
| 867 | REPORTER_ASSERT(reporter, fp->numChildProcessors() == clone->numChildProcessors(), |
| 868 | "%s\n", describe_fp(*fp).c_str()); |
Brian Osman | 9cf98dc | 2020-07-01 17:21:27 -0400 | [diff] [blame] | 869 | REPORTER_ASSERT(reporter, fp->usesVaryingCoords() == clone->usesVaryingCoords(), |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 870 | "%s\n", describe_fp(*fp).c_str()); |
| 871 | REPORTER_ASSERT(reporter, fp->referencesSampleCoords() == |
| 872 | clone->referencesSampleCoords(), |
John Stiles | 8c71f3e | 2020-06-15 15:07:19 -0400 | [diff] [blame] | 873 | "%s\n", describe_fp(*fp).c_str()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 874 | // Draw with original and read back the results. |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 875 | render_fp(context, rtc.get(), std::move(fp), readData1.data()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 876 | |
| 877 | // Draw with clone and read back the results. |
John Stiles | 0dee9b0 | 2020-07-20 11:45:58 -0400 | [diff] [blame] | 878 | render_fp(context, rtc.get(), std::move(clone), readData2.data()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 879 | |
| 880 | // Check that the results are the same. |
| 881 | bool passing = true; |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 882 | int failedPixelCount = 0; |
| 883 | int firstWrongX = 0; |
| 884 | int firstWrongY = 0; |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 885 | for (int y = 0; y < kRenderSize && passing; ++y) { |
| 886 | for (int x = 0; x < kRenderSize && passing; ++x) { |
| 887 | int idx = y * kRenderSize + x; |
| 888 | if (readData1[idx] != readData2[idx]) { |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 889 | if (!failedPixelCount) { |
| 890 | firstWrongX = x; |
| 891 | firstWrongY = y; |
| 892 | } |
| 893 | ++failedPixelCount; |
| 894 | } |
| 895 | if (failedPixelCount > kMaxAcceptableFailedPixels) { |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 896 | passing = false; |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 897 | idx = firstWrongY * kRenderSize + firstWrongX; |
| 898 | ERRORF(reporter, |
| 899 | "Processor %s made clone produced different output at (%d, %d). " |
| 900 | "Input color: 0x%08x, Original Output Color: 0x%08x, " |
| 901 | "Clone Output Color: 0x%08x.", |
| 902 | name, firstWrongX, firstWrongY, input_texel_color(x, y, 0.0f), |
| 903 | readData1[idx], readData2[idx]); |
| 904 | if (!loggedFirstFailure) { |
| 905 | // Write the images out as data urls for inspection. |
| 906 | // We mark the data as unpremul to avoid conversion when encoding as |
| 907 | // PNG. Also, even though we made the data by rendering into |
| 908 | // a "unpremul" GrRenderTargetContext, our input texture is unpremul and |
| 909 | // outside of the random effect configuration, we didn't do anything to |
| 910 | // ensure the output is actually premul. |
| 911 | auto info = SkImageInfo::Make(kRenderSize, kRenderSize, |
| 912 | kRGBA_8888_SkColorType, |
| 913 | kUnpremul_SkAlphaType); |
Brian Salomon | 3f4de78 | 2020-06-18 14:16:00 -0400 | [diff] [blame] | 914 | SkString inputURL, origURL, cloneURL; |
| 915 | if (log_texture_view(context, inputTexture, &inputURL) && |
John Stiles | af11030 | 2020-07-20 09:45:51 -0400 | [diff] [blame] | 916 | log_pixels(readData1.data(), kRenderSize, &origURL) && |
| 917 | log_pixels(readData2.data(), kRenderSize, &cloneURL)) { |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 918 | ERRORF(reporter, |
| 919 | "\nInput image:\n%s\n\n" |
| 920 | "===========================================================" |
| 921 | "\n\n" |
| 922 | "Orig output image:\n%s\n" |
| 923 | "===========================================================" |
| 924 | "\n\n" |
| 925 | "Clone output image:\n%s\n", |
Brian Salomon | 3f4de78 | 2020-06-18 14:16:00 -0400 | [diff] [blame] | 926 | inputURL.c_str(), origURL.c_str(), cloneURL.c_str()); |
Brian Salomon | cd8b6d5 | 2019-08-13 12:40:04 -0400 | [diff] [blame] | 927 | loggedFirstFailure = true; |
| 928 | } |
| 929 | } |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 930 | } |
| 931 | } |
| 932 | } |
| 933 | } |
| 934 | } |
| 935 | } |
| 936 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 937 | #endif // GR_TEST_UTILS |