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