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