blob: 82b06f048b18404551d431d257bbca9299830fb5 [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"
Robert Phillipsbe9aff22019-02-15 11:33:22 -050017#include "SkIPoint16.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060018#include "SkMakeUnique.h"
19#include "SkMathPriv.h"
Chris Dalton351e80c2019-01-06 22:51:00 -070020#include "ccpr/GrCCPathCache.h"
Mike Klein0ec1c572018-12-04 11:52:51 -050021#include <atomic>
Chris Dalton1a325d22017-07-14 15:17:41 -060022
Chris Dalton383a2ef2018-01-08 17:21:41 -050023class GrCCAtlas::Node {
Chris Dalton1a325d22017-07-14 15:17:41 -060024public:
25 Node(std::unique_ptr<Node> previous, int l, int t, int r, int b)
Chris Dalton383a2ef2018-01-08 17:21:41 -050026 : fPrevious(std::move(previous)), fX(l), fY(t), fRectanizer(r - l, b - t) {}
Chris Dalton1a325d22017-07-14 15:17:41 -060027
28 Node* previous() const { return fPrevious.get(); }
29
Chris Dalton2612bae2018-02-22 13:41:37 -070030 bool addRect(int w, int h, SkIPoint16* loc, int maxAtlasSize) {
31 // Pad all paths except those that are expected to take up an entire physical texture.
32 if (w < maxAtlasSize) {
33 w = SkTMin(w + kPadding, maxAtlasSize);
34 }
35 if (h < maxAtlasSize) {
36 h = SkTMin(h + kPadding, maxAtlasSize);
37 }
38 if (!fRectanizer.addRect(w, h, loc)) {
Chris Dalton1a325d22017-07-14 15:17:41 -060039 return false;
40 }
41 loc->fX += fX;
42 loc->fY += fY;
43 return true;
44 }
45
46private:
Chris Dalton383a2ef2018-01-08 17:21:41 -050047 const std::unique_ptr<Node> fPrevious;
48 const int fX, fY;
49 GrRectanizerSkyline fRectanizer;
Chris Dalton1a325d22017-07-14 15:17:41 -060050};
51
Chris Dalton351e80c2019-01-06 22:51:00 -070052GrCCAtlas::GrCCAtlas(CoverageType coverageType, const Specs& specs, const GrCaps& caps)
53 : fCoverageType(coverageType)
54 , fMaxTextureSize(SkTMax(SkTMax(specs.fMinHeight, specs.fMinWidth),
Chris Dalton42c21152018-06-13 15:28:19 -060055 specs.fMaxPreferredTextureSize)) {
Chris Dalton4c458b12018-06-16 17:22:59 -060056 // Caller should have cropped any paths to the destination render target instead of asking for
57 // an atlas larger than maxRenderTargetSize.
58 SkASSERT(fMaxTextureSize <= caps.maxTextureSize());
Chris Dalton42c21152018-06-13 15:28:19 -060059 SkASSERT(specs.fMaxPreferredTextureSize > 0);
60
61 // Begin with the first pow2 dimensions whose area is theoretically large enough to contain the
62 // pending paths, favoring height over width if necessary.
63 int log2area = SkNextLog2(SkTMax(specs.fApproxNumPixels, 1));
64 fHeight = 1 << ((log2area + 1) / 2);
65 fWidth = 1 << (log2area / 2);
66
67 fWidth = SkTClamp(fWidth, specs.fMinTextureSize, specs.fMaxPreferredTextureSize);
68 fHeight = SkTClamp(fHeight, specs.fMinTextureSize, specs.fMaxPreferredTextureSize);
69
70 if (fWidth < specs.fMinWidth || fHeight < specs.fMinHeight) {
71 // They want to stuff a particularly large path into the atlas. Just punt and go with their
72 // min width and height. The atlas will grow as needed.
73 fWidth = SkTMin(specs.fMinWidth + kPadding, fMaxTextureSize);
74 fHeight = SkTMin(specs.fMinHeight + kPadding, fMaxTextureSize);
75 }
76
Chris Dalton2612bae2018-02-22 13:41:37 -070077 fTopNode = skstd::make_unique<Node>(nullptr, 0, 0, fWidth, fHeight);
Chris Dalton4c458b12018-06-16 17:22:59 -060078
Chris Dalton351e80c2019-01-06 22:51:00 -070079 GrColorType colorType = (CoverageType::kFP16_CoverageCount == fCoverageType)
80 ? GrColorType::kAlpha_F16 : GrColorType::kAlpha_8;
Greg Daniel4065d452018-11-16 15:43:41 -050081 const GrBackendFormat format =
Chris Dalton351e80c2019-01-06 22:51:00 -070082 caps.getBackendFormatFromGrColorType(colorType, GrSRGBEncoded::kNo);
83 GrPixelConfig pixelConfig = (CoverageType::kFP16_CoverageCount == fCoverageType)
84 ? kAlpha_half_GrPixelConfig : kAlpha_8_GrPixelConfig;
Greg Daniel4065d452018-11-16 15:43:41 -050085
Chris Dalton4c458b12018-06-16 17:22:59 -060086 fTextureProxy = GrProxyProvider::MakeFullyLazyProxy(
87 [this, pixelConfig](GrResourceProvider* resourceProvider) {
Chris Daltonafde18f2018-06-22 12:44:19 -060088 if (!fBackingTexture) {
89 GrSurfaceDesc desc;
90 desc.fFlags = kRenderTarget_GrSurfaceFlag;
91 desc.fWidth = fWidth;
92 desc.fHeight = fHeight;
93 desc.fConfig = pixelConfig;
94 fBackingTexture = resourceProvider->createTexture(desc, SkBudgeted::kYes);
95 }
Brian Salomonb6a3a3b2019-04-01 12:29:34 -040096 return GrSurfaceProxy::LazyInstantiationResult(fBackingTexture);
Chris Dalton4c458b12018-06-16 17:22:59 -060097 },
Greg Daniel4065d452018-11-16 15:43:41 -050098 format, GrProxyProvider::Renderable::kYes, kTextureOrigin, pixelConfig, caps);
Chris Dalton1a325d22017-07-14 15:17:41 -060099}
100
Chris Dalton2612bae2018-02-22 13:41:37 -0700101GrCCAtlas::~GrCCAtlas() {
102}
Chris Dalton1a325d22017-07-14 15:17:41 -0600103
Chris Dalton9414c962018-06-14 10:14:50 -0600104bool GrCCAtlas::addRect(const SkIRect& devIBounds, SkIVector* offset) {
Chris Dalton4c458b12018-06-16 17:22:59 -0600105 // This can't be called anymore once makeRenderTargetContext() has been called.
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400106 SkASSERT(!fTextureProxy->isInstantiated());
Chris Dalton1a325d22017-07-14 15:17:41 -0600107
Chris Dalton9414c962018-06-14 10:14:50 -0600108 SkIPoint16 location;
109 if (!this->internalPlaceRect(devIBounds.width(), devIBounds.height(), &location)) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600110 return false;
111 }
Chris Dalton9414c962018-06-14 10:14:50 -0600112 offset->set(location.x() - devIBounds.left(), location.y() - devIBounds.top());
Chris Dalton1a325d22017-07-14 15:17:41 -0600113
Chris Dalton9414c962018-06-14 10:14:50 -0600114 fDrawBounds.fWidth = SkTMax(fDrawBounds.width(), location.x() + devIBounds.width());
115 fDrawBounds.fHeight = SkTMax(fDrawBounds.height(), location.y() + devIBounds.height());
Chris Dalton1a325d22017-07-14 15:17:41 -0600116 return true;
117}
118
Chris Dalton383a2ef2018-01-08 17:21:41 -0500119bool GrCCAtlas::internalPlaceRect(int w, int h, SkIPoint16* loc) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600120 for (Node* node = fTopNode.get(); node; node = node->previous()) {
Chris Dalton42c21152018-06-13 15:28:19 -0600121 if (node->addRect(w, h, loc, fMaxTextureSize)) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600122 return true;
123 }
124 }
125
126 // The rect didn't fit. Grow the atlas and try again.
127 do {
Chris Dalton42c21152018-06-13 15:28:19 -0600128 if (fWidth == fMaxTextureSize && fHeight == fMaxTextureSize) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600129 return false;
130 }
131 if (fHeight <= fWidth) {
132 int top = fHeight;
Chris Dalton42c21152018-06-13 15:28:19 -0600133 fHeight = SkTMin(fHeight * 2, fMaxTextureSize);
Chris Dalton1a325d22017-07-14 15:17:41 -0600134 fTopNode = skstd::make_unique<Node>(std::move(fTopNode), 0, top, fWidth, fHeight);
135 } else {
136 int left = fWidth;
Chris Dalton42c21152018-06-13 15:28:19 -0600137 fWidth = SkTMin(fWidth * 2, fMaxTextureSize);
Chris Dalton1a325d22017-07-14 15:17:41 -0600138 fTopNode = skstd::make_unique<Node>(std::move(fTopNode), left, 0, fWidth, fHeight);
139 }
Chris Dalton42c21152018-06-13 15:28:19 -0600140 } while (!fTopNode->addRect(w, h, loc, fMaxTextureSize));
Chris Dalton1a325d22017-07-14 15:17:41 -0600141
142 return true;
143}
144
Chris Dalton09a7bb22018-08-31 19:53:15 +0800145void GrCCAtlas::setFillBatchID(int id) {
Chris Dalton4c458b12018-06-16 17:22:59 -0600146 // This can't be called anymore once makeRenderTargetContext() has been called.
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400147 SkASSERT(!fTextureProxy->isInstantiated());
Chris Dalton09a7bb22018-08-31 19:53:15 +0800148 fFillBatchID = id;
149}
150
151void GrCCAtlas::setStrokeBatchID(int id) {
152 // This can't be called anymore once makeRenderTargetContext() has been called.
153 SkASSERT(!fTextureProxy->isInstantiated());
154 fStrokeBatchID = id;
Chris Dalton4c458b12018-06-16 17:22:59 -0600155}
156
Chris Dalton4da70192018-06-18 09:51:36 -0600157static uint32_t next_atlas_unique_id() {
Mike Klein0ec1c572018-12-04 11:52:51 -0500158 static std::atomic<uint32_t> nextID;
159 return nextID++;
Chris Dalton4da70192018-06-18 09:51:36 -0600160}
161
Chris Dalton351e80c2019-01-06 22:51:00 -0700162sk_sp<GrCCCachedAtlas> GrCCAtlas::refOrMakeCachedAtlas(GrOnFlushResourceProvider* onFlushRP) {
163 if (!fCachedAtlas) {
164 static const GrUniqueKey::Domain kAtlasDomain = GrUniqueKey::GenerateDomain();
Chris Dalton4da70192018-06-18 09:51:36 -0600165
Chris Dalton351e80c2019-01-06 22:51:00 -0700166 GrUniqueKey atlasUniqueKey;
167 GrUniqueKey::Builder builder(&atlasUniqueKey, kAtlasDomain, 1, "CCPR Atlas");
Chris Dalton4da70192018-06-18 09:51:36 -0600168 builder[0] = next_atlas_unique_id();
169 builder.finish();
170
Chris Dalton351e80c2019-01-06 22:51:00 -0700171 onFlushRP->assignUniqueKeyToProxy(atlasUniqueKey, fTextureProxy.get());
Chris Daltond6fa4542019-01-04 13:23:51 -0700172
Chris Dalton351e80c2019-01-06 22:51:00 -0700173 fCachedAtlas = sk_make_sp<GrCCCachedAtlas>(fCoverageType, atlasUniqueKey, fTextureProxy);
Chris Dalton2e825a32019-01-04 22:14:27 +0000174 }
Chris Dalton351e80c2019-01-06 22:51:00 -0700175
176 SkASSERT(fCachedAtlas->coverageType() == fCoverageType);
177 SkASSERT(fCachedAtlas->getOnFlushProxy() == fTextureProxy.get());
178 return fCachedAtlas;
Chris Dalton4da70192018-06-18 09:51:36 -0600179}
180
Chris Dalton4c458b12018-06-16 17:22:59 -0600181sk_sp<GrRenderTargetContext> GrCCAtlas::makeRenderTargetContext(
Chris Daltonafde18f2018-06-22 12:44:19 -0600182 GrOnFlushResourceProvider* onFlushRP, sk_sp<GrTexture> backingTexture) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400183 SkASSERT(!fTextureProxy->isInstantiated()); // This method should only be called once.
Chris Dalton4da70192018-06-18 09:51:36 -0600184 // Caller should have cropped any paths to the destination render target instead of asking for
185 // an atlas larger than maxRenderTargetSize.
186 SkASSERT(SkTMax(fHeight, fWidth) <= fMaxTextureSize);
Chris Dalton42c21152018-06-13 15:28:19 -0600187 SkASSERT(fMaxTextureSize <= onFlushRP->caps()->maxRenderTargetSize());
Chris Dalton1a325d22017-07-14 15:17:41 -0600188
Chris Daltonafde18f2018-06-22 12:44:19 -0600189 if (backingTexture) {
190 SkASSERT(backingTexture->config() == kAlpha_half_GrPixelConfig);
191 SkASSERT(backingTexture->width() == fWidth);
192 SkASSERT(backingTexture->height() == fHeight);
193 fBackingTexture = std::move(backingTexture);
194 }
195
Brian Salomon2a4f9832018-03-03 22:43:43 -0500196 sk_sp<GrRenderTargetContext> rtc =
Chris Dalton4c458b12018-06-16 17:22:59 -0600197 onFlushRP->makeRenderTargetContext(fTextureProxy, nullptr, nullptr);
Chris Dalton1a325d22017-07-14 15:17:41 -0600198 if (!rtc) {
199 SkDebugf("WARNING: failed to allocate a %ix%i atlas. Some paths will not be drawn.\n",
200 fWidth, fHeight);
201 return nullptr;
202 }
203
204 SkIRect clearRect = SkIRect::MakeSize(fDrawBounds);
Brian Osman9a9baae2018-11-05 15:06:26 -0500205 rtc->clear(&clearRect, SK_PMColor4fTRANSPARENT,
206 GrRenderTargetContext::CanClearFullscreen::kYes);
Chris Dalton1a325d22017-07-14 15:17:41 -0600207 return rtc;
208}
Chris Dalton9414c962018-06-14 10:14:50 -0600209
Chris Dalton4c458b12018-06-16 17:22:59 -0600210GrCCAtlas* GrCCAtlasStack::addRect(const SkIRect& devIBounds, SkIVector* devToAtlasOffset) {
Chris Dalton9414c962018-06-14 10:14:50 -0600211 GrCCAtlas* retiredAtlas = nullptr;
Chris Dalton4c458b12018-06-16 17:22:59 -0600212 if (fAtlases.empty() || !fAtlases.back().addRect(devIBounds, devToAtlasOffset)) {
Chris Dalton9414c962018-06-14 10:14:50 -0600213 // The retired atlas is out of room and can't grow any bigger.
214 retiredAtlas = !fAtlases.empty() ? &fAtlases.back() : nullptr;
Chris Dalton351e80c2019-01-06 22:51:00 -0700215 fAtlases.emplace_back(fCoverageType, fSpecs, *fCaps);
Chris Dalton9414c962018-06-14 10:14:50 -0600216 SkASSERT(devIBounds.width() <= fSpecs.fMinWidth);
217 SkASSERT(devIBounds.height() <= fSpecs.fMinHeight);
Chris Dalton4c458b12018-06-16 17:22:59 -0600218 SkAssertResult(fAtlases.back().addRect(devIBounds, devToAtlasOffset));
Chris Dalton9414c962018-06-14 10:14:50 -0600219 }
220 return retiredAtlas;
221}