blob: ae59b551352293a9f5d99b11cb2ec1a6593c668d [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"
12#include "src/gpu/GrClip.h"
13#include "src/gpu/GrContextPriv.h"
14#include "src/gpu/GrResourceCache.h"
15#include "src/gpu/GrShape.h"
16#include "src/gpu/GrSoftwarePathRenderer.h"
17#include "src/gpu/GrStyle.h"
18#include "src/gpu/effects/GrPorterDuffXferProcessor.h"
19#include "src/gpu/ops/GrTessellatingPathRenderer.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
Chris Dalton09e56892019-03-13 00:22:01 -060031using AATypeFlags = GrPathRenderer::AATypeFlags;
32
Brian Osmanf6f7cf62017-09-25 16:49:55 -040033static void draw_path(GrContext* ctx,
34 GrRenderTargetContext* renderTargetContext,
35 const SkPath& path,
36 GrPathRenderer* pr,
Chris Dalton09e56892019-03-13 00:22:01 -060037 AATypeFlags aaTypeFlags,
Brian Osmane9242ca2017-09-26 14:05:19 -040038 const GrStyle& style) {
Brian Osmanf6f7cf62017-09-25 16:49:55 -040039 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 Dalton09e56892019-03-13 00:22:01 -060058 aaTypeFlags,
Brian Osmanf6f7cf62017-09-25 16:49:55 -040059 false};
60 pr->drawPath(args);
61}
62
Brian Osmane9242ca2017-09-26 14:05:19 -040063static 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 Osmanf6f7cf62017-09-25 16:49:55 -040073static void test_path(skiatest::Reporter* reporter,
74 std::function<SkPath(void)> createPath,
Brian Osmane9242ca2017-09-26 14:05:19 -040075 std::function<GrPathRenderer*(GrContext*)> createPathRenderer,
Brian Osman5fcd3912017-10-04 14:45:04 -040076 int expected,
Chris Dalton09e56892019-03-13 00:22:01 -060077 AATypeFlags aaTypeFlags = AATypeFlags::kNone,
Brian Osmanf6f7cf62017-09-25 16:49:55 -040078 GrStyle style = GrStyle(SkStrokeRec::kFill_InitStyle)) {
79 sk_sp<GrContext> ctx = GrContext::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
Brian Salomon57b04302018-01-30 15:43:49 -050081 ctx->setResourceCacheLimits(100, 8000000);
Robert Phillips9da87e02019-02-04 13:26:26 -050082 GrResourceCache* cache = ctx->priv().getResourceCache();
Brian Osmanf6f7cf62017-09-25 16:49:55 -040083
Greg Daniel4065d452018-11-16 15:43:41 -050084 const GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -050085 ctx->priv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Greg Daniel4065d452018-11-16 15:43:41 -050086
Robert Phillips9da87e02019-02-04 13:26:26 -050087 sk_sp<GrRenderTargetContext> rtc(ctx->priv().makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -050088 format, SkBackingFit::kApprox, 800, 800, kRGBA_8888_GrPixelConfig, nullptr, 1,
89 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin));
Brian Osmanf6f7cf62017-09-25 16:49:55 -040090 if (!rtc) {
91 return;
92 }
93
Brian Osmane9242ca2017-09-26 14:05:19 -040094 sk_sp<GrPathRenderer> pathRenderer(createPathRenderer(ctx.get()));
Brian Osmanf6f7cf62017-09-25 16:49:55 -040095 SkPath path = createPath();
96
97 // Initially, cache only has the render target context
Brian Osmane9242ca2017-09-26 14:05:19 -040098 REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, 0));
Brian Osmanf6f7cf62017-09-25 16:49:55 -040099
100 // Draw the path, check that new resource count matches expectations
Chris Dalton09e56892019-03-13 00:22:01 -0600101 draw_path(ctx.get(), rtc.get(), path, pathRenderer.get(), aaTypeFlags, style);
Brian Osmanf6f7cf62017-09-25 16:49:55 -0400102 ctx->flush();
Brian Osman5fcd3912017-10-04 14:45:04 -0400103 REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, expected));
Brian Osmanf6f7cf62017-09-25 16:49:55 -0400104
105 // Nothing should be purgeable yet
106 cache->purgeAsNeeded();
Brian Osman5fcd3912017-10-04 14:45:04 -0400107 REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, expected));
Brian Osmanf6f7cf62017-09-25 16:49:55 -0400108
Brian Osman5fcd3912017-10-04 14:45:04 -0400109 // 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 Osmanf6f7cf62017-09-25 16:49:55 -0400111 path.reset();
112 cache->purgeAsNeeded();
Brian Osman5fcd3912017-10-04 14:45:04 -0400113 REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, expected - 1));
Brian Osmanf6f7cf62017-09-25 16:49:55 -0400114}
115
116// Test that deleting the original path invalidates the VBs cached by the tessellating path renderer
Brian Salomondcfca432017-11-15 15:48:03 -0500117DEF_GPUTEST(TessellatingPathRendererCacheTest, reporter, /* options */) {
Brian Osmane9242ca2017-09-26 14:05:19 -0400118 auto createPR = [](GrContext*) {
119 return new GrTessellatingPathRenderer();
120 };
Brian Osmanf6f7cf62017-09-25 16:49:55 -0400121
Brian Osman5fcd3912017-10-04 14:45:04 -0400122 // 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 Osmanf6f7cf62017-09-25 16:49:55 -0400127
Brian Osmane9242ca2017-09-26 14:05:19 -0400128 // 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 Osmanf6f7cf62017-09-25 16:49:55 -0400130 SkPaint paint;
131 paint.setStyle(SkPaint::kStroke_Style);
132 paint.setStrokeWidth(1);
133 GrStyle style(paint);
Chris Dalton09e56892019-03-13 00:22:01 -0600134 test_path(
135 reporter, create_concave_path, createPR, kExpectedResources, AATypeFlags::kNone, style);
Brian Osmane9242ca2017-09-26 14:05:19 -0400136}
137
138// Test that deleting the original path invalidates the textures cached by the SW path renderer
Brian Salomondcfca432017-11-15 15:48:03 -0500139DEF_GPUTEST(SoftwarePathRendererCacheTest, reporter, /* options */) {
Brian Osmane9242ca2017-09-26 14:05:19 -0400140 auto createPR = [](GrContext* ctx) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500141 return new GrSoftwarePathRenderer(ctx->priv().proxyProvider(), true);
Brian Osmane9242ca2017-09-26 14:05:19 -0400142 };
143
Michael Ludwig72ab3462018-12-10 12:43:36 -0500144 // 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 Osman5fcd3912017-10-04 14:45:04 -0400147
Chris Dalton09e56892019-03-13 00:22:01 -0600148 test_path(reporter, create_concave_path, createPR, kExpectedResources, AATypeFlags::kCoverage);
Brian Osmane9242ca2017-09-26 14:05:19 -0400149
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 Dalton09e56892019-03-13 00:22:01 -0600156 test_path(reporter, create_concave_path, createPR, kExpectedResources, AATypeFlags::kCoverage,
Brian Osman5fcd3912017-10-04 14:45:04 -0400157 style);
Brian Osmanf6f7cf62017-09-25 16:49:55 -0400158}