blob: 6dc523d239f5b349b1280bd631f470a70e6ce72d [file] [log] [blame]
Chris Dalton1a325d22017-07-14 15:17:41 -06001/*
2 * Copyright 2017 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
Chris Dalton383a2ef2018-01-08 17:21:41 -05008#include "GrCCAtlas.h"
Chris Dalton1a325d22017-07-14 15:17:41 -06009
Chris Dalton9414c962018-06-14 10:14:50 -060010#include "GrCaps.h"
Chris Dalton383a2ef2018-01-08 17:21:41 -050011#include "GrOnFlushResourceProvider.h"
Chris Dalton4c458b12018-06-16 17:22:59 -060012#include "GrProxyProvider.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060013#include "GrRectanizer_skyline.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060014#include "GrRenderTargetContext.h"
Chris Dalton4c458b12018-06-16 17:22:59 -060015#include "GrTexture.h"
Chris Dalton383a2ef2018-01-08 17:21:41 -050016#include "GrTextureProxy.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060017#include "SkMakeUnique.h"
18#include "SkMathPriv.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060019
Chris Dalton383a2ef2018-01-08 17:21:41 -050020class GrCCAtlas::Node {
Chris Dalton1a325d22017-07-14 15:17:41 -060021public:
22 Node(std::unique_ptr<Node> previous, int l, int t, int r, int b)
Chris Dalton383a2ef2018-01-08 17:21:41 -050023 : fPrevious(std::move(previous)), fX(l), fY(t), fRectanizer(r - l, b - t) {}
Chris Dalton1a325d22017-07-14 15:17:41 -060024
25 Node* previous() const { return fPrevious.get(); }
26
Chris Dalton2612bae2018-02-22 13:41:37 -070027 bool addRect(int w, int h, SkIPoint16* loc, int maxAtlasSize) {
28 // Pad all paths except those that are expected to take up an entire physical texture.
29 if (w < maxAtlasSize) {
30 w = SkTMin(w + kPadding, maxAtlasSize);
31 }
32 if (h < maxAtlasSize) {
33 h = SkTMin(h + kPadding, maxAtlasSize);
34 }
35 if (!fRectanizer.addRect(w, h, loc)) {
Chris Dalton1a325d22017-07-14 15:17:41 -060036 return false;
37 }
38 loc->fX += fX;
39 loc->fY += fY;
40 return true;
41 }
42
43private:
Chris Dalton383a2ef2018-01-08 17:21:41 -050044 const std::unique_ptr<Node> fPrevious;
45 const int fX, fY;
46 GrRectanizerSkyline fRectanizer;
Chris Dalton1a325d22017-07-14 15:17:41 -060047};
48
Chris Dalton4c458b12018-06-16 17:22:59 -060049GrCCAtlas::GrCCAtlas(GrPixelConfig pixelConfig, const Specs& specs, const GrCaps& caps)
Chris Dalton42c21152018-06-13 15:28:19 -060050 : fMaxTextureSize(SkTMax(SkTMax(specs.fMinHeight, specs.fMinWidth),
51 specs.fMaxPreferredTextureSize)) {
Chris Dalton4c458b12018-06-16 17:22:59 -060052 // Caller should have cropped any paths to the destination render target instead of asking for
53 // an atlas larger than maxRenderTargetSize.
54 SkASSERT(fMaxTextureSize <= caps.maxTextureSize());
Chris Dalton42c21152018-06-13 15:28:19 -060055 SkASSERT(specs.fMaxPreferredTextureSize > 0);
56
57 // Begin with the first pow2 dimensions whose area is theoretically large enough to contain the
58 // pending paths, favoring height over width if necessary.
59 int log2area = SkNextLog2(SkTMax(specs.fApproxNumPixels, 1));
60 fHeight = 1 << ((log2area + 1) / 2);
61 fWidth = 1 << (log2area / 2);
62
63 fWidth = SkTClamp(fWidth, specs.fMinTextureSize, specs.fMaxPreferredTextureSize);
64 fHeight = SkTClamp(fHeight, specs.fMinTextureSize, specs.fMaxPreferredTextureSize);
65
66 if (fWidth < specs.fMinWidth || fHeight < specs.fMinHeight) {
67 // They want to stuff a particularly large path into the atlas. Just punt and go with their
68 // min width and height. The atlas will grow as needed.
69 fWidth = SkTMin(specs.fMinWidth + kPadding, fMaxTextureSize);
70 fHeight = SkTMin(specs.fMinHeight + kPadding, fMaxTextureSize);
71 }
72
Chris Dalton2612bae2018-02-22 13:41:37 -070073 fTopNode = skstd::make_unique<Node>(nullptr, 0, 0, fWidth, fHeight);
Chris Dalton4c458b12018-06-16 17:22:59 -060074
Greg Daniel4065d452018-11-16 15:43:41 -050075 // TODO: don't have this rely on the GrPixelConfig
76 GrSRGBEncoded srgbEncoded = GrSRGBEncoded::kNo;
77 GrColorType colorType = GrPixelConfigToColorTypeAndEncoding(pixelConfig, &srgbEncoded);
78
79 const GrBackendFormat format =
80 caps.getBackendFormatFromGrColorType(colorType, srgbEncoded);
81
Chris Dalton4c458b12018-06-16 17:22:59 -060082 fTextureProxy = GrProxyProvider::MakeFullyLazyProxy(
83 [this, pixelConfig](GrResourceProvider* resourceProvider) {
84 if (!resourceProvider) {
85 return sk_sp<GrTexture>();
86 }
Chris Daltonafde18f2018-06-22 12:44:19 -060087 if (!fBackingTexture) {
88 GrSurfaceDesc desc;
89 desc.fFlags = kRenderTarget_GrSurfaceFlag;
90 desc.fWidth = fWidth;
91 desc.fHeight = fHeight;
92 desc.fConfig = pixelConfig;
93 fBackingTexture = resourceProvider->createTexture(desc, SkBudgeted::kYes);
94 }
95 return fBackingTexture;
Chris Dalton4c458b12018-06-16 17:22:59 -060096 },
Greg Daniel4065d452018-11-16 15:43:41 -050097 format, GrProxyProvider::Renderable::kYes, kTextureOrigin, pixelConfig, caps);
Chris Dalton1a325d22017-07-14 15:17:41 -060098}
99
Chris Dalton2612bae2018-02-22 13:41:37 -0700100GrCCAtlas::~GrCCAtlas() {
101}
Chris Dalton1a325d22017-07-14 15:17:41 -0600102
Chris Dalton9414c962018-06-14 10:14:50 -0600103bool GrCCAtlas::addRect(const SkIRect& devIBounds, SkIVector* offset) {
Chris Dalton4c458b12018-06-16 17:22:59 -0600104 // This can't be called anymore once makeRenderTargetContext() has been called.
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400105 SkASSERT(!fTextureProxy->isInstantiated());
Chris Dalton1a325d22017-07-14 15:17:41 -0600106
Chris Dalton9414c962018-06-14 10:14:50 -0600107 SkIPoint16 location;
108 if (!this->internalPlaceRect(devIBounds.width(), devIBounds.height(), &location)) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600109 return false;
110 }
Chris Dalton9414c962018-06-14 10:14:50 -0600111 offset->set(location.x() - devIBounds.left(), location.y() - devIBounds.top());
Chris Dalton1a325d22017-07-14 15:17:41 -0600112
Chris Dalton9414c962018-06-14 10:14:50 -0600113 fDrawBounds.fWidth = SkTMax(fDrawBounds.width(), location.x() + devIBounds.width());
114 fDrawBounds.fHeight = SkTMax(fDrawBounds.height(), location.y() + devIBounds.height());
Chris Dalton1a325d22017-07-14 15:17:41 -0600115 return true;
116}
117
Chris Dalton383a2ef2018-01-08 17:21:41 -0500118bool GrCCAtlas::internalPlaceRect(int w, int h, SkIPoint16* loc) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600119 for (Node* node = fTopNode.get(); node; node = node->previous()) {
Chris Dalton42c21152018-06-13 15:28:19 -0600120 if (node->addRect(w, h, loc, fMaxTextureSize)) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600121 return true;
122 }
123 }
124
125 // The rect didn't fit. Grow the atlas and try again.
126 do {
Chris Dalton42c21152018-06-13 15:28:19 -0600127 if (fWidth == fMaxTextureSize && fHeight == fMaxTextureSize) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600128 return false;
129 }
130 if (fHeight <= fWidth) {
131 int top = fHeight;
Chris Dalton42c21152018-06-13 15:28:19 -0600132 fHeight = SkTMin(fHeight * 2, fMaxTextureSize);
Chris Dalton1a325d22017-07-14 15:17:41 -0600133 fTopNode = skstd::make_unique<Node>(std::move(fTopNode), 0, top, fWidth, fHeight);
134 } else {
135 int left = fWidth;
Chris Dalton42c21152018-06-13 15:28:19 -0600136 fWidth = SkTMin(fWidth * 2, fMaxTextureSize);
Chris Dalton1a325d22017-07-14 15:17:41 -0600137 fTopNode = skstd::make_unique<Node>(std::move(fTopNode), left, 0, fWidth, fHeight);
138 }
Chris Dalton42c21152018-06-13 15:28:19 -0600139 } while (!fTopNode->addRect(w, h, loc, fMaxTextureSize));
Chris Dalton1a325d22017-07-14 15:17:41 -0600140
141 return true;
142}
143
Chris Dalton09a7bb22018-08-31 19:53:15 +0800144void GrCCAtlas::setFillBatchID(int id) {
Chris Dalton4c458b12018-06-16 17:22:59 -0600145 // This can't be called anymore once makeRenderTargetContext() has been called.
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400146 SkASSERT(!fTextureProxy->isInstantiated());
Chris Dalton09a7bb22018-08-31 19:53:15 +0800147 fFillBatchID = id;
148}
149
150void GrCCAtlas::setStrokeBatchID(int id) {
151 // This can't be called anymore once makeRenderTargetContext() has been called.
152 SkASSERT(!fTextureProxy->isInstantiated());
153 fStrokeBatchID = id;
Chris Dalton4c458b12018-06-16 17:22:59 -0600154}
155
Chris Dalton4da70192018-06-18 09:51:36 -0600156static uint32_t next_atlas_unique_id() {
157 static int32_t nextID;
158 return sk_atomic_inc(&nextID);
159}
160
161const GrUniqueKey& GrCCAtlas::getOrAssignUniqueKey(GrOnFlushResourceProvider* onFlushRP) {
162 static const GrUniqueKey::Domain kAtlasDomain = GrUniqueKey::GenerateDomain();
163
164 if (!fUniqueKey.isValid()) {
165 GrUniqueKey::Builder builder(&fUniqueKey, kAtlasDomain, 1, "CCPR Atlas");
166 builder[0] = next_atlas_unique_id();
167 builder.finish();
168
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400169 if (fTextureProxy->isInstantiated()) {
Chris Dalton4da70192018-06-18 09:51:36 -0600170 onFlushRP->assignUniqueKeyToProxy(fUniqueKey, fTextureProxy.get());
171 }
172 }
173 return fUniqueKey;
174}
175
Chris Dalton8429c792018-10-23 15:56:22 -0600176sk_sp<GrCCAtlas::CachedAtlasInfo> GrCCAtlas::refOrMakeCachedAtlasInfo(uint32_t contextUniqueID) {
Chris Dalton4da70192018-06-18 09:51:36 -0600177 if (!fCachedAtlasInfo) {
Chris Dalton8429c792018-10-23 15:56:22 -0600178 fCachedAtlasInfo = sk_make_sp<CachedAtlasInfo>(contextUniqueID);
Chris Dalton4da70192018-06-18 09:51:36 -0600179 }
Chris Dalton8429c792018-10-23 15:56:22 -0600180 SkASSERT(fCachedAtlasInfo->fContextUniqueID == contextUniqueID);
Chris Dalton4da70192018-06-18 09:51:36 -0600181 return fCachedAtlasInfo;
182}
183
Chris Dalton4c458b12018-06-16 17:22:59 -0600184sk_sp<GrRenderTargetContext> GrCCAtlas::makeRenderTargetContext(
Chris Daltonafde18f2018-06-22 12:44:19 -0600185 GrOnFlushResourceProvider* onFlushRP, sk_sp<GrTexture> backingTexture) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400186 SkASSERT(!fTextureProxy->isInstantiated()); // This method should only be called once.
Chris Dalton4da70192018-06-18 09:51:36 -0600187 // Caller should have cropped any paths to the destination render target instead of asking for
188 // an atlas larger than maxRenderTargetSize.
189 SkASSERT(SkTMax(fHeight, fWidth) <= fMaxTextureSize);
Chris Dalton42c21152018-06-13 15:28:19 -0600190 SkASSERT(fMaxTextureSize <= onFlushRP->caps()->maxRenderTargetSize());
Chris Dalton1a325d22017-07-14 15:17:41 -0600191
Chris Daltonafde18f2018-06-22 12:44:19 -0600192 if (backingTexture) {
193 SkASSERT(backingTexture->config() == kAlpha_half_GrPixelConfig);
194 SkASSERT(backingTexture->width() == fWidth);
195 SkASSERT(backingTexture->height() == fHeight);
196 fBackingTexture = std::move(backingTexture);
197 }
198
Brian Salomon2a4f9832018-03-03 22:43:43 -0500199 sk_sp<GrRenderTargetContext> rtc =
Chris Dalton4c458b12018-06-16 17:22:59 -0600200 onFlushRP->makeRenderTargetContext(fTextureProxy, nullptr, nullptr);
Chris Dalton1a325d22017-07-14 15:17:41 -0600201 if (!rtc) {
202 SkDebugf("WARNING: failed to allocate a %ix%i atlas. Some paths will not be drawn.\n",
203 fWidth, fHeight);
204 return nullptr;
205 }
206
Chris Dalton4da70192018-06-18 09:51:36 -0600207 if (fUniqueKey.isValid()) {
208 onFlushRP->assignUniqueKeyToProxy(fUniqueKey, fTextureProxy.get());
209 }
210
Chris Dalton1a325d22017-07-14 15:17:41 -0600211 SkIRect clearRect = SkIRect::MakeSize(fDrawBounds);
Brian Osman9a9baae2018-11-05 15:06:26 -0500212 rtc->clear(&clearRect, SK_PMColor4fTRANSPARENT,
213 GrRenderTargetContext::CanClearFullscreen::kYes);
Chris Dalton1a325d22017-07-14 15:17:41 -0600214 return rtc;
215}
Chris Dalton9414c962018-06-14 10:14:50 -0600216
Chris Dalton4c458b12018-06-16 17:22:59 -0600217GrCCAtlas* GrCCAtlasStack::addRect(const SkIRect& devIBounds, SkIVector* devToAtlasOffset) {
Chris Dalton9414c962018-06-14 10:14:50 -0600218 GrCCAtlas* retiredAtlas = nullptr;
Chris Dalton4c458b12018-06-16 17:22:59 -0600219 if (fAtlases.empty() || !fAtlases.back().addRect(devIBounds, devToAtlasOffset)) {
Chris Dalton9414c962018-06-14 10:14:50 -0600220 // The retired atlas is out of room and can't grow any bigger.
221 retiredAtlas = !fAtlases.empty() ? &fAtlases.back() : nullptr;
Chris Dalton4c458b12018-06-16 17:22:59 -0600222 fAtlases.emplace_back(fPixelConfig, fSpecs, *fCaps);
Chris Dalton9414c962018-06-14 10:14:50 -0600223 SkASSERT(devIBounds.width() <= fSpecs.fMinWidth);
224 SkASSERT(devIBounds.height() <= fSpecs.fMinHeight);
Chris Dalton4c458b12018-06-16 17:22:59 -0600225 SkAssertResult(fAtlases.back().addRect(devIBounds, devToAtlasOffset));
Chris Dalton9414c962018-06-14 10:14:50 -0600226 }
227 return retiredAtlas;
228}