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