blob: 7fe35a05095c4a2df820fd2d6b06a383a9e45c64 [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"
Robert Phillips400f52e2021-07-26 13:23:10 -040011#include "src/gpu/GrCaps.h"
Chris Daltona550cf22020-02-07 13:35:31 -070012#include "src/gpu/GrOnFlushResourceProvider.h"
13#include "src/gpu/GrProxyProvider.h"
Chris Daltond2dc8dd2020-05-19 16:32:02 -060014#include "src/gpu/GrRectanizerPow2.h"
Chris Daltona550cf22020-02-07 13:35:31 -070015#include "src/gpu/GrRectanizerSkyline.h"
16#include "src/gpu/GrRenderTarget.h"
Robert Phillips1a82a4e2021-07-01 10:27:44 -040017#include "src/gpu/GrResourceProvider.h"
Robert Phillips400f52e2021-07-26 13:23:10 -040018#include "src/gpu/GrSurfaceProxyPriv.h"
19#include "src/gpu/GrSurfaceProxyView.h"
Chris Daltona550cf22020-02-07 13:35:31 -070020
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).
24class GrDynamicAtlas::Node {
25public:
Chris Daltond2dc8dd2020-05-19 16:32:02 -060026 Node(Node* previous, GrRectanizer* rectanizer, int x, int y)
27 : fPrevious(previous), fRectanizer(rectanizer), fX(x), fY(y) {}
Chris Daltona550cf22020-02-07 13:35:31 -070028
Chris Daltond2dc8dd2020-05-19 16:32:02 -060029 Node* previous() const { return fPrevious; }
Chris Daltona550cf22020-02-07 13:35:31 -070030
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 Daltond2dc8dd2020-05-19 16:32:02 -060033 if (w < fRectanizer->width()) {
34 w = std::min(w + kPadding, fRectanizer->width());
Chris Daltona550cf22020-02-07 13:35:31 -070035 }
Chris Daltond2dc8dd2020-05-19 16:32:02 -060036 if (h < fRectanizer->height()) {
37 h = std::min(h + kPadding, fRectanizer->height());
Chris Daltona550cf22020-02-07 13:35:31 -070038 }
Chris Daltond2dc8dd2020-05-19 16:32:02 -060039 if (!fRectanizer->addRect(w, h, loc)) {
Chris Daltona550cf22020-02-07 13:35:31 -070040 return false;
41 }
42 loc->fX += fX;
43 loc->fY += fY;
44 return true;
45 }
46
47private:
Chris Daltond2dc8dd2020-05-19 16:32:02 -060048 Node* const fPrevious;
49 GrRectanizer* const fRectanizer;
Chris Daltona550cf22020-02-07 13:35:31 -070050 const int fX, fY;
Chris Daltona550cf22020-02-07 13:35:31 -070051};
52
53sk_sp<GrTextureProxy> GrDynamicAtlas::MakeLazyAtlasProxy(
Brian Salomon63410e92020-03-23 18:32:50 -040054 LazyInstantiateAtlasCallback&& callback,
55 GrColorType colorType,
56 InternalMultisample internalMultisample,
57 const GrCaps& caps,
Chris Daltona550cf22020-02-07 13:35:31 -070058 GrSurfaceProxy::UseAllocator useAllocator) {
59 GrBackendFormat format = caps.getDefaultBackendFormat(colorType, GrRenderable::kYes);
Chris Daltonf83d0342020-02-10 14:19:30 -070060
61 int sampleCount = 1;
Chris Dalton57ab06c2021-04-22 12:57:28 -060062 if (InternalMultisample::kYes == internalMultisample) {
Chris Daltonf83d0342020-02-10 14:19:30 -070063 sampleCount = caps.internalMultisampleCount(format);
64 }
Chris Daltona550cf22020-02-07 13:35:31 -070065
Brian Salomondf1bd6d2020-03-26 20:37:01 -040066 sk_sp<GrTextureProxy> proxy =
Brian Salomon63410e92020-03-23 18:32:50 -040067 GrProxyProvider::MakeFullyLazyProxy(std::move(callback), format, GrRenderable::kYes,
Brian Salomondf1bd6d2020-03-26 20:37:01 -040068 sampleCount, GrProtected::kNo, caps, useAllocator);
Chris Daltona550cf22020-02-07 13:35:31 -070069
70 return proxy;
71}
72
73GrDynamicAtlas::GrDynamicAtlas(GrColorType colorType, InternalMultisample internalMultisample,
Chris Daltond2dc8dd2020-05-19 16:32:02 -060074 SkISize initialSize, int maxAtlasSize, const GrCaps& caps,
75 RectanizerAlgorithm algorithm)
Chris Daltona550cf22020-02-07 13:35:31 -070076 : fColorType(colorType)
77 , fInternalMultisample(internalMultisample)
Chris Daltond2dc8dd2020-05-19 16:32:02 -060078 , fMaxAtlasSize(maxAtlasSize)
79 , fRectanizerAlgorithm(algorithm) {
Chris Daltona550cf22020-02-07 13:35:31 -070080 SkASSERT(fMaxAtlasSize <= caps.maxTextureSize());
81 this->reset(initialSize, caps);
82}
83
84GrDynamicAtlas::~GrDynamicAtlas() {
85}
86
87void GrDynamicAtlas::reset(SkISize initialSize, const GrCaps& caps) {
Chris Daltond2dc8dd2020-05-19 16:32:02 -060088 fNodeAllocator.reset();
Chris Daltona550cf22020-02-07 13:35:31 -070089 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 Salomon63410e92020-03-23 18:32:50 -040094 [this](GrResourceProvider* resourceProvider, const LazyAtlasDesc& desc) {
Chris Daltona550cf22020-02-07 13:35:31 -070095 if (!fBackingTexture) {
96 fBackingTexture = resourceProvider->createTexture(
Chris Dalton83420eb2021-06-23 18:47:09 -060097 fTextureProxy->backingStoreDimensions(), desc.fFormat, desc.fRenderable,
98 desc.fSampleCnt, desc.fMipmapped, desc.fBudgeted, desc.fProtected);
Chris Daltona550cf22020-02-07 13:35:31 -070099 }
100 return GrSurfaceProxy::LazyCallbackResult(fBackingTexture);
101 },
102 fColorType, fInternalMultisample, caps, GrSurfaceProxy::UseAllocator::kNo);
103 fBackingTexture = nullptr;
104}
105
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600106GrDynamicAtlas::Node* GrDynamicAtlas::makeNode(Node* previous, int l, int t, int r, int b) {
107 int width = r - l;
108 int height = b - t;
109 GrRectanizer* rectanizer = (fRectanizerAlgorithm == RectanizerAlgorithm::kSkyline)
110 ? (GrRectanizer*)fNodeAllocator.make<GrRectanizerSkyline>(width, height)
111 : fNodeAllocator.make<GrRectanizerPow2>(width, height);
112 return fNodeAllocator.make<Node>(previous, rectanizer, l, t);
113}
114
Chris Dalton83420eb2021-06-23 18:47:09 -0600115GrSurfaceProxyView GrDynamicAtlas::readView(const GrCaps& caps) const {
Chris Daltonabed2672021-06-17 16:54:28 -0600116 return {fTextureProxy, kTextureOrigin,
117 caps.getReadSwizzle(fTextureProxy->backendFormat(), fColorType)};
118}
119
Chris Dalton83420eb2021-06-23 18:47:09 -0600120GrSurfaceProxyView GrDynamicAtlas::writeView(const GrCaps& caps) const {
121 return {fTextureProxy, kTextureOrigin,
122 caps.getWriteSwizzle(fTextureProxy->backendFormat(), fColorType)};
123}
124
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600125bool GrDynamicAtlas::addRect(int width, int height, SkIPoint16* location) {
Chris Daltona550cf22020-02-07 13:35:31 -0700126 // This can't be called anymore once instantiate() has been called.
127 SkASSERT(!this->isInstantiated());
128
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600129 if (!this->internalPlaceRect(width, height, location)) {
Chris Daltona550cf22020-02-07 13:35:31 -0700130 return false;
131 }
Chris Daltona550cf22020-02-07 13:35:31 -0700132
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600133 fDrawBounds.fWidth = std::max(fDrawBounds.width(), location->x() + width);
134 fDrawBounds.fHeight = std::max(fDrawBounds.height(), location->y() + height);
Chris Daltona550cf22020-02-07 13:35:31 -0700135 return true;
136}
137
138bool GrDynamicAtlas::internalPlaceRect(int w, int h, SkIPoint16* loc) {
139 if (std::max(h, w) > fMaxAtlasSize) {
140 return false;
141 }
142 if (std::min(h, w) <= 0) {
143 loc->set(0, 0);
144 return true;
145 }
146
147 if (!fTopNode) {
148 if (w > fWidth) {
149 fWidth = std::min(SkNextPow2(w), fMaxAtlasSize);
150 }
151 if (h > fHeight) {
152 fHeight = std::min(SkNextPow2(h), fMaxAtlasSize);
153 }
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600154 fTopNode = this->makeNode(nullptr, 0, 0, fWidth, fHeight);
Chris Daltona550cf22020-02-07 13:35:31 -0700155 }
156
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600157 for (Node* node = fTopNode; node; node = node->previous()) {
Chris Daltona550cf22020-02-07 13:35:31 -0700158 if (node->addRect(w, h, loc)) {
159 return true;
160 }
161 }
162
163 // The rect didn't fit. Grow the atlas and try again.
164 do {
165 if (fWidth >= fMaxAtlasSize && fHeight >= fMaxAtlasSize) {
166 return false;
167 }
168 if (fHeight <= fWidth) {
169 int top = fHeight;
170 fHeight = std::min(fHeight * 2, fMaxAtlasSize);
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600171 fTopNode = this->makeNode(fTopNode, 0, top, fWidth, fHeight);
Chris Daltona550cf22020-02-07 13:35:31 -0700172 } else {
173 int left = fWidth;
174 fWidth = std::min(fWidth * 2, fMaxAtlasSize);
Chris Daltond2dc8dd2020-05-19 16:32:02 -0600175 fTopNode = this->makeNode(fTopNode, left, 0, fWidth, fHeight);
Chris Daltona550cf22020-02-07 13:35:31 -0700176 }
177 } while (!fTopNode->addRect(w, h, loc));
178
179 return true;
180}
181
Chris Dalton83420eb2021-06-23 18:47:09 -0600182void GrDynamicAtlas::instantiate(GrOnFlushResourceProvider* onFlushRP,
183 sk_sp<GrTexture> backingTexture) {
Chris Daltona550cf22020-02-07 13:35:31 -0700184 SkASSERT(!this->isInstantiated()); // This method should only be called once.
185 // Caller should have cropped any paths to the destination render target instead of asking for
186 // an atlas larger than maxRenderTargetSize.
187 SkASSERT(std::max(fHeight, fWidth) <= fMaxAtlasSize);
188 SkASSERT(fMaxAtlasSize <= onFlushRP->caps()->maxRenderTargetSize());
189
Chris Dalton83420eb2021-06-23 18:47:09 -0600190 if (fTextureProxy->isFullyLazy()) {
191 // Finalize the content size of our proxy. The GPU can potentially make optimizations if it
192 // knows we only intend to write out a smaller sub-rectangle of the backing texture.
193 fTextureProxy->priv().setLazyDimensions(fDrawBounds);
194 }
195 SkASSERT(fTextureProxy->dimensions() == fDrawBounds);
Chris Daltona550cf22020-02-07 13:35:31 -0700196
197 if (backingTexture) {
198#ifdef SK_DEBUG
199 auto backingRT = backingTexture->asRenderTarget();
200 SkASSERT(backingRT);
201 SkASSERT(backingRT->backendFormat() == fTextureProxy->backendFormat());
202 SkASSERT(backingRT->numSamples() == fTextureProxy->asRenderTargetProxy()->numSamples());
Chris Dalton83420eb2021-06-23 18:47:09 -0600203 SkASSERT(backingRT->dimensions() == fTextureProxy->backingStoreDimensions());
Chris Daltona550cf22020-02-07 13:35:31 -0700204#endif
205 fBackingTexture = std::move(backingTexture);
206 }
Chris Dalton83420eb2021-06-23 18:47:09 -0600207 onFlushRP->instatiateProxy(fTextureProxy.get());
Chris Daltona550cf22020-02-07 13:35:31 -0700208}