blob: 94a37e810f3ecd9c38d4649bf912865275f35591 [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 Dalton1a325d22017-07-14 15:17:41 -060019
Chris Dalton383a2ef2018-01-08 17:21:41 -050020class GrCCAtlas::Node {
Chris Dalton1a325d22017-07-14 15:17:41 -060021public:
22 Node(std::unique_ptr<Node> previous, int l, int t, int r, int b)
Chris Dalton383a2ef2018-01-08 17:21:41 -050023 : fPrevious(std::move(previous)), fX(l), fY(t), fRectanizer(r - l, b - t) {}
Chris Dalton1a325d22017-07-14 15:17:41 -060024
25 Node* previous() const { return fPrevious.get(); }
26
Chris Dalton2612bae2018-02-22 13:41:37 -070027 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 Dalton1a325d22017-07-14 15:17:41 -060036 return false;
37 }
38 loc->fX += fX;
39 loc->fY += fY;
40 return true;
41 }
42
43private:
Chris Dalton383a2ef2018-01-08 17:21:41 -050044 const std::unique_ptr<Node> fPrevious;
45 const int fX, fY;
46 GrRectanizerSkyline fRectanizer;
Chris Dalton1a325d22017-07-14 15:17:41 -060047};
48
Chris Dalton4c458b12018-06-16 17:22:59 -060049GrCCAtlas::GrCCAtlas(GrPixelConfig pixelConfig, const Specs& specs, const GrCaps& caps)
Chris Dalton42c21152018-06-13 15:28:19 -060050 : fMaxTextureSize(SkTMax(SkTMax(specs.fMinHeight, specs.fMinWidth),
51 specs.fMaxPreferredTextureSize)) {
Chris Dalton4c458b12018-06-16 17:22:59 -060052 // 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 Dalton42c21152018-06-13 15:28:19 -060055 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 Dalton2612bae2018-02-22 13:41:37 -070073 fTopNode = skstd::make_unique<Node>(nullptr, 0, 0, fWidth, fHeight);
Chris Dalton4c458b12018-06-16 17:22:59 -060074
75 fTextureProxy = GrProxyProvider::MakeFullyLazyProxy(
76 [this, pixelConfig](GrResourceProvider* resourceProvider) {
77 if (!resourceProvider) {
78 return sk_sp<GrTexture>();
79 }
80 GrSurfaceDesc desc;
81 desc.fFlags = kRenderTarget_GrSurfaceFlag;
82 desc.fWidth = fWidth;
83 desc.fHeight = fHeight;
84 desc.fConfig = pixelConfig;
85 return resourceProvider->createTexture(desc, SkBudgeted::kYes);
86 },
87 GrProxyProvider::Renderable::kYes, kTextureOrigin, pixelConfig, caps);
Chris Dalton1a325d22017-07-14 15:17:41 -060088}
89
Chris Dalton2612bae2018-02-22 13:41:37 -070090GrCCAtlas::~GrCCAtlas() {
91}
Chris Dalton1a325d22017-07-14 15:17:41 -060092
Chris Dalton9414c962018-06-14 10:14:50 -060093bool GrCCAtlas::addRect(const SkIRect& devIBounds, SkIVector* offset) {
Chris Dalton4c458b12018-06-16 17:22:59 -060094 // This can't be called anymore once makeRenderTargetContext() has been called.
95 SkASSERT(!fTextureProxy->priv().isInstantiated());
Chris Dalton1a325d22017-07-14 15:17:41 -060096
Chris Dalton9414c962018-06-14 10:14:50 -060097 SkIPoint16 location;
98 if (!this->internalPlaceRect(devIBounds.width(), devIBounds.height(), &location)) {
Chris Dalton1a325d22017-07-14 15:17:41 -060099 return false;
100 }
Chris Dalton9414c962018-06-14 10:14:50 -0600101 offset->set(location.x() - devIBounds.left(), location.y() - devIBounds.top());
Chris Dalton1a325d22017-07-14 15:17:41 -0600102
Chris Dalton9414c962018-06-14 10:14:50 -0600103 fDrawBounds.fWidth = SkTMax(fDrawBounds.width(), location.x() + devIBounds.width());
104 fDrawBounds.fHeight = SkTMax(fDrawBounds.height(), location.y() + devIBounds.height());
Chris Dalton1a325d22017-07-14 15:17:41 -0600105 return true;
106}
107
Chris Dalton383a2ef2018-01-08 17:21:41 -0500108bool GrCCAtlas::internalPlaceRect(int w, int h, SkIPoint16* loc) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600109 for (Node* node = fTopNode.get(); node; node = node->previous()) {
Chris Dalton42c21152018-06-13 15:28:19 -0600110 if (node->addRect(w, h, loc, fMaxTextureSize)) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600111 return true;
112 }
113 }
114
115 // The rect didn't fit. Grow the atlas and try again.
116 do {
Chris Dalton42c21152018-06-13 15:28:19 -0600117 if (fWidth == fMaxTextureSize && fHeight == fMaxTextureSize) {
Chris Dalton1a325d22017-07-14 15:17:41 -0600118 return false;
119 }
120 if (fHeight <= fWidth) {
121 int top = fHeight;
Chris Dalton42c21152018-06-13 15:28:19 -0600122 fHeight = SkTMin(fHeight * 2, fMaxTextureSize);
Chris Dalton1a325d22017-07-14 15:17:41 -0600123 fTopNode = skstd::make_unique<Node>(std::move(fTopNode), 0, top, fWidth, fHeight);
124 } else {
125 int left = fWidth;
Chris Dalton42c21152018-06-13 15:28:19 -0600126 fWidth = SkTMin(fWidth * 2, fMaxTextureSize);
Chris Dalton1a325d22017-07-14 15:17:41 -0600127 fTopNode = skstd::make_unique<Node>(std::move(fTopNode), left, 0, fWidth, fHeight);
128 }
Chris Dalton42c21152018-06-13 15:28:19 -0600129 } while (!fTopNode->addRect(w, h, loc, fMaxTextureSize));
Chris Dalton1a325d22017-07-14 15:17:41 -0600130
131 return true;
132}
133
Chris Dalton4c458b12018-06-16 17:22:59 -0600134void GrCCAtlas::setUserBatchID(int id) {
135 // This can't be called anymore once makeRenderTargetContext() has been called.
136 SkASSERT(!fTextureProxy->priv().isInstantiated());
137 fUserBatchID = id;
138}
139
140sk_sp<GrRenderTargetContext> GrCCAtlas::makeRenderTargetContext(
141 GrOnFlushResourceProvider* onFlushRP) {
142 SkASSERT(!fTextureProxy->priv().isInstantiated()); // This method should only be called once.
Chris Dalton42c21152018-06-13 15:28:19 -0600143 SkASSERT(fMaxTextureSize <= onFlushRP->caps()->maxRenderTargetSize());
Chris Dalton1a325d22017-07-14 15:17:41 -0600144
Brian Salomon2a4f9832018-03-03 22:43:43 -0500145 sk_sp<GrRenderTargetContext> rtc =
Chris Dalton4c458b12018-06-16 17:22:59 -0600146 onFlushRP->makeRenderTargetContext(fTextureProxy, nullptr, nullptr);
Chris Dalton1a325d22017-07-14 15:17:41 -0600147 if (!rtc) {
148 SkDebugf("WARNING: failed to allocate a %ix%i atlas. Some paths will not be drawn.\n",
149 fWidth, fHeight);
150 return nullptr;
151 }
152
153 SkIRect clearRect = SkIRect::MakeSize(fDrawBounds);
Chris Dalton344e9032017-12-11 15:42:09 -0700154 rtc->clear(&clearRect, 0, GrRenderTargetContext::CanClearFullscreen::kYes);
Chris Dalton1a325d22017-07-14 15:17:41 -0600155 return rtc;
156}
Chris Dalton9414c962018-06-14 10:14:50 -0600157
Chris Dalton4c458b12018-06-16 17:22:59 -0600158GrCCAtlas* GrCCAtlasStack::addRect(const SkIRect& devIBounds, SkIVector* devToAtlasOffset) {
Chris Dalton9414c962018-06-14 10:14:50 -0600159 GrCCAtlas* retiredAtlas = nullptr;
Chris Dalton4c458b12018-06-16 17:22:59 -0600160 if (fAtlases.empty() || !fAtlases.back().addRect(devIBounds, devToAtlasOffset)) {
Chris Dalton9414c962018-06-14 10:14:50 -0600161 // The retired atlas is out of room and can't grow any bigger.
162 retiredAtlas = !fAtlases.empty() ? &fAtlases.back() : nullptr;
Chris Dalton4c458b12018-06-16 17:22:59 -0600163 fAtlases.emplace_back(fPixelConfig, fSpecs, *fCaps);
Chris Dalton9414c962018-06-14 10:14:50 -0600164 SkASSERT(devIBounds.width() <= fSpecs.fMinWidth);
165 SkASSERT(devIBounds.height() <= fSpecs.fMinHeight);
Chris Dalton4c458b12018-06-16 17:22:59 -0600166 SkAssertResult(fAtlases.back().addRect(devIBounds, devToAtlasOffset));
Chris Dalton9414c962018-06-14 10:14:50 -0600167 }
168 return retiredAtlas;
169}