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