Flutter Engine
The Flutter Engine
PathAtlas.cpp
Go to the documentation of this file.
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
9
17
18namespace skgpu::graphite {
19namespace {
20
21constexpr int kMinAtlasTextureSize = 512; // the smallest we want the PathAtlas textures to be
22 // unless the device requires smaller
23
24} // namespace
25
26PathAtlas::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
36PathAtlas::~PathAtlas() = default;
37
38std::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
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
77 size_t plotWidth, size_t plotHeight,
78 DrawAtlas::UseStorageTextures useStorageTextures,
79 std::string_view label,
80 const Caps* caps) {
82
86 plotWidth, plotHeight,
87 /*generationCounter=*/this,
91 useStorageTextures,
92 /*evictor=*/this,
93 label);
95 fKeyLists.resize(fDrawAtlas->numPlots() * fDrawAtlas->maxPages());
96 for (int i = 0; i < fKeyLists.size(); ++i) {
97 fKeyLists[i].reset();
98 }
99}
100
101namespace {
102uint32_t shape_key_list_index(const PlotLocator& locator, const DrawAtlas* drawAtlas) {
103 return locator.pageIndex() * drawAtlas->numPlots() + locator.plotIndex();
104}
105} // namespace
106
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
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
185 return fDrawAtlas->recordUploads(dc, recorder);
186}
187
189 // Remove all entries for this Plot from the ShapeCache
190 uint32_t index = shape_key_list_index(plotLocator, fDrawAtlas.get());
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
203 fDrawAtlas->compact(recorder->priv().tokenTracker()->nextFlushToken());
204}
205
206} // namespace skgpu::graphite
#define SkASSERT(cond)
Definition: SkAssert.h:116
SkColorType
Definition: SkColorType.h:19
@ kAlpha_8_SkColorType
pixel with alpha in 8-bit byte
Definition: SkColorType.h:21
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
SK_API int SkColorTypeBytesPerPixel(SkColorType ct)
Definition: SkImageInfo.cpp:16
static int SkPrevPow2(int value)
Definition: SkMathPriv.h:287
T * init(const SkTInternalLList &list, IterStart startLoc)
SkIPoint topLeft() const
Definition: AtlasTypes.h:309
PlotLocator plotLocator() const
Definition: AtlasTypes.h:301
uint32_t pageIndex() const
Definition: AtlasTypes.h:303
uint32_t plotIndex() const
Definition: AtlasTypes.h:279
uint32_t pageIndex() const
Definition: AtlasTypes.h:278
AtlasToken nextFlushToken() const
Definition: AtlasTypes.h:207
bool allowMultipleAtlasTextures() const
Definition: Caps.h:284
int maxPathAtlasTextureSize() const
Definition: Caps.h:282
int maxTextureSize() const
Definition: Caps.h:141
static std::unique_ptr< DrawAtlas > Make(SkColorType ct, size_t bpp, int width, int height, int plotWidth, int plotHeight, AtlasGenerationCounter *generationCounter, AllowMultitexturing allowMultitexturing, UseStorageTextures useStorageTextures, PlotEvictionCallback *evictor, std::string_view label)
Definition: DrawAtlas.cpp:52
bool recordUploads(DrawContext *, Recorder *)
Definition: PathAtlas.cpp:184
void evict(PlotLocator) override
Definition: PathAtlas.cpp:188
const TextureProxy * addToAtlas(Recorder *recorder, const Shape &shape, const Transform &transform, const SkStrokeRec &strokeRec, skvx::half2 maskSize, skvx::half2 *outPos, AtlasLocator *locator)
Definition: PathAtlas.cpp:142
DrawAtlasMgr(size_t width, size_t height, size_t plotWidth, size_t plotHeight, DrawAtlas::UseStorageTextures useStorageTextures, std::string_view label, const Caps *)
Definition: PathAtlas.cpp:76
std::unique_ptr< DrawAtlas > fDrawAtlas
Definition: PathAtlas.h:154
const TextureProxy * findOrCreateEntry(Recorder *recorder, const Shape &shape, const Transform &transform, const SkStrokeRec &strokeRec, skvx::half2 maskSize, skvx::half2 *outPos)
Definition: PathAtlas.cpp:107
virtual const TextureProxy * onAddShape(const Shape &, const Transform &transform, const SkStrokeRec &, skvx::half2 maskSize, skvx::half2 *outPos)=0
uint32_t height() const
Definition: PathAtlas.h:109
static constexpr int kEntryPadding
Definition: PathAtlas.h:55
std::pair< const Renderer *, std::optional< MaskAndOrigin > > addShape(const Rect &transformedShapeBounds, const Shape &shape, const Transform &localToDevice, const SkStrokeRec &style)
Definition: PathAtlas.cpp:38
PathAtlas(Recorder *recorder, uint32_t requestedWidth, uint32_t requestedHeight)
Definition: PathAtlas.cpp:26
uint32_t width() const
Definition: PathAtlas.h:108
TokenTracker * tokenTracker()
Definition: RecorderPriv.h:62
const Caps * caps() const
Definition: RecorderPriv.h:31
const RendererProvider * rendererProvider() const
Definition: RecorderPriv.h:48
AI float top() const
Definition: Rect.h:77
AI bool isEmptyNegativeOrNaN() const
Definition: Rect.h:102
AI float left() const
Definition: Rect.h:76
AI float2 size() const
Definition: Rect.h:107
AI Rect makeRoundOut() const
Definition: Rect.h:133
const Renderer * coverageMask() const
Transform postTranslate(float x, float y) const
const SkM44 & inverse() const
static float max(float r, float g, float b)
Definition: hsl.cpp:49
static float min(float r, float g, float b)
Definition: hsl.cpp:48
drawAtlas(r.atlas.get(), r.xforms, r.texs, r.colors, r.count, r.mode, r.sampling, r.cull, r.paint)) DRAW(DrawAnnotation
skgpu::UniqueKey GeneratePathMaskKey(const Shape &shape, const Transform &transform, const SkStrokeRec &strokeRec, skvx::half2 maskSize)
Vec< 2, uint16_t > half2
Definition: SkVx.h:1175
SIT bool all(const Vec< 1, T > &x)
Definition: SkVx.h:582
static SkColor4f transform(SkColor4f c, SkColorSpace *src, SkColorSpace *dst)
Definition: p3.cpp:47
int32_t height
int32_t width
constexpr int32_t y() const
Definition: SkPoint_impl.h:52
constexpr int32_t x() const
Definition: SkPoint_impl.h:46
Definition: SkRect.h:32
SkIRect makeOutset(int32_t dx, int32_t dy) const
Definition: SkRect.h:350
constexpr int32_t height() const
Definition: SkRect.h:165
constexpr int32_t width() const
Definition: SkRect.h:158
static constexpr SkIRect MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h)
Definition: SkRect.h:104
Definition: SkVx.h:83