blob: 1b4cf24f45b3424c91ce28f373a948d75d49ed43 [file] [log] [blame]
Chris Daltona550cf22020-02-07 13:35:31 -07001/*
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 Daltone5a141d2020-05-19 11:57:53 -060010#include "src/core/SkIPoint16.h"
Chris Daltona550cf22020-02-07 13:35:31 -070011#include "src/gpu/GrOnFlushResourceProvider.h"
12#include "src/gpu/GrProxyProvider.h"
Chris Daltond2dc8dd2020-05-19 16:32:02 -060013#include "src/gpu/GrRectanizerPow2.h"
Chris Daltona550cf22020-02-07 13:35:31 -070014#include "src/gpu/GrRectanizerSkyline.h"
15#include "src/gpu/GrRenderTarget.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050016#include "src/gpu/GrSurfaceDrawContext.h"
Chris Daltona550cf22020-02-07 13:35:31 -070017
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).
21class GrDynamicAtlas::Node {
22public:
Chris Daltond2dc8dd2020-05-19 16:32:02 -060023 Node(Node* previous, GrRectanizer* rectanizer, int x, int y)
24 : fPrevious(previous), fRectanizer(rectanizer), fX(x), fY(y) {}
Chris Daltona550cf22020-02-07 13:35:31 -070025
Chris Daltond2dc8dd2020-05-19 16:32:02 -060026 Node* previous() const { return fPrevious; }
Chris Daltona550cf22020-02-07 13:35:31 -070027
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 Daltond2dc8dd2020-05-19 16:32:02 -060030 if (w < fRectanizer->width()) {
31 w = std::min(w + kPadding, fRectanizer->width());
Chris Daltona550cf22020-02-07 13:35:31 -070032 }
Chris Daltond2dc8dd2020-05-19 16:32:02 -060033 if (h < fRectanizer->height()) {
34 h = std::min(h + kPadding, fRectanizer->height());
Chris Daltona550cf22020-02-07 13:35:31 -070035 }
Chris Daltond2dc8dd2020-05-19 16:32:02 -060036 if (!fRectanizer->addRect(w, h, loc)) {
Chris Daltona550cf22020-02-07 13:35:31 -070037 return false;
38 }
39 loc->fX += fX;
40 loc->fY += fY;
41 return true;
42 }
43
44private:
Chris Daltond2dc8dd2020-05-19 16:32:02 -060045 Node* const fPrevious;
46 GrRectanizer* const fRectanizer;
Chris Daltona550cf22020-02-07 13:35:31 -070047 const int fX, fY;
Chris Daltona550cf22020-02-07 13:35:31 -070048};
49
50sk_sp<GrTextureProxy> GrDynamicAtlas::MakeLazyAtlasProxy(
Brian Salomon63410e92020-03-23 18:32:50 -040051 LazyInstantiateAtlasCallback&& callback,
52 GrColorType colorType,
53 InternalMultisample internalMultisample,
54 const GrCaps& caps,
Chris Daltona550cf22020-02-07 13:35:31 -070055 GrSurfaceProxy::UseAllocator useAllocator) {
56 GrBackendFormat format = caps.getDefaultBackendFormat(colorType, GrRenderable::kYes);
Chris Daltonf83d0342020-02-10 14:19:30 -070057
58 int sampleCount = 1;
Chris Dalton57ab06c2021-04-22 12:57:28 -060059 if (InternalMultisample::kYes == internalMultisample) {
Chris Daltonf83d0342020-02-10 14:19:30 -070060 sampleCount = caps.internalMultisampleCount(format);
61 }
Chris Daltona550cf22020-02-07 13:35:31 -070062
Brian Salomondf1bd6d2020-03-26 20:37:01 -040063 sk_sp<GrTextureProxy> proxy =
Brian Salomon63410e92020-03-23 18:32:50 -040064 GrProxyProvider::MakeFullyLazyProxy(std::move(callback), format, GrRenderable::kYes,
Brian Salomondf1bd6d2020-03-26 20:37:01 -040065 sampleCount, GrProtected::kNo, caps, useAllocator);
Chris Daltona550cf22020-02-07 13:35:31 -070066
67 return proxy;
68}
69
70GrDynamicAtlas::GrDynamicAtlas(GrColorType colorType, InternalMultisample internalMultisample,
Chris Daltond2dc8dd2020-05-19 16:32:02 -060071 SkISize initialSize, int maxAtlasSize, const GrCaps& caps,
72 RectanizerAlgorithm algorithm)
Chris Daltona550cf22020-02-07 13:35:31 -070073 : fColorType(colorType)
74 , fInternalMultisample(internalMultisample)
Chris Daltond2dc8dd2020-05-19 16:32:02 -060075 , fMaxAtlasSize(maxAtlasSize)
76 , fRectanizerAlgorithm(algorithm) {
Chris Daltona550cf22020-02-07 13:35:31 -070077 SkASSERT(fMaxAtlasSize <= caps.maxTextureSize());
78 this->reset(initialSize, caps);
79}
80
81GrDynamicAtlas::~GrDynamicAtlas() {
82}
83
84void GrDynamicAtlas::reset(SkISize initialSize, const GrCaps& caps) {
Chris Daltond2dc8dd2020-05-19 16:32:02 -060085 fNodeAllocator.reset();
Chris Daltona550cf22020-02-07 13:35:31 -070086 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 Salomon63410e92020-03-23 18:32:50 -040091 [this](GrResourceProvider* resourceProvider, const LazyAtlasDesc& desc) {
Chris Daltona550cf22020-02-07 13:35:31 -070092 if (!fBackingTexture) {
93 fBackingTexture = resourceProvider->createTexture(
Chris Dalton83420eb2021-06-23 18:47:09 -060094 fTextureProxy->backingStoreDimensions(), desc.fFormat, desc.fRenderable,
95 desc.fSampleCnt, desc.fMipmapped, desc.fBudgeted, desc.fProtected);
Chris Daltona550cf22020-02-07 13:35:31 -070096 }
97 return GrSurfaceProxy::LazyCallbackResult(fBackingTexture);
98 },
99 fColorType, fInternalMultisample, caps, GrSurfaceProxy::UseAllocator::kNo);
100 fBackingTexture = nullptr;
101}
102
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600103GrDynamicAtlas::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
Chris Dalton83420eb2021-06-23 18:47:09 -0600112GrSurfaceProxyView GrDynamicAtlas::readView(const GrCaps& caps) const {
Chris Daltonabed2672021-06-17 16:54:28 -0600113 return {fTextureProxy, kTextureOrigin,
114 caps.getReadSwizzle(fTextureProxy->backendFormat(), fColorType)};
115}
116
Chris Dalton83420eb2021-06-23 18:47:09 -0600117GrSurfaceProxyView GrDynamicAtlas::writeView(const GrCaps& caps) const {
118 return {fTextureProxy, kTextureOrigin,
119 caps.getWriteSwizzle(fTextureProxy->backendFormat(), fColorType)};
120}
121
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600122bool GrDynamicAtlas::addRect(int width, int height, SkIPoint16* location) {
Chris Daltona550cf22020-02-07 13:35:31 -0700123 // This can't be called anymore once instantiate() has been called.
124 SkASSERT(!this->isInstantiated());
125
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600126 if (!this->internalPlaceRect(width, height, location)) {
Chris Daltona550cf22020-02-07 13:35:31 -0700127 return false;
128 }
Chris Daltona550cf22020-02-07 13:35:31 -0700129
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600130 fDrawBounds.fWidth = std::max(fDrawBounds.width(), location->x() + width);
131 fDrawBounds.fHeight = std::max(fDrawBounds.height(), location->y() + height);
Chris Daltona550cf22020-02-07 13:35:31 -0700132 return true;
133}
134
135bool GrDynamicAtlas::internalPlaceRect(int w, int h, SkIPoint16* loc) {
136 if (std::max(h, w) > fMaxAtlasSize) {
137 return false;
138 }
139 if (std::min(h, w) <= 0) {
140 loc->set(0, 0);
141 return true;
142 }
143
144 if (!fTopNode) {
145 if (w > fWidth) {
146 fWidth = std::min(SkNextPow2(w), fMaxAtlasSize);
147 }
148 if (h > fHeight) {
149 fHeight = std::min(SkNextPow2(h), fMaxAtlasSize);
150 }
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600151 fTopNode = this->makeNode(nullptr, 0, 0, fWidth, fHeight);
Chris Daltona550cf22020-02-07 13:35:31 -0700152 }
153
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600154 for (Node* node = fTopNode; node; node = node->previous()) {
Chris Daltona550cf22020-02-07 13:35:31 -0700155 if (node->addRect(w, h, loc)) {
156 return true;
157 }
158 }
159
160 // The rect didn't fit. Grow the atlas and try again.
161 do {
162 if (fWidth >= fMaxAtlasSize && fHeight >= fMaxAtlasSize) {
163 return false;
164 }
165 if (fHeight <= fWidth) {
166 int top = fHeight;
167 fHeight = std::min(fHeight * 2, fMaxAtlasSize);
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600168 fTopNode = this->makeNode(fTopNode, 0, top, fWidth, fHeight);
Chris Daltona550cf22020-02-07 13:35:31 -0700169 } else {
170 int left = fWidth;
171 fWidth = std::min(fWidth * 2, fMaxAtlasSize);
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600172 fTopNode = this->makeNode(fTopNode, left, 0, fWidth, fHeight);
Chris Daltona550cf22020-02-07 13:35:31 -0700173 }
174 } while (!fTopNode->addRect(w, h, loc));
175
176 return true;
177}
178
Chris Dalton83420eb2021-06-23 18:47:09 -0600179void GrDynamicAtlas::instantiate(GrOnFlushResourceProvider* onFlushRP,
180 sk_sp<GrTexture> backingTexture) {
Chris Daltona550cf22020-02-07 13:35:31 -0700181 SkASSERT(!this->isInstantiated()); // This method should only be called once.
182 // Caller should have cropped any paths to the destination render target instead of asking for
183 // an atlas larger than maxRenderTargetSize.
184 SkASSERT(std::max(fHeight, fWidth) <= fMaxAtlasSize);
185 SkASSERT(fMaxAtlasSize <= onFlushRP->caps()->maxRenderTargetSize());
186
Chris Dalton83420eb2021-06-23 18:47:09 -0600187 if (fTextureProxy->isFullyLazy()) {
188 // Finalize the content size of our proxy. The GPU can potentially make optimizations if it
189 // knows we only intend to write out a smaller sub-rectangle of the backing texture.
190 fTextureProxy->priv().setLazyDimensions(fDrawBounds);
191 }
192 SkASSERT(fTextureProxy->dimensions() == fDrawBounds);
Chris Daltona550cf22020-02-07 13:35:31 -0700193
194 if (backingTexture) {
195#ifdef SK_DEBUG
196 auto backingRT = backingTexture->asRenderTarget();
197 SkASSERT(backingRT);
198 SkASSERT(backingRT->backendFormat() == fTextureProxy->backendFormat());
199 SkASSERT(backingRT->numSamples() == fTextureProxy->asRenderTargetProxy()->numSamples());
Chris Dalton83420eb2021-06-23 18:47:09 -0600200 SkASSERT(backingRT->dimensions() == fTextureProxy->backingStoreDimensions());
Chris Daltona550cf22020-02-07 13:35:31 -0700201#endif
202 fBackingTexture = std::move(backingTexture);
203 }
Chris Dalton83420eb2021-06-23 18:47:09 -0600204 onFlushRP->instatiateProxy(fTextureProxy.get());
Chris Daltona550cf22020-02-07 13:35:31 -0700205}