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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Test.h"
|
|
|
|
|
|
|
|
#include "SkPath.h"
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
#include "GrClip.h"
|
|
|
|
#include "GrContext.h"
|
|
|
|
#include "GrContextPriv.h"
|
|
|
|
#include "GrResourceCache.h"
|
2017-09-26 18:05:19 +00:00
|
|
|
#include "GrSoftwarePathRenderer.h"
|
2017-09-25 20:49:55 +00:00
|
|
|
#include "effects/GrPorterDuffXferProcessor.h"
|
|
|
|
#include "ops/GrTessellatingPathRenderer.h"
|
|
|
|
|
|
|
|
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,
|
2017-09-26 18:05:19 +00:00
|
|
|
GrAAType aaType,
|
|
|
|
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,
|
|
|
|
aaType,
|
|
|
|
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,
|
2017-09-25 20:49:55 +00:00
|
|
|
GrAAType aaType = GrAAType::kNone,
|
|
|
|
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
|
|
|
|
ctx->setResourceCacheLimits(100, 1000000);
|
2018-01-16 20:07:54 +00:00
|
|
|
GrResourceCache* cache = ctx->contextPriv().getResourceCache();
|
2017-09-25 20:49:55 +00:00
|
|
|
|
|
|
|
sk_sp<GrRenderTargetContext> rtc(ctx->makeDeferredRenderTargetContext(
|
|
|
|
SkBackingFit::kApprox, 800, 800, kRGBA_8888_GrPixelConfig, nullptr, 0,
|
2017-10-30 17:41:26 +00:00
|
|
|
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
|
2017-09-26 18:05:19 +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);
|
2017-10-04 18:45:04 +00:00
|
|
|
test_path(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) {
|
2018-01-08 18:40:32 +00:00
|
|
|
return new GrSoftwarePathRenderer(ctx->contextPriv().proxyProvider(), true);
|
2017-09-26 18:05:19 +00:00
|
|
|
};
|
2017-09-25 20:49:55 +00:00
|
|
|
|
2017-10-04 18:45:04 +00:00
|
|
|
// Software path renderer creates a mask texture, but also renders with a non-AA rect, which
|
|
|
|
// refs the quad index buffer.
|
|
|
|
const int kExpectedResources = 2;
|
|
|
|
|
|
|
|
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);
|
2017-10-04 18:45:04 +00:00
|
|
|
test_path(reporter, create_concave_path, createPR, kExpectedResources, GrAAType::kCoverage,
|
|
|
|
style);
|
2017-09-25 20:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|