blob: 626c7150692b1b0614d2749d7876c5db7f0f010d [file] [log] [blame]
Brian Osmanf6f7cf62017-09-25 16:49:55 -04001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "tests/Test.h"
Brian Osmanf6f7cf62017-09-25 16:49:55 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkPath.h"
11#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrContextPriv.h"
Michael Ludwig7c12e282020-05-29 09:54:07 -040013#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrResourceCache.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrSoftwarePathRenderer.h"
16#include "src/gpu/GrStyle.h"
17#include "src/gpu/effects/GrPorterDuffXferProcessor.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000018#include "src/gpu/geometry/GrStyledShape.h"
Chris Dalton17dc4182020-03-25 16:18:16 -060019#include "src/gpu/ops/GrTriangulatingPathRenderer.h"
Brian Osmanf6f7cf62017-09-25 16:49:55 -040020
21static 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
31static void draw_path(GrContext* ctx,
32 GrRenderTargetContext* renderTargetContext,
33 const SkPath& path,
34 GrPathRenderer* pr,
Chris Dalton6ce447a2019-06-23 18:07:38 -060035 GrAAType aaType,
Brian Salomon9f004942020-02-25 09:14:08 -050036 const GrStyle& style,
37 float scaleX = 1.f) {
Brian Osmanf6f7cf62017-09-25 16:49:55 -040038 GrPaint paint;
39 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
40
Brian Osmanf6f7cf62017-09-25 16:49:55 -040041 SkIRect clipConservativeBounds = SkIRect::MakeWH(renderTargetContext->width(),
42 renderTargetContext->height());
Michael Ludwig2686d692020-04-17 20:21:37 +000043 GrStyledShape shape(path, style);
Brian Osmanf6f7cf62017-09-25 16:49:55 -040044 if (shape.style().applies()) {
45 shape = shape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, 1.0f);
46 }
47 SkMatrix matrix = SkMatrix::I();
Brian Salomon9f004942020-02-25 09:14:08 -050048 matrix.setScaleX(scaleX);
Brian Osmanf6f7cf62017-09-25 16:49:55 -040049 GrPathRenderer::DrawPathArgs args{ctx,
50 std::move(paint),
51 &GrUserStencilSettings::kUnused,
52 renderTargetContext,
Michael Ludwig7c12e282020-05-29 09:54:07 -040053 nullptr,
Brian Osmanf6f7cf62017-09-25 16:49:55 -040054 &clipConservativeBounds,
55 &matrix,
56 &shape,
Chris Dalton6ce447a2019-06-23 18:07:38 -060057 aaType,
Brian Osmanf6f7cf62017-09-25 16:49:55 -040058 false};
59 pr->drawPath(args);
60}
61
Brian Osmane9242ca2017-09-26 14:05:19 -040062static bool cache_non_scratch_resources_equals(GrResourceCache* cache, int expected) {
63#if GR_CACHE_STATS
64 GrResourceCache::Stats stats;
65 cache->getStats(&stats);
66 return (stats.fTotal - stats.fScratch) == expected;
67#else
68 return true;
69#endif
70}
71
Brian Osmanf6f7cf62017-09-25 16:49:55 -040072static void test_path(skiatest::Reporter* reporter,
73 std::function<SkPath(void)> createPath,
Brian Osmane9242ca2017-09-26 14:05:19 -040074 std::function<GrPathRenderer*(GrContext*)> createPathRenderer,
Brian Osman5fcd3912017-10-04 14:45:04 -040075 int expected,
Brian Salomon9f004942020-02-25 09:14:08 -050076 bool checkListeners,
Chris Dalton6ce447a2019-06-23 18:07:38 -060077 GrAAType aaType = GrAAType::kNone,
Brian Osmanf6f7cf62017-09-25 16:49:55 -040078 GrStyle style = GrStyle(SkStrokeRec::kFill_InitStyle)) {
Robert Phillipsf4f80112020-07-13 16:13:31 -040079 sk_sp<GrDirectContext> ctx = GrDirectContext::MakeMock(nullptr);
Brian Osman5fcd3912017-10-04 14:45:04 -040080 // The cache needs to be big enough that nothing gets flushed, or our expectations can be wrong
Robert Phillipscf39f372019-09-03 10:29:20 -040081 ctx->setResourceCacheLimit(8000000);
Robert Phillips9da87e02019-02-04 13:26:26 -050082 GrResourceCache* cache = ctx->priv().getResourceCache();
Brian Osmanf6f7cf62017-09-25 16:49:55 -040083
Greg Daniele20fcad2020-01-08 11:52:34 -050084 auto rtc = GrRenderTargetContext::Make(
85 ctx.get(), GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {800, 800}, 1,
86 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
Brian Osmanf6f7cf62017-09-25 16:49:55 -040087 if (!rtc) {
88 return;
89 }
90
Brian Osmane9242ca2017-09-26 14:05:19 -040091 sk_sp<GrPathRenderer> pathRenderer(createPathRenderer(ctx.get()));
Brian Osmanf6f7cf62017-09-25 16:49:55 -040092 SkPath path = createPath();
93
94 // Initially, cache only has the render target context
Brian Osmane9242ca2017-09-26 14:05:19 -040095 REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, 0));
Brian Osmanf6f7cf62017-09-25 16:49:55 -040096
97 // Draw the path, check that new resource count matches expectations
Chris Dalton6ce447a2019-06-23 18:07:38 -060098 draw_path(ctx.get(), rtc.get(), path, pathRenderer.get(), aaType, style);
Greg Daniel0a2464f2020-05-14 15:45:44 -040099 ctx->flushAndSubmit();
Brian Osman5fcd3912017-10-04 14:45:04 -0400100 REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, expected));
Brian Osmanf6f7cf62017-09-25 16:49:55 -0400101
102 // Nothing should be purgeable yet
103 cache->purgeAsNeeded();
Brian Osman5fcd3912017-10-04 14:45:04 -0400104 REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, expected));
Brian Osmanf6f7cf62017-09-25 16:49:55 -0400105
Brian Osman5fcd3912017-10-04 14:45:04 -0400106 // Reset the path to change the GenID, which should invalidate one resource in the cache.
107 // Some path renderers may leave other unique-keyed resources in the cache, though.
Brian Osmanf6f7cf62017-09-25 16:49:55 -0400108 path.reset();
109 cache->purgeAsNeeded();
Brian Osman5fcd3912017-10-04 14:45:04 -0400110 REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, expected - 1));
Brian Salomon9f004942020-02-25 09:14:08 -0500111
112 if (!checkListeners) {
113 return;
114 }
115
116 // Test that purging the cache of masks also removes listeners from the path.
117 path = createPath();
118 REPORTER_ASSERT(reporter, SkPathPriv::GenIDChangeListenersCount(path) == 0);
119 for (int i = 0; i < 20; ++i) {
120 float scaleX = 1 + ((float)i + 1)/20.f;
121 draw_path(ctx.get(), rtc.get(), path, pathRenderer.get(), aaType, style, scaleX);
122 }
Greg Daniel0a2464f2020-05-14 15:45:44 -0400123 ctx->flushAndSubmit();
Brian Salomon9f004942020-02-25 09:14:08 -0500124 REPORTER_ASSERT(reporter, SkPathPriv::GenIDChangeListenersCount(path) == 20);
125 cache->purgeAllUnlocked();
126 // The listeners don't actually purge until we try to add another one.
127 draw_path(ctx.get(), rtc.get(), path, pathRenderer.get(), aaType, style);
128 REPORTER_ASSERT(reporter, SkPathPriv::GenIDChangeListenersCount(path) == 1);
Brian Osmanf6f7cf62017-09-25 16:49:55 -0400129}
130
131// Test that deleting the original path invalidates the VBs cached by the tessellating path renderer
Chris Dalton17dc4182020-03-25 16:18:16 -0600132DEF_GPUTEST(TriangulatingPathRendererCacheTest, reporter, /* options */) {
Brian Osmane9242ca2017-09-26 14:05:19 -0400133 auto createPR = [](GrContext*) {
Chris Dalton17dc4182020-03-25 16:18:16 -0600134 return new GrTriangulatingPathRenderer();
Brian Osmane9242ca2017-09-26 14:05:19 -0400135 };
Brian Osmanf6f7cf62017-09-25 16:49:55 -0400136
Chris Dalton17dc4182020-03-25 16:18:16 -0600137 // Triangulating path renderer creates a single vertex buffer for non-AA paths. No other
Brian Osman5fcd3912017-10-04 14:45:04 -0400138 // resources should be created.
139 const int kExpectedResources = 1;
140
Brian Salomon9f004942020-02-25 09:14:08 -0500141 test_path(reporter, create_concave_path, createPR, kExpectedResources, false);
Brian Osmanf6f7cf62017-09-25 16:49:55 -0400142
Brian Osmane9242ca2017-09-26 14:05:19 -0400143 // Test with a style that alters the path geometry. This needs to attach the invalidation logic
144 // to the original path, not the modified path produced by the style.
Brian Osmanf6f7cf62017-09-25 16:49:55 -0400145 SkPaint paint;
146 paint.setStyle(SkPaint::kStroke_Style);
147 paint.setStrokeWidth(1);
148 GrStyle style(paint);
Brian Salomon9f004942020-02-25 09:14:08 -0500149 test_path(reporter, create_concave_path, createPR, kExpectedResources, false, GrAAType::kNone,
150 style);
Brian Osmane9242ca2017-09-26 14:05:19 -0400151}
152
153// Test that deleting the original path invalidates the textures cached by the SW path renderer
Brian Salomondcfca432017-11-15 15:48:03 -0500154DEF_GPUTEST(SoftwarePathRendererCacheTest, reporter, /* options */) {
Brian Osmane9242ca2017-09-26 14:05:19 -0400155 auto createPR = [](GrContext* ctx) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500156 return new GrSoftwarePathRenderer(ctx->priv().proxyProvider(), true);
Brian Osmane9242ca2017-09-26 14:05:19 -0400157 };
158
Michael Ludwig72ab3462018-12-10 12:43:36 -0500159 // Software path renderer creates a mask texture and renders with a non-AA rect, but the flush
160 // only contains a single quad so GrFillRectOp doesn't need to use the shared index buffer.
161 const int kExpectedResources = 1;
Brian Osman5fcd3912017-10-04 14:45:04 -0400162
Brian Salomon9f004942020-02-25 09:14:08 -0500163 test_path(reporter, create_concave_path, createPR, kExpectedResources, true,
164 GrAAType::kCoverage);
Brian Osmane9242ca2017-09-26 14:05:19 -0400165
166 // Test with a style that alters the path geometry. This needs to attach the invalidation logic
167 // to the original path, not the modified path produced by the style.
168 SkPaint paint;
169 paint.setStyle(SkPaint::kStroke_Style);
170 paint.setStrokeWidth(1);
171 GrStyle style(paint);
Brian Salomon9f004942020-02-25 09:14:08 -0500172 test_path(reporter, create_concave_path, createPR, kExpectedResources, true,
173 GrAAType::kCoverage, style);
Brian Osmanf6f7cf62017-09-25 16:49:55 -0400174}