Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
| 13 | #include "GrContext.h" |
| 14 | #include "GrContextPriv.h" |
| 15 | #include "GrClip.h" |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame^] | 16 | #include "GrDrawingManager.h" |
| 17 | #include "GrPathRenderer.h" |
| 18 | #include "GrPaint.h" |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 19 | #include "GrRenderTargetContext.h" |
| 20 | #include "GrRenderTargetContextPriv.h" |
| 21 | #include "GrShape.h" |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 22 | #include "SkMatrix.h" |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame^] | 23 | #include "SkPathPriv.h" |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 24 | #include "SkRect.h" |
| 25 | #include "ccpr/GrCoverageCountingPathRenderer.h" |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame^] | 26 | #include "mock/GrMockTypes.h" |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 27 | #include <cmath> |
| 28 | |
| 29 | static constexpr int kCanvasSize = 100; |
| 30 | |
| 31 | class CCPRPathDrawer { |
| 32 | public: |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame^] | 33 | CCPRPathDrawer(GrContext* ctx, skiatest::Reporter* reporter) |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 34 | : fCtx(ctx) |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame^] | 35 | , fCCPR(fCtx->contextPriv().drawingManager()->getCoverageCountingPathRenderer()) |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 36 | , fRTC(fCtx->makeDeferredRenderTargetContext(SkBackingFit::kExact, kCanvasSize, |
| 37 | kCanvasSize, kRGBA_8888_GrPixelConfig, |
| 38 | nullptr)) { |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame^] | 39 | if (!fCCPR) { |
| 40 | ERRORF(reporter, "ccpr not enabled in GrContext for ccpr tests"); |
| 41 | } |
| 42 | if (!fRTC) { |
| 43 | ERRORF(reporter, "failed to create GrRenderTargetContext for ccpr tests"); |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame^] | 47 | bool valid() const { return fCCPR && fRTC; } |
| 48 | void clear() const { fRTC->clear(nullptr, 0, true); } |
| 49 | void abandonGrContext() { fCtx = nullptr; fCCPR = nullptr; fRTC = nullptr; } |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 50 | |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame^] | 51 | void drawPath(SkPath path, GrColor4f color = GrColor4f(0, 1, 0, 1)) const { |
| 52 | SkASSERT(this->valid()); |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 53 | |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 54 | GrPaint paint; |
| 55 | paint.setColor4f(color); |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame^] | 56 | |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 57 | GrNoClip noClip; |
| 58 | SkIRect clipBounds = SkIRect::MakeWH(kCanvasSize, kCanvasSize); |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame^] | 59 | |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 60 | SkMatrix matrix = SkMatrix::I(); |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame^] | 61 | |
| 62 | path.setIsVolatile(true); |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 63 | GrShape shape(path); |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame^] | 64 | |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 65 | fCCPR->drawPath({fCtx, std::move(paint), &GrUserStencilSettings::kUnused, fRTC.get(), |
| 66 | &noClip, &clipBounds, &matrix, &shape, GrAAType::kCoverage, false}); |
| 67 | } |
| 68 | |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame^] | 69 | void flush() const { |
| 70 | SkASSERT(this->valid()); |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 71 | fCtx->flush(); |
| 72 | } |
| 73 | |
| 74 | private: |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame^] | 75 | GrContext* fCtx; |
| 76 | GrCoverageCountingPathRenderer* fCCPR; |
| 77 | sk_sp<GrRenderTargetContext> fRTC; |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 78 | }; |
| 79 | |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame^] | 80 | class CCPRTest { |
| 81 | public: |
| 82 | void run(skiatest::Reporter* reporter) { |
| 83 | GrMockOptions mockOptions; |
| 84 | mockOptions.fInstanceAttribSupport = true; |
| 85 | mockOptions.fMapBufferFlags = GrCaps::kCanMap_MapFlag; |
| 86 | mockOptions.fConfigOptions[kAlpha_half_GrPixelConfig].fRenderable[0] = true; |
| 87 | mockOptions.fConfigOptions[kAlpha_half_GrPixelConfig].fTexturable = true; |
| 88 | mockOptions.fGeometryShaderSupport = true; |
| 89 | mockOptions.fTexelBufferSupport = true; |
| 90 | mockOptions.fIntegerSupport = true; |
| 91 | mockOptions.fFlatInterpolationSupport = true; |
| 92 | mockOptions.fMaxVertexSamplers = 1; |
| 93 | |
| 94 | GrContextOptions ctxOptions; |
| 95 | ctxOptions.fAllowPathMaskCaching = false; |
| 96 | ctxOptions.fGpuPathRenderers = GpuPathRenderers::kCoverageCounting; |
| 97 | |
| 98 | fMockContext = GrContext::MakeMock(&mockOptions, ctxOptions); |
| 99 | if (!fMockContext) { |
| 100 | ERRORF(reporter, "could not create mock context"); |
| 101 | return; |
| 102 | } |
| 103 | if (!fMockContext->unique()) { |
| 104 | ERRORF(reporter, "mock context is not unique"); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | CCPRPathDrawer ccpr(fMockContext.get(), reporter); |
| 109 | if (!ccpr.valid()) { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | fPath.moveTo(0, 0); |
| 114 | fPath.cubicTo(50, 50, 0, 50, 50, 0); |
| 115 | this->onRun(reporter, ccpr); |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 116 | } |
| 117 | |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame^] | 118 | virtual ~CCPRTest() {} |
| 119 | |
| 120 | protected: |
| 121 | virtual void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) = 0; |
| 122 | |
| 123 | sk_sp<GrContext> fMockContext; |
| 124 | SkPath fPath; |
| 125 | }; |
| 126 | |
| 127 | #define DEF_CCPR_TEST(name) \ |
| 128 | DEF_GPUTEST(name, reporter, factory) { \ |
| 129 | name test; \ |
| 130 | test.run(reporter); \ |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 131 | } |
| 132 | |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame^] | 133 | class GrCCPRTest_cleanup : public CCPRTest { |
| 134 | void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) override { |
| 135 | REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath)); |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 136 | |
Chris Dalton | fddb6c0 | 2017-11-04 15:22:22 -0600 | [diff] [blame^] | 137 | // Ensure paths get unreffed. |
| 138 | for (int i = 0; i < 10; ++i) { |
| 139 | ccpr.drawPath(fPath); |
| 140 | } |
| 141 | REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath)); |
| 142 | ccpr.flush(); |
| 143 | REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath)); |
| 144 | |
| 145 | // Ensure paths get unreffed when we delete the context without flushing. |
| 146 | for (int i = 0; i < 10; ++i) { |
| 147 | ccpr.drawPath(fPath); |
| 148 | } |
| 149 | ccpr.abandonGrContext(); |
| 150 | REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath)); |
| 151 | fMockContext.reset(); |
| 152 | REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath)); |
| 153 | } |
| 154 | }; |
| 155 | DEF_CCPR_TEST(GrCCPRTest_cleanup) |
| 156 | |
| 157 | class CCPRRenderingTest { |
| 158 | public: |
| 159 | void run(skiatest::Reporter* reporter, GrContext* ctx) const { |
| 160 | if (!ctx->contextPriv().drawingManager()->getCoverageCountingPathRenderer()) { |
| 161 | return; // CCPR is not enabled on this GPU. |
| 162 | } |
| 163 | CCPRPathDrawer ccpr(ctx, reporter); |
| 164 | if (!ccpr.valid()) { |
| 165 | return; |
| 166 | } |
| 167 | this->onRun(reporter, ccpr); |
| 168 | } |
| 169 | |
| 170 | virtual ~CCPRRenderingTest() {} |
| 171 | |
| 172 | protected: |
| 173 | virtual void onRun(skiatest::Reporter* reporter, const CCPRPathDrawer& ccpr) const = 0; |
| 174 | }; |
| 175 | |
| 176 | #define DEF_CCPR_RENDERING_TEST(name) \ |
| 177 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(name, reporter, ctxInfo) { \ |
| 178 | name test; \ |
| 179 | test.run(reporter, ctxInfo.grContext()); \ |
| 180 | } |
| 181 | |
| 182 | class GrCCPRTest_busyPath : public CCPRRenderingTest { |
| 183 | void onRun(skiatest::Reporter* reporter, const CCPRPathDrawer& ccpr) const override { |
| 184 | static constexpr int kNumBusyVerbs = 1 << 17; |
| 185 | ccpr.clear(); |
| 186 | SkPath busyPath; |
| 187 | busyPath.moveTo(0, 0); // top left |
| 188 | busyPath.lineTo(kCanvasSize, kCanvasSize); // bottom right |
| 189 | for (int i = 2; i < kNumBusyVerbs; ++i) { |
| 190 | float offset = i * ((float)kCanvasSize / kNumBusyVerbs); |
| 191 | busyPath.lineTo(kCanvasSize - offset, kCanvasSize + offset); // offscreen |
| 192 | } |
| 193 | ccpr.drawPath(busyPath); |
| 194 | |
| 195 | ccpr.flush(); // If this doesn't crash, the test passed. |
| 196 | // If it does, maybe fiddle with fMaxInstancesPerDrawArraysWithoutCrashing in |
| 197 | // your platform's GrGLCaps. |
| 198 | } |
| 199 | }; |
| 200 | DEF_CCPR_RENDERING_TEST(GrCCPRTest_busyPath) |
Chris Dalton | cc604e5 | 2017-10-06 16:27:32 -0600 | [diff] [blame] | 201 | |
| 202 | #endif |