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 | |
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). |
19 | class GrDynamicAtlas::Node { |
20 | public: |
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 | |
42 | private: |
43 | const std::unique_ptr<Node> fPrevious; |
44 | const int fX, fY; |
45 | GrRectanizerSkyline fRectanizer; |
46 | }; |
47 | |
48 | sk_sp<GrTextureProxy> GrDynamicAtlas::MakeLazyAtlasProxy( |
49 | const LazyInstantiateAtlasCallback& callback, GrColorType colorType, |
50 | InternalMultisample internalMultisample, const GrCaps& caps, |
51 | GrSurfaceProxy::UseAllocator useAllocator) { |
52 | GrBackendFormat format = caps.getDefaultBackendFormat(colorType, GrRenderable::kYes); |
53 | |
54 | int sampleCount = 1; |
55 | if (!caps.mixedSamplesSupport() && InternalMultisample::kYes == internalMultisample) { |
56 | sampleCount = caps.internalMultisampleCount(format); |
57 | } |
58 | |
59 | auto instantiate = [cb = std::move(callback), format, sampleCount](GrResourceProvider* rp) { |
60 | return cb(rp, format, sampleCount); |
61 | }; |
62 | |
63 | sk_sp<GrTextureProxy> proxy = |
64 | GrProxyProvider::MakeFullyLazyProxy(std::move(instantiate), format, GrRenderable::kYes, |
65 | sampleCount, GrProtected::kNo, caps, useAllocator); |
66 | |
67 | return proxy; |
68 | } |
69 | |
70 | GrDynamicAtlas::GrDynamicAtlas(GrColorType colorType, InternalMultisample internalMultisample, |
71 | SkISize initialSize, int maxAtlasSize, const GrCaps& caps) |
72 | : fColorType(colorType) |
73 | , fInternalMultisample(internalMultisample) |
74 | , fMaxAtlasSize(maxAtlasSize) { |
75 | SkASSERT(fMaxAtlasSize <= caps.maxTextureSize()); |
76 | this->reset(initialSize, caps); |
77 | } |
78 | |
79 | GrDynamicAtlas::~GrDynamicAtlas() { |
80 | } |
81 | |
82 | void GrDynamicAtlas::reset(SkISize initialSize, const GrCaps& caps) { |
83 | fWidth = std::min(SkNextPow2(initialSize.width()), fMaxAtlasSize); |
84 | fHeight = std::min(SkNextPow2(initialSize.height()), fMaxAtlasSize); |
85 | fTopNode = nullptr; |
86 | fDrawBounds.setEmpty(); |
87 | fTextureProxy = MakeLazyAtlasProxy( |
88 | [this](GrResourceProvider* resourceProvider, const GrBackendFormat& format, |
89 | int sampleCount) { |
90 | if (!fBackingTexture) { |
91 | fBackingTexture = resourceProvider->createTexture( |
92 | {fWidth, fHeight}, format, GrRenderable::kYes, sampleCount, |
93 | GrMipMapped::kNo, SkBudgeted::kYes, GrProtected::kNo); |
94 | } |
95 | return GrSurfaceProxy::LazyCallbackResult(fBackingTexture); |
96 | }, |
97 | fColorType, fInternalMultisample, caps, GrSurfaceProxy::UseAllocator::kNo); |
98 | fBackingTexture = nullptr; |
99 | } |
100 | |
101 | bool GrDynamicAtlas::addRect(const SkIRect& devIBounds, SkIVector* offset) { |
102 | // This can't be called anymore once instantiate() has been called. |
103 | SkASSERT(!this->isInstantiated()); |
104 | |
105 | SkIPoint16 location; |
106 | if (!this->internalPlaceRect(devIBounds.width(), devIBounds.height(), &location)) { |
107 | return false; |
108 | } |
109 | offset->set(location.x() - devIBounds.left(), location.y() - devIBounds.top()); |
110 | |
111 | fDrawBounds.fWidth = std::max(fDrawBounds.width(), location.x() + devIBounds.width()); |
112 | fDrawBounds.fHeight = std::max(fDrawBounds.height(), location.y() + devIBounds.height()); |
113 | return true; |
114 | } |
115 | |
116 | bool GrDynamicAtlas::internalPlaceRect(int w, int h, SkIPoint16* loc) { |
117 | if (std::max(h, w) > fMaxAtlasSize) { |
118 | return false; |
119 | } |
120 | if (std::min(h, w) <= 0) { |
121 | loc->set(0, 0); |
122 | return true; |
123 | } |
124 | |
125 | if (!fTopNode) { |
126 | if (w > fWidth) { |
127 | fWidth = std::min(SkNextPow2(w), fMaxAtlasSize); |
128 | } |
129 | if (h > fHeight) { |
130 | fHeight = std::min(SkNextPow2(h), fMaxAtlasSize); |
131 | } |
132 | fTopNode = std::make_unique<Node>(nullptr, 0, 0, fWidth, fHeight); |
133 | } |
134 | |
135 | for (Node* node = fTopNode.get(); node; node = node->previous()) { |
136 | if (node->addRect(w, h, loc)) { |
137 | return true; |
138 | } |
139 | } |
140 | |
141 | // The rect didn't fit. Grow the atlas and try again. |
142 | do { |
143 | if (fWidth >= fMaxAtlasSize && fHeight >= fMaxAtlasSize) { |
144 | return false; |
145 | } |
146 | if (fHeight <= fWidth) { |
147 | int top = fHeight; |
148 | fHeight = std::min(fHeight * 2, fMaxAtlasSize); |
149 | fTopNode = std::make_unique<Node>(std::move(fTopNode), 0, top, fWidth, fHeight); |
150 | } else { |
151 | int left = fWidth; |
152 | fWidth = std::min(fWidth * 2, fMaxAtlasSize); |
153 | fTopNode = std::make_unique<Node>(std::move(fTopNode), left, 0, fWidth, fHeight); |
154 | } |
155 | } while (!fTopNode->addRect(w, h, loc)); |
156 | |
157 | return true; |
158 | } |
159 | |
160 | std::unique_ptr<GrRenderTargetContext> GrDynamicAtlas::instantiate( |
161 | GrOnFlushResourceProvider* onFlushRP, sk_sp<GrTexture> backingTexture) { |
162 | SkASSERT(!this->isInstantiated()); // This method should only be called once. |
163 | // Caller should have cropped any paths to the destination render target instead of asking for |
164 | // an atlas larger than maxRenderTargetSize. |
165 | SkASSERT(std::max(fHeight, fWidth) <= fMaxAtlasSize); |
166 | SkASSERT(fMaxAtlasSize <= onFlushRP->caps()->maxRenderTargetSize()); |
167 | |
168 | // Finalize the content size of our proxy. The GPU can potentially make optimizations if it |
169 | // knows we only intend to write out a smaller sub-rectangle of the backing texture. |
170 | fTextureProxy->priv().setLazyDimensions(fDrawBounds); |
171 | |
172 | if (backingTexture) { |
173 | #ifdef SK_DEBUG |
174 | auto backingRT = backingTexture->asRenderTarget(); |
175 | SkASSERT(backingRT); |
176 | SkASSERT(backingRT->backendFormat() == fTextureProxy->backendFormat()); |
177 | SkASSERT(backingRT->numSamples() == fTextureProxy->asRenderTargetProxy()->numSamples()); |
178 | SkASSERT(backingRT->width() == fWidth); |
179 | SkASSERT(backingRT->height() == fHeight); |
180 | #endif |
181 | fBackingTexture = std::move(backingTexture); |
182 | } |
183 | auto rtc = onFlushRP->makeRenderTargetContext(fTextureProxy, kTextureOrigin, fColorType, |
184 | nullptr, nullptr); |
185 | if (!rtc) { |
186 | onFlushRP->printWarningMessage(SkStringPrintf( |
187 | "WARNING: failed to allocate a %ix%i atlas. Some masks will not be drawn.\n" , |
188 | fWidth, fHeight).c_str()); |
189 | return nullptr; |
190 | } |
191 | |
192 | SkIRect clearRect = SkIRect::MakeSize(fDrawBounds); |
193 | rtc->clear(&clearRect, SK_PMColor4fTRANSPARENT, |
194 | GrRenderTargetContext::CanClearFullscreen::kYes); |
195 | return rtc; |
196 | } |
197 | |