Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 1 | /* |
| 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/ccpr/GrCCAtlas.h" |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/gpu/GrTexture.h" |
| 11 | #include "include/private/GrTextureProxy.h" |
| 12 | #include "src/core/SkIPoint16.h" |
| 13 | #include "src/core/SkMakeUnique.h" |
| 14 | #include "src/core/SkMathPriv.h" |
| 15 | #include "src/gpu/GrCaps.h" |
| 16 | #include "src/gpu/GrOnFlushResourceProvider.h" |
| 17 | #include "src/gpu/GrProxyProvider.h" |
| 18 | #include "src/gpu/GrRectanizer_skyline.h" |
| 19 | #include "src/gpu/GrRenderTargetContext.h" |
| 20 | #include "src/gpu/ccpr/GrCCPathCache.h" |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 21 | #include <atomic> |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 22 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 23 | class GrCCAtlas::Node { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 24 | public: |
| 25 | Node(std::unique_ptr<Node> previous, int l, int t, int r, int b) |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 26 | : fPrevious(std::move(previous)), fX(l), fY(t), fRectanizer(r - l, b - t) {} |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 27 | |
| 28 | Node* previous() const { return fPrevious.get(); } |
| 29 | |
Chris Dalton | 2612bae | 2018-02-22 13:41:37 -0700 | [diff] [blame] | 30 | 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 Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 39 | return false; |
| 40 | } |
| 41 | loc->fX += fX; |
| 42 | loc->fY += fY; |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | private: |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 47 | const std::unique_ptr<Node> fPrevious; |
| 48 | const int fX, fY; |
| 49 | GrRectanizerSkyline fRectanizer; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 50 | }; |
| 51 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 52 | GrCCAtlas::GrCCAtlas(CoverageType coverageType, const Specs& specs, const GrCaps& caps) |
| 53 | : fCoverageType(coverageType) |
| 54 | , fMaxTextureSize(SkTMax(SkTMax(specs.fMinHeight, specs.fMinWidth), |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 55 | specs.fMaxPreferredTextureSize)) { |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 56 | // 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 Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 59 | 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 Dalton | 2612bae | 2018-02-22 13:41:37 -0700 | [diff] [blame] | 77 | fTopNode = skstd::make_unique<Node>(nullptr, 0, 0, fWidth, fHeight); |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 78 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 79 | GrColorType colorType = (CoverageType::kFP16_CoverageCount == fCoverageType) |
| 80 | ? GrColorType::kAlpha_F16 : GrColorType::kAlpha_8; |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 81 | const GrBackendFormat format = |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 82 | caps.getBackendFormatFromGrColorType(colorType, GrSRGBEncoded::kNo); |
| 83 | GrPixelConfig pixelConfig = (CoverageType::kFP16_CoverageCount == fCoverageType) |
| 84 | ? kAlpha_half_GrPixelConfig : kAlpha_8_GrPixelConfig; |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 85 | |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 86 | fTextureProxy = GrProxyProvider::MakeFullyLazyProxy( |
| 87 | [this, pixelConfig](GrResourceProvider* resourceProvider) { |
Chris Dalton | afde18f | 2018-06-22 12:44:19 -0600 | [diff] [blame] | 88 | if (!fBackingTexture) { |
| 89 | GrSurfaceDesc desc; |
| 90 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 91 | desc.fWidth = fWidth; |
| 92 | desc.fHeight = fHeight; |
| 93 | desc.fConfig = pixelConfig; |
Robert Phillips | 9313aa7 | 2019-04-09 18:41:27 -0400 | [diff] [blame] | 94 | fBackingTexture = resourceProvider->createTexture( |
| 95 | desc, SkBudgeted::kYes, GrResourceProvider::Flags::kNoPendingIO); |
Chris Dalton | afde18f | 2018-06-22 12:44:19 -0600 | [diff] [blame] | 96 | } |
Brian Salomon | b6a3a3b | 2019-04-01 12:29:34 -0400 | [diff] [blame] | 97 | return GrSurfaceProxy::LazyInstantiationResult(fBackingTexture); |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 98 | }, |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 99 | format, GrProxyProvider::Renderable::kYes, kTextureOrigin, pixelConfig, caps); |
Robert Phillips | 5f78adf | 2019-04-22 12:41:39 -0400 | [diff] [blame] | 100 | |
| 101 | fTextureProxy->priv().setIgnoredByResourceAllocator(); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 102 | } |
| 103 | |
Chris Dalton | 2612bae | 2018-02-22 13:41:37 -0700 | [diff] [blame] | 104 | GrCCAtlas::~GrCCAtlas() { |
| 105 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 106 | |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 107 | bool GrCCAtlas::addRect(const SkIRect& devIBounds, SkIVector* offset) { |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 108 | // This can't be called anymore once makeRenderTargetContext() has been called. |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 109 | SkASSERT(!fTextureProxy->isInstantiated()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 110 | |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 111 | SkIPoint16 location; |
| 112 | if (!this->internalPlaceRect(devIBounds.width(), devIBounds.height(), &location)) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 113 | return false; |
| 114 | } |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 115 | offset->set(location.x() - devIBounds.left(), location.y() - devIBounds.top()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 116 | |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 117 | fDrawBounds.fWidth = SkTMax(fDrawBounds.width(), location.x() + devIBounds.width()); |
| 118 | fDrawBounds.fHeight = SkTMax(fDrawBounds.height(), location.y() + devIBounds.height()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 119 | return true; |
| 120 | } |
| 121 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 122 | bool GrCCAtlas::internalPlaceRect(int w, int h, SkIPoint16* loc) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 123 | for (Node* node = fTopNode.get(); node; node = node->previous()) { |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 124 | if (node->addRect(w, h, loc, fMaxTextureSize)) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 125 | return true; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // The rect didn't fit. Grow the atlas and try again. |
| 130 | do { |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 131 | if (fWidth == fMaxTextureSize && fHeight == fMaxTextureSize) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 132 | return false; |
| 133 | } |
| 134 | if (fHeight <= fWidth) { |
| 135 | int top = fHeight; |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 136 | fHeight = SkTMin(fHeight * 2, fMaxTextureSize); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 137 | fTopNode = skstd::make_unique<Node>(std::move(fTopNode), 0, top, fWidth, fHeight); |
| 138 | } else { |
| 139 | int left = fWidth; |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 140 | fWidth = SkTMin(fWidth * 2, fMaxTextureSize); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 141 | fTopNode = skstd::make_unique<Node>(std::move(fTopNode), left, 0, fWidth, fHeight); |
| 142 | } |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 143 | } while (!fTopNode->addRect(w, h, loc, fMaxTextureSize)); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 144 | |
| 145 | return true; |
| 146 | } |
| 147 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 148 | void GrCCAtlas::setFillBatchID(int id) { |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 149 | // This can't be called anymore once makeRenderTargetContext() has been called. |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 150 | SkASSERT(!fTextureProxy->isInstantiated()); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 151 | fFillBatchID = id; |
| 152 | } |
| 153 | |
| 154 | void GrCCAtlas::setStrokeBatchID(int id) { |
| 155 | // This can't be called anymore once makeRenderTargetContext() has been called. |
| 156 | SkASSERT(!fTextureProxy->isInstantiated()); |
| 157 | fStrokeBatchID = id; |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 158 | } |
| 159 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 160 | static uint32_t next_atlas_unique_id() { |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 161 | static std::atomic<uint32_t> nextID; |
| 162 | return nextID++; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 163 | } |
| 164 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 165 | sk_sp<GrCCCachedAtlas> GrCCAtlas::refOrMakeCachedAtlas(GrOnFlushResourceProvider* onFlushRP) { |
| 166 | if (!fCachedAtlas) { |
| 167 | static const GrUniqueKey::Domain kAtlasDomain = GrUniqueKey::GenerateDomain(); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 168 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 169 | GrUniqueKey atlasUniqueKey; |
| 170 | GrUniqueKey::Builder builder(&atlasUniqueKey, kAtlasDomain, 1, "CCPR Atlas"); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 171 | builder[0] = next_atlas_unique_id(); |
| 172 | builder.finish(); |
| 173 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 174 | onFlushRP->assignUniqueKeyToProxy(atlasUniqueKey, fTextureProxy.get()); |
Chris Dalton | d6fa454 | 2019-01-04 13:23:51 -0700 | [diff] [blame] | 175 | |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 176 | fCachedAtlas = sk_make_sp<GrCCCachedAtlas>(fCoverageType, atlasUniqueKey, fTextureProxy); |
Chris Dalton | 2e825a3 | 2019-01-04 22:14:27 +0000 | [diff] [blame] | 177 | } |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 178 | |
| 179 | SkASSERT(fCachedAtlas->coverageType() == fCoverageType); |
| 180 | SkASSERT(fCachedAtlas->getOnFlushProxy() == fTextureProxy.get()); |
| 181 | return fCachedAtlas; |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 182 | } |
| 183 | |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 184 | sk_sp<GrRenderTargetContext> GrCCAtlas::makeRenderTargetContext( |
Chris Dalton | afde18f | 2018-06-22 12:44:19 -0600 | [diff] [blame] | 185 | GrOnFlushResourceProvider* onFlushRP, sk_sp<GrTexture> backingTexture) { |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 186 | SkASSERT(!fTextureProxy->isInstantiated()); // This method should only be called once. |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 187 | // 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 Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 190 | SkASSERT(fMaxTextureSize <= onFlushRP->caps()->maxRenderTargetSize()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 191 | |
Chris Dalton | afde18f | 2018-06-22 12:44:19 -0600 | [diff] [blame] | 192 | 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 Salomon | 2a4f983 | 2018-03-03 22:43:43 -0500 | [diff] [blame] | 199 | sk_sp<GrRenderTargetContext> rtc = |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 200 | onFlushRP->makeRenderTargetContext(fTextureProxy, nullptr, nullptr); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 201 | 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 | |
| 207 | SkIRect clearRect = SkIRect::MakeSize(fDrawBounds); |
Brian Osman | 9a9baae | 2018-11-05 15:06:26 -0500 | [diff] [blame] | 208 | rtc->clear(&clearRect, SK_PMColor4fTRANSPARENT, |
| 209 | GrRenderTargetContext::CanClearFullscreen::kYes); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 210 | return rtc; |
| 211 | } |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 212 | |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 213 | GrCCAtlas* GrCCAtlasStack::addRect(const SkIRect& devIBounds, SkIVector* devToAtlasOffset) { |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 214 | GrCCAtlas* retiredAtlas = nullptr; |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 215 | if (fAtlases.empty() || !fAtlases.back().addRect(devIBounds, devToAtlasOffset)) { |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 216 | // The retired atlas is out of room and can't grow any bigger. |
| 217 | retiredAtlas = !fAtlases.empty() ? &fAtlases.back() : nullptr; |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 218 | fAtlases.emplace_back(fCoverageType, fSpecs, *fCaps); |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 219 | SkASSERT(devIBounds.width() <= fSpecs.fMinWidth); |
| 220 | SkASSERT(devIBounds.height() <= fSpecs.fMinHeight); |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 221 | SkAssertResult(fAtlases.back().addRect(devIBounds, devToAtlasOffset)); |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 222 | } |
| 223 | return retiredAtlas; |
| 224 | } |