Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 | #include "src/gpu/GrDynamicAtlas.h" |
| 9 | |
Chris Dalton | e5a141d | 2020-05-19 11:57:53 -0600 | [diff] [blame] | 10 | #include "src/core/SkIPoint16.h" |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 11 | #include "src/gpu/GrOnFlushResourceProvider.h" |
| 12 | #include "src/gpu/GrProxyProvider.h" |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 13 | #include "src/gpu/GrRectanizerPow2.h" |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 14 | #include "src/gpu/GrRectanizerSkyline.h" |
| 15 | #include "src/gpu/GrRenderTarget.h" |
| 16 | #include "src/gpu/GrRenderTargetContext.h" |
| 17 | |
| 18 | // Each Node covers a sub-rectangle of the final atlas. When a GrDynamicAtlas runs out of room, we |
| 19 | // create a new Node the same size as all combined nodes in the atlas as-is, and then place the new |
| 20 | // Node immediately below or beside the others (thereby doubling the size of the GyDynamicAtlas). |
| 21 | class GrDynamicAtlas::Node { |
| 22 | public: |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 23 | Node(Node* previous, GrRectanizer* rectanizer, int x, int y) |
| 24 | : fPrevious(previous), fRectanizer(rectanizer), fX(x), fY(y) {} |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 25 | |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 26 | Node* previous() const { return fPrevious; } |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 27 | |
| 28 | bool addRect(int w, int h, SkIPoint16* loc) { |
| 29 | // Pad all paths except those that are expected to take up an entire physical texture. |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 30 | if (w < fRectanizer->width()) { |
| 31 | w = std::min(w + kPadding, fRectanizer->width()); |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 32 | } |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 33 | if (h < fRectanizer->height()) { |
| 34 | h = std::min(h + kPadding, fRectanizer->height()); |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 35 | } |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 36 | if (!fRectanizer->addRect(w, h, loc)) { |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 37 | return false; |
| 38 | } |
| 39 | loc->fX += fX; |
| 40 | loc->fY += fY; |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | private: |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 45 | Node* const fPrevious; |
| 46 | GrRectanizer* const fRectanizer; |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 47 | const int fX, fY; |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | sk_sp<GrTextureProxy> GrDynamicAtlas::MakeLazyAtlasProxy( |
Brian Salomon | 63410e9 | 2020-03-23 18:32:50 -0400 | [diff] [blame] | 51 | LazyInstantiateAtlasCallback&& callback, |
| 52 | GrColorType colorType, |
| 53 | InternalMultisample internalMultisample, |
| 54 | const GrCaps& caps, |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 55 | GrSurfaceProxy::UseAllocator useAllocator) { |
| 56 | GrBackendFormat format = caps.getDefaultBackendFormat(colorType, GrRenderable::kYes); |
Chris Dalton | f83d034 | 2020-02-10 14:19:30 -0700 | [diff] [blame] | 57 | |
| 58 | int sampleCount = 1; |
| 59 | if (!caps.mixedSamplesSupport() && InternalMultisample::kYes == internalMultisample) { |
| 60 | sampleCount = caps.internalMultisampleCount(format); |
| 61 | } |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 62 | |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 63 | sk_sp<GrTextureProxy> proxy = |
Brian Salomon | 63410e9 | 2020-03-23 18:32:50 -0400 | [diff] [blame] | 64 | GrProxyProvider::MakeFullyLazyProxy(std::move(callback), format, GrRenderable::kYes, |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 65 | sampleCount, GrProtected::kNo, caps, useAllocator); |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 66 | |
| 67 | return proxy; |
| 68 | } |
| 69 | |
| 70 | GrDynamicAtlas::GrDynamicAtlas(GrColorType colorType, InternalMultisample internalMultisample, |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 71 | SkISize initialSize, int maxAtlasSize, const GrCaps& caps, |
| 72 | RectanizerAlgorithm algorithm) |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 73 | : fColorType(colorType) |
| 74 | , fInternalMultisample(internalMultisample) |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 75 | , fMaxAtlasSize(maxAtlasSize) |
| 76 | , fRectanizerAlgorithm(algorithm) { |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 77 | SkASSERT(fMaxAtlasSize <= caps.maxTextureSize()); |
| 78 | this->reset(initialSize, caps); |
| 79 | } |
| 80 | |
| 81 | GrDynamicAtlas::~GrDynamicAtlas() { |
| 82 | } |
| 83 | |
| 84 | void GrDynamicAtlas::reset(SkISize initialSize, const GrCaps& caps) { |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 85 | fNodeAllocator.reset(); |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 86 | fWidth = std::min(SkNextPow2(initialSize.width()), fMaxAtlasSize); |
| 87 | fHeight = std::min(SkNextPow2(initialSize.height()), fMaxAtlasSize); |
| 88 | fTopNode = nullptr; |
| 89 | fDrawBounds.setEmpty(); |
| 90 | fTextureProxy = MakeLazyAtlasProxy( |
Brian Salomon | 63410e9 | 2020-03-23 18:32:50 -0400 | [diff] [blame] | 91 | [this](GrResourceProvider* resourceProvider, const LazyAtlasDesc& desc) { |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 92 | if (!fBackingTexture) { |
| 93 | fBackingTexture = resourceProvider->createTexture( |
Brian Salomon | 63410e9 | 2020-03-23 18:32:50 -0400 | [diff] [blame] | 94 | {fWidth, fHeight}, desc.fFormat, desc.fRenderable, desc.fSampleCnt, |
| 95 | desc.fMipMapped, desc.fBudgeted, desc.fProtected); |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 96 | } |
| 97 | return GrSurfaceProxy::LazyCallbackResult(fBackingTexture); |
| 98 | }, |
| 99 | fColorType, fInternalMultisample, caps, GrSurfaceProxy::UseAllocator::kNo); |
| 100 | fBackingTexture = nullptr; |
| 101 | } |
| 102 | |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 103 | GrDynamicAtlas::Node* GrDynamicAtlas::makeNode(Node* previous, int l, int t, int r, int b) { |
| 104 | int width = r - l; |
| 105 | int height = b - t; |
| 106 | GrRectanizer* rectanizer = (fRectanizerAlgorithm == RectanizerAlgorithm::kSkyline) |
| 107 | ? (GrRectanizer*)fNodeAllocator.make<GrRectanizerSkyline>(width, height) |
| 108 | : fNodeAllocator.make<GrRectanizerPow2>(width, height); |
| 109 | return fNodeAllocator.make<Node>(previous, rectanizer, l, t); |
| 110 | } |
| 111 | |
| 112 | bool GrDynamicAtlas::addRect(int width, int height, SkIPoint16* location) { |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 113 | // This can't be called anymore once instantiate() has been called. |
| 114 | SkASSERT(!this->isInstantiated()); |
| 115 | |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 116 | if (!this->internalPlaceRect(width, height, location)) { |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 117 | return false; |
| 118 | } |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 119 | |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 120 | fDrawBounds.fWidth = std::max(fDrawBounds.width(), location->x() + width); |
| 121 | fDrawBounds.fHeight = std::max(fDrawBounds.height(), location->y() + height); |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 122 | return true; |
| 123 | } |
| 124 | |
| 125 | bool GrDynamicAtlas::internalPlaceRect(int w, int h, SkIPoint16* loc) { |
| 126 | if (std::max(h, w) > fMaxAtlasSize) { |
| 127 | return false; |
| 128 | } |
| 129 | if (std::min(h, w) <= 0) { |
| 130 | loc->set(0, 0); |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | if (!fTopNode) { |
| 135 | if (w > fWidth) { |
| 136 | fWidth = std::min(SkNextPow2(w), fMaxAtlasSize); |
| 137 | } |
| 138 | if (h > fHeight) { |
| 139 | fHeight = std::min(SkNextPow2(h), fMaxAtlasSize); |
| 140 | } |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 141 | fTopNode = this->makeNode(nullptr, 0, 0, fWidth, fHeight); |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 144 | for (Node* node = fTopNode; node; node = node->previous()) { |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 145 | if (node->addRect(w, h, loc)) { |
| 146 | return true; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // The rect didn't fit. Grow the atlas and try again. |
| 151 | do { |
| 152 | if (fWidth >= fMaxAtlasSize && fHeight >= fMaxAtlasSize) { |
| 153 | return false; |
| 154 | } |
| 155 | if (fHeight <= fWidth) { |
| 156 | int top = fHeight; |
| 157 | fHeight = std::min(fHeight * 2, fMaxAtlasSize); |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 158 | fTopNode = this->makeNode(fTopNode, 0, top, fWidth, fHeight); |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 159 | } else { |
| 160 | int left = fWidth; |
| 161 | fWidth = std::min(fWidth * 2, fMaxAtlasSize); |
Chris Dalton | d2dc8dd | 2020-05-19 16:32:02 -0600 | [diff] [blame^] | 162 | fTopNode = this->makeNode(fTopNode, left, 0, fWidth, fHeight); |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 163 | } |
| 164 | } while (!fTopNode->addRect(w, h, loc)); |
| 165 | |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | std::unique_ptr<GrRenderTargetContext> GrDynamicAtlas::instantiate( |
| 170 | GrOnFlushResourceProvider* onFlushRP, sk_sp<GrTexture> backingTexture) { |
| 171 | SkASSERT(!this->isInstantiated()); // This method should only be called once. |
| 172 | // Caller should have cropped any paths to the destination render target instead of asking for |
| 173 | // an atlas larger than maxRenderTargetSize. |
| 174 | SkASSERT(std::max(fHeight, fWidth) <= fMaxAtlasSize); |
| 175 | SkASSERT(fMaxAtlasSize <= onFlushRP->caps()->maxRenderTargetSize()); |
| 176 | |
| 177 | // Finalize the content size of our proxy. The GPU can potentially make optimizations if it |
| 178 | // knows we only intend to write out a smaller sub-rectangle of the backing texture. |
| 179 | fTextureProxy->priv().setLazyDimensions(fDrawBounds); |
| 180 | |
| 181 | if (backingTexture) { |
| 182 | #ifdef SK_DEBUG |
| 183 | auto backingRT = backingTexture->asRenderTarget(); |
| 184 | SkASSERT(backingRT); |
| 185 | SkASSERT(backingRT->backendFormat() == fTextureProxy->backendFormat()); |
| 186 | SkASSERT(backingRT->numSamples() == fTextureProxy->asRenderTargetProxy()->numSamples()); |
| 187 | SkASSERT(backingRT->width() == fWidth); |
| 188 | SkASSERT(backingRT->height() == fHeight); |
| 189 | #endif |
| 190 | fBackingTexture = std::move(backingTexture); |
| 191 | } |
Greg Daniel | d11ae2e | 2020-02-10 16:36:07 -0500 | [diff] [blame] | 192 | auto rtc = onFlushRP->makeRenderTargetContext(fTextureProxy, kTextureOrigin, fColorType, |
| 193 | nullptr, nullptr); |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 194 | if (!rtc) { |
Chris Dalton | 5a5fe79 | 2020-02-15 11:41:30 -0700 | [diff] [blame] | 195 | onFlushRP->printWarningMessage(SkStringPrintf( |
| 196 | "WARNING: failed to allocate a %ix%i atlas. Some masks will not be drawn.\n", |
| 197 | fWidth, fHeight).c_str()); |
Chris Dalton | a550cf2 | 2020-02-07 13:35:31 -0700 | [diff] [blame] | 198 | return nullptr; |
| 199 | } |
| 200 | |
| 201 | SkIRect clearRect = SkIRect::MakeSize(fDrawBounds); |
| 202 | rtc->clear(&clearRect, SK_PMColor4fTRANSPARENT, |
| 203 | GrRenderTargetContext::CanClearFullscreen::kYes); |
| 204 | return rtc; |
| 205 | } |