GrCoverageCountingPathRenderer.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #ifndef GrCoverageCountingPathRenderer_DEFINED
  8. #define GrCoverageCountingPathRenderer_DEFINED
  9. #include <map>
  10. #include "src/gpu/GrOnFlushResourceProvider.h"
  11. #include "src/gpu/GrPathRenderer.h"
  12. #include "src/gpu/GrRenderTargetOpList.h"
  13. #include "src/gpu/ccpr/GrCCPerFlushResources.h"
  14. #include "src/gpu/ccpr/GrCCPerOpListPaths.h"
  15. class GrCCDrawPathsOp;
  16. class GrCCPathCache;
  17. /**
  18. * This is a path renderer that draws antialiased paths by counting coverage in an offscreen
  19. * buffer. (See GrCCCoverageProcessor, GrCCPathProcessor.)
  20. *
  21. * It also serves as the per-render-target tracker for pending path draws, and at the start of
  22. * flush, it compiles GPU buffers and renders a "coverage count atlas" for the upcoming paths.
  23. */
  24. class GrCoverageCountingPathRenderer : public GrPathRenderer, public GrOnFlushCallbackObject {
  25. public:
  26. using CoverageType = GrCCAtlas::CoverageType;
  27. static bool IsSupported(const GrCaps&, CoverageType* = nullptr);
  28. enum class AllowCaching : bool {
  29. kNo = false,
  30. kYes = true
  31. };
  32. static sk_sp<GrCoverageCountingPathRenderer> CreateIfSupported(
  33. const GrCaps&, AllowCaching, uint32_t contextUniqueID);
  34. CoverageType coverageType() const { return fCoverageType; }
  35. using PendingPathsMap = std::map<uint32_t, sk_sp<GrCCPerOpListPaths>>;
  36. // In DDL mode, Ganesh needs to be able to move the pending GrCCPerOpListPaths to the DDL object
  37. // (detachPendingPaths) and then return them upon replay (mergePendingPaths).
  38. PendingPathsMap detachPendingPaths() { return std::move(fPendingPaths); }
  39. void mergePendingPaths(const PendingPathsMap& paths) {
  40. #ifdef SK_DEBUG
  41. // Ensure there are no duplicate opList IDs between the incoming path map and ours.
  42. // This should always be true since opList IDs are globally unique and these are coming
  43. // from different DDL recordings.
  44. for (const auto& it : paths) {
  45. SkASSERT(!fPendingPaths.count(it.first));
  46. }
  47. #endif
  48. fPendingPaths.insert(paths.begin(), paths.end());
  49. }
  50. std::unique_ptr<GrFragmentProcessor> makeClipProcessor(
  51. uint32_t oplistID, const SkPath& deviceSpacePath, const SkIRect& accessRect,
  52. const GrCaps&);
  53. // GrOnFlushCallbackObject overrides.
  54. void preFlush(GrOnFlushResourceProvider*, const uint32_t* opListIDs, int numOpListIDs,
  55. SkTArray<sk_sp<GrRenderTargetContext>>* out) override;
  56. void postFlush(GrDeferredUploadToken, const uint32_t* opListIDs, int numOpListIDs) override;
  57. void purgeCacheEntriesOlderThan(GrProxyProvider*, const GrStdSteadyClock::time_point&);
  58. // If a path spans more pixels than this, we need to crop it or else analytic AA can run out of
  59. // fp32 precision.
  60. static constexpr float kPathCropThreshold = 1 << 16;
  61. static void CropPath(const SkPath&, const SkIRect& cropbox, SkPath* out);
  62. // Maximum inflation of path bounds due to stroking (from width, miter, caps). Strokes wider
  63. // than this will be converted to fill paths and drawn by the CCPR filler instead.
  64. static constexpr float kMaxBoundsInflationFromStroke = 4096;
  65. static float GetStrokeDevWidth(const SkMatrix&, const SkStrokeRec&,
  66. float* inflationRadius = nullptr);
  67. private:
  68. GrCoverageCountingPathRenderer(CoverageType, AllowCaching, uint32_t contextUniqueID);
  69. // GrPathRenderer overrides.
  70. StencilSupport onGetStencilSupport(const GrShape&) const override {
  71. return GrPathRenderer::kNoSupport_StencilSupport;
  72. }
  73. CanDrawPath onCanDrawPath(const CanDrawPathArgs&) const override;
  74. bool onDrawPath(const DrawPathArgs&) override;
  75. GrCCPerOpListPaths* lookupPendingPaths(uint32_t opListID);
  76. void recordOp(std::unique_ptr<GrCCDrawPathsOp>, const DrawPathArgs&);
  77. const CoverageType fCoverageType;
  78. // fPendingPaths holds the GrCCPerOpListPaths objects that have already been created, but not
  79. // flushed, and those that are still being created. All GrCCPerOpListPaths objects will first
  80. // reside in fPendingPaths, then be moved to fFlushingPaths during preFlush().
  81. PendingPathsMap fPendingPaths;
  82. // fFlushingPaths holds the GrCCPerOpListPaths objects that are currently being flushed.
  83. // (It will only contain elements when fFlushing is true.)
  84. SkSTArray<4, sk_sp<GrCCPerOpListPaths>> fFlushingPaths;
  85. std::unique_ptr<GrCCPathCache> fPathCache;
  86. SkDEBUGCODE(bool fFlushing = false);
  87. public:
  88. void testingOnly_drawPathDirectly(const DrawPathArgs&);
  89. const GrCCPerFlushResources* testingOnly_getCurrentFlushResources();
  90. const GrCCPathCache* testingOnly_getPathCache() const;
  91. };
  92. #endif