blob: 881d4398ccbf289f4c103bde7fc769958fecb03f [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"
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).
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;
59 if (!caps.mixedSamplesSupport() && InternalMultisample::kYes == internalMultisample) {
60 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(
Brian Salomon63410e92020-03-23 18:32:50 -040094 {fWidth, fHeight}, desc.fFormat, desc.fRenderable, desc.fSampleCnt,
95 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
112bool GrDynamicAtlas::addRect(int width, int height, SkIPoint16* location) {
Chris Daltona550cf22020-02-07 13:35:31 -0700113 // This can't be called anymore once instantiate() has been called.
114 SkASSERT(!this->isInstantiated());
115
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600116 if (!this->internalPlaceRect(width, height, location)) {
Chris Daltona550cf22020-02-07 13:35:31 -0700117 return false;
118 }
Chris Daltona550cf22020-02-07 13:35:31 -0700119
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600120 fDrawBounds.fWidth = std::max(fDrawBounds.width(), location->x() + width);
121 fDrawBounds.fHeight = std::max(fDrawBounds.height(), location->y() + height);
Chris Daltona550cf22020-02-07 13:35:31 -0700122 return true;
123}
124
125bool 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 Daltond2dc8dd2020-05-19 16:32:02 -0600141 fTopNode = this->makeNode(nullptr, 0, 0, fWidth, fHeight);
Chris Daltona550cf22020-02-07 13:35:31 -0700142 }
143
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600144 for (Node* node = fTopNode; node; node = node->previous()) {
Chris Daltona550cf22020-02-07 13:35:31 -0700145 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 Daltond2dc8dd2020-05-19 16:32:02 -0600158 fTopNode = this->makeNode(fTopNode, 0, top, fWidth, fHeight);
Chris Daltona550cf22020-02-07 13:35:31 -0700159 } else {
160 int left = fWidth;
161 fWidth = std::min(fWidth * 2, fMaxAtlasSize);
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600162 fTopNode = this->makeNode(fTopNode, left, 0, fWidth, fHeight);
Chris Daltona550cf22020-02-07 13:35:31 -0700163 }
164 } while (!fTopNode->addRect(w, h, loc));
165
166 return true;
167}
168
169std::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 Danield11ae2e2020-02-10 16:36:07 -0500192 auto rtc = onFlushRP->makeRenderTargetContext(fTextureProxy, kTextureOrigin, fColorType,
193 nullptr, nullptr);
Chris Daltona550cf22020-02-07 13:35:31 -0700194 if (!rtc) {
Chris Dalton5a5fe792020-02-15 11:41:30 -0700195 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 Daltona550cf22020-02-07 13:35:31 -0700198 return nullptr;
199 }
200
201 SkIRect clearRect = SkIRect::MakeSize(fDrawBounds);
202 rtc->clear(&clearRect, SK_PMColor4fTRANSPARENT,
203 GrRenderTargetContext::CanClearFullscreen::kYes);
204 return rtc;
205}