skia2/tests/PathRendererCacheTests.cpp
Michael Ludwig 7c12e28cf4 Reland "GrClips provided as pointers to GrRTC"
This reverts commit 074414fed5.

Reason for revert: updated to guard against nullptr before calling
quickContains(rrect).

Original change's description:
> Revert "GrClips provided as pointers to GrRTC"
>
> This reverts commit 226b689471.
>
> Reason for revert: Breaks Android roller
>
> Original change's description:
> > GrClips provided as pointers to GrRTC
> >
> > A null clip represents no high-level clipping is necessary (the implicit
> > clip to the render target's logical dimensions is fine).
> >
> > This also removes GrNoClip and GrFixedClip::Disabled() since they are
> > replaced with just nullptr.
> >
> > By allowing nullptr to represent no intended clipping, it makes it easier
> > to require GrClip and GrAppliedClip objects to know about the dimensions
> > of the device. If we required a non-null clip object to represent no
> > clipping, we'd have to have an instance for each device based on its
> > size and that just became cumbersome.
> >
> > Bug: skia:10205
> > Change-Id: Ie30cc71820b92d99356d393a4c98c8677082e761
> > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/290539
> > Commit-Queue: Michael Ludwig <michaelludwig@google.com>
> > Reviewed-by: Brian Salomon <bsalomon@google.com>
>
> TBR=bsalomon@google.com,csmartdalton@google.com,michaelludwig@google.com
>
> Change-Id: I42c4828bcf016ee3d30d5c20b771be96e125817b
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Bug: skia:10205
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/292856
> Reviewed-by: Weston Tracey <westont@google.com>
> Commit-Queue: Weston Tracey <westont@google.com>

TBR=bsalomon@google.com,csmartdalton@google.com,michaelludwig@google.com,westont@google.com

# Not skipping CQ checks because this is a reland.

Bug: skia:10205
Change-Id: I5715a4de3b7c8847b73020dc4937d3816d879803
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/292876
Reviewed-by: Michael Ludwig <michaelludwig@google.com>
Commit-Queue: Michael Ludwig <michaelludwig@google.com>
2020-05-29 15:09:54 +00:00

175 lines
6.9 KiB
C++

/*
* 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 "tests/Test.h"
#include "include/core/SkPath.h"
#include "include/gpu/GrContext.h"
#include "src/gpu/GrContextPriv.h"
#include "src/gpu/GrRenderTargetContext.h"
#include "src/gpu/GrResourceCache.h"
#include "src/gpu/GrSoftwarePathRenderer.h"
#include "src/gpu/GrStyle.h"
#include "src/gpu/effects/GrPorterDuffXferProcessor.h"
#include "src/gpu/geometry/GrStyledShape.h"
#include "src/gpu/ops/GrTriangulatingPathRenderer.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,
GrAAType aaType,
const GrStyle& style,
float scaleX = 1.f) {
GrPaint paint;
paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
SkIRect clipConservativeBounds = SkIRect::MakeWH(renderTargetContext->width(),
renderTargetContext->height());
GrStyledShape shape(path, style);
if (shape.style().applies()) {
shape = shape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, 1.0f);
}
SkMatrix matrix = SkMatrix::I();
matrix.setScaleX(scaleX);
GrPathRenderer::DrawPathArgs args{ctx,
std::move(paint),
&GrUserStencilSettings::kUnused,
renderTargetContext,
nullptr,
&clipConservativeBounds,
&matrix,
&shape,
aaType,
false};
pr->drawPath(args);
}
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
}
static void test_path(skiatest::Reporter* reporter,
std::function<SkPath(void)> createPath,
std::function<GrPathRenderer*(GrContext*)> createPathRenderer,
int expected,
bool checkListeners,
GrAAType aaType = GrAAType::kNone,
GrStyle style = GrStyle(SkStrokeRec::kFill_InitStyle)) {
sk_sp<GrContext> ctx = GrContext::MakeMock(nullptr);
// The cache needs to be big enough that nothing gets flushed, or our expectations can be wrong
ctx->setResourceCacheLimit(8000000);
GrResourceCache* cache = ctx->priv().getResourceCache();
auto rtc = GrRenderTargetContext::Make(
ctx.get(), GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {800, 800}, 1,
GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin);
if (!rtc) {
return;
}
sk_sp<GrPathRenderer> pathRenderer(createPathRenderer(ctx.get()));
SkPath path = createPath();
// Initially, cache only has the render target context
REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, 0));
// Draw the path, check that new resource count matches expectations
draw_path(ctx.get(), rtc.get(), path, pathRenderer.get(), aaType, style);
ctx->flushAndSubmit();
REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, expected));
// Nothing should be purgeable yet
cache->purgeAsNeeded();
REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, expected));
// 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.
path.reset();
cache->purgeAsNeeded();
REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, expected - 1));
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;
draw_path(ctx.get(), rtc.get(), path, pathRenderer.get(), aaType, style, scaleX);
}
ctx->flushAndSubmit();
REPORTER_ASSERT(reporter, SkPathPriv::GenIDChangeListenersCount(path) == 20);
cache->purgeAllUnlocked();
// The listeners don't actually purge until we try to add another one.
draw_path(ctx.get(), rtc.get(), path, pathRenderer.get(), aaType, style);
REPORTER_ASSERT(reporter, SkPathPriv::GenIDChangeListenersCount(path) == 1);
}
// Test that deleting the original path invalidates the VBs cached by the tessellating path renderer
DEF_GPUTEST(TriangulatingPathRendererCacheTest, reporter, /* options */) {
auto createPR = [](GrContext*) {
return new GrTriangulatingPathRenderer();
};
// Triangulating 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, false);
// 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);
test_path(reporter, create_concave_path, createPR, kExpectedResources, false, GrAAType::kNone,
style);
}
// Test that deleting the original path invalidates the textures cached by the SW path renderer
DEF_GPUTEST(SoftwarePathRendererCacheTest, reporter, /* options */) {
auto createPR = [](GrContext* ctx) {
return new GrSoftwarePathRenderer(ctx->priv().proxyProvider(), true);
};
// 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;
test_path(reporter, create_concave_path, createPR, kExpectedResources, true,
GrAAType::kCoverage);
// 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);
test_path(reporter, create_concave_path, createPR, kExpectedResources, true,
GrAAType::kCoverage, style);
}