| 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.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 |
|
| 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 |
|
| 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 |
// We use the origin of the clipped mask bounds relative to the full mask to distinguish |
| 59 |
// between clips of the same size. |
| 60 |
Rect shapeDevBounds = localToDevice.mapRect(shape.bounds()); |
| 61 |
skvx::float2 clippedMaskOrigin = maskBounds.topLeft() - shapeDevBounds.topLeft(); |
| 62 |
SkIVector transformedMaskOffset = SkIVector::Make(maskBounds.topLeft().x(), |
| 63 |
maskBounds.topLeft().y()); |
| 64 |
sk_sp<TextureProxy> atlasProxy = this->onAddShape(shape, |
| 65 |
localToDevice, |
| 66 |
style, |
| 67 |
skvx::cast<uint16_t>(clippedMaskOrigin), |
| 68 |
maskInfo.fMaskSize, |
| 69 |
transformedMaskOffset, |
| 70 |
&maskInfo.fTextureOrigin); |
| 71 |
if (!atlasProxy) { |
| 72 |
return std::make_pair(nullptr, std::nullopt); |
| 73 |
} |
| 74 |
|
| 75 |
std::optional<PathAtlas::MaskAndOrigin> atlasMask = |
| 76 |
std::make_pair(CoverageMaskShape(shape, std::move(atlasProxy), localToDevice.inverse(), |
| 77 |
maskInfo), |
| 78 |
SkIPoint{(int) maskBounds.left(), (int) maskBounds.top()}); |
| 79 |
return std::make_pair(fRecorder->priv().rendererProvider()->coverageMask(), atlasMask); |
| 80 |
} |
| 81 |
|
| 82 |
///////////////////////////////////////////////////////////////////////////////////////// |
| 83 |
|
| 84 |
PathAtlas::DrawAtlasMgr::DrawAtlasMgr(size_t width, size_t height, |
| 85 |
size_t plotWidth, size_t plotHeight, |
| 86 |
DrawAtlas::UseStorageTextures useStorageTextures, |
| 87 |
std::string_view label, |
| 88 |
const Caps* caps) { |
| 89 |
static constexpr SkColorType colorType = kAlpha_8_SkColorType; |
| 90 |
|
| 91 |
fDrawAtlas = DrawAtlas::Make(colorType, |
| 92 |
SkColorTypeBytesPerPixel(colorType), |
| 93 |
width, height, |
| 94 |
plotWidth, plotHeight, |
| 95 |
/*generationCounter=*/this, |
| 96 |
caps->allowMultipleAtlasTextures() ? |
| 97 |
DrawAtlas::AllowMultitexturing::kYes : |
| 98 |
DrawAtlas::AllowMultitexturing::kNo, |
| 99 |
useStorageTextures, |
| 100 |
/*evictor=*/this, |
| 101 |
label); |
| 102 |
SkASSERT(fDrawAtlas); |
| 103 |
fKeyLists.resize(fDrawAtlas->numPlots() * fDrawAtlas->maxPages()); |
| 104 |
for (int i = 0; i < fKeyLists.size(); ++i) { |
| 105 |
fKeyLists[i].reset(); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
sk_sp<TextureProxy> PathAtlas::DrawAtlasMgr::findOrCreateEntry(Recorder* recorder, |
| 110 |
const Shape& shape, |
| 111 |
const Transform& localToDevice, |
| 112 |
const SkStrokeRec& strokeRec, |
| 113 |
skvx::half2 maskOrigin, |
| 114 |
skvx::half2 maskSize, |
| 115 |
SkIVector transformedMaskOffset, |
| 116 |
skvx::half2* outPos) { |
| 117 |
// TODO: pull this out so we don't have to recalculate it for each atlas? |
| 118 |
skgpu::UniqueKey maskKey = GeneratePathMaskKey(shape, localToDevice, strokeRec, |
| 119 |
maskOrigin, maskSize); |
| 120 |
AtlasLocator* cachedLocator = fShapeCache.find(maskKey); |
| 121 |
if (cachedLocator) { |
| 122 |
SkIPoint topLeft = cachedLocator->topLeft(); |
| 123 |
*outPos = skvx::half2(topLeft.x() + kEntryPadding, topLeft.y() + kEntryPadding); |
| 124 |
fDrawAtlas->setLastUseToken(*cachedLocator, |
| 125 |
recorder->priv().tokenTracker()->nextFlushToken()); |
| 126 |
return fDrawAtlas->getProxies()[cachedLocator->pageIndex()]; |
| 127 |
} |
| 128 |
|
| 129 |
AtlasLocator locator; |
| 130 |
sk_sp<TextureProxy> proxy = this->addToAtlas(recorder, shape, localToDevice, strokeRec, |
| 131 |
maskSize, transformedMaskOffset, outPos, &locator); |
| 132 |
if (!proxy) { |
| 133 |
return nullptr; |
| 134 |
} |
| 135 |
|
| 136 |
// Add locator to ShapeCache. |
| 137 |
fShapeCache.set(maskKey, locator); |
| 138 |
// Add key to Plot's ShapeKeyList. |
| 139 |
uint32_t index = fDrawAtlas->getListIndex(locator.plotLocator()); |
| 140 |
ShapeKeyEntry* keyEntry = new ShapeKeyEntry(); |
| 141 |
keyEntry->fKey = maskKey; |
| 142 |
fKeyLists[index].addToTail(keyEntry); |
| 143 |
|
| 144 |
return proxy; |
| 145 |
} |
| 146 |
|
| 147 |
sk_sp<TextureProxy> PathAtlas::DrawAtlasMgr::addToAtlas(Recorder* recorder, |
| 148 |
const Shape& shape, |
| 149 |
const Transform& localToDevice, |
| 150 |
const SkStrokeRec& strokeRec, |
| 151 |
skvx::half2 maskSize, |
| 152 |
SkIVector transformedMaskOffset, |
| 153 |
skvx::half2* outPos, |
| 154 |
AtlasLocator* locator) { |
| 155 |
// Render mask. |
| 156 |
SkIRect iShapeBounds = SkIRect::MakeXYWH(0, 0, maskSize.x(), maskSize.y()); |
| 157 |
// Outset to take padding into account |
| 158 |
SkIRect iAtlasBounds = iShapeBounds.makeOutset(kEntryPadding, kEntryPadding); |
| 159 |
|
| 160 |
// Request space in DrawAtlas. |
| 161 |
DrawAtlas::ErrorCode errorCode = fDrawAtlas->addRect(recorder, |
| 162 |
iAtlasBounds.width(), |
| 163 |
iAtlasBounds.height(), |
| 164 |
locator); |
| 165 |
if (errorCode != DrawAtlas::ErrorCode::kSucceeded) { |
| 166 |
return nullptr; |
| 167 |
} |
| 168 |
SkIPoint topLeft = locator->topLeft(); |
| 169 |
*outPos = skvx::half2(topLeft.x()+kEntryPadding, topLeft.y()+kEntryPadding); |
| 170 |
|
| 171 |
// If the mask is empty, just return. |
| 172 |
// TODO: this may not be needed if we can handle clipped out bounds with inverse fills |
| 173 |
// another way. See PathAtlas::addShape(). |
| 174 |
if (!all(maskSize)) { |
| 175 |
fDrawAtlas->setLastUseToken(*locator, |
| 176 |
recorder->priv().tokenTracker()->nextFlushToken()); |
| 177 |
return fDrawAtlas->getProxies()[locator->pageIndex()]; |
| 178 |
} |
| 179 |
|
| 180 |
if (!this->onAddToAtlas(shape, localToDevice, strokeRec, iShapeBounds, transformedMaskOffset, |
| 181 |
*locator)) { |
| 182 |
return nullptr; |
| 183 |
} |
| 184 |
|
| 185 |
fDrawAtlas->setLastUseToken(*locator, |
| 186 |
recorder->priv().tokenTracker()->nextFlushToken()); |
| 187 |
|
| 188 |
return fDrawAtlas->getProxies()[locator->pageIndex()]; |
| 189 |
} |
| 190 |
|
| 191 |
bool PathAtlas::DrawAtlasMgr::recordUploads(DrawContext* dc, Recorder* recorder) { |
| 192 |
return fDrawAtlas->recordUploads(dc, recorder); |
| 193 |
} |
| 194 |
|
| 195 |
void PathAtlas::DrawAtlasMgr::evict(PlotLocator plotLocator) { |
| 196 |
// Remove all entries for this Plot from the ShapeCache |
| 197 |
uint32_t index = fDrawAtlas->getListIndex(plotLocator); |
| 198 |
ShapeKeyList::Iter iter; |
| 199 |
iter.init(fKeyLists[index], ShapeKeyList::Iter::kHead_IterStart); |
| 200 |
ShapeKeyEntry* currEntry; |
| 201 |
while ((currEntry = iter.get())) { |
| 202 |
iter.next(); |
| 203 |
fShapeCache.remove(currEntry->fKey); |
| 204 |
fKeyLists[index].remove(currEntry); |
| 205 |
delete currEntry; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
void PathAtlas::DrawAtlasMgr::evictAll() { |
| 210 |
fDrawAtlas->evictAllPlots(); |
| 211 |
SkASSERT(fShapeCache.empty()); |
| 212 |
} |
| 213 |
|
| 214 |
void PathAtlas::DrawAtlasMgr::compact(Recorder* recorder) { |
| 215 |
fDrawAtlas->compact(recorder->priv().tokenTracker()->nextFlushToken()); |
| 216 |
} |
| 217 |
|
| 218 |
void PathAtlas::DrawAtlasMgr::freeGpuResources(Recorder* recorder) { |
| 219 |
fDrawAtlas->freeGpuResources(recorder->priv().tokenTracker()->nextFlushToken()); |
| 220 |
} |
| 221 |
|
| 222 |
} // namespace skgpu::graphite |