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