PathRendererCacheTests.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  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. #include "tests/Test.h"
  8. #include "include/core/SkPath.h"
  9. #include "include/gpu/GrContext.h"
  10. #include "src/gpu/GrClip.h"
  11. #include "src/gpu/GrContextPriv.h"
  12. #include "src/gpu/GrResourceCache.h"
  13. #include "src/gpu/GrSoftwarePathRenderer.h"
  14. #include "src/gpu/GrStyle.h"
  15. #include "src/gpu/effects/GrPorterDuffXferProcessor.h"
  16. #include "src/gpu/geometry/GrShape.h"
  17. #include "src/gpu/ops/GrTessellatingPathRenderer.h"
  18. static SkPath create_concave_path() {
  19. SkPath path;
  20. path.moveTo(100, 0);
  21. path.lineTo(200, 200);
  22. path.lineTo(100, 150);
  23. path.lineTo(0, 200);
  24. path.close();
  25. return path;
  26. }
  27. static void draw_path(GrContext* ctx,
  28. GrRenderTargetContext* renderTargetContext,
  29. const SkPath& path,
  30. GrPathRenderer* pr,
  31. GrAAType aaType,
  32. const GrStyle& style) {
  33. GrPaint paint;
  34. paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
  35. GrNoClip noClip;
  36. SkIRect clipConservativeBounds = SkIRect::MakeWH(renderTargetContext->width(),
  37. renderTargetContext->height());
  38. GrShape shape(path, style);
  39. if (shape.style().applies()) {
  40. shape = shape.applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, 1.0f);
  41. }
  42. SkMatrix matrix = SkMatrix::I();
  43. GrPathRenderer::DrawPathArgs args{ctx,
  44. std::move(paint),
  45. &GrUserStencilSettings::kUnused,
  46. renderTargetContext,
  47. &noClip,
  48. &clipConservativeBounds,
  49. &matrix,
  50. &shape,
  51. aaType,
  52. false};
  53. pr->drawPath(args);
  54. }
  55. static bool cache_non_scratch_resources_equals(GrResourceCache* cache, int expected) {
  56. #if GR_CACHE_STATS
  57. GrResourceCache::Stats stats;
  58. cache->getStats(&stats);
  59. return (stats.fTotal - stats.fScratch) == expected;
  60. #else
  61. return true;
  62. #endif
  63. }
  64. static void test_path(skiatest::Reporter* reporter,
  65. std::function<SkPath(void)> createPath,
  66. std::function<GrPathRenderer*(GrContext*)> createPathRenderer,
  67. int expected,
  68. GrAAType aaType = GrAAType::kNone,
  69. GrStyle style = GrStyle(SkStrokeRec::kFill_InitStyle)) {
  70. sk_sp<GrContext> ctx = GrContext::MakeMock(nullptr);
  71. // The cache needs to be big enough that nothing gets flushed, or our expectations can be wrong
  72. ctx->setResourceCacheLimits(100, 8000000);
  73. GrResourceCache* cache = ctx->priv().getResourceCache();
  74. sk_sp<GrRenderTargetContext> rtc(ctx->priv().makeDeferredRenderTargetContext(
  75. SkBackingFit::kApprox, 800, 800, GrColorType::kRGBA_8888, nullptr, 1, GrMipMapped::kNo,
  76. kTopLeft_GrSurfaceOrigin));
  77. if (!rtc) {
  78. return;
  79. }
  80. sk_sp<GrPathRenderer> pathRenderer(createPathRenderer(ctx.get()));
  81. SkPath path = createPath();
  82. // Initially, cache only has the render target context
  83. REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, 0));
  84. // Draw the path, check that new resource count matches expectations
  85. draw_path(ctx.get(), rtc.get(), path, pathRenderer.get(), aaType, style);
  86. ctx->flush();
  87. REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, expected));
  88. // Nothing should be purgeable yet
  89. cache->purgeAsNeeded();
  90. REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, expected));
  91. // Reset the path to change the GenID, which should invalidate one resource in the cache.
  92. // Some path renderers may leave other unique-keyed resources in the cache, though.
  93. path.reset();
  94. cache->purgeAsNeeded();
  95. REPORTER_ASSERT(reporter, cache_non_scratch_resources_equals(cache, expected - 1));
  96. }
  97. // Test that deleting the original path invalidates the VBs cached by the tessellating path renderer
  98. DEF_GPUTEST(TessellatingPathRendererCacheTest, reporter, /* options */) {
  99. auto createPR = [](GrContext*) {
  100. return new GrTessellatingPathRenderer();
  101. };
  102. // Tessellating path renderer creates a single vertex buffer for non-AA paths. No other
  103. // resources should be created.
  104. const int kExpectedResources = 1;
  105. test_path(reporter, create_concave_path, createPR, kExpectedResources);
  106. // Test with a style that alters the path geometry. This needs to attach the invalidation logic
  107. // to the original path, not the modified path produced by the style.
  108. SkPaint paint;
  109. paint.setStyle(SkPaint::kStroke_Style);
  110. paint.setStrokeWidth(1);
  111. GrStyle style(paint);
  112. test_path(
  113. reporter, create_concave_path, createPR, kExpectedResources, GrAAType::kNone, style);
  114. }
  115. // Test that deleting the original path invalidates the textures cached by the SW path renderer
  116. DEF_GPUTEST(SoftwarePathRendererCacheTest, reporter, /* options */) {
  117. auto createPR = [](GrContext* ctx) {
  118. return new GrSoftwarePathRenderer(ctx->priv().proxyProvider(), true);
  119. };
  120. // Software path renderer creates a mask texture and renders with a non-AA rect, but the flush
  121. // only contains a single quad so GrFillRectOp doesn't need to use the shared index buffer.
  122. const int kExpectedResources = 1;
  123. test_path(reporter, create_concave_path, createPR, kExpectedResources, GrAAType::kCoverage);
  124. // Test with a style that alters the path geometry. This needs to attach the invalidation logic
  125. // to the original path, not the modified path produced by the style.
  126. SkPaint paint;
  127. paint.setStyle(SkPaint::kStroke_Style);
  128. paint.setStrokeWidth(1);
  129. GrStyle style(paint);
  130. test_path(reporter, create_concave_path, createPR, kExpectedResources, GrAAType::kCoverage,
  131. style);
  132. }