Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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 std::string_view label,
79 const Caps* caps) {
81
85 plotWidth, plotHeight,
86 this,
90 this,
91 label);
93 fKeyLists.resize(fDrawAtlas->numPlots() * fDrawAtlas->maxPages());
94 for (int i = 0; i < fKeyLists.size(); ++i) {
95 fKeyLists[i].reset();
96 }
97}
98
99namespace {
100uint32_t shape_key_list_index(const PlotLocator& locator, const DrawAtlas* drawAtlas) {
101 return locator.pageIndex() * drawAtlas->numPlots() + locator.plotIndex();
102}
103} // namespace
104
106 const Shape& shape,
107 const Transform& transform,
108 const SkStrokeRec& strokeRec,
109 skvx::half2 maskSize,
110 skvx::half2* outPos) {
111 // Shapes must have a key to use this method
112 skgpu::UniqueKey maskKey = GeneratePathMaskKey(shape, transform, strokeRec, maskSize);
113 AtlasLocator* cachedLocator = fShapeCache.find(maskKey);
114 if (cachedLocator) {
115 SkIPoint topLeft = cachedLocator->topLeft();
116 *outPos = skvx::half2(topLeft.x() + kEntryPadding, topLeft.y() + kEntryPadding);
117 fDrawAtlas->setLastUseToken(*cachedLocator,
118 recorder->priv().tokenTracker()->nextFlushToken());
119 return fDrawAtlas->getProxies()[cachedLocator->pageIndex()].get();
120 }
121
122 AtlasLocator locator;
123 const TextureProxy* proxy = this->addToAtlas(recorder, shape, transform, strokeRec,
124 maskSize, outPos, &locator);
125 if (!proxy) {
126 return nullptr;
127 }
128
129 // Add locator to ShapeCache.
130 fShapeCache.set(maskKey, locator);
131 // Add key to Plot's ShapeKeyList.
132 uint32_t index = shape_key_list_index(locator.plotLocator(), fDrawAtlas.get());
133 ShapeKeyEntry* keyEntry = new ShapeKeyEntry();
134 keyEntry->fKey = maskKey;
135 fKeyLists[index].addToTail(keyEntry);
136
137 return proxy;
138}
139
141 const Shape& shape,
142 const Transform& transform,
143 const SkStrokeRec& strokeRec,
144 skvx::half2 maskSize,
145 skvx::half2* outPos,
146 AtlasLocator* locator) {
147 // Render mask.
148 SkIRect iShapeBounds = SkIRect::MakeXYWH(0, 0, maskSize.x(), maskSize.y());
149 // Outset to take padding into account
150 SkIRect iAtlasBounds = iShapeBounds.makeOutset(kEntryPadding, kEntryPadding);
151
152 // Request space in DrawAtlas.
153 DrawAtlas::ErrorCode errorCode = fDrawAtlas->addRect(recorder,
154 iAtlasBounds.width(),
155 iAtlasBounds.height(),
156 locator);
157 if (errorCode != DrawAtlas::ErrorCode::kSucceeded) {
158 return nullptr;
159 }
160 SkIPoint topLeft = locator->topLeft();
161 *outPos = skvx::half2(topLeft.x()+kEntryPadding, topLeft.y()+kEntryPadding);
162
163 // If the mask is empty, just return.
164 // TODO: this may not be needed if we can handle clipped out bounds with inverse fills
165 // another way. See PathAtlas::addShape().
166 if (!all(maskSize)) {
167 fDrawAtlas->setLastUseToken(*locator,
168 recorder->priv().tokenTracker()->nextFlushToken());
169 return fDrawAtlas->getProxies()[locator->pageIndex()].get();
170 }
171
172 if (!this->onAddToAtlas(shape, transform, strokeRec, iShapeBounds, *locator)) {
173 return nullptr;
174 }
175
176 fDrawAtlas->setLastUseToken(*locator,
177 recorder->priv().tokenTracker()->nextFlushToken());
178
179 return fDrawAtlas->getProxies()[locator->pageIndex()].get();
180}
181
183 return fDrawAtlas->recordUploads(dc, recorder);
184}
185
187 // Remove all entries for this Plot from the ShapeCache
188 uint32_t index = shape_key_list_index(plotLocator, fDrawAtlas.get());
190 iter.init(fKeyLists[index], ShapeKeyList::Iter::kHead_IterStart);
191 ShapeKeyEntry* currEntry;
192 while ((currEntry = iter.get())) {
193 iter.next();
194 fShapeCache.remove(currEntry->fKey);
195 fKeyLists[index].remove(currEntry);
196 delete currEntry;
197 }
198}
199
201 fDrawAtlas->compact(recorder->priv().tokenTracker()->nextFlushToken());
202}
203
204} // 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)
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:277
int maxPathAtlasTextureSize() const
Definition Caps.h:275
int maxTextureSize() const
Definition Caps.h:134
static std::unique_ptr< DrawAtlas > Make(SkColorType ct, size_t bpp, int width, int height, int plotWidth, int plotHeight, AtlasGenerationCounter *generationCounter, AllowMultitexturing allowMultitexturing, PlotEvictionCallback *evictor, std::string_view label)
Definition DrawAtlas.cpp:52
bool recordUploads(DrawContext *, Recorder *)
void evict(PlotLocator) override
const TextureProxy * addToAtlas(Recorder *recorder, const Shape &shape, const Transform &transform, const SkStrokeRec &strokeRec, skvx::half2 maskSize, skvx::half2 *outPos, AtlasLocator *locator)
std::unique_ptr< DrawAtlas > fDrawAtlas
Definition PathAtlas.h:144
const TextureProxy * findOrCreateEntry(Recorder *recorder, const Shape &shape, const Transform &transform, const SkStrokeRec &strokeRec, skvx::half2 maskSize, skvx::half2 *outPos)
DrawAtlasMgr(size_t width, size_t height, size_t plotWidth, size_t plotHeight, std::string_view label, const Caps *)
Definition PathAtlas.cpp:76
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:95
static constexpr int kEntryPadding
Definition PathAtlas.h:101
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:94
TokenTracker * tokenTracker()
const Caps * caps() const
const RendererProvider * rendererProvider() const
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
skgpu::UniqueKey GeneratePathMaskKey(const Shape &shape, const Transform &transform, const SkStrokeRec &strokeRec, skvx::half2 maskSize)
Vec< 2, uint16_t > half2
Definition SkVx.h:1175
static SkColor4f transform(SkColor4f c, SkColorSpace *src, SkColorSpace *dst)
Definition p3.cpp:47
int32_t height
int32_t width
constexpr int32_t y() const
constexpr int32_t x() const
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