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