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