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