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 | |
| 8 | #include "Test.h" |
| 9 | |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 10 | #include "GrClip.h" |
| 11 | #include "GrContext.h" |
| 12 | #include "GrContextPriv.h" |
| 13 | #include "GrResourceCache.h" |
Brian Salomon | 653f42f | 2018-07-10 10:07:31 -0400 | [diff] [blame] | 14 | #include "GrShape.h" |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame] | 15 | #include "GrSoftwarePathRenderer.h" |
Brian Salomon | 653f42f | 2018-07-10 10:07:31 -0400 | [diff] [blame] | 16 | #include "GrStyle.h" |
| 17 | #include "SkPath.h" |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 18 | #include "effects/GrPorterDuffXferProcessor.h" |
| 19 | #include "ops/GrTessellatingPathRenderer.h" |
| 20 | |
| 21 | static SkPath create_concave_path() { |
| 22 | SkPath path; |
| 23 | path.moveTo(100, 0); |
| 24 | path.lineTo(200, 200); |
| 25 | path.lineTo(100, 150); |
| 26 | path.lineTo(0, 200); |
| 27 | path.close(); |
| 28 | return path; |
| 29 | } |
| 30 | |
Chris Dalton | 09e5689 | 2019-03-13 00:22:01 -0600 | [diff] [blame^] | 31 | using AATypeFlags = GrPathRenderer::AATypeFlags; |
| 32 | |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 33 | static void draw_path(GrContext* ctx, |
| 34 | GrRenderTargetContext* renderTargetContext, |
| 35 | const SkPath& path, |
| 36 | GrPathRenderer* pr, |
Chris Dalton | 09e5689 | 2019-03-13 00:22:01 -0600 | [diff] [blame^] | 37 | AATypeFlags aaTypeFlags, |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame] | 38 | const GrStyle& style) { |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 39 | GrPaint paint; |
| 40 | paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc)); |
| 41 | |
| 42 | GrNoClip noClip; |
| 43 | SkIRect clipConservativeBounds = SkIRect::MakeWH(renderTargetContext->width(), |
| 44 | renderTargetContext->height()); |
| 45 | GrShape shape(path, style); |
| 46 | if (shape.style().applies()) { |
| 47 | shape = shape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, 1.0f); |
| 48 | } |
| 49 | SkMatrix matrix = SkMatrix::I(); |
| 50 | GrPathRenderer::DrawPathArgs args{ctx, |
| 51 | std::move(paint), |
| 52 | &GrUserStencilSettings::kUnused, |
| 53 | renderTargetContext, |
| 54 | &noClip, |
| 55 | &clipConservativeBounds, |
| 56 | &matrix, |
| 57 | &shape, |
Chris Dalton | 09e5689 | 2019-03-13 00:22:01 -0600 | [diff] [blame^] | 58 | aaTypeFlags, |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 59 | false}; |
| 60 | pr->drawPath(args); |
| 61 | } |
| 62 | |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame] | 63 | static bool cache_non_scratch_resources_equals(GrResourceCache* cache, int expected) { |
| 64 | #if GR_CACHE_STATS |
| 65 | GrResourceCache::Stats stats; |
| 66 | cache->getStats(&stats); |
| 67 | return (stats.fTotal - stats.fScratch) == expected; |
| 68 | #else |
| 69 | return true; |
| 70 | #endif |
| 71 | } |
| 72 | |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 73 | static void test_path(skiatest::Reporter* reporter, |
| 74 | std::function<SkPath(void)> createPath, |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame] | 75 | std::function<GrPathRenderer*(GrContext*)> createPathRenderer, |
Brian Osman | 5fcd391 | 2017-10-04 14:45:04 -0400 | [diff] [blame] | 76 | int expected, |
Chris Dalton | 09e5689 | 2019-03-13 00:22:01 -0600 | [diff] [blame^] | 77 | AATypeFlags aaTypeFlags = AATypeFlags::kNone, |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 78 | GrStyle style = GrStyle(SkStrokeRec::kFill_InitStyle)) { |
| 79 | sk_sp<GrContext> ctx = GrContext::MakeMock(nullptr); |
Brian Osman | 5fcd391 | 2017-10-04 14:45:04 -0400 | [diff] [blame] | 80 | // The cache needs to be big enough that nothing gets flushed, or our expectations can be wrong |
Brian Salomon | 57b0430 | 2018-01-30 15:43:49 -0500 | [diff] [blame] | 81 | ctx->setResourceCacheLimits(100, 8000000); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 82 | GrResourceCache* cache = ctx->priv().getResourceCache(); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 83 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 84 | const GrBackendFormat format = |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 85 | ctx->priv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType); |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 86 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 87 | sk_sp<GrRenderTargetContext> rtc(ctx->priv().makeDeferredRenderTargetContext( |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 88 | format, SkBackingFit::kApprox, 800, 800, kRGBA_8888_GrPixelConfig, nullptr, 1, |
| 89 | GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin)); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 90 | if (!rtc) { |
| 91 | return; |
| 92 | } |
| 93 | |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame] | 94 | sk_sp<GrPathRenderer> pathRenderer(createPathRenderer(ctx.get())); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 95 | SkPath path = createPath(); |
| 96 | |
| 97 | // Initially, cache only has the render target context |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame] | 98 | REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, 0)); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 99 | |
| 100 | // Draw the path, check that new resource count matches expectations |
Chris Dalton | 09e5689 | 2019-03-13 00:22:01 -0600 | [diff] [blame^] | 101 | draw_path(ctx.get(), rtc.get(), path, pathRenderer.get(), aaTypeFlags, style); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 102 | ctx->flush(); |
Brian Osman | 5fcd391 | 2017-10-04 14:45:04 -0400 | [diff] [blame] | 103 | REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, expected)); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 104 | |
| 105 | // Nothing should be purgeable yet |
| 106 | cache->purgeAsNeeded(); |
Brian Osman | 5fcd391 | 2017-10-04 14:45:04 -0400 | [diff] [blame] | 107 | REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, expected)); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 108 | |
Brian Osman | 5fcd391 | 2017-10-04 14:45:04 -0400 | [diff] [blame] | 109 | // Reset the path to change the GenID, which should invalidate one resource in the cache. |
| 110 | // 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] | 111 | path.reset(); |
| 112 | cache->purgeAsNeeded(); |
Brian Osman | 5fcd391 | 2017-10-04 14:45:04 -0400 | [diff] [blame] | 113 | REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, expected - 1)); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // Test that deleting the original path invalidates the VBs cached by the tessellating path renderer |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 117 | DEF_GPUTEST(TessellatingPathRendererCacheTest, reporter, /* options */) { |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame] | 118 | auto createPR = [](GrContext*) { |
| 119 | return new GrTessellatingPathRenderer(); |
| 120 | }; |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 121 | |
Brian Osman | 5fcd391 | 2017-10-04 14:45:04 -0400 | [diff] [blame] | 122 | // Tessellating path renderer creates a single vertex buffer for non-AA paths. No other |
| 123 | // resources should be created. |
| 124 | const int kExpectedResources = 1; |
| 125 | |
| 126 | test_path(reporter, create_concave_path, createPR, kExpectedResources); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 127 | |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame] | 128 | // Test with a style that alters the path geometry. This needs to attach the invalidation logic |
| 129 | // to the original path, not the modified path produced by the style. |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 130 | SkPaint paint; |
| 131 | paint.setStyle(SkPaint::kStroke_Style); |
| 132 | paint.setStrokeWidth(1); |
| 133 | GrStyle style(paint); |
Chris Dalton | 09e5689 | 2019-03-13 00:22:01 -0600 | [diff] [blame^] | 134 | test_path( |
| 135 | reporter, create_concave_path, createPR, kExpectedResources, AATypeFlags::kNone, style); |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // 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] | 139 | DEF_GPUTEST(SoftwarePathRendererCacheTest, reporter, /* options */) { |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame] | 140 | auto createPR = [](GrContext* ctx) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 141 | return new GrSoftwarePathRenderer(ctx->priv().proxyProvider(), true); |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame] | 142 | }; |
| 143 | |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 144 | // Software path renderer creates a mask texture and renders with a non-AA rect, but the flush |
| 145 | // only contains a single quad so GrFillRectOp doesn't need to use the shared index buffer. |
| 146 | const int kExpectedResources = 1; |
Brian Osman | 5fcd391 | 2017-10-04 14:45:04 -0400 | [diff] [blame] | 147 | |
Chris Dalton | 09e5689 | 2019-03-13 00:22:01 -0600 | [diff] [blame^] | 148 | test_path(reporter, create_concave_path, createPR, kExpectedResources, AATypeFlags::kCoverage); |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame] | 149 | |
| 150 | // Test with a style that alters the path geometry. This needs to attach the invalidation logic |
| 151 | // to the original path, not the modified path produced by the style. |
| 152 | SkPaint paint; |
| 153 | paint.setStyle(SkPaint::kStroke_Style); |
| 154 | paint.setStrokeWidth(1); |
| 155 | GrStyle style(paint); |
Chris Dalton | 09e5689 | 2019-03-13 00:22:01 -0600 | [diff] [blame^] | 156 | test_path(reporter, create_concave_path, createPR, kExpectedResources, AATypeFlags::kCoverage, |
Brian Osman | 5fcd391 | 2017-10-04 14:45:04 -0400 | [diff] [blame] | 157 | style); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 158 | } |