blob: 44961d459c197cea9f7942af6c0b4a5fc3ad3912 [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"
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 Ludwig663afe52019-06-03 16:46:19 -040018#include "src/gpu/geometry/GrShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#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
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 Osmane9242ca2017-09-26 14:05:19 -040036 const GrStyle& style) {
Brian Osmanf6f7cf62017-09-25 16:49:55 -040037 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,
Chris Dalton6ce447a2019-06-23 18:07:38 -060056 aaType,
Brian Osmanf6f7cf62017-09-25 16:49:55 -040057 false};
58 pr->drawPath(args);
59}
60
Brian Osmane9242ca2017-09-26 14:05:19 -040061static 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 Osmanf6f7cf62017-09-25 16:49:55 -040071static void test_path(skiatest::Reporter* reporter,
72 std::function<SkPath(void)> createPath,
Brian Osmane9242ca2017-09-26 14:05:19 -040073 std::function<GrPathRenderer*(GrContext*)> createPathRenderer,
Brian Osman5fcd3912017-10-04 14:45:04 -040074 int expected,
Chris Dalton6ce447a2019-06-23 18:07:38 -060075 GrAAType aaType = GrAAType::kNone,
Brian Osmanf6f7cf62017-09-25 16:49:55 -040076 GrStyle style = GrStyle(SkStrokeRec::kFill_InitStyle)) {
77 sk_sp<GrContext> ctx = GrContext::MakeMock(nullptr);
Brian Osman5fcd3912017-10-04 14:45:04 -040078 // The cache needs to be big enough that nothing gets flushed, or our expectations can be wrong
Robert Phillipscf39f372019-09-03 10:29:20 -040079 ctx->setResourceCacheLimit(8000000);
Robert Phillips9da87e02019-02-04 13:26:26 -050080 GrResourceCache* cache = ctx->priv().getResourceCache();
Brian Osmanf6f7cf62017-09-25 16:49:55 -040081
Brian Salomonbf6b9792019-08-21 09:38:10 -040082 auto rtc = ctx->priv().makeDeferredRenderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -040083 SkBackingFit::kApprox, 800, 800, GrColorType::kRGBA_8888, nullptr, 1, GrMipMapped::kNo,
Brian Salomonbf6b9792019-08-21 09:38:10 -040084 kTopLeft_GrSurfaceOrigin);
Brian Osmanf6f7cf62017-09-25 16:49:55 -040085 if (!rtc) {
86 return;
87 }
88
Brian Osmane9242ca2017-09-26 14:05:19 -040089 sk_sp<GrPathRenderer> pathRenderer(createPathRenderer(ctx.get()));
Brian Osmanf6f7cf62017-09-25 16:49:55 -040090 SkPath path = createPath();
91
92 // Initially, cache only has the render target context
Brian Osmane9242ca2017-09-26 14:05:19 -040093 REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, 0));
Brian Osmanf6f7cf62017-09-25 16:49:55 -040094
95 // Draw the path, check that new resource count matches expectations
Chris Dalton6ce447a2019-06-23 18:07:38 -060096 draw_path(ctx.get(), rtc.get(), path, pathRenderer.get(), aaType, style);
Brian Osmanf6f7cf62017-09-25 16:49:55 -040097 ctx->flush();
Brian Osman5fcd3912017-10-04 14:45:04 -040098 REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, expected));
Brian Osmanf6f7cf62017-09-25 16:49:55 -040099
100 // Nothing should be purgeable yet
101 cache->purgeAsNeeded();
Brian Osman5fcd3912017-10-04 14:45:04 -0400102 REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, expected));
Brian Osmanf6f7cf62017-09-25 16:49:55 -0400103
Brian Osman5fcd3912017-10-04 14:45:04 -0400104 // Reset the path to change the GenID, which should invalidate one resource in the cache.
105 // Some path renderers may leave other unique-keyed resources in the cache, though.
Brian Osmanf6f7cf62017-09-25 16:49:55 -0400106 path.reset();
107 cache->purgeAsNeeded();
Brian Osman5fcd3912017-10-04 14:45:04 -0400108 REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, expected - 1));
Brian Osmanf6f7cf62017-09-25 16:49:55 -0400109}
110
111// Test that deleting the original path invalidates the VBs cached by the tessellating path renderer
Brian Salomondcfca432017-11-15 15:48:03 -0500112DEF_GPUTEST(TessellatingPathRendererCacheTest, reporter, /* options */) {
Brian Osmane9242ca2017-09-26 14:05:19 -0400113 auto createPR = [](GrContext*) {
114 return new GrTessellatingPathRenderer();
115 };
Brian Osmanf6f7cf62017-09-25 16:49:55 -0400116
Brian Osman5fcd3912017-10-04 14:45:04 -0400117 // Tessellating path renderer creates a single vertex buffer for non-AA paths. No other
118 // resources should be created.
119 const int kExpectedResources = 1;
120
121 test_path(reporter, create_concave_path, createPR, kExpectedResources);
Brian Osmanf6f7cf62017-09-25 16:49:55 -0400122
Brian Osmane9242ca2017-09-26 14:05:19 -0400123 // Test with a style that alters the path geometry. This needs to attach the invalidation logic
124 // to the original path, not the modified path produced by the style.
Brian Osmanf6f7cf62017-09-25 16:49:55 -0400125 SkPaint paint;
126 paint.setStyle(SkPaint::kStroke_Style);
127 paint.setStrokeWidth(1);
128 GrStyle style(paint);
Chris Dalton09e56892019-03-13 00:22:01 -0600129 test_path(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600130 reporter, create_concave_path, createPR, kExpectedResources, GrAAType::kNone, style);
Brian Osmane9242ca2017-09-26 14:05:19 -0400131}
132
133// Test that deleting the original path invalidates the textures cached by the SW path renderer
Brian Salomondcfca432017-11-15 15:48:03 -0500134DEF_GPUTEST(SoftwarePathRendererCacheTest, reporter, /* options */) {
Brian Osmane9242ca2017-09-26 14:05:19 -0400135 auto createPR = [](GrContext* ctx) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500136 return new GrSoftwarePathRenderer(ctx->priv().proxyProvider(), true);
Brian Osmane9242ca2017-09-26 14:05:19 -0400137 };
138
Michael Ludwig72ab3462018-12-10 12:43:36 -0500139 // Software path renderer creates a mask texture and renders with a non-AA rect, but the flush
140 // only contains a single quad so GrFillRectOp doesn't need to use the shared index buffer.
141 const int kExpectedResources = 1;
Brian Osman5fcd3912017-10-04 14:45:04 -0400142
Chris Dalton6ce447a2019-06-23 18:07:38 -0600143 test_path(reporter, create_concave_path, createPR, kExpectedResources, GrAAType::kCoverage);
Brian Osmane9242ca2017-09-26 14:05:19 -0400144
145 // Test with a style that alters the path geometry. This needs to attach the invalidation logic
146 // to the original path, not the modified path produced by the style.
147 SkPaint paint;
148 paint.setStyle(SkPaint::kStroke_Style);
149 paint.setStrokeWidth(1);
150 GrStyle style(paint);
Chris Dalton6ce447a2019-06-23 18:07:38 -0600151 test_path(reporter, create_concave_path, createPR, kExpectedResources, GrAAType::kCoverage,
Brian Osman5fcd3912017-10-04 14:45:04 -0400152 style);
Brian Osmanf6f7cf62017-09-25 16:49:55 -0400153}