Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 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 | |
| 8 | #ifndef GrCoverageCountingPathRenderer_DEFINED |
| 9 | #define GrCoverageCountingPathRenderer_DEFINED |
| 10 | |
Brian Salomon | 653f42f | 2018-07-10 10:07:31 -0400 | [diff] [blame] | 11 | #include <map> |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrOnFlushResourceProvider.h" |
| 13 | #include "src/gpu/GrPathRenderer.h" |
| 14 | #include "src/gpu/GrRenderTargetOpList.h" |
| 15 | #include "src/gpu/ccpr/GrCCPerFlushResources.h" |
Ben Wagner | 729a23f | 2019-05-17 16:29:34 -0400 | [diff] [blame^] | 16 | #include "src/gpu/ccpr/GrCCPerOpListPaths.h" |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 17 | |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 18 | class GrCCDrawPathsOp; |
Chris Dalton | a2b5b64 | 2018-06-24 13:08:57 -0600 | [diff] [blame] | 19 | class GrCCPathCache; |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 20 | |
| 21 | /** |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 22 | * This is a path renderer that draws antialiased paths by counting coverage in an offscreen |
Chris Dalton | 5ba36ba | 2018-05-09 01:08:38 -0600 | [diff] [blame] | 23 | * buffer. (See GrCCCoverageProcessor, GrCCPathProcessor.) |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 24 | * |
| 25 | * It also serves as the per-render-target tracker for pending path draws, and at the start of |
| 26 | * flush, it compiles GPU buffers and renders a "coverage count atlas" for the upcoming paths. |
| 27 | */ |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 28 | class GrCoverageCountingPathRenderer : public GrPathRenderer, public GrOnFlushCallbackObject { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 29 | public: |
| 30 | static bool IsSupported(const GrCaps&); |
Chris Dalton | a2b5b64 | 2018-06-24 13:08:57 -0600 | [diff] [blame] | 31 | |
| 32 | enum class AllowCaching : bool { |
| 33 | kNo = false, |
| 34 | kYes = true |
| 35 | }; |
| 36 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 37 | static sk_sp<GrCoverageCountingPathRenderer> CreateIfSupported(const GrCaps&, AllowCaching, |
| 38 | uint32_t contextUniqueID); |
Chris Dalton | a2b5b64 | 2018-06-24 13:08:57 -0600 | [diff] [blame] | 39 | |
Robert Phillips | 774168e | 2018-05-31 12:43:27 -0400 | [diff] [blame] | 40 | using PendingPathsMap = std::map<uint32_t, sk_sp<GrCCPerOpListPaths>>; |
Chris Dalton | a71305c | 2018-05-23 12:00:07 -0600 | [diff] [blame] | 41 | |
| 42 | // In DDL mode, Ganesh needs to be able to move the pending GrCCPerOpListPaths to the DDL object |
| 43 | // (detachPendingPaths) and then return them upon replay (mergePendingPaths). |
| 44 | PendingPathsMap detachPendingPaths() { return std::move(fPendingPaths); } |
| 45 | |
Robert Phillips | 774168e | 2018-05-31 12:43:27 -0400 | [diff] [blame] | 46 | void mergePendingPaths(const PendingPathsMap& paths) { |
| 47 | #ifdef SK_DEBUG |
Chris Dalton | a71305c | 2018-05-23 12:00:07 -0600 | [diff] [blame] | 48 | // Ensure there are no duplicate opList IDs between the incoming path map and ours. |
Robert Phillips | 774168e | 2018-05-31 12:43:27 -0400 | [diff] [blame] | 49 | // This should always be true since opList IDs are globally unique and these are coming |
| 50 | // from different DDL recordings. |
| 51 | for (const auto& it : paths) { |
| 52 | SkASSERT(!fPendingPaths.count(it.first)); |
| 53 | } |
| 54 | #endif |
| 55 | |
| 56 | fPendingPaths.insert(paths.begin(), paths.end()); |
Chris Dalton | a71305c | 2018-05-23 12:00:07 -0600 | [diff] [blame] | 57 | } |
| 58 | |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 59 | std::unique_ptr<GrFragmentProcessor> makeClipProcessor(uint32_t oplistID, |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 60 | const SkPath& deviceSpacePath, |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 61 | const SkIRect& accessRect, int rtWidth, |
| 62 | int rtHeight, const GrCaps&); |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 63 | |
| 64 | // GrOnFlushCallbackObject overrides. |
| 65 | void preFlush(GrOnFlushResourceProvider*, const uint32_t* opListIDs, int numOpListIDs, |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 66 | SkTArray<sk_sp<GrRenderTargetContext>>* out) override; |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 67 | void postFlush(GrDeferredUploadToken, const uint32_t* opListIDs, int numOpListIDs) override; |
| 68 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 69 | void purgeCacheEntriesOlderThan(GrProxyProvider*, const GrStdSteadyClock::time_point&); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 70 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 71 | // If a path spans more pixels than this, we need to crop it or else analytic AA can run out of |
| 72 | // fp32 precision. |
| 73 | static constexpr float kPathCropThreshold = 1 << 16; |
| 74 | |
| 75 | static void CropPath(const SkPath&, const SkIRect& cropbox, SkPath* out); |
| 76 | |
Chris Dalton | 82de18f | 2018-09-12 17:24:09 -0600 | [diff] [blame] | 77 | // Maximum inflation of path bounds due to stroking (from width, miter, caps). Strokes wider |
| 78 | // than this will be converted to fill paths and drawn by the CCPR filler instead. |
| 79 | static constexpr float kMaxBoundsInflationFromStroke = 4096; |
| 80 | |
| 81 | static float GetStrokeDevWidth(const SkMatrix&, const SkStrokeRec&, |
| 82 | float* inflationRadius = nullptr); |
| 83 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 84 | private: |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 85 | GrCoverageCountingPathRenderer(AllowCaching, uint32_t contextUniqueID); |
Chris Dalton | a2b5b64 | 2018-06-24 13:08:57 -0600 | [diff] [blame] | 86 | |
| 87 | // GrPathRenderer overrides. |
| 88 | StencilSupport onGetStencilSupport(const GrShape&) const override { |
| 89 | return GrPathRenderer::kNoSupport_StencilSupport; |
| 90 | } |
| 91 | CanDrawPath onCanDrawPath(const CanDrawPathArgs&) const override; |
| 92 | bool onDrawPath(const DrawPathArgs&) override; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 93 | |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 94 | GrCCPerOpListPaths* lookupPendingPaths(uint32_t opListID); |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 95 | void recordOp(std::unique_ptr<GrCCDrawPathsOp>, const DrawPathArgs&); |
Chris Dalton | c1e5963 | 2017-09-05 00:30:07 -0600 | [diff] [blame] | 96 | |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 97 | // fPendingPaths holds the GrCCPerOpListPaths objects that have already been created, but not |
| 98 | // flushed, and those that are still being created. All GrCCPerOpListPaths objects will first |
| 99 | // reside in fPendingPaths, then be moved to fFlushingPaths during preFlush(). |
Chris Dalton | a71305c | 2018-05-23 12:00:07 -0600 | [diff] [blame] | 100 | PendingPathsMap fPendingPaths; |
Chris Dalton | a32a3c3 | 2017-12-05 10:05:21 -0700 | [diff] [blame] | 101 | |
Chris Dalton | d7e2227 | 2018-05-23 10:17:17 -0600 | [diff] [blame] | 102 | // fFlushingPaths holds the GrCCPerOpListPaths objects that are currently being flushed. |
| 103 | // (It will only contain elements when fFlushing is true.) |
Robert Phillips | 774168e | 2018-05-31 12:43:27 -0400 | [diff] [blame] | 104 | SkSTArray<4, sk_sp<GrCCPerOpListPaths>> fFlushingPaths; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 105 | |
Chris Dalton | a2b5b64 | 2018-06-24 13:08:57 -0600 | [diff] [blame] | 106 | std::unique_ptr<GrCCPathCache> fPathCache; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 107 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 108 | SkDEBUGCODE(bool fFlushing = false); |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 109 | |
| 110 | public: |
| 111 | void testingOnly_drawPathDirectly(const DrawPathArgs&); |
| 112 | const GrCCPerFlushResources* testingOnly_getCurrentFlushResources(); |
| 113 | const GrCCPathCache* testingOnly_getPathCache() const; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | #endif |