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