blob: 4d147fdab44b7132621e3a3764b46b30e82b8c9b [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 Daltond6fa4542019-01-04 13:23:51 -070019#include "ccpr/GrCCPathCache.h"
Mike Klein0ec1c572018-12-04 11:52:51 -050020#include <atomic>
Chris Dalton1a325d22017-07-14 15:17:41 -060021
Chris Dalton383a2ef2018-01-08 17:21:41 -050022class GrCCAtlas::Node {
Chris Dalton1a325d22017-07-14 15:17:41 -060023public:
24 Node(std::unique_ptr<Node> previous, int l, int t, int r, int b)
Chris Dalton383a2ef2018-01-08 17:21:41 -050025 : fPrevious(std::move(previous)), fX(l), fY(t), fRectanizer(r - l, b - t) {}
Chris Dalton1a325d22017-07-14 15:17:41 -060026
27 Node* previous() const { return fPrevious.get(); }
28
Chris Dalton2612bae2018-02-22 13:41:37 -070029 bool addRect(int w, int h, SkIPoint16* loc, int maxAtlasSize) {
30 // Pad all paths except those that are expected to take up an entire physical texture.
31 if (w < maxAtlasSize) {
32 w = SkTMin(w + kPadding, maxAtlasSize);
33 }
34 if (h < maxAtlasSize) {
35 h = SkTMin(h + kPadding, maxAtlasSize);
36 }
37 if (!fRectanizer.addRect(w, h, loc)) {
Chris Dalton1a325d22017-07-14 15:17:41 -060038 return false;
39 }
40 loc->fX += fX;
41 loc->fY += fY;
42 return true;
43 }
44
45private:
Chris Dalton383a2ef2018-01-08 17:21:41 -050046 const std::unique_ptr<Node> fPrevious;
47 const int fX, fY;
48 GrRectanizerSkyline fRectanizer;
Chris Dalton1a325d22017-07-14 15:17:41 -060049};
50
Chris Daltond6fa4542019-01-04 13:23:51 -070051GrCCAtlas::GrCCAtlas(CoverageType coverageType, const Specs& specs, const GrCaps& caps)
52 : fCoverageType(coverageType)
53 , fMaxTextureSize(SkTMax(SkTMax(specs.fMinHeight, specs.fMinWidth),
Chris Dalton42c21152018-06-13 15:28:19 -060054 specs.fMaxPreferredTextureSize)) {
Chris Dalton4c458b12018-06-16 17:22:59 -060055 // Caller should have cropped any paths to the destination render target instead of asking for
56 // an atlas larger than maxRenderTargetSize.
57 SkASSERT(fMaxTextureSize <= caps.maxTextureSize());
Chris Dalton42c21152018-06-13 15:28:19 -060058 SkASSERT(specs.fMaxPreferredTextureSize > 0);
59
60 // Begin with the first pow2 dimensions whose area is theoretically large enough to contain the
61 // pending paths, favoring height over width if necessary.
62 int log2area = SkNextLog2(SkTMax(specs.fApproxNumPixels, 1));
63 fHeight = 1 << ((log2area + 1) / 2);
64 fWidth = 1 << (log2area / 2);
65
66 fWidth = SkTClamp(fWidth, specs.fMinTextureSize, specs.fMaxPreferredTextureSize);
67 fHeight = SkTClamp(fHeight, specs.fMinTextureSize, specs.fMaxPreferredTextureSize);
68
69 if (fWidth < specs.fMinWidth || fHeight < specs.fMinHeight) {
70 // They want to stuff a particularly large path into the atlas. Just punt and go with their
71 // min width and height. The atlas will grow as needed.
72 fWidth = SkTMin(specs.fMinWidth + kPadding, fMaxTextureSize);
73 fHeight = SkTMin(specs.fMinHeight + kPadding, fMaxTextureSize);
74 }
75
Chris Dalton2612bae2018-02-22 13:41:37 -070076 fTopNode = skstd::make_unique<Node>(nullptr, 0, 0, fWidth, fHeight);
Chris Dalton4c458b12018-06-16 17:22:59 -060077
Chris Daltond6fa4542019-01-04 13:23:51 -070078 GrColorType colorType = (CoverageType::kFP16_CoverageCount == fCoverageType)
79 ? GrColorType::kAlpha_F16 : GrColorType::kAlpha_8;
Greg Daniel4065d452018-11-16 15:43:41 -050080 const GrBackendFormat format =
Chris Daltond6fa4542019-01-04 13:23:51 -070081 caps.getBackendFormatFromGrColorType(colorType, GrSRGBEncoded::kNo);
82 GrPixelConfig pixelConfig = (CoverageType::kFP16_CoverageCount == fCoverageType)
83 ? kAlpha_half_GrPixelConfig : kAlpha_8_GrPixelConfig;
Greg Daniel4065d452018-11-16 15:43:41 -050084
Chris Dalton4c458b12018-06-16 17:22:59 -060085 fTextureProxy = GrProxyProvider::MakeFullyLazyProxy(
86 [this, pixelConfig](GrResourceProvider* resourceProvider) {
87 if (!resourceProvider) {
88 return sk_sp<GrTexture>();
89 }
Chris Daltonafde18f2018-06-22 12:44:19 -060090 if (!fBackingTexture) {
91 GrSurfaceDesc desc;
92 desc.fFlags = kRenderTarget_GrSurfaceFlag;
93 desc.fWidth = fWidth;
94 desc.fHeight = fHeight;
95 desc.fConfig = pixelConfig;
96 fBackingTexture = resourceProvider->createTexture(desc, SkBudgeted::kYes);
97 }
98 return fBackingTexture;
Chris Dalton4c458b12018-06-16 17:22:59 -060099 },
Greg Daniel4065d452018-11-16 15:43:41 -0500100 format, GrProxyProvider::Renderable::kYes, kTextureOrigin, pixelConfig, caps);
Chris Dalton1a325d22017-07-14 15:17:41 -0600101}
102
Chris Dalton2612bae2018-02-22 13:41:37 -0700103GrCCAtlas::~GrCCAtlas() {
104}
Chris Dalton1a325d22017-07-14 15:17:41 -0600105
Chris Dalton9414c962018-06-14 10:14:50 -0600106bool GrCCAtlas::addRect(const SkIRect& devIBounds, SkIVector* offset) {
Chris Dalton4c458b12018-06-16 17:22:59 -0600107 // This can't be called anymore once makeRenderTargetContext() has been called.
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400108 SkASSERT(!fTextureProxy->isInstantiated());
Chris Dalton1a325d22017-07-14 15:17:41 -0600109
Chris Dalton9414c962018-06-14 10:14:50 -0600110 SkIPoint16 location;
111 if (!this->internalPlaceRect(devIBounds.width(), devIBounds.height(), &location)) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600112 return false;
113 }
Chris Dalton9414c962018-06-14 10:14:50 -0600114 offset->set(location.x() - devIBounds.left(), location.y() - devIBounds.top());
Chris Dalton1a325d22017-07-14 15:17:41 -0600115
Chris Dalton9414c962018-06-14 10:14:50 -0600116 fDrawBounds.fWidth = SkTMax(fDrawBounds.width(), location.x() + devIBounds.width());
117 fDrawBounds.fHeight = SkTMax(fDrawBounds.height(), location.y() + devIBounds.height());
Chris Dalton1a325d22017-07-14 15:17:41 -0600118 return true;
119}
120
Chris Dalton383a2ef2018-01-08 17:21:41 -0500121bool GrCCAtlas::internalPlaceRect(int w, int h, SkIPoint16* loc) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600122 for (Node* node = fTopNode.get(); node; node = node->previous()) {
Chris Dalton42c21152018-06-13 15:28:19 -0600123 if (node->addRect(w, h, loc, fMaxTextureSize)) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600124 return true;
125 }
126 }
127
128 // The rect didn't fit. Grow the atlas and try again.
129 do {
Chris Dalton42c21152018-06-13 15:28:19 -0600130 if (fWidth == fMaxTextureSize && fHeight == fMaxTextureSize) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600131 return false;
132 }
133 if (fHeight <= fWidth) {
134 int top = fHeight;
Chris Dalton42c21152018-06-13 15:28:19 -0600135 fHeight = SkTMin(fHeight * 2, fMaxTextureSize);
Chris Dalton1a325d22017-07-14 15:17:41 -0600136 fTopNode = skstd::make_unique<Node>(std::move(fTopNode), 0, top, fWidth, fHeight);
137 } else {
138 int left = fWidth;
Chris Dalton42c21152018-06-13 15:28:19 -0600139 fWidth = SkTMin(fWidth * 2, fMaxTextureSize);
Chris Dalton1a325d22017-07-14 15:17:41 -0600140 fTopNode = skstd::make_unique<Node>(std::move(fTopNode), left, 0, fWidth, fHeight);
141 }
Chris Dalton42c21152018-06-13 15:28:19 -0600142 } while (!fTopNode->addRect(w, h, loc, fMaxTextureSize));
Chris Dalton1a325d22017-07-14 15:17:41 -0600143
144 return true;
145}
146
Chris Dalton09a7bb22018-08-31 19:53:15 +0800147void GrCCAtlas::setFillBatchID(int id) {
Chris Dalton4c458b12018-06-16 17:22:59 -0600148 // This can't be called anymore once makeRenderTargetContext() has been called.
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400149 SkASSERT(!fTextureProxy->isInstantiated());
Chris Dalton09a7bb22018-08-31 19:53:15 +0800150 fFillBatchID = id;
151}
152
153void GrCCAtlas::setStrokeBatchID(int id) {
154 // This can't be called anymore once makeRenderTargetContext() has been called.
155 SkASSERT(!fTextureProxy->isInstantiated());
156 fStrokeBatchID = id;
Chris Dalton4c458b12018-06-16 17:22:59 -0600157}
158
Chris Dalton4da70192018-06-18 09:51:36 -0600159static uint32_t next_atlas_unique_id() {
Mike Klein0ec1c572018-12-04 11:52:51 -0500160 static std::atomic<uint32_t> nextID;
161 return nextID++;
Chris Dalton4da70192018-06-18 09:51:36 -0600162}
163
Chris Daltond6fa4542019-01-04 13:23:51 -0700164sk_sp<GrCCCachedAtlas> GrCCAtlas::refOrMakeCachedAtlas(GrOnFlushResourceProvider* onFlushRP) {
165 if (!fCachedAtlas) {
166 static const GrUniqueKey::Domain kAtlasDomain = GrUniqueKey::GenerateDomain();
Chris Dalton4da70192018-06-18 09:51:36 -0600167
Chris Daltond6fa4542019-01-04 13:23:51 -0700168 GrUniqueKey atlasUniqueKey;
169 GrUniqueKey::Builder builder(&atlasUniqueKey, kAtlasDomain, 1, "CCPR Atlas");
Chris Dalton4da70192018-06-18 09:51:36 -0600170 builder[0] = next_atlas_unique_id();
171 builder.finish();
172
Chris Daltond6fa4542019-01-04 13:23:51 -0700173 onFlushRP->assignUniqueKeyToProxy(atlasUniqueKey, fTextureProxy.get());
Chris Dalton4da70192018-06-18 09:51:36 -0600174
Chris Daltond6fa4542019-01-04 13:23:51 -0700175 fCachedAtlas = sk_make_sp<GrCCCachedAtlas>(fCoverageType, atlasUniqueKey, fTextureProxy);
Chris Dalton4da70192018-06-18 09:51:36 -0600176 }
Chris Daltond6fa4542019-01-04 13:23:51 -0700177
178 SkASSERT(fCachedAtlas->coverageType() == fCoverageType);
179 SkASSERT(fCachedAtlas->getOnFlushProxy() == fTextureProxy.get());
180 return fCachedAtlas;
Chris Dalton4da70192018-06-18 09:51:36 -0600181}
182
Chris Dalton4c458b12018-06-16 17:22:59 -0600183sk_sp<GrRenderTargetContext> GrCCAtlas::makeRenderTargetContext(
Chris Daltonafde18f2018-06-22 12:44:19 -0600184 GrOnFlushResourceProvider* onFlushRP, sk_sp<GrTexture> backingTexture) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400185 SkASSERT(!fTextureProxy->isInstantiated()); // This method should only be called once.
Chris Dalton4da70192018-06-18 09:51:36 -0600186 // Caller should have cropped any paths to the destination render target instead of asking for
187 // an atlas larger than maxRenderTargetSize.
188 SkASSERT(SkTMax(fHeight, fWidth) <= fMaxTextureSize);
Chris Dalton42c21152018-06-13 15:28:19 -0600189 SkASSERT(fMaxTextureSize <= onFlushRP->caps()->maxRenderTargetSize());
Chris Dalton1a325d22017-07-14 15:17:41 -0600190
Chris Daltonafde18f2018-06-22 12:44:19 -0600191 if (backingTexture) {
192 SkASSERT(backingTexture->config() == kAlpha_half_GrPixelConfig);
193 SkASSERT(backingTexture->width() == fWidth);
194 SkASSERT(backingTexture->height() == fHeight);
195 fBackingTexture = std::move(backingTexture);
196 }
197
Brian Salomon2a4f9832018-03-03 22:43:43 -0500198 sk_sp<GrRenderTargetContext> rtc =
Chris Dalton4c458b12018-06-16 17:22:59 -0600199 onFlushRP->makeRenderTargetContext(fTextureProxy, nullptr, nullptr);
Chris Dalton1a325d22017-07-14 15:17:41 -0600200 if (!rtc) {
201 SkDebugf("WARNING: failed to allocate a %ix%i atlas. Some paths will not be drawn.\n",
202 fWidth, fHeight);
203 return nullptr;
204 }
205
206 SkIRect clearRect = SkIRect::MakeSize(fDrawBounds);
Brian Osman9a9baae2018-11-05 15:06:26 -0500207 rtc->clear(&clearRect, SK_PMColor4fTRANSPARENT,
208 GrRenderTargetContext::CanClearFullscreen::kYes);
Chris Dalton1a325d22017-07-14 15:17:41 -0600209 return rtc;
210}
Chris Dalton9414c962018-06-14 10:14:50 -0600211
Chris Dalton4c458b12018-06-16 17:22:59 -0600212GrCCAtlas* GrCCAtlasStack::addRect(const SkIRect& devIBounds, SkIVector* devToAtlasOffset) {
Chris Dalton9414c962018-06-14 10:14:50 -0600213 GrCCAtlas* retiredAtlas = nullptr;
Chris Dalton4c458b12018-06-16 17:22:59 -0600214 if (fAtlases.empty() || !fAtlases.back().addRect(devIBounds, devToAtlasOffset)) {
Chris Dalton9414c962018-06-14 10:14:50 -0600215 // The retired atlas is out of room and can't grow any bigger.
216 retiredAtlas = !fAtlases.empty() ? &fAtlases.back() : nullptr;
Chris Daltond6fa4542019-01-04 13:23:51 -0700217 fAtlases.emplace_back(fCoverageType, fSpecs, *fCaps);
Chris Dalton9414c962018-06-14 10:14:50 -0600218 SkASSERT(devIBounds.width() <= fSpecs.fMinWidth);
219 SkASSERT(devIBounds.height() <= fSpecs.fMinHeight);
Chris Dalton4c458b12018-06-16 17:22:59 -0600220 SkAssertResult(fAtlases.back().addRect(devIBounds, devToAtlasOffset));
Chris Dalton9414c962018-06-14 10:14:50 -0600221 }
222 return retiredAtlas;
223}