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 | |
| 10 | #include "SkPath.h" |
| 11 | |
| 12 | #if SK_SUPPORT_GPU |
| 13 | #include "GrClip.h" |
| 14 | #include "GrContext.h" |
| 15 | #include "GrContextPriv.h" |
| 16 | #include "GrResourceCache.h" |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame^] | 17 | #include "GrSoftwarePathRenderer.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 | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 74 | GrAAType aaType = GrAAType::kNone, |
| 75 | GrStyle style = GrStyle(SkStrokeRec::kFill_InitStyle)) { |
| 76 | sk_sp<GrContext> ctx = GrContext::MakeMock(nullptr); |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame^] | 77 | ctx->setResourceCacheLimits(100, 100000); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 78 | GrResourceCache* cache = ctx->getResourceCache(); |
| 79 | |
| 80 | sk_sp<GrRenderTargetContext> rtc(ctx->makeDeferredRenderTargetContext( |
| 81 | SkBackingFit::kApprox, 800, 800, kRGBA_8888_GrPixelConfig, nullptr, 0, |
| 82 | kTopLeft_GrSurfaceOrigin)); |
| 83 | if (!rtc) { |
| 84 | return; |
| 85 | } |
| 86 | |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame^] | 87 | sk_sp<GrPathRenderer> pathRenderer(createPathRenderer(ctx.get())); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 88 | SkPath path = createPath(); |
| 89 | |
| 90 | // Initially, cache only has the render target context |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame^] | 91 | REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, 0)); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 92 | |
| 93 | // Draw the path, check that new resource count matches expectations |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame^] | 94 | draw_path(ctx.get(), rtc.get(), path, pathRenderer.get(), aaType, style); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 95 | ctx->flush(); |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame^] | 96 | REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, 1)); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 97 | |
| 98 | // Nothing should be purgeable yet |
| 99 | cache->purgeAsNeeded(); |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame^] | 100 | REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, 1)); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 101 | |
| 102 | // Reset the path to change the GenID, which should invalidate any resources in the cache |
| 103 | path.reset(); |
| 104 | cache->purgeAsNeeded(); |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame^] | 105 | REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, 0)); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // Test that deleting the original path invalidates the VBs cached by the tessellating path renderer |
| 109 | DEF_GPUTEST(TessellatingPathRendererCacheTest, reporter, factory) { |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame^] | 110 | auto createPR = [](GrContext*) { |
| 111 | return new GrTessellatingPathRenderer(); |
| 112 | }; |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 113 | |
| 114 | // Tessellating path renderer stores vertex buffers in the cache (for non-AA paths) |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame^] | 115 | test_path(reporter, create_concave_path, createPR); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 116 | |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame^] | 117 | // Test with a style that alters the path geometry. This needs to attach the invalidation logic |
| 118 | // to the original path, not the modified path produced by the style. |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 119 | SkPaint paint; |
| 120 | paint.setStyle(SkPaint::kStroke_Style); |
| 121 | paint.setStrokeWidth(1); |
| 122 | GrStyle style(paint); |
Brian Osman | e9242ca | 2017-09-26 14:05:19 -0400 | [diff] [blame^] | 123 | test_path(reporter, create_concave_path, createPR, GrAAType::kNone, style); |
| 124 | } |
| 125 | |
| 126 | // Test that deleting the original path invalidates the textures cached by the SW path renderer |
| 127 | DEF_GPUTEST(SoftwarePathRendererCacheTest, reporter, factory) { |
| 128 | auto createPR = [](GrContext* ctx) { |
| 129 | return new GrSoftwarePathRenderer(ctx->resourceProvider(), true); |
| 130 | }; |
| 131 | |
| 132 | // Tessellating path renderer stores vertex buffers in the cache (for non-AA paths) |
| 133 | test_path(reporter, create_concave_path, createPR, GrAAType::kCoverage); |
| 134 | |
| 135 | // Test with a style that alters the path geometry. This needs to attach the invalidation logic |
| 136 | // to the original path, not the modified path produced by the style. |
| 137 | SkPaint paint; |
| 138 | paint.setStyle(SkPaint::kStroke_Style); |
| 139 | paint.setStrokeWidth(1); |
| 140 | GrStyle style(paint); |
| 141 | test_path(reporter, create_concave_path, createPR, GrAAType::kCoverage, style); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | #endif |