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