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