GrCCPRTest.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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 "include/core/SkTypes.h"
  8. #include "tests/Test.h"
  9. #include "include/core/SkMatrix.h"
  10. #include "include/core/SkRect.h"
  11. #include "include/gpu/GrTexture.h"
  12. #include "include/gpu/mock/GrMockTypes.h"
  13. #include "include/private/GrRecordingContext.h"
  14. #include "src/core/SkExchange.h"
  15. #include "src/core/SkPathPriv.h"
  16. #include "src/gpu/GrClip.h"
  17. #include "src/gpu/GrContextPriv.h"
  18. #include "src/gpu/GrDrawingManager.h"
  19. #include "src/gpu/GrPaint.h"
  20. #include "src/gpu/GrPathRenderer.h"
  21. #include "src/gpu/GrRecordingContextPriv.h"
  22. #include "src/gpu/GrRenderTargetContext.h"
  23. #include "src/gpu/GrRenderTargetContextPriv.h"
  24. #include "src/gpu/ccpr/GrCCPathCache.h"
  25. #include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
  26. #include "src/gpu/geometry/GrShape.h"
  27. #include "tools/ToolUtils.h"
  28. #include <cmath>
  29. static constexpr int kCanvasSize = 100;
  30. enum class DoCoverageCount { kNo = false, kYes };
  31. enum class DoStroke { kNo = false, kYes };
  32. class CCPRClip : public GrClip {
  33. public:
  34. CCPRClip(GrCoverageCountingPathRenderer* ccpr, const SkPath& path) : fCCPR(ccpr), fPath(path) {}
  35. private:
  36. bool apply(GrRecordingContext* context, GrRenderTargetContext* rtc, bool useHWAA,
  37. bool hasUserStencilSettings, GrAppliedClip* out, SkRect* bounds) const override {
  38. out->addCoverageFP(fCCPR->makeClipProcessor(rtc->priv().testingOnly_getOpListID(), fPath,
  39. SkIRect::MakeWH(rtc->width(), rtc->height()),
  40. *context->priv().caps()));
  41. return true;
  42. }
  43. bool quickContains(const SkRect&) const final { return false; }
  44. bool isRRect(const SkRect& rtBounds, SkRRect* rr, GrAA*) const final { return false; }
  45. void getConservativeBounds(int width, int height, SkIRect* rect, bool* iior) const final {
  46. rect->set(0, 0, width, height);
  47. if (iior) {
  48. *iior = false;
  49. }
  50. }
  51. GrCoverageCountingPathRenderer* const fCCPR;
  52. const SkPath fPath;
  53. };
  54. class CCPRPathDrawer {
  55. public:
  56. CCPRPathDrawer(sk_sp<GrContext> ctx, skiatest::Reporter* reporter, DoStroke doStroke)
  57. : fCtx(ctx)
  58. , fCCPR(fCtx->priv().drawingManager()->getCoverageCountingPathRenderer())
  59. , fRTC(fCtx->priv().makeDeferredRenderTargetContext(
  60. SkBackingFit::kExact, kCanvasSize, kCanvasSize, GrColorType::kRGBA_8888,
  61. nullptr))
  62. , fDoStroke(DoStroke::kYes == doStroke) {
  63. if (!fCCPR) {
  64. ERRORF(reporter, "ccpr not enabled in GrContext for ccpr tests");
  65. }
  66. if (!fRTC) {
  67. ERRORF(reporter, "failed to create GrRenderTargetContext for ccpr tests");
  68. }
  69. }
  70. GrContext* ctx() const { return fCtx.get(); }
  71. GrCoverageCountingPathRenderer* ccpr() const { return fCCPR; }
  72. bool valid() const { return fCCPR && fRTC; }
  73. void clear() const { fRTC->clear(nullptr, SK_PMColor4fTRANSPARENT,
  74. GrRenderTargetContext::CanClearFullscreen::kYes); }
  75. void destroyGrContext() {
  76. SkASSERT(fRTC->unique());
  77. SkASSERT(fCtx->unique());
  78. fRTC.reset();
  79. fCCPR = nullptr;
  80. fCtx.reset();
  81. }
  82. void drawPath(const SkPath& path, const SkMatrix& matrix = SkMatrix::I()) const {
  83. SkASSERT(this->valid());
  84. GrPaint paint;
  85. paint.setColor4f({ 0, 1, 0, 1 });
  86. GrNoClip noClip;
  87. SkIRect clipBounds = SkIRect::MakeWH(kCanvasSize, kCanvasSize);
  88. GrShape shape;
  89. if (!fDoStroke) {
  90. shape = GrShape(path);
  91. } else {
  92. // Use hairlines for now, since they are the only stroke type that doesn't require a
  93. // rigid-body transform. The CCPR stroke code makes no distinction between hairlines
  94. // and regular strokes other than how it decides the device-space stroke width.
  95. SkStrokeRec stroke(SkStrokeRec::kHairline_InitStyle);
  96. stroke.setStrokeParams(SkPaint::kRound_Cap, SkPaint::kMiter_Join, 4);
  97. shape = GrShape(path, GrStyle(stroke, nullptr));
  98. }
  99. fCCPR->testingOnly_drawPathDirectly({
  100. fCtx.get(), std::move(paint), &GrUserStencilSettings::kUnused, fRTC.get(), &noClip,
  101. &clipBounds, &matrix, &shape, GrAAType::kCoverage, false});
  102. }
  103. void clipFullscreenRect(SkPath clipPath, SkPMColor4f color = { 0, 1, 0, 1 }) {
  104. SkASSERT(this->valid());
  105. GrPaint paint;
  106. paint.setColor4f(color);
  107. fRTC->drawRect(CCPRClip(fCCPR, clipPath), std::move(paint), GrAA::kYes, SkMatrix::I(),
  108. SkRect::MakeIWH(kCanvasSize, kCanvasSize));
  109. }
  110. void flush() const {
  111. SkASSERT(this->valid());
  112. fCtx->flush();
  113. }
  114. private:
  115. sk_sp<GrContext> fCtx;
  116. GrCoverageCountingPathRenderer* fCCPR;
  117. sk_sp<GrRenderTargetContext> fRTC;
  118. const bool fDoStroke;
  119. };
  120. class CCPRTest {
  121. public:
  122. void run(skiatest::Reporter* reporter, DoCoverageCount doCoverageCount, DoStroke doStroke) {
  123. GrMockOptions mockOptions;
  124. mockOptions.fInstanceAttribSupport = true;
  125. mockOptions.fHalfFloatVertexAttributeSupport = true;
  126. mockOptions.fMapBufferFlags = GrCaps::kCanMap_MapFlag;
  127. mockOptions.fConfigOptions[(int)GrColorType::kAlpha_F16].fRenderability =
  128. GrMockOptions::ConfigOptions::Renderability::kNonMSAA;
  129. mockOptions.fConfigOptions[(int)GrColorType::kAlpha_F16].fTexturable = true;
  130. mockOptions.fConfigOptions[(int)GrColorType::kAlpha_8].fRenderability =
  131. GrMockOptions::ConfigOptions::Renderability::kMSAA;
  132. mockOptions.fConfigOptions[(int)GrColorType::kAlpha_8].fTexturable = true;
  133. mockOptions.fGeometryShaderSupport = true;
  134. mockOptions.fIntegerSupport = true;
  135. mockOptions.fFlatInterpolationSupport = true;
  136. GrContextOptions ctxOptions;
  137. ctxOptions.fDisableCoverageCountingPaths = (DoCoverageCount::kNo == doCoverageCount);
  138. ctxOptions.fAllowPathMaskCaching = false;
  139. ctxOptions.fGpuPathRenderers = GpuPathRenderers::kCoverageCounting;
  140. this->customizeOptions(&mockOptions, &ctxOptions);
  141. sk_sp<GrContext> mockContext = GrContext::MakeMock(&mockOptions, ctxOptions);
  142. if (!mockContext) {
  143. ERRORF(reporter, "could not create mock context");
  144. return;
  145. }
  146. if (!mockContext->unique()) {
  147. ERRORF(reporter, "mock context is not unique");
  148. return;
  149. }
  150. CCPRPathDrawer ccpr(skstd::exchange(mockContext, nullptr), reporter, doStroke);
  151. if (!ccpr.valid()) {
  152. return;
  153. }
  154. fPath.moveTo(0, 0);
  155. fPath.cubicTo(50, 50, 0, 50, 50, 0);
  156. this->onRun(reporter, ccpr);
  157. }
  158. virtual ~CCPRTest() {}
  159. protected:
  160. virtual void customizeOptions(GrMockOptions*, GrContextOptions*) {}
  161. virtual void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) = 0;
  162. SkPath fPath;
  163. };
  164. #define DEF_CCPR_TEST(name) \
  165. DEF_GPUTEST(name, reporter, /* options */) { \
  166. name test; \
  167. test.run(reporter, DoCoverageCount::kYes, DoStroke::kNo); \
  168. test.run(reporter, DoCoverageCount::kYes, DoStroke::kYes); \
  169. test.run(reporter, DoCoverageCount::kNo, DoStroke::kNo); \
  170. /* FIXME: test.run(reporter, (DoCoverageCount::kNo, DoStroke::kYes) once supported. */ \
  171. }
  172. class CCPR_cleanup : public CCPRTest {
  173. void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) override {
  174. REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
  175. // Ensure paths get unreffed.
  176. for (int i = 0; i < 10; ++i) {
  177. ccpr.drawPath(fPath);
  178. }
  179. REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath));
  180. ccpr.flush();
  181. REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
  182. // Ensure clip paths get unreffed.
  183. for (int i = 0; i < 10; ++i) {
  184. ccpr.clipFullscreenRect(fPath);
  185. }
  186. REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath));
  187. ccpr.flush();
  188. REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
  189. // Ensure paths get unreffed when we delete the context without flushing.
  190. for (int i = 0; i < 10; ++i) {
  191. ccpr.drawPath(fPath);
  192. ccpr.clipFullscreenRect(fPath);
  193. }
  194. REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath));
  195. ccpr.destroyGrContext();
  196. REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
  197. }
  198. };
  199. DEF_CCPR_TEST(CCPR_cleanup)
  200. class CCPR_cleanupWithTexAllocFail : public CCPR_cleanup {
  201. void customizeOptions(GrMockOptions* mockOptions, GrContextOptions*) override {
  202. mockOptions->fFailTextureAllocations = true;
  203. }
  204. };
  205. DEF_CCPR_TEST(CCPR_cleanupWithTexAllocFail)
  206. class CCPR_unregisterCulledOps : public CCPRTest {
  207. void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) override {
  208. REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
  209. // Ensure Ops get unregistered from CCPR when culled early.
  210. ccpr.drawPath(fPath);
  211. REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath));
  212. ccpr.clear(); // Clear should delete the CCPR Op.
  213. REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
  214. ccpr.flush(); // Should not crash (DrawPathsOp should have unregistered itself).
  215. // Ensure Op unregisters work when we delete the context without flushing.
  216. ccpr.drawPath(fPath);
  217. REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath));
  218. ccpr.clear(); // Clear should delete the CCPR DrawPathsOp.
  219. REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
  220. ccpr.destroyGrContext(); // Should not crash (DrawPathsOp should have unregistered itself).
  221. }
  222. };
  223. DEF_CCPR_TEST(CCPR_unregisterCulledOps)
  224. class CCPR_parseEmptyPath : public CCPRTest {
  225. void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) override {
  226. REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
  227. // Make a path large enough that ccpr chooses to crop it by the RT bounds, and ends up with
  228. // an empty path.
  229. SkPath largeOutsidePath;
  230. largeOutsidePath.moveTo(-1e30f, -1e30f);
  231. largeOutsidePath.lineTo(-1e30f, +1e30f);
  232. largeOutsidePath.lineTo(-1e10f, +1e30f);
  233. ccpr.drawPath(largeOutsidePath);
  234. // Normally an empty path is culled before reaching ccpr, however we use a back door for
  235. // testing so this path will make it.
  236. SkPath emptyPath;
  237. SkASSERT(emptyPath.isEmpty());
  238. ccpr.drawPath(emptyPath);
  239. // This is the test. It will exercise various internal asserts and verify we do not crash.
  240. ccpr.flush();
  241. // Now try again with clips.
  242. ccpr.clipFullscreenRect(largeOutsidePath);
  243. ccpr.clipFullscreenRect(emptyPath);
  244. ccpr.flush();
  245. // ... and both.
  246. ccpr.drawPath(largeOutsidePath);
  247. ccpr.clipFullscreenRect(largeOutsidePath);
  248. ccpr.drawPath(emptyPath);
  249. ccpr.clipFullscreenRect(emptyPath);
  250. ccpr.flush();
  251. }
  252. };
  253. DEF_CCPR_TEST(CCPR_parseEmptyPath)
  254. static int get_mock_texture_id(const GrTexture* texture) {
  255. const GrBackendTexture& backingTexture = texture->getBackendTexture();
  256. SkASSERT(GrBackendApi::kMock == backingTexture.backend());
  257. if (!backingTexture.isValid()) {
  258. return 0;
  259. }
  260. GrMockTextureInfo info;
  261. backingTexture.getMockTextureInfo(&info);
  262. return info.fID;
  263. }
  264. // Base class for cache path unit tests.
  265. class CCPRCacheTest : public CCPRTest {
  266. protected:
  267. // Registers as an onFlush callback in order to snag the CCPR per-flush resources and note the
  268. // texture IDs.
  269. class RecordLastMockAtlasIDs : public GrOnFlushCallbackObject {
  270. public:
  271. RecordLastMockAtlasIDs(sk_sp<GrCoverageCountingPathRenderer> ccpr) : fCCPR(ccpr) {}
  272. int lastCopyAtlasID() const { return fLastCopyAtlasID; }
  273. int lastRenderedAtlasID() const { return fLastRenderedAtlasID; }
  274. void preFlush(GrOnFlushResourceProvider*, const uint32_t* opListIDs, int numOpListIDs,
  275. SkTArray<sk_sp<GrRenderTargetContext>>* out) override {
  276. fLastRenderedAtlasID = fLastCopyAtlasID = 0;
  277. const GrCCPerFlushResources* resources = fCCPR->testingOnly_getCurrentFlushResources();
  278. if (!resources) {
  279. return;
  280. }
  281. if (const GrTexture* tex = resources->testingOnly_frontCopyAtlasTexture()) {
  282. fLastCopyAtlasID = get_mock_texture_id(tex);
  283. }
  284. if (const GrTexture* tex = resources->testingOnly_frontRenderedAtlasTexture()) {
  285. fLastRenderedAtlasID = get_mock_texture_id(tex);
  286. }
  287. }
  288. void postFlush(GrDeferredUploadToken, const uint32_t*, int) override {}
  289. private:
  290. sk_sp<GrCoverageCountingPathRenderer> fCCPR;
  291. int fLastCopyAtlasID = 0;
  292. int fLastRenderedAtlasID = 0;
  293. };
  294. CCPRCacheTest() {
  295. static constexpr int primes[11] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
  296. SkRandom rand;
  297. for (size_t i = 0; i < SK_ARRAY_COUNT(fPaths); ++i) {
  298. int numPts = rand.nextRangeU(GrShape::kMaxKeyFromDataVerbCnt + 1,
  299. GrShape::kMaxKeyFromDataVerbCnt * 2);
  300. int step;
  301. do {
  302. step = primes[rand.nextU() % SK_ARRAY_COUNT(primes)];
  303. } while (step == numPts);
  304. fPaths[i] = ToolUtils::make_star(SkRect::MakeLTRB(0, 0, 1, 1), numPts, step);
  305. }
  306. }
  307. void drawPathsAndFlush(CCPRPathDrawer& ccpr, const SkMatrix& m) {
  308. this->drawPathsAndFlush(ccpr, &m, 1);
  309. }
  310. void drawPathsAndFlush(CCPRPathDrawer& ccpr, const SkMatrix* matrices, int numMatrices) {
  311. // Draw all the paths.
  312. for (size_t i = 0; i < SK_ARRAY_COUNT(fPaths); ++i) {
  313. ccpr.drawPath(fPaths[i], matrices[i % numMatrices]);
  314. }
  315. // Re-draw a few paths, to test the case where a cache entry is hit more than once in a
  316. // single flush.
  317. SkRandom rand;
  318. int duplicateIndices[10];
  319. for (size_t i = 0; i < SK_ARRAY_COUNT(duplicateIndices); ++i) {
  320. duplicateIndices[i] = rand.nextULessThan(SK_ARRAY_COUNT(fPaths));
  321. }
  322. for (size_t i = 0; i < SK_ARRAY_COUNT(duplicateIndices); ++i) {
  323. for (size_t j = 0; j <= i; ++j) {
  324. int idx = duplicateIndices[j];
  325. ccpr.drawPath(fPaths[idx], matrices[idx % numMatrices]);
  326. }
  327. }
  328. ccpr.flush();
  329. }
  330. private:
  331. void customizeOptions(GrMockOptions*, GrContextOptions* ctxOptions) override {
  332. ctxOptions->fAllowPathMaskCaching = true;
  333. }
  334. void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) final {
  335. RecordLastMockAtlasIDs atlasIDRecorder(sk_ref_sp(ccpr.ccpr()));
  336. ccpr.ctx()->priv().addOnFlushCallbackObject(&atlasIDRecorder);
  337. this->onRun(reporter, ccpr, atlasIDRecorder);
  338. ccpr.ctx()->priv().testingOnly_flushAndRemoveOnFlushCallbackObject(&atlasIDRecorder);
  339. }
  340. virtual void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr,
  341. const RecordLastMockAtlasIDs&) = 0;
  342. protected:
  343. SkPath fPaths[350];
  344. };
  345. // Ensures ccpr always reuses the same atlas texture in the animation use case.
  346. class CCPR_cache_animationAtlasReuse : public CCPRCacheTest {
  347. void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr,
  348. const RecordLastMockAtlasIDs& atlasIDRecorder) override {
  349. SkMatrix m = SkMatrix::MakeTrans(kCanvasSize/2, kCanvasSize/2);
  350. m.preScale(80, 80);
  351. m.preTranslate(-.5,-.5);
  352. this->drawPathsAndFlush(ccpr, m);
  353. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
  354. REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
  355. const int atlasID = atlasIDRecorder.lastRenderedAtlasID();
  356. // Ensures we always reuse the same atlas texture in the animation use case.
  357. for (int i = 0; i < 12; ++i) {
  358. // 59 is prime, so we will hit every integer modulo 360 before repeating.
  359. m.preRotate(59, .5, .5);
  360. // Go twice. Paths have to get drawn twice with the same matrix before we cache their
  361. // atlas. This makes sure that on the subsequent draw, after an atlas has been cached
  362. // and is then invalidated since the matrix will change, that the same underlying
  363. // texture object is still reused for the next atlas.
  364. for (int j = 0; j < 2; ++j) {
  365. this->drawPathsAndFlush(ccpr, m);
  366. // Nothing should be copied to an 8-bit atlas after just two draws.
  367. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
  368. REPORTER_ASSERT(reporter, atlasIDRecorder.lastRenderedAtlasID() == atlasID);
  369. }
  370. }
  371. // Do the last draw again. (On draw 3 they should get copied to an 8-bit atlas.)
  372. this->drawPathsAndFlush(ccpr, m);
  373. REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID());
  374. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
  375. // Now double-check that everything continues to hit the cache as expected when the matrix
  376. // doesn't change.
  377. for (int i = 0; i < 10; ++i) {
  378. this->drawPathsAndFlush(ccpr, m);
  379. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
  380. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
  381. }
  382. }
  383. };
  384. DEF_CCPR_TEST(CCPR_cache_animationAtlasReuse)
  385. class CCPR_cache_recycleEntries : public CCPRCacheTest {
  386. void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr,
  387. const RecordLastMockAtlasIDs& atlasIDRecorder) override {
  388. SkMatrix m = SkMatrix::MakeTrans(kCanvasSize/2, kCanvasSize/2);
  389. m.preScale(80, 80);
  390. m.preTranslate(-.5,-.5);
  391. auto cache = ccpr.ccpr()->testingOnly_getPathCache();
  392. REPORTER_ASSERT(reporter, cache);
  393. const auto& lru = cache->testingOnly_getLRU();
  394. SkTArray<const void*> expectedPtrs;
  395. // Ensures we always reuse the same atlas texture in the animation use case.
  396. for (int i = 0; i < 5; ++i) {
  397. // 59 is prime, so we will hit every integer modulo 360 before repeating.
  398. m.preRotate(59, .5, .5);
  399. // Go twice. Paths have to get drawn twice with the same matrix before we cache their
  400. // atlas.
  401. for (int j = 0; j < 2; ++j) {
  402. this->drawPathsAndFlush(ccpr, m);
  403. // Nothing should be copied to an 8-bit atlas after just two draws.
  404. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
  405. REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
  406. }
  407. int idx = 0;
  408. for (const GrCCPathCacheEntry* entry : lru) {
  409. if (0 == i) {
  410. expectedPtrs.push_back(entry);
  411. } else {
  412. // The same pointer should have been recycled for the new matrix.
  413. REPORTER_ASSERT(reporter, entry == expectedPtrs[idx]);
  414. }
  415. ++idx;
  416. }
  417. }
  418. }
  419. };
  420. DEF_CCPR_TEST(CCPR_cache_recycleEntries)
  421. // Ensures mostly-visible paths get their full mask cached.
  422. class CCPR_cache_mostlyVisible : public CCPRCacheTest {
  423. void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr,
  424. const RecordLastMockAtlasIDs& atlasIDRecorder) override {
  425. SkMatrix matrices[3] = {
  426. SkMatrix::MakeScale(kCanvasSize/2, kCanvasSize/2), // Fully visible.
  427. SkMatrix::MakeScale(kCanvasSize * 1.25, kCanvasSize * 1.25), // Mostly visible.
  428. SkMatrix::MakeScale(kCanvasSize * 1.5, kCanvasSize * 1.5), // Mostly NOT visible.
  429. };
  430. for (int i = 0; i < 10; ++i) {
  431. this->drawPathsAndFlush(ccpr, matrices, 3);
  432. if (2 == i) {
  433. // The mostly-visible paths should still get cached.
  434. REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID());
  435. } else {
  436. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
  437. }
  438. // Ensure mostly NOT-visible paths never get cached.
  439. REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
  440. }
  441. // Clear the path cache.
  442. this->drawPathsAndFlush(ccpr, SkMatrix::I());
  443. // Now only draw the fully/mostly visible ones.
  444. for (int i = 0; i < 2; ++i) {
  445. this->drawPathsAndFlush(ccpr, matrices, 2);
  446. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
  447. REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
  448. }
  449. // On draw 3 they should get copied to an 8-bit atlas.
  450. this->drawPathsAndFlush(ccpr, matrices, 2);
  451. REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID());
  452. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
  453. for (int i = 0; i < 10; ++i) {
  454. this->drawPathsAndFlush(ccpr, matrices, 2);
  455. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
  456. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
  457. }
  458. // Draw a different part of the path to ensure the full mask was cached.
  459. matrices[1].postTranslate(SkScalarFloorToInt(kCanvasSize * -.25f),
  460. SkScalarFloorToInt(kCanvasSize * -.25f));
  461. for (int i = 0; i < 10; ++i) {
  462. this->drawPathsAndFlush(ccpr, matrices, 2);
  463. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
  464. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
  465. }
  466. }
  467. };
  468. DEF_CCPR_TEST(CCPR_cache_mostlyVisible)
  469. // Ensures GrContext::performDeferredCleanup works.
  470. class CCPR_cache_deferredCleanup : public CCPRCacheTest {
  471. void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr,
  472. const RecordLastMockAtlasIDs& atlasIDRecorder) override {
  473. SkMatrix m = SkMatrix::MakeScale(20, 20);
  474. int lastRenderedAtlasID = 0;
  475. for (int i = 0; i < 5; ++i) {
  476. this->drawPathsAndFlush(ccpr, m);
  477. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
  478. REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
  479. int renderedAtlasID = atlasIDRecorder.lastRenderedAtlasID();
  480. REPORTER_ASSERT(reporter, renderedAtlasID != lastRenderedAtlasID);
  481. lastRenderedAtlasID = renderedAtlasID;
  482. this->drawPathsAndFlush(ccpr, m);
  483. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
  484. REPORTER_ASSERT(reporter, lastRenderedAtlasID == atlasIDRecorder.lastRenderedAtlasID());
  485. // On draw 3 they should get copied to an 8-bit atlas.
  486. this->drawPathsAndFlush(ccpr, m);
  487. REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID());
  488. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
  489. for (int i = 0; i < 10; ++i) {
  490. this->drawPathsAndFlush(ccpr, m);
  491. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
  492. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
  493. }
  494. ccpr.ctx()->performDeferredCleanup(std::chrono::milliseconds(0));
  495. }
  496. }
  497. };
  498. DEF_CCPR_TEST(CCPR_cache_deferredCleanup)
  499. // Verifies the cache/hash table internals.
  500. class CCPR_cache_hashTable : public CCPRCacheTest {
  501. void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr,
  502. const RecordLastMockAtlasIDs& atlasIDRecorder) override {
  503. using CoverageType = GrCCAtlas::CoverageType;
  504. SkMatrix m = SkMatrix::MakeScale(20, 20);
  505. for (int i = 0; i < 5; ++i) {
  506. this->drawPathsAndFlush(ccpr, m);
  507. if (2 == i) {
  508. REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID());
  509. } else {
  510. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
  511. }
  512. if (i < 2) {
  513. REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
  514. } else {
  515. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
  516. }
  517. auto cache = ccpr.ccpr()->testingOnly_getPathCache();
  518. REPORTER_ASSERT(reporter, cache);
  519. const auto& hash = cache->testingOnly_getHashTable();
  520. const auto& lru = cache->testingOnly_getLRU();
  521. int count = 0;
  522. for (GrCCPathCacheEntry* entry : lru) {
  523. auto* node = hash.find(entry->cacheKey());
  524. REPORTER_ASSERT(reporter, node);
  525. REPORTER_ASSERT(reporter, node->entry() == entry);
  526. REPORTER_ASSERT(reporter, 0 == entry->testingOnly_peekOnFlushRefCnt());
  527. REPORTER_ASSERT(reporter, entry->unique());
  528. if (0 == i) {
  529. REPORTER_ASSERT(reporter, !entry->cachedAtlas());
  530. } else {
  531. const GrCCCachedAtlas* cachedAtlas = entry->cachedAtlas();
  532. REPORTER_ASSERT(reporter, cachedAtlas);
  533. if (1 == i) {
  534. REPORTER_ASSERT(reporter, ccpr.ccpr()->coverageType()
  535. == cachedAtlas->coverageType());
  536. } else {
  537. REPORTER_ASSERT(reporter, CoverageType::kA8_LiteralCoverage
  538. == cachedAtlas->coverageType());
  539. }
  540. REPORTER_ASSERT(reporter, cachedAtlas->textureKey().isValid());
  541. // The actual proxy should not be held past the end of a flush.
  542. REPORTER_ASSERT(reporter, !cachedAtlas->getOnFlushProxy());
  543. REPORTER_ASSERT(reporter, 0 == cachedAtlas->testingOnly_peekOnFlushRefCnt());
  544. }
  545. ++count;
  546. }
  547. REPORTER_ASSERT(reporter, hash.count() == count);
  548. }
  549. }
  550. };
  551. DEF_CCPR_TEST(CCPR_cache_hashTable)
  552. // Ensures paths get cached even when using a sporadic flushing pattern and drawing out of order
  553. // (a la Chrome tiles).
  554. class CCPR_cache_multiFlush : public CCPRCacheTest {
  555. void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr,
  556. const RecordLastMockAtlasIDs& atlasIDRecorder) override {
  557. static constexpr int kNumPaths = SK_ARRAY_COUNT(fPaths);
  558. static constexpr int kBigPrimes[] = {
  559. 9323, 11059, 22993, 38749, 45127, 53147, 64853, 77969, 83269, 99989};
  560. SkRandom rand;
  561. SkMatrix m = SkMatrix::I();
  562. for (size_t i = 0; i < SK_ARRAY_COUNT(kBigPrimes); ++i) {
  563. int prime = kBigPrimes[i];
  564. int endPathIdx = (int)rand.nextULessThan(kNumPaths);
  565. int pathIdx = endPathIdx;
  566. int nextFlush = rand.nextRangeU(1, 47);
  567. for (int j = 0; j < kNumPaths; ++j) {
  568. pathIdx = (pathIdx + prime) % kNumPaths;
  569. int repeat = rand.nextRangeU(1, 3);
  570. for (int k = 0; k < repeat; ++k) {
  571. ccpr.drawPath(fPaths[pathIdx], m);
  572. }
  573. if (nextFlush == j) {
  574. ccpr.flush();
  575. // The paths are small enough that we should never copy to an A8 atlas.
  576. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
  577. if (i < 2) {
  578. REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
  579. } else {
  580. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
  581. }
  582. nextFlush = SkTMin(j + (int)rand.nextRangeU(1, 29), kNumPaths - 1);
  583. }
  584. }
  585. SkASSERT(endPathIdx == pathIdx % kNumPaths);
  586. }
  587. }
  588. };
  589. DEF_CCPR_TEST(CCPR_cache_multiFlush)
  590. // Ensures a path drawn over mutiple tiles gets cached.
  591. class CCPR_cache_multiTileCache : public CCPRCacheTest {
  592. void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr,
  593. const RecordLastMockAtlasIDs& atlasIDRecorder) override {
  594. // Make sure a path drawn over 9 tiles gets cached (1 tile out of 9 is >10% visibility).
  595. const SkMatrix m0 = SkMatrix::MakeScale(kCanvasSize*3, kCanvasSize*3);
  596. const SkPath p0 = fPaths[0];
  597. for (int i = 0; i < 9; ++i) {
  598. static constexpr int kRowOrder[9] = {0,1,1,0,2,2,2,1,0};
  599. static constexpr int kColumnOrder[9] = {0,0,1,1,0,1,2,2,2};
  600. SkMatrix tileM = m0;
  601. tileM.postTranslate(-kCanvasSize * kColumnOrder[i], -kCanvasSize * kRowOrder[i]);
  602. ccpr.drawPath(p0, tileM);
  603. ccpr.flush();
  604. if (i < 5) {
  605. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
  606. REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
  607. } else if (5 == i) {
  608. REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID());
  609. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
  610. } else {
  611. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
  612. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
  613. }
  614. }
  615. // Now make sure paths don't get cached when visibility is <10% for every draw (12 tiles).
  616. const SkMatrix m1 = SkMatrix::MakeScale(kCanvasSize*4, kCanvasSize*3);
  617. const SkPath p1 = fPaths[1];
  618. for (int row = 0; row < 3; ++row) {
  619. for (int col = 0; col < 4; ++col) {
  620. SkMatrix tileM = m1;
  621. tileM.postTranslate(-kCanvasSize * col, -kCanvasSize * row);
  622. ccpr.drawPath(p1, tileM);
  623. ccpr.flush();
  624. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
  625. REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
  626. }
  627. }
  628. // Double-check the cache is still intact.
  629. ccpr.drawPath(p0, m0);
  630. ccpr.flush();
  631. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
  632. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
  633. ccpr.drawPath(p1, m1);
  634. ccpr.flush();
  635. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
  636. REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastRenderedAtlasID());
  637. }
  638. };
  639. DEF_CCPR_TEST(CCPR_cache_multiTileCache)
  640. // This test exercises CCPR's cache capabilities by drawing many paths with two different
  641. // transformation matrices. We then vary the matrices independently by whole and partial pixels,
  642. // and verify the caching behaved as expected.
  643. class CCPR_cache_partialInvalidate : public CCPRCacheTest {
  644. void customizeOptions(GrMockOptions*, GrContextOptions* ctxOptions) override {
  645. ctxOptions->fAllowPathMaskCaching = true;
  646. }
  647. static constexpr int kPathSize = 4;
  648. void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr,
  649. const RecordLastMockAtlasIDs& atlasIDRecorder) override {
  650. SkMatrix matrices[2] = {
  651. SkMatrix::MakeTrans(5, 5),
  652. SkMatrix::MakeTrans(kCanvasSize - kPathSize - 5, kCanvasSize - kPathSize - 5)
  653. };
  654. matrices[0].preScale(kPathSize, kPathSize);
  655. matrices[1].preScale(kPathSize, kPathSize);
  656. int firstAtlasID = 0;
  657. for (int iterIdx = 0; iterIdx < 4*3*2; ++iterIdx) {
  658. this->drawPathsAndFlush(ccpr, matrices, 2);
  659. if (0 == iterIdx) {
  660. // First iteration: just note the ID of the stashed atlas and continue.
  661. firstAtlasID = atlasIDRecorder.lastRenderedAtlasID();
  662. REPORTER_ASSERT(reporter, 0 != firstAtlasID);
  663. continue;
  664. }
  665. int testIdx = (iterIdx/2) % 3;
  666. int repetitionIdx = iterIdx % 2;
  667. switch (testIdx) {
  668. case 0:
  669. if (0 == repetitionIdx) {
  670. // This is the big test. New paths were drawn twice last round. On hit 2
  671. // (last time), 'firstAtlasID' was cached as a 16-bit atlas. Now, on hit 3,
  672. // these paths should be copied out of 'firstAtlasID', and into an A8 atlas.
  673. // THEN: we should recycle 'firstAtlasID' and reuse that same texture to
  674. // render the new masks.
  675. REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID());
  676. REPORTER_ASSERT(reporter,
  677. atlasIDRecorder.lastRenderedAtlasID() == firstAtlasID);
  678. } else {
  679. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
  680. // This is hit 2 for the new masks. Next time they will be copied to an A8
  681. // atlas.
  682. REPORTER_ASSERT(reporter,
  683. atlasIDRecorder.lastRenderedAtlasID() == firstAtlasID);
  684. }
  685. if (1 == repetitionIdx) {
  686. // Integer translates: all path masks stay valid.
  687. matrices[0].preTranslate(-1, -1);
  688. matrices[1].preTranslate(1, 1);
  689. }
  690. break;
  691. case 1:
  692. if (0 == repetitionIdx) {
  693. // New paths were drawn twice last round. The third hit (now) they should be
  694. // copied to an A8 atlas.
  695. REPORTER_ASSERT(reporter, 0 != atlasIDRecorder.lastCopyAtlasID());
  696. } else {
  697. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
  698. }
  699. // This draw should have gotten 100% cache hits; we only did integer translates
  700. // last time (or none if it was the first flush). Therefore, everything should
  701. // have been cached.
  702. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastRenderedAtlasID());
  703. if (1 == repetitionIdx) {
  704. // Invalidate even path masks.
  705. matrices[0].preTranslate(1.6f, 1.4f);
  706. }
  707. break;
  708. case 2:
  709. // No new masks to copy from last time; it had 100% cache hits.
  710. REPORTER_ASSERT(reporter, 0 == atlasIDRecorder.lastCopyAtlasID());
  711. // Even path masks were invalidated last iteration by a subpixel translate.
  712. // They should have been re-rendered this time in the original 'firstAtlasID'
  713. // texture.
  714. REPORTER_ASSERT(reporter,
  715. atlasIDRecorder.lastRenderedAtlasID() == firstAtlasID);
  716. if (1 == repetitionIdx) {
  717. // Invalidate odd path masks.
  718. matrices[1].preTranslate(-1.4f, -1.6f);
  719. }
  720. break;
  721. }
  722. }
  723. }
  724. };
  725. DEF_CCPR_TEST(CCPR_cache_partialInvalidate)
  726. class CCPR_unrefPerOpListPathsBeforeOps : public CCPRTest {
  727. void onRun(skiatest::Reporter* reporter, CCPRPathDrawer& ccpr) override {
  728. REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
  729. for (int i = 0; i < 10000; ++i) {
  730. // Draw enough paths to make the arena allocator hit the heap.
  731. ccpr.drawPath(fPath);
  732. }
  733. // Unref the GrCCPerOpListPaths object.
  734. auto perOpListPathsMap = ccpr.ccpr()->detachPendingPaths();
  735. perOpListPathsMap.clear();
  736. // Now delete the Op and all its draws.
  737. REPORTER_ASSERT(reporter, !SkPathPriv::TestingOnly_unique(fPath));
  738. ccpr.flush();
  739. REPORTER_ASSERT(reporter, SkPathPriv::TestingOnly_unique(fPath));
  740. }
  741. };
  742. DEF_CCPR_TEST(CCPR_unrefPerOpListPathsBeforeOps)
  743. class CCPRRenderingTest {
  744. public:
  745. void run(skiatest::Reporter* reporter, GrContext* ctx, DoStroke doStroke) const {
  746. if (auto ccpr = ctx->priv().drawingManager()->getCoverageCountingPathRenderer()) {
  747. if (DoStroke::kYes == doStroke &&
  748. GrCCAtlas::CoverageType::kA8_Multisample == ccpr->coverageType()) {
  749. return; // Stroking is not yet supported for multisample.
  750. }
  751. CCPRPathDrawer drawer(sk_ref_sp(ctx), reporter, doStroke);
  752. if (!drawer.valid()) {
  753. return;
  754. }
  755. this->onRun(reporter, drawer);
  756. }
  757. }
  758. virtual ~CCPRRenderingTest() {}
  759. protected:
  760. virtual void onRun(skiatest::Reporter* reporter, const CCPRPathDrawer& ccpr) const = 0;
  761. };
  762. #define DEF_CCPR_RENDERING_TEST(name) \
  763. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(name, reporter, ctxInfo) { \
  764. name test; \
  765. test.run(reporter, ctxInfo.grContext(), DoStroke::kNo); \
  766. test.run(reporter, ctxInfo.grContext(), DoStroke::kYes); \
  767. }
  768. class CCPR_busyPath : public CCPRRenderingTest {
  769. void onRun(skiatest::Reporter* reporter, const CCPRPathDrawer& ccpr) const override {
  770. static constexpr int kNumBusyVerbs = 1 << 17;
  771. ccpr.clear();
  772. SkPath busyPath;
  773. busyPath.moveTo(0, 0); // top left
  774. busyPath.lineTo(kCanvasSize, kCanvasSize); // bottom right
  775. for (int i = 2; i < kNumBusyVerbs; ++i) {
  776. float offset = i * ((float)kCanvasSize / kNumBusyVerbs);
  777. busyPath.lineTo(kCanvasSize - offset, kCanvasSize + offset); // offscreen
  778. }
  779. ccpr.drawPath(busyPath);
  780. ccpr.flush(); // If this doesn't crash, the test passed.
  781. // If it does, maybe fiddle with fMaxInstancesPerDrawArraysWithoutCrashing in
  782. // your platform's GrGLCaps.
  783. }
  784. };
  785. DEF_CCPR_RENDERING_TEST(CCPR_busyPath)