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