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 | |
| 8 | #include "SkTypes.h" |
| 9 | #include "Test.h" |
| 10 | |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 11 | #include <random> |
Brian Salomon | c65aec9 | 2017-03-09 09:03:58 -0500 | [diff] [blame] | 12 | #include "GrClip.h" |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 13 | #include "GrContext.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 14 | #include "GrContextPriv.h" |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 15 | #include "GrGpuResource.h" |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 16 | #include "GrMemoryPool.h" |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 17 | #include "GrProxyProvider.h" |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 18 | #include "GrRenderTargetContext.h" |
| 19 | #include "GrRenderTargetContextPriv.h" |
| 20 | #include "GrResourceProvider.h" |
| 21 | #include "glsl/GrGLSLFragmentProcessor.h" |
| 22 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 23 | #include "ops/GrMeshDrawOp.h" |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 24 | #include "ops/GrRectOpFactory.h" |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 25 | |
| 26 | namespace { |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 27 | class TestOp : public GrMeshDrawOp { |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 28 | public: |
| 29 | DEFINE_OP_CLASS_ID |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 30 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, |
| 31 | std::unique_ptr<GrFragmentProcessor> fp) { |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 32 | return std::unique_ptr<GrDrawOp>(new TestOp(std::move(fp))); |
| 33 | } |
| 34 | |
Robert Phillips | 5f567c7 | 2017-09-14 08:27:37 -0400 | [diff] [blame] | 35 | const char* name() const override { return "TestOp"; } |
| 36 | |
Robert Phillips | f1748f5 | 2017-09-14 14:11:24 -0400 | [diff] [blame] | 37 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | 5f567c7 | 2017-09-14 08:27:37 -0400 | [diff] [blame] | 38 | fProcessors.visitProxies(func); |
| 39 | } |
| 40 | |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 41 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
| 42 | |
Brian Osman | 9a725dd | 2017-09-20 09:53:22 -0400 | [diff] [blame] | 43 | RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip, |
| 44 | GrPixelConfigIsClamped dstIsClamped) override { |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 45 | static constexpr GrProcessorAnalysisColor kUnknownColor; |
| 46 | GrColor overrideColor; |
| 47 | fProcessors.finalize(kUnknownColor, GrProcessorAnalysisCoverage::kNone, clip, false, caps, |
Brian Osman | 9a725dd | 2017-09-20 09:53:22 -0400 | [diff] [blame] | 48 | dstIsClamped, &overrideColor); |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 49 | return RequiresDstTexture::kNo; |
Brian Salomon | 649a341 | 2017-03-09 13:50:43 -0500 | [diff] [blame] | 50 | } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 51 | |
| 52 | private: |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 53 | friend class ::GrOpMemoryPool; // for ctor |
| 54 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 55 | TestOp(std::unique_ptr<GrFragmentProcessor> fp) |
| 56 | : INHERITED(ClassID()), fProcessors(std::move(fp)) { |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 57 | this->setBounds(SkRect::MakeWH(100, 100), HasAABloat::kNo, IsZeroArea::kNo); |
| 58 | } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 59 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 60 | void onPrepareDraws(Target* target) override { return; } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 61 | |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame] | 62 | bool onCombineIfPossible(GrOp* op, const GrCaps& caps) override { return false; } |
| 63 | |
| 64 | GrProcessorSet fProcessors; |
| 65 | |
| 66 | typedef GrMeshDrawOp INHERITED; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | /** |
| 70 | * FP used to test ref/IO counts on owned GrGpuResources. Can also be a parent FP to test counts |
| 71 | * of resources owned by child FPs. |
| 72 | */ |
| 73 | class TestFP : public GrFragmentProcessor { |
| 74 | public: |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 75 | static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> child) { |
| 76 | return std::unique_ptr<GrFragmentProcessor>(new TestFP(std::move(child))); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 77 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 78 | static std::unique_ptr<GrFragmentProcessor> Make(const SkTArray<sk_sp<GrTextureProxy>>& proxies, |
Brian Salomon | 559f556 | 2017-11-15 14:28:33 -0500 | [diff] [blame] | 79 | const SkTArray<sk_sp<GrBuffer>>& buffers) { |
| 80 | return std::unique_ptr<GrFragmentProcessor>(new TestFP(proxies, buffers)); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | const char* name() const override { return "test"; } |
| 84 | |
| 85 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override { |
| 86 | // We don't really care about reusing these. |
| 87 | static int32_t gKey = 0; |
| 88 | b->add32(sk_atomic_inc(&gKey)); |
| 89 | } |
| 90 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 91 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 92 | return std::unique_ptr<GrFragmentProcessor>(new TestFP(*this)); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 93 | } |
| 94 | |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 95 | private: |
Brian Salomon | 559f556 | 2017-11-15 14:28:33 -0500 | [diff] [blame] | 96 | TestFP(const SkTArray<sk_sp<GrTextureProxy>>& proxies, const SkTArray<sk_sp<GrBuffer>>& buffers) |
| 97 | : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4), fBuffers(4) { |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 98 | for (const auto& proxy : proxies) { |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 99 | this->addTextureSampler(&fSamplers.emplace_back(proxy)); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 100 | } |
| 101 | for (const auto& buffer : buffers) { |
| 102 | this->addBufferAccess(&fBuffers.emplace_back(kRGBA_8888_GrPixelConfig, buffer.get())); |
| 103 | } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 104 | } |
| 105 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 106 | TestFP(std::unique_ptr<GrFragmentProcessor> child) |
Brian Salomon | 559f556 | 2017-11-15 14:28:33 -0500 | [diff] [blame] | 107 | : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4), fBuffers(4) { |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 108 | this->registerChildProcessor(std::move(child)); |
| 109 | } |
| 110 | |
Brian Salomon | 96271cd | 2017-07-31 16:27:23 -0400 | [diff] [blame] | 111 | explicit TestFP(const TestFP& that) |
Brian Salomon | 559f556 | 2017-11-15 14:28:33 -0500 | [diff] [blame] | 112 | : INHERITED(kTestFP_ClassID, that.optimizationFlags()), fSamplers(4), fBuffers(4) { |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 113 | for (int i = 0; i < that.fSamplers.count(); ++i) { |
| 114 | fSamplers.emplace_back(that.fSamplers[i]); |
| 115 | this->addTextureSampler(&fSamplers.back()); |
| 116 | } |
| 117 | for (int i = 0; i < that.fBuffers.count(); ++i) { |
| 118 | fBuffers.emplace_back(that.fBuffers[i]); |
| 119 | this->addBufferAccess(&fBuffers.back()); |
| 120 | } |
Brian Salomon | 96271cd | 2017-07-31 16:27:23 -0400 | [diff] [blame] | 121 | for (int i = 0; i < that.numChildProcessors(); ++i) { |
| 122 | this->registerChildProcessor(that.childProcessor(i).clone()); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 123 | } |
| 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 | |
| 142 | GrTAllocator<TextureSampler> fSamplers; |
| 143 | GrTAllocator<BufferAccess> fBuffers; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 144 | typedef GrFragmentProcessor INHERITED; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 145 | }; |
| 146 | } |
| 147 | |
Brian Salomon | f046e15 | 2017-01-11 15:24:47 -0500 | [diff] [blame] | 148 | template <typename T> |
| 149 | inline void testingOnly_getIORefCnts(const T* resource, int* refCnt, int* readCnt, int* writeCnt) { |
| 150 | *refCnt = resource->fRefCnt; |
| 151 | *readCnt = resource->fPendingReads; |
| 152 | *writeCnt = resource->fPendingWrites; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 153 | } |
| 154 | |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 155 | void testingOnly_getIORefCnts(GrTextureProxy* proxy, int* refCnt, int* readCnt, int* writeCnt) { |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 156 | *refCnt = proxy->getBackingRefCnt_TestOnly(); |
| 157 | *readCnt = proxy->getPendingReadCnt_TestOnly(); |
| 158 | *writeCnt = proxy->getPendingWriteCnt_TestOnly(); |
| 159 | } |
| 160 | |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 161 | DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) { |
| 162 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 163 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 164 | GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider(); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 165 | |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 166 | GrSurfaceDesc desc; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 167 | desc.fWidth = 10; |
| 168 | desc.fHeight = 10; |
Robert Phillips | 16d8ec6 | 2017-07-27 16:16:25 -0400 | [diff] [blame] | 169 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 170 | |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 171 | for (bool makeClone : {false, true}) { |
| 172 | for (int parentCnt = 0; parentCnt < 2; parentCnt++) { |
| 173 | sk_sp<GrRenderTargetContext> renderTargetContext( |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 174 | context->contextPriv().makeDeferredRenderTargetContext( |
| 175 | SkBackingFit::kApprox, 1, 1, |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 176 | kRGBA_8888_GrPixelConfig, nullptr)); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 177 | { |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 178 | bool texelBufferSupport = |
| 179 | context->contextPriv().caps()->shaderCaps()->texelBufferSupport(); |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 180 | sk_sp<GrTextureProxy> proxy1 = proxyProvider->createProxy( |
| 181 | desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes); |
| 182 | sk_sp<GrTextureProxy> proxy2 = proxyProvider->createProxy( |
| 183 | desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes); |
| 184 | sk_sp<GrTextureProxy> proxy3 = proxyProvider->createProxy( |
| 185 | desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes); |
| 186 | sk_sp<GrTextureProxy> proxy4 = proxyProvider->createProxy( |
| 187 | desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 188 | sk_sp<GrBuffer> buffer(texelBufferSupport |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 189 | ? resourceProvider->createBuffer( |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 190 | 1024, GrBufferType::kTexel_GrBufferType, |
| 191 | GrAccessPattern::kStatic_GrAccessPattern, 0) |
| 192 | : nullptr); |
| 193 | { |
| 194 | SkTArray<sk_sp<GrTextureProxy>> proxies; |
| 195 | SkTArray<sk_sp<GrBuffer>> buffers; |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 196 | proxies.push_back(proxy1); |
| 197 | if (texelBufferSupport) { |
| 198 | buffers.push_back(buffer); |
| 199 | } |
Brian Salomon | 559f556 | 2017-11-15 14:28:33 -0500 | [diff] [blame] | 200 | auto fp = TestFP::Make(std::move(proxies), std::move(buffers)); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 201 | for (int i = 0; i < parentCnt; ++i) { |
| 202 | fp = TestFP::Make(std::move(fp)); |
| 203 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 204 | std::unique_ptr<GrFragmentProcessor> clone; |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 205 | if (makeClone) { |
| 206 | clone = fp->clone(); |
| 207 | } |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 208 | std::unique_ptr<GrDrawOp> op(TestOp::Make(context, std::move(fp))); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 209 | renderTargetContext->priv().testingOnly_addDrawOp(std::move(op)); |
| 210 | if (clone) { |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 211 | op = TestOp::Make(context, std::move(clone)); |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 212 | renderTargetContext->priv().testingOnly_addDrawOp(std::move(op)); |
| 213 | } |
| 214 | } |
| 215 | int refCnt, readCnt, writeCnt; |
| 216 | |
| 217 | testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt); |
| 218 | // IO counts should be double if there is a clone of the FP. |
| 219 | int ioRefMul = makeClone ? 2 : 1; |
| 220 | REPORTER_ASSERT(reporter, 1 == refCnt); |
| 221 | REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt); |
| 222 | REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt); |
| 223 | |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 224 | if (texelBufferSupport) { |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 225 | testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt); |
| 226 | REPORTER_ASSERT(reporter, 1 == refCnt); |
| 227 | REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt); |
| 228 | REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 229 | } |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 230 | |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 231 | context->flush(); |
| 232 | |
| 233 | testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt); |
| 234 | REPORTER_ASSERT(reporter, 1 == refCnt); |
| 235 | REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt); |
| 236 | REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt); |
| 237 | |
| 238 | if (texelBufferSupport) { |
| 239 | testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt); |
| 240 | REPORTER_ASSERT(reporter, 1 == refCnt); |
| 241 | REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt); |
| 242 | REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 243 | } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 244 | |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 245 | if (texelBufferSupport) { |
| 246 | testingOnly_getIORefCnts(proxy2.get(), &refCnt, &readCnt, &writeCnt); |
| 247 | REPORTER_ASSERT(reporter, 1 == refCnt); |
| 248 | REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt); |
| 249 | REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 250 | |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 251 | testingOnly_getIORefCnts(proxy3.get(), &refCnt, &readCnt, &writeCnt); |
| 252 | REPORTER_ASSERT(reporter, 1 == refCnt); |
| 253 | REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt); |
| 254 | REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 255 | |
Brian Salomon | b17e639 | 2017-07-28 13:41:51 -0400 | [diff] [blame] | 256 | testingOnly_getIORefCnts(proxy4.get(), &refCnt, &readCnt, &writeCnt); |
| 257 | REPORTER_ASSERT(reporter, 1 == refCnt); |
| 258 | REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt); |
| 259 | REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt); |
| 260 | } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | } |
| 264 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 265 | |
| 266 | // This test uses the random GrFragmentProcessor test factory, which relies on static initializers. |
| 267 | #if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 268 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 269 | #include "SkCommandLineFlags.h" |
| 270 | DEFINE_bool(randomProcessorTest, false, "Use non-deterministic seed for random processor tests?"); |
| 271 | |
| 272 | #if GR_TEST_UTILS |
| 273 | |
| 274 | static GrColor input_texel_color(int i, int j) { |
| 275 | GrColor color = GrColorPackRGBA((uint8_t)j, (uint8_t)(i + j), (uint8_t)(2 * j - i), (uint8_t)i); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 276 | return GrPremulColor(color); |
| 277 | } |
| 278 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 279 | static GrColor4f input_texel_color4f(int i, int j) { |
| 280 | return GrColor4f::FromGrColor(input_texel_color(i, j)); |
| 281 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 282 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 283 | void test_draw_op(GrContext* context, |
| 284 | GrRenderTargetContext* rtc, |
| 285 | std::unique_ptr<GrFragmentProcessor> fp, |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 286 | sk_sp<GrTextureProxy> inputDataProxy) { |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 287 | GrPaint paint; |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 288 | paint.addColorTextureProcessor(std::move(inputDataProxy), SkMatrix::I()); |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 289 | paint.addColorFragmentProcessor(std::move(fp)); |
| 290 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 291 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 292 | auto op = GrRectOpFactory::MakeNonAAFill(context, std::move(paint), SkMatrix::I(), |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 293 | SkRect::MakeWH(rtc->width(), rtc->height()), |
| 294 | GrAAType::kNone); |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 295 | rtc->addDrawOp(GrNoClip(), std::move(op)); |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 296 | } |
| 297 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 298 | /** Initializes the two test texture proxies that are available to the FP test factories. */ |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 299 | bool init_test_textures(GrProxyProvider* proxyProvider, SkRandom* random, |
| 300 | sk_sp<GrTextureProxy> proxies[2]) { |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 301 | static const int kTestTextureSize = 256; |
| 302 | GrSurfaceDesc desc; |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 303 | desc.fWidth = kTestTextureSize; |
| 304 | desc.fHeight = kTestTextureSize; |
| 305 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 306 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 307 | { |
| 308 | // Put premul data into the RGBA texture that the test FPs can optionally use. |
| 309 | std::unique_ptr<GrColor[]> rgbaData(new GrColor[kTestTextureSize * kTestTextureSize]); |
| 310 | for (int y = 0; y < kTestTextureSize; ++y) { |
| 311 | for (int x = 0; x < kTestTextureSize; ++x) { |
| 312 | rgbaData[kTestTextureSize * y + x] = |
| 313 | input_texel_color(random->nextULessThan(256), random->nextULessThan(256)); |
| 314 | } |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 315 | } |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 316 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 317 | proxies[0] = proxyProvider->createTextureProxy(desc, SkBudgeted::kYes, rgbaData.get(), |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 318 | kTestTextureSize * sizeof(GrColor)); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 319 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 320 | |
| 321 | { |
| 322 | // Put random values into the alpha texture that the test FPs can optionally use. |
| 323 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 324 | std::unique_ptr<uint8_t[]> alphaData(new uint8_t[kTestTextureSize * kTestTextureSize]); |
| 325 | for (int y = 0; y < kTestTextureSize; ++y) { |
| 326 | for (int x = 0; x < kTestTextureSize; ++x) { |
| 327 | alphaData[kTestTextureSize * y + x] = random->nextULessThan(256); |
| 328 | } |
| 329 | } |
| 330 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 331 | proxies[1] = proxyProvider->createTextureProxy(desc, SkBudgeted::kYes, alphaData.get(), |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 332 | kTestTextureSize); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 333 | } |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 334 | |
| 335 | return proxies[0] && proxies[1]; |
| 336 | } |
| 337 | |
| 338 | // Creates a texture of premul colors used as the output of the fragment processor that precedes |
| 339 | // the fragment processor under test. Color values are those provided by input_texel_color(). |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 340 | sk_sp<GrTextureProxy> make_input_texture(GrProxyProvider* proxyProvider, int width, int height) { |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 341 | std::unique_ptr<GrColor[]> data(new GrColor[width * height]); |
| 342 | for (int y = 0; y < width; ++y) { |
| 343 | for (int x = 0; x < height; ++x) { |
| 344 | data.get()[width * y + x] = input_texel_color(x, y); |
| 345 | } |
| 346 | } |
| 347 | GrSurfaceDesc desc; |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 348 | desc.fWidth = width; |
| 349 | desc.fHeight = height; |
| 350 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 351 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 352 | return proxyProvider->createTextureProxy(desc, SkBudgeted::kYes, data.get(), |
| 353 | width * sizeof(GrColor)); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 354 | } |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 355 | |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 356 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 357 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 358 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 359 | auto resourceProvider = context->contextPriv().resourceProvider(); |
Brian Salomon | 1c05364 | 2017-07-24 10:16:19 -0400 | [diff] [blame] | 360 | using FPFactory = GrFragmentProcessorTestFactory; |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 361 | |
| 362 | uint32_t seed = 0; |
| 363 | if (FLAGS_randomProcessorTest) { |
| 364 | std::random_device rd; |
| 365 | seed = rd(); |
| 366 | } |
| 367 | // If a non-deterministic bot fails this test, check the output to see what seed it used, then |
| 368 | // hard-code that value here: |
| 369 | SkRandom random(seed); |
| 370 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 371 | // Make the destination context for the test. |
| 372 | static constexpr int kRenderSize = 256; |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 373 | sk_sp<GrRenderTargetContext> rtc = context->contextPriv().makeDeferredRenderTargetContext( |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 374 | SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr); |
Brian Salomon | d1a8bdf | 2017-02-10 12:39:26 -0500 | [diff] [blame] | 375 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 376 | sk_sp<GrTextureProxy> proxies[2]; |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 377 | if (!init_test_textures(proxyProvider, &random, proxies)) { |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 378 | ERRORF(reporter, "Could not create test textures"); |
Robert Phillips | bb581ce | 2017-05-29 15:05:15 -0400 | [diff] [blame] | 379 | return; |
| 380 | } |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 381 | GrProcessorTestData testData(&random, context, rtc.get(), proxies); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 382 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 383 | auto inputTexture = make_input_texture(proxyProvider, kRenderSize, kRenderSize); |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 384 | |
Ben Wagner | 8c79194 | 2017-07-25 11:47:48 -0400 | [diff] [blame] | 385 | std::unique_ptr<GrColor[]> readData(new GrColor[kRenderSize * kRenderSize]); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 386 | // Because processor factories configure themselves in random ways, this is not exhaustive. |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 387 | for (int i = 0; i < FPFactory::Count(); ++i) { |
| 388 | int timesToInvokeFactory = 5; |
| 389 | // Increase the number of attempts if the FP has child FPs since optimizations likely depend |
| 390 | // on child optimizations being present. |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 391 | std::unique_ptr<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 392 | for (int j = 0; j < fp->numChildProcessors(); ++j) { |
| 393 | // This value made a reasonable trade off between time and coverage when this test was |
| 394 | // written. |
| 395 | timesToInvokeFactory *= FPFactory::Count() / 2; |
| 396 | } |
| 397 | for (int j = 0; j < timesToInvokeFactory; ++j) { |
| 398 | fp = FPFactory::MakeIdx(i, &testData); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 399 | if (!fp->instantiate(resourceProvider)) { |
Robert Phillips | a91e0b7 | 2017-05-01 13:12:20 -0400 | [diff] [blame] | 400 | continue; |
| 401 | } |
| 402 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 403 | if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() && |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 404 | !fp->compatibleWithCoverageAsAlpha()) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 405 | continue; |
| 406 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 407 | |
| 408 | // Since we transfer away ownership of the original FP, we make a clone. |
| 409 | auto clone = fp->clone(); |
| 410 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 411 | test_draw_op(context, rtc.get(), std::move(fp), inputTexture); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 412 | memset(readData.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize); |
| 413 | rtc->readPixels(SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType, |
| 414 | kPremul_SkAlphaType), |
| 415 | readData.get(), 0, 0, 0); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 416 | bool passing = true; |
| 417 | if (0) { // Useful to see what FPs are being tested. |
| 418 | SkString children; |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 419 | for (int c = 0; c < clone->numChildProcessors(); ++c) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 420 | if (!c) { |
| 421 | children.append("("); |
| 422 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 423 | children.append(clone->name()); |
| 424 | children.append(c == clone->numChildProcessors() - 1 ? ")" : ", "); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 425 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 426 | SkDebugf("%s %s\n", clone->name(), children.c_str()); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 427 | } |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 428 | for (int y = 0; y < kRenderSize && passing; ++y) { |
| 429 | for (int x = 0; x < kRenderSize && passing; ++x) { |
| 430 | GrColor input = input_texel_color(x, y); |
| 431 | GrColor output = readData.get()[y * kRenderSize + x]; |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 432 | if (clone->compatibleWithCoverageAsAlpha()) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 433 | // A modulating processor is allowed to modulate either the input color or |
| 434 | // just the input alpha. |
| 435 | bool legalColorModulation = |
| 436 | GrColorUnpackA(output) <= GrColorUnpackA(input) && |
| 437 | GrColorUnpackR(output) <= GrColorUnpackR(input) && |
| 438 | GrColorUnpackG(output) <= GrColorUnpackG(input) && |
| 439 | GrColorUnpackB(output) <= GrColorUnpackB(input); |
| 440 | bool legalAlphaModulation = |
| 441 | GrColorUnpackA(output) <= GrColorUnpackA(input) && |
| 442 | GrColorUnpackR(output) <= GrColorUnpackA(input) && |
| 443 | GrColorUnpackG(output) <= GrColorUnpackA(input) && |
| 444 | GrColorUnpackB(output) <= GrColorUnpackA(input); |
| 445 | if (!legalColorModulation && !legalAlphaModulation) { |
| 446 | ERRORF(reporter, |
| 447 | "\"Modulating\" processor %s made color/alpha value larger. " |
Greg Daniel | 10ed243 | 2017-12-01 16:19:43 -0500 | [diff] [blame] | 448 | "Input: 0x%08x, Output: 0x%08x, pixel (%d, %d).", |
| 449 | clone->name(), input, output, x, y); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 450 | passing = false; |
| 451 | } |
| 452 | } |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 453 | GrColor4f input4f = input_texel_color4f(x, y); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 454 | GrColor4f output4f = GrColor4f::FromGrColor(output); |
| 455 | GrColor4f expected4f; |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 456 | if (clone->hasConstantOutputForConstantInput(input4f, &expected4f)) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 457 | float rDiff = fabsf(output4f.fRGBA[0] - expected4f.fRGBA[0]); |
| 458 | float gDiff = fabsf(output4f.fRGBA[1] - expected4f.fRGBA[1]); |
| 459 | float bDiff = fabsf(output4f.fRGBA[2] - expected4f.fRGBA[2]); |
| 460 | float aDiff = fabsf(output4f.fRGBA[3] - expected4f.fRGBA[3]); |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 461 | static constexpr float kTol = 4 / 255.f; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 462 | if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) { |
| 463 | ERRORF(reporter, |
| 464 | "Processor %s claimed output for const input doesn't match " |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 465 | "actual output. Error: %f, Tolerance: %f, input: (%f, %f, %f, " |
| 466 | "%f), actual: (%f, %f, %f, %f), expected(%f, %f, %f, %f)", |
Brian Salomon | f61711a | 2018-02-16 10:44:28 -0500 | [diff] [blame] | 467 | clone->name(), |
| 468 | SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))), kTol, |
| 469 | input4f.fRGBA[0], input4f.fRGBA[1], input4f.fRGBA[2], |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 470 | input4f.fRGBA[3], output4f.fRGBA[0], output4f.fRGBA[1], |
| 471 | output4f.fRGBA[2], output4f.fRGBA[3], expected4f.fRGBA[0], |
| 472 | expected4f.fRGBA[1], expected4f.fRGBA[2], expected4f.fRGBA[3]); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 473 | passing = false; |
| 474 | } |
| 475 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 476 | if (GrColorIsOpaque(input) && clone->preservesOpaqueInput() && |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 477 | !GrColorIsOpaque(output)) { |
| 478 | ERRORF(reporter, |
| 479 | "Processor %s claimed opaqueness is preserved but it is not. Input: " |
Brian Osman | 2f3865b | 2017-03-15 13:35:51 -0400 | [diff] [blame] | 480 | "0x%08x, Output: 0x%08x.", |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 481 | clone->name(), input, output); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 482 | passing = false; |
| 483 | } |
Brian Osman | bd1f76f | 2017-03-15 11:33:12 -0400 | [diff] [blame] | 484 | if (!passing) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 485 | ERRORF(reporter, "Seed: 0x%08x, Processor details: %s", seed, |
| 486 | clone->dumpInfo().c_str()); |
Brian Osman | bd1f76f | 2017-03-15 11:33:12 -0400 | [diff] [blame] | 487 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | } |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 493 | |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 494 | // Tests that fragment processors returned by GrFragmentProcessor::clone() are equivalent to their |
| 495 | // progenitors. |
| 496 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) { |
| 497 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 498 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 499 | auto resourceProvider = context->contextPriv().resourceProvider(); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 500 | |
| 501 | SkRandom random; |
| 502 | |
| 503 | // Make the destination context for the test. |
| 504 | static constexpr int kRenderSize = 1024; |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 505 | sk_sp<GrRenderTargetContext> rtc = context->contextPriv().makeDeferredRenderTargetContext( |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 506 | SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr); |
| 507 | |
| 508 | sk_sp<GrTextureProxy> proxies[2]; |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 509 | if (!init_test_textures(proxyProvider, &random, proxies)) { |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 510 | ERRORF(reporter, "Could not create test textures"); |
| 511 | return; |
| 512 | } |
| 513 | GrProcessorTestData testData(&random, context, rtc.get(), proxies); |
| 514 | |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 515 | auto inputTexture = make_input_texture(proxyProvider, kRenderSize, kRenderSize); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 516 | std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]); |
| 517 | std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]); |
| 518 | auto readInfo = SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType, |
| 519 | kPremul_SkAlphaType); |
| 520 | |
| 521 | // Because processor factories configure themselves in random ways, this is not exhaustive. |
| 522 | for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) { |
| 523 | static constexpr int kTimesToInvokeFactory = 10; |
| 524 | for (int j = 0; j < kTimesToInvokeFactory; ++j) { |
| 525 | auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &testData); |
| 526 | auto clone = fp->clone(); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 527 | if (!clone) { |
Brian Salomon | 96271cd | 2017-07-31 16:27:23 -0400 | [diff] [blame] | 528 | ERRORF(reporter, "Clone of processor %s failed.", fp->name()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 529 | continue; |
| 530 | } |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 531 | const char* name = fp->name(); |
Robert Phillips | 6be756b | 2018-01-16 15:07:54 -0500 | [diff] [blame] | 532 | if (!fp->instantiate(resourceProvider) || !clone->instantiate(resourceProvider)) { |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 533 | continue; |
| 534 | } |
Brian Salomon | ce06e26 | 2017-08-01 16:23:40 -0400 | [diff] [blame] | 535 | REPORTER_ASSERT(reporter, !strcmp(fp->name(), clone->name())); |
| 536 | REPORTER_ASSERT(reporter, fp->compatibleWithCoverageAsAlpha() == |
| 537 | clone->compatibleWithCoverageAsAlpha()); |
| 538 | REPORTER_ASSERT(reporter, fp->isEqual(*clone)); |
| 539 | REPORTER_ASSERT(reporter, fp->preservesOpaqueInput() == clone->preservesOpaqueInput()); |
| 540 | REPORTER_ASSERT(reporter, fp->hasConstantOutputForConstantInput() == |
| 541 | clone->hasConstantOutputForConstantInput()); |
| 542 | REPORTER_ASSERT(reporter, fp->numChildProcessors() == clone->numChildProcessors()); |
| 543 | REPORTER_ASSERT(reporter, fp->usesLocalCoords() == clone->usesLocalCoords()); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 544 | // Draw with original and read back the results. |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 545 | test_draw_op(context, rtc.get(), std::move(fp), inputTexture); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 546 | memset(readData1.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize); |
| 547 | rtc->readPixels(readInfo, readData1.get(), 0, 0, 0); |
| 548 | |
| 549 | // Draw with clone and read back the results. |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 550 | test_draw_op(context, rtc.get(), std::move(clone), inputTexture); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 551 | memset(readData2.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize); |
| 552 | rtc->readPixels(readInfo, readData2.get(), 0, 0, 0); |
| 553 | |
| 554 | // Check that the results are the same. |
| 555 | bool passing = true; |
| 556 | for (int y = 0; y < kRenderSize && passing; ++y) { |
| 557 | for (int x = 0; x < kRenderSize && passing; ++x) { |
| 558 | int idx = y * kRenderSize + x; |
| 559 | if (readData1[idx] != readData2[idx]) { |
| 560 | ERRORF(reporter, |
| 561 | "Processor %s made clone produced different output. " |
| 562 | "Input color: 0x%08x, Original Output Color: 0x%08x, " |
| 563 | "Clone Output Color: 0x%08x..", |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 564 | name, input_texel_color(x, y), readData1[idx], readData2[idx]); |
Brian Salomon | 0e05a82 | 2017-07-25 09:43:22 -0400 | [diff] [blame] | 565 | passing = false; |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 573 | #endif // GR_TEST_UTILS |
| 574 | #endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |