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 | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 19 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 20 | class GrCCAtlas::Node { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 21 | public: |
| 22 | 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] | 23 | : 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] | 24 | |
| 25 | Node* previous() const { return fPrevious.get(); } |
| 26 | |
Chris Dalton | 2612bae | 2018-02-22 13:41:37 -0700 | [diff] [blame] | 27 | 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 Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 36 | return false; |
| 37 | } |
| 38 | loc->fX += fX; |
| 39 | loc->fY += fY; |
| 40 | return true; |
| 41 | } |
| 42 | |
| 43 | private: |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 44 | const std::unique_ptr<Node> fPrevious; |
| 45 | const int fX, fY; |
| 46 | GrRectanizerSkyline fRectanizer; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 47 | }; |
| 48 | |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 49 | GrCCAtlas::GrCCAtlas(GrPixelConfig pixelConfig, const Specs& specs, const GrCaps& caps) |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 50 | : fMaxTextureSize(SkTMax(SkTMax(specs.fMinHeight, specs.fMinWidth), |
| 51 | specs.fMaxPreferredTextureSize)) { |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 52 | // 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 Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 55 | 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 Dalton | 2612bae | 2018-02-22 13:41:37 -0700 | [diff] [blame] | 73 | fTopNode = skstd::make_unique<Node>(nullptr, 0, 0, fWidth, fHeight); |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 74 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame^] | 75 | // 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 Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 82 | fTextureProxy = GrProxyProvider::MakeFullyLazyProxy( |
| 83 | [this, pixelConfig](GrResourceProvider* resourceProvider) { |
| 84 | if (!resourceProvider) { |
| 85 | return sk_sp<GrTexture>(); |
| 86 | } |
Chris Dalton | afde18f | 2018-06-22 12:44:19 -0600 | [diff] [blame] | 87 | 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 Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 96 | }, |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame^] | 97 | format, GrProxyProvider::Renderable::kYes, kTextureOrigin, pixelConfig, caps); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 98 | } |
| 99 | |
Chris Dalton | 2612bae | 2018-02-22 13:41:37 -0700 | [diff] [blame] | 100 | GrCCAtlas::~GrCCAtlas() { |
| 101 | } |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 102 | |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 103 | bool GrCCAtlas::addRect(const SkIRect& devIBounds, SkIVector* offset) { |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 104 | // This can't be called anymore once makeRenderTargetContext() has been called. |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 105 | SkASSERT(!fTextureProxy->isInstantiated()); |
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 | SkIPoint16 location; |
| 108 | if (!this->internalPlaceRect(devIBounds.width(), devIBounds.height(), &location)) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 109 | return false; |
| 110 | } |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 111 | offset->set(location.x() - devIBounds.left(), location.y() - devIBounds.top()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 112 | |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 113 | fDrawBounds.fWidth = SkTMax(fDrawBounds.width(), location.x() + devIBounds.width()); |
| 114 | fDrawBounds.fHeight = SkTMax(fDrawBounds.height(), location.y() + devIBounds.height()); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 115 | return true; |
| 116 | } |
| 117 | |
Chris Dalton | 383a2ef | 2018-01-08 17:21:41 -0500 | [diff] [blame] | 118 | bool GrCCAtlas::internalPlaceRect(int w, int h, SkIPoint16* loc) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 119 | for (Node* node = fTopNode.get(); node; node = node->previous()) { |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 120 | if (node->addRect(w, h, loc, fMaxTextureSize)) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 121 | return true; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // The rect didn't fit. Grow the atlas and try again. |
| 126 | do { |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 127 | if (fWidth == fMaxTextureSize && fHeight == fMaxTextureSize) { |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 128 | return false; |
| 129 | } |
| 130 | if (fHeight <= fWidth) { |
| 131 | int top = fHeight; |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 132 | fHeight = SkTMin(fHeight * 2, fMaxTextureSize); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 133 | fTopNode = skstd::make_unique<Node>(std::move(fTopNode), 0, top, fWidth, fHeight); |
| 134 | } else { |
| 135 | int left = fWidth; |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 136 | fWidth = SkTMin(fWidth * 2, fMaxTextureSize); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 137 | fTopNode = skstd::make_unique<Node>(std::move(fTopNode), left, 0, fWidth, fHeight); |
| 138 | } |
Chris Dalton | 42c2115 | 2018-06-13 15:28:19 -0600 | [diff] [blame] | 139 | } while (!fTopNode->addRect(w, h, loc, fMaxTextureSize)); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 140 | |
| 141 | return true; |
| 142 | } |
| 143 | |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 144 | void GrCCAtlas::setFillBatchID(int id) { |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 145 | // This can't be called anymore once makeRenderTargetContext() has been called. |
Brian Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 146 | SkASSERT(!fTextureProxy->isInstantiated()); |
Chris Dalton | 09a7bb2 | 2018-08-31 19:53:15 +0800 | [diff] [blame] | 147 | fFillBatchID = id; |
| 148 | } |
| 149 | |
| 150 | void GrCCAtlas::setStrokeBatchID(int id) { |
| 151 | // This can't be called anymore once makeRenderTargetContext() has been called. |
| 152 | SkASSERT(!fTextureProxy->isInstantiated()); |
| 153 | fStrokeBatchID = id; |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 154 | } |
| 155 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 156 | static uint32_t next_atlas_unique_id() { |
| 157 | static int32_t nextID; |
| 158 | return sk_atomic_inc(&nextID); |
| 159 | } |
| 160 | |
| 161 | const 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 Salomon | fd98c2c | 2018-07-31 17:25:29 -0400 | [diff] [blame] | 169 | if (fTextureProxy->isInstantiated()) { |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 170 | onFlushRP->assignUniqueKeyToProxy(fUniqueKey, fTextureProxy.get()); |
| 171 | } |
| 172 | } |
| 173 | return fUniqueKey; |
| 174 | } |
| 175 | |
Chris Dalton | 8429c79 | 2018-10-23 15:56:22 -0600 | [diff] [blame] | 176 | sk_sp<GrCCAtlas::CachedAtlasInfo> GrCCAtlas::refOrMakeCachedAtlasInfo(uint32_t contextUniqueID) { |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 177 | if (!fCachedAtlasInfo) { |
Chris Dalton | 8429c79 | 2018-10-23 15:56:22 -0600 | [diff] [blame] | 178 | fCachedAtlasInfo = sk_make_sp<CachedAtlasInfo>(contextUniqueID); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 179 | } |
Chris Dalton | 8429c79 | 2018-10-23 15:56:22 -0600 | [diff] [blame] | 180 | SkASSERT(fCachedAtlasInfo->fContextUniqueID == contextUniqueID); |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 181 | return fCachedAtlasInfo; |
| 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 | |
Chris Dalton | 4da7019 | 2018-06-18 09:51:36 -0600 | [diff] [blame] | 207 | if (fUniqueKey.isValid()) { |
| 208 | onFlushRP->assignUniqueKeyToProxy(fUniqueKey, fTextureProxy.get()); |
| 209 | } |
| 210 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 211 | SkIRect clearRect = SkIRect::MakeSize(fDrawBounds); |
Brian Osman | 9a9baae | 2018-11-05 15:06:26 -0500 | [diff] [blame] | 212 | rtc->clear(&clearRect, SK_PMColor4fTRANSPARENT, |
| 213 | GrRenderTargetContext::CanClearFullscreen::kYes); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 214 | return rtc; |
| 215 | } |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 216 | |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 217 | GrCCAtlas* GrCCAtlasStack::addRect(const SkIRect& devIBounds, SkIVector* devToAtlasOffset) { |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 218 | GrCCAtlas* retiredAtlas = nullptr; |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 219 | if (fAtlases.empty() || !fAtlases.back().addRect(devIBounds, devToAtlasOffset)) { |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 220 | // The retired atlas is out of room and can't grow any bigger. |
| 221 | retiredAtlas = !fAtlases.empty() ? &fAtlases.back() : nullptr; |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 222 | fAtlases.emplace_back(fPixelConfig, fSpecs, *fCaps); |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 223 | SkASSERT(devIBounds.width() <= fSpecs.fMinWidth); |
| 224 | SkASSERT(devIBounds.height() <= fSpecs.fMinHeight); |
Chris Dalton | 4c458b1 | 2018-06-16 17:22:59 -0600 | [diff] [blame] | 225 | SkAssertResult(fAtlases.back().addRect(devIBounds, devToAtlasOffset)); |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 226 | } |
| 227 | return retiredAtlas; |
| 228 | } |