| 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/RasterPathAtlas.h" |
| 9 |
|
| 10 |
#include "include/core/SkBitmap.h" |
| 11 |
#include "include/core/SkColorSpace.h" |
| 12 |
#include "include/gpu/graphite/Recorder.h" |
| 13 |
#include "src/gpu/graphite/AtlasProvider.h" |
| 14 |
#include "src/gpu/graphite/DrawContext.h" |
| 15 |
#include "src/gpu/graphite/Log.h" |
| 16 |
#include "src/gpu/graphite/ProxyCache.h" |
| 17 |
#include "src/gpu/graphite/RasterPathUtils.h" |
| 18 |
#include "src/gpu/graphite/RecorderPriv.h" |
| 19 |
|
| 20 |
namespace skgpu::graphite { |
| 21 |
|
| 22 |
static constexpr uint32_t kDefaultAtlasDim = 2048; |
| 23 |
|
| 24 |
static constexpr uint32_t kSmallPathPlotWidth = 512; |
| 25 |
static constexpr uint32_t kSmallPathPlotHeight = 256; |
| 26 |
|
| 27 |
static constexpr uint32_t kUncachedAtlasDim = 2048; |
| 28 |
|
| 29 |
RasterPathAtlas::RasterPathAtlas(Recorder* recorder) |
| 30 |
: PathAtlas(recorder, kDefaultAtlasDim, kDefaultAtlasDim) |
| 31 |
, fCachedAtlasMgr(fWidth, fHeight, |
| 32 |
/*plotWidth=*/fWidth/2, /*plotHeight=*/fHeight/2, |
| 33 |
recorder->priv().caps()) |
| 34 |
, fSmallPathAtlasMgr(std::max(fWidth, kSmallPathPlotWidth), |
| 35 |
std::max(fHeight, kSmallPathPlotHeight), |
| 36 |
kSmallPathPlotWidth, kSmallPathPlotHeight, |
| 37 |
recorder->priv().caps()) |
| 38 |
, fUncachedAtlasMgr(kUncachedAtlasDim, kUncachedAtlasDim, |
| 39 |
/*plotWidth=*/kUncachedAtlasDim/2, /*plotHeight=*/kUncachedAtlasDim/2, |
| 40 |
recorder->priv().caps()) { |
| 41 |
SkASSERT(recorder); |
| 42 |
} |
| 43 |
|
| 44 |
void RasterPathAtlas::recordUploads(DrawContext* dc) { |
| 45 |
fCachedAtlasMgr.recordUploads(dc, fRecorder); |
| 46 |
fSmallPathAtlasMgr.recordUploads(dc, fRecorder); |
| 47 |
fUncachedAtlasMgr.recordUploads(dc, fRecorder); |
| 48 |
} |
| 49 |
|
| 50 |
sk_sp<TextureProxy> RasterPathAtlas::onAddShape(const Shape& shape, |
| 51 |
const Transform& localToDevice, |
| 52 |
const SkStrokeRec& strokeRec, |
| 53 |
skvx::half2 maskOrigin, |
| 54 |
skvx::half2 maskSize, |
| 55 |
SkIVector transformedMaskOffset, |
| 56 |
skvx::half2* outPos) { |
| 57 |
sk_sp<TextureProxy> proxy; |
| 58 |
|
| 59 |
if (!shape.isVolatilePath()) { |
| 60 |
constexpr int kMaxSmallPathSize = 162; |
| 61 |
// Try to locate or add to cached DrawAtlas |
| 62 |
if (maskSize.x() <= kMaxSmallPathSize && maskSize.y() <= kMaxSmallPathSize) { |
| 63 |
proxy = fSmallPathAtlasMgr.findOrCreateEntry(fRecorder, |
| 64 |
shape, |
| 65 |
localToDevice, |
| 66 |
strokeRec, |
| 67 |
maskOrigin, |
| 68 |
maskSize, |
| 69 |
transformedMaskOffset, |
| 70 |
outPos); |
| 71 |
} |
| 72 |
if (!proxy) { |
| 73 |
proxy = fCachedAtlasMgr.findOrCreateEntry(fRecorder, |
| 74 |
shape, |
| 75 |
localToDevice, |
| 76 |
strokeRec, |
| 77 |
maskOrigin, |
| 78 |
maskSize, |
| 79 |
transformedMaskOffset, |
| 80 |
outPos); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
// Try to add to uncached DrawAtlas |
| 85 |
if (!proxy) { |
| 86 |
AtlasLocator loc; |
| 87 |
proxy = fUncachedAtlasMgr.addToAtlas(fRecorder, |
| 88 |
shape, |
| 89 |
localToDevice, |
| 90 |
strokeRec, |
| 91 |
maskSize, |
| 92 |
transformedMaskOffset, |
| 93 |
outPos, |
| 94 |
&loc); |
| 95 |
} |
| 96 |
if (proxy) { |
| 97 |
return proxy; |
| 98 |
} |
| 99 |
|
| 100 |
// Failed to add to atlases, try to add to ProxyCache |
| 101 |
skgpu::UniqueKey maskKey = GeneratePathMaskKey(shape, localToDevice, strokeRec, |
| 102 |
maskOrigin, maskSize); |
| 103 |
struct PathDrawContext { |
| 104 |
const Shape& fShape; |
| 105 |
const Transform& fLocalToDevice; |
| 106 |
const SkStrokeRec& fStrokeRec; |
| 107 |
SkIRect fShapeBounds; |
| 108 |
SkIVector fTransformedMaskOffset; |
| 109 |
} context = { shape, localToDevice, strokeRec, |
| 110 |
SkIRect::MakeSize({maskSize.x(), maskSize.y()}).makeOffset(kEntryPadding, |
| 111 |
kEntryPadding), |
| 112 |
transformedMaskOffset }; |
| 113 |
sk_sp<TextureProxy> cachedProxy = fRecorder->priv().proxyCache()->findOrCreateCachedProxy( |
| 114 |
fRecorder, maskKey, &context, |
| 115 |
[](const void* ctx) { |
| 116 |
const PathDrawContext* pdc = static_cast<const PathDrawContext*>(ctx); |
| 117 |
auto [bm, helper] = RasterMaskHelper::Allocate( |
| 118 |
pdc->fShapeBounds.size(), |
| 119 |
-pdc->fTransformedMaskOffset, |
| 120 |
kEntryPadding); |
| 121 |
helper.drawShape(pdc->fShape, pdc->fLocalToDevice, pdc->fStrokeRec); |
| 122 |
bm.setImmutable(); |
| 123 |
return bm; |
| 124 |
}); |
| 125 |
|
| 126 |
*outPos = { kEntryPadding, kEntryPadding }; |
| 127 |
return cachedProxy; |
| 128 |
} |
| 129 |
|
| 130 |
///////////////////////////////////////////////////////////////////////////////////////// |
| 131 |
|
| 132 |
bool RasterPathAtlas::RasterAtlasMgr::onAddToAtlas(const Shape& shape, |
| 133 |
const Transform& localToDevice, |
| 134 |
const SkStrokeRec& strokeRec, |
| 135 |
SkIRect shapeBounds, |
| 136 |
SkIVector transformedMaskOffset, |
| 137 |
const AtlasLocator& locator) { |
| 138 |
SkPixmap pixmap = fDrawAtlas->prepForRender(locator, kEntryPadding); |
| 139 |
|
| 140 |
RasterMaskHelper helper(pixmap, -transformedMaskOffset); |
| 141 |
helper.drawShape(shape, localToDevice, strokeRec); |
| 142 |
|
| 143 |
return true; |
| 144 |
} |
| 145 |
|
| 146 |
} // namespace skgpu::graphite |