blob: fe59c20f878693515946972a0f09d75e9411e888 [file] [log] [blame]
Chris Dalton1a325d22017-07-14 15:17:41 -06001/*
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 Salomon653f42f2018-07-10 10:07:31 -040011#include <map>
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#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 Wagner729a23f2019-05-17 16:29:34 -040016#include "src/gpu/ccpr/GrCCPerOpListPaths.h"
Chris Dalton5ba36ba2018-05-09 01:08:38 -060017
Chris Daltond7e22272018-05-23 10:17:17 -060018class GrCCDrawPathsOp;
Chris Daltona2b5b642018-06-24 13:08:57 -060019class GrCCPathCache;
Chris Dalton5ba36ba2018-05-09 01:08:38 -060020
21/**
Chris Dalton1a325d22017-07-14 15:17:41 -060022 * This is a path renderer that draws antialiased paths by counting coverage in an offscreen
Chris Dalton5ba36ba2018-05-09 01:08:38 -060023 * buffer. (See GrCCCoverageProcessor, GrCCPathProcessor.)
Chris Dalton1a325d22017-07-14 15:17:41 -060024 *
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 Dalton383a2ef2018-01-08 17:21:41 -050028class GrCoverageCountingPathRenderer : public GrPathRenderer, public GrOnFlushCallbackObject {
Chris Dalton1a325d22017-07-14 15:17:41 -060029public:
30 static bool IsSupported(const GrCaps&);
Chris Daltona2b5b642018-06-24 13:08:57 -060031
32 enum class AllowCaching : bool {
33 kNo = false,
34 kYes = true
35 };
36
Chris Dalton351e80c2019-01-06 22:51:00 -070037 static sk_sp<GrCoverageCountingPathRenderer> CreateIfSupported(const GrCaps&, AllowCaching,
38 uint32_t contextUniqueID);
Chris Daltona2b5b642018-06-24 13:08:57 -060039
Robert Phillips774168e2018-05-31 12:43:27 -040040 using PendingPathsMap = std::map<uint32_t, sk_sp<GrCCPerOpListPaths>>;
Chris Daltona71305c2018-05-23 12:00:07 -060041
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 Phillips774168e2018-05-31 12:43:27 -040046 void mergePendingPaths(const PendingPathsMap& paths) {
47#ifdef SK_DEBUG
Chris Daltona71305c2018-05-23 12:00:07 -060048 // Ensure there are no duplicate opList IDs between the incoming path map and ours.
Robert Phillips774168e2018-05-31 12:43:27 -040049 // 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 Daltona71305c2018-05-23 12:00:07 -060057 }
58
Chris Dalton4c458b12018-06-16 17:22:59 -060059 std::unique_ptr<GrFragmentProcessor> makeClipProcessor(uint32_t oplistID,
Chris Daltona32a3c32017-12-05 10:05:21 -070060 const SkPath& deviceSpacePath,
Chris Dalton4c458b12018-06-16 17:22:59 -060061 const SkIRect& accessRect, int rtWidth,
62 int rtHeight, const GrCaps&);
Chris Daltona32a3c32017-12-05 10:05:21 -070063
64 // GrOnFlushCallbackObject overrides.
65 void preFlush(GrOnFlushResourceProvider*, const uint32_t* opListIDs, int numOpListIDs,
Chris Dalton9414c962018-06-14 10:14:50 -060066 SkTArray<sk_sp<GrRenderTargetContext>>* out) override;
Chris Daltona32a3c32017-12-05 10:05:21 -070067 void postFlush(GrDeferredUploadToken, const uint32_t* opListIDs, int numOpListIDs) override;
68
Chris Dalton351e80c2019-01-06 22:51:00 -070069 void purgeCacheEntriesOlderThan(GrProxyProvider*, const GrStdSteadyClock::time_point&);
Chris Dalton4da70192018-06-18 09:51:36 -060070
Chris Dalton09a7bb22018-08-31 19:53:15 +080071 // 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 Dalton82de18f2018-09-12 17:24:09 -060077 // 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 Dalton1a325d22017-07-14 15:17:41 -060084private:
Chris Dalton351e80c2019-01-06 22:51:00 -070085 GrCoverageCountingPathRenderer(AllowCaching, uint32_t contextUniqueID);
Chris Daltona2b5b642018-06-24 13:08:57 -060086
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 Dalton1a325d22017-07-14 15:17:41 -060093
Chris Daltond7e22272018-05-23 10:17:17 -060094 GrCCPerOpListPaths* lookupPendingPaths(uint32_t opListID);
Chris Dalton42c21152018-06-13 15:28:19 -060095 void recordOp(std::unique_ptr<GrCCDrawPathsOp>, const DrawPathArgs&);
Chris Daltonc1e59632017-09-05 00:30:07 -060096
Chris Daltond7e22272018-05-23 10:17:17 -060097 // 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 Daltona71305c2018-05-23 12:00:07 -0600100 PendingPathsMap fPendingPaths;
Chris Daltona32a3c32017-12-05 10:05:21 -0700101
Chris Daltond7e22272018-05-23 10:17:17 -0600102 // fFlushingPaths holds the GrCCPerOpListPaths objects that are currently being flushed.
103 // (It will only contain elements when fFlushing is true.)
Robert Phillips774168e2018-05-31 12:43:27 -0400104 SkSTArray<4, sk_sp<GrCCPerOpListPaths>> fFlushingPaths;
Chris Dalton4da70192018-06-18 09:51:36 -0600105
Chris Daltona2b5b642018-06-24 13:08:57 -0600106 std::unique_ptr<GrCCPathCache> fPathCache;
Chris Dalton4da70192018-06-18 09:51:36 -0600107
Chris Dalton383a2ef2018-01-08 17:21:41 -0500108 SkDEBUGCODE(bool fFlushing = false);
Chris Dalton351e80c2019-01-06 22:51:00 -0700109
110public:
111 void testingOnly_drawPathDirectly(const DrawPathArgs&);
112 const GrCCPerFlushResources* testingOnly_getCurrentFlushResources();
113 const GrCCPathCache* testingOnly_getPathCache() const;
Chris Dalton1a325d22017-07-14 15:17:41 -0600114};
115
116#endif