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