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 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 8 | #include "GrCCAtlas.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 9 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 10 | #include "GrClip.h" |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 11 | #include "GrMemoryPool.h" |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 12 | #include "GrOnFlushResourceProvider.h" |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 13 | #include "GrSurfaceContextPriv.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 14 | #include "GrRectanizer_skyline.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 15 | #include "GrRenderTargetContext.h" |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 16 | #include "GrSurfaceContextPriv.h" |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 17 | #include "GrTextureProxy.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 18 | #include "SkMakeUnique.h" |
| 19 | #include "SkMathPriv.h" |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 20 | #include "ccpr/GrCCCoverageProcessor.h" |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 21 | #include "ccpr/GrCCPathParser.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 22 | #include "ops/GrDrawOp.h" |
| 23 | |
Chris Dalton | 2612bae | 2018-02-22 13:41:37 -0700 | [diff] [blame] | 24 | static constexpr int kAtlasMinSize = 1024; |
| 25 | static constexpr int kPadding = 1; |
| 26 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 27 | class GrCCAtlas::Node { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 28 | public: |
| 29 | Node(std::unique_ptr<Node> previous, int l, int t, int r, int b) |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 30 | : fPrevious(std::move(previous)), fX(l), fY(t), fRectanizer(r - l, b - t) {} |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 31 | |
| 32 | Node* previous() const { return fPrevious.get(); } |
| 33 | |
Chris Dalton | 2612bae | 2018-02-22 13:41:37 -0700 | [diff] [blame] | 34 | bool addRect(int w, int h, SkIPoint16* loc, int maxAtlasSize) { |
| 35 | // Pad all paths except those that are expected to take up an entire physical texture. |
| 36 | if (w < maxAtlasSize) { |
| 37 | w = SkTMin(w + kPadding, maxAtlasSize); |
| 38 | } |
| 39 | if (h < maxAtlasSize) { |
| 40 | h = SkTMin(h + kPadding, maxAtlasSize); |
| 41 | } |
| 42 | if (!fRectanizer.addRect(w, h, loc)) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 43 | return false; |
| 44 | } |
| 45 | loc->fX += fX; |
| 46 | loc->fY += fY; |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | private: |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 51 | const std::unique_ptr<Node> fPrevious; |
| 52 | const int fX, fY; |
| 53 | GrRectanizerSkyline fRectanizer; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 54 | }; |
| 55 | |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 56 | class GrCCAtlas::DrawCoverageCountOp : public GrDrawOp { |
| 57 | public: |
| 58 | DEFINE_OP_CLASS_ID |
| 59 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 60 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, |
| 61 | sk_sp<const GrCCPathParser> parser, |
| 62 | CoverageCountBatchID batchID, |
| 63 | const SkISize& drawBounds) { |
| 64 | return std::unique_ptr<GrDrawOp>(new DrawCoverageCountOp(std::move(parser), |
| 65 | batchID, drawBounds)); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | // GrDrawOp interface. |
| 69 | const char* name() const override { return "GrCCAtlas::DrawCoverageCountOp"; } |
| 70 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
| 71 | RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*, |
| 72 | GrPixelConfigIsClamped) override { return RequiresDstTexture::kNo; } |
| 73 | bool onCombineIfPossible(GrOp* other, const GrCaps& caps) override { return false; } |
| 74 | void onPrepare(GrOpFlushState*) override {} |
| 75 | void onExecute(GrOpFlushState* flushState) override { |
| 76 | fParser->drawCoverageCount(flushState, fBatchID, |
| 77 | SkIRect::MakeWH(fDrawBounds.width(), fDrawBounds.height())); |
| 78 | } |
| 79 | |
| 80 | private: |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 81 | friend class GrOpMemoryPool; // for ctor |
| 82 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 83 | DrawCoverageCountOp(sk_sp<const GrCCPathParser> parser, CoverageCountBatchID batchID, |
| 84 | const SkISize& drawBounds) |
| 85 | : INHERITED(ClassID()) |
| 86 | , fParser(std::move(parser)) |
| 87 | , fBatchID(batchID) |
| 88 | , fDrawBounds(drawBounds) { |
| 89 | this->setBounds(SkRect::MakeIWH(fDrawBounds.width(), fDrawBounds.height()), |
| 90 | GrOp::HasAABloat::kNo, GrOp::IsZeroArea::kNo); |
| 91 | } |
| 92 | |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 93 | const sk_sp<const GrCCPathParser> fParser; |
| 94 | const CoverageCountBatchID fBatchID; |
| 95 | const SkISize fDrawBounds; |
| 96 | |
| 97 | typedef GrDrawOp INHERITED; |
| 98 | }; |
| 99 | |
Chris Dalton | 2612bae | 2018-02-22 13:41:37 -0700 | [diff] [blame] | 100 | GrCCAtlas::GrCCAtlas(const GrCaps& caps, int minSize) |
| 101 | : fMaxAtlasSize(SkTMax(minSize, caps.maxPreferredRenderTargetSize())) { |
| 102 | // Caller should have cropped any paths to the destination render target instead of asking for |
| 103 | // an atlas larger than maxRenderTargetSize. |
| 104 | SkASSERT(fMaxAtlasSize <= caps.maxRenderTargetSize()); |
| 105 | int initialSize = GrNextPow2(minSize + kPadding); |
| 106 | initialSize = SkTMax(kAtlasMinSize, initialSize); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 107 | initialSize = SkTMin(initialSize, fMaxAtlasSize); |
| 108 | fHeight = fWidth = initialSize; |
Chris Dalton | 2612bae | 2018-02-22 13:41:37 -0700 | [diff] [blame] | 109 | fTopNode = skstd::make_unique<Node>(nullptr, 0, 0, fWidth, fHeight); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 110 | } |
| 111 | |
Chris Dalton | 2612bae | 2018-02-22 13:41:37 -0700 | [diff] [blame] | 112 | GrCCAtlas::~GrCCAtlas() { |
| 113 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 114 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 115 | bool GrCCAtlas::addRect(int w, int h, SkIPoint16* loc) { |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 116 | // This can't be called anymore once setCoverageCountBatchID() has been called. |
| 117 | SkASSERT(!fCoverageCountBatchID); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 118 | SkASSERT(!fTextureProxy); |
| 119 | |
| 120 | if (!this->internalPlaceRect(w, h, loc)) { |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | fDrawBounds.fWidth = SkTMax(fDrawBounds.width(), loc->x() + w); |
| 125 | fDrawBounds.fHeight = SkTMax(fDrawBounds.height(), loc->y() + h); |
| 126 | return true; |
| 127 | } |
| 128 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 129 | bool GrCCAtlas::internalPlaceRect(int w, int h, SkIPoint16* loc) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 130 | for (Node* node = fTopNode.get(); node; node = node->previous()) { |
Chris Dalton | 2612bae | 2018-02-22 13:41:37 -0700 | [diff] [blame] | 131 | if (node->addRect(w, h, loc, fMaxAtlasSize)) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 132 | return true; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // The rect didn't fit. Grow the atlas and try again. |
| 137 | do { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 138 | if (fWidth == fMaxAtlasSize && fHeight == fMaxAtlasSize) { |
| 139 | return false; |
| 140 | } |
| 141 | if (fHeight <= fWidth) { |
| 142 | int top = fHeight; |
| 143 | fHeight = SkTMin(fHeight * 2, fMaxAtlasSize); |
| 144 | fTopNode = skstd::make_unique<Node>(std::move(fTopNode), 0, top, fWidth, fHeight); |
| 145 | } else { |
| 146 | int left = fWidth; |
| 147 | fWidth = SkTMin(fWidth * 2, fMaxAtlasSize); |
| 148 | fTopNode = skstd::make_unique<Node>(std::move(fTopNode), left, 0, fWidth, fHeight); |
| 149 | } |
Chris Dalton | 2612bae | 2018-02-22 13:41:37 -0700 | [diff] [blame] | 150 | } while (!fTopNode->addRect(w, h, loc, fMaxAtlasSize)); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 151 | |
| 152 | return true; |
| 153 | } |
| 154 | |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 155 | sk_sp<GrRenderTargetContext> GrCCAtlas::finalize(GrOnFlushResourceProvider* onFlushRP, |
| 156 | sk_sp<const GrCCPathParser> parser) { |
| 157 | SkASSERT(fCoverageCountBatchID); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 158 | SkASSERT(!fTextureProxy); |
| 159 | |
| 160 | GrSurfaceDesc desc; |
Robert Phillips | ce5209a | 2018-02-13 11:13:51 -0500 | [diff] [blame] | 161 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 162 | desc.fWidth = fWidth; |
| 163 | desc.fHeight = fHeight; |
| 164 | desc.fConfig = kAlpha_half_GrPixelConfig; |
Brian Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 165 | sk_sp<GrRenderTargetContext> rtc = |
| 166 | onFlushRP->makeRenderTargetContext(desc, kTopLeft_GrSurfaceOrigin, nullptr, nullptr); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 167 | if (!rtc) { |
| 168 | SkDebugf("WARNING: failed to allocate a %ix%i atlas. Some paths will not be drawn.\n", |
| 169 | fWidth, fHeight); |
| 170 | return nullptr; |
| 171 | } |
| 172 | |
| 173 | SkIRect clearRect = SkIRect::MakeSize(fDrawBounds); |
Chris Dalton | 344e903 | 2017-12-11 15:42:09 -0700 | [diff] [blame] | 174 | rtc->clear(&clearRect, 0, GrRenderTargetContext::CanClearFullscreen::kYes); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 175 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 176 | GrContext* context = rtc->surfPriv().getContext(); |
| 177 | |
| 178 | std::unique_ptr<GrDrawOp> op = DrawCoverageCountOp::Make(context, |
| 179 | std::move(parser), |
| 180 | fCoverageCountBatchID, |
| 181 | fDrawBounds); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 182 | rtc->addDrawOp(GrNoClip(), std::move(op)); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 183 | |
| 184 | fTextureProxy = sk_ref_sp(rtc->asTextureProxy()); |
| 185 | return rtc; |
| 186 | } |