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