xref: /aosp_15_r20/external/skia/src/gpu/graphite/PathAtlas.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2023 Google LLC
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 
8 #include "src/gpu/graphite/PathAtlas.h"
9 
10 #include "include/gpu/graphite/Recorder.h"
11 #include "src/gpu/graphite/Caps.h"
12 #include "src/gpu/graphite/RasterPathUtils.h"
13 #include "src/gpu/graphite/RecorderPriv.h"
14 #include "src/gpu/graphite/RendererProvider.h"
15 #include "src/gpu/graphite/TextureProxy.h"
16 #include "src/gpu/graphite/geom/Transform_graphite.h"
17 
18 namespace skgpu::graphite {
19 namespace {
20 
21 constexpr int kMinAtlasTextureSize = 512;  // the smallest we want the PathAtlas textures to be
22                                            // unless the device requires smaller
23 
24 }  // namespace
25 
PathAtlas(Recorder * recorder,uint32_t requestedWidth,uint32_t requestedHeight)26 PathAtlas::PathAtlas(Recorder* recorder, uint32_t requestedWidth, uint32_t requestedHeight)
27         : fRecorder(recorder) {
28     const Caps* caps = recorder->priv().caps();
29     int maxTextureSize = std::max(caps->maxPathAtlasTextureSize(), kMinAtlasTextureSize);
30     maxTextureSize = std::min(maxTextureSize, caps->maxTextureSize());
31 
32     fWidth = SkPrevPow2(std::min<uint32_t>(requestedWidth, maxTextureSize));
33     fHeight = SkPrevPow2(std::min<uint32_t>(requestedHeight, maxTextureSize));
34 }
35 
36 PathAtlas::~PathAtlas() = default;
37 
addShape(const Rect & transformedShapeBounds,const Shape & shape,const Transform & localToDevice,const SkStrokeRec & style)38 std::pair<const Renderer*, std::optional<PathAtlas::MaskAndOrigin>> PathAtlas::addShape(
39         const Rect& transformedShapeBounds,
40         const Shape& shape,
41         const Transform& localToDevice,
42         const SkStrokeRec& style) {
43     // It is possible for the transformed shape bounds to be fully clipped out while the draw still
44     // produces coverage due to an inverse fill. In this case, don't render any mask;
45     // CoverageMaskShapeRenderStep will automatically handle the simple fill. We'll handle this
46     // by adding an empty mask.
47     // TODO: We could have addShape() handle this fully except we need a valid TextureProxy still.
48     const bool emptyMask = transformedShapeBounds.isEmptyNegativeOrNaN();
49 
50     // Round out the shape bounds to preserve any fractional offset so that it is present in the
51     // translation that we use when deriving the atlas-space transform later.
52     Rect maskBounds = transformedShapeBounds.makeRoundOut();
53 
54     CoverageMaskShape::MaskInfo maskInfo;
55     // This size does *not* include any padding that the atlas may place around the mask. This size
56     // represents the area the shape can actually modify.
57     maskInfo.fMaskSize = emptyMask ? skvx::half2(0) : skvx::cast<uint16_t>(maskBounds.size());
58     Transform atlasTransform = localToDevice.postTranslate(-maskBounds.left(), -maskBounds.top());
59     const TextureProxy* atlasProxy = this->onAddShape(shape,
60                                                       atlasTransform,
61                                                       style,
62                                                       maskInfo.fMaskSize,
63                                                       &maskInfo.fTextureOrigin);
64     if (!atlasProxy) {
65         return std::make_pair(nullptr, std::nullopt);
66     }
67 
68     std::optional<PathAtlas::MaskAndOrigin> atlasMask =
69             std::make_pair(CoverageMaskShape(shape, atlasProxy, localToDevice.inverse(), maskInfo),
70                            SkIPoint{(int) maskBounds.left(), (int) maskBounds.top()});
71     return std::make_pair(fRecorder->priv().rendererProvider()->coverageMask(), atlasMask);
72 }
73 
74 /////////////////////////////////////////////////////////////////////////////////////////
75 
DrawAtlasMgr(size_t width,size_t height,size_t plotWidth,size_t plotHeight,DrawAtlas::UseStorageTextures useStorageTextures,std::string_view label,const Caps * caps)76 PathAtlas::DrawAtlasMgr::DrawAtlasMgr(size_t width, size_t height,
77                                       size_t plotWidth, size_t plotHeight,
78                                       DrawAtlas::UseStorageTextures useStorageTextures,
79                                       std::string_view label,
80                                       const Caps* caps) {
81     static constexpr SkColorType colorType = kAlpha_8_SkColorType;
82 
83     fDrawAtlas = DrawAtlas::Make(colorType,
84                                  SkColorTypeBytesPerPixel(colorType),
85                                  width, height,
86                                  plotWidth, plotHeight,
87                                  /*generationCounter=*/this,
88                                  caps->allowMultipleAtlasTextures() ?
89                                          DrawAtlas::AllowMultitexturing::kYes :
90                                          DrawAtlas::AllowMultitexturing::kNo,
91                                  useStorageTextures,
92                                  /*evictor=*/this,
93                                  label);
94     SkASSERT(fDrawAtlas);
95     fKeyLists.resize(fDrawAtlas->numPlots() * fDrawAtlas->maxPages());
96     for (int i = 0; i < fKeyLists.size(); ++i) {
97         fKeyLists[i].reset();
98     }
99 }
100 
101 namespace {
shape_key_list_index(const PlotLocator & locator,const DrawAtlas * drawAtlas)102 uint32_t shape_key_list_index(const PlotLocator& locator, const DrawAtlas* drawAtlas) {
103     return locator.pageIndex() * drawAtlas->numPlots() + locator.plotIndex();
104 }
105 }  // namespace
106 
findOrCreateEntry(Recorder * recorder,const Shape & shape,const Transform & transform,const SkStrokeRec & strokeRec,skvx::half2 maskSize,skvx::half2 * outPos)107 const TextureProxy* PathAtlas::DrawAtlasMgr::findOrCreateEntry(Recorder* recorder,
108                                                                const Shape& shape,
109                                                                const Transform& transform,
110                                                                const SkStrokeRec& strokeRec,
111                                                                skvx::half2 maskSize,
112                                                                skvx::half2* outPos) {
113     // Shapes must have a key to use this method
114     skgpu::UniqueKey maskKey = GeneratePathMaskKey(shape, transform, strokeRec, maskSize);
115     AtlasLocator* cachedLocator = fShapeCache.find(maskKey);
116     if (cachedLocator) {
117         SkIPoint topLeft = cachedLocator->topLeft();
118         *outPos = skvx::half2(topLeft.x() + kEntryPadding, topLeft.y() + kEntryPadding);
119         fDrawAtlas->setLastUseToken(*cachedLocator,
120                                     recorder->priv().tokenTracker()->nextFlushToken());
121         return fDrawAtlas->getProxies()[cachedLocator->pageIndex()].get();
122     }
123 
124     AtlasLocator locator;
125     const TextureProxy* proxy = this->addToAtlas(recorder, shape, transform, strokeRec,
126                                                  maskSize, outPos, &locator);
127     if (!proxy) {
128         return nullptr;
129     }
130 
131     // Add locator to ShapeCache.
132     fShapeCache.set(maskKey, locator);
133     // Add key to Plot's ShapeKeyList.
134     uint32_t index = shape_key_list_index(locator.plotLocator(), fDrawAtlas.get());
135     ShapeKeyEntry* keyEntry = new ShapeKeyEntry();
136     keyEntry->fKey = maskKey;
137     fKeyLists[index].addToTail(keyEntry);
138 
139     return proxy;
140 }
141 
addToAtlas(Recorder * recorder,const Shape & shape,const Transform & transform,const SkStrokeRec & strokeRec,skvx::half2 maskSize,skvx::half2 * outPos,AtlasLocator * locator)142 const TextureProxy* PathAtlas::DrawAtlasMgr::addToAtlas(Recorder* recorder,
143                                                         const Shape& shape,
144                                                         const Transform& transform,
145                                                         const SkStrokeRec& strokeRec,
146                                                         skvx::half2 maskSize,
147                                                         skvx::half2* outPos,
148                                                         AtlasLocator* locator) {
149     // Render mask.
150     SkIRect iShapeBounds = SkIRect::MakeXYWH(0, 0, maskSize.x(), maskSize.y());
151     // Outset to take padding into account
152     SkIRect iAtlasBounds = iShapeBounds.makeOutset(kEntryPadding, kEntryPadding);
153 
154     // Request space in DrawAtlas.
155     DrawAtlas::ErrorCode errorCode = fDrawAtlas->addRect(recorder,
156                                                          iAtlasBounds.width(),
157                                                          iAtlasBounds.height(),
158                                                          locator);
159     if (errorCode != DrawAtlas::ErrorCode::kSucceeded) {
160         return nullptr;
161     }
162     SkIPoint topLeft = locator->topLeft();
163     *outPos = skvx::half2(topLeft.x()+kEntryPadding, topLeft.y()+kEntryPadding);
164 
165     // If the mask is empty, just return.
166     // TODO: this may not be needed if we can handle clipped out bounds with inverse fills
167     // another way. See PathAtlas::addShape().
168     if (!all(maskSize)) {
169         fDrawAtlas->setLastUseToken(*locator,
170                                     recorder->priv().tokenTracker()->nextFlushToken());
171         return fDrawAtlas->getProxies()[locator->pageIndex()].get();
172     }
173 
174     if (!this->onAddToAtlas(shape, transform, strokeRec, iShapeBounds, *locator)) {
175         return nullptr;
176     }
177 
178     fDrawAtlas->setLastUseToken(*locator,
179                                 recorder->priv().tokenTracker()->nextFlushToken());
180 
181     return fDrawAtlas->getProxies()[locator->pageIndex()].get();
182 }
183 
recordUploads(DrawContext * dc,Recorder * recorder)184 bool PathAtlas::DrawAtlasMgr::recordUploads(DrawContext* dc, Recorder* recorder) {
185     return fDrawAtlas->recordUploads(dc, recorder);
186 }
187 
evict(PlotLocator plotLocator)188 void PathAtlas::DrawAtlasMgr::evict(PlotLocator plotLocator) {
189     // Remove all entries for this Plot from the ShapeCache
190     uint32_t index = shape_key_list_index(plotLocator, fDrawAtlas.get());
191     ShapeKeyList::Iter iter;
192     iter.init(fKeyLists[index], ShapeKeyList::Iter::kHead_IterStart);
193     ShapeKeyEntry* currEntry;
194     while ((currEntry = iter.get())) {
195         iter.next();
196         fShapeCache.remove(currEntry->fKey);
197         fKeyLists[index].remove(currEntry);
198         delete currEntry;
199     }
200 }
201 
evictAll()202 void PathAtlas::DrawAtlasMgr::evictAll() {
203     fDrawAtlas->evictAllPlots();
204     SkASSERT(fShapeCache.empty());
205 }
206 
compact(Recorder * recorder,bool forceCompact)207 void PathAtlas::DrawAtlasMgr::compact(Recorder* recorder, bool forceCompact) {
208     fDrawAtlas->compact(recorder->priv().tokenTracker()->nextFlushToken(), forceCompact);
209 }
210 
211 }  // namespace skgpu::graphite
212