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