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" |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 16 | #include "GrPipelineBuilder.h" |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 17 | #include "GrRenderTargetContext.h" |
| 18 | #include "GrRenderTargetContextPriv.h" |
| 19 | #include "GrResourceProvider.h" |
| 20 | #include "glsl/GrGLSLFragmentProcessor.h" |
| 21 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame^] | 22 | #include "ops/GrMeshDrawOp.h" |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 23 | #include "ops/GrRectOpFactory.h" |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 24 | |
| 25 | namespace { |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame^] | 26 | class TestOp : public GrMeshDrawOp { |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 27 | public: |
| 28 | DEFINE_OP_CLASS_ID |
| 29 | const char* name() const override { return "TestOp"; } |
| 30 | |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame^] | 31 | static std::unique_ptr<GrDrawOp> Make(sk_sp<GrFragmentProcessor> fp) { |
| 32 | return std::unique_ptr<GrDrawOp>(new TestOp(std::move(fp))); |
| 33 | } |
| 34 | |
| 35 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
| 36 | |
| 37 | RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override { |
| 38 | static constexpr GrProcessorAnalysisColor kUnknownColor; |
| 39 | GrColor overrideColor; |
| 40 | fProcessors.finalize(kUnknownColor, GrProcessorAnalysisCoverage::kNone, clip, false, caps, |
| 41 | &overrideColor); |
| 42 | return RequiresDstTexture::kNo; |
Brian Salomon | 649a341 | 2017-03-09 13:50:43 -0500 | [diff] [blame] | 43 | } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 44 | |
| 45 | private: |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame^] | 46 | TestOp(sk_sp<GrFragmentProcessor> fp) : INHERITED(ClassID()), fProcessors(std::move(fp)) { |
| 47 | this->setBounds(SkRect::MakeWH(100, 100), HasAABloat::kNo, IsZeroArea::kNo); |
| 48 | } |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 49 | |
| 50 | void onPrepareDraws(Target* target) const override { return; } |
| 51 | |
Brian Salomon | 82ddc94 | 2017-07-14 12:00:13 -0400 | [diff] [blame^] | 52 | bool onCombineIfPossible(GrOp* op, const GrCaps& caps) override { return false; } |
| 53 | |
| 54 | GrProcessorSet fProcessors; |
| 55 | |
| 56 | typedef GrMeshDrawOp INHERITED; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | /** |
| 60 | * FP used to test ref/IO counts on owned GrGpuResources. Can also be a parent FP to test counts |
| 61 | * of resources owned by child FPs. |
| 62 | */ |
| 63 | class TestFP : public GrFragmentProcessor { |
| 64 | public: |
| 65 | struct Image { |
Robert Phillips | 8a02f65 | 2017-05-12 14:49:16 -0400 | [diff] [blame] | 66 | Image(sk_sp<GrTextureProxy> proxy, GrIOType ioType) : fProxy(proxy), fIOType(ioType) {} |
| 67 | sk_sp<GrTextureProxy> fProxy; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 68 | GrIOType fIOType; |
| 69 | }; |
| 70 | static sk_sp<GrFragmentProcessor> Make(sk_sp<GrFragmentProcessor> child) { |
| 71 | return sk_sp<GrFragmentProcessor>(new TestFP(std::move(child))); |
| 72 | } |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 73 | static sk_sp<GrFragmentProcessor> Make(const SkTArray<sk_sp<GrTextureProxy>>& proxies, |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 74 | const SkTArray<sk_sp<GrBuffer>>& buffers, |
| 75 | const SkTArray<Image>& images) { |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 76 | return sk_sp<GrFragmentProcessor>(new TestFP(proxies, buffers, images)); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | const char* name() const override { return "test"; } |
| 80 | |
| 81 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override { |
| 82 | // We don't really care about reusing these. |
| 83 | static int32_t gKey = 0; |
| 84 | b->add32(sk_atomic_inc(&gKey)); |
| 85 | } |
| 86 | |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 87 | private: |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 88 | TestFP(const SkTArray<sk_sp<GrTextureProxy>>& proxies, |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 89 | const SkTArray<sk_sp<GrBuffer>>& buffers, |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 90 | const SkTArray<Image>& images) |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 91 | : INHERITED(kNone_OptimizationFlags), fSamplers(4), fBuffers(4), fImages(4) { |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 92 | for (const auto& proxy : proxies) { |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 93 | this->addTextureSampler(&fSamplers.emplace_back(proxy)); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 94 | } |
| 95 | for (const auto& buffer : buffers) { |
| 96 | this->addBufferAccess(&fBuffers.emplace_back(kRGBA_8888_GrPixelConfig, buffer.get())); |
| 97 | } |
| 98 | for (const Image& image : images) { |
Robert Phillips | 8a02f65 | 2017-05-12 14:49:16 -0400 | [diff] [blame] | 99 | fImages.emplace_back(image.fProxy, image.fIOType, |
| 100 | GrSLMemoryModel::kNone, GrSLRestrict::kNo); |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 101 | this->addImageStorageAccess(&fImages.back()); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 105 | TestFP(sk_sp<GrFragmentProcessor> child) |
| 106 | : INHERITED(kNone_OptimizationFlags), fSamplers(4), fBuffers(4), fImages(4) { |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 107 | this->registerChildProcessor(std::move(child)); |
| 108 | } |
| 109 | |
| 110 | virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
| 111 | class TestGLSLFP : public GrGLSLFragmentProcessor { |
| 112 | public: |
| 113 | TestGLSLFP() {} |
| 114 | void emitCode(EmitArgs& args) override { |
| 115 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 116 | fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor); |
| 117 | } |
| 118 | |
| 119 | private: |
| 120 | }; |
| 121 | return new TestGLSLFP(); |
| 122 | } |
| 123 | |
| 124 | bool onIsEqual(const GrFragmentProcessor&) const override { return false; } |
| 125 | |
| 126 | GrTAllocator<TextureSampler> fSamplers; |
| 127 | GrTAllocator<BufferAccess> fBuffers; |
| 128 | GrTAllocator<ImageStorageAccess> fImages; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 129 | typedef GrFragmentProcessor INHERITED; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 130 | }; |
| 131 | } |
| 132 | |
Brian Salomon | f046e15 | 2017-01-11 15:24:47 -0500 | [diff] [blame] | 133 | template <typename T> |
| 134 | inline void testingOnly_getIORefCnts(const T* resource, int* refCnt, int* readCnt, int* writeCnt) { |
| 135 | *refCnt = resource->fRefCnt; |
| 136 | *readCnt = resource->fPendingReads; |
| 137 | *writeCnt = resource->fPendingWrites; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 138 | } |
| 139 | |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 140 | void testingOnly_getIORefCnts(GrTextureProxy* proxy, int* refCnt, int* readCnt, int* writeCnt) { |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 141 | *refCnt = proxy->getBackingRefCnt_TestOnly(); |
| 142 | *readCnt = proxy->getPendingReadCnt_TestOnly(); |
| 143 | *writeCnt = proxy->getPendingWriteCnt_TestOnly(); |
| 144 | } |
| 145 | |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 146 | DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) { |
| 147 | GrContext* context = ctxInfo.grContext(); |
| 148 | |
| 149 | GrTextureDesc desc; |
| 150 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 151 | desc.fWidth = 10; |
| 152 | desc.fHeight = 10; |
| 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 | |
| 265 | static GrColor texel_color(int i, int j) { |
| 266 | SkASSERT((unsigned)i < 256 && (unsigned)j < 256); |
| 267 | GrColor color = GrColorPackRGBA(j, (uint8_t)(i + j), (uint8_t)(2 * j - i), i); |
| 268 | return GrPremulColor(color); |
| 269 | } |
| 270 | |
| 271 | static GrColor4f texel_color4f(int i, int j) { return GrColor4f::FromGrColor(texel_color(i, j)); } |
| 272 | |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 273 | void test_draw_op(GrRenderTargetContext* rtc, sk_sp<GrFragmentProcessor> fp, |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 274 | sk_sp<GrTextureProxy> inputDataProxy) { |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 275 | GrPaint paint; |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 276 | paint.addColorTextureProcessor(std::move(inputDataProxy), nullptr, SkMatrix::I()); |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 277 | paint.addColorFragmentProcessor(std::move(fp)); |
| 278 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 279 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 280 | auto op = GrRectOpFactory::MakeNonAAFill(std::move(paint), SkMatrix::I(), |
| 281 | SkRect::MakeWH(rtc->width(), rtc->height()), |
| 282 | GrAAType::kNone); |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 283 | rtc->addDrawOp(GrNoClip(), std::move(op)); |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 284 | } |
| 285 | |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 286 | #include "SkCommandLineFlags.h" |
| 287 | DEFINE_bool(randomProcessorTest, false, "Use non-deterministic seed for random processor tests?"); |
| 288 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 289 | #if GR_TEST_UTILS |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 290 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 291 | GrContext* context = ctxInfo.grContext(); |
| 292 | using FPFactory = GrProcessorTestFactory<GrFragmentProcessor>; |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 293 | |
| 294 | uint32_t seed = 0; |
| 295 | if (FLAGS_randomProcessorTest) { |
| 296 | std::random_device rd; |
| 297 | seed = rd(); |
| 298 | } |
| 299 | // If a non-deterministic bot fails this test, check the output to see what seed it used, then |
| 300 | // hard-code that value here: |
| 301 | SkRandom random(seed); |
| 302 | |
Robert Phillips | dd3b3f4 | 2017-04-24 10:57:28 -0400 | [diff] [blame] | 303 | sk_sp<GrRenderTargetContext> rtc = context->makeDeferredRenderTargetContext( |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 304 | SkBackingFit::kExact, 256, 256, kRGBA_8888_GrPixelConfig, nullptr); |
| 305 | GrSurfaceDesc desc; |
| 306 | desc.fWidth = 256; |
| 307 | desc.fHeight = 256; |
| 308 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 309 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
Brian Salomon | d1a8bdf | 2017-02-10 12:39:26 -0500 | [diff] [blame] | 310 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 311 | sk_sp<GrTextureProxy> proxies[2]; |
| 312 | |
Brian Salomon | d1a8bdf | 2017-02-10 12:39:26 -0500 | [diff] [blame] | 313 | // Put premul data into the RGBA texture that the test FPs can optionally use. |
| 314 | std::unique_ptr<GrColor[]> rgbaData(new GrColor[256 * 256]); |
| 315 | for (int y = 0; y < 256; ++y) { |
| 316 | for (int x = 0; x < 256; ++x) { |
| 317 | rgbaData.get()[256 * y + x] = |
| 318 | texel_color(random.nextULessThan(256), random.nextULessThan(256)); |
| 319 | } |
| 320 | } |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 321 | proxies[0] = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes, |
| 322 | rgbaData.get(), 256 * sizeof(GrColor)); |
Brian Salomon | d1a8bdf | 2017-02-10 12:39:26 -0500 | [diff] [blame] | 323 | |
| 324 | // Put random values into the alpha texture that the test FPs can optionally use. |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 325 | desc.fConfig = kAlpha_8_GrPixelConfig; |
Brian Salomon | d1a8bdf | 2017-02-10 12:39:26 -0500 | [diff] [blame] | 326 | std::unique_ptr<uint8_t[]> alphaData(new uint8_t[256 * 256]); |
| 327 | for (int y = 0; y < 256; ++y) { |
| 328 | for (int x = 0; x < 256; ++x) { |
| 329 | alphaData.get()[256 * y + x] = random.nextULessThan(256); |
| 330 | } |
| 331 | } |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 332 | proxies[1] = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes, |
| 333 | alphaData.get(), 256); |
Robert Phillips | bb581ce | 2017-05-29 15:05:15 -0400 | [diff] [blame] | 334 | |
| 335 | if (!proxies[0] || !proxies[1]) { |
| 336 | return; |
| 337 | } |
| 338 | |
Robert Phillips | e78b725 | 2017-04-06 07:59:41 -0400 | [diff] [blame] | 339 | GrProcessorTestData testData(&random, context, rtc.get(), proxies); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 340 | |
Brian Salomon | d1a8bdf | 2017-02-10 12:39:26 -0500 | [diff] [blame] | 341 | // Use a different array of premul colors for the output of the fragment processor that preceeds |
| 342 | // the fragment processor under test. |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 343 | for (int y = 0; y < 256; ++y) { |
| 344 | for (int x = 0; x < 256; ++x) { |
Brian Salomon | d1a8bdf | 2017-02-10 12:39:26 -0500 | [diff] [blame] | 345 | rgbaData.get()[256 * y + x] = texel_color(x, y); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 349 | |
Robert Phillips | 26c90e0 | 2017-03-14 14:39:29 -0400 | [diff] [blame] | 350 | sk_sp<GrTextureProxy> dataProxy = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 351 | desc, SkBudgeted::kYes, |
| 352 | rgbaData.get(), |
| 353 | 256 * sizeof(GrColor)); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 354 | |
| 355 | // Because processors factories configure themselves in random ways, this is not exhaustive. |
| 356 | for (int i = 0; i < FPFactory::Count(); ++i) { |
| 357 | int timesToInvokeFactory = 5; |
| 358 | // Increase the number of attempts if the FP has child FPs since optimizations likely depend |
| 359 | // on child optimizations being present. |
| 360 | sk_sp<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData); |
| 361 | for (int j = 0; j < fp->numChildProcessors(); ++j) { |
| 362 | // This value made a reasonable trade off between time and coverage when this test was |
| 363 | // written. |
| 364 | timesToInvokeFactory *= FPFactory::Count() / 2; |
| 365 | } |
| 366 | for (int j = 0; j < timesToInvokeFactory; ++j) { |
| 367 | fp = FPFactory::MakeIdx(i, &testData); |
Robert Phillips | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 368 | if (!fp->instantiate(context->resourceProvider())) { |
Robert Phillips | a91e0b7 | 2017-05-01 13:12:20 -0400 | [diff] [blame] | 369 | continue; |
| 370 | } |
| 371 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 372 | if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() && |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 373 | !fp->compatibleWithCoverageAsAlpha()) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 374 | continue; |
| 375 | } |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 376 | test_draw_op(rtc.get(), fp, dataProxy); |
Brian Salomon | d1a8bdf | 2017-02-10 12:39:26 -0500 | [diff] [blame] | 377 | memset(rgbaData.get(), 0x0, sizeof(GrColor) * 256 * 256); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 378 | rtc->readPixels( |
| 379 | SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kPremul_SkAlphaType), |
Brian Salomon | d1a8bdf | 2017-02-10 12:39:26 -0500 | [diff] [blame] | 380 | rgbaData.get(), 0, 0, 0); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 381 | bool passing = true; |
| 382 | if (0) { // Useful to see what FPs are being tested. |
| 383 | SkString children; |
| 384 | for (int c = 0; c < fp->numChildProcessors(); ++c) { |
| 385 | if (!c) { |
| 386 | children.append("("); |
| 387 | } |
| 388 | children.append(fp->childProcessor(c).name()); |
| 389 | children.append(c == fp->numChildProcessors() - 1 ? ")" : ", "); |
| 390 | } |
| 391 | SkDebugf("%s %s\n", fp->name(), children.c_str()); |
| 392 | } |
| 393 | for (int y = 0; y < 256 && passing; ++y) { |
| 394 | for (int x = 0; x < 256 && passing; ++x) { |
| 395 | GrColor input = texel_color(x, y); |
Brian Salomon | d1a8bdf | 2017-02-10 12:39:26 -0500 | [diff] [blame] | 396 | GrColor output = rgbaData.get()[y * 256 + x]; |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 397 | if (fp->compatibleWithCoverageAsAlpha()) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 398 | // A modulating processor is allowed to modulate either the input color or |
| 399 | // just the input alpha. |
| 400 | bool legalColorModulation = |
| 401 | GrColorUnpackA(output) <= GrColorUnpackA(input) && |
| 402 | GrColorUnpackR(output) <= GrColorUnpackR(input) && |
| 403 | GrColorUnpackG(output) <= GrColorUnpackG(input) && |
| 404 | GrColorUnpackB(output) <= GrColorUnpackB(input); |
| 405 | bool legalAlphaModulation = |
| 406 | GrColorUnpackA(output) <= GrColorUnpackA(input) && |
| 407 | GrColorUnpackR(output) <= GrColorUnpackA(input) && |
| 408 | GrColorUnpackG(output) <= GrColorUnpackA(input) && |
| 409 | GrColorUnpackB(output) <= GrColorUnpackA(input); |
| 410 | if (!legalColorModulation && !legalAlphaModulation) { |
| 411 | ERRORF(reporter, |
| 412 | "\"Modulating\" processor %s made color/alpha value larger. " |
Brian Osman | 2f3865b | 2017-03-15 13:35:51 -0400 | [diff] [blame] | 413 | "Input: 0x%08x, Output: 0x%08x.", |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 414 | fp->name(), input, output); |
| 415 | passing = false; |
| 416 | } |
| 417 | } |
| 418 | GrColor4f input4f = texel_color4f(x, y); |
| 419 | GrColor4f output4f = GrColor4f::FromGrColor(output); |
| 420 | GrColor4f expected4f; |
| 421 | if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) { |
| 422 | float rDiff = fabsf(output4f.fRGBA[0] - expected4f.fRGBA[0]); |
| 423 | float gDiff = fabsf(output4f.fRGBA[1] - expected4f.fRGBA[1]); |
| 424 | float bDiff = fabsf(output4f.fRGBA[2] - expected4f.fRGBA[2]); |
| 425 | float aDiff = fabsf(output4f.fRGBA[3] - expected4f.fRGBA[3]); |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 426 | static constexpr float kTol = 4 / 255.f; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 427 | if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) { |
| 428 | ERRORF(reporter, |
| 429 | "Processor %s claimed output for const input doesn't match " |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 430 | "actual output. Error: %f, Tolerance: %f, input: (%f, %f, %f, " |
| 431 | "%f), actual: (%f, %f, %f, %f), expected(%f, %f, %f, %f)", |
| 432 | fp->name(), SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))), |
| 433 | kTol, input4f.fRGBA[0], input4f.fRGBA[1], input4f.fRGBA[2], |
| 434 | input4f.fRGBA[3], output4f.fRGBA[0], output4f.fRGBA[1], |
| 435 | output4f.fRGBA[2], output4f.fRGBA[3], expected4f.fRGBA[0], |
| 436 | expected4f.fRGBA[1], expected4f.fRGBA[2], expected4f.fRGBA[3]); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 437 | passing = false; |
| 438 | } |
| 439 | } |
| 440 | if (GrColorIsOpaque(input) && fp->preservesOpaqueInput() && |
| 441 | !GrColorIsOpaque(output)) { |
| 442 | ERRORF(reporter, |
| 443 | "Processor %s claimed opaqueness is preserved but it is not. Input: " |
Brian Osman | 2f3865b | 2017-03-15 13:35:51 -0400 | [diff] [blame] | 444 | "0x%08x, Output: 0x%08x.", |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 445 | fp->name(), input, output); |
| 446 | passing = false; |
| 447 | } |
Brian Osman | bd1f76f | 2017-03-15 11:33:12 -0400 | [diff] [blame] | 448 | if (!passing) { |
Brian Osman | c35a2d4 | 2017-03-17 10:58:53 -0400 | [diff] [blame] | 449 | ERRORF(reporter, "Seed: 0x%08x, Processor details: %s", |
| 450 | seed, fp->dumpInfo().c_str()); |
Brian Osman | bd1f76f | 2017-03-15 11:33:12 -0400 | [diff] [blame] | 451 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | } |
Robert Phillips | 18166ee | 2017-06-01 12:55:44 -0400 | [diff] [blame] | 457 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 458 | #endif // GR_TEST_UTILS |
| 459 | #endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 460 | #endif // SK_SUPPORT_GPU |