2017-09-25 20:49:55 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tests/Test.h"
|
2017-09-25 20:49:55 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPath.h"
|
2020-07-17 19:40:13 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
|
|
|
#include "include/gpu/GrRecordingContext.h"
|
2020-10-14 15:23:11 +00:00
|
|
|
#include "src/gpu/GrDirectContextPriv.h"
|
2020-07-17 19:40:13 +00:00
|
|
|
#include "src/gpu/GrRecordingContextPriv.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrResourceCache.h"
|
|
|
|
#include "src/gpu/GrStyle.h"
|
|
|
|
#include "src/gpu/effects/GrPorterDuffXferProcessor.h"
|
2020-04-17 20:21:37 +00:00
|
|
|
#include "src/gpu/geometry/GrStyledShape.h"
|
2021-08-17 15:57:31 +00:00
|
|
|
#include "src/gpu/ops/SoftwarePathRenderer.h"
|
2021-08-19 15:12:48 +00:00
|
|
|
#include "src/gpu/ops/TriangulatingPathRenderer.h"
|
2021-07-28 19:13:20 +00:00
|
|
|
#include "src/gpu/v1/SurfaceDrawContext_v1.h"
|
2017-09-25 20:49:55 +00:00
|
|
|
|
|
|
|
static SkPath create_concave_path() {
|
|
|
|
SkPath path;
|
|
|
|
path.moveTo(100, 0);
|
|
|
|
path.lineTo(200, 200);
|
|
|
|
path.lineTo(100, 150);
|
|
|
|
path.lineTo(0, 200);
|
|
|
|
path.close();
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2020-07-17 19:40:13 +00:00
|
|
|
static void draw_path(GrRecordingContext* rContext,
|
2021-07-28 19:13:20 +00:00
|
|
|
skgpu::v1::SurfaceDrawContext* sdc,
|
2017-09-25 20:49:55 +00:00
|
|
|
const SkPath& path,
|
2021-08-19 16:30:12 +00:00
|
|
|
skgpu::v1::PathRenderer* pr,
|
2019-06-24 00:07:38 +00:00
|
|
|
GrAAType aaType,
|
2020-02-25 14:14:08 +00:00
|
|
|
const GrStyle& style,
|
|
|
|
float scaleX = 1.f) {
|
2017-09-25 20:49:55 +00:00
|
|
|
GrPaint paint;
|
|
|
|
paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
|
|
|
|
|
2021-07-28 19:13:20 +00:00
|
|
|
SkIRect clipConservativeBounds = SkIRect::MakeWH(sdc->width(),
|
|
|
|
sdc->height());
|
2020-04-17 20:21:37 +00:00
|
|
|
GrStyledShape shape(path, style);
|
2017-09-25 20:49:55 +00:00
|
|
|
if (shape.style().applies()) {
|
|
|
|
shape = shape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, 1.0f);
|
|
|
|
}
|
|
|
|
SkMatrix matrix = SkMatrix::I();
|
2020-02-25 14:14:08 +00:00
|
|
|
matrix.setScaleX(scaleX);
|
2021-08-19 16:30:12 +00:00
|
|
|
skgpu::v1::PathRenderer::DrawPathArgs args{rContext,
|
|
|
|
std::move(paint),
|
|
|
|
&GrUserStencilSettings::kUnused,
|
|
|
|
sdc,
|
|
|
|
nullptr,
|
|
|
|
&clipConservativeBounds,
|
|
|
|
&matrix,
|
|
|
|
&shape,
|
|
|
|
aaType,
|
|
|
|
false};
|
2017-09-25 20:49:55 +00:00
|
|
|
pr->drawPath(args);
|
|
|
|
}
|
|
|
|
|
2017-09-26 18:05:19 +00:00
|
|
|
static bool cache_non_scratch_resources_equals(GrResourceCache* cache, int expected) {
|
|
|
|
#if GR_CACHE_STATS
|
|
|
|
GrResourceCache::Stats stats;
|
|
|
|
cache->getStats(&stats);
|
|
|
|
return (stats.fTotal - stats.fScratch) == expected;
|
|
|
|
#else
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-09-25 20:49:55 +00:00
|
|
|
static void test_path(skiatest::Reporter* reporter,
|
|
|
|
std::function<SkPath(void)> createPath,
|
2021-08-19 16:30:12 +00:00
|
|
|
std::function<skgpu::v1::PathRenderer*(GrRecordingContext*)> makePathRenderer,
|
2017-10-04 18:45:04 +00:00
|
|
|
int expected,
|
2020-02-25 14:14:08 +00:00
|
|
|
bool checkListeners,
|
2019-06-24 00:07:38 +00:00
|
|
|
GrAAType aaType = GrAAType::kNone,
|
2017-09-25 20:49:55 +00:00
|
|
|
GrStyle style = GrStyle(SkStrokeRec::kFill_InitStyle)) {
|
2020-07-17 19:40:13 +00:00
|
|
|
sk_sp<GrDirectContext> dContext = GrDirectContext::MakeMock(nullptr);
|
2017-10-04 18:45:04 +00:00
|
|
|
// The cache needs to be big enough that nothing gets flushed, or our expectations can be wrong
|
2020-07-17 19:40:13 +00:00
|
|
|
dContext->setResourceCacheLimit(8000000);
|
|
|
|
GrResourceCache* cache = dContext->priv().getResourceCache();
|
2017-09-25 20:49:55 +00:00
|
|
|
|
2021-07-28 19:13:20 +00:00
|
|
|
auto sdc = skgpu::v1::SurfaceDrawContext::Make(
|
2021-04-19 23:27:09 +00:00
|
|
|
dContext.get(), GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {800, 800},
|
|
|
|
SkSurfaceProps(), 1, GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
|
2021-06-16 16:05:54 +00:00
|
|
|
if (!sdc) {
|
2017-09-25 20:49:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-19 16:30:12 +00:00
|
|
|
sk_sp<skgpu::v1::PathRenderer> pathRenderer(makePathRenderer(dContext.get()));
|
2017-09-25 20:49:55 +00:00
|
|
|
SkPath path = createPath();
|
|
|
|
|
|
|
|
// Initially, cache only has the render target context
|
2017-09-26 18:05:19 +00:00
|
|
|
REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, 0));
|
2017-09-25 20:49:55 +00:00
|
|
|
|
|
|
|
// Draw the path, check that new resource count matches expectations
|
2021-06-16 16:05:54 +00:00
|
|
|
draw_path(dContext.get(), sdc.get(), path, pathRenderer.get(), aaType, style);
|
2020-07-17 19:40:13 +00:00
|
|
|
dContext->flushAndSubmit();
|
2017-10-04 18:45:04 +00:00
|
|
|
REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, expected));
|
2017-09-25 20:49:55 +00:00
|
|
|
|
|
|
|
// Nothing should be purgeable yet
|
|
|
|
cache->purgeAsNeeded();
|
2017-10-04 18:45:04 +00:00
|
|
|
REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, expected));
|
2017-09-25 20:49:55 +00:00
|
|
|
|
2017-10-04 18:45:04 +00:00
|
|
|
// Reset the path to change the GenID, which should invalidate one resource in the cache.
|
|
|
|
// Some path renderers may leave other unique-keyed resources in the cache, though.
|
2017-09-25 20:49:55 +00:00
|
|
|
path.reset();
|
|
|
|
cache->purgeAsNeeded();
|
2017-10-04 18:45:04 +00:00
|
|
|
REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, expected - 1));
|
2020-02-25 14:14:08 +00:00
|
|
|
|
|
|
|
if (!checkListeners) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that purging the cache of masks also removes listeners from the path.
|
|
|
|
path = createPath();
|
|
|
|
REPORTER_ASSERT(reporter, SkPathPriv::GenIDChangeListenersCount(path) == 0);
|
|
|
|
for (int i = 0; i < 20; ++i) {
|
|
|
|
float scaleX = 1 + ((float)i + 1)/20.f;
|
2021-06-16 16:05:54 +00:00
|
|
|
draw_path(dContext.get(), sdc.get(), path, pathRenderer.get(), aaType, style, scaleX);
|
2020-02-25 14:14:08 +00:00
|
|
|
}
|
2020-07-17 19:40:13 +00:00
|
|
|
dContext->flushAndSubmit();
|
2020-02-25 14:14:08 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkPathPriv::GenIDChangeListenersCount(path) == 20);
|
2021-06-10 00:49:48 +00:00
|
|
|
cache->purgeUnlockedResources();
|
2020-02-25 14:14:08 +00:00
|
|
|
// The listeners don't actually purge until we try to add another one.
|
2021-06-16 16:05:54 +00:00
|
|
|
draw_path(dContext.get(), sdc.get(), path, pathRenderer.get(), aaType, style);
|
2020-02-25 14:14:08 +00:00
|
|
|
REPORTER_ASSERT(reporter, SkPathPriv::GenIDChangeListenersCount(path) == 1);
|
2017-09-25 20:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test that deleting the original path invalidates the VBs cached by the tessellating path renderer
|
2020-03-25 22:18:16 +00:00
|
|
|
DEF_GPUTEST(TriangulatingPathRendererCacheTest, reporter, /* options */) {
|
2020-07-17 19:40:13 +00:00
|
|
|
auto createPR = [](GrRecordingContext*) {
|
2021-08-19 15:12:48 +00:00
|
|
|
return new skgpu::v1::TriangulatingPathRenderer();
|
2017-09-26 18:05:19 +00:00
|
|
|
};
|
|
|
|
|
2020-03-25 22:18:16 +00:00
|
|
|
// Triangulating path renderer creates a single vertex buffer for non-AA paths. No other
|
2017-10-04 18:45:04 +00:00
|
|
|
// resources should be created.
|
|
|
|
const int kExpectedResources = 1;
|
|
|
|
|
2020-02-25 14:14:08 +00:00
|
|
|
test_path(reporter, create_concave_path, createPR, kExpectedResources, false);
|
2017-09-26 18:05:19 +00:00
|
|
|
|
|
|
|
// Test with a style that alters the path geometry. This needs to attach the invalidation logic
|
|
|
|
// to the original path, not the modified path produced by the style.
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(1);
|
|
|
|
GrStyle style(paint);
|
2020-02-25 14:14:08 +00:00
|
|
|
test_path(reporter, create_concave_path, createPR, kExpectedResources, false, GrAAType::kNone,
|
|
|
|
style);
|
2017-09-26 18:05:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test that deleting the original path invalidates the textures cached by the SW path renderer
|
2017-11-15 20:48:03 +00:00
|
|
|
DEF_GPUTEST(SoftwarePathRendererCacheTest, reporter, /* options */) {
|
2020-07-17 19:40:13 +00:00
|
|
|
auto createPR = [](GrRecordingContext* rContext) {
|
2021-08-17 15:57:31 +00:00
|
|
|
return new skgpu::v1::SoftwarePathRenderer(rContext->priv().proxyProvider(), true);
|
2017-09-26 18:05:19 +00:00
|
|
|
};
|
2017-09-25 20:49:55 +00:00
|
|
|
|
2018-12-10 17:43:36 +00:00
|
|
|
// Software path renderer creates a mask texture and renders with a non-AA rect, but the flush
|
2021-09-01 20:31:34 +00:00
|
|
|
// only contains a single quad so FillRectOp doesn't need to use the shared index buffer.
|
2018-12-10 17:43:36 +00:00
|
|
|
const int kExpectedResources = 1;
|
2017-10-04 18:45:04 +00:00
|
|
|
|
2020-02-25 14:14:08 +00:00
|
|
|
test_path(reporter, create_concave_path, createPR, kExpectedResources, true,
|
|
|
|
GrAAType::kCoverage);
|
2017-09-25 20:49:55 +00:00
|
|
|
|
2017-09-26 18:05:19 +00:00
|
|
|
// Test with a style that alters the path geometry. This needs to attach the invalidation logic
|
|
|
|
// to the original path, not the modified path produced by the style.
|
2017-09-25 20:49:55 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
paint.setStrokeWidth(1);
|
|
|
|
GrStyle style(paint);
|
2020-02-25 14:14:08 +00:00
|
|
|
test_path(reporter, create_concave_path, createPR, kExpectedResources, true,
|
|
|
|
GrAAType::kCoverage, style);
|
2017-09-25 20:49:55 +00:00
|
|
|
}
|