Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "tests/Test.h" |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkPath.h" |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 11 | #include "include/gpu/GrDirectContext.h" |
| 12 | #include "include/gpu/GrRecordingContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrContextPriv.h" |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrRecordingContextPriv.h" |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrRenderTargetContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/gpu/GrResourceCache.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrSoftwarePathRenderer.h" |
| 18 | #include "src/gpu/GrStyle.h" |
| 19 | #include "src/gpu/effects/GrPorterDuffXferProcessor.h" |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 20 | #include "src/gpu/geometry/GrStyledShape.h" |
Chris Dalton | 17dc418 | 2020-03-25 16:18:16 -0600 | [diff] [blame] | 21 | #include "src/gpu/ops/GrTriangulatingPathRenderer.h" |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 22 | |
| 23 | static SkPath create_concave_path() { |
| 24 | SkPath path; |
| 25 | path.moveTo(100, 0); |
| 26 | path.lineTo(200, 200); |
| 27 | path.lineTo(100, 150); |
| 28 | path.lineTo(0, 200); |
| 29 | path.close(); |
| 30 | return path; |
| 31 | } |
| 32 | |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 33 | static void draw_path(GrRecordingContext* rContext, |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 34 | GrRenderTargetContext* renderTargetContext, |
| 35 | const SkPath& path, |
| 36 | GrPathRenderer* pr, |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 37 | GrAAType aaType, |
Brian Salomon | 9f00494 | 2020-02-25 09:14:08 -0500 | [diff] [blame] | 38 | const GrStyle& style, |
| 39 | float scaleX = 1.f) { |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 40 | GrPaint paint; |
| 41 | paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc)); |
| 42 | |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 43 | SkIRect clipConservativeBounds = SkIRect::MakeWH(renderTargetContext->width(), |
| 44 | renderTargetContext->height()); |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 45 | GrStyledShape shape(path, style); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 46 | if (shape.style().applies()) { |
| 47 | shape = shape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, 1.0f); |
| 48 | } |
| 49 | SkMatrix matrix = SkMatrix::I(); |
Brian Salomon | 9f00494 | 2020-02-25 09:14:08 -0500 | [diff] [blame] | 50 | matrix.setScaleX(scaleX); |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 51 | GrPathRenderer::DrawPathArgs args{rContext, |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 52 | std::move(paint), |
| 53 | &GrUserStencilSettings::kUnused, |
| 54 | renderTargetContext, |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 55 | nullptr, |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 56 | &clipConservativeBounds, |
| 57 | &matrix, |
| 58 | &shape, |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 59 | aaType, |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 60 | false}; |
| 61 | pr->drawPath(args); |
| 62 | } |
| 63 | |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame] | 64 | static bool cache_non_scratch_resources_equals(GrResourceCache* cache, int expected) { |
| 65 | #if GR_CACHE_STATS |
| 66 | GrResourceCache::Stats stats; |
| 67 | cache->getStats(&stats); |
| 68 | return (stats.fTotal - stats.fScratch) == expected; |
| 69 | #else |
| 70 | return true; |
| 71 | #endif |
| 72 | } |
| 73 | |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 74 | static void test_path(skiatest::Reporter* reporter, |
| 75 | std::function<SkPath(void)> createPath, |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 76 | std::function<GrPathRenderer*(GrRecordingContext*)> createPathRenderer, |
Brian Osman | 5fcd391 | 2017-10-04 14:45:04 -0400 | [diff] [blame] | 77 | int expected, |
Brian Salomon | 9f00494 | 2020-02-25 09:14:08 -0500 | [diff] [blame] | 78 | bool checkListeners, |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 79 | GrAAType aaType = GrAAType::kNone, |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 80 | GrStyle style = GrStyle(SkStrokeRec::kFill_InitStyle)) { |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 81 | sk_sp<GrDirectContext> dContext = GrDirectContext::MakeMock(nullptr); |
Brian Osman | 5fcd391 | 2017-10-04 14:45:04 -0400 | [diff] [blame] | 82 | // The cache needs to be big enough that nothing gets flushed, or our expectations can be wrong |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 83 | dContext->setResourceCacheLimit(8000000); |
| 84 | GrResourceCache* cache = dContext->priv().getResourceCache(); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 85 | |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 86 | auto rtc = GrRenderTargetContext::Make( |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 87 | dContext.get(), GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {800, 800}, 1, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 88 | GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 89 | if (!rtc) { |
| 90 | return; |
| 91 | } |
| 92 | |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 93 | sk_sp<GrPathRenderer> pathRenderer(createPathRenderer(dContext.get())); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 94 | SkPath path = createPath(); |
| 95 | |
| 96 | // Initially, cache only has the render target context |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame] | 97 | REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, 0)); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 98 | |
| 99 | // Draw the path, check that new resource count matches expectations |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 100 | draw_path(dContext.get(), rtc.get(), path, pathRenderer.get(), aaType, style); |
| 101 | dContext->flushAndSubmit(); |
Brian Osman | 5fcd391 | 2017-10-04 14:45:04 -0400 | [diff] [blame] | 102 | REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, expected)); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 103 | |
| 104 | // Nothing should be purgeable yet |
| 105 | cache->purgeAsNeeded(); |
Brian Osman | 5fcd391 | 2017-10-04 14:45:04 -0400 | [diff] [blame] | 106 | REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, expected)); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 107 | |
Brian Osman | 5fcd391 | 2017-10-04 14:45:04 -0400 | [diff] [blame] | 108 | // Reset the path to change the GenID, which should invalidate one resource in the cache. |
| 109 | // Some path renderers may leave other unique-keyed resources in the cache, though. |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 110 | path.reset(); |
| 111 | cache->purgeAsNeeded(); |
Brian Osman | 5fcd391 | 2017-10-04 14:45:04 -0400 | [diff] [blame] | 112 | REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, expected - 1)); |
Brian Salomon | 9f00494 | 2020-02-25 09:14:08 -0500 | [diff] [blame] | 113 | |
| 114 | if (!checkListeners) { |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | // Test that purging the cache of masks also removes listeners from the path. |
| 119 | path = createPath(); |
| 120 | REPORTER_ASSERT(reporter, SkPathPriv::GenIDChangeListenersCount(path) == 0); |
| 121 | for (int i = 0; i < 20; ++i) { |
| 122 | float scaleX = 1 + ((float)i + 1)/20.f; |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 123 | draw_path(dContext.get(), rtc.get(), path, pathRenderer.get(), aaType, style, scaleX); |
Brian Salomon | 9f00494 | 2020-02-25 09:14:08 -0500 | [diff] [blame] | 124 | } |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 125 | dContext->flushAndSubmit(); |
Brian Salomon | 9f00494 | 2020-02-25 09:14:08 -0500 | [diff] [blame] | 126 | REPORTER_ASSERT(reporter, SkPathPriv::GenIDChangeListenersCount(path) == 20); |
| 127 | cache->purgeAllUnlocked(); |
| 128 | // The listeners don't actually purge until we try to add another one. |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 129 | draw_path(dContext.get(), rtc.get(), path, pathRenderer.get(), aaType, style); |
Brian Salomon | 9f00494 | 2020-02-25 09:14:08 -0500 | [diff] [blame] | 130 | REPORTER_ASSERT(reporter, SkPathPriv::GenIDChangeListenersCount(path) == 1); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // Test that deleting the original path invalidates the VBs cached by the tessellating path renderer |
Chris Dalton | 17dc418 | 2020-03-25 16:18:16 -0600 | [diff] [blame] | 134 | DEF_GPUTEST(TriangulatingPathRendererCacheTest, reporter, /* options */) { |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 135 | auto createPR = [](GrRecordingContext*) { |
Chris Dalton | 17dc418 | 2020-03-25 16:18:16 -0600 | [diff] [blame] | 136 | return new GrTriangulatingPathRenderer(); |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame] | 137 | }; |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 138 | |
Chris Dalton | 17dc418 | 2020-03-25 16:18:16 -0600 | [diff] [blame] | 139 | // Triangulating path renderer creates a single vertex buffer for non-AA paths. No other |
Brian Osman | 5fcd391 | 2017-10-04 14:45:04 -0400 | [diff] [blame] | 140 | // resources should be created. |
| 141 | const int kExpectedResources = 1; |
| 142 | |
Brian Salomon | 9f00494 | 2020-02-25 09:14:08 -0500 | [diff] [blame] | 143 | test_path(reporter, create_concave_path, createPR, kExpectedResources, false); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 144 | |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame] | 145 | // Test with a style that alters the path geometry. This needs to attach the invalidation logic |
| 146 | // to the original path, not the modified path produced by the style. |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 147 | SkPaint paint; |
| 148 | paint.setStyle(SkPaint::kStroke_Style); |
| 149 | paint.setStrokeWidth(1); |
| 150 | GrStyle style(paint); |
Brian Salomon | 9f00494 | 2020-02-25 09:14:08 -0500 | [diff] [blame] | 151 | test_path(reporter, create_concave_path, createPR, kExpectedResources, false, GrAAType::kNone, |
| 152 | style); |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | // Test that deleting the original path invalidates the textures cached by the SW path renderer |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 156 | DEF_GPUTEST(SoftwarePathRendererCacheTest, reporter, /* options */) { |
Robert Phillips | 0c5bb2f | 2020-07-17 15:40:13 -0400 | [diff] [blame] | 157 | auto createPR = [](GrRecordingContext* rContext) { |
| 158 | return new GrSoftwarePathRenderer(rContext->priv().proxyProvider(), true); |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame] | 159 | }; |
| 160 | |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 161 | // Software path renderer creates a mask texture and renders with a non-AA rect, but the flush |
| 162 | // only contains a single quad so GrFillRectOp doesn't need to use the shared index buffer. |
| 163 | const int kExpectedResources = 1; |
Brian Osman | 5fcd391 | 2017-10-04 14:45:04 -0400 | [diff] [blame] | 164 | |
Brian Salomon | 9f00494 | 2020-02-25 09:14:08 -0500 | [diff] [blame] | 165 | test_path(reporter, create_concave_path, createPR, kExpectedResources, true, |
| 166 | GrAAType::kCoverage); |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame] | 167 | |
| 168 | // Test with a style that alters the path geometry. This needs to attach the invalidation logic |
| 169 | // to the original path, not the modified path produced by the style. |
| 170 | SkPaint paint; |
| 171 | paint.setStyle(SkPaint::kStroke_Style); |
| 172 | paint.setStrokeWidth(1); |
| 173 | GrStyle style(paint); |
Brian Salomon | 9f00494 | 2020-02-25 09:14:08 -0500 | [diff] [blame] | 174 | test_path(reporter, create_concave_path, createPR, kExpectedResources, true, |
| 175 | GrAAType::kCoverage, style); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 176 | } |