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 | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 18 | #include "ops/GrDrawOp.h" |
| 19 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame^] | 20 | class GrCCAtlas::Node { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 21 | public: |
| 22 | 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^] | 23 | : 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] | 24 | |
| 25 | Node* previous() const { return fPrevious.get(); } |
| 26 | |
| 27 | bool addRect(int w, int h, SkIPoint16* loc) { |
| 28 | static constexpr int kPad = 1; |
| 29 | |
| 30 | if (!fRectanizer.addRect(w + kPad, h + kPad, loc)) { |
| 31 | return false; |
| 32 | } |
| 33 | loc->fX += fX; |
| 34 | loc->fY += fY; |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | private: |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame^] | 39 | const std::unique_ptr<Node> fPrevious; |
| 40 | const int fX, fY; |
| 41 | GrRectanizerSkyline fRectanizer; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 42 | }; |
| 43 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame^] | 44 | GrCCAtlas::GrCCAtlas(const GrCaps& caps, int minWidth, int minHeight) |
| 45 | : fMaxAtlasSize(caps.maxRenderTargetSize()), fDrawBounds{0, 0} { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 46 | SkASSERT(fMaxAtlasSize <= caps.maxTextureSize()); |
| 47 | SkASSERT(SkTMax(minWidth, minHeight) <= fMaxAtlasSize); |
| 48 | int initialSize = GrNextPow2(SkTMax(minWidth, minHeight)); |
| 49 | initialSize = SkTMax(int(kMinSize), initialSize); |
| 50 | initialSize = SkTMin(initialSize, fMaxAtlasSize); |
| 51 | fHeight = fWidth = initialSize; |
| 52 | fTopNode = skstd::make_unique<Node>(nullptr, 0, 0, initialSize, initialSize); |
| 53 | } |
| 54 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame^] | 55 | GrCCAtlas::~GrCCAtlas() {} |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 56 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame^] | 57 | bool GrCCAtlas::addRect(int w, int h, SkIPoint16* loc) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 58 | // This can't be called anymore once finalize() has been called. |
| 59 | SkASSERT(!fTextureProxy); |
| 60 | |
| 61 | if (!this->internalPlaceRect(w, h, loc)) { |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | fDrawBounds.fWidth = SkTMax(fDrawBounds.width(), loc->x() + w); |
| 66 | fDrawBounds.fHeight = SkTMax(fDrawBounds.height(), loc->y() + h); |
| 67 | return true; |
| 68 | } |
| 69 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame^] | 70 | bool GrCCAtlas::internalPlaceRect(int w, int h, SkIPoint16* loc) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 71 | SkASSERT(SkTMax(w, h) < fMaxAtlasSize); |
| 72 | |
| 73 | for (Node* node = fTopNode.get(); node; node = node->previous()) { |
| 74 | if (node->addRect(w, h, loc)) { |
| 75 | return true; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // The rect didn't fit. Grow the atlas and try again. |
| 80 | do { |
| 81 | SkASSERT(SkTMax(fWidth, fHeight) <= fMaxAtlasSize); |
| 82 | if (fWidth == fMaxAtlasSize && fHeight == fMaxAtlasSize) { |
| 83 | return false; |
| 84 | } |
| 85 | if (fHeight <= fWidth) { |
| 86 | int top = fHeight; |
| 87 | fHeight = SkTMin(fHeight * 2, fMaxAtlasSize); |
| 88 | fTopNode = skstd::make_unique<Node>(std::move(fTopNode), 0, top, fWidth, fHeight); |
| 89 | } else { |
| 90 | int left = fWidth; |
| 91 | fWidth = SkTMin(fWidth * 2, fMaxAtlasSize); |
| 92 | fTopNode = skstd::make_unique<Node>(std::move(fTopNode), left, 0, fWidth, fHeight); |
| 93 | } |
| 94 | } while (!fTopNode->addRect(w, h, loc)); |
| 95 | |
| 96 | return true; |
| 97 | } |
| 98 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame^] | 99 | sk_sp<GrRenderTargetContext> GrCCAtlas::finalize( |
| 100 | GrOnFlushResourceProvider* onFlushRP, std::unique_ptr<GrDrawOp> atlasOp) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 101 | SkASSERT(!fTextureProxy); |
| 102 | |
| 103 | GrSurfaceDesc desc; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 104 | desc.fWidth = fWidth; |
| 105 | desc.fHeight = fHeight; |
| 106 | desc.fConfig = kAlpha_half_GrPixelConfig; |
| 107 | sk_sp<GrRenderTargetContext> rtc = onFlushRP->makeRenderTargetContext(desc, nullptr, nullptr); |
| 108 | if (!rtc) { |
| 109 | SkDebugf("WARNING: failed to allocate a %ix%i atlas. Some paths will not be drawn.\n", |
| 110 | fWidth, fHeight); |
| 111 | return nullptr; |
| 112 | } |
| 113 | |
| 114 | SkIRect clearRect = SkIRect::MakeSize(fDrawBounds); |
Chris Dalton | 344e903 | 2017-12-11 15:42:09 -0700 | [diff] [blame] | 115 | rtc->clear(&clearRect, 0, GrRenderTargetContext::CanClearFullscreen::kYes); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 116 | rtc->addDrawOp(GrNoClip(), std::move(atlasOp)); |
| 117 | |
| 118 | fTextureProxy = sk_ref_sp(rtc->asTextureProxy()); |
| 119 | return rtc; |
| 120 | } |