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"
|
|
|
|
#include "include/gpu/GrContext.h"
|
|
|
|
#include "src/gpu/GrClip.h"
|
|
|
|
#include "src/gpu/GrContextPriv.h"
|
|
|
|
#include "src/gpu/GrResourceCache.h"
|
|
|
|
#include "src/gpu/GrSoftwarePathRenderer.h"
|
|
|
|
#include "src/gpu/GrStyle.h"
|
|
|
|
#include "src/gpu/effects/GrPorterDuffXferProcessor.h"
|
2019-06-03 20:46:19 +00:00
|
|
|
#include "src/gpu/geometry/GrShape.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/ops/GrTessellatingPathRenderer.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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_path(GrContext* ctx,
|
|
|
|
GrRenderTargetContext* renderTargetContext,
|
|
|
|
const SkPath& path,
|
|
|
|
GrPathRenderer* pr,
|
2019-06-24 00:07:38 +00:00
|
|
|
GrAAType aaType,
|
2017-09-26 18:05:19 +00:00
|
|
|
const GrStyle& style) {
|
2017-09-25 20:49:55 +00:00
|
|
|
GrPaint paint;
|
|
|
|
paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
|
|
|
|
|
|
|
|
GrNoClip noClip;
|
|
|
|
SkIRect clipConservativeBounds = SkIRect::MakeWH(renderTargetContext->width(),
|
|
|
|
renderTargetContext->height());
|
|
|
|
GrShape shape(path, style);
|
|
|
|
if (shape.style().applies()) {
|
|
|
|
shape = shape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, 1.0f);
|
|
|
|
}
|
|
|
|
SkMatrix matrix = SkMatrix::I();
|
|
|
|
GrPathRenderer::DrawPathArgs args{ctx,
|
|
|
|
std::move(paint),
|
|
|
|
&GrUserStencilSettings::kUnused,
|
|
|
|
renderTargetContext,
|
|
|
|
&noClip,
|
|
|
|
&clipConservativeBounds,
|
|
|
|
&matrix,
|
|
|
|
&shape,
|
2019-06-24 00:07:38 +00:00
|
|
|
aaType,
|
2017-09-25 20:49:55 +00:00
|
|
|
false};
|
|
|
|
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,
|
2017-09-26 18:05:19 +00:00
|
|
|
std::function<GrPathRenderer*(GrContext*)> createPathRenderer,
|
2017-10-04 18:45:04 +00:00
|
|
|
int expected,
|
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)) {
|
|
|
|
sk_sp<GrContext> ctx = GrContext::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
|
2019-09-03 14:29:20 +00:00
|
|
|
ctx->setResourceCacheLimit(8000000);
|
2019-02-04 18:26:26 +00:00
|
|
|
GrResourceCache* cache = ctx->priv().getResourceCache();
|
2017-09-25 20:49:55 +00:00
|
|
|
|
2020-01-08 13:30:58 +00:00
|
|
|
auto rtc = ctx->priv().makeDeferredRenderTargetContext(
|
|
|
|
SkBackingFit::kApprox, 800, 800, GrColorType::kRGBA_8888, nullptr, 1, GrMipMapped::kNo,
|
|
|
|
kTopLeft_GrSurfaceOrigin);
|
2017-09-25 20:49:55 +00:00
|
|
|
if (!rtc) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-26 18:05:19 +00:00
|
|
|
sk_sp<GrPathRenderer> pathRenderer(createPathRenderer(ctx.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
|
2019-06-24 00:07:38 +00:00
|
|
|
draw_path(ctx.get(), rtc.get(), path, pathRenderer.get(), aaType, style);
|
2017-09-25 20:49:55 +00:00
|
|
|
ctx->flush();
|
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));
|
2017-09-25 20:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test that deleting the original path invalidates the VBs cached by the tessellating path renderer
|
2017-11-15 20:48:03 +00:00
|
|
|
DEF_GPUTEST(TessellatingPathRendererCacheTest, reporter, /* options */) {
|
2017-09-26 18:05:19 +00:00
|
|
|
auto createPR = [](GrContext*) {
|
|
|
|
return new GrTessellatingPathRenderer();
|
|
|
|
};
|
|
|
|
|
2017-10-04 18:45:04 +00:00
|
|
|
// Tessellating path renderer creates a single vertex buffer for non-AA paths. No other
|
|
|
|
// resources should be created.
|
|
|
|
const int kExpectedResources = 1;
|
|
|
|
|
|
|
|
test_path(reporter, create_concave_path, createPR, kExpectedResources);
|
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);
|
2019-03-13 06:22:01 +00:00
|
|
|
test_path(
|
2019-06-24 00:07:38 +00:00
|
|
|
reporter, create_concave_path, createPR, kExpectedResources, 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 */) {
|
2017-09-26 18:05:19 +00:00
|
|
|
auto createPR = [](GrContext* ctx) {
|
2019-02-04 18:26:26 +00:00
|
|
|
return new GrSoftwarePathRenderer(ctx->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
|
|
|
|
// only contains a single quad so GrFillRectOp doesn't need to use the shared index buffer.
|
|
|
|
const int kExpectedResources = 1;
|
2017-10-04 18:45:04 +00:00
|
|
|
|
2019-06-24 00:07:38 +00:00
|
|
|
test_path(reporter, create_concave_path, createPR, kExpectedResources, 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);
|
2019-06-24 00:07:38 +00:00
|
|
|
test_path(reporter, create_concave_path, createPR, kExpectedResources, GrAAType::kCoverage,
|
2017-10-04 18:45:04 +00:00
|
|
|
style);
|
2017-09-25 20:49:55 +00:00
|
|
|
}
|