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