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